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;
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 static inline int GetScrollX(int x
)
423 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
426 static inline int GetScrollY(int y
)
428 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
432 // ============================================================================
434 // ============================================================================
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 wxGridCellEditor::wxGridCellEditor()
447 wxGridCellEditor::~wxGridCellEditor()
452 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
453 wxWindowID
WXUNUSED(id
),
454 wxEvtHandler
* evtHandler
)
457 m_control
->PushEventHandler(evtHandler
);
460 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
461 wxGridCellAttr
*attr
)
463 // erase the background because we might not fill the cell
464 wxClientDC
dc(m_control
->GetParent());
465 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
467 gridWindow
->GetOwner()->PrepareDC(dc
);
469 dc
.SetPen(*wxTRANSPARENT_PEN
);
470 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
471 dc
.DrawRectangle(rectCell
);
473 // redraw the control we just painted over
474 m_control
->Refresh();
477 void wxGridCellEditor::Destroy()
481 m_control
->PopEventHandler(true /* delete it*/);
483 m_control
->Destroy();
488 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
490 wxASSERT_MSG(m_control
,
491 wxT("The wxGridCellEditor must be Created first!"));
492 m_control
->Show(show
);
496 // set the colours/fonts if we have any
499 m_colFgOld
= m_control
->GetForegroundColour();
500 m_control
->SetForegroundColour(attr
->GetTextColour());
502 m_colBgOld
= m_control
->GetBackgroundColour();
503 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
505 // Workaround for GTK+1 font setting problem on some platforms
506 #if !defined(__WXGTK__) || defined(__WXGTK20__)
507 m_fontOld
= m_control
->GetFont();
508 m_control
->SetFont(attr
->GetFont());
510 // can't do anything more in the base class version, the other
511 // attributes may only be used by the derived classes
516 // restore the standard colours fonts
517 if ( m_colFgOld
.Ok() )
519 m_control
->SetForegroundColour(m_colFgOld
);
520 m_colFgOld
= wxNullColour
;
523 if ( m_colBgOld
.Ok() )
525 m_control
->SetBackgroundColour(m_colBgOld
);
526 m_colBgOld
= wxNullColour
;
528 // Workaround for GTK+1 font setting problem on some platforms
529 #if !defined(__WXGTK__) || defined(__WXGTK20__)
530 if ( m_fontOld
.Ok() )
532 m_control
->SetFont(m_fontOld
);
533 m_fontOld
= wxNullFont
;
539 void wxGridCellEditor::SetSize(const wxRect
& rect
)
541 wxASSERT_MSG(m_control
,
542 wxT("The wxGridCellEditor must be Created first!"));
543 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
546 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
551 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
553 // accept the simple key presses, not anything with Ctrl/Alt/Meta
554 return !(event
.ControlDown() || event
.AltDown());
557 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
562 void wxGridCellEditor::StartingClick()
568 // ----------------------------------------------------------------------------
569 // wxGridCellTextEditor
570 // ----------------------------------------------------------------------------
572 wxGridCellTextEditor::wxGridCellTextEditor()
577 void wxGridCellTextEditor::Create(wxWindow
* parent
,
579 wxEvtHandler
* evtHandler
)
581 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
582 wxDefaultPosition
, wxDefaultSize
583 #if defined(__WXMSW__)
584 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
588 // set max length allowed in the textctrl, if the parameter was set
591 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
594 wxGridCellEditor::Create(parent
, id
, evtHandler
);
597 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
598 wxGridCellAttr
* WXUNUSED(attr
))
600 // as we fill the entire client area, don't do anything here to minimize
604 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
606 wxRect
rect(rectOrig
);
608 // Make the edit control large enough to allow for internal
611 // TODO: remove this if the text ctrl sizing is improved esp. for
614 #if defined(__WXGTK__)
623 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
625 // MB: treat MSW separately here otherwise the caret doesn't show
626 // when the editor is in the first row.
627 #if defined(__WXMSW__)
630 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
633 #if defined(__WXMOTIF__)
637 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
638 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
639 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
640 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
643 wxGridCellEditor::SetSize(rect
);
646 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
648 wxASSERT_MSG(m_control
,
649 wxT("The wxGridCellEditor must be Created first!"));
651 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
653 DoBeginEdit(m_startValue
);
656 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
658 Text()->SetValue(startValue
);
659 Text()->SetInsertionPointEnd();
660 Text()->SetSelection(-1,-1);
664 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
667 wxASSERT_MSG(m_control
,
668 wxT("The wxGridCellEditor must be Created first!"));
670 bool changed
= false;
671 wxString value
= Text()->GetValue();
672 if (value
!= m_startValue
)
676 grid
->GetTable()->SetValue(row
, col
, value
);
678 m_startValue
= wxEmptyString
;
679 // No point in setting the text of the hidden control
680 //Text()->SetValue(m_startValue);
686 void wxGridCellTextEditor::Reset()
688 wxASSERT_MSG(m_control
,
689 wxT("The wxGridCellEditor must be Created first!"));
691 DoReset(m_startValue
);
694 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
696 Text()->SetValue(startValue
);
697 Text()->SetInsertionPointEnd();
700 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
702 if ( wxGridCellEditor::IsAcceptedKey(event
) )
704 int keycode
= event
.GetKeyCode();
718 case WXK_NUMPAD_MULTIPLY
:
722 case WXK_NUMPAD_SUBTRACT
:
724 case WXK_NUMPAD_DECIMAL
:
726 case WXK_NUMPAD_DIVIDE
:
730 // accept 8 bit chars too if isprint() agrees
731 if ( (keycode
< 255) && (wxIsprint(keycode
)) )
739 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
741 if ( !Text()->EmulateKeyPress(event
) )
747 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
748 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
750 #if defined(__WXMOTIF__) || defined(__WXGTK__)
751 // wxMotif needs a little extra help...
752 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
753 wxString
s( Text()->GetValue() );
754 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
756 Text()->SetInsertionPoint( pos
);
758 // the other ports can handle a Return key press
764 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
774 if ( !params
.ToLong(&tmp
) )
776 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
780 m_maxChars
= (size_t)tmp
;
785 // return the value in the text control
786 wxString
wxGridCellTextEditor::GetValue() const
788 return Text()->GetValue();
791 // ----------------------------------------------------------------------------
792 // wxGridCellNumberEditor
793 // ----------------------------------------------------------------------------
795 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
801 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
803 wxEvtHandler
* evtHandler
)
808 // create a spin ctrl
809 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
810 wxDefaultPosition
, wxDefaultSize
,
814 wxGridCellEditor::Create(parent
, id
, evtHandler
);
819 // just a text control
820 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
823 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
824 #endif // wxUSE_VALIDATORS
828 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
830 // first get the value
831 wxGridTableBase
*table
= grid
->GetTable();
832 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
834 m_valueOld
= table
->GetValueAsLong(row
, col
);
839 wxString sValue
= table
->GetValue(row
, col
);
840 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
842 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
850 Spin()->SetValue((int)m_valueOld
);
856 DoBeginEdit(GetString());
860 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
870 value
= Spin()->GetValue();
871 changed
= value
!= m_valueOld
;
873 text
= wxString::Format(wxT("%ld"), value
);
878 text
= Text()->GetValue();
879 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
884 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
885 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
887 grid
->GetTable()->SetValue(row
, col
, text
);
893 void wxGridCellNumberEditor::Reset()
898 Spin()->SetValue((int)m_valueOld
);
903 DoReset(GetString());
907 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
909 if ( wxGridCellEditor::IsAcceptedKey(event
) )
911 int keycode
= event
.GetKeyCode();
927 case WXK_NUMPAD_SUBTRACT
:
933 if ( (keycode
< 128) && wxIsdigit(keycode
) )
941 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
945 int keycode
= event
.GetKeyCode();
946 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
947 || keycode
== WXK_NUMPAD0
948 || keycode
== WXK_NUMPAD1
949 || keycode
== WXK_NUMPAD2
950 || keycode
== WXK_NUMPAD3
951 || keycode
== WXK_NUMPAD4
952 || keycode
== WXK_NUMPAD5
953 || keycode
== WXK_NUMPAD6
954 || keycode
== WXK_NUMPAD7
955 || keycode
== WXK_NUMPAD8
956 || keycode
== WXK_NUMPAD9
957 || keycode
== WXK_ADD
958 || keycode
== WXK_NUMPAD_ADD
959 || keycode
== WXK_SUBTRACT
960 || keycode
== WXK_NUMPAD_SUBTRACT
)
962 wxGridCellTextEditor::StartingKey(event
);
972 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
983 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
987 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
991 // skip the error message below
996 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1000 // return the value in the spin control if it is there (the text control otherwise)
1001 wxString
wxGridCellNumberEditor::GetValue() const
1008 long value
= Spin()->GetValue();
1009 s
.Printf(wxT("%ld"), value
);
1014 s
= Text()->GetValue();
1020 // ----------------------------------------------------------------------------
1021 // wxGridCellFloatEditor
1022 // ----------------------------------------------------------------------------
1024 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1027 m_precision
= precision
;
1030 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1032 wxEvtHandler
* evtHandler
)
1034 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1036 #if wxUSE_VALIDATORS
1037 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1038 #endif // wxUSE_VALIDATORS
1041 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1043 // first get the value
1044 wxGridTableBase
*table
= grid
->GetTable();
1045 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1047 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1052 wxString sValue
= table
->GetValue(row
, col
);
1053 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1055 wxFAIL_MSG( _T("this cell doesn't have float value") );
1060 DoBeginEdit(GetString());
1063 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1067 wxString
text(Text()->GetValue());
1069 if ( (text
.empty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1071 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1072 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1074 grid
->GetTable()->SetValue(row
, col
, text
);
1081 void wxGridCellFloatEditor::Reset()
1083 DoReset(GetString());
1086 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1088 int keycode
= event
.GetKeyCode();
1090 tmpbuf
[0] = (char) keycode
;
1092 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1093 bool is_decimal_point
= ( strbuf
==
1094 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1095 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1097 || keycode
== WXK_NUMPAD0
1098 || keycode
== WXK_NUMPAD1
1099 || keycode
== WXK_NUMPAD2
1100 || keycode
== WXK_NUMPAD3
1101 || keycode
== WXK_NUMPAD4
1102 || keycode
== WXK_NUMPAD5
1103 || keycode
== WXK_NUMPAD6
1104 || keycode
== WXK_NUMPAD7
1105 || keycode
== WXK_NUMPAD8
1106 || keycode
== WXK_NUMPAD9
1107 || keycode
== WXK_ADD
1108 || keycode
== WXK_NUMPAD_ADD
1109 || keycode
== WXK_SUBTRACT
1110 || keycode
== WXK_NUMPAD_SUBTRACT
)
1112 wxGridCellTextEditor::StartingKey(event
);
1114 // skip Skip() below
1121 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1132 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1136 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1138 m_precision
= (int)tmp
;
1140 // skip the error message below
1145 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1149 wxString
wxGridCellFloatEditor::GetString() const
1152 if ( m_width
== -1 )
1154 // default width/precision
1157 else if ( m_precision
== -1 )
1159 // default precision
1160 fmt
.Printf(_T("%%%d.f"), m_width
);
1164 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1167 return wxString::Format(fmt
, m_valueOld
);
1170 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1172 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1174 int keycode
= event
.GetKeyCode();
1188 case WXK_NUMPAD_ADD
:
1190 case WXK_NUMPAD_SUBTRACT
:
1192 case WXK_NUMPAD_DECIMAL
:
1197 // additionally accept 'e' as in '1e+6', also '-', '+', and '.'
1199 tmpbuf
[0] = (char) keycode
;
1201 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1202 bool is_decimal_point
=
1203 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1204 wxLOCALE_CAT_NUMBER
) );
1205 if ( (keycode
< 128) &&
1206 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1207 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1216 #endif // wxUSE_TEXTCTRL
1220 // ----------------------------------------------------------------------------
1221 // wxGridCellBoolEditor
1222 // ----------------------------------------------------------------------------
1224 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1226 wxEvtHandler
* evtHandler
)
1228 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1229 wxDefaultPosition
, wxDefaultSize
,
1232 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1235 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1237 bool resize
= false;
1238 wxSize size
= m_control
->GetSize();
1239 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1241 // check if the checkbox is not too big/small for this cell
1242 wxSize sizeBest
= m_control
->GetBestSize();
1243 if ( !(size
== sizeBest
) )
1245 // reset to default size if it had been made smaller
1251 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1253 // leave 1 pixel margin
1254 size
.x
= size
.y
= minSize
- 2;
1261 m_control
->SetSize(size
);
1264 // position it in the centre of the rectangle (TODO: support alignment?)
1266 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1267 // the checkbox without label still has some space to the right in wxGTK,
1268 // so shift it to the right
1270 #elif defined(__WXMSW__)
1271 // here too, but in other way
1276 int hAlign
= wxALIGN_CENTRE
;
1277 int vAlign
= wxALIGN_CENTRE
;
1279 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1282 if (hAlign
== wxALIGN_LEFT
)
1288 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1290 else if (hAlign
== wxALIGN_RIGHT
)
1292 x
= r
.x
+ r
.width
- size
.x
- 2;
1293 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1295 else if (hAlign
== wxALIGN_CENTRE
)
1297 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1298 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1301 m_control
->Move(x
, y
);
1304 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1306 m_control
->Show(show
);
1310 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1311 CBox()->SetBackgroundColour(colBg
);
1315 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1317 wxASSERT_MSG(m_control
,
1318 wxT("The wxGridCellEditor must be Created first!"));
1320 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1321 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1324 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1325 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1327 CBox()->SetValue(m_startValue
);
1331 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1334 wxASSERT_MSG(m_control
,
1335 wxT("The wxGridCellEditor must be Created first!"));
1337 bool changed
= false;
1338 bool value
= CBox()->GetValue();
1339 if ( value
!= m_startValue
)
1344 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1345 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1347 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1353 void wxGridCellBoolEditor::Reset()
1355 wxASSERT_MSG(m_control
,
1356 wxT("The wxGridCellEditor must be Created first!"));
1358 CBox()->SetValue(m_startValue
);
1361 void wxGridCellBoolEditor::StartingClick()
1363 CBox()->SetValue(!CBox()->GetValue());
1366 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1368 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1370 int keycode
= event
.GetKeyCode();
1374 case WXK_NUMPAD_MULTIPLY
:
1376 case WXK_NUMPAD_ADD
:
1378 case WXK_NUMPAD_SUBTRACT
:
1389 // return the value as "1" for true and the empty string for false
1390 wxString
wxGridCellBoolEditor::GetValue() const
1392 bool bSet
= CBox()->GetValue();
1393 return bSet
? _T("1") : wxEmptyString
;
1396 #endif // wxUSE_CHECKBOX
1400 // ----------------------------------------------------------------------------
1401 // wxGridCellChoiceEditor
1402 // ----------------------------------------------------------------------------
1404 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1406 : m_choices(choices
),
1407 m_allowOthers(allowOthers
) { }
1409 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1410 const wxString choices
[],
1412 : m_allowOthers(allowOthers
)
1416 m_choices
.Alloc(count
);
1417 for ( size_t n
= 0; n
< count
; n
++ )
1419 m_choices
.Add(choices
[n
]);
1424 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1426 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1427 editor
->m_allowOthers
= m_allowOthers
;
1428 editor
->m_choices
= m_choices
;
1433 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1435 wxEvtHandler
* evtHandler
)
1437 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1438 wxDefaultPosition
, wxDefaultSize
,
1440 m_allowOthers
? 0 : wxCB_READONLY
);
1442 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1445 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1446 wxGridCellAttr
* attr
)
1448 // as we fill the entire client area, don't do anything here to minimize
1451 // TODO: It doesn't actually fill the client area since the height of a
1452 // combo always defaults to the standard... Until someone has time to
1453 // figure out the right rectangle to paint, just do it the normal way...
1454 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1457 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1459 wxASSERT_MSG(m_control
,
1460 wxT("The wxGridCellEditor must be Created first!"));
1462 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1465 Combo()->SetValue(m_startValue
);
1468 // find the right position, or default to the first if not found
1469 int pos
= Combo()->FindString(m_startValue
);
1472 Combo()->SetSelection(pos
);
1474 Combo()->SetInsertionPointEnd();
1475 Combo()->SetFocus();
1478 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1481 wxString value
= Combo()->GetValue();
1482 if ( value
== m_startValue
)
1485 grid
->GetTable()->SetValue(row
, col
, value
);
1490 void wxGridCellChoiceEditor::Reset()
1492 Combo()->SetValue(m_startValue
);
1493 Combo()->SetInsertionPointEnd();
1496 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1506 wxStringTokenizer
tk(params
, _T(','));
1507 while ( tk
.HasMoreTokens() )
1509 m_choices
.Add(tk
.GetNextToken());
1513 // return the value in the text control
1514 wxString
wxGridCellChoiceEditor::GetValue() const
1516 return Combo()->GetValue();
1519 #endif // wxUSE_COMBOBOX
1521 // ----------------------------------------------------------------------------
1522 // wxGridCellEditorEvtHandler
1523 // ----------------------------------------------------------------------------
1525 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1527 switch ( event
.GetKeyCode() )
1531 m_grid
->DisableCellEditControl();
1535 m_grid
->GetEventHandler()->ProcessEvent( event
);
1539 case WXK_NUMPAD_ENTER
:
1540 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1541 m_editor
->HandleReturn(event
);
1550 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1552 switch ( event
.GetKeyCode() )
1557 case WXK_NUMPAD_ENTER
:
1565 // ----------------------------------------------------------------------------
1566 // wxGridCellWorker is an (almost) empty common base class for
1567 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1568 // ----------------------------------------------------------------------------
1570 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1575 wxGridCellWorker::~wxGridCellWorker()
1579 // ============================================================================
1581 // ============================================================================
1583 // ----------------------------------------------------------------------------
1584 // wxGridCellRenderer
1585 // ----------------------------------------------------------------------------
1587 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1588 wxGridCellAttr
& attr
,
1591 int WXUNUSED(row
), int WXUNUSED(col
),
1594 dc
.SetBackgroundMode( wxSOLID
);
1596 // grey out fields if the grid is disabled
1597 if( grid
.IsEnabled() )
1601 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1605 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1610 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1613 dc
.SetPen( *wxTRANSPARENT_PEN
);
1614 dc
.DrawRectangle(rect
);
1617 // ----------------------------------------------------------------------------
1618 // wxGridCellStringRenderer
1619 // ----------------------------------------------------------------------------
1621 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1622 wxGridCellAttr
& attr
,
1626 dc
.SetBackgroundMode( wxTRANSPARENT
);
1628 // TODO some special colours for attr.IsReadOnly() case?
1630 // different coloured text when the grid is disabled
1631 if( grid
.IsEnabled() )
1635 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1636 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1640 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1641 dc
.SetTextForeground( attr
.GetTextColour() );
1646 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1647 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1650 dc
.SetFont( attr
.GetFont() );
1653 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1655 const wxString
& text
)
1657 wxCoord x
= 0, y
= 0, max_x
= 0;
1658 dc
.SetFont(attr
.GetFont());
1659 wxStringTokenizer
tk(text
, _T('\n'));
1660 while ( tk
.HasMoreTokens() )
1662 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1663 max_x
= wxMax(max_x
, x
);
1666 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1668 return wxSize(max_x
, y
);
1671 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1672 wxGridCellAttr
& attr
,
1676 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1679 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1680 wxGridCellAttr
& attr
,
1682 const wxRect
& rectCell
,
1686 wxRect rect
= rectCell
;
1689 // erase only this cells background, overflow cells should have been erased
1690 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1693 attr
.GetAlignment(&hAlign
, &vAlign
);
1695 int overflowCols
= 0;
1697 if (attr
.GetOverflow())
1699 int cols
= grid
.GetNumberCols();
1700 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1701 int cell_rows
, cell_cols
;
1702 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1703 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1705 int i
, c_cols
, c_rows
;
1706 for (i
= col
+cell_cols
; i
< cols
; i
++)
1708 bool is_empty
= true;
1709 for (int j
=row
; j
<row
+cell_rows
; j
++)
1711 // check w/ anchor cell for multicell block
1712 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1713 if (c_rows
> 0) c_rows
= 0;
1714 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1721 rect
.width
+= grid
.GetColSize(i
);
1727 if (rect
.width
>= best_width
) break;
1729 overflowCols
= i
- col
- cell_cols
+ 1;
1730 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1733 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1735 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1737 clip
.x
+= rectCell
.width
;
1738 // draw each overflow cell individually
1739 int col_end
= col
+cell_cols
+overflowCols
;
1740 if (col_end
>= grid
.GetNumberCols())
1741 col_end
= grid
.GetNumberCols() - 1;
1742 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1744 clip
.width
= grid
.GetColSize(i
) - 1;
1745 dc
.DestroyClippingRegion();
1746 dc
.SetClippingRegion(clip
);
1748 SetTextColoursAndFont(grid
, attr
, dc
,
1749 grid
.IsInSelection(row
,i
));
1751 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1752 rect
, hAlign
, vAlign
);
1753 clip
.x
+= grid
.GetColSize(i
) - 1;
1759 dc
.DestroyClippingRegion();
1763 // now we only have to draw the text
1764 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1766 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1767 rect
, hAlign
, vAlign
);
1770 // ----------------------------------------------------------------------------
1771 // wxGridCellNumberRenderer
1772 // ----------------------------------------------------------------------------
1774 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1776 wxGridTableBase
*table
= grid
.GetTable();
1778 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1780 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1784 text
= table
->GetValue(row
, col
);
1790 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1791 wxGridCellAttr
& attr
,
1793 const wxRect
& rectCell
,
1797 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1799 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1801 // draw the text right aligned by default
1803 attr
.GetAlignment(&hAlign
, &vAlign
);
1804 hAlign
= wxALIGN_RIGHT
;
1806 wxRect rect
= rectCell
;
1809 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1812 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1813 wxGridCellAttr
& attr
,
1817 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1820 // ----------------------------------------------------------------------------
1821 // wxGridCellFloatRenderer
1822 // ----------------------------------------------------------------------------
1824 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1827 SetPrecision(precision
);
1830 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1832 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1833 renderer
->m_width
= m_width
;
1834 renderer
->m_precision
= m_precision
;
1835 renderer
->m_format
= m_format
;
1840 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1842 wxGridTableBase
*table
= grid
.GetTable();
1847 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1849 val
= table
->GetValueAsDouble(row
, col
);
1854 text
= table
->GetValue(row
, col
);
1855 hasDouble
= text
.ToDouble(&val
);
1862 if ( m_width
== -1 )
1864 if ( m_precision
== -1 )
1866 // default width/precision
1867 m_format
= _T("%f");
1871 m_format
.Printf(_T("%%.%df"), m_precision
);
1874 else if ( m_precision
== -1 )
1876 // default precision
1877 m_format
.Printf(_T("%%%d.f"), m_width
);
1881 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1885 text
.Printf(m_format
, val
);
1888 //else: text already contains the string
1893 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1894 wxGridCellAttr
& attr
,
1896 const wxRect
& rectCell
,
1900 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1902 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1904 // draw the text right aligned by default
1906 attr
.GetAlignment(&hAlign
, &vAlign
);
1907 hAlign
= wxALIGN_RIGHT
;
1909 wxRect rect
= rectCell
;
1912 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1915 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1916 wxGridCellAttr
& attr
,
1920 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1923 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1927 // reset to defaults
1933 wxString tmp
= params
.BeforeFirst(_T(','));
1937 if ( tmp
.ToLong(&width
) )
1939 SetWidth((int)width
);
1943 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1947 tmp
= params
.AfterFirst(_T(','));
1951 if ( tmp
.ToLong(&precision
) )
1953 SetPrecision((int)precision
);
1957 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1965 // ----------------------------------------------------------------------------
1966 // wxGridCellBoolRenderer
1967 // ----------------------------------------------------------------------------
1969 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1971 // FIXME these checkbox size calculations are really ugly...
1973 // between checkmark and box
1974 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1976 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1977 wxGridCellAttr
& WXUNUSED(attr
),
1982 // compute it only once (no locks for MT safeness in GUI thread...)
1983 if ( !ms_sizeCheckMark
.x
)
1985 // get checkbox size
1986 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
1987 wxSize size
= checkbox
->GetBestSize();
1988 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1990 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1991 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1992 checkSize
-= size
.y
/ 2;
1997 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2000 return ms_sizeCheckMark
;
2003 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2004 wxGridCellAttr
& attr
,
2010 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2012 // draw a check mark in the centre (ignoring alignment - TODO)
2013 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2015 // don't draw outside the cell
2016 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2017 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2019 // and even leave (at least) 1 pixel margin
2020 size
.x
= size
.y
= minSize
- 2;
2023 // draw a border around checkmark
2025 attr
.GetAlignment(& hAlign
, &vAlign
);
2028 if (hAlign
== wxALIGN_CENTRE
)
2030 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2031 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2032 rectBorder
.width
= size
.x
;
2033 rectBorder
.height
= size
.y
;
2035 else if (hAlign
== wxALIGN_LEFT
)
2037 rectBorder
.x
= rect
.x
+ 2;
2038 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2039 rectBorder
.width
= size
.x
;
2040 rectBorder
.height
= size
.y
;
2042 else if (hAlign
== wxALIGN_RIGHT
)
2044 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2045 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2046 rectBorder
.width
= size
.x
;
2047 rectBorder
.height
= size
.y
;
2051 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2052 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2055 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2056 value
= !( !cellval
|| (cellval
== wxT("0")) );
2061 wxRect rectMark
= rectBorder
;
2063 // MSW DrawCheckMark() is weird (and should probably be changed...)
2064 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2068 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2071 dc
.SetTextForeground(attr
.GetTextColour());
2072 dc
.DrawCheckMark(rectMark
);
2075 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2076 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2077 dc
.DrawRectangle(rectBorder
);
2080 // ----------------------------------------------------------------------------
2082 // ----------------------------------------------------------------------------
2084 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2088 m_isReadOnly
= Unset
;
2093 m_attrkind
= wxGridCellAttr::Cell
;
2095 m_sizeRows
= m_sizeCols
= 1;
2096 m_overflow
= UnsetOverflow
;
2098 SetDefAttr(attrDefault
);
2101 wxGridCellAttr
*wxGridCellAttr::Clone() const
2103 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2105 if ( HasTextColour() )
2106 attr
->SetTextColour(GetTextColour());
2107 if ( HasBackgroundColour() )
2108 attr
->SetBackgroundColour(GetBackgroundColour());
2110 attr
->SetFont(GetFont());
2111 if ( HasAlignment() )
2112 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2114 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2118 attr
->SetRenderer(m_renderer
);
2119 m_renderer
->IncRef();
2123 attr
->SetEditor(m_editor
);
2128 attr
->SetReadOnly();
2130 attr
->SetKind( m_attrkind
);
2135 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2137 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2138 SetTextColour(mergefrom
->GetTextColour());
2139 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2140 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2141 if ( !HasFont() && mergefrom
->HasFont() )
2142 SetFont(mergefrom
->GetFont());
2143 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2145 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2146 SetAlignment(hAlign
, vAlign
);
2149 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2151 // Directly access member functions as GetRender/Editor don't just return
2152 // m_renderer/m_editor
2154 // Maybe add support for merge of Render and Editor?
2155 if (!HasRenderer() && mergefrom
->HasRenderer() )
2157 m_renderer
= mergefrom
->m_renderer
;
2158 m_renderer
->IncRef();
2160 if ( !HasEditor() && mergefrom
->HasEditor() )
2162 m_editor
= mergefrom
->m_editor
;
2165 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2166 SetReadOnly(mergefrom
->IsReadOnly());
2168 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2169 SetOverflow(mergefrom
->GetOverflow());
2171 SetDefAttr(mergefrom
->m_defGridAttr
);
2174 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2176 // The size of a cell is normally 1,1
2178 // If this cell is larger (2,2) then this is the top left cell
2179 // the other cells that will be covered (lower right cells) must be
2180 // set to negative or zero values such that
2181 // row + num_rows of the covered cell points to the larger cell (this cell)
2182 // same goes for the col + num_cols.
2184 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2186 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2187 !((num_rows
<=0)&&(num_cols
>0)) ||
2188 !((num_rows
==0)&&(num_cols
==0))),
2189 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2191 m_sizeRows
= num_rows
;
2192 m_sizeCols
= num_cols
;
2195 const wxColour
& wxGridCellAttr::GetTextColour() const
2197 if (HasTextColour())
2201 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2203 return m_defGridAttr
->GetTextColour();
2207 wxFAIL_MSG(wxT("Missing default cell attribute"));
2208 return wxNullColour
;
2213 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2215 if (HasBackgroundColour())
2217 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2218 return m_defGridAttr
->GetBackgroundColour();
2221 wxFAIL_MSG(wxT("Missing default cell attribute"));
2222 return wxNullColour
;
2227 const wxFont
& wxGridCellAttr::GetFont() const
2231 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2232 return m_defGridAttr
->GetFont();
2235 wxFAIL_MSG(wxT("Missing default cell attribute"));
2241 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2245 if ( hAlign
) *hAlign
= m_hAlign
;
2246 if ( vAlign
) *vAlign
= m_vAlign
;
2248 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2249 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2252 wxFAIL_MSG(wxT("Missing default cell attribute"));
2256 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2258 if ( num_rows
) *num_rows
= m_sizeRows
;
2259 if ( num_cols
) *num_cols
= m_sizeCols
;
2262 // GetRenderer and GetEditor use a slightly different decision path about
2263 // which attribute to use. If a non-default attr object has one then it is
2264 // used, otherwise the default editor or renderer is fetched from the grid and
2265 // used. It should be the default for the data type of the cell. If it is
2266 // NULL (because the table has a type that the grid does not have in its
2267 // registry,) then the grid's default editor or renderer is used.
2269 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2271 wxGridCellRenderer
*renderer
;
2273 if ( m_renderer
&& this != m_defGridAttr
)
2275 // use the cells renderer if it has one
2276 renderer
= m_renderer
;
2279 else // no non default cell renderer
2281 // get default renderer for the data type
2284 // GetDefaultRendererForCell() will do IncRef() for us
2285 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2294 if (m_defGridAttr
&& this != m_defGridAttr
)
2296 // if we still don't have one then use the grid default
2297 // (no need for IncRef() here neither)
2298 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2300 else // default grid attr
2302 // use m_renderer which we had decided not to use initially
2303 renderer
= m_renderer
;
2310 // we're supposed to always find something
2311 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2316 // same as above, except for s/renderer/editor/g
2317 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2319 wxGridCellEditor
*editor
;
2321 if ( m_editor
&& this != m_defGridAttr
)
2323 // use the cells editor if it has one
2327 else // no non default cell editor
2329 // get default editor for the data type
2332 // GetDefaultEditorForCell() will do IncRef() for us
2333 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2342 if ( m_defGridAttr
&& this != m_defGridAttr
)
2344 // if we still don't have one then use the grid default
2345 // (no need for IncRef() here neither)
2346 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2348 else // default grid attr
2350 // use m_editor which we had decided not to use initially
2358 // we're supposed to always find something
2359 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2364 // ----------------------------------------------------------------------------
2365 // wxGridCellAttrData
2366 // ----------------------------------------------------------------------------
2368 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2370 int n
= FindIndex(row
, col
);
2371 if ( n
== wxNOT_FOUND
)
2373 // add the attribute
2374 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2378 // free the old attribute
2379 m_attrs
[(size_t)n
].attr
->DecRef();
2383 // change the attribute
2384 m_attrs
[(size_t)n
].attr
= attr
;
2388 // remove this attribute
2389 m_attrs
.RemoveAt((size_t)n
);
2394 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2396 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2398 int n
= FindIndex(row
, col
);
2399 if ( n
!= wxNOT_FOUND
)
2401 attr
= m_attrs
[(size_t)n
].attr
;
2408 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2410 size_t count
= m_attrs
.GetCount();
2411 for ( size_t n
= 0; n
< count
; n
++ )
2413 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2414 wxCoord row
= coords
.GetRow();
2415 if ((size_t)row
>= pos
)
2419 // If rows inserted, include row counter where necessary
2420 coords
.SetRow(row
+ numRows
);
2422 else if (numRows
< 0)
2424 // If rows deleted ...
2425 if ((size_t)row
>= pos
- numRows
)
2427 // ...either decrement row counter (if row still exists)...
2428 coords
.SetRow(row
+ numRows
);
2432 // ...or remove the attribute
2433 m_attrs
.RemoveAt((size_t)n
);
2441 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2443 size_t count
= m_attrs
.GetCount();
2444 for ( size_t n
= 0; n
< count
; n
++ )
2446 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2447 wxCoord col
= coords
.GetCol();
2448 if ( (size_t)col
>= pos
)
2452 // If rows inserted, include row counter where necessary
2453 coords
.SetCol(col
+ numCols
);
2455 else if (numCols
< 0)
2457 // If rows deleted ...
2458 if ((size_t)col
>= pos
- numCols
)
2460 // ...either decrement row counter (if row still exists)...
2461 coords
.SetCol(col
+ numCols
);
2465 // ...or remove the attribute
2466 m_attrs
.RemoveAt((size_t)n
);
2474 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2476 size_t count
= m_attrs
.GetCount();
2477 for ( size_t n
= 0; n
< count
; n
++ )
2479 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2480 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2489 // ----------------------------------------------------------------------------
2490 // wxGridRowOrColAttrData
2491 // ----------------------------------------------------------------------------
2493 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2495 size_t count
= m_attrs
.Count();
2496 for ( size_t n
= 0; n
< count
; n
++ )
2498 m_attrs
[n
]->DecRef();
2502 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2504 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2506 int n
= m_rowsOrCols
.Index(rowOrCol
);
2507 if ( n
!= wxNOT_FOUND
)
2509 attr
= m_attrs
[(size_t)n
];
2516 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2518 int i
= m_rowsOrCols
.Index(rowOrCol
);
2519 if ( i
== wxNOT_FOUND
)
2521 // add the attribute
2522 m_rowsOrCols
.Add(rowOrCol
);
2527 size_t n
= (size_t)i
;
2530 // change the attribute
2531 m_attrs
[n
]->DecRef();
2536 // remove this attribute
2537 m_attrs
[n
]->DecRef();
2538 m_rowsOrCols
.RemoveAt(n
);
2539 m_attrs
.RemoveAt(n
);
2544 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2546 size_t count
= m_attrs
.GetCount();
2547 for ( size_t n
= 0; n
< count
; n
++ )
2549 int & rowOrCol
= m_rowsOrCols
[n
];
2550 if ( (size_t)rowOrCol
>= pos
)
2552 if ( numRowsOrCols
> 0 )
2554 // If rows inserted, include row counter where necessary
2555 rowOrCol
+= numRowsOrCols
;
2557 else if ( numRowsOrCols
< 0)
2559 // If rows deleted, either decrement row counter (if row still exists)
2560 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2561 rowOrCol
+= numRowsOrCols
;
2564 m_rowsOrCols
.RemoveAt((size_t)n
);
2565 m_attrs
.RemoveAt((size_t)n
);
2573 // ----------------------------------------------------------------------------
2574 // wxGridCellAttrProvider
2575 // ----------------------------------------------------------------------------
2577 wxGridCellAttrProvider::wxGridCellAttrProvider()
2579 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2582 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2587 void wxGridCellAttrProvider::InitData()
2589 m_data
= new wxGridCellAttrProviderData
;
2592 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2593 wxGridCellAttr::wxAttrKind kind
) const
2595 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2600 case (wxGridCellAttr::Any
):
2601 //Get cached merge attributes.
2602 // Currenlty not used as no cache implemented as not mutiable
2603 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2606 //Basicaly implement old version.
2607 //Also check merge cache, so we don't have to re-merge every time..
2608 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2609 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2610 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2612 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2613 // Two or more are non NULL
2614 attr
= new wxGridCellAttr
;
2615 attr
->SetKind(wxGridCellAttr::Merged
);
2619 attr
->MergeWith(attrcell
);
2623 attr
->MergeWith(attrcol
);
2627 attr
->MergeWith(attrrow
);
2630 //store merge attr if cache implemented
2632 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2636 // one or none is non null return it or null.
2637 if(attrrow
) attr
= attrrow
;
2653 case (wxGridCellAttr::Cell
):
2654 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2656 case (wxGridCellAttr::Col
):
2657 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2659 case (wxGridCellAttr::Row
):
2660 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2664 // (wxGridCellAttr::Default):
2665 // (wxGridCellAttr::Merged):
2672 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2678 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2681 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2686 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2689 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2694 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2697 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2701 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2703 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2707 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2711 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2713 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2717 // ----------------------------------------------------------------------------
2718 // wxGridTypeRegistry
2719 // ----------------------------------------------------------------------------
2721 wxGridTypeRegistry::~wxGridTypeRegistry()
2723 size_t count
= m_typeinfo
.Count();
2724 for ( size_t i
= 0; i
< count
; i
++ )
2725 delete m_typeinfo
[i
];
2729 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2730 wxGridCellRenderer
* renderer
,
2731 wxGridCellEditor
* editor
)
2733 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2735 // is it already registered?
2736 int loc
= FindRegisteredDataType(typeName
);
2737 if ( loc
!= wxNOT_FOUND
)
2739 delete m_typeinfo
[loc
];
2740 m_typeinfo
[loc
] = info
;
2744 m_typeinfo
.Add(info
);
2748 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2750 size_t count
= m_typeinfo
.GetCount();
2751 for ( size_t i
= 0; i
< count
; i
++ )
2753 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2762 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2764 int index
= FindRegisteredDataType(typeName
);
2765 if ( index
== wxNOT_FOUND
)
2767 // check whether this is one of the standard ones, in which case
2768 // register it "on the fly"
2770 if ( typeName
== wxGRID_VALUE_STRING
)
2772 RegisterDataType(wxGRID_VALUE_STRING
,
2773 new wxGridCellStringRenderer
,
2774 new wxGridCellTextEditor
);
2776 #endif // wxUSE_TEXTCTRL
2778 if ( typeName
== wxGRID_VALUE_BOOL
)
2780 RegisterDataType(wxGRID_VALUE_BOOL
,
2781 new wxGridCellBoolRenderer
,
2782 new wxGridCellBoolEditor
);
2784 #endif // wxUSE_CHECKBOX
2786 if ( typeName
== wxGRID_VALUE_NUMBER
)
2788 RegisterDataType(wxGRID_VALUE_NUMBER
,
2789 new wxGridCellNumberRenderer
,
2790 new wxGridCellNumberEditor
);
2792 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2794 RegisterDataType(wxGRID_VALUE_FLOAT
,
2795 new wxGridCellFloatRenderer
,
2796 new wxGridCellFloatEditor
);
2798 #endif // wxUSE_TEXTCTRL
2800 if ( typeName
== wxGRID_VALUE_CHOICE
)
2802 RegisterDataType(wxGRID_VALUE_CHOICE
,
2803 new wxGridCellStringRenderer
,
2804 new wxGridCellChoiceEditor
);
2806 #endif // wxUSE_COMBOBOX
2811 // we get here only if just added the entry for this type, so return
2813 index
= m_typeinfo
.GetCount() - 1;
2819 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2821 int index
= FindDataType(typeName
);
2822 if ( index
== wxNOT_FOUND
)
2824 // the first part of the typename is the "real" type, anything after ':'
2825 // are the parameters for the renderer
2826 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2827 if ( index
== wxNOT_FOUND
)
2832 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2833 wxGridCellRenderer
*rendererOld
= renderer
;
2834 renderer
= renderer
->Clone();
2835 rendererOld
->DecRef();
2837 wxGridCellEditor
*editor
= GetEditor(index
);
2838 wxGridCellEditor
*editorOld
= editor
;
2839 editor
= editor
->Clone();
2840 editorOld
->DecRef();
2842 // do it even if there are no parameters to reset them to defaults
2843 wxString params
= typeName
.AfterFirst(_T(':'));
2844 renderer
->SetParameters(params
);
2845 editor
->SetParameters(params
);
2847 // register the new typename
2848 RegisterDataType(typeName
, renderer
, editor
);
2850 // we just registered it, it's the last one
2851 index
= m_typeinfo
.GetCount() - 1;
2857 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2859 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2865 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2867 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2873 // ----------------------------------------------------------------------------
2875 // ----------------------------------------------------------------------------
2877 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2880 wxGridTableBase::wxGridTableBase()
2882 m_view
= (wxGrid
*) NULL
;
2883 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2886 wxGridTableBase::~wxGridTableBase()
2888 delete m_attrProvider
;
2891 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2893 delete m_attrProvider
;
2894 m_attrProvider
= attrProvider
;
2897 bool wxGridTableBase::CanHaveAttributes()
2899 if ( ! GetAttrProvider() )
2901 // use the default attr provider by default
2902 SetAttrProvider(new wxGridCellAttrProvider
);
2907 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2909 if ( m_attrProvider
)
2910 return m_attrProvider
->GetAttr(row
, col
, kind
);
2912 return (wxGridCellAttr
*)NULL
;
2915 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2917 if ( m_attrProvider
)
2919 attr
->SetKind(wxGridCellAttr::Cell
);
2920 m_attrProvider
->SetAttr(attr
, row
, col
);
2924 // as we take ownership of the pointer and don't store it, we must
2930 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2932 if ( m_attrProvider
)
2934 attr
->SetKind(wxGridCellAttr::Row
);
2935 m_attrProvider
->SetRowAttr(attr
, row
);
2939 // as we take ownership of the pointer and don't store it, we must
2945 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2947 if ( m_attrProvider
)
2949 attr
->SetKind(wxGridCellAttr::Col
);
2950 m_attrProvider
->SetColAttr(attr
, col
);
2954 // as we take ownership of the pointer and don't store it, we must
2960 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2961 size_t WXUNUSED(numRows
) )
2963 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2968 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2970 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2975 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2976 size_t WXUNUSED(numRows
) )
2978 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2983 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2984 size_t WXUNUSED(numCols
) )
2986 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2991 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2993 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2998 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2999 size_t WXUNUSED(numCols
) )
3001 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3007 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3010 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3011 // how much it makes sense to us geeks.
3015 wxString
wxGridTableBase::GetColLabelValue( int col
)
3017 // default col labels are:
3018 // cols 0 to 25 : A-Z
3019 // cols 26 to 675 : AA-ZZ
3024 for ( n
= 1; ; n
++ )
3026 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3028 if ( col
< 0 ) break;
3031 // reverse the string...
3033 for ( i
= 0; i
< n
; i
++ )
3042 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3044 return wxGRID_VALUE_STRING
;
3047 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3048 const wxString
& typeName
)
3050 return typeName
== wxGRID_VALUE_STRING
;
3053 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3055 return CanGetValueAs(row
, col
, typeName
);
3058 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3063 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3068 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3073 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3074 long WXUNUSED(value
) )
3078 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3079 double WXUNUSED(value
) )
3083 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3084 bool WXUNUSED(value
) )
3089 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3090 const wxString
& WXUNUSED(typeName
) )
3095 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3096 const wxString
& WXUNUSED(typeName
),
3097 void* WXUNUSED(value
) )
3101 //////////////////////////////////////////////////////////////////////
3103 // Message class for the grid table to send requests and notifications
3107 wxGridTableMessage::wxGridTableMessage()
3109 m_table
= (wxGridTableBase
*) NULL
;
3115 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3116 int commandInt1
, int commandInt2
)
3120 m_comInt1
= commandInt1
;
3121 m_comInt2
= commandInt2
;
3126 //////////////////////////////////////////////////////////////////////
3128 // A basic grid table for string data. An object of this class will
3129 // created by wxGrid if you don't specify an alternative table class.
3132 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3134 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3136 wxGridStringTable::wxGridStringTable()
3141 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3144 m_data
.Alloc( numRows
);
3147 sa
.Alloc( numCols
);
3148 sa
.Add( wxEmptyString
, numCols
);
3150 m_data
.Add( sa
, numRows
);
3153 wxGridStringTable::~wxGridStringTable()
3157 int wxGridStringTable::GetNumberRows()
3159 return m_data
.GetCount();
3162 int wxGridStringTable::GetNumberCols()
3164 if ( m_data
.GetCount() > 0 )
3165 return m_data
[0].GetCount();
3170 wxString
wxGridStringTable::GetValue( int row
, int col
)
3172 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3174 _T("invalid row or column index in wxGridStringTable") );
3176 return m_data
[row
][col
];
3179 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3181 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3182 _T("invalid row or column index in wxGridStringTable") );
3184 m_data
[row
][col
] = value
;
3187 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3189 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3191 _T("invalid row or column index in wxGridStringTable") );
3193 return (m_data
[row
][col
] == wxEmptyString
);
3196 void wxGridStringTable::Clear()
3199 int numRows
, numCols
;
3201 numRows
= m_data
.GetCount();
3204 numCols
= m_data
[0].GetCount();
3206 for ( row
= 0; row
< numRows
; row
++ )
3208 for ( col
= 0; col
< numCols
; col
++ )
3210 m_data
[row
][col
] = wxEmptyString
;
3217 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3219 size_t curNumRows
= m_data
.GetCount();
3220 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3221 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3223 if ( pos
>= curNumRows
)
3225 return AppendRows( numRows
);
3229 sa
.Alloc( curNumCols
);
3230 sa
.Add( wxEmptyString
, curNumCols
);
3231 m_data
.Insert( sa
, pos
, numRows
);
3234 wxGridTableMessage
msg( this,
3235 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3239 GetView()->ProcessTableMessage( msg
);
3245 bool wxGridStringTable::AppendRows( size_t numRows
)
3247 size_t curNumRows
= m_data
.GetCount();
3248 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3249 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3252 if ( curNumCols
> 0 )
3254 sa
.Alloc( curNumCols
);
3255 sa
.Add( wxEmptyString
, curNumCols
);
3258 m_data
.Add( sa
, numRows
);
3262 wxGridTableMessage
msg( this,
3263 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3266 GetView()->ProcessTableMessage( msg
);
3272 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3274 size_t curNumRows
= m_data
.GetCount();
3276 if ( pos
>= curNumRows
)
3278 wxFAIL_MSG( wxString::Format
3280 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3282 (unsigned long)numRows
,
3283 (unsigned long)curNumRows
3289 if ( numRows
> curNumRows
- pos
)
3291 numRows
= curNumRows
- pos
;
3294 if ( numRows
>= curNumRows
)
3300 m_data
.RemoveAt( pos
, numRows
);
3304 wxGridTableMessage
msg( this,
3305 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3309 GetView()->ProcessTableMessage( msg
);
3315 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3319 size_t curNumRows
= m_data
.GetCount();
3320 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3321 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3323 if ( pos
>= curNumCols
)
3325 return AppendCols( numCols
);
3328 for ( row
= 0; row
< curNumRows
; row
++ )
3330 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3332 m_data
[row
].Insert( wxEmptyString
, col
);
3337 wxGridTableMessage
msg( this,
3338 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3342 GetView()->ProcessTableMessage( msg
);
3348 bool wxGridStringTable::AppendCols( size_t numCols
)
3352 size_t curNumRows
= m_data
.GetCount();
3356 // TODO: something better than this ?
3358 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3363 for ( row
= 0; row
< curNumRows
; row
++ )
3365 m_data
[row
].Add( wxEmptyString
, numCols
);
3370 wxGridTableMessage
msg( this,
3371 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3374 GetView()->ProcessTableMessage( msg
);
3380 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3384 size_t curNumRows
= m_data
.GetCount();
3385 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3386 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3388 if ( pos
>= curNumCols
)
3390 wxFAIL_MSG( wxString::Format
3392 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3394 (unsigned long)numCols
,
3395 (unsigned long)curNumCols
3400 if ( numCols
> curNumCols
- pos
)
3402 numCols
= curNumCols
- pos
;
3405 for ( row
= 0; row
< curNumRows
; row
++ )
3407 if ( numCols
>= curNumCols
)
3409 m_data
[row
].Clear();
3413 m_data
[row
].RemoveAt( pos
, numCols
);
3418 wxGridTableMessage
msg( this,
3419 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3423 GetView()->ProcessTableMessage( msg
);
3429 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3431 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3433 // using default label
3435 return wxGridTableBase::GetRowLabelValue( row
);
3439 return m_rowLabels
[ row
];
3443 wxString
wxGridStringTable::GetColLabelValue( int col
)
3445 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3447 // using default label
3449 return wxGridTableBase::GetColLabelValue( col
);
3453 return m_colLabels
[ col
];
3457 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3459 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3461 int n
= m_rowLabels
.GetCount();
3463 for ( i
= n
; i
<= row
; i
++ )
3465 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3469 m_rowLabels
[row
] = value
;
3472 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3474 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3476 int n
= m_colLabels
.GetCount();
3478 for ( i
= n
; i
<= col
; i
++ )
3480 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3484 m_colLabels
[col
] = value
;
3489 //////////////////////////////////////////////////////////////////////
3490 //////////////////////////////////////////////////////////////////////
3492 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3494 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3495 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3496 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3497 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3498 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3499 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3502 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3504 const wxPoint
&pos
, const wxSize
&size
)
3505 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3510 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3514 // NO - don't do this because it will set both the x and y origin
3515 // coords to match the parent scrolled window and we just want to
3516 // set the y coord - MB
3518 // m_owner->PrepareDC( dc );
3521 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3522 dc
.SetDeviceOrigin( 0, -y
);
3524 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3525 m_owner
->DrawRowLabels( dc
, rows
);
3529 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3531 m_owner
->ProcessRowLabelMouseEvent( event
);
3535 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3537 m_owner
->GetEventHandler()->ProcessEvent(event
);
3541 // This seems to be required for wxMotif otherwise the mouse
3542 // cursor must be in the cell edit control to get key events
3544 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3546 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3549 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3551 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3556 //////////////////////////////////////////////////////////////////////
3558 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3560 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3561 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3562 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3563 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3564 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3565 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3568 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3570 const wxPoint
&pos
, const wxSize
&size
)
3571 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3576 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3580 // NO - don't do this because it will set both the x and y origin
3581 // coords to match the parent scrolled window and we just want to
3582 // set the x coord - MB
3584 // m_owner->PrepareDC( dc );
3587 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3588 dc
.SetDeviceOrigin( -x
, 0 );
3590 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3591 m_owner
->DrawColLabels( dc
, cols
);
3595 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3597 m_owner
->ProcessColLabelMouseEvent( event
);
3600 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3602 m_owner
->GetEventHandler()->ProcessEvent(event
);
3606 // This seems to be required for wxMotif otherwise the mouse
3607 // cursor must be in the cell edit control to get key events
3609 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3611 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3614 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3616 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3621 //////////////////////////////////////////////////////////////////////
3623 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3625 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3626 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3627 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3628 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3629 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3630 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3633 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3635 const wxPoint
&pos
, const wxSize
&size
)
3636 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3641 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3645 int client_height
= 0;
3646 int client_width
= 0;
3647 GetClientSize( &client_width
, &client_height
);
3649 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3650 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3651 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3652 dc
.DrawLine( 0, 0, client_width
, 0 );
3653 dc
.DrawLine( 0, 0, 0, client_height
);
3655 dc
.SetPen( *wxWHITE_PEN
);
3656 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3657 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3661 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3663 m_owner
->ProcessCornerLabelMouseEvent( event
);
3667 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3669 m_owner
->GetEventHandler()->ProcessEvent(event
);
3672 // This seems to be required for wxMotif otherwise the mouse
3673 // cursor must be in the cell edit control to get key events
3675 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3677 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3680 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3682 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3687 //////////////////////////////////////////////////////////////////////
3689 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3691 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3692 EVT_PAINT( wxGridWindow::OnPaint
)
3693 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3694 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3695 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3696 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3697 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3698 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3699 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3702 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3703 wxGridRowLabelWindow
*rowLblWin
,
3704 wxGridColLabelWindow
*colLblWin
,
3707 const wxSize
&size
)
3708 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3709 wxT("grid window") )
3713 m_rowLabelWin
= rowLblWin
;
3714 m_colLabelWin
= colLblWin
;
3718 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3720 wxPaintDC
dc( this );
3721 m_owner
->PrepareDC( dc
);
3722 wxRegion reg
= GetUpdateRegion();
3723 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3724 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3725 #if WXGRID_DRAW_LINES
3726 m_owner
->DrawAllGridLines( dc
, reg
);
3728 m_owner
->DrawGridSpace( dc
);
3729 m_owner
->DrawHighlight( dc
, DirtyCells
);
3733 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3735 wxWindow::ScrollWindow( dx
, dy
, rect
);
3736 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3737 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3741 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3743 m_owner
->ProcessGridCellMouseEvent( event
);
3746 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3748 m_owner
->GetEventHandler()->ProcessEvent(event
);
3751 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3752 // cursor must be in the cell edit control to get key events
3754 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3756 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3759 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3761 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3764 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3768 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3770 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3774 //////////////////////////////////////////////////////////////////////
3776 // Internal Helper function for computing row or column from some
3777 // (unscrolled) coordinate value, using either
3778 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3779 // of m_rowBottoms/m_ColRights to speed up the search!
3781 // Internal helper macros for simpler use of that function
3783 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3784 const wxArrayInt
& BorderArray
, int nMax
,
3787 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3788 m_minAcceptableColWidth, \
3789 m_colRights, m_numCols, true)
3790 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3791 m_minAcceptableRowHeight, \
3792 m_rowBottoms, m_numRows, true)
3793 /////////////////////////////////////////////////////////////////////
3795 #if wxUSE_EXTENDED_RTTI
3796 WX_DEFINE_FLAGS( wxGridStyle
)
3798 wxBEGIN_FLAGS( wxGridStyle
)
3799 // new style border flags, we put them first to
3800 // use them for streaming out
3801 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3802 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3803 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3804 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3805 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3806 wxFLAGS_MEMBER(wxBORDER_NONE
)
3808 // old style border flags
3809 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3810 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3811 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3812 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3813 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3814 wxFLAGS_MEMBER(wxBORDER
)
3816 // standard window styles
3817 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3818 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3819 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3820 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3821 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3822 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3823 wxFLAGS_MEMBER(wxVSCROLL
)
3824 wxFLAGS_MEMBER(wxHSCROLL
)
3826 wxEND_FLAGS( wxGridStyle
)
3828 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3830 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3831 wxHIDE_PROPERTY( Children
)
3832 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3833 wxEND_PROPERTIES_TABLE()
3835 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3836 wxEND_HANDLERS_TABLE()
3838 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3841 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3844 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3847 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3848 EVT_PAINT( wxGrid::OnPaint
)
3849 EVT_SIZE( wxGrid::OnSize
)
3850 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3851 EVT_KEY_UP( wxGrid::OnKeyUp
)
3852 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3857 // in order to make sure that a size event is not
3858 // trigerred in a unfinished state
3859 m_cornerLabelWin
= NULL
;
3860 m_rowLabelWin
= NULL
;
3861 m_colLabelWin
= NULL
;
3865 wxGrid::wxGrid( wxWindow
*parent
,
3870 const wxString
& name
)
3871 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3872 m_colMinWidths(GRID_HASH_SIZE
),
3873 m_rowMinHeights(GRID_HASH_SIZE
)
3876 SetBestFittingSize(size
);
3879 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3880 const wxPoint
& pos
, const wxSize
& size
,
3881 long style
, const wxString
& name
)
3883 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3884 style
| wxWANTS_CHARS
, name
))
3887 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3888 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3891 SetBestFittingSize(size
);
3899 // Must do this or ~wxScrollHelper will pop the wrong event handler
3900 SetTargetWindow(this);
3902 wxSafeDecRef(m_defaultCellAttr
);
3904 #ifdef DEBUG_ATTR_CACHE
3905 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3906 wxPrintf(_T("wxGrid attribute cache statistics: "
3907 "total: %u, hits: %u (%u%%)\n"),
3908 total
, gs_nAttrCacheHits
,
3909 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3915 delete m_typeRegistry
;
3921 // ----- internal init and update functions
3924 // NOTE: If using the default visual attributes works everywhere then this can
3925 // be removed as well as the #else cases below.
3926 #define _USE_VISATTR 0
3929 #include "wx/listbox.h"
3932 void wxGrid::Create()
3934 m_created
= false; // set to true by CreateGrid
3936 m_table
= (wxGridTableBase
*) NULL
;
3939 m_cellEditCtrlEnabled
= false;
3941 m_defaultCellAttr
= new wxGridCellAttr();
3943 // Set default cell attributes
3944 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3945 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3946 m_defaultCellAttr
->SetFont(GetFont());
3947 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3948 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3949 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3952 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3953 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3955 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3956 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3959 m_defaultCellAttr
->SetTextColour(
3960 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3961 m_defaultCellAttr
->SetBackgroundColour(
3962 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3967 m_currentCellCoords
= wxGridNoCellCoords
;
3969 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3970 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3972 // create the type registry
3973 m_typeRegistry
= new wxGridTypeRegistry
;
3976 // subwindow components that make up the wxGrid
3977 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3982 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3987 m_colLabelWin
= new wxGridColLabelWindow( this,
3992 m_gridWin
= new wxGridWindow( this,
3999 SetTargetWindow( m_gridWin
);
4002 wxColour gfg
= gva
.colFg
;
4003 wxColour gbg
= gva
.colBg
;
4004 wxColour lfg
= lva
.colFg
;
4005 wxColour lbg
= lva
.colBg
;
4007 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4008 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4009 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4010 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4012 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4013 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4014 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4015 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4016 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4017 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4019 m_gridWin
->SetOwnForegroundColour(gfg
);
4020 m_gridWin
->SetOwnBackgroundColour(gbg
);
4026 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4027 wxGrid::wxGridSelectionModes selmode
)
4029 wxCHECK_MSG( !m_created
,
4031 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4033 m_numRows
= numRows
;
4034 m_numCols
= numCols
;
4036 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4037 m_table
->SetView( this );
4039 m_selection
= new wxGridSelection( this, selmode
);
4048 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4050 wxCHECK_RET( m_created
,
4051 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4053 m_selection
->SetSelectionMode( selmode
);
4056 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4058 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4059 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4061 return m_selection
->GetSelectionMode();
4064 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4065 wxGrid::wxGridSelectionModes selmode
)
4069 // stop all processing
4074 wxGridTableBase
*t
=m_table
;
4087 m_numRows
= table
->GetNumberRows();
4088 m_numCols
= table
->GetNumberCols();
4091 m_table
->SetView( this );
4094 m_selection
= new wxGridSelection( this, selmode
);
4107 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4108 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4110 if ( m_rowLabelWin
)
4112 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4116 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4119 m_labelTextColour
= wxColour( _T("BLACK") );
4122 m_attrCache
.row
= -1;
4123 m_attrCache
.col
= -1;
4124 m_attrCache
.attr
= NULL
;
4126 // TODO: something better than this ?
4128 m_labelFont
= this->GetFont();
4129 m_labelFont
.SetWeight( wxBOLD
);
4131 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4132 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4134 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4135 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4136 m_colLabelTextOrientation
= wxHORIZONTAL
;
4138 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4139 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4141 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4142 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4144 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4145 m_defaultRowHeight
+= 8;
4147 m_defaultRowHeight
+= 4;
4150 m_gridLineColour
= wxColour( 192,192,192 );
4151 m_gridLinesEnabled
= true;
4152 m_cellHighlightColour
= *wxBLACK
;
4153 m_cellHighlightPenWidth
= 2;
4154 m_cellHighlightROPenWidth
= 1;
4156 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4157 m_winCapture
= (wxWindow
*)NULL
;
4158 m_canDragRowSize
= true;
4159 m_canDragColSize
= true;
4160 m_canDragGridSize
= true;
4161 m_canDragCell
= false;
4163 m_dragRowOrCol
= -1;
4164 m_isDragging
= false;
4165 m_startDragPos
= wxDefaultPosition
;
4167 m_waitForSlowClick
= false;
4169 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4170 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4172 m_currentCellCoords
= wxGridNoCellCoords
;
4174 m_selectingTopLeft
= wxGridNoCellCoords
;
4175 m_selectingBottomRight
= wxGridNoCellCoords
;
4176 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4177 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4179 m_editable
= true; // default for whole grid
4181 m_inOnKeyDown
= false;
4187 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4188 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4191 // ----------------------------------------------------------------------------
4192 // the idea is to call these functions only when necessary because they create
4193 // quite big arrays which eat memory mostly unnecessary - in particular, if
4194 // default widths/heights are used for all rows/columns, we may not use these
4197 // with some extra code, it should be possible to only store the
4198 // widths/heights different from default ones but this will be done later...
4199 // ----------------------------------------------------------------------------
4201 void wxGrid::InitRowHeights()
4203 m_rowHeights
.Empty();
4204 m_rowBottoms
.Empty();
4206 m_rowHeights
.Alloc( m_numRows
);
4207 m_rowBottoms
.Alloc( m_numRows
);
4211 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4213 for ( int i
= 0; i
< m_numRows
; i
++ )
4215 rowBottom
+= m_defaultRowHeight
;
4216 m_rowBottoms
.Add( rowBottom
);
4220 void wxGrid::InitColWidths()
4222 m_colWidths
.Empty();
4223 m_colRights
.Empty();
4225 m_colWidths
.Alloc( m_numCols
);
4226 m_colRights
.Alloc( m_numCols
);
4229 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4231 for ( int i
= 0; i
< m_numCols
; i
++ )
4233 colRight
+= m_defaultColWidth
;
4234 m_colRights
.Add( colRight
);
4238 int wxGrid::GetColWidth(int col
) const
4240 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4243 int wxGrid::GetColLeft(int col
) const
4245 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4246 : m_colRights
[col
] - m_colWidths
[col
];
4249 int wxGrid::GetColRight(int col
) const
4251 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4255 int wxGrid::GetRowHeight(int row
) const
4257 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4260 int wxGrid::GetRowTop(int row
) const
4262 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4263 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4266 int wxGrid::GetRowBottom(int row
) const
4268 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4269 : m_rowBottoms
[row
];
4272 void wxGrid::CalcDimensions()
4275 GetClientSize( &cw
, &ch
);
4277 if ( m_rowLabelWin
->IsShown() )
4278 cw
-= m_rowLabelWidth
;
4279 if ( m_colLabelWin
->IsShown() )
4280 ch
-= m_colLabelHeight
;
4283 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4284 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4286 // take into account editor if shown
4287 if( IsCellEditControlShown() )
4290 int r
= m_currentCellCoords
.GetRow();
4291 int c
= m_currentCellCoords
.GetCol();
4292 int x
= GetColLeft(c
);
4293 int y
= GetRowTop(r
);
4295 // how big is the editor
4296 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4297 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4298 editor
->GetControl()->GetSize(&w2
, &h2
);
4301 if( w2
> w
) w
= w2
;
4302 if( h2
> h
) h
= h2
;
4307 // preserve (more or less) the previous position
4309 GetViewStart( &x
, &y
);
4311 // ensure the position is valid for the new scroll ranges
4313 x
= wxMax( w
- 1, 0 );
4315 y
= wxMax( h
- 1, 0 );
4317 // do set scrollbar parameters
4318 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4319 GetScrollX(w
), GetScrollY(h
), x
, y
,
4320 GetBatchCount() != 0);
4322 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4323 // still must reposition the children
4328 void wxGrid::CalcWindowSizes()
4330 // escape if the window is has not been fully created yet
4332 if ( m_cornerLabelWin
== NULL
)
4336 GetClientSize( &cw
, &ch
);
4338 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4339 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4341 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4342 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4344 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4345 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4347 if ( m_gridWin
&& m_gridWin
->IsShown() )
4348 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4352 // this is called when the grid table sends a message to say that it
4353 // has been redimensioned
4355 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4358 bool result
= false;
4360 // Clear the attribute cache as the attribute might refer to a different
4361 // cell than stored in the cache after adding/removing rows/columns.
4363 // By the same reasoning, the editor should be dismissed if columns are
4364 // added or removed. And for consistency, it should IMHO always be
4365 // removed, not only if the cell "underneath" it actually changes.
4366 // For now, I intentionally do not save the editor's content as the
4367 // cell it might want to save that stuff to might no longer exist.
4368 HideCellEditControl();
4370 // if we were using the default widths/heights so far, we must change them
4372 if ( m_colWidths
.IsEmpty() )
4377 if ( m_rowHeights
.IsEmpty() )
4383 switch ( msg
.GetId() )
4385 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4387 size_t pos
= msg
.GetCommandInt();
4388 int numRows
= msg
.GetCommandInt2();
4390 m_numRows
+= numRows
;
4392 if ( !m_rowHeights
.IsEmpty() )
4394 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4395 m_rowBottoms
.Insert( 0, pos
, numRows
);
4398 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4400 for ( i
= pos
; i
< m_numRows
; i
++ )
4402 bottom
+= m_rowHeights
[i
];
4403 m_rowBottoms
[i
] = bottom
;
4406 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4408 // if we have just inserted cols into an empty grid the current
4409 // cell will be undefined...
4411 SetCurrentCell( 0, 0 );
4415 m_selection
->UpdateRows( pos
, numRows
);
4416 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4418 attrProvider
->UpdateAttrRows( pos
, numRows
);
4420 if ( !GetBatchCount() )
4423 m_rowLabelWin
->Refresh();
4429 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4431 int numRows
= msg
.GetCommandInt();
4432 int oldNumRows
= m_numRows
;
4433 m_numRows
+= numRows
;
4435 if ( !m_rowHeights
.IsEmpty() )
4437 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4438 m_rowBottoms
.Add( 0, numRows
);
4441 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4443 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4445 bottom
+= m_rowHeights
[i
];
4446 m_rowBottoms
[i
] = bottom
;
4449 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4451 // if we have just inserted cols into an empty grid the current
4452 // cell will be undefined...
4454 SetCurrentCell( 0, 0 );
4456 if ( !GetBatchCount() )
4459 m_rowLabelWin
->Refresh();
4465 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4467 size_t pos
= msg
.GetCommandInt();
4468 int numRows
= msg
.GetCommandInt2();
4469 m_numRows
-= numRows
;
4471 if ( !m_rowHeights
.IsEmpty() )
4473 m_rowHeights
.RemoveAt( pos
, numRows
);
4474 m_rowBottoms
.RemoveAt( pos
, numRows
);
4477 for ( i
= 0; i
< m_numRows
; i
++ )
4479 h
+= m_rowHeights
[i
];
4480 m_rowBottoms
[i
] = h
;
4485 m_currentCellCoords
= wxGridNoCellCoords
;
4489 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4490 m_currentCellCoords
.Set( 0, 0 );
4494 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4495 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4497 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4498 // ifdef'd out following patch from Paul Gammans
4500 // No need to touch column attributes, unless we
4501 // removed _all_ rows, in this case, we remove
4502 // all column attributes.
4503 // I hate to do this here, but the
4504 // needed data is not available inside UpdateAttrRows.
4505 if ( !GetNumberRows() )
4506 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4509 if ( !GetBatchCount() )
4512 m_rowLabelWin
->Refresh();
4518 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4520 size_t pos
= msg
.GetCommandInt();
4521 int numCols
= msg
.GetCommandInt2();
4522 m_numCols
+= numCols
;
4524 if ( !m_colWidths
.IsEmpty() )
4526 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4527 m_colRights
.Insert( 0, pos
, numCols
);
4530 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4532 for ( i
= pos
; i
< m_numCols
; i
++ )
4534 right
+= m_colWidths
[i
];
4535 m_colRights
[i
] = right
;
4538 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4540 // if we have just inserted cols into an empty grid the current
4541 // cell will be undefined...
4543 SetCurrentCell( 0, 0 );
4547 m_selection
->UpdateCols( pos
, numCols
);
4548 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4550 attrProvider
->UpdateAttrCols( pos
, numCols
);
4551 if ( !GetBatchCount() )
4554 m_colLabelWin
->Refresh();
4561 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4563 int numCols
= msg
.GetCommandInt();
4564 int oldNumCols
= m_numCols
;
4565 m_numCols
+= numCols
;
4566 if ( !m_colWidths
.IsEmpty() )
4568 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4569 m_colRights
.Add( 0, numCols
);
4572 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4574 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4576 right
+= m_colWidths
[i
];
4577 m_colRights
[i
] = right
;
4580 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4582 // if we have just inserted cols into an empty grid the current
4583 // cell will be undefined...
4585 SetCurrentCell( 0, 0 );
4587 if ( !GetBatchCount() )
4590 m_colLabelWin
->Refresh();
4596 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4598 size_t pos
= msg
.GetCommandInt();
4599 int numCols
= msg
.GetCommandInt2();
4600 m_numCols
-= numCols
;
4602 if ( !m_colWidths
.IsEmpty() )
4604 m_colWidths
.RemoveAt( pos
, numCols
);
4605 m_colRights
.RemoveAt( pos
, numCols
);
4608 for ( i
= 0; i
< m_numCols
; i
++ )
4610 w
+= m_colWidths
[i
];
4616 m_currentCellCoords
= wxGridNoCellCoords
;
4620 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4621 m_currentCellCoords
.Set( 0, 0 );
4625 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4626 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4628 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4629 // ifdef'd out following patch from Paul Gammans
4631 // No need to touch row attributes, unless we
4632 // removed _all_ columns, in this case, we remove
4633 // all row attributes.
4634 // I hate to do this here, but the
4635 // needed data is not available inside UpdateAttrCols.
4636 if ( !GetNumberCols() )
4637 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4640 if ( !GetBatchCount() )
4643 m_colLabelWin
->Refresh();
4650 if (result
&& !GetBatchCount() )
4651 m_gridWin
->Refresh();
4656 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4658 wxRegionIterator
iter( reg
);
4661 wxArrayInt rowlabels
;
4668 // TODO: remove this when we can...
4669 // There is a bug in wxMotif that gives garbage update
4670 // rectangles if you jump-scroll a long way by clicking the
4671 // scrollbar with middle button. This is a work-around
4673 #if defined(__WXMOTIF__)
4675 m_gridWin
->GetClientSize( &cw
, &ch
);
4676 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4677 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4680 // logical bounds of update region
4683 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4684 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4686 // find the row labels within these bounds
4689 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4691 if ( GetRowBottom(row
) < top
)
4694 if ( GetRowTop(row
) > bottom
)
4697 rowlabels
.Add( row
);
4707 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4709 wxRegionIterator
iter( reg
);
4712 wxArrayInt colLabels
;
4719 // TODO: remove this when we can...
4720 // There is a bug in wxMotif that gives garbage update
4721 // rectangles if you jump-scroll a long way by clicking the
4722 // scrollbar with middle button. This is a work-around
4724 #if defined(__WXMOTIF__)
4726 m_gridWin
->GetClientSize( &cw
, &ch
);
4727 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4728 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4731 // logical bounds of update region
4734 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4735 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4737 // find the cells within these bounds
4740 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4742 if ( GetColRight(col
) < left
)
4745 if ( GetColLeft(col
) > right
)
4748 colLabels
.Add( col
);
4757 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4759 wxRegionIterator
iter( reg
);
4762 wxGridCellCoordsArray cellsExposed
;
4764 int left
, top
, right
, bottom
;
4769 // TODO: remove this when we can...
4770 // There is a bug in wxMotif that gives garbage update
4771 // rectangles if you jump-scroll a long way by clicking the
4772 // scrollbar with middle button. This is a work-around
4774 #if defined(__WXMOTIF__)
4776 m_gridWin
->GetClientSize( &cw
, &ch
);
4777 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4778 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4779 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4780 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4783 // logical bounds of update region
4785 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4786 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4788 // find the cells within these bounds
4791 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4793 if ( GetRowBottom(row
) <= top
)
4796 if ( GetRowTop(row
) > bottom
)
4799 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4801 if ( GetColRight(col
) <= left
)
4804 if ( GetColLeft(col
) > right
)
4807 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4814 return cellsExposed
;
4818 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4821 wxPoint
pos( event
.GetPosition() );
4822 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4824 if ( event
.Dragging() )
4828 m_isDragging
= true;
4829 m_rowLabelWin
->CaptureMouse();
4832 if ( event
.LeftIsDown() )
4834 switch( m_cursorMode
)
4836 case WXGRID_CURSOR_RESIZE_ROW
:
4838 int cw
, ch
, left
, dummy
;
4839 m_gridWin
->GetClientSize( &cw
, &ch
);
4840 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4842 wxClientDC
dc( m_gridWin
);
4845 GetRowTop(m_dragRowOrCol
) +
4846 GetRowMinimalHeight(m_dragRowOrCol
) );
4847 dc
.SetLogicalFunction(wxINVERT
);
4848 if ( m_dragLastPos
>= 0 )
4850 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4852 dc
.DrawLine( left
, y
, left
+cw
, y
);
4857 case WXGRID_CURSOR_SELECT_ROW
:
4858 if ( (row
= YToRow( y
)) >= 0 )
4862 m_selection
->SelectRow( row
,
4863 event
.ControlDown(),
4870 // default label to suppress warnings about "enumeration value
4871 // 'xxx' not handled in switch
4879 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4884 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4885 m_isDragging
= false;
4888 // ------------ Entering or leaving the window
4890 if ( event
.Entering() || event
.Leaving() )
4892 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4896 // ------------ Left button pressed
4898 else if ( event
.LeftDown() )
4900 // don't send a label click event for a hit on the
4901 // edge of the row label - this is probably the user
4902 // wanting to resize the row
4904 if ( YToEdgeOfRow(y
) < 0 )
4908 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4910 if ( !event
.ShiftDown() && !event
.ControlDown() )
4914 if ( event
.ShiftDown() )
4916 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4919 GetNumberCols() - 1,
4920 event
.ControlDown(),
4927 m_selection
->SelectRow( row
,
4928 event
.ControlDown(),
4935 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4940 // starting to drag-resize a row
4942 if ( CanDragRowSize() )
4943 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4948 // ------------ Left double click
4950 else if (event
.LeftDClick() )
4952 int row
= YToEdgeOfRow(y
);
4957 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4959 // no default action at the moment
4964 // adjust row height depending on label text
4965 AutoSizeRowLabelSize( row
);
4967 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4973 // ------------ Left button released
4975 else if ( event
.LeftUp() )
4977 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4979 DoEndDragResizeRow();
4981 // Note: we are ending the event *after* doing
4982 // default processing in this case
4984 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4987 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4992 // ------------ Right button down
4994 else if ( event
.RightDown() )
4998 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5000 // no default action at the moment
5005 // ------------ Right double click
5007 else if ( event
.RightDClick() )
5011 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5013 // no default action at the moment
5018 // ------------ No buttons down and mouse moving
5020 else if ( event
.Moving() )
5022 m_dragRowOrCol
= YToEdgeOfRow( y
);
5023 if ( m_dragRowOrCol
>= 0 )
5025 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5027 // don't capture the mouse yet
5028 if ( CanDragRowSize() )
5029 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5032 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5034 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5040 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5043 wxPoint
pos( event
.GetPosition() );
5044 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5046 if ( event
.Dragging() )
5050 m_isDragging
= true;
5051 m_colLabelWin
->CaptureMouse();
5054 if ( event
.LeftIsDown() )
5056 switch( m_cursorMode
)
5058 case WXGRID_CURSOR_RESIZE_COL
:
5060 int cw
, ch
, dummy
, top
;
5061 m_gridWin
->GetClientSize( &cw
, &ch
);
5062 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5064 wxClientDC
dc( m_gridWin
);
5067 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5068 GetColMinimalWidth(m_dragRowOrCol
));
5069 dc
.SetLogicalFunction(wxINVERT
);
5070 if ( m_dragLastPos
>= 0 )
5072 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5074 dc
.DrawLine( x
, top
, x
, top
+ch
);
5079 case WXGRID_CURSOR_SELECT_COL
:
5080 if ( (col
= XToCol( x
)) >= 0 )
5084 m_selection
->SelectCol( col
,
5085 event
.ControlDown(),
5092 // default label to suppress warnings about "enumeration value
5093 // 'xxx' not handled in switch
5101 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5106 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5107 m_isDragging
= false;
5110 // ------------ Entering or leaving the window
5112 if ( event
.Entering() || event
.Leaving() )
5114 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5118 // ------------ Left button pressed
5120 else if ( event
.LeftDown() )
5122 // don't send a label click event for a hit on the
5123 // edge of the col label - this is probably the user
5124 // wanting to resize the col
5126 if ( XToEdgeOfCol(x
) < 0 )
5130 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5132 if ( !event
.ShiftDown() && !event
.ControlDown() )
5136 if ( event
.ShiftDown() )
5138 m_selection
->SelectBlock( 0,
5139 m_currentCellCoords
.GetCol(),
5140 GetNumberRows() - 1, col
,
5141 event
.ControlDown(),
5148 m_selection
->SelectCol( col
,
5149 event
.ControlDown(),
5156 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5161 // starting to drag-resize a col
5163 if ( CanDragColSize() )
5164 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5169 // ------------ Left double click
5171 if ( event
.LeftDClick() )
5173 int col
= XToEdgeOfCol(x
);
5178 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5180 // no default action at the moment
5185 // adjust column width depending on label text
5186 AutoSizeColLabelSize( col
);
5188 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5194 // ------------ Left button released
5196 else if ( event
.LeftUp() )
5198 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5200 DoEndDragResizeCol();
5202 // Note: we are ending the event *after* doing
5203 // default processing in this case
5205 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5208 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5213 // ------------ Right button down
5215 else if ( event
.RightDown() )
5219 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5221 // no default action at the moment
5226 // ------------ Right double click
5228 else if ( event
.RightDClick() )
5232 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5234 // no default action at the moment
5239 // ------------ No buttons down and mouse moving
5241 else if ( event
.Moving() )
5243 m_dragRowOrCol
= XToEdgeOfCol( x
);
5244 if ( m_dragRowOrCol
>= 0 )
5246 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5248 // don't capture the cursor yet
5249 if ( CanDragColSize() )
5250 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5253 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5255 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5261 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5263 if ( event
.LeftDown() )
5265 // indicate corner label by having both row and
5268 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5274 else if ( event
.LeftDClick() )
5276 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5279 else if ( event
.RightDown() )
5281 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5283 // no default action at the moment
5287 else if ( event
.RightDClick() )
5289 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5291 // no default action at the moment
5296 void wxGrid::ChangeCursorMode(CursorMode mode
,
5301 static const wxChar
*cursorModes
[] =
5310 wxLogTrace(_T("grid"),
5311 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5312 win
== m_colLabelWin
? _T("colLabelWin")
5313 : win
? _T("rowLabelWin")
5315 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5316 #endif // __WXDEBUG__
5318 if ( mode
== m_cursorMode
&&
5319 win
== m_winCapture
&&
5320 captureMouse
== (m_winCapture
!= NULL
))
5325 // by default use the grid itself
5331 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5332 m_winCapture
= (wxWindow
*)NULL
;
5335 m_cursorMode
= mode
;
5337 switch ( m_cursorMode
)
5339 case WXGRID_CURSOR_RESIZE_ROW
:
5340 win
->SetCursor( m_rowResizeCursor
);
5343 case WXGRID_CURSOR_RESIZE_COL
:
5344 win
->SetCursor( m_colResizeCursor
);
5348 win
->SetCursor( *wxSTANDARD_CURSOR
);
5351 // we need to capture mouse when resizing
5352 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5353 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5355 if ( captureMouse
&& resize
)
5357 win
->CaptureMouse();
5362 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5365 wxPoint
pos( event
.GetPosition() );
5366 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5368 wxGridCellCoords coords
;
5369 XYToCell( x
, y
, coords
);
5371 int cell_rows
, cell_cols
;
5372 bool isFirstDrag
= !m_isDragging
;
5373 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5374 if ((cell_rows
< 0) || (cell_cols
< 0))
5376 coords
.SetRow(coords
.GetRow() + cell_rows
);
5377 coords
.SetCol(coords
.GetCol() + cell_cols
);
5380 if ( event
.Dragging() )
5382 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5384 // Don't start doing anything until the mouse has been drug at
5385 // least 3 pixels in any direction...
5388 if (m_startDragPos
== wxDefaultPosition
)
5390 m_startDragPos
= pos
;
5393 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5397 m_isDragging
= true;
5398 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5400 // Hide the edit control, so it
5401 // won't interfer with drag-shrinking.
5402 if ( IsCellEditControlShown() )
5404 HideCellEditControl();
5405 SaveEditControlValue();
5408 // Have we captured the mouse yet?
5411 m_winCapture
= m_gridWin
;
5412 m_winCapture
->CaptureMouse();
5415 if ( coords
!= wxGridNoCellCoords
)
5417 if ( event
.ControlDown() )
5419 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5420 m_selectingKeyboard
= coords
;
5421 HighlightBlock ( m_selectingKeyboard
, coords
);
5423 else if ( CanDragCell() )
5427 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5428 m_selectingKeyboard
= coords
;
5430 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5438 if ( !IsSelection() )
5440 HighlightBlock( coords
, coords
);
5444 HighlightBlock( m_currentCellCoords
, coords
);
5448 if (! IsVisible(coords
))
5450 MakeCellVisible(coords
);
5451 // TODO: need to introduce a delay or something here. The
5452 // scrolling is way to fast, at least on MSW - also on GTK.
5456 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5458 int cw
, ch
, left
, dummy
;
5459 m_gridWin
->GetClientSize( &cw
, &ch
);
5460 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5462 wxClientDC
dc( m_gridWin
);
5464 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5465 GetRowMinimalHeight(m_dragRowOrCol
) );
5466 dc
.SetLogicalFunction(wxINVERT
);
5467 if ( m_dragLastPos
>= 0 )
5469 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5471 dc
.DrawLine( left
, y
, left
+cw
, y
);
5474 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5476 int cw
, ch
, dummy
, top
;
5477 m_gridWin
->GetClientSize( &cw
, &ch
);
5478 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5480 wxClientDC
dc( m_gridWin
);
5482 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5483 GetColMinimalWidth(m_dragRowOrCol
) );
5484 dc
.SetLogicalFunction(wxINVERT
);
5485 if ( m_dragLastPos
>= 0 )
5487 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5489 dc
.DrawLine( x
, top
, x
, top
+ch
);
5496 m_isDragging
= false;
5497 m_startDragPos
= wxDefaultPosition
;
5499 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5500 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5503 if ( event
.Entering() || event
.Leaving() )
5505 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5506 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5511 // ------------ Left button pressed
5513 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5515 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5520 if ( !event
.ControlDown() )
5522 if ( event
.ShiftDown() )
5526 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5527 m_currentCellCoords
.GetCol(),
5530 event
.ControlDown(),
5536 else if ( XToEdgeOfCol(x
) < 0 &&
5537 YToEdgeOfRow(y
) < 0 )
5539 DisableCellEditControl();
5540 MakeCellVisible( coords
);
5542 if ( event
.ControlDown() )
5546 m_selection
->ToggleCellSelection( coords
.GetRow(),
5548 event
.ControlDown(),
5553 m_selectingTopLeft
= wxGridNoCellCoords
;
5554 m_selectingBottomRight
= wxGridNoCellCoords
;
5555 m_selectingKeyboard
= coords
;
5559 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5560 SetCurrentCell( coords
);
5563 if ( m_selection
->GetSelectionMode() !=
5564 wxGrid::wxGridSelectCells
)
5566 HighlightBlock( coords
, coords
);
5575 // ------------ Left double click
5577 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5579 DisableCellEditControl();
5581 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5583 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5588 // we want double click to select a cell and start editing
5589 // (i.e. to behave in same way as sequence of two slow clicks):
5590 m_waitForSlowClick
= true;
5597 // ------------ Left button released
5599 else if ( event
.LeftUp() )
5601 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5605 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5606 m_winCapture
= NULL
;
5609 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5612 EnableCellEditControl();
5614 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5615 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5616 editor
->StartingClick();
5620 m_waitForSlowClick
= false;
5622 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5623 m_selectingBottomRight
!= wxGridNoCellCoords
)
5627 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5628 m_selectingTopLeft
.GetCol(),
5629 m_selectingBottomRight
.GetRow(),
5630 m_selectingBottomRight
.GetCol(),
5631 event
.ControlDown(),
5637 m_selectingTopLeft
= wxGridNoCellCoords
;
5638 m_selectingBottomRight
= wxGridNoCellCoords
;
5640 // Show the edit control, if it has been hidden for
5642 ShowCellEditControl();
5645 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5647 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5648 DoEndDragResizeRow();
5650 // Note: we are ending the event *after* doing
5651 // default processing in this case
5653 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5655 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5657 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5658 DoEndDragResizeCol();
5660 // Note: we are ending the event *after* doing
5661 // default processing in this case
5663 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5670 // ------------ Right button down
5672 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5674 DisableCellEditControl();
5675 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5680 // no default action at the moment
5685 // ------------ Right double click
5687 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5689 DisableCellEditControl();
5690 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5695 // no default action at the moment
5699 // ------------ Moving and no button action
5701 else if ( event
.Moving() && !event
.IsButton() )
5703 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5705 // out of grid cell area
5706 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5710 int dragRow
= YToEdgeOfRow( y
);
5711 int dragCol
= XToEdgeOfCol( x
);
5713 // Dragging on the corner of a cell to resize in both
5714 // directions is not implemented yet...
5716 if ( dragRow
>= 0 && dragCol
>= 0 )
5718 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5724 m_dragRowOrCol
= dragRow
;
5726 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5728 if ( CanDragRowSize() && CanDragGridSize() )
5729 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5734 m_dragRowOrCol
= dragCol
;
5742 m_dragRowOrCol
= dragCol
;
5744 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5746 if ( CanDragColSize() && CanDragGridSize() )
5747 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5753 // Neither on a row or col edge
5755 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5757 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5763 void wxGrid::DoEndDragResizeRow()
5765 if ( m_dragLastPos
>= 0 )
5767 // erase the last line and resize the row
5769 int cw
, ch
, left
, dummy
;
5770 m_gridWin
->GetClientSize( &cw
, &ch
);
5771 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5773 wxClientDC
dc( m_gridWin
);
5775 dc
.SetLogicalFunction( wxINVERT
);
5776 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5777 HideCellEditControl();
5778 SaveEditControlValue();
5780 int rowTop
= GetRowTop(m_dragRowOrCol
);
5781 SetRowSize( m_dragRowOrCol
,
5782 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5784 if ( !GetBatchCount() )
5786 // Only needed to get the correct rect.y:
5787 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5789 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5790 rect
.width
= m_rowLabelWidth
;
5791 rect
.height
= ch
- rect
.y
;
5792 m_rowLabelWin
->Refresh( true, &rect
);
5794 // if there is a multicell block, paint all of it
5797 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5798 int leftCol
= XToCol(left
);
5799 int rightCol
= internalXToCol(left
+cw
);
5802 for (i
=leftCol
; i
<rightCol
; i
++)
5804 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5805 if (cell_rows
< subtract_rows
)
5806 subtract_rows
= cell_rows
;
5808 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5809 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5810 rect
.height
= ch
- rect
.y
;
5813 m_gridWin
->Refresh( false, &rect
);
5816 ShowCellEditControl();
5821 void wxGrid::DoEndDragResizeCol()
5823 if ( m_dragLastPos
>= 0 )
5825 // erase the last line and resize the col
5827 int cw
, ch
, dummy
, top
;
5828 m_gridWin
->GetClientSize( &cw
, &ch
);
5829 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5831 wxClientDC
dc( m_gridWin
);
5833 dc
.SetLogicalFunction( wxINVERT
);
5834 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5835 HideCellEditControl();
5836 SaveEditControlValue();
5838 int colLeft
= GetColLeft(m_dragRowOrCol
);
5839 SetColSize( m_dragRowOrCol
,
5840 wxMax( m_dragLastPos
- colLeft
,
5841 GetColMinimalWidth(m_dragRowOrCol
) ) );
5843 if ( !GetBatchCount() )
5845 // Only needed to get the correct rect.x:
5846 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5848 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5849 rect
.width
= cw
- rect
.x
;
5850 rect
.height
= m_colLabelHeight
;
5851 m_colLabelWin
->Refresh( true, &rect
);
5853 // if there is a multicell block, paint all of it
5856 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5857 int topRow
= YToRow(top
);
5858 int bottomRow
= internalYToRow(top
+cw
);
5861 for (i
=topRow
; i
<bottomRow
; i
++)
5863 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5864 if (cell_cols
< subtract_cols
)
5865 subtract_cols
= cell_cols
;
5867 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5868 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5869 rect
.width
= cw
- rect
.x
;
5872 m_gridWin
->Refresh( false, &rect
);
5875 ShowCellEditControl();
5882 // ------ interaction with data model
5884 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5886 switch ( msg
.GetId() )
5888 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5889 return GetModelValues();
5891 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5892 return SetModelValues();
5894 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5895 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5896 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5897 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5898 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5899 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5900 return Redimension( msg
);
5909 // The behaviour of this function depends on the grid table class
5910 // Clear() function. For the default wxGridStringTable class the
5911 // behavious is to replace all cell contents with wxEmptyString but
5912 // not to change the number of rows or cols.
5914 void wxGrid::ClearGrid()
5918 if (IsCellEditControlEnabled())
5919 DisableCellEditControl();
5922 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5927 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5929 // TODO: something with updateLabels flag
5933 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5939 if (IsCellEditControlEnabled())
5940 DisableCellEditControl();
5942 bool done
= m_table
->InsertRows( pos
, numRows
);
5945 // the table will have sent the results of the insert row
5946 // operation to this view object as a grid table message
5952 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5954 // TODO: something with updateLabels flag
5958 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5964 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5966 // the table will have sent the results of the append row
5967 // operation to this view object as a grid table message
5973 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5975 // TODO: something with updateLabels flag
5979 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5985 if (IsCellEditControlEnabled())
5986 DisableCellEditControl();
5988 bool done
= m_table
->DeleteRows( pos
, numRows
);
5990 // the table will have sent the results of the delete row
5991 // operation to this view object as a grid table message
5997 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5999 // TODO: something with updateLabels flag
6003 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6009 if (IsCellEditControlEnabled())
6010 DisableCellEditControl();
6012 bool done
= m_table
->InsertCols( pos
, numCols
);
6014 // the table will have sent the results of the insert col
6015 // operation to this view object as a grid table message
6021 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6023 // TODO: something with updateLabels flag
6027 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6033 bool done
= m_table
->AppendCols( numCols
);
6035 // the table will have sent the results of the append col
6036 // operation to this view object as a grid table message
6042 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6044 // TODO: something with updateLabels flag
6048 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6054 if (IsCellEditControlEnabled())
6055 DisableCellEditControl();
6057 bool done
= m_table
->DeleteCols( pos
, numCols
);
6059 // the table will have sent the results of the delete col
6060 // operation to this view object as a grid table message
6068 // ----- event handlers
6071 // Generate a grid event based on a mouse event and
6072 // return the result of ProcessEvent()
6074 int wxGrid::SendEvent( const wxEventType type
,
6076 wxMouseEvent
& mouseEv
)
6081 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6083 int rowOrCol
= (row
== -1 ? col
: row
);
6085 wxGridSizeEvent
gridEvt( GetId(),
6089 mouseEv
.GetX() + GetRowLabelSize(),
6090 mouseEv
.GetY() + GetColLabelSize(),
6091 mouseEv
.ControlDown(),
6092 mouseEv
.ShiftDown(),
6094 mouseEv
.MetaDown() );
6096 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6097 vetoed
= !gridEvt
.IsAllowed();
6099 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6101 // Right now, it should _never_ end up here!
6102 wxGridRangeSelectEvent
gridEvt( GetId(),
6106 m_selectingBottomRight
,
6108 mouseEv
.ControlDown(),
6109 mouseEv
.ShiftDown(),
6111 mouseEv
.MetaDown() );
6113 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6114 vetoed
= !gridEvt
.IsAllowed();
6118 wxGridEvent
gridEvt( GetId(),
6122 mouseEv
.GetX() + GetRowLabelSize(),
6123 mouseEv
.GetY() + GetColLabelSize(),
6125 mouseEv
.ControlDown(),
6126 mouseEv
.ShiftDown(),
6128 mouseEv
.MetaDown() );
6129 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6130 vetoed
= !gridEvt
.IsAllowed();
6133 // A Veto'd event may not be `claimed' so test this first
6134 if (vetoed
) return -1;
6135 return claimed
? 1 : 0;
6139 // Generate a grid event of specified type and return the result
6140 // of ProcessEvent().
6142 int wxGrid::SendEvent( const wxEventType type
,
6148 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6150 int rowOrCol
= (row
== -1 ? col
: row
);
6152 wxGridSizeEvent
gridEvt( GetId(),
6157 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6158 vetoed
= !gridEvt
.IsAllowed();
6162 wxGridEvent
gridEvt( GetId(),
6167 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6168 vetoed
= !gridEvt
.IsAllowed();
6171 // A Veto'd event may not be `claimed' so test this first
6172 if (vetoed
) return -1;
6173 return claimed
? 1 : 0;
6177 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6179 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6182 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6184 // Don't do anything if between Begin/EndBatch...
6185 // EndBatch() will do all this on the last nested one anyway.
6186 if (! GetBatchCount())
6188 // Refresh to get correct scrolled position:
6189 wxScrolledWindow::Refresh(eraseb
,rect
);
6193 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6194 int width_label
, width_cell
, height_label
, height_cell
;
6197 //Copy rectangle can get scroll offsets..
6198 rect_x
= rect
->GetX();
6199 rect_y
= rect
->GetY();
6200 rectWidth
= rect
->GetWidth();
6201 rectHeight
= rect
->GetHeight();
6203 width_label
= m_rowLabelWidth
- rect_x
;
6204 if (width_label
> rectWidth
) width_label
= rectWidth
;
6206 height_label
= m_colLabelHeight
- rect_y
;
6207 if (height_label
> rectHeight
) height_label
= rectHeight
;
6209 if (rect_x
> m_rowLabelWidth
)
6211 x
= rect_x
- m_rowLabelWidth
;
6212 width_cell
= rectWidth
;
6217 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6220 if (rect_y
> m_colLabelHeight
)
6222 y
= rect_y
- m_colLabelHeight
;
6223 height_cell
= rectHeight
;
6228 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6231 // Paint corner label part intersecting rect.
6232 if ( width_label
> 0 && height_label
> 0 )
6234 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6235 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6238 // Paint col labels part intersecting rect.
6239 if ( width_cell
> 0 && height_label
> 0 )
6241 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6242 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6245 // Paint row labels part intersecting rect.
6246 if ( width_label
> 0 && height_cell
> 0 )
6248 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6249 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6252 // Paint cell area part intersecting rect.
6253 if ( width_cell
> 0 && height_cell
> 0 )
6255 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6256 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6261 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6262 m_colLabelWin
->Refresh(eraseb
, NULL
);
6263 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6264 m_gridWin
->Refresh(eraseb
, NULL
);
6269 void wxGrid::OnSize( wxSizeEvent
& event
)
6271 // position the child windows
6274 // don't call CalcDimensions() from here, the base class handles the size
6280 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6282 if ( m_inOnKeyDown
)
6284 // shouldn't be here - we are going round in circles...
6286 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6289 m_inOnKeyDown
= true;
6291 // propagate the event up and see if it gets processed
6293 wxWindow
*parent
= GetParent();
6294 wxKeyEvent
keyEvt( event
);
6295 keyEvt
.SetEventObject( parent
);
6297 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6300 // try local handlers
6302 switch ( event
.GetKeyCode() )
6305 if ( event
.ControlDown() )
6307 MoveCursorUpBlock( event
.ShiftDown() );
6311 MoveCursorUp( event
.ShiftDown() );
6316 if ( event
.ControlDown() )
6318 MoveCursorDownBlock( event
.ShiftDown() );
6322 MoveCursorDown( event
.ShiftDown() );
6327 if ( event
.ControlDown() )
6329 MoveCursorLeftBlock( event
.ShiftDown() );
6333 MoveCursorLeft( event
.ShiftDown() );
6338 if ( event
.ControlDown() )
6340 MoveCursorRightBlock( event
.ShiftDown() );
6344 MoveCursorRight( event
.ShiftDown() );
6349 case WXK_NUMPAD_ENTER
:
6350 if ( event
.ControlDown() )
6352 event
.Skip(); // to let the edit control have the return
6356 if ( GetGridCursorRow() < GetNumberRows()-1 )
6358 MoveCursorDown( event
.ShiftDown() );
6362 // at the bottom of a column
6363 DisableCellEditControl();
6373 if (event
.ShiftDown())
6375 if ( GetGridCursorCol() > 0 )
6377 MoveCursorLeft( false );
6382 DisableCellEditControl();
6387 if ( GetGridCursorCol() < GetNumberCols()-1 )
6389 MoveCursorRight( false );
6394 DisableCellEditControl();
6400 if ( event
.ControlDown() )
6402 MakeCellVisible( 0, 0 );
6403 SetCurrentCell( 0, 0 );
6412 if ( event
.ControlDown() )
6414 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6415 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6432 if ( event
.ControlDown() )
6436 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6437 m_currentCellCoords
.GetCol(),
6438 event
.ControlDown(),
6445 if ( !IsEditable() )
6447 MoveCursorRight( false );
6450 // Otherwise fall through to default
6453 // is it possible to edit the current cell at all?
6454 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6456 // yes, now check whether the cells editor accepts the key
6457 int row
= m_currentCellCoords
.GetRow();
6458 int col
= m_currentCellCoords
.GetCol();
6459 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6460 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6462 // <F2> is special and will always start editing, for
6463 // other keys - ask the editor itself
6464 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6465 || editor
->IsAcceptedKey(event
) )
6467 // ensure cell is visble
6468 MakeCellVisible(row
, col
);
6469 EnableCellEditControl();
6471 // a problem can arise if the cell is not completely
6472 // visible (even after calling MakeCellVisible the
6473 // control is not created and calling StartingKey will
6475 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6487 // let others process char events with modifiers or all
6488 // char events for readonly cells
6495 m_inOnKeyDown
= false;
6498 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6500 // try local handlers
6502 if ( event
.GetKeyCode() == WXK_SHIFT
)
6504 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6505 m_selectingBottomRight
!= wxGridNoCellCoords
)
6509 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6510 m_selectingTopLeft
.GetCol(),
6511 m_selectingBottomRight
.GetRow(),
6512 m_selectingBottomRight
.GetCol(),
6513 event
.ControlDown(),
6520 m_selectingTopLeft
= wxGridNoCellCoords
;
6521 m_selectingBottomRight
= wxGridNoCellCoords
;
6522 m_selectingKeyboard
= wxGridNoCellCoords
;
6526 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6530 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6532 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6534 // the event has been intercepted - do nothing
6538 wxClientDC
dc(m_gridWin
);
6541 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6543 DisableCellEditControl();
6545 if ( IsVisible( m_currentCellCoords
, false ) )
6548 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6549 if ( !m_gridLinesEnabled
)
6557 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6559 // Otherwise refresh redraws the highlight!
6560 m_currentCellCoords
= coords
;
6562 DrawGridCellArea(dc
,cells
);
6563 DrawAllGridLines( dc
, r
);
6567 m_currentCellCoords
= coords
;
6569 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6570 DrawCellHighlight(dc
, attr
);
6575 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6578 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6582 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6585 rightCol
= GetNumberCols() - 1;
6587 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6590 bottomRow
= GetNumberRows() - 1;
6594 if ( topRow
> bottomRow
)
6601 if ( leftCol
> rightCol
)
6608 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6609 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6611 // First the case that we selected a completely new area
6612 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6613 m_selectingBottomRight
== wxGridNoCellCoords
)
6616 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6617 wxGridCellCoords ( bottomRow
, rightCol
) );
6618 m_gridWin
->Refresh( false, &rect
);
6620 // Now handle changing an existing selection area.
6621 else if ( m_selectingTopLeft
!= updateTopLeft
||
6622 m_selectingBottomRight
!= updateBottomRight
)
6624 // Compute two optimal update rectangles:
6625 // Either one rectangle is a real subset of the
6626 // other, or they are (almost) disjoint!
6628 bool need_refresh
[4];
6632 need_refresh
[3] = false;
6635 // Store intermediate values
6636 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6637 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6638 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6639 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6641 // Determine the outer/inner coordinates.
6642 if (oldLeft
> leftCol
)
6648 if (oldTop
> topRow
)
6654 if (oldRight
< rightCol
)
6657 oldRight
= rightCol
;
6660 if (oldBottom
< bottomRow
)
6663 oldBottom
= bottomRow
;
6667 // Now, either the stuff marked old is the outer
6668 // rectangle or we don't have a situation where one
6669 // is contained in the other.
6671 if ( oldLeft
< leftCol
)
6673 // Refresh the newly selected or deselected
6674 // area to the left of the old or new selection.
6675 need_refresh
[0] = true;
6676 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6678 wxGridCellCoords ( oldBottom
,
6682 if ( oldTop
< topRow
)
6684 // Refresh the newly selected or deselected
6685 // area above the old or new selection.
6686 need_refresh
[1] = true;
6687 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6689 wxGridCellCoords ( topRow
- 1,
6693 if ( oldRight
> rightCol
)
6695 // Refresh the newly selected or deselected
6696 // area to the right of the old or new selection.
6697 need_refresh
[2] = true;
6698 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6700 wxGridCellCoords ( oldBottom
,
6704 if ( oldBottom
> bottomRow
)
6706 // Refresh the newly selected or deselected
6707 // area below the old or new selection.
6708 need_refresh
[3] = true;
6709 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6711 wxGridCellCoords ( oldBottom
,
6715 // various Refresh() calls
6716 for (i
= 0; i
< 4; i
++ )
6717 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6718 m_gridWin
->Refresh( false, &(rect
[i
]) );
6721 m_selectingTopLeft
= updateTopLeft
;
6722 m_selectingBottomRight
= updateBottomRight
;
6726 // ------ functions to get/send data (see also public functions)
6729 bool wxGrid::GetModelValues()
6731 // Hide the editor, so it won't hide a changed value.
6732 HideCellEditControl();
6736 // all we need to do is repaint the grid
6738 m_gridWin
->Refresh();
6746 bool wxGrid::SetModelValues()
6750 // Disable the editor, so it won't hide a changed value.
6751 // Do we also want to save the current value of the editor first?
6753 DisableCellEditControl();
6757 for ( row
= 0; row
< m_numRows
; row
++ )
6759 for ( col
= 0; col
< m_numCols
; col
++ )
6761 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6773 // Note - this function only draws cells that are in the list of
6774 // exposed cells (usually set from the update region by
6775 // CalcExposedCells)
6777 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6779 if ( !m_numRows
|| !m_numCols
) return;
6781 int i
, numCells
= cells
.GetCount();
6782 int row
, col
, cell_rows
, cell_cols
;
6783 wxGridCellCoordsArray redrawCells
;
6785 for ( i
= numCells
-1; i
>= 0; i
-- )
6787 row
= cells
[i
].GetRow();
6788 col
= cells
[i
].GetCol();
6789 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6791 // If this cell is part of a multicell block, find owner for repaint
6792 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6794 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6795 bool marked
= false;
6796 for ( int j
= 0; j
< numCells
; j
++ )
6798 if ( cell
== cells
[j
] )
6806 int count
= redrawCells
.GetCount();
6807 for (int j
= 0; j
< count
; j
++)
6809 if ( cell
== redrawCells
[j
] )
6815 if (!marked
) redrawCells
.Add( cell
);
6817 continue; // don't bother drawing this cell
6820 // If this cell is empty, find cell to left that might want to overflow
6821 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6823 for ( int l
= 0; l
< cell_rows
; l
++ )
6825 // find a cell in this row to left alreay marked for repaint
6827 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6828 if ((redrawCells
[k
].GetCol() < left
) &&
6829 (redrawCells
[k
].GetRow() == row
))
6830 left
=redrawCells
[k
].GetCol();
6832 if (left
== col
) left
= 0; // oh well
6834 for (int j
= col
-1; j
>= left
; j
--)
6836 if (!m_table
->IsEmptyCell(row
+l
, j
))
6838 if (GetCellOverflow(row
+l
, j
))
6840 wxGridCellCoords
cell(row
+l
, j
);
6841 bool marked
= false;
6843 for (int k
= 0; k
< numCells
; k
++)
6845 if ( cell
== cells
[k
] )
6853 int count
= redrawCells
.GetCount();
6854 for (int k
= 0; k
< count
; k
++)
6856 if ( cell
== redrawCells
[k
] )
6862 if (!marked
) redrawCells
.Add( cell
);
6870 DrawCell( dc
, cells
[i
] );
6873 numCells
= redrawCells
.GetCount();
6875 for ( i
= numCells
- 1; i
>= 0; i
-- )
6877 DrawCell( dc
, redrawCells
[i
] );
6882 void wxGrid::DrawGridSpace( wxDC
& dc
)
6885 m_gridWin
->GetClientSize( &cw
, &ch
);
6888 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6890 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6891 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6893 if ( right
> rightCol
|| bottom
> bottomRow
)
6896 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6898 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6899 dc
.SetPen( *wxTRANSPARENT_PEN
);
6901 if ( right
> rightCol
)
6903 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6906 if ( bottom
> bottomRow
)
6908 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6914 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6916 int row
= coords
.GetRow();
6917 int col
= coords
.GetCol();
6919 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6922 // we draw the cell border ourselves
6923 #if !WXGRID_DRAW_LINES
6924 if ( m_gridLinesEnabled
)
6925 DrawCellBorder( dc
, coords
);
6928 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6930 bool isCurrent
= coords
== m_currentCellCoords
;
6932 wxRect rect
= CellToRect( row
, col
);
6934 // if the editor is shown, we should use it and not the renderer
6935 // Note: However, only if it is really _shown_, i.e. not hidden!
6936 if ( isCurrent
&& IsCellEditControlShown() )
6938 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6939 editor
->PaintBackground(rect
, attr
);
6944 // but all the rest is drawn by the cell renderer and hence may be
6946 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6947 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6954 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6956 int row
= m_currentCellCoords
.GetRow();
6957 int col
= m_currentCellCoords
.GetCol();
6959 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6962 wxRect rect
= CellToRect(row
, col
);
6964 // hmmm... what could we do here to show that the cell is disabled?
6965 // for now, I just draw a thinner border than for the other ones, but
6966 // it doesn't look really good
6968 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6972 // The center of th drawn line is where the position/width/height of
6973 // the rectangle is actually at, (on wxMSW atr least,) so we will
6974 // reduce the size of the rectangle to compensate for the thickness of
6975 // the line. If this is too strange on non wxMSW platforms then
6976 // please #ifdef this appropriately.
6977 rect
.x
+= penWidth
/2;
6978 rect
.y
+= penWidth
/2;
6979 rect
.width
-= penWidth
-1;
6980 rect
.height
-= penWidth
-1;
6983 // Now draw the rectangle
6984 // use the cellHighlightColour if the cell is inside a selection, this
6985 // will ensure the cell is always visible.
6986 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6987 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6988 dc
.DrawRectangle(rect
);
6992 // VZ: my experiments with 3d borders...
6994 // how to properly set colours for arbitrary bg?
6995 wxCoord x1
= rect
.x
,
6997 x2
= rect
.x
+ rect
.width
-1,
6998 y2
= rect
.y
+ rect
.height
-1;
7000 dc
.SetPen(*wxWHITE_PEN
);
7001 dc
.DrawLine(x1
, y1
, x2
, y1
);
7002 dc
.DrawLine(x1
, y1
, x1
, y2
);
7004 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7005 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7007 dc
.SetPen(*wxBLACK_PEN
);
7008 dc
.DrawLine(x1
, y2
, x2
, y2
);
7009 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7014 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7016 int row
= coords
.GetRow();
7017 int col
= coords
.GetCol();
7018 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7021 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7023 wxRect rect
= CellToRect( row
, col
);
7025 // right hand border
7027 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7028 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7032 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7033 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7036 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7038 // This if block was previously in wxGrid::OnPaint but that doesn't
7039 // seem to get called under wxGTK - MB
7041 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7042 m_numRows
&& m_numCols
)
7044 m_currentCellCoords
.Set(0, 0);
7047 if ( IsCellEditControlShown() )
7049 // don't show highlight when the edit control is shown
7053 // if the active cell was repainted, repaint its highlight too because it
7054 // might have been damaged by the grid lines
7055 size_t count
= cells
.GetCount();
7056 for ( size_t n
= 0; n
< count
; n
++ )
7058 if ( cells
[n
] == m_currentCellCoords
)
7060 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7061 DrawCellHighlight(dc
, attr
);
7069 // TODO: remove this ???
7070 // This is used to redraw all grid lines e.g. when the grid line colour
7073 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7075 #if !WXGRID_DRAW_LINES
7079 if ( !m_gridLinesEnabled
||
7081 !m_numCols
) return;
7083 int top
, bottom
, left
, right
;
7085 #if 0 //#ifndef __WXGTK__
7089 m_gridWin
->GetClientSize(&cw
, &ch
);
7091 // virtual coords of visible area
7093 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7094 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7099 reg
.GetBox(x
, y
, w
, h
);
7100 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7101 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7105 m_gridWin
->GetClientSize(&cw
, &ch
);
7106 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7107 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7110 // avoid drawing grid lines past the last row and col
7112 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7113 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7115 // no gridlines inside multicells, clip them out
7116 int leftCol
= internalXToCol(left
);
7117 int topRow
= internalYToRow(top
);
7118 int rightCol
= internalXToCol(right
);
7119 int bottomRow
= internalYToRow(bottom
);
7120 wxRegion
clippedcells(0, 0, cw
, ch
);
7123 int i
, j
, cell_rows
, cell_cols
;
7126 for (j
=topRow
; j
<bottomRow
; j
++)
7128 for (i
=leftCol
; i
<rightCol
; i
++)
7130 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7131 if ((cell_rows
> 1) || (cell_cols
> 1))
7133 rect
= CellToRect(j
,i
);
7134 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7135 clippedcells
.Subtract(rect
);
7137 else if ((cell_rows
< 0) || (cell_cols
< 0))
7139 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7140 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7141 clippedcells
.Subtract(rect
);
7145 dc
.SetClippingRegion( clippedcells
);
7147 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7149 // horizontal grid lines
7151 // already declared above - int i;
7152 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7154 int bot
= GetRowBottom(i
) - 1;
7163 dc
.DrawLine( left
, bot
, right
, bot
);
7168 // vertical grid lines
7170 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7172 int colRight
= GetColRight(i
) - 1;
7173 if ( colRight
> right
)
7178 if ( colRight
>= left
)
7180 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7183 dc
.DestroyClippingRegion();
7187 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7189 if ( !m_numRows
) return;
7192 size_t numLabels
= rows
.GetCount();
7194 for ( i
= 0; i
< numLabels
; i
++ )
7196 DrawRowLabel( dc
, rows
[i
] );
7201 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7203 if ( GetRowHeight(row
) <= 0 )
7206 int rowTop
= GetRowTop(row
),
7207 rowBottom
= GetRowBottom(row
) - 1;
7209 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7210 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7211 m_rowLabelWidth
-1, rowBottom
);
7213 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7215 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7217 dc
.SetPen( *wxWHITE_PEN
);
7218 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7219 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7221 dc
.SetBackgroundMode( wxTRANSPARENT
);
7222 dc
.SetTextForeground( GetLabelTextColour() );
7223 dc
.SetFont( GetLabelFont() );
7226 GetRowLabelAlignment( &hAlign
, &vAlign
);
7230 rect
.SetY( GetRowTop(row
) + 2 );
7231 rect
.SetWidth( m_rowLabelWidth
- 4 );
7232 rect
.SetHeight( GetRowHeight(row
) - 4 );
7233 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7237 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7239 if ( !m_numCols
) return;
7242 size_t numLabels
= cols
.GetCount();
7244 for ( i
= 0; i
< numLabels
; i
++ )
7246 DrawColLabel( dc
, cols
[i
] );
7251 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7253 if ( GetColWidth(col
) <= 0 )
7256 int colLeft
= GetColLeft(col
),
7257 colRight
= GetColRight(col
) - 1;
7259 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7260 dc
.DrawLine( colRight
, 0,
7261 colRight
, m_colLabelHeight
-1 );
7263 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7265 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7266 colRight
+1, m_colLabelHeight
-1 );
7268 dc
.SetPen( *wxWHITE_PEN
);
7269 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7270 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7272 dc
.SetBackgroundMode( wxTRANSPARENT
);
7273 dc
.SetTextForeground( GetLabelTextColour() );
7274 dc
.SetFont( GetLabelFont() );
7276 int hAlign
, vAlign
, orient
;
7277 GetColLabelAlignment( &hAlign
, &vAlign
);
7278 orient
= GetColLabelTextOrientation();
7281 rect
.SetX( colLeft
+ 2 );
7283 rect
.SetWidth( GetColWidth(col
) - 4 );
7284 rect
.SetHeight( m_colLabelHeight
- 4 );
7285 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7288 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7289 const wxString
& value
,
7293 int textOrientation
)
7295 wxArrayString lines
;
7297 StringToLines( value
, lines
);
7300 //Forward to new API.
7301 DrawTextRectangle( dc
,
7310 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7311 const wxArrayString
& lines
,
7315 int textOrientation
)
7317 long textWidth
, textHeight
;
7318 long lineWidth
, lineHeight
;
7321 dc
.SetClippingRegion( rect
);
7323 nLines
= lines
.GetCount();
7327 float x
= 0.0, y
= 0.0;
7329 if( textOrientation
== wxHORIZONTAL
)
7330 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7332 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7336 case wxALIGN_BOTTOM
:
7337 if( textOrientation
== wxHORIZONTAL
)
7338 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7340 x
= rect
.x
+ rect
.width
- textWidth
;
7343 case wxALIGN_CENTRE
:
7344 if( textOrientation
== wxHORIZONTAL
)
7345 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7347 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7352 if( textOrientation
== wxHORIZONTAL
)
7359 // Align each line of a multi-line label
7360 for( l
= 0; l
< nLines
; l
++ )
7362 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7364 switch( horizAlign
)
7367 if( textOrientation
== wxHORIZONTAL
)
7368 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7370 y
= rect
.y
+ lineWidth
+ 1;
7373 case wxALIGN_CENTRE
:
7374 if( textOrientation
== wxHORIZONTAL
)
7375 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7377 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7382 if( textOrientation
== wxHORIZONTAL
)
7385 y
= rect
.y
+ rect
.height
- 1;
7389 if( textOrientation
== wxHORIZONTAL
)
7391 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7396 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7401 dc
.DestroyClippingRegion();
7405 // Split multi line text up into an array of strings. Any existing
7406 // contents of the string array are preserved.
7408 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7412 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7413 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7415 while ( startPos
< (int)tVal
.Length() )
7417 pos
= tVal
.Mid(startPos
).Find( eol
);
7422 else if ( pos
== 0 )
7424 lines
.Add( wxEmptyString
);
7428 lines
.Add( value
.Mid(startPos
, pos
) );
7432 if ( startPos
< (int)value
.Length() )
7434 lines
.Add( value
.Mid( startPos
) );
7439 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7440 const wxArrayString
& lines
,
7441 long *width
, long *height
)
7448 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7450 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7451 w
= wxMax( w
, lineW
);
7460 // ------ Batch processing.
7462 void wxGrid::EndBatch()
7464 if ( m_batchCount
> 0 )
7467 if ( !m_batchCount
)
7470 m_rowLabelWin
->Refresh();
7471 m_colLabelWin
->Refresh();
7472 m_cornerLabelWin
->Refresh();
7473 m_gridWin
->Refresh();
7478 // Use this, rather than wxWindow::Refresh(), to force an immediate
7479 // repainting of the grid. Has no effect if you are already inside a
7480 // BeginBatch / EndBatch block.
7482 void wxGrid::ForceRefresh()
7490 // ------ Edit control functions
7494 void wxGrid::EnableEditing( bool edit
)
7496 // TODO: improve this ?
7498 if ( edit
!= m_editable
)
7500 if(!edit
) EnableCellEditControl(edit
);
7506 void wxGrid::EnableCellEditControl( bool enable
)
7511 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7512 SetCurrentCell( 0, 0 );
7514 if ( enable
!= m_cellEditCtrlEnabled
)
7518 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7521 // this should be checked by the caller!
7522 wxASSERT_MSG( CanEnableCellControl(),
7523 _T("can't enable editing for this cell!") );
7525 // do it before ShowCellEditControl()
7526 m_cellEditCtrlEnabled
= enable
;
7528 ShowCellEditControl();
7532 //FIXME:add veto support
7533 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7535 HideCellEditControl();
7536 SaveEditControlValue();
7538 // do it after HideCellEditControl()
7539 m_cellEditCtrlEnabled
= enable
;
7544 bool wxGrid::IsCurrentCellReadOnly() const
7547 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7548 bool readonly
= attr
->IsReadOnly();
7554 bool wxGrid::CanEnableCellControl() const
7556 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7557 !IsCurrentCellReadOnly();
7561 bool wxGrid::IsCellEditControlEnabled() const
7563 // the cell edit control might be disable for all cells or just for the
7564 // current one if it's read only
7565 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7568 bool wxGrid::IsCellEditControlShown() const
7570 bool isShown
= false;
7572 if ( m_cellEditCtrlEnabled
)
7574 int row
= m_currentCellCoords
.GetRow();
7575 int col
= m_currentCellCoords
.GetCol();
7576 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7577 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7582 if ( editor
->IsCreated() )
7584 isShown
= editor
->GetControl()->IsShown();
7594 void wxGrid::ShowCellEditControl()
7596 if ( IsCellEditControlEnabled() )
7598 if ( !IsVisible( m_currentCellCoords
) )
7600 m_cellEditCtrlEnabled
= false;
7605 wxRect rect
= CellToRect( m_currentCellCoords
);
7606 int row
= m_currentCellCoords
.GetRow();
7607 int col
= m_currentCellCoords
.GetCol();
7609 // if this is part of a multicell, find owner (topleft)
7610 int cell_rows
, cell_cols
;
7611 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7612 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7616 m_currentCellCoords
.SetRow( row
);
7617 m_currentCellCoords
.SetCol( col
);
7620 // convert to scrolled coords
7622 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7624 // done in PaintBackground()
7626 // erase the highlight and the cell contents because the editor
7627 // might not cover the entire cell
7628 wxClientDC
dc( m_gridWin
);
7630 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7631 dc
.SetPen(*wxTRANSPARENT_PEN
);
7632 dc
.DrawRectangle(rect
);
7635 // cell is shifted by one pixel
7636 // However, don't allow x or y to become negative
7637 // since the SetSize() method interprets that as
7644 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7645 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7646 if ( !editor
->IsCreated() )
7648 editor
->Create(m_gridWin
, wxID_ANY
,
7649 new wxGridCellEditorEvtHandler(this, editor
));
7651 wxGridEditorCreatedEvent
evt(GetId(),
7652 wxEVT_GRID_EDITOR_CREATED
,
7656 editor
->GetControl());
7657 GetEventHandler()->ProcessEvent(evt
);
7661 // resize editor to overflow into righthand cells if allowed
7662 int maxWidth
= rect
.width
;
7663 wxString value
= GetCellValue(row
, col
);
7664 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7667 GetTextExtent(value
, &maxWidth
, &y
,
7668 NULL
, NULL
, &attr
->GetFont());
7669 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7671 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7672 if (rect
.x
+maxWidth
> client_right
)
7673 maxWidth
= client_right
- rect
.x
;
7675 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7677 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7678 // may have changed earlier
7679 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7682 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7683 // looks weird going over a multicell
7684 if (m_table
->IsEmptyCell(row
,i
) &&
7685 (rect
.width
< maxWidth
) && (c_rows
== 1))
7686 rect
.width
+= GetColWidth(i
);
7690 if (rect
.GetRight() > client_right
)
7691 rect
.SetRight(client_right
-1);
7694 editor
->SetCellAttr(attr
);
7695 editor
->SetSize( rect
);
7696 editor
->Show( true, attr
);
7698 // recalc dimensions in case we need to
7699 // expand the scrolled window to account for editor
7702 editor
->BeginEdit(row
, col
, this);
7703 editor
->SetCellAttr(NULL
);
7712 void wxGrid::HideCellEditControl()
7714 if ( IsCellEditControlEnabled() )
7716 int row
= m_currentCellCoords
.GetRow();
7717 int col
= m_currentCellCoords
.GetCol();
7719 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7720 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7721 editor
->Show( false );
7724 m_gridWin
->SetFocus();
7725 // refresh whole row to the right
7726 wxRect
rect( CellToRect(row
, col
) );
7727 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7728 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7729 m_gridWin
->Refresh( false, &rect
);
7734 void wxGrid::SaveEditControlValue()
7736 if ( IsCellEditControlEnabled() )
7738 int row
= m_currentCellCoords
.GetRow();
7739 int col
= m_currentCellCoords
.GetCol();
7741 wxString oldval
= GetCellValue(row
,col
);
7743 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7744 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7745 bool changed
= editor
->EndEdit(row
, col
, this);
7752 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7753 m_currentCellCoords
.GetRow(),
7754 m_currentCellCoords
.GetCol() ) < 0 ) {
7756 // Event has been vetoed, set the data back.
7757 SetCellValue(row
,col
,oldval
);
7765 // ------ Grid location functions
7766 // Note that all of these functions work with the logical coordinates of
7767 // grid cells and labels so you will need to convert from device
7768 // coordinates for mouse events etc.
7771 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7773 int row
= YToRow(y
);
7774 int col
= XToCol(x
);
7776 if ( row
== -1 || col
== -1 )
7778 coords
= wxGridNoCellCoords
;
7782 coords
.Set( row
, col
);
7787 // Internal Helper function for computing row or column from some
7788 // (unscrolled) coordinate value, using either
7789 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7790 // of m_rowBottoms/m_ColRights to speed up the search!
7792 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7793 const wxArrayInt
& BorderArray
, int nMax
,
7798 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7804 size_t i_max
= coord
/ defaultDist
,
7807 if (BorderArray
.IsEmpty())
7809 if((int) i_max
< nMax
)
7811 return clipToMinMax
? nMax
- 1 : -1;
7814 if ( i_max
>= BorderArray
.GetCount())
7815 i_max
= BorderArray
.GetCount() - 1;
7818 if ( coord
>= BorderArray
[i_max
])
7822 i_max
= coord
/ minDist
;
7824 i_max
= BorderArray
.GetCount() - 1;
7826 if ( i_max
>= BorderArray
.GetCount())
7827 i_max
= BorderArray
.GetCount() - 1;
7829 if ( coord
>= BorderArray
[i_max
])
7830 return clipToMinMax
? (int)i_max
: -1;
7831 if ( coord
< BorderArray
[0] )
7834 while ( i_max
- i_min
> 0 )
7836 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7837 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7838 if (coord
>= BorderArray
[ i_max
- 1])
7842 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7843 if (coord
< BorderArray
[median
])
7851 int wxGrid::YToRow( int y
)
7853 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7854 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7858 int wxGrid::XToCol( int x
)
7860 return CoordToRowOrCol(x
, m_defaultColWidth
,
7861 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7865 // return the row number that that the y coord is near the edge of, or
7866 // -1 if not near an edge
7868 int wxGrid::YToEdgeOfRow( int y
)
7871 i
= internalYToRow(y
);
7873 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7875 // We know that we are in row i, test whether we are
7876 // close enough to lower or upper border, respectively.
7877 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7879 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7887 // return the col number that that the x coord is near the edge of, or
7888 // -1 if not near an edge
7890 int wxGrid::XToEdgeOfCol( int x
)
7893 i
= internalXToCol(x
);
7895 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7897 // We know that we are in column i, test whether we are
7898 // close enough to right or left border, respectively.
7899 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7901 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7909 wxRect
wxGrid::CellToRect( int row
, int col
)
7911 wxRect
rect( -1, -1, -1, -1 );
7913 if ( row
>= 0 && row
< m_numRows
&&
7914 col
>= 0 && col
< m_numCols
)
7916 int i
, cell_rows
, cell_cols
;
7917 rect
.width
= rect
.height
= 0;
7918 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7919 // if negative then find multicell owner
7920 if (cell_rows
< 0) row
+= cell_rows
;
7921 if (cell_cols
< 0) col
+= cell_cols
;
7922 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7924 rect
.x
= GetColLeft(col
);
7925 rect
.y
= GetRowTop(row
);
7926 for (i
=col
; i
<col
+cell_cols
; i
++)
7927 rect
.width
+= GetColWidth(i
);
7928 for (i
=row
; i
<row
+cell_rows
; i
++)
7929 rect
.height
+= GetRowHeight(i
);
7932 // if grid lines are enabled, then the area of the cell is a bit smaller
7933 if (m_gridLinesEnabled
) {
7941 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7943 // get the cell rectangle in logical coords
7945 wxRect
r( CellToRect( row
, col
) );
7947 // convert to device coords
7949 int left
, top
, right
, bottom
;
7950 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7951 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7953 // check against the client area of the grid window
7956 m_gridWin
->GetClientSize( &cw
, &ch
);
7958 if ( wholeCellVisible
)
7960 // is the cell wholly visible ?
7962 return ( left
>= 0 && right
<= cw
&&
7963 top
>= 0 && bottom
<= ch
);
7967 // is the cell partly visible ?
7969 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7970 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7975 // make the specified cell location visible by doing a minimal amount
7978 void wxGrid::MakeCellVisible( int row
, int col
)
7982 int xpos
= -1, ypos
= -1;
7984 if ( row
>= 0 && row
< m_numRows
&&
7985 col
>= 0 && col
< m_numCols
)
7987 // get the cell rectangle in logical coords
7989 wxRect
r( CellToRect( row
, col
) );
7991 // convert to device coords
7993 int left
, top
, right
, bottom
;
7994 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7995 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7998 m_gridWin
->GetClientSize( &cw
, &ch
);
8004 else if ( bottom
> ch
)
8006 int h
= r
.GetHeight();
8008 for ( i
= row
-1; i
>= 0; i
-- )
8010 int rowHeight
= GetRowHeight(i
);
8011 if ( h
+ rowHeight
> ch
)
8018 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8019 // have rounding errors (this is important, because if we do, we
8020 // might not scroll at all and some cells won't be redrawn)
8022 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8024 ypos
+= GRID_SCROLL_LINE_Y
;
8031 else if ( right
> cw
)
8033 // position the view so that the cell is on the right
8035 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8036 xpos
= x0
+ (right
- cw
);
8038 // see comment for ypos above
8039 xpos
+= GRID_SCROLL_LINE_X
;
8042 if ( xpos
!= -1 || ypos
!= -1 )
8045 xpos
/= GRID_SCROLL_LINE_X
;
8047 ypos
/= GRID_SCROLL_LINE_Y
;
8048 Scroll( xpos
, ypos
);
8056 // ------ Grid cursor movement functions
8059 bool wxGrid::MoveCursorUp( bool expandSelection
)
8061 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8062 m_currentCellCoords
.GetRow() >= 0 )
8064 if ( expandSelection
)
8066 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8067 m_selectingKeyboard
= m_currentCellCoords
;
8068 if ( m_selectingKeyboard
.GetRow() > 0 )
8070 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8071 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8072 m_selectingKeyboard
.GetCol() );
8073 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8076 else if ( m_currentCellCoords
.GetRow() > 0 )
8079 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8080 m_currentCellCoords
.GetCol() );
8081 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8082 m_currentCellCoords
.GetCol() );
8093 bool wxGrid::MoveCursorDown( bool expandSelection
)
8095 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8096 m_currentCellCoords
.GetRow() < m_numRows
)
8098 if ( expandSelection
)
8100 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8101 m_selectingKeyboard
= m_currentCellCoords
;
8102 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8104 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8105 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8106 m_selectingKeyboard
.GetCol() );
8107 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8110 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8113 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8114 m_currentCellCoords
.GetCol() );
8115 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8116 m_currentCellCoords
.GetCol() );
8127 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8129 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8130 m_currentCellCoords
.GetCol() >= 0 )
8132 if ( expandSelection
)
8134 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8135 m_selectingKeyboard
= m_currentCellCoords
;
8136 if ( m_selectingKeyboard
.GetCol() > 0 )
8138 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8139 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8140 m_selectingKeyboard
.GetCol() );
8141 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8144 else if ( m_currentCellCoords
.GetCol() > 0 )
8147 MakeCellVisible( m_currentCellCoords
.GetRow(),
8148 m_currentCellCoords
.GetCol() - 1 );
8149 SetCurrentCell( m_currentCellCoords
.GetRow(),
8150 m_currentCellCoords
.GetCol() - 1 );
8161 bool wxGrid::MoveCursorRight( bool expandSelection
)
8163 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8164 m_currentCellCoords
.GetCol() < m_numCols
)
8166 if ( expandSelection
)
8168 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8169 m_selectingKeyboard
= m_currentCellCoords
;
8170 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8172 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8173 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8174 m_selectingKeyboard
.GetCol() );
8175 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8178 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8181 MakeCellVisible( m_currentCellCoords
.GetRow(),
8182 m_currentCellCoords
.GetCol() + 1 );
8183 SetCurrentCell( m_currentCellCoords
.GetRow(),
8184 m_currentCellCoords
.GetCol() + 1 );
8195 bool wxGrid::MovePageUp()
8197 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8199 int row
= m_currentCellCoords
.GetRow();
8203 m_gridWin
->GetClientSize( &cw
, &ch
);
8205 int y
= GetRowTop(row
);
8206 int newRow
= internalYToRow( y
- ch
+ 1 );
8208 if ( newRow
== row
)
8210 //row > 0 , so newrow can never be less than 0 here.
8214 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8215 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8223 bool wxGrid::MovePageDown()
8225 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8227 int row
= m_currentCellCoords
.GetRow();
8228 if ( (row
+1) < m_numRows
)
8231 m_gridWin
->GetClientSize( &cw
, &ch
);
8233 int y
= GetRowTop(row
);
8234 int newRow
= internalYToRow( y
+ ch
);
8235 if ( newRow
== row
)
8237 // row < m_numRows , so newrow can't overflow here.
8241 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8242 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8250 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8253 m_currentCellCoords
!= wxGridNoCellCoords
&&
8254 m_currentCellCoords
.GetRow() > 0 )
8256 int row
= m_currentCellCoords
.GetRow();
8257 int col
= m_currentCellCoords
.GetCol();
8259 if ( m_table
->IsEmptyCell(row
, col
) )
8261 // starting in an empty cell: find the next block of
8267 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8270 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8272 // starting at the top of a block: find the next block
8278 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8283 // starting within a block: find the top of the block
8288 if ( m_table
->IsEmptyCell(row
, col
) )
8296 MakeCellVisible( row
, col
);
8297 if ( expandSelection
)
8299 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8300 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8305 SetCurrentCell( row
, col
);
8313 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8316 m_currentCellCoords
!= wxGridNoCellCoords
&&
8317 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8319 int row
= m_currentCellCoords
.GetRow();
8320 int col
= m_currentCellCoords
.GetCol();
8322 if ( m_table
->IsEmptyCell(row
, col
) )
8324 // starting in an empty cell: find the next block of
8327 while ( row
< m_numRows
-1 )
8330 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8333 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8335 // starting at the bottom of a block: find the next block
8338 while ( row
< m_numRows
-1 )
8341 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8346 // starting within a block: find the bottom of the block
8348 while ( row
< m_numRows
-1 )
8351 if ( m_table
->IsEmptyCell(row
, col
) )
8359 MakeCellVisible( row
, col
);
8360 if ( expandSelection
)
8362 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8363 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8368 SetCurrentCell( row
, col
);
8377 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8380 m_currentCellCoords
!= wxGridNoCellCoords
&&
8381 m_currentCellCoords
.GetCol() > 0 )
8383 int row
= m_currentCellCoords
.GetRow();
8384 int col
= m_currentCellCoords
.GetCol();
8386 if ( m_table
->IsEmptyCell(row
, col
) )
8388 // starting in an empty cell: find the next block of
8394 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8397 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8399 // starting at the left of a block: find the next block
8405 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8410 // starting within a block: find the left of the block
8415 if ( m_table
->IsEmptyCell(row
, col
) )
8423 MakeCellVisible( row
, col
);
8424 if ( expandSelection
)
8426 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8427 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8432 SetCurrentCell( row
, col
);
8441 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8444 m_currentCellCoords
!= wxGridNoCellCoords
&&
8445 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8447 int row
= m_currentCellCoords
.GetRow();
8448 int col
= m_currentCellCoords
.GetCol();
8450 if ( m_table
->IsEmptyCell(row
, col
) )
8452 // starting in an empty cell: find the next block of
8455 while ( col
< m_numCols
-1 )
8458 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8461 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8463 // starting at the right of a block: find the next block
8466 while ( col
< m_numCols
-1 )
8469 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8474 // starting within a block: find the right of the block
8476 while ( col
< m_numCols
-1 )
8479 if ( m_table
->IsEmptyCell(row
, col
) )
8487 MakeCellVisible( row
, col
);
8488 if ( expandSelection
)
8490 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8491 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8496 SetCurrentCell( row
, col
);
8508 // ------ Label values and formatting
8511 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8513 *horiz
= m_rowLabelHorizAlign
;
8514 *vert
= m_rowLabelVertAlign
;
8517 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8519 *horiz
= m_colLabelHorizAlign
;
8520 *vert
= m_colLabelVertAlign
;
8523 int wxGrid::GetColLabelTextOrientation()
8525 return m_colLabelTextOrientation
;
8528 wxString
wxGrid::GetRowLabelValue( int row
)
8532 return m_table
->GetRowLabelValue( row
);
8542 wxString
wxGrid::GetColLabelValue( int col
)
8546 return m_table
->GetColLabelValue( col
);
8557 void wxGrid::SetRowLabelSize( int width
)
8559 width
= wxMax( width
, 0 );
8560 if ( width
!= m_rowLabelWidth
)
8564 m_rowLabelWin
->Show( false );
8565 m_cornerLabelWin
->Show( false );
8567 else if ( m_rowLabelWidth
== 0 )
8569 m_rowLabelWin
->Show( true );
8570 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8573 m_rowLabelWidth
= width
;
8575 wxScrolledWindow::Refresh( true );
8580 void wxGrid::SetColLabelSize( int height
)
8582 height
= wxMax( height
, 0 );
8583 if ( height
!= m_colLabelHeight
)
8587 m_colLabelWin
->Show( false );
8588 m_cornerLabelWin
->Show( false );
8590 else if ( m_colLabelHeight
== 0 )
8592 m_colLabelWin
->Show( true );
8593 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8596 m_colLabelHeight
= height
;
8598 wxScrolledWindow::Refresh( true );
8603 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8605 if ( m_labelBackgroundColour
!= colour
)
8607 m_labelBackgroundColour
= colour
;
8608 m_rowLabelWin
->SetBackgroundColour( colour
);
8609 m_colLabelWin
->SetBackgroundColour( colour
);
8610 m_cornerLabelWin
->SetBackgroundColour( colour
);
8612 if ( !GetBatchCount() )
8614 m_rowLabelWin
->Refresh();
8615 m_colLabelWin
->Refresh();
8616 m_cornerLabelWin
->Refresh();
8621 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8623 if ( m_labelTextColour
!= colour
)
8625 m_labelTextColour
= colour
;
8626 if ( !GetBatchCount() )
8628 m_rowLabelWin
->Refresh();
8629 m_colLabelWin
->Refresh();
8634 void wxGrid::SetLabelFont( const wxFont
& font
)
8637 if ( !GetBatchCount() )
8639 m_rowLabelWin
->Refresh();
8640 m_colLabelWin
->Refresh();
8644 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8646 // allow old (incorrect) defs to be used
8649 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8650 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8651 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8656 case wxTOP
: vert
= wxALIGN_TOP
; break;
8657 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8658 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8661 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8663 m_rowLabelHorizAlign
= horiz
;
8666 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8668 m_rowLabelVertAlign
= vert
;
8671 if ( !GetBatchCount() )
8673 m_rowLabelWin
->Refresh();
8677 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8679 // allow old (incorrect) defs to be used
8682 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8683 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8684 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8689 case wxTOP
: vert
= wxALIGN_TOP
; break;
8690 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8691 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8694 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8696 m_colLabelHorizAlign
= horiz
;
8699 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8701 m_colLabelVertAlign
= vert
;
8704 if ( !GetBatchCount() )
8706 m_colLabelWin
->Refresh();
8710 // Note: under MSW, the default column label font must be changed because it
8711 // does not support vertical printing
8713 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8714 // pGrid->SetLabelFont(font);
8715 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8717 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8719 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8721 m_colLabelTextOrientation
= textOrientation
;
8724 if ( !GetBatchCount() )
8726 m_colLabelWin
->Refresh();
8730 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8734 m_table
->SetRowLabelValue( row
, s
);
8735 if ( !GetBatchCount() )
8737 wxRect rect
= CellToRect( row
, 0);
8738 if ( rect
.height
> 0 )
8740 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8742 rect
.width
= m_rowLabelWidth
;
8743 m_rowLabelWin
->Refresh( true, &rect
);
8749 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8753 m_table
->SetColLabelValue( col
, s
);
8754 if ( !GetBatchCount() )
8756 wxRect rect
= CellToRect( 0, col
);
8757 if ( rect
.width
> 0 )
8759 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8761 rect
.height
= m_colLabelHeight
;
8762 m_colLabelWin
->Refresh( true, &rect
);
8768 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8770 if ( m_gridLineColour
!= colour
)
8772 m_gridLineColour
= colour
;
8774 wxClientDC
dc( m_gridWin
);
8776 DrawAllGridLines( dc
, wxRegion() );
8781 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8783 if ( m_cellHighlightColour
!= colour
)
8785 m_cellHighlightColour
= colour
;
8787 wxClientDC
dc( m_gridWin
);
8789 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8790 DrawCellHighlight(dc
, attr
);
8795 void wxGrid::SetCellHighlightPenWidth(int width
)
8797 if (m_cellHighlightPenWidth
!= width
) {
8798 m_cellHighlightPenWidth
= width
;
8800 // Just redrawing the cell highlight is not enough since that won't
8801 // make any visible change if the the thickness is getting smaller.
8802 int row
= m_currentCellCoords
.GetRow();
8803 int col
= m_currentCellCoords
.GetCol();
8804 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8806 wxRect rect
= CellToRect(row
, col
);
8807 m_gridWin
->Refresh(true, &rect
);
8811 void wxGrid::SetCellHighlightROPenWidth(int width
)
8813 if (m_cellHighlightROPenWidth
!= width
) {
8814 m_cellHighlightROPenWidth
= width
;
8816 // Just redrawing the cell highlight is not enough since that won't
8817 // make any visible change if the the thickness is getting smaller.
8818 int row
= m_currentCellCoords
.GetRow();
8819 int col
= m_currentCellCoords
.GetCol();
8820 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8822 wxRect rect
= CellToRect(row
, col
);
8823 m_gridWin
->Refresh(true, &rect
);
8827 void wxGrid::EnableGridLines( bool enable
)
8829 if ( enable
!= m_gridLinesEnabled
)
8831 m_gridLinesEnabled
= enable
;
8833 if ( !GetBatchCount() )
8837 wxClientDC
dc( m_gridWin
);
8839 DrawAllGridLines( dc
, wxRegion() );
8843 m_gridWin
->Refresh();
8850 int wxGrid::GetDefaultRowSize()
8852 return m_defaultRowHeight
;
8855 int wxGrid::GetRowSize( int row
)
8857 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8859 return GetRowHeight(row
);
8862 int wxGrid::GetDefaultColSize()
8864 return m_defaultColWidth
;
8867 int wxGrid::GetColSize( int col
)
8869 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8871 return GetColWidth(col
);
8874 // ============================================================================
8875 // access to the grid attributes: each of them has a default value in the grid
8876 // itself and may be overidden on a per-cell basis
8877 // ============================================================================
8879 // ----------------------------------------------------------------------------
8880 // setting default attributes
8881 // ----------------------------------------------------------------------------
8883 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8885 m_defaultCellAttr
->SetBackgroundColour(col
);
8887 m_gridWin
->SetBackgroundColour(col
);
8891 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8893 m_defaultCellAttr
->SetTextColour(col
);
8896 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8898 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8901 void wxGrid::SetDefaultCellOverflow( bool allow
)
8903 m_defaultCellAttr
->SetOverflow(allow
);
8906 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8908 m_defaultCellAttr
->SetFont(font
);
8912 // For editors and renderers the type registry takes precedence over the
8913 // default attr, so we need to register the new editor/renderer for the string
8914 // data type in order to make setting a default editor/renderer appear to
8917 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8919 RegisterDataType(wxGRID_VALUE_STRING
,
8921 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
8924 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8926 RegisterDataType(wxGRID_VALUE_STRING
,
8927 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
8931 // ----------------------------------------------------------------------------
8932 // access to the default attrbiutes
8933 // ----------------------------------------------------------------------------
8935 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8937 return m_defaultCellAttr
->GetBackgroundColour();
8940 wxColour
wxGrid::GetDefaultCellTextColour()
8942 return m_defaultCellAttr
->GetTextColour();
8945 wxFont
wxGrid::GetDefaultCellFont()
8947 return m_defaultCellAttr
->GetFont();
8950 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8952 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8955 bool wxGrid::GetDefaultCellOverflow()
8957 return m_defaultCellAttr
->GetOverflow();
8960 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8962 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8965 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8967 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8970 // ----------------------------------------------------------------------------
8971 // access to cell attributes
8972 // ----------------------------------------------------------------------------
8974 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8976 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8977 wxColour colour
= attr
->GetBackgroundColour();
8982 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8984 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8985 wxColour colour
= attr
->GetTextColour();
8990 wxFont
wxGrid::GetCellFont( int row
, int col
)
8992 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8993 wxFont font
= attr
->GetFont();
8998 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9000 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9001 attr
->GetAlignment(horiz
, vert
);
9005 bool wxGrid::GetCellOverflow( int row
, int col
)
9007 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9008 bool allow
= attr
->GetOverflow();
9013 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9015 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9016 attr
->GetSize( num_rows
, num_cols
);
9020 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9022 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9023 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9029 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9031 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9032 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9038 bool wxGrid::IsReadOnly(int row
, int col
) const
9040 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9041 bool isReadOnly
= attr
->IsReadOnly();
9046 // ----------------------------------------------------------------------------
9047 // attribute support: cache, automatic provider creation, ...
9048 // ----------------------------------------------------------------------------
9050 bool wxGrid::CanHaveAttributes()
9057 return m_table
->CanHaveAttributes();
9060 void wxGrid::ClearAttrCache()
9062 if ( m_attrCache
.row
!= -1 )
9064 wxSafeDecRef(m_attrCache
.attr
);
9065 m_attrCache
.attr
= NULL
;
9066 m_attrCache
.row
= -1;
9070 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9074 wxGrid
*self
= (wxGrid
*)this; // const_cast
9076 self
->ClearAttrCache();
9077 self
->m_attrCache
.row
= row
;
9078 self
->m_attrCache
.col
= col
;
9079 self
->m_attrCache
.attr
= attr
;
9084 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9086 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9088 *attr
= m_attrCache
.attr
;
9089 wxSafeIncRef(m_attrCache
.attr
);
9091 #ifdef DEBUG_ATTR_CACHE
9092 gs_nAttrCacheHits
++;
9099 #ifdef DEBUG_ATTR_CACHE
9100 gs_nAttrCacheMisses
++;
9106 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9108 wxGridCellAttr
*attr
= NULL
;
9109 // Additional test to avoid looking at the cache e.g. for
9110 // wxNoCellCoords, as this will confuse memory management.
9113 if ( !LookupAttr(row
, col
, &attr
) )
9115 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9116 : (wxGridCellAttr
*)NULL
;
9117 CacheAttr(row
, col
, attr
);
9122 attr
->SetDefAttr(m_defaultCellAttr
);
9126 attr
= m_defaultCellAttr
;
9133 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9135 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9136 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9138 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9139 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9141 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9144 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9146 // artificially inc the ref count to match DecRef() in caller
9148 m_table
->SetAttr(attr
, row
, col
);
9154 // ----------------------------------------------------------------------------
9155 // setting column attributes (wrappers around SetColAttr)
9156 // ----------------------------------------------------------------------------
9158 void wxGrid::SetColFormatBool(int col
)
9160 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9163 void wxGrid::SetColFormatNumber(int col
)
9165 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9168 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9170 wxString typeName
= wxGRID_VALUE_FLOAT
;
9171 if ( (width
!= -1) || (precision
!= -1) )
9173 typeName
<< _T(':') << width
<< _T(',') << precision
;
9176 SetColFormatCustom(col
, typeName
);
9179 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9181 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9183 attr
= new wxGridCellAttr
;
9184 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9185 attr
->SetRenderer(renderer
);
9187 SetColAttr(col
, attr
);
9191 // ----------------------------------------------------------------------------
9192 // setting cell attributes: this is forwarded to the table
9193 // ----------------------------------------------------------------------------
9195 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9197 if ( CanHaveAttributes() )
9199 m_table
->SetAttr(attr
, row
, col
);
9208 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9210 if ( CanHaveAttributes() )
9212 m_table
->SetRowAttr(attr
, row
);
9221 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9223 if ( CanHaveAttributes() )
9225 m_table
->SetColAttr(attr
, col
);
9234 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9236 if ( CanHaveAttributes() )
9238 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9239 attr
->SetBackgroundColour(colour
);
9244 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9246 if ( CanHaveAttributes() )
9248 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9249 attr
->SetTextColour(colour
);
9254 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9256 if ( CanHaveAttributes() )
9258 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9259 attr
->SetFont(font
);
9264 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9266 if ( CanHaveAttributes() )
9268 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9269 attr
->SetAlignment(horiz
, vert
);
9274 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9276 if ( CanHaveAttributes() )
9278 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9279 attr
->SetOverflow(allow
);
9284 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9286 if ( CanHaveAttributes() )
9288 int cell_rows
, cell_cols
;
9290 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9291 attr
->GetSize(&cell_rows
, &cell_cols
);
9292 attr
->SetSize(num_rows
, num_cols
);
9295 // Cannot set the size of a cell to 0 or negative values
9296 // While it is perfectly legal to do that, this function cannot
9297 // handle all the possibilies, do it by hand by getting the CellAttr.
9298 // You can only set the size of a cell to 1,1 or greater with this fn
9299 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9300 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9301 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9302 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9304 // if this was already a multicell then "turn off" the other cells first
9305 if ((cell_rows
> 1) || (cell_rows
> 1))
9308 for (j
=row
; j
<row
+cell_rows
; j
++)
9310 for (i
=col
; i
<col
+cell_cols
; i
++)
9312 if ((i
!= col
) || (j
!= row
))
9314 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9315 attr_stub
->SetSize( 1, 1 );
9316 attr_stub
->DecRef();
9322 // mark the cells that will be covered by this cell to
9323 // negative or zero values to point back at this cell
9324 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9327 for (j
=row
; j
<row
+num_rows
; j
++)
9329 for (i
=col
; i
<col
+num_cols
; i
++)
9331 if ((i
!= col
) || (j
!= row
))
9333 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9334 attr_stub
->SetSize( row
-j
, col
-i
);
9335 attr_stub
->DecRef();
9343 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9345 if ( CanHaveAttributes() )
9347 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9348 attr
->SetRenderer(renderer
);
9353 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9355 if ( CanHaveAttributes() )
9357 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9358 attr
->SetEditor(editor
);
9363 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9365 if ( CanHaveAttributes() )
9367 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9368 attr
->SetReadOnly(isReadOnly
);
9373 // ----------------------------------------------------------------------------
9374 // Data type registration
9375 // ----------------------------------------------------------------------------
9377 void wxGrid::RegisterDataType(const wxString
& typeName
,
9378 wxGridCellRenderer
* renderer
,
9379 wxGridCellEditor
* editor
)
9381 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9385 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9387 wxString typeName
= m_table
->GetTypeName(row
, col
);
9388 return GetDefaultEditorForType(typeName
);
9391 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9393 wxString typeName
= m_table
->GetTypeName(row
, col
);
9394 return GetDefaultRendererForType(typeName
);
9398 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9400 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9401 if ( index
== wxNOT_FOUND
)
9403 wxFAIL_MSG(wxT("Unknown data type name"));
9408 return m_typeRegistry
->GetEditor(index
);
9412 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9414 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9415 if ( index
== wxNOT_FOUND
)
9417 wxFAIL_MSG(wxT("Unknown data type name"));
9422 return m_typeRegistry
->GetRenderer(index
);
9426 // ----------------------------------------------------------------------------
9428 // ----------------------------------------------------------------------------
9430 void wxGrid::EnableDragRowSize( bool enable
)
9432 m_canDragRowSize
= enable
;
9436 void wxGrid::EnableDragColSize( bool enable
)
9438 m_canDragColSize
= enable
;
9441 void wxGrid::EnableDragGridSize( bool enable
)
9443 m_canDragGridSize
= enable
;
9446 void wxGrid::EnableDragCell( bool enable
)
9448 m_canDragCell
= enable
;
9451 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9453 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9455 if ( resizeExistingRows
)
9457 // since we are resizing all rows to the default row size,
9458 // we can simply clear the row heights and row bottoms
9459 // arrays (which also allows us to take advantage of
9460 // some speed optimisations)
9461 m_rowHeights
.Empty();
9462 m_rowBottoms
.Empty();
9463 if ( !GetBatchCount() )
9468 void wxGrid::SetRowSize( int row
, int height
)
9470 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9472 // See comment in SetColSize
9473 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9475 if ( m_rowHeights
.IsEmpty() )
9477 // need to really create the array
9481 int h
= wxMax( 0, height
);
9482 int diff
= h
- m_rowHeights
[row
];
9484 m_rowHeights
[row
] = h
;
9486 for ( i
= row
; i
< m_numRows
; i
++ )
9488 m_rowBottoms
[i
] += diff
;
9490 if ( !GetBatchCount() )
9494 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9496 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9498 if ( resizeExistingCols
)
9500 // since we are resizing all columns to the default column size,
9501 // we can simply clear the col widths and col rights
9502 // arrays (which also allows us to take advantage of
9503 // some speed optimisations)
9504 m_colWidths
.Empty();
9505 m_colRights
.Empty();
9506 if ( !GetBatchCount() )
9511 void wxGrid::SetColSize( int col
, int width
)
9513 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9515 // should we check that it's bigger than GetColMinimalWidth(col) here?
9517 // No, because it is reasonable to assume the library user know's
9518 // what he is doing. However whe should test against the weaker
9519 // constariant of minimalAcceptableWidth, as this breaks rendering
9521 // This test then fixes sf.net bug #645734
9523 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9525 if ( m_colWidths
.IsEmpty() )
9527 // need to really create the array
9531 // if < 0 calc new width from label
9535 wxArrayString lines
;
9536 wxClientDC
dc(m_colLabelWin
);
9537 dc
.SetFont(GetLabelFont());
9538 StringToLines(GetColLabelValue(col
), lines
);
9539 GetTextBoxSize(dc
, lines
, &w
, &h
);
9542 int w
= wxMax( 0, width
);
9543 int diff
= w
- m_colWidths
[col
];
9544 m_colWidths
[col
] = w
;
9547 for ( i
= col
; i
< m_numCols
; i
++ )
9549 m_colRights
[i
] += diff
;
9551 if ( !GetBatchCount() )
9556 void wxGrid::SetColMinimalWidth( int col
, int width
)
9558 if (width
> GetColMinimalAcceptableWidth()) {
9559 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9560 m_colMinWidths
[key
] = width
;
9564 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9566 if (width
> GetRowMinimalAcceptableHeight()) {
9567 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9568 m_rowMinHeights
[key
] = width
;
9572 int wxGrid::GetColMinimalWidth(int col
) const
9574 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9575 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9576 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9579 int wxGrid::GetRowMinimalHeight(int row
) const
9581 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9582 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9583 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9586 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9588 // We do allow a width of 0 since this gives us
9589 // an easy way to temporarily hidding columns.
9592 m_minAcceptableColWidth
= width
;
9595 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9597 // We do allow a height of 0 since this gives us
9598 // an easy way to temporarily hidding rows.
9601 m_minAcceptableRowHeight
= height
;
9604 int wxGrid::GetColMinimalAcceptableWidth() const
9606 return m_minAcceptableColWidth
;
9609 int wxGrid::GetRowMinimalAcceptableHeight() const
9611 return m_minAcceptableRowHeight
;
9614 // ----------------------------------------------------------------------------
9616 // ----------------------------------------------------------------------------
9618 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9620 wxClientDC
dc(m_gridWin
);
9622 //Cancel editting of cell
9623 HideCellEditControl();
9624 SaveEditControlValue();
9626 // init both of them to avoid compiler warnings, even if weo nly need one
9634 wxCoord extent
, extentMax
= 0;
9635 int max
= column
? m_numRows
: m_numCols
;
9636 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9643 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9644 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9647 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9648 extent
= column
? size
.x
: size
.y
;
9649 if ( extent
> extentMax
)
9660 // now also compare with the column label extent
9662 dc
.SetFont( GetLabelFont() );
9666 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9667 if( GetColLabelTextOrientation() == wxVERTICAL
)
9671 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9673 extent
= column
? w
: h
;
9674 if ( extent
> extentMax
)
9681 // empty column - give default extent (notice that if extentMax is less
9682 // than default extent but != 0, it's ok)
9683 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9689 // leave some space around text
9700 SetColSize(col
, extentMax
);
9701 if ( !GetBatchCount() )
9704 m_gridWin
->GetClientSize( &cw
, &ch
);
9705 wxRect
rect ( CellToRect( 0, col
) );
9707 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9708 rect
.width
= cw
- rect
.x
;
9709 rect
.height
= m_colLabelHeight
;
9710 m_colLabelWin
->Refresh( true, &rect
);
9715 SetRowSize(row
, extentMax
);
9716 if ( !GetBatchCount() )
9719 m_gridWin
->GetClientSize( &cw
, &ch
);
9720 wxRect
rect ( CellToRect( row
, 0 ) );
9722 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9723 rect
.width
= m_rowLabelWidth
;
9724 rect
.height
= ch
- rect
.y
;
9725 m_rowLabelWin
->Refresh( true, &rect
);
9731 SetColMinimalWidth(col
, extentMax
);
9733 SetRowMinimalHeight(row
, extentMax
);
9737 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9739 int width
= m_rowLabelWidth
;
9744 for ( int col
= 0; col
< m_numCols
; col
++ )
9748 AutoSizeColumn(col
, setAsMin
);
9751 width
+= GetColWidth(col
);
9760 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9762 int height
= m_colLabelHeight
;
9767 for ( int row
= 0; row
< m_numRows
; row
++ )
9771 AutoSizeRow(row
, setAsMin
);
9774 height
+= GetRowHeight(row
);
9783 void wxGrid::AutoSize()
9787 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9789 // round up the size to a multiple of scroll step - this ensures that we
9790 // won't get the scrollbars if we're sized exactly to this width
9791 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9793 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9794 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9796 // distribute the extra space between the columns/rows to avoid having
9797 // extra white space
9799 // Remove the extra m_extraWidth + 1 added above
9800 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9801 if ( diff
&& m_numCols
)
9803 // try to resize the columns uniformly
9804 wxCoord diffPerCol
= diff
/ m_numCols
;
9807 for ( int col
= 0; col
< m_numCols
; col
++ )
9809 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9813 // add remaining amount to the last columns
9814 diff
-= diffPerCol
* m_numCols
;
9817 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9819 SetColSize(col
, GetColWidth(col
) + 1);
9825 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9826 if ( diff
&& m_numRows
)
9828 // try to resize the columns uniformly
9829 wxCoord diffPerRow
= diff
/ m_numRows
;
9832 for ( int row
= 0; row
< m_numRows
; row
++ )
9834 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9838 // add remaining amount to the last rows
9839 diff
-= diffPerRow
* m_numRows
;
9842 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9844 SetRowSize(row
, GetRowHeight(row
) + 1);
9851 SetClientSize(sizeFit
);
9854 void wxGrid::AutoSizeRowLabelSize( int row
)
9856 wxArrayString lines
;
9859 // Hide the edit control, so it
9860 // won't interfer with drag-shrinking.
9861 if( IsCellEditControlShown() )
9863 HideCellEditControl();
9864 SaveEditControlValue();
9867 // autosize row height depending on label text
9868 StringToLines( GetRowLabelValue( row
), lines
);
9869 wxClientDC
dc( m_rowLabelWin
);
9870 GetTextBoxSize( dc
, lines
, &w
, &h
);
9871 if( h
< m_defaultRowHeight
)
9872 h
= m_defaultRowHeight
;
9877 void wxGrid::AutoSizeColLabelSize( int col
)
9879 wxArrayString lines
;
9882 // Hide the edit control, so it
9883 // won't interfer with drag-shrinking.
9884 if( IsCellEditControlShown() )
9886 HideCellEditControl();
9887 SaveEditControlValue();
9890 // autosize column width depending on label text
9891 StringToLines( GetColLabelValue( col
), lines
);
9892 wxClientDC
dc( m_colLabelWin
);
9893 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9894 GetTextBoxSize( dc
, lines
, &w
, &h
);
9896 GetTextBoxSize( dc
, lines
, &h
, &w
);
9897 if( w
< m_defaultColWidth
)
9898 w
= m_defaultColWidth
;
9903 wxSize
wxGrid::DoGetBestSize() const
9905 // don't set sizes, only calculate them
9906 wxGrid
*self
= (wxGrid
*)this; // const_cast
9909 width
= self
->SetOrCalcColumnSizes(true);
9910 height
= self
->SetOrCalcRowSizes(true);
9912 if (!width
) width
=100;
9913 if (!height
) height
=80;
9915 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9916 // of the scrollbars, is there any magic incantaion for that?
9918 GetScrollPixelsPerUnit(&xpu
, &ypu
);
9920 width
+= 1 + xpu
- (width
% xpu
);
9922 height
+= 1 + ypu
- (height
% ypu
);
9924 // limit to 1/4 of the screen size
9925 int maxwidth
, maxheight
;
9926 wxDisplaySize( & maxwidth
, & maxheight
);
9929 if ( width
> maxwidth
) width
= maxwidth
;
9930 if ( height
> maxheight
) height
= maxheight
;
9933 wxSize
best(width
, height
);
9934 // NOTE: This size should be cached, but first we need to add calls to
9935 // InvalidateBestSize everywhere that could change the results of this
9937 // CacheBestSize(size);
9947 wxPen
& wxGrid::GetDividerPen() const
9952 // ----------------------------------------------------------------------------
9953 // cell value accessor functions
9954 // ----------------------------------------------------------------------------
9956 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9960 m_table
->SetValue( row
, col
, s
);
9961 if ( !GetBatchCount() )
9964 wxRect
rect( CellToRect( row
, col
) );
9966 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9967 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9968 m_gridWin
->Refresh( false, &rect
);
9971 if ( m_currentCellCoords
.GetRow() == row
&&
9972 m_currentCellCoords
.GetCol() == col
&&
9973 IsCellEditControlShown())
9974 // Note: If we are using IsCellEditControlEnabled,
9975 // this interacts badly with calling SetCellValue from
9976 // an EVT_GRID_CELL_CHANGE handler.
9978 HideCellEditControl();
9979 ShowCellEditControl(); // will reread data from table
9986 // ------ Block, row and col selection
9989 void wxGrid::SelectRow( int row
, bool addToSelected
)
9991 if ( IsSelection() && !addToSelected
)
9995 m_selection
->SelectRow( row
, false, addToSelected
);
9999 void wxGrid::SelectCol( int col
, bool addToSelected
)
10001 if ( IsSelection() && !addToSelected
)
10005 m_selection
->SelectCol( col
, false, addToSelected
);
10009 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10010 bool addToSelected
)
10012 if ( IsSelection() && !addToSelected
)
10016 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10017 false, addToSelected
);
10021 void wxGrid::SelectAll()
10023 if ( m_numRows
> 0 && m_numCols
> 0 )
10026 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10031 // ------ Cell, row and col deselection
10034 void wxGrid::DeselectRow( int row
)
10036 if ( !m_selection
)
10039 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10041 if ( m_selection
->IsInSelection(row
, 0 ) )
10042 m_selection
->ToggleCellSelection( row
, 0);
10046 int nCols
= GetNumberCols();
10047 for ( int i
= 0; i
< nCols
; i
++ )
10049 if ( m_selection
->IsInSelection(row
, i
) )
10050 m_selection
->ToggleCellSelection( row
, i
);
10055 void wxGrid::DeselectCol( int col
)
10057 if ( !m_selection
)
10060 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10062 if ( m_selection
->IsInSelection(0, col
) )
10063 m_selection
->ToggleCellSelection( 0, col
);
10067 int nRows
= GetNumberRows();
10068 for ( int i
= 0; i
< nRows
; i
++ )
10070 if ( m_selection
->IsInSelection(i
, col
) )
10071 m_selection
->ToggleCellSelection(i
, col
);
10076 void wxGrid::DeselectCell( int row
, int col
)
10078 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10079 m_selection
->ToggleCellSelection(row
, col
);
10082 bool wxGrid::IsSelection()
10084 return ( m_selection
&& (m_selection
->IsSelection() ||
10085 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10086 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10089 bool wxGrid::IsInSelection( int row
, int col
) const
10091 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10092 ( row
>= m_selectingTopLeft
.GetRow() &&
10093 col
>= m_selectingTopLeft
.GetCol() &&
10094 row
<= m_selectingBottomRight
.GetRow() &&
10095 col
<= m_selectingBottomRight
.GetCol() )) );
10098 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10100 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10101 return m_selection
->m_cellSelection
;
10103 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10105 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10106 return m_selection
->m_blockSelectionTopLeft
;
10108 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10110 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10111 return m_selection
->m_blockSelectionBottomRight
;
10113 wxArrayInt
wxGrid::GetSelectedRows() const
10115 if (!m_selection
) { wxArrayInt a
; return a
; }
10116 return m_selection
->m_rowSelection
;
10118 wxArrayInt
wxGrid::GetSelectedCols() const
10120 if (!m_selection
) { wxArrayInt a
; return a
; }
10121 return m_selection
->m_colSelection
;
10125 void wxGrid::ClearSelection()
10127 m_selectingTopLeft
= wxGridNoCellCoords
;
10128 m_selectingBottomRight
= wxGridNoCellCoords
;
10130 m_selection
->ClearSelection();
10134 // This function returns the rectangle that encloses the given block
10135 // in device coords clipped to the client size of the grid window.
10137 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10138 const wxGridCellCoords
&bottomRight
)
10140 wxRect
rect( wxGridNoCellRect
);
10143 cellRect
= CellToRect( topLeft
);
10144 if ( cellRect
!= wxGridNoCellRect
)
10150 rect
= wxRect( 0, 0, 0, 0 );
10153 cellRect
= CellToRect( bottomRight
);
10154 if ( cellRect
!= wxGridNoCellRect
)
10160 return wxGridNoCellRect
;
10164 int left
= rect
.GetLeft();
10165 int top
= rect
.GetTop();
10166 int right
= rect
.GetRight();
10167 int bottom
= rect
.GetBottom();
10169 int leftCol
= topLeft
.GetCol();
10170 int topRow
= topLeft
.GetRow();
10171 int rightCol
= bottomRight
.GetCol();
10172 int bottomRow
= bottomRight
.GetRow();
10190 topRow
= bottomRow
;
10195 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10197 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10199 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10201 cellRect
= CellToRect( j
, i
);
10203 if (cellRect
.x
< left
)
10205 if (cellRect
.y
< top
)
10207 if (cellRect
.x
+ cellRect
.width
> right
)
10208 right
= cellRect
.x
+ cellRect
.width
;
10209 if (cellRect
.y
+ cellRect
.height
> bottom
)
10210 bottom
= cellRect
.y
+ cellRect
.height
;
10212 else i
= rightCol
; // jump over inner cells.
10216 // convert to scrolled coords
10218 CalcScrolledPosition( left
, top
, &left
, &top
);
10219 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10222 m_gridWin
->GetClientSize( &cw
, &ch
);
10224 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10225 return wxRect( 0, 0, 0, 0);
10227 rect
.SetLeft( wxMax(0, left
) );
10228 rect
.SetTop( wxMax(0, top
) );
10229 rect
.SetRight( wxMin(cw
, right
) );
10230 rect
.SetBottom( wxMin(ch
, bottom
) );
10236 // ------ Grid event classes
10239 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10241 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10242 int row
, int col
, int x
, int y
, bool sel
,
10243 bool control
, bool shift
, bool alt
, bool meta
)
10244 : wxNotifyEvent( type
, id
)
10251 m_control
= control
;
10256 SetEventObject(obj
);
10260 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10262 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10263 int rowOrCol
, int x
, int y
,
10264 bool control
, bool shift
, bool alt
, bool meta
)
10265 : wxNotifyEvent( type
, id
)
10267 m_rowOrCol
= rowOrCol
;
10270 m_control
= control
;
10275 SetEventObject(obj
);
10279 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10281 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10282 const wxGridCellCoords
& topLeft
,
10283 const wxGridCellCoords
& bottomRight
,
10284 bool sel
, bool control
,
10285 bool shift
, bool alt
, bool meta
)
10286 : wxNotifyEvent( type
, id
)
10288 m_topLeft
= topLeft
;
10289 m_bottomRight
= bottomRight
;
10291 m_control
= control
;
10296 SetEventObject(obj
);
10300 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10302 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10303 wxObject
* obj
, int row
,
10304 int col
, wxControl
* ctrl
)
10305 : wxCommandEvent(type
, id
)
10307 SetEventObject(obj
);
10313 #endif // wxUSE_GRID