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 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
95 // without rewriting the macros, which require a public copy constructor.
98 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
100 #include "wx/arrimpl.cpp"
102 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
103 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
121 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
122 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
123 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
124 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
133 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
134 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
135 const wxPoint
&pos
, const wxSize
&size
);
140 void OnPaint( wxPaintEvent
& event
);
141 void OnMouseEvent( wxMouseEvent
& event
);
142 void OnMouseWheel( wxMouseEvent
& event
);
143 void OnKeyDown( wxKeyEvent
& event
);
144 void OnKeyUp( wxKeyEvent
& );
146 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
147 DECLARE_EVENT_TABLE()
148 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
152 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
155 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
156 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
157 const wxPoint
&pos
, const wxSize
&size
);
162 void OnPaint( wxPaintEvent
&event
);
163 void OnMouseEvent( wxMouseEvent
& event
);
164 void OnMouseWheel( wxMouseEvent
& event
);
165 void OnKeyDown( wxKeyEvent
& event
);
166 void OnKeyUp( wxKeyEvent
& );
168 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
169 DECLARE_EVENT_TABLE()
170 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
174 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
177 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
178 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
179 const wxPoint
&pos
, const wxSize
&size
);
184 void OnMouseEvent( wxMouseEvent
& event
);
185 void OnMouseWheel( wxMouseEvent
& event
);
186 void OnKeyDown( wxKeyEvent
& event
);
187 void OnKeyUp( wxKeyEvent
& );
188 void OnPaint( wxPaintEvent
& event
);
190 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
191 DECLARE_EVENT_TABLE()
192 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
195 class WXDLLEXPORT wxGridWindow
: public wxWindow
200 m_owner
= (wxGrid
*)NULL
;
201 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
202 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
205 wxGridWindow( wxGrid
*parent
,
206 wxGridRowLabelWindow
*rowLblWin
,
207 wxGridColLabelWindow
*colLblWin
,
208 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
211 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
213 wxGrid
* GetOwner() { return m_owner
; }
217 wxGridRowLabelWindow
*m_rowLabelWin
;
218 wxGridColLabelWindow
*m_colLabelWin
;
220 void OnPaint( wxPaintEvent
&event
);
221 void OnMouseWheel( wxMouseEvent
& event
);
222 void OnMouseEvent( wxMouseEvent
& event
);
223 void OnKeyDown( wxKeyEvent
& );
224 void OnKeyUp( wxKeyEvent
& );
225 void OnEraseBackground( wxEraseEvent
& );
228 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
229 DECLARE_EVENT_TABLE()
230 DECLARE_NO_COPY_CLASS(wxGridWindow
)
235 class wxGridCellEditorEvtHandler
: public wxEvtHandler
238 wxGridCellEditorEvtHandler()
239 : m_grid(0), m_editor(0)
241 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
242 : m_grid(grid
), m_editor(editor
)
245 void OnKeyDown(wxKeyEvent
& event
);
246 void OnChar(wxKeyEvent
& event
);
250 wxGridCellEditor
* m_editor
;
251 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
252 DECLARE_EVENT_TABLE()
253 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
257 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
258 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
259 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
260 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
265 // ----------------------------------------------------------------------------
266 // the internal data representation used by wxGridCellAttrProvider
267 // ----------------------------------------------------------------------------
269 // this class stores attributes set for cells
270 class WXDLLEXPORT wxGridCellAttrData
273 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
274 wxGridCellAttr
*GetAttr(int row
, int col
) const;
275 void UpdateAttrRows( size_t pos
, int numRows
);
276 void UpdateAttrCols( size_t pos
, int numCols
);
279 // searches for the attr for given cell, returns wxNOT_FOUND if not found
280 int FindIndex(int row
, int col
) const;
282 wxGridCellWithAttrArray m_attrs
;
285 // this class stores attributes set for rows or columns
286 class WXDLLEXPORT wxGridRowOrColAttrData
289 // empty ctor to suppress warnings
290 wxGridRowOrColAttrData() { }
291 ~wxGridRowOrColAttrData();
293 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
294 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
295 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
298 wxArrayInt m_rowsOrCols
;
299 wxArrayAttrs m_attrs
;
302 // NB: this is just a wrapper around 3 objects: one which stores cell
303 // attributes, and 2 others for row/col ones
304 class WXDLLEXPORT wxGridCellAttrProviderData
307 wxGridCellAttrData m_cellAttrs
;
308 wxGridRowOrColAttrData m_rowAttrs
,
313 // ----------------------------------------------------------------------------
314 // data structures used for the data type registry
315 // ----------------------------------------------------------------------------
317 struct wxGridDataTypeInfo
319 wxGridDataTypeInfo(const wxString
& typeName
,
320 wxGridCellRenderer
* renderer
,
321 wxGridCellEditor
* editor
)
322 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
325 ~wxGridDataTypeInfo()
327 wxSafeDecRef(m_renderer
);
328 wxSafeDecRef(m_editor
);
332 wxGridCellRenderer
* m_renderer
;
333 wxGridCellEditor
* m_editor
;
335 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
339 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
342 class WXDLLEXPORT wxGridTypeRegistry
345 wxGridTypeRegistry() {}
346 ~wxGridTypeRegistry();
348 void RegisterDataType(const wxString
& typeName
,
349 wxGridCellRenderer
* renderer
,
350 wxGridCellEditor
* editor
);
352 // find one of already registered data types
353 int FindRegisteredDataType(const wxString
& typeName
);
355 // try to FindRegisteredDataType(), if this fails and typeName is one of
356 // standard typenames, register it and return its index
357 int FindDataType(const wxString
& typeName
);
359 // try to FindDataType(), if it fails see if it is not one of already
360 // registered data types with some params in which case clone the
361 // registered data type and set params for it
362 int FindOrCloneDataType(const wxString
& typeName
);
364 wxGridCellRenderer
* GetRenderer(int index
);
365 wxGridCellEditor
* GetEditor(int index
);
368 wxGridDataTypeInfoArray m_typeinfo
;
371 // ----------------------------------------------------------------------------
372 // conditional compilation
373 // ----------------------------------------------------------------------------
375 #ifndef WXGRID_DRAW_LINES
376 #define WXGRID_DRAW_LINES 1
379 // ----------------------------------------------------------------------------
381 // ----------------------------------------------------------------------------
383 //#define DEBUG_ATTR_CACHE
384 #ifdef DEBUG_ATTR_CACHE
385 static size_t gs_nAttrCacheHits
= 0;
386 static size_t gs_nAttrCacheMisses
= 0;
387 #endif // DEBUG_ATTR_CACHE
389 // ----------------------------------------------------------------------------
391 // ----------------------------------------------------------------------------
393 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
394 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
397 // TODO: this doesn't work at all, grid cells have different sizes and approx
398 // calculations don't work as because of the size mismatch scrollbars
399 // sometimes fail to be shown when they should be or vice versa
401 // The scroll bars may be a little flakey once in a while, but that is
402 // surely much less horrible than having scroll lines of only 1!!!
405 // Well, it's still seriously broken so it might be better but needs
408 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
409 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
411 // the size of hash tables used a bit everywhere (the max number of elements
412 // in these hash tables is the number of rows/columns)
413 static const int GRID_HASH_SIZE
= 100;
415 // ----------------------------------------------------------------------------
417 // ----------------------------------------------------------------------------
419 static inline int GetScrollX(int x
)
421 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
424 static inline int GetScrollY(int y
)
426 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
429 // ============================================================================
431 // ============================================================================
433 // ----------------------------------------------------------------------------
435 // ----------------------------------------------------------------------------
437 wxGridCellEditor::wxGridCellEditor()
444 wxGridCellEditor::~wxGridCellEditor()
449 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
450 wxWindowID
WXUNUSED(id
),
451 wxEvtHandler
* evtHandler
)
454 m_control
->PushEventHandler(evtHandler
);
457 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
458 wxGridCellAttr
*attr
)
460 // erase the background because we might not fill the cell
461 wxClientDC
dc(m_control
->GetParent());
462 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
464 gridWindow
->GetOwner()->PrepareDC(dc
);
466 dc
.SetPen(*wxTRANSPARENT_PEN
);
467 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
468 dc
.DrawRectangle(rectCell
);
470 // redraw the control we just painted over
471 m_control
->Refresh();
474 void wxGridCellEditor::Destroy()
478 m_control
->PopEventHandler(TRUE
/* delete it*/);
480 m_control
->Destroy();
485 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
487 wxASSERT_MSG(m_control
,
488 wxT("The wxGridCellEditor must be Created first!"));
489 m_control
->Show(show
);
493 // set the colours/fonts if we have any
496 m_colFgOld
= m_control
->GetForegroundColour();
497 m_control
->SetForegroundColour(attr
->GetTextColour());
499 m_colBgOld
= m_control
->GetBackgroundColour();
500 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
502 m_fontOld
= m_control
->GetFont();
503 m_control
->SetFont(attr
->GetFont());
505 // can't do anything more in the base class version, the other
506 // attributes may only be used by the derived classes
511 // restore the standard colours fonts
512 if ( m_colFgOld
.Ok() )
514 m_control
->SetForegroundColour(m_colFgOld
);
515 m_colFgOld
= wxNullColour
;
518 if ( m_colBgOld
.Ok() )
520 m_control
->SetBackgroundColour(m_colBgOld
);
521 m_colBgOld
= wxNullColour
;
524 if ( m_fontOld
.Ok() )
526 m_control
->SetFont(m_fontOld
);
527 m_fontOld
= wxNullFont
;
532 void wxGridCellEditor::SetSize(const wxRect
& rect
)
534 wxASSERT_MSG(m_control
,
535 wxT("The wxGridCellEditor must be Created first!"));
536 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
539 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
544 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
546 // accept the simple key presses, not anything with Ctrl/Alt/Meta
547 return !(event
.ControlDown() || event
.AltDown());
550 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
555 void wxGridCellEditor::StartingClick()
561 // ----------------------------------------------------------------------------
562 // wxGridCellTextEditor
563 // ----------------------------------------------------------------------------
565 wxGridCellTextEditor::wxGridCellTextEditor()
570 void wxGridCellTextEditor::Create(wxWindow
* parent
,
572 wxEvtHandler
* evtHandler
)
574 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
575 wxDefaultPosition
, wxDefaultSize
576 #if defined(__WXMSW__)
577 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
578 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
582 // TODO: use m_maxChars
584 wxGridCellEditor::Create(parent
, id
, evtHandler
);
587 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
588 wxGridCellAttr
* WXUNUSED(attr
))
590 // as we fill the entire client area, don't do anything here to minimize
594 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
596 wxRect
rect(rectOrig
);
598 // Make the edit control large enough to allow for internal
601 // TODO: remove this if the text ctrl sizing is improved esp. for
604 #if defined(__WXGTK__)
613 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
615 // MB: treat MSW separately here otherwise the caret doesn't show
616 // when the editor is in the first row.
617 #if defined(__WXMSW__)
620 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
623 #if defined(__WXMOTIF__)
627 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
628 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
629 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
630 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
633 wxGridCellEditor::SetSize(rect
);
636 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
638 wxASSERT_MSG(m_control
,
639 wxT("The wxGridCellEditor must be Created first!"));
641 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
643 DoBeginEdit(m_startValue
);
646 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
648 Text()->SetValue(startValue
);
649 Text()->SetInsertionPointEnd();
650 Text()->SetSelection(-1,-1);
654 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
657 wxASSERT_MSG(m_control
,
658 wxT("The wxGridCellEditor must be Created first!"));
660 bool changed
= FALSE
;
661 wxString value
= Text()->GetValue();
662 if (value
!= m_startValue
)
666 grid
->GetTable()->SetValue(row
, col
, value
);
668 m_startValue
= wxEmptyString
;
669 Text()->SetValue(m_startValue
);
675 void wxGridCellTextEditor::Reset()
677 wxASSERT_MSG(m_control
,
678 wxT("The wxGridCellEditor must be Created first!"));
680 DoReset(m_startValue
);
683 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
685 Text()->SetValue(startValue
);
686 Text()->SetInsertionPointEnd();
689 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
691 if ( wxGridCellEditor::IsAcceptedKey(event
) )
693 int keycode
= event
.GetKeyCode();
707 case WXK_NUMPAD_MULTIPLY
:
711 case WXK_NUMPAD_SUBTRACT
:
713 case WXK_NUMPAD_DECIMAL
:
715 case WXK_NUMPAD_DIVIDE
:
719 // accept 8 bit chars too if isprint() agrees
720 if ( (keycode
< 255) && (isprint(keycode
)) )
728 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
730 if ( !Text()->EmulateKeyPress(event
) )
736 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
737 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
739 #if defined(__WXMOTIF__) || defined(__WXGTK__)
740 // wxMotif needs a little extra help...
741 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
742 wxString
s( Text()->GetValue() );
743 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
745 Text()->SetInsertionPoint( pos
);
747 // the other ports can handle a Return key press
753 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
763 if ( !params
.ToLong(&tmp
) )
765 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
769 m_maxChars
= (size_t)tmp
;
774 // return the value in the text control
775 wxString
wxGridCellTextEditor::GetValue() const
777 return Text()->GetValue();
780 // ----------------------------------------------------------------------------
781 // wxGridCellNumberEditor
782 // ----------------------------------------------------------------------------
784 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
790 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
792 wxEvtHandler
* evtHandler
)
796 // create a spin ctrl
797 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
798 wxDefaultPosition
, wxDefaultSize
,
802 wxGridCellEditor::Create(parent
, id
, evtHandler
);
806 // just a text control
807 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
810 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
811 #endif // wxUSE_VALIDATORS
815 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
817 // first get the value
818 wxGridTableBase
*table
= grid
->GetTable();
819 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
821 m_valueOld
= table
->GetValueAsLong(row
, col
);
826 wxString sValue
= table
->GetValue(row
, col
);
827 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
829 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
836 Spin()->SetValue((int)m_valueOld
);
841 DoBeginEdit(GetString());
845 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
854 value
= Spin()->GetValue();
855 changed
= value
!= m_valueOld
;
857 text
= wxString::Format(wxT("%ld"), value
);
861 text
= Text()->GetValue();
862 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
867 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
868 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
870 grid
->GetTable()->SetValue(row
, col
, text
);
876 void wxGridCellNumberEditor::Reset()
880 Spin()->SetValue((int)m_valueOld
);
884 DoReset(GetString());
888 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
890 if ( wxGridCellEditor::IsAcceptedKey(event
) )
892 int keycode
= event
.GetKeyCode();
908 case WXK_NUMPAD_SUBTRACT
:
914 if ( (keycode
< 128) && isdigit(keycode
) )
922 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
926 int keycode
= (int) event
.KeyCode();
927 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-'
928 || keycode
== WXK_NUMPAD0
929 || keycode
== WXK_NUMPAD1
930 || keycode
== WXK_NUMPAD2
931 || keycode
== WXK_NUMPAD3
932 || keycode
== WXK_NUMPAD4
933 || keycode
== WXK_NUMPAD5
934 || keycode
== WXK_NUMPAD6
935 || keycode
== WXK_NUMPAD7
936 || keycode
== WXK_NUMPAD8
937 || keycode
== WXK_NUMPAD9
938 || keycode
== WXK_ADD
939 || keycode
== WXK_NUMPAD_ADD
940 || keycode
== WXK_SUBTRACT
941 || keycode
== WXK_NUMPAD_SUBTRACT
)
943 wxGridCellTextEditor::StartingKey(event
);
953 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
964 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
968 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
972 // skip the error message below
977 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
981 // return the value in the spin control if it is there (the text control otherwise)
982 wxString
wxGridCellNumberEditor::GetValue() const
988 long value
= Spin()->GetValue();
989 s
.Printf(wxT("%ld"), value
);
993 s
= Text()->GetValue();
998 // ----------------------------------------------------------------------------
999 // wxGridCellFloatEditor
1000 // ----------------------------------------------------------------------------
1002 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1005 m_precision
= precision
;
1008 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1010 wxEvtHandler
* evtHandler
)
1012 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1014 #if wxUSE_VALIDATORS
1015 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1016 #endif // wxUSE_VALIDATORS
1019 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1021 // first get the value
1022 wxGridTableBase
*table
= grid
->GetTable();
1023 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1025 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1030 wxString sValue
= table
->GetValue(row
, col
);
1031 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1033 wxFAIL_MSG( _T("this cell doesn't have float value") );
1038 DoBeginEdit(GetString());
1041 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1045 wxString
text(Text()->GetValue());
1047 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1049 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1050 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1052 grid
->GetTable()->SetValue(row
, col
, text
);
1059 void wxGridCellFloatEditor::Reset()
1061 DoReset(GetString());
1064 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1066 int keycode
= (int)event
.KeyCode();
1067 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1068 || keycode
== WXK_NUMPAD0
1069 || keycode
== WXK_NUMPAD1
1070 || keycode
== WXK_NUMPAD2
1071 || keycode
== WXK_NUMPAD3
1072 || keycode
== WXK_NUMPAD4
1073 || keycode
== WXK_NUMPAD5
1074 || keycode
== WXK_NUMPAD6
1075 || keycode
== WXK_NUMPAD7
1076 || keycode
== WXK_NUMPAD8
1077 || keycode
== WXK_NUMPAD9
1078 || keycode
== WXK_ADD
1079 || keycode
== WXK_NUMPAD_ADD
1080 || keycode
== WXK_SUBTRACT
1081 || keycode
== WXK_NUMPAD_SUBTRACT
)
1083 wxGridCellTextEditor::StartingKey(event
);
1085 // skip Skip() below
1092 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1103 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1107 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1109 m_precision
= (int)tmp
;
1111 // skip the error message below
1116 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1120 wxString
wxGridCellFloatEditor::GetString() const
1123 if ( m_width
== -1 )
1125 // default width/precision
1128 else if ( m_precision
== -1 )
1130 // default precision
1131 fmt
.Printf(_T("%%%d.f"), m_width
);
1135 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1138 return wxString::Format(fmt
, m_valueOld
);
1141 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1143 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1145 int keycode
= event
.GetKeyCode();
1159 case WXK_NUMPAD_ADD
:
1161 case WXK_NUMPAD_SUBTRACT
:
1163 case WXK_NUMPAD_DECIMAL
:
1167 // additionally accept 'e' as in '1e+6'
1168 if ( (keycode
< 128) &&
1169 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1177 #endif // wxUSE_TEXTCTRL
1181 // ----------------------------------------------------------------------------
1182 // wxGridCellBoolEditor
1183 // ----------------------------------------------------------------------------
1185 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1187 wxEvtHandler
* evtHandler
)
1189 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1190 wxDefaultPosition
, wxDefaultSize
,
1193 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1196 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1198 bool resize
= FALSE
;
1199 wxSize size
= m_control
->GetSize();
1200 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1202 // check if the checkbox is not too big/small for this cell
1203 wxSize sizeBest
= m_control
->GetBestSize();
1204 if ( !(size
== sizeBest
) )
1206 // reset to default size if it had been made smaller
1212 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1214 // leave 1 pixel margin
1215 size
.x
= size
.y
= minSize
- 2;
1222 m_control
->SetSize(size
);
1225 // position it in the centre of the rectangle (TODO: support alignment?)
1227 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1228 // the checkbox without label still has some space to the right in wxGTK,
1229 // so shift it to the right
1231 #elif defined(__WXMSW__)
1232 // here too, but in other way
1237 int hAlign
= wxALIGN_CENTRE
;
1238 int vAlign
= wxALIGN_CENTRE
;
1240 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1243 if (hAlign
== wxALIGN_LEFT
)
1249 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1251 else if (hAlign
== wxALIGN_RIGHT
)
1253 x
= r
.x
+ r
.width
- size
.x
- 2;
1254 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1256 else if (hAlign
== wxALIGN_CENTRE
)
1258 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1259 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1262 m_control
->Move(x
, y
);
1265 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1267 m_control
->Show(show
);
1271 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1272 CBox()->SetBackgroundColour(colBg
);
1276 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1278 wxASSERT_MSG(m_control
,
1279 wxT("The wxGridCellEditor must be Created first!"));
1281 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1282 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1285 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1286 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1288 CBox()->SetValue(m_startValue
);
1292 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1295 wxASSERT_MSG(m_control
,
1296 wxT("The wxGridCellEditor must be Created first!"));
1298 bool changed
= FALSE
;
1299 bool value
= CBox()->GetValue();
1300 if ( value
!= m_startValue
)
1305 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1306 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1308 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1314 void wxGridCellBoolEditor::Reset()
1316 wxASSERT_MSG(m_control
,
1317 wxT("The wxGridCellEditor must be Created first!"));
1319 CBox()->SetValue(m_startValue
);
1322 void wxGridCellBoolEditor::StartingClick()
1324 CBox()->SetValue(!CBox()->GetValue());
1327 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1329 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1331 int keycode
= event
.GetKeyCode();
1335 case WXK_NUMPAD_MULTIPLY
:
1337 case WXK_NUMPAD_ADD
:
1339 case WXK_NUMPAD_SUBTRACT
:
1350 // return the value as "1" for true and the empty string for false
1351 wxString
wxGridCellBoolEditor::GetValue() const
1353 bool bSet
= CBox()->GetValue();
1354 return bSet
? _T("1") : wxEmptyString
;
1357 #endif // wxUSE_CHECKBOX
1361 // ----------------------------------------------------------------------------
1362 // wxGridCellChoiceEditor
1363 // ----------------------------------------------------------------------------
1365 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1366 const wxString choices
[],
1368 : m_allowOthers(allowOthers
)
1372 m_choices
.Alloc(count
);
1373 for ( size_t n
= 0; n
< count
; n
++ )
1375 m_choices
.Add(choices
[n
]);
1380 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1382 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1383 editor
->m_allowOthers
= m_allowOthers
;
1384 editor
->m_choices
= m_choices
;
1389 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1391 wxEvtHandler
* evtHandler
)
1393 size_t count
= m_choices
.GetCount();
1394 wxString
*choices
= new wxString
[count
];
1395 for ( size_t n
= 0; n
< count
; n
++ )
1397 choices
[n
] = m_choices
[n
];
1400 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1401 wxDefaultPosition
, wxDefaultSize
,
1403 m_allowOthers
? 0 : wxCB_READONLY
);
1407 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1410 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1411 wxGridCellAttr
* attr
)
1413 // as we fill the entire client area, don't do anything here to minimize
1416 // TODO: It doesn't actually fill the client area since the height of a
1417 // combo always defaults to the standard... Until someone has time to
1418 // figure out the right rectangle to paint, just do it the normal way...
1419 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1422 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1424 wxASSERT_MSG(m_control
,
1425 wxT("The wxGridCellEditor must be Created first!"));
1427 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1430 Combo()->SetValue(m_startValue
);
1433 // find the right position, or default to the first if not found
1434 int pos
= Combo()->FindString(m_startValue
);
1437 Combo()->SetSelection(pos
);
1439 Combo()->SetInsertionPointEnd();
1440 Combo()->SetFocus();
1443 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1446 wxString value
= Combo()->GetValue();
1447 bool changed
= value
!= m_startValue
;
1450 grid
->GetTable()->SetValue(row
, col
, value
);
1452 m_startValue
= wxEmptyString
;
1454 Combo()->SetValue(m_startValue
);
1456 Combo()->SetSelection(0);
1461 void wxGridCellChoiceEditor::Reset()
1463 Combo()->SetValue(m_startValue
);
1464 Combo()->SetInsertionPointEnd();
1467 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1477 wxStringTokenizer
tk(params
, _T(','));
1478 while ( tk
.HasMoreTokens() )
1480 m_choices
.Add(tk
.GetNextToken());
1484 // return the value in the text control
1485 wxString
wxGridCellChoiceEditor::GetValue() const
1487 return Combo()->GetValue();
1490 #endif // wxUSE_COMBOBOX
1492 // ----------------------------------------------------------------------------
1493 // wxGridCellEditorEvtHandler
1494 // ----------------------------------------------------------------------------
1496 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1498 switch ( event
.KeyCode() )
1502 m_grid
->DisableCellEditControl();
1506 m_grid
->GetEventHandler()->ProcessEvent( event
);
1510 case WXK_NUMPAD_ENTER
:
1511 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1512 m_editor
->HandleReturn(event
);
1521 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1523 switch ( event
.KeyCode() )
1528 case WXK_NUMPAD_ENTER
:
1536 // ----------------------------------------------------------------------------
1537 // wxGridCellWorker is an (almost) empty common base class for
1538 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1539 // ----------------------------------------------------------------------------
1541 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1546 wxGridCellWorker::~wxGridCellWorker()
1550 // ============================================================================
1552 // ============================================================================
1554 // ----------------------------------------------------------------------------
1555 // wxGridCellRenderer
1556 // ----------------------------------------------------------------------------
1558 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1559 wxGridCellAttr
& attr
,
1562 int WXUNUSED(row
), int WXUNUSED(col
),
1565 dc
.SetBackgroundMode( wxSOLID
);
1567 // grey out fields if the grid is disabled
1568 if( grid
.IsEnabled() )
1572 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1576 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1581 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1584 dc
.SetPen( *wxTRANSPARENT_PEN
);
1585 dc
.DrawRectangle(rect
);
1588 // ----------------------------------------------------------------------------
1589 // wxGridCellStringRenderer
1590 // ----------------------------------------------------------------------------
1592 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1593 wxGridCellAttr
& attr
,
1597 dc
.SetBackgroundMode( wxTRANSPARENT
);
1599 // TODO some special colours for attr.IsReadOnly() case?
1601 // different coloured text when the grid is disabled
1602 if( grid
.IsEnabled() )
1606 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1607 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1611 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1612 dc
.SetTextForeground( attr
.GetTextColour() );
1617 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1618 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1621 dc
.SetFont( attr
.GetFont() );
1624 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1626 const wxString
& text
)
1628 wxCoord x
= 0, y
= 0, max_x
= 0;
1629 dc
.SetFont(attr
.GetFont());
1630 wxStringTokenizer
tk(text
, _T('\n'));
1631 while ( tk
.HasMoreTokens() )
1633 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1634 max_x
= wxMax(max_x
, x
);
1637 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1639 return wxSize(max_x
, y
);
1642 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1643 wxGridCellAttr
& attr
,
1647 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1650 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1651 wxGridCellAttr
& attr
,
1653 const wxRect
& rectCell
,
1657 wxRect rect
= rectCell
;
1660 // erase only this cells background, overflow cells should have been erased
1661 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1664 attr
.GetAlignment(&hAlign
, &vAlign
);
1666 int overflowCols
= 0;
1668 if (attr
.GetOverflow())
1670 int cols
= grid
.GetNumberCols();
1671 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1672 int cell_rows
, cell_cols
;
1673 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1674 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1676 int i
, c_cols
, c_rows
;
1677 for (i
= col
+cell_cols
; i
< cols
; i
++)
1679 bool is_empty
= TRUE
;
1680 for (int j
=row
; j
<row
+cell_rows
; j
++)
1682 // check w/ anchor cell for multicell block
1683 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1684 if (c_rows
> 0) c_rows
= 0;
1685 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1692 rect
.width
+= grid
.GetColSize(i
);
1698 if (rect
.width
>= best_width
) break;
1700 overflowCols
= i
- col
- cell_cols
+ 1;
1701 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1704 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1706 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1708 clip
.x
+= rectCell
.width
;
1709 // draw each overflow cell individually
1710 int col_end
= col
+cell_cols
+overflowCols
;
1711 if (col_end
>= grid
.GetNumberCols())
1712 col_end
= grid
.GetNumberCols() - 1;
1713 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1715 clip
.width
= grid
.GetColSize(i
) - 1;
1716 dc
.DestroyClippingRegion();
1717 dc
.SetClippingRegion(clip
);
1719 SetTextColoursAndFont(grid
, attr
, dc
,
1720 grid
.IsInSelection(row
,i
));
1722 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1723 rect
, hAlign
, vAlign
);
1724 clip
.x
+= grid
.GetColSize(i
) - 1;
1730 dc
.DestroyClippingRegion();
1734 // now we only have to draw the text
1735 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1737 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1738 rect
, hAlign
, vAlign
);
1741 // ----------------------------------------------------------------------------
1742 // wxGridCellNumberRenderer
1743 // ----------------------------------------------------------------------------
1745 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1747 wxGridTableBase
*table
= grid
.GetTable();
1749 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1751 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1755 text
= table
->GetValue(row
, col
);
1761 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1762 wxGridCellAttr
& attr
,
1764 const wxRect
& rectCell
,
1768 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1770 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1772 // draw the text right aligned by default
1774 attr
.GetAlignment(&hAlign
, &vAlign
);
1775 hAlign
= wxALIGN_RIGHT
;
1777 wxRect rect
= rectCell
;
1780 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1783 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1784 wxGridCellAttr
& attr
,
1788 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1791 // ----------------------------------------------------------------------------
1792 // wxGridCellFloatRenderer
1793 // ----------------------------------------------------------------------------
1795 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1798 SetPrecision(precision
);
1801 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1803 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1804 renderer
->m_width
= m_width
;
1805 renderer
->m_precision
= m_precision
;
1806 renderer
->m_format
= m_format
;
1811 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1813 wxGridTableBase
*table
= grid
.GetTable();
1818 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1820 val
= table
->GetValueAsDouble(row
, col
);
1825 text
= table
->GetValue(row
, col
);
1826 hasDouble
= text
.ToDouble(&val
);
1833 if ( m_width
== -1 )
1835 if ( m_precision
== -1 )
1837 // default width/precision
1838 m_format
= _T("%f");
1842 m_format
.Printf(_T("%%.%df"), m_precision
);
1845 else if ( m_precision
== -1 )
1847 // default precision
1848 m_format
.Printf(_T("%%%d.f"), m_width
);
1852 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1856 text
.Printf(m_format
, val
);
1859 //else: text already contains the string
1864 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1865 wxGridCellAttr
& attr
,
1867 const wxRect
& rectCell
,
1871 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1873 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1875 // draw the text right aligned by default
1877 attr
.GetAlignment(&hAlign
, &vAlign
);
1878 hAlign
= wxALIGN_RIGHT
;
1880 wxRect rect
= rectCell
;
1883 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1886 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1887 wxGridCellAttr
& attr
,
1891 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1894 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1898 // reset to defaults
1904 wxString tmp
= params
.BeforeFirst(_T(','));
1908 if ( tmp
.ToLong(&width
) )
1910 SetWidth((int)width
);
1914 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1918 tmp
= params
.AfterFirst(_T(','));
1922 if ( tmp
.ToLong(&precision
) )
1924 SetPrecision((int)precision
);
1928 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1936 // ----------------------------------------------------------------------------
1937 // wxGridCellBoolRenderer
1938 // ----------------------------------------------------------------------------
1940 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1942 // FIXME these checkbox size calculations are really ugly...
1944 // between checkmark and box
1945 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1947 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1948 wxGridCellAttr
& WXUNUSED(attr
),
1953 // compute it only once (no locks for MT safeness in GUI thread...)
1954 if ( !ms_sizeCheckMark
.x
)
1956 // get checkbox size
1957 wxCoord checkSize
= 0;
1958 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1959 wxSize size
= checkbox
->GetBestSize();
1960 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1962 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1963 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1964 checkSize
-= size
.y
/ 2;
1969 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1972 return ms_sizeCheckMark
;
1975 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1976 wxGridCellAttr
& attr
,
1982 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1984 // draw a check mark in the centre (ignoring alignment - TODO)
1985 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1987 // don't draw outside the cell
1988 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1989 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1991 // and even leave (at least) 1 pixel margin
1992 size
.x
= size
.y
= minSize
- 2;
1995 // draw a border around checkmark
1997 attr
.GetAlignment(& hAlign
, &vAlign
);
2000 if (hAlign
== wxALIGN_CENTRE
)
2002 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2003 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2004 rectBorder
.width
= size
.x
;
2005 rectBorder
.height
= size
.y
;
2007 else if (hAlign
== wxALIGN_LEFT
)
2009 rectBorder
.x
= rect
.x
+ 2;
2010 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2011 rectBorder
.width
= size
.x
;
2012 rectBorder
.height
= size
.y
;
2014 else if (hAlign
== wxALIGN_RIGHT
)
2016 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2017 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2018 rectBorder
.width
= size
.x
;
2019 rectBorder
.height
= size
.y
;
2023 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2024 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2027 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2028 value
= !( !cellval
|| (cellval
== wxT("0")) );
2033 wxRect rectMark
= rectBorder
;
2035 // MSW DrawCheckMark() is weird (and should probably be changed...)
2036 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2040 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2043 dc
.SetTextForeground(attr
.GetTextColour());
2044 dc
.DrawCheckMark(rectMark
);
2047 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2048 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2049 dc
.DrawRectangle(rectBorder
);
2052 // ----------------------------------------------------------------------------
2054 // ----------------------------------------------------------------------------
2056 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2060 m_isReadOnly
= Unset
;
2065 m_attrkind
= wxGridCellAttr::Cell
;
2067 m_sizeRows
= m_sizeCols
= 1;
2070 SetDefAttr(attrDefault
);
2073 wxGridCellAttr
*wxGridCellAttr::Clone() const
2075 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2077 if ( HasTextColour() )
2078 attr
->SetTextColour(GetTextColour());
2079 if ( HasBackgroundColour() )
2080 attr
->SetBackgroundColour(GetBackgroundColour());
2082 attr
->SetFont(GetFont());
2083 if ( HasAlignment() )
2084 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2086 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2090 attr
->SetRenderer(m_renderer
);
2091 m_renderer
->IncRef();
2095 attr
->SetEditor(m_editor
);
2100 attr
->SetReadOnly();
2102 attr
->SetKind( m_attrkind
);
2107 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2109 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2110 SetTextColour(mergefrom
->GetTextColour());
2111 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2112 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2113 if ( !HasFont() && mergefrom
->HasFont() )
2114 SetFont(mergefrom
->GetFont());
2115 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2117 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2118 SetAlignment(hAlign
, vAlign
);
2121 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2123 // Directly access member functions as GetRender/Editor don't just return
2124 // m_renderer/m_editor
2126 // Maybe add support for merge of Render and Editor?
2127 if (!HasRenderer() && mergefrom
->HasRenderer() )
2129 m_renderer
= mergefrom
->m_renderer
;
2130 m_renderer
->IncRef();
2132 if ( !HasEditor() && mergefrom
->HasEditor() )
2134 m_editor
= mergefrom
->m_editor
;
2137 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2138 SetReadOnly(mergefrom
->IsReadOnly());
2140 SetDefAttr(mergefrom
->m_defGridAttr
);
2143 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2145 // The size of a cell is normally 1,1
2147 // If this cell is larger (2,2) then this is the top left cell
2148 // the other cells that will be covered (lower right cells) must be
2149 // set to negative or zero values such that
2150 // row + num_rows of the covered cell points to the larger cell (this cell)
2151 // same goes for the col + num_cols.
2153 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2155 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2156 !((num_rows
<=0)&&(num_cols
>0)) ||
2157 !((num_rows
==0)&&(num_cols
==0))),
2158 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2160 m_sizeRows
= num_rows
;
2161 m_sizeCols
= num_cols
;
2164 const wxColour
& wxGridCellAttr::GetTextColour() const
2166 if (HasTextColour())
2170 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2172 return m_defGridAttr
->GetTextColour();
2176 wxFAIL_MSG(wxT("Missing default cell attribute"));
2177 return wxNullColour
;
2182 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2184 if (HasBackgroundColour())
2186 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2187 return m_defGridAttr
->GetBackgroundColour();
2190 wxFAIL_MSG(wxT("Missing default cell attribute"));
2191 return wxNullColour
;
2196 const wxFont
& wxGridCellAttr::GetFont() const
2200 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2201 return m_defGridAttr
->GetFont();
2204 wxFAIL_MSG(wxT("Missing default cell attribute"));
2210 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2214 if ( hAlign
) *hAlign
= m_hAlign
;
2215 if ( vAlign
) *vAlign
= m_vAlign
;
2217 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2218 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2221 wxFAIL_MSG(wxT("Missing default cell attribute"));
2225 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2227 if ( num_rows
) *num_rows
= m_sizeRows
;
2228 if ( num_cols
) *num_cols
= m_sizeCols
;
2231 // GetRenderer and GetEditor use a slightly different decision path about
2232 // which attribute to use. If a non-default attr object has one then it is
2233 // used, otherwise the default editor or renderer is fetched from the grid and
2234 // used. It should be the default for the data type of the cell. If it is
2235 // NULL (because the table has a type that the grid does not have in its
2236 // registry,) then the grid's default editor or renderer is used.
2238 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2240 wxGridCellRenderer
*renderer
;
2242 if ( m_renderer
&& this != m_defGridAttr
)
2244 // use the cells renderer if it has one
2245 renderer
= m_renderer
;
2248 else // no non default cell renderer
2250 // get default renderer for the data type
2253 // GetDefaultRendererForCell() will do IncRef() for us
2254 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2263 if (m_defGridAttr
&& this != m_defGridAttr
)
2265 // if we still don't have one then use the grid default
2266 // (no need for IncRef() here neither)
2267 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2269 else // default grid attr
2271 // use m_renderer which we had decided not to use initially
2272 renderer
= m_renderer
;
2279 // we're supposed to always find something
2280 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2285 // same as above, except for s/renderer/editor/g
2286 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2288 wxGridCellEditor
*editor
;
2290 if ( m_editor
&& this != m_defGridAttr
)
2292 // use the cells editor if it has one
2296 else // no non default cell editor
2298 // get default editor for the data type
2301 // GetDefaultEditorForCell() will do IncRef() for us
2302 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2311 if ( m_defGridAttr
&& this != m_defGridAttr
)
2313 // if we still don't have one then use the grid default
2314 // (no need for IncRef() here neither)
2315 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2317 else // default grid attr
2319 // use m_editor which we had decided not to use initially
2327 // we're supposed to always find something
2328 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2333 // ----------------------------------------------------------------------------
2334 // wxGridCellAttrData
2335 // ----------------------------------------------------------------------------
2337 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2339 int n
= FindIndex(row
, col
);
2340 if ( n
== wxNOT_FOUND
)
2342 // add the attribute
2343 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2347 // free the old attribute
2348 m_attrs
[(size_t)n
].attr
->DecRef();
2352 // change the attribute
2353 m_attrs
[(size_t)n
].attr
= attr
;
2357 // remove this attribute
2358 m_attrs
.RemoveAt((size_t)n
);
2363 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2365 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2367 int n
= FindIndex(row
, col
);
2368 if ( n
!= wxNOT_FOUND
)
2370 attr
= m_attrs
[(size_t)n
].attr
;
2377 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2379 size_t count
= m_attrs
.GetCount();
2380 for ( size_t n
= 0; n
< count
; n
++ )
2382 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2383 wxCoord row
= coords
.GetRow();
2384 if ((size_t)row
>= pos
)
2388 // If rows inserted, include row counter where necessary
2389 coords
.SetRow(row
+ numRows
);
2391 else if (numRows
< 0)
2393 // If rows deleted ...
2394 if ((size_t)row
>= pos
- numRows
)
2396 // ...either decrement row counter (if row still exists)...
2397 coords
.SetRow(row
+ numRows
);
2401 // ...or remove the attribute
2402 m_attrs
.RemoveAt((size_t)n
);
2410 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2412 size_t count
= m_attrs
.GetCount();
2413 for ( size_t n
= 0; n
< count
; n
++ )
2415 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2416 wxCoord col
= coords
.GetCol();
2417 if ( (size_t)col
>= pos
)
2421 // If rows inserted, include row counter where necessary
2422 coords
.SetCol(col
+ numCols
);
2424 else if (numCols
< 0)
2426 // If rows deleted ...
2427 if ((size_t)col
>= pos
- numCols
)
2429 // ...either decrement row counter (if row still exists)...
2430 coords
.SetCol(col
+ numCols
);
2434 // ...or remove the attribute
2435 m_attrs
.RemoveAt((size_t)n
);
2443 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2445 size_t count
= m_attrs
.GetCount();
2446 for ( size_t n
= 0; n
< count
; n
++ )
2448 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2449 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2458 // ----------------------------------------------------------------------------
2459 // wxGridRowOrColAttrData
2460 // ----------------------------------------------------------------------------
2462 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2464 size_t count
= m_attrs
.Count();
2465 for ( size_t n
= 0; n
< count
; n
++ )
2467 m_attrs
[n
]->DecRef();
2471 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2473 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2475 int n
= m_rowsOrCols
.Index(rowOrCol
);
2476 if ( n
!= wxNOT_FOUND
)
2478 attr
= m_attrs
[(size_t)n
];
2485 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2487 int i
= m_rowsOrCols
.Index(rowOrCol
);
2488 if ( i
== wxNOT_FOUND
)
2490 // add the attribute
2491 m_rowsOrCols
.Add(rowOrCol
);
2496 size_t n
= (size_t)i
;
2499 // change the attribute
2500 m_attrs
[n
]->DecRef();
2505 // remove this attribute
2506 m_attrs
[n
]->DecRef();
2507 m_rowsOrCols
.RemoveAt(n
);
2508 m_attrs
.RemoveAt(n
);
2513 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2515 size_t count
= m_attrs
.GetCount();
2516 for ( size_t n
= 0; n
< count
; n
++ )
2518 int & rowOrCol
= m_rowsOrCols
[n
];
2519 if ( (size_t)rowOrCol
>= pos
)
2521 if ( numRowsOrCols
> 0 )
2523 // If rows inserted, include row counter where necessary
2524 rowOrCol
+= numRowsOrCols
;
2526 else if ( numRowsOrCols
< 0)
2528 // If rows deleted, either decrement row counter (if row still exists)
2529 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2530 rowOrCol
+= numRowsOrCols
;
2533 m_rowsOrCols
.RemoveAt((size_t)n
);
2534 m_attrs
.RemoveAt((size_t)n
);
2542 // ----------------------------------------------------------------------------
2543 // wxGridCellAttrProvider
2544 // ----------------------------------------------------------------------------
2546 wxGridCellAttrProvider::wxGridCellAttrProvider()
2548 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2551 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2556 void wxGridCellAttrProvider::InitData()
2558 m_data
= new wxGridCellAttrProviderData
;
2561 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2562 wxGridCellAttr::wxAttrKind kind
) const
2564 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2569 case (wxGridCellAttr::Any
):
2570 //Get cached merge attributes.
2571 // Currenlty not used as no cache implemented as not mutiable
2572 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2575 //Basicaly implement old version.
2576 //Also check merge cache, so we don't have to re-merge every time..
2577 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2578 *attrrow
= (wxGridCellAttr
*)NULL
,
2579 *attrcol
= (wxGridCellAttr
*)NULL
;
2581 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2582 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2583 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2585 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2586 // Two or move are non NULL
2587 attr
= new wxGridCellAttr
;
2588 attr
->SetKind(wxGridCellAttr::Merged
);
2592 attr
->MergeWith(attrcell
);
2596 attr
->MergeWith(attrcol
);
2600 attr
->MergeWith(attrrow
);
2603 //store merge attr if cache implemented
2605 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2609 // one or none is non null return it or null.
2610 if(attrrow
) attr
= attrrow
;
2611 if(attrcol
) attr
= attrcol
;
2612 if(attrcell
) attr
= attrcell
;
2616 case (wxGridCellAttr::Cell
):
2617 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2619 case (wxGridCellAttr::Col
):
2620 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2622 case (wxGridCellAttr::Row
):
2623 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2627 // (wxGridCellAttr::Default):
2628 // (wxGridCellAttr::Merged):
2635 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2641 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2644 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2649 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2652 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2657 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2660 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2664 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2666 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2670 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2674 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2676 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2680 // ----------------------------------------------------------------------------
2681 // wxGridTypeRegistry
2682 // ----------------------------------------------------------------------------
2684 wxGridTypeRegistry::~wxGridTypeRegistry()
2686 size_t count
= m_typeinfo
.Count();
2687 for ( size_t i
= 0; i
< count
; i
++ )
2688 delete m_typeinfo
[i
];
2692 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2693 wxGridCellRenderer
* renderer
,
2694 wxGridCellEditor
* editor
)
2696 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2698 // is it already registered?
2699 int loc
= FindRegisteredDataType(typeName
);
2700 if ( loc
!= wxNOT_FOUND
)
2702 delete m_typeinfo
[loc
];
2703 m_typeinfo
[loc
] = info
;
2707 m_typeinfo
.Add(info
);
2711 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2713 size_t count
= m_typeinfo
.GetCount();
2714 for ( size_t i
= 0; i
< count
; i
++ )
2716 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2725 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2727 int index
= FindRegisteredDataType(typeName
);
2728 if ( index
== wxNOT_FOUND
)
2730 // check whether this is one of the standard ones, in which case
2731 // register it "on the fly"
2733 if ( typeName
== wxGRID_VALUE_STRING
)
2735 RegisterDataType(wxGRID_VALUE_STRING
,
2736 new wxGridCellStringRenderer
,
2737 new wxGridCellTextEditor
);
2739 #endif // wxUSE_TEXTCTRL
2741 if ( typeName
== wxGRID_VALUE_BOOL
)
2743 RegisterDataType(wxGRID_VALUE_BOOL
,
2744 new wxGridCellBoolRenderer
,
2745 new wxGridCellBoolEditor
);
2747 #endif // wxUSE_CHECKBOX
2749 if ( typeName
== wxGRID_VALUE_NUMBER
)
2751 RegisterDataType(wxGRID_VALUE_NUMBER
,
2752 new wxGridCellNumberRenderer
,
2753 new wxGridCellNumberEditor
);
2755 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2757 RegisterDataType(wxGRID_VALUE_FLOAT
,
2758 new wxGridCellFloatRenderer
,
2759 new wxGridCellFloatEditor
);
2761 #endif // wxUSE_TEXTCTRL
2763 if ( typeName
== wxGRID_VALUE_CHOICE
)
2765 RegisterDataType(wxGRID_VALUE_CHOICE
,
2766 new wxGridCellStringRenderer
,
2767 new wxGridCellChoiceEditor
);
2769 #endif // wxUSE_COMBOBOX
2774 // we get here only if just added the entry for this type, so return
2776 index
= m_typeinfo
.GetCount() - 1;
2782 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2784 int index
= FindDataType(typeName
);
2785 if ( index
== wxNOT_FOUND
)
2787 // the first part of the typename is the "real" type, anything after ':'
2788 // are the parameters for the renderer
2789 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2790 if ( index
== wxNOT_FOUND
)
2795 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2796 wxGridCellRenderer
*rendererOld
= renderer
;
2797 renderer
= renderer
->Clone();
2798 rendererOld
->DecRef();
2800 wxGridCellEditor
*editor
= GetEditor(index
);
2801 wxGridCellEditor
*editorOld
= editor
;
2802 editor
= editor
->Clone();
2803 editorOld
->DecRef();
2805 // do it even if there are no parameters to reset them to defaults
2806 wxString params
= typeName
.AfterFirst(_T(':'));
2807 renderer
->SetParameters(params
);
2808 editor
->SetParameters(params
);
2810 // register the new typename
2811 RegisterDataType(typeName
, renderer
, editor
);
2813 // we just registered it, it's the last one
2814 index
= m_typeinfo
.GetCount() - 1;
2820 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2822 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2828 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2830 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2836 // ----------------------------------------------------------------------------
2838 // ----------------------------------------------------------------------------
2840 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2843 wxGridTableBase::wxGridTableBase()
2845 m_view
= (wxGrid
*) NULL
;
2846 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2849 wxGridTableBase::~wxGridTableBase()
2851 delete m_attrProvider
;
2854 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2856 delete m_attrProvider
;
2857 m_attrProvider
= attrProvider
;
2860 bool wxGridTableBase::CanHaveAttributes()
2862 if ( ! GetAttrProvider() )
2864 // use the default attr provider by default
2865 SetAttrProvider(new wxGridCellAttrProvider
);
2870 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2872 if ( m_attrProvider
)
2873 return m_attrProvider
->GetAttr(row
, col
, kind
);
2875 return (wxGridCellAttr
*)NULL
;
2878 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2880 if ( m_attrProvider
)
2882 attr
->SetKind(wxGridCellAttr::Cell
);
2883 m_attrProvider
->SetAttr(attr
, row
, col
);
2887 // as we take ownership of the pointer and don't store it, we must
2893 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2895 if ( m_attrProvider
)
2897 attr
->SetKind(wxGridCellAttr::Row
);
2898 m_attrProvider
->SetRowAttr(attr
, row
);
2902 // as we take ownership of the pointer and don't store it, we must
2908 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2910 if ( m_attrProvider
)
2912 attr
->SetKind(wxGridCellAttr::Col
);
2913 m_attrProvider
->SetColAttr(attr
, col
);
2917 // as we take ownership of the pointer and don't store it, we must
2923 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2924 size_t WXUNUSED(numRows
) )
2926 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2931 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2933 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2938 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2939 size_t WXUNUSED(numRows
) )
2941 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2946 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2947 size_t WXUNUSED(numCols
) )
2949 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2954 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2956 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2961 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2962 size_t WXUNUSED(numCols
) )
2964 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2970 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2973 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2974 // how much it makes sense to us geeks.
2978 wxString
wxGridTableBase::GetColLabelValue( int col
)
2980 // default col labels are:
2981 // cols 0 to 25 : A-Z
2982 // cols 26 to 675 : AA-ZZ
2987 for ( n
= 1; ; n
++ )
2989 s
+= (_T('A') + (wxChar
)( col%26
));
2991 if ( col
< 0 ) break;
2994 // reverse the string...
2996 for ( i
= 0; i
< n
; i
++ )
3005 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3007 return wxGRID_VALUE_STRING
;
3010 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3011 const wxString
& typeName
)
3013 return typeName
== wxGRID_VALUE_STRING
;
3016 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3018 return CanGetValueAs(row
, col
, typeName
);
3021 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3026 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3031 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3036 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3037 long WXUNUSED(value
) )
3041 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3042 double WXUNUSED(value
) )
3046 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3047 bool WXUNUSED(value
) )
3052 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3053 const wxString
& WXUNUSED(typeName
) )
3058 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3059 const wxString
& WXUNUSED(typeName
),
3060 void* WXUNUSED(value
) )
3064 //////////////////////////////////////////////////////////////////////
3066 // Message class for the grid table to send requests and notifications
3070 wxGridTableMessage::wxGridTableMessage()
3072 m_table
= (wxGridTableBase
*) NULL
;
3078 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3079 int commandInt1
, int commandInt2
)
3083 m_comInt1
= commandInt1
;
3084 m_comInt2
= commandInt2
;
3089 //////////////////////////////////////////////////////////////////////
3091 // A basic grid table for string data. An object of this class will
3092 // created by wxGrid if you don't specify an alternative table class.
3095 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3097 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3099 wxGridStringTable::wxGridStringTable()
3104 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3107 m_data
.Alloc( numRows
);
3110 sa
.Alloc( numCols
);
3111 sa
.Add( wxEmptyString
, numCols
);
3113 m_data
.Add( sa
, numRows
);
3116 wxGridStringTable::~wxGridStringTable()
3120 int wxGridStringTable::GetNumberRows()
3122 return m_data
.GetCount();
3125 int wxGridStringTable::GetNumberCols()
3127 if ( m_data
.GetCount() > 0 )
3128 return m_data
[0].GetCount();
3133 wxString
wxGridStringTable::GetValue( int row
, int col
)
3135 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3136 _T("invalid row or column index in wxGridStringTable") );
3138 return m_data
[row
][col
];
3141 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3143 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3144 _T("invalid row or column index in wxGridStringTable") );
3146 m_data
[row
][col
] = value
;
3149 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3151 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3152 _T("invalid row or column index in wxGridStringTable") );
3154 return (m_data
[row
][col
] == wxEmptyString
);
3157 void wxGridStringTable::Clear()
3160 int numRows
, numCols
;
3162 numRows
= m_data
.GetCount();
3165 numCols
= m_data
[0].GetCount();
3167 for ( row
= 0; row
< numRows
; row
++ )
3169 for ( col
= 0; col
< numCols
; col
++ )
3171 m_data
[row
][col
] = wxEmptyString
;
3178 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3180 size_t curNumRows
= m_data
.GetCount();
3181 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3182 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3184 if ( pos
>= curNumRows
)
3186 return AppendRows( numRows
);
3190 sa
.Alloc( curNumCols
);
3191 sa
.Add( wxEmptyString
, curNumCols
);
3192 m_data
.Insert( sa
, pos
, numRows
);
3195 wxGridTableMessage
msg( this,
3196 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3200 GetView()->ProcessTableMessage( msg
);
3206 bool wxGridStringTable::AppendRows( size_t numRows
)
3208 size_t curNumRows
= m_data
.GetCount();
3209 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3210 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3213 if ( curNumCols
> 0 )
3215 sa
.Alloc( curNumCols
);
3216 sa
.Add( wxEmptyString
, curNumCols
);
3219 m_data
.Add( sa
, numRows
);
3223 wxGridTableMessage
msg( this,
3224 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3227 GetView()->ProcessTableMessage( msg
);
3233 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3235 size_t curNumRows
= m_data
.GetCount();
3237 if ( pos
>= curNumRows
)
3239 wxFAIL_MSG( wxString::Format
3241 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3243 (unsigned long)numRows
,
3244 (unsigned long)curNumRows
3250 if ( numRows
> curNumRows
- pos
)
3252 numRows
= curNumRows
- pos
;
3255 if ( numRows
>= curNumRows
)
3261 m_data
.RemoveAt( pos
, numRows
);
3265 wxGridTableMessage
msg( this,
3266 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3270 GetView()->ProcessTableMessage( msg
);
3276 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3280 size_t curNumRows
= m_data
.GetCount();
3281 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3282 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3284 if ( pos
>= curNumCols
)
3286 return AppendCols( numCols
);
3289 for ( row
= 0; row
< curNumRows
; row
++ )
3291 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3293 m_data
[row
].Insert( wxEmptyString
, col
);
3298 wxGridTableMessage
msg( this,
3299 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3303 GetView()->ProcessTableMessage( msg
);
3309 bool wxGridStringTable::AppendCols( size_t numCols
)
3313 size_t curNumRows
= m_data
.GetCount();
3317 // TODO: something better than this ?
3319 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3324 for ( row
= 0; row
< curNumRows
; row
++ )
3326 m_data
[row
].Add( wxEmptyString
, numCols
);
3331 wxGridTableMessage
msg( this,
3332 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3335 GetView()->ProcessTableMessage( msg
);
3341 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3345 size_t curNumRows
= m_data
.GetCount();
3346 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3347 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3349 if ( pos
>= curNumCols
)
3351 wxFAIL_MSG( wxString::Format
3353 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3355 (unsigned long)numCols
,
3356 (unsigned long)curNumCols
3361 if ( numCols
> curNumCols
- pos
)
3363 numCols
= curNumCols
- pos
;
3366 for ( row
= 0; row
< curNumRows
; row
++ )
3368 if ( numCols
>= curNumCols
)
3370 m_data
[row
].Clear();
3374 m_data
[row
].RemoveAt( pos
, numCols
);
3379 wxGridTableMessage
msg( this,
3380 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3384 GetView()->ProcessTableMessage( msg
);
3390 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3392 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3394 // using default label
3396 return wxGridTableBase::GetRowLabelValue( row
);
3400 return m_rowLabels
[ row
];
3404 wxString
wxGridStringTable::GetColLabelValue( int col
)
3406 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3408 // using default label
3410 return wxGridTableBase::GetColLabelValue( col
);
3414 return m_colLabels
[ col
];
3418 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3420 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3422 int n
= m_rowLabels
.GetCount();
3424 for ( i
= n
; i
<= row
; i
++ )
3426 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3430 m_rowLabels
[row
] = value
;
3433 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3435 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3437 int n
= m_colLabels
.GetCount();
3439 for ( i
= n
; i
<= col
; i
++ )
3441 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3445 m_colLabels
[col
] = value
;
3450 //////////////////////////////////////////////////////////////////////
3451 //////////////////////////////////////////////////////////////////////
3453 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3455 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3456 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3457 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3458 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3459 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3460 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3463 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3465 const wxPoint
&pos
, const wxSize
&size
)
3466 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3471 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3475 // NO - don't do this because it will set both the x and y origin
3476 // coords to match the parent scrolled window and we just want to
3477 // set the y coord - MB
3479 // m_owner->PrepareDC( dc );
3482 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3483 dc
.SetDeviceOrigin( 0, -y
);
3485 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3486 m_owner
->DrawRowLabels( dc
, rows
);
3490 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3492 m_owner
->ProcessRowLabelMouseEvent( event
);
3496 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3498 m_owner
->GetEventHandler()->ProcessEvent(event
);
3502 // This seems to be required for wxMotif otherwise the mouse
3503 // cursor must be in the cell edit control to get key events
3505 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3507 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3510 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3512 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3517 //////////////////////////////////////////////////////////////////////
3519 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3521 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3522 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3523 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3524 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3525 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3526 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3529 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3531 const wxPoint
&pos
, const wxSize
&size
)
3532 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3537 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3541 // NO - don't do this because it will set both the x and y origin
3542 // coords to match the parent scrolled window and we just want to
3543 // set the x coord - MB
3545 // m_owner->PrepareDC( dc );
3548 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3549 dc
.SetDeviceOrigin( -x
, 0 );
3551 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3552 m_owner
->DrawColLabels( dc
, cols
);
3556 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3558 m_owner
->ProcessColLabelMouseEvent( event
);
3561 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3563 m_owner
->GetEventHandler()->ProcessEvent(event
);
3567 // This seems to be required for wxMotif otherwise the mouse
3568 // cursor must be in the cell edit control to get key events
3570 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3572 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3575 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3577 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3582 //////////////////////////////////////////////////////////////////////
3584 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3586 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3587 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3588 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3589 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3590 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3591 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3594 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3596 const wxPoint
&pos
, const wxSize
&size
)
3597 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3602 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3606 int client_height
= 0;
3607 int client_width
= 0;
3608 GetClientSize( &client_width
, &client_height
);
3610 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3611 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3612 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3613 dc
.DrawLine( 0, 0, client_width
, 0 );
3614 dc
.DrawLine( 0, 0, 0, client_height
);
3616 dc
.SetPen( *wxWHITE_PEN
);
3617 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3618 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3622 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3624 m_owner
->ProcessCornerLabelMouseEvent( event
);
3628 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3630 m_owner
->GetEventHandler()->ProcessEvent(event
);
3633 // This seems to be required for wxMotif otherwise the mouse
3634 // cursor must be in the cell edit control to get key events
3636 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3638 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3641 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3643 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3648 //////////////////////////////////////////////////////////////////////
3650 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3652 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3653 EVT_PAINT( wxGridWindow::OnPaint
)
3654 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3655 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3656 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3657 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3658 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3661 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3662 wxGridRowLabelWindow
*rowLblWin
,
3663 wxGridColLabelWindow
*colLblWin
,
3666 const wxSize
&size
)
3667 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3668 wxT("grid window") )
3672 m_rowLabelWin
= rowLblWin
;
3673 m_colLabelWin
= colLblWin
;
3674 SetBackgroundColour(_T("WHITE"));
3678 wxGridWindow::~wxGridWindow()
3683 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3685 wxPaintDC
dc( this );
3686 m_owner
->PrepareDC( dc
);
3687 wxRegion reg
= GetUpdateRegion();
3688 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3689 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3690 #if WXGRID_DRAW_LINES
3691 m_owner
->DrawAllGridLines( dc
, reg
);
3693 m_owner
->DrawGridSpace( dc
);
3694 m_owner
->DrawHighlight( dc
, DirtyCells
);
3698 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3700 wxWindow::ScrollWindow( dx
, dy
, rect
);
3701 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3702 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3706 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3708 m_owner
->ProcessGridCellMouseEvent( event
);
3711 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3713 m_owner
->GetEventHandler()->ProcessEvent(event
);
3716 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3717 // cursor must be in the cell edit control to get key events
3719 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3721 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3724 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3726 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3729 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3734 //////////////////////////////////////////////////////////////////////
3736 // Internal Helper function for computing row or column from some
3737 // (unscrolled) coordinate value, using either
3738 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3739 // of m_rowBottoms/m_ColRights to speed up the search!
3741 // Internal helper macros for simpler use of that function
3743 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3744 const wxArrayInt
& BorderArray
, int nMax
,
3745 bool maxOnOverflow
);
3747 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3748 WXGRID_MIN_COL_WIDTH, \
3749 m_colRights, m_numCols, TRUE)
3750 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3751 WXGRID_MIN_ROW_HEIGHT, \
3752 m_rowBottoms, m_numRows, TRUE)
3753 /////////////////////////////////////////////////////////////////////
3755 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3757 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3758 EVT_PAINT( wxGrid::OnPaint
)
3759 EVT_SIZE( wxGrid::OnSize
)
3760 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3761 EVT_KEY_UP( wxGrid::OnKeyUp
)
3762 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3765 wxGrid::wxGrid( wxWindow
*parent
,
3770 const wxString
& name
)
3771 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3772 m_colMinWidths(GRID_HASH_SIZE
),
3773 m_rowMinHeights(GRID_HASH_SIZE
)
3781 // Must do this or ~wxScrollHelper will pop the wrong event handler
3782 SetTargetWindow(this);
3784 wxSafeDecRef(m_defaultCellAttr
);
3786 #ifdef DEBUG_ATTR_CACHE
3787 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3788 wxPrintf(_T("wxGrid attribute cache statistics: "
3789 "total: %u, hits: %u (%u%%)\n"),
3790 total
, gs_nAttrCacheHits
,
3791 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3797 delete m_typeRegistry
;
3803 // ----- internal init and update functions
3806 void wxGrid::Create()
3808 m_created
= FALSE
; // set to TRUE by CreateGrid
3810 m_table
= (wxGridTableBase
*) NULL
;
3813 m_cellEditCtrlEnabled
= FALSE
;
3815 m_defaultCellAttr
= new wxGridCellAttr();
3817 // Set default cell attributes
3818 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3819 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3820 m_defaultCellAttr
->SetFont(GetFont());
3821 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3822 m_defaultCellAttr
->SetTextColour(
3823 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3824 m_defaultCellAttr
->SetBackgroundColour(
3825 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3826 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3827 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3832 m_currentCellCoords
= wxGridNoCellCoords
;
3834 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3835 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3837 // create the type registry
3838 m_typeRegistry
= new wxGridTypeRegistry
;
3841 // subwindow components that make up the wxGrid
3842 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3847 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3852 m_colLabelWin
= new wxGridColLabelWindow( this,
3857 m_gridWin
= new wxGridWindow( this,
3864 SetTargetWindow( m_gridWin
);
3870 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3871 wxGrid::wxGridSelectionModes selmode
)
3873 wxCHECK_MSG( !m_created
,
3875 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3877 m_numRows
= numRows
;
3878 m_numCols
= numCols
;
3880 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3881 m_table
->SetView( this );
3883 m_selection
= new wxGridSelection( this, selmode
);
3892 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3894 wxCHECK_RET( m_created
,
3895 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3897 m_selection
->SetSelectionMode( selmode
);
3900 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3902 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3903 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3905 return m_selection
->GetSelectionMode();
3908 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3909 wxGrid::wxGridSelectionModes selmode
)
3913 // RD: Actually, this should probably be allowed. I think it would be
3914 // nice to be able to switch multiple Tables in and out of a single
3915 // View at runtime. Is there anything in the implementation that
3916 // would prevent this?
3918 // At least, you now have to cope with m_selection
3919 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3924 m_numRows
= table
->GetNumberRows();
3925 m_numCols
= table
->GetNumberCols();
3928 m_table
->SetView( this );
3931 m_selection
= new wxGridSelection( this, selmode
);
3944 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3945 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3947 if ( m_rowLabelWin
)
3949 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3953 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3956 m_labelTextColour
= wxColour( _T("BLACK") );
3959 m_attrCache
.row
= -1;
3960 m_attrCache
.col
= -1;
3961 m_attrCache
.attr
= NULL
;
3963 // TODO: something better than this ?
3965 m_labelFont
= this->GetFont();
3966 // m_labelFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
3967 // m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
3969 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
3970 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3972 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3973 m_colLabelVertAlign
= wxALIGN_CENTRE
;
3975 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3976 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3978 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3979 m_defaultRowHeight
+= 8;
3981 m_defaultRowHeight
+= 4;
3984 m_gridLineColour
= wxColour( 192,192,192 );
3985 m_gridLinesEnabled
= TRUE
;
3986 m_cellHighlightColour
= *wxBLACK
;
3987 m_cellHighlightPenWidth
= 2;
3988 m_cellHighlightROPenWidth
= 1;
3990 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3991 m_winCapture
= (wxWindow
*)NULL
;
3992 m_canDragRowSize
= TRUE
;
3993 m_canDragColSize
= TRUE
;
3994 m_canDragGridSize
= TRUE
;
3996 m_dragRowOrCol
= -1;
3997 m_isDragging
= FALSE
;
3998 m_startDragPos
= wxDefaultPosition
;
4000 m_waitForSlowClick
= FALSE
;
4002 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4003 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4005 m_currentCellCoords
= wxGridNoCellCoords
;
4007 m_selectingTopLeft
= wxGridNoCellCoords
;
4008 m_selectingBottomRight
= wxGridNoCellCoords
;
4009 // m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
4010 // m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
4011 m_selectionBackground
= *wxBLACK
;
4012 m_selectionForeground
= *wxWHITE
;
4014 m_editable
= TRUE
; // default for whole grid
4016 m_inOnKeyDown
= FALSE
;
4023 // ----------------------------------------------------------------------------
4024 // the idea is to call these functions only when necessary because they create
4025 // quite big arrays which eat memory mostly unnecessary - in particular, if
4026 // default widths/heights are used for all rows/columns, we may not use these
4029 // with some extra code, it should be possible to only store the
4030 // widths/heights different from default ones but this will be done later...
4031 // ----------------------------------------------------------------------------
4033 void wxGrid::InitRowHeights()
4035 m_rowHeights
.Empty();
4036 m_rowBottoms
.Empty();
4038 m_rowHeights
.Alloc( m_numRows
);
4039 m_rowBottoms
.Alloc( m_numRows
);
4043 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4045 for ( int i
= 0; i
< m_numRows
; i
++ )
4047 rowBottom
+= m_defaultRowHeight
;
4048 m_rowBottoms
.Add( rowBottom
);
4052 void wxGrid::InitColWidths()
4054 m_colWidths
.Empty();
4055 m_colRights
.Empty();
4057 m_colWidths
.Alloc( m_numCols
);
4058 m_colRights
.Alloc( m_numCols
);
4061 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4063 for ( int i
= 0; i
< m_numCols
; i
++ )
4065 colRight
+= m_defaultColWidth
;
4066 m_colRights
.Add( colRight
);
4070 int wxGrid::GetColWidth(int col
) const
4072 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4075 int wxGrid::GetColLeft(int col
) const
4077 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4078 : m_colRights
[col
] - m_colWidths
[col
];
4081 int wxGrid::GetColRight(int col
) const
4083 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4087 int wxGrid::GetRowHeight(int row
) const
4089 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4092 int wxGrid::GetRowTop(int row
) const
4094 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4095 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4098 int wxGrid::GetRowBottom(int row
) const
4100 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4101 : m_rowBottoms
[row
];
4104 void wxGrid::CalcDimensions()
4107 GetClientSize( &cw
, &ch
);
4109 if ( m_rowLabelWin
->IsShown() )
4110 cw
-= m_rowLabelWidth
;
4111 if ( m_colLabelWin
->IsShown() )
4112 ch
-= m_colLabelHeight
;
4115 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4116 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4118 // take into account editor if shown
4119 if( IsCellEditControlShown() )
4122 int r
= m_currentCellCoords
.GetRow();
4123 int c
= m_currentCellCoords
.GetCol();
4124 int x
= GetColLeft(c
);
4125 int y
= GetRowTop(r
);
4127 // how big is the editor
4128 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4129 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4130 editor
->GetControl()->GetSize(&w2
, &h2
);
4133 if( w2
> w
) w
= w2
;
4134 if( h2
> h
) h
= h2
;
4139 // preserve (more or less) the previous position
4141 GetViewStart( &x
, &y
);
4143 // maybe we don't need scrollbars at all?
4145 // also adjust the position to be valid for the new scroll rangs
4166 // do set scrollbar parameters
4167 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4168 GetScrollX(w
), GetScrollY(h
), x
, y
,
4169 GetBatchCount() != 0);
4171 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4172 // still must reposition the children
4177 void wxGrid::CalcWindowSizes()
4180 GetClientSize( &cw
, &ch
);
4182 if ( m_cornerLabelWin
->IsShown() )
4183 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4185 if ( m_colLabelWin
->IsShown() )
4186 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4188 if ( m_rowLabelWin
->IsShown() )
4189 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4191 if ( m_gridWin
->IsShown() )
4192 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4196 // this is called when the grid table sends a message to say that it
4197 // has been redimensioned
4199 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4202 bool result
= FALSE
;
4204 // Clear the attribute cache as the attribute might refer to a different
4205 // cell than stored in the cache after adding/removing rows/columns.
4207 // By the same reasoning, the editor should be dismissed if columns are
4208 // added or removed. And for consistency, it should IMHO always be
4209 // removed, not only if the cell "underneath" it actually changes.
4210 // For now, I intentionally do not save the editor's content as the
4211 // cell it might want to save that stuff to might no longer exist.
4212 HideCellEditControl();
4214 // if we were using the default widths/heights so far, we must change them
4216 if ( m_colWidths
.IsEmpty() )
4221 if ( m_rowHeights
.IsEmpty() )
4227 switch ( msg
.GetId() )
4229 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4231 size_t pos
= msg
.GetCommandInt();
4232 int numRows
= msg
.GetCommandInt2();
4234 m_numRows
+= numRows
;
4236 if ( !m_rowHeights
.IsEmpty() )
4238 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4239 m_rowBottoms
.Insert( 0, pos
, numRows
);
4242 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4244 for ( i
= pos
; i
< m_numRows
; i
++ )
4246 bottom
+= m_rowHeights
[i
];
4247 m_rowBottoms
[i
] = bottom
;
4250 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4252 // if we have just inserted cols into an empty grid the current
4253 // cell will be undefined...
4255 SetCurrentCell( 0, 0 );
4259 m_selection
->UpdateRows( pos
, numRows
);
4260 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4262 attrProvider
->UpdateAttrRows( pos
, numRows
);
4264 if ( !GetBatchCount() )
4267 m_rowLabelWin
->Refresh();
4273 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4275 int numRows
= msg
.GetCommandInt();
4276 int oldNumRows
= m_numRows
;
4277 m_numRows
+= numRows
;
4279 if ( !m_rowHeights
.IsEmpty() )
4281 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4282 m_rowBottoms
.Add( 0, numRows
);
4285 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4287 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4289 bottom
+= m_rowHeights
[i
];
4290 m_rowBottoms
[i
] = bottom
;
4293 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4295 // if we have just inserted cols into an empty grid the current
4296 // cell will be undefined...
4298 SetCurrentCell( 0, 0 );
4300 if ( !GetBatchCount() )
4303 m_rowLabelWin
->Refresh();
4309 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4311 size_t pos
= msg
.GetCommandInt();
4312 int numRows
= msg
.GetCommandInt2();
4313 m_numRows
-= numRows
;
4315 if ( !m_rowHeights
.IsEmpty() )
4317 m_rowHeights
.RemoveAt( pos
, numRows
);
4318 m_rowBottoms
.RemoveAt( pos
, numRows
);
4321 for ( i
= 0; i
< m_numRows
; i
++ )
4323 h
+= m_rowHeights
[i
];
4324 m_rowBottoms
[i
] = h
;
4329 m_currentCellCoords
= wxGridNoCellCoords
;
4333 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4334 m_currentCellCoords
.Set( 0, 0 );
4338 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4339 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4341 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4342 // ifdef'd out following patch from Paul Gammans
4344 // No need to touch column attributes, unless we
4345 // removed _all_ rows, in this case, we remove
4346 // all column attributes.
4347 // I hate to do this here, but the
4348 // needed data is not available inside UpdateAttrRows.
4349 if ( !GetNumberRows() )
4350 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4353 if ( !GetBatchCount() )
4356 m_rowLabelWin
->Refresh();
4362 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4364 size_t pos
= msg
.GetCommandInt();
4365 int numCols
= msg
.GetCommandInt2();
4366 m_numCols
+= numCols
;
4368 if ( !m_colWidths
.IsEmpty() )
4370 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4371 m_colRights
.Insert( 0, pos
, numCols
);
4374 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4376 for ( i
= pos
; i
< m_numCols
; i
++ )
4378 right
+= m_colWidths
[i
];
4379 m_colRights
[i
] = right
;
4382 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4384 // if we have just inserted cols into an empty grid the current
4385 // cell will be undefined...
4387 SetCurrentCell( 0, 0 );
4391 m_selection
->UpdateCols( pos
, numCols
);
4392 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4394 attrProvider
->UpdateAttrCols( pos
, numCols
);
4395 if ( !GetBatchCount() )
4398 m_colLabelWin
->Refresh();
4405 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4407 int numCols
= msg
.GetCommandInt();
4408 int oldNumCols
= m_numCols
;
4409 m_numCols
+= numCols
;
4410 if ( !m_colWidths
.IsEmpty() )
4412 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4413 m_colRights
.Add( 0, numCols
);
4416 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4418 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4420 right
+= m_colWidths
[i
];
4421 m_colRights
[i
] = right
;
4424 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4426 // if we have just inserted cols into an empty grid the current
4427 // cell will be undefined...
4429 SetCurrentCell( 0, 0 );
4431 if ( !GetBatchCount() )
4434 m_colLabelWin
->Refresh();
4440 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4442 size_t pos
= msg
.GetCommandInt();
4443 int numCols
= msg
.GetCommandInt2();
4444 m_numCols
-= numCols
;
4446 if ( !m_colWidths
.IsEmpty() )
4448 m_colWidths
.RemoveAt( pos
, numCols
);
4449 m_colRights
.RemoveAt( pos
, numCols
);
4452 for ( i
= 0; i
< m_numCols
; i
++ )
4454 w
+= m_colWidths
[i
];
4460 m_currentCellCoords
= wxGridNoCellCoords
;
4464 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4465 m_currentCellCoords
.Set( 0, 0 );
4469 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4470 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4472 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4473 // ifdef'd out following patch from Paul Gammans
4475 // No need to touch row attributes, unless we
4476 // removed _all_ columns, in this case, we remove
4477 // all row attributes.
4478 // I hate to do this here, but the
4479 // needed data is not available inside UpdateAttrCols.
4480 if ( !GetNumberCols() )
4481 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4484 if ( !GetBatchCount() )
4487 m_colLabelWin
->Refresh();
4494 if (result
&& !GetBatchCount() )
4495 m_gridWin
->Refresh();
4500 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4502 wxRegionIterator
iter( reg
);
4505 wxArrayInt rowlabels
;
4512 // TODO: remove this when we can...
4513 // There is a bug in wxMotif that gives garbage update
4514 // rectangles if you jump-scroll a long way by clicking the
4515 // scrollbar with middle button. This is a work-around
4517 #if defined(__WXMOTIF__)
4519 m_gridWin
->GetClientSize( &cw
, &ch
);
4520 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4521 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4524 // logical bounds of update region
4527 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4528 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4530 // find the row labels within these bounds
4533 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4535 if ( GetRowBottom(row
) < top
)
4538 if ( GetRowTop(row
) > bottom
)
4541 rowlabels
.Add( row
);
4551 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4553 wxRegionIterator
iter( reg
);
4556 wxArrayInt colLabels
;
4563 // TODO: remove this when we can...
4564 // There is a bug in wxMotif that gives garbage update
4565 // rectangles if you jump-scroll a long way by clicking the
4566 // scrollbar with middle button. This is a work-around
4568 #if defined(__WXMOTIF__)
4570 m_gridWin
->GetClientSize( &cw
, &ch
);
4571 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4572 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4575 // logical bounds of update region
4578 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4579 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4581 // find the cells within these bounds
4584 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4586 if ( GetColRight(col
) < left
)
4589 if ( GetColLeft(col
) > right
)
4592 colLabels
.Add( col
);
4601 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4603 wxRegionIterator
iter( reg
);
4606 wxGridCellCoordsArray cellsExposed
;
4608 int left
, top
, right
, bottom
;
4613 // TODO: remove this when we can...
4614 // There is a bug in wxMotif that gives garbage update
4615 // rectangles if you jump-scroll a long way by clicking the
4616 // scrollbar with middle button. This is a work-around
4618 #if defined(__WXMOTIF__)
4620 m_gridWin
->GetClientSize( &cw
, &ch
);
4621 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4622 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4623 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4624 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4627 // logical bounds of update region
4629 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4630 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4632 // find the cells within these bounds
4635 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4637 if ( GetRowBottom(row
) <= top
)
4640 if ( GetRowTop(row
) > bottom
)
4643 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4645 if ( GetColRight(col
) <= left
)
4648 if ( GetColLeft(col
) > right
)
4651 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4658 return cellsExposed
;
4662 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4665 wxPoint
pos( event
.GetPosition() );
4666 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4668 if ( event
.Dragging() )
4672 m_isDragging
= TRUE
;
4673 m_rowLabelWin
->CaptureMouse();
4676 if ( event
.LeftIsDown() )
4678 switch( m_cursorMode
)
4680 case WXGRID_CURSOR_RESIZE_ROW
:
4682 int cw
, ch
, left
, dummy
;
4683 m_gridWin
->GetClientSize( &cw
, &ch
);
4684 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4686 wxClientDC
dc( m_gridWin
);
4689 GetRowTop(m_dragRowOrCol
) +
4690 GetRowMinimalHeight(m_dragRowOrCol
) );
4691 dc
.SetLogicalFunction(wxINVERT
);
4692 if ( m_dragLastPos
>= 0 )
4694 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4696 dc
.DrawLine( left
, y
, left
+cw
, y
);
4701 case WXGRID_CURSOR_SELECT_ROW
:
4702 if ( (row
= YToRow( y
)) >= 0 )
4706 m_selection
->SelectRow( row
,
4707 event
.ControlDown(),
4714 // default label to suppress warnings about "enumeration value
4715 // 'xxx' not handled in switch
4723 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4728 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4729 m_isDragging
= FALSE
;
4732 // ------------ Entering or leaving the window
4734 if ( event
.Entering() || event
.Leaving() )
4736 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4740 // ------------ Left button pressed
4742 else if ( event
.LeftDown() )
4744 // don't send a label click event for a hit on the
4745 // edge of the row label - this is probably the user
4746 // wanting to resize the row
4748 if ( YToEdgeOfRow(y
) < 0 )
4752 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4754 if ( !event
.ShiftDown() && !event
.ControlDown() )
4758 if ( event
.ShiftDown() )
4760 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4763 GetNumberCols() - 1,
4764 event
.ControlDown(),
4771 m_selection
->SelectRow( row
,
4772 event
.ControlDown(),
4779 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4784 // starting to drag-resize a row
4786 if ( CanDragRowSize() )
4787 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4792 // ------------ Left double click
4794 else if (event
.LeftDClick() )
4796 if ( YToEdgeOfRow(y
) < 0 )
4799 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4804 // ------------ Left button released
4806 else if ( event
.LeftUp() )
4808 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4810 DoEndDragResizeRow();
4812 // Note: we are ending the event *after* doing
4813 // default processing in this case
4815 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4818 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4823 // ------------ Right button down
4825 else if ( event
.RightDown() )
4828 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4830 // no default action at the moment
4835 // ------------ Right double click
4837 else if ( event
.RightDClick() )
4840 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4842 // no default action at the moment
4847 // ------------ No buttons down and mouse moving
4849 else if ( event
.Moving() )
4851 m_dragRowOrCol
= YToEdgeOfRow( y
);
4852 if ( m_dragRowOrCol
>= 0 )
4854 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4856 // don't capture the mouse yet
4857 if ( CanDragRowSize() )
4858 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4861 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4863 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4869 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4872 wxPoint
pos( event
.GetPosition() );
4873 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4875 if ( event
.Dragging() )
4879 m_isDragging
= TRUE
;
4880 m_colLabelWin
->CaptureMouse();
4883 if ( event
.LeftIsDown() )
4885 switch( m_cursorMode
)
4887 case WXGRID_CURSOR_RESIZE_COL
:
4889 int cw
, ch
, dummy
, top
;
4890 m_gridWin
->GetClientSize( &cw
, &ch
);
4891 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4893 wxClientDC
dc( m_gridWin
);
4896 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4897 GetColMinimalWidth(m_dragRowOrCol
));
4898 dc
.SetLogicalFunction(wxINVERT
);
4899 if ( m_dragLastPos
>= 0 )
4901 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4903 dc
.DrawLine( x
, top
, x
, top
+ch
);
4908 case WXGRID_CURSOR_SELECT_COL
:
4909 if ( (col
= XToCol( x
)) >= 0 )
4913 m_selection
->SelectCol( col
,
4914 event
.ControlDown(),
4921 // default label to suppress warnings about "enumeration value
4922 // 'xxx' not handled in switch
4930 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4935 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4936 m_isDragging
= FALSE
;
4939 // ------------ Entering or leaving the window
4941 if ( event
.Entering() || event
.Leaving() )
4943 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4947 // ------------ Left button pressed
4949 else if ( event
.LeftDown() )
4951 // don't send a label click event for a hit on the
4952 // edge of the col label - this is probably the user
4953 // wanting to resize the col
4955 if ( XToEdgeOfCol(x
) < 0 )
4959 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4961 if ( !event
.ShiftDown() && !event
.ControlDown() )
4965 if ( event
.ShiftDown() )
4967 m_selection
->SelectBlock( 0,
4968 m_currentCellCoords
.GetCol(),
4969 GetNumberRows() - 1, col
,
4970 event
.ControlDown(),
4977 m_selection
->SelectCol( col
,
4978 event
.ControlDown(),
4985 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4990 // starting to drag-resize a col
4992 if ( CanDragColSize() )
4993 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4998 // ------------ Left double click
5000 if ( event
.LeftDClick() )
5002 if ( XToEdgeOfCol(x
) < 0 )
5005 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
5010 // ------------ Left button released
5012 else if ( event
.LeftUp() )
5014 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5016 DoEndDragResizeCol();
5018 // Note: we are ending the event *after* doing
5019 // default processing in this case
5021 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5024 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5029 // ------------ Right button down
5031 else if ( event
.RightDown() )
5034 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5036 // no default action at the moment
5041 // ------------ Right double click
5043 else if ( event
.RightDClick() )
5046 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5048 // no default action at the moment
5053 // ------------ No buttons down and mouse moving
5055 else if ( event
.Moving() )
5057 m_dragRowOrCol
= XToEdgeOfCol( x
);
5058 if ( m_dragRowOrCol
>= 0 )
5060 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5062 // don't capture the cursor yet
5063 if ( CanDragColSize() )
5064 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
5067 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5069 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
5075 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5077 if ( event
.LeftDown() )
5079 // indicate corner label by having both row and
5082 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5088 else if ( event
.LeftDClick() )
5090 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5093 else if ( event
.RightDown() )
5095 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5097 // no default action at the moment
5101 else if ( event
.RightDClick() )
5103 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5105 // no default action at the moment
5110 void wxGrid::ChangeCursorMode(CursorMode mode
,
5115 static const wxChar
*cursorModes
[] =
5124 wxLogTrace(_T("grid"),
5125 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5126 win
== m_colLabelWin
? _T("colLabelWin")
5127 : win
? _T("rowLabelWin")
5129 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5130 #endif // __WXDEBUG__
5132 if ( mode
== m_cursorMode
&&
5133 win
== m_winCapture
&&
5134 captureMouse
== (m_winCapture
!= NULL
))
5139 // by default use the grid itself
5145 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5146 m_winCapture
= (wxWindow
*)NULL
;
5149 m_cursorMode
= mode
;
5151 switch ( m_cursorMode
)
5153 case WXGRID_CURSOR_RESIZE_ROW
:
5154 win
->SetCursor( m_rowResizeCursor
);
5157 case WXGRID_CURSOR_RESIZE_COL
:
5158 win
->SetCursor( m_colResizeCursor
);
5162 win
->SetCursor( *wxSTANDARD_CURSOR
);
5165 // we need to capture mouse when resizing
5166 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5167 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5169 if ( captureMouse
&& resize
)
5171 win
->CaptureMouse();
5176 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5179 wxPoint
pos( event
.GetPosition() );
5180 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5182 wxGridCellCoords coords
;
5183 XYToCell( x
, y
, coords
);
5185 int cell_rows
, cell_cols
;
5186 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5187 if ((cell_rows
< 0) || (cell_cols
< 0))
5189 coords
.SetRow(coords
.GetRow() + cell_rows
);
5190 coords
.SetCol(coords
.GetCol() + cell_cols
);
5193 if ( event
.Dragging() )
5195 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5197 // Don't start doing anything until the mouse has been drug at
5198 // least 3 pixels in any direction...
5201 if (m_startDragPos
== wxDefaultPosition
)
5203 m_startDragPos
= pos
;
5206 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5210 m_isDragging
= TRUE
;
5211 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5213 // Hide the edit control, so it
5214 // won't interfer with drag-shrinking.
5215 if ( IsCellEditControlShown() )
5217 HideCellEditControl();
5218 SaveEditControlValue();
5221 // Have we captured the mouse yet?
5224 m_winCapture
= m_gridWin
;
5225 m_winCapture
->CaptureMouse();
5228 if ( coords
!= wxGridNoCellCoords
)
5230 if ( event
.ControlDown() )
5232 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5233 m_selectingKeyboard
= coords
;
5234 HighlightBlock ( m_selectingKeyboard
, coords
);
5238 if ( !IsSelection() )
5240 HighlightBlock( coords
, coords
);
5244 HighlightBlock( m_currentCellCoords
, coords
);
5248 if (! IsVisible(coords
))
5250 MakeCellVisible(coords
);
5251 // TODO: need to introduce a delay or something here. The
5252 // scrolling is way to fast, at least on MSW - also on GTK.
5256 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5258 int cw
, ch
, left
, dummy
;
5259 m_gridWin
->GetClientSize( &cw
, &ch
);
5260 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5262 wxClientDC
dc( m_gridWin
);
5264 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5265 GetRowMinimalHeight(m_dragRowOrCol
) );
5266 dc
.SetLogicalFunction(wxINVERT
);
5267 if ( m_dragLastPos
>= 0 )
5269 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5271 dc
.DrawLine( left
, y
, left
+cw
, y
);
5274 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5276 int cw
, ch
, dummy
, top
;
5277 m_gridWin
->GetClientSize( &cw
, &ch
);
5278 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5280 wxClientDC
dc( m_gridWin
);
5282 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5283 GetColMinimalWidth(m_dragRowOrCol
) );
5284 dc
.SetLogicalFunction(wxINVERT
);
5285 if ( m_dragLastPos
>= 0 )
5287 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5289 dc
.DrawLine( x
, top
, x
, top
+ch
);
5296 m_isDragging
= FALSE
;
5297 m_startDragPos
= wxDefaultPosition
;
5299 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5300 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5303 if ( event
.Entering() || event
.Leaving() )
5305 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5306 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5311 // ------------ Left button pressed
5313 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5315 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5320 if ( !event
.ControlDown() )
5322 if ( event
.ShiftDown() )
5326 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5327 m_currentCellCoords
.GetCol(),
5330 event
.ControlDown(),
5336 else if ( XToEdgeOfCol(x
) < 0 &&
5337 YToEdgeOfRow(y
) < 0 )
5339 DisableCellEditControl();
5340 MakeCellVisible( coords
);
5342 if ( event
.ControlDown() )
5346 m_selection
->ToggleCellSelection( coords
.GetRow(),
5348 event
.ControlDown(),
5353 m_selectingTopLeft
= wxGridNoCellCoords
;
5354 m_selectingBottomRight
= wxGridNoCellCoords
;
5355 m_selectingKeyboard
= coords
;
5359 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5360 SetCurrentCell( coords
);
5363 if ( m_selection
->GetSelectionMode() !=
5364 wxGrid::wxGridSelectCells
)
5366 HighlightBlock( coords
, coords
);
5375 // ------------ Left double click
5377 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5379 DisableCellEditControl();
5381 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5383 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5391 // ------------ Left button released
5393 else if ( event
.LeftUp() )
5395 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5397 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5398 m_selectingBottomRight
!= wxGridNoCellCoords
)
5402 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5403 m_winCapture
= NULL
;
5408 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5409 m_selectingTopLeft
.GetCol(),
5410 m_selectingBottomRight
.GetRow(),
5411 m_selectingBottomRight
.GetCol(),
5412 event
.ControlDown(),
5418 m_selectingTopLeft
= wxGridNoCellCoords
;
5419 m_selectingBottomRight
= wxGridNoCellCoords
;
5421 // Show the edit control, if it has been hidden for
5423 ShowCellEditControl();
5427 if( m_waitForSlowClick
&& CanEnableCellControl())
5429 EnableCellEditControl();
5431 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5432 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5433 editor
->StartingClick();
5437 m_waitForSlowClick
= FALSE
;
5441 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5443 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5444 DoEndDragResizeRow();
5446 // Note: we are ending the event *after* doing
5447 // default processing in this case
5449 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5451 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5453 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5454 DoEndDragResizeCol();
5456 // Note: we are ending the event *after* doing
5457 // default processing in this case
5459 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5466 // ------------ Right button down
5468 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5470 DisableCellEditControl();
5471 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5476 // no default action at the moment
5481 // ------------ Right double click
5483 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5485 DisableCellEditControl();
5486 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5491 // no default action at the moment
5495 // ------------ Moving and no button action
5497 else if ( event
.Moving() && !event
.IsButton() )
5499 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5501 // out of grid cell area
5502 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5506 int dragRow
= YToEdgeOfRow( y
);
5507 int dragCol
= XToEdgeOfCol( x
);
5509 // Dragging on the corner of a cell to resize in both
5510 // directions is not implemented yet...
5512 if ( dragRow
>= 0 && dragCol
>= 0 )
5514 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5520 m_dragRowOrCol
= dragRow
;
5522 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5524 if ( CanDragRowSize() && CanDragGridSize() )
5525 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5530 m_dragRowOrCol
= dragCol
;
5538 m_dragRowOrCol
= dragCol
;
5540 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5542 if ( CanDragColSize() && CanDragGridSize() )
5543 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5549 // Neither on a row or col edge
5551 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5553 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5559 void wxGrid::DoEndDragResizeRow()
5561 if ( m_dragLastPos
>= 0 )
5563 // erase the last line and resize the row
5565 int cw
, ch
, left
, dummy
;
5566 m_gridWin
->GetClientSize( &cw
, &ch
);
5567 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5569 wxClientDC
dc( m_gridWin
);
5571 dc
.SetLogicalFunction( wxINVERT
);
5572 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5573 HideCellEditControl();
5574 SaveEditControlValue();
5576 int rowTop
= GetRowTop(m_dragRowOrCol
);
5577 SetRowSize( m_dragRowOrCol
,
5578 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5580 if ( !GetBatchCount() )
5582 // Only needed to get the correct rect.y:
5583 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5585 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5586 rect
.width
= m_rowLabelWidth
;
5587 rect
.height
= ch
- rect
.y
;
5588 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5590 // if there is a multicell block, paint all of it
5593 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5594 int leftCol
= XToCol(left
);
5595 int rightCol
= XToCol(left
+cw
);
5598 if (rightCol
< 0) rightCol
= m_numCols
;
5599 for (i
=leftCol
; i
<rightCol
; i
++)
5601 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5602 if (cell_rows
< subtract_rows
)
5603 subtract_rows
= cell_rows
;
5605 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5606 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5607 rect
.height
= ch
- rect
.y
;
5610 m_gridWin
->Refresh( FALSE
, &rect
);
5613 ShowCellEditControl();
5618 void wxGrid::DoEndDragResizeCol()
5620 if ( m_dragLastPos
>= 0 )
5622 // erase the last line and resize the col
5624 int cw
, ch
, dummy
, top
;
5625 m_gridWin
->GetClientSize( &cw
, &ch
);
5626 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5628 wxClientDC
dc( m_gridWin
);
5630 dc
.SetLogicalFunction( wxINVERT
);
5631 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5632 HideCellEditControl();
5633 SaveEditControlValue();
5635 int colLeft
= GetColLeft(m_dragRowOrCol
);
5636 SetColSize( m_dragRowOrCol
,
5637 wxMax( m_dragLastPos
- colLeft
,
5638 GetColMinimalWidth(m_dragRowOrCol
) ) );
5640 if ( !GetBatchCount() )
5642 // Only needed to get the correct rect.x:
5643 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5645 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5646 rect
.width
= cw
- rect
.x
;
5647 rect
.height
= m_colLabelHeight
;
5648 m_colLabelWin
->Refresh( TRUE
, &rect
);
5650 // if there is a multicell block, paint all of it
5653 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5654 int topRow
= YToRow(top
);
5655 int bottomRow
= YToRow(top
+cw
);
5658 if (bottomRow
< 0) bottomRow
= m_numRows
;
5659 for (i
=topRow
; i
<bottomRow
; i
++)
5661 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5662 if (cell_cols
< subtract_cols
)
5663 subtract_cols
= cell_cols
;
5665 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5666 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5667 rect
.width
= cw
- rect
.x
;
5670 m_gridWin
->Refresh( FALSE
, &rect
);
5673 ShowCellEditControl();
5680 // ------ interaction with data model
5682 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5684 switch ( msg
.GetId() )
5686 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5687 return GetModelValues();
5689 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5690 return SetModelValues();
5692 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5693 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5694 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5695 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5696 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5697 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5698 return Redimension( msg
);
5707 // The behaviour of this function depends on the grid table class
5708 // Clear() function. For the default wxGridStringTable class the
5709 // behavious is to replace all cell contents with wxEmptyString but
5710 // not to change the number of rows or cols.
5712 void wxGrid::ClearGrid()
5716 if (IsCellEditControlEnabled())
5717 DisableCellEditControl();
5720 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5725 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5727 // TODO: something with updateLabels flag
5731 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5737 if (IsCellEditControlEnabled())
5738 DisableCellEditControl();
5740 return m_table
->InsertRows( pos
, numRows
);
5742 // the table will have sent the results of the insert row
5743 // operation to this view object as a grid table message
5749 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5751 // TODO: something with updateLabels flag
5755 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5759 return ( m_table
&& m_table
->AppendRows( numRows
) );
5760 // the table will have sent the results of the append row
5761 // operation to this view object as a grid table message
5765 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5767 // TODO: something with updateLabels flag
5771 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5777 if (IsCellEditControlEnabled())
5778 DisableCellEditControl();
5780 return (m_table
->DeleteRows( pos
, numRows
));
5781 // the table will have sent the results of the delete row
5782 // operation to this view object as a grid table message
5788 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5790 // TODO: something with updateLabels flag
5794 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5800 if (IsCellEditControlEnabled())
5801 DisableCellEditControl();
5803 return m_table
->InsertCols( pos
, numCols
);
5804 // the table will have sent the results of the insert col
5805 // operation to this view object as a grid table message
5811 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5813 // TODO: something with updateLabels flag
5817 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5821 return ( m_table
&& m_table
->AppendCols( numCols
) );
5822 // the table will have sent the results of the append col
5823 // operation to this view object as a grid table message
5827 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5829 // TODO: something with updateLabels flag
5833 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5839 if (IsCellEditControlEnabled())
5840 DisableCellEditControl();
5842 return ( m_table
->DeleteCols( pos
, numCols
) );
5843 // the table will have sent the results of the delete col
5844 // operation to this view object as a grid table message
5852 // ----- event handlers
5855 // Generate a grid event based on a mouse event and
5856 // return the result of ProcessEvent()
5858 int wxGrid::SendEvent( const wxEventType type
,
5860 wxMouseEvent
& mouseEv
)
5865 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5867 int rowOrCol
= (row
== -1 ? col
: row
);
5869 wxGridSizeEvent
gridEvt( GetId(),
5873 mouseEv
.GetX() + GetRowLabelSize(),
5874 mouseEv
.GetY() + GetColLabelSize(),
5875 mouseEv
.ControlDown(),
5876 mouseEv
.ShiftDown(),
5878 mouseEv
.MetaDown() );
5880 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5881 vetoed
= !gridEvt
.IsAllowed();
5883 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5885 // Right now, it should _never_ end up here!
5886 wxGridRangeSelectEvent
gridEvt( GetId(),
5890 m_selectingBottomRight
,
5892 mouseEv
.ControlDown(),
5893 mouseEv
.ShiftDown(),
5895 mouseEv
.MetaDown() );
5897 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5898 vetoed
= !gridEvt
.IsAllowed();
5902 wxGridEvent
gridEvt( GetId(),
5906 mouseEv
.GetX() + GetRowLabelSize(),
5907 mouseEv
.GetY() + GetColLabelSize(),
5909 mouseEv
.ControlDown(),
5910 mouseEv
.ShiftDown(),
5912 mouseEv
.MetaDown() );
5913 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5914 vetoed
= !gridEvt
.IsAllowed();
5917 // A Veto'd event may not be `claimed' so test this first
5918 if (vetoed
) return -1;
5919 return claimed
? 1 : 0;
5923 // Generate a grid event of specified type and return the result
5924 // of ProcessEvent().
5926 int wxGrid::SendEvent( const wxEventType type
,
5932 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5934 int rowOrCol
= (row
== -1 ? col
: row
);
5936 wxGridSizeEvent
gridEvt( GetId(),
5941 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5942 vetoed
= !gridEvt
.IsAllowed();
5946 wxGridEvent
gridEvt( GetId(),
5951 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5952 vetoed
= !gridEvt
.IsAllowed();
5955 // A Veto'd event may not be `claimed' so test this first
5956 if (vetoed
) return -1;
5957 return claimed
? 1 : 0;
5961 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5963 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5966 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
5968 // Don't do anything if between Begin/EndBatch...
5969 // EndBatch() will do all this on the last nested one anyway.
5970 if (! GetBatchCount())
5972 // Refresh to get correct scrolled position:
5973 wxScrolledWindow::Refresh(eraseb
,rect
);
5977 int rect_x
, rect_y
, rectWidth
, rectHeight
;
5978 int width_label
, width_cell
, height_label
, height_cell
;
5981 //Copy rectangle can get scroll offsets..
5982 rect_x
= rect
->GetX();
5983 rect_y
= rect
->GetY();
5984 rectWidth
= rect
->GetWidth();
5985 rectHeight
= rect
->GetHeight();
5987 width_label
= m_rowLabelWidth
- rect_x
;
5988 if (width_label
> rectWidth
) width_label
= rectWidth
;
5990 height_label
= m_colLabelHeight
- rect_y
;
5991 if (height_label
> rectHeight
) height_label
= rectHeight
;
5993 if (rect_x
> m_rowLabelWidth
)
5995 x
= rect_x
- m_rowLabelWidth
;
5996 width_cell
= rectWidth
;
6001 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6004 if (rect_y
> m_colLabelHeight
)
6006 y
= rect_y
- m_colLabelHeight
;
6007 height_cell
= rectHeight
;
6012 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6015 // Paint corner label part intersecting rect.
6016 if ( width_label
> 0 && height_label
> 0 )
6018 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6019 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6022 // Paint col labels part intersecting rect.
6023 if ( width_cell
> 0 && height_label
> 0 )
6025 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6026 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6029 // Paint row labels part intersecting rect.
6030 if ( width_label
> 0 && height_cell
> 0 )
6032 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6033 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6036 // Paint cell area part intersecting rect.
6037 if ( width_cell
> 0 && height_cell
> 0 )
6039 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6040 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6045 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6046 m_colLabelWin
->Refresh(eraseb
, NULL
);
6047 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6048 m_gridWin
->Refresh(eraseb
, NULL
);
6053 void wxGrid::OnSize( wxSizeEvent
& event
)
6055 // position the child windows
6058 // don't call CalcDimensions() from here, the base class handles the size
6064 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6066 if ( m_inOnKeyDown
)
6068 // shouldn't be here - we are going round in circles...
6070 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6073 m_inOnKeyDown
= TRUE
;
6075 // propagate the event up and see if it gets processed
6077 wxWindow
*parent
= GetParent();
6078 wxKeyEvent
keyEvt( event
);
6079 keyEvt
.SetEventObject( parent
);
6081 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6084 // try local handlers
6086 switch ( event
.KeyCode() )
6089 if ( event
.ControlDown() )
6091 MoveCursorUpBlock( event
.ShiftDown() );
6095 MoveCursorUp( event
.ShiftDown() );
6100 if ( event
.ControlDown() )
6102 MoveCursorDownBlock( event
.ShiftDown() );
6106 MoveCursorDown( event
.ShiftDown() );
6111 if ( event
.ControlDown() )
6113 MoveCursorLeftBlock( event
.ShiftDown() );
6117 MoveCursorLeft( event
.ShiftDown() );
6122 if ( event
.ControlDown() )
6124 MoveCursorRightBlock( event
.ShiftDown() );
6128 MoveCursorRight( event
.ShiftDown() );
6133 case WXK_NUMPAD_ENTER
:
6134 if ( event
.ControlDown() )
6136 event
.Skip(); // to let the edit control have the return
6140 if ( GetGridCursorRow() < GetNumberRows()-1 )
6142 MoveCursorDown( event
.ShiftDown() );
6146 // at the bottom of a column
6147 HideCellEditControl();
6148 SaveEditControlValue();
6158 if (event
.ShiftDown())
6160 if ( GetGridCursorCol() > 0 )
6162 MoveCursorLeft( FALSE
);
6167 HideCellEditControl();
6168 SaveEditControlValue();
6173 if ( GetGridCursorCol() < GetNumberCols()-1 )
6175 MoveCursorRight( FALSE
);
6180 HideCellEditControl();
6181 SaveEditControlValue();
6187 if ( event
.ControlDown() )
6189 MakeCellVisible( 0, 0 );
6190 SetCurrentCell( 0, 0 );
6199 if ( event
.ControlDown() )
6201 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6202 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6219 if ( event
.ControlDown() )
6223 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6224 m_currentCellCoords
.GetCol(),
6225 event
.ControlDown(),
6232 if ( !IsEditable() )
6234 MoveCursorRight( FALSE
);
6237 // Otherwise fall through to default
6240 // is it possible to edit the current cell at all?
6241 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6243 // yes, now check whether the cells editor accepts the key
6244 int row
= m_currentCellCoords
.GetRow();
6245 int col
= m_currentCellCoords
.GetCol();
6246 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6247 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6249 // <F2> is special and will always start editing, for
6250 // other keys - ask the editor itself
6251 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
6252 || editor
->IsAcceptedKey(event
) )
6254 // ensure cell is visble
6255 MakeCellVisible(row
, col
);
6256 EnableCellEditControl();
6258 // a problem can arise if the cell is not completely
6259 // visible (even after calling MakeCellVisible the
6260 // control is not created and calling StartingKey will
6262 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6274 // let others process char events with modifiers or all
6275 // char events for readonly cells
6282 m_inOnKeyDown
= FALSE
;
6285 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6287 // try local handlers
6289 if ( event
.KeyCode() == WXK_SHIFT
)
6291 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6292 m_selectingBottomRight
!= wxGridNoCellCoords
)
6296 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6297 m_selectingTopLeft
.GetCol(),
6298 m_selectingBottomRight
.GetRow(),
6299 m_selectingBottomRight
.GetCol(),
6300 event
.ControlDown(),
6307 m_selectingTopLeft
= wxGridNoCellCoords
;
6308 m_selectingBottomRight
= wxGridNoCellCoords
;
6309 m_selectingKeyboard
= wxGridNoCellCoords
;
6313 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6317 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6319 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6321 // the event has been intercepted - do nothing
6325 wxClientDC
dc(m_gridWin
);
6328 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6330 HideCellEditControl();
6331 DisableCellEditControl();
6333 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6336 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6337 if ( !m_gridLinesEnabled
)
6345 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6347 // Otherwise refresh redraws the highlight!
6348 m_currentCellCoords
= coords
;
6350 DrawGridCellArea(dc
,cells
);
6351 DrawAllGridLines( dc
, r
);
6355 m_currentCellCoords
= coords
;
6357 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6358 DrawCellHighlight(dc
, attr
);
6363 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6366 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6370 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6373 rightCol
= GetNumberCols() - 1;
6375 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6378 bottomRow
= GetNumberRows() - 1;
6382 if ( topRow
> bottomRow
)
6389 if ( leftCol
> rightCol
)
6396 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6397 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6399 // First the case that we selected a completely new area
6400 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6401 m_selectingBottomRight
== wxGridNoCellCoords
)
6404 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6405 wxGridCellCoords ( bottomRow
, rightCol
) );
6406 m_gridWin
->Refresh( FALSE
, &rect
);
6408 // Now handle changing an existing selection area.
6409 else if ( m_selectingTopLeft
!= updateTopLeft
||
6410 m_selectingBottomRight
!= updateBottomRight
)
6412 // Compute two optimal update rectangles:
6413 // Either one rectangle is a real subset of the
6414 // other, or they are (almost) disjoint!
6416 bool need_refresh
[4];
6420 need_refresh
[3] = FALSE
;
6423 // Store intermediate values
6424 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6425 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6426 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6427 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6429 // Determine the outer/inner coordinates.
6430 if (oldLeft
> leftCol
)
6436 if (oldTop
> topRow
)
6442 if (oldRight
< rightCol
)
6445 oldRight
= rightCol
;
6448 if (oldBottom
< bottomRow
)
6451 oldBottom
= bottomRow
;
6455 // Now, either the stuff marked old is the outer
6456 // rectangle or we don't have a situation where one
6457 // is contained in the other.
6459 if ( oldLeft
< leftCol
)
6461 // Refresh the newly selected or deselected
6462 // area to the left of the old or new selection.
6463 need_refresh
[0] = TRUE
;
6464 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6466 wxGridCellCoords ( oldBottom
,
6470 if ( oldTop
< topRow
)
6472 // Refresh the newly selected or deselected
6473 // area above the old or new selection.
6474 need_refresh
[1] = TRUE
;
6475 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6477 wxGridCellCoords ( topRow
- 1,
6481 if ( oldRight
> rightCol
)
6483 // Refresh the newly selected or deselected
6484 // area to the right of the old or new selection.
6485 need_refresh
[2] = TRUE
;
6486 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6488 wxGridCellCoords ( oldBottom
,
6492 if ( oldBottom
> bottomRow
)
6494 // Refresh the newly selected or deselected
6495 // area below the old or new selection.
6496 need_refresh
[3] = TRUE
;
6497 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6499 wxGridCellCoords ( oldBottom
,
6503 // various Refresh() calls
6504 for (i
= 0; i
< 4; i
++ )
6505 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6506 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6509 m_selectingTopLeft
= updateTopLeft
;
6510 m_selectingBottomRight
= updateBottomRight
;
6514 // ------ functions to get/send data (see also public functions)
6517 bool wxGrid::GetModelValues()
6519 // Hide the editor, so it won't hide a changed value.
6520 HideCellEditControl();
6524 // all we need to do is repaint the grid
6526 m_gridWin
->Refresh();
6534 bool wxGrid::SetModelValues()
6538 // Disable the editor, so it won't hide a changed value.
6539 // Do we also want to save the current value of the editor first?
6541 DisableCellEditControl();
6545 for ( row
= 0; row
< m_numRows
; row
++ )
6547 for ( col
= 0; col
< m_numCols
; col
++ )
6549 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6561 // Note - this function only draws cells that are in the list of
6562 // exposed cells (usually set from the update region by
6563 // CalcExposedCells)
6565 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6567 if ( !m_numRows
|| !m_numCols
) return;
6569 int i
, numCells
= cells
.GetCount();
6570 int row
, col
, cell_rows
, cell_cols
;
6571 wxGridCellCoordsArray redrawCells
;
6573 for ( i
= numCells
-1; i
>= 0; i
-- )
6575 row
= cells
[i
].GetRow();
6576 col
= cells
[i
].GetCol();
6577 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6579 // If this cell is part of a multicell block, find owner for repaint
6580 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6582 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6583 bool marked
= FALSE
;
6584 for ( int j
= 0; j
< numCells
; j
++ )
6586 if ( cell
== cells
[j
] )
6594 int count
= redrawCells
.GetCount();
6595 for (int j
= 0; j
< count
; j
++)
6597 if ( cell
== redrawCells
[j
] )
6603 if (!marked
) redrawCells
.Add( cell
);
6605 continue; // don't bother drawing this cell
6608 // If this cell is empty, find cell to left that might want to overflow
6609 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6611 for ( int l
= 0; l
< cell_rows
; l
++ )
6613 // find a cell in this row to left alreay marked for repaint
6615 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6616 if ((redrawCells
[k
].GetCol() < left
) &&
6617 (redrawCells
[k
].GetRow() == row
))
6618 left
=redrawCells
[k
].GetCol();
6620 if (left
== col
) left
= 0; // oh well
6622 for (int j
= col
-1; j
>= left
; j
--)
6624 if (!m_table
->IsEmptyCell(row
+l
, j
))
6626 if (GetCellOverflow(row
+l
, j
))
6628 wxGridCellCoords
cell(row
+l
, j
);
6629 bool marked
= FALSE
;
6631 for (int k
= 0; k
< numCells
; k
++)
6633 if ( cell
== cells
[k
] )
6641 int count
= redrawCells
.GetCount();
6642 for (int k
= 0; k
< count
; k
++)
6644 if ( cell
== redrawCells
[k
] )
6650 if (!marked
) redrawCells
.Add( cell
);
6658 DrawCell( dc
, cells
[i
] );
6661 numCells
= redrawCells
.GetCount();
6663 for ( i
= numCells
- 1; i
>= 0; i
-- )
6665 DrawCell( dc
, redrawCells
[i
] );
6670 void wxGrid::DrawGridSpace( wxDC
& dc
)
6673 m_gridWin
->GetClientSize( &cw
, &ch
);
6676 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6678 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6679 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6681 if ( right
> rightCol
|| bottom
> bottomRow
)
6684 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6686 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6687 dc
.SetPen( *wxTRANSPARENT_PEN
);
6689 if ( right
> rightCol
)
6691 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6694 if ( bottom
> bottomRow
)
6696 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6702 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6704 int row
= coords
.GetRow();
6705 int col
= coords
.GetCol();
6707 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6710 // we draw the cell border ourselves
6711 #if !WXGRID_DRAW_LINES
6712 if ( m_gridLinesEnabled
)
6713 DrawCellBorder( dc
, coords
);
6716 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6718 bool isCurrent
= coords
== m_currentCellCoords
;
6720 wxRect rect
= CellToRect( row
, col
);
6722 // if the editor is shown, we should use it and not the renderer
6723 // Note: However, only if it is really _shown_, i.e. not hidden!
6724 if ( isCurrent
&& IsCellEditControlShown() )
6726 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6727 editor
->PaintBackground(rect
, attr
);
6732 // but all the rest is drawn by the cell renderer and hence may be
6734 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6735 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6742 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6744 int row
= m_currentCellCoords
.GetRow();
6745 int col
= m_currentCellCoords
.GetCol();
6747 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6750 wxRect rect
= CellToRect(row
, col
);
6752 // hmmm... what could we do here to show that the cell is disabled?
6753 // for now, I just draw a thinner border than for the other ones, but
6754 // it doesn't look really good
6756 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6760 // The center of th drawn line is where the position/width/height of
6761 // the rectangle is actually at, (on wxMSW atr least,) so we will
6762 // reduce the size of the rectangle to compensate for the thickness of
6763 // the line. If this is too strange on non wxMSW platforms then
6764 // please #ifdef this appropriately.
6765 rect
.x
+= penWidth
/2;
6766 rect
.y
+= penWidth
/2;
6767 rect
.width
-= penWidth
-1;
6768 rect
.height
-= penWidth
-1;
6771 // Now draw the rectangle
6772 // use the cellHighlightColour if the cell is inside a selection, this
6773 // will ensure the cell is always visible.
6774 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6775 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6776 dc
.DrawRectangle(rect
);
6780 // VZ: my experiments with 3d borders...
6782 // how to properly set colours for arbitrary bg?
6783 wxCoord x1
= rect
.x
,
6785 x2
= rect
.x
+ rect
.width
-1,
6786 y2
= rect
.y
+ rect
.height
-1;
6788 dc
.SetPen(*wxWHITE_PEN
);
6789 dc
.DrawLine(x1
, y1
, x2
, y1
);
6790 dc
.DrawLine(x1
, y1
, x1
, y2
);
6792 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6793 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6795 dc
.SetPen(*wxBLACK_PEN
);
6796 dc
.DrawLine(x1
, y2
, x2
, y2
);
6797 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6802 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6804 int row
= coords
.GetRow();
6805 int col
= coords
.GetCol();
6806 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6809 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6811 wxRect rect
= CellToRect( row
, col
);
6813 // right hand border
6815 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6816 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6820 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6821 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6824 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6826 // This if block was previously in wxGrid::OnPaint but that doesn't
6827 // seem to get called under wxGTK - MB
6829 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6830 m_numRows
&& m_numCols
)
6832 m_currentCellCoords
.Set(0, 0);
6835 if ( IsCellEditControlShown() )
6837 // don't show highlight when the edit control is shown
6841 // if the active cell was repainted, repaint its highlight too because it
6842 // might have been damaged by the grid lines
6843 size_t count
= cells
.GetCount();
6844 for ( size_t n
= 0; n
< count
; n
++ )
6846 if ( cells
[n
] == m_currentCellCoords
)
6848 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6849 DrawCellHighlight(dc
, attr
);
6857 // TODO: remove this ???
6858 // This is used to redraw all grid lines e.g. when the grid line colour
6861 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6863 #if !WXGRID_DRAW_LINES
6867 if ( !m_gridLinesEnabled
||
6869 !m_numCols
) return;
6871 int top
, bottom
, left
, right
;
6873 #if 0 //#ifndef __WXGTK__
6877 m_gridWin
->GetClientSize(&cw
, &ch
);
6879 // virtual coords of visible area
6881 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6882 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6887 reg
.GetBox(x
, y
, w
, h
);
6888 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6889 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6893 m_gridWin
->GetClientSize(&cw
, &ch
);
6894 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6895 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6898 // avoid drawing grid lines past the last row and col
6900 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6901 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6903 // no gridlines inside multicells, clip them out
6904 int leftCol
= XToCol(left
);
6905 int topRow
= YToRow(top
);
6906 int rightCol
= XToCol(right
);
6907 int bottomRow
= YToRow(bottom
);
6908 wxRegion
clippedcells(0, 0, cw
, ch
);
6910 if ((leftCol
>= 0) && (topRow
>= 0))
6912 if (rightCol
< 0) rightCol
= m_numCols
;
6913 if (bottomRow
< 0) bottomRow
= m_numRows
;
6915 int i
, j
, cell_rows
, cell_cols
;
6918 for (j
=topRow
; j
<bottomRow
; j
++)
6920 for (i
=leftCol
; i
<rightCol
; i
++)
6922 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
6923 if ((cell_rows
> 1) || (cell_cols
> 1))
6925 rect
= CellToRect(j
,i
);
6926 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6927 clippedcells
.Subtract(rect
);
6929 else if ((cell_rows
< 0) || (cell_cols
< 0))
6931 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
6932 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6933 clippedcells
.Subtract(rect
);
6938 dc
.SetClippingRegion( clippedcells
);
6940 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6942 // horizontal grid lines
6945 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6947 int bot
= GetRowBottom(i
) - 1;
6956 dc
.DrawLine( left
, bot
, right
, bot
);
6961 // vertical grid lines
6963 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6965 int colRight
= GetColRight(i
) - 1;
6966 if ( colRight
> right
)
6971 if ( colRight
>= left
)
6973 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6976 dc
.DestroyClippingRegion();
6980 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6982 if ( !m_numRows
) return;
6985 size_t numLabels
= rows
.GetCount();
6987 for ( i
= 0; i
< numLabels
; i
++ )
6989 DrawRowLabel( dc
, rows
[i
] );
6994 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6996 if ( GetRowHeight(row
) <= 0 )
6999 int rowTop
= GetRowTop(row
),
7000 rowBottom
= GetRowBottom(row
) - 1;
7002 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7003 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7004 m_rowLabelWidth
-1, rowBottom
);
7006 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7008 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7010 dc
.SetPen( *wxWHITE_PEN
);
7011 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7012 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7014 dc
.SetBackgroundMode( wxTRANSPARENT
);
7015 dc
.SetTextForeground( GetLabelTextColour() );
7016 dc
.SetFont( GetLabelFont() );
7019 GetRowLabelAlignment( &hAlign
, &vAlign
);
7023 rect
.SetY( GetRowTop(row
) + 2 );
7024 rect
.SetWidth( m_rowLabelWidth
- 4 );
7025 rect
.SetHeight( GetRowHeight(row
) - 4 );
7026 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7030 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7032 if ( !m_numCols
) return;
7035 size_t numLabels
= cols
.GetCount();
7037 for ( i
= 0; i
< numLabels
; i
++ )
7039 DrawColLabel( dc
, cols
[i
] );
7044 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7046 if ( GetColWidth(col
) <= 0 )
7049 int colLeft
= GetColLeft(col
),
7050 colRight
= GetColRight(col
) - 1;
7052 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7053 dc
.DrawLine( colRight
, 0,
7054 colRight
, m_colLabelHeight
-1 );
7056 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7058 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7059 colRight
+1, m_colLabelHeight
-1 );
7061 dc
.SetPen( *wxWHITE_PEN
);
7062 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7063 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7065 dc
.SetBackgroundMode( wxTRANSPARENT
);
7066 dc
.SetTextForeground( GetLabelTextColour() );
7067 dc
.SetFont( GetLabelFont() );
7069 dc
.SetBackgroundMode( wxTRANSPARENT
);
7070 dc
.SetTextForeground( GetLabelTextColour() );
7071 dc
.SetFont( GetLabelFont() );
7074 GetColLabelAlignment( &hAlign
, &vAlign
);
7077 rect
.SetX( colLeft
+ 2 );
7079 rect
.SetWidth( GetColWidth(col
) - 4 );
7080 rect
.SetHeight( m_colLabelHeight
- 4 );
7081 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
7084 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7085 const wxString
& value
,
7090 wxArrayString lines
;
7092 StringToLines( value
, lines
);
7095 //Forward to new API.
7096 DrawTextRectangle( dc
,
7104 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7105 const wxArrayString
& lines
,
7110 long textWidth
, textHeight
;
7111 long lineWidth
, lineHeight
;
7114 dc
.SetClippingRegion( rect
);
7116 nLines
= lines
.GetCount();
7121 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7124 case wxALIGN_BOTTOM
:
7125 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7128 case wxALIGN_CENTRE
:
7129 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7138 // Align each line of a multi-line label
7139 for( l
= 0; l
< nLines
; l
++ )
7141 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7143 switch( horizAlign
)
7146 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7149 case wxALIGN_CENTRE
:
7150 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7159 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7163 dc
.DestroyClippingRegion();
7167 // Split multi line text up into an array of strings. Any existing
7168 // contents of the string array are preserved.
7170 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7174 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7175 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7177 while ( startPos
< (int)tVal
.Length() )
7179 pos
= tVal
.Mid(startPos
).Find( eol
);
7184 else if ( pos
== 0 )
7186 lines
.Add( wxEmptyString
);
7190 lines
.Add( value
.Mid(startPos
, pos
) );
7194 if ( startPos
< (int)value
.Length() )
7196 lines
.Add( value
.Mid( startPos
) );
7201 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7202 const wxArrayString
& lines
,
7203 long *width
, long *height
)
7210 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7212 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7213 w
= wxMax( w
, lineW
);
7222 // ------ Batch processing.
7224 void wxGrid::EndBatch()
7226 if ( m_batchCount
> 0 )
7229 if ( !m_batchCount
)
7232 m_rowLabelWin
->Refresh();
7233 m_colLabelWin
->Refresh();
7234 m_cornerLabelWin
->Refresh();
7235 m_gridWin
->Refresh();
7240 // Use this, rather than wxWindow::Refresh(), to force an immediate
7241 // repainting of the grid. Has no effect if you are already inside a
7242 // BeginBatch / EndBatch block.
7244 void wxGrid::ForceRefresh()
7252 // ------ Edit control functions
7256 void wxGrid::EnableEditing( bool edit
)
7258 // TODO: improve this ?
7260 if ( edit
!= m_editable
)
7262 if(!edit
) EnableCellEditControl(edit
);
7268 void wxGrid::EnableCellEditControl( bool enable
)
7273 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7274 SetCurrentCell( 0, 0 );
7276 if ( enable
!= m_cellEditCtrlEnabled
)
7280 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7283 // this should be checked by the caller!
7284 wxASSERT_MSG( CanEnableCellControl(),
7285 _T("can't enable editing for this cell!") );
7287 // do it before ShowCellEditControl()
7288 m_cellEditCtrlEnabled
= enable
;
7290 ShowCellEditControl();
7294 //FIXME:add veto support
7295 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7297 HideCellEditControl();
7298 SaveEditControlValue();
7300 // do it after HideCellEditControl()
7301 m_cellEditCtrlEnabled
= enable
;
7306 bool wxGrid::IsCurrentCellReadOnly() const
7309 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7310 bool readonly
= attr
->IsReadOnly();
7316 bool wxGrid::CanEnableCellControl() const
7318 return m_editable
&& !IsCurrentCellReadOnly();
7321 bool wxGrid::IsCellEditControlEnabled() const
7323 // the cell edit control might be disable for all cells or just for the
7324 // current one if it's read only
7325 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7328 bool wxGrid::IsCellEditControlShown() const
7330 bool isShown
= FALSE
;
7332 if ( m_cellEditCtrlEnabled
)
7334 int row
= m_currentCellCoords
.GetRow();
7335 int col
= m_currentCellCoords
.GetCol();
7336 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7337 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7342 if ( editor
->IsCreated() )
7344 isShown
= editor
->GetControl()->IsShown();
7354 void wxGrid::ShowCellEditControl()
7356 if ( IsCellEditControlEnabled() )
7358 if ( !IsVisible( m_currentCellCoords
) )
7360 m_cellEditCtrlEnabled
= FALSE
;
7365 wxRect rect
= CellToRect( m_currentCellCoords
);
7366 int row
= m_currentCellCoords
.GetRow();
7367 int col
= m_currentCellCoords
.GetCol();
7369 // if this is part of a multicell, find owner (topleft)
7370 int cell_rows
, cell_cols
;
7371 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7372 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7376 m_currentCellCoords
.SetRow( row
);
7377 m_currentCellCoords
.SetCol( col
);
7380 // convert to scrolled coords
7382 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7384 // done in PaintBackground()
7386 // erase the highlight and the cell contents because the editor
7387 // might not cover the entire cell
7388 wxClientDC
dc( m_gridWin
);
7390 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7391 dc
.SetPen(*wxTRANSPARENT_PEN
);
7392 dc
.DrawRectangle(rect
);
7395 // cell is shifted by one pixel
7396 // However, don't allow x or y to become negative
7397 // since the SetSize() method interprets that as
7404 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7405 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7406 if ( !editor
->IsCreated() )
7408 editor
->Create(m_gridWin
, -1,
7409 new wxGridCellEditorEvtHandler(this, editor
));
7411 wxGridEditorCreatedEvent
evt(GetId(),
7412 wxEVT_GRID_EDITOR_CREATED
,
7416 editor
->GetControl());
7417 GetEventHandler()->ProcessEvent(evt
);
7421 // resize editor to overflow into righthand cells if allowed
7422 int maxWidth
= rect
.width
;
7423 wxString value
= GetCellValue(row
, col
);
7424 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7427 GetTextExtent(value
, &maxWidth
, &y
,
7428 NULL
, NULL
, &attr
->GetFont());
7429 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7431 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7432 if (rect
.x
+maxWidth
> client_right
)
7433 maxWidth
= client_right
- rect
.x
;
7435 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7437 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7438 // may have changed earlier
7439 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7442 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7443 // looks weird going over a multicell
7444 if (m_table
->IsEmptyCell(row
,i
) &&
7445 (rect
.width
< maxWidth
) && (c_rows
== 1))
7446 rect
.width
+= GetColWidth(i
);
7450 if (rect
.GetRight() > client_right
)
7451 rect
.SetRight(client_right
-1);
7454 editor
->SetCellAttr(attr
);
7455 editor
->SetSize( rect
);
7456 editor
->Show( TRUE
, attr
);
7458 // recalc dimensions in case we need to
7459 // expand the scrolled window to account for editor
7462 editor
->BeginEdit(row
, col
, this);
7463 editor
->SetCellAttr(NULL
);
7472 void wxGrid::HideCellEditControl()
7474 if ( IsCellEditControlEnabled() )
7476 int row
= m_currentCellCoords
.GetRow();
7477 int col
= m_currentCellCoords
.GetCol();
7479 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7480 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7481 editor
->Show( FALSE
);
7484 m_gridWin
->SetFocus();
7485 // refresh whole row to the right
7486 wxRect
rect( CellToRect(row
, col
) );
7487 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7488 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7489 m_gridWin
->Refresh( FALSE
, &rect
);
7494 void wxGrid::SaveEditControlValue()
7496 if ( IsCellEditControlEnabled() )
7498 int row
= m_currentCellCoords
.GetRow();
7499 int col
= m_currentCellCoords
.GetCol();
7501 wxString oldval
= GetCellValue(row
,col
);
7503 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7504 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7505 bool changed
= editor
->EndEdit(row
, col
, this);
7512 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7513 m_currentCellCoords
.GetRow(),
7514 m_currentCellCoords
.GetCol() ) < 0 ) {
7516 // Event has been vetoed, set the data back.
7517 SetCellValue(row
,col
,oldval
);
7525 // ------ Grid location functions
7526 // Note that all of these functions work with the logical coordinates of
7527 // grid cells and labels so you will need to convert from device
7528 // coordinates for mouse events etc.
7531 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7533 int row
= YToRow(y
);
7534 int col
= XToCol(x
);
7536 if ( row
== -1 || col
== -1 )
7538 coords
= wxGridNoCellCoords
;
7542 coords
.Set( row
, col
);
7547 // Internal Helper function for computing row or column from some
7548 // (unscrolled) coordinate value, using either
7549 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7550 // of m_rowBottoms/m_ColRights to speed up the search!
7552 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7553 const wxArrayInt
& BorderArray
, int nMax
,
7558 size_t i_max
= coord
/ defaultDist
,
7561 if (BorderArray
.IsEmpty())
7563 if((int) i_max
< nMax
)
7565 return maxOnOverflow
? nMax
- 1 : -1;
7568 if ( i_max
>= BorderArray
.GetCount())
7569 i_max
= BorderArray
.GetCount() - 1;
7572 if ( coord
>= BorderArray
[i_max
])
7575 i_max
= coord
/ minDist
;
7577 if ( i_max
>= BorderArray
.GetCount())
7578 i_max
= BorderArray
.GetCount() - 1;
7580 if ( coord
>= BorderArray
[i_max
])
7581 return maxOnOverflow
? (int)i_max
: -1;
7582 if ( coord
< BorderArray
[0] )
7585 while ( i_max
- i_min
> 0 )
7587 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7588 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7589 if (coord
>= BorderArray
[ i_max
- 1])
7593 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7594 if (coord
< BorderArray
[median
])
7602 int wxGrid::YToRow( int y
)
7604 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7605 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, m_numRows
, FALSE
);
7609 int wxGrid::XToCol( int x
)
7611 return CoordToRowOrCol(x
, m_defaultColWidth
,
7612 WXGRID_MIN_COL_WIDTH
, m_colRights
, m_numCols
, FALSE
);
7616 // return the row number that that the y coord is near the edge of, or
7617 // -1 if not near an edge
7619 int wxGrid::YToEdgeOfRow( int y
)
7622 i
= internalYToRow(y
);
7624 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7626 // We know that we are in row i, test whether we are
7627 // close enough to lower or upper border, respectively.
7628 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7630 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7638 // return the col number that that the x coord is near the edge of, or
7639 // -1 if not near an edge
7641 int wxGrid::XToEdgeOfCol( int x
)
7644 i
= internalXToCol(x
);
7646 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7648 // We know that we are in column i, test whether we are
7649 // close enough to right or left border, respectively.
7650 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7652 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7660 wxRect
wxGrid::CellToRect( int row
, int col
)
7662 wxRect
rect( -1, -1, -1, -1 );
7664 if ( row
>= 0 && row
< m_numRows
&&
7665 col
>= 0 && col
< m_numCols
)
7667 int i
, cell_rows
, cell_cols
;
7668 rect
.width
= rect
.height
= 0;
7669 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7670 // if negative then find multicell owner
7671 if (cell_rows
< 0) row
+= cell_rows
;
7672 if (cell_cols
< 0) col
+= cell_cols
;
7673 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7675 rect
.x
= GetColLeft(col
);
7676 rect
.y
= GetRowTop(row
);
7677 for (i
=col
; i
<col
+cell_cols
; i
++)
7678 rect
.width
+= GetColWidth(i
);
7679 for (i
=row
; i
<row
+cell_rows
; i
++)
7680 rect
.height
+= GetRowHeight(i
);
7683 // if grid lines are enabled, then the area of the cell is a bit smaller
7684 if (m_gridLinesEnabled
) {
7692 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7694 // get the cell rectangle in logical coords
7696 wxRect
r( CellToRect( row
, col
) );
7698 // convert to device coords
7700 int left
, top
, right
, bottom
;
7701 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7702 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7704 // check against the client area of the grid window
7707 m_gridWin
->GetClientSize( &cw
, &ch
);
7709 if ( wholeCellVisible
)
7711 // is the cell wholly visible ?
7713 return ( left
>= 0 && right
<= cw
&&
7714 top
>= 0 && bottom
<= ch
);
7718 // is the cell partly visible ?
7720 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7721 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7726 // make the specified cell location visible by doing a minimal amount
7729 void wxGrid::MakeCellVisible( int row
, int col
)
7733 int xpos
= -1, ypos
= -1;
7735 if ( row
>= 0 && row
< m_numRows
&&
7736 col
>= 0 && col
< m_numCols
)
7738 // get the cell rectangle in logical coords
7740 wxRect
r( CellToRect( row
, col
) );
7742 // convert to device coords
7744 int left
, top
, right
, bottom
;
7745 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7746 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7749 m_gridWin
->GetClientSize( &cw
, &ch
);
7755 else if ( bottom
> ch
)
7757 int h
= r
.GetHeight();
7759 for ( i
= row
-1; i
>= 0; i
-- )
7761 int rowHeight
= GetRowHeight(i
);
7762 if ( h
+ rowHeight
> ch
)
7769 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7770 // have rounding errors (this is important, because if we do, we
7771 // might not scroll at all and some cells won't be redrawn)
7773 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7775 ypos
+= GRID_SCROLL_LINE_Y
;
7782 else if ( right
> cw
)
7784 // position the view so that the cell is on the right
7786 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
7787 xpos
= x0
+ (right
- cw
);
7789 // see comment for ypos above
7790 xpos
+= GRID_SCROLL_LINE_X
;
7793 if ( xpos
!= -1 || ypos
!= -1 )
7796 xpos
/= GRID_SCROLL_LINE_X
;
7798 ypos
/= GRID_SCROLL_LINE_Y
;
7799 Scroll( xpos
, ypos
);
7807 // ------ Grid cursor movement functions
7810 bool wxGrid::MoveCursorUp( bool expandSelection
)
7812 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7813 m_currentCellCoords
.GetRow() >= 0 )
7815 if ( expandSelection
)
7817 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7818 m_selectingKeyboard
= m_currentCellCoords
;
7819 if ( m_selectingKeyboard
.GetRow() > 0 )
7821 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7822 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7823 m_selectingKeyboard
.GetCol() );
7824 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7827 else if ( m_currentCellCoords
.GetRow() > 0 )
7830 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7831 m_currentCellCoords
.GetCol() );
7832 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7833 m_currentCellCoords
.GetCol() );
7844 bool wxGrid::MoveCursorDown( bool expandSelection
)
7846 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7847 m_currentCellCoords
.GetRow() < m_numRows
)
7849 if ( expandSelection
)
7851 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7852 m_selectingKeyboard
= m_currentCellCoords
;
7853 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7855 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7856 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7857 m_selectingKeyboard
.GetCol() );
7858 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7861 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7864 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7865 m_currentCellCoords
.GetCol() );
7866 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7867 m_currentCellCoords
.GetCol() );
7878 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7880 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7881 m_currentCellCoords
.GetCol() >= 0 )
7883 if ( expandSelection
)
7885 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7886 m_selectingKeyboard
= m_currentCellCoords
;
7887 if ( m_selectingKeyboard
.GetCol() > 0 )
7889 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7890 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7891 m_selectingKeyboard
.GetCol() );
7892 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7895 else if ( m_currentCellCoords
.GetCol() > 0 )
7898 MakeCellVisible( m_currentCellCoords
.GetRow(),
7899 m_currentCellCoords
.GetCol() - 1 );
7900 SetCurrentCell( m_currentCellCoords
.GetRow(),
7901 m_currentCellCoords
.GetCol() - 1 );
7912 bool wxGrid::MoveCursorRight( bool expandSelection
)
7914 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7915 m_currentCellCoords
.GetCol() < m_numCols
)
7917 if ( expandSelection
)
7919 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7920 m_selectingKeyboard
= m_currentCellCoords
;
7921 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7923 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7924 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7925 m_selectingKeyboard
.GetCol() );
7926 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7929 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7932 MakeCellVisible( m_currentCellCoords
.GetRow(),
7933 m_currentCellCoords
.GetCol() + 1 );
7934 SetCurrentCell( m_currentCellCoords
.GetRow(),
7935 m_currentCellCoords
.GetCol() + 1 );
7946 bool wxGrid::MovePageUp()
7948 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7950 int row
= m_currentCellCoords
.GetRow();
7954 m_gridWin
->GetClientSize( &cw
, &ch
);
7956 int y
= GetRowTop(row
);
7957 int newRow
= YToRow( y
- ch
+ 1 );
7962 else if ( newRow
== row
)
7967 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7968 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7976 bool wxGrid::MovePageDown()
7978 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7980 int row
= m_currentCellCoords
.GetRow();
7981 if ( row
< m_numRows
)
7984 m_gridWin
->GetClientSize( &cw
, &ch
);
7986 int y
= GetRowTop(row
);
7987 int newRow
= YToRow( y
+ ch
);
7990 newRow
= m_numRows
- 1;
7992 else if ( newRow
== row
)
7997 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7998 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8006 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8009 m_currentCellCoords
!= wxGridNoCellCoords
&&
8010 m_currentCellCoords
.GetRow() > 0 )
8012 int row
= m_currentCellCoords
.GetRow();
8013 int col
= m_currentCellCoords
.GetCol();
8015 if ( m_table
->IsEmptyCell(row
, col
) )
8017 // starting in an empty cell: find the next block of
8023 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8026 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8028 // starting at the top of a block: find the next block
8034 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8039 // starting within a block: find the top of the block
8044 if ( m_table
->IsEmptyCell(row
, col
) )
8052 MakeCellVisible( row
, col
);
8053 if ( expandSelection
)
8055 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8056 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8061 SetCurrentCell( row
, col
);
8069 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8072 m_currentCellCoords
!= wxGridNoCellCoords
&&
8073 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8075 int row
= m_currentCellCoords
.GetRow();
8076 int col
= m_currentCellCoords
.GetCol();
8078 if ( m_table
->IsEmptyCell(row
, col
) )
8080 // starting in an empty cell: find the next block of
8083 while ( row
< m_numRows
-1 )
8086 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8089 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8091 // starting at the bottom of a block: find the next block
8094 while ( row
< m_numRows
-1 )
8097 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8102 // starting within a block: find the bottom of the block
8104 while ( row
< m_numRows
-1 )
8107 if ( m_table
->IsEmptyCell(row
, col
) )
8115 MakeCellVisible( row
, col
);
8116 if ( expandSelection
)
8118 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8119 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8124 SetCurrentCell( row
, col
);
8133 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8136 m_currentCellCoords
!= wxGridNoCellCoords
&&
8137 m_currentCellCoords
.GetCol() > 0 )
8139 int row
= m_currentCellCoords
.GetRow();
8140 int col
= m_currentCellCoords
.GetCol();
8142 if ( m_table
->IsEmptyCell(row
, col
) )
8144 // starting in an empty cell: find the next block of
8150 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8153 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8155 // starting at the left of a block: find the next block
8161 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8166 // starting within a block: find the left of the block
8171 if ( m_table
->IsEmptyCell(row
, col
) )
8179 MakeCellVisible( row
, col
);
8180 if ( expandSelection
)
8182 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8183 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8188 SetCurrentCell( row
, col
);
8197 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8200 m_currentCellCoords
!= wxGridNoCellCoords
&&
8201 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8203 int row
= m_currentCellCoords
.GetRow();
8204 int col
= m_currentCellCoords
.GetCol();
8206 if ( m_table
->IsEmptyCell(row
, col
) )
8208 // starting in an empty cell: find the next block of
8211 while ( col
< m_numCols
-1 )
8214 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8217 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8219 // starting at the right of a block: find the next block
8222 while ( col
< m_numCols
-1 )
8225 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8230 // starting within a block: find the right of the block
8232 while ( col
< m_numCols
-1 )
8235 if ( m_table
->IsEmptyCell(row
, col
) )
8243 MakeCellVisible( row
, col
);
8244 if ( expandSelection
)
8246 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8247 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8252 SetCurrentCell( row
, col
);
8264 // ------ Label values and formatting
8267 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8269 *horiz
= m_rowLabelHorizAlign
;
8270 *vert
= m_rowLabelVertAlign
;
8273 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8275 *horiz
= m_colLabelHorizAlign
;
8276 *vert
= m_colLabelVertAlign
;
8279 wxString
wxGrid::GetRowLabelValue( int row
)
8283 return m_table
->GetRowLabelValue( row
);
8293 wxString
wxGrid::GetColLabelValue( int col
)
8297 return m_table
->GetColLabelValue( col
);
8308 void wxGrid::SetRowLabelSize( int width
)
8310 width
= wxMax( width
, 0 );
8311 if ( width
!= m_rowLabelWidth
)
8315 m_rowLabelWin
->Show( FALSE
);
8316 m_cornerLabelWin
->Show( FALSE
);
8318 else if ( m_rowLabelWidth
== 0 )
8320 m_rowLabelWin
->Show( TRUE
);
8321 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8324 m_rowLabelWidth
= width
;
8326 wxScrolledWindow::Refresh( TRUE
);
8331 void wxGrid::SetColLabelSize( int height
)
8333 height
= wxMax( height
, 0 );
8334 if ( height
!= m_colLabelHeight
)
8338 m_colLabelWin
->Show( FALSE
);
8339 m_cornerLabelWin
->Show( FALSE
);
8341 else if ( m_colLabelHeight
== 0 )
8343 m_colLabelWin
->Show( TRUE
);
8344 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8347 m_colLabelHeight
= height
;
8349 wxScrolledWindow::Refresh( TRUE
);
8354 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8356 if ( m_labelBackgroundColour
!= colour
)
8358 m_labelBackgroundColour
= colour
;
8359 m_rowLabelWin
->SetBackgroundColour( colour
);
8360 m_colLabelWin
->SetBackgroundColour( colour
);
8361 m_cornerLabelWin
->SetBackgroundColour( colour
);
8363 if ( !GetBatchCount() )
8365 m_rowLabelWin
->Refresh();
8366 m_colLabelWin
->Refresh();
8367 m_cornerLabelWin
->Refresh();
8372 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8374 if ( m_labelTextColour
!= colour
)
8376 m_labelTextColour
= colour
;
8377 if ( !GetBatchCount() )
8379 m_rowLabelWin
->Refresh();
8380 m_colLabelWin
->Refresh();
8385 void wxGrid::SetLabelFont( const wxFont
& font
)
8388 if ( !GetBatchCount() )
8390 m_rowLabelWin
->Refresh();
8391 m_colLabelWin
->Refresh();
8395 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8397 // allow old (incorrect) defs to be used
8400 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8401 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8402 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8407 case wxTOP
: vert
= wxALIGN_TOP
; break;
8408 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8409 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8412 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8414 m_rowLabelHorizAlign
= horiz
;
8417 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8419 m_rowLabelVertAlign
= vert
;
8422 if ( !GetBatchCount() )
8424 m_rowLabelWin
->Refresh();
8428 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8430 // allow old (incorrect) defs to be used
8433 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8434 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8435 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8440 case wxTOP
: vert
= wxALIGN_TOP
; break;
8441 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8442 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8445 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8447 m_colLabelHorizAlign
= horiz
;
8450 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8452 m_colLabelVertAlign
= vert
;
8455 if ( !GetBatchCount() )
8457 m_colLabelWin
->Refresh();
8461 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8465 m_table
->SetRowLabelValue( row
, s
);
8466 if ( !GetBatchCount() )
8468 wxRect rect
= CellToRect( row
, 0);
8469 if ( rect
.height
> 0 )
8471 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8473 rect
.width
= m_rowLabelWidth
;
8474 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8480 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8484 m_table
->SetColLabelValue( col
, s
);
8485 if ( !GetBatchCount() )
8487 wxRect rect
= CellToRect( 0, col
);
8488 if ( rect
.width
> 0 )
8490 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8492 rect
.height
= m_colLabelHeight
;
8493 m_colLabelWin
->Refresh( TRUE
, &rect
);
8499 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8501 if ( m_gridLineColour
!= colour
)
8503 m_gridLineColour
= colour
;
8505 wxClientDC
dc( m_gridWin
);
8507 DrawAllGridLines( dc
, wxRegion() );
8512 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8514 if ( m_cellHighlightColour
!= colour
)
8516 m_cellHighlightColour
= colour
;
8518 wxClientDC
dc( m_gridWin
);
8520 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8521 DrawCellHighlight(dc
, attr
);
8526 void wxGrid::SetCellHighlightPenWidth(int width
)
8528 if (m_cellHighlightPenWidth
!= width
) {
8529 m_cellHighlightPenWidth
= width
;
8531 // Just redrawing the cell highlight is not enough since that won't
8532 // make any visible change if the the thickness is getting smaller.
8533 int row
= m_currentCellCoords
.GetRow();
8534 int col
= m_currentCellCoords
.GetCol();
8535 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8537 wxRect rect
= CellToRect(row
, col
);
8538 m_gridWin
->Refresh(TRUE
, &rect
);
8542 void wxGrid::SetCellHighlightROPenWidth(int width
)
8544 if (m_cellHighlightROPenWidth
!= width
) {
8545 m_cellHighlightROPenWidth
= width
;
8547 // Just redrawing the cell highlight is not enough since that won't
8548 // make any visible change if the the thickness is getting smaller.
8549 int row
= m_currentCellCoords
.GetRow();
8550 int col
= m_currentCellCoords
.GetCol();
8551 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8553 wxRect rect
= CellToRect(row
, col
);
8554 m_gridWin
->Refresh(TRUE
, &rect
);
8558 void wxGrid::EnableGridLines( bool enable
)
8560 if ( enable
!= m_gridLinesEnabled
)
8562 m_gridLinesEnabled
= enable
;
8564 if ( !GetBatchCount() )
8568 wxClientDC
dc( m_gridWin
);
8570 DrawAllGridLines( dc
, wxRegion() );
8574 m_gridWin
->Refresh();
8581 int wxGrid::GetDefaultRowSize()
8583 return m_defaultRowHeight
;
8586 int wxGrid::GetRowSize( int row
)
8588 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8590 return GetRowHeight(row
);
8593 int wxGrid::GetDefaultColSize()
8595 return m_defaultColWidth
;
8598 int wxGrid::GetColSize( int col
)
8600 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8602 return GetColWidth(col
);
8605 // ============================================================================
8606 // access to the grid attributes: each of them has a default value in the grid
8607 // itself and may be overidden on a per-cell basis
8608 // ============================================================================
8610 // ----------------------------------------------------------------------------
8611 // setting default attributes
8612 // ----------------------------------------------------------------------------
8614 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8616 m_defaultCellAttr
->SetBackgroundColour(col
);
8618 m_gridWin
->SetBackgroundColour(col
);
8622 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8624 m_defaultCellAttr
->SetTextColour(col
);
8627 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8629 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8632 void wxGrid::SetDefaultCellOverflow( bool allow
)
8634 m_defaultCellAttr
->SetOverflow(allow
);
8637 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8639 m_defaultCellAttr
->SetFont(font
);
8642 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8644 m_defaultCellAttr
->SetRenderer(renderer
);
8647 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8649 m_defaultCellAttr
->SetEditor(editor
);
8652 // ----------------------------------------------------------------------------
8653 // access to the default attrbiutes
8654 // ----------------------------------------------------------------------------
8656 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8658 return m_defaultCellAttr
->GetBackgroundColour();
8661 wxColour
wxGrid::GetDefaultCellTextColour()
8663 return m_defaultCellAttr
->GetTextColour();
8666 wxFont
wxGrid::GetDefaultCellFont()
8668 return m_defaultCellAttr
->GetFont();
8671 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8673 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8676 bool wxGrid::GetDefaultCellOverflow()
8678 return m_defaultCellAttr
->GetOverflow();
8681 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8683 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8686 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8688 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8691 // ----------------------------------------------------------------------------
8692 // access to cell attributes
8693 // ----------------------------------------------------------------------------
8695 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8697 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8698 wxColour colour
= attr
->GetBackgroundColour();
8703 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8705 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8706 wxColour colour
= attr
->GetTextColour();
8711 wxFont
wxGrid::GetCellFont( int row
, int col
)
8713 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8714 wxFont font
= attr
->GetFont();
8719 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8721 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8722 attr
->GetAlignment(horiz
, vert
);
8726 bool wxGrid::GetCellOverflow( int row
, int col
)
8728 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8729 bool allow
= attr
->GetOverflow();
8734 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8736 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8737 attr
->GetSize( num_rows
, num_cols
);
8741 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8743 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8744 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8750 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8752 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8753 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8759 bool wxGrid::IsReadOnly(int row
, int col
) const
8761 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8762 bool isReadOnly
= attr
->IsReadOnly();
8767 // ----------------------------------------------------------------------------
8768 // attribute support: cache, automatic provider creation, ...
8769 // ----------------------------------------------------------------------------
8771 bool wxGrid::CanHaveAttributes()
8778 return m_table
->CanHaveAttributes();
8781 void wxGrid::ClearAttrCache()
8783 if ( m_attrCache
.row
!= -1 )
8785 wxSafeDecRef(m_attrCache
.attr
);
8786 m_attrCache
.attr
= NULL
;
8787 m_attrCache
.row
= -1;
8791 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8795 wxGrid
*self
= (wxGrid
*)this; // const_cast
8797 self
->ClearAttrCache();
8798 self
->m_attrCache
.row
= row
;
8799 self
->m_attrCache
.col
= col
;
8800 self
->m_attrCache
.attr
= attr
;
8805 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8807 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8809 *attr
= m_attrCache
.attr
;
8810 wxSafeIncRef(m_attrCache
.attr
);
8812 #ifdef DEBUG_ATTR_CACHE
8813 gs_nAttrCacheHits
++;
8820 #ifdef DEBUG_ATTR_CACHE
8821 gs_nAttrCacheMisses
++;
8827 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8829 wxGridCellAttr
*attr
= NULL
;
8830 // Additional test to avoid looking at the cache e.g. for
8831 // wxNoCellCoords, as this will confuse memory management.
8834 if ( !LookupAttr(row
, col
, &attr
) )
8836 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8837 : (wxGridCellAttr
*)NULL
;
8838 CacheAttr(row
, col
, attr
);
8843 attr
->SetDefAttr(m_defaultCellAttr
);
8847 attr
= m_defaultCellAttr
;
8854 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8856 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8858 wxCHECK_MSG( m_table
, attr
,
8859 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8861 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8864 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8866 // artificially inc the ref count to match DecRef() in caller
8868 m_table
->SetAttr(attr
, row
, col
);
8874 // ----------------------------------------------------------------------------
8875 // setting column attributes (wrappers around SetColAttr)
8876 // ----------------------------------------------------------------------------
8878 void wxGrid::SetColFormatBool(int col
)
8880 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8883 void wxGrid::SetColFormatNumber(int col
)
8885 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8888 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8890 wxString typeName
= wxGRID_VALUE_FLOAT
;
8891 if ( (width
!= -1) || (precision
!= -1) )
8893 typeName
<< _T(':') << width
<< _T(',') << precision
;
8896 SetColFormatCustom(col
, typeName
);
8899 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8901 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8903 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8905 attr
= new wxGridCellAttr
;
8906 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8907 attr
->SetRenderer(renderer
);
8909 SetColAttr(col
, attr
);
8913 // ----------------------------------------------------------------------------
8914 // setting cell attributes: this is forwarded to the table
8915 // ----------------------------------------------------------------------------
8917 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
8919 if ( CanHaveAttributes() )
8921 m_table
->SetAttr(attr
, row
, col
);
8930 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8932 if ( CanHaveAttributes() )
8934 m_table
->SetRowAttr(attr
, row
);
8943 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8945 if ( CanHaveAttributes() )
8947 m_table
->SetColAttr(attr
, col
);
8956 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8958 if ( CanHaveAttributes() )
8960 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8961 attr
->SetBackgroundColour(colour
);
8966 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8968 if ( CanHaveAttributes() )
8970 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8971 attr
->SetTextColour(colour
);
8976 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8978 if ( CanHaveAttributes() )
8980 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8981 attr
->SetFont(font
);
8986 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8988 if ( CanHaveAttributes() )
8990 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8991 attr
->SetAlignment(horiz
, vert
);
8996 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
8998 if ( CanHaveAttributes() )
9000 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9001 attr
->SetOverflow(allow
);
9006 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9008 if ( CanHaveAttributes() )
9010 int cell_rows
, cell_cols
;
9012 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9013 attr
->GetSize(&cell_rows
, &cell_cols
);
9014 attr
->SetSize(num_rows
, num_cols
);
9017 // Cannot set the size of a cell to 0 or negative values
9018 // While it is perfectly legal to do that, this function cannot
9019 // handle all the possibilies, do it by hand by getting the CellAttr.
9020 // You can only set the size of a cell to 1,1 or greater with this fn
9021 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9022 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9023 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9024 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9026 // if this was already a multicell then "turn off" the other cells first
9027 if ((cell_rows
> 1) || (cell_rows
> 1))
9030 for (j
=row
; j
<row
+cell_rows
; j
++)
9032 for (i
=col
; i
<col
+cell_cols
; i
++)
9034 if ((i
!= col
) || (j
!= row
))
9036 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9037 attr_stub
->SetSize( 1, 1 );
9038 attr_stub
->DecRef();
9044 // mark the cells that will be covered by this cell to
9045 // negative or zero values to point back at this cell
9046 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9049 for (j
=row
; j
<row
+num_rows
; j
++)
9051 for (i
=col
; i
<col
+num_cols
; i
++)
9053 if ((i
!= col
) || (j
!= row
))
9055 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9056 attr_stub
->SetSize( row
-j
, col
-i
);
9057 attr_stub
->DecRef();
9065 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9067 if ( CanHaveAttributes() )
9069 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9070 attr
->SetRenderer(renderer
);
9075 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9077 if ( CanHaveAttributes() )
9079 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9080 attr
->SetEditor(editor
);
9085 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9087 if ( CanHaveAttributes() )
9089 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9090 attr
->SetReadOnly(isReadOnly
);
9095 // ----------------------------------------------------------------------------
9096 // Data type registration
9097 // ----------------------------------------------------------------------------
9099 void wxGrid::RegisterDataType(const wxString
& typeName
,
9100 wxGridCellRenderer
* renderer
,
9101 wxGridCellEditor
* editor
)
9103 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9107 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9109 wxString typeName
= m_table
->GetTypeName(row
, col
);
9110 return GetDefaultEditorForType(typeName
);
9113 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9115 wxString typeName
= m_table
->GetTypeName(row
, col
);
9116 return GetDefaultRendererForType(typeName
);
9120 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9122 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9123 if ( index
== wxNOT_FOUND
)
9125 wxFAIL_MSG(wxT("Unknown data type name"));
9130 return m_typeRegistry
->GetEditor(index
);
9134 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9136 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9137 if ( index
== wxNOT_FOUND
)
9139 wxFAIL_MSG(wxT("Unknown data type name"));
9144 return m_typeRegistry
->GetRenderer(index
);
9148 // ----------------------------------------------------------------------------
9150 // ----------------------------------------------------------------------------
9152 void wxGrid::EnableDragRowSize( bool enable
)
9154 m_canDragRowSize
= enable
;
9158 void wxGrid::EnableDragColSize( bool enable
)
9160 m_canDragColSize
= enable
;
9163 void wxGrid::EnableDragGridSize( bool enable
)
9165 m_canDragGridSize
= enable
;
9169 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9171 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
9173 if ( resizeExistingRows
)
9175 // since we are resizing all rows to the default row size,
9176 // we can simply clear the row heights and row bottoms
9177 // arrays (which also allows us to take advantage of
9178 // some speed optimisations)
9179 m_rowHeights
.Empty();
9180 m_rowBottoms
.Empty();
9181 if ( !GetBatchCount() )
9186 void wxGrid::SetRowSize( int row
, int height
)
9188 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9190 if ( m_rowHeights
.IsEmpty() )
9192 // need to really create the array
9196 int h
= wxMax( 0, height
);
9197 int diff
= h
- m_rowHeights
[row
];
9199 m_rowHeights
[row
] = h
;
9201 for ( i
= row
; i
< m_numRows
; i
++ )
9203 m_rowBottoms
[i
] += diff
;
9205 if ( !GetBatchCount() )
9209 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9211 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
9213 if ( resizeExistingCols
)
9215 // since we are resizing all columns to the default column size,
9216 // we can simply clear the col widths and col rights
9217 // arrays (which also allows us to take advantage of
9218 // some speed optimisations)
9219 m_colWidths
.Empty();
9220 m_colRights
.Empty();
9221 if ( !GetBatchCount() )
9226 void wxGrid::SetColSize( int col
, int width
)
9228 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9230 // should we check that it's bigger than GetColMinimalWidth(col) here?
9232 if ( m_colWidths
.IsEmpty() )
9234 // need to really create the array
9238 // if < 0 calc new width from label
9242 wxArrayString lines
;
9243 wxClientDC
dc(m_colLabelWin
);
9244 dc
.SetFont(GetLabelFont());
9245 StringToLines(GetColLabelValue(col
), lines
);
9246 GetTextBoxSize(dc
, lines
, &w
, &h
);
9249 int w
= wxMax( 0, width
);
9250 int diff
= w
- m_colWidths
[col
];
9251 m_colWidths
[col
] = w
;
9254 for ( i
= col
; i
< m_numCols
; i
++ )
9256 m_colRights
[i
] += diff
;
9258 if ( !GetBatchCount() )
9263 void wxGrid::SetColMinimalWidth( int col
, int width
)
9265 m_colMinWidths
.Put(col
, width
);
9268 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9270 m_rowMinHeights
.Put(row
, width
);
9273 int wxGrid::GetColMinimalWidth(int col
) const
9275 long value
= m_colMinWidths
.Get(col
);
9276 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
9279 int wxGrid::GetRowMinimalHeight(int row
) const
9281 long value
= m_rowMinHeights
.Get(row
);
9282 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
9285 // ----------------------------------------------------------------------------
9287 // ----------------------------------------------------------------------------
9289 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9291 wxClientDC
dc(m_gridWin
);
9293 // init both of them to avoid compiler warnings, even if weo nly need one
9301 wxCoord extent
, extentMax
= 0;
9302 int max
= column
? m_numRows
: m_numCols
;
9303 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9310 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9311 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9314 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9315 extent
= column
? size
.x
: size
.y
;
9316 if ( extent
> extentMax
)
9327 // now also compare with the column label extent
9329 dc
.SetFont( GetLabelFont() );
9332 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9334 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9336 extent
= column
? w
: h
;
9337 if ( extent
> extentMax
)
9344 // empty column - give default extent (notice that if extentMax is less
9345 // than default extent but != 0, it's ok)
9346 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9352 // leave some space around text
9363 SetColSize(col
, extentMax
);
9364 if ( !GetBatchCount() )
9367 m_gridWin
->GetClientSize( &cw
, &ch
);
9368 wxRect
rect ( CellToRect( 0, col
) );
9370 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9371 rect
.width
= cw
- rect
.x
;
9372 rect
.height
= m_colLabelHeight
;
9373 m_colLabelWin
->Refresh( TRUE
, &rect
);
9378 SetRowSize(row
, extentMax
);
9379 if ( !GetBatchCount() )
9382 m_gridWin
->GetClientSize( &cw
, &ch
);
9383 wxRect
rect ( CellToRect( row
, 0 ) );
9385 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9386 rect
.width
= m_rowLabelWidth
;
9387 rect
.height
= ch
- rect
.y
;
9388 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9394 SetColMinimalWidth(col
, extentMax
);
9396 SetRowMinimalHeight(row
, extentMax
);
9400 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9402 int width
= m_rowLabelWidth
;
9407 for ( int col
= 0; col
< m_numCols
; col
++ )
9411 AutoSizeColumn(col
, setAsMin
);
9414 width
+= GetColWidth(col
);
9423 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9425 int height
= m_colLabelHeight
;
9430 for ( int row
= 0; row
< m_numRows
; row
++ )
9434 AutoSizeRow(row
, setAsMin
);
9437 height
+= GetRowHeight(row
);
9446 void wxGrid::AutoSize()
9450 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9452 // round up the size to a multiple of scroll step - this ensures that we
9453 // won't get the scrollbars if we're sized exactly to this width
9454 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9456 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9457 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9459 // distribute the extra space between the columns/rows to avoid having
9460 // extra white space
9462 // Remove the extra m_extraWidth + 1 added above
9463 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9464 if ( diff
&& m_numCols
)
9466 // try to resize the columns uniformly
9467 wxCoord diffPerCol
= diff
/ m_numCols
;
9470 for ( int col
= 0; col
< m_numCols
; col
++ )
9472 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9476 // add remaining amount to the last columns
9477 diff
-= diffPerCol
* m_numCols
;
9480 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9482 SetColSize(col
, GetColWidth(col
) + 1);
9488 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9489 if ( diff
&& m_numRows
)
9491 // try to resize the columns uniformly
9492 wxCoord diffPerRow
= diff
/ m_numRows
;
9495 for ( int row
= 0; row
< m_numRows
; row
++ )
9497 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9501 // add remaining amount to the last rows
9502 diff
-= diffPerRow
* m_numRows
;
9505 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9507 SetRowSize(row
, GetRowHeight(row
) + 1);
9514 SetClientSize(sizeFit
);
9517 wxSize
wxGrid::DoGetBestSize() const
9519 // don't set sizes, only calculate them
9520 wxGrid
*self
= (wxGrid
*)this; // const_cast
9523 width
= self
->SetOrCalcColumnSizes(TRUE
);
9524 height
= self
->SetOrCalcRowSizes(TRUE
);
9526 int maxwidth
, maxheight
;
9527 wxDisplaySize( & maxwidth
, & maxheight
);
9529 if ( width
> maxwidth
) width
= maxwidth
;
9530 if ( height
> maxheight
) height
= maxheight
;
9532 return wxSize( width
, height
);
9541 wxPen
& wxGrid::GetDividerPen() const
9546 // ----------------------------------------------------------------------------
9547 // cell value accessor functions
9548 // ----------------------------------------------------------------------------
9550 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9554 m_table
->SetValue( row
, col
, s
);
9555 if ( !GetBatchCount() )
9558 wxRect
rect( CellToRect( row
, col
) );
9560 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9561 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9562 m_gridWin
->Refresh( FALSE
, &rect
);
9565 if ( m_currentCellCoords
.GetRow() == row
&&
9566 m_currentCellCoords
.GetCol() == col
&&
9567 IsCellEditControlShown())
9568 // Note: If we are using IsCellEditControlEnabled,
9569 // this interacts badly with calling SetCellValue from
9570 // an EVT_GRID_CELL_CHANGE handler.
9572 HideCellEditControl();
9573 ShowCellEditControl(); // will reread data from table
9580 // ------ Block, row and col selection
9583 void wxGrid::SelectRow( int row
, bool addToSelected
)
9585 if ( IsSelection() && !addToSelected
)
9589 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9593 void wxGrid::SelectCol( int col
, bool addToSelected
)
9595 if ( IsSelection() && !addToSelected
)
9599 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9603 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9604 bool addToSelected
)
9606 if ( IsSelection() && !addToSelected
)
9610 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9611 FALSE
, addToSelected
);
9615 void wxGrid::SelectAll()
9617 if ( m_numRows
> 0 && m_numCols
> 0 )
9620 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9625 // ------ Cell, row and col deselection
9628 void wxGrid::DeselectRow( int row
)
9633 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9635 if ( m_selection
->IsInSelection(row
, 0 ) )
9636 m_selection
->ToggleCellSelection( row
, 0);
9640 int nCols
= GetNumberCols();
9641 for ( int i
= 0; i
< nCols
; i
++ )
9643 if ( m_selection
->IsInSelection(row
, i
) )
9644 m_selection
->ToggleCellSelection( row
, i
);
9649 void wxGrid::DeselectCol( int col
)
9654 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9656 if ( m_selection
->IsInSelection(0, col
) )
9657 m_selection
->ToggleCellSelection( 0, col
);
9661 int nRows
= GetNumberRows();
9662 for ( int i
= 0; i
< nRows
; i
++ )
9664 if ( m_selection
->IsInSelection(i
, col
) )
9665 m_selection
->ToggleCellSelection(i
, col
);
9670 void wxGrid::DeselectCell( int row
, int col
)
9672 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9673 m_selection
->ToggleCellSelection(row
, col
);
9676 bool wxGrid::IsSelection()
9678 return ( m_selection
&& (m_selection
->IsSelection() ||
9679 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9680 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9683 bool wxGrid::IsInSelection( int row
, int col
) const
9685 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9686 ( row
>= m_selectingTopLeft
.GetRow() &&
9687 col
>= m_selectingTopLeft
.GetCol() &&
9688 row
<= m_selectingBottomRight
.GetRow() &&
9689 col
<= m_selectingBottomRight
.GetCol() )) );
9692 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9694 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9695 return m_selection
->m_cellSelection
;
9697 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9699 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9700 return m_selection
->m_blockSelectionTopLeft
;
9702 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9704 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9705 return m_selection
->m_blockSelectionBottomRight
;
9707 wxArrayInt
wxGrid::GetSelectedRows() const
9709 if (!m_selection
) { wxArrayInt a
; return a
; }
9710 return m_selection
->m_rowSelection
;
9712 wxArrayInt
wxGrid::GetSelectedCols() const
9714 if (!m_selection
) { wxArrayInt a
; return a
; }
9715 return m_selection
->m_colSelection
;
9719 void wxGrid::ClearSelection()
9721 m_selectingTopLeft
= wxGridNoCellCoords
;
9722 m_selectingBottomRight
= wxGridNoCellCoords
;
9724 m_selection
->ClearSelection();
9728 // This function returns the rectangle that encloses the given block
9729 // in device coords clipped to the client size of the grid window.
9731 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9732 const wxGridCellCoords
&bottomRight
)
9734 wxRect
rect( wxGridNoCellRect
);
9737 cellRect
= CellToRect( topLeft
);
9738 if ( cellRect
!= wxGridNoCellRect
)
9744 rect
= wxRect( 0, 0, 0, 0 );
9747 cellRect
= CellToRect( bottomRight
);
9748 if ( cellRect
!= wxGridNoCellRect
)
9754 return wxGridNoCellRect
;
9758 int left
= rect
.GetLeft();
9759 int top
= rect
.GetTop();
9760 int right
= rect
.GetRight();
9761 int bottom
= rect
.GetBottom();
9763 int leftCol
= topLeft
.GetCol();
9764 int topRow
= topLeft
.GetRow();
9765 int rightCol
= bottomRight
.GetCol();
9766 int bottomRow
= bottomRight
.GetRow();
9789 for ( j
= topRow
; j
<= bottomRow
; j
++ )
9791 for ( i
= leftCol
; i
<= rightCol
; i
++ )
9793 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
9795 cellRect
= CellToRect( j
, i
);
9797 if (cellRect
.x
< left
)
9799 if (cellRect
.y
< top
)
9801 if (cellRect
.x
+ cellRect
.width
> right
)
9802 right
= cellRect
.x
+ cellRect
.width
;
9803 if (cellRect
.y
+ cellRect
.height
> bottom
)
9804 bottom
= cellRect
.y
+ cellRect
.height
;
9806 else i
= rightCol
; // jump over inner cells.
9810 // convert to scrolled coords
9812 CalcScrolledPosition( left
, top
, &left
, &top
);
9813 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9816 m_gridWin
->GetClientSize( &cw
, &ch
);
9818 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9819 return wxRect( 0, 0, 0, 0);
9821 rect
.SetLeft( wxMax(0, left
) );
9822 rect
.SetTop( wxMax(0, top
) );
9823 rect
.SetRight( wxMin(cw
, right
) );
9824 rect
.SetBottom( wxMin(ch
, bottom
) );
9832 // ------ Grid event classes
9835 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9837 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9838 int row
, int col
, int x
, int y
, bool sel
,
9839 bool control
, bool shift
, bool alt
, bool meta
)
9840 : wxNotifyEvent( type
, id
)
9847 m_control
= control
;
9852 SetEventObject(obj
);
9856 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9858 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9859 int rowOrCol
, int x
, int y
,
9860 bool control
, bool shift
, bool alt
, bool meta
)
9861 : wxNotifyEvent( type
, id
)
9863 m_rowOrCol
= rowOrCol
;
9866 m_control
= control
;
9871 SetEventObject(obj
);
9875 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9877 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9878 const wxGridCellCoords
& topLeft
,
9879 const wxGridCellCoords
& bottomRight
,
9880 bool sel
, bool control
,
9881 bool shift
, bool alt
, bool meta
)
9882 : wxNotifyEvent( type
, id
)
9884 m_topLeft
= topLeft
;
9885 m_bottomRight
= bottomRight
;
9887 m_control
= control
;
9892 SetEventObject(obj
);
9896 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9898 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9899 wxObject
* obj
, int row
,
9900 int col
, wxControl
* ctrl
)
9901 : wxCommandEvent(type
, id
)
9903 SetEventObject(obj
);
9910 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9912 #endif // wxUSE_GRID