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()
7527 bool wxGrid::Enable(bool enable
)
7529 if ( !wxScrolledWindow::Enable(enable
) )
7532 // redraw in the new state
7533 m_gridWin
->Refresh();
7539 // ------ Edit control functions
7543 void wxGrid::EnableEditing( bool edit
)
7545 // TODO: improve this ?
7547 if ( edit
!= m_editable
)
7549 if(!edit
) EnableCellEditControl(edit
);
7555 void wxGrid::EnableCellEditControl( bool enable
)
7560 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7561 SetCurrentCell( 0, 0 );
7563 if ( enable
!= m_cellEditCtrlEnabled
)
7567 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7570 // this should be checked by the caller!
7571 wxASSERT_MSG( CanEnableCellControl(),
7572 _T("can't enable editing for this cell!") );
7574 // do it before ShowCellEditControl()
7575 m_cellEditCtrlEnabled
= enable
;
7577 ShowCellEditControl();
7581 //FIXME:add veto support
7582 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7584 HideCellEditControl();
7585 SaveEditControlValue();
7587 // do it after HideCellEditControl()
7588 m_cellEditCtrlEnabled
= enable
;
7593 bool wxGrid::IsCurrentCellReadOnly() const
7596 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7597 bool readonly
= attr
->IsReadOnly();
7603 bool wxGrid::CanEnableCellControl() const
7605 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7606 !IsCurrentCellReadOnly();
7610 bool wxGrid::IsCellEditControlEnabled() const
7612 // the cell edit control might be disable for all cells or just for the
7613 // current one if it's read only
7614 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7617 bool wxGrid::IsCellEditControlShown() const
7619 bool isShown
= false;
7621 if ( m_cellEditCtrlEnabled
)
7623 int row
= m_currentCellCoords
.GetRow();
7624 int col
= m_currentCellCoords
.GetCol();
7625 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7626 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7631 if ( editor
->IsCreated() )
7633 isShown
= editor
->GetControl()->IsShown();
7643 void wxGrid::ShowCellEditControl()
7645 if ( IsCellEditControlEnabled() )
7647 if ( !IsVisible( m_currentCellCoords
) )
7649 m_cellEditCtrlEnabled
= false;
7654 wxRect rect
= CellToRect( m_currentCellCoords
);
7655 int row
= m_currentCellCoords
.GetRow();
7656 int col
= m_currentCellCoords
.GetCol();
7658 // if this is part of a multicell, find owner (topleft)
7659 int cell_rows
, cell_cols
;
7660 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7661 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7665 m_currentCellCoords
.SetRow( row
);
7666 m_currentCellCoords
.SetCol( col
);
7669 // convert to scrolled coords
7671 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7673 // done in PaintBackground()
7675 // erase the highlight and the cell contents because the editor
7676 // might not cover the entire cell
7677 wxClientDC
dc( m_gridWin
);
7679 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7680 dc
.SetPen(*wxTRANSPARENT_PEN
);
7681 dc
.DrawRectangle(rect
);
7684 // cell is shifted by one pixel
7685 // However, don't allow x or y to become negative
7686 // since the SetSize() method interprets that as
7693 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7694 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7695 if ( !editor
->IsCreated() )
7697 editor
->Create(m_gridWin
, wxID_ANY
,
7698 new wxGridCellEditorEvtHandler(this, editor
));
7700 wxGridEditorCreatedEvent
evt(GetId(),
7701 wxEVT_GRID_EDITOR_CREATED
,
7705 editor
->GetControl());
7706 GetEventHandler()->ProcessEvent(evt
);
7710 // resize editor to overflow into righthand cells if allowed
7711 int maxWidth
= rect
.width
;
7712 wxString value
= GetCellValue(row
, col
);
7713 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7716 GetTextExtent(value
, &maxWidth
, &y
,
7717 NULL
, NULL
, &attr
->GetFont());
7718 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7720 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7721 if (rect
.x
+maxWidth
> client_right
)
7722 maxWidth
= client_right
- rect
.x
;
7724 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7726 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7727 // may have changed earlier
7728 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7731 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7732 // looks weird going over a multicell
7733 if (m_table
->IsEmptyCell(row
,i
) &&
7734 (rect
.width
< maxWidth
) && (c_rows
== 1))
7735 rect
.width
+= GetColWidth(i
);
7739 if (rect
.GetRight() > client_right
)
7740 rect
.SetRight(client_right
-1);
7743 editor
->SetCellAttr(attr
);
7744 editor
->SetSize( rect
);
7745 editor
->Show( true, attr
);
7747 // recalc dimensions in case we need to
7748 // expand the scrolled window to account for editor
7751 editor
->BeginEdit(row
, col
, this);
7752 editor
->SetCellAttr(NULL
);
7761 void wxGrid::HideCellEditControl()
7763 if ( IsCellEditControlEnabled() )
7765 int row
= m_currentCellCoords
.GetRow();
7766 int col
= m_currentCellCoords
.GetCol();
7768 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7769 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7770 editor
->Show( false );
7773 m_gridWin
->SetFocus();
7774 // refresh whole row to the right
7775 wxRect
rect( CellToRect(row
, col
) );
7776 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7777 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7778 m_gridWin
->Refresh( false, &rect
);
7783 void wxGrid::SaveEditControlValue()
7785 if ( IsCellEditControlEnabled() )
7787 int row
= m_currentCellCoords
.GetRow();
7788 int col
= m_currentCellCoords
.GetCol();
7790 wxString oldval
= GetCellValue(row
,col
);
7792 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7793 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7794 bool changed
= editor
->EndEdit(row
, col
, this);
7801 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7802 m_currentCellCoords
.GetRow(),
7803 m_currentCellCoords
.GetCol() ) < 0 ) {
7805 // Event has been vetoed, set the data back.
7806 SetCellValue(row
,col
,oldval
);
7814 // ------ Grid location functions
7815 // Note that all of these functions work with the logical coordinates of
7816 // grid cells and labels so you will need to convert from device
7817 // coordinates for mouse events etc.
7820 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7822 int row
= YToRow(y
);
7823 int col
= XToCol(x
);
7825 if ( row
== -1 || col
== -1 )
7827 coords
= wxGridNoCellCoords
;
7831 coords
.Set( row
, col
);
7836 // Internal Helper function for computing row or column from some
7837 // (unscrolled) coordinate value, using either
7838 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7839 // of m_rowBottoms/m_ColRights to speed up the search!
7841 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7842 const wxArrayInt
& BorderArray
, int nMax
,
7847 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7853 size_t i_max
= coord
/ defaultDist
,
7856 if (BorderArray
.IsEmpty())
7858 if((int) i_max
< nMax
)
7860 return clipToMinMax
? nMax
- 1 : -1;
7863 if ( i_max
>= BorderArray
.GetCount())
7864 i_max
= BorderArray
.GetCount() - 1;
7867 if ( coord
>= BorderArray
[i_max
])
7871 i_max
= coord
/ minDist
;
7873 i_max
= BorderArray
.GetCount() - 1;
7875 if ( i_max
>= BorderArray
.GetCount())
7876 i_max
= BorderArray
.GetCount() - 1;
7878 if ( coord
>= BorderArray
[i_max
])
7879 return clipToMinMax
? (int)i_max
: -1;
7880 if ( coord
< BorderArray
[0] )
7883 while ( i_max
- i_min
> 0 )
7885 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7886 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7887 if (coord
>= BorderArray
[ i_max
- 1])
7891 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7892 if (coord
< BorderArray
[median
])
7900 int wxGrid::YToRow( int y
)
7902 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7903 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7907 int wxGrid::XToCol( int x
)
7909 return CoordToRowOrCol(x
, m_defaultColWidth
,
7910 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7914 // return the row number that that the y coord is near the edge of, or
7915 // -1 if not near an edge
7917 int wxGrid::YToEdgeOfRow( int y
)
7920 i
= internalYToRow(y
);
7922 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7924 // We know that we are in row i, test whether we are
7925 // close enough to lower or upper border, respectively.
7926 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7928 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7936 // return the col number that that the x coord is near the edge of, or
7937 // -1 if not near an edge
7939 int wxGrid::XToEdgeOfCol( int x
)
7942 i
= internalXToCol(x
);
7944 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7946 // We know that we are in column i, test whether we are
7947 // close enough to right or left border, respectively.
7948 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7950 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7958 wxRect
wxGrid::CellToRect( int row
, int col
)
7960 wxRect
rect( -1, -1, -1, -1 );
7962 if ( row
>= 0 && row
< m_numRows
&&
7963 col
>= 0 && col
< m_numCols
)
7965 int i
, cell_rows
, cell_cols
;
7966 rect
.width
= rect
.height
= 0;
7967 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7968 // if negative then find multicell owner
7969 if (cell_rows
< 0) row
+= cell_rows
;
7970 if (cell_cols
< 0) col
+= cell_cols
;
7971 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7973 rect
.x
= GetColLeft(col
);
7974 rect
.y
= GetRowTop(row
);
7975 for (i
=col
; i
<col
+cell_cols
; i
++)
7976 rect
.width
+= GetColWidth(i
);
7977 for (i
=row
; i
<row
+cell_rows
; i
++)
7978 rect
.height
+= GetRowHeight(i
);
7981 // if grid lines are enabled, then the area of the cell is a bit smaller
7982 if (m_gridLinesEnabled
) {
7990 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7992 // get the cell rectangle in logical coords
7994 wxRect
r( CellToRect( row
, col
) );
7996 // convert to device coords
7998 int left
, top
, right
, bottom
;
7999 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8000 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8002 // check against the client area of the grid window
8005 m_gridWin
->GetClientSize( &cw
, &ch
);
8007 if ( wholeCellVisible
)
8009 // is the cell wholly visible ?
8011 return ( left
>= 0 && right
<= cw
&&
8012 top
>= 0 && bottom
<= ch
);
8016 // is the cell partly visible ?
8018 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8019 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8024 // make the specified cell location visible by doing a minimal amount
8027 void wxGrid::MakeCellVisible( int row
, int col
)
8031 int xpos
= -1, ypos
= -1;
8033 if ( row
>= 0 && row
< m_numRows
&&
8034 col
>= 0 && col
< m_numCols
)
8036 // get the cell rectangle in logical coords
8038 wxRect
r( CellToRect( row
, col
) );
8040 // convert to device coords
8042 int left
, top
, right
, bottom
;
8043 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8044 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8047 m_gridWin
->GetClientSize( &cw
, &ch
);
8053 else if ( bottom
> ch
)
8055 int h
= r
.GetHeight();
8057 for ( i
= row
-1; i
>= 0; i
-- )
8059 int rowHeight
= GetRowHeight(i
);
8060 if ( h
+ rowHeight
> ch
)
8067 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8068 // have rounding errors (this is important, because if we do, we
8069 // might not scroll at all and some cells won't be redrawn)
8071 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8073 ypos
+= GRID_SCROLL_LINE_Y
;
8080 else if ( right
> cw
)
8082 // position the view so that the cell is on the right
8084 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8085 xpos
= x0
+ (right
- cw
);
8087 // see comment for ypos above
8088 xpos
+= GRID_SCROLL_LINE_X
;
8091 if ( xpos
!= -1 || ypos
!= -1 )
8094 xpos
/= GRID_SCROLL_LINE_X
;
8096 ypos
/= GRID_SCROLL_LINE_Y
;
8097 Scroll( xpos
, ypos
);
8105 // ------ Grid cursor movement functions
8108 bool wxGrid::MoveCursorUp( bool expandSelection
)
8110 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8111 m_currentCellCoords
.GetRow() >= 0 )
8113 if ( expandSelection
)
8115 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8116 m_selectingKeyboard
= m_currentCellCoords
;
8117 if ( m_selectingKeyboard
.GetRow() > 0 )
8119 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8120 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8121 m_selectingKeyboard
.GetCol() );
8122 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8125 else if ( m_currentCellCoords
.GetRow() > 0 )
8128 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8129 m_currentCellCoords
.GetCol() );
8130 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8131 m_currentCellCoords
.GetCol() );
8142 bool wxGrid::MoveCursorDown( bool expandSelection
)
8144 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8145 m_currentCellCoords
.GetRow() < m_numRows
)
8147 if ( expandSelection
)
8149 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8150 m_selectingKeyboard
= m_currentCellCoords
;
8151 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8153 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8154 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8155 m_selectingKeyboard
.GetCol() );
8156 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8159 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8162 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8163 m_currentCellCoords
.GetCol() );
8164 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8165 m_currentCellCoords
.GetCol() );
8176 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8178 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8179 m_currentCellCoords
.GetCol() >= 0 )
8181 if ( expandSelection
)
8183 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8184 m_selectingKeyboard
= m_currentCellCoords
;
8185 if ( m_selectingKeyboard
.GetCol() > 0 )
8187 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8188 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8189 m_selectingKeyboard
.GetCol() );
8190 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8193 else if ( m_currentCellCoords
.GetCol() > 0 )
8196 MakeCellVisible( m_currentCellCoords
.GetRow(),
8197 m_currentCellCoords
.GetCol() - 1 );
8198 SetCurrentCell( m_currentCellCoords
.GetRow(),
8199 m_currentCellCoords
.GetCol() - 1 );
8210 bool wxGrid::MoveCursorRight( bool expandSelection
)
8212 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8213 m_currentCellCoords
.GetCol() < m_numCols
)
8215 if ( expandSelection
)
8217 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8218 m_selectingKeyboard
= m_currentCellCoords
;
8219 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8221 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8222 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8223 m_selectingKeyboard
.GetCol() );
8224 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8227 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8230 MakeCellVisible( m_currentCellCoords
.GetRow(),
8231 m_currentCellCoords
.GetCol() + 1 );
8232 SetCurrentCell( m_currentCellCoords
.GetRow(),
8233 m_currentCellCoords
.GetCol() + 1 );
8244 bool wxGrid::MovePageUp()
8246 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8248 int row
= m_currentCellCoords
.GetRow();
8252 m_gridWin
->GetClientSize( &cw
, &ch
);
8254 int y
= GetRowTop(row
);
8255 int newRow
= internalYToRow( y
- ch
+ 1 );
8257 if ( newRow
== row
)
8259 //row > 0 , so newrow can never be less than 0 here.
8263 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8264 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8272 bool wxGrid::MovePageDown()
8274 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8276 int row
= m_currentCellCoords
.GetRow();
8277 if ( (row
+1) < m_numRows
)
8280 m_gridWin
->GetClientSize( &cw
, &ch
);
8282 int y
= GetRowTop(row
);
8283 int newRow
= internalYToRow( y
+ ch
);
8284 if ( newRow
== row
)
8286 // row < m_numRows , so newrow can't overflow here.
8290 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8291 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8299 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8302 m_currentCellCoords
!= wxGridNoCellCoords
&&
8303 m_currentCellCoords
.GetRow() > 0 )
8305 int row
= m_currentCellCoords
.GetRow();
8306 int col
= m_currentCellCoords
.GetCol();
8308 if ( m_table
->IsEmptyCell(row
, col
) )
8310 // starting in an empty cell: find the next block of
8316 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8319 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8321 // starting at the top of a block: find the next block
8327 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8332 // starting within a block: find the top of the block
8337 if ( m_table
->IsEmptyCell(row
, col
) )
8345 MakeCellVisible( row
, col
);
8346 if ( expandSelection
)
8348 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8349 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8354 SetCurrentCell( row
, col
);
8362 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8365 m_currentCellCoords
!= wxGridNoCellCoords
&&
8366 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8368 int row
= m_currentCellCoords
.GetRow();
8369 int col
= m_currentCellCoords
.GetCol();
8371 if ( m_table
->IsEmptyCell(row
, col
) )
8373 // starting in an empty cell: find the next block of
8376 while ( row
< m_numRows
-1 )
8379 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8382 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8384 // starting at the bottom of a block: find the next block
8387 while ( row
< m_numRows
-1 )
8390 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8395 // starting within a block: find the bottom of the block
8397 while ( row
< m_numRows
-1 )
8400 if ( m_table
->IsEmptyCell(row
, col
) )
8408 MakeCellVisible( row
, col
);
8409 if ( expandSelection
)
8411 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8412 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8417 SetCurrentCell( row
, col
);
8426 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8429 m_currentCellCoords
!= wxGridNoCellCoords
&&
8430 m_currentCellCoords
.GetCol() > 0 )
8432 int row
= m_currentCellCoords
.GetRow();
8433 int col
= m_currentCellCoords
.GetCol();
8435 if ( m_table
->IsEmptyCell(row
, col
) )
8437 // starting in an empty cell: find the next block of
8443 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8446 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8448 // starting at the left of a block: find the next block
8454 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8459 // starting within a block: find the left of the block
8464 if ( m_table
->IsEmptyCell(row
, col
) )
8472 MakeCellVisible( row
, col
);
8473 if ( expandSelection
)
8475 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8476 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8481 SetCurrentCell( row
, col
);
8490 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8493 m_currentCellCoords
!= wxGridNoCellCoords
&&
8494 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8496 int row
= m_currentCellCoords
.GetRow();
8497 int col
= m_currentCellCoords
.GetCol();
8499 if ( m_table
->IsEmptyCell(row
, col
) )
8501 // starting in an empty cell: find the next block of
8504 while ( col
< m_numCols
-1 )
8507 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8510 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8512 // starting at the right of a block: find the next block
8515 while ( col
< m_numCols
-1 )
8518 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8523 // starting within a block: find the right of the block
8525 while ( col
< m_numCols
-1 )
8528 if ( m_table
->IsEmptyCell(row
, col
) )
8536 MakeCellVisible( row
, col
);
8537 if ( expandSelection
)
8539 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8540 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8545 SetCurrentCell( row
, col
);
8557 // ------ Label values and formatting
8560 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8562 *horiz
= m_rowLabelHorizAlign
;
8563 *vert
= m_rowLabelVertAlign
;
8566 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8568 *horiz
= m_colLabelHorizAlign
;
8569 *vert
= m_colLabelVertAlign
;
8572 int wxGrid::GetColLabelTextOrientation()
8574 return m_colLabelTextOrientation
;
8577 wxString
wxGrid::GetRowLabelValue( int row
)
8581 return m_table
->GetRowLabelValue( row
);
8591 wxString
wxGrid::GetColLabelValue( int col
)
8595 return m_table
->GetColLabelValue( col
);
8606 void wxGrid::SetRowLabelSize( int width
)
8608 width
= wxMax( width
, 0 );
8609 if ( width
!= m_rowLabelWidth
)
8613 m_rowLabelWin
->Show( false );
8614 m_cornerLabelWin
->Show( false );
8616 else if ( m_rowLabelWidth
== 0 )
8618 m_rowLabelWin
->Show( true );
8619 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8622 m_rowLabelWidth
= width
;
8624 wxScrolledWindow::Refresh( true );
8629 void wxGrid::SetColLabelSize( int height
)
8631 height
= wxMax( height
, 0 );
8632 if ( height
!= m_colLabelHeight
)
8636 m_colLabelWin
->Show( false );
8637 m_cornerLabelWin
->Show( false );
8639 else if ( m_colLabelHeight
== 0 )
8641 m_colLabelWin
->Show( true );
8642 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8645 m_colLabelHeight
= height
;
8647 wxScrolledWindow::Refresh( true );
8652 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8654 if ( m_labelBackgroundColour
!= colour
)
8656 m_labelBackgroundColour
= colour
;
8657 m_rowLabelWin
->SetBackgroundColour( colour
);
8658 m_colLabelWin
->SetBackgroundColour( colour
);
8659 m_cornerLabelWin
->SetBackgroundColour( colour
);
8661 if ( !GetBatchCount() )
8663 m_rowLabelWin
->Refresh();
8664 m_colLabelWin
->Refresh();
8665 m_cornerLabelWin
->Refresh();
8670 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8672 if ( m_labelTextColour
!= colour
)
8674 m_labelTextColour
= colour
;
8675 if ( !GetBatchCount() )
8677 m_rowLabelWin
->Refresh();
8678 m_colLabelWin
->Refresh();
8683 void wxGrid::SetLabelFont( const wxFont
& font
)
8686 if ( !GetBatchCount() )
8688 m_rowLabelWin
->Refresh();
8689 m_colLabelWin
->Refresh();
8693 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8695 // allow old (incorrect) defs to be used
8698 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8699 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8700 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8705 case wxTOP
: vert
= wxALIGN_TOP
; break;
8706 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8707 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8710 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8712 m_rowLabelHorizAlign
= horiz
;
8715 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8717 m_rowLabelVertAlign
= vert
;
8720 if ( !GetBatchCount() )
8722 m_rowLabelWin
->Refresh();
8726 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8728 // allow old (incorrect) defs to be used
8731 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8732 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8733 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8738 case wxTOP
: vert
= wxALIGN_TOP
; break;
8739 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8740 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8743 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8745 m_colLabelHorizAlign
= horiz
;
8748 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8750 m_colLabelVertAlign
= vert
;
8753 if ( !GetBatchCount() )
8755 m_colLabelWin
->Refresh();
8759 // Note: under MSW, the default column label font must be changed because it
8760 // does not support vertical printing
8762 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8763 // pGrid->SetLabelFont(font);
8764 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8766 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8768 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8770 m_colLabelTextOrientation
= textOrientation
;
8773 if ( !GetBatchCount() )
8775 m_colLabelWin
->Refresh();
8779 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8783 m_table
->SetRowLabelValue( row
, s
);
8784 if ( !GetBatchCount() )
8786 wxRect rect
= CellToRect( row
, 0);
8787 if ( rect
.height
> 0 )
8789 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8791 rect
.width
= m_rowLabelWidth
;
8792 m_rowLabelWin
->Refresh( true, &rect
);
8798 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8802 m_table
->SetColLabelValue( col
, s
);
8803 if ( !GetBatchCount() )
8805 wxRect rect
= CellToRect( 0, col
);
8806 if ( rect
.width
> 0 )
8808 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8810 rect
.height
= m_colLabelHeight
;
8811 m_colLabelWin
->Refresh( true, &rect
);
8817 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8819 if ( m_gridLineColour
!= colour
)
8821 m_gridLineColour
= colour
;
8823 wxClientDC
dc( m_gridWin
);
8825 DrawAllGridLines( dc
, wxRegion() );
8830 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8832 if ( m_cellHighlightColour
!= colour
)
8834 m_cellHighlightColour
= colour
;
8836 wxClientDC
dc( m_gridWin
);
8838 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8839 DrawCellHighlight(dc
, attr
);
8844 void wxGrid::SetCellHighlightPenWidth(int width
)
8846 if (m_cellHighlightPenWidth
!= width
) {
8847 m_cellHighlightPenWidth
= width
;
8849 // Just redrawing the cell highlight is not enough since that won't
8850 // make any visible change if the the thickness is getting smaller.
8851 int row
= m_currentCellCoords
.GetRow();
8852 int col
= m_currentCellCoords
.GetCol();
8853 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8855 wxRect rect
= CellToRect(row
, col
);
8856 m_gridWin
->Refresh(true, &rect
);
8860 void wxGrid::SetCellHighlightROPenWidth(int width
)
8862 if (m_cellHighlightROPenWidth
!= width
) {
8863 m_cellHighlightROPenWidth
= width
;
8865 // Just redrawing the cell highlight is not enough since that won't
8866 // make any visible change if the the thickness is getting smaller.
8867 int row
= m_currentCellCoords
.GetRow();
8868 int col
= m_currentCellCoords
.GetCol();
8869 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8871 wxRect rect
= CellToRect(row
, col
);
8872 m_gridWin
->Refresh(true, &rect
);
8876 void wxGrid::EnableGridLines( bool enable
)
8878 if ( enable
!= m_gridLinesEnabled
)
8880 m_gridLinesEnabled
= enable
;
8882 if ( !GetBatchCount() )
8886 wxClientDC
dc( m_gridWin
);
8888 DrawAllGridLines( dc
, wxRegion() );
8892 m_gridWin
->Refresh();
8899 int wxGrid::GetDefaultRowSize()
8901 return m_defaultRowHeight
;
8904 int wxGrid::GetRowSize( int row
)
8906 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8908 return GetRowHeight(row
);
8911 int wxGrid::GetDefaultColSize()
8913 return m_defaultColWidth
;
8916 int wxGrid::GetColSize( int col
)
8918 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8920 return GetColWidth(col
);
8923 // ============================================================================
8924 // access to the grid attributes: each of them has a default value in the grid
8925 // itself and may be overidden on a per-cell basis
8926 // ============================================================================
8928 // ----------------------------------------------------------------------------
8929 // setting default attributes
8930 // ----------------------------------------------------------------------------
8932 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8934 m_defaultCellAttr
->SetBackgroundColour(col
);
8936 m_gridWin
->SetBackgroundColour(col
);
8940 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8942 m_defaultCellAttr
->SetTextColour(col
);
8945 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8947 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8950 void wxGrid::SetDefaultCellOverflow( bool allow
)
8952 m_defaultCellAttr
->SetOverflow(allow
);
8955 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8957 m_defaultCellAttr
->SetFont(font
);
8961 // For editors and renderers the type registry takes precedence over the
8962 // default attr, so we need to register the new editor/renderer for the string
8963 // data type in order to make setting a default editor/renderer appear to
8966 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8968 RegisterDataType(wxGRID_VALUE_STRING
,
8970 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
8973 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8975 RegisterDataType(wxGRID_VALUE_STRING
,
8976 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
8980 // ----------------------------------------------------------------------------
8981 // access to the default attrbiutes
8982 // ----------------------------------------------------------------------------
8984 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8986 return m_defaultCellAttr
->GetBackgroundColour();
8989 wxColour
wxGrid::GetDefaultCellTextColour()
8991 return m_defaultCellAttr
->GetTextColour();
8994 wxFont
wxGrid::GetDefaultCellFont()
8996 return m_defaultCellAttr
->GetFont();
8999 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9001 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9004 bool wxGrid::GetDefaultCellOverflow()
9006 return m_defaultCellAttr
->GetOverflow();
9009 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9011 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9014 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9016 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
9019 // ----------------------------------------------------------------------------
9020 // access to cell attributes
9021 // ----------------------------------------------------------------------------
9023 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9025 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9026 wxColour colour
= attr
->GetBackgroundColour();
9031 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9033 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9034 wxColour colour
= attr
->GetTextColour();
9039 wxFont
wxGrid::GetCellFont( int row
, int col
)
9041 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9042 wxFont font
= attr
->GetFont();
9047 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9049 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9050 attr
->GetAlignment(horiz
, vert
);
9054 bool wxGrid::GetCellOverflow( int row
, int col
)
9056 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9057 bool allow
= attr
->GetOverflow();
9062 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9064 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9065 attr
->GetSize( num_rows
, num_cols
);
9069 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9071 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9072 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9078 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9080 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9081 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9087 bool wxGrid::IsReadOnly(int row
, int col
) const
9089 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9090 bool isReadOnly
= attr
->IsReadOnly();
9095 // ----------------------------------------------------------------------------
9096 // attribute support: cache, automatic provider creation, ...
9097 // ----------------------------------------------------------------------------
9099 bool wxGrid::CanHaveAttributes()
9106 return m_table
->CanHaveAttributes();
9109 void wxGrid::ClearAttrCache()
9111 if ( m_attrCache
.row
!= -1 )
9113 wxSafeDecRef(m_attrCache
.attr
);
9114 m_attrCache
.attr
= NULL
;
9115 m_attrCache
.row
= -1;
9119 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9123 wxGrid
*self
= (wxGrid
*)this; // const_cast
9125 self
->ClearAttrCache();
9126 self
->m_attrCache
.row
= row
;
9127 self
->m_attrCache
.col
= col
;
9128 self
->m_attrCache
.attr
= attr
;
9133 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9135 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9137 *attr
= m_attrCache
.attr
;
9138 wxSafeIncRef(m_attrCache
.attr
);
9140 #ifdef DEBUG_ATTR_CACHE
9141 gs_nAttrCacheHits
++;
9148 #ifdef DEBUG_ATTR_CACHE
9149 gs_nAttrCacheMisses
++;
9155 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9157 wxGridCellAttr
*attr
= NULL
;
9158 // Additional test to avoid looking at the cache e.g. for
9159 // wxNoCellCoords, as this will confuse memory management.
9162 if ( !LookupAttr(row
, col
, &attr
) )
9164 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9165 : (wxGridCellAttr
*)NULL
;
9166 CacheAttr(row
, col
, attr
);
9171 attr
->SetDefAttr(m_defaultCellAttr
);
9175 attr
= m_defaultCellAttr
;
9182 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9184 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9185 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9187 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9188 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9190 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9193 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9195 // artificially inc the ref count to match DecRef() in caller
9197 m_table
->SetAttr(attr
, row
, col
);
9203 // ----------------------------------------------------------------------------
9204 // setting column attributes (wrappers around SetColAttr)
9205 // ----------------------------------------------------------------------------
9207 void wxGrid::SetColFormatBool(int col
)
9209 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9212 void wxGrid::SetColFormatNumber(int col
)
9214 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9217 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9219 wxString typeName
= wxGRID_VALUE_FLOAT
;
9220 if ( (width
!= -1) || (precision
!= -1) )
9222 typeName
<< _T(':') << width
<< _T(',') << precision
;
9225 SetColFormatCustom(col
, typeName
);
9228 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9230 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9232 attr
= new wxGridCellAttr
;
9233 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9234 attr
->SetRenderer(renderer
);
9236 SetColAttr(col
, attr
);
9240 // ----------------------------------------------------------------------------
9241 // setting cell attributes: this is forwarded to the table
9242 // ----------------------------------------------------------------------------
9244 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9246 if ( CanHaveAttributes() )
9248 m_table
->SetAttr(attr
, row
, col
);
9257 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9259 if ( CanHaveAttributes() )
9261 m_table
->SetRowAttr(attr
, row
);
9270 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9272 if ( CanHaveAttributes() )
9274 m_table
->SetColAttr(attr
, col
);
9283 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9285 if ( CanHaveAttributes() )
9287 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9288 attr
->SetBackgroundColour(colour
);
9293 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9295 if ( CanHaveAttributes() )
9297 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9298 attr
->SetTextColour(colour
);
9303 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9305 if ( CanHaveAttributes() )
9307 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9308 attr
->SetFont(font
);
9313 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9315 if ( CanHaveAttributes() )
9317 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9318 attr
->SetAlignment(horiz
, vert
);
9323 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9325 if ( CanHaveAttributes() )
9327 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9328 attr
->SetOverflow(allow
);
9333 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9335 if ( CanHaveAttributes() )
9337 int cell_rows
, cell_cols
;
9339 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9340 attr
->GetSize(&cell_rows
, &cell_cols
);
9341 attr
->SetSize(num_rows
, num_cols
);
9344 // Cannot set the size of a cell to 0 or negative values
9345 // While it is perfectly legal to do that, this function cannot
9346 // handle all the possibilies, do it by hand by getting the CellAttr.
9347 // You can only set the size of a cell to 1,1 or greater with this fn
9348 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9349 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9350 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9351 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9353 // if this was already a multicell then "turn off" the other cells first
9354 if ((cell_rows
> 1) || (cell_rows
> 1))
9357 for (j
=row
; j
<row
+cell_rows
; j
++)
9359 for (i
=col
; i
<col
+cell_cols
; i
++)
9361 if ((i
!= col
) || (j
!= row
))
9363 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9364 attr_stub
->SetSize( 1, 1 );
9365 attr_stub
->DecRef();
9371 // mark the cells that will be covered by this cell to
9372 // negative or zero values to point back at this cell
9373 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9376 for (j
=row
; j
<row
+num_rows
; j
++)
9378 for (i
=col
; i
<col
+num_cols
; i
++)
9380 if ((i
!= col
) || (j
!= row
))
9382 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9383 attr_stub
->SetSize( row
-j
, col
-i
);
9384 attr_stub
->DecRef();
9392 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9394 if ( CanHaveAttributes() )
9396 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9397 attr
->SetRenderer(renderer
);
9402 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9404 if ( CanHaveAttributes() )
9406 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9407 attr
->SetEditor(editor
);
9412 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9414 if ( CanHaveAttributes() )
9416 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9417 attr
->SetReadOnly(isReadOnly
);
9422 // ----------------------------------------------------------------------------
9423 // Data type registration
9424 // ----------------------------------------------------------------------------
9426 void wxGrid::RegisterDataType(const wxString
& typeName
,
9427 wxGridCellRenderer
* renderer
,
9428 wxGridCellEditor
* editor
)
9430 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9434 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9436 wxString typeName
= m_table
->GetTypeName(row
, col
);
9437 return GetDefaultEditorForType(typeName
);
9440 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9442 wxString typeName
= m_table
->GetTypeName(row
, col
);
9443 return GetDefaultRendererForType(typeName
);
9447 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9449 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9450 if ( index
== wxNOT_FOUND
)
9452 wxFAIL_MSG(wxT("Unknown data type name"));
9457 return m_typeRegistry
->GetEditor(index
);
9461 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9463 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9464 if ( index
== wxNOT_FOUND
)
9466 wxFAIL_MSG(wxT("Unknown data type name"));
9471 return m_typeRegistry
->GetRenderer(index
);
9475 // ----------------------------------------------------------------------------
9477 // ----------------------------------------------------------------------------
9479 void wxGrid::EnableDragRowSize( bool enable
)
9481 m_canDragRowSize
= enable
;
9485 void wxGrid::EnableDragColSize( bool enable
)
9487 m_canDragColSize
= enable
;
9490 void wxGrid::EnableDragGridSize( bool enable
)
9492 m_canDragGridSize
= enable
;
9495 void wxGrid::EnableDragCell( bool enable
)
9497 m_canDragCell
= enable
;
9500 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9502 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9504 if ( resizeExistingRows
)
9506 // since we are resizing all rows to the default row size,
9507 // we can simply clear the row heights and row bottoms
9508 // arrays (which also allows us to take advantage of
9509 // some speed optimisations)
9510 m_rowHeights
.Empty();
9511 m_rowBottoms
.Empty();
9512 if ( !GetBatchCount() )
9517 void wxGrid::SetRowSize( int row
, int height
)
9519 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9521 // See comment in SetColSize
9522 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9524 if ( m_rowHeights
.IsEmpty() )
9526 // need to really create the array
9530 int h
= wxMax( 0, height
);
9531 int diff
= h
- m_rowHeights
[row
];
9533 m_rowHeights
[row
] = h
;
9535 for ( i
= row
; i
< m_numRows
; i
++ )
9537 m_rowBottoms
[i
] += diff
;
9539 if ( !GetBatchCount() )
9543 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9545 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9547 if ( resizeExistingCols
)
9549 // since we are resizing all columns to the default column size,
9550 // we can simply clear the col widths and col rights
9551 // arrays (which also allows us to take advantage of
9552 // some speed optimisations)
9553 m_colWidths
.Empty();
9554 m_colRights
.Empty();
9555 if ( !GetBatchCount() )
9560 void wxGrid::SetColSize( int col
, int width
)
9562 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9564 // should we check that it's bigger than GetColMinimalWidth(col) here?
9566 // No, because it is reasonable to assume the library user know's
9567 // what he is doing. However whe should test against the weaker
9568 // constariant of minimalAcceptableWidth, as this breaks rendering
9570 // This test then fixes sf.net bug #645734
9572 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9574 if ( m_colWidths
.IsEmpty() )
9576 // need to really create the array
9580 // if < 0 calc new width from label
9584 wxArrayString lines
;
9585 wxClientDC
dc(m_colLabelWin
);
9586 dc
.SetFont(GetLabelFont());
9587 StringToLines(GetColLabelValue(col
), lines
);
9588 GetTextBoxSize(dc
, lines
, &w
, &h
);
9591 int w
= wxMax( 0, width
);
9592 int diff
= w
- m_colWidths
[col
];
9593 m_colWidths
[col
] = w
;
9596 for ( i
= col
; i
< m_numCols
; i
++ )
9598 m_colRights
[i
] += diff
;
9600 if ( !GetBatchCount() )
9605 void wxGrid::SetColMinimalWidth( int col
, int width
)
9607 if (width
> GetColMinimalAcceptableWidth()) {
9608 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9609 m_colMinWidths
[key
] = width
;
9613 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9615 if (width
> GetRowMinimalAcceptableHeight()) {
9616 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9617 m_rowMinHeights
[key
] = width
;
9621 int wxGrid::GetColMinimalWidth(int col
) const
9623 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9624 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9625 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9628 int wxGrid::GetRowMinimalHeight(int row
) const
9630 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9631 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9632 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9635 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9637 // We do allow a width of 0 since this gives us
9638 // an easy way to temporarily hidding columns.
9641 m_minAcceptableColWidth
= width
;
9644 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9646 // We do allow a height of 0 since this gives us
9647 // an easy way to temporarily hidding rows.
9650 m_minAcceptableRowHeight
= height
;
9653 int wxGrid::GetColMinimalAcceptableWidth() const
9655 return m_minAcceptableColWidth
;
9658 int wxGrid::GetRowMinimalAcceptableHeight() const
9660 return m_minAcceptableRowHeight
;
9663 // ----------------------------------------------------------------------------
9665 // ----------------------------------------------------------------------------
9667 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9669 wxClientDC
dc(m_gridWin
);
9671 //Cancel editting of cell
9672 HideCellEditControl();
9673 SaveEditControlValue();
9675 // init both of them to avoid compiler warnings, even if weo nly need one
9683 wxCoord extent
, extentMax
= 0;
9684 int max
= column
? m_numRows
: m_numCols
;
9685 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9692 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9693 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9696 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9697 extent
= column
? size
.x
: size
.y
;
9698 if ( extent
> extentMax
)
9709 // now also compare with the column label extent
9711 dc
.SetFont( GetLabelFont() );
9715 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9716 if( GetColLabelTextOrientation() == wxVERTICAL
)
9720 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9722 extent
= column
? w
: h
;
9723 if ( extent
> extentMax
)
9730 // empty column - give default extent (notice that if extentMax is less
9731 // than default extent but != 0, it's ok)
9732 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9738 // leave some space around text
9749 SetColSize(col
, extentMax
);
9750 if ( !GetBatchCount() )
9753 m_gridWin
->GetClientSize( &cw
, &ch
);
9754 wxRect
rect ( CellToRect( 0, col
) );
9756 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9757 rect
.width
= cw
- rect
.x
;
9758 rect
.height
= m_colLabelHeight
;
9759 m_colLabelWin
->Refresh( true, &rect
);
9764 SetRowSize(row
, extentMax
);
9765 if ( !GetBatchCount() )
9768 m_gridWin
->GetClientSize( &cw
, &ch
);
9769 wxRect
rect ( CellToRect( row
, 0 ) );
9771 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9772 rect
.width
= m_rowLabelWidth
;
9773 rect
.height
= ch
- rect
.y
;
9774 m_rowLabelWin
->Refresh( true, &rect
);
9780 SetColMinimalWidth(col
, extentMax
);
9782 SetRowMinimalHeight(row
, extentMax
);
9786 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9788 int width
= m_rowLabelWidth
;
9793 for ( int col
= 0; col
< m_numCols
; col
++ )
9797 AutoSizeColumn(col
, setAsMin
);
9800 width
+= GetColWidth(col
);
9809 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9811 int height
= m_colLabelHeight
;
9816 for ( int row
= 0; row
< m_numRows
; row
++ )
9820 AutoSizeRow(row
, setAsMin
);
9823 height
+= GetRowHeight(row
);
9832 void wxGrid::AutoSize()
9836 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9838 // round up the size to a multiple of scroll step - this ensures that we
9839 // won't get the scrollbars if we're sized exactly to this width
9840 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9842 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9843 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9845 // distribute the extra space between the columns/rows to avoid having
9846 // extra white space
9848 // Remove the extra m_extraWidth + 1 added above
9849 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9850 if ( diff
&& m_numCols
)
9852 // try to resize the columns uniformly
9853 wxCoord diffPerCol
= diff
/ m_numCols
;
9856 for ( int col
= 0; col
< m_numCols
; col
++ )
9858 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9862 // add remaining amount to the last columns
9863 diff
-= diffPerCol
* m_numCols
;
9866 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9868 SetColSize(col
, GetColWidth(col
) + 1);
9874 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9875 if ( diff
&& m_numRows
)
9877 // try to resize the columns uniformly
9878 wxCoord diffPerRow
= diff
/ m_numRows
;
9881 for ( int row
= 0; row
< m_numRows
; row
++ )
9883 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9887 // add remaining amount to the last rows
9888 diff
-= diffPerRow
* m_numRows
;
9891 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9893 SetRowSize(row
, GetRowHeight(row
) + 1);
9900 SetClientSize(sizeFit
);
9903 void wxGrid::AutoSizeRowLabelSize( int row
)
9905 wxArrayString lines
;
9908 // Hide the edit control, so it
9909 // won't interfer with drag-shrinking.
9910 if( IsCellEditControlShown() )
9912 HideCellEditControl();
9913 SaveEditControlValue();
9916 // autosize row height depending on label text
9917 StringToLines( GetRowLabelValue( row
), lines
);
9918 wxClientDC
dc( m_rowLabelWin
);
9919 GetTextBoxSize( dc
, lines
, &w
, &h
);
9920 if( h
< m_defaultRowHeight
)
9921 h
= m_defaultRowHeight
;
9926 void wxGrid::AutoSizeColLabelSize( int col
)
9928 wxArrayString lines
;
9931 // Hide the edit control, so it
9932 // won't interfer with drag-shrinking.
9933 if( IsCellEditControlShown() )
9935 HideCellEditControl();
9936 SaveEditControlValue();
9939 // autosize column width depending on label text
9940 StringToLines( GetColLabelValue( col
), lines
);
9941 wxClientDC
dc( m_colLabelWin
);
9942 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9943 GetTextBoxSize( dc
, lines
, &w
, &h
);
9945 GetTextBoxSize( dc
, lines
, &h
, &w
);
9946 if( w
< m_defaultColWidth
)
9947 w
= m_defaultColWidth
;
9952 wxSize
wxGrid::DoGetBestSize() const
9954 // don't set sizes, only calculate them
9955 wxGrid
*self
= (wxGrid
*)this; // const_cast
9958 width
= self
->SetOrCalcColumnSizes(true);
9959 height
= self
->SetOrCalcRowSizes(true);
9961 if (!width
) width
=100;
9962 if (!height
) height
=80;
9964 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9965 // of the scrollbars, is there any magic incantaion for that?
9967 GetScrollPixelsPerUnit(&xpu
, &ypu
);
9969 width
+= 1 + xpu
- (width
% xpu
);
9971 height
+= 1 + ypu
- (height
% ypu
);
9973 // limit to 1/4 of the screen size
9974 int maxwidth
, maxheight
;
9975 wxDisplaySize( & maxwidth
, & maxheight
);
9978 if ( width
> maxwidth
) width
= maxwidth
;
9979 if ( height
> maxheight
) height
= maxheight
;
9982 wxSize
best(width
, height
);
9983 // NOTE: This size should be cached, but first we need to add calls to
9984 // InvalidateBestSize everywhere that could change the results of this
9986 // CacheBestSize(size);
9996 wxPen
& wxGrid::GetDividerPen() const
10001 // ----------------------------------------------------------------------------
10002 // cell value accessor functions
10003 // ----------------------------------------------------------------------------
10005 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10009 m_table
->SetValue( row
, col
, s
);
10010 if ( !GetBatchCount() )
10013 wxRect
rect( CellToRect( row
, col
) );
10015 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10016 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10017 m_gridWin
->Refresh( false, &rect
);
10020 if ( m_currentCellCoords
.GetRow() == row
&&
10021 m_currentCellCoords
.GetCol() == col
&&
10022 IsCellEditControlShown())
10023 // Note: If we are using IsCellEditControlEnabled,
10024 // this interacts badly with calling SetCellValue from
10025 // an EVT_GRID_CELL_CHANGE handler.
10027 HideCellEditControl();
10028 ShowCellEditControl(); // will reread data from table
10035 // ------ Block, row and col selection
10038 void wxGrid::SelectRow( int row
, bool addToSelected
)
10040 if ( IsSelection() && !addToSelected
)
10044 m_selection
->SelectRow( row
, false, addToSelected
);
10048 void wxGrid::SelectCol( int col
, bool addToSelected
)
10050 if ( IsSelection() && !addToSelected
)
10054 m_selection
->SelectCol( col
, false, addToSelected
);
10058 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10059 bool addToSelected
)
10061 if ( IsSelection() && !addToSelected
)
10065 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10066 false, addToSelected
);
10070 void wxGrid::SelectAll()
10072 if ( m_numRows
> 0 && m_numCols
> 0 )
10075 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10080 // ------ Cell, row and col deselection
10083 void wxGrid::DeselectRow( int row
)
10085 if ( !m_selection
)
10088 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10090 if ( m_selection
->IsInSelection(row
, 0 ) )
10091 m_selection
->ToggleCellSelection( row
, 0);
10095 int nCols
= GetNumberCols();
10096 for ( int i
= 0; i
< nCols
; i
++ )
10098 if ( m_selection
->IsInSelection(row
, i
) )
10099 m_selection
->ToggleCellSelection( row
, i
);
10104 void wxGrid::DeselectCol( int col
)
10106 if ( !m_selection
)
10109 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10111 if ( m_selection
->IsInSelection(0, col
) )
10112 m_selection
->ToggleCellSelection( 0, col
);
10116 int nRows
= GetNumberRows();
10117 for ( int i
= 0; i
< nRows
; i
++ )
10119 if ( m_selection
->IsInSelection(i
, col
) )
10120 m_selection
->ToggleCellSelection(i
, col
);
10125 void wxGrid::DeselectCell( int row
, int col
)
10127 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10128 m_selection
->ToggleCellSelection(row
, col
);
10131 bool wxGrid::IsSelection()
10133 return ( m_selection
&& (m_selection
->IsSelection() ||
10134 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10135 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10138 bool wxGrid::IsInSelection( int row
, int col
) const
10140 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10141 ( row
>= m_selectingTopLeft
.GetRow() &&
10142 col
>= m_selectingTopLeft
.GetCol() &&
10143 row
<= m_selectingBottomRight
.GetRow() &&
10144 col
<= m_selectingBottomRight
.GetCol() )) );
10147 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10149 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10150 return m_selection
->m_cellSelection
;
10152 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10154 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10155 return m_selection
->m_blockSelectionTopLeft
;
10157 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10159 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10160 return m_selection
->m_blockSelectionBottomRight
;
10162 wxArrayInt
wxGrid::GetSelectedRows() const
10164 if (!m_selection
) { wxArrayInt a
; return a
; }
10165 return m_selection
->m_rowSelection
;
10167 wxArrayInt
wxGrid::GetSelectedCols() const
10169 if (!m_selection
) { wxArrayInt a
; return a
; }
10170 return m_selection
->m_colSelection
;
10174 void wxGrid::ClearSelection()
10176 m_selectingTopLeft
= wxGridNoCellCoords
;
10177 m_selectingBottomRight
= wxGridNoCellCoords
;
10179 m_selection
->ClearSelection();
10183 // This function returns the rectangle that encloses the given block
10184 // in device coords clipped to the client size of the grid window.
10186 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10187 const wxGridCellCoords
&bottomRight
)
10189 wxRect
rect( wxGridNoCellRect
);
10192 cellRect
= CellToRect( topLeft
);
10193 if ( cellRect
!= wxGridNoCellRect
)
10199 rect
= wxRect(0,0,0,0);
10202 cellRect
= CellToRect( bottomRight
);
10203 if ( cellRect
!= wxGridNoCellRect
)
10209 return wxGridNoCellRect
;
10213 int left
= rect
.GetLeft();
10214 int top
= rect
.GetTop();
10215 int right
= rect
.GetRight();
10216 int bottom
= rect
.GetBottom();
10218 int leftCol
= topLeft
.GetCol();
10219 int topRow
= topLeft
.GetRow();
10220 int rightCol
= bottomRight
.GetCol();
10221 int bottomRow
= bottomRight
.GetRow();
10239 topRow
= bottomRow
;
10244 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10246 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10248 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10250 cellRect
= CellToRect( j
, i
);
10252 if (cellRect
.x
< left
)
10254 if (cellRect
.y
< top
)
10256 if (cellRect
.x
+ cellRect
.width
> right
)
10257 right
= cellRect
.x
+ cellRect
.width
;
10258 if (cellRect
.y
+ cellRect
.height
> bottom
)
10259 bottom
= cellRect
.y
+ cellRect
.height
;
10261 else i
= rightCol
; // jump over inner cells.
10265 // convert to scrolled coords
10267 CalcScrolledPosition( left
, top
, &left
, &top
);
10268 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10271 m_gridWin
->GetClientSize( &cw
, &ch
);
10273 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10274 return wxRect(0,0,0,0);
10276 rect
.SetLeft( wxMax(0, left
) );
10277 rect
.SetTop( wxMax(0, top
) );
10278 rect
.SetRight( wxMin(cw
, right
) );
10279 rect
.SetBottom( wxMin(ch
, bottom
) );
10285 // ------ Grid event classes
10288 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10290 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10291 int row
, int col
, int x
, int y
, bool sel
,
10292 bool control
, bool shift
, bool alt
, bool meta
)
10293 : wxNotifyEvent( type
, id
)
10300 m_control
= control
;
10305 SetEventObject(obj
);
10309 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10311 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10312 int rowOrCol
, int x
, int y
,
10313 bool control
, bool shift
, bool alt
, bool meta
)
10314 : wxNotifyEvent( type
, id
)
10316 m_rowOrCol
= rowOrCol
;
10319 m_control
= control
;
10324 SetEventObject(obj
);
10328 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10330 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10331 const wxGridCellCoords
& topLeft
,
10332 const wxGridCellCoords
& bottomRight
,
10333 bool sel
, bool control
,
10334 bool shift
, bool alt
, bool meta
)
10335 : wxNotifyEvent( type
, id
)
10337 m_topLeft
= topLeft
;
10338 m_bottomRight
= bottomRight
;
10340 m_control
= control
;
10345 SetEventObject(obj
);
10349 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10351 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10352 wxObject
* obj
, int row
,
10353 int col
, wxControl
* ctrl
)
10354 : wxCommandEvent(type
, id
)
10356 SetEventObject(obj
);
10362 #endif // wxUSE_GRID