1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "grid.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/dcclient.h"
38 #include "wx/settings.h"
40 #include "wx/textctrl.h"
41 #include "wx/checkbox.h"
42 #include "wx/combobox.h"
43 #include "wx/valtext.h"
47 #include "wx/textfile.h"
48 #include "wx/spinctrl.h"
49 #include "wx/tokenzr.h"
52 #include "wx/generic/gridsel.h"
54 #if defined(__WXMOTIF__)
55 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
57 #define WXUNUSED_MOTIF(identifier) identifier
60 #if defined(__WXGTK__)
61 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
63 #define WXUNUSED_GTK(identifier) identifier
66 // Required for wxIs... functions
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
74 class WXDLLIMPEXP_ADV
);
76 struct wxGridCellWithAttr
78 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
79 : coords(row
, col
), attr(attr_
)
88 wxGridCellCoords coords
;
92 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
93 // without rewriting the macros, which require a public copy constructor.
96 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
97 class WXDLLIMPEXP_ADV
);
99 #include "wx/arrimpl.cpp"
101 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
102 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
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 WXDLLIMPEXP_ADV 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 WXDLLIMPEXP_ADV 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 WXDLLIMPEXP_ADV 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 WXDLLIMPEXP_ADV 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
& );
226 void OnFocus( wxFocusEvent
& );
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 WXDLLIMPEXP_ADV 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 WXDLLIMPEXP_ADV 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 WXDLLIMPEXP_ADV 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_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
340 class WXDLLIMPEXP_ADV
);
343 class WXDLLIMPEXP_ADV wxGridTypeRegistry
346 wxGridTypeRegistry() {}
347 ~wxGridTypeRegistry();
349 void RegisterDataType(const wxString
& typeName
,
350 wxGridCellRenderer
* renderer
,
351 wxGridCellEditor
* editor
);
353 // find one of already registered data types
354 int FindRegisteredDataType(const wxString
& typeName
);
356 // try to FindRegisteredDataType(), if this fails and typeName is one of
357 // standard typenames, register it and return its index
358 int FindDataType(const wxString
& typeName
);
360 // try to FindDataType(), if it fails see if it is not one of already
361 // registered data types with some params in which case clone the
362 // registered data type and set params for it
363 int FindOrCloneDataType(const wxString
& typeName
);
365 wxGridCellRenderer
* GetRenderer(int index
);
366 wxGridCellEditor
* GetEditor(int index
);
369 wxGridDataTypeInfoArray m_typeinfo
;
372 // ----------------------------------------------------------------------------
373 // conditional compilation
374 // ----------------------------------------------------------------------------
376 #ifndef WXGRID_DRAW_LINES
377 #define WXGRID_DRAW_LINES 1
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 //#define DEBUG_ATTR_CACHE
385 #ifdef DEBUG_ATTR_CACHE
386 static size_t gs_nAttrCacheHits
= 0;
387 static size_t gs_nAttrCacheMisses
= 0;
388 #endif // DEBUG_ATTR_CACHE
390 // ----------------------------------------------------------------------------
392 // ----------------------------------------------------------------------------
394 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
395 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
398 // TODO: this doesn't work at all, grid cells have different sizes and approx
399 // calculations don't work as because of the size mismatch scrollbars
400 // sometimes fail to be shown when they should be or vice versa
402 // The scroll bars may be a little flakey once in a while, but that is
403 // surely much less horrible than having scroll lines of only 1!!!
406 // Well, it's still seriously broken so it might be better but needs
409 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
410 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
412 // the size of hash tables used a bit everywhere (the max number of elements
413 // in these hash tables is the number of rows/columns)
414 static const int GRID_HASH_SIZE
= 100;
416 // ----------------------------------------------------------------------------
418 // ----------------------------------------------------------------------------
420 static inline int GetScrollX(int x
)
422 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
425 static inline int GetScrollY(int y
)
427 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
430 // ============================================================================
432 // ============================================================================
434 // ----------------------------------------------------------------------------
436 // ----------------------------------------------------------------------------
438 wxGridCellEditor::wxGridCellEditor()
445 wxGridCellEditor::~wxGridCellEditor()
450 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
451 wxWindowID
WXUNUSED(id
),
452 wxEvtHandler
* evtHandler
)
455 m_control
->PushEventHandler(evtHandler
);
458 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
459 wxGridCellAttr
*attr
)
461 // erase the background because we might not fill the cell
462 wxClientDC
dc(m_control
->GetParent());
463 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
465 gridWindow
->GetOwner()->PrepareDC(dc
);
467 dc
.SetPen(*wxTRANSPARENT_PEN
);
468 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
469 dc
.DrawRectangle(rectCell
);
471 // redraw the control we just painted over
472 m_control
->Refresh();
475 void wxGridCellEditor::Destroy()
479 m_control
->PopEventHandler(true /* delete it*/);
481 m_control
->Destroy();
486 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
488 wxASSERT_MSG(m_control
,
489 wxT("The wxGridCellEditor must be Created first!"));
490 m_control
->Show(show
);
494 // set the colours/fonts if we have any
497 m_colFgOld
= m_control
->GetForegroundColour();
498 m_control
->SetForegroundColour(attr
->GetTextColour());
500 m_colBgOld
= m_control
->GetBackgroundColour();
501 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
503 m_fontOld
= m_control
->GetFont();
504 m_control
->SetFont(attr
->GetFont());
506 // can't do anything more in the base class version, the other
507 // attributes may only be used by the derived classes
512 // restore the standard colours fonts
513 if ( m_colFgOld
.Ok() )
515 m_control
->SetForegroundColour(m_colFgOld
);
516 m_colFgOld
= wxNullColour
;
519 if ( m_colBgOld
.Ok() )
521 m_control
->SetBackgroundColour(m_colBgOld
);
522 m_colBgOld
= wxNullColour
;
525 if ( m_fontOld
.Ok() )
527 m_control
->SetFont(m_fontOld
);
528 m_fontOld
= wxNullFont
;
533 void wxGridCellEditor::SetSize(const wxRect
& rect
)
535 wxASSERT_MSG(m_control
,
536 wxT("The wxGridCellEditor must be Created first!"));
537 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
540 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
545 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
547 // accept the simple key presses, not anything with Ctrl/Alt/Meta
548 return !(event
.ControlDown() || event
.AltDown());
551 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
556 void wxGridCellEditor::StartingClick()
562 // ----------------------------------------------------------------------------
563 // wxGridCellTextEditor
564 // ----------------------------------------------------------------------------
566 wxGridCellTextEditor::wxGridCellTextEditor()
571 void wxGridCellTextEditor::Create(wxWindow
* parent
,
573 wxEvtHandler
* evtHandler
)
575 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
576 wxDefaultPosition
, wxDefaultSize
577 #if defined(__WXMSW__)
578 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
582 // set max length allowed in the textctrl, if the parameter was set
585 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
588 wxGridCellEditor::Create(parent
, id
, evtHandler
);
591 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
592 wxGridCellAttr
* WXUNUSED(attr
))
594 // as we fill the entire client area, don't do anything here to minimize
598 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
600 wxRect
rect(rectOrig
);
602 // Make the edit control large enough to allow for internal
605 // TODO: remove this if the text ctrl sizing is improved esp. for
608 #if defined(__WXGTK__)
617 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
619 // MB: treat MSW separately here otherwise the caret doesn't show
620 // when the editor is in the first row.
621 #if defined(__WXMSW__)
624 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
627 #if defined(__WXMOTIF__)
631 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
632 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
633 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
634 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
637 wxGridCellEditor::SetSize(rect
);
640 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
642 wxASSERT_MSG(m_control
,
643 wxT("The wxGridCellEditor must be Created first!"));
645 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
647 DoBeginEdit(m_startValue
);
650 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
652 Text()->SetValue(startValue
);
653 Text()->SetInsertionPointEnd();
654 Text()->SetSelection(-1,-1);
658 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
661 wxASSERT_MSG(m_control
,
662 wxT("The wxGridCellEditor must be Created first!"));
664 bool changed
= false;
665 wxString value
= Text()->GetValue();
666 if (value
!= m_startValue
)
670 grid
->GetTable()->SetValue(row
, col
, value
);
672 m_startValue
= wxEmptyString
;
673 // No point in setting the text of the hidden control
674 //Text()->SetValue(m_startValue);
680 void wxGridCellTextEditor::Reset()
682 wxASSERT_MSG(m_control
,
683 wxT("The wxGridCellEditor must be Created first!"));
685 DoReset(m_startValue
);
688 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
690 Text()->SetValue(startValue
);
691 Text()->SetInsertionPointEnd();
694 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
696 if ( wxGridCellEditor::IsAcceptedKey(event
) )
698 int keycode
= event
.GetKeyCode();
712 case WXK_NUMPAD_MULTIPLY
:
716 case WXK_NUMPAD_SUBTRACT
:
718 case WXK_NUMPAD_DECIMAL
:
720 case WXK_NUMPAD_DIVIDE
:
724 // accept 8 bit chars too if isprint() agrees
725 if ( (keycode
< 255) && (wxIsprint(keycode
)) )
733 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
735 if ( !Text()->EmulateKeyPress(event
) )
741 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
742 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
744 #if defined(__WXMOTIF__) || defined(__WXGTK__)
745 // wxMotif needs a little extra help...
746 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
747 wxString
s( Text()->GetValue() );
748 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
750 Text()->SetInsertionPoint( pos
);
752 // the other ports can handle a Return key press
758 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
768 if ( !params
.ToLong(&tmp
) )
770 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
774 m_maxChars
= (size_t)tmp
;
779 // return the value in the text control
780 wxString
wxGridCellTextEditor::GetValue() const
782 return Text()->GetValue();
785 // ----------------------------------------------------------------------------
786 // wxGridCellNumberEditor
787 // ----------------------------------------------------------------------------
789 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
795 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
797 wxEvtHandler
* evtHandler
)
801 // create a spin ctrl
802 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
803 wxDefaultPosition
, wxDefaultSize
,
807 wxGridCellEditor::Create(parent
, id
, evtHandler
);
811 // just a text control
812 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
815 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
816 #endif // wxUSE_VALIDATORS
820 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
822 // first get the value
823 wxGridTableBase
*table
= grid
->GetTable();
824 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
826 m_valueOld
= table
->GetValueAsLong(row
, col
);
831 wxString sValue
= table
->GetValue(row
, col
);
832 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
834 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
841 Spin()->SetValue((int)m_valueOld
);
846 DoBeginEdit(GetString());
850 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
859 value
= Spin()->GetValue();
860 changed
= value
!= m_valueOld
;
862 text
= wxString::Format(wxT("%ld"), value
);
866 text
= Text()->GetValue();
867 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
872 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
873 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
875 grid
->GetTable()->SetValue(row
, col
, text
);
881 void wxGridCellNumberEditor::Reset()
885 Spin()->SetValue((int)m_valueOld
);
889 DoReset(GetString());
893 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
895 if ( wxGridCellEditor::IsAcceptedKey(event
) )
897 int keycode
= event
.GetKeyCode();
913 case WXK_NUMPAD_SUBTRACT
:
919 if ( (keycode
< 128) && wxIsdigit(keycode
) )
927 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
931 int keycode
= event
.GetKeyCode();
932 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
933 || keycode
== WXK_NUMPAD0
934 || keycode
== WXK_NUMPAD1
935 || keycode
== WXK_NUMPAD2
936 || keycode
== WXK_NUMPAD3
937 || keycode
== WXK_NUMPAD4
938 || keycode
== WXK_NUMPAD5
939 || keycode
== WXK_NUMPAD6
940 || keycode
== WXK_NUMPAD7
941 || keycode
== WXK_NUMPAD8
942 || keycode
== WXK_NUMPAD9
943 || keycode
== WXK_ADD
944 || keycode
== WXK_NUMPAD_ADD
945 || keycode
== WXK_SUBTRACT
946 || keycode
== WXK_NUMPAD_SUBTRACT
)
948 wxGridCellTextEditor::StartingKey(event
);
958 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
969 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
973 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
977 // skip the error message below
982 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
986 // return the value in the spin control if it is there (the text control otherwise)
987 wxString
wxGridCellNumberEditor::GetValue() const
993 long value
= Spin()->GetValue();
994 s
.Printf(wxT("%ld"), value
);
998 s
= Text()->GetValue();
1003 // ----------------------------------------------------------------------------
1004 // wxGridCellFloatEditor
1005 // ----------------------------------------------------------------------------
1007 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1010 m_precision
= precision
;
1013 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1015 wxEvtHandler
* evtHandler
)
1017 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1019 #if wxUSE_VALIDATORS
1020 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1021 #endif // wxUSE_VALIDATORS
1024 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1026 // first get the value
1027 wxGridTableBase
*table
= grid
->GetTable();
1028 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1030 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1035 wxString sValue
= table
->GetValue(row
, col
);
1036 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1038 wxFAIL_MSG( _T("this cell doesn't have float value") );
1043 DoBeginEdit(GetString());
1046 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1050 wxString
text(Text()->GetValue());
1052 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1054 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1055 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1057 grid
->GetTable()->SetValue(row
, col
, text
);
1064 void wxGridCellFloatEditor::Reset()
1066 DoReset(GetString());
1069 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1071 int keycode
= event
.GetKeyCode();
1073 tmpbuf
[0] = (char) keycode
;
1075 bool is_decimal_point
= ( wxString(tmpbuf
, *wxConvCurrent
) ==
1076 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1077 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1079 || keycode
== WXK_NUMPAD0
1080 || keycode
== WXK_NUMPAD1
1081 || keycode
== WXK_NUMPAD2
1082 || keycode
== WXK_NUMPAD3
1083 || keycode
== WXK_NUMPAD4
1084 || keycode
== WXK_NUMPAD5
1085 || keycode
== WXK_NUMPAD6
1086 || keycode
== WXK_NUMPAD7
1087 || keycode
== WXK_NUMPAD8
1088 || keycode
== WXK_NUMPAD9
1089 || keycode
== WXK_ADD
1090 || keycode
== WXK_NUMPAD_ADD
1091 || keycode
== WXK_SUBTRACT
1092 || keycode
== WXK_NUMPAD_SUBTRACT
)
1094 wxGridCellTextEditor::StartingKey(event
);
1096 // skip Skip() below
1103 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1114 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1118 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1120 m_precision
= (int)tmp
;
1122 // skip the error message below
1127 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1131 wxString
wxGridCellFloatEditor::GetString() const
1134 if ( m_width
== -1 )
1136 // default width/precision
1139 else if ( m_precision
== -1 )
1141 // default precision
1142 fmt
.Printf(_T("%%%d.f"), m_width
);
1146 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1149 return wxString::Format(fmt
, m_valueOld
);
1152 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1154 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1156 int keycode
= event
.GetKeyCode();
1170 case WXK_NUMPAD_ADD
:
1172 case WXK_NUMPAD_SUBTRACT
:
1174 case WXK_NUMPAD_DECIMAL
:
1179 // additionally accept 'e' as in '1e+6', also '-', '+', and '.'
1181 tmpbuf
[0] = (char) keycode
;
1183 bool is_decimal_point
=
1184 ( wxString(tmpbuf
, *wxConvCurrent
) ==
1185 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1186 wxLOCALE_CAT_NUMBER
) );
1187 if ( (keycode
< 128) &&
1188 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1189 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1198 #endif // wxUSE_TEXTCTRL
1202 // ----------------------------------------------------------------------------
1203 // wxGridCellBoolEditor
1204 // ----------------------------------------------------------------------------
1206 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1208 wxEvtHandler
* evtHandler
)
1210 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1211 wxDefaultPosition
, wxDefaultSize
,
1214 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1217 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1219 bool resize
= false;
1220 wxSize size
= m_control
->GetSize();
1221 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1223 // check if the checkbox is not too big/small for this cell
1224 wxSize sizeBest
= m_control
->GetBestSize();
1225 if ( !(size
== sizeBest
) )
1227 // reset to default size if it had been made smaller
1233 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1235 // leave 1 pixel margin
1236 size
.x
= size
.y
= minSize
- 2;
1243 m_control
->SetSize(size
);
1246 // position it in the centre of the rectangle (TODO: support alignment?)
1248 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1249 // the checkbox without label still has some space to the right in wxGTK,
1250 // so shift it to the right
1252 #elif defined(__WXMSW__)
1253 // here too, but in other way
1258 int hAlign
= wxALIGN_CENTRE
;
1259 int vAlign
= wxALIGN_CENTRE
;
1261 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1264 if (hAlign
== wxALIGN_LEFT
)
1270 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1272 else if (hAlign
== wxALIGN_RIGHT
)
1274 x
= r
.x
+ r
.width
- size
.x
- 2;
1275 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1277 else if (hAlign
== wxALIGN_CENTRE
)
1279 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1280 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1283 m_control
->Move(x
, y
);
1286 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1288 m_control
->Show(show
);
1292 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1293 CBox()->SetBackgroundColour(colBg
);
1297 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1299 wxASSERT_MSG(m_control
,
1300 wxT("The wxGridCellEditor must be Created first!"));
1302 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1303 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1306 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1307 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1309 CBox()->SetValue(m_startValue
);
1313 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1316 wxASSERT_MSG(m_control
,
1317 wxT("The wxGridCellEditor must be Created first!"));
1319 bool changed
= false;
1320 bool value
= CBox()->GetValue();
1321 if ( value
!= m_startValue
)
1326 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1327 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1329 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1335 void wxGridCellBoolEditor::Reset()
1337 wxASSERT_MSG(m_control
,
1338 wxT("The wxGridCellEditor must be Created first!"));
1340 CBox()->SetValue(m_startValue
);
1343 void wxGridCellBoolEditor::StartingClick()
1345 CBox()->SetValue(!CBox()->GetValue());
1348 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1350 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1352 int keycode
= event
.GetKeyCode();
1356 case WXK_NUMPAD_MULTIPLY
:
1358 case WXK_NUMPAD_ADD
:
1360 case WXK_NUMPAD_SUBTRACT
:
1371 // return the value as "1" for true and the empty string for false
1372 wxString
wxGridCellBoolEditor::GetValue() const
1374 bool bSet
= CBox()->GetValue();
1375 return bSet
? _T("1") : wxEmptyString
;
1378 #endif // wxUSE_CHECKBOX
1382 // ----------------------------------------------------------------------------
1383 // wxGridCellChoiceEditor
1384 // ----------------------------------------------------------------------------
1386 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1388 : m_choices(choices
),
1389 m_allowOthers(allowOthers
) { }
1391 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1392 const wxString choices
[],
1394 : m_allowOthers(allowOthers
)
1398 m_choices
.Alloc(count
);
1399 for ( size_t n
= 0; n
< count
; n
++ )
1401 m_choices
.Add(choices
[n
]);
1406 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1408 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1409 editor
->m_allowOthers
= m_allowOthers
;
1410 editor
->m_choices
= m_choices
;
1415 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1417 wxEvtHandler
* evtHandler
)
1419 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1420 wxDefaultPosition
, wxDefaultSize
,
1422 m_allowOthers
? 0 : wxCB_READONLY
);
1424 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1427 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1428 wxGridCellAttr
* attr
)
1430 // as we fill the entire client area, don't do anything here to minimize
1433 // TODO: It doesn't actually fill the client area since the height of a
1434 // combo always defaults to the standard... Until someone has time to
1435 // figure out the right rectangle to paint, just do it the normal way...
1436 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1439 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1441 wxASSERT_MSG(m_control
,
1442 wxT("The wxGridCellEditor must be Created first!"));
1444 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1447 Combo()->SetValue(m_startValue
);
1450 // find the right position, or default to the first if not found
1451 int pos
= Combo()->FindString(m_startValue
);
1454 Combo()->SetSelection(pos
);
1456 Combo()->SetInsertionPointEnd();
1457 Combo()->SetFocus();
1460 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1463 wxString value
= Combo()->GetValue();
1464 bool changed
= value
!= m_startValue
;
1467 grid
->GetTable()->SetValue(row
, col
, value
);
1469 m_startValue
= wxEmptyString
;
1471 Combo()->SetValue(m_startValue
);
1473 Combo()->SetSelection(0);
1478 void wxGridCellChoiceEditor::Reset()
1480 Combo()->SetValue(m_startValue
);
1481 Combo()->SetInsertionPointEnd();
1484 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1494 wxStringTokenizer
tk(params
, _T(','));
1495 while ( tk
.HasMoreTokens() )
1497 m_choices
.Add(tk
.GetNextToken());
1501 // return the value in the text control
1502 wxString
wxGridCellChoiceEditor::GetValue() const
1504 return Combo()->GetValue();
1507 #endif // wxUSE_COMBOBOX
1509 // ----------------------------------------------------------------------------
1510 // wxGridCellEditorEvtHandler
1511 // ----------------------------------------------------------------------------
1513 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1515 switch ( event
.GetKeyCode() )
1519 m_grid
->DisableCellEditControl();
1523 m_grid
->GetEventHandler()->ProcessEvent( event
);
1527 case WXK_NUMPAD_ENTER
:
1528 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1529 m_editor
->HandleReturn(event
);
1538 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1540 switch ( event
.GetKeyCode() )
1545 case WXK_NUMPAD_ENTER
:
1553 // ----------------------------------------------------------------------------
1554 // wxGridCellWorker is an (almost) empty common base class for
1555 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1556 // ----------------------------------------------------------------------------
1558 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1563 wxGridCellWorker::~wxGridCellWorker()
1567 // ============================================================================
1569 // ============================================================================
1571 // ----------------------------------------------------------------------------
1572 // wxGridCellRenderer
1573 // ----------------------------------------------------------------------------
1575 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1576 wxGridCellAttr
& attr
,
1579 int WXUNUSED(row
), int WXUNUSED(col
),
1582 dc
.SetBackgroundMode( wxSOLID
);
1584 // grey out fields if the grid is disabled
1585 if( grid
.IsEnabled() )
1589 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1593 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1598 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1601 dc
.SetPen( *wxTRANSPARENT_PEN
);
1602 dc
.DrawRectangle(rect
);
1605 // ----------------------------------------------------------------------------
1606 // wxGridCellStringRenderer
1607 // ----------------------------------------------------------------------------
1609 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1610 wxGridCellAttr
& attr
,
1614 dc
.SetBackgroundMode( wxTRANSPARENT
);
1616 // TODO some special colours for attr.IsReadOnly() case?
1618 // different coloured text when the grid is disabled
1619 if( grid
.IsEnabled() )
1623 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1624 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1628 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1629 dc
.SetTextForeground( attr
.GetTextColour() );
1634 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1635 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1638 dc
.SetFont( attr
.GetFont() );
1641 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1643 const wxString
& text
)
1645 wxCoord x
= 0, y
= 0, max_x
= 0;
1646 dc
.SetFont(attr
.GetFont());
1647 wxStringTokenizer
tk(text
, _T('\n'));
1648 while ( tk
.HasMoreTokens() )
1650 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1651 max_x
= wxMax(max_x
, x
);
1654 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1656 return wxSize(max_x
, y
);
1659 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1660 wxGridCellAttr
& attr
,
1664 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1667 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1668 wxGridCellAttr
& attr
,
1670 const wxRect
& rectCell
,
1674 wxRect rect
= rectCell
;
1677 // erase only this cells background, overflow cells should have been erased
1678 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1681 attr
.GetAlignment(&hAlign
, &vAlign
);
1683 int overflowCols
= 0;
1685 if (attr
.GetOverflow())
1687 int cols
= grid
.GetNumberCols();
1688 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1689 int cell_rows
, cell_cols
;
1690 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1691 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1693 int i
, c_cols
, c_rows
;
1694 for (i
= col
+cell_cols
; i
< cols
; i
++)
1696 bool is_empty
= true;
1697 for (int j
=row
; j
<row
+cell_rows
; j
++)
1699 // check w/ anchor cell for multicell block
1700 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1701 if (c_rows
> 0) c_rows
= 0;
1702 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1709 rect
.width
+= grid
.GetColSize(i
);
1715 if (rect
.width
>= best_width
) break;
1717 overflowCols
= i
- col
- cell_cols
+ 1;
1718 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1721 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1723 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1725 clip
.x
+= rectCell
.width
;
1726 // draw each overflow cell individually
1727 int col_end
= col
+cell_cols
+overflowCols
;
1728 if (col_end
>= grid
.GetNumberCols())
1729 col_end
= grid
.GetNumberCols() - 1;
1730 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1732 clip
.width
= grid
.GetColSize(i
) - 1;
1733 dc
.DestroyClippingRegion();
1734 dc
.SetClippingRegion(clip
);
1736 SetTextColoursAndFont(grid
, attr
, dc
,
1737 grid
.IsInSelection(row
,i
));
1739 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1740 rect
, hAlign
, vAlign
);
1741 clip
.x
+= grid
.GetColSize(i
) - 1;
1747 dc
.DestroyClippingRegion();
1751 // now we only have to draw the text
1752 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1754 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1755 rect
, hAlign
, vAlign
);
1758 // ----------------------------------------------------------------------------
1759 // wxGridCellNumberRenderer
1760 // ----------------------------------------------------------------------------
1762 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1764 wxGridTableBase
*table
= grid
.GetTable();
1766 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1768 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1772 text
= table
->GetValue(row
, col
);
1778 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1779 wxGridCellAttr
& attr
,
1781 const wxRect
& rectCell
,
1785 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1787 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1789 // draw the text right aligned by default
1791 attr
.GetAlignment(&hAlign
, &vAlign
);
1792 hAlign
= wxALIGN_RIGHT
;
1794 wxRect rect
= rectCell
;
1797 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1800 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1801 wxGridCellAttr
& attr
,
1805 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1808 // ----------------------------------------------------------------------------
1809 // wxGridCellFloatRenderer
1810 // ----------------------------------------------------------------------------
1812 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1815 SetPrecision(precision
);
1818 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1820 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1821 renderer
->m_width
= m_width
;
1822 renderer
->m_precision
= m_precision
;
1823 renderer
->m_format
= m_format
;
1828 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1830 wxGridTableBase
*table
= grid
.GetTable();
1835 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1837 val
= table
->GetValueAsDouble(row
, col
);
1842 text
= table
->GetValue(row
, col
);
1843 hasDouble
= text
.ToDouble(&val
);
1850 if ( m_width
== -1 )
1852 if ( m_precision
== -1 )
1854 // default width/precision
1855 m_format
= _T("%f");
1859 m_format
.Printf(_T("%%.%df"), m_precision
);
1862 else if ( m_precision
== -1 )
1864 // default precision
1865 m_format
.Printf(_T("%%%d.f"), m_width
);
1869 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1873 text
.Printf(m_format
, val
);
1876 //else: text already contains the string
1881 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1882 wxGridCellAttr
& attr
,
1884 const wxRect
& rectCell
,
1888 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1890 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1892 // draw the text right aligned by default
1894 attr
.GetAlignment(&hAlign
, &vAlign
);
1895 hAlign
= wxALIGN_RIGHT
;
1897 wxRect rect
= rectCell
;
1900 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1903 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1904 wxGridCellAttr
& attr
,
1908 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1911 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1915 // reset to defaults
1921 wxString tmp
= params
.BeforeFirst(_T(','));
1925 if ( tmp
.ToLong(&width
) )
1927 SetWidth((int)width
);
1931 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1935 tmp
= params
.AfterFirst(_T(','));
1939 if ( tmp
.ToLong(&precision
) )
1941 SetPrecision((int)precision
);
1945 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1953 // ----------------------------------------------------------------------------
1954 // wxGridCellBoolRenderer
1955 // ----------------------------------------------------------------------------
1957 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1959 // FIXME these checkbox size calculations are really ugly...
1961 // between checkmark and box
1962 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1964 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1965 wxGridCellAttr
& WXUNUSED(attr
),
1970 // compute it only once (no locks for MT safeness in GUI thread...)
1971 if ( !ms_sizeCheckMark
.x
)
1973 // get checkbox size
1974 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
1975 wxSize size
= checkbox
->GetBestSize();
1976 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1978 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1979 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1980 checkSize
-= size
.y
/ 2;
1985 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1988 return ms_sizeCheckMark
;
1991 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1992 wxGridCellAttr
& attr
,
1998 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2000 // draw a check mark in the centre (ignoring alignment - TODO)
2001 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2003 // don't draw outside the cell
2004 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2005 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2007 // and even leave (at least) 1 pixel margin
2008 size
.x
= size
.y
= minSize
- 2;
2011 // draw a border around checkmark
2013 attr
.GetAlignment(& hAlign
, &vAlign
);
2016 if (hAlign
== wxALIGN_CENTRE
)
2018 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2019 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2020 rectBorder
.width
= size
.x
;
2021 rectBorder
.height
= size
.y
;
2023 else if (hAlign
== wxALIGN_LEFT
)
2025 rectBorder
.x
= rect
.x
+ 2;
2026 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2027 rectBorder
.width
= size
.x
;
2028 rectBorder
.height
= size
.y
;
2030 else if (hAlign
== wxALIGN_RIGHT
)
2032 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2033 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2034 rectBorder
.width
= size
.x
;
2035 rectBorder
.height
= size
.y
;
2039 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2040 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2043 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2044 value
= !( !cellval
|| (cellval
== wxT("0")) );
2049 wxRect rectMark
= rectBorder
;
2051 // MSW DrawCheckMark() is weird (and should probably be changed...)
2052 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2056 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2059 dc
.SetTextForeground(attr
.GetTextColour());
2060 dc
.DrawCheckMark(rectMark
);
2063 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2064 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2065 dc
.DrawRectangle(rectBorder
);
2068 // ----------------------------------------------------------------------------
2070 // ----------------------------------------------------------------------------
2072 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2076 m_isReadOnly
= Unset
;
2081 m_attrkind
= wxGridCellAttr::Cell
;
2083 m_sizeRows
= m_sizeCols
= 1;
2084 m_overflow
= UnsetOverflow
;
2086 SetDefAttr(attrDefault
);
2089 wxGridCellAttr
*wxGridCellAttr::Clone() const
2091 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2093 if ( HasTextColour() )
2094 attr
->SetTextColour(GetTextColour());
2095 if ( HasBackgroundColour() )
2096 attr
->SetBackgroundColour(GetBackgroundColour());
2098 attr
->SetFont(GetFont());
2099 if ( HasAlignment() )
2100 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2102 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2106 attr
->SetRenderer(m_renderer
);
2107 m_renderer
->IncRef();
2111 attr
->SetEditor(m_editor
);
2116 attr
->SetReadOnly();
2118 attr
->SetKind( m_attrkind
);
2123 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2125 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2126 SetTextColour(mergefrom
->GetTextColour());
2127 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2128 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2129 if ( !HasFont() && mergefrom
->HasFont() )
2130 SetFont(mergefrom
->GetFont());
2131 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2133 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2134 SetAlignment(hAlign
, vAlign
);
2137 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2139 // Directly access member functions as GetRender/Editor don't just return
2140 // m_renderer/m_editor
2142 // Maybe add support for merge of Render and Editor?
2143 if (!HasRenderer() && mergefrom
->HasRenderer() )
2145 m_renderer
= mergefrom
->m_renderer
;
2146 m_renderer
->IncRef();
2148 if ( !HasEditor() && mergefrom
->HasEditor() )
2150 m_editor
= mergefrom
->m_editor
;
2153 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2154 SetReadOnly(mergefrom
->IsReadOnly());
2156 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2157 SetOverflow(mergefrom
->GetOverflow());
2159 SetDefAttr(mergefrom
->m_defGridAttr
);
2162 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2164 // The size of a cell is normally 1,1
2166 // If this cell is larger (2,2) then this is the top left cell
2167 // the other cells that will be covered (lower right cells) must be
2168 // set to negative or zero values such that
2169 // row + num_rows of the covered cell points to the larger cell (this cell)
2170 // same goes for the col + num_cols.
2172 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2174 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2175 !((num_rows
<=0)&&(num_cols
>0)) ||
2176 !((num_rows
==0)&&(num_cols
==0))),
2177 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2179 m_sizeRows
= num_rows
;
2180 m_sizeCols
= num_cols
;
2183 const wxColour
& wxGridCellAttr::GetTextColour() const
2185 if (HasTextColour())
2189 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2191 return m_defGridAttr
->GetTextColour();
2195 wxFAIL_MSG(wxT("Missing default cell attribute"));
2196 return wxNullColour
;
2201 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2203 if (HasBackgroundColour())
2205 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2206 return m_defGridAttr
->GetBackgroundColour();
2209 wxFAIL_MSG(wxT("Missing default cell attribute"));
2210 return wxNullColour
;
2215 const wxFont
& wxGridCellAttr::GetFont() const
2219 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2220 return m_defGridAttr
->GetFont();
2223 wxFAIL_MSG(wxT("Missing default cell attribute"));
2229 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2233 if ( hAlign
) *hAlign
= m_hAlign
;
2234 if ( vAlign
) *vAlign
= m_vAlign
;
2236 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2237 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2240 wxFAIL_MSG(wxT("Missing default cell attribute"));
2244 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2246 if ( num_rows
) *num_rows
= m_sizeRows
;
2247 if ( num_cols
) *num_cols
= m_sizeCols
;
2250 // GetRenderer and GetEditor use a slightly different decision path about
2251 // which attribute to use. If a non-default attr object has one then it is
2252 // used, otherwise the default editor or renderer is fetched from the grid and
2253 // used. It should be the default for the data type of the cell. If it is
2254 // NULL (because the table has a type that the grid does not have in its
2255 // registry,) then the grid's default editor or renderer is used.
2257 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2259 wxGridCellRenderer
*renderer
;
2261 if ( m_renderer
&& this != m_defGridAttr
)
2263 // use the cells renderer if it has one
2264 renderer
= m_renderer
;
2267 else // no non default cell renderer
2269 // get default renderer for the data type
2272 // GetDefaultRendererForCell() will do IncRef() for us
2273 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2282 if (m_defGridAttr
&& this != m_defGridAttr
)
2284 // if we still don't have one then use the grid default
2285 // (no need for IncRef() here neither)
2286 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2288 else // default grid attr
2290 // use m_renderer which we had decided not to use initially
2291 renderer
= m_renderer
;
2298 // we're supposed to always find something
2299 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2304 // same as above, except for s/renderer/editor/g
2305 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2307 wxGridCellEditor
*editor
;
2309 if ( m_editor
&& this != m_defGridAttr
)
2311 // use the cells editor if it has one
2315 else // no non default cell editor
2317 // get default editor for the data type
2320 // GetDefaultEditorForCell() will do IncRef() for us
2321 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2330 if ( m_defGridAttr
&& this != m_defGridAttr
)
2332 // if we still don't have one then use the grid default
2333 // (no need for IncRef() here neither)
2334 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2336 else // default grid attr
2338 // use m_editor which we had decided not to use initially
2346 // we're supposed to always find something
2347 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2352 // ----------------------------------------------------------------------------
2353 // wxGridCellAttrData
2354 // ----------------------------------------------------------------------------
2356 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2358 int n
= FindIndex(row
, col
);
2359 if ( n
== wxNOT_FOUND
)
2361 // add the attribute
2362 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2366 // free the old attribute
2367 m_attrs
[(size_t)n
].attr
->DecRef();
2371 // change the attribute
2372 m_attrs
[(size_t)n
].attr
= attr
;
2376 // remove this attribute
2377 m_attrs
.RemoveAt((size_t)n
);
2382 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2384 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2386 int n
= FindIndex(row
, col
);
2387 if ( n
!= wxNOT_FOUND
)
2389 attr
= m_attrs
[(size_t)n
].attr
;
2396 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2398 size_t count
= m_attrs
.GetCount();
2399 for ( size_t n
= 0; n
< count
; n
++ )
2401 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2402 wxCoord row
= coords
.GetRow();
2403 if ((size_t)row
>= pos
)
2407 // If rows inserted, include row counter where necessary
2408 coords
.SetRow(row
+ numRows
);
2410 else if (numRows
< 0)
2412 // If rows deleted ...
2413 if ((size_t)row
>= pos
- numRows
)
2415 // ...either decrement row counter (if row still exists)...
2416 coords
.SetRow(row
+ numRows
);
2420 // ...or remove the attribute
2421 m_attrs
.RemoveAt((size_t)n
);
2429 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2431 size_t count
= m_attrs
.GetCount();
2432 for ( size_t n
= 0; n
< count
; n
++ )
2434 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2435 wxCoord col
= coords
.GetCol();
2436 if ( (size_t)col
>= pos
)
2440 // If rows inserted, include row counter where necessary
2441 coords
.SetCol(col
+ numCols
);
2443 else if (numCols
< 0)
2445 // If rows deleted ...
2446 if ((size_t)col
>= pos
- numCols
)
2448 // ...either decrement row counter (if row still exists)...
2449 coords
.SetCol(col
+ numCols
);
2453 // ...or remove the attribute
2454 m_attrs
.RemoveAt((size_t)n
);
2462 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2464 size_t count
= m_attrs
.GetCount();
2465 for ( size_t n
= 0; n
< count
; n
++ )
2467 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2468 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2477 // ----------------------------------------------------------------------------
2478 // wxGridRowOrColAttrData
2479 // ----------------------------------------------------------------------------
2481 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2483 size_t count
= m_attrs
.Count();
2484 for ( size_t n
= 0; n
< count
; n
++ )
2486 m_attrs
[n
]->DecRef();
2490 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2492 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2494 int n
= m_rowsOrCols
.Index(rowOrCol
);
2495 if ( n
!= wxNOT_FOUND
)
2497 attr
= m_attrs
[(size_t)n
];
2504 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2506 int i
= m_rowsOrCols
.Index(rowOrCol
);
2507 if ( i
== wxNOT_FOUND
)
2509 // add the attribute
2510 m_rowsOrCols
.Add(rowOrCol
);
2515 size_t n
= (size_t)i
;
2518 // change the attribute
2519 m_attrs
[n
]->DecRef();
2524 // remove this attribute
2525 m_attrs
[n
]->DecRef();
2526 m_rowsOrCols
.RemoveAt(n
);
2527 m_attrs
.RemoveAt(n
);
2532 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2534 size_t count
= m_attrs
.GetCount();
2535 for ( size_t n
= 0; n
< count
; n
++ )
2537 int & rowOrCol
= m_rowsOrCols
[n
];
2538 if ( (size_t)rowOrCol
>= pos
)
2540 if ( numRowsOrCols
> 0 )
2542 // If rows inserted, include row counter where necessary
2543 rowOrCol
+= numRowsOrCols
;
2545 else if ( numRowsOrCols
< 0)
2547 // If rows deleted, either decrement row counter (if row still exists)
2548 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2549 rowOrCol
+= numRowsOrCols
;
2552 m_rowsOrCols
.RemoveAt((size_t)n
);
2553 m_attrs
.RemoveAt((size_t)n
);
2561 // ----------------------------------------------------------------------------
2562 // wxGridCellAttrProvider
2563 // ----------------------------------------------------------------------------
2565 wxGridCellAttrProvider::wxGridCellAttrProvider()
2567 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2570 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2575 void wxGridCellAttrProvider::InitData()
2577 m_data
= new wxGridCellAttrProviderData
;
2580 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2581 wxGridCellAttr::wxAttrKind kind
) const
2583 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2588 case (wxGridCellAttr::Any
):
2589 //Get cached merge attributes.
2590 // Currenlty not used as no cache implemented as not mutiable
2591 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2594 //Basicaly implement old version.
2595 //Also check merge cache, so we don't have to re-merge every time..
2596 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2597 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2598 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2600 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2601 // Two or move are non NULL
2602 attr
= new wxGridCellAttr
;
2603 attr
->SetKind(wxGridCellAttr::Merged
);
2607 attr
->MergeWith(attrcell
);
2611 attr
->MergeWith(attrcol
);
2615 attr
->MergeWith(attrrow
);
2618 //store merge attr if cache implemented
2620 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2624 // one or none is non null return it or null.
2625 if(attrrow
) attr
= attrrow
;
2626 if(attrcol
) attr
= attrcol
;
2627 if(attrcell
) attr
= attrcell
;
2631 case (wxGridCellAttr::Cell
):
2632 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2634 case (wxGridCellAttr::Col
):
2635 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2637 case (wxGridCellAttr::Row
):
2638 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2642 // (wxGridCellAttr::Default):
2643 // (wxGridCellAttr::Merged):
2650 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2656 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2659 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2664 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2667 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2672 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2675 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2679 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2681 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2685 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2689 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2691 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2695 // ----------------------------------------------------------------------------
2696 // wxGridTypeRegistry
2697 // ----------------------------------------------------------------------------
2699 wxGridTypeRegistry::~wxGridTypeRegistry()
2701 size_t count
= m_typeinfo
.Count();
2702 for ( size_t i
= 0; i
< count
; i
++ )
2703 delete m_typeinfo
[i
];
2707 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2708 wxGridCellRenderer
* renderer
,
2709 wxGridCellEditor
* editor
)
2711 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2713 // is it already registered?
2714 int loc
= FindRegisteredDataType(typeName
);
2715 if ( loc
!= wxNOT_FOUND
)
2717 delete m_typeinfo
[loc
];
2718 m_typeinfo
[loc
] = info
;
2722 m_typeinfo
.Add(info
);
2726 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2728 size_t count
= m_typeinfo
.GetCount();
2729 for ( size_t i
= 0; i
< count
; i
++ )
2731 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2740 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2742 int index
= FindRegisteredDataType(typeName
);
2743 if ( index
== wxNOT_FOUND
)
2745 // check whether this is one of the standard ones, in which case
2746 // register it "on the fly"
2748 if ( typeName
== wxGRID_VALUE_STRING
)
2750 RegisterDataType(wxGRID_VALUE_STRING
,
2751 new wxGridCellStringRenderer
,
2752 new wxGridCellTextEditor
);
2754 #endif // wxUSE_TEXTCTRL
2756 if ( typeName
== wxGRID_VALUE_BOOL
)
2758 RegisterDataType(wxGRID_VALUE_BOOL
,
2759 new wxGridCellBoolRenderer
,
2760 new wxGridCellBoolEditor
);
2762 #endif // wxUSE_CHECKBOX
2764 if ( typeName
== wxGRID_VALUE_NUMBER
)
2766 RegisterDataType(wxGRID_VALUE_NUMBER
,
2767 new wxGridCellNumberRenderer
,
2768 new wxGridCellNumberEditor
);
2770 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2772 RegisterDataType(wxGRID_VALUE_FLOAT
,
2773 new wxGridCellFloatRenderer
,
2774 new wxGridCellFloatEditor
);
2776 #endif // wxUSE_TEXTCTRL
2778 if ( typeName
== wxGRID_VALUE_CHOICE
)
2780 RegisterDataType(wxGRID_VALUE_CHOICE
,
2781 new wxGridCellStringRenderer
,
2782 new wxGridCellChoiceEditor
);
2784 #endif // wxUSE_COMBOBOX
2789 // we get here only if just added the entry for this type, so return
2791 index
= m_typeinfo
.GetCount() - 1;
2797 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2799 int index
= FindDataType(typeName
);
2800 if ( index
== wxNOT_FOUND
)
2802 // the first part of the typename is the "real" type, anything after ':'
2803 // are the parameters for the renderer
2804 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2805 if ( index
== wxNOT_FOUND
)
2810 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2811 wxGridCellRenderer
*rendererOld
= renderer
;
2812 renderer
= renderer
->Clone();
2813 rendererOld
->DecRef();
2815 wxGridCellEditor
*editor
= GetEditor(index
);
2816 wxGridCellEditor
*editorOld
= editor
;
2817 editor
= editor
->Clone();
2818 editorOld
->DecRef();
2820 // do it even if there are no parameters to reset them to defaults
2821 wxString params
= typeName
.AfterFirst(_T(':'));
2822 renderer
->SetParameters(params
);
2823 editor
->SetParameters(params
);
2825 // register the new typename
2826 RegisterDataType(typeName
, renderer
, editor
);
2828 // we just registered it, it's the last one
2829 index
= m_typeinfo
.GetCount() - 1;
2835 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2837 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2843 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2845 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2851 // ----------------------------------------------------------------------------
2853 // ----------------------------------------------------------------------------
2855 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2858 wxGridTableBase::wxGridTableBase()
2860 m_view
= (wxGrid
*) NULL
;
2861 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2864 wxGridTableBase::~wxGridTableBase()
2866 delete m_attrProvider
;
2869 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2871 delete m_attrProvider
;
2872 m_attrProvider
= attrProvider
;
2875 bool wxGridTableBase::CanHaveAttributes()
2877 if ( ! GetAttrProvider() )
2879 // use the default attr provider by default
2880 SetAttrProvider(new wxGridCellAttrProvider
);
2885 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2887 if ( m_attrProvider
)
2888 return m_attrProvider
->GetAttr(row
, col
, kind
);
2890 return (wxGridCellAttr
*)NULL
;
2893 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2895 if ( m_attrProvider
)
2897 attr
->SetKind(wxGridCellAttr::Cell
);
2898 m_attrProvider
->SetAttr(attr
, row
, col
);
2902 // as we take ownership of the pointer and don't store it, we must
2908 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2910 if ( m_attrProvider
)
2912 attr
->SetKind(wxGridCellAttr::Row
);
2913 m_attrProvider
->SetRowAttr(attr
, row
);
2917 // as we take ownership of the pointer and don't store it, we must
2923 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2925 if ( m_attrProvider
)
2927 attr
->SetKind(wxGridCellAttr::Col
);
2928 m_attrProvider
->SetColAttr(attr
, col
);
2932 // as we take ownership of the pointer and don't store it, we must
2938 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2939 size_t WXUNUSED(numRows
) )
2941 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2946 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2948 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2953 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2954 size_t WXUNUSED(numRows
) )
2956 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2961 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2962 size_t WXUNUSED(numCols
) )
2964 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2969 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2971 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2976 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2977 size_t WXUNUSED(numCols
) )
2979 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2985 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2988 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2989 // how much it makes sense to us geeks.
2993 wxString
wxGridTableBase::GetColLabelValue( int col
)
2995 // default col labels are:
2996 // cols 0 to 25 : A-Z
2997 // cols 26 to 675 : AA-ZZ
3002 for ( n
= 1; ; n
++ )
3004 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3006 if ( col
< 0 ) break;
3009 // reverse the string...
3011 for ( i
= 0; i
< n
; i
++ )
3020 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3022 return wxGRID_VALUE_STRING
;
3025 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3026 const wxString
& typeName
)
3028 return typeName
== wxGRID_VALUE_STRING
;
3031 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3033 return CanGetValueAs(row
, col
, typeName
);
3036 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3041 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3046 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3051 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3052 long WXUNUSED(value
) )
3056 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3057 double WXUNUSED(value
) )
3061 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3062 bool WXUNUSED(value
) )
3067 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3068 const wxString
& WXUNUSED(typeName
) )
3073 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3074 const wxString
& WXUNUSED(typeName
),
3075 void* WXUNUSED(value
) )
3079 //////////////////////////////////////////////////////////////////////
3081 // Message class for the grid table to send requests and notifications
3085 wxGridTableMessage::wxGridTableMessage()
3087 m_table
= (wxGridTableBase
*) NULL
;
3093 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3094 int commandInt1
, int commandInt2
)
3098 m_comInt1
= commandInt1
;
3099 m_comInt2
= commandInt2
;
3104 //////////////////////////////////////////////////////////////////////
3106 // A basic grid table for string data. An object of this class will
3107 // created by wxGrid if you don't specify an alternative table class.
3110 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3112 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3114 wxGridStringTable::wxGridStringTable()
3119 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3122 m_data
.Alloc( numRows
);
3125 sa
.Alloc( numCols
);
3126 sa
.Add( wxEmptyString
, numCols
);
3128 m_data
.Add( sa
, numRows
);
3131 wxGridStringTable::~wxGridStringTable()
3135 int wxGridStringTable::GetNumberRows()
3137 return m_data
.GetCount();
3140 int wxGridStringTable::GetNumberCols()
3142 if ( m_data
.GetCount() > 0 )
3143 return m_data
[0].GetCount();
3148 wxString
wxGridStringTable::GetValue( int row
, int col
)
3150 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3152 _T("invalid row or column index in wxGridStringTable") );
3154 return m_data
[row
][col
];
3157 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3159 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3160 _T("invalid row or column index in wxGridStringTable") );
3162 m_data
[row
][col
] = value
;
3165 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3167 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3169 _T("invalid row or column index in wxGridStringTable") );
3171 return (m_data
[row
][col
] == wxEmptyString
);
3174 void wxGridStringTable::Clear()
3177 int numRows
, numCols
;
3179 numRows
= m_data
.GetCount();
3182 numCols
= m_data
[0].GetCount();
3184 for ( row
= 0; row
< numRows
; row
++ )
3186 for ( col
= 0; col
< numCols
; col
++ )
3188 m_data
[row
][col
] = wxEmptyString
;
3195 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3197 size_t curNumRows
= m_data
.GetCount();
3198 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3199 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3201 if ( pos
>= curNumRows
)
3203 return AppendRows( numRows
);
3207 sa
.Alloc( curNumCols
);
3208 sa
.Add( wxEmptyString
, curNumCols
);
3209 m_data
.Insert( sa
, pos
, numRows
);
3212 wxGridTableMessage
msg( this,
3213 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3217 GetView()->ProcessTableMessage( msg
);
3223 bool wxGridStringTable::AppendRows( size_t numRows
)
3225 size_t curNumRows
= m_data
.GetCount();
3226 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3227 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3230 if ( curNumCols
> 0 )
3232 sa
.Alloc( curNumCols
);
3233 sa
.Add( wxEmptyString
, curNumCols
);
3236 m_data
.Add( sa
, numRows
);
3240 wxGridTableMessage
msg( this,
3241 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3244 GetView()->ProcessTableMessage( msg
);
3250 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3252 size_t curNumRows
= m_data
.GetCount();
3254 if ( pos
>= curNumRows
)
3256 wxFAIL_MSG( wxString::Format
3258 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3260 (unsigned long)numRows
,
3261 (unsigned long)curNumRows
3267 if ( numRows
> curNumRows
- pos
)
3269 numRows
= curNumRows
- pos
;
3272 if ( numRows
>= curNumRows
)
3278 m_data
.RemoveAt( pos
, numRows
);
3282 wxGridTableMessage
msg( this,
3283 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3287 GetView()->ProcessTableMessage( msg
);
3293 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3297 size_t curNumRows
= m_data
.GetCount();
3298 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3299 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3301 if ( pos
>= curNumCols
)
3303 return AppendCols( numCols
);
3306 for ( row
= 0; row
< curNumRows
; row
++ )
3308 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3310 m_data
[row
].Insert( wxEmptyString
, col
);
3315 wxGridTableMessage
msg( this,
3316 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3320 GetView()->ProcessTableMessage( msg
);
3326 bool wxGridStringTable::AppendCols( size_t numCols
)
3330 size_t curNumRows
= m_data
.GetCount();
3334 // TODO: something better than this ?
3336 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3341 for ( row
= 0; row
< curNumRows
; row
++ )
3343 m_data
[row
].Add( wxEmptyString
, numCols
);
3348 wxGridTableMessage
msg( this,
3349 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3352 GetView()->ProcessTableMessage( msg
);
3358 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3362 size_t curNumRows
= m_data
.GetCount();
3363 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3364 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3366 if ( pos
>= curNumCols
)
3368 wxFAIL_MSG( wxString::Format
3370 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3372 (unsigned long)numCols
,
3373 (unsigned long)curNumCols
3378 if ( numCols
> curNumCols
- pos
)
3380 numCols
= curNumCols
- pos
;
3383 for ( row
= 0; row
< curNumRows
; row
++ )
3385 if ( numCols
>= curNumCols
)
3387 m_data
[row
].Clear();
3391 m_data
[row
].RemoveAt( pos
, numCols
);
3396 wxGridTableMessage
msg( this,
3397 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3401 GetView()->ProcessTableMessage( msg
);
3407 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3409 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3411 // using default label
3413 return wxGridTableBase::GetRowLabelValue( row
);
3417 return m_rowLabels
[ row
];
3421 wxString
wxGridStringTable::GetColLabelValue( int col
)
3423 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3425 // using default label
3427 return wxGridTableBase::GetColLabelValue( col
);
3431 return m_colLabels
[ col
];
3435 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3437 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3439 int n
= m_rowLabels
.GetCount();
3441 for ( i
= n
; i
<= row
; i
++ )
3443 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3447 m_rowLabels
[row
] = value
;
3450 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3452 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3454 int n
= m_colLabels
.GetCount();
3456 for ( i
= n
; i
<= col
; i
++ )
3458 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3462 m_colLabels
[col
] = value
;
3467 //////////////////////////////////////////////////////////////////////
3468 //////////////////////////////////////////////////////////////////////
3470 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3472 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3473 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3474 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3475 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3476 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3477 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3480 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3482 const wxPoint
&pos
, const wxSize
&size
)
3483 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3488 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3492 // NO - don't do this because it will set both the x and y origin
3493 // coords to match the parent scrolled window and we just want to
3494 // set the y coord - MB
3496 // m_owner->PrepareDC( dc );
3499 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3500 dc
.SetDeviceOrigin( 0, -y
);
3502 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3503 m_owner
->DrawRowLabels( dc
, rows
);
3507 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3509 m_owner
->ProcessRowLabelMouseEvent( event
);
3513 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3515 m_owner
->GetEventHandler()->ProcessEvent(event
);
3519 // This seems to be required for wxMotif otherwise the mouse
3520 // cursor must be in the cell edit control to get key events
3522 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3524 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3527 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3529 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3534 //////////////////////////////////////////////////////////////////////
3536 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3538 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3539 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3540 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3541 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3542 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3543 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3546 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3548 const wxPoint
&pos
, const wxSize
&size
)
3549 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3554 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3558 // NO - don't do this because it will set both the x and y origin
3559 // coords to match the parent scrolled window and we just want to
3560 // set the x coord - MB
3562 // m_owner->PrepareDC( dc );
3565 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3566 dc
.SetDeviceOrigin( -x
, 0 );
3568 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3569 m_owner
->DrawColLabels( dc
, cols
);
3573 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3575 m_owner
->ProcessColLabelMouseEvent( event
);
3578 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3580 m_owner
->GetEventHandler()->ProcessEvent(event
);
3584 // This seems to be required for wxMotif otherwise the mouse
3585 // cursor must be in the cell edit control to get key events
3587 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3589 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3592 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3594 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3599 //////////////////////////////////////////////////////////////////////
3601 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3603 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3604 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3605 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3606 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3607 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3608 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3611 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3613 const wxPoint
&pos
, const wxSize
&size
)
3614 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3619 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3623 int client_height
= 0;
3624 int client_width
= 0;
3625 GetClientSize( &client_width
, &client_height
);
3627 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3628 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3629 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3630 dc
.DrawLine( 0, 0, client_width
, 0 );
3631 dc
.DrawLine( 0, 0, 0, client_height
);
3633 dc
.SetPen( *wxWHITE_PEN
);
3634 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3635 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3639 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3641 m_owner
->ProcessCornerLabelMouseEvent( event
);
3645 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3647 m_owner
->GetEventHandler()->ProcessEvent(event
);
3650 // This seems to be required for wxMotif otherwise the mouse
3651 // cursor must be in the cell edit control to get key events
3653 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3655 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3658 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3660 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3665 //////////////////////////////////////////////////////////////////////
3667 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3669 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3670 EVT_PAINT( wxGridWindow::OnPaint
)
3671 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3672 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3673 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3674 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3675 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3676 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3677 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3680 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3681 wxGridRowLabelWindow
*rowLblWin
,
3682 wxGridColLabelWindow
*colLblWin
,
3685 const wxSize
&size
)
3686 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3687 wxT("grid window") )
3691 m_rowLabelWin
= rowLblWin
;
3692 m_colLabelWin
= colLblWin
;
3696 wxGridWindow::~wxGridWindow()
3701 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3703 wxPaintDC
dc( this );
3704 m_owner
->PrepareDC( dc
);
3705 wxRegion reg
= GetUpdateRegion();
3706 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3707 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3708 #if WXGRID_DRAW_LINES
3709 m_owner
->DrawAllGridLines( dc
, reg
);
3711 m_owner
->DrawGridSpace( dc
);
3712 m_owner
->DrawHighlight( dc
, DirtyCells
);
3716 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3718 wxWindow::ScrollWindow( dx
, dy
, rect
);
3719 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3720 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3724 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3726 m_owner
->ProcessGridCellMouseEvent( event
);
3729 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3731 m_owner
->GetEventHandler()->ProcessEvent(event
);
3734 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3735 // cursor must be in the cell edit control to get key events
3737 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3739 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3742 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3744 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3747 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3751 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3753 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3757 //////////////////////////////////////////////////////////////////////
3759 // Internal Helper function for computing row or column from some
3760 // (unscrolled) coordinate value, using either
3761 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3762 // of m_rowBottoms/m_ColRights to speed up the search!
3764 // Internal helper macros for simpler use of that function
3766 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3767 const wxArrayInt
& BorderArray
, int nMax
,
3770 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3771 m_minAcceptableColWidth, \
3772 m_colRights, m_numCols, true)
3773 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3774 m_minAcceptableRowHeight, \
3775 m_rowBottoms, m_numRows, true)
3776 /////////////////////////////////////////////////////////////////////
3778 #if wxUSE_EXTENDED_RTTI
3779 WX_DEFINE_FLAGS( wxGridStyle
)
3781 wxBEGIN_FLAGS( wxGridStyle
)
3782 // new style border flags, we put them first to
3783 // use them for streaming out
3784 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3785 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3786 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3787 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3788 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3789 wxFLAGS_MEMBER(wxBORDER_NONE
)
3791 // old style border flags
3792 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3793 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3794 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3795 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3796 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3797 wxFLAGS_MEMBER(wxBORDER
)
3799 // standard window styles
3800 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3801 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3802 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3803 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3804 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3805 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3806 wxFLAGS_MEMBER(wxVSCROLL
)
3807 wxFLAGS_MEMBER(wxHSCROLL
)
3809 wxEND_FLAGS( wxGridStyle
)
3811 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3813 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3814 wxHIDE_PROPERTY( Children
)
3815 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3816 wxEND_PROPERTIES_TABLE()
3818 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3819 wxEND_HANDLERS_TABLE()
3821 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3824 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
3827 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3830 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3831 EVT_PAINT( wxGrid::OnPaint
)
3832 EVT_SIZE( wxGrid::OnSize
)
3833 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3834 EVT_KEY_UP( wxGrid::OnKeyUp
)
3835 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3840 // in order to make sure that a size event is not
3841 // trigerred in a unfinished state
3842 m_cornerLabelWin
= NULL
;
3843 m_rowLabelWin
= NULL
;
3844 m_colLabelWin
= NULL
;
3848 wxGrid::wxGrid( wxWindow
*parent
,
3853 const wxString
& name
)
3854 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3855 m_colMinWidths(GRID_HASH_SIZE
),
3856 m_rowMinHeights(GRID_HASH_SIZE
)
3861 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3862 const wxPoint
& pos
, const wxSize
& size
,
3863 long style
, const wxString
& name
)
3865 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3866 style
| wxWANTS_CHARS
, name
))
3869 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3870 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3881 // Must do this or ~wxScrollHelper will pop the wrong event handler
3882 SetTargetWindow(this);
3884 wxSafeDecRef(m_defaultCellAttr
);
3886 #ifdef DEBUG_ATTR_CACHE
3887 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3888 wxPrintf(_T("wxGrid attribute cache statistics: "
3889 "total: %u, hits: %u (%u%%)\n"),
3890 total
, gs_nAttrCacheHits
,
3891 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3897 delete m_typeRegistry
;
3903 // ----- internal init and update functions
3906 // NOTE: If using the default visual attributes works everywhere then this can
3907 // be removed as well as the #else cases below.
3908 #define _USE_VISATTR 0
3911 #include "wx/listbox.h"
3914 void wxGrid::Create()
3916 m_created
= false; // set to true by CreateGrid
3918 m_table
= (wxGridTableBase
*) NULL
;
3921 m_cellEditCtrlEnabled
= false;
3923 m_defaultCellAttr
= new wxGridCellAttr();
3925 // Set default cell attributes
3926 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3927 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3928 m_defaultCellAttr
->SetFont(GetFont());
3929 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3930 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3931 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3934 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3935 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3937 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3938 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3941 m_defaultCellAttr
->SetTextColour(
3942 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3943 m_defaultCellAttr
->SetBackgroundColour(
3944 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3949 m_currentCellCoords
= wxGridNoCellCoords
;
3951 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3952 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3954 // create the type registry
3955 m_typeRegistry
= new wxGridTypeRegistry
;
3958 // subwindow components that make up the wxGrid
3959 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3964 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3969 m_colLabelWin
= new wxGridColLabelWindow( this,
3974 m_gridWin
= new wxGridWindow( this,
3981 SetTargetWindow( m_gridWin
);
3984 wxColour gfg
= gva
.colFg
;
3985 wxColour gbg
= gva
.colBg
;
3986 wxColour lfg
= lva
.colFg
;
3987 wxColour lbg
= lva
.colBg
;
3989 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3990 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
3991 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3992 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
3994 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
3995 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
3996 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
3997 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
3998 m_colLabelWin
->SetOwnForegroundColour(lfg
);
3999 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4001 m_gridWin
->SetOwnForegroundColour(gfg
);
4002 m_gridWin
->SetOwnBackgroundColour(gbg
);
4008 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4009 wxGrid::wxGridSelectionModes selmode
)
4011 wxCHECK_MSG( !m_created
,
4013 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4015 m_numRows
= numRows
;
4016 m_numCols
= numCols
;
4018 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4019 m_table
->SetView( this );
4021 m_selection
= new wxGridSelection( this, selmode
);
4030 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4032 wxCHECK_RET( m_created
,
4033 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4035 m_selection
->SetSelectionMode( selmode
);
4038 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4040 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4041 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4043 return m_selection
->GetSelectionMode();
4046 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4047 wxGrid::wxGridSelectionModes selmode
)
4051 // stop all processing
4056 wxGridTableBase
*t
=m_table
;
4069 m_numRows
= table
->GetNumberRows();
4070 m_numCols
= table
->GetNumberCols();
4073 m_table
->SetView( this );
4076 m_selection
= new wxGridSelection( this, selmode
);
4089 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4090 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4092 if ( m_rowLabelWin
)
4094 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4098 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4101 m_labelTextColour
= wxColour( _T("BLACK") );
4104 m_attrCache
.row
= -1;
4105 m_attrCache
.col
= -1;
4106 m_attrCache
.attr
= NULL
;
4108 // TODO: something better than this ?
4110 m_labelFont
= this->GetFont();
4111 m_labelFont
.SetWeight( wxBOLD
);
4113 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4114 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4116 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4117 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4118 m_colLabelTextOrientation
= wxHORIZONTAL
;
4120 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4121 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4123 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4124 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4126 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4127 m_defaultRowHeight
+= 8;
4129 m_defaultRowHeight
+= 4;
4132 m_gridLineColour
= wxColour( 192,192,192 );
4133 m_gridLinesEnabled
= true;
4134 m_cellHighlightColour
= *wxBLACK
;
4135 m_cellHighlightPenWidth
= 2;
4136 m_cellHighlightROPenWidth
= 1;
4138 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4139 m_winCapture
= (wxWindow
*)NULL
;
4140 m_canDragRowSize
= true;
4141 m_canDragColSize
= true;
4142 m_canDragGridSize
= true;
4143 m_canDragCell
= false;
4145 m_dragRowOrCol
= -1;
4146 m_isDragging
= false;
4147 m_startDragPos
= wxDefaultPosition
;
4149 m_waitForSlowClick
= false;
4151 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4152 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4154 m_currentCellCoords
= wxGridNoCellCoords
;
4156 m_selectingTopLeft
= wxGridNoCellCoords
;
4157 m_selectingBottomRight
= wxGridNoCellCoords
;
4158 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4159 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4161 m_editable
= true; // default for whole grid
4163 m_inOnKeyDown
= false;
4170 // ----------------------------------------------------------------------------
4171 // the idea is to call these functions only when necessary because they create
4172 // quite big arrays which eat memory mostly unnecessary - in particular, if
4173 // default widths/heights are used for all rows/columns, we may not use these
4176 // with some extra code, it should be possible to only store the
4177 // widths/heights different from default ones but this will be done later...
4178 // ----------------------------------------------------------------------------
4180 void wxGrid::InitRowHeights()
4182 m_rowHeights
.Empty();
4183 m_rowBottoms
.Empty();
4185 m_rowHeights
.Alloc( m_numRows
);
4186 m_rowBottoms
.Alloc( m_numRows
);
4190 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4192 for ( int i
= 0; i
< m_numRows
; i
++ )
4194 rowBottom
+= m_defaultRowHeight
;
4195 m_rowBottoms
.Add( rowBottom
);
4199 void wxGrid::InitColWidths()
4201 m_colWidths
.Empty();
4202 m_colRights
.Empty();
4204 m_colWidths
.Alloc( m_numCols
);
4205 m_colRights
.Alloc( m_numCols
);
4208 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4210 for ( int i
= 0; i
< m_numCols
; i
++ )
4212 colRight
+= m_defaultColWidth
;
4213 m_colRights
.Add( colRight
);
4217 int wxGrid::GetColWidth(int col
) const
4219 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4222 int wxGrid::GetColLeft(int col
) const
4224 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4225 : m_colRights
[col
] - m_colWidths
[col
];
4228 int wxGrid::GetColRight(int col
) const
4230 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4234 int wxGrid::GetRowHeight(int row
) const
4236 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4239 int wxGrid::GetRowTop(int row
) const
4241 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4242 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4245 int wxGrid::GetRowBottom(int row
) const
4247 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4248 : m_rowBottoms
[row
];
4251 void wxGrid::CalcDimensions()
4254 GetClientSize( &cw
, &ch
);
4256 if ( m_rowLabelWin
->IsShown() )
4257 cw
-= m_rowLabelWidth
;
4258 if ( m_colLabelWin
->IsShown() )
4259 ch
-= m_colLabelHeight
;
4262 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4263 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4265 // take into account editor if shown
4266 if( IsCellEditControlShown() )
4269 int r
= m_currentCellCoords
.GetRow();
4270 int c
= m_currentCellCoords
.GetCol();
4271 int x
= GetColLeft(c
);
4272 int y
= GetRowTop(r
);
4274 // how big is the editor
4275 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4276 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4277 editor
->GetControl()->GetSize(&w2
, &h2
);
4280 if( w2
> w
) w
= w2
;
4281 if( h2
> h
) h
= h2
;
4286 // preserve (more or less) the previous position
4288 GetViewStart( &x
, &y
);
4290 // ensure the position is valid for the new scroll ranges
4292 x
= wxMax( w
- 1, 0 );
4294 y
= wxMax( h
- 1, 0 );
4296 // do set scrollbar parameters
4297 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4298 GetScrollX(w
), GetScrollY(h
), x
, y
,
4299 GetBatchCount() != 0);
4301 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4302 // still must reposition the children
4307 void wxGrid::CalcWindowSizes()
4309 // escape if the window is has not been fully created yet
4311 if ( m_cornerLabelWin
== NULL
)
4315 GetClientSize( &cw
, &ch
);
4317 if ( m_cornerLabelWin
->IsShown() )
4318 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4320 if ( m_colLabelWin
->IsShown() )
4321 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4323 if ( m_rowLabelWin
->IsShown() )
4324 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4326 if ( m_gridWin
->IsShown() )
4327 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4331 // this is called when the grid table sends a message to say that it
4332 // has been redimensioned
4334 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4337 bool result
= false;
4339 // Clear the attribute cache as the attribute might refer to a different
4340 // cell than stored in the cache after adding/removing rows/columns.
4342 // By the same reasoning, the editor should be dismissed if columns are
4343 // added or removed. And for consistency, it should IMHO always be
4344 // removed, not only if the cell "underneath" it actually changes.
4345 // For now, I intentionally do not save the editor's content as the
4346 // cell it might want to save that stuff to might no longer exist.
4347 HideCellEditControl();
4349 // if we were using the default widths/heights so far, we must change them
4351 if ( m_colWidths
.IsEmpty() )
4356 if ( m_rowHeights
.IsEmpty() )
4362 switch ( msg
.GetId() )
4364 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4366 size_t pos
= msg
.GetCommandInt();
4367 int numRows
= msg
.GetCommandInt2();
4369 m_numRows
+= numRows
;
4371 if ( !m_rowHeights
.IsEmpty() )
4373 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4374 m_rowBottoms
.Insert( 0, pos
, numRows
);
4377 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4379 for ( i
= pos
; i
< m_numRows
; i
++ )
4381 bottom
+= m_rowHeights
[i
];
4382 m_rowBottoms
[i
] = bottom
;
4385 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4387 // if we have just inserted cols into an empty grid the current
4388 // cell will be undefined...
4390 SetCurrentCell( 0, 0 );
4394 m_selection
->UpdateRows( pos
, numRows
);
4395 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4397 attrProvider
->UpdateAttrRows( pos
, numRows
);
4399 if ( !GetBatchCount() )
4402 m_rowLabelWin
->Refresh();
4408 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4410 int numRows
= msg
.GetCommandInt();
4411 int oldNumRows
= m_numRows
;
4412 m_numRows
+= numRows
;
4414 if ( !m_rowHeights
.IsEmpty() )
4416 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4417 m_rowBottoms
.Add( 0, numRows
);
4420 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4422 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4424 bottom
+= m_rowHeights
[i
];
4425 m_rowBottoms
[i
] = bottom
;
4428 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4430 // if we have just inserted cols into an empty grid the current
4431 // cell will be undefined...
4433 SetCurrentCell( 0, 0 );
4435 if ( !GetBatchCount() )
4438 m_rowLabelWin
->Refresh();
4444 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4446 size_t pos
= msg
.GetCommandInt();
4447 int numRows
= msg
.GetCommandInt2();
4448 m_numRows
-= numRows
;
4450 if ( !m_rowHeights
.IsEmpty() )
4452 m_rowHeights
.RemoveAt( pos
, numRows
);
4453 m_rowBottoms
.RemoveAt( pos
, numRows
);
4456 for ( i
= 0; i
< m_numRows
; i
++ )
4458 h
+= m_rowHeights
[i
];
4459 m_rowBottoms
[i
] = h
;
4464 m_currentCellCoords
= wxGridNoCellCoords
;
4468 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4469 m_currentCellCoords
.Set( 0, 0 );
4473 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4474 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4476 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4477 // ifdef'd out following patch from Paul Gammans
4479 // No need to touch column attributes, unless we
4480 // removed _all_ rows, in this case, we remove
4481 // all column attributes.
4482 // I hate to do this here, but the
4483 // needed data is not available inside UpdateAttrRows.
4484 if ( !GetNumberRows() )
4485 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4488 if ( !GetBatchCount() )
4491 m_rowLabelWin
->Refresh();
4497 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4499 size_t pos
= msg
.GetCommandInt();
4500 int numCols
= msg
.GetCommandInt2();
4501 m_numCols
+= numCols
;
4503 if ( !m_colWidths
.IsEmpty() )
4505 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4506 m_colRights
.Insert( 0, pos
, numCols
);
4509 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4511 for ( i
= pos
; i
< m_numCols
; i
++ )
4513 right
+= m_colWidths
[i
];
4514 m_colRights
[i
] = right
;
4517 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4519 // if we have just inserted cols into an empty grid the current
4520 // cell will be undefined...
4522 SetCurrentCell( 0, 0 );
4526 m_selection
->UpdateCols( pos
, numCols
);
4527 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4529 attrProvider
->UpdateAttrCols( pos
, numCols
);
4530 if ( !GetBatchCount() )
4533 m_colLabelWin
->Refresh();
4540 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4542 int numCols
= msg
.GetCommandInt();
4543 int oldNumCols
= m_numCols
;
4544 m_numCols
+= numCols
;
4545 if ( !m_colWidths
.IsEmpty() )
4547 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4548 m_colRights
.Add( 0, numCols
);
4551 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4553 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4555 right
+= m_colWidths
[i
];
4556 m_colRights
[i
] = right
;
4559 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4561 // if we have just inserted cols into an empty grid the current
4562 // cell will be undefined...
4564 SetCurrentCell( 0, 0 );
4566 if ( !GetBatchCount() )
4569 m_colLabelWin
->Refresh();
4575 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4577 size_t pos
= msg
.GetCommandInt();
4578 int numCols
= msg
.GetCommandInt2();
4579 m_numCols
-= numCols
;
4581 if ( !m_colWidths
.IsEmpty() )
4583 m_colWidths
.RemoveAt( pos
, numCols
);
4584 m_colRights
.RemoveAt( pos
, numCols
);
4587 for ( i
= 0; i
< m_numCols
; i
++ )
4589 w
+= m_colWidths
[i
];
4595 m_currentCellCoords
= wxGridNoCellCoords
;
4599 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4600 m_currentCellCoords
.Set( 0, 0 );
4604 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4605 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4607 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4608 // ifdef'd out following patch from Paul Gammans
4610 // No need to touch row attributes, unless we
4611 // removed _all_ columns, in this case, we remove
4612 // all row attributes.
4613 // I hate to do this here, but the
4614 // needed data is not available inside UpdateAttrCols.
4615 if ( !GetNumberCols() )
4616 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4619 if ( !GetBatchCount() )
4622 m_colLabelWin
->Refresh();
4629 if (result
&& !GetBatchCount() )
4630 m_gridWin
->Refresh();
4635 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4637 wxRegionIterator
iter( reg
);
4640 wxArrayInt rowlabels
;
4647 // TODO: remove this when we can...
4648 // There is a bug in wxMotif that gives garbage update
4649 // rectangles if you jump-scroll a long way by clicking the
4650 // scrollbar with middle button. This is a work-around
4652 #if defined(__WXMOTIF__)
4654 m_gridWin
->GetClientSize( &cw
, &ch
);
4655 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4656 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4659 // logical bounds of update region
4662 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4663 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4665 // find the row labels within these bounds
4668 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4670 if ( GetRowBottom(row
) < top
)
4673 if ( GetRowTop(row
) > bottom
)
4676 rowlabels
.Add( row
);
4686 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4688 wxRegionIterator
iter( reg
);
4691 wxArrayInt colLabels
;
4698 // TODO: remove this when we can...
4699 // There is a bug in wxMotif that gives garbage update
4700 // rectangles if you jump-scroll a long way by clicking the
4701 // scrollbar with middle button. This is a work-around
4703 #if defined(__WXMOTIF__)
4705 m_gridWin
->GetClientSize( &cw
, &ch
);
4706 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4707 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4710 // logical bounds of update region
4713 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4714 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4716 // find the cells within these bounds
4719 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4721 if ( GetColRight(col
) < left
)
4724 if ( GetColLeft(col
) > right
)
4727 colLabels
.Add( col
);
4736 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4738 wxRegionIterator
iter( reg
);
4741 wxGridCellCoordsArray cellsExposed
;
4743 int left
, top
, right
, bottom
;
4748 // TODO: remove this when we can...
4749 // There is a bug in wxMotif that gives garbage update
4750 // rectangles if you jump-scroll a long way by clicking the
4751 // scrollbar with middle button. This is a work-around
4753 #if defined(__WXMOTIF__)
4755 m_gridWin
->GetClientSize( &cw
, &ch
);
4756 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4757 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4758 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4759 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4762 // logical bounds of update region
4764 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4765 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4767 // find the cells within these bounds
4770 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4772 if ( GetRowBottom(row
) <= top
)
4775 if ( GetRowTop(row
) > bottom
)
4778 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4780 if ( GetColRight(col
) <= left
)
4783 if ( GetColLeft(col
) > right
)
4786 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4793 return cellsExposed
;
4797 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4800 wxPoint
pos( event
.GetPosition() );
4801 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4803 if ( event
.Dragging() )
4807 m_isDragging
= true;
4808 m_rowLabelWin
->CaptureMouse();
4811 if ( event
.LeftIsDown() )
4813 switch( m_cursorMode
)
4815 case WXGRID_CURSOR_RESIZE_ROW
:
4817 int cw
, ch
, left
, dummy
;
4818 m_gridWin
->GetClientSize( &cw
, &ch
);
4819 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4821 wxClientDC
dc( m_gridWin
);
4824 GetRowTop(m_dragRowOrCol
) +
4825 GetRowMinimalHeight(m_dragRowOrCol
) );
4826 dc
.SetLogicalFunction(wxINVERT
);
4827 if ( m_dragLastPos
>= 0 )
4829 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4831 dc
.DrawLine( left
, y
, left
+cw
, y
);
4836 case WXGRID_CURSOR_SELECT_ROW
:
4837 if ( (row
= YToRow( y
)) >= 0 )
4841 m_selection
->SelectRow( row
,
4842 event
.ControlDown(),
4849 // default label to suppress warnings about "enumeration value
4850 // 'xxx' not handled in switch
4858 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4863 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4864 m_isDragging
= false;
4867 // ------------ Entering or leaving the window
4869 if ( event
.Entering() || event
.Leaving() )
4871 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4875 // ------------ Left button pressed
4877 else if ( event
.LeftDown() )
4879 // don't send a label click event for a hit on the
4880 // edge of the row label - this is probably the user
4881 // wanting to resize the row
4883 if ( YToEdgeOfRow(y
) < 0 )
4887 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4889 if ( !event
.ShiftDown() && !event
.ControlDown() )
4893 if ( event
.ShiftDown() )
4895 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4898 GetNumberCols() - 1,
4899 event
.ControlDown(),
4906 m_selection
->SelectRow( row
,
4907 event
.ControlDown(),
4914 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4919 // starting to drag-resize a row
4921 if ( CanDragRowSize() )
4922 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4927 // ------------ Left double click
4929 else if (event
.LeftDClick() )
4931 int row
= YToEdgeOfRow(y
);
4936 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4938 // no default action at the moment
4943 // adjust row height depending on label text
4944 AutoSizeRowLabelSize( row
);
4946 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4952 // ------------ Left button released
4954 else if ( event
.LeftUp() )
4956 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4958 DoEndDragResizeRow();
4960 // Note: we are ending the event *after* doing
4961 // default processing in this case
4963 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4966 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4971 // ------------ Right button down
4973 else if ( event
.RightDown() )
4977 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4979 // no default action at the moment
4984 // ------------ Right double click
4986 else if ( event
.RightDClick() )
4990 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4992 // no default action at the moment
4997 // ------------ No buttons down and mouse moving
4999 else if ( event
.Moving() )
5001 m_dragRowOrCol
= YToEdgeOfRow( y
);
5002 if ( m_dragRowOrCol
>= 0 )
5004 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5006 // don't capture the mouse yet
5007 if ( CanDragRowSize() )
5008 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5011 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5013 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5019 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5022 wxPoint
pos( event
.GetPosition() );
5023 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5025 if ( event
.Dragging() )
5029 m_isDragging
= true;
5030 m_colLabelWin
->CaptureMouse();
5033 if ( event
.LeftIsDown() )
5035 switch( m_cursorMode
)
5037 case WXGRID_CURSOR_RESIZE_COL
:
5039 int cw
, ch
, dummy
, top
;
5040 m_gridWin
->GetClientSize( &cw
, &ch
);
5041 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5043 wxClientDC
dc( m_gridWin
);
5046 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5047 GetColMinimalWidth(m_dragRowOrCol
));
5048 dc
.SetLogicalFunction(wxINVERT
);
5049 if ( m_dragLastPos
>= 0 )
5051 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5053 dc
.DrawLine( x
, top
, x
, top
+ch
);
5058 case WXGRID_CURSOR_SELECT_COL
:
5059 if ( (col
= XToCol( x
)) >= 0 )
5063 m_selection
->SelectCol( col
,
5064 event
.ControlDown(),
5071 // default label to suppress warnings about "enumeration value
5072 // 'xxx' not handled in switch
5080 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5085 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5086 m_isDragging
= false;
5089 // ------------ Entering or leaving the window
5091 if ( event
.Entering() || event
.Leaving() )
5093 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5097 // ------------ Left button pressed
5099 else if ( event
.LeftDown() )
5101 // don't send a label click event for a hit on the
5102 // edge of the col label - this is probably the user
5103 // wanting to resize the col
5105 if ( XToEdgeOfCol(x
) < 0 )
5109 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5111 if ( !event
.ShiftDown() && !event
.ControlDown() )
5115 if ( event
.ShiftDown() )
5117 m_selection
->SelectBlock( 0,
5118 m_currentCellCoords
.GetCol(),
5119 GetNumberRows() - 1, col
,
5120 event
.ControlDown(),
5127 m_selection
->SelectCol( col
,
5128 event
.ControlDown(),
5135 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5140 // starting to drag-resize a col
5142 if ( CanDragColSize() )
5143 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5148 // ------------ Left double click
5150 if ( event
.LeftDClick() )
5152 int col
= XToEdgeOfCol(x
);
5157 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5159 // no default action at the moment
5164 // adjust column width depending on label text
5165 AutoSizeColLabelSize( col
);
5167 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5173 // ------------ Left button released
5175 else if ( event
.LeftUp() )
5177 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5179 DoEndDragResizeCol();
5181 // Note: we are ending the event *after* doing
5182 // default processing in this case
5184 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5187 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5192 // ------------ Right button down
5194 else if ( event
.RightDown() )
5198 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5200 // no default action at the moment
5205 // ------------ Right double click
5207 else if ( event
.RightDClick() )
5211 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5213 // no default action at the moment
5218 // ------------ No buttons down and mouse moving
5220 else if ( event
.Moving() )
5222 m_dragRowOrCol
= XToEdgeOfCol( x
);
5223 if ( m_dragRowOrCol
>= 0 )
5225 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5227 // don't capture the cursor yet
5228 if ( CanDragColSize() )
5229 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5232 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5234 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5240 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5242 if ( event
.LeftDown() )
5244 // indicate corner label by having both row and
5247 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5253 else if ( event
.LeftDClick() )
5255 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5258 else if ( event
.RightDown() )
5260 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5262 // no default action at the moment
5266 else if ( event
.RightDClick() )
5268 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5270 // no default action at the moment
5275 void wxGrid::ChangeCursorMode(CursorMode mode
,
5280 static const wxChar
*cursorModes
[] =
5289 wxLogTrace(_T("grid"),
5290 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5291 win
== m_colLabelWin
? _T("colLabelWin")
5292 : win
? _T("rowLabelWin")
5294 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5295 #endif // __WXDEBUG__
5297 if ( mode
== m_cursorMode
&&
5298 win
== m_winCapture
&&
5299 captureMouse
== (m_winCapture
!= NULL
))
5304 // by default use the grid itself
5310 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5311 m_winCapture
= (wxWindow
*)NULL
;
5314 m_cursorMode
= mode
;
5316 switch ( m_cursorMode
)
5318 case WXGRID_CURSOR_RESIZE_ROW
:
5319 win
->SetCursor( m_rowResizeCursor
);
5322 case WXGRID_CURSOR_RESIZE_COL
:
5323 win
->SetCursor( m_colResizeCursor
);
5327 win
->SetCursor( *wxSTANDARD_CURSOR
);
5330 // we need to capture mouse when resizing
5331 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5332 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5334 if ( captureMouse
&& resize
)
5336 win
->CaptureMouse();
5341 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5344 wxPoint
pos( event
.GetPosition() );
5345 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5347 wxGridCellCoords coords
;
5348 XYToCell( x
, y
, coords
);
5350 int cell_rows
, cell_cols
;
5351 bool isFirstDrag
= !m_isDragging
;
5352 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5353 if ((cell_rows
< 0) || (cell_cols
< 0))
5355 coords
.SetRow(coords
.GetRow() + cell_rows
);
5356 coords
.SetCol(coords
.GetCol() + cell_cols
);
5359 if ( event
.Dragging() )
5361 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5363 // Don't start doing anything until the mouse has been drug at
5364 // least 3 pixels in any direction...
5367 if (m_startDragPos
== wxDefaultPosition
)
5369 m_startDragPos
= pos
;
5372 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5376 m_isDragging
= true;
5377 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5379 // Hide the edit control, so it
5380 // won't interfer with drag-shrinking.
5381 if ( IsCellEditControlShown() )
5383 HideCellEditControl();
5384 SaveEditControlValue();
5387 // Have we captured the mouse yet?
5390 m_winCapture
= m_gridWin
;
5391 m_winCapture
->CaptureMouse();
5394 if ( coords
!= wxGridNoCellCoords
)
5396 if ( event
.ControlDown() )
5398 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5399 m_selectingKeyboard
= coords
;
5400 HighlightBlock ( m_selectingKeyboard
, coords
);
5402 else if ( CanDragCell() )
5406 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5407 m_selectingKeyboard
= coords
;
5409 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5417 if ( !IsSelection() )
5419 HighlightBlock( coords
, coords
);
5423 HighlightBlock( m_currentCellCoords
, coords
);
5427 if (! IsVisible(coords
))
5429 MakeCellVisible(coords
);
5430 // TODO: need to introduce a delay or something here. The
5431 // scrolling is way to fast, at least on MSW - also on GTK.
5435 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5437 int cw
, ch
, left
, dummy
;
5438 m_gridWin
->GetClientSize( &cw
, &ch
);
5439 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5441 wxClientDC
dc( m_gridWin
);
5443 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5444 GetRowMinimalHeight(m_dragRowOrCol
) );
5445 dc
.SetLogicalFunction(wxINVERT
);
5446 if ( m_dragLastPos
>= 0 )
5448 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5450 dc
.DrawLine( left
, y
, left
+cw
, y
);
5453 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5455 int cw
, ch
, dummy
, top
;
5456 m_gridWin
->GetClientSize( &cw
, &ch
);
5457 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5459 wxClientDC
dc( m_gridWin
);
5461 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5462 GetColMinimalWidth(m_dragRowOrCol
) );
5463 dc
.SetLogicalFunction(wxINVERT
);
5464 if ( m_dragLastPos
>= 0 )
5466 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5468 dc
.DrawLine( x
, top
, x
, top
+ch
);
5475 m_isDragging
= false;
5476 m_startDragPos
= wxDefaultPosition
;
5478 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5479 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5482 if ( event
.Entering() || event
.Leaving() )
5484 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5485 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5490 // ------------ Left button pressed
5492 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5494 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5499 if ( !event
.ControlDown() )
5501 if ( event
.ShiftDown() )
5505 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5506 m_currentCellCoords
.GetCol(),
5509 event
.ControlDown(),
5515 else if ( XToEdgeOfCol(x
) < 0 &&
5516 YToEdgeOfRow(y
) < 0 )
5518 DisableCellEditControl();
5519 MakeCellVisible( coords
);
5521 if ( event
.ControlDown() )
5525 m_selection
->ToggleCellSelection( coords
.GetRow(),
5527 event
.ControlDown(),
5532 m_selectingTopLeft
= wxGridNoCellCoords
;
5533 m_selectingBottomRight
= wxGridNoCellCoords
;
5534 m_selectingKeyboard
= coords
;
5538 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5539 SetCurrentCell( coords
);
5542 if ( m_selection
->GetSelectionMode() !=
5543 wxGrid::wxGridSelectCells
)
5545 HighlightBlock( coords
, coords
);
5554 // ------------ Left double click
5556 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5558 DisableCellEditControl();
5560 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5562 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5567 // we want double click to select a cell and start editing
5568 // (i.e. to behave in same way as sequence of two slow clicks):
5569 m_waitForSlowClick
= true;
5576 // ------------ Left button released
5578 else if ( event
.LeftUp() )
5580 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5584 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5585 m_winCapture
= NULL
;
5588 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5591 EnableCellEditControl();
5593 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5594 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5595 editor
->StartingClick();
5599 m_waitForSlowClick
= false;
5601 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5602 m_selectingBottomRight
!= wxGridNoCellCoords
)
5606 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5607 m_selectingTopLeft
.GetCol(),
5608 m_selectingBottomRight
.GetRow(),
5609 m_selectingBottomRight
.GetCol(),
5610 event
.ControlDown(),
5616 m_selectingTopLeft
= wxGridNoCellCoords
;
5617 m_selectingBottomRight
= wxGridNoCellCoords
;
5619 // Show the edit control, if it has been hidden for
5621 ShowCellEditControl();
5624 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5626 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5627 DoEndDragResizeRow();
5629 // Note: we are ending the event *after* doing
5630 // default processing in this case
5632 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5634 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5636 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5637 DoEndDragResizeCol();
5639 // Note: we are ending the event *after* doing
5640 // default processing in this case
5642 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5649 // ------------ Right button down
5651 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5653 DisableCellEditControl();
5654 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5659 // no default action at the moment
5664 // ------------ Right double click
5666 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5668 DisableCellEditControl();
5669 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5674 // no default action at the moment
5678 // ------------ Moving and no button action
5680 else if ( event
.Moving() && !event
.IsButton() )
5682 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5684 // out of grid cell area
5685 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5689 int dragRow
= YToEdgeOfRow( y
);
5690 int dragCol
= XToEdgeOfCol( x
);
5692 // Dragging on the corner of a cell to resize in both
5693 // directions is not implemented yet...
5695 if ( dragRow
>= 0 && dragCol
>= 0 )
5697 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5703 m_dragRowOrCol
= dragRow
;
5705 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5707 if ( CanDragRowSize() && CanDragGridSize() )
5708 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5713 m_dragRowOrCol
= dragCol
;
5721 m_dragRowOrCol
= dragCol
;
5723 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5725 if ( CanDragColSize() && CanDragGridSize() )
5726 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5732 // Neither on a row or col edge
5734 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5736 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5742 void wxGrid::DoEndDragResizeRow()
5744 if ( m_dragLastPos
>= 0 )
5746 // erase the last line and resize the row
5748 int cw
, ch
, left
, dummy
;
5749 m_gridWin
->GetClientSize( &cw
, &ch
);
5750 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5752 wxClientDC
dc( m_gridWin
);
5754 dc
.SetLogicalFunction( wxINVERT
);
5755 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5756 HideCellEditControl();
5757 SaveEditControlValue();
5759 int rowTop
= GetRowTop(m_dragRowOrCol
);
5760 SetRowSize( m_dragRowOrCol
,
5761 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5763 if ( !GetBatchCount() )
5765 // Only needed to get the correct rect.y:
5766 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5768 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5769 rect
.width
= m_rowLabelWidth
;
5770 rect
.height
= ch
- rect
.y
;
5771 m_rowLabelWin
->Refresh( true, &rect
);
5773 // if there is a multicell block, paint all of it
5776 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5777 int leftCol
= XToCol(left
);
5778 int rightCol
= internalXToCol(left
+cw
);
5781 for (i
=leftCol
; i
<rightCol
; i
++)
5783 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5784 if (cell_rows
< subtract_rows
)
5785 subtract_rows
= cell_rows
;
5787 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5788 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5789 rect
.height
= ch
- rect
.y
;
5792 m_gridWin
->Refresh( false, &rect
);
5795 ShowCellEditControl();
5800 void wxGrid::DoEndDragResizeCol()
5802 if ( m_dragLastPos
>= 0 )
5804 // erase the last line and resize the col
5806 int cw
, ch
, dummy
, top
;
5807 m_gridWin
->GetClientSize( &cw
, &ch
);
5808 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5810 wxClientDC
dc( m_gridWin
);
5812 dc
.SetLogicalFunction( wxINVERT
);
5813 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5814 HideCellEditControl();
5815 SaveEditControlValue();
5817 int colLeft
= GetColLeft(m_dragRowOrCol
);
5818 SetColSize( m_dragRowOrCol
,
5819 wxMax( m_dragLastPos
- colLeft
,
5820 GetColMinimalWidth(m_dragRowOrCol
) ) );
5822 if ( !GetBatchCount() )
5824 // Only needed to get the correct rect.x:
5825 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5827 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5828 rect
.width
= cw
- rect
.x
;
5829 rect
.height
= m_colLabelHeight
;
5830 m_colLabelWin
->Refresh( true, &rect
);
5832 // if there is a multicell block, paint all of it
5835 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5836 int topRow
= YToRow(top
);
5837 int bottomRow
= internalYToRow(top
+cw
);
5840 for (i
=topRow
; i
<bottomRow
; i
++)
5842 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5843 if (cell_cols
< subtract_cols
)
5844 subtract_cols
= cell_cols
;
5846 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5847 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5848 rect
.width
= cw
- rect
.x
;
5851 m_gridWin
->Refresh( false, &rect
);
5854 ShowCellEditControl();
5861 // ------ interaction with data model
5863 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5865 switch ( msg
.GetId() )
5867 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5868 return GetModelValues();
5870 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5871 return SetModelValues();
5873 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5874 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5875 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5876 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5877 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5878 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5879 return Redimension( msg
);
5888 // The behaviour of this function depends on the grid table class
5889 // Clear() function. For the default wxGridStringTable class the
5890 // behavious is to replace all cell contents with wxEmptyString but
5891 // not to change the number of rows or cols.
5893 void wxGrid::ClearGrid()
5897 if (IsCellEditControlEnabled())
5898 DisableCellEditControl();
5901 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5906 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5908 // TODO: something with updateLabels flag
5912 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5918 if (IsCellEditControlEnabled())
5919 DisableCellEditControl();
5921 bool done
= m_table
->InsertRows( pos
, numRows
);
5924 // the table will have sent the results of the insert row
5925 // operation to this view object as a grid table message
5931 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5933 // TODO: something with updateLabels flag
5937 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5943 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5945 // the table will have sent the results of the append row
5946 // operation to this view object as a grid table message
5952 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5954 // TODO: something with updateLabels flag
5958 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5964 if (IsCellEditControlEnabled())
5965 DisableCellEditControl();
5967 bool done
= m_table
->DeleteRows( pos
, numRows
);
5969 // the table will have sent the results of the delete row
5970 // operation to this view object as a grid table message
5976 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5978 // TODO: something with updateLabels flag
5982 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5988 if (IsCellEditControlEnabled())
5989 DisableCellEditControl();
5991 bool done
= m_table
->InsertCols( pos
, numCols
);
5993 // the table will have sent the results of the insert col
5994 // operation to this view object as a grid table message
6000 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6002 // TODO: something with updateLabels flag
6006 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6012 bool done
= m_table
->AppendCols( numCols
);
6014 // the table will have sent the results of the append col
6015 // operation to this view object as a grid table message
6021 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6023 // TODO: something with updateLabels flag
6027 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6033 if (IsCellEditControlEnabled())
6034 DisableCellEditControl();
6036 bool done
= m_table
->DeleteCols( pos
, numCols
);
6038 // the table will have sent the results of the delete col
6039 // operation to this view object as a grid table message
6047 // ----- event handlers
6050 // Generate a grid event based on a mouse event and
6051 // return the result of ProcessEvent()
6053 int wxGrid::SendEvent( const wxEventType type
,
6055 wxMouseEvent
& mouseEv
)
6060 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6062 int rowOrCol
= (row
== -1 ? col
: row
);
6064 wxGridSizeEvent
gridEvt( GetId(),
6068 mouseEv
.GetX() + GetRowLabelSize(),
6069 mouseEv
.GetY() + GetColLabelSize(),
6070 mouseEv
.ControlDown(),
6071 mouseEv
.ShiftDown(),
6073 mouseEv
.MetaDown() );
6075 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6076 vetoed
= !gridEvt
.IsAllowed();
6078 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6080 // Right now, it should _never_ end up here!
6081 wxGridRangeSelectEvent
gridEvt( GetId(),
6085 m_selectingBottomRight
,
6087 mouseEv
.ControlDown(),
6088 mouseEv
.ShiftDown(),
6090 mouseEv
.MetaDown() );
6092 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6093 vetoed
= !gridEvt
.IsAllowed();
6097 wxGridEvent
gridEvt( GetId(),
6101 mouseEv
.GetX() + GetRowLabelSize(),
6102 mouseEv
.GetY() + GetColLabelSize(),
6104 mouseEv
.ControlDown(),
6105 mouseEv
.ShiftDown(),
6107 mouseEv
.MetaDown() );
6108 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6109 vetoed
= !gridEvt
.IsAllowed();
6112 // A Veto'd event may not be `claimed' so test this first
6113 if (vetoed
) return -1;
6114 return claimed
? 1 : 0;
6118 // Generate a grid event of specified type and return the result
6119 // of ProcessEvent().
6121 int wxGrid::SendEvent( const wxEventType type
,
6127 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6129 int rowOrCol
= (row
== -1 ? col
: row
);
6131 wxGridSizeEvent
gridEvt( GetId(),
6136 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6137 vetoed
= !gridEvt
.IsAllowed();
6141 wxGridEvent
gridEvt( GetId(),
6146 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6147 vetoed
= !gridEvt
.IsAllowed();
6150 // A Veto'd event may not be `claimed' so test this first
6151 if (vetoed
) return -1;
6152 return claimed
? 1 : 0;
6156 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6158 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6161 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6163 // Don't do anything if between Begin/EndBatch...
6164 // EndBatch() will do all this on the last nested one anyway.
6165 if (! GetBatchCount())
6167 // Refresh to get correct scrolled position:
6168 wxScrolledWindow::Refresh(eraseb
,rect
);
6172 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6173 int width_label
, width_cell
, height_label
, height_cell
;
6176 //Copy rectangle can get scroll offsets..
6177 rect_x
= rect
->GetX();
6178 rect_y
= rect
->GetY();
6179 rectWidth
= rect
->GetWidth();
6180 rectHeight
= rect
->GetHeight();
6182 width_label
= m_rowLabelWidth
- rect_x
;
6183 if (width_label
> rectWidth
) width_label
= rectWidth
;
6185 height_label
= m_colLabelHeight
- rect_y
;
6186 if (height_label
> rectHeight
) height_label
= rectHeight
;
6188 if (rect_x
> m_rowLabelWidth
)
6190 x
= rect_x
- m_rowLabelWidth
;
6191 width_cell
= rectWidth
;
6196 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6199 if (rect_y
> m_colLabelHeight
)
6201 y
= rect_y
- m_colLabelHeight
;
6202 height_cell
= rectHeight
;
6207 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6210 // Paint corner label part intersecting rect.
6211 if ( width_label
> 0 && height_label
> 0 )
6213 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6214 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6217 // Paint col labels part intersecting rect.
6218 if ( width_cell
> 0 && height_label
> 0 )
6220 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6221 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6224 // Paint row labels part intersecting rect.
6225 if ( width_label
> 0 && height_cell
> 0 )
6227 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6228 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6231 // Paint cell area part intersecting rect.
6232 if ( width_cell
> 0 && height_cell
> 0 )
6234 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6235 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6240 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6241 m_colLabelWin
->Refresh(eraseb
, NULL
);
6242 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6243 m_gridWin
->Refresh(eraseb
, NULL
);
6248 void wxGrid::OnSize( wxSizeEvent
& event
)
6250 // position the child windows
6253 // don't call CalcDimensions() from here, the base class handles the size
6259 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6261 if ( m_inOnKeyDown
)
6263 // shouldn't be here - we are going round in circles...
6265 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6268 m_inOnKeyDown
= true;
6270 // propagate the event up and see if it gets processed
6272 wxWindow
*parent
= GetParent();
6273 wxKeyEvent
keyEvt( event
);
6274 keyEvt
.SetEventObject( parent
);
6276 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6279 // try local handlers
6281 switch ( event
.GetKeyCode() )
6284 if ( event
.ControlDown() )
6286 MoveCursorUpBlock( event
.ShiftDown() );
6290 MoveCursorUp( event
.ShiftDown() );
6295 if ( event
.ControlDown() )
6297 MoveCursorDownBlock( event
.ShiftDown() );
6301 MoveCursorDown( event
.ShiftDown() );
6306 if ( event
.ControlDown() )
6308 MoveCursorLeftBlock( event
.ShiftDown() );
6312 MoveCursorLeft( event
.ShiftDown() );
6317 if ( event
.ControlDown() )
6319 MoveCursorRightBlock( event
.ShiftDown() );
6323 MoveCursorRight( event
.ShiftDown() );
6328 case WXK_NUMPAD_ENTER
:
6329 if ( event
.ControlDown() )
6331 event
.Skip(); // to let the edit control have the return
6335 if ( GetGridCursorRow() < GetNumberRows()-1 )
6337 MoveCursorDown( event
.ShiftDown() );
6341 // at the bottom of a column
6342 DisableCellEditControl();
6352 if (event
.ShiftDown())
6354 if ( GetGridCursorCol() > 0 )
6356 MoveCursorLeft( false );
6361 DisableCellEditControl();
6366 if ( GetGridCursorCol() < GetNumberCols()-1 )
6368 MoveCursorRight( false );
6373 DisableCellEditControl();
6379 if ( event
.ControlDown() )
6381 MakeCellVisible( 0, 0 );
6382 SetCurrentCell( 0, 0 );
6391 if ( event
.ControlDown() )
6393 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6394 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6411 if ( event
.ControlDown() )
6415 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6416 m_currentCellCoords
.GetCol(),
6417 event
.ControlDown(),
6424 if ( !IsEditable() )
6426 MoveCursorRight( false );
6429 // Otherwise fall through to default
6432 // is it possible to edit the current cell at all?
6433 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6435 // yes, now check whether the cells editor accepts the key
6436 int row
= m_currentCellCoords
.GetRow();
6437 int col
= m_currentCellCoords
.GetCol();
6438 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6439 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6441 // <F2> is special and will always start editing, for
6442 // other keys - ask the editor itself
6443 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6444 || editor
->IsAcceptedKey(event
) )
6446 // ensure cell is visble
6447 MakeCellVisible(row
, col
);
6448 EnableCellEditControl();
6450 // a problem can arise if the cell is not completely
6451 // visible (even after calling MakeCellVisible the
6452 // control is not created and calling StartingKey will
6454 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6466 // let others process char events with modifiers or all
6467 // char events for readonly cells
6474 m_inOnKeyDown
= false;
6477 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6479 // try local handlers
6481 if ( event
.GetKeyCode() == WXK_SHIFT
)
6483 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6484 m_selectingBottomRight
!= wxGridNoCellCoords
)
6488 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6489 m_selectingTopLeft
.GetCol(),
6490 m_selectingBottomRight
.GetRow(),
6491 m_selectingBottomRight
.GetCol(),
6492 event
.ControlDown(),
6499 m_selectingTopLeft
= wxGridNoCellCoords
;
6500 m_selectingBottomRight
= wxGridNoCellCoords
;
6501 m_selectingKeyboard
= wxGridNoCellCoords
;
6505 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6509 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6511 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6513 // the event has been intercepted - do nothing
6517 wxClientDC
dc(m_gridWin
);
6520 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6522 DisableCellEditControl();
6524 if ( IsVisible( m_currentCellCoords
, false ) )
6527 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6528 if ( !m_gridLinesEnabled
)
6536 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6538 // Otherwise refresh redraws the highlight!
6539 m_currentCellCoords
= coords
;
6541 DrawGridCellArea(dc
,cells
);
6542 DrawAllGridLines( dc
, r
);
6546 m_currentCellCoords
= coords
;
6548 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6549 DrawCellHighlight(dc
, attr
);
6554 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6557 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6561 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6564 rightCol
= GetNumberCols() - 1;
6566 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6569 bottomRow
= GetNumberRows() - 1;
6573 if ( topRow
> bottomRow
)
6580 if ( leftCol
> rightCol
)
6587 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6588 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6590 // First the case that we selected a completely new area
6591 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6592 m_selectingBottomRight
== wxGridNoCellCoords
)
6595 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6596 wxGridCellCoords ( bottomRow
, rightCol
) );
6597 m_gridWin
->Refresh( false, &rect
);
6599 // Now handle changing an existing selection area.
6600 else if ( m_selectingTopLeft
!= updateTopLeft
||
6601 m_selectingBottomRight
!= updateBottomRight
)
6603 // Compute two optimal update rectangles:
6604 // Either one rectangle is a real subset of the
6605 // other, or they are (almost) disjoint!
6607 bool need_refresh
[4];
6611 need_refresh
[3] = false;
6614 // Store intermediate values
6615 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6616 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6617 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6618 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6620 // Determine the outer/inner coordinates.
6621 if (oldLeft
> leftCol
)
6627 if (oldTop
> topRow
)
6633 if (oldRight
< rightCol
)
6636 oldRight
= rightCol
;
6639 if (oldBottom
< bottomRow
)
6642 oldBottom
= bottomRow
;
6646 // Now, either the stuff marked old is the outer
6647 // rectangle or we don't have a situation where one
6648 // is contained in the other.
6650 if ( oldLeft
< leftCol
)
6652 // Refresh the newly selected or deselected
6653 // area to the left of the old or new selection.
6654 need_refresh
[0] = true;
6655 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6657 wxGridCellCoords ( oldBottom
,
6661 if ( oldTop
< topRow
)
6663 // Refresh the newly selected or deselected
6664 // area above the old or new selection.
6665 need_refresh
[1] = true;
6666 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6668 wxGridCellCoords ( topRow
- 1,
6672 if ( oldRight
> rightCol
)
6674 // Refresh the newly selected or deselected
6675 // area to the right of the old or new selection.
6676 need_refresh
[2] = true;
6677 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6679 wxGridCellCoords ( oldBottom
,
6683 if ( oldBottom
> bottomRow
)
6685 // Refresh the newly selected or deselected
6686 // area below the old or new selection.
6687 need_refresh
[3] = true;
6688 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6690 wxGridCellCoords ( oldBottom
,
6694 // various Refresh() calls
6695 for (i
= 0; i
< 4; i
++ )
6696 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6697 m_gridWin
->Refresh( false, &(rect
[i
]) );
6700 m_selectingTopLeft
= updateTopLeft
;
6701 m_selectingBottomRight
= updateBottomRight
;
6705 // ------ functions to get/send data (see also public functions)
6708 bool wxGrid::GetModelValues()
6710 // Hide the editor, so it won't hide a changed value.
6711 HideCellEditControl();
6715 // all we need to do is repaint the grid
6717 m_gridWin
->Refresh();
6725 bool wxGrid::SetModelValues()
6729 // Disable the editor, so it won't hide a changed value.
6730 // Do we also want to save the current value of the editor first?
6732 DisableCellEditControl();
6736 for ( row
= 0; row
< m_numRows
; row
++ )
6738 for ( col
= 0; col
< m_numCols
; col
++ )
6740 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6752 // Note - this function only draws cells that are in the list of
6753 // exposed cells (usually set from the update region by
6754 // CalcExposedCells)
6756 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6758 if ( !m_numRows
|| !m_numCols
) return;
6760 int i
, numCells
= cells
.GetCount();
6761 int row
, col
, cell_rows
, cell_cols
;
6762 wxGridCellCoordsArray redrawCells
;
6764 for ( i
= numCells
-1; i
>= 0; i
-- )
6766 row
= cells
[i
].GetRow();
6767 col
= cells
[i
].GetCol();
6768 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6770 // If this cell is part of a multicell block, find owner for repaint
6771 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6773 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6774 bool marked
= false;
6775 for ( int j
= 0; j
< numCells
; j
++ )
6777 if ( cell
== cells
[j
] )
6785 int count
= redrawCells
.GetCount();
6786 for (int j
= 0; j
< count
; j
++)
6788 if ( cell
== redrawCells
[j
] )
6794 if (!marked
) redrawCells
.Add( cell
);
6796 continue; // don't bother drawing this cell
6799 // If this cell is empty, find cell to left that might want to overflow
6800 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6802 for ( int l
= 0; l
< cell_rows
; l
++ )
6804 // find a cell in this row to left alreay marked for repaint
6806 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6807 if ((redrawCells
[k
].GetCol() < left
) &&
6808 (redrawCells
[k
].GetRow() == row
))
6809 left
=redrawCells
[k
].GetCol();
6811 if (left
== col
) left
= 0; // oh well
6813 for (int j
= col
-1; j
>= left
; j
--)
6815 if (!m_table
->IsEmptyCell(row
+l
, j
))
6817 if (GetCellOverflow(row
+l
, j
))
6819 wxGridCellCoords
cell(row
+l
, j
);
6820 bool marked
= false;
6822 for (int k
= 0; k
< numCells
; k
++)
6824 if ( cell
== cells
[k
] )
6832 int count
= redrawCells
.GetCount();
6833 for (int k
= 0; k
< count
; k
++)
6835 if ( cell
== redrawCells
[k
] )
6841 if (!marked
) redrawCells
.Add( cell
);
6849 DrawCell( dc
, cells
[i
] );
6852 numCells
= redrawCells
.GetCount();
6854 for ( i
= numCells
- 1; i
>= 0; i
-- )
6856 DrawCell( dc
, redrawCells
[i
] );
6861 void wxGrid::DrawGridSpace( wxDC
& dc
)
6864 m_gridWin
->GetClientSize( &cw
, &ch
);
6867 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6869 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6870 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6872 if ( right
> rightCol
|| bottom
> bottomRow
)
6875 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6877 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6878 dc
.SetPen( *wxTRANSPARENT_PEN
);
6880 if ( right
> rightCol
)
6882 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6885 if ( bottom
> bottomRow
)
6887 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6893 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6895 int row
= coords
.GetRow();
6896 int col
= coords
.GetCol();
6898 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6901 // we draw the cell border ourselves
6902 #if !WXGRID_DRAW_LINES
6903 if ( m_gridLinesEnabled
)
6904 DrawCellBorder( dc
, coords
);
6907 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6909 bool isCurrent
= coords
== m_currentCellCoords
;
6911 wxRect rect
= CellToRect( row
, col
);
6913 // if the editor is shown, we should use it and not the renderer
6914 // Note: However, only if it is really _shown_, i.e. not hidden!
6915 if ( isCurrent
&& IsCellEditControlShown() )
6917 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6918 editor
->PaintBackground(rect
, attr
);
6923 // but all the rest is drawn by the cell renderer and hence may be
6925 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6926 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6933 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6935 int row
= m_currentCellCoords
.GetRow();
6936 int col
= m_currentCellCoords
.GetCol();
6938 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6941 wxRect rect
= CellToRect(row
, col
);
6943 // hmmm... what could we do here to show that the cell is disabled?
6944 // for now, I just draw a thinner border than for the other ones, but
6945 // it doesn't look really good
6947 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6951 // The center of th drawn line is where the position/width/height of
6952 // the rectangle is actually at, (on wxMSW atr least,) so we will
6953 // reduce the size of the rectangle to compensate for the thickness of
6954 // the line. If this is too strange on non wxMSW platforms then
6955 // please #ifdef this appropriately.
6956 rect
.x
+= penWidth
/2;
6957 rect
.y
+= penWidth
/2;
6958 rect
.width
-= penWidth
-1;
6959 rect
.height
-= penWidth
-1;
6962 // Now draw the rectangle
6963 // use the cellHighlightColour if the cell is inside a selection, this
6964 // will ensure the cell is always visible.
6965 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6966 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6967 dc
.DrawRectangle(rect
);
6971 // VZ: my experiments with 3d borders...
6973 // how to properly set colours for arbitrary bg?
6974 wxCoord x1
= rect
.x
,
6976 x2
= rect
.x
+ rect
.width
-1,
6977 y2
= rect
.y
+ rect
.height
-1;
6979 dc
.SetPen(*wxWHITE_PEN
);
6980 dc
.DrawLine(x1
, y1
, x2
, y1
);
6981 dc
.DrawLine(x1
, y1
, x1
, y2
);
6983 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6984 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6986 dc
.SetPen(*wxBLACK_PEN
);
6987 dc
.DrawLine(x1
, y2
, x2
, y2
);
6988 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6993 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6995 int row
= coords
.GetRow();
6996 int col
= coords
.GetCol();
6997 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7000 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7002 wxRect rect
= CellToRect( row
, col
);
7004 // right hand border
7006 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7007 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7011 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7012 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7015 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7017 // This if block was previously in wxGrid::OnPaint but that doesn't
7018 // seem to get called under wxGTK - MB
7020 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7021 m_numRows
&& m_numCols
)
7023 m_currentCellCoords
.Set(0, 0);
7026 if ( IsCellEditControlShown() )
7028 // don't show highlight when the edit control is shown
7032 // if the active cell was repainted, repaint its highlight too because it
7033 // might have been damaged by the grid lines
7034 size_t count
= cells
.GetCount();
7035 for ( size_t n
= 0; n
< count
; n
++ )
7037 if ( cells
[n
] == m_currentCellCoords
)
7039 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7040 DrawCellHighlight(dc
, attr
);
7048 // TODO: remove this ???
7049 // This is used to redraw all grid lines e.g. when the grid line colour
7052 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7054 #if !WXGRID_DRAW_LINES
7058 if ( !m_gridLinesEnabled
||
7060 !m_numCols
) return;
7062 int top
, bottom
, left
, right
;
7064 #if 0 //#ifndef __WXGTK__
7068 m_gridWin
->GetClientSize(&cw
, &ch
);
7070 // virtual coords of visible area
7072 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7073 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7078 reg
.GetBox(x
, y
, w
, h
);
7079 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7080 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7084 m_gridWin
->GetClientSize(&cw
, &ch
);
7085 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7086 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7089 // avoid drawing grid lines past the last row and col
7091 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7092 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7094 // no gridlines inside multicells, clip them out
7095 int leftCol
= internalXToCol(left
);
7096 int topRow
= internalYToRow(top
);
7097 int rightCol
= internalXToCol(right
);
7098 int bottomRow
= internalYToRow(bottom
);
7099 wxRegion
clippedcells(0, 0, cw
, ch
);
7102 int i
, j
, cell_rows
, cell_cols
;
7105 for (j
=topRow
; j
<bottomRow
; j
++)
7107 for (i
=leftCol
; i
<rightCol
; i
++)
7109 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7110 if ((cell_rows
> 1) || (cell_cols
> 1))
7112 rect
= CellToRect(j
,i
);
7113 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7114 clippedcells
.Subtract(rect
);
7116 else if ((cell_rows
< 0) || (cell_cols
< 0))
7118 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7119 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7120 clippedcells
.Subtract(rect
);
7124 dc
.SetClippingRegion( clippedcells
);
7126 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7128 // horizontal grid lines
7130 // already declared above - int i;
7131 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7133 int bot
= GetRowBottom(i
) - 1;
7142 dc
.DrawLine( left
, bot
, right
, bot
);
7147 // vertical grid lines
7149 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7151 int colRight
= GetColRight(i
) - 1;
7152 if ( colRight
> right
)
7157 if ( colRight
>= left
)
7159 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7162 dc
.DestroyClippingRegion();
7166 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7168 if ( !m_numRows
) return;
7171 size_t numLabels
= rows
.GetCount();
7173 for ( i
= 0; i
< numLabels
; i
++ )
7175 DrawRowLabel( dc
, rows
[i
] );
7180 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7182 if ( GetRowHeight(row
) <= 0 )
7185 int rowTop
= GetRowTop(row
),
7186 rowBottom
= GetRowBottom(row
) - 1;
7188 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7189 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7190 m_rowLabelWidth
-1, rowBottom
);
7192 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7194 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7196 dc
.SetPen( *wxWHITE_PEN
);
7197 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7198 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7200 dc
.SetBackgroundMode( wxTRANSPARENT
);
7201 dc
.SetTextForeground( GetLabelTextColour() );
7202 dc
.SetFont( GetLabelFont() );
7205 GetRowLabelAlignment( &hAlign
, &vAlign
);
7209 rect
.SetY( GetRowTop(row
) + 2 );
7210 rect
.SetWidth( m_rowLabelWidth
- 4 );
7211 rect
.SetHeight( GetRowHeight(row
) - 4 );
7212 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7216 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7218 if ( !m_numCols
) return;
7221 size_t numLabels
= cols
.GetCount();
7223 for ( i
= 0; i
< numLabels
; i
++ )
7225 DrawColLabel( dc
, cols
[i
] );
7230 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7232 if ( GetColWidth(col
) <= 0 )
7235 int colLeft
= GetColLeft(col
),
7236 colRight
= GetColRight(col
) - 1;
7238 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7239 dc
.DrawLine( colRight
, 0,
7240 colRight
, m_colLabelHeight
-1 );
7242 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7244 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7245 colRight
+1, m_colLabelHeight
-1 );
7247 dc
.SetPen( *wxWHITE_PEN
);
7248 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7249 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7251 dc
.SetBackgroundMode( wxTRANSPARENT
);
7252 dc
.SetTextForeground( GetLabelTextColour() );
7253 dc
.SetFont( GetLabelFont() );
7255 int hAlign
, vAlign
, orient
;
7256 GetColLabelAlignment( &hAlign
, &vAlign
);
7257 orient
= GetColLabelTextOrientation();
7260 rect
.SetX( colLeft
+ 2 );
7262 rect
.SetWidth( GetColWidth(col
) - 4 );
7263 rect
.SetHeight( m_colLabelHeight
- 4 );
7264 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7267 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7268 const wxString
& value
,
7272 int textOrientation
)
7274 wxArrayString lines
;
7276 StringToLines( value
, lines
);
7279 //Forward to new API.
7280 DrawTextRectangle( dc
,
7289 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7290 const wxArrayString
& lines
,
7294 int textOrientation
)
7296 long textWidth
, textHeight
;
7297 long lineWidth
, lineHeight
;
7300 dc
.SetClippingRegion( rect
);
7302 nLines
= lines
.GetCount();
7306 float x
= 0.0, y
= 0.0;
7308 if( textOrientation
== wxHORIZONTAL
)
7309 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7311 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7315 case wxALIGN_BOTTOM
:
7316 if( textOrientation
== wxHORIZONTAL
)
7317 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7319 x
= rect
.x
+ rect
.width
- textWidth
;
7322 case wxALIGN_CENTRE
:
7323 if( textOrientation
== wxHORIZONTAL
)
7324 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7326 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7331 if( textOrientation
== wxHORIZONTAL
)
7338 // Align each line of a multi-line label
7339 for( l
= 0; l
< nLines
; l
++ )
7341 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7343 switch( horizAlign
)
7346 if( textOrientation
== wxHORIZONTAL
)
7347 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7349 y
= rect
.y
+ lineWidth
+ 1;
7352 case wxALIGN_CENTRE
:
7353 if( textOrientation
== wxHORIZONTAL
)
7354 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7356 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7361 if( textOrientation
== wxHORIZONTAL
)
7364 y
= rect
.y
+ rect
.height
- 1;
7368 if( textOrientation
== wxHORIZONTAL
)
7370 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7375 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7380 dc
.DestroyClippingRegion();
7384 // Split multi line text up into an array of strings. Any existing
7385 // contents of the string array are preserved.
7387 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7391 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7392 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7394 while ( startPos
< (int)tVal
.Length() )
7396 pos
= tVal
.Mid(startPos
).Find( eol
);
7401 else if ( pos
== 0 )
7403 lines
.Add( wxEmptyString
);
7407 lines
.Add( value
.Mid(startPos
, pos
) );
7411 if ( startPos
< (int)value
.Length() )
7413 lines
.Add( value
.Mid( startPos
) );
7418 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7419 const wxArrayString
& lines
,
7420 long *width
, long *height
)
7427 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7429 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7430 w
= wxMax( w
, lineW
);
7439 // ------ Batch processing.
7441 void wxGrid::EndBatch()
7443 if ( m_batchCount
> 0 )
7446 if ( !m_batchCount
)
7449 m_rowLabelWin
->Refresh();
7450 m_colLabelWin
->Refresh();
7451 m_cornerLabelWin
->Refresh();
7452 m_gridWin
->Refresh();
7457 // Use this, rather than wxWindow::Refresh(), to force an immediate
7458 // repainting of the grid. Has no effect if you are already inside a
7459 // BeginBatch / EndBatch block.
7461 void wxGrid::ForceRefresh()
7469 // ------ Edit control functions
7473 void wxGrid::EnableEditing( bool edit
)
7475 // TODO: improve this ?
7477 if ( edit
!= m_editable
)
7479 if(!edit
) EnableCellEditControl(edit
);
7485 void wxGrid::EnableCellEditControl( bool enable
)
7490 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7491 SetCurrentCell( 0, 0 );
7493 if ( enable
!= m_cellEditCtrlEnabled
)
7497 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7500 // this should be checked by the caller!
7501 wxASSERT_MSG( CanEnableCellControl(),
7502 _T("can't enable editing for this cell!") );
7504 // do it before ShowCellEditControl()
7505 m_cellEditCtrlEnabled
= enable
;
7507 ShowCellEditControl();
7511 //FIXME:add veto support
7512 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7514 HideCellEditControl();
7515 SaveEditControlValue();
7517 // do it after HideCellEditControl()
7518 m_cellEditCtrlEnabled
= enable
;
7523 bool wxGrid::IsCurrentCellReadOnly() const
7526 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7527 bool readonly
= attr
->IsReadOnly();
7533 bool wxGrid::CanEnableCellControl() const
7535 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7536 !IsCurrentCellReadOnly();
7540 bool wxGrid::IsCellEditControlEnabled() const
7542 // the cell edit control might be disable for all cells or just for the
7543 // current one if it's read only
7544 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7547 bool wxGrid::IsCellEditControlShown() const
7549 bool isShown
= false;
7551 if ( m_cellEditCtrlEnabled
)
7553 int row
= m_currentCellCoords
.GetRow();
7554 int col
= m_currentCellCoords
.GetCol();
7555 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7556 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7561 if ( editor
->IsCreated() )
7563 isShown
= editor
->GetControl()->IsShown();
7573 void wxGrid::ShowCellEditControl()
7575 if ( IsCellEditControlEnabled() )
7577 if ( !IsVisible( m_currentCellCoords
) )
7579 m_cellEditCtrlEnabled
= false;
7584 wxRect rect
= CellToRect( m_currentCellCoords
);
7585 int row
= m_currentCellCoords
.GetRow();
7586 int col
= m_currentCellCoords
.GetCol();
7588 // if this is part of a multicell, find owner (topleft)
7589 int cell_rows
, cell_cols
;
7590 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7591 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7595 m_currentCellCoords
.SetRow( row
);
7596 m_currentCellCoords
.SetCol( col
);
7599 // convert to scrolled coords
7601 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7603 // done in PaintBackground()
7605 // erase the highlight and the cell contents because the editor
7606 // might not cover the entire cell
7607 wxClientDC
dc( m_gridWin
);
7609 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7610 dc
.SetPen(*wxTRANSPARENT_PEN
);
7611 dc
.DrawRectangle(rect
);
7614 // cell is shifted by one pixel
7615 // However, don't allow x or y to become negative
7616 // since the SetSize() method interprets that as
7623 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7624 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7625 if ( !editor
->IsCreated() )
7627 editor
->Create(m_gridWin
, wxID_ANY
,
7628 new wxGridCellEditorEvtHandler(this, editor
));
7630 wxGridEditorCreatedEvent
evt(GetId(),
7631 wxEVT_GRID_EDITOR_CREATED
,
7635 editor
->GetControl());
7636 GetEventHandler()->ProcessEvent(evt
);
7640 // resize editor to overflow into righthand cells if allowed
7641 int maxWidth
= rect
.width
;
7642 wxString value
= GetCellValue(row
, col
);
7643 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7646 GetTextExtent(value
, &maxWidth
, &y
,
7647 NULL
, NULL
, &attr
->GetFont());
7648 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7650 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7651 if (rect
.x
+maxWidth
> client_right
)
7652 maxWidth
= client_right
- rect
.x
;
7654 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7656 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7657 // may have changed earlier
7658 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7661 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7662 // looks weird going over a multicell
7663 if (m_table
->IsEmptyCell(row
,i
) &&
7664 (rect
.width
< maxWidth
) && (c_rows
== 1))
7665 rect
.width
+= GetColWidth(i
);
7669 if (rect
.GetRight() > client_right
)
7670 rect
.SetRight(client_right
-1);
7673 editor
->SetCellAttr(attr
);
7674 editor
->SetSize( rect
);
7675 editor
->Show( true, attr
);
7677 // recalc dimensions in case we need to
7678 // expand the scrolled window to account for editor
7681 editor
->BeginEdit(row
, col
, this);
7682 editor
->SetCellAttr(NULL
);
7691 void wxGrid::HideCellEditControl()
7693 if ( IsCellEditControlEnabled() )
7695 int row
= m_currentCellCoords
.GetRow();
7696 int col
= m_currentCellCoords
.GetCol();
7698 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7699 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7700 editor
->Show( false );
7703 m_gridWin
->SetFocus();
7704 // refresh whole row to the right
7705 wxRect
rect( CellToRect(row
, col
) );
7706 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7707 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7708 m_gridWin
->Refresh( false, &rect
);
7713 void wxGrid::SaveEditControlValue()
7715 if ( IsCellEditControlEnabled() )
7717 int row
= m_currentCellCoords
.GetRow();
7718 int col
= m_currentCellCoords
.GetCol();
7720 wxString oldval
= GetCellValue(row
,col
);
7722 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7723 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7724 bool changed
= editor
->EndEdit(row
, col
, this);
7731 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7732 m_currentCellCoords
.GetRow(),
7733 m_currentCellCoords
.GetCol() ) < 0 ) {
7735 // Event has been vetoed, set the data back.
7736 SetCellValue(row
,col
,oldval
);
7744 // ------ Grid location functions
7745 // Note that all of these functions work with the logical coordinates of
7746 // grid cells and labels so you will need to convert from device
7747 // coordinates for mouse events etc.
7750 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7752 int row
= YToRow(y
);
7753 int col
= XToCol(x
);
7755 if ( row
== -1 || col
== -1 )
7757 coords
= wxGridNoCellCoords
;
7761 coords
.Set( row
, col
);
7766 // Internal Helper function for computing row or column from some
7767 // (unscrolled) coordinate value, using either
7768 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7769 // of m_rowBottoms/m_ColRights to speed up the search!
7771 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7772 const wxArrayInt
& BorderArray
, int nMax
,
7777 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7783 size_t i_max
= coord
/ defaultDist
,
7786 if (BorderArray
.IsEmpty())
7788 if((int) i_max
< nMax
)
7790 return clipToMinMax
? nMax
- 1 : -1;
7793 if ( i_max
>= BorderArray
.GetCount())
7794 i_max
= BorderArray
.GetCount() - 1;
7797 if ( coord
>= BorderArray
[i_max
])
7801 i_max
= coord
/ minDist
;
7803 i_max
= BorderArray
.GetCount() - 1;
7805 if ( i_max
>= BorderArray
.GetCount())
7806 i_max
= BorderArray
.GetCount() - 1;
7808 if ( coord
>= BorderArray
[i_max
])
7809 return clipToMinMax
? (int)i_max
: -1;
7810 if ( coord
< BorderArray
[0] )
7813 while ( i_max
- i_min
> 0 )
7815 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7816 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7817 if (coord
>= BorderArray
[ i_max
- 1])
7821 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7822 if (coord
< BorderArray
[median
])
7830 int wxGrid::YToRow( int y
)
7832 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7833 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7837 int wxGrid::XToCol( int x
)
7839 return CoordToRowOrCol(x
, m_defaultColWidth
,
7840 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7844 // return the row number that that the y coord is near the edge of, or
7845 // -1 if not near an edge
7847 int wxGrid::YToEdgeOfRow( int y
)
7850 i
= internalYToRow(y
);
7852 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7854 // We know that we are in row i, test whether we are
7855 // close enough to lower or upper border, respectively.
7856 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7858 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7866 // return the col number that that the x coord is near the edge of, or
7867 // -1 if not near an edge
7869 int wxGrid::XToEdgeOfCol( int x
)
7872 i
= internalXToCol(x
);
7874 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7876 // We know that we are in column i, test whether we are
7877 // close enough to right or left border, respectively.
7878 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7880 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7888 wxRect
wxGrid::CellToRect( int row
, int col
)
7890 wxRect
rect( -1, -1, -1, -1 );
7892 if ( row
>= 0 && row
< m_numRows
&&
7893 col
>= 0 && col
< m_numCols
)
7895 int i
, cell_rows
, cell_cols
;
7896 rect
.width
= rect
.height
= 0;
7897 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7898 // if negative then find multicell owner
7899 if (cell_rows
< 0) row
+= cell_rows
;
7900 if (cell_cols
< 0) col
+= cell_cols
;
7901 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7903 rect
.x
= GetColLeft(col
);
7904 rect
.y
= GetRowTop(row
);
7905 for (i
=col
; i
<col
+cell_cols
; i
++)
7906 rect
.width
+= GetColWidth(i
);
7907 for (i
=row
; i
<row
+cell_rows
; i
++)
7908 rect
.height
+= GetRowHeight(i
);
7911 // if grid lines are enabled, then the area of the cell is a bit smaller
7912 if (m_gridLinesEnabled
) {
7920 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7922 // get the cell rectangle in logical coords
7924 wxRect
r( CellToRect( row
, col
) );
7926 // convert to device coords
7928 int left
, top
, right
, bottom
;
7929 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7930 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7932 // check against the client area of the grid window
7935 m_gridWin
->GetClientSize( &cw
, &ch
);
7937 if ( wholeCellVisible
)
7939 // is the cell wholly visible ?
7941 return ( left
>= 0 && right
<= cw
&&
7942 top
>= 0 && bottom
<= ch
);
7946 // is the cell partly visible ?
7948 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7949 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7954 // make the specified cell location visible by doing a minimal amount
7957 void wxGrid::MakeCellVisible( int row
, int col
)
7961 int xpos
= -1, ypos
= -1;
7963 if ( row
>= 0 && row
< m_numRows
&&
7964 col
>= 0 && col
< m_numCols
)
7966 // get the cell rectangle in logical coords
7968 wxRect
r( CellToRect( row
, col
) );
7970 // convert to device coords
7972 int left
, top
, right
, bottom
;
7973 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7974 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7977 m_gridWin
->GetClientSize( &cw
, &ch
);
7983 else if ( bottom
> ch
)
7985 int h
= r
.GetHeight();
7987 for ( i
= row
-1; i
>= 0; i
-- )
7989 int rowHeight
= GetRowHeight(i
);
7990 if ( h
+ rowHeight
> ch
)
7997 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7998 // have rounding errors (this is important, because if we do, we
7999 // might not scroll at all and some cells won't be redrawn)
8001 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8003 ypos
+= GRID_SCROLL_LINE_Y
;
8010 else if ( right
> cw
)
8012 // position the view so that the cell is on the right
8014 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8015 xpos
= x0
+ (right
- cw
);
8017 // see comment for ypos above
8018 xpos
+= GRID_SCROLL_LINE_X
;
8021 if ( xpos
!= -1 || ypos
!= -1 )
8024 xpos
/= GRID_SCROLL_LINE_X
;
8026 ypos
/= GRID_SCROLL_LINE_Y
;
8027 Scroll( xpos
, ypos
);
8035 // ------ Grid cursor movement functions
8038 bool wxGrid::MoveCursorUp( bool expandSelection
)
8040 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8041 m_currentCellCoords
.GetRow() >= 0 )
8043 if ( expandSelection
)
8045 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8046 m_selectingKeyboard
= m_currentCellCoords
;
8047 if ( m_selectingKeyboard
.GetRow() > 0 )
8049 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8050 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8051 m_selectingKeyboard
.GetCol() );
8052 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8055 else if ( m_currentCellCoords
.GetRow() > 0 )
8058 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8059 m_currentCellCoords
.GetCol() );
8060 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8061 m_currentCellCoords
.GetCol() );
8072 bool wxGrid::MoveCursorDown( bool expandSelection
)
8074 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8075 m_currentCellCoords
.GetRow() < m_numRows
)
8077 if ( expandSelection
)
8079 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8080 m_selectingKeyboard
= m_currentCellCoords
;
8081 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8083 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8084 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8085 m_selectingKeyboard
.GetCol() );
8086 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8089 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8092 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8093 m_currentCellCoords
.GetCol() );
8094 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8095 m_currentCellCoords
.GetCol() );
8106 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8108 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8109 m_currentCellCoords
.GetCol() >= 0 )
8111 if ( expandSelection
)
8113 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8114 m_selectingKeyboard
= m_currentCellCoords
;
8115 if ( m_selectingKeyboard
.GetCol() > 0 )
8117 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8118 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8119 m_selectingKeyboard
.GetCol() );
8120 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8123 else if ( m_currentCellCoords
.GetCol() > 0 )
8126 MakeCellVisible( m_currentCellCoords
.GetRow(),
8127 m_currentCellCoords
.GetCol() - 1 );
8128 SetCurrentCell( m_currentCellCoords
.GetRow(),
8129 m_currentCellCoords
.GetCol() - 1 );
8140 bool wxGrid::MoveCursorRight( bool expandSelection
)
8142 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8143 m_currentCellCoords
.GetCol() < m_numCols
)
8145 if ( expandSelection
)
8147 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8148 m_selectingKeyboard
= m_currentCellCoords
;
8149 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8151 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8152 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8153 m_selectingKeyboard
.GetCol() );
8154 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8157 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8160 MakeCellVisible( m_currentCellCoords
.GetRow(),
8161 m_currentCellCoords
.GetCol() + 1 );
8162 SetCurrentCell( m_currentCellCoords
.GetRow(),
8163 m_currentCellCoords
.GetCol() + 1 );
8174 bool wxGrid::MovePageUp()
8176 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8178 int row
= m_currentCellCoords
.GetRow();
8182 m_gridWin
->GetClientSize( &cw
, &ch
);
8184 int y
= GetRowTop(row
);
8185 int newRow
= internalYToRow( y
- ch
+ 1 );
8187 if ( newRow
== row
)
8189 //row > 0 , so newrow can never be less than 0 here.
8193 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8194 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8202 bool wxGrid::MovePageDown()
8204 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8206 int row
= m_currentCellCoords
.GetRow();
8207 if ( (row
+1) < m_numRows
)
8210 m_gridWin
->GetClientSize( &cw
, &ch
);
8212 int y
= GetRowTop(row
);
8213 int newRow
= internalYToRow( y
+ ch
);
8214 if ( newRow
== row
)
8216 // row < m_numRows , so newrow can't overflow here.
8220 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8221 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8229 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8232 m_currentCellCoords
!= wxGridNoCellCoords
&&
8233 m_currentCellCoords
.GetRow() > 0 )
8235 int row
= m_currentCellCoords
.GetRow();
8236 int col
= m_currentCellCoords
.GetCol();
8238 if ( m_table
->IsEmptyCell(row
, col
) )
8240 // starting in an empty cell: find the next block of
8246 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8249 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8251 // starting at the top of a block: find the next block
8257 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8262 // starting within a block: find the top of the block
8267 if ( m_table
->IsEmptyCell(row
, col
) )
8275 MakeCellVisible( row
, col
);
8276 if ( expandSelection
)
8278 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8279 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8284 SetCurrentCell( row
, col
);
8292 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8295 m_currentCellCoords
!= wxGridNoCellCoords
&&
8296 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8298 int row
= m_currentCellCoords
.GetRow();
8299 int col
= m_currentCellCoords
.GetCol();
8301 if ( m_table
->IsEmptyCell(row
, col
) )
8303 // starting in an empty cell: find the next block of
8306 while ( row
< m_numRows
-1 )
8309 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8312 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8314 // starting at the bottom of a block: find the next block
8317 while ( row
< m_numRows
-1 )
8320 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8325 // starting within a block: find the bottom of the block
8327 while ( row
< m_numRows
-1 )
8330 if ( m_table
->IsEmptyCell(row
, col
) )
8338 MakeCellVisible( row
, col
);
8339 if ( expandSelection
)
8341 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8342 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8347 SetCurrentCell( row
, col
);
8356 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8359 m_currentCellCoords
!= wxGridNoCellCoords
&&
8360 m_currentCellCoords
.GetCol() > 0 )
8362 int row
= m_currentCellCoords
.GetRow();
8363 int col
= m_currentCellCoords
.GetCol();
8365 if ( m_table
->IsEmptyCell(row
, col
) )
8367 // starting in an empty cell: find the next block of
8373 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8376 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8378 // starting at the left of a block: find the next block
8384 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8389 // starting within a block: find the left of the block
8394 if ( m_table
->IsEmptyCell(row
, col
) )
8402 MakeCellVisible( row
, col
);
8403 if ( expandSelection
)
8405 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8406 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8411 SetCurrentCell( row
, col
);
8420 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8423 m_currentCellCoords
!= wxGridNoCellCoords
&&
8424 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8426 int row
= m_currentCellCoords
.GetRow();
8427 int col
= m_currentCellCoords
.GetCol();
8429 if ( m_table
->IsEmptyCell(row
, col
) )
8431 // starting in an empty cell: find the next block of
8434 while ( col
< m_numCols
-1 )
8437 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8440 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8442 // starting at the right of a block: find the next block
8445 while ( col
< m_numCols
-1 )
8448 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8453 // starting within a block: find the right of the block
8455 while ( col
< m_numCols
-1 )
8458 if ( m_table
->IsEmptyCell(row
, col
) )
8466 MakeCellVisible( row
, col
);
8467 if ( expandSelection
)
8469 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8470 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8475 SetCurrentCell( row
, col
);
8487 // ------ Label values and formatting
8490 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8492 *horiz
= m_rowLabelHorizAlign
;
8493 *vert
= m_rowLabelVertAlign
;
8496 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8498 *horiz
= m_colLabelHorizAlign
;
8499 *vert
= m_colLabelVertAlign
;
8502 int wxGrid::GetColLabelTextOrientation()
8504 return m_colLabelTextOrientation
;
8507 wxString
wxGrid::GetRowLabelValue( int row
)
8511 return m_table
->GetRowLabelValue( row
);
8521 wxString
wxGrid::GetColLabelValue( int col
)
8525 return m_table
->GetColLabelValue( col
);
8536 void wxGrid::SetRowLabelSize( int width
)
8538 width
= wxMax( width
, 0 );
8539 if ( width
!= m_rowLabelWidth
)
8543 m_rowLabelWin
->Show( false );
8544 m_cornerLabelWin
->Show( false );
8546 else if ( m_rowLabelWidth
== 0 )
8548 m_rowLabelWin
->Show( true );
8549 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8552 m_rowLabelWidth
= width
;
8554 wxScrolledWindow::Refresh( true );
8559 void wxGrid::SetColLabelSize( int height
)
8561 height
= wxMax( height
, 0 );
8562 if ( height
!= m_colLabelHeight
)
8566 m_colLabelWin
->Show( false );
8567 m_cornerLabelWin
->Show( false );
8569 else if ( m_colLabelHeight
== 0 )
8571 m_colLabelWin
->Show( true );
8572 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8575 m_colLabelHeight
= height
;
8577 wxScrolledWindow::Refresh( true );
8582 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8584 if ( m_labelBackgroundColour
!= colour
)
8586 m_labelBackgroundColour
= colour
;
8587 m_rowLabelWin
->SetBackgroundColour( colour
);
8588 m_colLabelWin
->SetBackgroundColour( colour
);
8589 m_cornerLabelWin
->SetBackgroundColour( colour
);
8591 if ( !GetBatchCount() )
8593 m_rowLabelWin
->Refresh();
8594 m_colLabelWin
->Refresh();
8595 m_cornerLabelWin
->Refresh();
8600 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8602 if ( m_labelTextColour
!= colour
)
8604 m_labelTextColour
= colour
;
8605 if ( !GetBatchCount() )
8607 m_rowLabelWin
->Refresh();
8608 m_colLabelWin
->Refresh();
8613 void wxGrid::SetLabelFont( const wxFont
& font
)
8616 if ( !GetBatchCount() )
8618 m_rowLabelWin
->Refresh();
8619 m_colLabelWin
->Refresh();
8623 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8625 // allow old (incorrect) defs to be used
8628 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8629 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8630 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8635 case wxTOP
: vert
= wxALIGN_TOP
; break;
8636 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8637 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8640 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8642 m_rowLabelHorizAlign
= horiz
;
8645 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8647 m_rowLabelVertAlign
= vert
;
8650 if ( !GetBatchCount() )
8652 m_rowLabelWin
->Refresh();
8656 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8658 // allow old (incorrect) defs to be used
8661 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8662 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8663 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8668 case wxTOP
: vert
= wxALIGN_TOP
; break;
8669 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8670 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8673 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8675 m_colLabelHorizAlign
= horiz
;
8678 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8680 m_colLabelVertAlign
= vert
;
8683 if ( !GetBatchCount() )
8685 m_colLabelWin
->Refresh();
8689 // Note: under MSW, the default column label font must be changed because it
8690 // does not support vertical printing
8692 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8693 // pGrid->SetLabelFont(font);
8694 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8696 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8698 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8700 m_colLabelTextOrientation
= textOrientation
;
8703 if ( !GetBatchCount() )
8705 m_colLabelWin
->Refresh();
8709 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8713 m_table
->SetRowLabelValue( row
, s
);
8714 if ( !GetBatchCount() )
8716 wxRect rect
= CellToRect( row
, 0);
8717 if ( rect
.height
> 0 )
8719 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8721 rect
.width
= m_rowLabelWidth
;
8722 m_rowLabelWin
->Refresh( true, &rect
);
8728 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8732 m_table
->SetColLabelValue( col
, s
);
8733 if ( !GetBatchCount() )
8735 wxRect rect
= CellToRect( 0, col
);
8736 if ( rect
.width
> 0 )
8738 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8740 rect
.height
= m_colLabelHeight
;
8741 m_colLabelWin
->Refresh( true, &rect
);
8747 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8749 if ( m_gridLineColour
!= colour
)
8751 m_gridLineColour
= colour
;
8753 wxClientDC
dc( m_gridWin
);
8755 DrawAllGridLines( dc
, wxRegion() );
8760 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8762 if ( m_cellHighlightColour
!= colour
)
8764 m_cellHighlightColour
= colour
;
8766 wxClientDC
dc( m_gridWin
);
8768 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8769 DrawCellHighlight(dc
, attr
);
8774 void wxGrid::SetCellHighlightPenWidth(int width
)
8776 if (m_cellHighlightPenWidth
!= width
) {
8777 m_cellHighlightPenWidth
= width
;
8779 // Just redrawing the cell highlight is not enough since that won't
8780 // make any visible change if the the thickness is getting smaller.
8781 int row
= m_currentCellCoords
.GetRow();
8782 int col
= m_currentCellCoords
.GetCol();
8783 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8785 wxRect rect
= CellToRect(row
, col
);
8786 m_gridWin
->Refresh(true, &rect
);
8790 void wxGrid::SetCellHighlightROPenWidth(int width
)
8792 if (m_cellHighlightROPenWidth
!= width
) {
8793 m_cellHighlightROPenWidth
= width
;
8795 // Just redrawing the cell highlight is not enough since that won't
8796 // make any visible change if the the thickness is getting smaller.
8797 int row
= m_currentCellCoords
.GetRow();
8798 int col
= m_currentCellCoords
.GetCol();
8799 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8801 wxRect rect
= CellToRect(row
, col
);
8802 m_gridWin
->Refresh(true, &rect
);
8806 void wxGrid::EnableGridLines( bool enable
)
8808 if ( enable
!= m_gridLinesEnabled
)
8810 m_gridLinesEnabled
= enable
;
8812 if ( !GetBatchCount() )
8816 wxClientDC
dc( m_gridWin
);
8818 DrawAllGridLines( dc
, wxRegion() );
8822 m_gridWin
->Refresh();
8829 int wxGrid::GetDefaultRowSize()
8831 return m_defaultRowHeight
;
8834 int wxGrid::GetRowSize( int row
)
8836 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8838 return GetRowHeight(row
);
8841 int wxGrid::GetDefaultColSize()
8843 return m_defaultColWidth
;
8846 int wxGrid::GetColSize( int col
)
8848 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8850 return GetColWidth(col
);
8853 // ============================================================================
8854 // access to the grid attributes: each of them has a default value in the grid
8855 // itself and may be overidden on a per-cell basis
8856 // ============================================================================
8858 // ----------------------------------------------------------------------------
8859 // setting default attributes
8860 // ----------------------------------------------------------------------------
8862 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8864 m_defaultCellAttr
->SetBackgroundColour(col
);
8866 m_gridWin
->SetBackgroundColour(col
);
8870 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8872 m_defaultCellAttr
->SetTextColour(col
);
8875 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8877 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8880 void wxGrid::SetDefaultCellOverflow( bool allow
)
8882 m_defaultCellAttr
->SetOverflow(allow
);
8885 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8887 m_defaultCellAttr
->SetFont(font
);
8890 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8892 m_defaultCellAttr
->SetRenderer(renderer
);
8895 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8897 m_defaultCellAttr
->SetEditor(editor
);
8900 // ----------------------------------------------------------------------------
8901 // access to the default attrbiutes
8902 // ----------------------------------------------------------------------------
8904 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8906 return m_defaultCellAttr
->GetBackgroundColour();
8909 wxColour
wxGrid::GetDefaultCellTextColour()
8911 return m_defaultCellAttr
->GetTextColour();
8914 wxFont
wxGrid::GetDefaultCellFont()
8916 return m_defaultCellAttr
->GetFont();
8919 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8921 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8924 bool wxGrid::GetDefaultCellOverflow()
8926 return m_defaultCellAttr
->GetOverflow();
8929 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8931 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8934 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8936 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8939 // ----------------------------------------------------------------------------
8940 // access to cell attributes
8941 // ----------------------------------------------------------------------------
8943 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8945 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8946 wxColour colour
= attr
->GetBackgroundColour();
8951 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8953 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8954 wxColour colour
= attr
->GetTextColour();
8959 wxFont
wxGrid::GetCellFont( int row
, int col
)
8961 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8962 wxFont font
= attr
->GetFont();
8967 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8969 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8970 attr
->GetAlignment(horiz
, vert
);
8974 bool wxGrid::GetCellOverflow( int row
, int col
)
8976 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8977 bool allow
= attr
->GetOverflow();
8982 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8984 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8985 attr
->GetSize( num_rows
, num_cols
);
8989 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8991 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8992 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8998 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9000 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9001 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9007 bool wxGrid::IsReadOnly(int row
, int col
) const
9009 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9010 bool isReadOnly
= attr
->IsReadOnly();
9015 // ----------------------------------------------------------------------------
9016 // attribute support: cache, automatic provider creation, ...
9017 // ----------------------------------------------------------------------------
9019 bool wxGrid::CanHaveAttributes()
9026 return m_table
->CanHaveAttributes();
9029 void wxGrid::ClearAttrCache()
9031 if ( m_attrCache
.row
!= -1 )
9033 wxSafeDecRef(m_attrCache
.attr
);
9034 m_attrCache
.attr
= NULL
;
9035 m_attrCache
.row
= -1;
9039 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9043 wxGrid
*self
= (wxGrid
*)this; // const_cast
9045 self
->ClearAttrCache();
9046 self
->m_attrCache
.row
= row
;
9047 self
->m_attrCache
.col
= col
;
9048 self
->m_attrCache
.attr
= attr
;
9053 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9055 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9057 *attr
= m_attrCache
.attr
;
9058 wxSafeIncRef(m_attrCache
.attr
);
9060 #ifdef DEBUG_ATTR_CACHE
9061 gs_nAttrCacheHits
++;
9068 #ifdef DEBUG_ATTR_CACHE
9069 gs_nAttrCacheMisses
++;
9075 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9077 wxGridCellAttr
*attr
= NULL
;
9078 // Additional test to avoid looking at the cache e.g. for
9079 // wxNoCellCoords, as this will confuse memory management.
9082 if ( !LookupAttr(row
, col
, &attr
) )
9084 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9085 : (wxGridCellAttr
*)NULL
;
9086 CacheAttr(row
, col
, attr
);
9091 attr
->SetDefAttr(m_defaultCellAttr
);
9095 attr
= m_defaultCellAttr
;
9102 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9104 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9106 wxCHECK_MSG( m_table
, attr
,
9107 _T("we may only be called if CanHaveAttributes() returned true and then m_table should be !NULL") );
9109 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9112 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9114 // artificially inc the ref count to match DecRef() in caller
9116 m_table
->SetAttr(attr
, row
, col
);
9122 // ----------------------------------------------------------------------------
9123 // setting column attributes (wrappers around SetColAttr)
9124 // ----------------------------------------------------------------------------
9126 void wxGrid::SetColFormatBool(int col
)
9128 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9131 void wxGrid::SetColFormatNumber(int col
)
9133 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9136 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9138 wxString typeName
= wxGRID_VALUE_FLOAT
;
9139 if ( (width
!= -1) || (precision
!= -1) )
9141 typeName
<< _T(':') << width
<< _T(',') << precision
;
9144 SetColFormatCustom(col
, typeName
);
9147 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9149 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9151 attr
= new wxGridCellAttr
;
9152 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9153 attr
->SetRenderer(renderer
);
9155 SetColAttr(col
, attr
);
9159 // ----------------------------------------------------------------------------
9160 // setting cell attributes: this is forwarded to the table
9161 // ----------------------------------------------------------------------------
9163 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9165 if ( CanHaveAttributes() )
9167 m_table
->SetAttr(attr
, row
, col
);
9176 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9178 if ( CanHaveAttributes() )
9180 m_table
->SetRowAttr(attr
, row
);
9189 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9191 if ( CanHaveAttributes() )
9193 m_table
->SetColAttr(attr
, col
);
9202 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9204 if ( CanHaveAttributes() )
9206 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9207 attr
->SetBackgroundColour(colour
);
9212 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9214 if ( CanHaveAttributes() )
9216 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9217 attr
->SetTextColour(colour
);
9222 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9224 if ( CanHaveAttributes() )
9226 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9227 attr
->SetFont(font
);
9232 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9234 if ( CanHaveAttributes() )
9236 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9237 attr
->SetAlignment(horiz
, vert
);
9242 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9244 if ( CanHaveAttributes() )
9246 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9247 attr
->SetOverflow(allow
);
9252 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9254 if ( CanHaveAttributes() )
9256 int cell_rows
, cell_cols
;
9258 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9259 attr
->GetSize(&cell_rows
, &cell_cols
);
9260 attr
->SetSize(num_rows
, num_cols
);
9263 // Cannot set the size of a cell to 0 or negative values
9264 // While it is perfectly legal to do that, this function cannot
9265 // handle all the possibilies, do it by hand by getting the CellAttr.
9266 // You can only set the size of a cell to 1,1 or greater with this fn
9267 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9268 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9269 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9270 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9272 // if this was already a multicell then "turn off" the other cells first
9273 if ((cell_rows
> 1) || (cell_rows
> 1))
9276 for (j
=row
; j
<row
+cell_rows
; j
++)
9278 for (i
=col
; i
<col
+cell_cols
; i
++)
9280 if ((i
!= col
) || (j
!= row
))
9282 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9283 attr_stub
->SetSize( 1, 1 );
9284 attr_stub
->DecRef();
9290 // mark the cells that will be covered by this cell to
9291 // negative or zero values to point back at this cell
9292 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9295 for (j
=row
; j
<row
+num_rows
; j
++)
9297 for (i
=col
; i
<col
+num_cols
; i
++)
9299 if ((i
!= col
) || (j
!= row
))
9301 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9302 attr_stub
->SetSize( row
-j
, col
-i
);
9303 attr_stub
->DecRef();
9311 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9313 if ( CanHaveAttributes() )
9315 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9316 attr
->SetRenderer(renderer
);
9321 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9323 if ( CanHaveAttributes() )
9325 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9326 attr
->SetEditor(editor
);
9331 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9333 if ( CanHaveAttributes() )
9335 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9336 attr
->SetReadOnly(isReadOnly
);
9341 // ----------------------------------------------------------------------------
9342 // Data type registration
9343 // ----------------------------------------------------------------------------
9345 void wxGrid::RegisterDataType(const wxString
& typeName
,
9346 wxGridCellRenderer
* renderer
,
9347 wxGridCellEditor
* editor
)
9349 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9353 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9355 wxString typeName
= m_table
->GetTypeName(row
, col
);
9356 return GetDefaultEditorForType(typeName
);
9359 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9361 wxString typeName
= m_table
->GetTypeName(row
, col
);
9362 return GetDefaultRendererForType(typeName
);
9366 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9368 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9369 if ( index
== wxNOT_FOUND
)
9371 wxFAIL_MSG(wxT("Unknown data type name"));
9376 return m_typeRegistry
->GetEditor(index
);
9380 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9382 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9383 if ( index
== wxNOT_FOUND
)
9385 wxFAIL_MSG(wxT("Unknown data type name"));
9390 return m_typeRegistry
->GetRenderer(index
);
9394 // ----------------------------------------------------------------------------
9396 // ----------------------------------------------------------------------------
9398 void wxGrid::EnableDragRowSize( bool enable
)
9400 m_canDragRowSize
= enable
;
9404 void wxGrid::EnableDragColSize( bool enable
)
9406 m_canDragColSize
= enable
;
9409 void wxGrid::EnableDragGridSize( bool enable
)
9411 m_canDragGridSize
= enable
;
9414 void wxGrid::EnableDragCell( bool enable
)
9416 m_canDragCell
= enable
;
9419 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9421 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9423 if ( resizeExistingRows
)
9425 // since we are resizing all rows to the default row size,
9426 // we can simply clear the row heights and row bottoms
9427 // arrays (which also allows us to take advantage of
9428 // some speed optimisations)
9429 m_rowHeights
.Empty();
9430 m_rowBottoms
.Empty();
9431 if ( !GetBatchCount() )
9436 void wxGrid::SetRowSize( int row
, int height
)
9438 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9440 // See comment in SetColSize
9441 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9443 if ( m_rowHeights
.IsEmpty() )
9445 // need to really create the array
9449 int h
= wxMax( 0, height
);
9450 int diff
= h
- m_rowHeights
[row
];
9452 m_rowHeights
[row
] = h
;
9454 for ( i
= row
; i
< m_numRows
; i
++ )
9456 m_rowBottoms
[i
] += diff
;
9458 if ( !GetBatchCount() )
9462 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9464 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9466 if ( resizeExistingCols
)
9468 // since we are resizing all columns to the default column size,
9469 // we can simply clear the col widths and col rights
9470 // arrays (which also allows us to take advantage of
9471 // some speed optimisations)
9472 m_colWidths
.Empty();
9473 m_colRights
.Empty();
9474 if ( !GetBatchCount() )
9479 void wxGrid::SetColSize( int col
, int width
)
9481 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9483 // should we check that it's bigger than GetColMinimalWidth(col) here?
9485 // No, because it is reasonable to assume the library user know's
9486 // what he is doing. However whe should test against the weaker
9487 // constariant of minimalAcceptableWidth, as this breaks rendering
9489 // This test then fixes sf.net bug #645734
9491 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9493 if ( m_colWidths
.IsEmpty() )
9495 // need to really create the array
9499 // if < 0 calc new width from label
9503 wxArrayString lines
;
9504 wxClientDC
dc(m_colLabelWin
);
9505 dc
.SetFont(GetLabelFont());
9506 StringToLines(GetColLabelValue(col
), lines
);
9507 GetTextBoxSize(dc
, lines
, &w
, &h
);
9510 int w
= wxMax( 0, width
);
9511 int diff
= w
- m_colWidths
[col
];
9512 m_colWidths
[col
] = w
;
9515 for ( i
= col
; i
< m_numCols
; i
++ )
9517 m_colRights
[i
] += diff
;
9519 if ( !GetBatchCount() )
9524 void wxGrid::SetColMinimalWidth( int col
, int width
)
9526 if (width
> GetColMinimalAcceptableWidth()) {
9527 m_colMinWidths
[col
] = width
;
9531 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9533 if (width
> GetRowMinimalAcceptableHeight()) {
9534 m_rowMinHeights
[row
] = width
;
9538 int wxGrid::GetColMinimalWidth(int col
) const
9540 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(col
);
9541 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9544 int wxGrid::GetRowMinimalHeight(int row
) const
9546 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(row
);
9547 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9550 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9552 // We do allow a width of 0 since this gives us
9553 // an easy way to temporarily hidding columns.
9556 m_minAcceptableColWidth
= width
;
9559 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9561 // We do allow a height of 0 since this gives us
9562 // an easy way to temporarily hidding rows.
9565 m_minAcceptableRowHeight
= height
;
9568 int wxGrid::GetColMinimalAcceptableWidth() const
9570 return m_minAcceptableColWidth
;
9573 int wxGrid::GetRowMinimalAcceptableHeight() const
9575 return m_minAcceptableRowHeight
;
9578 // ----------------------------------------------------------------------------
9580 // ----------------------------------------------------------------------------
9582 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9584 wxClientDC
dc(m_gridWin
);
9586 //Cancel editting of cell
9587 HideCellEditControl();
9588 SaveEditControlValue();
9590 // init both of them to avoid compiler warnings, even if weo nly need one
9598 wxCoord extent
, extentMax
= 0;
9599 int max
= column
? m_numRows
: m_numCols
;
9600 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9607 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9608 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9611 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9612 extent
= column
? size
.x
: size
.y
;
9613 if ( extent
> extentMax
)
9624 // now also compare with the column label extent
9626 dc
.SetFont( GetLabelFont() );
9630 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9631 if( GetColLabelTextOrientation() == wxVERTICAL
)
9635 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9637 extent
= column
? w
: h
;
9638 if ( extent
> extentMax
)
9645 // empty column - give default extent (notice that if extentMax is less
9646 // than default extent but != 0, it's ok)
9647 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9653 // leave some space around text
9664 SetColSize(col
, extentMax
);
9665 if ( !GetBatchCount() )
9668 m_gridWin
->GetClientSize( &cw
, &ch
);
9669 wxRect
rect ( CellToRect( 0, col
) );
9671 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9672 rect
.width
= cw
- rect
.x
;
9673 rect
.height
= m_colLabelHeight
;
9674 m_colLabelWin
->Refresh( true, &rect
);
9679 SetRowSize(row
, extentMax
);
9680 if ( !GetBatchCount() )
9683 m_gridWin
->GetClientSize( &cw
, &ch
);
9684 wxRect
rect ( CellToRect( row
, 0 ) );
9686 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9687 rect
.width
= m_rowLabelWidth
;
9688 rect
.height
= ch
- rect
.y
;
9689 m_rowLabelWin
->Refresh( true, &rect
);
9695 SetColMinimalWidth(col
, extentMax
);
9697 SetRowMinimalHeight(row
, extentMax
);
9701 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9703 int width
= m_rowLabelWidth
;
9708 for ( int col
= 0; col
< m_numCols
; col
++ )
9712 AutoSizeColumn(col
, setAsMin
);
9715 width
+= GetColWidth(col
);
9724 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9726 int height
= m_colLabelHeight
;
9731 for ( int row
= 0; row
< m_numRows
; row
++ )
9735 AutoSizeRow(row
, setAsMin
);
9738 height
+= GetRowHeight(row
);
9747 void wxGrid::AutoSize()
9751 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9753 // round up the size to a multiple of scroll step - this ensures that we
9754 // won't get the scrollbars if we're sized exactly to this width
9755 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9757 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9758 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9760 // distribute the extra space between the columns/rows to avoid having
9761 // extra white space
9763 // Remove the extra m_extraWidth + 1 added above
9764 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9765 if ( diff
&& m_numCols
)
9767 // try to resize the columns uniformly
9768 wxCoord diffPerCol
= diff
/ m_numCols
;
9771 for ( int col
= 0; col
< m_numCols
; col
++ )
9773 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9777 // add remaining amount to the last columns
9778 diff
-= diffPerCol
* m_numCols
;
9781 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9783 SetColSize(col
, GetColWidth(col
) + 1);
9789 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9790 if ( diff
&& m_numRows
)
9792 // try to resize the columns uniformly
9793 wxCoord diffPerRow
= diff
/ m_numRows
;
9796 for ( int row
= 0; row
< m_numRows
; row
++ )
9798 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9802 // add remaining amount to the last rows
9803 diff
-= diffPerRow
* m_numRows
;
9806 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9808 SetRowSize(row
, GetRowHeight(row
) + 1);
9815 SetClientSize(sizeFit
);
9818 void wxGrid::AutoSizeRowLabelSize( int row
)
9820 wxArrayString lines
;
9823 // Hide the edit control, so it
9824 // won't interfer with drag-shrinking.
9825 if( IsCellEditControlShown() )
9827 HideCellEditControl();
9828 SaveEditControlValue();
9831 // autosize row height depending on label text
9832 StringToLines( GetRowLabelValue( row
), lines
);
9833 wxClientDC
dc( m_rowLabelWin
);
9834 GetTextBoxSize( dc
, lines
, &w
, &h
);
9835 if( h
< m_defaultRowHeight
)
9836 h
= m_defaultRowHeight
;
9841 void wxGrid::AutoSizeColLabelSize( int col
)
9843 wxArrayString lines
;
9846 // Hide the edit control, so it
9847 // won't interfer with drag-shrinking.
9848 if( IsCellEditControlShown() )
9850 HideCellEditControl();
9851 SaveEditControlValue();
9854 // autosize column width depending on label text
9855 StringToLines( GetColLabelValue( col
), lines
);
9856 wxClientDC
dc( m_colLabelWin
);
9857 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9858 GetTextBoxSize( dc
, lines
, &w
, &h
);
9860 GetTextBoxSize( dc
, lines
, &h
, &w
);
9861 if( w
< m_defaultColWidth
)
9862 w
= m_defaultColWidth
;
9867 wxSize
wxGrid::DoGetBestSize() const
9869 // don't set sizes, only calculate them
9870 wxGrid
*self
= (wxGrid
*)this; // const_cast
9873 width
= self
->SetOrCalcColumnSizes(true);
9874 height
= self
->SetOrCalcRowSizes(true);
9876 int maxwidth
, maxheight
;
9877 wxDisplaySize( & maxwidth
, & maxheight
);
9879 if ( width
> maxwidth
) width
= maxwidth
;
9880 if ( height
> maxheight
) height
= maxheight
;
9882 return wxSize( width
, height
);
9891 wxPen
& wxGrid::GetDividerPen() const
9896 // ----------------------------------------------------------------------------
9897 // cell value accessor functions
9898 // ----------------------------------------------------------------------------
9900 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9904 m_table
->SetValue( row
, col
, s
);
9905 if ( !GetBatchCount() )
9908 wxRect
rect( CellToRect( row
, col
) );
9910 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9911 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9912 m_gridWin
->Refresh( false, &rect
);
9915 if ( m_currentCellCoords
.GetRow() == row
&&
9916 m_currentCellCoords
.GetCol() == col
&&
9917 IsCellEditControlShown())
9918 // Note: If we are using IsCellEditControlEnabled,
9919 // this interacts badly with calling SetCellValue from
9920 // an EVT_GRID_CELL_CHANGE handler.
9922 HideCellEditControl();
9923 ShowCellEditControl(); // will reread data from table
9930 // ------ Block, row and col selection
9933 void wxGrid::SelectRow( int row
, bool addToSelected
)
9935 if ( IsSelection() && !addToSelected
)
9939 m_selection
->SelectRow( row
, false, addToSelected
);
9943 void wxGrid::SelectCol( int col
, bool addToSelected
)
9945 if ( IsSelection() && !addToSelected
)
9949 m_selection
->SelectCol( col
, false, addToSelected
);
9953 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9954 bool addToSelected
)
9956 if ( IsSelection() && !addToSelected
)
9960 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9961 false, addToSelected
);
9965 void wxGrid::SelectAll()
9967 if ( m_numRows
> 0 && m_numCols
> 0 )
9970 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9975 // ------ Cell, row and col deselection
9978 void wxGrid::DeselectRow( int row
)
9983 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9985 if ( m_selection
->IsInSelection(row
, 0 ) )
9986 m_selection
->ToggleCellSelection( row
, 0);
9990 int nCols
= GetNumberCols();
9991 for ( int i
= 0; i
< nCols
; i
++ )
9993 if ( m_selection
->IsInSelection(row
, i
) )
9994 m_selection
->ToggleCellSelection( row
, i
);
9999 void wxGrid::DeselectCol( int col
)
10001 if ( !m_selection
)
10004 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10006 if ( m_selection
->IsInSelection(0, col
) )
10007 m_selection
->ToggleCellSelection( 0, col
);
10011 int nRows
= GetNumberRows();
10012 for ( int i
= 0; i
< nRows
; i
++ )
10014 if ( m_selection
->IsInSelection(i
, col
) )
10015 m_selection
->ToggleCellSelection(i
, col
);
10020 void wxGrid::DeselectCell( int row
, int col
)
10022 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10023 m_selection
->ToggleCellSelection(row
, col
);
10026 bool wxGrid::IsSelection()
10028 return ( m_selection
&& (m_selection
->IsSelection() ||
10029 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10030 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10033 bool wxGrid::IsInSelection( int row
, int col
) const
10035 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10036 ( row
>= m_selectingTopLeft
.GetRow() &&
10037 col
>= m_selectingTopLeft
.GetCol() &&
10038 row
<= m_selectingBottomRight
.GetRow() &&
10039 col
<= m_selectingBottomRight
.GetCol() )) );
10042 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10044 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10045 return m_selection
->m_cellSelection
;
10047 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10049 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10050 return m_selection
->m_blockSelectionTopLeft
;
10052 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10054 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10055 return m_selection
->m_blockSelectionBottomRight
;
10057 wxArrayInt
wxGrid::GetSelectedRows() const
10059 if (!m_selection
) { wxArrayInt a
; return a
; }
10060 return m_selection
->m_rowSelection
;
10062 wxArrayInt
wxGrid::GetSelectedCols() const
10064 if (!m_selection
) { wxArrayInt a
; return a
; }
10065 return m_selection
->m_colSelection
;
10069 void wxGrid::ClearSelection()
10071 m_selectingTopLeft
= wxGridNoCellCoords
;
10072 m_selectingBottomRight
= wxGridNoCellCoords
;
10074 m_selection
->ClearSelection();
10078 // This function returns the rectangle that encloses the given block
10079 // in device coords clipped to the client size of the grid window.
10081 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10082 const wxGridCellCoords
&bottomRight
)
10084 wxRect
rect( wxGridNoCellRect
);
10087 cellRect
= CellToRect( topLeft
);
10088 if ( cellRect
!= wxGridNoCellRect
)
10094 rect
= wxRect( 0, 0, 0, 0 );
10097 cellRect
= CellToRect( bottomRight
);
10098 if ( cellRect
!= wxGridNoCellRect
)
10104 return wxGridNoCellRect
;
10108 int left
= rect
.GetLeft();
10109 int top
= rect
.GetTop();
10110 int right
= rect
.GetRight();
10111 int bottom
= rect
.GetBottom();
10113 int leftCol
= topLeft
.GetCol();
10114 int topRow
= topLeft
.GetRow();
10115 int rightCol
= bottomRight
.GetCol();
10116 int bottomRow
= bottomRight
.GetRow();
10134 topRow
= bottomRow
;
10139 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10141 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10143 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10145 cellRect
= CellToRect( j
, i
);
10147 if (cellRect
.x
< left
)
10149 if (cellRect
.y
< top
)
10151 if (cellRect
.x
+ cellRect
.width
> right
)
10152 right
= cellRect
.x
+ cellRect
.width
;
10153 if (cellRect
.y
+ cellRect
.height
> bottom
)
10154 bottom
= cellRect
.y
+ cellRect
.height
;
10156 else i
= rightCol
; // jump over inner cells.
10160 // convert to scrolled coords
10162 CalcScrolledPosition( left
, top
, &left
, &top
);
10163 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10166 m_gridWin
->GetClientSize( &cw
, &ch
);
10168 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10169 return wxRect( 0, 0, 0, 0);
10171 rect
.SetLeft( wxMax(0, left
) );
10172 rect
.SetTop( wxMax(0, top
) );
10173 rect
.SetRight( wxMin(cw
, right
) );
10174 rect
.SetBottom( wxMin(ch
, bottom
) );
10180 // ------ Grid event classes
10183 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10185 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10186 int row
, int col
, int x
, int y
, bool sel
,
10187 bool control
, bool shift
, bool alt
, bool meta
)
10188 : wxNotifyEvent( type
, id
)
10195 m_control
= control
;
10200 SetEventObject(obj
);
10204 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10206 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10207 int rowOrCol
, int x
, int y
,
10208 bool control
, bool shift
, bool alt
, bool meta
)
10209 : wxNotifyEvent( type
, id
)
10211 m_rowOrCol
= rowOrCol
;
10214 m_control
= control
;
10219 SetEventObject(obj
);
10223 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10225 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10226 const wxGridCellCoords
& topLeft
,
10227 const wxGridCellCoords
& bottomRight
,
10228 bool sel
, bool control
,
10229 bool shift
, bool alt
, bool meta
)
10230 : wxNotifyEvent( type
, id
)
10232 m_topLeft
= topLeft
;
10233 m_bottomRight
= bottomRight
;
10235 m_control
= control
;
10240 SetEventObject(obj
);
10244 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10246 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10247 wxObject
* obj
, int row
,
10248 int col
, wxControl
* ctrl
)
10249 : wxCommandEvent(type
, id
)
10251 SetEventObject(obj
);
10257 #endif // wxUSE_GRID