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 // No need to DecRef the attribute itself since this is
2435 // done be wxGridCellWithAttr's destructor!
2436 m_attrs
.RemoveAt(n
);
2444 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2446 size_t count
= m_attrs
.GetCount();
2447 for ( size_t n
= 0; n
< count
; n
++ )
2449 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2450 wxCoord col
= coords
.GetCol();
2451 if ( (size_t)col
>= pos
)
2455 // If rows inserted, include row counter where necessary
2456 coords
.SetCol(col
+ numCols
);
2458 else if (numCols
< 0)
2460 // If rows deleted ...
2461 if ((size_t)col
>= pos
- numCols
)
2463 // ...either decrement row counter (if row still exists)...
2464 coords
.SetCol(col
+ numCols
);
2468 // ...or remove the attribute
2469 // No need to DecRef the attribute itself since this is
2470 // done be wxGridCellWithAttr's destructor!
2471 m_attrs
.RemoveAt(n
);
2479 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2481 size_t count
= m_attrs
.GetCount();
2482 for ( size_t n
= 0; n
< count
; n
++ )
2484 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2485 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2494 // ----------------------------------------------------------------------------
2495 // wxGridRowOrColAttrData
2496 // ----------------------------------------------------------------------------
2498 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2500 size_t count
= m_attrs
.Count();
2501 for ( size_t n
= 0; n
< count
; n
++ )
2503 m_attrs
[n
]->DecRef();
2507 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2509 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2511 int n
= m_rowsOrCols
.Index(rowOrCol
);
2512 if ( n
!= wxNOT_FOUND
)
2514 attr
= m_attrs
[(size_t)n
];
2521 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2523 int i
= m_rowsOrCols
.Index(rowOrCol
);
2524 if ( i
== wxNOT_FOUND
)
2526 // add the attribute
2527 m_rowsOrCols
.Add(rowOrCol
);
2532 size_t n
= (size_t)i
;
2535 // change the attribute
2536 m_attrs
[n
]->DecRef();
2541 // remove this attribute
2542 m_attrs
[n
]->DecRef();
2543 m_rowsOrCols
.RemoveAt(n
);
2544 m_attrs
.RemoveAt(n
);
2549 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2551 size_t count
= m_attrs
.GetCount();
2552 for ( size_t n
= 0; n
< count
; n
++ )
2554 int & rowOrCol
= m_rowsOrCols
[n
];
2555 if ( (size_t)rowOrCol
>= pos
)
2557 if ( numRowsOrCols
> 0 )
2559 // If rows inserted, include row counter where necessary
2560 rowOrCol
+= numRowsOrCols
;
2562 else if ( numRowsOrCols
< 0)
2564 // If rows deleted, either decrement row counter (if row still exists)
2565 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2566 rowOrCol
+= numRowsOrCols
;
2569 m_rowsOrCols
.RemoveAt(n
);
2570 m_attrs
[n
]->DecRef();
2571 m_attrs
.RemoveAt(n
);
2579 // ----------------------------------------------------------------------------
2580 // wxGridCellAttrProvider
2581 // ----------------------------------------------------------------------------
2583 wxGridCellAttrProvider::wxGridCellAttrProvider()
2585 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2588 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2593 void wxGridCellAttrProvider::InitData()
2595 m_data
= new wxGridCellAttrProviderData
;
2598 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2599 wxGridCellAttr::wxAttrKind kind
) const
2601 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2606 case (wxGridCellAttr::Any
):
2607 //Get cached merge attributes.
2608 // Currenlty not used as no cache implemented as not mutiable
2609 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2612 //Basicaly implement old version.
2613 //Also check merge cache, so we don't have to re-merge every time..
2614 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2615 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2616 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2618 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2619 // Two or more are non NULL
2620 attr
= new wxGridCellAttr
;
2621 attr
->SetKind(wxGridCellAttr::Merged
);
2625 attr
->MergeWith(attrcell
);
2629 attr
->MergeWith(attrcol
);
2633 attr
->MergeWith(attrrow
);
2636 //store merge attr if cache implemented
2638 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2642 // one or none is non null return it or null.
2643 if(attrrow
) attr
= attrrow
;
2659 case (wxGridCellAttr::Cell
):
2660 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2662 case (wxGridCellAttr::Col
):
2663 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2665 case (wxGridCellAttr::Row
):
2666 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2670 // (wxGridCellAttr::Default):
2671 // (wxGridCellAttr::Merged):
2678 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2684 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2687 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2692 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2695 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2700 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2703 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2707 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2709 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2713 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2717 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2719 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2723 // ----------------------------------------------------------------------------
2724 // wxGridTypeRegistry
2725 // ----------------------------------------------------------------------------
2727 wxGridTypeRegistry::~wxGridTypeRegistry()
2729 size_t count
= m_typeinfo
.Count();
2730 for ( size_t i
= 0; i
< count
; i
++ )
2731 delete m_typeinfo
[i
];
2735 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2736 wxGridCellRenderer
* renderer
,
2737 wxGridCellEditor
* editor
)
2739 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2741 // is it already registered?
2742 int loc
= FindRegisteredDataType(typeName
);
2743 if ( loc
!= wxNOT_FOUND
)
2745 delete m_typeinfo
[loc
];
2746 m_typeinfo
[loc
] = info
;
2750 m_typeinfo
.Add(info
);
2754 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2756 size_t count
= m_typeinfo
.GetCount();
2757 for ( size_t i
= 0; i
< count
; i
++ )
2759 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2768 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2770 int index
= FindRegisteredDataType(typeName
);
2771 if ( index
== wxNOT_FOUND
)
2773 // check whether this is one of the standard ones, in which case
2774 // register it "on the fly"
2776 if ( typeName
== wxGRID_VALUE_STRING
)
2778 RegisterDataType(wxGRID_VALUE_STRING
,
2779 new wxGridCellStringRenderer
,
2780 new wxGridCellTextEditor
);
2782 #endif // wxUSE_TEXTCTRL
2784 if ( typeName
== wxGRID_VALUE_BOOL
)
2786 RegisterDataType(wxGRID_VALUE_BOOL
,
2787 new wxGridCellBoolRenderer
,
2788 new wxGridCellBoolEditor
);
2790 #endif // wxUSE_CHECKBOX
2792 if ( typeName
== wxGRID_VALUE_NUMBER
)
2794 RegisterDataType(wxGRID_VALUE_NUMBER
,
2795 new wxGridCellNumberRenderer
,
2796 new wxGridCellNumberEditor
);
2798 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2800 RegisterDataType(wxGRID_VALUE_FLOAT
,
2801 new wxGridCellFloatRenderer
,
2802 new wxGridCellFloatEditor
);
2804 #endif // wxUSE_TEXTCTRL
2806 if ( typeName
== wxGRID_VALUE_CHOICE
)
2808 RegisterDataType(wxGRID_VALUE_CHOICE
,
2809 new wxGridCellStringRenderer
,
2810 new wxGridCellChoiceEditor
);
2812 #endif // wxUSE_COMBOBOX
2817 // we get here only if just added the entry for this type, so return
2819 index
= m_typeinfo
.GetCount() - 1;
2825 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2827 int index
= FindDataType(typeName
);
2828 if ( index
== wxNOT_FOUND
)
2830 // the first part of the typename is the "real" type, anything after ':'
2831 // are the parameters for the renderer
2832 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2833 if ( index
== wxNOT_FOUND
)
2838 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2839 wxGridCellRenderer
*rendererOld
= renderer
;
2840 renderer
= renderer
->Clone();
2841 rendererOld
->DecRef();
2843 wxGridCellEditor
*editor
= GetEditor(index
);
2844 wxGridCellEditor
*editorOld
= editor
;
2845 editor
= editor
->Clone();
2846 editorOld
->DecRef();
2848 // do it even if there are no parameters to reset them to defaults
2849 wxString params
= typeName
.AfterFirst(_T(':'));
2850 renderer
->SetParameters(params
);
2851 editor
->SetParameters(params
);
2853 // register the new typename
2854 RegisterDataType(typeName
, renderer
, editor
);
2856 // we just registered it, it's the last one
2857 index
= m_typeinfo
.GetCount() - 1;
2863 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2865 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2871 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2873 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2879 // ----------------------------------------------------------------------------
2881 // ----------------------------------------------------------------------------
2883 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2886 wxGridTableBase::wxGridTableBase()
2888 m_view
= (wxGrid
*) NULL
;
2889 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2892 wxGridTableBase::~wxGridTableBase()
2894 delete m_attrProvider
;
2897 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2899 delete m_attrProvider
;
2900 m_attrProvider
= attrProvider
;
2903 bool wxGridTableBase::CanHaveAttributes()
2905 if ( ! GetAttrProvider() )
2907 // use the default attr provider by default
2908 SetAttrProvider(new wxGridCellAttrProvider
);
2913 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2915 if ( m_attrProvider
)
2916 return m_attrProvider
->GetAttr(row
, col
, kind
);
2918 return (wxGridCellAttr
*)NULL
;
2921 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2923 if ( m_attrProvider
)
2925 attr
->SetKind(wxGridCellAttr::Cell
);
2926 m_attrProvider
->SetAttr(attr
, row
, col
);
2930 // as we take ownership of the pointer and don't store it, we must
2936 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2938 if ( m_attrProvider
)
2940 attr
->SetKind(wxGridCellAttr::Row
);
2941 m_attrProvider
->SetRowAttr(attr
, row
);
2945 // as we take ownership of the pointer and don't store it, we must
2951 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2953 if ( m_attrProvider
)
2955 attr
->SetKind(wxGridCellAttr::Col
);
2956 m_attrProvider
->SetColAttr(attr
, col
);
2960 // as we take ownership of the pointer and don't store it, we must
2966 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2967 size_t WXUNUSED(numRows
) )
2969 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2974 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2976 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2981 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2982 size_t WXUNUSED(numRows
) )
2984 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2989 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2990 size_t WXUNUSED(numCols
) )
2992 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2997 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2999 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3004 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3005 size_t WXUNUSED(numCols
) )
3007 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3013 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3016 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3017 // how much it makes sense to us geeks.
3021 wxString
wxGridTableBase::GetColLabelValue( int col
)
3023 // default col labels are:
3024 // cols 0 to 25 : A-Z
3025 // cols 26 to 675 : AA-ZZ
3030 for ( n
= 1; ; n
++ )
3032 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3034 if ( col
< 0 ) break;
3037 // reverse the string...
3039 for ( i
= 0; i
< n
; i
++ )
3048 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3050 return wxGRID_VALUE_STRING
;
3053 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3054 const wxString
& typeName
)
3056 return typeName
== wxGRID_VALUE_STRING
;
3059 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3061 return CanGetValueAs(row
, col
, typeName
);
3064 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3069 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3074 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3079 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3080 long WXUNUSED(value
) )
3084 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3085 double WXUNUSED(value
) )
3089 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3090 bool WXUNUSED(value
) )
3095 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3096 const wxString
& WXUNUSED(typeName
) )
3101 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3102 const wxString
& WXUNUSED(typeName
),
3103 void* WXUNUSED(value
) )
3107 //////////////////////////////////////////////////////////////////////
3109 // Message class for the grid table to send requests and notifications
3113 wxGridTableMessage::wxGridTableMessage()
3115 m_table
= (wxGridTableBase
*) NULL
;
3121 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3122 int commandInt1
, int commandInt2
)
3126 m_comInt1
= commandInt1
;
3127 m_comInt2
= commandInt2
;
3132 //////////////////////////////////////////////////////////////////////
3134 // A basic grid table for string data. An object of this class will
3135 // created by wxGrid if you don't specify an alternative table class.
3138 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3140 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3142 wxGridStringTable::wxGridStringTable()
3147 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3150 m_data
.Alloc( numRows
);
3153 sa
.Alloc( numCols
);
3154 sa
.Add( wxEmptyString
, numCols
);
3156 m_data
.Add( sa
, numRows
);
3159 wxGridStringTable::~wxGridStringTable()
3163 int wxGridStringTable::GetNumberRows()
3165 return m_data
.GetCount();
3168 int wxGridStringTable::GetNumberCols()
3170 if ( m_data
.GetCount() > 0 )
3171 return m_data
[0].GetCount();
3176 wxString
wxGridStringTable::GetValue( int row
, int col
)
3178 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3180 _T("invalid row or column index in wxGridStringTable") );
3182 return m_data
[row
][col
];
3185 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3187 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3188 _T("invalid row or column index in wxGridStringTable") );
3190 m_data
[row
][col
] = value
;
3193 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3195 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3197 _T("invalid row or column index in wxGridStringTable") );
3199 return (m_data
[row
][col
] == wxEmptyString
);
3202 void wxGridStringTable::Clear()
3205 int numRows
, numCols
;
3207 numRows
= m_data
.GetCount();
3210 numCols
= m_data
[0].GetCount();
3212 for ( row
= 0; row
< numRows
; row
++ )
3214 for ( col
= 0; col
< numCols
; col
++ )
3216 m_data
[row
][col
] = wxEmptyString
;
3223 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3225 size_t curNumRows
= m_data
.GetCount();
3226 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3227 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3229 if ( pos
>= curNumRows
)
3231 return AppendRows( numRows
);
3235 sa
.Alloc( curNumCols
);
3236 sa
.Add( wxEmptyString
, curNumCols
);
3237 m_data
.Insert( sa
, pos
, numRows
);
3240 wxGridTableMessage
msg( this,
3241 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3245 GetView()->ProcessTableMessage( msg
);
3251 bool wxGridStringTable::AppendRows( size_t numRows
)
3253 size_t curNumRows
= m_data
.GetCount();
3254 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3255 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3258 if ( curNumCols
> 0 )
3260 sa
.Alloc( curNumCols
);
3261 sa
.Add( wxEmptyString
, curNumCols
);
3264 m_data
.Add( sa
, numRows
);
3268 wxGridTableMessage
msg( this,
3269 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3272 GetView()->ProcessTableMessage( msg
);
3278 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3280 size_t curNumRows
= m_data
.GetCount();
3282 if ( pos
>= curNumRows
)
3284 wxFAIL_MSG( wxString::Format
3286 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3288 (unsigned long)numRows
,
3289 (unsigned long)curNumRows
3295 if ( numRows
> curNumRows
- pos
)
3297 numRows
= curNumRows
- pos
;
3300 if ( numRows
>= curNumRows
)
3306 m_data
.RemoveAt( pos
, numRows
);
3310 wxGridTableMessage
msg( this,
3311 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3315 GetView()->ProcessTableMessage( msg
);
3321 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3325 size_t curNumRows
= m_data
.GetCount();
3326 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3327 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3329 if ( pos
>= curNumCols
)
3331 return AppendCols( numCols
);
3334 for ( row
= 0; row
< curNumRows
; row
++ )
3336 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3338 m_data
[row
].Insert( wxEmptyString
, col
);
3343 wxGridTableMessage
msg( this,
3344 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3348 GetView()->ProcessTableMessage( msg
);
3354 bool wxGridStringTable::AppendCols( size_t numCols
)
3358 size_t curNumRows
= m_data
.GetCount();
3362 // TODO: something better than this ?
3364 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3369 for ( row
= 0; row
< curNumRows
; row
++ )
3371 m_data
[row
].Add( wxEmptyString
, numCols
);
3376 wxGridTableMessage
msg( this,
3377 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3380 GetView()->ProcessTableMessage( msg
);
3386 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3390 size_t curNumRows
= m_data
.GetCount();
3391 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3392 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3394 if ( pos
>= curNumCols
)
3396 wxFAIL_MSG( wxString::Format
3398 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3400 (unsigned long)numCols
,
3401 (unsigned long)curNumCols
3406 if ( numCols
> curNumCols
- pos
)
3408 numCols
= curNumCols
- pos
;
3411 for ( row
= 0; row
< curNumRows
; row
++ )
3413 if ( numCols
>= curNumCols
)
3415 m_data
[row
].Clear();
3419 m_data
[row
].RemoveAt( pos
, numCols
);
3424 wxGridTableMessage
msg( this,
3425 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3429 GetView()->ProcessTableMessage( msg
);
3435 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3437 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3439 // using default label
3441 return wxGridTableBase::GetRowLabelValue( row
);
3445 return m_rowLabels
[ row
];
3449 wxString
wxGridStringTable::GetColLabelValue( int col
)
3451 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3453 // using default label
3455 return wxGridTableBase::GetColLabelValue( col
);
3459 return m_colLabels
[ col
];
3463 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3465 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3467 int n
= m_rowLabels
.GetCount();
3469 for ( i
= n
; i
<= row
; i
++ )
3471 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3475 m_rowLabels
[row
] = value
;
3478 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3480 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3482 int n
= m_colLabels
.GetCount();
3484 for ( i
= n
; i
<= col
; i
++ )
3486 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3490 m_colLabels
[col
] = value
;
3495 //////////////////////////////////////////////////////////////////////
3496 //////////////////////////////////////////////////////////////////////
3498 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3500 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3501 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3502 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3503 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3504 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3505 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3508 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3510 const wxPoint
&pos
, const wxSize
&size
)
3511 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3516 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3520 // NO - don't do this because it will set both the x and y origin
3521 // coords to match the parent scrolled window and we just want to
3522 // set the y coord - MB
3524 // m_owner->PrepareDC( dc );
3527 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3528 dc
.SetDeviceOrigin( 0, -y
);
3530 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3531 m_owner
->DrawRowLabels( dc
, rows
);
3535 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3537 m_owner
->ProcessRowLabelMouseEvent( event
);
3541 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3543 m_owner
->GetEventHandler()->ProcessEvent(event
);
3547 // This seems to be required for wxMotif otherwise the mouse
3548 // cursor must be in the cell edit control to get key events
3550 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3552 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3555 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3557 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3562 //////////////////////////////////////////////////////////////////////
3564 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3566 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3567 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3568 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3569 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3570 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3571 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3574 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3576 const wxPoint
&pos
, const wxSize
&size
)
3577 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3582 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3586 // NO - don't do this because it will set both the x and y origin
3587 // coords to match the parent scrolled window and we just want to
3588 // set the x coord - MB
3590 // m_owner->PrepareDC( dc );
3593 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3594 dc
.SetDeviceOrigin( -x
, 0 );
3596 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3597 m_owner
->DrawColLabels( dc
, cols
);
3601 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3603 m_owner
->ProcessColLabelMouseEvent( event
);
3606 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3608 m_owner
->GetEventHandler()->ProcessEvent(event
);
3612 // This seems to be required for wxMotif otherwise the mouse
3613 // cursor must be in the cell edit control to get key events
3615 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3617 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3620 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3622 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3627 //////////////////////////////////////////////////////////////////////
3629 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3631 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3632 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3633 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3634 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3635 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3636 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3639 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3641 const wxPoint
&pos
, const wxSize
&size
)
3642 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3647 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3651 int client_height
= 0;
3652 int client_width
= 0;
3653 GetClientSize( &client_width
, &client_height
);
3659 rect
.SetWidth( client_width
- 2 );
3660 rect
.SetHeight( client_height
- 2 );
3662 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3664 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3665 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3666 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3667 dc
.DrawLine( 0, 0, client_width
, 0 );
3668 dc
.DrawLine( 0, 0, 0, client_height
);
3670 dc
.SetPen( *wxWHITE_PEN
);
3671 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3672 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3677 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3679 m_owner
->ProcessCornerLabelMouseEvent( event
);
3683 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3685 m_owner
->GetEventHandler()->ProcessEvent(event
);
3688 // This seems to be required for wxMotif otherwise the mouse
3689 // cursor must be in the cell edit control to get key events
3691 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3693 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3696 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3698 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3703 //////////////////////////////////////////////////////////////////////
3705 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3707 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3708 EVT_PAINT( wxGridWindow::OnPaint
)
3709 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3710 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3711 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3712 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3713 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3714 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3715 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3718 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3719 wxGridRowLabelWindow
*rowLblWin
,
3720 wxGridColLabelWindow
*colLblWin
,
3723 const wxSize
&size
)
3724 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3725 wxT("grid window") )
3729 m_rowLabelWin
= rowLblWin
;
3730 m_colLabelWin
= colLblWin
;
3734 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3736 wxPaintDC
dc( this );
3737 m_owner
->PrepareDC( dc
);
3738 wxRegion reg
= GetUpdateRegion();
3739 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3740 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3741 #if WXGRID_DRAW_LINES
3742 m_owner
->DrawAllGridLines( dc
, reg
);
3744 m_owner
->DrawGridSpace( dc
);
3745 m_owner
->DrawHighlight( dc
, DirtyCells
);
3749 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3751 wxWindow::ScrollWindow( dx
, dy
, rect
);
3752 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3753 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3757 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3759 m_owner
->ProcessGridCellMouseEvent( event
);
3762 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3764 m_owner
->GetEventHandler()->ProcessEvent(event
);
3767 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3768 // cursor must be in the cell edit control to get key events
3770 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3772 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3775 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3777 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3780 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3784 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3786 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3790 //////////////////////////////////////////////////////////////////////
3792 // Internal Helper function for computing row or column from some
3793 // (unscrolled) coordinate value, using either
3794 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3795 // of m_rowBottoms/m_ColRights to speed up the search!
3797 // Internal helper macros for simpler use of that function
3799 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3800 const wxArrayInt
& BorderArray
, int nMax
,
3803 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3804 m_minAcceptableColWidth, \
3805 m_colRights, m_numCols, true)
3806 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3807 m_minAcceptableRowHeight, \
3808 m_rowBottoms, m_numRows, true)
3809 /////////////////////////////////////////////////////////////////////
3811 #if wxUSE_EXTENDED_RTTI
3812 WX_DEFINE_FLAGS( wxGridStyle
)
3814 wxBEGIN_FLAGS( wxGridStyle
)
3815 // new style border flags, we put them first to
3816 // use them for streaming out
3817 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3818 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3819 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3820 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3821 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3822 wxFLAGS_MEMBER(wxBORDER_NONE
)
3824 // old style border flags
3825 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3826 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3827 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3828 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3829 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3830 wxFLAGS_MEMBER(wxBORDER
)
3832 // standard window styles
3833 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3834 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3835 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3836 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3837 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3838 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3839 wxFLAGS_MEMBER(wxVSCROLL
)
3840 wxFLAGS_MEMBER(wxHSCROLL
)
3842 wxEND_FLAGS( wxGridStyle
)
3844 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3846 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3847 wxHIDE_PROPERTY( Children
)
3848 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3849 wxEND_PROPERTIES_TABLE()
3851 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3852 wxEND_HANDLERS_TABLE()
3854 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3857 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3860 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3863 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3864 EVT_PAINT( wxGrid::OnPaint
)
3865 EVT_SIZE( wxGrid::OnSize
)
3866 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3867 EVT_KEY_UP( wxGrid::OnKeyUp
)
3868 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3873 // in order to make sure that a size event is not
3874 // trigerred in a unfinished state
3875 m_cornerLabelWin
= NULL
;
3876 m_rowLabelWin
= NULL
;
3877 m_colLabelWin
= NULL
;
3881 wxGrid::wxGrid( wxWindow
*parent
,
3886 const wxString
& name
)
3887 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3888 m_colMinWidths(GRID_HASH_SIZE
),
3889 m_rowMinHeights(GRID_HASH_SIZE
)
3892 SetBestFittingSize(size
);
3895 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3896 const wxPoint
& pos
, const wxSize
& size
,
3897 long style
, const wxString
& name
)
3899 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3900 style
| wxWANTS_CHARS
, name
))
3903 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3904 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3907 SetBestFittingSize(size
);
3915 // Must do this or ~wxScrollHelper will pop the wrong event handler
3916 SetTargetWindow(this);
3918 wxSafeDecRef(m_defaultCellAttr
);
3920 #ifdef DEBUG_ATTR_CACHE
3921 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3922 wxPrintf(_T("wxGrid attribute cache statistics: "
3923 "total: %u, hits: %u (%u%%)\n"),
3924 total
, gs_nAttrCacheHits
,
3925 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3931 delete m_typeRegistry
;
3937 // ----- internal init and update functions
3940 // NOTE: If using the default visual attributes works everywhere then this can
3941 // be removed as well as the #else cases below.
3942 #define _USE_VISATTR 0
3945 #include "wx/listbox.h"
3948 void wxGrid::Create()
3950 m_created
= false; // set to true by CreateGrid
3952 m_table
= (wxGridTableBase
*) NULL
;
3955 m_cellEditCtrlEnabled
= false;
3957 m_defaultCellAttr
= new wxGridCellAttr();
3959 // Set default cell attributes
3960 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3961 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3962 m_defaultCellAttr
->SetFont(GetFont());
3963 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3964 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3965 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3968 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3969 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3971 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3972 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3975 m_defaultCellAttr
->SetTextColour(
3976 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3977 m_defaultCellAttr
->SetBackgroundColour(
3978 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3983 m_currentCellCoords
= wxGridNoCellCoords
;
3985 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3986 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3988 // create the type registry
3989 m_typeRegistry
= new wxGridTypeRegistry
;
3992 // subwindow components that make up the wxGrid
3993 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3998 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4003 m_colLabelWin
= new wxGridColLabelWindow( this,
4008 m_gridWin
= new wxGridWindow( this,
4015 SetTargetWindow( m_gridWin
);
4018 wxColour gfg
= gva
.colFg
;
4019 wxColour gbg
= gva
.colBg
;
4020 wxColour lfg
= lva
.colFg
;
4021 wxColour lbg
= lva
.colBg
;
4023 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4024 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4025 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4026 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4028 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4029 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4030 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4031 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4032 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4033 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4035 m_gridWin
->SetOwnForegroundColour(gfg
);
4036 m_gridWin
->SetOwnBackgroundColour(gbg
);
4042 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4043 wxGrid::wxGridSelectionModes selmode
)
4045 wxCHECK_MSG( !m_created
,
4047 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4049 m_numRows
= numRows
;
4050 m_numCols
= numCols
;
4052 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4053 m_table
->SetView( this );
4055 m_selection
= new wxGridSelection( this, selmode
);
4064 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4066 wxCHECK_RET( m_created
,
4067 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4069 m_selection
->SetSelectionMode( selmode
);
4072 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4074 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4075 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4077 return m_selection
->GetSelectionMode();
4080 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4081 wxGrid::wxGridSelectionModes selmode
)
4085 // stop all processing
4090 wxGridTableBase
*t
=m_table
;
4103 m_numRows
= table
->GetNumberRows();
4104 m_numCols
= table
->GetNumberCols();
4107 m_table
->SetView( this );
4110 m_selection
= new wxGridSelection( this, selmode
);
4123 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4124 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4126 if ( m_rowLabelWin
)
4128 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4132 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4135 m_labelTextColour
= wxColour( _T("BLACK") );
4138 m_attrCache
.row
= -1;
4139 m_attrCache
.col
= -1;
4140 m_attrCache
.attr
= NULL
;
4142 // TODO: something better than this ?
4144 m_labelFont
= this->GetFont();
4145 m_labelFont
.SetWeight( wxBOLD
);
4147 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4148 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4150 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4151 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4152 m_colLabelTextOrientation
= wxHORIZONTAL
;
4154 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4155 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4157 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4158 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4160 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4161 m_defaultRowHeight
+= 8;
4163 m_defaultRowHeight
+= 4;
4166 m_gridLineColour
= wxColour( 192,192,192 );
4167 m_gridLinesEnabled
= true;
4168 m_cellHighlightColour
= *wxBLACK
;
4169 m_cellHighlightPenWidth
= 2;
4170 m_cellHighlightROPenWidth
= 1;
4172 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4173 m_winCapture
= (wxWindow
*)NULL
;
4174 m_canDragRowSize
= true;
4175 m_canDragColSize
= true;
4176 m_canDragGridSize
= true;
4177 m_canDragCell
= false;
4179 m_dragRowOrCol
= -1;
4180 m_isDragging
= false;
4181 m_startDragPos
= wxDefaultPosition
;
4183 m_waitForSlowClick
= false;
4185 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4186 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4188 m_currentCellCoords
= wxGridNoCellCoords
;
4190 m_selectingTopLeft
= wxGridNoCellCoords
;
4191 m_selectingBottomRight
= wxGridNoCellCoords
;
4192 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4193 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4195 m_editable
= true; // default for whole grid
4197 m_inOnKeyDown
= false;
4203 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4204 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4207 // ----------------------------------------------------------------------------
4208 // the idea is to call these functions only when necessary because they create
4209 // quite big arrays which eat memory mostly unnecessary - in particular, if
4210 // default widths/heights are used for all rows/columns, we may not use these
4213 // with some extra code, it should be possible to only store the
4214 // widths/heights different from default ones but this will be done later...
4215 // ----------------------------------------------------------------------------
4217 void wxGrid::InitRowHeights()
4219 m_rowHeights
.Empty();
4220 m_rowBottoms
.Empty();
4222 m_rowHeights
.Alloc( m_numRows
);
4223 m_rowBottoms
.Alloc( m_numRows
);
4227 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4229 for ( int i
= 0; i
< m_numRows
; i
++ )
4231 rowBottom
+= m_defaultRowHeight
;
4232 m_rowBottoms
.Add( rowBottom
);
4236 void wxGrid::InitColWidths()
4238 m_colWidths
.Empty();
4239 m_colRights
.Empty();
4241 m_colWidths
.Alloc( m_numCols
);
4242 m_colRights
.Alloc( m_numCols
);
4245 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4247 for ( int i
= 0; i
< m_numCols
; i
++ )
4249 colRight
+= m_defaultColWidth
;
4250 m_colRights
.Add( colRight
);
4254 int wxGrid::GetColWidth(int col
) const
4256 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4259 int wxGrid::GetColLeft(int col
) const
4261 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4262 : m_colRights
[col
] - m_colWidths
[col
];
4265 int wxGrid::GetColRight(int col
) const
4267 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4271 int wxGrid::GetRowHeight(int row
) const
4273 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4276 int wxGrid::GetRowTop(int row
) const
4278 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4279 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4282 int wxGrid::GetRowBottom(int row
) const
4284 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4285 : m_rowBottoms
[row
];
4288 void wxGrid::CalcDimensions()
4291 GetClientSize( &cw
, &ch
);
4293 if ( m_rowLabelWin
->IsShown() )
4294 cw
-= m_rowLabelWidth
;
4295 if ( m_colLabelWin
->IsShown() )
4296 ch
-= m_colLabelHeight
;
4299 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4300 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4302 // take into account editor if shown
4303 if( IsCellEditControlShown() )
4306 int r
= m_currentCellCoords
.GetRow();
4307 int c
= m_currentCellCoords
.GetCol();
4308 int x
= GetColLeft(c
);
4309 int y
= GetRowTop(r
);
4311 // how big is the editor
4312 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4313 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4314 editor
->GetControl()->GetSize(&w2
, &h2
);
4317 if( w2
> w
) w
= w2
;
4318 if( h2
> h
) h
= h2
;
4323 // preserve (more or less) the previous position
4325 GetViewStart( &x
, &y
);
4327 // ensure the position is valid for the new scroll ranges
4329 x
= wxMax( w
- 1, 0 );
4331 y
= wxMax( h
- 1, 0 );
4333 // do set scrollbar parameters
4334 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4335 GetScrollX(w
), GetScrollY(h
), x
, y
,
4336 GetBatchCount() != 0);
4338 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4339 // still must reposition the children
4344 void wxGrid::CalcWindowSizes()
4346 // escape if the window is has not been fully created yet
4348 if ( m_cornerLabelWin
== NULL
)
4352 GetClientSize( &cw
, &ch
);
4354 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4355 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4357 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4358 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4360 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4361 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4363 if ( m_gridWin
&& m_gridWin
->IsShown() )
4364 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4368 // this is called when the grid table sends a message to say that it
4369 // has been redimensioned
4371 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4374 bool result
= false;
4376 // Clear the attribute cache as the attribute might refer to a different
4377 // cell than stored in the cache after adding/removing rows/columns.
4379 // By the same reasoning, the editor should be dismissed if columns are
4380 // added or removed. And for consistency, it should IMHO always be
4381 // removed, not only if the cell "underneath" it actually changes.
4382 // For now, I intentionally do not save the editor's content as the
4383 // cell it might want to save that stuff to might no longer exist.
4384 HideCellEditControl();
4386 // if we were using the default widths/heights so far, we must change them
4388 if ( m_colWidths
.IsEmpty() )
4393 if ( m_rowHeights
.IsEmpty() )
4399 switch ( msg
.GetId() )
4401 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4403 size_t pos
= msg
.GetCommandInt();
4404 int numRows
= msg
.GetCommandInt2();
4406 m_numRows
+= numRows
;
4408 if ( !m_rowHeights
.IsEmpty() )
4410 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4411 m_rowBottoms
.Insert( 0, pos
, numRows
);
4414 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4416 for ( i
= pos
; i
< m_numRows
; i
++ )
4418 bottom
+= m_rowHeights
[i
];
4419 m_rowBottoms
[i
] = bottom
;
4422 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4424 // if we have just inserted cols into an empty grid the current
4425 // cell will be undefined...
4427 SetCurrentCell( 0, 0 );
4431 m_selection
->UpdateRows( pos
, numRows
);
4432 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4434 attrProvider
->UpdateAttrRows( pos
, numRows
);
4436 if ( !GetBatchCount() )
4439 m_rowLabelWin
->Refresh();
4445 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4447 int numRows
= msg
.GetCommandInt();
4448 int oldNumRows
= m_numRows
;
4449 m_numRows
+= numRows
;
4451 if ( !m_rowHeights
.IsEmpty() )
4453 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4454 m_rowBottoms
.Add( 0, numRows
);
4457 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4459 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4461 bottom
+= m_rowHeights
[i
];
4462 m_rowBottoms
[i
] = bottom
;
4465 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4467 // if we have just inserted cols into an empty grid the current
4468 // cell will be undefined...
4470 SetCurrentCell( 0, 0 );
4472 if ( !GetBatchCount() )
4475 m_rowLabelWin
->Refresh();
4481 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4483 size_t pos
= msg
.GetCommandInt();
4484 int numRows
= msg
.GetCommandInt2();
4485 m_numRows
-= numRows
;
4487 if ( !m_rowHeights
.IsEmpty() )
4489 m_rowHeights
.RemoveAt( pos
, numRows
);
4490 m_rowBottoms
.RemoveAt( pos
, numRows
);
4493 for ( i
= 0; i
< m_numRows
; i
++ )
4495 h
+= m_rowHeights
[i
];
4496 m_rowBottoms
[i
] = h
;
4501 m_currentCellCoords
= wxGridNoCellCoords
;
4505 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4506 m_currentCellCoords
.Set( 0, 0 );
4510 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4511 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4513 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4514 // ifdef'd out following patch from Paul Gammans
4516 // No need to touch column attributes, unless we
4517 // removed _all_ rows, in this case, we remove
4518 // all column attributes.
4519 // I hate to do this here, but the
4520 // needed data is not available inside UpdateAttrRows.
4521 if ( !GetNumberRows() )
4522 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4525 if ( !GetBatchCount() )
4528 m_rowLabelWin
->Refresh();
4534 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4536 size_t pos
= msg
.GetCommandInt();
4537 int numCols
= msg
.GetCommandInt2();
4538 m_numCols
+= numCols
;
4540 if ( !m_colWidths
.IsEmpty() )
4542 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4543 m_colRights
.Insert( 0, pos
, numCols
);
4546 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4548 for ( i
= pos
; i
< m_numCols
; i
++ )
4550 right
+= m_colWidths
[i
];
4551 m_colRights
[i
] = right
;
4554 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4556 // if we have just inserted cols into an empty grid the current
4557 // cell will be undefined...
4559 SetCurrentCell( 0, 0 );
4563 m_selection
->UpdateCols( pos
, numCols
);
4564 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4566 attrProvider
->UpdateAttrCols( pos
, numCols
);
4567 if ( !GetBatchCount() )
4570 m_colLabelWin
->Refresh();
4577 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4579 int numCols
= msg
.GetCommandInt();
4580 int oldNumCols
= m_numCols
;
4581 m_numCols
+= numCols
;
4582 if ( !m_colWidths
.IsEmpty() )
4584 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4585 m_colRights
.Add( 0, numCols
);
4588 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4590 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4592 right
+= m_colWidths
[i
];
4593 m_colRights
[i
] = right
;
4596 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4598 // if we have just inserted cols into an empty grid the current
4599 // cell will be undefined...
4601 SetCurrentCell( 0, 0 );
4603 if ( !GetBatchCount() )
4606 m_colLabelWin
->Refresh();
4612 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4614 size_t pos
= msg
.GetCommandInt();
4615 int numCols
= msg
.GetCommandInt2();
4616 m_numCols
-= numCols
;
4618 if ( !m_colWidths
.IsEmpty() )
4620 m_colWidths
.RemoveAt( pos
, numCols
);
4621 m_colRights
.RemoveAt( pos
, numCols
);
4624 for ( i
= 0; i
< m_numCols
; i
++ )
4626 w
+= m_colWidths
[i
];
4632 m_currentCellCoords
= wxGridNoCellCoords
;
4636 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4637 m_currentCellCoords
.Set( 0, 0 );
4641 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4642 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4644 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4645 // ifdef'd out following patch from Paul Gammans
4647 // No need to touch row attributes, unless we
4648 // removed _all_ columns, in this case, we remove
4649 // all row attributes.
4650 // I hate to do this here, but the
4651 // needed data is not available inside UpdateAttrCols.
4652 if ( !GetNumberCols() )
4653 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4656 if ( !GetBatchCount() )
4659 m_colLabelWin
->Refresh();
4666 if (result
&& !GetBatchCount() )
4667 m_gridWin
->Refresh();
4672 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4674 wxRegionIterator
iter( reg
);
4677 wxArrayInt rowlabels
;
4684 // TODO: remove this when we can...
4685 // There is a bug in wxMotif that gives garbage update
4686 // rectangles if you jump-scroll a long way by clicking the
4687 // scrollbar with middle button. This is a work-around
4689 #if defined(__WXMOTIF__)
4691 m_gridWin
->GetClientSize( &cw
, &ch
);
4692 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4693 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4696 // logical bounds of update region
4699 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4700 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4702 // find the row labels within these bounds
4705 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4707 if ( GetRowBottom(row
) < top
)
4710 if ( GetRowTop(row
) > bottom
)
4713 rowlabels
.Add( row
);
4723 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4725 wxRegionIterator
iter( reg
);
4728 wxArrayInt colLabels
;
4735 // TODO: remove this when we can...
4736 // There is a bug in wxMotif that gives garbage update
4737 // rectangles if you jump-scroll a long way by clicking the
4738 // scrollbar with middle button. This is a work-around
4740 #if defined(__WXMOTIF__)
4742 m_gridWin
->GetClientSize( &cw
, &ch
);
4743 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4744 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4747 // logical bounds of update region
4750 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4751 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4753 // find the cells within these bounds
4756 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4758 if ( GetColRight(col
) < left
)
4761 if ( GetColLeft(col
) > right
)
4764 colLabels
.Add( col
);
4773 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4775 wxRegionIterator
iter( reg
);
4778 wxGridCellCoordsArray cellsExposed
;
4780 int left
, top
, right
, bottom
;
4785 // TODO: remove this when we can...
4786 // There is a bug in wxMotif that gives garbage update
4787 // rectangles if you jump-scroll a long way by clicking the
4788 // scrollbar with middle button. This is a work-around
4790 #if defined(__WXMOTIF__)
4792 m_gridWin
->GetClientSize( &cw
, &ch
);
4793 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4794 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4795 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4796 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4799 // logical bounds of update region
4801 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4802 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4804 // find the cells within these bounds
4807 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4809 if ( GetRowBottom(row
) <= top
)
4812 if ( GetRowTop(row
) > bottom
)
4815 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4817 if ( GetColRight(col
) <= left
)
4820 if ( GetColLeft(col
) > right
)
4823 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4830 return cellsExposed
;
4834 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4837 wxPoint
pos( event
.GetPosition() );
4838 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4840 if ( event
.Dragging() )
4844 m_isDragging
= true;
4845 m_rowLabelWin
->CaptureMouse();
4848 if ( event
.LeftIsDown() )
4850 switch( m_cursorMode
)
4852 case WXGRID_CURSOR_RESIZE_ROW
:
4854 int cw
, ch
, left
, dummy
;
4855 m_gridWin
->GetClientSize( &cw
, &ch
);
4856 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4858 wxClientDC
dc( m_gridWin
);
4861 GetRowTop(m_dragRowOrCol
) +
4862 GetRowMinimalHeight(m_dragRowOrCol
) );
4863 dc
.SetLogicalFunction(wxINVERT
);
4864 if ( m_dragLastPos
>= 0 )
4866 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4868 dc
.DrawLine( left
, y
, left
+cw
, y
);
4873 case WXGRID_CURSOR_SELECT_ROW
:
4874 if ( (row
= YToRow( y
)) >= 0 )
4878 m_selection
->SelectRow( row
,
4879 event
.ControlDown(),
4886 // default label to suppress warnings about "enumeration value
4887 // 'xxx' not handled in switch
4895 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4900 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4901 m_isDragging
= false;
4904 // ------------ Entering or leaving the window
4906 if ( event
.Entering() || event
.Leaving() )
4908 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4912 // ------------ Left button pressed
4914 else if ( event
.LeftDown() )
4916 // don't send a label click event for a hit on the
4917 // edge of the row label - this is probably the user
4918 // wanting to resize the row
4920 if ( YToEdgeOfRow(y
) < 0 )
4924 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4926 if ( !event
.ShiftDown() && !event
.ControlDown() )
4930 if ( event
.ShiftDown() )
4932 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4935 GetNumberCols() - 1,
4936 event
.ControlDown(),
4943 m_selection
->SelectRow( row
,
4944 event
.ControlDown(),
4951 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4956 // starting to drag-resize a row
4958 if ( CanDragRowSize() )
4959 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4964 // ------------ Left double click
4966 else if (event
.LeftDClick() )
4968 int row
= YToEdgeOfRow(y
);
4973 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4975 // no default action at the moment
4980 // adjust row height depending on label text
4981 AutoSizeRowLabelSize( row
);
4983 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4989 // ------------ Left button released
4991 else if ( event
.LeftUp() )
4993 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4995 DoEndDragResizeRow();
4997 // Note: we are ending the event *after* doing
4998 // default processing in this case
5000 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5003 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5008 // ------------ Right button down
5010 else if ( event
.RightDown() )
5014 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5016 // no default action at the moment
5021 // ------------ Right double click
5023 else if ( event
.RightDClick() )
5027 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5029 // no default action at the moment
5034 // ------------ No buttons down and mouse moving
5036 else if ( event
.Moving() )
5038 m_dragRowOrCol
= YToEdgeOfRow( y
);
5039 if ( m_dragRowOrCol
>= 0 )
5041 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5043 // don't capture the mouse yet
5044 if ( CanDragRowSize() )
5045 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5048 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5050 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5056 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5059 wxPoint
pos( event
.GetPosition() );
5060 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5062 if ( event
.Dragging() )
5066 m_isDragging
= true;
5067 m_colLabelWin
->CaptureMouse();
5070 if ( event
.LeftIsDown() )
5072 switch( m_cursorMode
)
5074 case WXGRID_CURSOR_RESIZE_COL
:
5076 int cw
, ch
, dummy
, top
;
5077 m_gridWin
->GetClientSize( &cw
, &ch
);
5078 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5080 wxClientDC
dc( m_gridWin
);
5083 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5084 GetColMinimalWidth(m_dragRowOrCol
));
5085 dc
.SetLogicalFunction(wxINVERT
);
5086 if ( m_dragLastPos
>= 0 )
5088 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5090 dc
.DrawLine( x
, top
, x
, top
+ch
);
5095 case WXGRID_CURSOR_SELECT_COL
:
5096 if ( (col
= XToCol( x
)) >= 0 )
5100 m_selection
->SelectCol( col
,
5101 event
.ControlDown(),
5108 // default label to suppress warnings about "enumeration value
5109 // 'xxx' not handled in switch
5117 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5122 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5123 m_isDragging
= false;
5126 // ------------ Entering or leaving the window
5128 if ( event
.Entering() || event
.Leaving() )
5130 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5134 // ------------ Left button pressed
5136 else if ( event
.LeftDown() )
5138 // don't send a label click event for a hit on the
5139 // edge of the col label - this is probably the user
5140 // wanting to resize the col
5142 if ( XToEdgeOfCol(x
) < 0 )
5146 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5148 if ( !event
.ShiftDown() && !event
.ControlDown() )
5152 if ( event
.ShiftDown() )
5154 m_selection
->SelectBlock( 0,
5155 m_currentCellCoords
.GetCol(),
5156 GetNumberRows() - 1, col
,
5157 event
.ControlDown(),
5164 m_selection
->SelectCol( col
,
5165 event
.ControlDown(),
5172 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5177 // starting to drag-resize a col
5179 if ( CanDragColSize() )
5180 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5185 // ------------ Left double click
5187 if ( event
.LeftDClick() )
5189 int col
= XToEdgeOfCol(x
);
5194 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5196 // no default action at the moment
5201 // adjust column width depending on label text
5202 AutoSizeColLabelSize( col
);
5204 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5210 // ------------ Left button released
5212 else if ( event
.LeftUp() )
5214 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5216 DoEndDragResizeCol();
5218 // Note: we are ending the event *after* doing
5219 // default processing in this case
5221 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5224 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5229 // ------------ Right button down
5231 else if ( event
.RightDown() )
5235 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5237 // no default action at the moment
5242 // ------------ Right double click
5244 else if ( event
.RightDClick() )
5248 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5250 // no default action at the moment
5255 // ------------ No buttons down and mouse moving
5257 else if ( event
.Moving() )
5259 m_dragRowOrCol
= XToEdgeOfCol( x
);
5260 if ( m_dragRowOrCol
>= 0 )
5262 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5264 // don't capture the cursor yet
5265 if ( CanDragColSize() )
5266 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5269 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5271 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5277 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5279 if ( event
.LeftDown() )
5281 // indicate corner label by having both row and
5284 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5290 else if ( event
.LeftDClick() )
5292 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5295 else if ( event
.RightDown() )
5297 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5299 // no default action at the moment
5303 else if ( event
.RightDClick() )
5305 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5307 // no default action at the moment
5312 void wxGrid::ChangeCursorMode(CursorMode mode
,
5317 static const wxChar
*cursorModes
[] =
5326 wxLogTrace(_T("grid"),
5327 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5328 win
== m_colLabelWin
? _T("colLabelWin")
5329 : win
? _T("rowLabelWin")
5331 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5332 #endif // __WXDEBUG__
5334 if ( mode
== m_cursorMode
&&
5335 win
== m_winCapture
&&
5336 captureMouse
== (m_winCapture
!= NULL
))
5341 // by default use the grid itself
5347 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5348 m_winCapture
= (wxWindow
*)NULL
;
5351 m_cursorMode
= mode
;
5353 switch ( m_cursorMode
)
5355 case WXGRID_CURSOR_RESIZE_ROW
:
5356 win
->SetCursor( m_rowResizeCursor
);
5359 case WXGRID_CURSOR_RESIZE_COL
:
5360 win
->SetCursor( m_colResizeCursor
);
5364 win
->SetCursor( *wxSTANDARD_CURSOR
);
5367 // we need to capture mouse when resizing
5368 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5369 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5371 if ( captureMouse
&& resize
)
5373 win
->CaptureMouse();
5378 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5381 wxPoint
pos( event
.GetPosition() );
5382 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5384 wxGridCellCoords coords
;
5385 XYToCell( x
, y
, coords
);
5387 int cell_rows
, cell_cols
;
5388 bool isFirstDrag
= !m_isDragging
;
5389 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5390 if ((cell_rows
< 0) || (cell_cols
< 0))
5392 coords
.SetRow(coords
.GetRow() + cell_rows
);
5393 coords
.SetCol(coords
.GetCol() + cell_cols
);
5396 if ( event
.Dragging() )
5398 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5400 // Don't start doing anything until the mouse has been drug at
5401 // least 3 pixels in any direction...
5404 if (m_startDragPos
== wxDefaultPosition
)
5406 m_startDragPos
= pos
;
5409 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5413 m_isDragging
= true;
5414 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5416 // Hide the edit control, so it
5417 // won't interfer with drag-shrinking.
5418 if ( IsCellEditControlShown() )
5420 HideCellEditControl();
5421 SaveEditControlValue();
5424 // Have we captured the mouse yet?
5427 m_winCapture
= m_gridWin
;
5428 m_winCapture
->CaptureMouse();
5431 if ( coords
!= wxGridNoCellCoords
)
5433 if ( event
.ControlDown() )
5435 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5436 m_selectingKeyboard
= coords
;
5437 HighlightBlock ( m_selectingKeyboard
, coords
);
5439 else if ( CanDragCell() )
5443 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5444 m_selectingKeyboard
= coords
;
5446 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5454 if ( !IsSelection() )
5456 HighlightBlock( coords
, coords
);
5460 HighlightBlock( m_currentCellCoords
, coords
);
5464 if (! IsVisible(coords
))
5466 MakeCellVisible(coords
);
5467 // TODO: need to introduce a delay or something here. The
5468 // scrolling is way to fast, at least on MSW - also on GTK.
5472 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5474 int cw
, ch
, left
, dummy
;
5475 m_gridWin
->GetClientSize( &cw
, &ch
);
5476 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5478 wxClientDC
dc( m_gridWin
);
5480 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5481 GetRowMinimalHeight(m_dragRowOrCol
) );
5482 dc
.SetLogicalFunction(wxINVERT
);
5483 if ( m_dragLastPos
>= 0 )
5485 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5487 dc
.DrawLine( left
, y
, left
+cw
, y
);
5490 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5492 int cw
, ch
, dummy
, top
;
5493 m_gridWin
->GetClientSize( &cw
, &ch
);
5494 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5496 wxClientDC
dc( m_gridWin
);
5498 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5499 GetColMinimalWidth(m_dragRowOrCol
) );
5500 dc
.SetLogicalFunction(wxINVERT
);
5501 if ( m_dragLastPos
>= 0 )
5503 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5505 dc
.DrawLine( x
, top
, x
, top
+ch
);
5512 m_isDragging
= false;
5513 m_startDragPos
= wxDefaultPosition
;
5515 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5516 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5519 if ( event
.Entering() || event
.Leaving() )
5521 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5522 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5527 // ------------ Left button pressed
5529 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5531 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5536 if ( !event
.ControlDown() )
5538 if ( event
.ShiftDown() )
5542 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5543 m_currentCellCoords
.GetCol(),
5546 event
.ControlDown(),
5552 else if ( XToEdgeOfCol(x
) < 0 &&
5553 YToEdgeOfRow(y
) < 0 )
5555 DisableCellEditControl();
5556 MakeCellVisible( coords
);
5558 if ( event
.ControlDown() )
5562 m_selection
->ToggleCellSelection( coords
.GetRow(),
5564 event
.ControlDown(),
5569 m_selectingTopLeft
= wxGridNoCellCoords
;
5570 m_selectingBottomRight
= wxGridNoCellCoords
;
5571 m_selectingKeyboard
= coords
;
5575 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5576 SetCurrentCell( coords
);
5579 if ( m_selection
->GetSelectionMode() !=
5580 wxGrid::wxGridSelectCells
)
5582 HighlightBlock( coords
, coords
);
5591 // ------------ Left double click
5593 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5595 DisableCellEditControl();
5597 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5599 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5604 // we want double click to select a cell and start editing
5605 // (i.e. to behave in same way as sequence of two slow clicks):
5606 m_waitForSlowClick
= true;
5613 // ------------ Left button released
5615 else if ( event
.LeftUp() )
5617 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5621 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5622 m_winCapture
= NULL
;
5625 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5628 EnableCellEditControl();
5630 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5631 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5632 editor
->StartingClick();
5636 m_waitForSlowClick
= false;
5638 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5639 m_selectingBottomRight
!= wxGridNoCellCoords
)
5643 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5644 m_selectingTopLeft
.GetCol(),
5645 m_selectingBottomRight
.GetRow(),
5646 m_selectingBottomRight
.GetCol(),
5647 event
.ControlDown(),
5653 m_selectingTopLeft
= wxGridNoCellCoords
;
5654 m_selectingBottomRight
= wxGridNoCellCoords
;
5656 // Show the edit control, if it has been hidden for
5658 ShowCellEditControl();
5661 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5663 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5664 DoEndDragResizeRow();
5666 // Note: we are ending the event *after* doing
5667 // default processing in this case
5669 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5671 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5673 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5674 DoEndDragResizeCol();
5676 // Note: we are ending the event *after* doing
5677 // default processing in this case
5679 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5686 // ------------ Right button down
5688 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5690 DisableCellEditControl();
5691 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5696 // no default action at the moment
5701 // ------------ Right double click
5703 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5705 DisableCellEditControl();
5706 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5711 // no default action at the moment
5715 // ------------ Moving and no button action
5717 else if ( event
.Moving() && !event
.IsButton() )
5719 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5721 // out of grid cell area
5722 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5726 int dragRow
= YToEdgeOfRow( y
);
5727 int dragCol
= XToEdgeOfCol( x
);
5729 // Dragging on the corner of a cell to resize in both
5730 // directions is not implemented yet...
5732 if ( dragRow
>= 0 && dragCol
>= 0 )
5734 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5740 m_dragRowOrCol
= dragRow
;
5742 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5744 if ( CanDragRowSize() && CanDragGridSize() )
5745 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5750 m_dragRowOrCol
= dragCol
;
5758 m_dragRowOrCol
= dragCol
;
5760 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5762 if ( CanDragColSize() && CanDragGridSize() )
5763 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5769 // Neither on a row or col edge
5771 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5773 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5779 void wxGrid::DoEndDragResizeRow()
5781 if ( m_dragLastPos
>= 0 )
5783 // erase the last line and resize the row
5785 int cw
, ch
, left
, dummy
;
5786 m_gridWin
->GetClientSize( &cw
, &ch
);
5787 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5789 wxClientDC
dc( m_gridWin
);
5791 dc
.SetLogicalFunction( wxINVERT
);
5792 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5793 HideCellEditControl();
5794 SaveEditControlValue();
5796 int rowTop
= GetRowTop(m_dragRowOrCol
);
5797 SetRowSize( m_dragRowOrCol
,
5798 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5800 if ( !GetBatchCount() )
5802 // Only needed to get the correct rect.y:
5803 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5805 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5806 rect
.width
= m_rowLabelWidth
;
5807 rect
.height
= ch
- rect
.y
;
5808 m_rowLabelWin
->Refresh( true, &rect
);
5810 // if there is a multicell block, paint all of it
5813 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5814 int leftCol
= XToCol(left
);
5815 int rightCol
= internalXToCol(left
+cw
);
5818 for (i
=leftCol
; i
<rightCol
; i
++)
5820 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5821 if (cell_rows
< subtract_rows
)
5822 subtract_rows
= cell_rows
;
5824 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5825 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5826 rect
.height
= ch
- rect
.y
;
5829 m_gridWin
->Refresh( false, &rect
);
5832 ShowCellEditControl();
5837 void wxGrid::DoEndDragResizeCol()
5839 if ( m_dragLastPos
>= 0 )
5841 // erase the last line and resize the col
5843 int cw
, ch
, dummy
, top
;
5844 m_gridWin
->GetClientSize( &cw
, &ch
);
5845 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5847 wxClientDC
dc( m_gridWin
);
5849 dc
.SetLogicalFunction( wxINVERT
);
5850 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5851 HideCellEditControl();
5852 SaveEditControlValue();
5854 int colLeft
= GetColLeft(m_dragRowOrCol
);
5855 SetColSize( m_dragRowOrCol
,
5856 wxMax( m_dragLastPos
- colLeft
,
5857 GetColMinimalWidth(m_dragRowOrCol
) ) );
5859 if ( !GetBatchCount() )
5861 // Only needed to get the correct rect.x:
5862 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5864 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5865 rect
.width
= cw
- rect
.x
;
5866 rect
.height
= m_colLabelHeight
;
5867 m_colLabelWin
->Refresh( true, &rect
);
5869 // if there is a multicell block, paint all of it
5872 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5873 int topRow
= YToRow(top
);
5874 int bottomRow
= internalYToRow(top
+cw
);
5877 for (i
=topRow
; i
<bottomRow
; i
++)
5879 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5880 if (cell_cols
< subtract_cols
)
5881 subtract_cols
= cell_cols
;
5883 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5884 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5885 rect
.width
= cw
- rect
.x
;
5888 m_gridWin
->Refresh( false, &rect
);
5891 ShowCellEditControl();
5898 // ------ interaction with data model
5900 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5902 switch ( msg
.GetId() )
5904 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5905 return GetModelValues();
5907 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5908 return SetModelValues();
5910 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5911 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5912 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5913 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5914 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5915 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5916 return Redimension( msg
);
5925 // The behaviour of this function depends on the grid table class
5926 // Clear() function. For the default wxGridStringTable class the
5927 // behavious is to replace all cell contents with wxEmptyString but
5928 // not to change the number of rows or cols.
5930 void wxGrid::ClearGrid()
5934 if (IsCellEditControlEnabled())
5935 DisableCellEditControl();
5938 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5943 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5945 // TODO: something with updateLabels flag
5949 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5955 if (IsCellEditControlEnabled())
5956 DisableCellEditControl();
5958 bool done
= m_table
->InsertRows( pos
, numRows
);
5961 // the table will have sent the results of the insert row
5962 // operation to this view object as a grid table message
5968 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5970 // TODO: something with updateLabels flag
5974 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5980 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5982 // the table will have sent the results of the append row
5983 // operation to this view object as a grid table message
5989 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5991 // TODO: something with updateLabels flag
5995 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6001 if (IsCellEditControlEnabled())
6002 DisableCellEditControl();
6004 bool done
= m_table
->DeleteRows( pos
, numRows
);
6006 // the table will have sent the results of the delete row
6007 // operation to this view object as a grid table message
6013 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6015 // TODO: something with updateLabels flag
6019 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6025 if (IsCellEditControlEnabled())
6026 DisableCellEditControl();
6028 bool done
= m_table
->InsertCols( pos
, numCols
);
6030 // the table will have sent the results of the insert col
6031 // operation to this view object as a grid table message
6037 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6039 // TODO: something with updateLabels flag
6043 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6049 bool done
= m_table
->AppendCols( numCols
);
6051 // the table will have sent the results of the append col
6052 // operation to this view object as a grid table message
6058 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6060 // TODO: something with updateLabels flag
6064 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6070 if (IsCellEditControlEnabled())
6071 DisableCellEditControl();
6073 bool done
= m_table
->DeleteCols( pos
, numCols
);
6075 // the table will have sent the results of the delete col
6076 // operation to this view object as a grid table message
6084 // ----- event handlers
6087 // Generate a grid event based on a mouse event and
6088 // return the result of ProcessEvent()
6090 int wxGrid::SendEvent( const wxEventType type
,
6092 wxMouseEvent
& mouseEv
)
6097 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6099 int rowOrCol
= (row
== -1 ? col
: row
);
6101 wxGridSizeEvent
gridEvt( GetId(),
6105 mouseEv
.GetX() + GetRowLabelSize(),
6106 mouseEv
.GetY() + GetColLabelSize(),
6107 mouseEv
.ControlDown(),
6108 mouseEv
.ShiftDown(),
6110 mouseEv
.MetaDown() );
6112 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6113 vetoed
= !gridEvt
.IsAllowed();
6115 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6117 // Right now, it should _never_ end up here!
6118 wxGridRangeSelectEvent
gridEvt( GetId(),
6122 m_selectingBottomRight
,
6124 mouseEv
.ControlDown(),
6125 mouseEv
.ShiftDown(),
6127 mouseEv
.MetaDown() );
6129 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6130 vetoed
= !gridEvt
.IsAllowed();
6134 wxGridEvent
gridEvt( GetId(),
6138 mouseEv
.GetX() + GetRowLabelSize(),
6139 mouseEv
.GetY() + GetColLabelSize(),
6141 mouseEv
.ControlDown(),
6142 mouseEv
.ShiftDown(),
6144 mouseEv
.MetaDown() );
6145 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6146 vetoed
= !gridEvt
.IsAllowed();
6149 // A Veto'd event may not be `claimed' so test this first
6150 if (vetoed
) return -1;
6151 return claimed
? 1 : 0;
6155 // Generate a grid event of specified type and return the result
6156 // of ProcessEvent().
6158 int wxGrid::SendEvent( const wxEventType type
,
6164 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6166 int rowOrCol
= (row
== -1 ? col
: row
);
6168 wxGridSizeEvent
gridEvt( GetId(),
6173 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6174 vetoed
= !gridEvt
.IsAllowed();
6178 wxGridEvent
gridEvt( GetId(),
6183 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6184 vetoed
= !gridEvt
.IsAllowed();
6187 // A Veto'd event may not be `claimed' so test this first
6188 if (vetoed
) return -1;
6189 return claimed
? 1 : 0;
6193 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6195 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6198 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6200 // Don't do anything if between Begin/EndBatch...
6201 // EndBatch() will do all this on the last nested one anyway.
6202 if (! GetBatchCount())
6204 // Refresh to get correct scrolled position:
6205 wxScrolledWindow::Refresh(eraseb
,rect
);
6209 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6210 int width_label
, width_cell
, height_label
, height_cell
;
6213 //Copy rectangle can get scroll offsets..
6214 rect_x
= rect
->GetX();
6215 rect_y
= rect
->GetY();
6216 rectWidth
= rect
->GetWidth();
6217 rectHeight
= rect
->GetHeight();
6219 width_label
= m_rowLabelWidth
- rect_x
;
6220 if (width_label
> rectWidth
) width_label
= rectWidth
;
6222 height_label
= m_colLabelHeight
- rect_y
;
6223 if (height_label
> rectHeight
) height_label
= rectHeight
;
6225 if (rect_x
> m_rowLabelWidth
)
6227 x
= rect_x
- m_rowLabelWidth
;
6228 width_cell
= rectWidth
;
6233 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6236 if (rect_y
> m_colLabelHeight
)
6238 y
= rect_y
- m_colLabelHeight
;
6239 height_cell
= rectHeight
;
6244 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6247 // Paint corner label part intersecting rect.
6248 if ( width_label
> 0 && height_label
> 0 )
6250 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6251 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6254 // Paint col labels part intersecting rect.
6255 if ( width_cell
> 0 && height_label
> 0 )
6257 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6258 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6261 // Paint row labels part intersecting rect.
6262 if ( width_label
> 0 && height_cell
> 0 )
6264 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6265 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6268 // Paint cell area part intersecting rect.
6269 if ( width_cell
> 0 && height_cell
> 0 )
6271 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6272 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6277 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6278 m_colLabelWin
->Refresh(eraseb
, NULL
);
6279 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6280 m_gridWin
->Refresh(eraseb
, NULL
);
6285 void wxGrid::OnSize( wxSizeEvent
& event
)
6287 // position the child windows
6290 // don't call CalcDimensions() from here, the base class handles the size
6296 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6298 if ( m_inOnKeyDown
)
6300 // shouldn't be here - we are going round in circles...
6302 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6305 m_inOnKeyDown
= true;
6307 // propagate the event up and see if it gets processed
6309 wxWindow
*parent
= GetParent();
6310 wxKeyEvent
keyEvt( event
);
6311 keyEvt
.SetEventObject( parent
);
6313 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6316 // try local handlers
6318 switch ( event
.GetKeyCode() )
6321 if ( event
.ControlDown() )
6323 MoveCursorUpBlock( event
.ShiftDown() );
6327 MoveCursorUp( event
.ShiftDown() );
6332 if ( event
.ControlDown() )
6334 MoveCursorDownBlock( event
.ShiftDown() );
6338 MoveCursorDown( event
.ShiftDown() );
6343 if ( event
.ControlDown() )
6345 MoveCursorLeftBlock( event
.ShiftDown() );
6349 MoveCursorLeft( event
.ShiftDown() );
6354 if ( event
.ControlDown() )
6356 MoveCursorRightBlock( event
.ShiftDown() );
6360 MoveCursorRight( event
.ShiftDown() );
6365 case WXK_NUMPAD_ENTER
:
6366 if ( event
.ControlDown() )
6368 event
.Skip(); // to let the edit control have the return
6372 if ( GetGridCursorRow() < GetNumberRows()-1 )
6374 MoveCursorDown( event
.ShiftDown() );
6378 // at the bottom of a column
6379 DisableCellEditControl();
6389 if (event
.ShiftDown())
6391 if ( GetGridCursorCol() > 0 )
6393 MoveCursorLeft( false );
6398 DisableCellEditControl();
6403 if ( GetGridCursorCol() < GetNumberCols()-1 )
6405 MoveCursorRight( false );
6410 DisableCellEditControl();
6416 if ( event
.ControlDown() )
6418 MakeCellVisible( 0, 0 );
6419 SetCurrentCell( 0, 0 );
6428 if ( event
.ControlDown() )
6430 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6431 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6448 if ( event
.ControlDown() )
6452 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6453 m_currentCellCoords
.GetCol(),
6454 event
.ControlDown(),
6461 if ( !IsEditable() )
6463 MoveCursorRight( false );
6466 // Otherwise fall through to default
6469 // is it possible to edit the current cell at all?
6470 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6472 // yes, now check whether the cells editor accepts the key
6473 int row
= m_currentCellCoords
.GetRow();
6474 int col
= m_currentCellCoords
.GetCol();
6475 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6476 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6478 // <F2> is special and will always start editing, for
6479 // other keys - ask the editor itself
6480 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6481 || editor
->IsAcceptedKey(event
) )
6483 // ensure cell is visble
6484 MakeCellVisible(row
, col
);
6485 EnableCellEditControl();
6487 // a problem can arise if the cell is not completely
6488 // visible (even after calling MakeCellVisible the
6489 // control is not created and calling StartingKey will
6491 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6503 // let others process char events with modifiers or all
6504 // char events for readonly cells
6511 m_inOnKeyDown
= false;
6514 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6516 // try local handlers
6518 if ( event
.GetKeyCode() == WXK_SHIFT
)
6520 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6521 m_selectingBottomRight
!= wxGridNoCellCoords
)
6525 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6526 m_selectingTopLeft
.GetCol(),
6527 m_selectingBottomRight
.GetRow(),
6528 m_selectingBottomRight
.GetCol(),
6529 event
.ControlDown(),
6536 m_selectingTopLeft
= wxGridNoCellCoords
;
6537 m_selectingBottomRight
= wxGridNoCellCoords
;
6538 m_selectingKeyboard
= wxGridNoCellCoords
;
6542 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6546 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6548 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6550 // the event has been intercepted - do nothing
6554 wxClientDC
dc(m_gridWin
);
6557 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6559 DisableCellEditControl();
6561 if ( IsVisible( m_currentCellCoords
, false ) )
6564 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6565 if ( !m_gridLinesEnabled
)
6573 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6575 // Otherwise refresh redraws the highlight!
6576 m_currentCellCoords
= coords
;
6578 DrawGridCellArea(dc
,cells
);
6579 DrawAllGridLines( dc
, r
);
6583 m_currentCellCoords
= coords
;
6585 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6586 DrawCellHighlight(dc
, attr
);
6591 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6594 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6598 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6601 rightCol
= GetNumberCols() - 1;
6603 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6606 bottomRow
= GetNumberRows() - 1;
6610 if ( topRow
> bottomRow
)
6617 if ( leftCol
> rightCol
)
6624 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6625 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6627 // First the case that we selected a completely new area
6628 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6629 m_selectingBottomRight
== wxGridNoCellCoords
)
6632 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6633 wxGridCellCoords ( bottomRow
, rightCol
) );
6634 m_gridWin
->Refresh( false, &rect
);
6636 // Now handle changing an existing selection area.
6637 else if ( m_selectingTopLeft
!= updateTopLeft
||
6638 m_selectingBottomRight
!= updateBottomRight
)
6640 // Compute two optimal update rectangles:
6641 // Either one rectangle is a real subset of the
6642 // other, or they are (almost) disjoint!
6644 bool need_refresh
[4];
6648 need_refresh
[3] = false;
6651 // Store intermediate values
6652 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6653 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6654 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6655 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6657 // Determine the outer/inner coordinates.
6658 if (oldLeft
> leftCol
)
6664 if (oldTop
> topRow
)
6670 if (oldRight
< rightCol
)
6673 oldRight
= rightCol
;
6676 if (oldBottom
< bottomRow
)
6679 oldBottom
= bottomRow
;
6683 // Now, either the stuff marked old is the outer
6684 // rectangle or we don't have a situation where one
6685 // is contained in the other.
6687 if ( oldLeft
< leftCol
)
6689 // Refresh the newly selected or deselected
6690 // area to the left of the old or new selection.
6691 need_refresh
[0] = true;
6692 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6694 wxGridCellCoords ( oldBottom
,
6698 if ( oldTop
< topRow
)
6700 // Refresh the newly selected or deselected
6701 // area above the old or new selection.
6702 need_refresh
[1] = true;
6703 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6705 wxGridCellCoords ( topRow
- 1,
6709 if ( oldRight
> rightCol
)
6711 // Refresh the newly selected or deselected
6712 // area to the right of the old or new selection.
6713 need_refresh
[2] = true;
6714 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6716 wxGridCellCoords ( oldBottom
,
6720 if ( oldBottom
> bottomRow
)
6722 // Refresh the newly selected or deselected
6723 // area below the old or new selection.
6724 need_refresh
[3] = true;
6725 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6727 wxGridCellCoords ( oldBottom
,
6731 // various Refresh() calls
6732 for (i
= 0; i
< 4; i
++ )
6733 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6734 m_gridWin
->Refresh( false, &(rect
[i
]) );
6737 m_selectingTopLeft
= updateTopLeft
;
6738 m_selectingBottomRight
= updateBottomRight
;
6742 // ------ functions to get/send data (see also public functions)
6745 bool wxGrid::GetModelValues()
6747 // Hide the editor, so it won't hide a changed value.
6748 HideCellEditControl();
6752 // all we need to do is repaint the grid
6754 m_gridWin
->Refresh();
6762 bool wxGrid::SetModelValues()
6766 // Disable the editor, so it won't hide a changed value.
6767 // Do we also want to save the current value of the editor first?
6769 DisableCellEditControl();
6773 for ( row
= 0; row
< m_numRows
; row
++ )
6775 for ( col
= 0; col
< m_numCols
; col
++ )
6777 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6789 // Note - this function only draws cells that are in the list of
6790 // exposed cells (usually set from the update region by
6791 // CalcExposedCells)
6793 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6795 if ( !m_numRows
|| !m_numCols
) return;
6797 int i
, numCells
= cells
.GetCount();
6798 int row
, col
, cell_rows
, cell_cols
;
6799 wxGridCellCoordsArray redrawCells
;
6801 for ( i
= numCells
-1; i
>= 0; i
-- )
6803 row
= cells
[i
].GetRow();
6804 col
= cells
[i
].GetCol();
6805 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6807 // If this cell is part of a multicell block, find owner for repaint
6808 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6810 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6811 bool marked
= false;
6812 for ( int j
= 0; j
< numCells
; j
++ )
6814 if ( cell
== cells
[j
] )
6822 int count
= redrawCells
.GetCount();
6823 for (int j
= 0; j
< count
; j
++)
6825 if ( cell
== redrawCells
[j
] )
6831 if (!marked
) redrawCells
.Add( cell
);
6833 continue; // don't bother drawing this cell
6836 // If this cell is empty, find cell to left that might want to overflow
6837 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6839 for ( int l
= 0; l
< cell_rows
; l
++ )
6841 // find a cell in this row to left alreay marked for repaint
6843 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6844 if ((redrawCells
[k
].GetCol() < left
) &&
6845 (redrawCells
[k
].GetRow() == row
))
6846 left
=redrawCells
[k
].GetCol();
6848 if (left
== col
) left
= 0; // oh well
6850 for (int j
= col
-1; j
>= left
; j
--)
6852 if (!m_table
->IsEmptyCell(row
+l
, j
))
6854 if (GetCellOverflow(row
+l
, j
))
6856 wxGridCellCoords
cell(row
+l
, j
);
6857 bool marked
= false;
6859 for (int k
= 0; k
< numCells
; k
++)
6861 if ( cell
== cells
[k
] )
6869 int count
= redrawCells
.GetCount();
6870 for (int k
= 0; k
< count
; k
++)
6872 if ( cell
== redrawCells
[k
] )
6878 if (!marked
) redrawCells
.Add( cell
);
6886 DrawCell( dc
, cells
[i
] );
6889 numCells
= redrawCells
.GetCount();
6891 for ( i
= numCells
- 1; i
>= 0; i
-- )
6893 DrawCell( dc
, redrawCells
[i
] );
6898 void wxGrid::DrawGridSpace( wxDC
& dc
)
6901 m_gridWin
->GetClientSize( &cw
, &ch
);
6904 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6906 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6907 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6909 if ( right
> rightCol
|| bottom
> bottomRow
)
6912 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6914 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6915 dc
.SetPen( *wxTRANSPARENT_PEN
);
6917 if ( right
> rightCol
)
6919 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6922 if ( bottom
> bottomRow
)
6924 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6930 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6932 int row
= coords
.GetRow();
6933 int col
= coords
.GetCol();
6935 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6938 // we draw the cell border ourselves
6939 #if !WXGRID_DRAW_LINES
6940 if ( m_gridLinesEnabled
)
6941 DrawCellBorder( dc
, coords
);
6944 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6946 bool isCurrent
= coords
== m_currentCellCoords
;
6948 wxRect rect
= CellToRect( row
, col
);
6950 // if the editor is shown, we should use it and not the renderer
6951 // Note: However, only if it is really _shown_, i.e. not hidden!
6952 if ( isCurrent
&& IsCellEditControlShown() )
6954 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6955 editor
->PaintBackground(rect
, attr
);
6960 // but all the rest is drawn by the cell renderer and hence may be
6962 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6963 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6970 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6972 int row
= m_currentCellCoords
.GetRow();
6973 int col
= m_currentCellCoords
.GetCol();
6975 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6978 wxRect rect
= CellToRect(row
, col
);
6980 // hmmm... what could we do here to show that the cell is disabled?
6981 // for now, I just draw a thinner border than for the other ones, but
6982 // it doesn't look really good
6984 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6988 // The center of th drawn line is where the position/width/height of
6989 // the rectangle is actually at, (on wxMSW atr least,) so we will
6990 // reduce the size of the rectangle to compensate for the thickness of
6991 // the line. If this is too strange on non wxMSW platforms then
6992 // please #ifdef this appropriately.
6993 rect
.x
+= penWidth
/2;
6994 rect
.y
+= penWidth
/2;
6995 rect
.width
-= penWidth
-1;
6996 rect
.height
-= penWidth
-1;
6999 // Now draw the rectangle
7000 // use the cellHighlightColour if the cell is inside a selection, this
7001 // will ensure the cell is always visible.
7002 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
7003 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7004 dc
.DrawRectangle(rect
);
7008 // VZ: my experiments with 3d borders...
7010 // how to properly set colours for arbitrary bg?
7011 wxCoord x1
= rect
.x
,
7013 x2
= rect
.x
+ rect
.width
-1,
7014 y2
= rect
.y
+ rect
.height
-1;
7016 dc
.SetPen(*wxWHITE_PEN
);
7017 dc
.DrawLine(x1
, y1
, x2
, y1
);
7018 dc
.DrawLine(x1
, y1
, x1
, y2
);
7020 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7021 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7023 dc
.SetPen(*wxBLACK_PEN
);
7024 dc
.DrawLine(x1
, y2
, x2
, y2
);
7025 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7030 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7032 int row
= coords
.GetRow();
7033 int col
= coords
.GetCol();
7034 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7037 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7039 wxRect rect
= CellToRect( row
, col
);
7041 // right hand border
7043 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7044 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7048 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7049 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7052 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7054 // This if block was previously in wxGrid::OnPaint but that doesn't
7055 // seem to get called under wxGTK - MB
7057 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7058 m_numRows
&& m_numCols
)
7060 m_currentCellCoords
.Set(0, 0);
7063 if ( IsCellEditControlShown() )
7065 // don't show highlight when the edit control is shown
7069 // if the active cell was repainted, repaint its highlight too because it
7070 // might have been damaged by the grid lines
7071 size_t count
= cells
.GetCount();
7072 for ( size_t n
= 0; n
< count
; n
++ )
7074 if ( cells
[n
] == m_currentCellCoords
)
7076 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7077 DrawCellHighlight(dc
, attr
);
7085 // TODO: remove this ???
7086 // This is used to redraw all grid lines e.g. when the grid line colour
7089 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7091 #if !WXGRID_DRAW_LINES
7095 if ( !m_gridLinesEnabled
||
7097 !m_numCols
) return;
7099 int top
, bottom
, left
, right
;
7101 #if 0 //#ifndef __WXGTK__
7105 m_gridWin
->GetClientSize(&cw
, &ch
);
7107 // virtual coords of visible area
7109 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7110 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7115 reg
.GetBox(x
, y
, w
, h
);
7116 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7117 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7121 m_gridWin
->GetClientSize(&cw
, &ch
);
7122 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7123 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7126 // avoid drawing grid lines past the last row and col
7128 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7129 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7131 // no gridlines inside multicells, clip them out
7132 int leftCol
= internalXToCol(left
);
7133 int topRow
= internalYToRow(top
);
7134 int rightCol
= internalXToCol(right
);
7135 int bottomRow
= internalYToRow(bottom
);
7136 wxRegion
clippedcells(0, 0, cw
, ch
);
7139 int i
, j
, cell_rows
, cell_cols
;
7142 for (j
=topRow
; j
<bottomRow
; j
++)
7144 for (i
=leftCol
; i
<rightCol
; i
++)
7146 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7147 if ((cell_rows
> 1) || (cell_cols
> 1))
7149 rect
= CellToRect(j
,i
);
7150 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7151 clippedcells
.Subtract(rect
);
7153 else if ((cell_rows
< 0) || (cell_cols
< 0))
7155 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7156 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7157 clippedcells
.Subtract(rect
);
7161 dc
.SetClippingRegion( clippedcells
);
7163 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7165 // horizontal grid lines
7167 // already declared above - int i;
7168 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7170 int bot
= GetRowBottom(i
) - 1;
7179 dc
.DrawLine( left
, bot
, right
, bot
);
7184 // vertical grid lines
7186 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7188 int colRight
= GetColRight(i
) - 1;
7189 if ( colRight
> right
)
7194 if ( colRight
>= left
)
7196 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7199 dc
.DestroyClippingRegion();
7203 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7205 if ( !m_numRows
) return;
7208 size_t numLabels
= rows
.GetCount();
7210 for ( i
= 0; i
< numLabels
; i
++ )
7212 DrawRowLabel( dc
, rows
[i
] );
7217 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7219 if ( GetRowHeight(row
) <= 0 )
7225 rect
.SetY( GetRowTop(row
) + 1 );
7226 rect
.SetWidth( m_rowLabelWidth
- 2 );
7227 rect
.SetHeight( GetRowHeight(row
) - 2 );
7229 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7231 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7233 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7235 int rowTop
= GetRowTop(row
),
7236 rowBottom
= GetRowBottom(row
) - 1;
7238 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7239 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7240 m_rowLabelWidth
-1, rowBottom
);
7242 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7244 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7246 dc
.SetPen( *wxWHITE_PEN
);
7247 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7248 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7250 dc
.SetBackgroundMode( wxTRANSPARENT
);
7251 dc
.SetTextForeground( GetLabelTextColour() );
7252 dc
.SetFont( GetLabelFont() );
7255 GetRowLabelAlignment( &hAlign
, &vAlign
);
7258 rect
.SetY( GetRowTop(row
) + 2 );
7259 rect
.SetWidth( m_rowLabelWidth
- 4 );
7260 rect
.SetHeight( GetRowHeight(row
) - 4 );
7261 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7265 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7267 if ( !m_numCols
) return;
7270 size_t numLabels
= cols
.GetCount();
7272 for ( i
= 0; i
< numLabels
; i
++ )
7274 DrawColLabel( dc
, cols
[i
] );
7279 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7281 if ( GetColWidth(col
) <= 0 )
7284 int colLeft
= GetColLeft(col
);
7288 rect
.SetX( colLeft
+ 1 );
7290 rect
.SetWidth( GetColWidth(col
) - 2 );
7291 rect
.SetHeight( m_colLabelHeight
- 2 );
7293 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7295 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7297 int colRight
= GetColRight(col
) - 1;
7299 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7300 dc
.DrawLine( colRight
, 0,
7301 colRight
, m_colLabelHeight
-1 );
7303 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7305 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7306 colRight
+1, m_colLabelHeight
-1 );
7308 dc
.SetPen( *wxWHITE_PEN
);
7309 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7310 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7312 dc
.SetBackgroundMode( wxTRANSPARENT
);
7313 dc
.SetTextForeground( GetLabelTextColour() );
7314 dc
.SetFont( GetLabelFont() );
7316 int hAlign
, vAlign
, orient
;
7317 GetColLabelAlignment( &hAlign
, &vAlign
);
7318 orient
= GetColLabelTextOrientation();
7320 rect
.SetX( colLeft
+ 2 );
7322 rect
.SetWidth( GetColWidth(col
) - 4 );
7323 rect
.SetHeight( m_colLabelHeight
- 4 );
7324 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7327 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7328 const wxString
& value
,
7332 int textOrientation
)
7334 wxArrayString lines
;
7336 StringToLines( value
, lines
);
7339 //Forward to new API.
7340 DrawTextRectangle( dc
,
7349 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7350 const wxArrayString
& lines
,
7354 int textOrientation
)
7356 long textWidth
, textHeight
;
7357 long lineWidth
, lineHeight
;
7360 dc
.SetClippingRegion( rect
);
7362 nLines
= lines
.GetCount();
7366 float x
= 0.0, y
= 0.0;
7368 if( textOrientation
== wxHORIZONTAL
)
7369 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7371 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7375 case wxALIGN_BOTTOM
:
7376 if( textOrientation
== wxHORIZONTAL
)
7377 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7379 x
= rect
.x
+ rect
.width
- textWidth
;
7382 case wxALIGN_CENTRE
:
7383 if( textOrientation
== wxHORIZONTAL
)
7384 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7386 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7391 if( textOrientation
== wxHORIZONTAL
)
7398 // Align each line of a multi-line label
7399 for( l
= 0; l
< nLines
; l
++ )
7401 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7403 switch( horizAlign
)
7406 if( textOrientation
== wxHORIZONTAL
)
7407 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7409 y
= rect
.y
+ lineWidth
+ 1;
7412 case wxALIGN_CENTRE
:
7413 if( textOrientation
== wxHORIZONTAL
)
7414 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7416 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7421 if( textOrientation
== wxHORIZONTAL
)
7424 y
= rect
.y
+ rect
.height
- 1;
7428 if( textOrientation
== wxHORIZONTAL
)
7430 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7435 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7440 dc
.DestroyClippingRegion();
7444 // Split multi line text up into an array of strings. Any existing
7445 // contents of the string array are preserved.
7447 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7451 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7452 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7454 while ( startPos
< (int)tVal
.Length() )
7456 pos
= tVal
.Mid(startPos
).Find( eol
);
7461 else if ( pos
== 0 )
7463 lines
.Add( wxEmptyString
);
7467 lines
.Add( value
.Mid(startPos
, pos
) );
7471 if ( startPos
< (int)value
.Length() )
7473 lines
.Add( value
.Mid( startPos
) );
7478 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7479 const wxArrayString
& lines
,
7480 long *width
, long *height
)
7487 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7489 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7490 w
= wxMax( w
, lineW
);
7499 // ------ Batch processing.
7501 void wxGrid::EndBatch()
7503 if ( m_batchCount
> 0 )
7506 if ( !m_batchCount
)
7509 m_rowLabelWin
->Refresh();
7510 m_colLabelWin
->Refresh();
7511 m_cornerLabelWin
->Refresh();
7512 m_gridWin
->Refresh();
7517 // Use this, rather than wxWindow::Refresh(), to force an immediate
7518 // repainting of the grid. Has no effect if you are already inside a
7519 // BeginBatch / EndBatch block.
7521 void wxGrid::ForceRefresh()
7529 // ------ Edit control functions
7533 void wxGrid::EnableEditing( bool edit
)
7535 // TODO: improve this ?
7537 if ( edit
!= m_editable
)
7539 if(!edit
) EnableCellEditControl(edit
);
7545 void wxGrid::EnableCellEditControl( bool enable
)
7550 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7551 SetCurrentCell( 0, 0 );
7553 if ( enable
!= m_cellEditCtrlEnabled
)
7557 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7560 // this should be checked by the caller!
7561 wxASSERT_MSG( CanEnableCellControl(),
7562 _T("can't enable editing for this cell!") );
7564 // do it before ShowCellEditControl()
7565 m_cellEditCtrlEnabled
= enable
;
7567 ShowCellEditControl();
7571 //FIXME:add veto support
7572 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7574 HideCellEditControl();
7575 SaveEditControlValue();
7577 // do it after HideCellEditControl()
7578 m_cellEditCtrlEnabled
= enable
;
7583 bool wxGrid::IsCurrentCellReadOnly() const
7586 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7587 bool readonly
= attr
->IsReadOnly();
7593 bool wxGrid::CanEnableCellControl() const
7595 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7596 !IsCurrentCellReadOnly();
7600 bool wxGrid::IsCellEditControlEnabled() const
7602 // the cell edit control might be disable for all cells or just for the
7603 // current one if it's read only
7604 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7607 bool wxGrid::IsCellEditControlShown() const
7609 bool isShown
= false;
7611 if ( m_cellEditCtrlEnabled
)
7613 int row
= m_currentCellCoords
.GetRow();
7614 int col
= m_currentCellCoords
.GetCol();
7615 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7616 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7621 if ( editor
->IsCreated() )
7623 isShown
= editor
->GetControl()->IsShown();
7633 void wxGrid::ShowCellEditControl()
7635 if ( IsCellEditControlEnabled() )
7637 if ( !IsVisible( m_currentCellCoords
) )
7639 m_cellEditCtrlEnabled
= false;
7644 wxRect rect
= CellToRect( m_currentCellCoords
);
7645 int row
= m_currentCellCoords
.GetRow();
7646 int col
= m_currentCellCoords
.GetCol();
7648 // if this is part of a multicell, find owner (topleft)
7649 int cell_rows
, cell_cols
;
7650 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7651 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7655 m_currentCellCoords
.SetRow( row
);
7656 m_currentCellCoords
.SetCol( col
);
7659 // convert to scrolled coords
7661 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7663 // done in PaintBackground()
7665 // erase the highlight and the cell contents because the editor
7666 // might not cover the entire cell
7667 wxClientDC
dc( m_gridWin
);
7669 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7670 dc
.SetPen(*wxTRANSPARENT_PEN
);
7671 dc
.DrawRectangle(rect
);
7674 // cell is shifted by one pixel
7675 // However, don't allow x or y to become negative
7676 // since the SetSize() method interprets that as
7683 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7684 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7685 if ( !editor
->IsCreated() )
7687 editor
->Create(m_gridWin
, wxID_ANY
,
7688 new wxGridCellEditorEvtHandler(this, editor
));
7690 wxGridEditorCreatedEvent
evt(GetId(),
7691 wxEVT_GRID_EDITOR_CREATED
,
7695 editor
->GetControl());
7696 GetEventHandler()->ProcessEvent(evt
);
7700 // resize editor to overflow into righthand cells if allowed
7701 int maxWidth
= rect
.width
;
7702 wxString value
= GetCellValue(row
, col
);
7703 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7706 GetTextExtent(value
, &maxWidth
, &y
,
7707 NULL
, NULL
, &attr
->GetFont());
7708 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7710 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7711 if (rect
.x
+maxWidth
> client_right
)
7712 maxWidth
= client_right
- rect
.x
;
7714 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7716 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7717 // may have changed earlier
7718 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7721 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7722 // looks weird going over a multicell
7723 if (m_table
->IsEmptyCell(row
,i
) &&
7724 (rect
.width
< maxWidth
) && (c_rows
== 1))
7725 rect
.width
+= GetColWidth(i
);
7729 if (rect
.GetRight() > client_right
)
7730 rect
.SetRight(client_right
-1);
7733 editor
->SetCellAttr(attr
);
7734 editor
->SetSize( rect
);
7735 editor
->Show( true, attr
);
7737 // recalc dimensions in case we need to
7738 // expand the scrolled window to account for editor
7741 editor
->BeginEdit(row
, col
, this);
7742 editor
->SetCellAttr(NULL
);
7751 void wxGrid::HideCellEditControl()
7753 if ( IsCellEditControlEnabled() )
7755 int row
= m_currentCellCoords
.GetRow();
7756 int col
= m_currentCellCoords
.GetCol();
7758 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7759 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7760 editor
->Show( false );
7763 m_gridWin
->SetFocus();
7764 // refresh whole row to the right
7765 wxRect
rect( CellToRect(row
, col
) );
7766 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7767 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7768 m_gridWin
->Refresh( false, &rect
);
7773 void wxGrid::SaveEditControlValue()
7775 if ( IsCellEditControlEnabled() )
7777 int row
= m_currentCellCoords
.GetRow();
7778 int col
= m_currentCellCoords
.GetCol();
7780 wxString oldval
= GetCellValue(row
,col
);
7782 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7783 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7784 bool changed
= editor
->EndEdit(row
, col
, this);
7791 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7792 m_currentCellCoords
.GetRow(),
7793 m_currentCellCoords
.GetCol() ) < 0 ) {
7795 // Event has been vetoed, set the data back.
7796 SetCellValue(row
,col
,oldval
);
7804 // ------ Grid location functions
7805 // Note that all of these functions work with the logical coordinates of
7806 // grid cells and labels so you will need to convert from device
7807 // coordinates for mouse events etc.
7810 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7812 int row
= YToRow(y
);
7813 int col
= XToCol(x
);
7815 if ( row
== -1 || col
== -1 )
7817 coords
= wxGridNoCellCoords
;
7821 coords
.Set( row
, col
);
7826 // Internal Helper function for computing row or column from some
7827 // (unscrolled) coordinate value, using either
7828 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7829 // of m_rowBottoms/m_ColRights to speed up the search!
7831 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7832 const wxArrayInt
& BorderArray
, int nMax
,
7837 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7843 size_t i_max
= coord
/ defaultDist
,
7846 if (BorderArray
.IsEmpty())
7848 if((int) i_max
< nMax
)
7850 return clipToMinMax
? nMax
- 1 : -1;
7853 if ( i_max
>= BorderArray
.GetCount())
7854 i_max
= BorderArray
.GetCount() - 1;
7857 if ( coord
>= BorderArray
[i_max
])
7861 i_max
= coord
/ minDist
;
7863 i_max
= BorderArray
.GetCount() - 1;
7865 if ( i_max
>= BorderArray
.GetCount())
7866 i_max
= BorderArray
.GetCount() - 1;
7868 if ( coord
>= BorderArray
[i_max
])
7869 return clipToMinMax
? (int)i_max
: -1;
7870 if ( coord
< BorderArray
[0] )
7873 while ( i_max
- i_min
> 0 )
7875 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7876 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7877 if (coord
>= BorderArray
[ i_max
- 1])
7881 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7882 if (coord
< BorderArray
[median
])
7890 int wxGrid::YToRow( int y
)
7892 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7893 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7897 int wxGrid::XToCol( int x
)
7899 return CoordToRowOrCol(x
, m_defaultColWidth
,
7900 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7904 // return the row number that that the y coord is near the edge of, or
7905 // -1 if not near an edge
7907 int wxGrid::YToEdgeOfRow( int y
)
7910 i
= internalYToRow(y
);
7912 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7914 // We know that we are in row i, test whether we are
7915 // close enough to lower or upper border, respectively.
7916 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7918 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7926 // return the col number that that the x coord is near the edge of, or
7927 // -1 if not near an edge
7929 int wxGrid::XToEdgeOfCol( int x
)
7932 i
= internalXToCol(x
);
7934 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7936 // We know that we are in column i, test whether we are
7937 // close enough to right or left border, respectively.
7938 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7940 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7948 wxRect
wxGrid::CellToRect( int row
, int col
)
7950 wxRect
rect( -1, -1, -1, -1 );
7952 if ( row
>= 0 && row
< m_numRows
&&
7953 col
>= 0 && col
< m_numCols
)
7955 int i
, cell_rows
, cell_cols
;
7956 rect
.width
= rect
.height
= 0;
7957 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7958 // if negative then find multicell owner
7959 if (cell_rows
< 0) row
+= cell_rows
;
7960 if (cell_cols
< 0) col
+= cell_cols
;
7961 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7963 rect
.x
= GetColLeft(col
);
7964 rect
.y
= GetRowTop(row
);
7965 for (i
=col
; i
<col
+cell_cols
; i
++)
7966 rect
.width
+= GetColWidth(i
);
7967 for (i
=row
; i
<row
+cell_rows
; i
++)
7968 rect
.height
+= GetRowHeight(i
);
7971 // if grid lines are enabled, then the area of the cell is a bit smaller
7972 if (m_gridLinesEnabled
) {
7980 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7982 // get the cell rectangle in logical coords
7984 wxRect
r( CellToRect( row
, col
) );
7986 // convert to device coords
7988 int left
, top
, right
, bottom
;
7989 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7990 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7992 // check against the client area of the grid window
7995 m_gridWin
->GetClientSize( &cw
, &ch
);
7997 if ( wholeCellVisible
)
7999 // is the cell wholly visible ?
8001 return ( left
>= 0 && right
<= cw
&&
8002 top
>= 0 && bottom
<= ch
);
8006 // is the cell partly visible ?
8008 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8009 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8014 // make the specified cell location visible by doing a minimal amount
8017 void wxGrid::MakeCellVisible( int row
, int col
)
8021 int xpos
= -1, ypos
= -1;
8023 if ( row
>= 0 && row
< m_numRows
&&
8024 col
>= 0 && col
< m_numCols
)
8026 // get the cell rectangle in logical coords
8028 wxRect
r( CellToRect( row
, col
) );
8030 // convert to device coords
8032 int left
, top
, right
, bottom
;
8033 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8034 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8037 m_gridWin
->GetClientSize( &cw
, &ch
);
8043 else if ( bottom
> ch
)
8045 int h
= r
.GetHeight();
8047 for ( i
= row
-1; i
>= 0; i
-- )
8049 int rowHeight
= GetRowHeight(i
);
8050 if ( h
+ rowHeight
> ch
)
8057 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8058 // have rounding errors (this is important, because if we do, we
8059 // might not scroll at all and some cells won't be redrawn)
8061 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8063 ypos
+= GRID_SCROLL_LINE_Y
;
8070 else if ( right
> cw
)
8072 // position the view so that the cell is on the right
8074 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8075 xpos
= x0
+ (right
- cw
);
8077 // see comment for ypos above
8078 xpos
+= GRID_SCROLL_LINE_X
;
8081 if ( xpos
!= -1 || ypos
!= -1 )
8084 xpos
/= GRID_SCROLL_LINE_X
;
8086 ypos
/= GRID_SCROLL_LINE_Y
;
8087 Scroll( xpos
, ypos
);
8095 // ------ Grid cursor movement functions
8098 bool wxGrid::MoveCursorUp( bool expandSelection
)
8100 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8101 m_currentCellCoords
.GetRow() >= 0 )
8103 if ( expandSelection
)
8105 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8106 m_selectingKeyboard
= m_currentCellCoords
;
8107 if ( m_selectingKeyboard
.GetRow() > 0 )
8109 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8110 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8111 m_selectingKeyboard
.GetCol() );
8112 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8115 else if ( m_currentCellCoords
.GetRow() > 0 )
8118 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8119 m_currentCellCoords
.GetCol() );
8120 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8121 m_currentCellCoords
.GetCol() );
8132 bool wxGrid::MoveCursorDown( bool expandSelection
)
8134 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8135 m_currentCellCoords
.GetRow() < m_numRows
)
8137 if ( expandSelection
)
8139 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8140 m_selectingKeyboard
= m_currentCellCoords
;
8141 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8143 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8144 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8145 m_selectingKeyboard
.GetCol() );
8146 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8149 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8152 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8153 m_currentCellCoords
.GetCol() );
8154 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8155 m_currentCellCoords
.GetCol() );
8166 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8168 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8169 m_currentCellCoords
.GetCol() >= 0 )
8171 if ( expandSelection
)
8173 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8174 m_selectingKeyboard
= m_currentCellCoords
;
8175 if ( m_selectingKeyboard
.GetCol() > 0 )
8177 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8178 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8179 m_selectingKeyboard
.GetCol() );
8180 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8183 else if ( m_currentCellCoords
.GetCol() > 0 )
8186 MakeCellVisible( m_currentCellCoords
.GetRow(),
8187 m_currentCellCoords
.GetCol() - 1 );
8188 SetCurrentCell( m_currentCellCoords
.GetRow(),
8189 m_currentCellCoords
.GetCol() - 1 );
8200 bool wxGrid::MoveCursorRight( bool expandSelection
)
8202 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8203 m_currentCellCoords
.GetCol() < m_numCols
)
8205 if ( expandSelection
)
8207 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8208 m_selectingKeyboard
= m_currentCellCoords
;
8209 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8211 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8212 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8213 m_selectingKeyboard
.GetCol() );
8214 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8217 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8220 MakeCellVisible( m_currentCellCoords
.GetRow(),
8221 m_currentCellCoords
.GetCol() + 1 );
8222 SetCurrentCell( m_currentCellCoords
.GetRow(),
8223 m_currentCellCoords
.GetCol() + 1 );
8234 bool wxGrid::MovePageUp()
8236 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8238 int row
= m_currentCellCoords
.GetRow();
8242 m_gridWin
->GetClientSize( &cw
, &ch
);
8244 int y
= GetRowTop(row
);
8245 int newRow
= internalYToRow( y
- ch
+ 1 );
8247 if ( newRow
== row
)
8249 //row > 0 , so newrow can never be less than 0 here.
8253 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8254 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8262 bool wxGrid::MovePageDown()
8264 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8266 int row
= m_currentCellCoords
.GetRow();
8267 if ( (row
+1) < m_numRows
)
8270 m_gridWin
->GetClientSize( &cw
, &ch
);
8272 int y
= GetRowTop(row
);
8273 int newRow
= internalYToRow( y
+ ch
);
8274 if ( newRow
== row
)
8276 // row < m_numRows , so newrow can't overflow here.
8280 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8281 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8289 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8292 m_currentCellCoords
!= wxGridNoCellCoords
&&
8293 m_currentCellCoords
.GetRow() > 0 )
8295 int row
= m_currentCellCoords
.GetRow();
8296 int col
= m_currentCellCoords
.GetCol();
8298 if ( m_table
->IsEmptyCell(row
, col
) )
8300 // starting in an empty cell: find the next block of
8306 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8309 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8311 // starting at the top of a block: find the next block
8317 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8322 // starting within a block: find the top of the block
8327 if ( m_table
->IsEmptyCell(row
, col
) )
8335 MakeCellVisible( row
, col
);
8336 if ( expandSelection
)
8338 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8339 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8344 SetCurrentCell( row
, col
);
8352 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8355 m_currentCellCoords
!= wxGridNoCellCoords
&&
8356 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8358 int row
= m_currentCellCoords
.GetRow();
8359 int col
= m_currentCellCoords
.GetCol();
8361 if ( m_table
->IsEmptyCell(row
, col
) )
8363 // starting in an empty cell: find the next block of
8366 while ( row
< m_numRows
-1 )
8369 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8372 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8374 // starting at the bottom of a block: find the next block
8377 while ( row
< m_numRows
-1 )
8380 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8385 // starting within a block: find the bottom of the block
8387 while ( row
< m_numRows
-1 )
8390 if ( m_table
->IsEmptyCell(row
, col
) )
8398 MakeCellVisible( row
, col
);
8399 if ( expandSelection
)
8401 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8402 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8407 SetCurrentCell( row
, col
);
8416 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8419 m_currentCellCoords
!= wxGridNoCellCoords
&&
8420 m_currentCellCoords
.GetCol() > 0 )
8422 int row
= m_currentCellCoords
.GetRow();
8423 int col
= m_currentCellCoords
.GetCol();
8425 if ( m_table
->IsEmptyCell(row
, col
) )
8427 // starting in an empty cell: find the next block of
8433 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8436 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8438 // starting at the left of a block: find the next block
8444 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8449 // starting within a block: find the left of the block
8454 if ( m_table
->IsEmptyCell(row
, col
) )
8462 MakeCellVisible( row
, col
);
8463 if ( expandSelection
)
8465 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8466 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8471 SetCurrentCell( row
, col
);
8480 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8483 m_currentCellCoords
!= wxGridNoCellCoords
&&
8484 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8486 int row
= m_currentCellCoords
.GetRow();
8487 int col
= m_currentCellCoords
.GetCol();
8489 if ( m_table
->IsEmptyCell(row
, col
) )
8491 // starting in an empty cell: find the next block of
8494 while ( col
< m_numCols
-1 )
8497 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8500 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8502 // starting at the right of a block: find the next block
8505 while ( col
< m_numCols
-1 )
8508 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8513 // starting within a block: find the right of the block
8515 while ( col
< m_numCols
-1 )
8518 if ( m_table
->IsEmptyCell(row
, col
) )
8526 MakeCellVisible( row
, col
);
8527 if ( expandSelection
)
8529 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8530 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8535 SetCurrentCell( row
, col
);
8547 // ------ Label values and formatting
8550 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8552 *horiz
= m_rowLabelHorizAlign
;
8553 *vert
= m_rowLabelVertAlign
;
8556 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8558 *horiz
= m_colLabelHorizAlign
;
8559 *vert
= m_colLabelVertAlign
;
8562 int wxGrid::GetColLabelTextOrientation()
8564 return m_colLabelTextOrientation
;
8567 wxString
wxGrid::GetRowLabelValue( int row
)
8571 return m_table
->GetRowLabelValue( row
);
8581 wxString
wxGrid::GetColLabelValue( int col
)
8585 return m_table
->GetColLabelValue( col
);
8596 void wxGrid::SetRowLabelSize( int width
)
8598 width
= wxMax( width
, 0 );
8599 if ( width
!= m_rowLabelWidth
)
8603 m_rowLabelWin
->Show( false );
8604 m_cornerLabelWin
->Show( false );
8606 else if ( m_rowLabelWidth
== 0 )
8608 m_rowLabelWin
->Show( true );
8609 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8612 m_rowLabelWidth
= width
;
8614 wxScrolledWindow::Refresh( true );
8619 void wxGrid::SetColLabelSize( int height
)
8621 height
= wxMax( height
, 0 );
8622 if ( height
!= m_colLabelHeight
)
8626 m_colLabelWin
->Show( false );
8627 m_cornerLabelWin
->Show( false );
8629 else if ( m_colLabelHeight
== 0 )
8631 m_colLabelWin
->Show( true );
8632 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8635 m_colLabelHeight
= height
;
8637 wxScrolledWindow::Refresh( true );
8642 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8644 if ( m_labelBackgroundColour
!= colour
)
8646 m_labelBackgroundColour
= colour
;
8647 m_rowLabelWin
->SetBackgroundColour( colour
);
8648 m_colLabelWin
->SetBackgroundColour( colour
);
8649 m_cornerLabelWin
->SetBackgroundColour( colour
);
8651 if ( !GetBatchCount() )
8653 m_rowLabelWin
->Refresh();
8654 m_colLabelWin
->Refresh();
8655 m_cornerLabelWin
->Refresh();
8660 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8662 if ( m_labelTextColour
!= colour
)
8664 m_labelTextColour
= colour
;
8665 if ( !GetBatchCount() )
8667 m_rowLabelWin
->Refresh();
8668 m_colLabelWin
->Refresh();
8673 void wxGrid::SetLabelFont( const wxFont
& font
)
8676 if ( !GetBatchCount() )
8678 m_rowLabelWin
->Refresh();
8679 m_colLabelWin
->Refresh();
8683 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8685 // allow old (incorrect) defs to be used
8688 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8689 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8690 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8695 case wxTOP
: vert
= wxALIGN_TOP
; break;
8696 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8697 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8700 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8702 m_rowLabelHorizAlign
= horiz
;
8705 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8707 m_rowLabelVertAlign
= vert
;
8710 if ( !GetBatchCount() )
8712 m_rowLabelWin
->Refresh();
8716 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8718 // allow old (incorrect) defs to be used
8721 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8722 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8723 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8728 case wxTOP
: vert
= wxALIGN_TOP
; break;
8729 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8730 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8733 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8735 m_colLabelHorizAlign
= horiz
;
8738 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8740 m_colLabelVertAlign
= vert
;
8743 if ( !GetBatchCount() )
8745 m_colLabelWin
->Refresh();
8749 // Note: under MSW, the default column label font must be changed because it
8750 // does not support vertical printing
8752 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8753 // pGrid->SetLabelFont(font);
8754 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8756 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8758 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8760 m_colLabelTextOrientation
= textOrientation
;
8763 if ( !GetBatchCount() )
8765 m_colLabelWin
->Refresh();
8769 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8773 m_table
->SetRowLabelValue( row
, s
);
8774 if ( !GetBatchCount() )
8776 wxRect rect
= CellToRect( row
, 0);
8777 if ( rect
.height
> 0 )
8779 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8781 rect
.width
= m_rowLabelWidth
;
8782 m_rowLabelWin
->Refresh( true, &rect
);
8788 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8792 m_table
->SetColLabelValue( col
, s
);
8793 if ( !GetBatchCount() )
8795 wxRect rect
= CellToRect( 0, col
);
8796 if ( rect
.width
> 0 )
8798 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8800 rect
.height
= m_colLabelHeight
;
8801 m_colLabelWin
->Refresh( true, &rect
);
8807 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8809 if ( m_gridLineColour
!= colour
)
8811 m_gridLineColour
= colour
;
8813 wxClientDC
dc( m_gridWin
);
8815 DrawAllGridLines( dc
, wxRegion() );
8820 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8822 if ( m_cellHighlightColour
!= colour
)
8824 m_cellHighlightColour
= colour
;
8826 wxClientDC
dc( m_gridWin
);
8828 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8829 DrawCellHighlight(dc
, attr
);
8834 void wxGrid::SetCellHighlightPenWidth(int width
)
8836 if (m_cellHighlightPenWidth
!= width
) {
8837 m_cellHighlightPenWidth
= width
;
8839 // Just redrawing the cell highlight is not enough since that won't
8840 // make any visible change if the the thickness is getting smaller.
8841 int row
= m_currentCellCoords
.GetRow();
8842 int col
= m_currentCellCoords
.GetCol();
8843 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8845 wxRect rect
= CellToRect(row
, col
);
8846 m_gridWin
->Refresh(true, &rect
);
8850 void wxGrid::SetCellHighlightROPenWidth(int width
)
8852 if (m_cellHighlightROPenWidth
!= width
) {
8853 m_cellHighlightROPenWidth
= width
;
8855 // Just redrawing the cell highlight is not enough since that won't
8856 // make any visible change if the the thickness is getting smaller.
8857 int row
= m_currentCellCoords
.GetRow();
8858 int col
= m_currentCellCoords
.GetCol();
8859 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8861 wxRect rect
= CellToRect(row
, col
);
8862 m_gridWin
->Refresh(true, &rect
);
8866 void wxGrid::EnableGridLines( bool enable
)
8868 if ( enable
!= m_gridLinesEnabled
)
8870 m_gridLinesEnabled
= enable
;
8872 if ( !GetBatchCount() )
8876 wxClientDC
dc( m_gridWin
);
8878 DrawAllGridLines( dc
, wxRegion() );
8882 m_gridWin
->Refresh();
8889 int wxGrid::GetDefaultRowSize()
8891 return m_defaultRowHeight
;
8894 int wxGrid::GetRowSize( int row
)
8896 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8898 return GetRowHeight(row
);
8901 int wxGrid::GetDefaultColSize()
8903 return m_defaultColWidth
;
8906 int wxGrid::GetColSize( int col
)
8908 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8910 return GetColWidth(col
);
8913 // ============================================================================
8914 // access to the grid attributes: each of them has a default value in the grid
8915 // itself and may be overidden on a per-cell basis
8916 // ============================================================================
8918 // ----------------------------------------------------------------------------
8919 // setting default attributes
8920 // ----------------------------------------------------------------------------
8922 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8924 m_defaultCellAttr
->SetBackgroundColour(col
);
8926 m_gridWin
->SetBackgroundColour(col
);
8930 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8932 m_defaultCellAttr
->SetTextColour(col
);
8935 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8937 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8940 void wxGrid::SetDefaultCellOverflow( bool allow
)
8942 m_defaultCellAttr
->SetOverflow(allow
);
8945 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8947 m_defaultCellAttr
->SetFont(font
);
8951 // For editors and renderers the type registry takes precedence over the
8952 // default attr, so we need to register the new editor/renderer for the string
8953 // data type in order to make setting a default editor/renderer appear to
8956 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8958 RegisterDataType(wxGRID_VALUE_STRING
,
8960 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
8963 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8965 RegisterDataType(wxGRID_VALUE_STRING
,
8966 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
8970 // ----------------------------------------------------------------------------
8971 // access to the default attrbiutes
8972 // ----------------------------------------------------------------------------
8974 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8976 return m_defaultCellAttr
->GetBackgroundColour();
8979 wxColour
wxGrid::GetDefaultCellTextColour()
8981 return m_defaultCellAttr
->GetTextColour();
8984 wxFont
wxGrid::GetDefaultCellFont()
8986 return m_defaultCellAttr
->GetFont();
8989 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8991 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8994 bool wxGrid::GetDefaultCellOverflow()
8996 return m_defaultCellAttr
->GetOverflow();
8999 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9001 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9004 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9006 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
9009 // ----------------------------------------------------------------------------
9010 // access to cell attributes
9011 // ----------------------------------------------------------------------------
9013 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9015 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9016 wxColour colour
= attr
->GetBackgroundColour();
9021 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9023 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9024 wxColour colour
= attr
->GetTextColour();
9029 wxFont
wxGrid::GetCellFont( int row
, int col
)
9031 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9032 wxFont font
= attr
->GetFont();
9037 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9039 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9040 attr
->GetAlignment(horiz
, vert
);
9044 bool wxGrid::GetCellOverflow( int row
, int col
)
9046 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9047 bool allow
= attr
->GetOverflow();
9052 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9054 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9055 attr
->GetSize( num_rows
, num_cols
);
9059 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9061 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9062 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9068 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9070 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9071 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9077 bool wxGrid::IsReadOnly(int row
, int col
) const
9079 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9080 bool isReadOnly
= attr
->IsReadOnly();
9085 // ----------------------------------------------------------------------------
9086 // attribute support: cache, automatic provider creation, ...
9087 // ----------------------------------------------------------------------------
9089 bool wxGrid::CanHaveAttributes()
9096 return m_table
->CanHaveAttributes();
9099 void wxGrid::ClearAttrCache()
9101 if ( m_attrCache
.row
!= -1 )
9103 wxSafeDecRef(m_attrCache
.attr
);
9104 m_attrCache
.attr
= NULL
;
9105 m_attrCache
.row
= -1;
9109 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9113 wxGrid
*self
= (wxGrid
*)this; // const_cast
9115 self
->ClearAttrCache();
9116 self
->m_attrCache
.row
= row
;
9117 self
->m_attrCache
.col
= col
;
9118 self
->m_attrCache
.attr
= attr
;
9123 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9125 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9127 *attr
= m_attrCache
.attr
;
9128 wxSafeIncRef(m_attrCache
.attr
);
9130 #ifdef DEBUG_ATTR_CACHE
9131 gs_nAttrCacheHits
++;
9138 #ifdef DEBUG_ATTR_CACHE
9139 gs_nAttrCacheMisses
++;
9145 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9147 wxGridCellAttr
*attr
= NULL
;
9148 // Additional test to avoid looking at the cache e.g. for
9149 // wxNoCellCoords, as this will confuse memory management.
9152 if ( !LookupAttr(row
, col
, &attr
) )
9154 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9155 : (wxGridCellAttr
*)NULL
;
9156 CacheAttr(row
, col
, attr
);
9161 attr
->SetDefAttr(m_defaultCellAttr
);
9165 attr
= m_defaultCellAttr
;
9172 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9174 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9175 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9177 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9178 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9180 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9183 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9185 // artificially inc the ref count to match DecRef() in caller
9187 m_table
->SetAttr(attr
, row
, col
);
9193 // ----------------------------------------------------------------------------
9194 // setting column attributes (wrappers around SetColAttr)
9195 // ----------------------------------------------------------------------------
9197 void wxGrid::SetColFormatBool(int col
)
9199 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9202 void wxGrid::SetColFormatNumber(int col
)
9204 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9207 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9209 wxString typeName
= wxGRID_VALUE_FLOAT
;
9210 if ( (width
!= -1) || (precision
!= -1) )
9212 typeName
<< _T(':') << width
<< _T(',') << precision
;
9215 SetColFormatCustom(col
, typeName
);
9218 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9220 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9222 attr
= new wxGridCellAttr
;
9223 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9224 attr
->SetRenderer(renderer
);
9226 SetColAttr(col
, attr
);
9230 // ----------------------------------------------------------------------------
9231 // setting cell attributes: this is forwarded to the table
9232 // ----------------------------------------------------------------------------
9234 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9236 if ( CanHaveAttributes() )
9238 m_table
->SetAttr(attr
, row
, col
);
9247 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9249 if ( CanHaveAttributes() )
9251 m_table
->SetRowAttr(attr
, row
);
9260 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9262 if ( CanHaveAttributes() )
9264 m_table
->SetColAttr(attr
, col
);
9273 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9275 if ( CanHaveAttributes() )
9277 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9278 attr
->SetBackgroundColour(colour
);
9283 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9285 if ( CanHaveAttributes() )
9287 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9288 attr
->SetTextColour(colour
);
9293 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9295 if ( CanHaveAttributes() )
9297 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9298 attr
->SetFont(font
);
9303 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9305 if ( CanHaveAttributes() )
9307 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9308 attr
->SetAlignment(horiz
, vert
);
9313 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9315 if ( CanHaveAttributes() )
9317 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9318 attr
->SetOverflow(allow
);
9323 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9325 if ( CanHaveAttributes() )
9327 int cell_rows
, cell_cols
;
9329 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9330 attr
->GetSize(&cell_rows
, &cell_cols
);
9331 attr
->SetSize(num_rows
, num_cols
);
9334 // Cannot set the size of a cell to 0 or negative values
9335 // While it is perfectly legal to do that, this function cannot
9336 // handle all the possibilies, do it by hand by getting the CellAttr.
9337 // You can only set the size of a cell to 1,1 or greater with this fn
9338 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9339 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9340 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9341 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9343 // if this was already a multicell then "turn off" the other cells first
9344 if ((cell_rows
> 1) || (cell_rows
> 1))
9347 for (j
=row
; j
<row
+cell_rows
; j
++)
9349 for (i
=col
; i
<col
+cell_cols
; i
++)
9351 if ((i
!= col
) || (j
!= row
))
9353 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9354 attr_stub
->SetSize( 1, 1 );
9355 attr_stub
->DecRef();
9361 // mark the cells that will be covered by this cell to
9362 // negative or zero values to point back at this cell
9363 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9366 for (j
=row
; j
<row
+num_rows
; j
++)
9368 for (i
=col
; i
<col
+num_cols
; i
++)
9370 if ((i
!= col
) || (j
!= row
))
9372 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9373 attr_stub
->SetSize( row
-j
, col
-i
);
9374 attr_stub
->DecRef();
9382 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9384 if ( CanHaveAttributes() )
9386 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9387 attr
->SetRenderer(renderer
);
9392 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9394 if ( CanHaveAttributes() )
9396 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9397 attr
->SetEditor(editor
);
9402 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9404 if ( CanHaveAttributes() )
9406 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9407 attr
->SetReadOnly(isReadOnly
);
9412 // ----------------------------------------------------------------------------
9413 // Data type registration
9414 // ----------------------------------------------------------------------------
9416 void wxGrid::RegisterDataType(const wxString
& typeName
,
9417 wxGridCellRenderer
* renderer
,
9418 wxGridCellEditor
* editor
)
9420 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9424 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9426 wxString typeName
= m_table
->GetTypeName(row
, col
);
9427 return GetDefaultEditorForType(typeName
);
9430 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9432 wxString typeName
= m_table
->GetTypeName(row
, col
);
9433 return GetDefaultRendererForType(typeName
);
9437 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9439 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9440 if ( index
== wxNOT_FOUND
)
9442 wxFAIL_MSG(wxT("Unknown data type name"));
9447 return m_typeRegistry
->GetEditor(index
);
9451 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9453 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9454 if ( index
== wxNOT_FOUND
)
9456 wxFAIL_MSG(wxT("Unknown data type name"));
9461 return m_typeRegistry
->GetRenderer(index
);
9465 // ----------------------------------------------------------------------------
9467 // ----------------------------------------------------------------------------
9469 void wxGrid::EnableDragRowSize( bool enable
)
9471 m_canDragRowSize
= enable
;
9475 void wxGrid::EnableDragColSize( bool enable
)
9477 m_canDragColSize
= enable
;
9480 void wxGrid::EnableDragGridSize( bool enable
)
9482 m_canDragGridSize
= enable
;
9485 void wxGrid::EnableDragCell( bool enable
)
9487 m_canDragCell
= enable
;
9490 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9492 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9494 if ( resizeExistingRows
)
9496 // since we are resizing all rows to the default row size,
9497 // we can simply clear the row heights and row bottoms
9498 // arrays (which also allows us to take advantage of
9499 // some speed optimisations)
9500 m_rowHeights
.Empty();
9501 m_rowBottoms
.Empty();
9502 if ( !GetBatchCount() )
9507 void wxGrid::SetRowSize( int row
, int height
)
9509 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9511 // See comment in SetColSize
9512 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9514 if ( m_rowHeights
.IsEmpty() )
9516 // need to really create the array
9520 int h
= wxMax( 0, height
);
9521 int diff
= h
- m_rowHeights
[row
];
9523 m_rowHeights
[row
] = h
;
9525 for ( i
= row
; i
< m_numRows
; i
++ )
9527 m_rowBottoms
[i
] += diff
;
9529 if ( !GetBatchCount() )
9533 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9535 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9537 if ( resizeExistingCols
)
9539 // since we are resizing all columns to the default column size,
9540 // we can simply clear the col widths and col rights
9541 // arrays (which also allows us to take advantage of
9542 // some speed optimisations)
9543 m_colWidths
.Empty();
9544 m_colRights
.Empty();
9545 if ( !GetBatchCount() )
9550 void wxGrid::SetColSize( int col
, int width
)
9552 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9554 // should we check that it's bigger than GetColMinimalWidth(col) here?
9556 // No, because it is reasonable to assume the library user know's
9557 // what he is doing. However whe should test against the weaker
9558 // constariant of minimalAcceptableWidth, as this breaks rendering
9560 // This test then fixes sf.net bug #645734
9562 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9564 if ( m_colWidths
.IsEmpty() )
9566 // need to really create the array
9570 // if < 0 calc new width from label
9574 wxArrayString lines
;
9575 wxClientDC
dc(m_colLabelWin
);
9576 dc
.SetFont(GetLabelFont());
9577 StringToLines(GetColLabelValue(col
), lines
);
9578 GetTextBoxSize(dc
, lines
, &w
, &h
);
9581 int w
= wxMax( 0, width
);
9582 int diff
= w
- m_colWidths
[col
];
9583 m_colWidths
[col
] = w
;
9586 for ( i
= col
; i
< m_numCols
; i
++ )
9588 m_colRights
[i
] += diff
;
9590 if ( !GetBatchCount() )
9595 void wxGrid::SetColMinimalWidth( int col
, int width
)
9597 if (width
> GetColMinimalAcceptableWidth()) {
9598 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9599 m_colMinWidths
[key
] = width
;
9603 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9605 if (width
> GetRowMinimalAcceptableHeight()) {
9606 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9607 m_rowMinHeights
[key
] = width
;
9611 int wxGrid::GetColMinimalWidth(int col
) const
9613 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9614 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9615 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9618 int wxGrid::GetRowMinimalHeight(int row
) const
9620 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9621 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9622 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9625 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9627 // We do allow a width of 0 since this gives us
9628 // an easy way to temporarily hidding columns.
9631 m_minAcceptableColWidth
= width
;
9634 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9636 // We do allow a height of 0 since this gives us
9637 // an easy way to temporarily hidding rows.
9640 m_minAcceptableRowHeight
= height
;
9643 int wxGrid::GetColMinimalAcceptableWidth() const
9645 return m_minAcceptableColWidth
;
9648 int wxGrid::GetRowMinimalAcceptableHeight() const
9650 return m_minAcceptableRowHeight
;
9653 // ----------------------------------------------------------------------------
9655 // ----------------------------------------------------------------------------
9657 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9659 wxClientDC
dc(m_gridWin
);
9661 //Cancel editting of cell
9662 HideCellEditControl();
9663 SaveEditControlValue();
9665 // init both of them to avoid compiler warnings, even if weo nly need one
9673 wxCoord extent
, extentMax
= 0;
9674 int max
= column
? m_numRows
: m_numCols
;
9675 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9682 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9683 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9686 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9687 extent
= column
? size
.x
: size
.y
;
9688 if ( extent
> extentMax
)
9699 // now also compare with the column label extent
9701 dc
.SetFont( GetLabelFont() );
9705 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9706 if( GetColLabelTextOrientation() == wxVERTICAL
)
9710 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9712 extent
= column
? w
: h
;
9713 if ( extent
> extentMax
)
9720 // empty column - give default extent (notice that if extentMax is less
9721 // than default extent but != 0, it's ok)
9722 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9728 // leave some space around text
9739 SetColSize(col
, extentMax
);
9740 if ( !GetBatchCount() )
9743 m_gridWin
->GetClientSize( &cw
, &ch
);
9744 wxRect
rect ( CellToRect( 0, col
) );
9746 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9747 rect
.width
= cw
- rect
.x
;
9748 rect
.height
= m_colLabelHeight
;
9749 m_colLabelWin
->Refresh( true, &rect
);
9754 SetRowSize(row
, extentMax
);
9755 if ( !GetBatchCount() )
9758 m_gridWin
->GetClientSize( &cw
, &ch
);
9759 wxRect
rect ( CellToRect( row
, 0 ) );
9761 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9762 rect
.width
= m_rowLabelWidth
;
9763 rect
.height
= ch
- rect
.y
;
9764 m_rowLabelWin
->Refresh( true, &rect
);
9770 SetColMinimalWidth(col
, extentMax
);
9772 SetRowMinimalHeight(row
, extentMax
);
9776 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9778 int width
= m_rowLabelWidth
;
9783 for ( int col
= 0; col
< m_numCols
; col
++ )
9787 AutoSizeColumn(col
, setAsMin
);
9790 width
+= GetColWidth(col
);
9799 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9801 int height
= m_colLabelHeight
;
9806 for ( int row
= 0; row
< m_numRows
; row
++ )
9810 AutoSizeRow(row
, setAsMin
);
9813 height
+= GetRowHeight(row
);
9822 void wxGrid::AutoSize()
9826 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9828 // round up the size to a multiple of scroll step - this ensures that we
9829 // won't get the scrollbars if we're sized exactly to this width
9830 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9832 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9833 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9835 // distribute the extra space between the columns/rows to avoid having
9836 // extra white space
9838 // Remove the extra m_extraWidth + 1 added above
9839 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9840 if ( diff
&& m_numCols
)
9842 // try to resize the columns uniformly
9843 wxCoord diffPerCol
= diff
/ m_numCols
;
9846 for ( int col
= 0; col
< m_numCols
; col
++ )
9848 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9852 // add remaining amount to the last columns
9853 diff
-= diffPerCol
* m_numCols
;
9856 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9858 SetColSize(col
, GetColWidth(col
) + 1);
9864 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9865 if ( diff
&& m_numRows
)
9867 // try to resize the columns uniformly
9868 wxCoord diffPerRow
= diff
/ m_numRows
;
9871 for ( int row
= 0; row
< m_numRows
; row
++ )
9873 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9877 // add remaining amount to the last rows
9878 diff
-= diffPerRow
* m_numRows
;
9881 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9883 SetRowSize(row
, GetRowHeight(row
) + 1);
9890 SetClientSize(sizeFit
);
9893 void wxGrid::AutoSizeRowLabelSize( int row
)
9895 wxArrayString lines
;
9898 // Hide the edit control, so it
9899 // won't interfer with drag-shrinking.
9900 if( IsCellEditControlShown() )
9902 HideCellEditControl();
9903 SaveEditControlValue();
9906 // autosize row height depending on label text
9907 StringToLines( GetRowLabelValue( row
), lines
);
9908 wxClientDC
dc( m_rowLabelWin
);
9909 GetTextBoxSize( dc
, lines
, &w
, &h
);
9910 if( h
< m_defaultRowHeight
)
9911 h
= m_defaultRowHeight
;
9916 void wxGrid::AutoSizeColLabelSize( int col
)
9918 wxArrayString lines
;
9921 // Hide the edit control, so it
9922 // won't interfer with drag-shrinking.
9923 if( IsCellEditControlShown() )
9925 HideCellEditControl();
9926 SaveEditControlValue();
9929 // autosize column width depending on label text
9930 StringToLines( GetColLabelValue( col
), lines
);
9931 wxClientDC
dc( m_colLabelWin
);
9932 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9933 GetTextBoxSize( dc
, lines
, &w
, &h
);
9935 GetTextBoxSize( dc
, lines
, &h
, &w
);
9936 if( w
< m_defaultColWidth
)
9937 w
= m_defaultColWidth
;
9942 wxSize
wxGrid::DoGetBestSize() const
9944 // don't set sizes, only calculate them
9945 wxGrid
*self
= (wxGrid
*)this; // const_cast
9948 width
= self
->SetOrCalcColumnSizes(true);
9949 height
= self
->SetOrCalcRowSizes(true);
9951 if (!width
) width
=100;
9952 if (!height
) height
=80;
9954 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9955 // of the scrollbars, is there any magic incantaion for that?
9957 GetScrollPixelsPerUnit(&xpu
, &ypu
);
9959 width
+= 1 + xpu
- (width
% xpu
);
9961 height
+= 1 + ypu
- (height
% ypu
);
9963 // limit to 1/4 of the screen size
9964 int maxwidth
, maxheight
;
9965 wxDisplaySize( & maxwidth
, & maxheight
);
9968 if ( width
> maxwidth
) width
= maxwidth
;
9969 if ( height
> maxheight
) height
= maxheight
;
9972 wxSize
best(width
, height
);
9973 // NOTE: This size should be cached, but first we need to add calls to
9974 // InvalidateBestSize everywhere that could change the results of this
9976 // CacheBestSize(size);
9986 wxPen
& wxGrid::GetDividerPen() const
9991 // ----------------------------------------------------------------------------
9992 // cell value accessor functions
9993 // ----------------------------------------------------------------------------
9995 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9999 m_table
->SetValue( row
, col
, s
);
10000 if ( !GetBatchCount() )
10003 wxRect
rect( CellToRect( row
, col
) );
10005 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10006 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10007 m_gridWin
->Refresh( false, &rect
);
10010 if ( m_currentCellCoords
.GetRow() == row
&&
10011 m_currentCellCoords
.GetCol() == col
&&
10012 IsCellEditControlShown())
10013 // Note: If we are using IsCellEditControlEnabled,
10014 // this interacts badly with calling SetCellValue from
10015 // an EVT_GRID_CELL_CHANGE handler.
10017 HideCellEditControl();
10018 ShowCellEditControl(); // will reread data from table
10025 // ------ Block, row and col selection
10028 void wxGrid::SelectRow( int row
, bool addToSelected
)
10030 if ( IsSelection() && !addToSelected
)
10034 m_selection
->SelectRow( row
, false, addToSelected
);
10038 void wxGrid::SelectCol( int col
, bool addToSelected
)
10040 if ( IsSelection() && !addToSelected
)
10044 m_selection
->SelectCol( col
, false, addToSelected
);
10048 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10049 bool addToSelected
)
10051 if ( IsSelection() && !addToSelected
)
10055 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10056 false, addToSelected
);
10060 void wxGrid::SelectAll()
10062 if ( m_numRows
> 0 && m_numCols
> 0 )
10065 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10070 // ------ Cell, row and col deselection
10073 void wxGrid::DeselectRow( int row
)
10075 if ( !m_selection
)
10078 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10080 if ( m_selection
->IsInSelection(row
, 0 ) )
10081 m_selection
->ToggleCellSelection( row
, 0);
10085 int nCols
= GetNumberCols();
10086 for ( int i
= 0; i
< nCols
; i
++ )
10088 if ( m_selection
->IsInSelection(row
, i
) )
10089 m_selection
->ToggleCellSelection( row
, i
);
10094 void wxGrid::DeselectCol( int col
)
10096 if ( !m_selection
)
10099 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10101 if ( m_selection
->IsInSelection(0, col
) )
10102 m_selection
->ToggleCellSelection( 0, col
);
10106 int nRows
= GetNumberRows();
10107 for ( int i
= 0; i
< nRows
; i
++ )
10109 if ( m_selection
->IsInSelection(i
, col
) )
10110 m_selection
->ToggleCellSelection(i
, col
);
10115 void wxGrid::DeselectCell( int row
, int col
)
10117 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10118 m_selection
->ToggleCellSelection(row
, col
);
10121 bool wxGrid::IsSelection()
10123 return ( m_selection
&& (m_selection
->IsSelection() ||
10124 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10125 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10128 bool wxGrid::IsInSelection( int row
, int col
) const
10130 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10131 ( row
>= m_selectingTopLeft
.GetRow() &&
10132 col
>= m_selectingTopLeft
.GetCol() &&
10133 row
<= m_selectingBottomRight
.GetRow() &&
10134 col
<= m_selectingBottomRight
.GetCol() )) );
10137 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10139 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10140 return m_selection
->m_cellSelection
;
10142 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10144 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10145 return m_selection
->m_blockSelectionTopLeft
;
10147 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10149 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10150 return m_selection
->m_blockSelectionBottomRight
;
10152 wxArrayInt
wxGrid::GetSelectedRows() const
10154 if (!m_selection
) { wxArrayInt a
; return a
; }
10155 return m_selection
->m_rowSelection
;
10157 wxArrayInt
wxGrid::GetSelectedCols() const
10159 if (!m_selection
) { wxArrayInt a
; return a
; }
10160 return m_selection
->m_colSelection
;
10164 void wxGrid::ClearSelection()
10166 m_selectingTopLeft
= wxGridNoCellCoords
;
10167 m_selectingBottomRight
= wxGridNoCellCoords
;
10169 m_selection
->ClearSelection();
10173 // This function returns the rectangle that encloses the given block
10174 // in device coords clipped to the client size of the grid window.
10176 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10177 const wxGridCellCoords
&bottomRight
)
10179 wxRect
rect( wxGridNoCellRect
);
10182 cellRect
= CellToRect( topLeft
);
10183 if ( cellRect
!= wxGridNoCellRect
)
10189 rect
= wxRect( 0, 0, 0, 0 );
10192 cellRect
= CellToRect( bottomRight
);
10193 if ( cellRect
!= wxGridNoCellRect
)
10199 return wxGridNoCellRect
;
10203 int left
= rect
.GetLeft();
10204 int top
= rect
.GetTop();
10205 int right
= rect
.GetRight();
10206 int bottom
= rect
.GetBottom();
10208 int leftCol
= topLeft
.GetCol();
10209 int topRow
= topLeft
.GetRow();
10210 int rightCol
= bottomRight
.GetCol();
10211 int bottomRow
= bottomRight
.GetRow();
10229 topRow
= bottomRow
;
10234 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10236 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10238 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10240 cellRect
= CellToRect( j
, i
);
10242 if (cellRect
.x
< left
)
10244 if (cellRect
.y
< top
)
10246 if (cellRect
.x
+ cellRect
.width
> right
)
10247 right
= cellRect
.x
+ cellRect
.width
;
10248 if (cellRect
.y
+ cellRect
.height
> bottom
)
10249 bottom
= cellRect
.y
+ cellRect
.height
;
10251 else i
= rightCol
; // jump over inner cells.
10255 // convert to scrolled coords
10257 CalcScrolledPosition( left
, top
, &left
, &top
);
10258 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10261 m_gridWin
->GetClientSize( &cw
, &ch
);
10263 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10264 return wxRect( 0, 0, 0, 0);
10266 rect
.SetLeft( wxMax(0, left
) );
10267 rect
.SetTop( wxMax(0, top
) );
10268 rect
.SetRight( wxMin(cw
, right
) );
10269 rect
.SetBottom( wxMin(ch
, bottom
) );
10275 // ------ Grid event classes
10278 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10280 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10281 int row
, int col
, int x
, int y
, bool sel
,
10282 bool control
, bool shift
, bool alt
, bool meta
)
10283 : wxNotifyEvent( type
, id
)
10290 m_control
= control
;
10295 SetEventObject(obj
);
10299 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10301 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10302 int rowOrCol
, int x
, int y
,
10303 bool control
, bool shift
, bool alt
, bool meta
)
10304 : wxNotifyEvent( type
, id
)
10306 m_rowOrCol
= rowOrCol
;
10309 m_control
= control
;
10314 SetEventObject(obj
);
10318 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10320 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10321 const wxGridCellCoords
& topLeft
,
10322 const wxGridCellCoords
& bottomRight
,
10323 bool sel
, bool control
,
10324 bool shift
, bool alt
, bool meta
)
10325 : wxNotifyEvent( type
, id
)
10327 m_topLeft
= topLeft
;
10328 m_bottomRight
= bottomRight
;
10330 m_control
= control
;
10335 SetEventObject(obj
);
10339 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10341 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10342 wxObject
* obj
, int row
,
10343 int col
, wxControl
* ctrl
)
10344 : wxCommandEvent(type
, id
)
10346 SetEventObject(obj
);
10352 #endif // wxUSE_GRID