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"
35 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
37 #else // wxUSE_NEW_GRID
41 #include "wx/dcclient.h"
42 #include "wx/settings.h"
44 #include "wx/textctrl.h"
45 #include "wx/checkbox.h"
46 #include "wx/combobox.h"
47 #include "wx/valtext.h"
50 #include "wx/textfile.h"
51 #include "wx/spinctrl.h"
52 #include "wx/tokenzr.h"
55 #include "wx/generic/gridsel.h"
57 #if defined(__WXMOTIF__)
58 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
60 #define WXUNUSED_MOTIF(identifier) identifier
63 #if defined(__WXGTK__)
64 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
66 #define WXUNUSED_GTK(identifier) identifier
69 // Required for wxIs... functions
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
78 struct wxGridCellWithAttr
80 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
81 : coords(row
, col
), attr(attr_
)
90 wxGridCellCoords coords
;
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()
147 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
150 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
151 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
152 const wxPoint
&pos
, const wxSize
&size
);
157 void OnPaint( wxPaintEvent
&event
);
158 void OnMouseEvent( wxMouseEvent
& event
);
159 void OnMouseWheel( wxMouseEvent
& event
);
160 void OnKeyDown( wxKeyEvent
& event
);
161 void OnKeyUp( wxKeyEvent
& );
163 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
164 DECLARE_EVENT_TABLE()
168 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
171 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
172 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
173 const wxPoint
&pos
, const wxSize
&size
);
178 void OnMouseEvent( wxMouseEvent
& event
);
179 void OnMouseWheel( wxMouseEvent
& event
);
180 void OnKeyDown( wxKeyEvent
& event
);
181 void OnKeyUp( wxKeyEvent
& );
182 void OnPaint( wxPaintEvent
& event
);
184 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
185 DECLARE_EVENT_TABLE()
188 class WXDLLEXPORT wxGridWindow
: public wxWindow
193 m_owner
= (wxGrid
*)NULL
;
194 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
195 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
198 wxGridWindow( wxGrid
*parent
,
199 wxGridRowLabelWindow
*rowLblWin
,
200 wxGridColLabelWindow
*colLblWin
,
201 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
204 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
206 wxGrid
* GetOwner() { return m_owner
; }
210 wxGridRowLabelWindow
*m_rowLabelWin
;
211 wxGridColLabelWindow
*m_colLabelWin
;
213 void OnPaint( wxPaintEvent
&event
);
214 void OnMouseWheel( wxMouseEvent
& event
);
215 void OnMouseEvent( wxMouseEvent
& event
);
216 void OnKeyDown( wxKeyEvent
& );
217 void OnKeyUp( wxKeyEvent
& );
218 void OnEraseBackground( wxEraseEvent
& );
221 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
222 DECLARE_EVENT_TABLE()
227 class wxGridCellEditorEvtHandler
: public wxEvtHandler
230 wxGridCellEditorEvtHandler()
231 : m_grid(0), m_editor(0)
233 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
234 : m_grid(grid
), m_editor(editor
)
237 void OnKeyDown(wxKeyEvent
& event
);
238 void OnChar(wxKeyEvent
& event
);
242 wxGridCellEditor
* m_editor
;
243 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
244 DECLARE_EVENT_TABLE()
248 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
249 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
250 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
251 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
256 // ----------------------------------------------------------------------------
257 // the internal data representation used by wxGridCellAttrProvider
258 // ----------------------------------------------------------------------------
260 // this class stores attributes set for cells
261 class WXDLLEXPORT wxGridCellAttrData
264 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
265 wxGridCellAttr
*GetAttr(int row
, int col
) const;
266 void UpdateAttrRows( size_t pos
, int numRows
);
267 void UpdateAttrCols( size_t pos
, int numCols
);
270 // searches for the attr for given cell, returns wxNOT_FOUND if not found
271 int FindIndex(int row
, int col
) const;
273 wxGridCellWithAttrArray m_attrs
;
276 // this class stores attributes set for rows or columns
277 class WXDLLEXPORT wxGridRowOrColAttrData
280 // empty ctor to suppress warnings
281 wxGridRowOrColAttrData() { }
282 ~wxGridRowOrColAttrData();
284 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
285 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
286 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
289 wxArrayInt m_rowsOrCols
;
290 wxArrayAttrs m_attrs
;
293 // NB: this is just a wrapper around 3 objects: one which stores cell
294 // attributes, and 2 others for row/col ones
295 class WXDLLEXPORT wxGridCellAttrProviderData
298 wxGridCellAttrData m_cellAttrs
;
299 wxGridRowOrColAttrData m_rowAttrs
,
304 // ----------------------------------------------------------------------------
305 // data structures used for the data type registry
306 // ----------------------------------------------------------------------------
308 struct wxGridDataTypeInfo
310 wxGridDataTypeInfo(const wxString
& typeName
,
311 wxGridCellRenderer
* renderer
,
312 wxGridCellEditor
* editor
)
313 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
316 ~wxGridDataTypeInfo()
318 wxSafeDecRef(m_renderer
);
319 wxSafeDecRef(m_editor
);
323 wxGridCellRenderer
* m_renderer
;
324 wxGridCellEditor
* m_editor
;
328 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
331 class WXDLLEXPORT wxGridTypeRegistry
334 wxGridTypeRegistry() {}
335 ~wxGridTypeRegistry();
337 void RegisterDataType(const wxString
& typeName
,
338 wxGridCellRenderer
* renderer
,
339 wxGridCellEditor
* editor
);
341 // find one of already registered data types
342 int FindRegisteredDataType(const wxString
& typeName
);
344 // try to FindRegisteredDataType(), if this fails and typeName is one of
345 // standard typenames, register it and return its index
346 int FindDataType(const wxString
& typeName
);
348 // try to FindDataType(), if it fails see if it is not one of already
349 // registered data types with some params in which case clone the
350 // registered data type and set params for it
351 int FindOrCloneDataType(const wxString
& typeName
);
353 wxGridCellRenderer
* GetRenderer(int index
);
354 wxGridCellEditor
* GetEditor(int index
);
357 wxGridDataTypeInfoArray m_typeinfo
;
360 // ----------------------------------------------------------------------------
361 // conditional compilation
362 // ----------------------------------------------------------------------------
364 #ifndef WXGRID_DRAW_LINES
365 #define WXGRID_DRAW_LINES 1
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
372 //#define DEBUG_ATTR_CACHE
373 #ifdef DEBUG_ATTR_CACHE
374 static size_t gs_nAttrCacheHits
= 0;
375 static size_t gs_nAttrCacheMisses
= 0;
376 #endif // DEBUG_ATTR_CACHE
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
383 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
386 // TODO: this doesn't work at all, grid cells have different sizes and approx
387 // calculations don't work as because of the size mismatch scrollbars
388 // sometimes fail to be shown when they should be or vice versa
390 // The scroll bars may be a little flakey once in a while, but that is
391 // surely much less horrible than having scroll lines of only 1!!!
394 // Well, it's still seriously broken so it might be better but needs
397 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
398 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
400 // the size of hash tables used a bit everywhere (the max number of elements
401 // in these hash tables is the number of rows/columns)
402 static const int GRID_HASH_SIZE
= 100;
404 // ----------------------------------------------------------------------------
406 // ----------------------------------------------------------------------------
408 static inline int GetScrollX(int x
)
410 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
413 static inline int GetScrollY(int y
)
415 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
418 // ============================================================================
420 // ============================================================================
422 // ----------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------
426 wxGridCellEditor::wxGridCellEditor()
433 wxGridCellEditor::~wxGridCellEditor()
438 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
439 wxWindowID
WXUNUSED(id
),
440 wxEvtHandler
* evtHandler
)
443 m_control
->PushEventHandler(evtHandler
);
446 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
447 wxGridCellAttr
*attr
)
449 // erase the background because we might not fill the cell
450 wxClientDC
dc(m_control
->GetParent());
451 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
453 gridWindow
->GetOwner()->PrepareDC(dc
);
455 dc
.SetPen(*wxTRANSPARENT_PEN
);
456 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
457 dc
.DrawRectangle(rectCell
);
459 // redraw the control we just painted over
460 m_control
->Refresh();
463 void wxGridCellEditor::Destroy()
467 m_control
->PopEventHandler(TRUE
/* delete it*/);
469 m_control
->Destroy();
474 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
476 wxASSERT_MSG(m_control
,
477 wxT("The wxGridCellEditor must be Created first!"));
478 m_control
->Show(show
);
482 // set the colours/fonts if we have any
485 m_colFgOld
= m_control
->GetForegroundColour();
486 m_control
->SetForegroundColour(attr
->GetTextColour());
488 m_colBgOld
= m_control
->GetBackgroundColour();
489 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
491 m_fontOld
= m_control
->GetFont();
492 m_control
->SetFont(attr
->GetFont());
494 // can't do anything more in the base class version, the other
495 // attributes may only be used by the derived classes
500 // restore the standard colours fonts
501 if ( m_colFgOld
.Ok() )
503 m_control
->SetForegroundColour(m_colFgOld
);
504 m_colFgOld
= wxNullColour
;
507 if ( m_colBgOld
.Ok() )
509 m_control
->SetBackgroundColour(m_colBgOld
);
510 m_colBgOld
= wxNullColour
;
513 if ( m_fontOld
.Ok() )
515 m_control
->SetFont(m_fontOld
);
516 m_fontOld
= wxNullFont
;
521 void wxGridCellEditor::SetSize(const wxRect
& rect
)
523 wxASSERT_MSG(m_control
,
524 wxT("The wxGridCellEditor must be Created first!"));
525 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
528 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
533 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
535 // accept the simple key presses, not anything with Ctrl/Alt/Meta
536 return !(event
.ControlDown() || event
.AltDown());
539 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
544 void wxGridCellEditor::StartingClick()
550 // ----------------------------------------------------------------------------
551 // wxGridCellTextEditor
552 // ----------------------------------------------------------------------------
554 wxGridCellTextEditor::wxGridCellTextEditor()
559 void wxGridCellTextEditor::Create(wxWindow
* parent
,
561 wxEvtHandler
* evtHandler
)
563 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
564 wxDefaultPosition
, wxDefaultSize
565 #if defined(__WXMSW__)
566 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
567 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
571 // TODO: use m_maxChars
573 wxGridCellEditor::Create(parent
, id
, evtHandler
);
576 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
577 wxGridCellAttr
* WXUNUSED(attr
))
579 // as we fill the entire client area, don't do anything here to minimize
583 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
585 wxRect
rect(rectOrig
);
587 // Make the edit control large enough to allow for internal
590 // TODO: remove this if the text ctrl sizing is improved esp. for
593 #if defined(__WXGTK__)
602 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
604 // MB: treat MSW separately here otherwise the caret doesn't show
605 // when the editor is in the first row.
606 #if defined(__WXMSW__)
609 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
612 #if defined(__WXMOTIF__)
616 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
617 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
618 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
619 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
622 wxGridCellEditor::SetSize(rect
);
625 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
627 wxASSERT_MSG(m_control
,
628 wxT("The wxGridCellEditor must be Created first!"));
630 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
632 DoBeginEdit(m_startValue
);
635 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
637 Text()->SetValue(startValue
);
638 Text()->SetInsertionPointEnd();
639 Text()->SetSelection(-1,-1);
643 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
646 wxASSERT_MSG(m_control
,
647 wxT("The wxGridCellEditor must be Created first!"));
649 bool changed
= FALSE
;
650 wxString value
= Text()->GetValue();
651 if (value
!= m_startValue
)
655 grid
->GetTable()->SetValue(row
, col
, value
);
657 m_startValue
= wxEmptyString
;
658 Text()->SetValue(m_startValue
);
664 void wxGridCellTextEditor::Reset()
666 wxASSERT_MSG(m_control
,
667 wxT("The wxGridCellEditor must be Created first!"));
669 DoReset(m_startValue
);
672 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
674 Text()->SetValue(startValue
);
675 Text()->SetInsertionPointEnd();
678 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
680 if ( wxGridCellEditor::IsAcceptedKey(event
) )
682 int keycode
= event
.GetKeyCode();
696 case WXK_NUMPAD_MULTIPLY
:
700 case WXK_NUMPAD_SUBTRACT
:
702 case WXK_NUMPAD_DECIMAL
:
704 case WXK_NUMPAD_DIVIDE
:
708 // accept 8 bit chars too if isprint() agrees
709 if ( (keycode
< 255) && (isprint(keycode
)) )
717 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
719 if ( !Text()->EmulateKeyPress(event
) )
725 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
726 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
728 #if defined(__WXMOTIF__) || defined(__WXGTK__)
729 // wxMotif needs a little extra help...
730 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
731 wxString
s( Text()->GetValue() );
732 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
734 Text()->SetInsertionPoint( pos
);
736 // the other ports can handle a Return key press
742 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
752 if ( !params
.ToLong(&tmp
) )
754 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
758 m_maxChars
= (size_t)tmp
;
763 // return the value in the text control
764 wxString
wxGridCellTextEditor::GetValue() const
766 return Text()->GetValue();
769 // ----------------------------------------------------------------------------
770 // wxGridCellNumberEditor
771 // ----------------------------------------------------------------------------
773 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
779 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
781 wxEvtHandler
* evtHandler
)
785 // create a spin ctrl
786 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
787 wxDefaultPosition
, wxDefaultSize
,
791 wxGridCellEditor::Create(parent
, id
, evtHandler
);
795 // just a text control
796 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
799 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
800 #endif // wxUSE_VALIDATORS
804 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
806 // first get the value
807 wxGridTableBase
*table
= grid
->GetTable();
808 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
810 m_valueOld
= table
->GetValueAsLong(row
, col
);
815 wxString sValue
= table
->GetValue(row
, col
);
816 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
818 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
825 Spin()->SetValue((int)m_valueOld
);
830 DoBeginEdit(GetString());
834 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
843 value
= Spin()->GetValue();
844 changed
= value
!= m_valueOld
;
846 text
= wxString::Format(wxT("%ld"), value
);
850 text
= Text()->GetValue();
851 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
856 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
857 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
859 grid
->GetTable()->SetValue(row
, col
, text
);
865 void wxGridCellNumberEditor::Reset()
869 Spin()->SetValue((int)m_valueOld
);
873 DoReset(GetString());
877 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
879 if ( wxGridCellEditor::IsAcceptedKey(event
) )
881 int keycode
= event
.GetKeyCode();
897 case WXK_NUMPAD_SUBTRACT
:
903 if ( (keycode
< 128) && isdigit(keycode
) )
911 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
915 int keycode
= (int) event
.KeyCode();
916 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-'
917 || keycode
== WXK_NUMPAD0
918 || keycode
== WXK_NUMPAD1
919 || keycode
== WXK_NUMPAD2
920 || keycode
== WXK_NUMPAD3
921 || keycode
== WXK_NUMPAD4
922 || keycode
== WXK_NUMPAD5
923 || keycode
== WXK_NUMPAD6
924 || keycode
== WXK_NUMPAD7
925 || keycode
== WXK_NUMPAD8
926 || keycode
== WXK_NUMPAD9
927 || keycode
== WXK_ADD
928 || keycode
== WXK_NUMPAD_ADD
929 || keycode
== WXK_SUBTRACT
930 || keycode
== WXK_NUMPAD_SUBTRACT
)
932 wxGridCellTextEditor::StartingKey(event
);
942 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
953 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
957 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
961 // skip the error message below
966 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
970 // return the value in the spin control if it is there (the text control otherwise)
971 wxString
wxGridCellNumberEditor::GetValue() const
977 long value
= Spin()->GetValue();
978 s
.Printf(wxT("%ld"), value
);
982 s
= Text()->GetValue();
987 // ----------------------------------------------------------------------------
988 // wxGridCellFloatEditor
989 // ----------------------------------------------------------------------------
991 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
994 m_precision
= precision
;
997 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
999 wxEvtHandler
* evtHandler
)
1001 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1003 #if wxUSE_VALIDATORS
1004 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1005 #endif // wxUSE_VALIDATORS
1008 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1010 // first get the value
1011 wxGridTableBase
*table
= grid
->GetTable();
1012 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1014 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1019 wxString sValue
= table
->GetValue(row
, col
);
1020 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1022 wxFAIL_MSG( _T("this cell doesn't have float value") );
1027 DoBeginEdit(GetString());
1030 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1034 wxString
text(Text()->GetValue());
1036 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1038 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1039 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1041 grid
->GetTable()->SetValue(row
, col
, text
);
1048 void wxGridCellFloatEditor::Reset()
1050 DoReset(GetString());
1053 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1055 int keycode
= (int)event
.KeyCode();
1056 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1057 || keycode
== WXK_NUMPAD0
1058 || keycode
== WXK_NUMPAD1
1059 || keycode
== WXK_NUMPAD2
1060 || keycode
== WXK_NUMPAD3
1061 || keycode
== WXK_NUMPAD4
1062 || keycode
== WXK_NUMPAD5
1063 || keycode
== WXK_NUMPAD6
1064 || keycode
== WXK_NUMPAD7
1065 || keycode
== WXK_NUMPAD8
1066 || keycode
== WXK_NUMPAD9
1067 || keycode
== WXK_ADD
1068 || keycode
== WXK_NUMPAD_ADD
1069 || keycode
== WXK_SUBTRACT
1070 || keycode
== WXK_NUMPAD_SUBTRACT
)
1072 wxGridCellTextEditor::StartingKey(event
);
1074 // skip Skip() below
1081 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1092 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1096 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1098 m_precision
= (int)tmp
;
1100 // skip the error message below
1105 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1109 wxString
wxGridCellFloatEditor::GetString() const
1112 if ( m_width
== -1 )
1114 // default width/precision
1117 else if ( m_precision
== -1 )
1119 // default precision
1120 fmt
.Printf(_T("%%%d.f"), m_width
);
1124 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1127 return wxString::Format(fmt
, m_valueOld
);
1130 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1132 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1134 int keycode
= event
.GetKeyCode();
1148 case WXK_NUMPAD_ADD
:
1150 case WXK_NUMPAD_SUBTRACT
:
1152 case WXK_NUMPAD_DECIMAL
:
1156 // additionally accept 'e' as in '1e+6'
1157 if ( (keycode
< 128) &&
1158 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1166 #endif // wxUSE_TEXTCTRL
1170 // ----------------------------------------------------------------------------
1171 // wxGridCellBoolEditor
1172 // ----------------------------------------------------------------------------
1174 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1176 wxEvtHandler
* evtHandler
)
1178 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1179 wxDefaultPosition
, wxDefaultSize
,
1182 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1185 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1187 bool resize
= FALSE
;
1188 wxSize size
= m_control
->GetSize();
1189 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1191 // check if the checkbox is not too big/small for this cell
1192 wxSize sizeBest
= m_control
->GetBestSize();
1193 if ( !(size
== sizeBest
) )
1195 // reset to default size if it had been made smaller
1201 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1203 // leave 1 pixel margin
1204 size
.x
= size
.y
= minSize
- 2;
1211 m_control
->SetSize(size
);
1214 // position it in the centre of the rectangle (TODO: support alignment?)
1216 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1217 // the checkbox without label still has some space to the right in wxGTK,
1218 // so shift it to the right
1220 #elif defined(__WXMSW__)
1221 // here too, but in other way
1226 int hAlign
= wxALIGN_CENTRE
;
1227 int vAlign
= wxALIGN_CENTRE
;
1229 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1232 if (hAlign
== wxALIGN_LEFT
)
1238 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1240 else if (hAlign
== wxALIGN_RIGHT
)
1242 x
= r
.x
+ r
.width
- size
.x
- 2;
1243 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1245 else if (hAlign
== wxALIGN_CENTRE
)
1247 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1248 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1251 m_control
->Move(x
, y
);
1254 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1256 m_control
->Show(show
);
1260 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1261 CBox()->SetBackgroundColour(colBg
);
1265 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1267 wxASSERT_MSG(m_control
,
1268 wxT("The wxGridCellEditor must be Created first!"));
1270 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1271 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1274 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1275 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1277 CBox()->SetValue(m_startValue
);
1281 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1284 wxASSERT_MSG(m_control
,
1285 wxT("The wxGridCellEditor must be Created first!"));
1287 bool changed
= FALSE
;
1288 bool value
= CBox()->GetValue();
1289 if ( value
!= m_startValue
)
1294 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1295 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1297 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1303 void wxGridCellBoolEditor::Reset()
1305 wxASSERT_MSG(m_control
,
1306 wxT("The wxGridCellEditor must be Created first!"));
1308 CBox()->SetValue(m_startValue
);
1311 void wxGridCellBoolEditor::StartingClick()
1313 CBox()->SetValue(!CBox()->GetValue());
1316 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1318 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1320 int keycode
= event
.GetKeyCode();
1324 case WXK_NUMPAD_MULTIPLY
:
1326 case WXK_NUMPAD_ADD
:
1328 case WXK_NUMPAD_SUBTRACT
:
1339 // return the value as "1" for true and the empty string for false
1340 wxString
wxGridCellBoolEditor::GetValue() const
1342 bool bSet
= CBox()->GetValue();
1343 return bSet
? _T("1") : wxEmptyString
;
1346 #endif // wxUSE_CHECKBOX
1350 // ----------------------------------------------------------------------------
1351 // wxGridCellChoiceEditor
1352 // ----------------------------------------------------------------------------
1354 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1355 const wxString choices
[],
1357 : m_allowOthers(allowOthers
)
1361 m_choices
.Alloc(count
);
1362 for ( size_t n
= 0; n
< count
; n
++ )
1364 m_choices
.Add(choices
[n
]);
1369 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1371 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1372 editor
->m_allowOthers
= m_allowOthers
;
1373 editor
->m_choices
= m_choices
;
1378 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1380 wxEvtHandler
* evtHandler
)
1382 size_t count
= m_choices
.GetCount();
1383 wxString
*choices
= new wxString
[count
];
1384 for ( size_t n
= 0; n
< count
; n
++ )
1386 choices
[n
] = m_choices
[n
];
1389 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1390 wxDefaultPosition
, wxDefaultSize
,
1392 m_allowOthers
? 0 : wxCB_READONLY
);
1396 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1399 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1400 wxGridCellAttr
* attr
)
1402 // as we fill the entire client area, don't do anything here to minimize
1405 // TODO: It doesn't actually fill the client area since the height of a
1406 // combo always defaults to the standard... Until someone has time to
1407 // figure out the right rectangle to paint, just do it the normal way...
1408 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1411 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1413 wxASSERT_MSG(m_control
,
1414 wxT("The wxGridCellEditor must be Created first!"));
1416 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1419 Combo()->SetValue(m_startValue
);
1422 // find the right position, or default to the first if not found
1423 int pos
= Combo()->FindString(m_startValue
);
1426 Combo()->SetSelection(pos
);
1428 Combo()->SetInsertionPointEnd();
1429 Combo()->SetFocus();
1432 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1435 wxString value
= Combo()->GetValue();
1436 bool changed
= value
!= m_startValue
;
1439 grid
->GetTable()->SetValue(row
, col
, value
);
1441 m_startValue
= wxEmptyString
;
1443 Combo()->SetValue(m_startValue
);
1445 Combo()->SetSelection(0);
1450 void wxGridCellChoiceEditor::Reset()
1452 Combo()->SetValue(m_startValue
);
1453 Combo()->SetInsertionPointEnd();
1456 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1466 wxStringTokenizer
tk(params
, _T(','));
1467 while ( tk
.HasMoreTokens() )
1469 m_choices
.Add(tk
.GetNextToken());
1473 // return the value in the text control
1474 wxString
wxGridCellChoiceEditor::GetValue() const
1476 return Combo()->GetValue();
1479 #endif // wxUSE_COMBOBOX
1481 // ----------------------------------------------------------------------------
1482 // wxGridCellEditorEvtHandler
1483 // ----------------------------------------------------------------------------
1485 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1487 switch ( event
.KeyCode() )
1491 m_grid
->DisableCellEditControl();
1495 m_grid
->GetEventHandler()->ProcessEvent( event
);
1499 case WXK_NUMPAD_ENTER
:
1500 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1501 m_editor
->HandleReturn(event
);
1510 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1512 switch ( event
.KeyCode() )
1517 case WXK_NUMPAD_ENTER
:
1525 // ----------------------------------------------------------------------------
1526 // wxGridCellWorker is an (almost) empty common base class for
1527 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1528 // ----------------------------------------------------------------------------
1530 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1535 wxGridCellWorker::~wxGridCellWorker()
1539 // ============================================================================
1541 // ============================================================================
1543 // ----------------------------------------------------------------------------
1544 // wxGridCellRenderer
1545 // ----------------------------------------------------------------------------
1547 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1548 wxGridCellAttr
& attr
,
1551 int WXUNUSED(row
), int WXUNUSED(col
),
1554 dc
.SetBackgroundMode( wxSOLID
);
1556 // grey out fields if the grid is disabled
1557 if( grid
.IsEnabled() )
1561 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1565 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1570 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1573 dc
.SetPen( *wxTRANSPARENT_PEN
);
1574 dc
.DrawRectangle(rect
);
1577 // ----------------------------------------------------------------------------
1578 // wxGridCellStringRenderer
1579 // ----------------------------------------------------------------------------
1581 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1582 wxGridCellAttr
& attr
,
1586 dc
.SetBackgroundMode( wxTRANSPARENT
);
1588 // TODO some special colours for attr.IsReadOnly() case?
1590 // different coloured text when the grid is disabled
1591 if( grid
.IsEnabled() )
1595 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1596 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1600 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1601 dc
.SetTextForeground( attr
.GetTextColour() );
1606 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1607 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1610 dc
.SetFont( attr
.GetFont() );
1613 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1615 const wxString
& text
)
1617 wxCoord x
= 0, y
= 0, max_x
= 0;
1618 dc
.SetFont(attr
.GetFont());
1619 wxStringTokenizer
tk(text
, _T('\n'));
1620 while ( tk
.HasMoreTokens() )
1622 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1623 max_x
= wxMax(max_x
, x
);
1626 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1628 return wxSize(max_x
, y
);
1631 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1632 wxGridCellAttr
& attr
,
1636 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1639 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1640 wxGridCellAttr
& attr
,
1642 const wxRect
& rectCell
,
1646 wxRect rect
= rectCell
;
1649 // erase only this cells background, overflow cells should have been erased
1650 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1653 attr
.GetAlignment(&hAlign
, &vAlign
);
1655 int overflowCols
= 0;
1657 if (attr
.GetOverflow())
1659 int cols
= grid
.GetNumberCols();
1660 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1661 int cell_rows
, cell_cols
;
1662 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1663 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1665 int i
, c_cols
, c_rows
;
1666 for (i
= col
+cell_cols
; i
< cols
; i
++)
1668 bool is_empty
= TRUE
;
1669 for (int j
=row
; j
<row
+cell_rows
; j
++)
1671 // check w/ anchor cell for multicell block
1672 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1673 if (c_rows
> 0) c_rows
= 0;
1674 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1681 rect
.width
+= grid
.GetColSize(i
);
1687 if (rect
.width
>= best_width
) break;
1689 overflowCols
= i
- col
- cell_cols
+ 1;
1690 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1693 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1695 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1697 clip
.x
+= rectCell
.width
;
1698 // draw each overflow cell individually
1699 int col_end
= col
+cell_cols
+overflowCols
;
1700 if (col_end
>= grid
.GetNumberCols())
1701 col_end
= grid
.GetNumberCols() - 1;
1702 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1704 clip
.width
= grid
.GetColSize(i
) - 1;
1705 dc
.DestroyClippingRegion();
1706 dc
.SetClippingRegion(clip
);
1708 SetTextColoursAndFont(grid
, attr
, dc
,
1709 grid
.IsInSelection(row
,i
));
1711 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1712 rect
, hAlign
, vAlign
);
1713 clip
.x
+= grid
.GetColSize(i
) - 1;
1719 dc
.DestroyClippingRegion();
1723 // now we only have to draw the text
1724 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1726 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1727 rect
, hAlign
, vAlign
);
1730 // ----------------------------------------------------------------------------
1731 // wxGridCellNumberRenderer
1732 // ----------------------------------------------------------------------------
1734 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1736 wxGridTableBase
*table
= grid
.GetTable();
1738 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1740 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1744 text
= table
->GetValue(row
, col
);
1750 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1751 wxGridCellAttr
& attr
,
1753 const wxRect
& rectCell
,
1757 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1759 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1761 // draw the text right aligned by default
1763 attr
.GetAlignment(&hAlign
, &vAlign
);
1764 hAlign
= wxALIGN_RIGHT
;
1766 wxRect rect
= rectCell
;
1769 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1772 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1773 wxGridCellAttr
& attr
,
1777 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1780 // ----------------------------------------------------------------------------
1781 // wxGridCellFloatRenderer
1782 // ----------------------------------------------------------------------------
1784 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1787 SetPrecision(precision
);
1790 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1792 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1793 renderer
->m_width
= m_width
;
1794 renderer
->m_precision
= m_precision
;
1795 renderer
->m_format
= m_format
;
1800 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1802 wxGridTableBase
*table
= grid
.GetTable();
1807 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1809 val
= table
->GetValueAsDouble(row
, col
);
1814 text
= table
->GetValue(row
, col
);
1815 hasDouble
= text
.ToDouble(&val
);
1822 if ( m_width
== -1 )
1824 if ( m_precision
== -1 )
1826 // default width/precision
1827 m_format
= _T("%f");
1831 m_format
.Printf(_T("%%.%df"), m_precision
);
1834 else if ( m_precision
== -1 )
1836 // default precision
1837 m_format
.Printf(_T("%%%d.f"), m_width
);
1841 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1845 text
.Printf(m_format
, val
);
1848 //else: text already contains the string
1853 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1854 wxGridCellAttr
& attr
,
1856 const wxRect
& rectCell
,
1860 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1862 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1864 // draw the text right aligned by default
1866 attr
.GetAlignment(&hAlign
, &vAlign
);
1867 hAlign
= wxALIGN_RIGHT
;
1869 wxRect rect
= rectCell
;
1872 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1875 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1876 wxGridCellAttr
& attr
,
1880 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1883 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1887 // reset to defaults
1893 wxString tmp
= params
.BeforeFirst(_T(','));
1897 if ( tmp
.ToLong(&width
) )
1899 SetWidth((int)width
);
1903 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1907 tmp
= params
.AfterFirst(_T(','));
1911 if ( tmp
.ToLong(&precision
) )
1913 SetPrecision((int)precision
);
1917 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1925 // ----------------------------------------------------------------------------
1926 // wxGridCellBoolRenderer
1927 // ----------------------------------------------------------------------------
1929 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1931 // FIXME these checkbox size calculations are really ugly...
1933 // between checkmark and box
1934 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1936 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1937 wxGridCellAttr
& WXUNUSED(attr
),
1942 // compute it only once (no locks for MT safeness in GUI thread...)
1943 if ( !ms_sizeCheckMark
.x
)
1945 // get checkbox size
1946 wxCoord checkSize
= 0;
1947 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1948 wxSize size
= checkbox
->GetBestSize();
1949 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1951 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1952 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1953 checkSize
-= size
.y
/ 2;
1958 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1961 return ms_sizeCheckMark
;
1964 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1965 wxGridCellAttr
& attr
,
1971 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1973 // draw a check mark in the centre (ignoring alignment - TODO)
1974 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1976 // don't draw outside the cell
1977 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1978 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1980 // and even leave (at least) 1 pixel margin
1981 size
.x
= size
.y
= minSize
- 2;
1984 // draw a border around checkmark
1986 attr
.GetAlignment(& hAlign
, &vAlign
);
1989 if (hAlign
== wxALIGN_CENTRE
)
1991 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1992 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1993 rectBorder
.width
= size
.x
;
1994 rectBorder
.height
= size
.y
;
1996 else if (hAlign
== wxALIGN_LEFT
)
1998 rectBorder
.x
= rect
.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_RIGHT
)
2005 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2006 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2007 rectBorder
.width
= size
.x
;
2008 rectBorder
.height
= size
.y
;
2012 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2013 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2016 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2017 value
= !( !cellval
|| (cellval
== wxT("0")) );
2022 wxRect rectMark
= rectBorder
;
2024 // MSW DrawCheckMark() is weird (and should probably be changed...)
2025 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2029 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2032 dc
.SetTextForeground(attr
.GetTextColour());
2033 dc
.DrawCheckMark(rectMark
);
2036 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2037 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2038 dc
.DrawRectangle(rectBorder
);
2041 // ----------------------------------------------------------------------------
2043 // ----------------------------------------------------------------------------
2045 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2049 m_isReadOnly
= Unset
;
2054 m_attrkind
= wxGridCellAttr::Cell
;
2056 m_sizeRows
= m_sizeCols
= 1;
2059 SetDefAttr(attrDefault
);
2062 wxGridCellAttr
*wxGridCellAttr::Clone() const
2064 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2066 if ( HasTextColour() )
2067 attr
->SetTextColour(GetTextColour());
2068 if ( HasBackgroundColour() )
2069 attr
->SetBackgroundColour(GetBackgroundColour());
2071 attr
->SetFont(GetFont());
2072 if ( HasAlignment() )
2073 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2075 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2079 attr
->SetRenderer(m_renderer
);
2080 m_renderer
->IncRef();
2084 attr
->SetEditor(m_editor
);
2089 attr
->SetReadOnly();
2091 attr
->SetKind( m_attrkind
);
2096 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2098 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2099 SetTextColour(mergefrom
->GetTextColour());
2100 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2101 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2102 if ( !HasFont() && mergefrom
->HasFont() )
2103 SetFont(mergefrom
->GetFont());
2104 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2106 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2107 SetAlignment(hAlign
, vAlign
);
2110 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2112 // Directly access member functions as GetRender/Editor don't just return
2113 // m_renderer/m_editor
2115 // Maybe add support for merge of Render and Editor?
2116 if (!HasRenderer() && mergefrom
->HasRenderer() )
2118 m_renderer
= mergefrom
->m_renderer
;
2119 m_renderer
->IncRef();
2121 if ( !HasEditor() && mergefrom
->HasEditor() )
2123 m_editor
= mergefrom
->m_editor
;
2126 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2127 SetReadOnly(mergefrom
->IsReadOnly());
2129 SetDefAttr(mergefrom
->m_defGridAttr
);
2132 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2134 // The size of a cell is normally 1,1
2136 // If this cell is larger (2,2) then this is the top left cell
2137 // the other cells that will be covered (lower right cells) must be
2138 // set to negative or zero values such that
2139 // row + num_rows of the covered cell points to the larger cell (this cell)
2140 // same goes for the col + num_cols.
2142 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2144 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2145 !((num_rows
<=0)&&(num_cols
>0)) ||
2146 !((num_rows
==0)&&(num_cols
==0))),
2147 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2149 m_sizeRows
= num_rows
;
2150 m_sizeCols
= num_cols
;
2153 const wxColour
& wxGridCellAttr::GetTextColour() const
2155 if (HasTextColour())
2159 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2161 return m_defGridAttr
->GetTextColour();
2165 wxFAIL_MSG(wxT("Missing default cell attribute"));
2166 return wxNullColour
;
2171 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2173 if (HasBackgroundColour())
2175 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2176 return m_defGridAttr
->GetBackgroundColour();
2179 wxFAIL_MSG(wxT("Missing default cell attribute"));
2180 return wxNullColour
;
2185 const wxFont
& wxGridCellAttr::GetFont() const
2189 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2190 return m_defGridAttr
->GetFont();
2193 wxFAIL_MSG(wxT("Missing default cell attribute"));
2199 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2203 if ( hAlign
) *hAlign
= m_hAlign
;
2204 if ( vAlign
) *vAlign
= m_vAlign
;
2206 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2207 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2210 wxFAIL_MSG(wxT("Missing default cell attribute"));
2214 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2216 if ( num_rows
) *num_rows
= m_sizeRows
;
2217 if ( num_cols
) *num_cols
= m_sizeCols
;
2220 // GetRenderer and GetEditor use a slightly different decision path about
2221 // which attribute to use. If a non-default attr object has one then it is
2222 // used, otherwise the default editor or renderer is fetched from the grid and
2223 // used. It should be the default for the data type of the cell. If it is
2224 // NULL (because the table has a type that the grid does not have in its
2225 // registry,) then the grid's default editor or renderer is used.
2227 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2229 wxGridCellRenderer
*renderer
;
2231 if ( m_renderer
&& this != m_defGridAttr
)
2233 // use the cells renderer if it has one
2234 renderer
= m_renderer
;
2237 else // no non default cell renderer
2239 // get default renderer for the data type
2242 // GetDefaultRendererForCell() will do IncRef() for us
2243 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2252 if (m_defGridAttr
&& this != m_defGridAttr
)
2254 // if we still don't have one then use the grid default
2255 // (no need for IncRef() here neither)
2256 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2258 else // default grid attr
2260 // use m_renderer which we had decided not to use initially
2261 renderer
= m_renderer
;
2268 // we're supposed to always find something
2269 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2274 // same as above, except for s/renderer/editor/g
2275 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2277 wxGridCellEditor
*editor
;
2279 if ( m_editor
&& this != m_defGridAttr
)
2281 // use the cells editor if it has one
2285 else // no non default cell editor
2287 // get default editor for the data type
2290 // GetDefaultEditorForCell() will do IncRef() for us
2291 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2300 if ( m_defGridAttr
&& this != m_defGridAttr
)
2302 // if we still don't have one then use the grid default
2303 // (no need for IncRef() here neither)
2304 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2306 else // default grid attr
2308 // use m_editor which we had decided not to use initially
2316 // we're supposed to always find something
2317 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2322 // ----------------------------------------------------------------------------
2323 // wxGridCellAttrData
2324 // ----------------------------------------------------------------------------
2326 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2328 int n
= FindIndex(row
, col
);
2329 if ( n
== wxNOT_FOUND
)
2331 // add the attribute
2332 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2336 // free the old attribute
2337 m_attrs
[(size_t)n
].attr
->DecRef();
2341 // change the attribute
2342 m_attrs
[(size_t)n
].attr
= attr
;
2346 // remove this attribute
2347 m_attrs
.RemoveAt((size_t)n
);
2352 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2354 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2356 int n
= FindIndex(row
, col
);
2357 if ( n
!= wxNOT_FOUND
)
2359 attr
= m_attrs
[(size_t)n
].attr
;
2366 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2368 size_t count
= m_attrs
.GetCount();
2369 for ( size_t n
= 0; n
< count
; n
++ )
2371 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2372 wxCoord row
= coords
.GetRow();
2373 if ((size_t)row
>= pos
)
2377 // If rows inserted, include row counter where necessary
2378 coords
.SetRow(row
+ numRows
);
2380 else if (numRows
< 0)
2382 // If rows deleted ...
2383 if ((size_t)row
>= pos
- numRows
)
2385 // ...either decrement row counter (if row still exists)...
2386 coords
.SetRow(row
+ numRows
);
2390 // ...or remove the attribute
2391 m_attrs
.RemoveAt((size_t)n
);
2399 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2401 size_t count
= m_attrs
.GetCount();
2402 for ( size_t n
= 0; n
< count
; n
++ )
2404 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2405 wxCoord col
= coords
.GetCol();
2406 if ( (size_t)col
>= pos
)
2410 // If rows inserted, include row counter where necessary
2411 coords
.SetCol(col
+ numCols
);
2413 else if (numCols
< 0)
2415 // If rows deleted ...
2416 if ((size_t)col
>= pos
- numCols
)
2418 // ...either decrement row counter (if row still exists)...
2419 coords
.SetCol(col
+ numCols
);
2423 // ...or remove the attribute
2424 m_attrs
.RemoveAt((size_t)n
);
2432 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2434 size_t count
= m_attrs
.GetCount();
2435 for ( size_t n
= 0; n
< count
; n
++ )
2437 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2438 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2447 // ----------------------------------------------------------------------------
2448 // wxGridRowOrColAttrData
2449 // ----------------------------------------------------------------------------
2451 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2453 size_t count
= m_attrs
.Count();
2454 for ( size_t n
= 0; n
< count
; n
++ )
2456 m_attrs
[n
]->DecRef();
2460 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2462 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2464 int n
= m_rowsOrCols
.Index(rowOrCol
);
2465 if ( n
!= wxNOT_FOUND
)
2467 attr
= m_attrs
[(size_t)n
];
2474 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2476 int i
= m_rowsOrCols
.Index(rowOrCol
);
2477 if ( i
== wxNOT_FOUND
)
2479 // add the attribute
2480 m_rowsOrCols
.Add(rowOrCol
);
2485 size_t n
= (size_t)i
;
2488 // change the attribute
2489 m_attrs
[n
]->DecRef();
2494 // remove this attribute
2495 m_attrs
[n
]->DecRef();
2496 m_rowsOrCols
.RemoveAt(n
);
2497 m_attrs
.RemoveAt(n
);
2502 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2504 size_t count
= m_attrs
.GetCount();
2505 for ( size_t n
= 0; n
< count
; n
++ )
2507 int & rowOrCol
= m_rowsOrCols
[n
];
2508 if ( (size_t)rowOrCol
>= pos
)
2510 if ( numRowsOrCols
> 0 )
2512 // If rows inserted, include row counter where necessary
2513 rowOrCol
+= numRowsOrCols
;
2515 else if ( numRowsOrCols
< 0)
2517 // If rows deleted, either decrement row counter (if row still exists)
2518 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2519 rowOrCol
+= numRowsOrCols
;
2522 m_rowsOrCols
.RemoveAt((size_t)n
);
2523 m_attrs
.RemoveAt((size_t)n
);
2531 // ----------------------------------------------------------------------------
2532 // wxGridCellAttrProvider
2533 // ----------------------------------------------------------------------------
2535 wxGridCellAttrProvider::wxGridCellAttrProvider()
2537 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2540 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2545 void wxGridCellAttrProvider::InitData()
2547 m_data
= new wxGridCellAttrProviderData
;
2550 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2551 wxGridCellAttr::wxAttrKind kind
) const
2553 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2558 case (wxGridCellAttr::Any
):
2559 //Get cached merge attributes.
2560 // Currenlty not used as no cache implemented as not mutiable
2561 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2564 //Basicaly implement old version.
2565 //Also check merge cache, so we don't have to re-merge every time..
2566 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2567 *attrrow
= (wxGridCellAttr
*)NULL
,
2568 *attrcol
= (wxGridCellAttr
*)NULL
;
2570 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2571 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2572 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2574 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2575 // Two or move are non NULL
2576 attr
= new wxGridCellAttr
;
2577 attr
->SetKind(wxGridCellAttr::Merged
);
2581 attr
->MergeWith(attrcell
);
2585 attr
->MergeWith(attrcol
);
2589 attr
->MergeWith(attrrow
);
2592 //store merge attr if cache implemented
2594 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2598 // one or none is non null return it or null.
2599 if(attrrow
) attr
= attrrow
;
2600 if(attrcol
) attr
= attrcol
;
2601 if(attrcell
) attr
= attrcell
;
2605 case (wxGridCellAttr::Cell
):
2606 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2608 case (wxGridCellAttr::Col
):
2609 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2611 case (wxGridCellAttr::Row
):
2612 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2616 // (wxGridCellAttr::Default):
2617 // (wxGridCellAttr::Merged):
2624 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2630 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2633 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2638 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2641 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2646 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2649 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2653 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2655 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2659 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2663 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2665 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2669 // ----------------------------------------------------------------------------
2670 // wxGridTypeRegistry
2671 // ----------------------------------------------------------------------------
2673 wxGridTypeRegistry::~wxGridTypeRegistry()
2675 size_t count
= m_typeinfo
.Count();
2676 for ( size_t i
= 0; i
< count
; i
++ )
2677 delete m_typeinfo
[i
];
2681 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2682 wxGridCellRenderer
* renderer
,
2683 wxGridCellEditor
* editor
)
2685 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2687 // is it already registered?
2688 int loc
= FindRegisteredDataType(typeName
);
2689 if ( loc
!= wxNOT_FOUND
)
2691 delete m_typeinfo
[loc
];
2692 m_typeinfo
[loc
] = info
;
2696 m_typeinfo
.Add(info
);
2700 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2702 size_t count
= m_typeinfo
.GetCount();
2703 for ( size_t i
= 0; i
< count
; i
++ )
2705 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2714 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2716 int index
= FindRegisteredDataType(typeName
);
2717 if ( index
== wxNOT_FOUND
)
2719 // check whether this is one of the standard ones, in which case
2720 // register it "on the fly"
2722 if ( typeName
== wxGRID_VALUE_STRING
)
2724 RegisterDataType(wxGRID_VALUE_STRING
,
2725 new wxGridCellStringRenderer
,
2726 new wxGridCellTextEditor
);
2728 #endif // wxUSE_TEXTCTRL
2730 if ( typeName
== wxGRID_VALUE_BOOL
)
2732 RegisterDataType(wxGRID_VALUE_BOOL
,
2733 new wxGridCellBoolRenderer
,
2734 new wxGridCellBoolEditor
);
2736 #endif // wxUSE_CHECKBOX
2738 if ( typeName
== wxGRID_VALUE_NUMBER
)
2740 RegisterDataType(wxGRID_VALUE_NUMBER
,
2741 new wxGridCellNumberRenderer
,
2742 new wxGridCellNumberEditor
);
2744 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2746 RegisterDataType(wxGRID_VALUE_FLOAT
,
2747 new wxGridCellFloatRenderer
,
2748 new wxGridCellFloatEditor
);
2750 #endif // wxUSE_TEXTCTRL
2752 if ( typeName
== wxGRID_VALUE_CHOICE
)
2754 RegisterDataType(wxGRID_VALUE_CHOICE
,
2755 new wxGridCellStringRenderer
,
2756 new wxGridCellChoiceEditor
);
2758 #endif // wxUSE_COMBOBOX
2763 // we get here only if just added the entry for this type, so return
2765 index
= m_typeinfo
.GetCount() - 1;
2771 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2773 int index
= FindDataType(typeName
);
2774 if ( index
== wxNOT_FOUND
)
2776 // the first part of the typename is the "real" type, anything after ':'
2777 // are the parameters for the renderer
2778 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2779 if ( index
== wxNOT_FOUND
)
2784 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2785 wxGridCellRenderer
*rendererOld
= renderer
;
2786 renderer
= renderer
->Clone();
2787 rendererOld
->DecRef();
2789 wxGridCellEditor
*editor
= GetEditor(index
);
2790 wxGridCellEditor
*editorOld
= editor
;
2791 editor
= editor
->Clone();
2792 editorOld
->DecRef();
2794 // do it even if there are no parameters to reset them to defaults
2795 wxString params
= typeName
.AfterFirst(_T(':'));
2796 renderer
->SetParameters(params
);
2797 editor
->SetParameters(params
);
2799 // register the new typename
2800 RegisterDataType(typeName
, renderer
, editor
);
2802 // we just registered it, it's the last one
2803 index
= m_typeinfo
.GetCount() - 1;
2809 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2811 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2817 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2819 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2825 // ----------------------------------------------------------------------------
2827 // ----------------------------------------------------------------------------
2829 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2832 wxGridTableBase::wxGridTableBase()
2834 m_view
= (wxGrid
*) NULL
;
2835 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2838 wxGridTableBase::~wxGridTableBase()
2840 delete m_attrProvider
;
2843 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2845 delete m_attrProvider
;
2846 m_attrProvider
= attrProvider
;
2849 bool wxGridTableBase::CanHaveAttributes()
2851 if ( ! GetAttrProvider() )
2853 // use the default attr provider by default
2854 SetAttrProvider(new wxGridCellAttrProvider
);
2859 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2861 if ( m_attrProvider
)
2862 return m_attrProvider
->GetAttr(row
, col
, kind
);
2864 return (wxGridCellAttr
*)NULL
;
2867 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2869 if ( m_attrProvider
)
2871 attr
->SetKind(wxGridCellAttr::Cell
);
2872 m_attrProvider
->SetAttr(attr
, row
, col
);
2876 // as we take ownership of the pointer and don't store it, we must
2882 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2884 if ( m_attrProvider
)
2886 attr
->SetKind(wxGridCellAttr::Row
);
2887 m_attrProvider
->SetRowAttr(attr
, row
);
2891 // as we take ownership of the pointer and don't store it, we must
2897 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2899 if ( m_attrProvider
)
2901 attr
->SetKind(wxGridCellAttr::Col
);
2902 m_attrProvider
->SetColAttr(attr
, col
);
2906 // as we take ownership of the pointer and don't store it, we must
2912 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2913 size_t WXUNUSED(numRows
) )
2915 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2920 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2922 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2927 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2928 size_t WXUNUSED(numRows
) )
2930 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2935 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2936 size_t WXUNUSED(numCols
) )
2938 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2943 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2945 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2950 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2951 size_t WXUNUSED(numCols
) )
2953 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2959 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2962 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2963 // how much it makes sense to us geeks.
2967 wxString
wxGridTableBase::GetColLabelValue( int col
)
2969 // default col labels are:
2970 // cols 0 to 25 : A-Z
2971 // cols 26 to 675 : AA-ZZ
2976 for ( n
= 1; ; n
++ )
2978 s
+= (_T('A') + (wxChar
)( col%26
));
2980 if ( col
< 0 ) break;
2983 // reverse the string...
2985 for ( i
= 0; i
< n
; i
++ )
2994 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2996 return wxGRID_VALUE_STRING
;
2999 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3000 const wxString
& typeName
)
3002 return typeName
== wxGRID_VALUE_STRING
;
3005 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3007 return CanGetValueAs(row
, col
, typeName
);
3010 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3015 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3020 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3025 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3026 long WXUNUSED(value
) )
3030 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3031 double WXUNUSED(value
) )
3035 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3036 bool WXUNUSED(value
) )
3041 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3042 const wxString
& WXUNUSED(typeName
) )
3047 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3048 const wxString
& WXUNUSED(typeName
),
3049 void* WXUNUSED(value
) )
3053 //////////////////////////////////////////////////////////////////////
3055 // Message class for the grid table to send requests and notifications
3059 wxGridTableMessage::wxGridTableMessage()
3061 m_table
= (wxGridTableBase
*) NULL
;
3067 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3068 int commandInt1
, int commandInt2
)
3072 m_comInt1
= commandInt1
;
3073 m_comInt2
= commandInt2
;
3078 //////////////////////////////////////////////////////////////////////
3080 // A basic grid table for string data. An object of this class will
3081 // created by wxGrid if you don't specify an alternative table class.
3084 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3086 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3088 wxGridStringTable::wxGridStringTable()
3093 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3096 m_data
.Alloc( numRows
);
3099 sa
.Alloc( numCols
);
3100 sa
.Add( wxEmptyString
, numCols
);
3102 m_data
.Add( sa
, numRows
);
3105 wxGridStringTable::~wxGridStringTable()
3109 int wxGridStringTable::GetNumberRows()
3111 return m_data
.GetCount();
3114 int wxGridStringTable::GetNumberCols()
3116 if ( m_data
.GetCount() > 0 )
3117 return m_data
[0].GetCount();
3122 wxString
wxGridStringTable::GetValue( int row
, int col
)
3124 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3125 _T("invalid row or column index in wxGridStringTable") );
3127 return m_data
[row
][col
];
3130 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3132 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3133 _T("invalid row or column index in wxGridStringTable") );
3135 m_data
[row
][col
] = value
;
3138 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3140 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3141 _T("invalid row or column index in wxGridStringTable") );
3143 return (m_data
[row
][col
] == wxEmptyString
);
3146 void wxGridStringTable::Clear()
3149 int numRows
, numCols
;
3151 numRows
= m_data
.GetCount();
3154 numCols
= m_data
[0].GetCount();
3156 for ( row
= 0; row
< numRows
; row
++ )
3158 for ( col
= 0; col
< numCols
; col
++ )
3160 m_data
[row
][col
] = wxEmptyString
;
3167 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3169 size_t curNumRows
= m_data
.GetCount();
3170 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3171 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3173 if ( pos
>= curNumRows
)
3175 return AppendRows( numRows
);
3179 sa
.Alloc( curNumCols
);
3180 sa
.Add( wxEmptyString
, curNumCols
);
3181 m_data
.Insert( sa
, pos
, numRows
);
3184 wxGridTableMessage
msg( this,
3185 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3189 GetView()->ProcessTableMessage( msg
);
3195 bool wxGridStringTable::AppendRows( size_t numRows
)
3197 size_t curNumRows
= m_data
.GetCount();
3198 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3199 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3202 if ( curNumCols
> 0 )
3204 sa
.Alloc( curNumCols
);
3205 sa
.Add( wxEmptyString
, curNumCols
);
3208 m_data
.Add( sa
, numRows
);
3212 wxGridTableMessage
msg( this,
3213 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3216 GetView()->ProcessTableMessage( msg
);
3222 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3224 size_t curNumRows
= m_data
.GetCount();
3226 if ( pos
>= curNumRows
)
3228 wxFAIL_MSG( wxString::Format
3230 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3232 (unsigned long)numRows
,
3233 (unsigned long)curNumRows
3239 if ( numRows
> curNumRows
- pos
)
3241 numRows
= curNumRows
- pos
;
3244 if ( numRows
>= curNumRows
)
3250 m_data
.RemoveAt( pos
, numRows
);
3254 wxGridTableMessage
msg( this,
3255 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3259 GetView()->ProcessTableMessage( msg
);
3265 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3269 size_t curNumRows
= m_data
.GetCount();
3270 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3271 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3273 if ( pos
>= curNumCols
)
3275 return AppendCols( numCols
);
3278 for ( row
= 0; row
< curNumRows
; row
++ )
3280 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3282 m_data
[row
].Insert( wxEmptyString
, col
);
3287 wxGridTableMessage
msg( this,
3288 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3292 GetView()->ProcessTableMessage( msg
);
3298 bool wxGridStringTable::AppendCols( size_t numCols
)
3302 size_t curNumRows
= m_data
.GetCount();
3306 // TODO: something better than this ?
3308 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3313 for ( row
= 0; row
< curNumRows
; row
++ )
3315 m_data
[row
].Add( wxEmptyString
, numCols
);
3320 wxGridTableMessage
msg( this,
3321 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3324 GetView()->ProcessTableMessage( msg
);
3330 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3334 size_t curNumRows
= m_data
.GetCount();
3335 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3336 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3338 if ( pos
>= curNumCols
)
3340 wxFAIL_MSG( wxString::Format
3342 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3344 (unsigned long)numCols
,
3345 (unsigned long)curNumCols
3350 if ( numCols
> curNumCols
- pos
)
3352 numCols
= curNumCols
- pos
;
3355 for ( row
= 0; row
< curNumRows
; row
++ )
3357 if ( numCols
>= curNumCols
)
3359 m_data
[row
].Clear();
3363 m_data
[row
].RemoveAt( pos
, numCols
);
3368 wxGridTableMessage
msg( this,
3369 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3373 GetView()->ProcessTableMessage( msg
);
3379 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3381 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3383 // using default label
3385 return wxGridTableBase::GetRowLabelValue( row
);
3389 return m_rowLabels
[ row
];
3393 wxString
wxGridStringTable::GetColLabelValue( int col
)
3395 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3397 // using default label
3399 return wxGridTableBase::GetColLabelValue( col
);
3403 return m_colLabels
[ col
];
3407 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3409 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3411 int n
= m_rowLabels
.GetCount();
3413 for ( i
= n
; i
<= row
; i
++ )
3415 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3419 m_rowLabels
[row
] = value
;
3422 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3424 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3426 int n
= m_colLabels
.GetCount();
3428 for ( i
= n
; i
<= col
; i
++ )
3430 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3434 m_colLabels
[col
] = value
;
3439 //////////////////////////////////////////////////////////////////////
3440 //////////////////////////////////////////////////////////////////////
3442 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3444 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3445 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3446 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3447 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3448 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3449 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3452 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3454 const wxPoint
&pos
, const wxSize
&size
)
3455 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3460 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3464 // NO - don't do this because it will set both the x and y origin
3465 // coords to match the parent scrolled window and we just want to
3466 // set the y coord - MB
3468 // m_owner->PrepareDC( dc );
3471 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3472 dc
.SetDeviceOrigin( 0, -y
);
3474 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3475 m_owner
->DrawRowLabels( dc
, rows
);
3479 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3481 m_owner
->ProcessRowLabelMouseEvent( event
);
3485 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3487 m_owner
->GetEventHandler()->ProcessEvent(event
);
3491 // This seems to be required for wxMotif otherwise the mouse
3492 // cursor must be in the cell edit control to get key events
3494 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3496 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3499 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3501 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3506 //////////////////////////////////////////////////////////////////////
3508 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3510 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3511 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3512 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3513 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3514 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3515 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3518 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3520 const wxPoint
&pos
, const wxSize
&size
)
3521 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3526 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3530 // NO - don't do this because it will set both the x and y origin
3531 // coords to match the parent scrolled window and we just want to
3532 // set the x coord - MB
3534 // m_owner->PrepareDC( dc );
3537 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3538 dc
.SetDeviceOrigin( -x
, 0 );
3540 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3541 m_owner
->DrawColLabels( dc
, cols
);
3545 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3547 m_owner
->ProcessColLabelMouseEvent( event
);
3550 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3552 m_owner
->GetEventHandler()->ProcessEvent(event
);
3556 // This seems to be required for wxMotif otherwise the mouse
3557 // cursor must be in the cell edit control to get key events
3559 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3561 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3564 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3566 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3571 //////////////////////////////////////////////////////////////////////
3573 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3575 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3576 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3577 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3578 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3579 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3580 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3583 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3585 const wxPoint
&pos
, const wxSize
&size
)
3586 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3591 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3595 int client_height
= 0;
3596 int client_width
= 0;
3597 GetClientSize( &client_width
, &client_height
);
3599 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3600 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3601 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3602 dc
.DrawLine( 0, 0, client_width
, 0 );
3603 dc
.DrawLine( 0, 0, 0, client_height
);
3605 dc
.SetPen( *wxWHITE_PEN
);
3606 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3607 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3611 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3613 m_owner
->ProcessCornerLabelMouseEvent( event
);
3617 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3619 m_owner
->GetEventHandler()->ProcessEvent(event
);
3622 // This seems to be required for wxMotif otherwise the mouse
3623 // cursor must be in the cell edit control to get key events
3625 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3627 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3630 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3632 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3637 //////////////////////////////////////////////////////////////////////
3639 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3641 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3642 EVT_PAINT( wxGridWindow::OnPaint
)
3643 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3644 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3645 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3646 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3647 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3650 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3651 wxGridRowLabelWindow
*rowLblWin
,
3652 wxGridColLabelWindow
*colLblWin
,
3655 const wxSize
&size
)
3656 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3657 wxT("grid window") )
3661 m_rowLabelWin
= rowLblWin
;
3662 m_colLabelWin
= colLblWin
;
3663 SetBackgroundColour(_T("WHITE"));
3667 wxGridWindow::~wxGridWindow()
3672 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3674 wxPaintDC
dc( this );
3675 m_owner
->PrepareDC( dc
);
3676 wxRegion reg
= GetUpdateRegion();
3677 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3678 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3679 #if WXGRID_DRAW_LINES
3680 m_owner
->DrawAllGridLines( dc
, reg
);
3682 m_owner
->DrawGridSpace( dc
);
3683 m_owner
->DrawHighlight( dc
, DirtyCells
);
3687 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3689 wxWindow::ScrollWindow( dx
, dy
, rect
);
3690 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3691 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3695 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3697 m_owner
->ProcessGridCellMouseEvent( event
);
3700 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3702 m_owner
->GetEventHandler()->ProcessEvent(event
);
3705 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3706 // cursor must be in the cell edit control to get key events
3708 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3710 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3713 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3715 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3718 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3723 //////////////////////////////////////////////////////////////////////
3725 // Internal Helper function for computing row or column from some
3726 // (unscrolled) coordinate value, using either
3727 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3728 // of m_rowBottoms/m_ColRights to speed up the search!
3730 // Internal helper macros for simpler use of that function
3732 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3733 const wxArrayInt
& BorderArray
, int nMax
,
3734 bool maxOnOverflow
);
3736 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3737 WXGRID_MIN_COL_WIDTH, \
3738 m_colRights, m_numCols, TRUE)
3739 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3740 WXGRID_MIN_ROW_HEIGHT, \
3741 m_rowBottoms, m_numRows, TRUE)
3742 /////////////////////////////////////////////////////////////////////
3744 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3746 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3747 EVT_PAINT( wxGrid::OnPaint
)
3748 EVT_SIZE( wxGrid::OnSize
)
3749 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3750 EVT_KEY_UP( wxGrid::OnKeyUp
)
3751 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3754 wxGrid::wxGrid( wxWindow
*parent
,
3759 const wxString
& name
)
3760 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3761 m_colMinWidths(GRID_HASH_SIZE
),
3762 m_rowMinHeights(GRID_HASH_SIZE
)
3770 // Must do this or ~wxScrollHelper will pop the wrong event handler
3771 SetTargetWindow(this);
3773 wxSafeDecRef(m_defaultCellAttr
);
3775 #ifdef DEBUG_ATTR_CACHE
3776 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3777 wxPrintf(_T("wxGrid attribute cache statistics: "
3778 "total: %u, hits: %u (%u%%)\n"),
3779 total
, gs_nAttrCacheHits
,
3780 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3786 delete m_typeRegistry
;
3792 // ----- internal init and update functions
3795 void wxGrid::Create()
3797 m_created
= FALSE
; // set to TRUE by CreateGrid
3799 m_table
= (wxGridTableBase
*) NULL
;
3802 m_cellEditCtrlEnabled
= FALSE
;
3804 m_defaultCellAttr
= new wxGridCellAttr();
3806 // Set default cell attributes
3807 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3808 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3809 m_defaultCellAttr
->SetFont(GetFont());
3810 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3811 m_defaultCellAttr
->SetTextColour(
3812 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3813 m_defaultCellAttr
->SetBackgroundColour(
3814 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3815 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3816 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3821 m_currentCellCoords
= wxGridNoCellCoords
;
3823 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3824 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3826 // create the type registry
3827 m_typeRegistry
= new wxGridTypeRegistry
;
3830 // subwindow components that make up the wxGrid
3831 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3836 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3841 m_colLabelWin
= new wxGridColLabelWindow( this,
3846 m_gridWin
= new wxGridWindow( this,
3853 SetTargetWindow( m_gridWin
);
3859 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3860 wxGrid::wxGridSelectionModes selmode
)
3862 wxCHECK_MSG( !m_created
,
3864 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3866 m_numRows
= numRows
;
3867 m_numCols
= numCols
;
3869 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3870 m_table
->SetView( this );
3872 m_selection
= new wxGridSelection( this, selmode
);
3881 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3883 wxCHECK_RET( m_created
,
3884 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3886 m_selection
->SetSelectionMode( selmode
);
3889 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3891 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3892 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3894 return m_selection
->GetSelectionMode();
3897 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3898 wxGrid::wxGridSelectionModes selmode
)
3902 // RD: Actually, this should probably be allowed. I think it would be
3903 // nice to be able to switch multiple Tables in and out of a single
3904 // View at runtime. Is there anything in the implementation that
3905 // would prevent this?
3907 // At least, you now have to cope with m_selection
3908 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3913 m_numRows
= table
->GetNumberRows();
3914 m_numCols
= table
->GetNumberCols();
3917 m_table
->SetView( this );
3920 m_selection
= new wxGridSelection( this, selmode
);
3933 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3934 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3936 if ( m_rowLabelWin
)
3938 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3942 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3945 m_labelTextColour
= wxColour( _T("BLACK") );
3948 m_attrCache
.row
= -1;
3949 m_attrCache
.col
= -1;
3950 m_attrCache
.attr
= NULL
;
3952 // TODO: something better than this ?
3954 m_labelFont
= this->GetFont();
3955 // m_labelFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
3956 // m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
3958 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
3959 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3961 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3962 m_colLabelVertAlign
= wxALIGN_CENTRE
;
3964 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3965 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3967 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3968 m_defaultRowHeight
+= 8;
3970 m_defaultRowHeight
+= 4;
3973 m_gridLineColour
= wxColour( 192,192,192 );
3974 m_gridLinesEnabled
= TRUE
;
3975 m_cellHighlightColour
= *wxBLACK
;
3976 m_cellHighlightPenWidth
= 2;
3977 m_cellHighlightROPenWidth
= 1;
3979 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3980 m_winCapture
= (wxWindow
*)NULL
;
3981 m_canDragRowSize
= TRUE
;
3982 m_canDragColSize
= TRUE
;
3983 m_canDragGridSize
= TRUE
;
3985 m_dragRowOrCol
= -1;
3986 m_isDragging
= FALSE
;
3987 m_startDragPos
= wxDefaultPosition
;
3989 m_waitForSlowClick
= FALSE
;
3991 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3992 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3994 m_currentCellCoords
= wxGridNoCellCoords
;
3996 m_selectingTopLeft
= wxGridNoCellCoords
;
3997 m_selectingBottomRight
= wxGridNoCellCoords
;
3998 // m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
3999 // m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
4000 m_selectionBackground
= *wxBLACK
;
4001 m_selectionForeground
= *wxWHITE
;
4003 m_editable
= TRUE
; // default for whole grid
4005 m_inOnKeyDown
= FALSE
;
4012 // ----------------------------------------------------------------------------
4013 // the idea is to call these functions only when necessary because they create
4014 // quite big arrays which eat memory mostly unnecessary - in particular, if
4015 // default widths/heights are used for all rows/columns, we may not use these
4018 // with some extra code, it should be possible to only store the
4019 // widths/heights different from default ones but this will be done later...
4020 // ----------------------------------------------------------------------------
4022 void wxGrid::InitRowHeights()
4024 m_rowHeights
.Empty();
4025 m_rowBottoms
.Empty();
4027 m_rowHeights
.Alloc( m_numRows
);
4028 m_rowBottoms
.Alloc( m_numRows
);
4032 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4034 for ( int i
= 0; i
< m_numRows
; i
++ )
4036 rowBottom
+= m_defaultRowHeight
;
4037 m_rowBottoms
.Add( rowBottom
);
4041 void wxGrid::InitColWidths()
4043 m_colWidths
.Empty();
4044 m_colRights
.Empty();
4046 m_colWidths
.Alloc( m_numCols
);
4047 m_colRights
.Alloc( m_numCols
);
4050 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4052 for ( int i
= 0; i
< m_numCols
; i
++ )
4054 colRight
+= m_defaultColWidth
;
4055 m_colRights
.Add( colRight
);
4059 int wxGrid::GetColWidth(int col
) const
4061 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4064 int wxGrid::GetColLeft(int col
) const
4066 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4067 : m_colRights
[col
] - m_colWidths
[col
];
4070 int wxGrid::GetColRight(int col
) const
4072 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4076 int wxGrid::GetRowHeight(int row
) const
4078 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4081 int wxGrid::GetRowTop(int row
) const
4083 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4084 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4087 int wxGrid::GetRowBottom(int row
) const
4089 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4090 : m_rowBottoms
[row
];
4093 void wxGrid::CalcDimensions()
4096 GetClientSize( &cw
, &ch
);
4098 if ( m_rowLabelWin
->IsShown() )
4099 cw
-= m_rowLabelWidth
;
4100 if ( m_colLabelWin
->IsShown() )
4101 ch
-= m_colLabelHeight
;
4104 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4105 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4107 // take into account editor if shown
4108 if( IsCellEditControlShown() )
4111 int r
= m_currentCellCoords
.GetRow();
4112 int c
= m_currentCellCoords
.GetCol();
4113 int x
= GetColLeft(c
);
4114 int y
= GetRowTop(r
);
4116 // how big is the editor
4117 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4118 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4119 editor
->GetControl()->GetSize(&w2
, &h2
);
4122 if( w2
> w
) w
= w2
;
4123 if( h2
> h
) h
= h2
;
4128 // preserve (more or less) the previous position
4130 GetViewStart( &x
, &y
);
4132 // maybe we don't need scrollbars at all?
4134 // also adjust the position to be valid for the new scroll rangs
4155 // do set scrollbar parameters
4156 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4157 GetScrollX(w
), GetScrollY(h
), x
, y
,
4158 GetBatchCount() != 0);
4160 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4161 // still must reposition the children
4166 void wxGrid::CalcWindowSizes()
4169 GetClientSize( &cw
, &ch
);
4171 if ( m_cornerLabelWin
->IsShown() )
4172 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4174 if ( m_colLabelWin
->IsShown() )
4175 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4177 if ( m_rowLabelWin
->IsShown() )
4178 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4180 if ( m_gridWin
->IsShown() )
4181 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4185 // this is called when the grid table sends a message to say that it
4186 // has been redimensioned
4188 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4191 bool result
= FALSE
;
4193 // Clear the attribute cache as the attribute might refer to a different
4194 // cell than stored in the cache after adding/removing rows/columns.
4196 // By the same reasoning, the editor should be dismissed if columns are
4197 // added or removed. And for consistency, it should IMHO always be
4198 // removed, not only if the cell "underneath" it actually changes.
4199 // For now, I intentionally do not save the editor's content as the
4200 // cell it might want to save that stuff to might no longer exist.
4201 HideCellEditControl();
4203 // if we were using the default widths/heights so far, we must change them
4205 if ( m_colWidths
.IsEmpty() )
4210 if ( m_rowHeights
.IsEmpty() )
4216 switch ( msg
.GetId() )
4218 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4220 size_t pos
= msg
.GetCommandInt();
4221 int numRows
= msg
.GetCommandInt2();
4223 m_numRows
+= numRows
;
4225 if ( !m_rowHeights
.IsEmpty() )
4227 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4228 m_rowBottoms
.Insert( 0, pos
, numRows
);
4231 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4233 for ( i
= pos
; i
< m_numRows
; i
++ )
4235 bottom
+= m_rowHeights
[i
];
4236 m_rowBottoms
[i
] = bottom
;
4239 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4241 // if we have just inserted cols into an empty grid the current
4242 // cell will be undefined...
4244 SetCurrentCell( 0, 0 );
4248 m_selection
->UpdateRows( pos
, numRows
);
4249 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4251 attrProvider
->UpdateAttrRows( pos
, numRows
);
4253 if ( !GetBatchCount() )
4256 m_rowLabelWin
->Refresh();
4262 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4264 int numRows
= msg
.GetCommandInt();
4265 int oldNumRows
= m_numRows
;
4266 m_numRows
+= numRows
;
4268 if ( !m_rowHeights
.IsEmpty() )
4270 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4271 m_rowBottoms
.Add( 0, numRows
);
4274 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4276 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4278 bottom
+= m_rowHeights
[i
];
4279 m_rowBottoms
[i
] = bottom
;
4282 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4284 // if we have just inserted cols into an empty grid the current
4285 // cell will be undefined...
4287 SetCurrentCell( 0, 0 );
4289 if ( !GetBatchCount() )
4292 m_rowLabelWin
->Refresh();
4298 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4300 size_t pos
= msg
.GetCommandInt();
4301 int numRows
= msg
.GetCommandInt2();
4302 m_numRows
-= numRows
;
4304 if ( !m_rowHeights
.IsEmpty() )
4306 m_rowHeights
.RemoveAt( pos
, numRows
);
4307 m_rowBottoms
.RemoveAt( pos
, numRows
);
4310 for ( i
= 0; i
< m_numRows
; i
++ )
4312 h
+= m_rowHeights
[i
];
4313 m_rowBottoms
[i
] = h
;
4318 m_currentCellCoords
= wxGridNoCellCoords
;
4322 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4323 m_currentCellCoords
.Set( 0, 0 );
4327 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4328 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4330 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4331 // ifdef'd out following patch from Paul Gammans
4333 // No need to touch column attributes, unless we
4334 // removed _all_ rows, in this case, we remove
4335 // all column attributes.
4336 // I hate to do this here, but the
4337 // needed data is not available inside UpdateAttrRows.
4338 if ( !GetNumberRows() )
4339 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4342 if ( !GetBatchCount() )
4345 m_rowLabelWin
->Refresh();
4351 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4353 size_t pos
= msg
.GetCommandInt();
4354 int numCols
= msg
.GetCommandInt2();
4355 m_numCols
+= numCols
;
4357 if ( !m_colWidths
.IsEmpty() )
4359 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4360 m_colRights
.Insert( 0, pos
, numCols
);
4363 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4365 for ( i
= pos
; i
< m_numCols
; i
++ )
4367 right
+= m_colWidths
[i
];
4368 m_colRights
[i
] = right
;
4371 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4373 // if we have just inserted cols into an empty grid the current
4374 // cell will be undefined...
4376 SetCurrentCell( 0, 0 );
4380 m_selection
->UpdateCols( pos
, numCols
);
4381 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4383 attrProvider
->UpdateAttrCols( pos
, numCols
);
4384 if ( !GetBatchCount() )
4387 m_colLabelWin
->Refresh();
4394 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4396 int numCols
= msg
.GetCommandInt();
4397 int oldNumCols
= m_numCols
;
4398 m_numCols
+= numCols
;
4399 if ( !m_colWidths
.IsEmpty() )
4401 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4402 m_colRights
.Add( 0, numCols
);
4405 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4407 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4409 right
+= m_colWidths
[i
];
4410 m_colRights
[i
] = right
;
4413 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4415 // if we have just inserted cols into an empty grid the current
4416 // cell will be undefined...
4418 SetCurrentCell( 0, 0 );
4420 if ( !GetBatchCount() )
4423 m_colLabelWin
->Refresh();
4429 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4431 size_t pos
= msg
.GetCommandInt();
4432 int numCols
= msg
.GetCommandInt2();
4433 m_numCols
-= numCols
;
4435 if ( !m_colWidths
.IsEmpty() )
4437 m_colWidths
.RemoveAt( pos
, numCols
);
4438 m_colRights
.RemoveAt( pos
, numCols
);
4441 for ( i
= 0; i
< m_numCols
; i
++ )
4443 w
+= m_colWidths
[i
];
4449 m_currentCellCoords
= wxGridNoCellCoords
;
4453 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4454 m_currentCellCoords
.Set( 0, 0 );
4458 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4459 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4461 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4462 // ifdef'd out following patch from Paul Gammans
4464 // No need to touch row attributes, unless we
4465 // removed _all_ columns, in this case, we remove
4466 // all row attributes.
4467 // I hate to do this here, but the
4468 // needed data is not available inside UpdateAttrCols.
4469 if ( !GetNumberCols() )
4470 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4473 if ( !GetBatchCount() )
4476 m_colLabelWin
->Refresh();
4483 if (result
&& !GetBatchCount() )
4484 m_gridWin
->Refresh();
4489 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4491 wxRegionIterator
iter( reg
);
4494 wxArrayInt rowlabels
;
4501 // TODO: remove this when we can...
4502 // There is a bug in wxMotif that gives garbage update
4503 // rectangles if you jump-scroll a long way by clicking the
4504 // scrollbar with middle button. This is a work-around
4506 #if defined(__WXMOTIF__)
4508 m_gridWin
->GetClientSize( &cw
, &ch
);
4509 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4510 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4513 // logical bounds of update region
4516 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4517 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4519 // find the row labels within these bounds
4522 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4524 if ( GetRowBottom(row
) < top
)
4527 if ( GetRowTop(row
) > bottom
)
4530 rowlabels
.Add( row
);
4540 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4542 wxRegionIterator
iter( reg
);
4545 wxArrayInt colLabels
;
4552 // TODO: remove this when we can...
4553 // There is a bug in wxMotif that gives garbage update
4554 // rectangles if you jump-scroll a long way by clicking the
4555 // scrollbar with middle button. This is a work-around
4557 #if defined(__WXMOTIF__)
4559 m_gridWin
->GetClientSize( &cw
, &ch
);
4560 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4561 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4564 // logical bounds of update region
4567 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4568 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4570 // find the cells within these bounds
4573 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4575 if ( GetColRight(col
) < left
)
4578 if ( GetColLeft(col
) > right
)
4581 colLabels
.Add( col
);
4590 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4592 wxRegionIterator
iter( reg
);
4595 wxGridCellCoordsArray cellsExposed
;
4597 int left
, top
, right
, bottom
;
4602 // TODO: remove this when we can...
4603 // There is a bug in wxMotif that gives garbage update
4604 // rectangles if you jump-scroll a long way by clicking the
4605 // scrollbar with middle button. This is a work-around
4607 #if defined(__WXMOTIF__)
4609 m_gridWin
->GetClientSize( &cw
, &ch
);
4610 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4611 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4612 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4613 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4616 // logical bounds of update region
4618 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4619 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4621 // find the cells within these bounds
4624 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4626 if ( GetRowBottom(row
) <= top
)
4629 if ( GetRowTop(row
) > bottom
)
4632 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4634 if ( GetColRight(col
) <= left
)
4637 if ( GetColLeft(col
) > right
)
4640 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4647 return cellsExposed
;
4651 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4654 wxPoint
pos( event
.GetPosition() );
4655 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4657 if ( event
.Dragging() )
4661 m_isDragging
= TRUE
;
4662 m_rowLabelWin
->CaptureMouse();
4665 if ( event
.LeftIsDown() )
4667 switch( m_cursorMode
)
4669 case WXGRID_CURSOR_RESIZE_ROW
:
4671 int cw
, ch
, left
, dummy
;
4672 m_gridWin
->GetClientSize( &cw
, &ch
);
4673 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4675 wxClientDC
dc( m_gridWin
);
4678 GetRowTop(m_dragRowOrCol
) +
4679 GetRowMinimalHeight(m_dragRowOrCol
) );
4680 dc
.SetLogicalFunction(wxINVERT
);
4681 if ( m_dragLastPos
>= 0 )
4683 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4685 dc
.DrawLine( left
, y
, left
+cw
, y
);
4690 case WXGRID_CURSOR_SELECT_ROW
:
4691 if ( (row
= YToRow( y
)) >= 0 )
4695 m_selection
->SelectRow( row
,
4696 event
.ControlDown(),
4703 // default label to suppress warnings about "enumeration value
4704 // 'xxx' not handled in switch
4712 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4717 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4718 m_isDragging
= FALSE
;
4721 // ------------ Entering or leaving the window
4723 if ( event
.Entering() || event
.Leaving() )
4725 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4729 // ------------ Left button pressed
4731 else if ( event
.LeftDown() )
4733 // don't send a label click event for a hit on the
4734 // edge of the row label - this is probably the user
4735 // wanting to resize the row
4737 if ( YToEdgeOfRow(y
) < 0 )
4741 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4743 if ( !event
.ShiftDown() && !event
.ControlDown() )
4747 if ( event
.ShiftDown() )
4749 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4752 GetNumberCols() - 1,
4753 event
.ControlDown(),
4760 m_selection
->SelectRow( row
,
4761 event
.ControlDown(),
4768 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4773 // starting to drag-resize a row
4775 if ( CanDragRowSize() )
4776 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4781 // ------------ Left double click
4783 else if (event
.LeftDClick() )
4785 if ( YToEdgeOfRow(y
) < 0 )
4788 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4793 // ------------ Left button released
4795 else if ( event
.LeftUp() )
4797 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4799 DoEndDragResizeRow();
4801 // Note: we are ending the event *after* doing
4802 // default processing in this case
4804 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4807 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4812 // ------------ Right button down
4814 else if ( event
.RightDown() )
4817 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4819 // no default action at the moment
4824 // ------------ Right double click
4826 else if ( event
.RightDClick() )
4829 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4831 // no default action at the moment
4836 // ------------ No buttons down and mouse moving
4838 else if ( event
.Moving() )
4840 m_dragRowOrCol
= YToEdgeOfRow( y
);
4841 if ( m_dragRowOrCol
>= 0 )
4843 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4845 // don't capture the mouse yet
4846 if ( CanDragRowSize() )
4847 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4850 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4852 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4858 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4861 wxPoint
pos( event
.GetPosition() );
4862 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4864 if ( event
.Dragging() )
4868 m_isDragging
= TRUE
;
4869 m_colLabelWin
->CaptureMouse();
4872 if ( event
.LeftIsDown() )
4874 switch( m_cursorMode
)
4876 case WXGRID_CURSOR_RESIZE_COL
:
4878 int cw
, ch
, dummy
, top
;
4879 m_gridWin
->GetClientSize( &cw
, &ch
);
4880 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4882 wxClientDC
dc( m_gridWin
);
4885 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4886 GetColMinimalWidth(m_dragRowOrCol
));
4887 dc
.SetLogicalFunction(wxINVERT
);
4888 if ( m_dragLastPos
>= 0 )
4890 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4892 dc
.DrawLine( x
, top
, x
, top
+ch
);
4897 case WXGRID_CURSOR_SELECT_COL
:
4898 if ( (col
= XToCol( x
)) >= 0 )
4902 m_selection
->SelectCol( col
,
4903 event
.ControlDown(),
4910 // default label to suppress warnings about "enumeration value
4911 // 'xxx' not handled in switch
4919 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4924 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4925 m_isDragging
= FALSE
;
4928 // ------------ Entering or leaving the window
4930 if ( event
.Entering() || event
.Leaving() )
4932 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4936 // ------------ Left button pressed
4938 else if ( event
.LeftDown() )
4940 // don't send a label click event for a hit on the
4941 // edge of the col label - this is probably the user
4942 // wanting to resize the col
4944 if ( XToEdgeOfCol(x
) < 0 )
4948 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4950 if ( !event
.ShiftDown() && !event
.ControlDown() )
4954 if ( event
.ShiftDown() )
4956 m_selection
->SelectBlock( 0,
4957 m_currentCellCoords
.GetCol(),
4958 GetNumberRows() - 1, col
,
4959 event
.ControlDown(),
4966 m_selection
->SelectCol( col
,
4967 event
.ControlDown(),
4974 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4979 // starting to drag-resize a col
4981 if ( CanDragColSize() )
4982 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4987 // ------------ Left double click
4989 if ( event
.LeftDClick() )
4991 if ( XToEdgeOfCol(x
) < 0 )
4994 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4999 // ------------ Left button released
5001 else if ( event
.LeftUp() )
5003 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5005 DoEndDragResizeCol();
5007 // Note: we are ending the event *after* doing
5008 // default processing in this case
5010 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5013 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5018 // ------------ Right button down
5020 else if ( event
.RightDown() )
5023 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5025 // no default action at the moment
5030 // ------------ Right double click
5032 else if ( event
.RightDClick() )
5035 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5037 // no default action at the moment
5042 // ------------ No buttons down and mouse moving
5044 else if ( event
.Moving() )
5046 m_dragRowOrCol
= XToEdgeOfCol( x
);
5047 if ( m_dragRowOrCol
>= 0 )
5049 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5051 // don't capture the cursor yet
5052 if ( CanDragColSize() )
5053 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
5056 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5058 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
5064 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5066 if ( event
.LeftDown() )
5068 // indicate corner label by having both row and
5071 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5077 else if ( event
.LeftDClick() )
5079 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5082 else if ( event
.RightDown() )
5084 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5086 // no default action at the moment
5090 else if ( event
.RightDClick() )
5092 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5094 // no default action at the moment
5099 void wxGrid::ChangeCursorMode(CursorMode mode
,
5104 static const wxChar
*cursorModes
[] =
5113 wxLogTrace(_T("grid"),
5114 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5115 win
== m_colLabelWin
? _T("colLabelWin")
5116 : win
? _T("rowLabelWin")
5118 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5119 #endif // __WXDEBUG__
5121 if ( mode
== m_cursorMode
&&
5122 win
== m_winCapture
&&
5123 captureMouse
== (m_winCapture
!= NULL
))
5128 // by default use the grid itself
5134 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5135 m_winCapture
= (wxWindow
*)NULL
;
5138 m_cursorMode
= mode
;
5140 switch ( m_cursorMode
)
5142 case WXGRID_CURSOR_RESIZE_ROW
:
5143 win
->SetCursor( m_rowResizeCursor
);
5146 case WXGRID_CURSOR_RESIZE_COL
:
5147 win
->SetCursor( m_colResizeCursor
);
5151 win
->SetCursor( *wxSTANDARD_CURSOR
);
5154 // we need to capture mouse when resizing
5155 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5156 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5158 if ( captureMouse
&& resize
)
5160 win
->CaptureMouse();
5165 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5168 wxPoint
pos( event
.GetPosition() );
5169 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5171 wxGridCellCoords coords
;
5172 XYToCell( x
, y
, coords
);
5174 int cell_rows
, cell_cols
;
5175 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5176 if ((cell_rows
< 0) || (cell_cols
< 0))
5178 coords
.SetRow(coords
.GetRow() + cell_rows
);
5179 coords
.SetCol(coords
.GetCol() + cell_cols
);
5182 if ( event
.Dragging() )
5184 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5186 // Don't start doing anything until the mouse has been drug at
5187 // least 3 pixels in any direction...
5190 if (m_startDragPos
== wxDefaultPosition
)
5192 m_startDragPos
= pos
;
5195 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5199 m_isDragging
= TRUE
;
5200 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5202 // Hide the edit control, so it
5203 // won't interfer with drag-shrinking.
5204 if ( IsCellEditControlShown() )
5206 HideCellEditControl();
5207 SaveEditControlValue();
5210 // Have we captured the mouse yet?
5213 m_winCapture
= m_gridWin
;
5214 m_winCapture
->CaptureMouse();
5217 if ( coords
!= wxGridNoCellCoords
)
5219 if ( event
.ControlDown() )
5221 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5222 m_selectingKeyboard
= coords
;
5223 HighlightBlock ( m_selectingKeyboard
, coords
);
5227 if ( !IsSelection() )
5229 HighlightBlock( coords
, coords
);
5233 HighlightBlock( m_currentCellCoords
, coords
);
5237 if (! IsVisible(coords
))
5239 MakeCellVisible(coords
);
5240 // TODO: need to introduce a delay or something here. The
5241 // scrolling is way to fast, at least on MSW - also on GTK.
5245 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5247 int cw
, ch
, left
, dummy
;
5248 m_gridWin
->GetClientSize( &cw
, &ch
);
5249 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5251 wxClientDC
dc( m_gridWin
);
5253 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5254 GetRowMinimalHeight(m_dragRowOrCol
) );
5255 dc
.SetLogicalFunction(wxINVERT
);
5256 if ( m_dragLastPos
>= 0 )
5258 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5260 dc
.DrawLine( left
, y
, left
+cw
, y
);
5263 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5265 int cw
, ch
, dummy
, top
;
5266 m_gridWin
->GetClientSize( &cw
, &ch
);
5267 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5269 wxClientDC
dc( m_gridWin
);
5271 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5272 GetColMinimalWidth(m_dragRowOrCol
) );
5273 dc
.SetLogicalFunction(wxINVERT
);
5274 if ( m_dragLastPos
>= 0 )
5276 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5278 dc
.DrawLine( x
, top
, x
, top
+ch
);
5285 m_isDragging
= FALSE
;
5286 m_startDragPos
= wxDefaultPosition
;
5288 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5289 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5292 if ( event
.Entering() || event
.Leaving() )
5294 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5295 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5300 // ------------ Left button pressed
5302 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5304 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5309 if ( !event
.ControlDown() )
5311 if ( event
.ShiftDown() )
5315 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5316 m_currentCellCoords
.GetCol(),
5319 event
.ControlDown(),
5325 else if ( XToEdgeOfCol(x
) < 0 &&
5326 YToEdgeOfRow(y
) < 0 )
5328 DisableCellEditControl();
5329 MakeCellVisible( coords
);
5331 if ( event
.ControlDown() )
5335 m_selection
->ToggleCellSelection( coords
.GetRow(),
5337 event
.ControlDown(),
5342 m_selectingTopLeft
= wxGridNoCellCoords
;
5343 m_selectingBottomRight
= wxGridNoCellCoords
;
5344 m_selectingKeyboard
= coords
;
5348 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5349 SetCurrentCell( coords
);
5352 if ( m_selection
->GetSelectionMode() !=
5353 wxGrid::wxGridSelectCells
)
5355 HighlightBlock( coords
, coords
);
5364 // ------------ Left double click
5366 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5368 DisableCellEditControl();
5370 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5372 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5380 // ------------ Left button released
5382 else if ( event
.LeftUp() )
5384 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5386 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5387 m_selectingBottomRight
!= wxGridNoCellCoords
)
5391 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5392 m_winCapture
= NULL
;
5397 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5398 m_selectingTopLeft
.GetCol(),
5399 m_selectingBottomRight
.GetRow(),
5400 m_selectingBottomRight
.GetCol(),
5401 event
.ControlDown(),
5407 m_selectingTopLeft
= wxGridNoCellCoords
;
5408 m_selectingBottomRight
= wxGridNoCellCoords
;
5410 // Show the edit control, if it has been hidden for
5412 ShowCellEditControl();
5416 if( m_waitForSlowClick
&& CanEnableCellControl())
5418 EnableCellEditControl();
5420 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5421 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5422 editor
->StartingClick();
5426 m_waitForSlowClick
= FALSE
;
5430 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5432 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5433 DoEndDragResizeRow();
5435 // Note: we are ending the event *after* doing
5436 // default processing in this case
5438 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5440 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5442 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5443 DoEndDragResizeCol();
5445 // Note: we are ending the event *after* doing
5446 // default processing in this case
5448 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5455 // ------------ Right button down
5457 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5459 DisableCellEditControl();
5460 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5465 // no default action at the moment
5470 // ------------ Right double click
5472 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5474 DisableCellEditControl();
5475 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5480 // no default action at the moment
5484 // ------------ Moving and no button action
5486 else if ( event
.Moving() && !event
.IsButton() )
5488 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5490 // out of grid cell area
5491 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5495 int dragRow
= YToEdgeOfRow( y
);
5496 int dragCol
= XToEdgeOfCol( x
);
5498 // Dragging on the corner of a cell to resize in both
5499 // directions is not implemented yet...
5501 if ( dragRow
>= 0 && dragCol
>= 0 )
5503 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5509 m_dragRowOrCol
= dragRow
;
5511 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5513 if ( CanDragRowSize() && CanDragGridSize() )
5514 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5519 m_dragRowOrCol
= dragCol
;
5527 m_dragRowOrCol
= dragCol
;
5529 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5531 if ( CanDragColSize() && CanDragGridSize() )
5532 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5538 // Neither on a row or col edge
5540 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5542 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5548 void wxGrid::DoEndDragResizeRow()
5550 if ( m_dragLastPos
>= 0 )
5552 // erase the last line and resize the row
5554 int cw
, ch
, left
, dummy
;
5555 m_gridWin
->GetClientSize( &cw
, &ch
);
5556 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5558 wxClientDC
dc( m_gridWin
);
5560 dc
.SetLogicalFunction( wxINVERT
);
5561 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5562 HideCellEditControl();
5563 SaveEditControlValue();
5565 int rowTop
= GetRowTop(m_dragRowOrCol
);
5566 SetRowSize( m_dragRowOrCol
,
5567 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5569 if ( !GetBatchCount() )
5571 // Only needed to get the correct rect.y:
5572 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5574 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5575 rect
.width
= m_rowLabelWidth
;
5576 rect
.height
= ch
- rect
.y
;
5577 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5579 // if there is a multicell block, paint all of it
5582 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5583 int leftCol
= XToCol(left
);
5584 int rightCol
= XToCol(left
+cw
);
5587 if (rightCol
< 0) rightCol
= m_numCols
;
5588 for (i
=leftCol
; i
<rightCol
; i
++)
5590 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5591 if (cell_rows
< subtract_rows
)
5592 subtract_rows
= cell_rows
;
5594 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5595 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5596 rect
.height
= ch
- rect
.y
;
5599 m_gridWin
->Refresh( FALSE
, &rect
);
5602 ShowCellEditControl();
5607 void wxGrid::DoEndDragResizeCol()
5609 if ( m_dragLastPos
>= 0 )
5611 // erase the last line and resize the col
5613 int cw
, ch
, dummy
, top
;
5614 m_gridWin
->GetClientSize( &cw
, &ch
);
5615 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5617 wxClientDC
dc( m_gridWin
);
5619 dc
.SetLogicalFunction( wxINVERT
);
5620 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5621 HideCellEditControl();
5622 SaveEditControlValue();
5624 int colLeft
= GetColLeft(m_dragRowOrCol
);
5625 SetColSize( m_dragRowOrCol
,
5626 wxMax( m_dragLastPos
- colLeft
,
5627 GetColMinimalWidth(m_dragRowOrCol
) ) );
5629 if ( !GetBatchCount() )
5631 // Only needed to get the correct rect.x:
5632 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5634 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5635 rect
.width
= cw
- rect
.x
;
5636 rect
.height
= m_colLabelHeight
;
5637 m_colLabelWin
->Refresh( TRUE
, &rect
);
5639 // if there is a multicell block, paint all of it
5642 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5643 int topRow
= YToRow(top
);
5644 int bottomRow
= YToRow(top
+cw
);
5647 if (bottomRow
< 0) bottomRow
= m_numRows
;
5648 for (i
=topRow
; i
<bottomRow
; i
++)
5650 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5651 if (cell_cols
< subtract_cols
)
5652 subtract_cols
= cell_cols
;
5654 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5655 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5656 rect
.width
= cw
- rect
.x
;
5659 m_gridWin
->Refresh( FALSE
, &rect
);
5662 ShowCellEditControl();
5669 // ------ interaction with data model
5671 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5673 switch ( msg
.GetId() )
5675 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5676 return GetModelValues();
5678 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5679 return SetModelValues();
5681 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5682 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5683 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5684 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5685 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5686 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5687 return Redimension( msg
);
5696 // The behaviour of this function depends on the grid table class
5697 // Clear() function. For the default wxGridStringTable class the
5698 // behavious is to replace all cell contents with wxEmptyString but
5699 // not to change the number of rows or cols.
5701 void wxGrid::ClearGrid()
5705 if (IsCellEditControlEnabled())
5706 DisableCellEditControl();
5709 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5714 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5716 // TODO: something with updateLabels flag
5720 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5726 if (IsCellEditControlEnabled())
5727 DisableCellEditControl();
5729 return m_table
->InsertRows( pos
, numRows
);
5731 // the table will have sent the results of the insert row
5732 // operation to this view object as a grid table message
5738 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5740 // TODO: something with updateLabels flag
5744 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5748 return ( m_table
&& m_table
->AppendRows( numRows
) );
5749 // the table will have sent the results of the append row
5750 // operation to this view object as a grid table message
5754 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5756 // TODO: something with updateLabels flag
5760 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5766 if (IsCellEditControlEnabled())
5767 DisableCellEditControl();
5769 return (m_table
->DeleteRows( pos
, numRows
));
5770 // the table will have sent the results of the delete row
5771 // operation to this view object as a grid table message
5777 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5779 // TODO: something with updateLabels flag
5783 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5789 if (IsCellEditControlEnabled())
5790 DisableCellEditControl();
5792 return m_table
->InsertCols( pos
, numCols
);
5793 // the table will have sent the results of the insert col
5794 // operation to this view object as a grid table message
5800 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5802 // TODO: something with updateLabels flag
5806 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5810 return ( m_table
&& m_table
->AppendCols( numCols
) );
5811 // the table will have sent the results of the append col
5812 // operation to this view object as a grid table message
5816 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5818 // TODO: something with updateLabels flag
5822 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5828 if (IsCellEditControlEnabled())
5829 DisableCellEditControl();
5831 return ( m_table
->DeleteCols( pos
, numCols
) );
5832 // the table will have sent the results of the delete col
5833 // operation to this view object as a grid table message
5841 // ----- event handlers
5844 // Generate a grid event based on a mouse event and
5845 // return the result of ProcessEvent()
5847 int wxGrid::SendEvent( const wxEventType type
,
5849 wxMouseEvent
& mouseEv
)
5854 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5856 int rowOrCol
= (row
== -1 ? col
: row
);
5858 wxGridSizeEvent
gridEvt( GetId(),
5862 mouseEv
.GetX() + GetRowLabelSize(),
5863 mouseEv
.GetY() + GetColLabelSize(),
5864 mouseEv
.ControlDown(),
5865 mouseEv
.ShiftDown(),
5867 mouseEv
.MetaDown() );
5869 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5870 vetoed
= !gridEvt
.IsAllowed();
5872 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5874 // Right now, it should _never_ end up here!
5875 wxGridRangeSelectEvent
gridEvt( GetId(),
5879 m_selectingBottomRight
,
5881 mouseEv
.ControlDown(),
5882 mouseEv
.ShiftDown(),
5884 mouseEv
.MetaDown() );
5886 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5887 vetoed
= !gridEvt
.IsAllowed();
5891 wxGridEvent
gridEvt( GetId(),
5895 mouseEv
.GetX() + GetRowLabelSize(),
5896 mouseEv
.GetY() + GetColLabelSize(),
5898 mouseEv
.ControlDown(),
5899 mouseEv
.ShiftDown(),
5901 mouseEv
.MetaDown() );
5902 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5903 vetoed
= !gridEvt
.IsAllowed();
5906 // A Veto'd event may not be `claimed' so test this first
5907 if (vetoed
) return -1;
5908 return claimed
? 1 : 0;
5912 // Generate a grid event of specified type and return the result
5913 // of ProcessEvent().
5915 int wxGrid::SendEvent( const wxEventType type
,
5921 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5923 int rowOrCol
= (row
== -1 ? col
: row
);
5925 wxGridSizeEvent
gridEvt( GetId(),
5930 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5931 vetoed
= !gridEvt
.IsAllowed();
5935 wxGridEvent
gridEvt( GetId(),
5940 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5941 vetoed
= !gridEvt
.IsAllowed();
5944 // A Veto'd event may not be `claimed' so test this first
5945 if (vetoed
) return -1;
5946 return claimed
? 1 : 0;
5950 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5952 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5955 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
5957 // Don't do anything if between Begin/EndBatch...
5958 // EndBatch() will do all this on the last nested one anyway.
5959 if (! GetBatchCount())
5961 // Refresh to get correct scrolled position:
5962 wxScrolledWindow::Refresh(eraseb
,rect
);
5966 int rect_x
, rect_y
, rectWidth
, rectHeight
;
5967 int width_label
, width_cell
, height_label
, height_cell
;
5970 //Copy rectangle can get scroll offsets..
5971 rect_x
= rect
->GetX();
5972 rect_y
= rect
->GetY();
5973 rectWidth
= rect
->GetWidth();
5974 rectHeight
= rect
->GetHeight();
5976 width_label
= m_rowLabelWidth
- rect_x
;
5977 if (width_label
> rectWidth
) width_label
= rectWidth
;
5979 height_label
= m_colLabelHeight
- rect_y
;
5980 if (height_label
> rectHeight
) height_label
= rectHeight
;
5982 if (rect_x
> m_rowLabelWidth
)
5984 x
= rect_x
- m_rowLabelWidth
;
5985 width_cell
= rectWidth
;
5990 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
5993 if (rect_y
> m_colLabelHeight
)
5995 y
= rect_y
- m_colLabelHeight
;
5996 height_cell
= rectHeight
;
6001 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6004 // Paint corner label part intersecting rect.
6005 if ( width_label
> 0 && height_label
> 0 )
6007 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6008 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6011 // Paint col labels part intersecting rect.
6012 if ( width_cell
> 0 && height_label
> 0 )
6014 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6015 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6018 // Paint row labels part intersecting rect.
6019 if ( width_label
> 0 && height_cell
> 0 )
6021 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6022 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6025 // Paint cell area part intersecting rect.
6026 if ( width_cell
> 0 && height_cell
> 0 )
6028 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6029 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6034 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6035 m_colLabelWin
->Refresh(eraseb
, NULL
);
6036 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6037 m_gridWin
->Refresh(eraseb
, NULL
);
6042 void wxGrid::OnSize( wxSizeEvent
& event
)
6044 // position the child windows
6047 // don't call CalcDimensions() from here, the base class handles the size
6053 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6055 if ( m_inOnKeyDown
)
6057 // shouldn't be here - we are going round in circles...
6059 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6062 m_inOnKeyDown
= TRUE
;
6064 // propagate the event up and see if it gets processed
6066 wxWindow
*parent
= GetParent();
6067 wxKeyEvent
keyEvt( event
);
6068 keyEvt
.SetEventObject( parent
);
6070 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6073 // try local handlers
6075 switch ( event
.KeyCode() )
6078 if ( event
.ControlDown() )
6080 MoveCursorUpBlock( event
.ShiftDown() );
6084 MoveCursorUp( event
.ShiftDown() );
6089 if ( event
.ControlDown() )
6091 MoveCursorDownBlock( event
.ShiftDown() );
6095 MoveCursorDown( event
.ShiftDown() );
6100 if ( event
.ControlDown() )
6102 MoveCursorLeftBlock( event
.ShiftDown() );
6106 MoveCursorLeft( event
.ShiftDown() );
6111 if ( event
.ControlDown() )
6113 MoveCursorRightBlock( event
.ShiftDown() );
6117 MoveCursorRight( event
.ShiftDown() );
6122 case WXK_NUMPAD_ENTER
:
6123 if ( event
.ControlDown() )
6125 event
.Skip(); // to let the edit control have the return
6129 if ( GetGridCursorRow() < GetNumberRows()-1 )
6131 MoveCursorDown( event
.ShiftDown() );
6135 // at the bottom of a column
6136 HideCellEditControl();
6137 SaveEditControlValue();
6147 if (event
.ShiftDown())
6149 if ( GetGridCursorCol() > 0 )
6151 MoveCursorLeft( FALSE
);
6156 HideCellEditControl();
6157 SaveEditControlValue();
6162 if ( GetGridCursorCol() < GetNumberCols()-1 )
6164 MoveCursorRight( FALSE
);
6169 HideCellEditControl();
6170 SaveEditControlValue();
6176 if ( event
.ControlDown() )
6178 MakeCellVisible( 0, 0 );
6179 SetCurrentCell( 0, 0 );
6188 if ( event
.ControlDown() )
6190 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6191 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6208 if ( event
.ControlDown() )
6212 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6213 m_currentCellCoords
.GetCol(),
6214 event
.ControlDown(),
6221 if ( !IsEditable() )
6223 MoveCursorRight( FALSE
);
6226 // Otherwise fall through to default
6229 // is it possible to edit the current cell at all?
6230 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6232 // yes, now check whether the cells editor accepts the key
6233 int row
= m_currentCellCoords
.GetRow();
6234 int col
= m_currentCellCoords
.GetCol();
6235 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6236 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6238 // <F2> is special and will always start editing, for
6239 // other keys - ask the editor itself
6240 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
6241 || editor
->IsAcceptedKey(event
) )
6243 // ensure cell is visble
6244 MakeCellVisible(row
, col
);
6245 EnableCellEditControl();
6247 // a problem can arise if the cell is not completely
6248 // visible (even after calling MakeCellVisible the
6249 // control is not created and calling StartingKey will
6251 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6263 // let others process char events with modifiers or all
6264 // char events for readonly cells
6271 m_inOnKeyDown
= FALSE
;
6274 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6276 // try local handlers
6278 if ( event
.KeyCode() == WXK_SHIFT
)
6280 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6281 m_selectingBottomRight
!= wxGridNoCellCoords
)
6285 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6286 m_selectingTopLeft
.GetCol(),
6287 m_selectingBottomRight
.GetRow(),
6288 m_selectingBottomRight
.GetCol(),
6289 event
.ControlDown(),
6296 m_selectingTopLeft
= wxGridNoCellCoords
;
6297 m_selectingBottomRight
= wxGridNoCellCoords
;
6298 m_selectingKeyboard
= wxGridNoCellCoords
;
6302 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6306 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6308 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6310 // the event has been intercepted - do nothing
6314 wxClientDC
dc(m_gridWin
);
6317 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6319 HideCellEditControl();
6320 DisableCellEditControl();
6322 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6325 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6326 if ( !m_gridLinesEnabled
)
6334 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6336 // Otherwise refresh redraws the highlight!
6337 m_currentCellCoords
= coords
;
6339 DrawGridCellArea(dc
,cells
);
6340 DrawAllGridLines( dc
, r
);
6344 m_currentCellCoords
= coords
;
6346 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6347 DrawCellHighlight(dc
, attr
);
6352 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6355 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6359 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6362 rightCol
= GetNumberCols() - 1;
6364 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6367 bottomRow
= GetNumberRows() - 1;
6371 if ( topRow
> bottomRow
)
6378 if ( leftCol
> rightCol
)
6385 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6386 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6388 // First the case that we selected a completely new area
6389 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6390 m_selectingBottomRight
== wxGridNoCellCoords
)
6393 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6394 wxGridCellCoords ( bottomRow
, rightCol
) );
6395 m_gridWin
->Refresh( FALSE
, &rect
);
6397 // Now handle changing an existing selection area.
6398 else if ( m_selectingTopLeft
!= updateTopLeft
||
6399 m_selectingBottomRight
!= updateBottomRight
)
6401 // Compute two optimal update rectangles:
6402 // Either one rectangle is a real subset of the
6403 // other, or they are (almost) disjoint!
6405 bool need_refresh
[4];
6409 need_refresh
[3] = FALSE
;
6412 // Store intermediate values
6413 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6414 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6415 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6416 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6418 // Determine the outer/inner coordinates.
6419 if (oldLeft
> leftCol
)
6425 if (oldTop
> topRow
)
6431 if (oldRight
< rightCol
)
6434 oldRight
= rightCol
;
6437 if (oldBottom
< bottomRow
)
6440 oldBottom
= bottomRow
;
6444 // Now, either the stuff marked old is the outer
6445 // rectangle or we don't have a situation where one
6446 // is contained in the other.
6448 if ( oldLeft
< leftCol
)
6450 // Refresh the newly selected or deselected
6451 // area to the left of the old or new selection.
6452 need_refresh
[0] = TRUE
;
6453 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6455 wxGridCellCoords ( oldBottom
,
6459 if ( oldTop
< topRow
)
6461 // Refresh the newly selected or deselected
6462 // area above the old or new selection.
6463 need_refresh
[1] = TRUE
;
6464 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6466 wxGridCellCoords ( topRow
- 1,
6470 if ( oldRight
> rightCol
)
6472 // Refresh the newly selected or deselected
6473 // area to the right of the old or new selection.
6474 need_refresh
[2] = TRUE
;
6475 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6477 wxGridCellCoords ( oldBottom
,
6481 if ( oldBottom
> bottomRow
)
6483 // Refresh the newly selected or deselected
6484 // area below the old or new selection.
6485 need_refresh
[3] = TRUE
;
6486 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6488 wxGridCellCoords ( oldBottom
,
6492 // various Refresh() calls
6493 for (i
= 0; i
< 4; i
++ )
6494 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6495 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6498 m_selectingTopLeft
= updateTopLeft
;
6499 m_selectingBottomRight
= updateBottomRight
;
6503 // ------ functions to get/send data (see also public functions)
6506 bool wxGrid::GetModelValues()
6508 // Hide the editor, so it won't hide a changed value.
6509 HideCellEditControl();
6513 // all we need to do is repaint the grid
6515 m_gridWin
->Refresh();
6523 bool wxGrid::SetModelValues()
6527 // Disable the editor, so it won't hide a changed value.
6528 // Do we also want to save the current value of the editor first?
6530 DisableCellEditControl();
6534 for ( row
= 0; row
< m_numRows
; row
++ )
6536 for ( col
= 0; col
< m_numCols
; col
++ )
6538 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6550 // Note - this function only draws cells that are in the list of
6551 // exposed cells (usually set from the update region by
6552 // CalcExposedCells)
6554 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6556 if ( !m_numRows
|| !m_numCols
) return;
6558 int i
, numCells
= cells
.GetCount();
6559 int row
, col
, cell_rows
, cell_cols
;
6560 wxGridCellCoordsArray redrawCells
;
6562 for ( i
= numCells
-1; i
>= 0; i
-- )
6564 row
= cells
[i
].GetRow();
6565 col
= cells
[i
].GetCol();
6566 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6568 // If this cell is part of a multicell block, find owner for repaint
6569 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6571 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6572 bool marked
= FALSE
;
6573 for ( int j
= 0; j
< numCells
; j
++ )
6575 if ( cell
== cells
[j
] )
6583 int count
= redrawCells
.GetCount();
6584 for (int j
= 0; j
< count
; j
++)
6586 if ( cell
== redrawCells
[j
] )
6592 if (!marked
) redrawCells
.Add( cell
);
6594 continue; // don't bother drawing this cell
6597 // If this cell is empty, find cell to left that might want to overflow
6598 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6600 for ( int l
= 0; l
< cell_rows
; l
++ )
6602 // find a cell in this row to left alreay marked for repaint
6604 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6605 if ((redrawCells
[k
].GetCol() < left
) &&
6606 (redrawCells
[k
].GetRow() == row
))
6607 left
=redrawCells
[k
].GetCol();
6609 if (left
== col
) left
= 0; // oh well
6611 for (int j
= col
-1; j
>= left
; j
--)
6613 if (!m_table
->IsEmptyCell(row
+l
, j
))
6615 if (GetCellOverflow(row
+l
, j
))
6617 wxGridCellCoords
cell(row
+l
, j
);
6618 bool marked
= FALSE
;
6620 for (int k
= 0; k
< numCells
; k
++)
6622 if ( cell
== cells
[k
] )
6630 int count
= redrawCells
.GetCount();
6631 for (int k
= 0; k
< count
; k
++)
6633 if ( cell
== redrawCells
[k
] )
6639 if (!marked
) redrawCells
.Add( cell
);
6647 DrawCell( dc
, cells
[i
] );
6650 numCells
= redrawCells
.GetCount();
6652 for ( i
= numCells
- 1; i
>= 0; i
-- )
6654 DrawCell( dc
, redrawCells
[i
] );
6659 void wxGrid::DrawGridSpace( wxDC
& dc
)
6662 m_gridWin
->GetClientSize( &cw
, &ch
);
6665 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6667 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6668 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6670 if ( right
> rightCol
|| bottom
> bottomRow
)
6673 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6675 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6676 dc
.SetPen( *wxTRANSPARENT_PEN
);
6678 if ( right
> rightCol
)
6680 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6683 if ( bottom
> bottomRow
)
6685 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6691 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6693 int row
= coords
.GetRow();
6694 int col
= coords
.GetCol();
6696 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6699 // we draw the cell border ourselves
6700 #if !WXGRID_DRAW_LINES
6701 if ( m_gridLinesEnabled
)
6702 DrawCellBorder( dc
, coords
);
6705 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6707 bool isCurrent
= coords
== m_currentCellCoords
;
6709 wxRect rect
= CellToRect( row
, col
);
6711 // if the editor is shown, we should use it and not the renderer
6712 // Note: However, only if it is really _shown_, i.e. not hidden!
6713 if ( isCurrent
&& IsCellEditControlShown() )
6715 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6716 editor
->PaintBackground(rect
, attr
);
6721 // but all the rest is drawn by the cell renderer and hence may be
6723 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6724 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6731 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6733 int row
= m_currentCellCoords
.GetRow();
6734 int col
= m_currentCellCoords
.GetCol();
6736 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6739 wxRect rect
= CellToRect(row
, col
);
6741 // hmmm... what could we do here to show that the cell is disabled?
6742 // for now, I just draw a thinner border than for the other ones, but
6743 // it doesn't look really good
6745 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6749 // The center of th drawn line is where the position/width/height of
6750 // the rectangle is actually at, (on wxMSW atr least,) so we will
6751 // reduce the size of the rectangle to compensate for the thickness of
6752 // the line. If this is too strange on non wxMSW platforms then
6753 // please #ifdef this appropriately.
6754 rect
.x
+= penWidth
/2;
6755 rect
.y
+= penWidth
/2;
6756 rect
.width
-= penWidth
-1;
6757 rect
.height
-= penWidth
-1;
6760 // Now draw the rectangle
6761 // use the cellHighlightColour if the cell is inside a selection, this
6762 // will ensure the cell is always visible.
6763 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6764 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6765 dc
.DrawRectangle(rect
);
6769 // VZ: my experiments with 3d borders...
6771 // how to properly set colours for arbitrary bg?
6772 wxCoord x1
= rect
.x
,
6774 x2
= rect
.x
+ rect
.width
-1,
6775 y2
= rect
.y
+ rect
.height
-1;
6777 dc
.SetPen(*wxWHITE_PEN
);
6778 dc
.DrawLine(x1
, y1
, x2
, y1
);
6779 dc
.DrawLine(x1
, y1
, x1
, y2
);
6781 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6782 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6784 dc
.SetPen(*wxBLACK_PEN
);
6785 dc
.DrawLine(x1
, y2
, x2
, y2
);
6786 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6791 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6793 int row
= coords
.GetRow();
6794 int col
= coords
.GetCol();
6795 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6798 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6800 wxRect rect
= CellToRect( row
, col
);
6802 // right hand border
6804 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6805 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6809 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6810 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6813 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6815 // This if block was previously in wxGrid::OnPaint but that doesn't
6816 // seem to get called under wxGTK - MB
6818 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6819 m_numRows
&& m_numCols
)
6821 m_currentCellCoords
.Set(0, 0);
6824 if ( IsCellEditControlShown() )
6826 // don't show highlight when the edit control is shown
6830 // if the active cell was repainted, repaint its highlight too because it
6831 // might have been damaged by the grid lines
6832 size_t count
= cells
.GetCount();
6833 for ( size_t n
= 0; n
< count
; n
++ )
6835 if ( cells
[n
] == m_currentCellCoords
)
6837 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6838 DrawCellHighlight(dc
, attr
);
6846 // TODO: remove this ???
6847 // This is used to redraw all grid lines e.g. when the grid line colour
6850 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6852 #if !WXGRID_DRAW_LINES
6856 if ( !m_gridLinesEnabled
||
6858 !m_numCols
) return;
6860 int top
, bottom
, left
, right
;
6862 #if 0 //#ifndef __WXGTK__
6866 m_gridWin
->GetClientSize(&cw
, &ch
);
6868 // virtual coords of visible area
6870 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6871 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6876 reg
.GetBox(x
, y
, w
, h
);
6877 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6878 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6882 m_gridWin
->GetClientSize(&cw
, &ch
);
6883 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6884 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6887 // avoid drawing grid lines past the last row and col
6889 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6890 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6892 // no gridlines inside multicells, clip them out
6893 int leftCol
= XToCol(left
);
6894 int topRow
= YToRow(top
);
6895 int rightCol
= XToCol(right
);
6896 int bottomRow
= YToRow(bottom
);
6897 wxRegion
clippedcells(0, 0, cw
, ch
);
6899 if ((leftCol
>= 0) && (topRow
>= 0))
6901 if (rightCol
< 0) rightCol
= m_numCols
;
6902 if (bottomRow
< 0) bottomRow
= m_numRows
;
6904 int i
, j
, cell_rows
, cell_cols
;
6907 for (j
=topRow
; j
<bottomRow
; j
++)
6909 for (i
=leftCol
; i
<rightCol
; i
++)
6911 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
6912 if ((cell_rows
> 1) || (cell_cols
> 1))
6914 rect
= CellToRect(j
,i
);
6915 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6916 clippedcells
.Subtract(rect
);
6918 else if ((cell_rows
< 0) || (cell_cols
< 0))
6920 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
6921 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6922 clippedcells
.Subtract(rect
);
6927 dc
.SetClippingRegion( clippedcells
);
6929 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6931 // horizontal grid lines
6934 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6936 int bot
= GetRowBottom(i
) - 1;
6945 dc
.DrawLine( left
, bot
, right
, bot
);
6950 // vertical grid lines
6952 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6954 int colRight
= GetColRight(i
) - 1;
6955 if ( colRight
> right
)
6960 if ( colRight
>= left
)
6962 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6965 dc
.DestroyClippingRegion();
6969 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6971 if ( !m_numRows
) return;
6974 size_t numLabels
= rows
.GetCount();
6976 for ( i
= 0; i
< numLabels
; i
++ )
6978 DrawRowLabel( dc
, rows
[i
] );
6983 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6985 if ( GetRowHeight(row
) <= 0 )
6988 int rowTop
= GetRowTop(row
),
6989 rowBottom
= GetRowBottom(row
) - 1;
6991 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
6992 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6993 m_rowLabelWidth
-1, rowBottom
);
6995 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6997 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
6999 dc
.SetPen( *wxWHITE_PEN
);
7000 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7001 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7003 dc
.SetBackgroundMode( wxTRANSPARENT
);
7004 dc
.SetTextForeground( GetLabelTextColour() );
7005 dc
.SetFont( GetLabelFont() );
7008 GetRowLabelAlignment( &hAlign
, &vAlign
);
7012 rect
.SetY( GetRowTop(row
) + 2 );
7013 rect
.SetWidth( m_rowLabelWidth
- 4 );
7014 rect
.SetHeight( GetRowHeight(row
) - 4 );
7015 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7019 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7021 if ( !m_numCols
) return;
7024 size_t numLabels
= cols
.GetCount();
7026 for ( i
= 0; i
< numLabels
; i
++ )
7028 DrawColLabel( dc
, cols
[i
] );
7033 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7035 if ( GetColWidth(col
) <= 0 )
7038 int colLeft
= GetColLeft(col
),
7039 colRight
= GetColRight(col
) - 1;
7041 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7042 dc
.DrawLine( colRight
, 0,
7043 colRight
, m_colLabelHeight
-1 );
7045 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7047 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7048 colRight
+1, m_colLabelHeight
-1 );
7050 dc
.SetPen( *wxWHITE_PEN
);
7051 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7052 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7054 dc
.SetBackgroundMode( wxTRANSPARENT
);
7055 dc
.SetTextForeground( GetLabelTextColour() );
7056 dc
.SetFont( GetLabelFont() );
7058 dc
.SetBackgroundMode( wxTRANSPARENT
);
7059 dc
.SetTextForeground( GetLabelTextColour() );
7060 dc
.SetFont( GetLabelFont() );
7063 GetColLabelAlignment( &hAlign
, &vAlign
);
7066 rect
.SetX( colLeft
+ 2 );
7068 rect
.SetWidth( GetColWidth(col
) - 4 );
7069 rect
.SetHeight( m_colLabelHeight
- 4 );
7070 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
7073 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7074 const wxString
& value
,
7079 wxArrayString lines
;
7081 StringToLines( value
, lines
);
7084 //Forward to new API.
7085 DrawTextRectangle( dc
,
7093 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7094 const wxArrayString
& lines
,
7099 long textWidth
, textHeight
;
7100 long lineWidth
, lineHeight
;
7103 dc
.SetClippingRegion( rect
);
7105 nLines
= lines
.GetCount();
7110 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7113 case wxALIGN_BOTTOM
:
7114 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7117 case wxALIGN_CENTRE
:
7118 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7127 // Align each line of a multi-line label
7128 for( l
= 0; l
< nLines
; l
++ )
7130 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7132 switch( horizAlign
)
7135 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7138 case wxALIGN_CENTRE
:
7139 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7148 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7152 dc
.DestroyClippingRegion();
7156 // Split multi line text up into an array of strings. Any existing
7157 // contents of the string array are preserved.
7159 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7163 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7164 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7166 while ( startPos
< (int)tVal
.Length() )
7168 pos
= tVal
.Mid(startPos
).Find( eol
);
7173 else if ( pos
== 0 )
7175 lines
.Add( wxEmptyString
);
7179 lines
.Add( value
.Mid(startPos
, pos
) );
7183 if ( startPos
< (int)value
.Length() )
7185 lines
.Add( value
.Mid( startPos
) );
7190 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7191 const wxArrayString
& lines
,
7192 long *width
, long *height
)
7199 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7201 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7202 w
= wxMax( w
, lineW
);
7211 // ------ Batch processing.
7213 void wxGrid::EndBatch()
7215 if ( m_batchCount
> 0 )
7218 if ( !m_batchCount
)
7221 m_rowLabelWin
->Refresh();
7222 m_colLabelWin
->Refresh();
7223 m_cornerLabelWin
->Refresh();
7224 m_gridWin
->Refresh();
7229 // Use this, rather than wxWindow::Refresh(), to force an immediate
7230 // repainting of the grid. Has no effect if you are already inside a
7231 // BeginBatch / EndBatch block.
7233 void wxGrid::ForceRefresh()
7241 // ------ Edit control functions
7245 void wxGrid::EnableEditing( bool edit
)
7247 // TODO: improve this ?
7249 if ( edit
!= m_editable
)
7251 if(!edit
) EnableCellEditControl(edit
);
7257 void wxGrid::EnableCellEditControl( bool enable
)
7262 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7263 SetCurrentCell( 0, 0 );
7265 if ( enable
!= m_cellEditCtrlEnabled
)
7269 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7272 // this should be checked by the caller!
7273 wxASSERT_MSG( CanEnableCellControl(),
7274 _T("can't enable editing for this cell!") );
7276 // do it before ShowCellEditControl()
7277 m_cellEditCtrlEnabled
= enable
;
7279 ShowCellEditControl();
7283 //FIXME:add veto support
7284 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7286 HideCellEditControl();
7287 SaveEditControlValue();
7289 // do it after HideCellEditControl()
7290 m_cellEditCtrlEnabled
= enable
;
7295 bool wxGrid::IsCurrentCellReadOnly() const
7298 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7299 bool readonly
= attr
->IsReadOnly();
7305 bool wxGrid::CanEnableCellControl() const
7307 return m_editable
&& !IsCurrentCellReadOnly();
7310 bool wxGrid::IsCellEditControlEnabled() const
7312 // the cell edit control might be disable for all cells or just for the
7313 // current one if it's read only
7314 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7317 bool wxGrid::IsCellEditControlShown() const
7319 bool isShown
= FALSE
;
7321 if ( m_cellEditCtrlEnabled
)
7323 int row
= m_currentCellCoords
.GetRow();
7324 int col
= m_currentCellCoords
.GetCol();
7325 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7326 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7331 if ( editor
->IsCreated() )
7333 isShown
= editor
->GetControl()->IsShown();
7343 void wxGrid::ShowCellEditControl()
7345 if ( IsCellEditControlEnabled() )
7347 if ( !IsVisible( m_currentCellCoords
) )
7349 m_cellEditCtrlEnabled
= FALSE
;
7354 wxRect rect
= CellToRect( m_currentCellCoords
);
7355 int row
= m_currentCellCoords
.GetRow();
7356 int col
= m_currentCellCoords
.GetCol();
7358 // if this is part of a multicell, find owner (topleft)
7359 int cell_rows
, cell_cols
;
7360 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7361 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7365 m_currentCellCoords
.SetRow( row
);
7366 m_currentCellCoords
.SetCol( col
);
7369 // convert to scrolled coords
7371 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7373 // done in PaintBackground()
7375 // erase the highlight and the cell contents because the editor
7376 // might not cover the entire cell
7377 wxClientDC
dc( m_gridWin
);
7379 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7380 dc
.SetPen(*wxTRANSPARENT_PEN
);
7381 dc
.DrawRectangle(rect
);
7384 // cell is shifted by one pixel
7385 // However, don't allow x or y to become negative
7386 // since the SetSize() method interprets that as
7393 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7394 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7395 if ( !editor
->IsCreated() )
7397 editor
->Create(m_gridWin
, -1,
7398 new wxGridCellEditorEvtHandler(this, editor
));
7400 wxGridEditorCreatedEvent
evt(GetId(),
7401 wxEVT_GRID_EDITOR_CREATED
,
7405 editor
->GetControl());
7406 GetEventHandler()->ProcessEvent(evt
);
7410 // resize editor to overflow into righthand cells if allowed
7411 int maxWidth
= rect
.width
;
7412 wxString value
= GetCellValue(row
, col
);
7413 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7416 GetTextExtent(value
, &maxWidth
, &y
,
7417 NULL
, NULL
, &attr
->GetFont());
7418 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7420 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7421 if (rect
.x
+maxWidth
> client_right
)
7422 maxWidth
= client_right
- rect
.x
;
7424 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7426 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7427 // may have changed earlier
7428 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7431 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7432 // looks weird going over a multicell
7433 if (m_table
->IsEmptyCell(row
,i
) &&
7434 (rect
.width
< maxWidth
) && (c_rows
== 1))
7435 rect
.width
+= GetColWidth(i
);
7439 if (rect
.GetRight() > client_right
)
7440 rect
.SetRight(client_right
-1);
7443 editor
->SetCellAttr(attr
);
7444 editor
->SetSize( rect
);
7445 editor
->Show( TRUE
, attr
);
7447 // recalc dimensions in case we need to
7448 // expand the scrolled window to account for editor
7451 editor
->BeginEdit(row
, col
, this);
7452 editor
->SetCellAttr(NULL
);
7461 void wxGrid::HideCellEditControl()
7463 if ( IsCellEditControlEnabled() )
7465 int row
= m_currentCellCoords
.GetRow();
7466 int col
= m_currentCellCoords
.GetCol();
7468 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7469 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7470 editor
->Show( FALSE
);
7473 m_gridWin
->SetFocus();
7474 // refresh whole row to the right
7475 wxRect
rect( CellToRect(row
, col
) );
7476 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7477 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7478 m_gridWin
->Refresh( FALSE
, &rect
);
7483 void wxGrid::SaveEditControlValue()
7485 if ( IsCellEditControlEnabled() )
7487 int row
= m_currentCellCoords
.GetRow();
7488 int col
= m_currentCellCoords
.GetCol();
7490 wxString oldval
= GetCellValue(row
,col
);
7492 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7493 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7494 bool changed
= editor
->EndEdit(row
, col
, this);
7501 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7502 m_currentCellCoords
.GetRow(),
7503 m_currentCellCoords
.GetCol() ) < 0 ) {
7505 // Event has been vetoed, set the data back.
7506 SetCellValue(row
,col
,oldval
);
7514 // ------ Grid location functions
7515 // Note that all of these functions work with the logical coordinates of
7516 // grid cells and labels so you will need to convert from device
7517 // coordinates for mouse events etc.
7520 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7522 int row
= YToRow(y
);
7523 int col
= XToCol(x
);
7525 if ( row
== -1 || col
== -1 )
7527 coords
= wxGridNoCellCoords
;
7531 coords
.Set( row
, col
);
7536 // Internal Helper function for computing row or column from some
7537 // (unscrolled) coordinate value, using either
7538 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7539 // of m_rowBottoms/m_ColRights to speed up the search!
7541 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7542 const wxArrayInt
& BorderArray
, int nMax
,
7547 size_t i_max
= coord
/ defaultDist
,
7550 if (BorderArray
.IsEmpty())
7552 if((int) i_max
< nMax
)
7554 return maxOnOverflow
? nMax
- 1 : -1;
7557 if ( i_max
>= BorderArray
.GetCount())
7558 i_max
= BorderArray
.GetCount() - 1;
7561 if ( coord
>= BorderArray
[i_max
])
7564 i_max
= coord
/ minDist
;
7566 if ( i_max
>= BorderArray
.GetCount())
7567 i_max
= BorderArray
.GetCount() - 1;
7569 if ( coord
>= BorderArray
[i_max
])
7570 return maxOnOverflow
? (int)i_max
: -1;
7571 if ( coord
< BorderArray
[0] )
7574 while ( i_max
- i_min
> 0 )
7576 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7577 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7578 if (coord
>= BorderArray
[ i_max
- 1])
7582 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7583 if (coord
< BorderArray
[median
])
7591 int wxGrid::YToRow( int y
)
7593 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7594 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, m_numRows
, FALSE
);
7598 int wxGrid::XToCol( int x
)
7600 return CoordToRowOrCol(x
, m_defaultColWidth
,
7601 WXGRID_MIN_COL_WIDTH
, m_colRights
, m_numCols
, FALSE
);
7605 // return the row number that that the y coord is near the edge of, or
7606 // -1 if not near an edge
7608 int wxGrid::YToEdgeOfRow( int y
)
7611 i
= internalYToRow(y
);
7613 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7615 // We know that we are in row i, test whether we are
7616 // close enough to lower or upper border, respectively.
7617 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7619 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7627 // return the col number that that the x coord is near the edge of, or
7628 // -1 if not near an edge
7630 int wxGrid::XToEdgeOfCol( int x
)
7633 i
= internalXToCol(x
);
7635 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7637 // We know that we are in column i, test whether we are
7638 // close enough to right or left border, respectively.
7639 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7641 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7649 wxRect
wxGrid::CellToRect( int row
, int col
)
7651 wxRect
rect( -1, -1, -1, -1 );
7653 if ( row
>= 0 && row
< m_numRows
&&
7654 col
>= 0 && col
< m_numCols
)
7656 int i
, cell_rows
, cell_cols
;
7657 rect
.width
= rect
.height
= 0;
7658 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7659 // if negative then find multicell owner
7660 if (cell_rows
< 0) row
+= cell_rows
;
7661 if (cell_cols
< 0) col
+= cell_cols
;
7662 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7664 rect
.x
= GetColLeft(col
);
7665 rect
.y
= GetRowTop(row
);
7666 for (i
=col
; i
<col
+cell_cols
; i
++)
7667 rect
.width
+= GetColWidth(i
);
7668 for (i
=row
; i
<row
+cell_rows
; i
++)
7669 rect
.height
+= GetRowHeight(i
);
7672 // if grid lines are enabled, then the area of the cell is a bit smaller
7673 if (m_gridLinesEnabled
) {
7681 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7683 // get the cell rectangle in logical coords
7685 wxRect
r( CellToRect( row
, col
) );
7687 // convert to device coords
7689 int left
, top
, right
, bottom
;
7690 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7691 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7693 // check against the client area of the grid window
7696 m_gridWin
->GetClientSize( &cw
, &ch
);
7698 if ( wholeCellVisible
)
7700 // is the cell wholly visible ?
7702 return ( left
>= 0 && right
<= cw
&&
7703 top
>= 0 && bottom
<= ch
);
7707 // is the cell partly visible ?
7709 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7710 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7715 // make the specified cell location visible by doing a minimal amount
7718 void wxGrid::MakeCellVisible( int row
, int col
)
7722 int xpos
= -1, ypos
= -1;
7724 if ( row
>= 0 && row
< m_numRows
&&
7725 col
>= 0 && col
< m_numCols
)
7727 // get the cell rectangle in logical coords
7729 wxRect
r( CellToRect( row
, col
) );
7731 // convert to device coords
7733 int left
, top
, right
, bottom
;
7734 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7735 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7738 m_gridWin
->GetClientSize( &cw
, &ch
);
7744 else if ( bottom
> ch
)
7746 int h
= r
.GetHeight();
7748 for ( i
= row
-1; i
>= 0; i
-- )
7750 int rowHeight
= GetRowHeight(i
);
7751 if ( h
+ rowHeight
> ch
)
7758 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7759 // have rounding errors (this is important, because if we do, we
7760 // might not scroll at all and some cells won't be redrawn)
7762 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7764 ypos
+= GRID_SCROLL_LINE_Y
;
7771 else if ( right
> cw
)
7773 // position the view so that the cell is on the right
7775 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
7776 xpos
= x0
+ (right
- cw
);
7778 // see comment for ypos above
7779 xpos
+= GRID_SCROLL_LINE_X
;
7782 if ( xpos
!= -1 || ypos
!= -1 )
7785 xpos
/= GRID_SCROLL_LINE_X
;
7787 ypos
/= GRID_SCROLL_LINE_Y
;
7788 Scroll( xpos
, ypos
);
7796 // ------ Grid cursor movement functions
7799 bool wxGrid::MoveCursorUp( bool expandSelection
)
7801 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7802 m_currentCellCoords
.GetRow() >= 0 )
7804 if ( expandSelection
)
7806 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7807 m_selectingKeyboard
= m_currentCellCoords
;
7808 if ( m_selectingKeyboard
.GetRow() > 0 )
7810 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7811 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7812 m_selectingKeyboard
.GetCol() );
7813 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7816 else if ( m_currentCellCoords
.GetRow() > 0 )
7819 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7820 m_currentCellCoords
.GetCol() );
7821 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7822 m_currentCellCoords
.GetCol() );
7833 bool wxGrid::MoveCursorDown( bool expandSelection
)
7835 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7836 m_currentCellCoords
.GetRow() < m_numRows
)
7838 if ( expandSelection
)
7840 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7841 m_selectingKeyboard
= m_currentCellCoords
;
7842 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7844 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7845 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7846 m_selectingKeyboard
.GetCol() );
7847 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7850 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7853 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7854 m_currentCellCoords
.GetCol() );
7855 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7856 m_currentCellCoords
.GetCol() );
7867 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7869 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7870 m_currentCellCoords
.GetCol() >= 0 )
7872 if ( expandSelection
)
7874 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7875 m_selectingKeyboard
= m_currentCellCoords
;
7876 if ( m_selectingKeyboard
.GetCol() > 0 )
7878 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7879 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7880 m_selectingKeyboard
.GetCol() );
7881 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7884 else if ( m_currentCellCoords
.GetCol() > 0 )
7887 MakeCellVisible( m_currentCellCoords
.GetRow(),
7888 m_currentCellCoords
.GetCol() - 1 );
7889 SetCurrentCell( m_currentCellCoords
.GetRow(),
7890 m_currentCellCoords
.GetCol() - 1 );
7901 bool wxGrid::MoveCursorRight( bool expandSelection
)
7903 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7904 m_currentCellCoords
.GetCol() < m_numCols
)
7906 if ( expandSelection
)
7908 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7909 m_selectingKeyboard
= m_currentCellCoords
;
7910 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7912 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7913 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7914 m_selectingKeyboard
.GetCol() );
7915 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7918 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7921 MakeCellVisible( m_currentCellCoords
.GetRow(),
7922 m_currentCellCoords
.GetCol() + 1 );
7923 SetCurrentCell( m_currentCellCoords
.GetRow(),
7924 m_currentCellCoords
.GetCol() + 1 );
7935 bool wxGrid::MovePageUp()
7937 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7939 int row
= m_currentCellCoords
.GetRow();
7943 m_gridWin
->GetClientSize( &cw
, &ch
);
7945 int y
= GetRowTop(row
);
7946 int newRow
= YToRow( y
- ch
+ 1 );
7951 else if ( newRow
== row
)
7956 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7957 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7965 bool wxGrid::MovePageDown()
7967 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7969 int row
= m_currentCellCoords
.GetRow();
7970 if ( row
< m_numRows
)
7973 m_gridWin
->GetClientSize( &cw
, &ch
);
7975 int y
= GetRowTop(row
);
7976 int newRow
= YToRow( y
+ ch
);
7979 newRow
= m_numRows
- 1;
7981 else if ( newRow
== row
)
7986 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7987 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7995 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7998 m_currentCellCoords
!= wxGridNoCellCoords
&&
7999 m_currentCellCoords
.GetRow() > 0 )
8001 int row
= m_currentCellCoords
.GetRow();
8002 int col
= m_currentCellCoords
.GetCol();
8004 if ( m_table
->IsEmptyCell(row
, col
) )
8006 // starting in an empty cell: find the next block of
8012 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8015 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8017 // starting at the top of a block: find the next block
8023 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8028 // starting within a block: find the top of the block
8033 if ( m_table
->IsEmptyCell(row
, col
) )
8041 MakeCellVisible( row
, col
);
8042 if ( expandSelection
)
8044 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8045 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8050 SetCurrentCell( row
, col
);
8058 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8061 m_currentCellCoords
!= wxGridNoCellCoords
&&
8062 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8064 int row
= m_currentCellCoords
.GetRow();
8065 int col
= m_currentCellCoords
.GetCol();
8067 if ( m_table
->IsEmptyCell(row
, col
) )
8069 // starting in an empty cell: find the next block of
8072 while ( row
< m_numRows
-1 )
8075 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8078 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8080 // starting at the bottom of a block: find the next block
8083 while ( row
< m_numRows
-1 )
8086 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8091 // starting within a block: find the bottom of the block
8093 while ( row
< m_numRows
-1 )
8096 if ( m_table
->IsEmptyCell(row
, col
) )
8104 MakeCellVisible( row
, col
);
8105 if ( expandSelection
)
8107 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8108 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8113 SetCurrentCell( row
, col
);
8122 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8125 m_currentCellCoords
!= wxGridNoCellCoords
&&
8126 m_currentCellCoords
.GetCol() > 0 )
8128 int row
= m_currentCellCoords
.GetRow();
8129 int col
= m_currentCellCoords
.GetCol();
8131 if ( m_table
->IsEmptyCell(row
, col
) )
8133 // starting in an empty cell: find the next block of
8139 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8142 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8144 // starting at the left of a block: find the next block
8150 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8155 // starting within a block: find the left of the block
8160 if ( m_table
->IsEmptyCell(row
, col
) )
8168 MakeCellVisible( row
, col
);
8169 if ( expandSelection
)
8171 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8172 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8177 SetCurrentCell( row
, col
);
8186 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8189 m_currentCellCoords
!= wxGridNoCellCoords
&&
8190 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8192 int row
= m_currentCellCoords
.GetRow();
8193 int col
= m_currentCellCoords
.GetCol();
8195 if ( m_table
->IsEmptyCell(row
, col
) )
8197 // starting in an empty cell: find the next block of
8200 while ( col
< m_numCols
-1 )
8203 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8206 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8208 // starting at the right of a block: find the next block
8211 while ( col
< m_numCols
-1 )
8214 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8219 // starting within a block: find the right of the block
8221 while ( col
< m_numCols
-1 )
8224 if ( m_table
->IsEmptyCell(row
, col
) )
8232 MakeCellVisible( row
, col
);
8233 if ( expandSelection
)
8235 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8236 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8241 SetCurrentCell( row
, col
);
8253 // ------ Label values and formatting
8256 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8258 *horiz
= m_rowLabelHorizAlign
;
8259 *vert
= m_rowLabelVertAlign
;
8262 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8264 *horiz
= m_colLabelHorizAlign
;
8265 *vert
= m_colLabelVertAlign
;
8268 wxString
wxGrid::GetRowLabelValue( int row
)
8272 return m_table
->GetRowLabelValue( row
);
8282 wxString
wxGrid::GetColLabelValue( int col
)
8286 return m_table
->GetColLabelValue( col
);
8297 void wxGrid::SetRowLabelSize( int width
)
8299 width
= wxMax( width
, 0 );
8300 if ( width
!= m_rowLabelWidth
)
8304 m_rowLabelWin
->Show( FALSE
);
8305 m_cornerLabelWin
->Show( FALSE
);
8307 else if ( m_rowLabelWidth
== 0 )
8309 m_rowLabelWin
->Show( TRUE
);
8310 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8313 m_rowLabelWidth
= width
;
8315 wxScrolledWindow::Refresh( TRUE
);
8320 void wxGrid::SetColLabelSize( int height
)
8322 height
= wxMax( height
, 0 );
8323 if ( height
!= m_colLabelHeight
)
8327 m_colLabelWin
->Show( FALSE
);
8328 m_cornerLabelWin
->Show( FALSE
);
8330 else if ( m_colLabelHeight
== 0 )
8332 m_colLabelWin
->Show( TRUE
);
8333 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8336 m_colLabelHeight
= height
;
8338 wxScrolledWindow::Refresh( TRUE
);
8343 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8345 if ( m_labelBackgroundColour
!= colour
)
8347 m_labelBackgroundColour
= colour
;
8348 m_rowLabelWin
->SetBackgroundColour( colour
);
8349 m_colLabelWin
->SetBackgroundColour( colour
);
8350 m_cornerLabelWin
->SetBackgroundColour( colour
);
8352 if ( !GetBatchCount() )
8354 m_rowLabelWin
->Refresh();
8355 m_colLabelWin
->Refresh();
8356 m_cornerLabelWin
->Refresh();
8361 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8363 if ( m_labelTextColour
!= colour
)
8365 m_labelTextColour
= colour
;
8366 if ( !GetBatchCount() )
8368 m_rowLabelWin
->Refresh();
8369 m_colLabelWin
->Refresh();
8374 void wxGrid::SetLabelFont( const wxFont
& font
)
8377 if ( !GetBatchCount() )
8379 m_rowLabelWin
->Refresh();
8380 m_colLabelWin
->Refresh();
8384 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8386 // allow old (incorrect) defs to be used
8389 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8390 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8391 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8396 case wxTOP
: vert
= wxALIGN_TOP
; break;
8397 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8398 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8401 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8403 m_rowLabelHorizAlign
= horiz
;
8406 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8408 m_rowLabelVertAlign
= vert
;
8411 if ( !GetBatchCount() )
8413 m_rowLabelWin
->Refresh();
8417 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8419 // allow old (incorrect) defs to be used
8422 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8423 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8424 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8429 case wxTOP
: vert
= wxALIGN_TOP
; break;
8430 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8431 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8434 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8436 m_colLabelHorizAlign
= horiz
;
8439 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8441 m_colLabelVertAlign
= vert
;
8444 if ( !GetBatchCount() )
8446 m_colLabelWin
->Refresh();
8450 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8454 m_table
->SetRowLabelValue( row
, s
);
8455 if ( !GetBatchCount() )
8457 wxRect rect
= CellToRect( row
, 0);
8458 if ( rect
.height
> 0 )
8460 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8462 rect
.width
= m_rowLabelWidth
;
8463 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8469 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8473 m_table
->SetColLabelValue( col
, s
);
8474 if ( !GetBatchCount() )
8476 wxRect rect
= CellToRect( 0, col
);
8477 if ( rect
.width
> 0 )
8479 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8481 rect
.height
= m_colLabelHeight
;
8482 m_colLabelWin
->Refresh( TRUE
, &rect
);
8488 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8490 if ( m_gridLineColour
!= colour
)
8492 m_gridLineColour
= colour
;
8494 wxClientDC
dc( m_gridWin
);
8496 DrawAllGridLines( dc
, wxRegion() );
8501 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8503 if ( m_cellHighlightColour
!= colour
)
8505 m_cellHighlightColour
= colour
;
8507 wxClientDC
dc( m_gridWin
);
8509 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8510 DrawCellHighlight(dc
, attr
);
8515 void wxGrid::SetCellHighlightPenWidth(int width
)
8517 if (m_cellHighlightPenWidth
!= width
) {
8518 m_cellHighlightPenWidth
= width
;
8520 // Just redrawing the cell highlight is not enough since that won't
8521 // make any visible change if the the thickness is getting smaller.
8522 int row
= m_currentCellCoords
.GetRow();
8523 int col
= m_currentCellCoords
.GetCol();
8524 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8526 wxRect rect
= CellToRect(row
, col
);
8527 m_gridWin
->Refresh(TRUE
, &rect
);
8531 void wxGrid::SetCellHighlightROPenWidth(int width
)
8533 if (m_cellHighlightROPenWidth
!= width
) {
8534 m_cellHighlightROPenWidth
= width
;
8536 // Just redrawing the cell highlight is not enough since that won't
8537 // make any visible change if the the thickness is getting smaller.
8538 int row
= m_currentCellCoords
.GetRow();
8539 int col
= m_currentCellCoords
.GetCol();
8540 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8542 wxRect rect
= CellToRect(row
, col
);
8543 m_gridWin
->Refresh(TRUE
, &rect
);
8547 void wxGrid::EnableGridLines( bool enable
)
8549 if ( enable
!= m_gridLinesEnabled
)
8551 m_gridLinesEnabled
= enable
;
8553 if ( !GetBatchCount() )
8557 wxClientDC
dc( m_gridWin
);
8559 DrawAllGridLines( dc
, wxRegion() );
8563 m_gridWin
->Refresh();
8570 int wxGrid::GetDefaultRowSize()
8572 return m_defaultRowHeight
;
8575 int wxGrid::GetRowSize( int row
)
8577 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8579 return GetRowHeight(row
);
8582 int wxGrid::GetDefaultColSize()
8584 return m_defaultColWidth
;
8587 int wxGrid::GetColSize( int col
)
8589 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8591 return GetColWidth(col
);
8594 // ============================================================================
8595 // access to the grid attributes: each of them has a default value in the grid
8596 // itself and may be overidden on a per-cell basis
8597 // ============================================================================
8599 // ----------------------------------------------------------------------------
8600 // setting default attributes
8601 // ----------------------------------------------------------------------------
8603 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8605 m_defaultCellAttr
->SetBackgroundColour(col
);
8607 m_gridWin
->SetBackgroundColour(col
);
8611 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8613 m_defaultCellAttr
->SetTextColour(col
);
8616 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8618 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8621 void wxGrid::SetDefaultCellOverflow( bool allow
)
8623 m_defaultCellAttr
->SetOverflow(allow
);
8626 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8628 m_defaultCellAttr
->SetFont(font
);
8631 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8633 m_defaultCellAttr
->SetRenderer(renderer
);
8636 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8638 m_defaultCellAttr
->SetEditor(editor
);
8641 // ----------------------------------------------------------------------------
8642 // access to the default attrbiutes
8643 // ----------------------------------------------------------------------------
8645 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8647 return m_defaultCellAttr
->GetBackgroundColour();
8650 wxColour
wxGrid::GetDefaultCellTextColour()
8652 return m_defaultCellAttr
->GetTextColour();
8655 wxFont
wxGrid::GetDefaultCellFont()
8657 return m_defaultCellAttr
->GetFont();
8660 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8662 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8665 bool wxGrid::GetDefaultCellOverflow()
8667 return m_defaultCellAttr
->GetOverflow();
8670 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8672 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8675 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8677 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8680 // ----------------------------------------------------------------------------
8681 // access to cell attributes
8682 // ----------------------------------------------------------------------------
8684 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8686 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8687 wxColour colour
= attr
->GetBackgroundColour();
8692 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8694 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8695 wxColour colour
= attr
->GetTextColour();
8700 wxFont
wxGrid::GetCellFont( int row
, int col
)
8702 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8703 wxFont font
= attr
->GetFont();
8708 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8710 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8711 attr
->GetAlignment(horiz
, vert
);
8715 bool wxGrid::GetCellOverflow( int row
, int col
)
8717 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8718 bool allow
= attr
->GetOverflow();
8723 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8725 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8726 attr
->GetSize( num_rows
, num_cols
);
8730 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8732 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8733 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8739 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8741 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8742 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8748 bool wxGrid::IsReadOnly(int row
, int col
) const
8750 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8751 bool isReadOnly
= attr
->IsReadOnly();
8756 // ----------------------------------------------------------------------------
8757 // attribute support: cache, automatic provider creation, ...
8758 // ----------------------------------------------------------------------------
8760 bool wxGrid::CanHaveAttributes()
8767 return m_table
->CanHaveAttributes();
8770 void wxGrid::ClearAttrCache()
8772 if ( m_attrCache
.row
!= -1 )
8774 wxSafeDecRef(m_attrCache
.attr
);
8775 m_attrCache
.attr
= NULL
;
8776 m_attrCache
.row
= -1;
8780 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8784 wxGrid
*self
= (wxGrid
*)this; // const_cast
8786 self
->ClearAttrCache();
8787 self
->m_attrCache
.row
= row
;
8788 self
->m_attrCache
.col
= col
;
8789 self
->m_attrCache
.attr
= attr
;
8794 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8796 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8798 *attr
= m_attrCache
.attr
;
8799 wxSafeIncRef(m_attrCache
.attr
);
8801 #ifdef DEBUG_ATTR_CACHE
8802 gs_nAttrCacheHits
++;
8809 #ifdef DEBUG_ATTR_CACHE
8810 gs_nAttrCacheMisses
++;
8816 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8818 wxGridCellAttr
*attr
= NULL
;
8819 // Additional test to avoid looking at the cache e.g. for
8820 // wxNoCellCoords, as this will confuse memory management.
8823 if ( !LookupAttr(row
, col
, &attr
) )
8825 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8826 : (wxGridCellAttr
*)NULL
;
8827 CacheAttr(row
, col
, attr
);
8832 attr
->SetDefAttr(m_defaultCellAttr
);
8836 attr
= m_defaultCellAttr
;
8843 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8845 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8847 wxCHECK_MSG( m_table
, attr
,
8848 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8850 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8853 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8855 // artificially inc the ref count to match DecRef() in caller
8857 m_table
->SetAttr(attr
, row
, col
);
8863 // ----------------------------------------------------------------------------
8864 // setting column attributes (wrappers around SetColAttr)
8865 // ----------------------------------------------------------------------------
8867 void wxGrid::SetColFormatBool(int col
)
8869 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8872 void wxGrid::SetColFormatNumber(int col
)
8874 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8877 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8879 wxString typeName
= wxGRID_VALUE_FLOAT
;
8880 if ( (width
!= -1) || (precision
!= -1) )
8882 typeName
<< _T(':') << width
<< _T(',') << precision
;
8885 SetColFormatCustom(col
, typeName
);
8888 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8890 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8892 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8894 attr
= new wxGridCellAttr
;
8895 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8896 attr
->SetRenderer(renderer
);
8898 SetColAttr(col
, attr
);
8902 // ----------------------------------------------------------------------------
8903 // setting cell attributes: this is forwarded to the table
8904 // ----------------------------------------------------------------------------
8906 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
8908 if ( CanHaveAttributes() )
8910 m_table
->SetAttr(attr
, row
, col
);
8919 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8921 if ( CanHaveAttributes() )
8923 m_table
->SetRowAttr(attr
, row
);
8932 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8934 if ( CanHaveAttributes() )
8936 m_table
->SetColAttr(attr
, col
);
8945 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8947 if ( CanHaveAttributes() )
8949 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8950 attr
->SetBackgroundColour(colour
);
8955 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8957 if ( CanHaveAttributes() )
8959 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8960 attr
->SetTextColour(colour
);
8965 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8967 if ( CanHaveAttributes() )
8969 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8970 attr
->SetFont(font
);
8975 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8977 if ( CanHaveAttributes() )
8979 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8980 attr
->SetAlignment(horiz
, vert
);
8985 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
8987 if ( CanHaveAttributes() )
8989 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8990 attr
->SetOverflow(allow
);
8995 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
8997 if ( CanHaveAttributes() )
8999 int cell_rows
, cell_cols
;
9001 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9002 attr
->GetSize(&cell_rows
, &cell_cols
);
9003 attr
->SetSize(num_rows
, num_cols
);
9006 // Cannot set the size of a cell to 0 or negative values
9007 // While it is perfectly legal to do that, this function cannot
9008 // handle all the possibilies, do it by hand by getting the CellAttr.
9009 // You can only set the size of a cell to 1,1 or greater with this fn
9010 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9011 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9012 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9013 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9015 // if this was already a multicell then "turn off" the other cells first
9016 if ((cell_rows
> 1) || (cell_rows
> 1))
9019 for (j
=row
; j
<row
+cell_rows
; j
++)
9021 for (i
=col
; i
<col
+cell_cols
; i
++)
9023 if ((i
!= col
) || (j
!= row
))
9025 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9026 attr_stub
->SetSize( 1, 1 );
9027 attr_stub
->DecRef();
9033 // mark the cells that will be covered by this cell to
9034 // negative or zero values to point back at this cell
9035 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9038 for (j
=row
; j
<row
+num_rows
; j
++)
9040 for (i
=col
; i
<col
+num_cols
; i
++)
9042 if ((i
!= col
) || (j
!= row
))
9044 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9045 attr_stub
->SetSize( row
-j
, col
-i
);
9046 attr_stub
->DecRef();
9054 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9056 if ( CanHaveAttributes() )
9058 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9059 attr
->SetRenderer(renderer
);
9064 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9066 if ( CanHaveAttributes() )
9068 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9069 attr
->SetEditor(editor
);
9074 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9076 if ( CanHaveAttributes() )
9078 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9079 attr
->SetReadOnly(isReadOnly
);
9084 // ----------------------------------------------------------------------------
9085 // Data type registration
9086 // ----------------------------------------------------------------------------
9088 void wxGrid::RegisterDataType(const wxString
& typeName
,
9089 wxGridCellRenderer
* renderer
,
9090 wxGridCellEditor
* editor
)
9092 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9096 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9098 wxString typeName
= m_table
->GetTypeName(row
, col
);
9099 return GetDefaultEditorForType(typeName
);
9102 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9104 wxString typeName
= m_table
->GetTypeName(row
, col
);
9105 return GetDefaultRendererForType(typeName
);
9109 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9111 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9112 if ( index
== wxNOT_FOUND
)
9114 wxFAIL_MSG(wxT("Unknown data type name"));
9119 return m_typeRegistry
->GetEditor(index
);
9123 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9125 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9126 if ( index
== wxNOT_FOUND
)
9128 wxFAIL_MSG(wxT("Unknown data type name"));
9133 return m_typeRegistry
->GetRenderer(index
);
9137 // ----------------------------------------------------------------------------
9139 // ----------------------------------------------------------------------------
9141 void wxGrid::EnableDragRowSize( bool enable
)
9143 m_canDragRowSize
= enable
;
9147 void wxGrid::EnableDragColSize( bool enable
)
9149 m_canDragColSize
= enable
;
9152 void wxGrid::EnableDragGridSize( bool enable
)
9154 m_canDragGridSize
= enable
;
9158 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9160 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
9162 if ( resizeExistingRows
)
9164 // since we are resizing all rows to the default row size,
9165 // we can simply clear the row heights and row bottoms
9166 // arrays (which also allows us to take advantage of
9167 // some speed optimisations)
9168 m_rowHeights
.Empty();
9169 m_rowBottoms
.Empty();
9170 if ( !GetBatchCount() )
9175 void wxGrid::SetRowSize( int row
, int height
)
9177 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9179 if ( m_rowHeights
.IsEmpty() )
9181 // need to really create the array
9185 int h
= wxMax( 0, height
);
9186 int diff
= h
- m_rowHeights
[row
];
9188 m_rowHeights
[row
] = h
;
9190 for ( i
= row
; i
< m_numRows
; i
++ )
9192 m_rowBottoms
[i
] += diff
;
9194 if ( !GetBatchCount() )
9198 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9200 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
9202 if ( resizeExistingCols
)
9204 // since we are resizing all columns to the default column size,
9205 // we can simply clear the col widths and col rights
9206 // arrays (which also allows us to take advantage of
9207 // some speed optimisations)
9208 m_colWidths
.Empty();
9209 m_colRights
.Empty();
9210 if ( !GetBatchCount() )
9215 void wxGrid::SetColSize( int col
, int width
)
9217 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9219 // should we check that it's bigger than GetColMinimalWidth(col) here?
9221 if ( m_colWidths
.IsEmpty() )
9223 // need to really create the array
9227 // if < 0 calc new width from label
9231 wxArrayString lines
;
9232 wxClientDC
dc(m_colLabelWin
);
9233 dc
.SetFont(GetLabelFont());
9234 StringToLines(GetColLabelValue(col
), lines
);
9235 GetTextBoxSize(dc
, lines
, &w
, &h
);
9238 int w
= wxMax( 0, width
);
9239 int diff
= w
- m_colWidths
[col
];
9240 m_colWidths
[col
] = w
;
9243 for ( i
= col
; i
< m_numCols
; i
++ )
9245 m_colRights
[i
] += diff
;
9247 if ( !GetBatchCount() )
9252 void wxGrid::SetColMinimalWidth( int col
, int width
)
9254 m_colMinWidths
.Put(col
, width
);
9257 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9259 m_rowMinHeights
.Put(row
, width
);
9262 int wxGrid::GetColMinimalWidth(int col
) const
9264 long value
= m_colMinWidths
.Get(col
);
9265 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
9268 int wxGrid::GetRowMinimalHeight(int row
) const
9270 long value
= m_rowMinHeights
.Get(row
);
9271 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
9274 // ----------------------------------------------------------------------------
9276 // ----------------------------------------------------------------------------
9278 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9280 wxClientDC
dc(m_gridWin
);
9282 // init both of them to avoid compiler warnings, even if weo nly need one
9290 wxCoord extent
, extentMax
= 0;
9291 int max
= column
? m_numRows
: m_numCols
;
9292 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9299 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9300 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9303 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9304 extent
= column
? size
.x
: size
.y
;
9305 if ( extent
> extentMax
)
9316 // now also compare with the column label extent
9318 dc
.SetFont( GetLabelFont() );
9321 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9323 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9325 extent
= column
? w
: h
;
9326 if ( extent
> extentMax
)
9333 // empty column - give default extent (notice that if extentMax is less
9334 // than default extent but != 0, it's ok)
9335 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9341 // leave some space around text
9352 SetColSize(col
, extentMax
);
9353 if ( !GetBatchCount() )
9356 m_gridWin
->GetClientSize( &cw
, &ch
);
9357 wxRect
rect ( CellToRect( 0, col
) );
9359 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9360 rect
.width
= cw
- rect
.x
;
9361 rect
.height
= m_colLabelHeight
;
9362 m_colLabelWin
->Refresh( TRUE
, &rect
);
9367 SetRowSize(row
, extentMax
);
9368 if ( !GetBatchCount() )
9371 m_gridWin
->GetClientSize( &cw
, &ch
);
9372 wxRect
rect ( CellToRect( row
, 0 ) );
9374 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9375 rect
.width
= m_rowLabelWidth
;
9376 rect
.height
= ch
- rect
.y
;
9377 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9383 SetColMinimalWidth(col
, extentMax
);
9385 SetRowMinimalHeight(row
, extentMax
);
9389 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9391 int width
= m_rowLabelWidth
;
9396 for ( int col
= 0; col
< m_numCols
; col
++ )
9400 AutoSizeColumn(col
, setAsMin
);
9403 width
+= GetColWidth(col
);
9412 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9414 int height
= m_colLabelHeight
;
9419 for ( int row
= 0; row
< m_numRows
; row
++ )
9423 AutoSizeRow(row
, setAsMin
);
9426 height
+= GetRowHeight(row
);
9435 void wxGrid::AutoSize()
9439 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9441 // round up the size to a multiple of scroll step - this ensures that we
9442 // won't get the scrollbars if we're sized exactly to this width
9443 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9445 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9446 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9448 // distribute the extra space between the columns/rows to avoid having
9449 // extra white space
9451 // Remove the extra m_extraWidth + 1 added above
9452 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9453 if ( diff
&& m_numCols
)
9455 // try to resize the columns uniformly
9456 wxCoord diffPerCol
= diff
/ m_numCols
;
9459 for ( int col
= 0; col
< m_numCols
; col
++ )
9461 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9465 // add remaining amount to the last columns
9466 diff
-= diffPerCol
* m_numCols
;
9469 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9471 SetColSize(col
, GetColWidth(col
) + 1);
9477 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9478 if ( diff
&& m_numRows
)
9480 // try to resize the columns uniformly
9481 wxCoord diffPerRow
= diff
/ m_numRows
;
9484 for ( int row
= 0; row
< m_numRows
; row
++ )
9486 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9490 // add remaining amount to the last rows
9491 diff
-= diffPerRow
* m_numRows
;
9494 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9496 SetRowSize(row
, GetRowHeight(row
) + 1);
9503 SetClientSize(sizeFit
);
9506 wxSize
wxGrid::DoGetBestSize() const
9508 // don't set sizes, only calculate them
9509 wxGrid
*self
= (wxGrid
*)this; // const_cast
9512 width
= self
->SetOrCalcColumnSizes(TRUE
);
9513 height
= self
->SetOrCalcRowSizes(TRUE
);
9515 int maxwidth
, maxheight
;
9516 wxDisplaySize( & maxwidth
, & maxheight
);
9518 if ( width
> maxwidth
) width
= maxwidth
;
9519 if ( height
> maxheight
) height
= maxheight
;
9521 return wxSize( width
, height
);
9530 wxPen
& wxGrid::GetDividerPen() const
9535 // ----------------------------------------------------------------------------
9536 // cell value accessor functions
9537 // ----------------------------------------------------------------------------
9539 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9543 m_table
->SetValue( row
, col
, s
);
9544 if ( !GetBatchCount() )
9547 wxRect
rect( CellToRect( row
, col
) );
9549 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9550 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9551 m_gridWin
->Refresh( FALSE
, &rect
);
9554 if ( m_currentCellCoords
.GetRow() == row
&&
9555 m_currentCellCoords
.GetCol() == col
&&
9556 IsCellEditControlShown())
9557 // Note: If we are using IsCellEditControlEnabled,
9558 // this interacts badly with calling SetCellValue from
9559 // an EVT_GRID_CELL_CHANGE handler.
9561 HideCellEditControl();
9562 ShowCellEditControl(); // will reread data from table
9569 // ------ Block, row and col selection
9572 void wxGrid::SelectRow( int row
, bool addToSelected
)
9574 if ( IsSelection() && !addToSelected
)
9578 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9582 void wxGrid::SelectCol( int col
, bool addToSelected
)
9584 if ( IsSelection() && !addToSelected
)
9588 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9592 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9593 bool addToSelected
)
9595 if ( IsSelection() && !addToSelected
)
9599 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9600 FALSE
, addToSelected
);
9604 void wxGrid::SelectAll()
9606 if ( m_numRows
> 0 && m_numCols
> 0 )
9609 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9614 // ------ Cell, row and col deselection
9617 void wxGrid::DeselectRow( int row
)
9622 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9624 if ( m_selection
->IsInSelection(row
, 0 ) )
9625 m_selection
->ToggleCellSelection( row
, 0);
9629 int nCols
= GetNumberCols();
9630 for ( int i
= 0; i
< nCols
; i
++ )
9632 if ( m_selection
->IsInSelection(row
, i
) )
9633 m_selection
->ToggleCellSelection( row
, i
);
9638 void wxGrid::DeselectCol( int col
)
9643 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9645 if ( m_selection
->IsInSelection(0, col
) )
9646 m_selection
->ToggleCellSelection( 0, col
);
9650 int nRows
= GetNumberRows();
9651 for ( int i
= 0; i
< nRows
; i
++ )
9653 if ( m_selection
->IsInSelection(i
, col
) )
9654 m_selection
->ToggleCellSelection(i
, col
);
9659 void wxGrid::DeselectCell( int row
, int col
)
9661 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9662 m_selection
->ToggleCellSelection(row
, col
);
9665 bool wxGrid::IsSelection()
9667 return ( m_selection
&& (m_selection
->IsSelection() ||
9668 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9669 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9672 bool wxGrid::IsInSelection( int row
, int col
) const
9674 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9675 ( row
>= m_selectingTopLeft
.GetRow() &&
9676 col
>= m_selectingTopLeft
.GetCol() &&
9677 row
<= m_selectingBottomRight
.GetRow() &&
9678 col
<= m_selectingBottomRight
.GetCol() )) );
9681 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9683 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9684 return m_selection
->m_cellSelection
;
9686 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9688 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9689 return m_selection
->m_blockSelectionTopLeft
;
9691 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9693 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9694 return m_selection
->m_blockSelectionBottomRight
;
9696 wxArrayInt
wxGrid::GetSelectedRows() const
9698 if (!m_selection
) { wxArrayInt a
; return a
; }
9699 return m_selection
->m_rowSelection
;
9701 wxArrayInt
wxGrid::GetSelectedCols() const
9703 if (!m_selection
) { wxArrayInt a
; return a
; }
9704 return m_selection
->m_colSelection
;
9708 void wxGrid::ClearSelection()
9710 m_selectingTopLeft
= wxGridNoCellCoords
;
9711 m_selectingBottomRight
= wxGridNoCellCoords
;
9713 m_selection
->ClearSelection();
9717 // This function returns the rectangle that encloses the given block
9718 // in device coords clipped to the client size of the grid window.
9720 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9721 const wxGridCellCoords
&bottomRight
)
9723 wxRect
rect( wxGridNoCellRect
);
9726 cellRect
= CellToRect( topLeft
);
9727 if ( cellRect
!= wxGridNoCellRect
)
9733 rect
= wxRect( 0, 0, 0, 0 );
9736 cellRect
= CellToRect( bottomRight
);
9737 if ( cellRect
!= wxGridNoCellRect
)
9743 return wxGridNoCellRect
;
9747 int left
= rect
.GetLeft();
9748 int top
= rect
.GetTop();
9749 int right
= rect
.GetRight();
9750 int bottom
= rect
.GetBottom();
9752 int leftCol
= topLeft
.GetCol();
9753 int topRow
= topLeft
.GetRow();
9754 int rightCol
= bottomRight
.GetCol();
9755 int bottomRow
= bottomRight
.GetRow();
9778 for ( j
= topRow
; j
<= bottomRow
; j
++ )
9780 for ( i
= leftCol
; i
<= rightCol
; i
++ )
9782 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
9784 cellRect
= CellToRect( j
, i
);
9786 if (cellRect
.x
< left
)
9788 if (cellRect
.y
< top
)
9790 if (cellRect
.x
+ cellRect
.width
> right
)
9791 right
= cellRect
.x
+ cellRect
.width
;
9792 if (cellRect
.y
+ cellRect
.height
> bottom
)
9793 bottom
= cellRect
.y
+ cellRect
.height
;
9795 else i
= rightCol
; // jump over inner cells.
9799 // convert to scrolled coords
9801 CalcScrolledPosition( left
, top
, &left
, &top
);
9802 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9805 m_gridWin
->GetClientSize( &cw
, &ch
);
9807 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9808 return wxRect( 0, 0, 0, 0);
9810 rect
.SetLeft( wxMax(0, left
) );
9811 rect
.SetTop( wxMax(0, top
) );
9812 rect
.SetRight( wxMin(cw
, right
) );
9813 rect
.SetBottom( wxMin(ch
, bottom
) );
9821 // ------ Grid event classes
9824 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9826 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9827 int row
, int col
, int x
, int y
, bool sel
,
9828 bool control
, bool shift
, bool alt
, bool meta
)
9829 : wxNotifyEvent( type
, id
)
9836 m_control
= control
;
9841 SetEventObject(obj
);
9845 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9847 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9848 int rowOrCol
, int x
, int y
,
9849 bool control
, bool shift
, bool alt
, bool meta
)
9850 : wxNotifyEvent( type
, id
)
9852 m_rowOrCol
= rowOrCol
;
9855 m_control
= control
;
9860 SetEventObject(obj
);
9864 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9866 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9867 const wxGridCellCoords
& topLeft
,
9868 const wxGridCellCoords
& bottomRight
,
9869 bool sel
, bool control
,
9870 bool shift
, bool alt
, bool meta
)
9871 : wxNotifyEvent( type
, id
)
9873 m_topLeft
= topLeft
;
9874 m_bottomRight
= bottomRight
;
9876 m_control
= control
;
9881 SetEventObject(obj
);
9885 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9887 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9888 wxObject
* obj
, int row
,
9889 int col
, wxControl
* ctrl
)
9890 : wxCommandEvent(type
, id
)
9892 SetEventObject(obj
);
9899 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9901 #endif // wxUSE_GRID