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 // ----------------------------------------------------------------------------
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"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
51 #include "wx/generic/gridsel.h"
53 #if defined(__WXMOTIF__)
54 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
56 #define WXUNUSED_MOTIF(identifier) identifier
59 #if defined(__WXGTK__)
60 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
62 #define WXUNUSED_GTK(identifier) identifier
65 // Required for wxIs... functions
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
74 struct wxGridCellWithAttr
76 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
77 : coords(row
, col
), attr(attr_
)
86 wxGridCellCoords coords
;
90 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
91 // without rewriting the macros, which require a public copy constructor.
94 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
96 #include "wx/arrimpl.cpp"
98 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
99 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
129 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
130 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
131 const wxPoint
&pos
, const wxSize
&size
);
136 void OnPaint( wxPaintEvent
& event
);
137 void OnMouseEvent( wxMouseEvent
& event
);
138 void OnMouseWheel( wxMouseEvent
& event
);
139 void OnKeyDown( wxKeyEvent
& event
);
140 void OnKeyUp( wxKeyEvent
& );
142 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
143 DECLARE_EVENT_TABLE()
144 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
148 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
151 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
152 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
153 const wxPoint
&pos
, const wxSize
&size
);
158 void OnPaint( wxPaintEvent
&event
);
159 void OnMouseEvent( wxMouseEvent
& event
);
160 void OnMouseWheel( wxMouseEvent
& event
);
161 void OnKeyDown( wxKeyEvent
& event
);
162 void OnKeyUp( wxKeyEvent
& );
164 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
165 DECLARE_EVENT_TABLE()
166 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
170 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
173 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
174 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
175 const wxPoint
&pos
, const wxSize
&size
);
180 void OnMouseEvent( wxMouseEvent
& event
);
181 void OnMouseWheel( wxMouseEvent
& event
);
182 void OnKeyDown( wxKeyEvent
& event
);
183 void OnKeyUp( wxKeyEvent
& );
184 void OnPaint( wxPaintEvent
& event
);
186 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
187 DECLARE_EVENT_TABLE()
188 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
191 class WXDLLEXPORT wxGridWindow
: public wxWindow
196 m_owner
= (wxGrid
*)NULL
;
197 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
198 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
201 wxGridWindow( wxGrid
*parent
,
202 wxGridRowLabelWindow
*rowLblWin
,
203 wxGridColLabelWindow
*colLblWin
,
204 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
207 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
209 wxGrid
* GetOwner() { return m_owner
; }
213 wxGridRowLabelWindow
*m_rowLabelWin
;
214 wxGridColLabelWindow
*m_colLabelWin
;
216 void OnPaint( wxPaintEvent
&event
);
217 void OnMouseWheel( wxMouseEvent
& event
);
218 void OnMouseEvent( wxMouseEvent
& event
);
219 void OnKeyDown( wxKeyEvent
& );
220 void OnKeyUp( wxKeyEvent
& );
221 void OnEraseBackground( wxEraseEvent
& );
224 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
225 DECLARE_EVENT_TABLE()
226 DECLARE_NO_COPY_CLASS(wxGridWindow
)
231 class wxGridCellEditorEvtHandler
: public wxEvtHandler
234 wxGridCellEditorEvtHandler()
235 : m_grid(0), m_editor(0)
237 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
238 : m_grid(grid
), m_editor(editor
)
241 void OnKeyDown(wxKeyEvent
& event
);
242 void OnChar(wxKeyEvent
& event
);
246 wxGridCellEditor
* m_editor
;
247 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
248 DECLARE_EVENT_TABLE()
249 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
253 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
254 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
255 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
256 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
261 // ----------------------------------------------------------------------------
262 // the internal data representation used by wxGridCellAttrProvider
263 // ----------------------------------------------------------------------------
265 // this class stores attributes set for cells
266 class WXDLLEXPORT wxGridCellAttrData
269 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
270 wxGridCellAttr
*GetAttr(int row
, int col
) const;
271 void UpdateAttrRows( size_t pos
, int numRows
);
272 void UpdateAttrCols( size_t pos
, int numCols
);
275 // searches for the attr for given cell, returns wxNOT_FOUND if not found
276 int FindIndex(int row
, int col
) const;
278 wxGridCellWithAttrArray m_attrs
;
281 // this class stores attributes set for rows or columns
282 class WXDLLEXPORT wxGridRowOrColAttrData
285 // empty ctor to suppress warnings
286 wxGridRowOrColAttrData() { }
287 ~wxGridRowOrColAttrData();
289 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
290 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
291 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
294 wxArrayInt m_rowsOrCols
;
295 wxArrayAttrs m_attrs
;
298 // NB: this is just a wrapper around 3 objects: one which stores cell
299 // attributes, and 2 others for row/col ones
300 class WXDLLEXPORT wxGridCellAttrProviderData
303 wxGridCellAttrData m_cellAttrs
;
304 wxGridRowOrColAttrData m_rowAttrs
,
309 // ----------------------------------------------------------------------------
310 // data structures used for the data type registry
311 // ----------------------------------------------------------------------------
313 struct wxGridDataTypeInfo
315 wxGridDataTypeInfo(const wxString
& typeName
,
316 wxGridCellRenderer
* renderer
,
317 wxGridCellEditor
* editor
)
318 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
321 ~wxGridDataTypeInfo()
323 wxSafeDecRef(m_renderer
);
324 wxSafeDecRef(m_editor
);
328 wxGridCellRenderer
* m_renderer
;
329 wxGridCellEditor
* m_editor
;
331 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
335 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
338 class WXDLLEXPORT wxGridTypeRegistry
341 wxGridTypeRegistry() {}
342 ~wxGridTypeRegistry();
344 void RegisterDataType(const wxString
& typeName
,
345 wxGridCellRenderer
* renderer
,
346 wxGridCellEditor
* editor
);
348 // find one of already registered data types
349 int FindRegisteredDataType(const wxString
& typeName
);
351 // try to FindRegisteredDataType(), if this fails and typeName is one of
352 // standard typenames, register it and return its index
353 int FindDataType(const wxString
& typeName
);
355 // try to FindDataType(), if it fails see if it is not one of already
356 // registered data types with some params in which case clone the
357 // registered data type and set params for it
358 int FindOrCloneDataType(const wxString
& typeName
);
360 wxGridCellRenderer
* GetRenderer(int index
);
361 wxGridCellEditor
* GetEditor(int index
);
364 wxGridDataTypeInfoArray m_typeinfo
;
367 // ----------------------------------------------------------------------------
368 // conditional compilation
369 // ----------------------------------------------------------------------------
371 #ifndef WXGRID_DRAW_LINES
372 #define WXGRID_DRAW_LINES 1
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 //#define DEBUG_ATTR_CACHE
380 #ifdef DEBUG_ATTR_CACHE
381 static size_t gs_nAttrCacheHits
= 0;
382 static size_t gs_nAttrCacheMisses
= 0;
383 #endif // DEBUG_ATTR_CACHE
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
389 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
390 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
393 // TODO: this doesn't work at all, grid cells have different sizes and approx
394 // calculations don't work as because of the size mismatch scrollbars
395 // sometimes fail to be shown when they should be or vice versa
397 // The scroll bars may be a little flakey once in a while, but that is
398 // surely much less horrible than having scroll lines of only 1!!!
401 // Well, it's still seriously broken so it might be better but needs
404 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
405 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
407 // the size of hash tables used a bit everywhere (the max number of elements
408 // in these hash tables is the number of rows/columns)
409 static const int GRID_HASH_SIZE
= 100;
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 static inline int GetScrollX(int x
)
417 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
420 static inline int GetScrollY(int y
)
422 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
425 // ============================================================================
427 // ============================================================================
429 // ----------------------------------------------------------------------------
431 // ----------------------------------------------------------------------------
433 wxGridCellEditor::wxGridCellEditor()
440 wxGridCellEditor::~wxGridCellEditor()
445 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
446 wxWindowID
WXUNUSED(id
),
447 wxEvtHandler
* evtHandler
)
450 m_control
->PushEventHandler(evtHandler
);
453 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
454 wxGridCellAttr
*attr
)
456 // erase the background because we might not fill the cell
457 wxClientDC
dc(m_control
->GetParent());
458 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
460 gridWindow
->GetOwner()->PrepareDC(dc
);
462 dc
.SetPen(*wxTRANSPARENT_PEN
);
463 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
464 dc
.DrawRectangle(rectCell
);
466 // redraw the control we just painted over
467 m_control
->Refresh();
470 void wxGridCellEditor::Destroy()
474 m_control
->PopEventHandler(TRUE
/* delete it*/);
476 m_control
->Destroy();
481 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
483 wxASSERT_MSG(m_control
,
484 wxT("The wxGridCellEditor must be Created first!"));
485 m_control
->Show(show
);
489 // set the colours/fonts if we have any
492 m_colFgOld
= m_control
->GetForegroundColour();
493 m_control
->SetForegroundColour(attr
->GetTextColour());
495 m_colBgOld
= m_control
->GetBackgroundColour();
496 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
498 m_fontOld
= m_control
->GetFont();
499 m_control
->SetFont(attr
->GetFont());
501 // can't do anything more in the base class version, the other
502 // attributes may only be used by the derived classes
507 // restore the standard colours fonts
508 if ( m_colFgOld
.Ok() )
510 m_control
->SetForegroundColour(m_colFgOld
);
511 m_colFgOld
= wxNullColour
;
514 if ( m_colBgOld
.Ok() )
516 m_control
->SetBackgroundColour(m_colBgOld
);
517 m_colBgOld
= wxNullColour
;
520 if ( m_fontOld
.Ok() )
522 m_control
->SetFont(m_fontOld
);
523 m_fontOld
= wxNullFont
;
528 void wxGridCellEditor::SetSize(const wxRect
& rect
)
530 wxASSERT_MSG(m_control
,
531 wxT("The wxGridCellEditor must be Created first!"));
532 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
535 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
540 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
542 // accept the simple key presses, not anything with Ctrl/Alt/Meta
543 return !(event
.ControlDown() || event
.AltDown());
546 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
551 void wxGridCellEditor::StartingClick()
557 // ----------------------------------------------------------------------------
558 // wxGridCellTextEditor
559 // ----------------------------------------------------------------------------
561 wxGridCellTextEditor::wxGridCellTextEditor()
566 void wxGridCellTextEditor::Create(wxWindow
* parent
,
568 wxEvtHandler
* evtHandler
)
570 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
571 wxDefaultPosition
, wxDefaultSize
572 #if defined(__WXMSW__)
573 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
574 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
578 // TODO: use m_maxChars
580 wxGridCellEditor::Create(parent
, id
, evtHandler
);
583 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
584 wxGridCellAttr
* WXUNUSED(attr
))
586 // as we fill the entire client area, don't do anything here to minimize
590 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
592 wxRect
rect(rectOrig
);
594 // Make the edit control large enough to allow for internal
597 // TODO: remove this if the text ctrl sizing is improved esp. for
600 #if defined(__WXGTK__)
609 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
611 // MB: treat MSW separately here otherwise the caret doesn't show
612 // when the editor is in the first row.
613 #if defined(__WXMSW__)
616 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
619 #if defined(__WXMOTIF__)
623 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
624 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
625 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
626 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
629 wxGridCellEditor::SetSize(rect
);
632 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
634 wxASSERT_MSG(m_control
,
635 wxT("The wxGridCellEditor must be Created first!"));
637 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
639 DoBeginEdit(m_startValue
);
642 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
644 Text()->SetValue(startValue
);
645 Text()->SetInsertionPointEnd();
646 Text()->SetSelection(-1,-1);
650 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
653 wxASSERT_MSG(m_control
,
654 wxT("The wxGridCellEditor must be Created first!"));
656 bool changed
= FALSE
;
657 wxString value
= Text()->GetValue();
658 if (value
!= m_startValue
)
662 grid
->GetTable()->SetValue(row
, col
, value
);
664 m_startValue
= wxEmptyString
;
665 Text()->SetValue(m_startValue
);
671 void wxGridCellTextEditor::Reset()
673 wxASSERT_MSG(m_control
,
674 wxT("The wxGridCellEditor must be Created first!"));
676 DoReset(m_startValue
);
679 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
681 Text()->SetValue(startValue
);
682 Text()->SetInsertionPointEnd();
685 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
687 if ( wxGridCellEditor::IsAcceptedKey(event
) )
689 int keycode
= event
.GetKeyCode();
703 case WXK_NUMPAD_MULTIPLY
:
707 case WXK_NUMPAD_SUBTRACT
:
709 case WXK_NUMPAD_DECIMAL
:
711 case WXK_NUMPAD_DIVIDE
:
715 // accept 8 bit chars too if isprint() agrees
716 if ( (keycode
< 255) && (isprint(keycode
)) )
724 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
726 if ( !Text()->EmulateKeyPress(event
) )
732 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
733 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
735 #if defined(__WXMOTIF__) || defined(__WXGTK__)
736 // wxMotif needs a little extra help...
737 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
738 wxString
s( Text()->GetValue() );
739 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
741 Text()->SetInsertionPoint( pos
);
743 // the other ports can handle a Return key press
749 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
759 if ( !params
.ToLong(&tmp
) )
761 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
765 m_maxChars
= (size_t)tmp
;
770 // return the value in the text control
771 wxString
wxGridCellTextEditor::GetValue() const
773 return Text()->GetValue();
776 // ----------------------------------------------------------------------------
777 // wxGridCellNumberEditor
778 // ----------------------------------------------------------------------------
780 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
786 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
788 wxEvtHandler
* evtHandler
)
792 // create a spin ctrl
793 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
794 wxDefaultPosition
, wxDefaultSize
,
798 wxGridCellEditor::Create(parent
, id
, evtHandler
);
802 // just a text control
803 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
806 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
807 #endif // wxUSE_VALIDATORS
811 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
813 // first get the value
814 wxGridTableBase
*table
= grid
->GetTable();
815 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
817 m_valueOld
= table
->GetValueAsLong(row
, col
);
822 wxString sValue
= table
->GetValue(row
, col
);
823 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
825 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
832 Spin()->SetValue((int)m_valueOld
);
837 DoBeginEdit(GetString());
841 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
850 value
= Spin()->GetValue();
851 changed
= value
!= m_valueOld
;
853 text
= wxString::Format(wxT("%ld"), value
);
857 text
= Text()->GetValue();
858 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
863 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
864 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
866 grid
->GetTable()->SetValue(row
, col
, text
);
872 void wxGridCellNumberEditor::Reset()
876 Spin()->SetValue((int)m_valueOld
);
880 DoReset(GetString());
884 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
886 if ( wxGridCellEditor::IsAcceptedKey(event
) )
888 int keycode
= event
.GetKeyCode();
904 case WXK_NUMPAD_SUBTRACT
:
910 if ( (keycode
< 128) && isdigit(keycode
) )
918 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
922 int keycode
= event
.GetKeyCode();
923 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-'
924 || keycode
== WXK_NUMPAD0
925 || keycode
== WXK_NUMPAD1
926 || keycode
== WXK_NUMPAD2
927 || keycode
== WXK_NUMPAD3
928 || keycode
== WXK_NUMPAD4
929 || keycode
== WXK_NUMPAD5
930 || keycode
== WXK_NUMPAD6
931 || keycode
== WXK_NUMPAD7
932 || keycode
== WXK_NUMPAD8
933 || keycode
== WXK_NUMPAD9
934 || keycode
== WXK_ADD
935 || keycode
== WXK_NUMPAD_ADD
936 || keycode
== WXK_SUBTRACT
937 || keycode
== WXK_NUMPAD_SUBTRACT
)
939 wxGridCellTextEditor::StartingKey(event
);
949 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
960 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
964 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
968 // skip the error message below
973 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
977 // return the value in the spin control if it is there (the text control otherwise)
978 wxString
wxGridCellNumberEditor::GetValue() const
984 long value
= Spin()->GetValue();
985 s
.Printf(wxT("%ld"), value
);
989 s
= Text()->GetValue();
994 // ----------------------------------------------------------------------------
995 // wxGridCellFloatEditor
996 // ----------------------------------------------------------------------------
998 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1001 m_precision
= precision
;
1004 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1006 wxEvtHandler
* evtHandler
)
1008 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1010 #if wxUSE_VALIDATORS
1011 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1012 #endif // wxUSE_VALIDATORS
1015 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1017 // first get the value
1018 wxGridTableBase
*table
= grid
->GetTable();
1019 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1021 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1026 wxString sValue
= table
->GetValue(row
, col
);
1027 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1029 wxFAIL_MSG( _T("this cell doesn't have float value") );
1034 DoBeginEdit(GetString());
1037 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1041 wxString
text(Text()->GetValue());
1043 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1045 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1046 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1048 grid
->GetTable()->SetValue(row
, col
, text
);
1055 void wxGridCellFloatEditor::Reset()
1057 DoReset(GetString());
1060 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1062 int keycode
= event
.GetKeyCode();
1063 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1064 || keycode
== WXK_NUMPAD0
1065 || keycode
== WXK_NUMPAD1
1066 || keycode
== WXK_NUMPAD2
1067 || keycode
== WXK_NUMPAD3
1068 || keycode
== WXK_NUMPAD4
1069 || keycode
== WXK_NUMPAD5
1070 || keycode
== WXK_NUMPAD6
1071 || keycode
== WXK_NUMPAD7
1072 || keycode
== WXK_NUMPAD8
1073 || keycode
== WXK_NUMPAD9
1074 || keycode
== WXK_ADD
1075 || keycode
== WXK_NUMPAD_ADD
1076 || keycode
== WXK_SUBTRACT
1077 || keycode
== WXK_NUMPAD_SUBTRACT
)
1079 wxGridCellTextEditor::StartingKey(event
);
1081 // skip Skip() below
1088 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1099 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1103 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1105 m_precision
= (int)tmp
;
1107 // skip the error message below
1112 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1116 wxString
wxGridCellFloatEditor::GetString() const
1119 if ( m_width
== -1 )
1121 // default width/precision
1124 else if ( m_precision
== -1 )
1126 // default precision
1127 fmt
.Printf(_T("%%%d.f"), m_width
);
1131 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1134 return wxString::Format(fmt
, m_valueOld
);
1137 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1139 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1141 int keycode
= event
.GetKeyCode();
1155 case WXK_NUMPAD_ADD
:
1157 case WXK_NUMPAD_SUBTRACT
:
1159 case WXK_NUMPAD_DECIMAL
:
1163 // additionally accept 'e' as in '1e+6'
1164 if ( (keycode
< 128) &&
1165 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1173 #endif // wxUSE_TEXTCTRL
1177 // ----------------------------------------------------------------------------
1178 // wxGridCellBoolEditor
1179 // ----------------------------------------------------------------------------
1181 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1183 wxEvtHandler
* evtHandler
)
1185 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1186 wxDefaultPosition
, wxDefaultSize
,
1189 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1192 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1194 bool resize
= FALSE
;
1195 wxSize size
= m_control
->GetSize();
1196 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1198 // check if the checkbox is not too big/small for this cell
1199 wxSize sizeBest
= m_control
->GetBestSize();
1200 if ( !(size
== sizeBest
) )
1202 // reset to default size if it had been made smaller
1208 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1210 // leave 1 pixel margin
1211 size
.x
= size
.y
= minSize
- 2;
1218 m_control
->SetSize(size
);
1221 // position it in the centre of the rectangle (TODO: support alignment?)
1223 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1224 // the checkbox without label still has some space to the right in wxGTK,
1225 // so shift it to the right
1227 #elif defined(__WXMSW__)
1228 // here too, but in other way
1233 int hAlign
= wxALIGN_CENTRE
;
1234 int vAlign
= wxALIGN_CENTRE
;
1236 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1239 if (hAlign
== wxALIGN_LEFT
)
1245 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1247 else if (hAlign
== wxALIGN_RIGHT
)
1249 x
= r
.x
+ r
.width
- size
.x
- 2;
1250 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1252 else if (hAlign
== wxALIGN_CENTRE
)
1254 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1255 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1258 m_control
->Move(x
, y
);
1261 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1263 m_control
->Show(show
);
1267 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1268 CBox()->SetBackgroundColour(colBg
);
1272 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1274 wxASSERT_MSG(m_control
,
1275 wxT("The wxGridCellEditor must be Created first!"));
1277 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1278 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1281 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1282 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1284 CBox()->SetValue(m_startValue
);
1288 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1291 wxASSERT_MSG(m_control
,
1292 wxT("The wxGridCellEditor must be Created first!"));
1294 bool changed
= FALSE
;
1295 bool value
= CBox()->GetValue();
1296 if ( value
!= m_startValue
)
1301 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1302 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1304 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1310 void wxGridCellBoolEditor::Reset()
1312 wxASSERT_MSG(m_control
,
1313 wxT("The wxGridCellEditor must be Created first!"));
1315 CBox()->SetValue(m_startValue
);
1318 void wxGridCellBoolEditor::StartingClick()
1320 CBox()->SetValue(!CBox()->GetValue());
1323 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1325 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1327 int keycode
= event
.GetKeyCode();
1331 case WXK_NUMPAD_MULTIPLY
:
1333 case WXK_NUMPAD_ADD
:
1335 case WXK_NUMPAD_SUBTRACT
:
1346 // return the value as "1" for true and the empty string for false
1347 wxString
wxGridCellBoolEditor::GetValue() const
1349 bool bSet
= CBox()->GetValue();
1350 return bSet
? _T("1") : wxEmptyString
;
1353 #endif // wxUSE_CHECKBOX
1357 // ----------------------------------------------------------------------------
1358 // wxGridCellChoiceEditor
1359 // ----------------------------------------------------------------------------
1361 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1362 const wxString choices
[],
1364 : m_allowOthers(allowOthers
)
1368 m_choices
.Alloc(count
);
1369 for ( size_t n
= 0; n
< count
; n
++ )
1371 m_choices
.Add(choices
[n
]);
1376 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1378 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1379 editor
->m_allowOthers
= m_allowOthers
;
1380 editor
->m_choices
= m_choices
;
1385 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1387 wxEvtHandler
* evtHandler
)
1389 size_t count
= m_choices
.GetCount();
1390 wxString
*choices
= new wxString
[count
];
1391 for ( size_t n
= 0; n
< count
; n
++ )
1393 choices
[n
] = m_choices
[n
];
1396 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1397 wxDefaultPosition
, wxDefaultSize
,
1399 m_allowOthers
? 0 : wxCB_READONLY
);
1403 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1406 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1407 wxGridCellAttr
* attr
)
1409 // as we fill the entire client area, don't do anything here to minimize
1412 // TODO: It doesn't actually fill the client area since the height of a
1413 // combo always defaults to the standard... Until someone has time to
1414 // figure out the right rectangle to paint, just do it the normal way...
1415 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1418 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1420 wxASSERT_MSG(m_control
,
1421 wxT("The wxGridCellEditor must be Created first!"));
1423 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1426 Combo()->SetValue(m_startValue
);
1429 // find the right position, or default to the first if not found
1430 int pos
= Combo()->FindString(m_startValue
);
1433 Combo()->SetSelection(pos
);
1435 Combo()->SetInsertionPointEnd();
1436 Combo()->SetFocus();
1439 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1442 wxString value
= Combo()->GetValue();
1443 bool changed
= value
!= m_startValue
;
1446 grid
->GetTable()->SetValue(row
, col
, value
);
1448 m_startValue
= wxEmptyString
;
1450 Combo()->SetValue(m_startValue
);
1452 Combo()->SetSelection(0);
1457 void wxGridCellChoiceEditor::Reset()
1459 Combo()->SetValue(m_startValue
);
1460 Combo()->SetInsertionPointEnd();
1463 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1473 wxStringTokenizer
tk(params
, _T(','));
1474 while ( tk
.HasMoreTokens() )
1476 m_choices
.Add(tk
.GetNextToken());
1480 // return the value in the text control
1481 wxString
wxGridCellChoiceEditor::GetValue() const
1483 return Combo()->GetValue();
1486 #endif // wxUSE_COMBOBOX
1488 // ----------------------------------------------------------------------------
1489 // wxGridCellEditorEvtHandler
1490 // ----------------------------------------------------------------------------
1492 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1494 switch ( event
.GetKeyCode() )
1498 m_grid
->DisableCellEditControl();
1502 m_grid
->GetEventHandler()->ProcessEvent( event
);
1506 case WXK_NUMPAD_ENTER
:
1507 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1508 m_editor
->HandleReturn(event
);
1517 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1519 switch ( event
.GetKeyCode() )
1524 case WXK_NUMPAD_ENTER
:
1532 // ----------------------------------------------------------------------------
1533 // wxGridCellWorker is an (almost) empty common base class for
1534 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1535 // ----------------------------------------------------------------------------
1537 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1542 wxGridCellWorker::~wxGridCellWorker()
1546 // ============================================================================
1548 // ============================================================================
1550 // ----------------------------------------------------------------------------
1551 // wxGridCellRenderer
1552 // ----------------------------------------------------------------------------
1554 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1555 wxGridCellAttr
& attr
,
1558 int WXUNUSED(row
), int WXUNUSED(col
),
1561 dc
.SetBackgroundMode( wxSOLID
);
1563 // grey out fields if the grid is disabled
1564 if( grid
.IsEnabled() )
1568 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1572 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1577 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1580 dc
.SetPen( *wxTRANSPARENT_PEN
);
1581 dc
.DrawRectangle(rect
);
1584 // ----------------------------------------------------------------------------
1585 // wxGridCellStringRenderer
1586 // ----------------------------------------------------------------------------
1588 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1589 wxGridCellAttr
& attr
,
1593 dc
.SetBackgroundMode( wxTRANSPARENT
);
1595 // TODO some special colours for attr.IsReadOnly() case?
1597 // different coloured text when the grid is disabled
1598 if( grid
.IsEnabled() )
1602 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1603 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1607 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1608 dc
.SetTextForeground( attr
.GetTextColour() );
1613 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1614 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1617 dc
.SetFont( attr
.GetFont() );
1620 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1622 const wxString
& text
)
1624 wxCoord x
= 0, y
= 0, max_x
= 0;
1625 dc
.SetFont(attr
.GetFont());
1626 wxStringTokenizer
tk(text
, _T('\n'));
1627 while ( tk
.HasMoreTokens() )
1629 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1630 max_x
= wxMax(max_x
, x
);
1633 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1635 return wxSize(max_x
, y
);
1638 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1639 wxGridCellAttr
& attr
,
1643 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1646 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1647 wxGridCellAttr
& attr
,
1649 const wxRect
& rectCell
,
1653 wxRect rect
= rectCell
;
1656 // erase only this cells background, overflow cells should have been erased
1657 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1660 attr
.GetAlignment(&hAlign
, &vAlign
);
1662 int overflowCols
= 0;
1664 if (attr
.GetOverflow())
1666 int cols
= grid
.GetNumberCols();
1667 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1668 int cell_rows
, cell_cols
;
1669 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1670 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1672 int i
, c_cols
, c_rows
;
1673 for (i
= col
+cell_cols
; i
< cols
; i
++)
1675 bool is_empty
= TRUE
;
1676 for (int j
=row
; j
<row
+cell_rows
; j
++)
1678 // check w/ anchor cell for multicell block
1679 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1680 if (c_rows
> 0) c_rows
= 0;
1681 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1688 rect
.width
+= grid
.GetColSize(i
);
1694 if (rect
.width
>= best_width
) break;
1696 overflowCols
= i
- col
- cell_cols
+ 1;
1697 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1700 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1702 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1704 clip
.x
+= rectCell
.width
;
1705 // draw each overflow cell individually
1706 int col_end
= col
+cell_cols
+overflowCols
;
1707 if (col_end
>= grid
.GetNumberCols())
1708 col_end
= grid
.GetNumberCols() - 1;
1709 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1711 clip
.width
= grid
.GetColSize(i
) - 1;
1712 dc
.DestroyClippingRegion();
1713 dc
.SetClippingRegion(clip
);
1715 SetTextColoursAndFont(grid
, attr
, dc
,
1716 grid
.IsInSelection(row
,i
));
1718 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1719 rect
, hAlign
, vAlign
);
1720 clip
.x
+= grid
.GetColSize(i
) - 1;
1726 dc
.DestroyClippingRegion();
1730 // now we only have to draw the text
1731 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1733 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1734 rect
, hAlign
, vAlign
);
1737 // ----------------------------------------------------------------------------
1738 // wxGridCellNumberRenderer
1739 // ----------------------------------------------------------------------------
1741 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1743 wxGridTableBase
*table
= grid
.GetTable();
1745 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1747 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1751 text
= table
->GetValue(row
, col
);
1757 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1758 wxGridCellAttr
& attr
,
1760 const wxRect
& rectCell
,
1764 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1766 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1768 // draw the text right aligned by default
1770 attr
.GetAlignment(&hAlign
, &vAlign
);
1771 hAlign
= wxALIGN_RIGHT
;
1773 wxRect rect
= rectCell
;
1776 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1779 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1780 wxGridCellAttr
& attr
,
1784 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1787 // ----------------------------------------------------------------------------
1788 // wxGridCellFloatRenderer
1789 // ----------------------------------------------------------------------------
1791 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1794 SetPrecision(precision
);
1797 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1799 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1800 renderer
->m_width
= m_width
;
1801 renderer
->m_precision
= m_precision
;
1802 renderer
->m_format
= m_format
;
1807 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1809 wxGridTableBase
*table
= grid
.GetTable();
1814 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1816 val
= table
->GetValueAsDouble(row
, col
);
1821 text
= table
->GetValue(row
, col
);
1822 hasDouble
= text
.ToDouble(&val
);
1829 if ( m_width
== -1 )
1831 if ( m_precision
== -1 )
1833 // default width/precision
1834 m_format
= _T("%f");
1838 m_format
.Printf(_T("%%.%df"), m_precision
);
1841 else if ( m_precision
== -1 )
1843 // default precision
1844 m_format
.Printf(_T("%%%d.f"), m_width
);
1848 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1852 text
.Printf(m_format
, val
);
1855 //else: text already contains the string
1860 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1861 wxGridCellAttr
& attr
,
1863 const wxRect
& rectCell
,
1867 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1869 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1871 // draw the text right aligned by default
1873 attr
.GetAlignment(&hAlign
, &vAlign
);
1874 hAlign
= wxALIGN_RIGHT
;
1876 wxRect rect
= rectCell
;
1879 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1882 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1883 wxGridCellAttr
& attr
,
1887 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1890 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1894 // reset to defaults
1900 wxString tmp
= params
.BeforeFirst(_T(','));
1904 if ( tmp
.ToLong(&width
) )
1906 SetWidth((int)width
);
1910 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1914 tmp
= params
.AfterFirst(_T(','));
1918 if ( tmp
.ToLong(&precision
) )
1920 SetPrecision((int)precision
);
1924 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1932 // ----------------------------------------------------------------------------
1933 // wxGridCellBoolRenderer
1934 // ----------------------------------------------------------------------------
1936 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1938 // FIXME these checkbox size calculations are really ugly...
1940 // between checkmark and box
1941 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1943 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1944 wxGridCellAttr
& WXUNUSED(attr
),
1949 // compute it only once (no locks for MT safeness in GUI thread...)
1950 if ( !ms_sizeCheckMark
.x
)
1952 // get checkbox size
1953 wxCoord checkSize
= 0;
1954 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1955 wxSize size
= checkbox
->GetBestSize();
1956 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1958 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1959 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1960 checkSize
-= size
.y
/ 2;
1965 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1968 return ms_sizeCheckMark
;
1971 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1972 wxGridCellAttr
& attr
,
1978 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1980 // draw a check mark in the centre (ignoring alignment - TODO)
1981 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1983 // don't draw outside the cell
1984 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1985 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1987 // and even leave (at least) 1 pixel margin
1988 size
.x
= size
.y
= minSize
- 2;
1991 // draw a border around checkmark
1993 attr
.GetAlignment(& hAlign
, &vAlign
);
1996 if (hAlign
== wxALIGN_CENTRE
)
1998 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1999 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2000 rectBorder
.width
= size
.x
;
2001 rectBorder
.height
= size
.y
;
2003 else if (hAlign
== wxALIGN_LEFT
)
2005 rectBorder
.x
= rect
.x
+ 2;
2006 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2007 rectBorder
.width
= size
.x
;
2008 rectBorder
.height
= size
.y
;
2010 else if (hAlign
== wxALIGN_RIGHT
)
2012 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2013 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2014 rectBorder
.width
= size
.x
;
2015 rectBorder
.height
= size
.y
;
2019 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2020 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2023 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2024 value
= !( !cellval
|| (cellval
== wxT("0")) );
2029 wxRect rectMark
= rectBorder
;
2031 // MSW DrawCheckMark() is weird (and should probably be changed...)
2032 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2036 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2039 dc
.SetTextForeground(attr
.GetTextColour());
2040 dc
.DrawCheckMark(rectMark
);
2043 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2044 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2045 dc
.DrawRectangle(rectBorder
);
2048 // ----------------------------------------------------------------------------
2050 // ----------------------------------------------------------------------------
2052 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2056 m_isReadOnly
= Unset
;
2061 m_attrkind
= wxGridCellAttr::Cell
;
2063 m_sizeRows
= m_sizeCols
= 1;
2066 SetDefAttr(attrDefault
);
2069 wxGridCellAttr
*wxGridCellAttr::Clone() const
2071 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2073 if ( HasTextColour() )
2074 attr
->SetTextColour(GetTextColour());
2075 if ( HasBackgroundColour() )
2076 attr
->SetBackgroundColour(GetBackgroundColour());
2078 attr
->SetFont(GetFont());
2079 if ( HasAlignment() )
2080 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2082 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2086 attr
->SetRenderer(m_renderer
);
2087 m_renderer
->IncRef();
2091 attr
->SetEditor(m_editor
);
2096 attr
->SetReadOnly();
2098 attr
->SetKind( m_attrkind
);
2103 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2105 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2106 SetTextColour(mergefrom
->GetTextColour());
2107 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2108 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2109 if ( !HasFont() && mergefrom
->HasFont() )
2110 SetFont(mergefrom
->GetFont());
2111 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2113 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2114 SetAlignment(hAlign
, vAlign
);
2117 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2119 // Directly access member functions as GetRender/Editor don't just return
2120 // m_renderer/m_editor
2122 // Maybe add support for merge of Render and Editor?
2123 if (!HasRenderer() && mergefrom
->HasRenderer() )
2125 m_renderer
= mergefrom
->m_renderer
;
2126 m_renderer
->IncRef();
2128 if ( !HasEditor() && mergefrom
->HasEditor() )
2130 m_editor
= mergefrom
->m_editor
;
2133 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2134 SetReadOnly(mergefrom
->IsReadOnly());
2136 SetDefAttr(mergefrom
->m_defGridAttr
);
2139 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2141 // The size of a cell is normally 1,1
2143 // If this cell is larger (2,2) then this is the top left cell
2144 // the other cells that will be covered (lower right cells) must be
2145 // set to negative or zero values such that
2146 // row + num_rows of the covered cell points to the larger cell (this cell)
2147 // same goes for the col + num_cols.
2149 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2151 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2152 !((num_rows
<=0)&&(num_cols
>0)) ||
2153 !((num_rows
==0)&&(num_cols
==0))),
2154 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2156 m_sizeRows
= num_rows
;
2157 m_sizeCols
= num_cols
;
2160 const wxColour
& wxGridCellAttr::GetTextColour() const
2162 if (HasTextColour())
2166 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2168 return m_defGridAttr
->GetTextColour();
2172 wxFAIL_MSG(wxT("Missing default cell attribute"));
2173 return wxNullColour
;
2178 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2180 if (HasBackgroundColour())
2182 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2183 return m_defGridAttr
->GetBackgroundColour();
2186 wxFAIL_MSG(wxT("Missing default cell attribute"));
2187 return wxNullColour
;
2192 const wxFont
& wxGridCellAttr::GetFont() const
2196 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2197 return m_defGridAttr
->GetFont();
2200 wxFAIL_MSG(wxT("Missing default cell attribute"));
2206 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2210 if ( hAlign
) *hAlign
= m_hAlign
;
2211 if ( vAlign
) *vAlign
= m_vAlign
;
2213 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2214 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2217 wxFAIL_MSG(wxT("Missing default cell attribute"));
2221 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2223 if ( num_rows
) *num_rows
= m_sizeRows
;
2224 if ( num_cols
) *num_cols
= m_sizeCols
;
2227 // GetRenderer and GetEditor use a slightly different decision path about
2228 // which attribute to use. If a non-default attr object has one then it is
2229 // used, otherwise the default editor or renderer is fetched from the grid and
2230 // used. It should be the default for the data type of the cell. If it is
2231 // NULL (because the table has a type that the grid does not have in its
2232 // registry,) then the grid's default editor or renderer is used.
2234 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2236 wxGridCellRenderer
*renderer
;
2238 if ( m_renderer
&& this != m_defGridAttr
)
2240 // use the cells renderer if it has one
2241 renderer
= m_renderer
;
2244 else // no non default cell renderer
2246 // get default renderer for the data type
2249 // GetDefaultRendererForCell() will do IncRef() for us
2250 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2259 if (m_defGridAttr
&& this != m_defGridAttr
)
2261 // if we still don't have one then use the grid default
2262 // (no need for IncRef() here neither)
2263 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2265 else // default grid attr
2267 // use m_renderer which we had decided not to use initially
2268 renderer
= m_renderer
;
2275 // we're supposed to always find something
2276 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2281 // same as above, except for s/renderer/editor/g
2282 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2284 wxGridCellEditor
*editor
;
2286 if ( m_editor
&& this != m_defGridAttr
)
2288 // use the cells editor if it has one
2292 else // no non default cell editor
2294 // get default editor for the data type
2297 // GetDefaultEditorForCell() will do IncRef() for us
2298 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2307 if ( m_defGridAttr
&& this != m_defGridAttr
)
2309 // if we still don't have one then use the grid default
2310 // (no need for IncRef() here neither)
2311 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2313 else // default grid attr
2315 // use m_editor which we had decided not to use initially
2323 // we're supposed to always find something
2324 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2329 // ----------------------------------------------------------------------------
2330 // wxGridCellAttrData
2331 // ----------------------------------------------------------------------------
2333 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2335 int n
= FindIndex(row
, col
);
2336 if ( n
== wxNOT_FOUND
)
2338 // add the attribute
2339 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2343 // free the old attribute
2344 m_attrs
[(size_t)n
].attr
->DecRef();
2348 // change the attribute
2349 m_attrs
[(size_t)n
].attr
= attr
;
2353 // remove this attribute
2354 m_attrs
.RemoveAt((size_t)n
);
2359 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2361 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2363 int n
= FindIndex(row
, col
);
2364 if ( n
!= wxNOT_FOUND
)
2366 attr
= m_attrs
[(size_t)n
].attr
;
2373 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2375 size_t count
= m_attrs
.GetCount();
2376 for ( size_t n
= 0; n
< count
; n
++ )
2378 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2379 wxCoord row
= coords
.GetRow();
2380 if ((size_t)row
>= pos
)
2384 // If rows inserted, include row counter where necessary
2385 coords
.SetRow(row
+ numRows
);
2387 else if (numRows
< 0)
2389 // If rows deleted ...
2390 if ((size_t)row
>= pos
- numRows
)
2392 // ...either decrement row counter (if row still exists)...
2393 coords
.SetRow(row
+ numRows
);
2397 // ...or remove the attribute
2398 m_attrs
.RemoveAt((size_t)n
);
2406 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2408 size_t count
= m_attrs
.GetCount();
2409 for ( size_t n
= 0; n
< count
; n
++ )
2411 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2412 wxCoord col
= coords
.GetCol();
2413 if ( (size_t)col
>= pos
)
2417 // If rows inserted, include row counter where necessary
2418 coords
.SetCol(col
+ numCols
);
2420 else if (numCols
< 0)
2422 // If rows deleted ...
2423 if ((size_t)col
>= pos
- numCols
)
2425 // ...either decrement row counter (if row still exists)...
2426 coords
.SetCol(col
+ numCols
);
2430 // ...or remove the attribute
2431 m_attrs
.RemoveAt((size_t)n
);
2439 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2441 size_t count
= m_attrs
.GetCount();
2442 for ( size_t n
= 0; n
< count
; n
++ )
2444 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2445 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2454 // ----------------------------------------------------------------------------
2455 // wxGridRowOrColAttrData
2456 // ----------------------------------------------------------------------------
2458 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2460 size_t count
= m_attrs
.Count();
2461 for ( size_t n
= 0; n
< count
; n
++ )
2463 m_attrs
[n
]->DecRef();
2467 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2469 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2471 int n
= m_rowsOrCols
.Index(rowOrCol
);
2472 if ( n
!= wxNOT_FOUND
)
2474 attr
= m_attrs
[(size_t)n
];
2481 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2483 int i
= m_rowsOrCols
.Index(rowOrCol
);
2484 if ( i
== wxNOT_FOUND
)
2486 // add the attribute
2487 m_rowsOrCols
.Add(rowOrCol
);
2492 size_t n
= (size_t)i
;
2495 // change the attribute
2496 m_attrs
[n
]->DecRef();
2501 // remove this attribute
2502 m_attrs
[n
]->DecRef();
2503 m_rowsOrCols
.RemoveAt(n
);
2504 m_attrs
.RemoveAt(n
);
2509 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2511 size_t count
= m_attrs
.GetCount();
2512 for ( size_t n
= 0; n
< count
; n
++ )
2514 int & rowOrCol
= m_rowsOrCols
[n
];
2515 if ( (size_t)rowOrCol
>= pos
)
2517 if ( numRowsOrCols
> 0 )
2519 // If rows inserted, include row counter where necessary
2520 rowOrCol
+= numRowsOrCols
;
2522 else if ( numRowsOrCols
< 0)
2524 // If rows deleted, either decrement row counter (if row still exists)
2525 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2526 rowOrCol
+= numRowsOrCols
;
2529 m_rowsOrCols
.RemoveAt((size_t)n
);
2530 m_attrs
.RemoveAt((size_t)n
);
2538 // ----------------------------------------------------------------------------
2539 // wxGridCellAttrProvider
2540 // ----------------------------------------------------------------------------
2542 wxGridCellAttrProvider::wxGridCellAttrProvider()
2544 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2547 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2552 void wxGridCellAttrProvider::InitData()
2554 m_data
= new wxGridCellAttrProviderData
;
2557 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2558 wxGridCellAttr::wxAttrKind kind
) const
2560 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2565 case (wxGridCellAttr::Any
):
2566 //Get cached merge attributes.
2567 // Currenlty not used as no cache implemented as not mutiable
2568 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2571 //Basicaly implement old version.
2572 //Also check merge cache, so we don't have to re-merge every time..
2573 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2574 *attrrow
= (wxGridCellAttr
*)NULL
,
2575 *attrcol
= (wxGridCellAttr
*)NULL
;
2577 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2578 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2579 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2581 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2582 // Two or move are non NULL
2583 attr
= new wxGridCellAttr
;
2584 attr
->SetKind(wxGridCellAttr::Merged
);
2588 attr
->MergeWith(attrcell
);
2592 attr
->MergeWith(attrcol
);
2596 attr
->MergeWith(attrrow
);
2599 //store merge attr if cache implemented
2601 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2605 // one or none is non null return it or null.
2606 if(attrrow
) attr
= attrrow
;
2607 if(attrcol
) attr
= attrcol
;
2608 if(attrcell
) attr
= attrcell
;
2612 case (wxGridCellAttr::Cell
):
2613 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2615 case (wxGridCellAttr::Col
):
2616 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2618 case (wxGridCellAttr::Row
):
2619 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2623 // (wxGridCellAttr::Default):
2624 // (wxGridCellAttr::Merged):
2631 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2637 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2640 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2645 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2648 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2653 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2656 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2660 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2662 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2666 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2670 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2672 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2676 // ----------------------------------------------------------------------------
2677 // wxGridTypeRegistry
2678 // ----------------------------------------------------------------------------
2680 wxGridTypeRegistry::~wxGridTypeRegistry()
2682 size_t count
= m_typeinfo
.Count();
2683 for ( size_t i
= 0; i
< count
; i
++ )
2684 delete m_typeinfo
[i
];
2688 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2689 wxGridCellRenderer
* renderer
,
2690 wxGridCellEditor
* editor
)
2692 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2694 // is it already registered?
2695 int loc
= FindRegisteredDataType(typeName
);
2696 if ( loc
!= wxNOT_FOUND
)
2698 delete m_typeinfo
[loc
];
2699 m_typeinfo
[loc
] = info
;
2703 m_typeinfo
.Add(info
);
2707 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2709 size_t count
= m_typeinfo
.GetCount();
2710 for ( size_t i
= 0; i
< count
; i
++ )
2712 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2721 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2723 int index
= FindRegisteredDataType(typeName
);
2724 if ( index
== wxNOT_FOUND
)
2726 // check whether this is one of the standard ones, in which case
2727 // register it "on the fly"
2729 if ( typeName
== wxGRID_VALUE_STRING
)
2731 RegisterDataType(wxGRID_VALUE_STRING
,
2732 new wxGridCellStringRenderer
,
2733 new wxGridCellTextEditor
);
2735 #endif // wxUSE_TEXTCTRL
2737 if ( typeName
== wxGRID_VALUE_BOOL
)
2739 RegisterDataType(wxGRID_VALUE_BOOL
,
2740 new wxGridCellBoolRenderer
,
2741 new wxGridCellBoolEditor
);
2743 #endif // wxUSE_CHECKBOX
2745 if ( typeName
== wxGRID_VALUE_NUMBER
)
2747 RegisterDataType(wxGRID_VALUE_NUMBER
,
2748 new wxGridCellNumberRenderer
,
2749 new wxGridCellNumberEditor
);
2751 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2753 RegisterDataType(wxGRID_VALUE_FLOAT
,
2754 new wxGridCellFloatRenderer
,
2755 new wxGridCellFloatEditor
);
2757 #endif // wxUSE_TEXTCTRL
2759 if ( typeName
== wxGRID_VALUE_CHOICE
)
2761 RegisterDataType(wxGRID_VALUE_CHOICE
,
2762 new wxGridCellStringRenderer
,
2763 new wxGridCellChoiceEditor
);
2765 #endif // wxUSE_COMBOBOX
2770 // we get here only if just added the entry for this type, so return
2772 index
= m_typeinfo
.GetCount() - 1;
2778 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2780 int index
= FindDataType(typeName
);
2781 if ( index
== wxNOT_FOUND
)
2783 // the first part of the typename is the "real" type, anything after ':'
2784 // are the parameters for the renderer
2785 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2786 if ( index
== wxNOT_FOUND
)
2791 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2792 wxGridCellRenderer
*rendererOld
= renderer
;
2793 renderer
= renderer
->Clone();
2794 rendererOld
->DecRef();
2796 wxGridCellEditor
*editor
= GetEditor(index
);
2797 wxGridCellEditor
*editorOld
= editor
;
2798 editor
= editor
->Clone();
2799 editorOld
->DecRef();
2801 // do it even if there are no parameters to reset them to defaults
2802 wxString params
= typeName
.AfterFirst(_T(':'));
2803 renderer
->SetParameters(params
);
2804 editor
->SetParameters(params
);
2806 // register the new typename
2807 RegisterDataType(typeName
, renderer
, editor
);
2809 // we just registered it, it's the last one
2810 index
= m_typeinfo
.GetCount() - 1;
2816 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2818 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2824 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2826 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2832 // ----------------------------------------------------------------------------
2834 // ----------------------------------------------------------------------------
2836 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2839 wxGridTableBase::wxGridTableBase()
2841 m_view
= (wxGrid
*) NULL
;
2842 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2845 wxGridTableBase::~wxGridTableBase()
2847 delete m_attrProvider
;
2850 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2852 delete m_attrProvider
;
2853 m_attrProvider
= attrProvider
;
2856 bool wxGridTableBase::CanHaveAttributes()
2858 if ( ! GetAttrProvider() )
2860 // use the default attr provider by default
2861 SetAttrProvider(new wxGridCellAttrProvider
);
2866 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2868 if ( m_attrProvider
)
2869 return m_attrProvider
->GetAttr(row
, col
, kind
);
2871 return (wxGridCellAttr
*)NULL
;
2874 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2876 if ( m_attrProvider
)
2878 attr
->SetKind(wxGridCellAttr::Cell
);
2879 m_attrProvider
->SetAttr(attr
, row
, col
);
2883 // as we take ownership of the pointer and don't store it, we must
2889 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2891 if ( m_attrProvider
)
2893 attr
->SetKind(wxGridCellAttr::Row
);
2894 m_attrProvider
->SetRowAttr(attr
, row
);
2898 // as we take ownership of the pointer and don't store it, we must
2904 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2906 if ( m_attrProvider
)
2908 attr
->SetKind(wxGridCellAttr::Col
);
2909 m_attrProvider
->SetColAttr(attr
, col
);
2913 // as we take ownership of the pointer and don't store it, we must
2919 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2920 size_t WXUNUSED(numRows
) )
2922 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2927 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2929 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2934 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2935 size_t WXUNUSED(numRows
) )
2937 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2942 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2943 size_t WXUNUSED(numCols
) )
2945 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2950 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2952 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2957 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2958 size_t WXUNUSED(numCols
) )
2960 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2966 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2969 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2970 // how much it makes sense to us geeks.
2974 wxString
wxGridTableBase::GetColLabelValue( int col
)
2976 // default col labels are:
2977 // cols 0 to 25 : A-Z
2978 // cols 26 to 675 : AA-ZZ
2983 for ( n
= 1; ; n
++ )
2985 s
+= (_T('A') + (wxChar
)( col%26
));
2987 if ( col
< 0 ) break;
2990 // reverse the string...
2992 for ( i
= 0; i
< n
; i
++ )
3001 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3003 return wxGRID_VALUE_STRING
;
3006 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3007 const wxString
& typeName
)
3009 return typeName
== wxGRID_VALUE_STRING
;
3012 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3014 return CanGetValueAs(row
, col
, typeName
);
3017 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3022 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3027 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3032 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3033 long WXUNUSED(value
) )
3037 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3038 double WXUNUSED(value
) )
3042 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3043 bool WXUNUSED(value
) )
3048 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3049 const wxString
& WXUNUSED(typeName
) )
3054 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3055 const wxString
& WXUNUSED(typeName
),
3056 void* WXUNUSED(value
) )
3060 //////////////////////////////////////////////////////////////////////
3062 // Message class for the grid table to send requests and notifications
3066 wxGridTableMessage::wxGridTableMessage()
3068 m_table
= (wxGridTableBase
*) NULL
;
3074 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3075 int commandInt1
, int commandInt2
)
3079 m_comInt1
= commandInt1
;
3080 m_comInt2
= commandInt2
;
3085 //////////////////////////////////////////////////////////////////////
3087 // A basic grid table for string data. An object of this class will
3088 // created by wxGrid if you don't specify an alternative table class.
3091 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3093 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3095 wxGridStringTable::wxGridStringTable()
3100 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3103 m_data
.Alloc( numRows
);
3106 sa
.Alloc( numCols
);
3107 sa
.Add( wxEmptyString
, numCols
);
3109 m_data
.Add( sa
, numRows
);
3112 wxGridStringTable::~wxGridStringTable()
3116 int wxGridStringTable::GetNumberRows()
3118 return m_data
.GetCount();
3121 int wxGridStringTable::GetNumberCols()
3123 if ( m_data
.GetCount() > 0 )
3124 return m_data
[0].GetCount();
3129 wxString
wxGridStringTable::GetValue( int row
, int col
)
3131 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3132 _T("invalid row or column index in wxGridStringTable") );
3134 return m_data
[row
][col
];
3137 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3139 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3140 _T("invalid row or column index in wxGridStringTable") );
3142 m_data
[row
][col
] = value
;
3145 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3147 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3148 _T("invalid row or column index in wxGridStringTable") );
3150 return (m_data
[row
][col
] == wxEmptyString
);
3153 void wxGridStringTable::Clear()
3156 int numRows
, numCols
;
3158 numRows
= m_data
.GetCount();
3161 numCols
= m_data
[0].GetCount();
3163 for ( row
= 0; row
< numRows
; row
++ )
3165 for ( col
= 0; col
< numCols
; col
++ )
3167 m_data
[row
][col
] = wxEmptyString
;
3174 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3176 size_t curNumRows
= m_data
.GetCount();
3177 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3178 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3180 if ( pos
>= curNumRows
)
3182 return AppendRows( numRows
);
3186 sa
.Alloc( curNumCols
);
3187 sa
.Add( wxEmptyString
, curNumCols
);
3188 m_data
.Insert( sa
, pos
, numRows
);
3191 wxGridTableMessage
msg( this,
3192 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3196 GetView()->ProcessTableMessage( msg
);
3202 bool wxGridStringTable::AppendRows( size_t numRows
)
3204 size_t curNumRows
= m_data
.GetCount();
3205 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3206 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3209 if ( curNumCols
> 0 )
3211 sa
.Alloc( curNumCols
);
3212 sa
.Add( wxEmptyString
, curNumCols
);
3215 m_data
.Add( sa
, numRows
);
3219 wxGridTableMessage
msg( this,
3220 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3223 GetView()->ProcessTableMessage( msg
);
3229 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3231 size_t curNumRows
= m_data
.GetCount();
3233 if ( pos
>= curNumRows
)
3235 wxFAIL_MSG( wxString::Format
3237 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3239 (unsigned long)numRows
,
3240 (unsigned long)curNumRows
3246 if ( numRows
> curNumRows
- pos
)
3248 numRows
= curNumRows
- pos
;
3251 if ( numRows
>= curNumRows
)
3257 m_data
.RemoveAt( pos
, numRows
);
3261 wxGridTableMessage
msg( this,
3262 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3266 GetView()->ProcessTableMessage( msg
);
3272 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3276 size_t curNumRows
= m_data
.GetCount();
3277 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3278 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3280 if ( pos
>= curNumCols
)
3282 return AppendCols( numCols
);
3285 for ( row
= 0; row
< curNumRows
; row
++ )
3287 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3289 m_data
[row
].Insert( wxEmptyString
, col
);
3294 wxGridTableMessage
msg( this,
3295 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3299 GetView()->ProcessTableMessage( msg
);
3305 bool wxGridStringTable::AppendCols( size_t numCols
)
3309 size_t curNumRows
= m_data
.GetCount();
3313 // TODO: something better than this ?
3315 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3320 for ( row
= 0; row
< curNumRows
; row
++ )
3322 m_data
[row
].Add( wxEmptyString
, numCols
);
3327 wxGridTableMessage
msg( this,
3328 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3331 GetView()->ProcessTableMessage( msg
);
3337 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3341 size_t curNumRows
= m_data
.GetCount();
3342 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3343 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3345 if ( pos
>= curNumCols
)
3347 wxFAIL_MSG( wxString::Format
3349 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3351 (unsigned long)numCols
,
3352 (unsigned long)curNumCols
3357 if ( numCols
> curNumCols
- pos
)
3359 numCols
= curNumCols
- pos
;
3362 for ( row
= 0; row
< curNumRows
; row
++ )
3364 if ( numCols
>= curNumCols
)
3366 m_data
[row
].Clear();
3370 m_data
[row
].RemoveAt( pos
, numCols
);
3375 wxGridTableMessage
msg( this,
3376 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3380 GetView()->ProcessTableMessage( msg
);
3386 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3388 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3390 // using default label
3392 return wxGridTableBase::GetRowLabelValue( row
);
3396 return m_rowLabels
[ row
];
3400 wxString
wxGridStringTable::GetColLabelValue( int col
)
3402 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3404 // using default label
3406 return wxGridTableBase::GetColLabelValue( col
);
3410 return m_colLabels
[ col
];
3414 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3416 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3418 int n
= m_rowLabels
.GetCount();
3420 for ( i
= n
; i
<= row
; i
++ )
3422 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3426 m_rowLabels
[row
] = value
;
3429 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3431 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3433 int n
= m_colLabels
.GetCount();
3435 for ( i
= n
; i
<= col
; i
++ )
3437 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3441 m_colLabels
[col
] = value
;
3446 //////////////////////////////////////////////////////////////////////
3447 //////////////////////////////////////////////////////////////////////
3449 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3451 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3452 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3453 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3454 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3455 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3456 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3459 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3461 const wxPoint
&pos
, const wxSize
&size
)
3462 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3467 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3471 // NO - don't do this because it will set both the x and y origin
3472 // coords to match the parent scrolled window and we just want to
3473 // set the y coord - MB
3475 // m_owner->PrepareDC( dc );
3478 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3479 dc
.SetDeviceOrigin( 0, -y
);
3481 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3482 m_owner
->DrawRowLabels( dc
, rows
);
3486 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3488 m_owner
->ProcessRowLabelMouseEvent( event
);
3492 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3494 m_owner
->GetEventHandler()->ProcessEvent(event
);
3498 // This seems to be required for wxMotif otherwise the mouse
3499 // cursor must be in the cell edit control to get key events
3501 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3503 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3506 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3508 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3513 //////////////////////////////////////////////////////////////////////
3515 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3517 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3518 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3519 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3520 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3521 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3522 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3525 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3527 const wxPoint
&pos
, const wxSize
&size
)
3528 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3533 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3537 // NO - don't do this because it will set both the x and y origin
3538 // coords to match the parent scrolled window and we just want to
3539 // set the x coord - MB
3541 // m_owner->PrepareDC( dc );
3544 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3545 dc
.SetDeviceOrigin( -x
, 0 );
3547 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3548 m_owner
->DrawColLabels( dc
, cols
);
3552 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3554 m_owner
->ProcessColLabelMouseEvent( event
);
3557 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3559 m_owner
->GetEventHandler()->ProcessEvent(event
);
3563 // This seems to be required for wxMotif otherwise the mouse
3564 // cursor must be in the cell edit control to get key events
3566 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3568 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3571 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3573 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3578 //////////////////////////////////////////////////////////////////////
3580 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3582 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3583 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3584 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3585 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3586 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3587 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3590 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3592 const wxPoint
&pos
, const wxSize
&size
)
3593 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3598 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3602 int client_height
= 0;
3603 int client_width
= 0;
3604 GetClientSize( &client_width
, &client_height
);
3606 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3607 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3608 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3609 dc
.DrawLine( 0, 0, client_width
, 0 );
3610 dc
.DrawLine( 0, 0, 0, client_height
);
3612 dc
.SetPen( *wxWHITE_PEN
);
3613 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3614 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3618 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3620 m_owner
->ProcessCornerLabelMouseEvent( event
);
3624 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3626 m_owner
->GetEventHandler()->ProcessEvent(event
);
3629 // This seems to be required for wxMotif otherwise the mouse
3630 // cursor must be in the cell edit control to get key events
3632 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3634 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3637 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3639 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3644 //////////////////////////////////////////////////////////////////////
3646 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3648 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3649 EVT_PAINT( wxGridWindow::OnPaint
)
3650 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3651 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3652 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3653 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3654 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3657 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3658 wxGridRowLabelWindow
*rowLblWin
,
3659 wxGridColLabelWindow
*colLblWin
,
3662 const wxSize
&size
)
3663 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3664 wxT("grid window") )
3668 m_rowLabelWin
= rowLblWin
;
3669 m_colLabelWin
= colLblWin
;
3670 SetBackgroundColour(_T("WHITE"));
3674 wxGridWindow::~wxGridWindow()
3679 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3681 wxPaintDC
dc( this );
3682 m_owner
->PrepareDC( dc
);
3683 wxRegion reg
= GetUpdateRegion();
3684 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3685 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3686 #if WXGRID_DRAW_LINES
3687 m_owner
->DrawAllGridLines( dc
, reg
);
3689 m_owner
->DrawGridSpace( dc
);
3690 m_owner
->DrawHighlight( dc
, DirtyCells
);
3694 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3696 wxWindow::ScrollWindow( dx
, dy
, rect
);
3697 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3698 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3702 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3704 m_owner
->ProcessGridCellMouseEvent( event
);
3707 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3709 m_owner
->GetEventHandler()->ProcessEvent(event
);
3712 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3713 // cursor must be in the cell edit control to get key events
3715 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3717 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3720 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3722 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3725 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3730 //////////////////////////////////////////////////////////////////////
3732 // Internal Helper function for computing row or column from some
3733 // (unscrolled) coordinate value, using either
3734 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3735 // of m_rowBottoms/m_ColRights to speed up the search!
3737 // Internal helper macros for simpler use of that function
3739 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3740 const wxArrayInt
& BorderArray
, int nMax
,
3743 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3744 WXGRID_MIN_COL_WIDTH, \
3745 m_colRights, m_numCols, TRUE)
3746 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3747 WXGRID_MIN_ROW_HEIGHT, \
3748 m_rowBottoms, m_numRows, TRUE)
3749 /////////////////////////////////////////////////////////////////////
3751 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3753 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3754 EVT_PAINT( wxGrid::OnPaint
)
3755 EVT_SIZE( wxGrid::OnSize
)
3756 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3757 EVT_KEY_UP( wxGrid::OnKeyUp
)
3758 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3761 wxGrid::wxGrid( wxWindow
*parent
,
3766 const wxString
& name
)
3767 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3768 m_colMinWidths(GRID_HASH_SIZE
),
3769 m_rowMinHeights(GRID_HASH_SIZE
)
3777 // Must do this or ~wxScrollHelper will pop the wrong event handler
3778 SetTargetWindow(this);
3780 wxSafeDecRef(m_defaultCellAttr
);
3782 #ifdef DEBUG_ATTR_CACHE
3783 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3784 wxPrintf(_T("wxGrid attribute cache statistics: "
3785 "total: %u, hits: %u (%u%%)\n"),
3786 total
, gs_nAttrCacheHits
,
3787 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3793 delete m_typeRegistry
;
3799 // ----- internal init and update functions
3802 void wxGrid::Create()
3804 m_created
= FALSE
; // set to TRUE by CreateGrid
3806 m_table
= (wxGridTableBase
*) NULL
;
3809 m_cellEditCtrlEnabled
= FALSE
;
3811 m_defaultCellAttr
= new wxGridCellAttr();
3813 // Set default cell attributes
3814 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3815 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3816 m_defaultCellAttr
->SetFont(GetFont());
3817 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3818 m_defaultCellAttr
->SetTextColour(
3819 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3820 m_defaultCellAttr
->SetBackgroundColour(
3821 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3822 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3823 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3828 m_currentCellCoords
= wxGridNoCellCoords
;
3830 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3831 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3833 // create the type registry
3834 m_typeRegistry
= new wxGridTypeRegistry
;
3837 // subwindow components that make up the wxGrid
3838 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3843 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3848 m_colLabelWin
= new wxGridColLabelWindow( this,
3853 m_gridWin
= new wxGridWindow( this,
3860 SetTargetWindow( m_gridWin
);
3866 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3867 wxGrid::wxGridSelectionModes selmode
)
3869 wxCHECK_MSG( !m_created
,
3871 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3873 m_numRows
= numRows
;
3874 m_numCols
= numCols
;
3876 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3877 m_table
->SetView( this );
3879 m_selection
= new wxGridSelection( this, selmode
);
3888 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3890 wxCHECK_RET( m_created
,
3891 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3893 m_selection
->SetSelectionMode( selmode
);
3896 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3898 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3899 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3901 return m_selection
->GetSelectionMode();
3904 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3905 wxGrid::wxGridSelectionModes selmode
)
3909 // RD: Actually, this should probably be allowed. I think it would be
3910 // nice to be able to switch multiple Tables in and out of a single
3911 // View at runtime. Is there anything in the implementation that
3912 // would prevent this?
3914 // At least, you now have to cope with m_selection
3915 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3920 m_numRows
= table
->GetNumberRows();
3921 m_numCols
= table
->GetNumberCols();
3924 m_table
->SetView( this );
3927 m_selection
= new wxGridSelection( this, selmode
);
3940 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3941 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3943 if ( m_rowLabelWin
)
3945 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3949 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3952 m_labelTextColour
= wxColour( _T("BLACK") );
3955 m_attrCache
.row
= -1;
3956 m_attrCache
.col
= -1;
3957 m_attrCache
.attr
= NULL
;
3959 // TODO: something better than this ?
3961 m_labelFont
= this->GetFont();
3962 m_labelFont
.SetWeight( wxBOLD
);
3964 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
3965 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3967 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3968 m_colLabelVertAlign
= wxALIGN_CENTRE
;
3969 m_colLabelTextOrientation
= wxHORIZONTAL
;
3971 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3972 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3974 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3975 m_defaultRowHeight
+= 8;
3977 m_defaultRowHeight
+= 4;
3980 m_gridLineColour
= wxColour( 192,192,192 );
3981 m_gridLinesEnabled
= TRUE
;
3982 m_cellHighlightColour
= *wxBLACK
;
3983 m_cellHighlightPenWidth
= 2;
3984 m_cellHighlightROPenWidth
= 1;
3986 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3987 m_winCapture
= (wxWindow
*)NULL
;
3988 m_canDragRowSize
= TRUE
;
3989 m_canDragColSize
= TRUE
;
3990 m_canDragGridSize
= TRUE
;
3992 m_dragRowOrCol
= -1;
3993 m_isDragging
= FALSE
;
3994 m_startDragPos
= wxDefaultPosition
;
3996 m_waitForSlowClick
= FALSE
;
3998 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3999 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4001 m_currentCellCoords
= wxGridNoCellCoords
;
4003 m_selectingTopLeft
= wxGridNoCellCoords
;
4004 m_selectingBottomRight
= wxGridNoCellCoords
;
4005 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4006 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4008 m_editable
= TRUE
; // default for whole grid
4010 m_inOnKeyDown
= FALSE
;
4017 // ----------------------------------------------------------------------------
4018 // the idea is to call these functions only when necessary because they create
4019 // quite big arrays which eat memory mostly unnecessary - in particular, if
4020 // default widths/heights are used for all rows/columns, we may not use these
4023 // with some extra code, it should be possible to only store the
4024 // widths/heights different from default ones but this will be done later...
4025 // ----------------------------------------------------------------------------
4027 void wxGrid::InitRowHeights()
4029 m_rowHeights
.Empty();
4030 m_rowBottoms
.Empty();
4032 m_rowHeights
.Alloc( m_numRows
);
4033 m_rowBottoms
.Alloc( m_numRows
);
4037 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4039 for ( int i
= 0; i
< m_numRows
; i
++ )
4041 rowBottom
+= m_defaultRowHeight
;
4042 m_rowBottoms
.Add( rowBottom
);
4046 void wxGrid::InitColWidths()
4048 m_colWidths
.Empty();
4049 m_colRights
.Empty();
4051 m_colWidths
.Alloc( m_numCols
);
4052 m_colRights
.Alloc( m_numCols
);
4055 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4057 for ( int i
= 0; i
< m_numCols
; i
++ )
4059 colRight
+= m_defaultColWidth
;
4060 m_colRights
.Add( colRight
);
4064 int wxGrid::GetColWidth(int col
) const
4066 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4069 int wxGrid::GetColLeft(int col
) const
4071 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4072 : m_colRights
[col
] - m_colWidths
[col
];
4075 int wxGrid::GetColRight(int col
) const
4077 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4081 int wxGrid::GetRowHeight(int row
) const
4083 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4086 int wxGrid::GetRowTop(int row
) const
4088 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4089 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4092 int wxGrid::GetRowBottom(int row
) const
4094 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4095 : m_rowBottoms
[row
];
4098 void wxGrid::CalcDimensions()
4101 GetClientSize( &cw
, &ch
);
4103 if ( m_rowLabelWin
->IsShown() )
4104 cw
-= m_rowLabelWidth
;
4105 if ( m_colLabelWin
->IsShown() )
4106 ch
-= m_colLabelHeight
;
4109 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4110 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4112 // take into account editor if shown
4113 if( IsCellEditControlShown() )
4116 int r
= m_currentCellCoords
.GetRow();
4117 int c
= m_currentCellCoords
.GetCol();
4118 int x
= GetColLeft(c
);
4119 int y
= GetRowTop(r
);
4121 // how big is the editor
4122 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4123 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4124 editor
->GetControl()->GetSize(&w2
, &h2
);
4127 if( w2
> w
) w
= w2
;
4128 if( h2
> h
) h
= h2
;
4133 // preserve (more or less) the previous position
4135 GetViewStart( &x
, &y
);
4137 // maybe we don't need scrollbars at all?
4139 // also adjust the position to be valid for the new scroll rangs
4160 // do set scrollbar parameters
4161 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4162 GetScrollX(w
), GetScrollY(h
), x
, y
,
4163 GetBatchCount() != 0);
4165 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4166 // still must reposition the children
4171 void wxGrid::CalcWindowSizes()
4174 GetClientSize( &cw
, &ch
);
4176 if ( m_cornerLabelWin
->IsShown() )
4177 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4179 if ( m_colLabelWin
->IsShown() )
4180 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4182 if ( m_rowLabelWin
->IsShown() )
4183 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4185 if ( m_gridWin
->IsShown() )
4186 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4190 // this is called when the grid table sends a message to say that it
4191 // has been redimensioned
4193 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4196 bool result
= FALSE
;
4198 // Clear the attribute cache as the attribute might refer to a different
4199 // cell than stored in the cache after adding/removing rows/columns.
4201 // By the same reasoning, the editor should be dismissed if columns are
4202 // added or removed. And for consistency, it should IMHO always be
4203 // removed, not only if the cell "underneath" it actually changes.
4204 // For now, I intentionally do not save the editor's content as the
4205 // cell it might want to save that stuff to might no longer exist.
4206 HideCellEditControl();
4208 // if we were using the default widths/heights so far, we must change them
4210 if ( m_colWidths
.IsEmpty() )
4215 if ( m_rowHeights
.IsEmpty() )
4221 switch ( msg
.GetId() )
4223 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4225 size_t pos
= msg
.GetCommandInt();
4226 int numRows
= msg
.GetCommandInt2();
4228 m_numRows
+= numRows
;
4230 if ( !m_rowHeights
.IsEmpty() )
4232 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4233 m_rowBottoms
.Insert( 0, pos
, numRows
);
4236 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4238 for ( i
= pos
; i
< m_numRows
; i
++ )
4240 bottom
+= m_rowHeights
[i
];
4241 m_rowBottoms
[i
] = bottom
;
4244 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4246 // if we have just inserted cols into an empty grid the current
4247 // cell will be undefined...
4249 SetCurrentCell( 0, 0 );
4253 m_selection
->UpdateRows( pos
, numRows
);
4254 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4256 attrProvider
->UpdateAttrRows( pos
, numRows
);
4258 if ( !GetBatchCount() )
4261 m_rowLabelWin
->Refresh();
4267 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4269 int numRows
= msg
.GetCommandInt();
4270 int oldNumRows
= m_numRows
;
4271 m_numRows
+= numRows
;
4273 if ( !m_rowHeights
.IsEmpty() )
4275 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4276 m_rowBottoms
.Add( 0, numRows
);
4279 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4281 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4283 bottom
+= m_rowHeights
[i
];
4284 m_rowBottoms
[i
] = bottom
;
4287 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4289 // if we have just inserted cols into an empty grid the current
4290 // cell will be undefined...
4292 SetCurrentCell( 0, 0 );
4294 if ( !GetBatchCount() )
4297 m_rowLabelWin
->Refresh();
4303 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4305 size_t pos
= msg
.GetCommandInt();
4306 int numRows
= msg
.GetCommandInt2();
4307 m_numRows
-= numRows
;
4309 if ( !m_rowHeights
.IsEmpty() )
4311 m_rowHeights
.RemoveAt( pos
, numRows
);
4312 m_rowBottoms
.RemoveAt( pos
, numRows
);
4315 for ( i
= 0; i
< m_numRows
; i
++ )
4317 h
+= m_rowHeights
[i
];
4318 m_rowBottoms
[i
] = h
;
4323 m_currentCellCoords
= wxGridNoCellCoords
;
4327 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4328 m_currentCellCoords
.Set( 0, 0 );
4332 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4333 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4335 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4336 // ifdef'd out following patch from Paul Gammans
4338 // No need to touch column attributes, unless we
4339 // removed _all_ rows, in this case, we remove
4340 // all column attributes.
4341 // I hate to do this here, but the
4342 // needed data is not available inside UpdateAttrRows.
4343 if ( !GetNumberRows() )
4344 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4347 if ( !GetBatchCount() )
4350 m_rowLabelWin
->Refresh();
4356 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4358 size_t pos
= msg
.GetCommandInt();
4359 int numCols
= msg
.GetCommandInt2();
4360 m_numCols
+= numCols
;
4362 if ( !m_colWidths
.IsEmpty() )
4364 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4365 m_colRights
.Insert( 0, pos
, numCols
);
4368 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4370 for ( i
= pos
; i
< m_numCols
; i
++ )
4372 right
+= m_colWidths
[i
];
4373 m_colRights
[i
] = right
;
4376 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4378 // if we have just inserted cols into an empty grid the current
4379 // cell will be undefined...
4381 SetCurrentCell( 0, 0 );
4385 m_selection
->UpdateCols( pos
, numCols
);
4386 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4388 attrProvider
->UpdateAttrCols( pos
, numCols
);
4389 if ( !GetBatchCount() )
4392 m_colLabelWin
->Refresh();
4399 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4401 int numCols
= msg
.GetCommandInt();
4402 int oldNumCols
= m_numCols
;
4403 m_numCols
+= numCols
;
4404 if ( !m_colWidths
.IsEmpty() )
4406 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4407 m_colRights
.Add( 0, numCols
);
4410 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4412 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4414 right
+= m_colWidths
[i
];
4415 m_colRights
[i
] = right
;
4418 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4420 // if we have just inserted cols into an empty grid the current
4421 // cell will be undefined...
4423 SetCurrentCell( 0, 0 );
4425 if ( !GetBatchCount() )
4428 m_colLabelWin
->Refresh();
4434 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4436 size_t pos
= msg
.GetCommandInt();
4437 int numCols
= msg
.GetCommandInt2();
4438 m_numCols
-= numCols
;
4440 if ( !m_colWidths
.IsEmpty() )
4442 m_colWidths
.RemoveAt( pos
, numCols
);
4443 m_colRights
.RemoveAt( pos
, numCols
);
4446 for ( i
= 0; i
< m_numCols
; i
++ )
4448 w
+= m_colWidths
[i
];
4454 m_currentCellCoords
= wxGridNoCellCoords
;
4458 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4459 m_currentCellCoords
.Set( 0, 0 );
4463 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4464 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4466 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4467 // ifdef'd out following patch from Paul Gammans
4469 // No need to touch row attributes, unless we
4470 // removed _all_ columns, in this case, we remove
4471 // all row attributes.
4472 // I hate to do this here, but the
4473 // needed data is not available inside UpdateAttrCols.
4474 if ( !GetNumberCols() )
4475 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4478 if ( !GetBatchCount() )
4481 m_colLabelWin
->Refresh();
4488 if (result
&& !GetBatchCount() )
4489 m_gridWin
->Refresh();
4494 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4496 wxRegionIterator
iter( reg
);
4499 wxArrayInt rowlabels
;
4506 // TODO: remove this when we can...
4507 // There is a bug in wxMotif that gives garbage update
4508 // rectangles if you jump-scroll a long way by clicking the
4509 // scrollbar with middle button. This is a work-around
4511 #if defined(__WXMOTIF__)
4513 m_gridWin
->GetClientSize( &cw
, &ch
);
4514 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4515 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4518 // logical bounds of update region
4521 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4522 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4524 // find the row labels within these bounds
4527 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4529 if ( GetRowBottom(row
) < top
)
4532 if ( GetRowTop(row
) > bottom
)
4535 rowlabels
.Add( row
);
4545 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4547 wxRegionIterator
iter( reg
);
4550 wxArrayInt colLabels
;
4557 // TODO: remove this when we can...
4558 // There is a bug in wxMotif that gives garbage update
4559 // rectangles if you jump-scroll a long way by clicking the
4560 // scrollbar with middle button. This is a work-around
4562 #if defined(__WXMOTIF__)
4564 m_gridWin
->GetClientSize( &cw
, &ch
);
4565 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4566 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4569 // logical bounds of update region
4572 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4573 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4575 // find the cells within these bounds
4578 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4580 if ( GetColRight(col
) < left
)
4583 if ( GetColLeft(col
) > right
)
4586 colLabels
.Add( col
);
4595 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4597 wxRegionIterator
iter( reg
);
4600 wxGridCellCoordsArray cellsExposed
;
4602 int left
, top
, right
, bottom
;
4607 // TODO: remove this when we can...
4608 // There is a bug in wxMotif that gives garbage update
4609 // rectangles if you jump-scroll a long way by clicking the
4610 // scrollbar with middle button. This is a work-around
4612 #if defined(__WXMOTIF__)
4614 m_gridWin
->GetClientSize( &cw
, &ch
);
4615 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4616 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4617 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4618 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4621 // logical bounds of update region
4623 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4624 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4626 // find the cells within these bounds
4629 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4631 if ( GetRowBottom(row
) <= top
)
4634 if ( GetRowTop(row
) > bottom
)
4637 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4639 if ( GetColRight(col
) <= left
)
4642 if ( GetColLeft(col
) > right
)
4645 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4652 return cellsExposed
;
4656 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4659 wxPoint
pos( event
.GetPosition() );
4660 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4662 if ( event
.Dragging() )
4666 m_isDragging
= TRUE
;
4667 m_rowLabelWin
->CaptureMouse();
4670 if ( event
.LeftIsDown() )
4672 switch( m_cursorMode
)
4674 case WXGRID_CURSOR_RESIZE_ROW
:
4676 int cw
, ch
, left
, dummy
;
4677 m_gridWin
->GetClientSize( &cw
, &ch
);
4678 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4680 wxClientDC
dc( m_gridWin
);
4683 GetRowTop(m_dragRowOrCol
) +
4684 GetRowMinimalHeight(m_dragRowOrCol
) );
4685 dc
.SetLogicalFunction(wxINVERT
);
4686 if ( m_dragLastPos
>= 0 )
4688 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4690 dc
.DrawLine( left
, y
, left
+cw
, y
);
4695 case WXGRID_CURSOR_SELECT_ROW
:
4696 if ( (row
= YToRow( y
)) >= 0 )
4700 m_selection
->SelectRow( row
,
4701 event
.ControlDown(),
4708 // default label to suppress warnings about "enumeration value
4709 // 'xxx' not handled in switch
4717 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4722 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4723 m_isDragging
= FALSE
;
4726 // ------------ Entering or leaving the window
4728 if ( event
.Entering() || event
.Leaving() )
4730 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4734 // ------------ Left button pressed
4736 else if ( event
.LeftDown() )
4738 // don't send a label click event for a hit on the
4739 // edge of the row label - this is probably the user
4740 // wanting to resize the row
4742 if ( YToEdgeOfRow(y
) < 0 )
4746 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4748 if ( !event
.ShiftDown() && !event
.ControlDown() )
4752 if ( event
.ShiftDown() )
4754 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4757 GetNumberCols() - 1,
4758 event
.ControlDown(),
4765 m_selection
->SelectRow( row
,
4766 event
.ControlDown(),
4773 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4778 // starting to drag-resize a row
4780 if ( CanDragRowSize() )
4781 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4786 // ------------ Left double click
4788 else if (event
.LeftDClick() )
4790 int row
= YToEdgeOfRow(y
);
4795 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4797 // no default action at the moment
4802 // adjust row height depending on label text
4803 AutoSizeRowLabelSize( row
);
4805 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4811 // ------------ Left button released
4813 else if ( event
.LeftUp() )
4815 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4817 DoEndDragResizeRow();
4819 // Note: we are ending the event *after* doing
4820 // default processing in this case
4822 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4825 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4830 // ------------ Right button down
4832 else if ( event
.RightDown() )
4836 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4838 // no default action at the moment
4843 // ------------ Right double click
4845 else if ( event
.RightDClick() )
4849 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4851 // no default action at the moment
4856 // ------------ No buttons down and mouse moving
4858 else if ( event
.Moving() )
4860 m_dragRowOrCol
= YToEdgeOfRow( y
);
4861 if ( m_dragRowOrCol
>= 0 )
4863 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4865 // don't capture the mouse yet
4866 if ( CanDragRowSize() )
4867 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4870 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4872 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4878 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4881 wxPoint
pos( event
.GetPosition() );
4882 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4884 if ( event
.Dragging() )
4888 m_isDragging
= TRUE
;
4889 m_colLabelWin
->CaptureMouse();
4892 if ( event
.LeftIsDown() )
4894 switch( m_cursorMode
)
4896 case WXGRID_CURSOR_RESIZE_COL
:
4898 int cw
, ch
, dummy
, top
;
4899 m_gridWin
->GetClientSize( &cw
, &ch
);
4900 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4902 wxClientDC
dc( m_gridWin
);
4905 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4906 GetColMinimalWidth(m_dragRowOrCol
));
4907 dc
.SetLogicalFunction(wxINVERT
);
4908 if ( m_dragLastPos
>= 0 )
4910 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4912 dc
.DrawLine( x
, top
, x
, top
+ch
);
4917 case WXGRID_CURSOR_SELECT_COL
:
4918 if ( (col
= XToCol( x
)) >= 0 )
4922 m_selection
->SelectCol( col
,
4923 event
.ControlDown(),
4930 // default label to suppress warnings about "enumeration value
4931 // 'xxx' not handled in switch
4939 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4944 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4945 m_isDragging
= FALSE
;
4948 // ------------ Entering or leaving the window
4950 if ( event
.Entering() || event
.Leaving() )
4952 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4956 // ------------ Left button pressed
4958 else if ( event
.LeftDown() )
4960 // don't send a label click event for a hit on the
4961 // edge of the col label - this is probably the user
4962 // wanting to resize the col
4964 if ( XToEdgeOfCol(x
) < 0 )
4968 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4970 if ( !event
.ShiftDown() && !event
.ControlDown() )
4974 if ( event
.ShiftDown() )
4976 m_selection
->SelectBlock( 0,
4977 m_currentCellCoords
.GetCol(),
4978 GetNumberRows() - 1, col
,
4979 event
.ControlDown(),
4986 m_selection
->SelectCol( col
,
4987 event
.ControlDown(),
4994 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4999 // starting to drag-resize a col
5001 if ( CanDragColSize() )
5002 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5007 // ------------ Left double click
5009 if ( event
.LeftDClick() )
5011 int col
= XToEdgeOfCol(x
);
5016 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5018 // no default action at the moment
5023 // adjust column width depending on label text
5024 AutoSizeColLabelSize( col
);
5026 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5032 // ------------ Left button released
5034 else if ( event
.LeftUp() )
5036 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5038 DoEndDragResizeCol();
5040 // Note: we are ending the event *after* doing
5041 // default processing in this case
5043 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5046 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5051 // ------------ Right button down
5053 else if ( event
.RightDown() )
5057 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5059 // no default action at the moment
5064 // ------------ Right double click
5066 else if ( event
.RightDClick() )
5070 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5072 // no default action at the moment
5077 // ------------ No buttons down and mouse moving
5079 else if ( event
.Moving() )
5081 m_dragRowOrCol
= XToEdgeOfCol( x
);
5082 if ( m_dragRowOrCol
>= 0 )
5084 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5086 // don't capture the cursor yet
5087 if ( CanDragColSize() )
5088 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
5091 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5093 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
5099 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5101 if ( event
.LeftDown() )
5103 // indicate corner label by having both row and
5106 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5112 else if ( event
.LeftDClick() )
5114 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5117 else if ( event
.RightDown() )
5119 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5121 // no default action at the moment
5125 else if ( event
.RightDClick() )
5127 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5129 // no default action at the moment
5134 void wxGrid::ChangeCursorMode(CursorMode mode
,
5139 static const wxChar
*cursorModes
[] =
5148 wxLogTrace(_T("grid"),
5149 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5150 win
== m_colLabelWin
? _T("colLabelWin")
5151 : win
? _T("rowLabelWin")
5153 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5154 #endif // __WXDEBUG__
5156 if ( mode
== m_cursorMode
&&
5157 win
== m_winCapture
&&
5158 captureMouse
== (m_winCapture
!= NULL
))
5163 // by default use the grid itself
5169 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5170 m_winCapture
= (wxWindow
*)NULL
;
5173 m_cursorMode
= mode
;
5175 switch ( m_cursorMode
)
5177 case WXGRID_CURSOR_RESIZE_ROW
:
5178 win
->SetCursor( m_rowResizeCursor
);
5181 case WXGRID_CURSOR_RESIZE_COL
:
5182 win
->SetCursor( m_colResizeCursor
);
5186 win
->SetCursor( *wxSTANDARD_CURSOR
);
5189 // we need to capture mouse when resizing
5190 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5191 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5193 if ( captureMouse
&& resize
)
5195 win
->CaptureMouse();
5200 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5203 wxPoint
pos( event
.GetPosition() );
5204 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5206 wxGridCellCoords coords
;
5207 XYToCell( x
, y
, coords
);
5209 int cell_rows
, cell_cols
;
5210 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5211 if ((cell_rows
< 0) || (cell_cols
< 0))
5213 coords
.SetRow(coords
.GetRow() + cell_rows
);
5214 coords
.SetCol(coords
.GetCol() + cell_cols
);
5217 if ( event
.Dragging() )
5219 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5221 // Don't start doing anything until the mouse has been drug at
5222 // least 3 pixels in any direction...
5225 if (m_startDragPos
== wxDefaultPosition
)
5227 m_startDragPos
= pos
;
5230 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5234 m_isDragging
= TRUE
;
5235 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5237 // Hide the edit control, so it
5238 // won't interfer with drag-shrinking.
5239 if ( IsCellEditControlShown() )
5241 HideCellEditControl();
5242 SaveEditControlValue();
5245 // Have we captured the mouse yet?
5248 m_winCapture
= m_gridWin
;
5249 m_winCapture
->CaptureMouse();
5252 if ( coords
!= wxGridNoCellCoords
)
5254 if ( event
.ControlDown() )
5256 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5257 m_selectingKeyboard
= coords
;
5258 HighlightBlock ( m_selectingKeyboard
, coords
);
5262 if ( !IsSelection() )
5264 HighlightBlock( coords
, coords
);
5268 HighlightBlock( m_currentCellCoords
, coords
);
5272 if (! IsVisible(coords
))
5274 MakeCellVisible(coords
);
5275 // TODO: need to introduce a delay or something here. The
5276 // scrolling is way to fast, at least on MSW - also on GTK.
5280 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5282 int cw
, ch
, left
, dummy
;
5283 m_gridWin
->GetClientSize( &cw
, &ch
);
5284 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5286 wxClientDC
dc( m_gridWin
);
5288 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5289 GetRowMinimalHeight(m_dragRowOrCol
) );
5290 dc
.SetLogicalFunction(wxINVERT
);
5291 if ( m_dragLastPos
>= 0 )
5293 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5295 dc
.DrawLine( left
, y
, left
+cw
, y
);
5298 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5300 int cw
, ch
, dummy
, top
;
5301 m_gridWin
->GetClientSize( &cw
, &ch
);
5302 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5304 wxClientDC
dc( m_gridWin
);
5306 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5307 GetColMinimalWidth(m_dragRowOrCol
) );
5308 dc
.SetLogicalFunction(wxINVERT
);
5309 if ( m_dragLastPos
>= 0 )
5311 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5313 dc
.DrawLine( x
, top
, x
, top
+ch
);
5320 m_isDragging
= FALSE
;
5321 m_startDragPos
= wxDefaultPosition
;
5323 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5324 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5327 if ( event
.Entering() || event
.Leaving() )
5329 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5330 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5335 // ------------ Left button pressed
5337 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5339 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5344 if ( !event
.ControlDown() )
5346 if ( event
.ShiftDown() )
5350 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5351 m_currentCellCoords
.GetCol(),
5354 event
.ControlDown(),
5360 else if ( XToEdgeOfCol(x
) < 0 &&
5361 YToEdgeOfRow(y
) < 0 )
5363 DisableCellEditControl();
5364 MakeCellVisible( coords
);
5366 if ( event
.ControlDown() )
5370 m_selection
->ToggleCellSelection( coords
.GetRow(),
5372 event
.ControlDown(),
5377 m_selectingTopLeft
= wxGridNoCellCoords
;
5378 m_selectingBottomRight
= wxGridNoCellCoords
;
5379 m_selectingKeyboard
= coords
;
5383 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5384 SetCurrentCell( coords
);
5387 if ( m_selection
->GetSelectionMode() !=
5388 wxGrid::wxGridSelectCells
)
5390 HighlightBlock( coords
, coords
);
5399 // ------------ Left double click
5401 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5403 DisableCellEditControl();
5405 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5407 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5415 // ------------ Left button released
5417 else if ( event
.LeftUp() )
5419 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5423 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5424 m_winCapture
= NULL
;
5427 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5428 m_selectingBottomRight
!= wxGridNoCellCoords
)
5432 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5433 m_selectingTopLeft
.GetCol(),
5434 m_selectingBottomRight
.GetRow(),
5435 m_selectingBottomRight
.GetCol(),
5436 event
.ControlDown(),
5442 m_selectingTopLeft
= wxGridNoCellCoords
;
5443 m_selectingBottomRight
= wxGridNoCellCoords
;
5445 // Show the edit control, if it has been hidden for
5447 ShowCellEditControl();
5451 if( m_waitForSlowClick
&& CanEnableCellControl())
5453 EnableCellEditControl();
5455 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5456 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5457 editor
->StartingClick();
5461 m_waitForSlowClick
= FALSE
;
5465 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5467 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5468 DoEndDragResizeRow();
5470 // Note: we are ending the event *after* doing
5471 // default processing in this case
5473 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5475 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5477 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5478 DoEndDragResizeCol();
5480 // Note: we are ending the event *after* doing
5481 // default processing in this case
5483 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5490 // ------------ Right button down
5492 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5494 DisableCellEditControl();
5495 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5500 // no default action at the moment
5505 // ------------ Right double click
5507 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5509 DisableCellEditControl();
5510 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5515 // no default action at the moment
5519 // ------------ Moving and no button action
5521 else if ( event
.Moving() && !event
.IsButton() )
5523 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5525 // out of grid cell area
5526 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5530 int dragRow
= YToEdgeOfRow( y
);
5531 int dragCol
= XToEdgeOfCol( x
);
5533 // Dragging on the corner of a cell to resize in both
5534 // directions is not implemented yet...
5536 if ( dragRow
>= 0 && dragCol
>= 0 )
5538 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5544 m_dragRowOrCol
= dragRow
;
5546 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5548 if ( CanDragRowSize() && CanDragGridSize() )
5549 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5554 m_dragRowOrCol
= dragCol
;
5562 m_dragRowOrCol
= dragCol
;
5564 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5566 if ( CanDragColSize() && CanDragGridSize() )
5567 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5573 // Neither on a row or col edge
5575 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5577 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5583 void wxGrid::DoEndDragResizeRow()
5585 if ( m_dragLastPos
>= 0 )
5587 // erase the last line and resize the row
5589 int cw
, ch
, left
, dummy
;
5590 m_gridWin
->GetClientSize( &cw
, &ch
);
5591 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5593 wxClientDC
dc( m_gridWin
);
5595 dc
.SetLogicalFunction( wxINVERT
);
5596 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5597 HideCellEditControl();
5598 SaveEditControlValue();
5600 int rowTop
= GetRowTop(m_dragRowOrCol
);
5601 SetRowSize( m_dragRowOrCol
,
5602 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5604 if ( !GetBatchCount() )
5606 // Only needed to get the correct rect.y:
5607 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5609 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5610 rect
.width
= m_rowLabelWidth
;
5611 rect
.height
= ch
- rect
.y
;
5612 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5614 // if there is a multicell block, paint all of it
5617 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5618 int leftCol
= XToCol(left
);
5619 int rightCol
= internalXToCol(left
+cw
);
5622 for (i
=leftCol
; i
<rightCol
; i
++)
5624 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5625 if (cell_rows
< subtract_rows
)
5626 subtract_rows
= cell_rows
;
5628 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5629 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5630 rect
.height
= ch
- rect
.y
;
5633 m_gridWin
->Refresh( FALSE
, &rect
);
5636 ShowCellEditControl();
5641 void wxGrid::DoEndDragResizeCol()
5643 if ( m_dragLastPos
>= 0 )
5645 // erase the last line and resize the col
5647 int cw
, ch
, dummy
, top
;
5648 m_gridWin
->GetClientSize( &cw
, &ch
);
5649 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5651 wxClientDC
dc( m_gridWin
);
5653 dc
.SetLogicalFunction( wxINVERT
);
5654 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5655 HideCellEditControl();
5656 SaveEditControlValue();
5658 int colLeft
= GetColLeft(m_dragRowOrCol
);
5659 SetColSize( m_dragRowOrCol
,
5660 wxMax( m_dragLastPos
- colLeft
,
5661 GetColMinimalWidth(m_dragRowOrCol
) ) );
5663 if ( !GetBatchCount() )
5665 // Only needed to get the correct rect.x:
5666 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5668 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5669 rect
.width
= cw
- rect
.x
;
5670 rect
.height
= m_colLabelHeight
;
5671 m_colLabelWin
->Refresh( TRUE
, &rect
);
5673 // if there is a multicell block, paint all of it
5676 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5677 int topRow
= YToRow(top
);
5678 int bottomRow
= internalYToRow(top
+cw
);
5681 for (i
=topRow
; i
<bottomRow
; i
++)
5683 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5684 if (cell_cols
< subtract_cols
)
5685 subtract_cols
= cell_cols
;
5687 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5688 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5689 rect
.width
= cw
- rect
.x
;
5692 m_gridWin
->Refresh( FALSE
, &rect
);
5695 ShowCellEditControl();
5702 // ------ interaction with data model
5704 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5706 switch ( msg
.GetId() )
5708 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5709 return GetModelValues();
5711 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5712 return SetModelValues();
5714 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5715 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5716 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5717 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5718 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5719 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5720 return Redimension( msg
);
5729 // The behaviour of this function depends on the grid table class
5730 // Clear() function. For the default wxGridStringTable class the
5731 // behavious is to replace all cell contents with wxEmptyString but
5732 // not to change the number of rows or cols.
5734 void wxGrid::ClearGrid()
5738 if (IsCellEditControlEnabled())
5739 DisableCellEditControl();
5742 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5747 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5749 // TODO: something with updateLabels flag
5753 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5759 if (IsCellEditControlEnabled())
5760 DisableCellEditControl();
5762 return m_table
->InsertRows( pos
, numRows
);
5764 // the table will have sent the results of the insert row
5765 // operation to this view object as a grid table message
5771 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5773 // TODO: something with updateLabels flag
5777 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5781 return ( m_table
&& m_table
->AppendRows( numRows
) );
5782 // the table will have sent the results of the append row
5783 // operation to this view object as a grid table message
5787 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5789 // TODO: something with updateLabels flag
5793 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5799 if (IsCellEditControlEnabled())
5800 DisableCellEditControl();
5802 return (m_table
->DeleteRows( pos
, numRows
));
5803 // the table will have sent the results of the delete row
5804 // operation to this view object as a grid table message
5810 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5812 // TODO: something with updateLabels flag
5816 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5822 if (IsCellEditControlEnabled())
5823 DisableCellEditControl();
5825 return m_table
->InsertCols( pos
, numCols
);
5826 // the table will have sent the results of the insert col
5827 // operation to this view object as a grid table message
5833 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5835 // TODO: something with updateLabels flag
5839 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5843 return ( m_table
&& m_table
->AppendCols( numCols
) );
5844 // the table will have sent the results of the append col
5845 // operation to this view object as a grid table message
5849 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5851 // TODO: something with updateLabels flag
5855 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5861 if (IsCellEditControlEnabled())
5862 DisableCellEditControl();
5864 return ( m_table
->DeleteCols( pos
, numCols
) );
5865 // the table will have sent the results of the delete col
5866 // operation to this view object as a grid table message
5874 // ----- event handlers
5877 // Generate a grid event based on a mouse event and
5878 // return the result of ProcessEvent()
5880 int wxGrid::SendEvent( const wxEventType type
,
5882 wxMouseEvent
& mouseEv
)
5887 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5889 int rowOrCol
= (row
== -1 ? col
: row
);
5891 wxGridSizeEvent
gridEvt( GetId(),
5895 mouseEv
.GetX() + GetRowLabelSize(),
5896 mouseEv
.GetY() + GetColLabelSize(),
5897 mouseEv
.ControlDown(),
5898 mouseEv
.ShiftDown(),
5900 mouseEv
.MetaDown() );
5902 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5903 vetoed
= !gridEvt
.IsAllowed();
5905 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5907 // Right now, it should _never_ end up here!
5908 wxGridRangeSelectEvent
gridEvt( GetId(),
5912 m_selectingBottomRight
,
5914 mouseEv
.ControlDown(),
5915 mouseEv
.ShiftDown(),
5917 mouseEv
.MetaDown() );
5919 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5920 vetoed
= !gridEvt
.IsAllowed();
5924 wxGridEvent
gridEvt( GetId(),
5928 mouseEv
.GetX() + GetRowLabelSize(),
5929 mouseEv
.GetY() + GetColLabelSize(),
5931 mouseEv
.ControlDown(),
5932 mouseEv
.ShiftDown(),
5934 mouseEv
.MetaDown() );
5935 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5936 vetoed
= !gridEvt
.IsAllowed();
5939 // A Veto'd event may not be `claimed' so test this first
5940 if (vetoed
) return -1;
5941 return claimed
? 1 : 0;
5945 // Generate a grid event of specified type and return the result
5946 // of ProcessEvent().
5948 int wxGrid::SendEvent( const wxEventType type
,
5954 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5956 int rowOrCol
= (row
== -1 ? col
: row
);
5958 wxGridSizeEvent
gridEvt( GetId(),
5963 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5964 vetoed
= !gridEvt
.IsAllowed();
5968 wxGridEvent
gridEvt( GetId(),
5973 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5974 vetoed
= !gridEvt
.IsAllowed();
5977 // A Veto'd event may not be `claimed' so test this first
5978 if (vetoed
) return -1;
5979 return claimed
? 1 : 0;
5983 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5985 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5988 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
5990 // Don't do anything if between Begin/EndBatch...
5991 // EndBatch() will do all this on the last nested one anyway.
5992 if (! GetBatchCount())
5994 // Refresh to get correct scrolled position:
5995 wxScrolledWindow::Refresh(eraseb
,rect
);
5999 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6000 int width_label
, width_cell
, height_label
, height_cell
;
6003 //Copy rectangle can get scroll offsets..
6004 rect_x
= rect
->GetX();
6005 rect_y
= rect
->GetY();
6006 rectWidth
= rect
->GetWidth();
6007 rectHeight
= rect
->GetHeight();
6009 width_label
= m_rowLabelWidth
- rect_x
;
6010 if (width_label
> rectWidth
) width_label
= rectWidth
;
6012 height_label
= m_colLabelHeight
- rect_y
;
6013 if (height_label
> rectHeight
) height_label
= rectHeight
;
6015 if (rect_x
> m_rowLabelWidth
)
6017 x
= rect_x
- m_rowLabelWidth
;
6018 width_cell
= rectWidth
;
6023 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6026 if (rect_y
> m_colLabelHeight
)
6028 y
= rect_y
- m_colLabelHeight
;
6029 height_cell
= rectHeight
;
6034 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6037 // Paint corner label part intersecting rect.
6038 if ( width_label
> 0 && height_label
> 0 )
6040 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6041 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6044 // Paint col labels part intersecting rect.
6045 if ( width_cell
> 0 && height_label
> 0 )
6047 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6048 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6051 // Paint row labels part intersecting rect.
6052 if ( width_label
> 0 && height_cell
> 0 )
6054 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6055 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6058 // Paint cell area part intersecting rect.
6059 if ( width_cell
> 0 && height_cell
> 0 )
6061 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6062 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6067 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6068 m_colLabelWin
->Refresh(eraseb
, NULL
);
6069 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6070 m_gridWin
->Refresh(eraseb
, NULL
);
6075 void wxGrid::OnSize( wxSizeEvent
& event
)
6077 // position the child windows
6080 // don't call CalcDimensions() from here, the base class handles the size
6086 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6088 if ( m_inOnKeyDown
)
6090 // shouldn't be here - we are going round in circles...
6092 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6095 m_inOnKeyDown
= TRUE
;
6097 // propagate the event up and see if it gets processed
6099 wxWindow
*parent
= GetParent();
6100 wxKeyEvent
keyEvt( event
);
6101 keyEvt
.SetEventObject( parent
);
6103 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6106 // try local handlers
6108 switch ( event
.GetKeyCode() )
6111 if ( event
.ControlDown() )
6113 MoveCursorUpBlock( event
.ShiftDown() );
6117 MoveCursorUp( event
.ShiftDown() );
6122 if ( event
.ControlDown() )
6124 MoveCursorDownBlock( event
.ShiftDown() );
6128 MoveCursorDown( event
.ShiftDown() );
6133 if ( event
.ControlDown() )
6135 MoveCursorLeftBlock( event
.ShiftDown() );
6139 MoveCursorLeft( event
.ShiftDown() );
6144 if ( event
.ControlDown() )
6146 MoveCursorRightBlock( event
.ShiftDown() );
6150 MoveCursorRight( event
.ShiftDown() );
6155 case WXK_NUMPAD_ENTER
:
6156 if ( event
.ControlDown() )
6158 event
.Skip(); // to let the edit control have the return
6162 if ( GetGridCursorRow() < GetNumberRows()-1 )
6164 MoveCursorDown( event
.ShiftDown() );
6168 // at the bottom of a column
6169 HideCellEditControl();
6170 SaveEditControlValue();
6180 if (event
.ShiftDown())
6182 if ( GetGridCursorCol() > 0 )
6184 MoveCursorLeft( FALSE
);
6189 HideCellEditControl();
6190 SaveEditControlValue();
6195 if ( GetGridCursorCol() < GetNumberCols()-1 )
6197 MoveCursorRight( FALSE
);
6202 HideCellEditControl();
6203 SaveEditControlValue();
6209 if ( event
.ControlDown() )
6211 MakeCellVisible( 0, 0 );
6212 SetCurrentCell( 0, 0 );
6221 if ( event
.ControlDown() )
6223 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6224 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6241 if ( event
.ControlDown() )
6245 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6246 m_currentCellCoords
.GetCol(),
6247 event
.ControlDown(),
6254 if ( !IsEditable() )
6256 MoveCursorRight( FALSE
);
6259 // Otherwise fall through to default
6262 // is it possible to edit the current cell at all?
6263 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6265 // yes, now check whether the cells editor accepts the key
6266 int row
= m_currentCellCoords
.GetRow();
6267 int col
= m_currentCellCoords
.GetCol();
6268 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6269 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6271 // <F2> is special and will always start editing, for
6272 // other keys - ask the editor itself
6273 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6274 || editor
->IsAcceptedKey(event
) )
6276 // ensure cell is visble
6277 MakeCellVisible(row
, col
);
6278 EnableCellEditControl();
6280 // a problem can arise if the cell is not completely
6281 // visible (even after calling MakeCellVisible the
6282 // control is not created and calling StartingKey will
6284 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6296 // let others process char events with modifiers or all
6297 // char events for readonly cells
6304 m_inOnKeyDown
= FALSE
;
6307 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6309 // try local handlers
6311 if ( event
.GetKeyCode() == WXK_SHIFT
)
6313 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6314 m_selectingBottomRight
!= wxGridNoCellCoords
)
6318 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6319 m_selectingTopLeft
.GetCol(),
6320 m_selectingBottomRight
.GetRow(),
6321 m_selectingBottomRight
.GetCol(),
6322 event
.ControlDown(),
6329 m_selectingTopLeft
= wxGridNoCellCoords
;
6330 m_selectingBottomRight
= wxGridNoCellCoords
;
6331 m_selectingKeyboard
= wxGridNoCellCoords
;
6335 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6339 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6341 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6343 // the event has been intercepted - do nothing
6347 wxClientDC
dc(m_gridWin
);
6350 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6352 HideCellEditControl();
6353 DisableCellEditControl();
6355 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6358 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6359 if ( !m_gridLinesEnabled
)
6367 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6369 // Otherwise refresh redraws the highlight!
6370 m_currentCellCoords
= coords
;
6372 DrawGridCellArea(dc
,cells
);
6373 DrawAllGridLines( dc
, r
);
6377 m_currentCellCoords
= coords
;
6379 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6380 DrawCellHighlight(dc
, attr
);
6385 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6388 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6392 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6395 rightCol
= GetNumberCols() - 1;
6397 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6400 bottomRow
= GetNumberRows() - 1;
6404 if ( topRow
> bottomRow
)
6411 if ( leftCol
> rightCol
)
6418 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6419 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6421 // First the case that we selected a completely new area
6422 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6423 m_selectingBottomRight
== wxGridNoCellCoords
)
6426 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6427 wxGridCellCoords ( bottomRow
, rightCol
) );
6428 m_gridWin
->Refresh( FALSE
, &rect
);
6430 // Now handle changing an existing selection area.
6431 else if ( m_selectingTopLeft
!= updateTopLeft
||
6432 m_selectingBottomRight
!= updateBottomRight
)
6434 // Compute two optimal update rectangles:
6435 // Either one rectangle is a real subset of the
6436 // other, or they are (almost) disjoint!
6438 bool need_refresh
[4];
6442 need_refresh
[3] = FALSE
;
6445 // Store intermediate values
6446 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6447 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6448 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6449 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6451 // Determine the outer/inner coordinates.
6452 if (oldLeft
> leftCol
)
6458 if (oldTop
> topRow
)
6464 if (oldRight
< rightCol
)
6467 oldRight
= rightCol
;
6470 if (oldBottom
< bottomRow
)
6473 oldBottom
= bottomRow
;
6477 // Now, either the stuff marked old is the outer
6478 // rectangle or we don't have a situation where one
6479 // is contained in the other.
6481 if ( oldLeft
< leftCol
)
6483 // Refresh the newly selected or deselected
6484 // area to the left of the old or new selection.
6485 need_refresh
[0] = TRUE
;
6486 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6488 wxGridCellCoords ( oldBottom
,
6492 if ( oldTop
< topRow
)
6494 // Refresh the newly selected or deselected
6495 // area above the old or new selection.
6496 need_refresh
[1] = TRUE
;
6497 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6499 wxGridCellCoords ( topRow
- 1,
6503 if ( oldRight
> rightCol
)
6505 // Refresh the newly selected or deselected
6506 // area to the right of the old or new selection.
6507 need_refresh
[2] = TRUE
;
6508 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6510 wxGridCellCoords ( oldBottom
,
6514 if ( oldBottom
> bottomRow
)
6516 // Refresh the newly selected or deselected
6517 // area below the old or new selection.
6518 need_refresh
[3] = TRUE
;
6519 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6521 wxGridCellCoords ( oldBottom
,
6525 // various Refresh() calls
6526 for (i
= 0; i
< 4; i
++ )
6527 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6528 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6531 m_selectingTopLeft
= updateTopLeft
;
6532 m_selectingBottomRight
= updateBottomRight
;
6536 // ------ functions to get/send data (see also public functions)
6539 bool wxGrid::GetModelValues()
6541 // Hide the editor, so it won't hide a changed value.
6542 HideCellEditControl();
6546 // all we need to do is repaint the grid
6548 m_gridWin
->Refresh();
6556 bool wxGrid::SetModelValues()
6560 // Disable the editor, so it won't hide a changed value.
6561 // Do we also want to save the current value of the editor first?
6563 DisableCellEditControl();
6567 for ( row
= 0; row
< m_numRows
; row
++ )
6569 for ( col
= 0; col
< m_numCols
; col
++ )
6571 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6583 // Note - this function only draws cells that are in the list of
6584 // exposed cells (usually set from the update region by
6585 // CalcExposedCells)
6587 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6589 if ( !m_numRows
|| !m_numCols
) return;
6591 int i
, numCells
= cells
.GetCount();
6592 int row
, col
, cell_rows
, cell_cols
;
6593 wxGridCellCoordsArray redrawCells
;
6595 for ( i
= numCells
-1; i
>= 0; i
-- )
6597 row
= cells
[i
].GetRow();
6598 col
= cells
[i
].GetCol();
6599 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6601 // If this cell is part of a multicell block, find owner for repaint
6602 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6604 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6605 bool marked
= FALSE
;
6606 for ( int j
= 0; j
< numCells
; j
++ )
6608 if ( cell
== cells
[j
] )
6616 int count
= redrawCells
.GetCount();
6617 for (int j
= 0; j
< count
; j
++)
6619 if ( cell
== redrawCells
[j
] )
6625 if (!marked
) redrawCells
.Add( cell
);
6627 continue; // don't bother drawing this cell
6630 // If this cell is empty, find cell to left that might want to overflow
6631 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6633 for ( int l
= 0; l
< cell_rows
; l
++ )
6635 // find a cell in this row to left alreay marked for repaint
6637 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6638 if ((redrawCells
[k
].GetCol() < left
) &&
6639 (redrawCells
[k
].GetRow() == row
))
6640 left
=redrawCells
[k
].GetCol();
6642 if (left
== col
) left
= 0; // oh well
6644 for (int j
= col
-1; j
>= left
; j
--)
6646 if (!m_table
->IsEmptyCell(row
+l
, j
))
6648 if (GetCellOverflow(row
+l
, j
))
6650 wxGridCellCoords
cell(row
+l
, j
);
6651 bool marked
= FALSE
;
6653 for (int k
= 0; k
< numCells
; k
++)
6655 if ( cell
== cells
[k
] )
6663 int count
= redrawCells
.GetCount();
6664 for (int k
= 0; k
< count
; k
++)
6666 if ( cell
== redrawCells
[k
] )
6672 if (!marked
) redrawCells
.Add( cell
);
6680 DrawCell( dc
, cells
[i
] );
6683 numCells
= redrawCells
.GetCount();
6685 for ( i
= numCells
- 1; i
>= 0; i
-- )
6687 DrawCell( dc
, redrawCells
[i
] );
6692 void wxGrid::DrawGridSpace( wxDC
& dc
)
6695 m_gridWin
->GetClientSize( &cw
, &ch
);
6698 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6700 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6701 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6703 if ( right
> rightCol
|| bottom
> bottomRow
)
6706 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6708 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6709 dc
.SetPen( *wxTRANSPARENT_PEN
);
6711 if ( right
> rightCol
)
6713 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6716 if ( bottom
> bottomRow
)
6718 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6724 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6726 int row
= coords
.GetRow();
6727 int col
= coords
.GetCol();
6729 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6732 // we draw the cell border ourselves
6733 #if !WXGRID_DRAW_LINES
6734 if ( m_gridLinesEnabled
)
6735 DrawCellBorder( dc
, coords
);
6738 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6740 bool isCurrent
= coords
== m_currentCellCoords
;
6742 wxRect rect
= CellToRect( row
, col
);
6744 // if the editor is shown, we should use it and not the renderer
6745 // Note: However, only if it is really _shown_, i.e. not hidden!
6746 if ( isCurrent
&& IsCellEditControlShown() )
6748 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6749 editor
->PaintBackground(rect
, attr
);
6754 // but all the rest is drawn by the cell renderer and hence may be
6756 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6757 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6764 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6766 int row
= m_currentCellCoords
.GetRow();
6767 int col
= m_currentCellCoords
.GetCol();
6769 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6772 wxRect rect
= CellToRect(row
, col
);
6774 // hmmm... what could we do here to show that the cell is disabled?
6775 // for now, I just draw a thinner border than for the other ones, but
6776 // it doesn't look really good
6778 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6782 // The center of th drawn line is where the position/width/height of
6783 // the rectangle is actually at, (on wxMSW atr least,) so we will
6784 // reduce the size of the rectangle to compensate for the thickness of
6785 // the line. If this is too strange on non wxMSW platforms then
6786 // please #ifdef this appropriately.
6787 rect
.x
+= penWidth
/2;
6788 rect
.y
+= penWidth
/2;
6789 rect
.width
-= penWidth
-1;
6790 rect
.height
-= penWidth
-1;
6793 // Now draw the rectangle
6794 // use the cellHighlightColour if the cell is inside a selection, this
6795 // will ensure the cell is always visible.
6796 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6797 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6798 dc
.DrawRectangle(rect
);
6802 // VZ: my experiments with 3d borders...
6804 // how to properly set colours for arbitrary bg?
6805 wxCoord x1
= rect
.x
,
6807 x2
= rect
.x
+ rect
.width
-1,
6808 y2
= rect
.y
+ rect
.height
-1;
6810 dc
.SetPen(*wxWHITE_PEN
);
6811 dc
.DrawLine(x1
, y1
, x2
, y1
);
6812 dc
.DrawLine(x1
, y1
, x1
, y2
);
6814 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6815 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6817 dc
.SetPen(*wxBLACK_PEN
);
6818 dc
.DrawLine(x1
, y2
, x2
, y2
);
6819 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6824 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6826 int row
= coords
.GetRow();
6827 int col
= coords
.GetCol();
6828 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6831 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6833 wxRect rect
= CellToRect( row
, col
);
6835 // right hand border
6837 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6838 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6842 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6843 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6846 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6848 // This if block was previously in wxGrid::OnPaint but that doesn't
6849 // seem to get called under wxGTK - MB
6851 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6852 m_numRows
&& m_numCols
)
6854 m_currentCellCoords
.Set(0, 0);
6857 if ( IsCellEditControlShown() )
6859 // don't show highlight when the edit control is shown
6863 // if the active cell was repainted, repaint its highlight too because it
6864 // might have been damaged by the grid lines
6865 size_t count
= cells
.GetCount();
6866 for ( size_t n
= 0; n
< count
; n
++ )
6868 if ( cells
[n
] == m_currentCellCoords
)
6870 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6871 DrawCellHighlight(dc
, attr
);
6879 // TODO: remove this ???
6880 // This is used to redraw all grid lines e.g. when the grid line colour
6883 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6885 #if !WXGRID_DRAW_LINES
6889 if ( !m_gridLinesEnabled
||
6891 !m_numCols
) return;
6893 int top
, bottom
, left
, right
;
6895 #if 0 //#ifndef __WXGTK__
6899 m_gridWin
->GetClientSize(&cw
, &ch
);
6901 // virtual coords of visible area
6903 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6904 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6909 reg
.GetBox(x
, y
, w
, h
);
6910 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6911 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6915 m_gridWin
->GetClientSize(&cw
, &ch
);
6916 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6917 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6920 // avoid drawing grid lines past the last row and col
6922 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6923 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6925 // no gridlines inside multicells, clip them out
6926 int leftCol
= internalXToCol(left
);
6927 int topRow
= internalYToRow(top
);
6928 int rightCol
= internalXToCol(right
);
6929 int bottomRow
= internalYToRow(bottom
);
6930 wxRegion
clippedcells(0, 0, cw
, ch
);
6933 int i
, j
, cell_rows
, cell_cols
;
6936 for (j
=topRow
; j
<bottomRow
; j
++)
6938 for (i
=leftCol
; i
<rightCol
; i
++)
6940 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
6941 if ((cell_rows
> 1) || (cell_cols
> 1))
6943 rect
= CellToRect(j
,i
);
6944 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6945 clippedcells
.Subtract(rect
);
6947 else if ((cell_rows
< 0) || (cell_cols
< 0))
6949 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
6950 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6951 clippedcells
.Subtract(rect
);
6955 dc
.SetClippingRegion( clippedcells
);
6957 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6959 // horizontal grid lines
6961 // already declared above - int i;
6962 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6964 int bot
= GetRowBottom(i
) - 1;
6973 dc
.DrawLine( left
, bot
, right
, bot
);
6978 // vertical grid lines
6980 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6982 int colRight
= GetColRight(i
) - 1;
6983 if ( colRight
> right
)
6988 if ( colRight
>= left
)
6990 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6993 dc
.DestroyClippingRegion();
6997 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6999 if ( !m_numRows
) return;
7002 size_t numLabels
= rows
.GetCount();
7004 for ( i
= 0; i
< numLabels
; i
++ )
7006 DrawRowLabel( dc
, rows
[i
] );
7011 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7013 if ( GetRowHeight(row
) <= 0 )
7016 int rowTop
= GetRowTop(row
),
7017 rowBottom
= GetRowBottom(row
) - 1;
7019 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7020 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7021 m_rowLabelWidth
-1, rowBottom
);
7023 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7025 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7027 dc
.SetPen( *wxWHITE_PEN
);
7028 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7029 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7031 dc
.SetBackgroundMode( wxTRANSPARENT
);
7032 dc
.SetTextForeground( GetLabelTextColour() );
7033 dc
.SetFont( GetLabelFont() );
7036 GetRowLabelAlignment( &hAlign
, &vAlign
);
7040 rect
.SetY( GetRowTop(row
) + 2 );
7041 rect
.SetWidth( m_rowLabelWidth
- 4 );
7042 rect
.SetHeight( GetRowHeight(row
) - 4 );
7043 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7047 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7049 if ( !m_numCols
) return;
7052 size_t numLabels
= cols
.GetCount();
7054 for ( i
= 0; i
< numLabels
; i
++ )
7056 DrawColLabel( dc
, cols
[i
] );
7061 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7063 if ( GetColWidth(col
) <= 0 )
7066 int colLeft
= GetColLeft(col
),
7067 colRight
= GetColRight(col
) - 1;
7069 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7070 dc
.DrawLine( colRight
, 0,
7071 colRight
, m_colLabelHeight
-1 );
7073 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7075 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7076 colRight
+1, m_colLabelHeight
-1 );
7078 dc
.SetPen( *wxWHITE_PEN
);
7079 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7080 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7082 dc
.SetBackgroundMode( wxTRANSPARENT
);
7083 dc
.SetTextForeground( GetLabelTextColour() );
7084 dc
.SetFont( GetLabelFont() );
7086 int hAlign
, vAlign
, orient
;
7087 GetColLabelAlignment( &hAlign
, &vAlign
);
7088 orient
= GetColLabelTextOrientation();
7091 rect
.SetX( colLeft
+ 2 );
7093 rect
.SetWidth( GetColWidth(col
) - 4 );
7094 rect
.SetHeight( m_colLabelHeight
- 4 );
7095 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7098 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7099 const wxString
& value
,
7103 int textOrientation
)
7105 wxArrayString lines
;
7107 StringToLines( value
, lines
);
7110 //Forward to new API.
7111 DrawTextRectangle( dc
,
7120 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7121 const wxArrayString
& lines
,
7125 int textOrientation
)
7127 long textWidth
, textHeight
;
7128 long lineWidth
, lineHeight
;
7131 dc
.SetClippingRegion( rect
);
7133 nLines
= lines
.GetCount();
7137 float x
= 0.0, y
= 0.0;
7139 if( textOrientation
== wxHORIZONTAL
)
7140 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7142 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7146 case wxALIGN_BOTTOM
:
7147 if( textOrientation
== wxHORIZONTAL
)
7148 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7150 x
= rect
.x
+ rect
.width
- textWidth
;
7153 case wxALIGN_CENTRE
:
7154 if( textOrientation
== wxHORIZONTAL
)
7155 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7157 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7162 if( textOrientation
== wxHORIZONTAL
)
7169 // Align each line of a multi-line label
7170 for( l
= 0; l
< nLines
; l
++ )
7172 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7174 switch( horizAlign
)
7177 if( textOrientation
== wxHORIZONTAL
)
7178 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7180 y
= rect
.y
+ lineWidth
+ 1;
7183 case wxALIGN_CENTRE
:
7184 if( textOrientation
== wxHORIZONTAL
)
7185 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7187 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7192 if( textOrientation
== wxHORIZONTAL
)
7195 y
= rect
.y
+ rect
.height
- 1;
7199 if( textOrientation
== wxHORIZONTAL
)
7201 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7206 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7211 dc
.DestroyClippingRegion();
7215 // Split multi line text up into an array of strings. Any existing
7216 // contents of the string array are preserved.
7218 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7222 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7223 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7225 while ( startPos
< (int)tVal
.Length() )
7227 pos
= tVal
.Mid(startPos
).Find( eol
);
7232 else if ( pos
== 0 )
7234 lines
.Add( wxEmptyString
);
7238 lines
.Add( value
.Mid(startPos
, pos
) );
7242 if ( startPos
< (int)value
.Length() )
7244 lines
.Add( value
.Mid( startPos
) );
7249 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7250 const wxArrayString
& lines
,
7251 long *width
, long *height
)
7258 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7260 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7261 w
= wxMax( w
, lineW
);
7270 // ------ Batch processing.
7272 void wxGrid::EndBatch()
7274 if ( m_batchCount
> 0 )
7277 if ( !m_batchCount
)
7280 m_rowLabelWin
->Refresh();
7281 m_colLabelWin
->Refresh();
7282 m_cornerLabelWin
->Refresh();
7283 m_gridWin
->Refresh();
7288 // Use this, rather than wxWindow::Refresh(), to force an immediate
7289 // repainting of the grid. Has no effect if you are already inside a
7290 // BeginBatch / EndBatch block.
7292 void wxGrid::ForceRefresh()
7300 // ------ Edit control functions
7304 void wxGrid::EnableEditing( bool edit
)
7306 // TODO: improve this ?
7308 if ( edit
!= m_editable
)
7310 if(!edit
) EnableCellEditControl(edit
);
7316 void wxGrid::EnableCellEditControl( bool enable
)
7321 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7322 SetCurrentCell( 0, 0 );
7324 if ( enable
!= m_cellEditCtrlEnabled
)
7328 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7331 // this should be checked by the caller!
7332 wxASSERT_MSG( CanEnableCellControl(),
7333 _T("can't enable editing for this cell!") );
7335 // do it before ShowCellEditControl()
7336 m_cellEditCtrlEnabled
= enable
;
7338 ShowCellEditControl();
7342 //FIXME:add veto support
7343 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7345 HideCellEditControl();
7346 SaveEditControlValue();
7348 // do it after HideCellEditControl()
7349 m_cellEditCtrlEnabled
= enable
;
7354 bool wxGrid::IsCurrentCellReadOnly() const
7357 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7358 bool readonly
= attr
->IsReadOnly();
7364 bool wxGrid::CanEnableCellControl() const
7366 return m_editable
&& !IsCurrentCellReadOnly();
7369 bool wxGrid::IsCellEditControlEnabled() const
7371 // the cell edit control might be disable for all cells or just for the
7372 // current one if it's read only
7373 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7376 bool wxGrid::IsCellEditControlShown() const
7378 bool isShown
= FALSE
;
7380 if ( m_cellEditCtrlEnabled
)
7382 int row
= m_currentCellCoords
.GetRow();
7383 int col
= m_currentCellCoords
.GetCol();
7384 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7385 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7390 if ( editor
->IsCreated() )
7392 isShown
= editor
->GetControl()->IsShown();
7402 void wxGrid::ShowCellEditControl()
7404 if ( IsCellEditControlEnabled() )
7406 if ( !IsVisible( m_currentCellCoords
) )
7408 m_cellEditCtrlEnabled
= FALSE
;
7413 wxRect rect
= CellToRect( m_currentCellCoords
);
7414 int row
= m_currentCellCoords
.GetRow();
7415 int col
= m_currentCellCoords
.GetCol();
7417 // if this is part of a multicell, find owner (topleft)
7418 int cell_rows
, cell_cols
;
7419 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7420 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7424 m_currentCellCoords
.SetRow( row
);
7425 m_currentCellCoords
.SetCol( col
);
7428 // convert to scrolled coords
7430 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7432 // done in PaintBackground()
7434 // erase the highlight and the cell contents because the editor
7435 // might not cover the entire cell
7436 wxClientDC
dc( m_gridWin
);
7438 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7439 dc
.SetPen(*wxTRANSPARENT_PEN
);
7440 dc
.DrawRectangle(rect
);
7443 // cell is shifted by one pixel
7444 // However, don't allow x or y to become negative
7445 // since the SetSize() method interprets that as
7452 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7453 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7454 if ( !editor
->IsCreated() )
7456 editor
->Create(m_gridWin
, -1,
7457 new wxGridCellEditorEvtHandler(this, editor
));
7459 wxGridEditorCreatedEvent
evt(GetId(),
7460 wxEVT_GRID_EDITOR_CREATED
,
7464 editor
->GetControl());
7465 GetEventHandler()->ProcessEvent(evt
);
7469 // resize editor to overflow into righthand cells if allowed
7470 int maxWidth
= rect
.width
;
7471 wxString value
= GetCellValue(row
, col
);
7472 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7475 GetTextExtent(value
, &maxWidth
, &y
,
7476 NULL
, NULL
, &attr
->GetFont());
7477 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7479 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7480 if (rect
.x
+maxWidth
> client_right
)
7481 maxWidth
= client_right
- rect
.x
;
7483 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7485 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7486 // may have changed earlier
7487 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7490 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7491 // looks weird going over a multicell
7492 if (m_table
->IsEmptyCell(row
,i
) &&
7493 (rect
.width
< maxWidth
) && (c_rows
== 1))
7494 rect
.width
+= GetColWidth(i
);
7498 if (rect
.GetRight() > client_right
)
7499 rect
.SetRight(client_right
-1);
7502 editor
->SetCellAttr(attr
);
7503 editor
->SetSize( rect
);
7504 editor
->Show( TRUE
, attr
);
7506 // recalc dimensions in case we need to
7507 // expand the scrolled window to account for editor
7510 editor
->BeginEdit(row
, col
, this);
7511 editor
->SetCellAttr(NULL
);
7520 void wxGrid::HideCellEditControl()
7522 if ( IsCellEditControlEnabled() )
7524 int row
= m_currentCellCoords
.GetRow();
7525 int col
= m_currentCellCoords
.GetCol();
7527 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7528 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7529 editor
->Show( FALSE
);
7532 m_gridWin
->SetFocus();
7533 // refresh whole row to the right
7534 wxRect
rect( CellToRect(row
, col
) );
7535 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7536 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7537 m_gridWin
->Refresh( FALSE
, &rect
);
7542 void wxGrid::SaveEditControlValue()
7544 if ( IsCellEditControlEnabled() )
7546 int row
= m_currentCellCoords
.GetRow();
7547 int col
= m_currentCellCoords
.GetCol();
7549 wxString oldval
= GetCellValue(row
,col
);
7551 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7552 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7553 bool changed
= editor
->EndEdit(row
, col
, this);
7560 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7561 m_currentCellCoords
.GetRow(),
7562 m_currentCellCoords
.GetCol() ) < 0 ) {
7564 // Event has been vetoed, set the data back.
7565 SetCellValue(row
,col
,oldval
);
7573 // ------ Grid location functions
7574 // Note that all of these functions work with the logical coordinates of
7575 // grid cells and labels so you will need to convert from device
7576 // coordinates for mouse events etc.
7579 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7581 int row
= YToRow(y
);
7582 int col
= XToCol(x
);
7584 if ( row
== -1 || col
== -1 )
7586 coords
= wxGridNoCellCoords
;
7590 coords
.Set( row
, col
);
7595 // Internal Helper function for computing row or column from some
7596 // (unscrolled) coordinate value, using either
7597 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7598 // of m_rowBottoms/m_ColRights to speed up the search!
7600 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7601 const wxArrayInt
& BorderArray
, int nMax
,
7606 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7612 size_t i_max
= coord
/ defaultDist
,
7615 if (BorderArray
.IsEmpty())
7617 if((int) i_max
< nMax
)
7619 return clipToMinMax
? nMax
- 1 : -1;
7622 if ( i_max
>= BorderArray
.GetCount())
7623 i_max
= BorderArray
.GetCount() - 1;
7626 if ( coord
>= BorderArray
[i_max
])
7629 i_max
= coord
/ minDist
;
7631 if ( i_max
>= BorderArray
.GetCount())
7632 i_max
= BorderArray
.GetCount() - 1;
7634 if ( coord
>= BorderArray
[i_max
])
7635 return clipToMinMax
? (int)i_max
: -1;
7636 if ( coord
< BorderArray
[0] )
7639 while ( i_max
- i_min
> 0 )
7641 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7642 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7643 if (coord
>= BorderArray
[ i_max
- 1])
7647 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7648 if (coord
< BorderArray
[median
])
7656 int wxGrid::YToRow( int y
)
7658 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7659 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, m_numRows
, FALSE
);
7663 int wxGrid::XToCol( int x
)
7665 return CoordToRowOrCol(x
, m_defaultColWidth
,
7666 WXGRID_MIN_COL_WIDTH
, m_colRights
, m_numCols
, FALSE
);
7670 // return the row number that that the y coord is near the edge of, or
7671 // -1 if not near an edge
7673 int wxGrid::YToEdgeOfRow( int y
)
7676 i
= internalYToRow(y
);
7678 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7680 // We know that we are in row i, test whether we are
7681 // close enough to lower or upper border, respectively.
7682 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7684 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7692 // return the col number that that the x coord is near the edge of, or
7693 // -1 if not near an edge
7695 int wxGrid::XToEdgeOfCol( int x
)
7698 i
= internalXToCol(x
);
7700 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7702 // We know that we are in column i, test whether we are
7703 // close enough to right or left border, respectively.
7704 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7706 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7714 wxRect
wxGrid::CellToRect( int row
, int col
)
7716 wxRect
rect( -1, -1, -1, -1 );
7718 if ( row
>= 0 && row
< m_numRows
&&
7719 col
>= 0 && col
< m_numCols
)
7721 int i
, cell_rows
, cell_cols
;
7722 rect
.width
= rect
.height
= 0;
7723 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7724 // if negative then find multicell owner
7725 if (cell_rows
< 0) row
+= cell_rows
;
7726 if (cell_cols
< 0) col
+= cell_cols
;
7727 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7729 rect
.x
= GetColLeft(col
);
7730 rect
.y
= GetRowTop(row
);
7731 for (i
=col
; i
<col
+cell_cols
; i
++)
7732 rect
.width
+= GetColWidth(i
);
7733 for (i
=row
; i
<row
+cell_rows
; i
++)
7734 rect
.height
+= GetRowHeight(i
);
7737 // if grid lines are enabled, then the area of the cell is a bit smaller
7738 if (m_gridLinesEnabled
) {
7746 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7748 // get the cell rectangle in logical coords
7750 wxRect
r( CellToRect( row
, col
) );
7752 // convert to device coords
7754 int left
, top
, right
, bottom
;
7755 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7756 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7758 // check against the client area of the grid window
7761 m_gridWin
->GetClientSize( &cw
, &ch
);
7763 if ( wholeCellVisible
)
7765 // is the cell wholly visible ?
7767 return ( left
>= 0 && right
<= cw
&&
7768 top
>= 0 && bottom
<= ch
);
7772 // is the cell partly visible ?
7774 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7775 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7780 // make the specified cell location visible by doing a minimal amount
7783 void wxGrid::MakeCellVisible( int row
, int col
)
7787 int xpos
= -1, ypos
= -1;
7789 if ( row
>= 0 && row
< m_numRows
&&
7790 col
>= 0 && col
< m_numCols
)
7792 // get the cell rectangle in logical coords
7794 wxRect
r( CellToRect( row
, col
) );
7796 // convert to device coords
7798 int left
, top
, right
, bottom
;
7799 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7800 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7803 m_gridWin
->GetClientSize( &cw
, &ch
);
7809 else if ( bottom
> ch
)
7811 int h
= r
.GetHeight();
7813 for ( i
= row
-1; i
>= 0; i
-- )
7815 int rowHeight
= GetRowHeight(i
);
7816 if ( h
+ rowHeight
> ch
)
7823 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7824 // have rounding errors (this is important, because if we do, we
7825 // might not scroll at all and some cells won't be redrawn)
7827 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7829 ypos
+= GRID_SCROLL_LINE_Y
;
7836 else if ( right
> cw
)
7838 // position the view so that the cell is on the right
7840 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
7841 xpos
= x0
+ (right
- cw
);
7843 // see comment for ypos above
7844 xpos
+= GRID_SCROLL_LINE_X
;
7847 if ( xpos
!= -1 || ypos
!= -1 )
7850 xpos
/= GRID_SCROLL_LINE_X
;
7852 ypos
/= GRID_SCROLL_LINE_Y
;
7853 Scroll( xpos
, ypos
);
7861 // ------ Grid cursor movement functions
7864 bool wxGrid::MoveCursorUp( bool expandSelection
)
7866 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7867 m_currentCellCoords
.GetRow() >= 0 )
7869 if ( expandSelection
)
7871 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7872 m_selectingKeyboard
= m_currentCellCoords
;
7873 if ( m_selectingKeyboard
.GetRow() > 0 )
7875 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7876 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7877 m_selectingKeyboard
.GetCol() );
7878 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7881 else if ( m_currentCellCoords
.GetRow() > 0 )
7884 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7885 m_currentCellCoords
.GetCol() );
7886 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7887 m_currentCellCoords
.GetCol() );
7898 bool wxGrid::MoveCursorDown( bool expandSelection
)
7900 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7901 m_currentCellCoords
.GetRow() < m_numRows
)
7903 if ( expandSelection
)
7905 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7906 m_selectingKeyboard
= m_currentCellCoords
;
7907 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7909 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7910 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7911 m_selectingKeyboard
.GetCol() );
7912 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7915 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7918 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7919 m_currentCellCoords
.GetCol() );
7920 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7921 m_currentCellCoords
.GetCol() );
7932 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7934 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7935 m_currentCellCoords
.GetCol() >= 0 )
7937 if ( expandSelection
)
7939 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7940 m_selectingKeyboard
= m_currentCellCoords
;
7941 if ( m_selectingKeyboard
.GetCol() > 0 )
7943 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7944 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7945 m_selectingKeyboard
.GetCol() );
7946 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7949 else if ( m_currentCellCoords
.GetCol() > 0 )
7952 MakeCellVisible( m_currentCellCoords
.GetRow(),
7953 m_currentCellCoords
.GetCol() - 1 );
7954 SetCurrentCell( m_currentCellCoords
.GetRow(),
7955 m_currentCellCoords
.GetCol() - 1 );
7966 bool wxGrid::MoveCursorRight( bool expandSelection
)
7968 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7969 m_currentCellCoords
.GetCol() < m_numCols
)
7971 if ( expandSelection
)
7973 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7974 m_selectingKeyboard
= m_currentCellCoords
;
7975 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7977 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7978 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7979 m_selectingKeyboard
.GetCol() );
7980 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7983 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7986 MakeCellVisible( m_currentCellCoords
.GetRow(),
7987 m_currentCellCoords
.GetCol() + 1 );
7988 SetCurrentCell( m_currentCellCoords
.GetRow(),
7989 m_currentCellCoords
.GetCol() + 1 );
8000 bool wxGrid::MovePageUp()
8002 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8004 int row
= m_currentCellCoords
.GetRow();
8008 m_gridWin
->GetClientSize( &cw
, &ch
);
8010 int y
= GetRowTop(row
);
8011 int newRow
= internalYToRow( y
- ch
+ 1 );
8013 if ( newRow
== row
)
8015 //row > 0 , so newrow can never be less than 0 here.
8019 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8020 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8028 bool wxGrid::MovePageDown()
8030 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8032 int row
= m_currentCellCoords
.GetRow();
8033 if ( row
< m_numRows
)
8036 m_gridWin
->GetClientSize( &cw
, &ch
);
8038 int y
= GetRowTop(row
);
8039 int newRow
= internalYToRow( y
+ ch
);
8040 if ( newRow
== row
)
8042 // row < m_numRows , so newrow can't overflow here.
8046 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8047 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8055 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8058 m_currentCellCoords
!= wxGridNoCellCoords
&&
8059 m_currentCellCoords
.GetRow() > 0 )
8061 int row
= m_currentCellCoords
.GetRow();
8062 int col
= m_currentCellCoords
.GetCol();
8064 if ( m_table
->IsEmptyCell(row
, col
) )
8066 // starting in an empty cell: find the next block of
8072 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8075 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8077 // starting at the top of a block: find the next block
8083 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8088 // starting within a block: find the top of the block
8093 if ( m_table
->IsEmptyCell(row
, col
) )
8101 MakeCellVisible( row
, col
);
8102 if ( expandSelection
)
8104 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8105 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8110 SetCurrentCell( row
, col
);
8118 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8121 m_currentCellCoords
!= wxGridNoCellCoords
&&
8122 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8124 int row
= m_currentCellCoords
.GetRow();
8125 int col
= m_currentCellCoords
.GetCol();
8127 if ( m_table
->IsEmptyCell(row
, col
) )
8129 // starting in an empty cell: find the next block of
8132 while ( row
< m_numRows
-1 )
8135 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8138 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8140 // starting at the bottom of a block: find the next block
8143 while ( row
< m_numRows
-1 )
8146 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8151 // starting within a block: find the bottom of the block
8153 while ( row
< m_numRows
-1 )
8156 if ( m_table
->IsEmptyCell(row
, col
) )
8164 MakeCellVisible( row
, col
);
8165 if ( expandSelection
)
8167 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8168 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8173 SetCurrentCell( row
, col
);
8182 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8185 m_currentCellCoords
!= wxGridNoCellCoords
&&
8186 m_currentCellCoords
.GetCol() > 0 )
8188 int row
= m_currentCellCoords
.GetRow();
8189 int col
= m_currentCellCoords
.GetCol();
8191 if ( m_table
->IsEmptyCell(row
, col
) )
8193 // starting in an empty cell: find the next block of
8199 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8202 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8204 // starting at the left of a block: find the next block
8210 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8215 // starting within a block: find the left of the block
8220 if ( m_table
->IsEmptyCell(row
, col
) )
8228 MakeCellVisible( row
, col
);
8229 if ( expandSelection
)
8231 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8232 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8237 SetCurrentCell( row
, col
);
8246 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8249 m_currentCellCoords
!= wxGridNoCellCoords
&&
8250 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8252 int row
= m_currentCellCoords
.GetRow();
8253 int col
= m_currentCellCoords
.GetCol();
8255 if ( m_table
->IsEmptyCell(row
, col
) )
8257 // starting in an empty cell: find the next block of
8260 while ( col
< m_numCols
-1 )
8263 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8266 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8268 // starting at the right of a block: find the next block
8271 while ( col
< m_numCols
-1 )
8274 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8279 // starting within a block: find the right of the block
8281 while ( col
< m_numCols
-1 )
8284 if ( m_table
->IsEmptyCell(row
, col
) )
8292 MakeCellVisible( row
, col
);
8293 if ( expandSelection
)
8295 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8296 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8301 SetCurrentCell( row
, col
);
8313 // ------ Label values and formatting
8316 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8318 *horiz
= m_rowLabelHorizAlign
;
8319 *vert
= m_rowLabelVertAlign
;
8322 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8324 *horiz
= m_colLabelHorizAlign
;
8325 *vert
= m_colLabelVertAlign
;
8328 int wxGrid::GetColLabelTextOrientation()
8330 return m_colLabelTextOrientation
;
8333 wxString
wxGrid::GetRowLabelValue( int row
)
8337 return m_table
->GetRowLabelValue( row
);
8347 wxString
wxGrid::GetColLabelValue( int col
)
8351 return m_table
->GetColLabelValue( col
);
8362 void wxGrid::SetRowLabelSize( int width
)
8364 width
= wxMax( width
, 0 );
8365 if ( width
!= m_rowLabelWidth
)
8369 m_rowLabelWin
->Show( FALSE
);
8370 m_cornerLabelWin
->Show( FALSE
);
8372 else if ( m_rowLabelWidth
== 0 )
8374 m_rowLabelWin
->Show( TRUE
);
8375 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8378 m_rowLabelWidth
= width
;
8380 wxScrolledWindow::Refresh( TRUE
);
8385 void wxGrid::SetColLabelSize( int height
)
8387 height
= wxMax( height
, 0 );
8388 if ( height
!= m_colLabelHeight
)
8392 m_colLabelWin
->Show( FALSE
);
8393 m_cornerLabelWin
->Show( FALSE
);
8395 else if ( m_colLabelHeight
== 0 )
8397 m_colLabelWin
->Show( TRUE
);
8398 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8401 m_colLabelHeight
= height
;
8403 wxScrolledWindow::Refresh( TRUE
);
8408 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8410 if ( m_labelBackgroundColour
!= colour
)
8412 m_labelBackgroundColour
= colour
;
8413 m_rowLabelWin
->SetBackgroundColour( colour
);
8414 m_colLabelWin
->SetBackgroundColour( colour
);
8415 m_cornerLabelWin
->SetBackgroundColour( colour
);
8417 if ( !GetBatchCount() )
8419 m_rowLabelWin
->Refresh();
8420 m_colLabelWin
->Refresh();
8421 m_cornerLabelWin
->Refresh();
8426 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8428 if ( m_labelTextColour
!= colour
)
8430 m_labelTextColour
= colour
;
8431 if ( !GetBatchCount() )
8433 m_rowLabelWin
->Refresh();
8434 m_colLabelWin
->Refresh();
8439 void wxGrid::SetLabelFont( const wxFont
& font
)
8442 if ( !GetBatchCount() )
8444 m_rowLabelWin
->Refresh();
8445 m_colLabelWin
->Refresh();
8449 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8451 // allow old (incorrect) defs to be used
8454 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8455 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8456 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8461 case wxTOP
: vert
= wxALIGN_TOP
; break;
8462 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8463 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8466 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8468 m_rowLabelHorizAlign
= horiz
;
8471 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8473 m_rowLabelVertAlign
= vert
;
8476 if ( !GetBatchCount() )
8478 m_rowLabelWin
->Refresh();
8482 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8484 // allow old (incorrect) defs to be used
8487 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8488 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8489 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8494 case wxTOP
: vert
= wxALIGN_TOP
; break;
8495 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8496 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8499 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8501 m_colLabelHorizAlign
= horiz
;
8504 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8506 m_colLabelVertAlign
= vert
;
8509 if ( !GetBatchCount() )
8511 m_colLabelWin
->Refresh();
8515 // Note: under MSW, the default column label font must be changed because it
8516 // does not support vertical printing
8518 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8519 // pGrid->SetLabelFont(font);
8520 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8522 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8524 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8526 m_colLabelTextOrientation
= textOrientation
;
8529 if ( !GetBatchCount() )
8531 m_colLabelWin
->Refresh();
8535 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8539 m_table
->SetRowLabelValue( row
, s
);
8540 if ( !GetBatchCount() )
8542 wxRect rect
= CellToRect( row
, 0);
8543 if ( rect
.height
> 0 )
8545 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8547 rect
.width
= m_rowLabelWidth
;
8548 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8554 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8558 m_table
->SetColLabelValue( col
, s
);
8559 if ( !GetBatchCount() )
8561 wxRect rect
= CellToRect( 0, col
);
8562 if ( rect
.width
> 0 )
8564 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8566 rect
.height
= m_colLabelHeight
;
8567 m_colLabelWin
->Refresh( TRUE
, &rect
);
8573 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8575 if ( m_gridLineColour
!= colour
)
8577 m_gridLineColour
= colour
;
8579 wxClientDC
dc( m_gridWin
);
8581 DrawAllGridLines( dc
, wxRegion() );
8586 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8588 if ( m_cellHighlightColour
!= colour
)
8590 m_cellHighlightColour
= colour
;
8592 wxClientDC
dc( m_gridWin
);
8594 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8595 DrawCellHighlight(dc
, attr
);
8600 void wxGrid::SetCellHighlightPenWidth(int width
)
8602 if (m_cellHighlightPenWidth
!= width
) {
8603 m_cellHighlightPenWidth
= width
;
8605 // Just redrawing the cell highlight is not enough since that won't
8606 // make any visible change if the the thickness is getting smaller.
8607 int row
= m_currentCellCoords
.GetRow();
8608 int col
= m_currentCellCoords
.GetCol();
8609 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8611 wxRect rect
= CellToRect(row
, col
);
8612 m_gridWin
->Refresh(TRUE
, &rect
);
8616 void wxGrid::SetCellHighlightROPenWidth(int width
)
8618 if (m_cellHighlightROPenWidth
!= width
) {
8619 m_cellHighlightROPenWidth
= width
;
8621 // Just redrawing the cell highlight is not enough since that won't
8622 // make any visible change if the the thickness is getting smaller.
8623 int row
= m_currentCellCoords
.GetRow();
8624 int col
= m_currentCellCoords
.GetCol();
8625 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8627 wxRect rect
= CellToRect(row
, col
);
8628 m_gridWin
->Refresh(TRUE
, &rect
);
8632 void wxGrid::EnableGridLines( bool enable
)
8634 if ( enable
!= m_gridLinesEnabled
)
8636 m_gridLinesEnabled
= enable
;
8638 if ( !GetBatchCount() )
8642 wxClientDC
dc( m_gridWin
);
8644 DrawAllGridLines( dc
, wxRegion() );
8648 m_gridWin
->Refresh();
8655 int wxGrid::GetDefaultRowSize()
8657 return m_defaultRowHeight
;
8660 int wxGrid::GetRowSize( int row
)
8662 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8664 return GetRowHeight(row
);
8667 int wxGrid::GetDefaultColSize()
8669 return m_defaultColWidth
;
8672 int wxGrid::GetColSize( int col
)
8674 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8676 return GetColWidth(col
);
8679 // ============================================================================
8680 // access to the grid attributes: each of them has a default value in the grid
8681 // itself and may be overidden on a per-cell basis
8682 // ============================================================================
8684 // ----------------------------------------------------------------------------
8685 // setting default attributes
8686 // ----------------------------------------------------------------------------
8688 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8690 m_defaultCellAttr
->SetBackgroundColour(col
);
8692 m_gridWin
->SetBackgroundColour(col
);
8696 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8698 m_defaultCellAttr
->SetTextColour(col
);
8701 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8703 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8706 void wxGrid::SetDefaultCellOverflow( bool allow
)
8708 m_defaultCellAttr
->SetOverflow(allow
);
8711 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8713 m_defaultCellAttr
->SetFont(font
);
8716 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8718 m_defaultCellAttr
->SetRenderer(renderer
);
8721 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8723 m_defaultCellAttr
->SetEditor(editor
);
8726 // ----------------------------------------------------------------------------
8727 // access to the default attrbiutes
8728 // ----------------------------------------------------------------------------
8730 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8732 return m_defaultCellAttr
->GetBackgroundColour();
8735 wxColour
wxGrid::GetDefaultCellTextColour()
8737 return m_defaultCellAttr
->GetTextColour();
8740 wxFont
wxGrid::GetDefaultCellFont()
8742 return m_defaultCellAttr
->GetFont();
8745 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8747 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8750 bool wxGrid::GetDefaultCellOverflow()
8752 return m_defaultCellAttr
->GetOverflow();
8755 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8757 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8760 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8762 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8765 // ----------------------------------------------------------------------------
8766 // access to cell attributes
8767 // ----------------------------------------------------------------------------
8769 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8771 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8772 wxColour colour
= attr
->GetBackgroundColour();
8777 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8779 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8780 wxColour colour
= attr
->GetTextColour();
8785 wxFont
wxGrid::GetCellFont( int row
, int col
)
8787 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8788 wxFont font
= attr
->GetFont();
8793 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8795 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8796 attr
->GetAlignment(horiz
, vert
);
8800 bool wxGrid::GetCellOverflow( int row
, int col
)
8802 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8803 bool allow
= attr
->GetOverflow();
8808 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8810 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8811 attr
->GetSize( num_rows
, num_cols
);
8815 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8817 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8818 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8824 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8826 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8827 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8833 bool wxGrid::IsReadOnly(int row
, int col
) const
8835 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8836 bool isReadOnly
= attr
->IsReadOnly();
8841 // ----------------------------------------------------------------------------
8842 // attribute support: cache, automatic provider creation, ...
8843 // ----------------------------------------------------------------------------
8845 bool wxGrid::CanHaveAttributes()
8852 return m_table
->CanHaveAttributes();
8855 void wxGrid::ClearAttrCache()
8857 if ( m_attrCache
.row
!= -1 )
8859 wxSafeDecRef(m_attrCache
.attr
);
8860 m_attrCache
.attr
= NULL
;
8861 m_attrCache
.row
= -1;
8865 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8869 wxGrid
*self
= (wxGrid
*)this; // const_cast
8871 self
->ClearAttrCache();
8872 self
->m_attrCache
.row
= row
;
8873 self
->m_attrCache
.col
= col
;
8874 self
->m_attrCache
.attr
= attr
;
8879 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8881 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8883 *attr
= m_attrCache
.attr
;
8884 wxSafeIncRef(m_attrCache
.attr
);
8886 #ifdef DEBUG_ATTR_CACHE
8887 gs_nAttrCacheHits
++;
8894 #ifdef DEBUG_ATTR_CACHE
8895 gs_nAttrCacheMisses
++;
8901 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8903 wxGridCellAttr
*attr
= NULL
;
8904 // Additional test to avoid looking at the cache e.g. for
8905 // wxNoCellCoords, as this will confuse memory management.
8908 if ( !LookupAttr(row
, col
, &attr
) )
8910 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8911 : (wxGridCellAttr
*)NULL
;
8912 CacheAttr(row
, col
, attr
);
8917 attr
->SetDefAttr(m_defaultCellAttr
);
8921 attr
= m_defaultCellAttr
;
8928 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8930 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8932 wxCHECK_MSG( m_table
, attr
,
8933 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8935 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8938 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8940 // artificially inc the ref count to match DecRef() in caller
8942 m_table
->SetAttr(attr
, row
, col
);
8948 // ----------------------------------------------------------------------------
8949 // setting column attributes (wrappers around SetColAttr)
8950 // ----------------------------------------------------------------------------
8952 void wxGrid::SetColFormatBool(int col
)
8954 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8957 void wxGrid::SetColFormatNumber(int col
)
8959 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8962 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8964 wxString typeName
= wxGRID_VALUE_FLOAT
;
8965 if ( (width
!= -1) || (precision
!= -1) )
8967 typeName
<< _T(':') << width
<< _T(',') << precision
;
8970 SetColFormatCustom(col
, typeName
);
8973 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8975 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8977 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8979 attr
= new wxGridCellAttr
;
8980 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8981 attr
->SetRenderer(renderer
);
8983 SetColAttr(col
, attr
);
8987 // ----------------------------------------------------------------------------
8988 // setting cell attributes: this is forwarded to the table
8989 // ----------------------------------------------------------------------------
8991 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
8993 if ( CanHaveAttributes() )
8995 m_table
->SetAttr(attr
, row
, col
);
9004 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9006 if ( CanHaveAttributes() )
9008 m_table
->SetRowAttr(attr
, row
);
9017 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9019 if ( CanHaveAttributes() )
9021 m_table
->SetColAttr(attr
, col
);
9030 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9032 if ( CanHaveAttributes() )
9034 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9035 attr
->SetBackgroundColour(colour
);
9040 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9042 if ( CanHaveAttributes() )
9044 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9045 attr
->SetTextColour(colour
);
9050 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9052 if ( CanHaveAttributes() )
9054 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9055 attr
->SetFont(font
);
9060 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9062 if ( CanHaveAttributes() )
9064 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9065 attr
->SetAlignment(horiz
, vert
);
9070 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9072 if ( CanHaveAttributes() )
9074 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9075 attr
->SetOverflow(allow
);
9080 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9082 if ( CanHaveAttributes() )
9084 int cell_rows
, cell_cols
;
9086 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9087 attr
->GetSize(&cell_rows
, &cell_cols
);
9088 attr
->SetSize(num_rows
, num_cols
);
9091 // Cannot set the size of a cell to 0 or negative values
9092 // While it is perfectly legal to do that, this function cannot
9093 // handle all the possibilies, do it by hand by getting the CellAttr.
9094 // You can only set the size of a cell to 1,1 or greater with this fn
9095 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9096 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9097 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9098 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9100 // if this was already a multicell then "turn off" the other cells first
9101 if ((cell_rows
> 1) || (cell_rows
> 1))
9104 for (j
=row
; j
<row
+cell_rows
; j
++)
9106 for (i
=col
; i
<col
+cell_cols
; i
++)
9108 if ((i
!= col
) || (j
!= row
))
9110 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9111 attr_stub
->SetSize( 1, 1 );
9112 attr_stub
->DecRef();
9118 // mark the cells that will be covered by this cell to
9119 // negative or zero values to point back at this cell
9120 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9123 for (j
=row
; j
<row
+num_rows
; j
++)
9125 for (i
=col
; i
<col
+num_cols
; i
++)
9127 if ((i
!= col
) || (j
!= row
))
9129 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9130 attr_stub
->SetSize( row
-j
, col
-i
);
9131 attr_stub
->DecRef();
9139 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9141 if ( CanHaveAttributes() )
9143 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9144 attr
->SetRenderer(renderer
);
9149 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9151 if ( CanHaveAttributes() )
9153 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9154 attr
->SetEditor(editor
);
9159 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9161 if ( CanHaveAttributes() )
9163 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9164 attr
->SetReadOnly(isReadOnly
);
9169 // ----------------------------------------------------------------------------
9170 // Data type registration
9171 // ----------------------------------------------------------------------------
9173 void wxGrid::RegisterDataType(const wxString
& typeName
,
9174 wxGridCellRenderer
* renderer
,
9175 wxGridCellEditor
* editor
)
9177 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9181 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9183 wxString typeName
= m_table
->GetTypeName(row
, col
);
9184 return GetDefaultEditorForType(typeName
);
9187 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9189 wxString typeName
= m_table
->GetTypeName(row
, col
);
9190 return GetDefaultRendererForType(typeName
);
9194 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9196 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9197 if ( index
== wxNOT_FOUND
)
9199 wxFAIL_MSG(wxT("Unknown data type name"));
9204 return m_typeRegistry
->GetEditor(index
);
9208 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9210 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9211 if ( index
== wxNOT_FOUND
)
9213 wxFAIL_MSG(wxT("Unknown data type name"));
9218 return m_typeRegistry
->GetRenderer(index
);
9222 // ----------------------------------------------------------------------------
9224 // ----------------------------------------------------------------------------
9226 void wxGrid::EnableDragRowSize( bool enable
)
9228 m_canDragRowSize
= enable
;
9232 void wxGrid::EnableDragColSize( bool enable
)
9234 m_canDragColSize
= enable
;
9237 void wxGrid::EnableDragGridSize( bool enable
)
9239 m_canDragGridSize
= enable
;
9243 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9245 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
9247 if ( resizeExistingRows
)
9249 // since we are resizing all rows to the default row size,
9250 // we can simply clear the row heights and row bottoms
9251 // arrays (which also allows us to take advantage of
9252 // some speed optimisations)
9253 m_rowHeights
.Empty();
9254 m_rowBottoms
.Empty();
9255 if ( !GetBatchCount() )
9260 void wxGrid::SetRowSize( int row
, int height
)
9262 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9264 if ( m_rowHeights
.IsEmpty() )
9266 // need to really create the array
9270 int h
= wxMax( 0, height
);
9271 int diff
= h
- m_rowHeights
[row
];
9273 m_rowHeights
[row
] = h
;
9275 for ( i
= row
; i
< m_numRows
; i
++ )
9277 m_rowBottoms
[i
] += diff
;
9279 if ( !GetBatchCount() )
9283 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9285 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
9287 if ( resizeExistingCols
)
9289 // since we are resizing all columns to the default column size,
9290 // we can simply clear the col widths and col rights
9291 // arrays (which also allows us to take advantage of
9292 // some speed optimisations)
9293 m_colWidths
.Empty();
9294 m_colRights
.Empty();
9295 if ( !GetBatchCount() )
9300 void wxGrid::SetColSize( int col
, int width
)
9302 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9304 // should we check that it's bigger than GetColMinimalWidth(col) here?
9306 if ( m_colWidths
.IsEmpty() )
9308 // need to really create the array
9312 // if < 0 calc new width from label
9316 wxArrayString lines
;
9317 wxClientDC
dc(m_colLabelWin
);
9318 dc
.SetFont(GetLabelFont());
9319 StringToLines(GetColLabelValue(col
), lines
);
9320 GetTextBoxSize(dc
, lines
, &w
, &h
);
9323 int w
= wxMax( 0, width
);
9324 int diff
= w
- m_colWidths
[col
];
9325 m_colWidths
[col
] = w
;
9328 for ( i
= col
; i
< m_numCols
; i
++ )
9330 m_colRights
[i
] += diff
;
9332 if ( !GetBatchCount() )
9337 void wxGrid::SetColMinimalWidth( int col
, int width
)
9339 m_colMinWidths
.Put(col
, width
);
9342 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9344 m_rowMinHeights
.Put(row
, width
);
9347 int wxGrid::GetColMinimalWidth(int col
) const
9349 long value
= m_colMinWidths
.Get(col
);
9350 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
9353 int wxGrid::GetRowMinimalHeight(int row
) const
9355 long value
= m_rowMinHeights
.Get(row
);
9356 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
9359 // ----------------------------------------------------------------------------
9361 // ----------------------------------------------------------------------------
9363 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9365 wxClientDC
dc(m_gridWin
);
9367 // init both of them to avoid compiler warnings, even if weo nly need one
9375 wxCoord extent
, extentMax
= 0;
9376 int max
= column
? m_numRows
: m_numCols
;
9377 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9384 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9385 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9388 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9389 extent
= column
? size
.x
: size
.y
;
9390 if ( extent
> extentMax
)
9401 // now also compare with the column label extent
9403 dc
.SetFont( GetLabelFont() );
9407 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9408 if( GetColLabelTextOrientation() == wxVERTICAL
)
9412 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9414 extent
= column
? w
: h
;
9415 if ( extent
> extentMax
)
9422 // empty column - give default extent (notice that if extentMax is less
9423 // than default extent but != 0, it's ok)
9424 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9430 // leave some space around text
9441 SetColSize(col
, extentMax
);
9442 if ( !GetBatchCount() )
9445 m_gridWin
->GetClientSize( &cw
, &ch
);
9446 wxRect
rect ( CellToRect( 0, col
) );
9448 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9449 rect
.width
= cw
- rect
.x
;
9450 rect
.height
= m_colLabelHeight
;
9451 m_colLabelWin
->Refresh( TRUE
, &rect
);
9456 SetRowSize(row
, extentMax
);
9457 if ( !GetBatchCount() )
9460 m_gridWin
->GetClientSize( &cw
, &ch
);
9461 wxRect
rect ( CellToRect( row
, 0 ) );
9463 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9464 rect
.width
= m_rowLabelWidth
;
9465 rect
.height
= ch
- rect
.y
;
9466 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9472 SetColMinimalWidth(col
, extentMax
);
9474 SetRowMinimalHeight(row
, extentMax
);
9478 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9480 int width
= m_rowLabelWidth
;
9485 for ( int col
= 0; col
< m_numCols
; col
++ )
9489 AutoSizeColumn(col
, setAsMin
);
9492 width
+= GetColWidth(col
);
9501 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9503 int height
= m_colLabelHeight
;
9508 for ( int row
= 0; row
< m_numRows
; row
++ )
9512 AutoSizeRow(row
, setAsMin
);
9515 height
+= GetRowHeight(row
);
9524 void wxGrid::AutoSize()
9528 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9530 // round up the size to a multiple of scroll step - this ensures that we
9531 // won't get the scrollbars if we're sized exactly to this width
9532 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9534 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9535 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9537 // distribute the extra space between the columns/rows to avoid having
9538 // extra white space
9540 // Remove the extra m_extraWidth + 1 added above
9541 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9542 if ( diff
&& m_numCols
)
9544 // try to resize the columns uniformly
9545 wxCoord diffPerCol
= diff
/ m_numCols
;
9548 for ( int col
= 0; col
< m_numCols
; col
++ )
9550 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9554 // add remaining amount to the last columns
9555 diff
-= diffPerCol
* m_numCols
;
9558 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9560 SetColSize(col
, GetColWidth(col
) + 1);
9566 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9567 if ( diff
&& m_numRows
)
9569 // try to resize the columns uniformly
9570 wxCoord diffPerRow
= diff
/ m_numRows
;
9573 for ( int row
= 0; row
< m_numRows
; row
++ )
9575 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9579 // add remaining amount to the last rows
9580 diff
-= diffPerRow
* m_numRows
;
9583 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9585 SetRowSize(row
, GetRowHeight(row
) + 1);
9592 SetClientSize(sizeFit
);
9595 void wxGrid::AutoSizeRowLabelSize( int row
)
9597 wxArrayString lines
;
9600 // Hide the edit control, so it
9601 // won't interfer with drag-shrinking.
9602 if( IsCellEditControlShown() )
9604 HideCellEditControl();
9605 SaveEditControlValue();
9608 // autosize row height depending on label text
9609 StringToLines( GetRowLabelValue( row
), lines
);
9610 wxClientDC
dc( m_rowLabelWin
);
9611 GetTextBoxSize( dc
, lines
, &w
, &h
);
9612 if( h
< m_defaultRowHeight
)
9613 h
= m_defaultRowHeight
;
9618 void wxGrid::AutoSizeColLabelSize( int col
)
9620 wxArrayString lines
;
9623 // Hide the edit control, so it
9624 // won't interfer with drag-shrinking.
9625 if( IsCellEditControlShown() )
9627 HideCellEditControl();
9628 SaveEditControlValue();
9631 // autosize column width depending on label text
9632 StringToLines( GetColLabelValue( col
), lines
);
9633 wxClientDC
dc( m_colLabelWin
);
9634 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9635 GetTextBoxSize( dc
, lines
, &w
, &h
);
9637 GetTextBoxSize( dc
, lines
, &h
, &w
);
9638 if( w
< m_defaultColWidth
)
9639 w
= m_defaultColWidth
;
9644 wxSize
wxGrid::DoGetBestSize() const
9646 // don't set sizes, only calculate them
9647 wxGrid
*self
= (wxGrid
*)this; // const_cast
9650 width
= self
->SetOrCalcColumnSizes(TRUE
);
9651 height
= self
->SetOrCalcRowSizes(TRUE
);
9653 int maxwidth
, maxheight
;
9654 wxDisplaySize( & maxwidth
, & maxheight
);
9656 if ( width
> maxwidth
) width
= maxwidth
;
9657 if ( height
> maxheight
) height
= maxheight
;
9659 return wxSize( width
, height
);
9668 wxPen
& wxGrid::GetDividerPen() const
9673 // ----------------------------------------------------------------------------
9674 // cell value accessor functions
9675 // ----------------------------------------------------------------------------
9677 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9681 m_table
->SetValue( row
, col
, s
);
9682 if ( !GetBatchCount() )
9685 wxRect
rect( CellToRect( row
, col
) );
9687 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9688 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9689 m_gridWin
->Refresh( FALSE
, &rect
);
9692 if ( m_currentCellCoords
.GetRow() == row
&&
9693 m_currentCellCoords
.GetCol() == col
&&
9694 IsCellEditControlShown())
9695 // Note: If we are using IsCellEditControlEnabled,
9696 // this interacts badly with calling SetCellValue from
9697 // an EVT_GRID_CELL_CHANGE handler.
9699 HideCellEditControl();
9700 ShowCellEditControl(); // will reread data from table
9707 // ------ Block, row and col selection
9710 void wxGrid::SelectRow( int row
, bool addToSelected
)
9712 if ( IsSelection() && !addToSelected
)
9716 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9720 void wxGrid::SelectCol( int col
, bool addToSelected
)
9722 if ( IsSelection() && !addToSelected
)
9726 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9730 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9731 bool addToSelected
)
9733 if ( IsSelection() && !addToSelected
)
9737 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9738 FALSE
, addToSelected
);
9742 void wxGrid::SelectAll()
9744 if ( m_numRows
> 0 && m_numCols
> 0 )
9747 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9752 // ------ Cell, row and col deselection
9755 void wxGrid::DeselectRow( int row
)
9760 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9762 if ( m_selection
->IsInSelection(row
, 0 ) )
9763 m_selection
->ToggleCellSelection( row
, 0);
9767 int nCols
= GetNumberCols();
9768 for ( int i
= 0; i
< nCols
; i
++ )
9770 if ( m_selection
->IsInSelection(row
, i
) )
9771 m_selection
->ToggleCellSelection( row
, i
);
9776 void wxGrid::DeselectCol( int col
)
9781 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9783 if ( m_selection
->IsInSelection(0, col
) )
9784 m_selection
->ToggleCellSelection( 0, col
);
9788 int nRows
= GetNumberRows();
9789 for ( int i
= 0; i
< nRows
; i
++ )
9791 if ( m_selection
->IsInSelection(i
, col
) )
9792 m_selection
->ToggleCellSelection(i
, col
);
9797 void wxGrid::DeselectCell( int row
, int col
)
9799 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9800 m_selection
->ToggleCellSelection(row
, col
);
9803 bool wxGrid::IsSelection()
9805 return ( m_selection
&& (m_selection
->IsSelection() ||
9806 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9807 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9810 bool wxGrid::IsInSelection( int row
, int col
) const
9812 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9813 ( row
>= m_selectingTopLeft
.GetRow() &&
9814 col
>= m_selectingTopLeft
.GetCol() &&
9815 row
<= m_selectingBottomRight
.GetRow() &&
9816 col
<= m_selectingBottomRight
.GetCol() )) );
9819 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9821 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9822 return m_selection
->m_cellSelection
;
9824 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9826 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9827 return m_selection
->m_blockSelectionTopLeft
;
9829 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9831 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9832 return m_selection
->m_blockSelectionBottomRight
;
9834 wxArrayInt
wxGrid::GetSelectedRows() const
9836 if (!m_selection
) { wxArrayInt a
; return a
; }
9837 return m_selection
->m_rowSelection
;
9839 wxArrayInt
wxGrid::GetSelectedCols() const
9841 if (!m_selection
) { wxArrayInt a
; return a
; }
9842 return m_selection
->m_colSelection
;
9846 void wxGrid::ClearSelection()
9848 m_selectingTopLeft
= wxGridNoCellCoords
;
9849 m_selectingBottomRight
= wxGridNoCellCoords
;
9851 m_selection
->ClearSelection();
9855 // This function returns the rectangle that encloses the given block
9856 // in device coords clipped to the client size of the grid window.
9858 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9859 const wxGridCellCoords
&bottomRight
)
9861 wxRect
rect( wxGridNoCellRect
);
9864 cellRect
= CellToRect( topLeft
);
9865 if ( cellRect
!= wxGridNoCellRect
)
9871 rect
= wxRect( 0, 0, 0, 0 );
9874 cellRect
= CellToRect( bottomRight
);
9875 if ( cellRect
!= wxGridNoCellRect
)
9881 return wxGridNoCellRect
;
9885 int left
= rect
.GetLeft();
9886 int top
= rect
.GetTop();
9887 int right
= rect
.GetRight();
9888 int bottom
= rect
.GetBottom();
9890 int leftCol
= topLeft
.GetCol();
9891 int topRow
= topLeft
.GetRow();
9892 int rightCol
= bottomRight
.GetCol();
9893 int bottomRow
= bottomRight
.GetRow();
9916 for ( j
= topRow
; j
<= bottomRow
; j
++ )
9918 for ( i
= leftCol
; i
<= rightCol
; i
++ )
9920 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
9922 cellRect
= CellToRect( j
, i
);
9924 if (cellRect
.x
< left
)
9926 if (cellRect
.y
< top
)
9928 if (cellRect
.x
+ cellRect
.width
> right
)
9929 right
= cellRect
.x
+ cellRect
.width
;
9930 if (cellRect
.y
+ cellRect
.height
> bottom
)
9931 bottom
= cellRect
.y
+ cellRect
.height
;
9933 else i
= rightCol
; // jump over inner cells.
9937 // convert to scrolled coords
9939 CalcScrolledPosition( left
, top
, &left
, &top
);
9940 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9943 m_gridWin
->GetClientSize( &cw
, &ch
);
9945 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9946 return wxRect( 0, 0, 0, 0);
9948 rect
.SetLeft( wxMax(0, left
) );
9949 rect
.SetTop( wxMax(0, top
) );
9950 rect
.SetRight( wxMin(cw
, right
) );
9951 rect
.SetBottom( wxMin(ch
, bottom
) );
9959 // ------ Grid event classes
9962 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9964 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9965 int row
, int col
, int x
, int y
, bool sel
,
9966 bool control
, bool shift
, bool alt
, bool meta
)
9967 : wxNotifyEvent( type
, id
)
9974 m_control
= control
;
9979 SetEventObject(obj
);
9983 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9985 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9986 int rowOrCol
, int x
, int y
,
9987 bool control
, bool shift
, bool alt
, bool meta
)
9988 : wxNotifyEvent( type
, id
)
9990 m_rowOrCol
= rowOrCol
;
9993 m_control
= control
;
9998 SetEventObject(obj
);
10002 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10004 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10005 const wxGridCellCoords
& topLeft
,
10006 const wxGridCellCoords
& bottomRight
,
10007 bool sel
, bool control
,
10008 bool shift
, bool alt
, bool meta
)
10009 : wxNotifyEvent( type
, id
)
10011 m_topLeft
= topLeft
;
10012 m_bottomRight
= bottomRight
;
10014 m_control
= control
;
10019 SetEventObject(obj
);
10023 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10025 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10026 wxObject
* obj
, int row
,
10027 int col
, wxControl
* ctrl
)
10028 : wxCommandEvent(type
, id
)
10030 SetEventObject(obj
);
10036 #endif // wxUSE_GRID