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::GetSystemColour(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::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1648 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1651 dc
.SetFont( attr
.GetFont() );
1654 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1656 const wxString
& text
)
1658 wxCoord x
= 0, y
= 0, max_x
= 0;
1659 dc
.SetFont(attr
.GetFont());
1660 wxStringTokenizer
tk(text
, _T('\n'));
1661 while ( tk
.HasMoreTokens() )
1663 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1664 max_x
= wxMax(max_x
, x
);
1667 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1669 return wxSize(max_x
, y
);
1672 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1673 wxGridCellAttr
& attr
,
1677 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1680 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1681 wxGridCellAttr
& attr
,
1683 const wxRect
& rectCell
,
1687 wxRect rect
= rectCell
;
1690 // erase only this cells background, overflow cells should have been erased
1691 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1694 attr
.GetAlignment(&hAlign
, &vAlign
);
1696 int overflowCols
= 0;
1698 if (attr
.GetOverflow())
1700 int cols
= grid
.GetNumberCols();
1701 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1702 int cell_rows
, cell_cols
;
1703 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1704 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1706 int i
, c_cols
, c_rows
;
1707 for (i
= col
+cell_cols
; i
< cols
; i
++)
1709 bool is_empty
= true;
1710 for (int j
=row
; j
<row
+cell_rows
; j
++)
1712 // check w/ anchor cell for multicell block
1713 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1714 if (c_rows
> 0) c_rows
= 0;
1715 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1722 rect
.width
+= grid
.GetColSize(i
);
1728 if (rect
.width
>= best_width
) break;
1730 overflowCols
= i
- col
- cell_cols
+ 1;
1731 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1734 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1736 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1738 clip
.x
+= rectCell
.width
;
1739 // draw each overflow cell individually
1740 int col_end
= col
+cell_cols
+overflowCols
;
1741 if (col_end
>= grid
.GetNumberCols())
1742 col_end
= grid
.GetNumberCols() - 1;
1743 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1745 clip
.width
= grid
.GetColSize(i
) - 1;
1746 dc
.DestroyClippingRegion();
1747 dc
.SetClippingRegion(clip
);
1749 SetTextColoursAndFont(grid
, attr
, dc
,
1750 grid
.IsInSelection(row
,i
));
1752 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1753 rect
, hAlign
, vAlign
);
1754 clip
.x
+= grid
.GetColSize(i
) - 1;
1760 dc
.DestroyClippingRegion();
1764 // now we only have to draw the text
1765 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1767 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1768 rect
, hAlign
, vAlign
);
1771 // ----------------------------------------------------------------------------
1772 // wxGridCellNumberRenderer
1773 // ----------------------------------------------------------------------------
1775 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1777 wxGridTableBase
*table
= grid
.GetTable();
1779 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1781 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1785 text
= table
->GetValue(row
, col
);
1791 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1792 wxGridCellAttr
& attr
,
1794 const wxRect
& rectCell
,
1798 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1800 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1802 // draw the text right aligned by default
1804 attr
.GetAlignment(&hAlign
, &vAlign
);
1805 hAlign
= wxALIGN_RIGHT
;
1807 wxRect rect
= rectCell
;
1810 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1813 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1814 wxGridCellAttr
& attr
,
1818 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1821 // ----------------------------------------------------------------------------
1822 // wxGridCellFloatRenderer
1823 // ----------------------------------------------------------------------------
1825 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1828 SetPrecision(precision
);
1831 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1833 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1834 renderer
->m_width
= m_width
;
1835 renderer
->m_precision
= m_precision
;
1836 renderer
->m_format
= m_format
;
1841 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1843 wxGridTableBase
*table
= grid
.GetTable();
1848 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1850 val
= table
->GetValueAsDouble(row
, col
);
1855 text
= table
->GetValue(row
, col
);
1856 hasDouble
= text
.ToDouble(&val
);
1863 if ( m_width
== -1 )
1865 if ( m_precision
== -1 )
1867 // default width/precision
1868 m_format
= _T("%f");
1872 m_format
.Printf(_T("%%.%df"), m_precision
);
1875 else if ( m_precision
== -1 )
1877 // default precision
1878 m_format
.Printf(_T("%%%d.f"), m_width
);
1882 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1886 text
.Printf(m_format
, val
);
1889 //else: text already contains the string
1894 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1895 wxGridCellAttr
& attr
,
1897 const wxRect
& rectCell
,
1901 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1903 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1905 // draw the text right aligned by default
1907 attr
.GetAlignment(&hAlign
, &vAlign
);
1908 hAlign
= wxALIGN_RIGHT
;
1910 wxRect rect
= rectCell
;
1913 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1916 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1917 wxGridCellAttr
& attr
,
1921 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1924 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1928 // reset to defaults
1934 wxString tmp
= params
.BeforeFirst(_T(','));
1938 if ( tmp
.ToLong(&width
) )
1940 SetWidth((int)width
);
1944 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1948 tmp
= params
.AfterFirst(_T(','));
1952 if ( tmp
.ToLong(&precision
) )
1954 SetPrecision((int)precision
);
1958 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1966 // ----------------------------------------------------------------------------
1967 // wxGridCellBoolRenderer
1968 // ----------------------------------------------------------------------------
1970 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1972 // FIXME these checkbox size calculations are really ugly...
1974 // between checkmark and box
1975 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1977 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1978 wxGridCellAttr
& WXUNUSED(attr
),
1983 // compute it only once (no locks for MT safeness in GUI thread...)
1984 if ( !ms_sizeCheckMark
.x
)
1986 // get checkbox size
1987 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
1988 wxSize size
= checkbox
->GetBestSize();
1989 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1991 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1992 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1993 checkSize
-= size
.y
/ 2;
1998 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2001 return ms_sizeCheckMark
;
2004 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2005 wxGridCellAttr
& attr
,
2011 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2013 // draw a check mark in the centre (ignoring alignment - TODO)
2014 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2016 // don't draw outside the cell
2017 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2018 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2020 // and even leave (at least) 1 pixel margin
2021 size
.x
= size
.y
= minSize
- 2;
2024 // draw a border around checkmark
2026 attr
.GetAlignment(& hAlign
, &vAlign
);
2029 if (hAlign
== wxALIGN_CENTRE
)
2031 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2032 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2033 rectBorder
.width
= size
.x
;
2034 rectBorder
.height
= size
.y
;
2036 else if (hAlign
== wxALIGN_LEFT
)
2038 rectBorder
.x
= rect
.x
+ 2;
2039 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2040 rectBorder
.width
= size
.x
;
2041 rectBorder
.height
= size
.y
;
2043 else if (hAlign
== wxALIGN_RIGHT
)
2045 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2046 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2047 rectBorder
.width
= size
.x
;
2048 rectBorder
.height
= size
.y
;
2052 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2053 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2056 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2057 value
= !( !cellval
|| (cellval
== wxT("0")) );
2062 wxRect rectMark
= rectBorder
;
2064 // MSW DrawCheckMark() is weird (and should probably be changed...)
2065 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2069 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2072 dc
.SetTextForeground(attr
.GetTextColour());
2073 dc
.DrawCheckMark(rectMark
);
2076 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2077 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2078 dc
.DrawRectangle(rectBorder
);
2081 // ----------------------------------------------------------------------------
2083 // ----------------------------------------------------------------------------
2085 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2089 m_isReadOnly
= Unset
;
2094 m_attrkind
= wxGridCellAttr::Cell
;
2096 m_sizeRows
= m_sizeCols
= 1;
2097 m_overflow
= UnsetOverflow
;
2099 SetDefAttr(attrDefault
);
2102 wxGridCellAttr
*wxGridCellAttr::Clone() const
2104 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2106 if ( HasTextColour() )
2107 attr
->SetTextColour(GetTextColour());
2108 if ( HasBackgroundColour() )
2109 attr
->SetBackgroundColour(GetBackgroundColour());
2111 attr
->SetFont(GetFont());
2112 if ( HasAlignment() )
2113 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2115 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2119 attr
->SetRenderer(m_renderer
);
2120 m_renderer
->IncRef();
2124 attr
->SetEditor(m_editor
);
2129 attr
->SetReadOnly();
2131 attr
->SetKind( m_attrkind
);
2136 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2138 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2139 SetTextColour(mergefrom
->GetTextColour());
2140 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2141 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2142 if ( !HasFont() && mergefrom
->HasFont() )
2143 SetFont(mergefrom
->GetFont());
2144 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2146 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2147 SetAlignment(hAlign
, vAlign
);
2150 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2152 // Directly access member functions as GetRender/Editor don't just return
2153 // m_renderer/m_editor
2155 // Maybe add support for merge of Render and Editor?
2156 if (!HasRenderer() && mergefrom
->HasRenderer() )
2158 m_renderer
= mergefrom
->m_renderer
;
2159 m_renderer
->IncRef();
2161 if ( !HasEditor() && mergefrom
->HasEditor() )
2163 m_editor
= mergefrom
->m_editor
;
2166 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2167 SetReadOnly(mergefrom
->IsReadOnly());
2169 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2170 SetOverflow(mergefrom
->GetOverflow());
2172 SetDefAttr(mergefrom
->m_defGridAttr
);
2175 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2177 // The size of a cell is normally 1,1
2179 // If this cell is larger (2,2) then this is the top left cell
2180 // the other cells that will be covered (lower right cells) must be
2181 // set to negative or zero values such that
2182 // row + num_rows of the covered cell points to the larger cell (this cell)
2183 // same goes for the col + num_cols.
2185 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2187 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2188 !((num_rows
<=0)&&(num_cols
>0)) ||
2189 !((num_rows
==0)&&(num_cols
==0))),
2190 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2192 m_sizeRows
= num_rows
;
2193 m_sizeCols
= num_cols
;
2196 const wxColour
& wxGridCellAttr::GetTextColour() const
2198 if (HasTextColour())
2202 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2204 return m_defGridAttr
->GetTextColour();
2208 wxFAIL_MSG(wxT("Missing default cell attribute"));
2209 return wxNullColour
;
2214 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2216 if (HasBackgroundColour())
2218 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2219 return m_defGridAttr
->GetBackgroundColour();
2222 wxFAIL_MSG(wxT("Missing default cell attribute"));
2223 return wxNullColour
;
2228 const wxFont
& wxGridCellAttr::GetFont() const
2232 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2233 return m_defGridAttr
->GetFont();
2236 wxFAIL_MSG(wxT("Missing default cell attribute"));
2242 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2246 if ( hAlign
) *hAlign
= m_hAlign
;
2247 if ( vAlign
) *vAlign
= m_vAlign
;
2249 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2250 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2253 wxFAIL_MSG(wxT("Missing default cell attribute"));
2257 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2259 if ( num_rows
) *num_rows
= m_sizeRows
;
2260 if ( num_cols
) *num_cols
= m_sizeCols
;
2263 // GetRenderer and GetEditor use a slightly different decision path about
2264 // which attribute to use. If a non-default attr object has one then it is
2265 // used, otherwise the default editor or renderer is fetched from the grid and
2266 // used. It should be the default for the data type of the cell. If it is
2267 // NULL (because the table has a type that the grid does not have in its
2268 // registry,) then the grid's default editor or renderer is used.
2270 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2272 wxGridCellRenderer
*renderer
;
2274 if ( m_renderer
&& this != m_defGridAttr
)
2276 // use the cells renderer if it has one
2277 renderer
= m_renderer
;
2280 else // no non default cell renderer
2282 // get default renderer for the data type
2285 // GetDefaultRendererForCell() will do IncRef() for us
2286 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2295 if (m_defGridAttr
&& this != m_defGridAttr
)
2297 // if we still don't have one then use the grid default
2298 // (no need for IncRef() here neither)
2299 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2301 else // default grid attr
2303 // use m_renderer which we had decided not to use initially
2304 renderer
= m_renderer
;
2311 // we're supposed to always find something
2312 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2317 // same as above, except for s/renderer/editor/g
2318 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2320 wxGridCellEditor
*editor
;
2322 if ( m_editor
&& this != m_defGridAttr
)
2324 // use the cells editor if it has one
2328 else // no non default cell editor
2330 // get default editor for the data type
2333 // GetDefaultEditorForCell() will do IncRef() for us
2334 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2343 if ( m_defGridAttr
&& this != m_defGridAttr
)
2345 // if we still don't have one then use the grid default
2346 // (no need for IncRef() here neither)
2347 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2349 else // default grid attr
2351 // use m_editor which we had decided not to use initially
2359 // we're supposed to always find something
2360 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2365 // ----------------------------------------------------------------------------
2366 // wxGridCellAttrData
2367 // ----------------------------------------------------------------------------
2369 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2371 int n
= FindIndex(row
, col
);
2372 if ( n
== wxNOT_FOUND
)
2374 // add the attribute
2375 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2379 // free the old attribute
2380 m_attrs
[(size_t)n
].attr
->DecRef();
2384 // change the attribute
2385 m_attrs
[(size_t)n
].attr
= attr
;
2389 // remove this attribute
2390 m_attrs
.RemoveAt((size_t)n
);
2395 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2397 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2399 int n
= FindIndex(row
, col
);
2400 if ( n
!= wxNOT_FOUND
)
2402 attr
= m_attrs
[(size_t)n
].attr
;
2409 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2411 size_t count
= m_attrs
.GetCount();
2412 for ( size_t n
= 0; n
< count
; n
++ )
2414 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2415 wxCoord row
= coords
.GetRow();
2416 if ((size_t)row
>= pos
)
2420 // If rows inserted, include row counter where necessary
2421 coords
.SetRow(row
+ numRows
);
2423 else if (numRows
< 0)
2425 // If rows deleted ...
2426 if ((size_t)row
>= pos
- numRows
)
2428 // ...either decrement row counter (if row still exists)...
2429 coords
.SetRow(row
+ numRows
);
2433 // ...or remove the attribute
2434 m_attrs
.RemoveAt((size_t)n
);
2442 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2444 size_t count
= m_attrs
.GetCount();
2445 for ( size_t n
= 0; n
< count
; n
++ )
2447 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2448 wxCoord col
= coords
.GetCol();
2449 if ( (size_t)col
>= pos
)
2453 // If rows inserted, include row counter where necessary
2454 coords
.SetCol(col
+ numCols
);
2456 else if (numCols
< 0)
2458 // If rows deleted ...
2459 if ((size_t)col
>= pos
- numCols
)
2461 // ...either decrement row counter (if row still exists)...
2462 coords
.SetCol(col
+ numCols
);
2466 // ...or remove the attribute
2467 m_attrs
.RemoveAt((size_t)n
);
2475 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2477 size_t count
= m_attrs
.GetCount();
2478 for ( size_t n
= 0; n
< count
; n
++ )
2480 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2481 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2490 // ----------------------------------------------------------------------------
2491 // wxGridRowOrColAttrData
2492 // ----------------------------------------------------------------------------
2494 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2496 size_t count
= m_attrs
.Count();
2497 for ( size_t n
= 0; n
< count
; n
++ )
2499 m_attrs
[n
]->DecRef();
2503 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2505 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2507 int n
= m_rowsOrCols
.Index(rowOrCol
);
2508 if ( n
!= wxNOT_FOUND
)
2510 attr
= m_attrs
[(size_t)n
];
2517 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2519 int i
= m_rowsOrCols
.Index(rowOrCol
);
2520 if ( i
== wxNOT_FOUND
)
2522 // add the attribute
2523 m_rowsOrCols
.Add(rowOrCol
);
2528 size_t n
= (size_t)i
;
2531 // change the attribute
2532 m_attrs
[n
]->DecRef();
2537 // remove this attribute
2538 m_attrs
[n
]->DecRef();
2539 m_rowsOrCols
.RemoveAt(n
);
2540 m_attrs
.RemoveAt(n
);
2545 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2547 size_t count
= m_attrs
.GetCount();
2548 for ( size_t n
= 0; n
< count
; n
++ )
2550 int & rowOrCol
= m_rowsOrCols
[n
];
2551 if ( (size_t)rowOrCol
>= pos
)
2553 if ( numRowsOrCols
> 0 )
2555 // If rows inserted, include row counter where necessary
2556 rowOrCol
+= numRowsOrCols
;
2558 else if ( numRowsOrCols
< 0)
2560 // If rows deleted, either decrement row counter (if row still exists)
2561 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2562 rowOrCol
+= numRowsOrCols
;
2565 m_rowsOrCols
.RemoveAt((size_t)n
);
2566 m_attrs
.RemoveAt((size_t)n
);
2574 // ----------------------------------------------------------------------------
2575 // wxGridCellAttrProvider
2576 // ----------------------------------------------------------------------------
2578 wxGridCellAttrProvider::wxGridCellAttrProvider()
2580 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2583 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2588 void wxGridCellAttrProvider::InitData()
2590 m_data
= new wxGridCellAttrProviderData
;
2593 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2594 wxGridCellAttr::wxAttrKind kind
) const
2596 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2601 case (wxGridCellAttr::Any
):
2602 //Get cached merge attributes.
2603 // Currenlty not used as no cache implemented as not mutiable
2604 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2607 //Basicaly implement old version.
2608 //Also check merge cache, so we don't have to re-merge every time..
2609 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2610 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2611 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2613 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2614 // Two or more are non NULL
2615 attr
= new wxGridCellAttr
;
2616 attr
->SetKind(wxGridCellAttr::Merged
);
2620 attr
->MergeWith(attrcell
);
2624 attr
->MergeWith(attrcol
);
2628 attr
->MergeWith(attrrow
);
2631 //store merge attr if cache implemented
2633 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2637 // one or none is non null return it or null.
2638 if(attrrow
) attr
= attrrow
;
2654 case (wxGridCellAttr::Cell
):
2655 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2657 case (wxGridCellAttr::Col
):
2658 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2660 case (wxGridCellAttr::Row
):
2661 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2665 // (wxGridCellAttr::Default):
2666 // (wxGridCellAttr::Merged):
2673 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2679 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2682 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2687 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2690 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2695 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2698 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2702 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2704 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2708 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2712 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2714 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2718 // ----------------------------------------------------------------------------
2719 // wxGridTypeRegistry
2720 // ----------------------------------------------------------------------------
2722 wxGridTypeRegistry::~wxGridTypeRegistry()
2724 size_t count
= m_typeinfo
.Count();
2725 for ( size_t i
= 0; i
< count
; i
++ )
2726 delete m_typeinfo
[i
];
2730 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2731 wxGridCellRenderer
* renderer
,
2732 wxGridCellEditor
* editor
)
2734 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2736 // is it already registered?
2737 int loc
= FindRegisteredDataType(typeName
);
2738 if ( loc
!= wxNOT_FOUND
)
2740 delete m_typeinfo
[loc
];
2741 m_typeinfo
[loc
] = info
;
2745 m_typeinfo
.Add(info
);
2749 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2751 size_t count
= m_typeinfo
.GetCount();
2752 for ( size_t i
= 0; i
< count
; i
++ )
2754 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2763 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2765 int index
= FindRegisteredDataType(typeName
);
2766 if ( index
== wxNOT_FOUND
)
2768 // check whether this is one of the standard ones, in which case
2769 // register it "on the fly"
2771 if ( typeName
== wxGRID_VALUE_STRING
)
2773 RegisterDataType(wxGRID_VALUE_STRING
,
2774 new wxGridCellStringRenderer
,
2775 new wxGridCellTextEditor
);
2777 #endif // wxUSE_TEXTCTRL
2779 if ( typeName
== wxGRID_VALUE_BOOL
)
2781 RegisterDataType(wxGRID_VALUE_BOOL
,
2782 new wxGridCellBoolRenderer
,
2783 new wxGridCellBoolEditor
);
2785 #endif // wxUSE_CHECKBOX
2787 if ( typeName
== wxGRID_VALUE_NUMBER
)
2789 RegisterDataType(wxGRID_VALUE_NUMBER
,
2790 new wxGridCellNumberRenderer
,
2791 new wxGridCellNumberEditor
);
2793 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2795 RegisterDataType(wxGRID_VALUE_FLOAT
,
2796 new wxGridCellFloatRenderer
,
2797 new wxGridCellFloatEditor
);
2799 #endif // wxUSE_TEXTCTRL
2801 if ( typeName
== wxGRID_VALUE_CHOICE
)
2803 RegisterDataType(wxGRID_VALUE_CHOICE
,
2804 new wxGridCellStringRenderer
,
2805 new wxGridCellChoiceEditor
);
2807 #endif // wxUSE_COMBOBOX
2812 // we get here only if just added the entry for this type, so return
2814 index
= m_typeinfo
.GetCount() - 1;
2820 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2822 int index
= FindDataType(typeName
);
2823 if ( index
== wxNOT_FOUND
)
2825 // the first part of the typename is the "real" type, anything after ':'
2826 // are the parameters for the renderer
2827 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2828 if ( index
== wxNOT_FOUND
)
2833 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2834 wxGridCellRenderer
*rendererOld
= renderer
;
2835 renderer
= renderer
->Clone();
2836 rendererOld
->DecRef();
2838 wxGridCellEditor
*editor
= GetEditor(index
);
2839 wxGridCellEditor
*editorOld
= editor
;
2840 editor
= editor
->Clone();
2841 editorOld
->DecRef();
2843 // do it even if there are no parameters to reset them to defaults
2844 wxString params
= typeName
.AfterFirst(_T(':'));
2845 renderer
->SetParameters(params
);
2846 editor
->SetParameters(params
);
2848 // register the new typename
2849 RegisterDataType(typeName
, renderer
, editor
);
2851 // we just registered it, it's the last one
2852 index
= m_typeinfo
.GetCount() - 1;
2858 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2860 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2866 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2868 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2874 // ----------------------------------------------------------------------------
2876 // ----------------------------------------------------------------------------
2878 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2881 wxGridTableBase::wxGridTableBase()
2883 m_view
= (wxGrid
*) NULL
;
2884 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2887 wxGridTableBase::~wxGridTableBase()
2889 delete m_attrProvider
;
2892 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2894 delete m_attrProvider
;
2895 m_attrProvider
= attrProvider
;
2898 bool wxGridTableBase::CanHaveAttributes()
2900 if ( ! GetAttrProvider() )
2902 // use the default attr provider by default
2903 SetAttrProvider(new wxGridCellAttrProvider
);
2908 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2910 if ( m_attrProvider
)
2911 return m_attrProvider
->GetAttr(row
, col
, kind
);
2913 return (wxGridCellAttr
*)NULL
;
2916 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2918 if ( m_attrProvider
)
2920 attr
->SetKind(wxGridCellAttr::Cell
);
2921 m_attrProvider
->SetAttr(attr
, row
, col
);
2925 // as we take ownership of the pointer and don't store it, we must
2931 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2933 if ( m_attrProvider
)
2935 attr
->SetKind(wxGridCellAttr::Row
);
2936 m_attrProvider
->SetRowAttr(attr
, row
);
2940 // as we take ownership of the pointer and don't store it, we must
2946 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2948 if ( m_attrProvider
)
2950 attr
->SetKind(wxGridCellAttr::Col
);
2951 m_attrProvider
->SetColAttr(attr
, col
);
2955 // as we take ownership of the pointer and don't store it, we must
2961 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2962 size_t WXUNUSED(numRows
) )
2964 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2969 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2971 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2976 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2977 size_t WXUNUSED(numRows
) )
2979 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2984 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2985 size_t WXUNUSED(numCols
) )
2987 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2992 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2994 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2999 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3000 size_t WXUNUSED(numCols
) )
3002 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3008 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3011 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3012 // how much it makes sense to us geeks.
3016 wxString
wxGridTableBase::GetColLabelValue( int col
)
3018 // default col labels are:
3019 // cols 0 to 25 : A-Z
3020 // cols 26 to 675 : AA-ZZ
3025 for ( n
= 1; ; n
++ )
3027 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3029 if ( col
< 0 ) break;
3032 // reverse the string...
3034 for ( i
= 0; i
< n
; i
++ )
3043 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3045 return wxGRID_VALUE_STRING
;
3048 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3049 const wxString
& typeName
)
3051 return typeName
== wxGRID_VALUE_STRING
;
3054 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3056 return CanGetValueAs(row
, col
, typeName
);
3059 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3064 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3069 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3074 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3075 long WXUNUSED(value
) )
3079 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3080 double WXUNUSED(value
) )
3084 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3085 bool WXUNUSED(value
) )
3090 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3091 const wxString
& WXUNUSED(typeName
) )
3096 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3097 const wxString
& WXUNUSED(typeName
),
3098 void* WXUNUSED(value
) )
3102 //////////////////////////////////////////////////////////////////////
3104 // Message class for the grid table to send requests and notifications
3108 wxGridTableMessage::wxGridTableMessage()
3110 m_table
= (wxGridTableBase
*) NULL
;
3116 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3117 int commandInt1
, int commandInt2
)
3121 m_comInt1
= commandInt1
;
3122 m_comInt2
= commandInt2
;
3127 //////////////////////////////////////////////////////////////////////
3129 // A basic grid table for string data. An object of this class will
3130 // created by wxGrid if you don't specify an alternative table class.
3133 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3135 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3137 wxGridStringTable::wxGridStringTable()
3142 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3145 m_data
.Alloc( numRows
);
3148 sa
.Alloc( numCols
);
3149 sa
.Add( wxEmptyString
, numCols
);
3151 m_data
.Add( sa
, numRows
);
3154 wxGridStringTable::~wxGridStringTable()
3158 int wxGridStringTable::GetNumberRows()
3160 return m_data
.GetCount();
3163 int wxGridStringTable::GetNumberCols()
3165 if ( m_data
.GetCount() > 0 )
3166 return m_data
[0].GetCount();
3171 wxString
wxGridStringTable::GetValue( int row
, int col
)
3173 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3175 _T("invalid row or column index in wxGridStringTable") );
3177 return m_data
[row
][col
];
3180 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3182 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3183 _T("invalid row or column index in wxGridStringTable") );
3185 m_data
[row
][col
] = value
;
3188 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3190 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3192 _T("invalid row or column index in wxGridStringTable") );
3194 return (m_data
[row
][col
] == wxEmptyString
);
3197 void wxGridStringTable::Clear()
3200 int numRows
, numCols
;
3202 numRows
= m_data
.GetCount();
3205 numCols
= m_data
[0].GetCount();
3207 for ( row
= 0; row
< numRows
; row
++ )
3209 for ( col
= 0; col
< numCols
; col
++ )
3211 m_data
[row
][col
] = wxEmptyString
;
3218 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3220 size_t curNumRows
= m_data
.GetCount();
3221 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3222 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3224 if ( pos
>= curNumRows
)
3226 return AppendRows( numRows
);
3230 sa
.Alloc( curNumCols
);
3231 sa
.Add( wxEmptyString
, curNumCols
);
3232 m_data
.Insert( sa
, pos
, numRows
);
3235 wxGridTableMessage
msg( this,
3236 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3240 GetView()->ProcessTableMessage( msg
);
3246 bool wxGridStringTable::AppendRows( size_t numRows
)
3248 size_t curNumRows
= m_data
.GetCount();
3249 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3250 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3253 if ( curNumCols
> 0 )
3255 sa
.Alloc( curNumCols
);
3256 sa
.Add( wxEmptyString
, curNumCols
);
3259 m_data
.Add( sa
, numRows
);
3263 wxGridTableMessage
msg( this,
3264 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3267 GetView()->ProcessTableMessage( msg
);
3273 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3275 size_t curNumRows
= m_data
.GetCount();
3277 if ( pos
>= curNumRows
)
3279 wxFAIL_MSG( wxString::Format
3281 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3283 (unsigned long)numRows
,
3284 (unsigned long)curNumRows
3290 if ( numRows
> curNumRows
- pos
)
3292 numRows
= curNumRows
- pos
;
3295 if ( numRows
>= curNumRows
)
3301 m_data
.RemoveAt( pos
, numRows
);
3305 wxGridTableMessage
msg( this,
3306 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3310 GetView()->ProcessTableMessage( msg
);
3316 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3320 size_t curNumRows
= m_data
.GetCount();
3321 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3322 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3324 if ( pos
>= curNumCols
)
3326 return AppendCols( numCols
);
3329 for ( row
= 0; row
< curNumRows
; row
++ )
3331 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3333 m_data
[row
].Insert( wxEmptyString
, col
);
3338 wxGridTableMessage
msg( this,
3339 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3343 GetView()->ProcessTableMessage( msg
);
3349 bool wxGridStringTable::AppendCols( size_t numCols
)
3353 size_t curNumRows
= m_data
.GetCount();
3357 // TODO: something better than this ?
3359 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3364 for ( row
= 0; row
< curNumRows
; row
++ )
3366 m_data
[row
].Add( wxEmptyString
, numCols
);
3371 wxGridTableMessage
msg( this,
3372 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3375 GetView()->ProcessTableMessage( msg
);
3381 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3385 size_t curNumRows
= m_data
.GetCount();
3386 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3387 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3389 if ( pos
>= curNumCols
)
3391 wxFAIL_MSG( wxString::Format
3393 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3395 (unsigned long)numCols
,
3396 (unsigned long)curNumCols
3401 if ( numCols
> curNumCols
- pos
)
3403 numCols
= curNumCols
- pos
;
3406 for ( row
= 0; row
< curNumRows
; row
++ )
3408 if ( numCols
>= curNumCols
)
3410 m_data
[row
].Clear();
3414 m_data
[row
].RemoveAt( pos
, numCols
);
3419 wxGridTableMessage
msg( this,
3420 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3424 GetView()->ProcessTableMessage( msg
);
3430 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3432 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3434 // using default label
3436 return wxGridTableBase::GetRowLabelValue( row
);
3440 return m_rowLabels
[ row
];
3444 wxString
wxGridStringTable::GetColLabelValue( int col
)
3446 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3448 // using default label
3450 return wxGridTableBase::GetColLabelValue( col
);
3454 return m_colLabels
[ col
];
3458 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3460 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3462 int n
= m_rowLabels
.GetCount();
3464 for ( i
= n
; i
<= row
; i
++ )
3466 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3470 m_rowLabels
[row
] = value
;
3473 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3475 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3477 int n
= m_colLabels
.GetCount();
3479 for ( i
= n
; i
<= col
; i
++ )
3481 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3485 m_colLabels
[col
] = value
;
3490 //////////////////////////////////////////////////////////////////////
3491 //////////////////////////////////////////////////////////////////////
3493 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3495 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3496 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3497 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3498 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3499 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3500 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3503 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3505 const wxPoint
&pos
, const wxSize
&size
)
3506 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3511 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3515 // NO - don't do this because it will set both the x and y origin
3516 // coords to match the parent scrolled window and we just want to
3517 // set the y coord - MB
3519 // m_owner->PrepareDC( dc );
3522 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3523 dc
.SetDeviceOrigin( 0, -y
);
3525 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3526 m_owner
->DrawRowLabels( dc
, rows
);
3530 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3532 m_owner
->ProcessRowLabelMouseEvent( event
);
3536 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3538 m_owner
->GetEventHandler()->ProcessEvent(event
);
3542 // This seems to be required for wxMotif otherwise the mouse
3543 // cursor must be in the cell edit control to get key events
3545 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3547 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3550 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3552 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3557 //////////////////////////////////////////////////////////////////////
3559 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3561 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3562 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3563 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3564 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3565 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3566 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3569 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3571 const wxPoint
&pos
, const wxSize
&size
)
3572 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3577 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3581 // NO - don't do this because it will set both the x and y origin
3582 // coords to match the parent scrolled window and we just want to
3583 // set the x coord - MB
3585 // m_owner->PrepareDC( dc );
3588 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3589 dc
.SetDeviceOrigin( -x
, 0 );
3591 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3592 m_owner
->DrawColLabels( dc
, cols
);
3596 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3598 m_owner
->ProcessColLabelMouseEvent( event
);
3601 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3603 m_owner
->GetEventHandler()->ProcessEvent(event
);
3607 // This seems to be required for wxMotif otherwise the mouse
3608 // cursor must be in the cell edit control to get key events
3610 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3612 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3615 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3617 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3622 //////////////////////////////////////////////////////////////////////
3624 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3626 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3627 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3628 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3629 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3630 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3631 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3634 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3636 const wxPoint
&pos
, const wxSize
&size
)
3637 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3642 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3646 int client_height
= 0;
3647 int client_width
= 0;
3648 GetClientSize( &client_width
, &client_height
);
3654 rect
.SetWidth( client_width
- 2 );
3655 rect
.SetHeight( client_height
- 2 );
3657 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3659 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3660 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3661 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3662 dc
.DrawLine( 0, 0, client_width
, 0 );
3663 dc
.DrawLine( 0, 0, 0, client_height
);
3665 dc
.SetPen( *wxWHITE_PEN
);
3666 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3667 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3672 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3674 m_owner
->ProcessCornerLabelMouseEvent( event
);
3678 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3680 m_owner
->GetEventHandler()->ProcessEvent(event
);
3683 // This seems to be required for wxMotif otherwise the mouse
3684 // cursor must be in the cell edit control to get key events
3686 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3688 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3691 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3693 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3698 //////////////////////////////////////////////////////////////////////
3700 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3702 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3703 EVT_PAINT( wxGridWindow::OnPaint
)
3704 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3705 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3706 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3707 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3708 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3709 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3710 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3713 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3714 wxGridRowLabelWindow
*rowLblWin
,
3715 wxGridColLabelWindow
*colLblWin
,
3718 const wxSize
&size
)
3719 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3720 wxT("grid window") )
3724 m_rowLabelWin
= rowLblWin
;
3725 m_colLabelWin
= colLblWin
;
3729 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3731 wxPaintDC
dc( this );
3732 m_owner
->PrepareDC( dc
);
3733 wxRegion reg
= GetUpdateRegion();
3734 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3735 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3736 #if WXGRID_DRAW_LINES
3737 m_owner
->DrawAllGridLines( dc
, reg
);
3739 m_owner
->DrawGridSpace( dc
);
3740 m_owner
->DrawHighlight( dc
, DirtyCells
);
3744 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3746 wxWindow::ScrollWindow( dx
, dy
, rect
);
3747 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3748 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3752 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3754 m_owner
->ProcessGridCellMouseEvent( event
);
3757 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3759 m_owner
->GetEventHandler()->ProcessEvent(event
);
3762 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3763 // cursor must be in the cell edit control to get key events
3765 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3767 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3770 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3772 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3775 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3779 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3781 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3785 //////////////////////////////////////////////////////////////////////
3787 // Internal Helper function for computing row or column from some
3788 // (unscrolled) coordinate value, using either
3789 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3790 // of m_rowBottoms/m_ColRights to speed up the search!
3792 // Internal helper macros for simpler use of that function
3794 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3795 const wxArrayInt
& BorderArray
, int nMax
,
3798 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3799 m_minAcceptableColWidth, \
3800 m_colRights, m_numCols, true)
3801 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3802 m_minAcceptableRowHeight, \
3803 m_rowBottoms, m_numRows, true)
3804 /////////////////////////////////////////////////////////////////////
3806 #if wxUSE_EXTENDED_RTTI
3807 WX_DEFINE_FLAGS( wxGridStyle
)
3809 wxBEGIN_FLAGS( wxGridStyle
)
3810 // new style border flags, we put them first to
3811 // use them for streaming out
3812 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3813 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3814 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3815 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3816 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3817 wxFLAGS_MEMBER(wxBORDER_NONE
)
3819 // old style border flags
3820 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3821 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3822 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3823 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3824 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3825 wxFLAGS_MEMBER(wxBORDER
)
3827 // standard window styles
3828 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3829 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3830 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3831 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3832 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3833 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3834 wxFLAGS_MEMBER(wxVSCROLL
)
3835 wxFLAGS_MEMBER(wxHSCROLL
)
3837 wxEND_FLAGS( wxGridStyle
)
3839 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3841 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3842 wxHIDE_PROPERTY( Children
)
3843 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3844 wxEND_PROPERTIES_TABLE()
3846 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3847 wxEND_HANDLERS_TABLE()
3849 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3852 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3855 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3858 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3859 EVT_PAINT( wxGrid::OnPaint
)
3860 EVT_SIZE( wxGrid::OnSize
)
3861 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3862 EVT_KEY_UP( wxGrid::OnKeyUp
)
3863 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3868 // in order to make sure that a size event is not
3869 // trigerred in a unfinished state
3870 m_cornerLabelWin
= NULL
;
3871 m_rowLabelWin
= NULL
;
3872 m_colLabelWin
= NULL
;
3876 wxGrid::wxGrid( wxWindow
*parent
,
3881 const wxString
& name
)
3882 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3883 m_colMinWidths(GRID_HASH_SIZE
),
3884 m_rowMinHeights(GRID_HASH_SIZE
)
3887 SetBestFittingSize(size
);
3890 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3891 const wxPoint
& pos
, const wxSize
& size
,
3892 long style
, const wxString
& name
)
3894 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3895 style
| wxWANTS_CHARS
, name
))
3898 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3899 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3902 SetBestFittingSize(size
);
3910 // Must do this or ~wxScrollHelper will pop the wrong event handler
3911 SetTargetWindow(this);
3913 wxSafeDecRef(m_defaultCellAttr
);
3915 #ifdef DEBUG_ATTR_CACHE
3916 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3917 wxPrintf(_T("wxGrid attribute cache statistics: "
3918 "total: %u, hits: %u (%u%%)\n"),
3919 total
, gs_nAttrCacheHits
,
3920 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3926 delete m_typeRegistry
;
3932 // ----- internal init and update functions
3935 // NOTE: If using the default visual attributes works everywhere then this can
3936 // be removed as well as the #else cases below.
3937 #define _USE_VISATTR 0
3940 #include "wx/listbox.h"
3943 void wxGrid::Create()
3945 m_created
= false; // set to true by CreateGrid
3947 m_table
= (wxGridTableBase
*) NULL
;
3950 m_cellEditCtrlEnabled
= false;
3952 m_defaultCellAttr
= new wxGridCellAttr();
3954 // Set default cell attributes
3955 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3956 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3957 m_defaultCellAttr
->SetFont(GetFont());
3958 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3959 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3960 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3963 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3964 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3966 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3967 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3970 m_defaultCellAttr
->SetTextColour(
3971 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3972 m_defaultCellAttr
->SetBackgroundColour(
3973 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3978 m_currentCellCoords
= wxGridNoCellCoords
;
3980 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3981 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3983 // create the type registry
3984 m_typeRegistry
= new wxGridTypeRegistry
;
3987 // subwindow components that make up the wxGrid
3988 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3993 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3998 m_colLabelWin
= new wxGridColLabelWindow( this,
4003 m_gridWin
= new wxGridWindow( this,
4010 SetTargetWindow( m_gridWin
);
4013 wxColour gfg
= gva
.colFg
;
4014 wxColour gbg
= gva
.colBg
;
4015 wxColour lfg
= lva
.colFg
;
4016 wxColour lbg
= lva
.colBg
;
4018 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4019 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4020 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4021 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4023 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4024 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4025 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4026 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4027 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4028 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4030 m_gridWin
->SetOwnForegroundColour(gfg
);
4031 m_gridWin
->SetOwnBackgroundColour(gbg
);
4037 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4038 wxGrid::wxGridSelectionModes selmode
)
4040 wxCHECK_MSG( !m_created
,
4042 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4044 m_numRows
= numRows
;
4045 m_numCols
= numCols
;
4047 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4048 m_table
->SetView( this );
4050 m_selection
= new wxGridSelection( this, selmode
);
4059 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4061 wxCHECK_RET( m_created
,
4062 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4064 m_selection
->SetSelectionMode( selmode
);
4067 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4069 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4070 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4072 return m_selection
->GetSelectionMode();
4075 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4076 wxGrid::wxGridSelectionModes selmode
)
4080 // stop all processing
4085 wxGridTableBase
*t
=m_table
;
4098 m_numRows
= table
->GetNumberRows();
4099 m_numCols
= table
->GetNumberCols();
4102 m_table
->SetView( this );
4105 m_selection
= new wxGridSelection( this, selmode
);
4118 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4119 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4121 if ( m_rowLabelWin
)
4123 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4127 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4130 m_labelTextColour
= wxColour( _T("BLACK") );
4133 m_attrCache
.row
= -1;
4134 m_attrCache
.col
= -1;
4135 m_attrCache
.attr
= NULL
;
4137 // TODO: something better than this ?
4139 m_labelFont
= this->GetFont();
4140 m_labelFont
.SetWeight( wxBOLD
);
4142 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4143 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4145 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4146 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4147 m_colLabelTextOrientation
= wxHORIZONTAL
;
4149 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4150 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4152 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4153 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4155 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4156 m_defaultRowHeight
+= 8;
4158 m_defaultRowHeight
+= 4;
4161 m_gridLineColour
= wxColour( 192,192,192 );
4162 m_gridLinesEnabled
= true;
4163 m_cellHighlightColour
= *wxBLACK
;
4164 m_cellHighlightPenWidth
= 2;
4165 m_cellHighlightROPenWidth
= 1;
4167 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4168 m_winCapture
= (wxWindow
*)NULL
;
4169 m_canDragRowSize
= true;
4170 m_canDragColSize
= true;
4171 m_canDragGridSize
= true;
4172 m_canDragCell
= false;
4174 m_dragRowOrCol
= -1;
4175 m_isDragging
= false;
4176 m_startDragPos
= wxDefaultPosition
;
4178 m_waitForSlowClick
= false;
4180 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4181 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4183 m_currentCellCoords
= wxGridNoCellCoords
;
4185 m_selectingTopLeft
= wxGridNoCellCoords
;
4186 m_selectingBottomRight
= wxGridNoCellCoords
;
4187 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4188 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4190 m_editable
= true; // default for whole grid
4192 m_inOnKeyDown
= false;
4198 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4199 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4202 // ----------------------------------------------------------------------------
4203 // the idea is to call these functions only when necessary because they create
4204 // quite big arrays which eat memory mostly unnecessary - in particular, if
4205 // default widths/heights are used for all rows/columns, we may not use these
4208 // with some extra code, it should be possible to only store the
4209 // widths/heights different from default ones but this will be done later...
4210 // ----------------------------------------------------------------------------
4212 void wxGrid::InitRowHeights()
4214 m_rowHeights
.Empty();
4215 m_rowBottoms
.Empty();
4217 m_rowHeights
.Alloc( m_numRows
);
4218 m_rowBottoms
.Alloc( m_numRows
);
4222 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4224 for ( int i
= 0; i
< m_numRows
; i
++ )
4226 rowBottom
+= m_defaultRowHeight
;
4227 m_rowBottoms
.Add( rowBottom
);
4231 void wxGrid::InitColWidths()
4233 m_colWidths
.Empty();
4234 m_colRights
.Empty();
4236 m_colWidths
.Alloc( m_numCols
);
4237 m_colRights
.Alloc( m_numCols
);
4240 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4242 for ( int i
= 0; i
< m_numCols
; i
++ )
4244 colRight
+= m_defaultColWidth
;
4245 m_colRights
.Add( colRight
);
4249 int wxGrid::GetColWidth(int col
) const
4251 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4254 int wxGrid::GetColLeft(int col
) const
4256 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4257 : m_colRights
[col
] - m_colWidths
[col
];
4260 int wxGrid::GetColRight(int col
) const
4262 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4266 int wxGrid::GetRowHeight(int row
) const
4268 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4271 int wxGrid::GetRowTop(int row
) const
4273 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4274 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4277 int wxGrid::GetRowBottom(int row
) const
4279 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4280 : m_rowBottoms
[row
];
4283 void wxGrid::CalcDimensions()
4286 GetClientSize( &cw
, &ch
);
4288 if ( m_rowLabelWin
->IsShown() )
4289 cw
-= m_rowLabelWidth
;
4290 if ( m_colLabelWin
->IsShown() )
4291 ch
-= m_colLabelHeight
;
4294 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4295 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4297 // take into account editor if shown
4298 if( IsCellEditControlShown() )
4301 int r
= m_currentCellCoords
.GetRow();
4302 int c
= m_currentCellCoords
.GetCol();
4303 int x
= GetColLeft(c
);
4304 int y
= GetRowTop(r
);
4306 // how big is the editor
4307 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4308 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4309 editor
->GetControl()->GetSize(&w2
, &h2
);
4312 if( w2
> w
) w
= w2
;
4313 if( h2
> h
) h
= h2
;
4318 // preserve (more or less) the previous position
4320 GetViewStart( &x
, &y
);
4322 // ensure the position is valid for the new scroll ranges
4324 x
= wxMax( w
- 1, 0 );
4326 y
= wxMax( h
- 1, 0 );
4328 // do set scrollbar parameters
4329 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4330 GetScrollX(w
), GetScrollY(h
), x
, y
,
4331 GetBatchCount() != 0);
4333 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4334 // still must reposition the children
4339 void wxGrid::CalcWindowSizes()
4341 // escape if the window is has not been fully created yet
4343 if ( m_cornerLabelWin
== NULL
)
4347 GetClientSize( &cw
, &ch
);
4349 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4350 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4352 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4353 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4355 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4356 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4358 if ( m_gridWin
&& m_gridWin
->IsShown() )
4359 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4363 // this is called when the grid table sends a message to say that it
4364 // has been redimensioned
4366 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4369 bool result
= false;
4371 // Clear the attribute cache as the attribute might refer to a different
4372 // cell than stored in the cache after adding/removing rows/columns.
4374 // By the same reasoning, the editor should be dismissed if columns are
4375 // added or removed. And for consistency, it should IMHO always be
4376 // removed, not only if the cell "underneath" it actually changes.
4377 // For now, I intentionally do not save the editor's content as the
4378 // cell it might want to save that stuff to might no longer exist.
4379 HideCellEditControl();
4381 // if we were using the default widths/heights so far, we must change them
4383 if ( m_colWidths
.IsEmpty() )
4388 if ( m_rowHeights
.IsEmpty() )
4394 switch ( msg
.GetId() )
4396 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4398 size_t pos
= msg
.GetCommandInt();
4399 int numRows
= msg
.GetCommandInt2();
4401 m_numRows
+= numRows
;
4403 if ( !m_rowHeights
.IsEmpty() )
4405 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4406 m_rowBottoms
.Insert( 0, pos
, numRows
);
4409 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4411 for ( i
= pos
; i
< m_numRows
; i
++ )
4413 bottom
+= m_rowHeights
[i
];
4414 m_rowBottoms
[i
] = bottom
;
4417 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4419 // if we have just inserted cols into an empty grid the current
4420 // cell will be undefined...
4422 SetCurrentCell( 0, 0 );
4426 m_selection
->UpdateRows( pos
, numRows
);
4427 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4429 attrProvider
->UpdateAttrRows( pos
, numRows
);
4431 if ( !GetBatchCount() )
4434 m_rowLabelWin
->Refresh();
4440 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4442 int numRows
= msg
.GetCommandInt();
4443 int oldNumRows
= m_numRows
;
4444 m_numRows
+= numRows
;
4446 if ( !m_rowHeights
.IsEmpty() )
4448 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4449 m_rowBottoms
.Add( 0, numRows
);
4452 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4454 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4456 bottom
+= m_rowHeights
[i
];
4457 m_rowBottoms
[i
] = bottom
;
4460 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4462 // if we have just inserted cols into an empty grid the current
4463 // cell will be undefined...
4465 SetCurrentCell( 0, 0 );
4467 if ( !GetBatchCount() )
4470 m_rowLabelWin
->Refresh();
4476 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4478 size_t pos
= msg
.GetCommandInt();
4479 int numRows
= msg
.GetCommandInt2();
4480 m_numRows
-= numRows
;
4482 if ( !m_rowHeights
.IsEmpty() )
4484 m_rowHeights
.RemoveAt( pos
, numRows
);
4485 m_rowBottoms
.RemoveAt( pos
, numRows
);
4488 for ( i
= 0; i
< m_numRows
; i
++ )
4490 h
+= m_rowHeights
[i
];
4491 m_rowBottoms
[i
] = h
;
4496 m_currentCellCoords
= wxGridNoCellCoords
;
4500 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4501 m_currentCellCoords
.Set( 0, 0 );
4505 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4506 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4508 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4509 // ifdef'd out following patch from Paul Gammans
4511 // No need to touch column attributes, unless we
4512 // removed _all_ rows, in this case, we remove
4513 // all column attributes.
4514 // I hate to do this here, but the
4515 // needed data is not available inside UpdateAttrRows.
4516 if ( !GetNumberRows() )
4517 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4520 if ( !GetBatchCount() )
4523 m_rowLabelWin
->Refresh();
4529 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4531 size_t pos
= msg
.GetCommandInt();
4532 int numCols
= msg
.GetCommandInt2();
4533 m_numCols
+= numCols
;
4535 if ( !m_colWidths
.IsEmpty() )
4537 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4538 m_colRights
.Insert( 0, pos
, numCols
);
4541 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4543 for ( i
= pos
; i
< m_numCols
; i
++ )
4545 right
+= m_colWidths
[i
];
4546 m_colRights
[i
] = right
;
4549 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4551 // if we have just inserted cols into an empty grid the current
4552 // cell will be undefined...
4554 SetCurrentCell( 0, 0 );
4558 m_selection
->UpdateCols( pos
, numCols
);
4559 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4561 attrProvider
->UpdateAttrCols( pos
, numCols
);
4562 if ( !GetBatchCount() )
4565 m_colLabelWin
->Refresh();
4572 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4574 int numCols
= msg
.GetCommandInt();
4575 int oldNumCols
= m_numCols
;
4576 m_numCols
+= numCols
;
4577 if ( !m_colWidths
.IsEmpty() )
4579 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4580 m_colRights
.Add( 0, numCols
);
4583 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4585 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4587 right
+= m_colWidths
[i
];
4588 m_colRights
[i
] = right
;
4591 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4593 // if we have just inserted cols into an empty grid the current
4594 // cell will be undefined...
4596 SetCurrentCell( 0, 0 );
4598 if ( !GetBatchCount() )
4601 m_colLabelWin
->Refresh();
4607 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4609 size_t pos
= msg
.GetCommandInt();
4610 int numCols
= msg
.GetCommandInt2();
4611 m_numCols
-= numCols
;
4613 if ( !m_colWidths
.IsEmpty() )
4615 m_colWidths
.RemoveAt( pos
, numCols
);
4616 m_colRights
.RemoveAt( pos
, numCols
);
4619 for ( i
= 0; i
< m_numCols
; i
++ )
4621 w
+= m_colWidths
[i
];
4627 m_currentCellCoords
= wxGridNoCellCoords
;
4631 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4632 m_currentCellCoords
.Set( 0, 0 );
4636 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4637 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4639 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4640 // ifdef'd out following patch from Paul Gammans
4642 // No need to touch row attributes, unless we
4643 // removed _all_ columns, in this case, we remove
4644 // all row attributes.
4645 // I hate to do this here, but the
4646 // needed data is not available inside UpdateAttrCols.
4647 if ( !GetNumberCols() )
4648 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4651 if ( !GetBatchCount() )
4654 m_colLabelWin
->Refresh();
4661 if (result
&& !GetBatchCount() )
4662 m_gridWin
->Refresh();
4667 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4669 wxRegionIterator
iter( reg
);
4672 wxArrayInt rowlabels
;
4679 // TODO: remove this when we can...
4680 // There is a bug in wxMotif that gives garbage update
4681 // rectangles if you jump-scroll a long way by clicking the
4682 // scrollbar with middle button. This is a work-around
4684 #if defined(__WXMOTIF__)
4686 m_gridWin
->GetClientSize( &cw
, &ch
);
4687 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4688 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4691 // logical bounds of update region
4694 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4695 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4697 // find the row labels within these bounds
4700 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4702 if ( GetRowBottom(row
) < top
)
4705 if ( GetRowTop(row
) > bottom
)
4708 rowlabels
.Add( row
);
4718 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4720 wxRegionIterator
iter( reg
);
4723 wxArrayInt colLabels
;
4730 // TODO: remove this when we can...
4731 // There is a bug in wxMotif that gives garbage update
4732 // rectangles if you jump-scroll a long way by clicking the
4733 // scrollbar with middle button. This is a work-around
4735 #if defined(__WXMOTIF__)
4737 m_gridWin
->GetClientSize( &cw
, &ch
);
4738 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4739 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4742 // logical bounds of update region
4745 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4746 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4748 // find the cells within these bounds
4751 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4753 if ( GetColRight(col
) < left
)
4756 if ( GetColLeft(col
) > right
)
4759 colLabels
.Add( col
);
4768 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4770 wxRegionIterator
iter( reg
);
4773 wxGridCellCoordsArray cellsExposed
;
4775 int left
, top
, right
, bottom
;
4780 // TODO: remove this when we can...
4781 // There is a bug in wxMotif that gives garbage update
4782 // rectangles if you jump-scroll a long way by clicking the
4783 // scrollbar with middle button. This is a work-around
4785 #if defined(__WXMOTIF__)
4787 m_gridWin
->GetClientSize( &cw
, &ch
);
4788 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4789 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4790 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4791 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4794 // logical bounds of update region
4796 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4797 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4799 // find the cells within these bounds
4802 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4804 if ( GetRowBottom(row
) <= top
)
4807 if ( GetRowTop(row
) > bottom
)
4810 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4812 if ( GetColRight(col
) <= left
)
4815 if ( GetColLeft(col
) > right
)
4818 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4825 return cellsExposed
;
4829 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4832 wxPoint
pos( event
.GetPosition() );
4833 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4835 if ( event
.Dragging() )
4839 m_isDragging
= true;
4840 m_rowLabelWin
->CaptureMouse();
4843 if ( event
.LeftIsDown() )
4845 switch( m_cursorMode
)
4847 case WXGRID_CURSOR_RESIZE_ROW
:
4849 int cw
, ch
, left
, dummy
;
4850 m_gridWin
->GetClientSize( &cw
, &ch
);
4851 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4853 wxClientDC
dc( m_gridWin
);
4856 GetRowTop(m_dragRowOrCol
) +
4857 GetRowMinimalHeight(m_dragRowOrCol
) );
4858 dc
.SetLogicalFunction(wxINVERT
);
4859 if ( m_dragLastPos
>= 0 )
4861 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4863 dc
.DrawLine( left
, y
, left
+cw
, y
);
4868 case WXGRID_CURSOR_SELECT_ROW
:
4869 if ( (row
= YToRow( y
)) >= 0 )
4873 m_selection
->SelectRow( row
,
4874 event
.ControlDown(),
4881 // default label to suppress warnings about "enumeration value
4882 // 'xxx' not handled in switch
4890 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4895 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4896 m_isDragging
= false;
4899 // ------------ Entering or leaving the window
4901 if ( event
.Entering() || event
.Leaving() )
4903 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4907 // ------------ Left button pressed
4909 else if ( event
.LeftDown() )
4911 // don't send a label click event for a hit on the
4912 // edge of the row label - this is probably the user
4913 // wanting to resize the row
4915 if ( YToEdgeOfRow(y
) < 0 )
4919 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4921 if ( !event
.ShiftDown() && !event
.ControlDown() )
4925 if ( event
.ShiftDown() )
4927 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4930 GetNumberCols() - 1,
4931 event
.ControlDown(),
4938 m_selection
->SelectRow( row
,
4939 event
.ControlDown(),
4946 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4951 // starting to drag-resize a row
4953 if ( CanDragRowSize() )
4954 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4959 // ------------ Left double click
4961 else if (event
.LeftDClick() )
4963 int row
= YToEdgeOfRow(y
);
4968 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4970 // no default action at the moment
4975 // adjust row height depending on label text
4976 AutoSizeRowLabelSize( row
);
4978 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4984 // ------------ Left button released
4986 else if ( event
.LeftUp() )
4988 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4990 DoEndDragResizeRow();
4992 // Note: we are ending the event *after* doing
4993 // default processing in this case
4995 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4998 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5003 // ------------ Right button down
5005 else if ( event
.RightDown() )
5009 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5011 // no default action at the moment
5016 // ------------ Right double click
5018 else if ( event
.RightDClick() )
5022 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5024 // no default action at the moment
5029 // ------------ No buttons down and mouse moving
5031 else if ( event
.Moving() )
5033 m_dragRowOrCol
= YToEdgeOfRow( y
);
5034 if ( m_dragRowOrCol
>= 0 )
5036 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5038 // don't capture the mouse yet
5039 if ( CanDragRowSize() )
5040 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5043 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5045 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5051 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5054 wxPoint
pos( event
.GetPosition() );
5055 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5057 if ( event
.Dragging() )
5061 m_isDragging
= true;
5062 m_colLabelWin
->CaptureMouse();
5065 if ( event
.LeftIsDown() )
5067 switch( m_cursorMode
)
5069 case WXGRID_CURSOR_RESIZE_COL
:
5071 int cw
, ch
, dummy
, top
;
5072 m_gridWin
->GetClientSize( &cw
, &ch
);
5073 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5075 wxClientDC
dc( m_gridWin
);
5078 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5079 GetColMinimalWidth(m_dragRowOrCol
));
5080 dc
.SetLogicalFunction(wxINVERT
);
5081 if ( m_dragLastPos
>= 0 )
5083 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5085 dc
.DrawLine( x
, top
, x
, top
+ch
);
5090 case WXGRID_CURSOR_SELECT_COL
:
5091 if ( (col
= XToCol( x
)) >= 0 )
5095 m_selection
->SelectCol( col
,
5096 event
.ControlDown(),
5103 // default label to suppress warnings about "enumeration value
5104 // 'xxx' not handled in switch
5112 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5117 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5118 m_isDragging
= false;
5121 // ------------ Entering or leaving the window
5123 if ( event
.Entering() || event
.Leaving() )
5125 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5129 // ------------ Left button pressed
5131 else if ( event
.LeftDown() )
5133 // don't send a label click event for a hit on the
5134 // edge of the col label - this is probably the user
5135 // wanting to resize the col
5137 if ( XToEdgeOfCol(x
) < 0 )
5141 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5143 if ( !event
.ShiftDown() && !event
.ControlDown() )
5147 if ( event
.ShiftDown() )
5149 m_selection
->SelectBlock( 0,
5150 m_currentCellCoords
.GetCol(),
5151 GetNumberRows() - 1, col
,
5152 event
.ControlDown(),
5159 m_selection
->SelectCol( col
,
5160 event
.ControlDown(),
5167 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5172 // starting to drag-resize a col
5174 if ( CanDragColSize() )
5175 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5180 // ------------ Left double click
5182 if ( event
.LeftDClick() )
5184 int col
= XToEdgeOfCol(x
);
5189 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5191 // no default action at the moment
5196 // adjust column width depending on label text
5197 AutoSizeColLabelSize( col
);
5199 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5205 // ------------ Left button released
5207 else if ( event
.LeftUp() )
5209 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5211 DoEndDragResizeCol();
5213 // Note: we are ending the event *after* doing
5214 // default processing in this case
5216 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5219 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5224 // ------------ Right button down
5226 else if ( event
.RightDown() )
5230 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5232 // no default action at the moment
5237 // ------------ Right double click
5239 else if ( event
.RightDClick() )
5243 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5245 // no default action at the moment
5250 // ------------ No buttons down and mouse moving
5252 else if ( event
.Moving() )
5254 m_dragRowOrCol
= XToEdgeOfCol( x
);
5255 if ( m_dragRowOrCol
>= 0 )
5257 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5259 // don't capture the cursor yet
5260 if ( CanDragColSize() )
5261 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5264 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5266 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5272 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5274 if ( event
.LeftDown() )
5276 // indicate corner label by having both row and
5279 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5285 else if ( event
.LeftDClick() )
5287 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5290 else if ( event
.RightDown() )
5292 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5294 // no default action at the moment
5298 else if ( event
.RightDClick() )
5300 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5302 // no default action at the moment
5307 void wxGrid::ChangeCursorMode(CursorMode mode
,
5312 static const wxChar
*cursorModes
[] =
5321 wxLogTrace(_T("grid"),
5322 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5323 win
== m_colLabelWin
? _T("colLabelWin")
5324 : win
? _T("rowLabelWin")
5326 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5327 #endif // __WXDEBUG__
5329 if ( mode
== m_cursorMode
&&
5330 win
== m_winCapture
&&
5331 captureMouse
== (m_winCapture
!= NULL
))
5336 // by default use the grid itself
5342 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5343 m_winCapture
= (wxWindow
*)NULL
;
5346 m_cursorMode
= mode
;
5348 switch ( m_cursorMode
)
5350 case WXGRID_CURSOR_RESIZE_ROW
:
5351 win
->SetCursor( m_rowResizeCursor
);
5354 case WXGRID_CURSOR_RESIZE_COL
:
5355 win
->SetCursor( m_colResizeCursor
);
5359 win
->SetCursor( *wxSTANDARD_CURSOR
);
5362 // we need to capture mouse when resizing
5363 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5364 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5366 if ( captureMouse
&& resize
)
5368 win
->CaptureMouse();
5373 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5376 wxPoint
pos( event
.GetPosition() );
5377 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5379 wxGridCellCoords coords
;
5380 XYToCell( x
, y
, coords
);
5382 int cell_rows
, cell_cols
;
5383 bool isFirstDrag
= !m_isDragging
;
5384 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5385 if ((cell_rows
< 0) || (cell_cols
< 0))
5387 coords
.SetRow(coords
.GetRow() + cell_rows
);
5388 coords
.SetCol(coords
.GetCol() + cell_cols
);
5391 if ( event
.Dragging() )
5393 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5395 // Don't start doing anything until the mouse has been drug at
5396 // least 3 pixels in any direction...
5399 if (m_startDragPos
== wxDefaultPosition
)
5401 m_startDragPos
= pos
;
5404 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5408 m_isDragging
= true;
5409 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5411 // Hide the edit control, so it
5412 // won't interfer with drag-shrinking.
5413 if ( IsCellEditControlShown() )
5415 HideCellEditControl();
5416 SaveEditControlValue();
5419 // Have we captured the mouse yet?
5422 m_winCapture
= m_gridWin
;
5423 m_winCapture
->CaptureMouse();
5426 if ( coords
!= wxGridNoCellCoords
)
5428 if ( event
.ControlDown() )
5430 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5431 m_selectingKeyboard
= coords
;
5432 HighlightBlock ( m_selectingKeyboard
, coords
);
5434 else if ( CanDragCell() )
5438 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5439 m_selectingKeyboard
= coords
;
5441 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5449 if ( !IsSelection() )
5451 HighlightBlock( coords
, coords
);
5455 HighlightBlock( m_currentCellCoords
, coords
);
5459 if (! IsVisible(coords
))
5461 MakeCellVisible(coords
);
5462 // TODO: need to introduce a delay or something here. The
5463 // scrolling is way to fast, at least on MSW - also on GTK.
5467 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5469 int cw
, ch
, left
, dummy
;
5470 m_gridWin
->GetClientSize( &cw
, &ch
);
5471 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5473 wxClientDC
dc( m_gridWin
);
5475 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5476 GetRowMinimalHeight(m_dragRowOrCol
) );
5477 dc
.SetLogicalFunction(wxINVERT
);
5478 if ( m_dragLastPos
>= 0 )
5480 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5482 dc
.DrawLine( left
, y
, left
+cw
, y
);
5485 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5487 int cw
, ch
, dummy
, top
;
5488 m_gridWin
->GetClientSize( &cw
, &ch
);
5489 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5491 wxClientDC
dc( m_gridWin
);
5493 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5494 GetColMinimalWidth(m_dragRowOrCol
) );
5495 dc
.SetLogicalFunction(wxINVERT
);
5496 if ( m_dragLastPos
>= 0 )
5498 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5500 dc
.DrawLine( x
, top
, x
, top
+ch
);
5507 m_isDragging
= false;
5508 m_startDragPos
= wxDefaultPosition
;
5510 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5511 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5514 if ( event
.Entering() || event
.Leaving() )
5516 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5517 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5522 // ------------ Left button pressed
5524 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5526 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5531 if ( !event
.ControlDown() )
5533 if ( event
.ShiftDown() )
5537 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5538 m_currentCellCoords
.GetCol(),
5541 event
.ControlDown(),
5547 else if ( XToEdgeOfCol(x
) < 0 &&
5548 YToEdgeOfRow(y
) < 0 )
5550 DisableCellEditControl();
5551 MakeCellVisible( coords
);
5553 if ( event
.ControlDown() )
5557 m_selection
->ToggleCellSelection( coords
.GetRow(),
5559 event
.ControlDown(),
5564 m_selectingTopLeft
= wxGridNoCellCoords
;
5565 m_selectingBottomRight
= wxGridNoCellCoords
;
5566 m_selectingKeyboard
= coords
;
5570 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5571 SetCurrentCell( coords
);
5574 if ( m_selection
->GetSelectionMode() !=
5575 wxGrid::wxGridSelectCells
)
5577 HighlightBlock( coords
, coords
);
5586 // ------------ Left double click
5588 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5590 DisableCellEditControl();
5592 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5594 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5599 // we want double click to select a cell and start editing
5600 // (i.e. to behave in same way as sequence of two slow clicks):
5601 m_waitForSlowClick
= true;
5608 // ------------ Left button released
5610 else if ( event
.LeftUp() )
5612 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5616 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5617 m_winCapture
= NULL
;
5620 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5623 EnableCellEditControl();
5625 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5626 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5627 editor
->StartingClick();
5631 m_waitForSlowClick
= false;
5633 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5634 m_selectingBottomRight
!= wxGridNoCellCoords
)
5638 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5639 m_selectingTopLeft
.GetCol(),
5640 m_selectingBottomRight
.GetRow(),
5641 m_selectingBottomRight
.GetCol(),
5642 event
.ControlDown(),
5648 m_selectingTopLeft
= wxGridNoCellCoords
;
5649 m_selectingBottomRight
= wxGridNoCellCoords
;
5651 // Show the edit control, if it has been hidden for
5653 ShowCellEditControl();
5656 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5658 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5659 DoEndDragResizeRow();
5661 // Note: we are ending the event *after* doing
5662 // default processing in this case
5664 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5666 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5668 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5669 DoEndDragResizeCol();
5671 // Note: we are ending the event *after* doing
5672 // default processing in this case
5674 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5681 // ------------ Right button down
5683 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5685 DisableCellEditControl();
5686 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5691 // no default action at the moment
5696 // ------------ Right double click
5698 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5700 DisableCellEditControl();
5701 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5706 // no default action at the moment
5710 // ------------ Moving and no button action
5712 else if ( event
.Moving() && !event
.IsButton() )
5714 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5716 // out of grid cell area
5717 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5721 int dragRow
= YToEdgeOfRow( y
);
5722 int dragCol
= XToEdgeOfCol( x
);
5724 // Dragging on the corner of a cell to resize in both
5725 // directions is not implemented yet...
5727 if ( dragRow
>= 0 && dragCol
>= 0 )
5729 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5735 m_dragRowOrCol
= dragRow
;
5737 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5739 if ( CanDragRowSize() && CanDragGridSize() )
5740 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5745 m_dragRowOrCol
= dragCol
;
5753 m_dragRowOrCol
= dragCol
;
5755 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5757 if ( CanDragColSize() && CanDragGridSize() )
5758 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5764 // Neither on a row or col edge
5766 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5768 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5774 void wxGrid::DoEndDragResizeRow()
5776 if ( m_dragLastPos
>= 0 )
5778 // erase the last line and resize the row
5780 int cw
, ch
, left
, dummy
;
5781 m_gridWin
->GetClientSize( &cw
, &ch
);
5782 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5784 wxClientDC
dc( m_gridWin
);
5786 dc
.SetLogicalFunction( wxINVERT
);
5787 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5788 HideCellEditControl();
5789 SaveEditControlValue();
5791 int rowTop
= GetRowTop(m_dragRowOrCol
);
5792 SetRowSize( m_dragRowOrCol
,
5793 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5795 if ( !GetBatchCount() )
5797 // Only needed to get the correct rect.y:
5798 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5800 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5801 rect
.width
= m_rowLabelWidth
;
5802 rect
.height
= ch
- rect
.y
;
5803 m_rowLabelWin
->Refresh( true, &rect
);
5805 // if there is a multicell block, paint all of it
5808 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5809 int leftCol
= XToCol(left
);
5810 int rightCol
= internalXToCol(left
+cw
);
5813 for (i
=leftCol
; i
<rightCol
; i
++)
5815 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5816 if (cell_rows
< subtract_rows
)
5817 subtract_rows
= cell_rows
;
5819 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5820 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5821 rect
.height
= ch
- rect
.y
;
5824 m_gridWin
->Refresh( false, &rect
);
5827 ShowCellEditControl();
5832 void wxGrid::DoEndDragResizeCol()
5834 if ( m_dragLastPos
>= 0 )
5836 // erase the last line and resize the col
5838 int cw
, ch
, dummy
, top
;
5839 m_gridWin
->GetClientSize( &cw
, &ch
);
5840 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5842 wxClientDC
dc( m_gridWin
);
5844 dc
.SetLogicalFunction( wxINVERT
);
5845 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5846 HideCellEditControl();
5847 SaveEditControlValue();
5849 int colLeft
= GetColLeft(m_dragRowOrCol
);
5850 SetColSize( m_dragRowOrCol
,
5851 wxMax( m_dragLastPos
- colLeft
,
5852 GetColMinimalWidth(m_dragRowOrCol
) ) );
5854 if ( !GetBatchCount() )
5856 // Only needed to get the correct rect.x:
5857 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5859 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5860 rect
.width
= cw
- rect
.x
;
5861 rect
.height
= m_colLabelHeight
;
5862 m_colLabelWin
->Refresh( true, &rect
);
5864 // if there is a multicell block, paint all of it
5867 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5868 int topRow
= YToRow(top
);
5869 int bottomRow
= internalYToRow(top
+cw
);
5872 for (i
=topRow
; i
<bottomRow
; i
++)
5874 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5875 if (cell_cols
< subtract_cols
)
5876 subtract_cols
= cell_cols
;
5878 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5879 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5880 rect
.width
= cw
- rect
.x
;
5883 m_gridWin
->Refresh( false, &rect
);
5886 ShowCellEditControl();
5893 // ------ interaction with data model
5895 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5897 switch ( msg
.GetId() )
5899 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5900 return GetModelValues();
5902 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5903 return SetModelValues();
5905 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5906 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5907 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5908 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5909 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5910 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5911 return Redimension( msg
);
5920 // The behaviour of this function depends on the grid table class
5921 // Clear() function. For the default wxGridStringTable class the
5922 // behavious is to replace all cell contents with wxEmptyString but
5923 // not to change the number of rows or cols.
5925 void wxGrid::ClearGrid()
5929 if (IsCellEditControlEnabled())
5930 DisableCellEditControl();
5933 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5938 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5940 // TODO: something with updateLabels flag
5944 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5950 if (IsCellEditControlEnabled())
5951 DisableCellEditControl();
5953 bool done
= m_table
->InsertRows( pos
, numRows
);
5956 // the table will have sent the results of the insert row
5957 // operation to this view object as a grid table message
5963 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5965 // TODO: something with updateLabels flag
5969 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5975 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5977 // the table will have sent the results of the append row
5978 // operation to this view object as a grid table message
5984 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5986 // TODO: something with updateLabels flag
5990 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5996 if (IsCellEditControlEnabled())
5997 DisableCellEditControl();
5999 bool done
= m_table
->DeleteRows( pos
, numRows
);
6001 // the table will have sent the results of the delete row
6002 // operation to this view object as a grid table message
6008 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6010 // TODO: something with updateLabels flag
6014 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6020 if (IsCellEditControlEnabled())
6021 DisableCellEditControl();
6023 bool done
= m_table
->InsertCols( pos
, numCols
);
6025 // the table will have sent the results of the insert col
6026 // operation to this view object as a grid table message
6032 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6034 // TODO: something with updateLabels flag
6038 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6044 bool done
= m_table
->AppendCols( numCols
);
6046 // the table will have sent the results of the append col
6047 // operation to this view object as a grid table message
6053 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6055 // TODO: something with updateLabels flag
6059 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6065 if (IsCellEditControlEnabled())
6066 DisableCellEditControl();
6068 bool done
= m_table
->DeleteCols( pos
, numCols
);
6070 // the table will have sent the results of the delete col
6071 // operation to this view object as a grid table message
6079 // ----- event handlers
6082 // Generate a grid event based on a mouse event and
6083 // return the result of ProcessEvent()
6085 int wxGrid::SendEvent( const wxEventType type
,
6087 wxMouseEvent
& mouseEv
)
6092 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6094 int rowOrCol
= (row
== -1 ? col
: row
);
6096 wxGridSizeEvent
gridEvt( GetId(),
6100 mouseEv
.GetX() + GetRowLabelSize(),
6101 mouseEv
.GetY() + GetColLabelSize(),
6102 mouseEv
.ControlDown(),
6103 mouseEv
.ShiftDown(),
6105 mouseEv
.MetaDown() );
6107 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6108 vetoed
= !gridEvt
.IsAllowed();
6110 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6112 // Right now, it should _never_ end up here!
6113 wxGridRangeSelectEvent
gridEvt( GetId(),
6117 m_selectingBottomRight
,
6119 mouseEv
.ControlDown(),
6120 mouseEv
.ShiftDown(),
6122 mouseEv
.MetaDown() );
6124 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6125 vetoed
= !gridEvt
.IsAllowed();
6129 wxGridEvent
gridEvt( GetId(),
6133 mouseEv
.GetX() + GetRowLabelSize(),
6134 mouseEv
.GetY() + GetColLabelSize(),
6136 mouseEv
.ControlDown(),
6137 mouseEv
.ShiftDown(),
6139 mouseEv
.MetaDown() );
6140 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6141 vetoed
= !gridEvt
.IsAllowed();
6144 // A Veto'd event may not be `claimed' so test this first
6145 if (vetoed
) return -1;
6146 return claimed
? 1 : 0;
6150 // Generate a grid event of specified type and return the result
6151 // of ProcessEvent().
6153 int wxGrid::SendEvent( const wxEventType type
,
6159 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6161 int rowOrCol
= (row
== -1 ? col
: row
);
6163 wxGridSizeEvent
gridEvt( GetId(),
6168 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6169 vetoed
= !gridEvt
.IsAllowed();
6173 wxGridEvent
gridEvt( GetId(),
6178 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6179 vetoed
= !gridEvt
.IsAllowed();
6182 // A Veto'd event may not be `claimed' so test this first
6183 if (vetoed
) return -1;
6184 return claimed
? 1 : 0;
6188 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6190 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6193 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6195 // Don't do anything if between Begin/EndBatch...
6196 // EndBatch() will do all this on the last nested one anyway.
6197 if (! GetBatchCount())
6199 // Refresh to get correct scrolled position:
6200 wxScrolledWindow::Refresh(eraseb
,rect
);
6204 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6205 int width_label
, width_cell
, height_label
, height_cell
;
6208 //Copy rectangle can get scroll offsets..
6209 rect_x
= rect
->GetX();
6210 rect_y
= rect
->GetY();
6211 rectWidth
= rect
->GetWidth();
6212 rectHeight
= rect
->GetHeight();
6214 width_label
= m_rowLabelWidth
- rect_x
;
6215 if (width_label
> rectWidth
) width_label
= rectWidth
;
6217 height_label
= m_colLabelHeight
- rect_y
;
6218 if (height_label
> rectHeight
) height_label
= rectHeight
;
6220 if (rect_x
> m_rowLabelWidth
)
6222 x
= rect_x
- m_rowLabelWidth
;
6223 width_cell
= rectWidth
;
6228 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6231 if (rect_y
> m_colLabelHeight
)
6233 y
= rect_y
- m_colLabelHeight
;
6234 height_cell
= rectHeight
;
6239 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6242 // Paint corner label part intersecting rect.
6243 if ( width_label
> 0 && height_label
> 0 )
6245 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6246 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6249 // Paint col labels part intersecting rect.
6250 if ( width_cell
> 0 && height_label
> 0 )
6252 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6253 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6256 // Paint row labels part intersecting rect.
6257 if ( width_label
> 0 && height_cell
> 0 )
6259 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6260 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6263 // Paint cell area part intersecting rect.
6264 if ( width_cell
> 0 && height_cell
> 0 )
6266 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6267 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6272 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6273 m_colLabelWin
->Refresh(eraseb
, NULL
);
6274 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6275 m_gridWin
->Refresh(eraseb
, NULL
);
6280 void wxGrid::OnSize( wxSizeEvent
& event
)
6282 // position the child windows
6285 // don't call CalcDimensions() from here, the base class handles the size
6291 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6293 if ( m_inOnKeyDown
)
6295 // shouldn't be here - we are going round in circles...
6297 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6300 m_inOnKeyDown
= true;
6302 // propagate the event up and see if it gets processed
6304 wxWindow
*parent
= GetParent();
6305 wxKeyEvent
keyEvt( event
);
6306 keyEvt
.SetEventObject( parent
);
6308 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6311 // try local handlers
6313 switch ( event
.GetKeyCode() )
6316 if ( event
.ControlDown() )
6318 MoveCursorUpBlock( event
.ShiftDown() );
6322 MoveCursorUp( event
.ShiftDown() );
6327 if ( event
.ControlDown() )
6329 MoveCursorDownBlock( event
.ShiftDown() );
6333 MoveCursorDown( event
.ShiftDown() );
6338 if ( event
.ControlDown() )
6340 MoveCursorLeftBlock( event
.ShiftDown() );
6344 MoveCursorLeft( event
.ShiftDown() );
6349 if ( event
.ControlDown() )
6351 MoveCursorRightBlock( event
.ShiftDown() );
6355 MoveCursorRight( event
.ShiftDown() );
6360 case WXK_NUMPAD_ENTER
:
6361 if ( event
.ControlDown() )
6363 event
.Skip(); // to let the edit control have the return
6367 if ( GetGridCursorRow() < GetNumberRows()-1 )
6369 MoveCursorDown( event
.ShiftDown() );
6373 // at the bottom of a column
6374 DisableCellEditControl();
6384 if (event
.ShiftDown())
6386 if ( GetGridCursorCol() > 0 )
6388 MoveCursorLeft( false );
6393 DisableCellEditControl();
6398 if ( GetGridCursorCol() < GetNumberCols()-1 )
6400 MoveCursorRight( false );
6405 DisableCellEditControl();
6411 if ( event
.ControlDown() )
6413 MakeCellVisible( 0, 0 );
6414 SetCurrentCell( 0, 0 );
6423 if ( event
.ControlDown() )
6425 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6426 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6443 if ( event
.ControlDown() )
6447 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6448 m_currentCellCoords
.GetCol(),
6449 event
.ControlDown(),
6456 if ( !IsEditable() )
6458 MoveCursorRight( false );
6461 // Otherwise fall through to default
6464 // is it possible to edit the current cell at all?
6465 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6467 // yes, now check whether the cells editor accepts the key
6468 int row
= m_currentCellCoords
.GetRow();
6469 int col
= m_currentCellCoords
.GetCol();
6470 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6471 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6473 // <F2> is special and will always start editing, for
6474 // other keys - ask the editor itself
6475 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6476 || editor
->IsAcceptedKey(event
) )
6478 // ensure cell is visble
6479 MakeCellVisible(row
, col
);
6480 EnableCellEditControl();
6482 // a problem can arise if the cell is not completely
6483 // visible (even after calling MakeCellVisible the
6484 // control is not created and calling StartingKey will
6486 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6498 // let others process char events with modifiers or all
6499 // char events for readonly cells
6506 m_inOnKeyDown
= false;
6509 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6511 // try local handlers
6513 if ( event
.GetKeyCode() == WXK_SHIFT
)
6515 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6516 m_selectingBottomRight
!= wxGridNoCellCoords
)
6520 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6521 m_selectingTopLeft
.GetCol(),
6522 m_selectingBottomRight
.GetRow(),
6523 m_selectingBottomRight
.GetCol(),
6524 event
.ControlDown(),
6531 m_selectingTopLeft
= wxGridNoCellCoords
;
6532 m_selectingBottomRight
= wxGridNoCellCoords
;
6533 m_selectingKeyboard
= wxGridNoCellCoords
;
6537 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6541 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6543 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6545 // the event has been intercepted - do nothing
6549 wxClientDC
dc(m_gridWin
);
6552 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6554 DisableCellEditControl();
6556 if ( IsVisible( m_currentCellCoords
, false ) )
6559 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6560 if ( !m_gridLinesEnabled
)
6568 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6570 // Otherwise refresh redraws the highlight!
6571 m_currentCellCoords
= coords
;
6573 DrawGridCellArea(dc
,cells
);
6574 DrawAllGridLines( dc
, r
);
6578 m_currentCellCoords
= coords
;
6580 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6581 DrawCellHighlight(dc
, attr
);
6586 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6589 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6593 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6596 rightCol
= GetNumberCols() - 1;
6598 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6601 bottomRow
= GetNumberRows() - 1;
6605 if ( topRow
> bottomRow
)
6612 if ( leftCol
> rightCol
)
6619 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6620 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6622 // First the case that we selected a completely new area
6623 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6624 m_selectingBottomRight
== wxGridNoCellCoords
)
6627 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6628 wxGridCellCoords ( bottomRow
, rightCol
) );
6629 m_gridWin
->Refresh( false, &rect
);
6631 // Now handle changing an existing selection area.
6632 else if ( m_selectingTopLeft
!= updateTopLeft
||
6633 m_selectingBottomRight
!= updateBottomRight
)
6635 // Compute two optimal update rectangles:
6636 // Either one rectangle is a real subset of the
6637 // other, or they are (almost) disjoint!
6639 bool need_refresh
[4];
6643 need_refresh
[3] = false;
6646 // Store intermediate values
6647 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6648 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6649 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6650 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6652 // Determine the outer/inner coordinates.
6653 if (oldLeft
> leftCol
)
6659 if (oldTop
> topRow
)
6665 if (oldRight
< rightCol
)
6668 oldRight
= rightCol
;
6671 if (oldBottom
< bottomRow
)
6674 oldBottom
= bottomRow
;
6678 // Now, either the stuff marked old is the outer
6679 // rectangle or we don't have a situation where one
6680 // is contained in the other.
6682 if ( oldLeft
< leftCol
)
6684 // Refresh the newly selected or deselected
6685 // area to the left of the old or new selection.
6686 need_refresh
[0] = true;
6687 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6689 wxGridCellCoords ( oldBottom
,
6693 if ( oldTop
< topRow
)
6695 // Refresh the newly selected or deselected
6696 // area above the old or new selection.
6697 need_refresh
[1] = true;
6698 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6700 wxGridCellCoords ( topRow
- 1,
6704 if ( oldRight
> rightCol
)
6706 // Refresh the newly selected or deselected
6707 // area to the right of the old or new selection.
6708 need_refresh
[2] = true;
6709 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6711 wxGridCellCoords ( oldBottom
,
6715 if ( oldBottom
> bottomRow
)
6717 // Refresh the newly selected or deselected
6718 // area below the old or new selection.
6719 need_refresh
[3] = true;
6720 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6722 wxGridCellCoords ( oldBottom
,
6726 // various Refresh() calls
6727 for (i
= 0; i
< 4; i
++ )
6728 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6729 m_gridWin
->Refresh( false, &(rect
[i
]) );
6732 m_selectingTopLeft
= updateTopLeft
;
6733 m_selectingBottomRight
= updateBottomRight
;
6737 // ------ functions to get/send data (see also public functions)
6740 bool wxGrid::GetModelValues()
6742 // Hide the editor, so it won't hide a changed value.
6743 HideCellEditControl();
6747 // all we need to do is repaint the grid
6749 m_gridWin
->Refresh();
6757 bool wxGrid::SetModelValues()
6761 // Disable the editor, so it won't hide a changed value.
6762 // Do we also want to save the current value of the editor first?
6764 DisableCellEditControl();
6768 for ( row
= 0; row
< m_numRows
; row
++ )
6770 for ( col
= 0; col
< m_numCols
; col
++ )
6772 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6784 // Note - this function only draws cells that are in the list of
6785 // exposed cells (usually set from the update region by
6786 // CalcExposedCells)
6788 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6790 if ( !m_numRows
|| !m_numCols
) return;
6792 int i
, numCells
= cells
.GetCount();
6793 int row
, col
, cell_rows
, cell_cols
;
6794 wxGridCellCoordsArray redrawCells
;
6796 for ( i
= numCells
-1; i
>= 0; i
-- )
6798 row
= cells
[i
].GetRow();
6799 col
= cells
[i
].GetCol();
6800 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6802 // If this cell is part of a multicell block, find owner for repaint
6803 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6805 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6806 bool marked
= false;
6807 for ( int j
= 0; j
< numCells
; j
++ )
6809 if ( cell
== cells
[j
] )
6817 int count
= redrawCells
.GetCount();
6818 for (int j
= 0; j
< count
; j
++)
6820 if ( cell
== redrawCells
[j
] )
6826 if (!marked
) redrawCells
.Add( cell
);
6828 continue; // don't bother drawing this cell
6831 // If this cell is empty, find cell to left that might want to overflow
6832 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6834 for ( int l
= 0; l
< cell_rows
; l
++ )
6836 // find a cell in this row to left alreay marked for repaint
6838 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6839 if ((redrawCells
[k
].GetCol() < left
) &&
6840 (redrawCells
[k
].GetRow() == row
))
6841 left
=redrawCells
[k
].GetCol();
6843 if (left
== col
) left
= 0; // oh well
6845 for (int j
= col
-1; j
>= left
; j
--)
6847 if (!m_table
->IsEmptyCell(row
+l
, j
))
6849 if (GetCellOverflow(row
+l
, j
))
6851 wxGridCellCoords
cell(row
+l
, j
);
6852 bool marked
= false;
6854 for (int k
= 0; k
< numCells
; k
++)
6856 if ( cell
== cells
[k
] )
6864 int count
= redrawCells
.GetCount();
6865 for (int k
= 0; k
< count
; k
++)
6867 if ( cell
== redrawCells
[k
] )
6873 if (!marked
) redrawCells
.Add( cell
);
6881 DrawCell( dc
, cells
[i
] );
6884 numCells
= redrawCells
.GetCount();
6886 for ( i
= numCells
- 1; i
>= 0; i
-- )
6888 DrawCell( dc
, redrawCells
[i
] );
6893 void wxGrid::DrawGridSpace( wxDC
& dc
)
6896 m_gridWin
->GetClientSize( &cw
, &ch
);
6899 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6901 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6902 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6904 if ( right
> rightCol
|| bottom
> bottomRow
)
6907 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6909 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6910 dc
.SetPen( *wxTRANSPARENT_PEN
);
6912 if ( right
> rightCol
)
6914 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6917 if ( bottom
> bottomRow
)
6919 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6925 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6927 int row
= coords
.GetRow();
6928 int col
= coords
.GetCol();
6930 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6933 // we draw the cell border ourselves
6934 #if !WXGRID_DRAW_LINES
6935 if ( m_gridLinesEnabled
)
6936 DrawCellBorder( dc
, coords
);
6939 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6941 bool isCurrent
= coords
== m_currentCellCoords
;
6943 wxRect rect
= CellToRect( row
, col
);
6945 // if the editor is shown, we should use it and not the renderer
6946 // Note: However, only if it is really _shown_, i.e. not hidden!
6947 if ( isCurrent
&& IsCellEditControlShown() )
6949 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6950 editor
->PaintBackground(rect
, attr
);
6955 // but all the rest is drawn by the cell renderer and hence may be
6957 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6958 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6965 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6967 int row
= m_currentCellCoords
.GetRow();
6968 int col
= m_currentCellCoords
.GetCol();
6970 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6973 wxRect rect
= CellToRect(row
, col
);
6975 // hmmm... what could we do here to show that the cell is disabled?
6976 // for now, I just draw a thinner border than for the other ones, but
6977 // it doesn't look really good
6979 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6983 // The center of th drawn line is where the position/width/height of
6984 // the rectangle is actually at, (on wxMSW atr least,) so we will
6985 // reduce the size of the rectangle to compensate for the thickness of
6986 // the line. If this is too strange on non wxMSW platforms then
6987 // please #ifdef this appropriately.
6988 rect
.x
+= penWidth
/2;
6989 rect
.y
+= penWidth
/2;
6990 rect
.width
-= penWidth
-1;
6991 rect
.height
-= penWidth
-1;
6994 // Now draw the rectangle
6995 // use the cellHighlightColour if the cell is inside a selection, this
6996 // will ensure the cell is always visible.
6997 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6998 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6999 dc
.DrawRectangle(rect
);
7003 // VZ: my experiments with 3d borders...
7005 // how to properly set colours for arbitrary bg?
7006 wxCoord x1
= rect
.x
,
7008 x2
= rect
.x
+ rect
.width
-1,
7009 y2
= rect
.y
+ rect
.height
-1;
7011 dc
.SetPen(*wxWHITE_PEN
);
7012 dc
.DrawLine(x1
, y1
, x2
, y1
);
7013 dc
.DrawLine(x1
, y1
, x1
, y2
);
7015 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7016 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7018 dc
.SetPen(*wxBLACK_PEN
);
7019 dc
.DrawLine(x1
, y2
, x2
, y2
);
7020 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7025 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7027 int row
= coords
.GetRow();
7028 int col
= coords
.GetCol();
7029 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7032 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7034 wxRect rect
= CellToRect( row
, col
);
7036 // right hand border
7038 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7039 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7043 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7044 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7047 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7049 // This if block was previously in wxGrid::OnPaint but that doesn't
7050 // seem to get called under wxGTK - MB
7052 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7053 m_numRows
&& m_numCols
)
7055 m_currentCellCoords
.Set(0, 0);
7058 if ( IsCellEditControlShown() )
7060 // don't show highlight when the edit control is shown
7064 // if the active cell was repainted, repaint its highlight too because it
7065 // might have been damaged by the grid lines
7066 size_t count
= cells
.GetCount();
7067 for ( size_t n
= 0; n
< count
; n
++ )
7069 if ( cells
[n
] == m_currentCellCoords
)
7071 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7072 DrawCellHighlight(dc
, attr
);
7080 // TODO: remove this ???
7081 // This is used to redraw all grid lines e.g. when the grid line colour
7084 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7086 #if !WXGRID_DRAW_LINES
7090 if ( !m_gridLinesEnabled
||
7092 !m_numCols
) return;
7094 int top
, bottom
, left
, right
;
7096 #if 0 //#ifndef __WXGTK__
7100 m_gridWin
->GetClientSize(&cw
, &ch
);
7102 // virtual coords of visible area
7104 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7105 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7110 reg
.GetBox(x
, y
, w
, h
);
7111 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7112 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7116 m_gridWin
->GetClientSize(&cw
, &ch
);
7117 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7118 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7121 // avoid drawing grid lines past the last row and col
7123 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7124 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7126 // no gridlines inside multicells, clip them out
7127 int leftCol
= internalXToCol(left
);
7128 int topRow
= internalYToRow(top
);
7129 int rightCol
= internalXToCol(right
);
7130 int bottomRow
= internalYToRow(bottom
);
7131 wxRegion
clippedcells(0, 0, cw
, ch
);
7134 int i
, j
, cell_rows
, cell_cols
;
7137 for (j
=topRow
; j
<bottomRow
; j
++)
7139 for (i
=leftCol
; i
<rightCol
; i
++)
7141 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7142 if ((cell_rows
> 1) || (cell_cols
> 1))
7144 rect
= CellToRect(j
,i
);
7145 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7146 clippedcells
.Subtract(rect
);
7148 else if ((cell_rows
< 0) || (cell_cols
< 0))
7150 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7151 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7152 clippedcells
.Subtract(rect
);
7156 dc
.SetClippingRegion( clippedcells
);
7158 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7160 // horizontal grid lines
7162 // already declared above - int i;
7163 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7165 int bot
= GetRowBottom(i
) - 1;
7174 dc
.DrawLine( left
, bot
, right
, bot
);
7179 // vertical grid lines
7181 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7183 int colRight
= GetColRight(i
) - 1;
7184 if ( colRight
> right
)
7189 if ( colRight
>= left
)
7191 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7194 dc
.DestroyClippingRegion();
7198 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7200 if ( !m_numRows
) return;
7203 size_t numLabels
= rows
.GetCount();
7205 for ( i
= 0; i
< numLabels
; i
++ )
7207 DrawRowLabel( dc
, rows
[i
] );
7212 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7214 if ( GetRowHeight(row
) <= 0 )
7220 rect
.SetY( GetRowTop(row
) + 1 );
7221 rect
.SetWidth( m_rowLabelWidth
- 2 );
7222 rect
.SetHeight( GetRowHeight(row
) - 2 );
7224 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7226 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7228 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7230 int rowTop
= GetRowTop(row
),
7231 rowBottom
= GetRowBottom(row
) - 1;
7233 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7234 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7235 m_rowLabelWidth
-1, rowBottom
);
7237 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7239 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7241 dc
.SetPen( *wxWHITE_PEN
);
7242 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7243 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7245 dc
.SetBackgroundMode( wxTRANSPARENT
);
7246 dc
.SetTextForeground( GetLabelTextColour() );
7247 dc
.SetFont( GetLabelFont() );
7250 GetRowLabelAlignment( &hAlign
, &vAlign
);
7253 rect
.SetY( GetRowTop(row
) + 2 );
7254 rect
.SetWidth( m_rowLabelWidth
- 4 );
7255 rect
.SetHeight( GetRowHeight(row
) - 4 );
7256 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7260 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7262 if ( !m_numCols
) return;
7265 size_t numLabels
= cols
.GetCount();
7267 for ( i
= 0; i
< numLabels
; i
++ )
7269 DrawColLabel( dc
, cols
[i
] );
7274 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7276 if ( GetColWidth(col
) <= 0 )
7279 int colLeft
= GetColLeft(col
);
7283 rect
.SetX( colLeft
+ 1 );
7285 rect
.SetWidth( GetColWidth(col
) - 2 );
7286 rect
.SetHeight( m_colLabelHeight
- 2 );
7288 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7290 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7292 int colRight
= GetColRight(col
) - 1;
7294 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7295 dc
.DrawLine( colRight
, 0,
7296 colRight
, m_colLabelHeight
-1 );
7298 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7300 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7301 colRight
+1, m_colLabelHeight
-1 );
7303 dc
.SetPen( *wxWHITE_PEN
);
7304 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7305 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7307 dc
.SetBackgroundMode( wxTRANSPARENT
);
7308 dc
.SetTextForeground( GetLabelTextColour() );
7309 dc
.SetFont( GetLabelFont() );
7311 int hAlign
, vAlign
, orient
;
7312 GetColLabelAlignment( &hAlign
, &vAlign
);
7313 orient
= GetColLabelTextOrientation();
7315 rect
.SetX( colLeft
+ 2 );
7317 rect
.SetWidth( GetColWidth(col
) - 4 );
7318 rect
.SetHeight( m_colLabelHeight
- 4 );
7319 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7322 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7323 const wxString
& value
,
7327 int textOrientation
)
7329 wxArrayString lines
;
7331 StringToLines( value
, lines
);
7334 //Forward to new API.
7335 DrawTextRectangle( dc
,
7344 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7345 const wxArrayString
& lines
,
7349 int textOrientation
)
7351 long textWidth
, textHeight
;
7352 long lineWidth
, lineHeight
;
7355 dc
.SetClippingRegion( rect
);
7357 nLines
= lines
.GetCount();
7361 float x
= 0.0, y
= 0.0;
7363 if( textOrientation
== wxHORIZONTAL
)
7364 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7366 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7370 case wxALIGN_BOTTOM
:
7371 if( textOrientation
== wxHORIZONTAL
)
7372 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7374 x
= rect
.x
+ rect
.width
- textWidth
;
7377 case wxALIGN_CENTRE
:
7378 if( textOrientation
== wxHORIZONTAL
)
7379 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7381 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7386 if( textOrientation
== wxHORIZONTAL
)
7393 // Align each line of a multi-line label
7394 for( l
= 0; l
< nLines
; l
++ )
7396 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7398 switch( horizAlign
)
7401 if( textOrientation
== wxHORIZONTAL
)
7402 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7404 y
= rect
.y
+ lineWidth
+ 1;
7407 case wxALIGN_CENTRE
:
7408 if( textOrientation
== wxHORIZONTAL
)
7409 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7411 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7416 if( textOrientation
== wxHORIZONTAL
)
7419 y
= rect
.y
+ rect
.height
- 1;
7423 if( textOrientation
== wxHORIZONTAL
)
7425 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7430 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7435 dc
.DestroyClippingRegion();
7439 // Split multi line text up into an array of strings. Any existing
7440 // contents of the string array are preserved.
7442 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7446 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7447 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7449 while ( startPos
< (int)tVal
.Length() )
7451 pos
= tVal
.Mid(startPos
).Find( eol
);
7456 else if ( pos
== 0 )
7458 lines
.Add( wxEmptyString
);
7462 lines
.Add( value
.Mid(startPos
, pos
) );
7466 if ( startPos
< (int)value
.Length() )
7468 lines
.Add( value
.Mid( startPos
) );
7473 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7474 const wxArrayString
& lines
,
7475 long *width
, long *height
)
7482 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7484 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7485 w
= wxMax( w
, lineW
);
7494 // ------ Batch processing.
7496 void wxGrid::EndBatch()
7498 if ( m_batchCount
> 0 )
7501 if ( !m_batchCount
)
7504 m_rowLabelWin
->Refresh();
7505 m_colLabelWin
->Refresh();
7506 m_cornerLabelWin
->Refresh();
7507 m_gridWin
->Refresh();
7512 // Use this, rather than wxWindow::Refresh(), to force an immediate
7513 // repainting of the grid. Has no effect if you are already inside a
7514 // BeginBatch / EndBatch block.
7516 void wxGrid::ForceRefresh()
7524 // ------ Edit control functions
7528 void wxGrid::EnableEditing( bool edit
)
7530 // TODO: improve this ?
7532 if ( edit
!= m_editable
)
7534 if(!edit
) EnableCellEditControl(edit
);
7540 void wxGrid::EnableCellEditControl( bool enable
)
7545 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7546 SetCurrentCell( 0, 0 );
7548 if ( enable
!= m_cellEditCtrlEnabled
)
7552 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7555 // this should be checked by the caller!
7556 wxASSERT_MSG( CanEnableCellControl(),
7557 _T("can't enable editing for this cell!") );
7559 // do it before ShowCellEditControl()
7560 m_cellEditCtrlEnabled
= enable
;
7562 ShowCellEditControl();
7566 //FIXME:add veto support
7567 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7569 HideCellEditControl();
7570 SaveEditControlValue();
7572 // do it after HideCellEditControl()
7573 m_cellEditCtrlEnabled
= enable
;
7578 bool wxGrid::IsCurrentCellReadOnly() const
7581 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7582 bool readonly
= attr
->IsReadOnly();
7588 bool wxGrid::CanEnableCellControl() const
7590 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7591 !IsCurrentCellReadOnly();
7595 bool wxGrid::IsCellEditControlEnabled() const
7597 // the cell edit control might be disable for all cells or just for the
7598 // current one if it's read only
7599 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7602 bool wxGrid::IsCellEditControlShown() const
7604 bool isShown
= false;
7606 if ( m_cellEditCtrlEnabled
)
7608 int row
= m_currentCellCoords
.GetRow();
7609 int col
= m_currentCellCoords
.GetCol();
7610 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7611 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7616 if ( editor
->IsCreated() )
7618 isShown
= editor
->GetControl()->IsShown();
7628 void wxGrid::ShowCellEditControl()
7630 if ( IsCellEditControlEnabled() )
7632 if ( !IsVisible( m_currentCellCoords
) )
7634 m_cellEditCtrlEnabled
= false;
7639 wxRect rect
= CellToRect( m_currentCellCoords
);
7640 int row
= m_currentCellCoords
.GetRow();
7641 int col
= m_currentCellCoords
.GetCol();
7643 // if this is part of a multicell, find owner (topleft)
7644 int cell_rows
, cell_cols
;
7645 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7646 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7650 m_currentCellCoords
.SetRow( row
);
7651 m_currentCellCoords
.SetCol( col
);
7654 // convert to scrolled coords
7656 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7658 // done in PaintBackground()
7660 // erase the highlight and the cell contents because the editor
7661 // might not cover the entire cell
7662 wxClientDC
dc( m_gridWin
);
7664 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7665 dc
.SetPen(*wxTRANSPARENT_PEN
);
7666 dc
.DrawRectangle(rect
);
7669 // cell is shifted by one pixel
7670 // However, don't allow x or y to become negative
7671 // since the SetSize() method interprets that as
7678 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7679 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7680 if ( !editor
->IsCreated() )
7682 editor
->Create(m_gridWin
, wxID_ANY
,
7683 new wxGridCellEditorEvtHandler(this, editor
));
7685 wxGridEditorCreatedEvent
evt(GetId(),
7686 wxEVT_GRID_EDITOR_CREATED
,
7690 editor
->GetControl());
7691 GetEventHandler()->ProcessEvent(evt
);
7695 // resize editor to overflow into righthand cells if allowed
7696 int maxWidth
= rect
.width
;
7697 wxString value
= GetCellValue(row
, col
);
7698 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7701 GetTextExtent(value
, &maxWidth
, &y
,
7702 NULL
, NULL
, &attr
->GetFont());
7703 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7705 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7706 if (rect
.x
+maxWidth
> client_right
)
7707 maxWidth
= client_right
- rect
.x
;
7709 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7711 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7712 // may have changed earlier
7713 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7716 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7717 // looks weird going over a multicell
7718 if (m_table
->IsEmptyCell(row
,i
) &&
7719 (rect
.width
< maxWidth
) && (c_rows
== 1))
7720 rect
.width
+= GetColWidth(i
);
7724 if (rect
.GetRight() > client_right
)
7725 rect
.SetRight(client_right
-1);
7728 editor
->SetCellAttr(attr
);
7729 editor
->SetSize( rect
);
7730 editor
->Show( true, attr
);
7732 // recalc dimensions in case we need to
7733 // expand the scrolled window to account for editor
7736 editor
->BeginEdit(row
, col
, this);
7737 editor
->SetCellAttr(NULL
);
7746 void wxGrid::HideCellEditControl()
7748 if ( IsCellEditControlEnabled() )
7750 int row
= m_currentCellCoords
.GetRow();
7751 int col
= m_currentCellCoords
.GetCol();
7753 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7754 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7755 editor
->Show( false );
7758 m_gridWin
->SetFocus();
7759 // refresh whole row to the right
7760 wxRect
rect( CellToRect(row
, col
) );
7761 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7762 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7763 m_gridWin
->Refresh( false, &rect
);
7768 void wxGrid::SaveEditControlValue()
7770 if ( IsCellEditControlEnabled() )
7772 int row
= m_currentCellCoords
.GetRow();
7773 int col
= m_currentCellCoords
.GetCol();
7775 wxString oldval
= GetCellValue(row
,col
);
7777 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7778 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7779 bool changed
= editor
->EndEdit(row
, col
, this);
7786 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7787 m_currentCellCoords
.GetRow(),
7788 m_currentCellCoords
.GetCol() ) < 0 ) {
7790 // Event has been vetoed, set the data back.
7791 SetCellValue(row
,col
,oldval
);
7799 // ------ Grid location functions
7800 // Note that all of these functions work with the logical coordinates of
7801 // grid cells and labels so you will need to convert from device
7802 // coordinates for mouse events etc.
7805 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7807 int row
= YToRow(y
);
7808 int col
= XToCol(x
);
7810 if ( row
== -1 || col
== -1 )
7812 coords
= wxGridNoCellCoords
;
7816 coords
.Set( row
, col
);
7821 // Internal Helper function for computing row or column from some
7822 // (unscrolled) coordinate value, using either
7823 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7824 // of m_rowBottoms/m_ColRights to speed up the search!
7826 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7827 const wxArrayInt
& BorderArray
, int nMax
,
7832 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7838 size_t i_max
= coord
/ defaultDist
,
7841 if (BorderArray
.IsEmpty())
7843 if((int) i_max
< nMax
)
7845 return clipToMinMax
? nMax
- 1 : -1;
7848 if ( i_max
>= BorderArray
.GetCount())
7849 i_max
= BorderArray
.GetCount() - 1;
7852 if ( coord
>= BorderArray
[i_max
])
7856 i_max
= coord
/ minDist
;
7858 i_max
= BorderArray
.GetCount() - 1;
7860 if ( i_max
>= BorderArray
.GetCount())
7861 i_max
= BorderArray
.GetCount() - 1;
7863 if ( coord
>= BorderArray
[i_max
])
7864 return clipToMinMax
? (int)i_max
: -1;
7865 if ( coord
< BorderArray
[0] )
7868 while ( i_max
- i_min
> 0 )
7870 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7871 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7872 if (coord
>= BorderArray
[ i_max
- 1])
7876 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7877 if (coord
< BorderArray
[median
])
7885 int wxGrid::YToRow( int y
)
7887 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7888 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7892 int wxGrid::XToCol( int x
)
7894 return CoordToRowOrCol(x
, m_defaultColWidth
,
7895 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7899 // return the row number that that the y coord is near the edge of, or
7900 // -1 if not near an edge
7902 int wxGrid::YToEdgeOfRow( int y
)
7905 i
= internalYToRow(y
);
7907 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7909 // We know that we are in row i, test whether we are
7910 // close enough to lower or upper border, respectively.
7911 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7913 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7921 // return the col number that that the x coord is near the edge of, or
7922 // -1 if not near an edge
7924 int wxGrid::XToEdgeOfCol( int x
)
7927 i
= internalXToCol(x
);
7929 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7931 // We know that we are in column i, test whether we are
7932 // close enough to right or left border, respectively.
7933 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7935 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7943 wxRect
wxGrid::CellToRect( int row
, int col
)
7945 wxRect
rect( -1, -1, -1, -1 );
7947 if ( row
>= 0 && row
< m_numRows
&&
7948 col
>= 0 && col
< m_numCols
)
7950 int i
, cell_rows
, cell_cols
;
7951 rect
.width
= rect
.height
= 0;
7952 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7953 // if negative then find multicell owner
7954 if (cell_rows
< 0) row
+= cell_rows
;
7955 if (cell_cols
< 0) col
+= cell_cols
;
7956 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7958 rect
.x
= GetColLeft(col
);
7959 rect
.y
= GetRowTop(row
);
7960 for (i
=col
; i
<col
+cell_cols
; i
++)
7961 rect
.width
+= GetColWidth(i
);
7962 for (i
=row
; i
<row
+cell_rows
; i
++)
7963 rect
.height
+= GetRowHeight(i
);
7966 // if grid lines are enabled, then the area of the cell is a bit smaller
7967 if (m_gridLinesEnabled
) {
7975 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7977 // get the cell rectangle in logical coords
7979 wxRect
r( CellToRect( row
, col
) );
7981 // convert to device coords
7983 int left
, top
, right
, bottom
;
7984 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7985 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7987 // check against the client area of the grid window
7990 m_gridWin
->GetClientSize( &cw
, &ch
);
7992 if ( wholeCellVisible
)
7994 // is the cell wholly visible ?
7996 return ( left
>= 0 && right
<= cw
&&
7997 top
>= 0 && bottom
<= ch
);
8001 // is the cell partly visible ?
8003 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8004 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8009 // make the specified cell location visible by doing a minimal amount
8012 void wxGrid::MakeCellVisible( int row
, int col
)
8016 int xpos
= -1, ypos
= -1;
8018 if ( row
>= 0 && row
< m_numRows
&&
8019 col
>= 0 && col
< m_numCols
)
8021 // get the cell rectangle in logical coords
8023 wxRect
r( CellToRect( row
, col
) );
8025 // convert to device coords
8027 int left
, top
, right
, bottom
;
8028 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8029 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8032 m_gridWin
->GetClientSize( &cw
, &ch
);
8038 else if ( bottom
> ch
)
8040 int h
= r
.GetHeight();
8042 for ( i
= row
-1; i
>= 0; i
-- )
8044 int rowHeight
= GetRowHeight(i
);
8045 if ( h
+ rowHeight
> ch
)
8052 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8053 // have rounding errors (this is important, because if we do, we
8054 // might not scroll at all and some cells won't be redrawn)
8056 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8058 ypos
+= GRID_SCROLL_LINE_Y
;
8065 else if ( right
> cw
)
8067 // position the view so that the cell is on the right
8069 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8070 xpos
= x0
+ (right
- cw
);
8072 // see comment for ypos above
8073 xpos
+= GRID_SCROLL_LINE_X
;
8076 if ( xpos
!= -1 || ypos
!= -1 )
8079 xpos
/= GRID_SCROLL_LINE_X
;
8081 ypos
/= GRID_SCROLL_LINE_Y
;
8082 Scroll( xpos
, ypos
);
8090 // ------ Grid cursor movement functions
8093 bool wxGrid::MoveCursorUp( bool expandSelection
)
8095 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8096 m_currentCellCoords
.GetRow() >= 0 )
8098 if ( expandSelection
)
8100 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8101 m_selectingKeyboard
= m_currentCellCoords
;
8102 if ( m_selectingKeyboard
.GetRow() > 0 )
8104 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8105 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8106 m_selectingKeyboard
.GetCol() );
8107 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8110 else if ( m_currentCellCoords
.GetRow() > 0 )
8113 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8114 m_currentCellCoords
.GetCol() );
8115 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8116 m_currentCellCoords
.GetCol() );
8127 bool wxGrid::MoveCursorDown( bool expandSelection
)
8129 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8130 m_currentCellCoords
.GetRow() < m_numRows
)
8132 if ( expandSelection
)
8134 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8135 m_selectingKeyboard
= m_currentCellCoords
;
8136 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8138 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8139 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8140 m_selectingKeyboard
.GetCol() );
8141 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8144 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8147 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8148 m_currentCellCoords
.GetCol() );
8149 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8150 m_currentCellCoords
.GetCol() );
8161 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8163 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8164 m_currentCellCoords
.GetCol() >= 0 )
8166 if ( expandSelection
)
8168 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8169 m_selectingKeyboard
= m_currentCellCoords
;
8170 if ( m_selectingKeyboard
.GetCol() > 0 )
8172 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8173 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8174 m_selectingKeyboard
.GetCol() );
8175 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8178 else if ( m_currentCellCoords
.GetCol() > 0 )
8181 MakeCellVisible( m_currentCellCoords
.GetRow(),
8182 m_currentCellCoords
.GetCol() - 1 );
8183 SetCurrentCell( m_currentCellCoords
.GetRow(),
8184 m_currentCellCoords
.GetCol() - 1 );
8195 bool wxGrid::MoveCursorRight( bool expandSelection
)
8197 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8198 m_currentCellCoords
.GetCol() < m_numCols
)
8200 if ( expandSelection
)
8202 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8203 m_selectingKeyboard
= m_currentCellCoords
;
8204 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8206 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8207 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8208 m_selectingKeyboard
.GetCol() );
8209 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8212 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8215 MakeCellVisible( m_currentCellCoords
.GetRow(),
8216 m_currentCellCoords
.GetCol() + 1 );
8217 SetCurrentCell( m_currentCellCoords
.GetRow(),
8218 m_currentCellCoords
.GetCol() + 1 );
8229 bool wxGrid::MovePageUp()
8231 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8233 int row
= m_currentCellCoords
.GetRow();
8237 m_gridWin
->GetClientSize( &cw
, &ch
);
8239 int y
= GetRowTop(row
);
8240 int newRow
= internalYToRow( y
- ch
+ 1 );
8242 if ( newRow
== row
)
8244 //row > 0 , so newrow can never be less than 0 here.
8248 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8249 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8257 bool wxGrid::MovePageDown()
8259 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8261 int row
= m_currentCellCoords
.GetRow();
8262 if ( (row
+1) < m_numRows
)
8265 m_gridWin
->GetClientSize( &cw
, &ch
);
8267 int y
= GetRowTop(row
);
8268 int newRow
= internalYToRow( y
+ ch
);
8269 if ( newRow
== row
)
8271 // row < m_numRows , so newrow can't overflow here.
8275 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8276 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8284 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8287 m_currentCellCoords
!= wxGridNoCellCoords
&&
8288 m_currentCellCoords
.GetRow() > 0 )
8290 int row
= m_currentCellCoords
.GetRow();
8291 int col
= m_currentCellCoords
.GetCol();
8293 if ( m_table
->IsEmptyCell(row
, col
) )
8295 // starting in an empty cell: find the next block of
8301 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8304 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8306 // starting at the top of a block: find the next block
8312 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8317 // starting within a block: find the top of the block
8322 if ( m_table
->IsEmptyCell(row
, col
) )
8330 MakeCellVisible( row
, col
);
8331 if ( expandSelection
)
8333 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8334 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8339 SetCurrentCell( row
, col
);
8347 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8350 m_currentCellCoords
!= wxGridNoCellCoords
&&
8351 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8353 int row
= m_currentCellCoords
.GetRow();
8354 int col
= m_currentCellCoords
.GetCol();
8356 if ( m_table
->IsEmptyCell(row
, col
) )
8358 // starting in an empty cell: find the next block of
8361 while ( row
< m_numRows
-1 )
8364 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8367 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8369 // starting at the bottom of a block: find the next block
8372 while ( row
< m_numRows
-1 )
8375 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8380 // starting within a block: find the bottom of the block
8382 while ( row
< m_numRows
-1 )
8385 if ( m_table
->IsEmptyCell(row
, col
) )
8393 MakeCellVisible( row
, col
);
8394 if ( expandSelection
)
8396 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8397 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8402 SetCurrentCell( row
, col
);
8411 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8414 m_currentCellCoords
!= wxGridNoCellCoords
&&
8415 m_currentCellCoords
.GetCol() > 0 )
8417 int row
= m_currentCellCoords
.GetRow();
8418 int col
= m_currentCellCoords
.GetCol();
8420 if ( m_table
->IsEmptyCell(row
, col
) )
8422 // starting in an empty cell: find the next block of
8428 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8431 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8433 // starting at the left of a block: find the next block
8439 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8444 // starting within a block: find the left of the block
8449 if ( m_table
->IsEmptyCell(row
, col
) )
8457 MakeCellVisible( row
, col
);
8458 if ( expandSelection
)
8460 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8461 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8466 SetCurrentCell( row
, col
);
8475 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8478 m_currentCellCoords
!= wxGridNoCellCoords
&&
8479 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8481 int row
= m_currentCellCoords
.GetRow();
8482 int col
= m_currentCellCoords
.GetCol();
8484 if ( m_table
->IsEmptyCell(row
, col
) )
8486 // starting in an empty cell: find the next block of
8489 while ( col
< m_numCols
-1 )
8492 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8495 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8497 // starting at the right of a block: find the next block
8500 while ( col
< m_numCols
-1 )
8503 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8508 // starting within a block: find the right of the block
8510 while ( col
< m_numCols
-1 )
8513 if ( m_table
->IsEmptyCell(row
, col
) )
8521 MakeCellVisible( row
, col
);
8522 if ( expandSelection
)
8524 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8525 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8530 SetCurrentCell( row
, col
);
8542 // ------ Label values and formatting
8545 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8547 *horiz
= m_rowLabelHorizAlign
;
8548 *vert
= m_rowLabelVertAlign
;
8551 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8553 *horiz
= m_colLabelHorizAlign
;
8554 *vert
= m_colLabelVertAlign
;
8557 int wxGrid::GetColLabelTextOrientation()
8559 return m_colLabelTextOrientation
;
8562 wxString
wxGrid::GetRowLabelValue( int row
)
8566 return m_table
->GetRowLabelValue( row
);
8576 wxString
wxGrid::GetColLabelValue( int col
)
8580 return m_table
->GetColLabelValue( col
);
8591 void wxGrid::SetRowLabelSize( int width
)
8593 width
= wxMax( width
, 0 );
8594 if ( width
!= m_rowLabelWidth
)
8598 m_rowLabelWin
->Show( false );
8599 m_cornerLabelWin
->Show( false );
8601 else if ( m_rowLabelWidth
== 0 )
8603 m_rowLabelWin
->Show( true );
8604 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8607 m_rowLabelWidth
= width
;
8609 wxScrolledWindow::Refresh( true );
8614 void wxGrid::SetColLabelSize( int height
)
8616 height
= wxMax( height
, 0 );
8617 if ( height
!= m_colLabelHeight
)
8621 m_colLabelWin
->Show( false );
8622 m_cornerLabelWin
->Show( false );
8624 else if ( m_colLabelHeight
== 0 )
8626 m_colLabelWin
->Show( true );
8627 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8630 m_colLabelHeight
= height
;
8632 wxScrolledWindow::Refresh( true );
8637 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8639 if ( m_labelBackgroundColour
!= colour
)
8641 m_labelBackgroundColour
= colour
;
8642 m_rowLabelWin
->SetBackgroundColour( colour
);
8643 m_colLabelWin
->SetBackgroundColour( colour
);
8644 m_cornerLabelWin
->SetBackgroundColour( colour
);
8646 if ( !GetBatchCount() )
8648 m_rowLabelWin
->Refresh();
8649 m_colLabelWin
->Refresh();
8650 m_cornerLabelWin
->Refresh();
8655 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8657 if ( m_labelTextColour
!= colour
)
8659 m_labelTextColour
= colour
;
8660 if ( !GetBatchCount() )
8662 m_rowLabelWin
->Refresh();
8663 m_colLabelWin
->Refresh();
8668 void wxGrid::SetLabelFont( const wxFont
& font
)
8671 if ( !GetBatchCount() )
8673 m_rowLabelWin
->Refresh();
8674 m_colLabelWin
->Refresh();
8678 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8680 // allow old (incorrect) defs to be used
8683 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8684 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8685 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8690 case wxTOP
: vert
= wxALIGN_TOP
; break;
8691 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8692 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8695 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8697 m_rowLabelHorizAlign
= horiz
;
8700 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8702 m_rowLabelVertAlign
= vert
;
8705 if ( !GetBatchCount() )
8707 m_rowLabelWin
->Refresh();
8711 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8713 // allow old (incorrect) defs to be used
8716 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8717 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8718 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8723 case wxTOP
: vert
= wxALIGN_TOP
; break;
8724 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8725 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8728 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8730 m_colLabelHorizAlign
= horiz
;
8733 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8735 m_colLabelVertAlign
= vert
;
8738 if ( !GetBatchCount() )
8740 m_colLabelWin
->Refresh();
8744 // Note: under MSW, the default column label font must be changed because it
8745 // does not support vertical printing
8747 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8748 // pGrid->SetLabelFont(font);
8749 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8751 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8753 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8755 m_colLabelTextOrientation
= textOrientation
;
8758 if ( !GetBatchCount() )
8760 m_colLabelWin
->Refresh();
8764 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8768 m_table
->SetRowLabelValue( row
, s
);
8769 if ( !GetBatchCount() )
8771 wxRect rect
= CellToRect( row
, 0);
8772 if ( rect
.height
> 0 )
8774 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8776 rect
.width
= m_rowLabelWidth
;
8777 m_rowLabelWin
->Refresh( true, &rect
);
8783 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8787 m_table
->SetColLabelValue( col
, s
);
8788 if ( !GetBatchCount() )
8790 wxRect rect
= CellToRect( 0, col
);
8791 if ( rect
.width
> 0 )
8793 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8795 rect
.height
= m_colLabelHeight
;
8796 m_colLabelWin
->Refresh( true, &rect
);
8802 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8804 if ( m_gridLineColour
!= colour
)
8806 m_gridLineColour
= colour
;
8808 wxClientDC
dc( m_gridWin
);
8810 DrawAllGridLines( dc
, wxRegion() );
8815 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8817 if ( m_cellHighlightColour
!= colour
)
8819 m_cellHighlightColour
= colour
;
8821 wxClientDC
dc( m_gridWin
);
8823 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8824 DrawCellHighlight(dc
, attr
);
8829 void wxGrid::SetCellHighlightPenWidth(int width
)
8831 if (m_cellHighlightPenWidth
!= width
) {
8832 m_cellHighlightPenWidth
= width
;
8834 // Just redrawing the cell highlight is not enough since that won't
8835 // make any visible change if the the thickness is getting smaller.
8836 int row
= m_currentCellCoords
.GetRow();
8837 int col
= m_currentCellCoords
.GetCol();
8838 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8840 wxRect rect
= CellToRect(row
, col
);
8841 m_gridWin
->Refresh(true, &rect
);
8845 void wxGrid::SetCellHighlightROPenWidth(int width
)
8847 if (m_cellHighlightROPenWidth
!= width
) {
8848 m_cellHighlightROPenWidth
= width
;
8850 // Just redrawing the cell highlight is not enough since that won't
8851 // make any visible change if the the thickness is getting smaller.
8852 int row
= m_currentCellCoords
.GetRow();
8853 int col
= m_currentCellCoords
.GetCol();
8854 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8856 wxRect rect
= CellToRect(row
, col
);
8857 m_gridWin
->Refresh(true, &rect
);
8861 void wxGrid::EnableGridLines( bool enable
)
8863 if ( enable
!= m_gridLinesEnabled
)
8865 m_gridLinesEnabled
= enable
;
8867 if ( !GetBatchCount() )
8871 wxClientDC
dc( m_gridWin
);
8873 DrawAllGridLines( dc
, wxRegion() );
8877 m_gridWin
->Refresh();
8884 int wxGrid::GetDefaultRowSize()
8886 return m_defaultRowHeight
;
8889 int wxGrid::GetRowSize( int row
)
8891 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8893 return GetRowHeight(row
);
8896 int wxGrid::GetDefaultColSize()
8898 return m_defaultColWidth
;
8901 int wxGrid::GetColSize( int col
)
8903 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8905 return GetColWidth(col
);
8908 // ============================================================================
8909 // access to the grid attributes: each of them has a default value in the grid
8910 // itself and may be overidden on a per-cell basis
8911 // ============================================================================
8913 // ----------------------------------------------------------------------------
8914 // setting default attributes
8915 // ----------------------------------------------------------------------------
8917 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8919 m_defaultCellAttr
->SetBackgroundColour(col
);
8921 m_gridWin
->SetBackgroundColour(col
);
8925 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8927 m_defaultCellAttr
->SetTextColour(col
);
8930 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8932 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8935 void wxGrid::SetDefaultCellOverflow( bool allow
)
8937 m_defaultCellAttr
->SetOverflow(allow
);
8940 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8942 m_defaultCellAttr
->SetFont(font
);
8946 // For editors and renderers the type registry takes precedence over the
8947 // default attr, so we need to register the new editor/renderer for the string
8948 // data type in order to make setting a default editor/renderer appear to
8951 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8953 RegisterDataType(wxGRID_VALUE_STRING
,
8955 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
8958 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8960 RegisterDataType(wxGRID_VALUE_STRING
,
8961 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
8965 // ----------------------------------------------------------------------------
8966 // access to the default attrbiutes
8967 // ----------------------------------------------------------------------------
8969 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8971 return m_defaultCellAttr
->GetBackgroundColour();
8974 wxColour
wxGrid::GetDefaultCellTextColour()
8976 return m_defaultCellAttr
->GetTextColour();
8979 wxFont
wxGrid::GetDefaultCellFont()
8981 return m_defaultCellAttr
->GetFont();
8984 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8986 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8989 bool wxGrid::GetDefaultCellOverflow()
8991 return m_defaultCellAttr
->GetOverflow();
8994 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8996 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8999 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9001 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
9004 // ----------------------------------------------------------------------------
9005 // access to cell attributes
9006 // ----------------------------------------------------------------------------
9008 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9010 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9011 wxColour colour
= attr
->GetBackgroundColour();
9016 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9018 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9019 wxColour colour
= attr
->GetTextColour();
9024 wxFont
wxGrid::GetCellFont( int row
, int col
)
9026 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9027 wxFont font
= attr
->GetFont();
9032 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9034 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9035 attr
->GetAlignment(horiz
, vert
);
9039 bool wxGrid::GetCellOverflow( int row
, int col
)
9041 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9042 bool allow
= attr
->GetOverflow();
9047 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9049 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9050 attr
->GetSize( num_rows
, num_cols
);
9054 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9056 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9057 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9063 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9065 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9066 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9072 bool wxGrid::IsReadOnly(int row
, int col
) const
9074 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9075 bool isReadOnly
= attr
->IsReadOnly();
9080 // ----------------------------------------------------------------------------
9081 // attribute support: cache, automatic provider creation, ...
9082 // ----------------------------------------------------------------------------
9084 bool wxGrid::CanHaveAttributes()
9091 return m_table
->CanHaveAttributes();
9094 void wxGrid::ClearAttrCache()
9096 if ( m_attrCache
.row
!= -1 )
9098 wxSafeDecRef(m_attrCache
.attr
);
9099 m_attrCache
.attr
= NULL
;
9100 m_attrCache
.row
= -1;
9104 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9108 wxGrid
*self
= (wxGrid
*)this; // const_cast
9110 self
->ClearAttrCache();
9111 self
->m_attrCache
.row
= row
;
9112 self
->m_attrCache
.col
= col
;
9113 self
->m_attrCache
.attr
= attr
;
9118 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9120 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9122 *attr
= m_attrCache
.attr
;
9123 wxSafeIncRef(m_attrCache
.attr
);
9125 #ifdef DEBUG_ATTR_CACHE
9126 gs_nAttrCacheHits
++;
9133 #ifdef DEBUG_ATTR_CACHE
9134 gs_nAttrCacheMisses
++;
9140 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9142 wxGridCellAttr
*attr
= NULL
;
9143 // Additional test to avoid looking at the cache e.g. for
9144 // wxNoCellCoords, as this will confuse memory management.
9147 if ( !LookupAttr(row
, col
, &attr
) )
9149 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9150 : (wxGridCellAttr
*)NULL
;
9151 CacheAttr(row
, col
, attr
);
9156 attr
->SetDefAttr(m_defaultCellAttr
);
9160 attr
= m_defaultCellAttr
;
9167 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9169 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9170 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9172 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9173 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9175 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9178 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9180 // artificially inc the ref count to match DecRef() in caller
9182 m_table
->SetAttr(attr
, row
, col
);
9188 // ----------------------------------------------------------------------------
9189 // setting column attributes (wrappers around SetColAttr)
9190 // ----------------------------------------------------------------------------
9192 void wxGrid::SetColFormatBool(int col
)
9194 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9197 void wxGrid::SetColFormatNumber(int col
)
9199 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9202 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9204 wxString typeName
= wxGRID_VALUE_FLOAT
;
9205 if ( (width
!= -1) || (precision
!= -1) )
9207 typeName
<< _T(':') << width
<< _T(',') << precision
;
9210 SetColFormatCustom(col
, typeName
);
9213 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9215 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9217 attr
= new wxGridCellAttr
;
9218 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9219 attr
->SetRenderer(renderer
);
9221 SetColAttr(col
, attr
);
9225 // ----------------------------------------------------------------------------
9226 // setting cell attributes: this is forwarded to the table
9227 // ----------------------------------------------------------------------------
9229 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9231 if ( CanHaveAttributes() )
9233 m_table
->SetAttr(attr
, row
, col
);
9242 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9244 if ( CanHaveAttributes() )
9246 m_table
->SetRowAttr(attr
, row
);
9255 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9257 if ( CanHaveAttributes() )
9259 m_table
->SetColAttr(attr
, col
);
9268 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9270 if ( CanHaveAttributes() )
9272 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9273 attr
->SetBackgroundColour(colour
);
9278 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9280 if ( CanHaveAttributes() )
9282 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9283 attr
->SetTextColour(colour
);
9288 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9290 if ( CanHaveAttributes() )
9292 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9293 attr
->SetFont(font
);
9298 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9300 if ( CanHaveAttributes() )
9302 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9303 attr
->SetAlignment(horiz
, vert
);
9308 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9310 if ( CanHaveAttributes() )
9312 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9313 attr
->SetOverflow(allow
);
9318 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9320 if ( CanHaveAttributes() )
9322 int cell_rows
, cell_cols
;
9324 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9325 attr
->GetSize(&cell_rows
, &cell_cols
);
9326 attr
->SetSize(num_rows
, num_cols
);
9329 // Cannot set the size of a cell to 0 or negative values
9330 // While it is perfectly legal to do that, this function cannot
9331 // handle all the possibilies, do it by hand by getting the CellAttr.
9332 // You can only set the size of a cell to 1,1 or greater with this fn
9333 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9334 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9335 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9336 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9338 // if this was already a multicell then "turn off" the other cells first
9339 if ((cell_rows
> 1) || (cell_rows
> 1))
9342 for (j
=row
; j
<row
+cell_rows
; j
++)
9344 for (i
=col
; i
<col
+cell_cols
; i
++)
9346 if ((i
!= col
) || (j
!= row
))
9348 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9349 attr_stub
->SetSize( 1, 1 );
9350 attr_stub
->DecRef();
9356 // mark the cells that will be covered by this cell to
9357 // negative or zero values to point back at this cell
9358 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9361 for (j
=row
; j
<row
+num_rows
; j
++)
9363 for (i
=col
; i
<col
+num_cols
; i
++)
9365 if ((i
!= col
) || (j
!= row
))
9367 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9368 attr_stub
->SetSize( row
-j
, col
-i
);
9369 attr_stub
->DecRef();
9377 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9379 if ( CanHaveAttributes() )
9381 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9382 attr
->SetRenderer(renderer
);
9387 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9389 if ( CanHaveAttributes() )
9391 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9392 attr
->SetEditor(editor
);
9397 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9399 if ( CanHaveAttributes() )
9401 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9402 attr
->SetReadOnly(isReadOnly
);
9407 // ----------------------------------------------------------------------------
9408 // Data type registration
9409 // ----------------------------------------------------------------------------
9411 void wxGrid::RegisterDataType(const wxString
& typeName
,
9412 wxGridCellRenderer
* renderer
,
9413 wxGridCellEditor
* editor
)
9415 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9419 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9421 wxString typeName
= m_table
->GetTypeName(row
, col
);
9422 return GetDefaultEditorForType(typeName
);
9425 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9427 wxString typeName
= m_table
->GetTypeName(row
, col
);
9428 return GetDefaultRendererForType(typeName
);
9432 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9434 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9435 if ( index
== wxNOT_FOUND
)
9437 wxFAIL_MSG(wxT("Unknown data type name"));
9442 return m_typeRegistry
->GetEditor(index
);
9446 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9448 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9449 if ( index
== wxNOT_FOUND
)
9451 wxFAIL_MSG(wxT("Unknown data type name"));
9456 return m_typeRegistry
->GetRenderer(index
);
9460 // ----------------------------------------------------------------------------
9462 // ----------------------------------------------------------------------------
9464 void wxGrid::EnableDragRowSize( bool enable
)
9466 m_canDragRowSize
= enable
;
9470 void wxGrid::EnableDragColSize( bool enable
)
9472 m_canDragColSize
= enable
;
9475 void wxGrid::EnableDragGridSize( bool enable
)
9477 m_canDragGridSize
= enable
;
9480 void wxGrid::EnableDragCell( bool enable
)
9482 m_canDragCell
= enable
;
9485 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9487 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9489 if ( resizeExistingRows
)
9491 // since we are resizing all rows to the default row size,
9492 // we can simply clear the row heights and row bottoms
9493 // arrays (which also allows us to take advantage of
9494 // some speed optimisations)
9495 m_rowHeights
.Empty();
9496 m_rowBottoms
.Empty();
9497 if ( !GetBatchCount() )
9502 void wxGrid::SetRowSize( int row
, int height
)
9504 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9506 // See comment in SetColSize
9507 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9509 if ( m_rowHeights
.IsEmpty() )
9511 // need to really create the array
9515 int h
= wxMax( 0, height
);
9516 int diff
= h
- m_rowHeights
[row
];
9518 m_rowHeights
[row
] = h
;
9520 for ( i
= row
; i
< m_numRows
; i
++ )
9522 m_rowBottoms
[i
] += diff
;
9524 if ( !GetBatchCount() )
9528 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9530 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9532 if ( resizeExistingCols
)
9534 // since we are resizing all columns to the default column size,
9535 // we can simply clear the col widths and col rights
9536 // arrays (which also allows us to take advantage of
9537 // some speed optimisations)
9538 m_colWidths
.Empty();
9539 m_colRights
.Empty();
9540 if ( !GetBatchCount() )
9545 void wxGrid::SetColSize( int col
, int width
)
9547 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9549 // should we check that it's bigger than GetColMinimalWidth(col) here?
9551 // No, because it is reasonable to assume the library user know's
9552 // what he is doing. However whe should test against the weaker
9553 // constariant of minimalAcceptableWidth, as this breaks rendering
9555 // This test then fixes sf.net bug #645734
9557 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9559 if ( m_colWidths
.IsEmpty() )
9561 // need to really create the array
9565 // if < 0 calc new width from label
9569 wxArrayString lines
;
9570 wxClientDC
dc(m_colLabelWin
);
9571 dc
.SetFont(GetLabelFont());
9572 StringToLines(GetColLabelValue(col
), lines
);
9573 GetTextBoxSize(dc
, lines
, &w
, &h
);
9576 int w
= wxMax( 0, width
);
9577 int diff
= w
- m_colWidths
[col
];
9578 m_colWidths
[col
] = w
;
9581 for ( i
= col
; i
< m_numCols
; i
++ )
9583 m_colRights
[i
] += diff
;
9585 if ( !GetBatchCount() )
9590 void wxGrid::SetColMinimalWidth( int col
, int width
)
9592 if (width
> GetColMinimalAcceptableWidth()) {
9593 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9594 m_colMinWidths
[key
] = width
;
9598 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9600 if (width
> GetRowMinimalAcceptableHeight()) {
9601 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9602 m_rowMinHeights
[key
] = width
;
9606 int wxGrid::GetColMinimalWidth(int col
) const
9608 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9609 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9610 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9613 int wxGrid::GetRowMinimalHeight(int row
) const
9615 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9616 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9617 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9620 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9622 // We do allow a width of 0 since this gives us
9623 // an easy way to temporarily hidding columns.
9626 m_minAcceptableColWidth
= width
;
9629 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9631 // We do allow a height of 0 since this gives us
9632 // an easy way to temporarily hidding rows.
9635 m_minAcceptableRowHeight
= height
;
9638 int wxGrid::GetColMinimalAcceptableWidth() const
9640 return m_minAcceptableColWidth
;
9643 int wxGrid::GetRowMinimalAcceptableHeight() const
9645 return m_minAcceptableRowHeight
;
9648 // ----------------------------------------------------------------------------
9650 // ----------------------------------------------------------------------------
9652 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9654 wxClientDC
dc(m_gridWin
);
9656 //Cancel editting of cell
9657 HideCellEditControl();
9658 SaveEditControlValue();
9660 // init both of them to avoid compiler warnings, even if weo nly need one
9668 wxCoord extent
, extentMax
= 0;
9669 int max
= column
? m_numRows
: m_numCols
;
9670 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9677 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9678 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9681 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9682 extent
= column
? size
.x
: size
.y
;
9683 if ( extent
> extentMax
)
9694 // now also compare with the column label extent
9696 dc
.SetFont( GetLabelFont() );
9700 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9701 if( GetColLabelTextOrientation() == wxVERTICAL
)
9705 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9707 extent
= column
? w
: h
;
9708 if ( extent
> extentMax
)
9715 // empty column - give default extent (notice that if extentMax is less
9716 // than default extent but != 0, it's ok)
9717 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9723 // leave some space around text
9734 SetColSize(col
, extentMax
);
9735 if ( !GetBatchCount() )
9738 m_gridWin
->GetClientSize( &cw
, &ch
);
9739 wxRect
rect ( CellToRect( 0, col
) );
9741 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9742 rect
.width
= cw
- rect
.x
;
9743 rect
.height
= m_colLabelHeight
;
9744 m_colLabelWin
->Refresh( true, &rect
);
9749 SetRowSize(row
, extentMax
);
9750 if ( !GetBatchCount() )
9753 m_gridWin
->GetClientSize( &cw
, &ch
);
9754 wxRect
rect ( CellToRect( row
, 0 ) );
9756 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9757 rect
.width
= m_rowLabelWidth
;
9758 rect
.height
= ch
- rect
.y
;
9759 m_rowLabelWin
->Refresh( true, &rect
);
9765 SetColMinimalWidth(col
, extentMax
);
9767 SetRowMinimalHeight(row
, extentMax
);
9771 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9773 int width
= m_rowLabelWidth
;
9778 for ( int col
= 0; col
< m_numCols
; col
++ )
9782 AutoSizeColumn(col
, setAsMin
);
9785 width
+= GetColWidth(col
);
9794 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9796 int height
= m_colLabelHeight
;
9801 for ( int row
= 0; row
< m_numRows
; row
++ )
9805 AutoSizeRow(row
, setAsMin
);
9808 height
+= GetRowHeight(row
);
9817 void wxGrid::AutoSize()
9821 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9823 // round up the size to a multiple of scroll step - this ensures that we
9824 // won't get the scrollbars if we're sized exactly to this width
9825 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9827 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9828 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9830 // distribute the extra space between the columns/rows to avoid having
9831 // extra white space
9833 // Remove the extra m_extraWidth + 1 added above
9834 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9835 if ( diff
&& m_numCols
)
9837 // try to resize the columns uniformly
9838 wxCoord diffPerCol
= diff
/ m_numCols
;
9841 for ( int col
= 0; col
< m_numCols
; col
++ )
9843 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9847 // add remaining amount to the last columns
9848 diff
-= diffPerCol
* m_numCols
;
9851 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9853 SetColSize(col
, GetColWidth(col
) + 1);
9859 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9860 if ( diff
&& m_numRows
)
9862 // try to resize the columns uniformly
9863 wxCoord diffPerRow
= diff
/ m_numRows
;
9866 for ( int row
= 0; row
< m_numRows
; row
++ )
9868 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9872 // add remaining amount to the last rows
9873 diff
-= diffPerRow
* m_numRows
;
9876 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9878 SetRowSize(row
, GetRowHeight(row
) + 1);
9885 SetClientSize(sizeFit
);
9888 void wxGrid::AutoSizeRowLabelSize( int row
)
9890 wxArrayString lines
;
9893 // Hide the edit control, so it
9894 // won't interfer with drag-shrinking.
9895 if( IsCellEditControlShown() )
9897 HideCellEditControl();
9898 SaveEditControlValue();
9901 // autosize row height depending on label text
9902 StringToLines( GetRowLabelValue( row
), lines
);
9903 wxClientDC
dc( m_rowLabelWin
);
9904 GetTextBoxSize( dc
, lines
, &w
, &h
);
9905 if( h
< m_defaultRowHeight
)
9906 h
= m_defaultRowHeight
;
9911 void wxGrid::AutoSizeColLabelSize( int col
)
9913 wxArrayString lines
;
9916 // Hide the edit control, so it
9917 // won't interfer with drag-shrinking.
9918 if( IsCellEditControlShown() )
9920 HideCellEditControl();
9921 SaveEditControlValue();
9924 // autosize column width depending on label text
9925 StringToLines( GetColLabelValue( col
), lines
);
9926 wxClientDC
dc( m_colLabelWin
);
9927 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9928 GetTextBoxSize( dc
, lines
, &w
, &h
);
9930 GetTextBoxSize( dc
, lines
, &h
, &w
);
9931 if( w
< m_defaultColWidth
)
9932 w
= m_defaultColWidth
;
9937 wxSize
wxGrid::DoGetBestSize() const
9939 // don't set sizes, only calculate them
9940 wxGrid
*self
= (wxGrid
*)this; // const_cast
9943 width
= self
->SetOrCalcColumnSizes(true);
9944 height
= self
->SetOrCalcRowSizes(true);
9946 if (!width
) width
=100;
9947 if (!height
) height
=80;
9949 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9950 // of the scrollbars, is there any magic incantaion for that?
9952 GetScrollPixelsPerUnit(&xpu
, &ypu
);
9954 width
+= 1 + xpu
- (width
% xpu
);
9956 height
+= 1 + ypu
- (height
% ypu
);
9958 // limit to 1/4 of the screen size
9959 int maxwidth
, maxheight
;
9960 wxDisplaySize( & maxwidth
, & maxheight
);
9963 if ( width
> maxwidth
) width
= maxwidth
;
9964 if ( height
> maxheight
) height
= maxheight
;
9967 wxSize
best(width
, height
);
9968 // NOTE: This size should be cached, but first we need to add calls to
9969 // InvalidateBestSize everywhere that could change the results of this
9971 // CacheBestSize(size);
9981 wxPen
& wxGrid::GetDividerPen() const
9986 // ----------------------------------------------------------------------------
9987 // cell value accessor functions
9988 // ----------------------------------------------------------------------------
9990 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9994 m_table
->SetValue( row
, col
, s
);
9995 if ( !GetBatchCount() )
9998 wxRect
rect( CellToRect( row
, col
) );
10000 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10001 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10002 m_gridWin
->Refresh( false, &rect
);
10005 if ( m_currentCellCoords
.GetRow() == row
&&
10006 m_currentCellCoords
.GetCol() == col
&&
10007 IsCellEditControlShown())
10008 // Note: If we are using IsCellEditControlEnabled,
10009 // this interacts badly with calling SetCellValue from
10010 // an EVT_GRID_CELL_CHANGE handler.
10012 HideCellEditControl();
10013 ShowCellEditControl(); // will reread data from table
10020 // ------ Block, row and col selection
10023 void wxGrid::SelectRow( int row
, bool addToSelected
)
10025 if ( IsSelection() && !addToSelected
)
10029 m_selection
->SelectRow( row
, false, addToSelected
);
10033 void wxGrid::SelectCol( int col
, bool addToSelected
)
10035 if ( IsSelection() && !addToSelected
)
10039 m_selection
->SelectCol( col
, false, addToSelected
);
10043 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10044 bool addToSelected
)
10046 if ( IsSelection() && !addToSelected
)
10050 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10051 false, addToSelected
);
10055 void wxGrid::SelectAll()
10057 if ( m_numRows
> 0 && m_numCols
> 0 )
10060 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10065 // ------ Cell, row and col deselection
10068 void wxGrid::DeselectRow( int row
)
10070 if ( !m_selection
)
10073 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10075 if ( m_selection
->IsInSelection(row
, 0 ) )
10076 m_selection
->ToggleCellSelection( row
, 0);
10080 int nCols
= GetNumberCols();
10081 for ( int i
= 0; i
< nCols
; i
++ )
10083 if ( m_selection
->IsInSelection(row
, i
) )
10084 m_selection
->ToggleCellSelection( row
, i
);
10089 void wxGrid::DeselectCol( int col
)
10091 if ( !m_selection
)
10094 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10096 if ( m_selection
->IsInSelection(0, col
) )
10097 m_selection
->ToggleCellSelection( 0, col
);
10101 int nRows
= GetNumberRows();
10102 for ( int i
= 0; i
< nRows
; i
++ )
10104 if ( m_selection
->IsInSelection(i
, col
) )
10105 m_selection
->ToggleCellSelection(i
, col
);
10110 void wxGrid::DeselectCell( int row
, int col
)
10112 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10113 m_selection
->ToggleCellSelection(row
, col
);
10116 bool wxGrid::IsSelection()
10118 return ( m_selection
&& (m_selection
->IsSelection() ||
10119 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10120 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10123 bool wxGrid::IsInSelection( int row
, int col
) const
10125 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10126 ( row
>= m_selectingTopLeft
.GetRow() &&
10127 col
>= m_selectingTopLeft
.GetCol() &&
10128 row
<= m_selectingBottomRight
.GetRow() &&
10129 col
<= m_selectingBottomRight
.GetCol() )) );
10132 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10134 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10135 return m_selection
->m_cellSelection
;
10137 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10139 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10140 return m_selection
->m_blockSelectionTopLeft
;
10142 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10144 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10145 return m_selection
->m_blockSelectionBottomRight
;
10147 wxArrayInt
wxGrid::GetSelectedRows() const
10149 if (!m_selection
) { wxArrayInt a
; return a
; }
10150 return m_selection
->m_rowSelection
;
10152 wxArrayInt
wxGrid::GetSelectedCols() const
10154 if (!m_selection
) { wxArrayInt a
; return a
; }
10155 return m_selection
->m_colSelection
;
10159 void wxGrid::ClearSelection()
10161 m_selectingTopLeft
= wxGridNoCellCoords
;
10162 m_selectingBottomRight
= wxGridNoCellCoords
;
10164 m_selection
->ClearSelection();
10168 // This function returns the rectangle that encloses the given block
10169 // in device coords clipped to the client size of the grid window.
10171 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10172 const wxGridCellCoords
&bottomRight
)
10174 wxRect
rect( wxGridNoCellRect
);
10177 cellRect
= CellToRect( topLeft
);
10178 if ( cellRect
!= wxGridNoCellRect
)
10184 rect
= wxRect( 0, 0, 0, 0 );
10187 cellRect
= CellToRect( bottomRight
);
10188 if ( cellRect
!= wxGridNoCellRect
)
10194 return wxGridNoCellRect
;
10198 int left
= rect
.GetLeft();
10199 int top
= rect
.GetTop();
10200 int right
= rect
.GetRight();
10201 int bottom
= rect
.GetBottom();
10203 int leftCol
= topLeft
.GetCol();
10204 int topRow
= topLeft
.GetRow();
10205 int rightCol
= bottomRight
.GetCol();
10206 int bottomRow
= bottomRight
.GetRow();
10224 topRow
= bottomRow
;
10229 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10231 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10233 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10235 cellRect
= CellToRect( j
, i
);
10237 if (cellRect
.x
< left
)
10239 if (cellRect
.y
< top
)
10241 if (cellRect
.x
+ cellRect
.width
> right
)
10242 right
= cellRect
.x
+ cellRect
.width
;
10243 if (cellRect
.y
+ cellRect
.height
> bottom
)
10244 bottom
= cellRect
.y
+ cellRect
.height
;
10246 else i
= rightCol
; // jump over inner cells.
10250 // convert to scrolled coords
10252 CalcScrolledPosition( left
, top
, &left
, &top
);
10253 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10256 m_gridWin
->GetClientSize( &cw
, &ch
);
10258 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10259 return wxRect( 0, 0, 0, 0);
10261 rect
.SetLeft( wxMax(0, left
) );
10262 rect
.SetTop( wxMax(0, top
) );
10263 rect
.SetRight( wxMin(cw
, right
) );
10264 rect
.SetBottom( wxMin(ch
, bottom
) );
10270 // ------ Grid event classes
10273 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10275 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10276 int row
, int col
, int x
, int y
, bool sel
,
10277 bool control
, bool shift
, bool alt
, bool meta
)
10278 : wxNotifyEvent( type
, id
)
10285 m_control
= control
;
10290 SetEventObject(obj
);
10294 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10296 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10297 int rowOrCol
, int x
, int y
,
10298 bool control
, bool shift
, bool alt
, bool meta
)
10299 : wxNotifyEvent( type
, id
)
10301 m_rowOrCol
= rowOrCol
;
10304 m_control
= control
;
10309 SetEventObject(obj
);
10313 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10315 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10316 const wxGridCellCoords
& topLeft
,
10317 const wxGridCellCoords
& bottomRight
,
10318 bool sel
, bool control
,
10319 bool shift
, bool alt
, bool meta
)
10320 : wxNotifyEvent( type
, id
)
10322 m_topLeft
= topLeft
;
10323 m_bottomRight
= bottomRight
;
10325 m_control
= control
;
10330 SetEventObject(obj
);
10334 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10336 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10337 wxObject
* obj
, int row
,
10338 int col
, wxControl
* ctrl
)
10339 : wxCommandEvent(type
, id
)
10341 SetEventObject(obj
);
10347 #endif // wxUSE_GRID