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
;
529 // Workaround for GTK+1 font setting problem on some platforms
530 #if !defined(__WXGTK__) || defined(__WXGTK20__)
531 if ( m_fontOld
.Ok() )
533 m_control
->SetFont(m_fontOld
);
534 m_fontOld
= wxNullFont
;
540 void wxGridCellEditor::SetSize(const wxRect
& rect
)
542 wxASSERT_MSG(m_control
,
543 wxT("The wxGridCellEditor must be Created first!"));
544 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
547 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
552 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
554 // accept the simple key presses, not anything with Ctrl/Alt/Meta
555 return !(event
.ControlDown() || event
.AltDown());
558 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
563 void wxGridCellEditor::StartingClick()
569 // ----------------------------------------------------------------------------
570 // wxGridCellTextEditor
571 // ----------------------------------------------------------------------------
573 wxGridCellTextEditor::wxGridCellTextEditor()
578 void wxGridCellTextEditor::Create(wxWindow
* parent
,
580 wxEvtHandler
* evtHandler
)
582 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
583 wxDefaultPosition
, wxDefaultSize
584 #if defined(__WXMSW__)
585 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
589 // set max length allowed in the textctrl, if the parameter was set
592 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
595 wxGridCellEditor::Create(parent
, id
, evtHandler
);
598 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
599 wxGridCellAttr
* WXUNUSED(attr
))
601 // as we fill the entire client area, don't do anything here to minimize
605 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
607 wxRect
rect(rectOrig
);
609 // Make the edit control large enough to allow for internal
612 // TODO: remove this if the text ctrl sizing is improved esp. for
615 #if defined(__WXGTK__)
624 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
626 // MB: treat MSW separately here otherwise the caret doesn't show
627 // when the editor is in the first row.
628 #if defined(__WXMSW__)
631 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
634 #if defined(__WXMOTIF__)
638 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
639 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
640 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
641 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
644 wxGridCellEditor::SetSize(rect
);
647 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
649 wxASSERT_MSG(m_control
,
650 wxT("The wxGridCellEditor must be Created first!"));
652 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
654 DoBeginEdit(m_startValue
);
657 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
659 Text()->SetValue(startValue
);
660 Text()->SetInsertionPointEnd();
661 Text()->SetSelection(-1,-1);
665 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
668 wxASSERT_MSG(m_control
,
669 wxT("The wxGridCellEditor must be Created first!"));
671 bool changed
= false;
672 wxString value
= Text()->GetValue();
673 if (value
!= m_startValue
)
677 grid
->GetTable()->SetValue(row
, col
, value
);
679 m_startValue
= wxEmptyString
;
680 // No point in setting the text of the hidden control
681 //Text()->SetValue(m_startValue);
687 void wxGridCellTextEditor::Reset()
689 wxASSERT_MSG(m_control
,
690 wxT("The wxGridCellEditor must be Created first!"));
692 DoReset(m_startValue
);
695 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
697 Text()->SetValue(startValue
);
698 Text()->SetInsertionPointEnd();
701 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
703 if ( wxGridCellEditor::IsAcceptedKey(event
) )
705 int keycode
= event
.GetKeyCode();
719 case WXK_NUMPAD_MULTIPLY
:
723 case WXK_NUMPAD_SUBTRACT
:
725 case WXK_NUMPAD_DECIMAL
:
727 case WXK_NUMPAD_DIVIDE
:
731 // accept 8 bit chars too if isprint() agrees
732 if ( (keycode
< 255) && (wxIsprint((wxChar
)keycode
)) )
740 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
742 if ( !Text()->EmulateKeyPress(event
) )
748 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
749 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
751 #if defined(__WXMOTIF__) || defined(__WXGTK__)
752 // wxMotif needs a little extra help...
753 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
754 wxString
s( Text()->GetValue() );
755 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
757 Text()->SetInsertionPoint( pos
);
759 // the other ports can handle a Return key press
765 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
775 if ( !params
.ToLong(&tmp
) )
777 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
781 m_maxChars
= (size_t)tmp
;
786 // return the value in the text control
787 wxString
wxGridCellTextEditor::GetValue() const
789 return Text()->GetValue();
792 // ----------------------------------------------------------------------------
793 // wxGridCellNumberEditor
794 // ----------------------------------------------------------------------------
796 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
802 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
804 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
);
818 // just a text control
819 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
822 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
823 #endif // wxUSE_VALIDATORS
827 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
829 // first get the value
830 wxGridTableBase
*table
= grid
->GetTable();
831 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
833 m_valueOld
= table
->GetValueAsLong(row
, col
);
838 wxString sValue
= table
->GetValue(row
, col
);
839 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
841 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
848 Spin()->SetValue((int)m_valueOld
);
853 DoBeginEdit(GetString());
857 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
866 value
= Spin()->GetValue();
867 changed
= value
!= m_valueOld
;
869 text
= wxString::Format(wxT("%ld"), value
);
873 text
= Text()->GetValue();
874 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
879 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
880 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
882 grid
->GetTable()->SetValue(row
, col
, text
);
888 void wxGridCellNumberEditor::Reset()
892 Spin()->SetValue((int)m_valueOld
);
896 DoReset(GetString());
900 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
902 if ( wxGridCellEditor::IsAcceptedKey(event
) )
904 int keycode
= event
.GetKeyCode();
920 case WXK_NUMPAD_SUBTRACT
:
926 if ( (keycode
< 128) && wxIsdigit((wxChar
)keycode
) )
934 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
938 int keycode
= event
.GetKeyCode();
939 if ( wxIsdigit((wxChar
)keycode
) || keycode
== '+' || keycode
== '-'
940 || keycode
== WXK_NUMPAD0
941 || keycode
== WXK_NUMPAD1
942 || keycode
== WXK_NUMPAD2
943 || keycode
== WXK_NUMPAD3
944 || keycode
== WXK_NUMPAD4
945 || keycode
== WXK_NUMPAD5
946 || keycode
== WXK_NUMPAD6
947 || keycode
== WXK_NUMPAD7
948 || keycode
== WXK_NUMPAD8
949 || keycode
== WXK_NUMPAD9
950 || keycode
== WXK_ADD
951 || keycode
== WXK_NUMPAD_ADD
952 || keycode
== WXK_SUBTRACT
953 || keycode
== WXK_NUMPAD_SUBTRACT
)
955 wxGridCellTextEditor::StartingKey(event
);
965 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
976 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
980 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
984 // skip the error message below
989 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
993 // return the value in the spin control if it is there (the text control otherwise)
994 wxString
wxGridCellNumberEditor::GetValue() const
1000 long value
= Spin()->GetValue();
1001 s
.Printf(wxT("%ld"), value
);
1005 s
= Text()->GetValue();
1010 // ----------------------------------------------------------------------------
1011 // wxGridCellFloatEditor
1012 // ----------------------------------------------------------------------------
1014 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1017 m_precision
= precision
;
1020 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1022 wxEvtHandler
* evtHandler
)
1024 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1026 #if wxUSE_VALIDATORS
1027 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1028 #endif // wxUSE_VALIDATORS
1031 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1033 // first get the value
1034 wxGridTableBase
*table
= grid
->GetTable();
1035 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1037 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1042 wxString sValue
= table
->GetValue(row
, col
);
1043 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1045 wxFAIL_MSG( _T("this cell doesn't have float value") );
1050 DoBeginEdit(GetString());
1053 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1057 wxString
text(Text()->GetValue());
1059 if ( (text
.empty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1061 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1062 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1064 grid
->GetTable()->SetValue(row
, col
, text
);
1071 void wxGridCellFloatEditor::Reset()
1073 DoReset(GetString());
1076 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1078 int keycode
= event
.GetKeyCode();
1080 tmpbuf
[0] = (char) keycode
;
1082 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1083 bool is_decimal_point
= ( strbuf
==
1084 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1085 if ( wxIsdigit((wxChar
)keycode
) || keycode
== '+' || keycode
== '-'
1087 || keycode
== WXK_NUMPAD0
1088 || keycode
== WXK_NUMPAD1
1089 || keycode
== WXK_NUMPAD2
1090 || keycode
== WXK_NUMPAD3
1091 || keycode
== WXK_NUMPAD4
1092 || keycode
== WXK_NUMPAD5
1093 || keycode
== WXK_NUMPAD6
1094 || keycode
== WXK_NUMPAD7
1095 || keycode
== WXK_NUMPAD8
1096 || keycode
== WXK_NUMPAD9
1097 || keycode
== WXK_ADD
1098 || keycode
== WXK_NUMPAD_ADD
1099 || keycode
== WXK_SUBTRACT
1100 || keycode
== WXK_NUMPAD_SUBTRACT
)
1102 wxGridCellTextEditor::StartingKey(event
);
1104 // skip Skip() below
1111 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1122 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1126 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1128 m_precision
= (int)tmp
;
1130 // skip the error message below
1135 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1139 wxString
wxGridCellFloatEditor::GetString() const
1142 if ( m_width
== -1 )
1144 // default width/precision
1147 else if ( m_precision
== -1 )
1149 // default precision
1150 fmt
.Printf(_T("%%%d.f"), m_width
);
1154 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1157 return wxString::Format(fmt
, m_valueOld
);
1160 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1162 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1164 int keycode
= event
.GetKeyCode();
1178 case WXK_NUMPAD_ADD
:
1180 case WXK_NUMPAD_SUBTRACT
:
1182 case WXK_NUMPAD_DECIMAL
:
1187 // additionally accept 'e' as in '1e+6', also '-', '+', and '.'
1189 tmpbuf
[0] = (char) keycode
;
1191 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1192 bool is_decimal_point
=
1193 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1194 wxLOCALE_CAT_NUMBER
) );
1195 if ( (keycode
< 128) &&
1196 (wxIsdigit((wxChar
)keycode
) || tolower(keycode
) == 'e' ||
1197 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1206 #endif // wxUSE_TEXTCTRL
1210 // ----------------------------------------------------------------------------
1211 // wxGridCellBoolEditor
1212 // ----------------------------------------------------------------------------
1214 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1216 wxEvtHandler
* evtHandler
)
1218 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1219 wxDefaultPosition
, wxDefaultSize
,
1222 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1225 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1227 bool resize
= false;
1228 wxSize size
= m_control
->GetSize();
1229 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1231 // check if the checkbox is not too big/small for this cell
1232 wxSize sizeBest
= m_control
->GetBestSize();
1233 if ( !(size
== sizeBest
) )
1235 // reset to default size if it had been made smaller
1241 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1243 // leave 1 pixel margin
1244 size
.x
= size
.y
= minSize
- 2;
1251 m_control
->SetSize(size
);
1254 // position it in the centre of the rectangle (TODO: support alignment?)
1256 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1257 // the checkbox without label still has some space to the right in wxGTK,
1258 // so shift it to the right
1260 #elif defined(__WXMSW__)
1261 // here too, but in other way
1266 int hAlign
= wxALIGN_CENTRE
;
1267 int vAlign
= wxALIGN_CENTRE
;
1269 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1272 if (hAlign
== wxALIGN_LEFT
)
1278 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1280 else if (hAlign
== wxALIGN_RIGHT
)
1282 x
= r
.x
+ r
.width
- size
.x
- 2;
1283 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1285 else if (hAlign
== wxALIGN_CENTRE
)
1287 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1288 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1291 m_control
->Move(x
, y
);
1294 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1296 m_control
->Show(show
);
1300 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1301 CBox()->SetBackgroundColour(colBg
);
1305 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1307 wxASSERT_MSG(m_control
,
1308 wxT("The wxGridCellEditor must be Created first!"));
1310 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1311 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1314 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1315 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1317 CBox()->SetValue(m_startValue
);
1321 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1324 wxASSERT_MSG(m_control
,
1325 wxT("The wxGridCellEditor must be Created first!"));
1327 bool changed
= false;
1328 bool value
= CBox()->GetValue();
1329 if ( value
!= m_startValue
)
1334 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1335 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1337 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1343 void wxGridCellBoolEditor::Reset()
1345 wxASSERT_MSG(m_control
,
1346 wxT("The wxGridCellEditor must be Created first!"));
1348 CBox()->SetValue(m_startValue
);
1351 void wxGridCellBoolEditor::StartingClick()
1353 CBox()->SetValue(!CBox()->GetValue());
1356 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1358 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1360 int keycode
= event
.GetKeyCode();
1364 case WXK_NUMPAD_MULTIPLY
:
1366 case WXK_NUMPAD_ADD
:
1368 case WXK_NUMPAD_SUBTRACT
:
1379 // return the value as "1" for true and the empty string for false
1380 wxString
wxGridCellBoolEditor::GetValue() const
1382 bool bSet
= CBox()->GetValue();
1383 return bSet
? _T("1") : wxEmptyString
;
1386 #endif // wxUSE_CHECKBOX
1390 // ----------------------------------------------------------------------------
1391 // wxGridCellChoiceEditor
1392 // ----------------------------------------------------------------------------
1394 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1396 : m_choices(choices
),
1397 m_allowOthers(allowOthers
) { }
1399 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1400 const wxString choices
[],
1402 : m_allowOthers(allowOthers
)
1406 m_choices
.Alloc(count
);
1407 for ( size_t n
= 0; n
< count
; n
++ )
1409 m_choices
.Add(choices
[n
]);
1414 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1416 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1417 editor
->m_allowOthers
= m_allowOthers
;
1418 editor
->m_choices
= m_choices
;
1423 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1425 wxEvtHandler
* evtHandler
)
1427 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1428 wxDefaultPosition
, wxDefaultSize
,
1430 m_allowOthers
? 0 : wxCB_READONLY
);
1432 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1435 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1436 wxGridCellAttr
* attr
)
1438 // as we fill the entire client area, don't do anything here to minimize
1441 // TODO: It doesn't actually fill the client area since the height of a
1442 // combo always defaults to the standard... Until someone has time to
1443 // figure out the right rectangle to paint, just do it the normal way...
1444 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1447 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1449 wxASSERT_MSG(m_control
,
1450 wxT("The wxGridCellEditor must be Created first!"));
1452 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1455 Combo()->SetValue(m_startValue
);
1458 // find the right position, or default to the first if not found
1459 int pos
= Combo()->FindString(m_startValue
);
1462 Combo()->SetSelection(pos
);
1464 Combo()->SetInsertionPointEnd();
1465 Combo()->SetFocus();
1468 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1471 wxString value
= Combo()->GetValue();
1472 if ( value
== m_startValue
)
1475 grid
->GetTable()->SetValue(row
, col
, value
);
1480 void wxGridCellChoiceEditor::Reset()
1482 Combo()->SetValue(m_startValue
);
1483 Combo()->SetInsertionPointEnd();
1486 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1496 wxStringTokenizer
tk(params
, _T(','));
1497 while ( tk
.HasMoreTokens() )
1499 m_choices
.Add(tk
.GetNextToken());
1503 // return the value in the text control
1504 wxString
wxGridCellChoiceEditor::GetValue() const
1506 return Combo()->GetValue();
1509 #endif // wxUSE_COMBOBOX
1511 // ----------------------------------------------------------------------------
1512 // wxGridCellEditorEvtHandler
1513 // ----------------------------------------------------------------------------
1515 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1517 switch ( event
.GetKeyCode() )
1521 m_grid
->DisableCellEditControl();
1525 m_grid
->GetEventHandler()->ProcessEvent( event
);
1529 case WXK_NUMPAD_ENTER
:
1530 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1531 m_editor
->HandleReturn(event
);
1540 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1542 switch ( event
.GetKeyCode() )
1547 case WXK_NUMPAD_ENTER
:
1555 // ----------------------------------------------------------------------------
1556 // wxGridCellWorker is an (almost) empty common base class for
1557 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1558 // ----------------------------------------------------------------------------
1560 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1565 wxGridCellWorker::~wxGridCellWorker()
1569 // ============================================================================
1571 // ============================================================================
1573 // ----------------------------------------------------------------------------
1574 // wxGridCellRenderer
1575 // ----------------------------------------------------------------------------
1577 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1578 wxGridCellAttr
& attr
,
1581 int WXUNUSED(row
), int WXUNUSED(col
),
1584 dc
.SetBackgroundMode( wxSOLID
);
1586 // grey out fields if the grid is disabled
1587 if( grid
.IsEnabled() )
1591 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1595 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1600 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1603 dc
.SetPen( *wxTRANSPARENT_PEN
);
1604 dc
.DrawRectangle(rect
);
1607 // ----------------------------------------------------------------------------
1608 // wxGridCellStringRenderer
1609 // ----------------------------------------------------------------------------
1611 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1612 wxGridCellAttr
& attr
,
1616 dc
.SetBackgroundMode( wxTRANSPARENT
);
1618 // TODO some special colours for attr.IsReadOnly() case?
1620 // different coloured text when the grid is disabled
1621 if( grid
.IsEnabled() )
1625 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1626 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1630 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1631 dc
.SetTextForeground( attr
.GetTextColour() );
1636 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1637 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1640 dc
.SetFont( attr
.GetFont() );
1643 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1645 const wxString
& text
)
1647 wxCoord x
= 0, y
= 0, max_x
= 0;
1648 dc
.SetFont(attr
.GetFont());
1649 wxStringTokenizer
tk(text
, _T('\n'));
1650 while ( tk
.HasMoreTokens() )
1652 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1653 max_x
= wxMax(max_x
, x
);
1656 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1658 return wxSize(max_x
, y
);
1661 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1662 wxGridCellAttr
& attr
,
1666 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1669 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1670 wxGridCellAttr
& attr
,
1672 const wxRect
& rectCell
,
1676 wxRect rect
= rectCell
;
1679 // erase only this cells background, overflow cells should have been erased
1680 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1683 attr
.GetAlignment(&hAlign
, &vAlign
);
1685 int overflowCols
= 0;
1687 if (attr
.GetOverflow())
1689 int cols
= grid
.GetNumberCols();
1690 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1691 int cell_rows
, cell_cols
;
1692 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1693 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1695 int i
, c_cols
, c_rows
;
1696 for (i
= col
+cell_cols
; i
< cols
; i
++)
1698 bool is_empty
= true;
1699 for (int j
=row
; j
<row
+cell_rows
; j
++)
1701 // check w/ anchor cell for multicell block
1702 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1703 if (c_rows
> 0) c_rows
= 0;
1704 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1711 rect
.width
+= grid
.GetColSize(i
);
1717 if (rect
.width
>= best_width
) break;
1719 overflowCols
= i
- col
- cell_cols
+ 1;
1720 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1723 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1725 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1727 clip
.x
+= rectCell
.width
;
1728 // draw each overflow cell individually
1729 int col_end
= col
+cell_cols
+overflowCols
;
1730 if (col_end
>= grid
.GetNumberCols())
1731 col_end
= grid
.GetNumberCols() - 1;
1732 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1734 clip
.width
= grid
.GetColSize(i
) - 1;
1735 dc
.DestroyClippingRegion();
1736 dc
.SetClippingRegion(clip
);
1738 SetTextColoursAndFont(grid
, attr
, dc
,
1739 grid
.IsInSelection(row
,i
));
1741 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1742 rect
, hAlign
, vAlign
);
1743 clip
.x
+= grid
.GetColSize(i
) - 1;
1749 dc
.DestroyClippingRegion();
1753 // now we only have to draw the text
1754 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1756 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1757 rect
, hAlign
, vAlign
);
1760 // ----------------------------------------------------------------------------
1761 // wxGridCellNumberRenderer
1762 // ----------------------------------------------------------------------------
1764 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1766 wxGridTableBase
*table
= grid
.GetTable();
1768 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1770 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1774 text
= table
->GetValue(row
, col
);
1780 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1781 wxGridCellAttr
& attr
,
1783 const wxRect
& rectCell
,
1787 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1789 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1791 // draw the text right aligned by default
1793 attr
.GetAlignment(&hAlign
, &vAlign
);
1794 hAlign
= wxALIGN_RIGHT
;
1796 wxRect rect
= rectCell
;
1799 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1802 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1803 wxGridCellAttr
& attr
,
1807 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1810 // ----------------------------------------------------------------------------
1811 // wxGridCellFloatRenderer
1812 // ----------------------------------------------------------------------------
1814 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1817 SetPrecision(precision
);
1820 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1822 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1823 renderer
->m_width
= m_width
;
1824 renderer
->m_precision
= m_precision
;
1825 renderer
->m_format
= m_format
;
1830 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1832 wxGridTableBase
*table
= grid
.GetTable();
1837 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1839 val
= table
->GetValueAsDouble(row
, col
);
1844 text
= table
->GetValue(row
, col
);
1845 hasDouble
= text
.ToDouble(&val
);
1852 if ( m_width
== -1 )
1854 if ( m_precision
== -1 )
1856 // default width/precision
1857 m_format
= _T("%f");
1861 m_format
.Printf(_T("%%.%df"), m_precision
);
1864 else if ( m_precision
== -1 )
1866 // default precision
1867 m_format
.Printf(_T("%%%d.f"), m_width
);
1871 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1875 text
.Printf(m_format
, val
);
1878 //else: text already contains the string
1883 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1884 wxGridCellAttr
& attr
,
1886 const wxRect
& rectCell
,
1890 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1892 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1894 // draw the text right aligned by default
1896 attr
.GetAlignment(&hAlign
, &vAlign
);
1897 hAlign
= wxALIGN_RIGHT
;
1899 wxRect rect
= rectCell
;
1902 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1905 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1906 wxGridCellAttr
& attr
,
1910 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1913 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1917 // reset to defaults
1923 wxString tmp
= params
.BeforeFirst(_T(','));
1927 if ( tmp
.ToLong(&width
) )
1929 SetWidth((int)width
);
1933 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1937 tmp
= params
.AfterFirst(_T(','));
1941 if ( tmp
.ToLong(&precision
) )
1943 SetPrecision((int)precision
);
1947 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1955 // ----------------------------------------------------------------------------
1956 // wxGridCellBoolRenderer
1957 // ----------------------------------------------------------------------------
1959 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1961 // FIXME these checkbox size calculations are really ugly...
1963 // between checkmark and box
1964 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1966 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1967 wxGridCellAttr
& WXUNUSED(attr
),
1972 // compute it only once (no locks for MT safeness in GUI thread...)
1973 if ( !ms_sizeCheckMark
.x
)
1975 // get checkbox size
1976 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
1977 wxSize size
= checkbox
->GetBestSize();
1978 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1980 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1981 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1982 checkSize
-= size
.y
/ 2;
1987 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1990 return ms_sizeCheckMark
;
1993 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1994 wxGridCellAttr
& attr
,
2000 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2002 // draw a check mark in the centre (ignoring alignment - TODO)
2003 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2005 // don't draw outside the cell
2006 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2007 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2009 // and even leave (at least) 1 pixel margin
2010 size
.x
= size
.y
= minSize
- 2;
2013 // draw a border around checkmark
2015 attr
.GetAlignment(& hAlign
, &vAlign
);
2018 if (hAlign
== wxALIGN_CENTRE
)
2020 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2021 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2022 rectBorder
.width
= size
.x
;
2023 rectBorder
.height
= size
.y
;
2025 else if (hAlign
== wxALIGN_LEFT
)
2027 rectBorder
.x
= rect
.x
+ 2;
2028 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2029 rectBorder
.width
= size
.x
;
2030 rectBorder
.height
= size
.y
;
2032 else if (hAlign
== wxALIGN_RIGHT
)
2034 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2035 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2036 rectBorder
.width
= size
.x
;
2037 rectBorder
.height
= size
.y
;
2041 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2042 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2045 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2046 value
= !( !cellval
|| (cellval
== wxT("0")) );
2051 wxRect rectMark
= rectBorder
;
2053 // MSW DrawCheckMark() is weird (and should probably be changed...)
2054 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2058 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2061 dc
.SetTextForeground(attr
.GetTextColour());
2062 dc
.DrawCheckMark(rectMark
);
2065 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2066 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2067 dc
.DrawRectangle(rectBorder
);
2070 // ----------------------------------------------------------------------------
2072 // ----------------------------------------------------------------------------
2074 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2078 m_isReadOnly
= Unset
;
2083 m_attrkind
= wxGridCellAttr::Cell
;
2085 m_sizeRows
= m_sizeCols
= 1;
2086 m_overflow
= UnsetOverflow
;
2088 SetDefAttr(attrDefault
);
2091 wxGridCellAttr
*wxGridCellAttr::Clone() const
2093 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2095 if ( HasTextColour() )
2096 attr
->SetTextColour(GetTextColour());
2097 if ( HasBackgroundColour() )
2098 attr
->SetBackgroundColour(GetBackgroundColour());
2100 attr
->SetFont(GetFont());
2101 if ( HasAlignment() )
2102 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2104 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2108 attr
->SetRenderer(m_renderer
);
2109 m_renderer
->IncRef();
2113 attr
->SetEditor(m_editor
);
2118 attr
->SetReadOnly();
2120 attr
->SetKind( m_attrkind
);
2125 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2127 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2128 SetTextColour(mergefrom
->GetTextColour());
2129 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2130 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2131 if ( !HasFont() && mergefrom
->HasFont() )
2132 SetFont(mergefrom
->GetFont());
2133 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2135 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2136 SetAlignment(hAlign
, vAlign
);
2139 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2141 // Directly access member functions as GetRender/Editor don't just return
2142 // m_renderer/m_editor
2144 // Maybe add support for merge of Render and Editor?
2145 if (!HasRenderer() && mergefrom
->HasRenderer() )
2147 m_renderer
= mergefrom
->m_renderer
;
2148 m_renderer
->IncRef();
2150 if ( !HasEditor() && mergefrom
->HasEditor() )
2152 m_editor
= mergefrom
->m_editor
;
2155 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2156 SetReadOnly(mergefrom
->IsReadOnly());
2158 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2159 SetOverflow(mergefrom
->GetOverflow());
2161 SetDefAttr(mergefrom
->m_defGridAttr
);
2164 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2166 // The size of a cell is normally 1,1
2168 // If this cell is larger (2,2) then this is the top left cell
2169 // the other cells that will be covered (lower right cells) must be
2170 // set to negative or zero values such that
2171 // row + num_rows of the covered cell points to the larger cell (this cell)
2172 // same goes for the col + num_cols.
2174 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2176 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2177 !((num_rows
<=0)&&(num_cols
>0)) ||
2178 !((num_rows
==0)&&(num_cols
==0))),
2179 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2181 m_sizeRows
= num_rows
;
2182 m_sizeCols
= num_cols
;
2185 const wxColour
& wxGridCellAttr::GetTextColour() const
2187 if (HasTextColour())
2191 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2193 return m_defGridAttr
->GetTextColour();
2197 wxFAIL_MSG(wxT("Missing default cell attribute"));
2198 return wxNullColour
;
2203 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2205 if (HasBackgroundColour())
2207 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2208 return m_defGridAttr
->GetBackgroundColour();
2211 wxFAIL_MSG(wxT("Missing default cell attribute"));
2212 return wxNullColour
;
2217 const wxFont
& wxGridCellAttr::GetFont() const
2221 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2222 return m_defGridAttr
->GetFont();
2225 wxFAIL_MSG(wxT("Missing default cell attribute"));
2231 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2235 if ( hAlign
) *hAlign
= m_hAlign
;
2236 if ( vAlign
) *vAlign
= m_vAlign
;
2238 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2239 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2242 wxFAIL_MSG(wxT("Missing default cell attribute"));
2246 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2248 if ( num_rows
) *num_rows
= m_sizeRows
;
2249 if ( num_cols
) *num_cols
= m_sizeCols
;
2252 // GetRenderer and GetEditor use a slightly different decision path about
2253 // which attribute to use. If a non-default attr object has one then it is
2254 // used, otherwise the default editor or renderer is fetched from the grid and
2255 // used. It should be the default for the data type of the cell. If it is
2256 // NULL (because the table has a type that the grid does not have in its
2257 // registry,) then the grid's default editor or renderer is used.
2259 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2261 wxGridCellRenderer
*renderer
;
2263 if ( m_renderer
&& this != m_defGridAttr
)
2265 // use the cells renderer if it has one
2266 renderer
= m_renderer
;
2269 else // no non default cell renderer
2271 // get default renderer for the data type
2274 // GetDefaultRendererForCell() will do IncRef() for us
2275 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2284 if (m_defGridAttr
&& this != m_defGridAttr
)
2286 // if we still don't have one then use the grid default
2287 // (no need for IncRef() here neither)
2288 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2290 else // default grid attr
2292 // use m_renderer which we had decided not to use initially
2293 renderer
= m_renderer
;
2300 // we're supposed to always find something
2301 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2306 // same as above, except for s/renderer/editor/g
2307 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2309 wxGridCellEditor
*editor
;
2311 if ( m_editor
&& this != m_defGridAttr
)
2313 // use the cells editor if it has one
2317 else // no non default cell editor
2319 // get default editor for the data type
2322 // GetDefaultEditorForCell() will do IncRef() for us
2323 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2332 if ( m_defGridAttr
&& this != m_defGridAttr
)
2334 // if we still don't have one then use the grid default
2335 // (no need for IncRef() here neither)
2336 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2338 else // default grid attr
2340 // use m_editor which we had decided not to use initially
2348 // we're supposed to always find something
2349 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2354 // ----------------------------------------------------------------------------
2355 // wxGridCellAttrData
2356 // ----------------------------------------------------------------------------
2358 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2360 int n
= FindIndex(row
, col
);
2361 if ( n
== wxNOT_FOUND
)
2363 // add the attribute
2364 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2368 // free the old attribute
2369 m_attrs
[(size_t)n
].attr
->DecRef();
2373 // change the attribute
2374 m_attrs
[(size_t)n
].attr
= attr
;
2378 // remove this attribute
2379 m_attrs
.RemoveAt((size_t)n
);
2384 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2386 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2388 int n
= FindIndex(row
, col
);
2389 if ( n
!= wxNOT_FOUND
)
2391 attr
= m_attrs
[(size_t)n
].attr
;
2398 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2400 size_t count
= m_attrs
.GetCount();
2401 for ( size_t n
= 0; n
< count
; n
++ )
2403 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2404 wxCoord row
= coords
.GetRow();
2405 if ((size_t)row
>= pos
)
2409 // If rows inserted, include row counter where necessary
2410 coords
.SetRow(row
+ numRows
);
2412 else if (numRows
< 0)
2414 // If rows deleted ...
2415 if ((size_t)row
>= pos
- numRows
)
2417 // ...either decrement row counter (if row still exists)...
2418 coords
.SetRow(row
+ numRows
);
2422 // ...or remove the attribute
2423 m_attrs
.RemoveAt((size_t)n
);
2431 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2433 size_t count
= m_attrs
.GetCount();
2434 for ( size_t n
= 0; n
< count
; n
++ )
2436 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2437 wxCoord col
= coords
.GetCol();
2438 if ( (size_t)col
>= pos
)
2442 // If rows inserted, include row counter where necessary
2443 coords
.SetCol(col
+ numCols
);
2445 else if (numCols
< 0)
2447 // If rows deleted ...
2448 if ((size_t)col
>= pos
- numCols
)
2450 // ...either decrement row counter (if row still exists)...
2451 coords
.SetCol(col
+ numCols
);
2455 // ...or remove the attribute
2456 m_attrs
.RemoveAt((size_t)n
);
2464 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2466 size_t count
= m_attrs
.GetCount();
2467 for ( size_t n
= 0; n
< count
; n
++ )
2469 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2470 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2479 // ----------------------------------------------------------------------------
2480 // wxGridRowOrColAttrData
2481 // ----------------------------------------------------------------------------
2483 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2485 size_t count
= m_attrs
.Count();
2486 for ( size_t n
= 0; n
< count
; n
++ )
2488 m_attrs
[n
]->DecRef();
2492 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2494 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2496 int n
= m_rowsOrCols
.Index(rowOrCol
);
2497 if ( n
!= wxNOT_FOUND
)
2499 attr
= m_attrs
[(size_t)n
];
2506 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2508 int i
= m_rowsOrCols
.Index(rowOrCol
);
2509 if ( i
== wxNOT_FOUND
)
2511 // add the attribute
2512 m_rowsOrCols
.Add(rowOrCol
);
2517 size_t n
= (size_t)i
;
2520 // change the attribute
2521 m_attrs
[n
]->DecRef();
2526 // remove this attribute
2527 m_attrs
[n
]->DecRef();
2528 m_rowsOrCols
.RemoveAt(n
);
2529 m_attrs
.RemoveAt(n
);
2534 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2536 size_t count
= m_attrs
.GetCount();
2537 for ( size_t n
= 0; n
< count
; n
++ )
2539 int & rowOrCol
= m_rowsOrCols
[n
];
2540 if ( (size_t)rowOrCol
>= pos
)
2542 if ( numRowsOrCols
> 0 )
2544 // If rows inserted, include row counter where necessary
2545 rowOrCol
+= numRowsOrCols
;
2547 else if ( numRowsOrCols
< 0)
2549 // If rows deleted, either decrement row counter (if row still exists)
2550 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2551 rowOrCol
+= numRowsOrCols
;
2554 m_rowsOrCols
.RemoveAt((size_t)n
);
2555 m_attrs
.RemoveAt((size_t)n
);
2563 // ----------------------------------------------------------------------------
2564 // wxGridCellAttrProvider
2565 // ----------------------------------------------------------------------------
2567 wxGridCellAttrProvider::wxGridCellAttrProvider()
2569 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2572 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2577 void wxGridCellAttrProvider::InitData()
2579 m_data
= new wxGridCellAttrProviderData
;
2582 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2583 wxGridCellAttr::wxAttrKind kind
) const
2585 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2590 case (wxGridCellAttr::Any
):
2591 //Get cached merge attributes.
2592 // Currenlty not used as no cache implemented as not mutiable
2593 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2596 //Basicaly implement old version.
2597 //Also check merge cache, so we don't have to re-merge every time..
2598 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2599 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2600 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2602 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2603 // Two or more are non NULL
2604 attr
= new wxGridCellAttr
;
2605 attr
->SetKind(wxGridCellAttr::Merged
);
2609 attr
->MergeWith(attrcell
);
2613 attr
->MergeWith(attrcol
);
2617 attr
->MergeWith(attrrow
);
2620 //store merge attr if cache implemented
2622 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2626 // one or none is non null return it or null.
2627 if(attrrow
) attr
= attrrow
;
2643 case (wxGridCellAttr::Cell
):
2644 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2646 case (wxGridCellAttr::Col
):
2647 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2649 case (wxGridCellAttr::Row
):
2650 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2654 // (wxGridCellAttr::Default):
2655 // (wxGridCellAttr::Merged):
2662 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2668 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2671 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2676 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2679 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2684 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2687 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2691 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2693 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2697 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2701 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2703 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2707 // ----------------------------------------------------------------------------
2708 // wxGridTypeRegistry
2709 // ----------------------------------------------------------------------------
2711 wxGridTypeRegistry::~wxGridTypeRegistry()
2713 size_t count
= m_typeinfo
.Count();
2714 for ( size_t i
= 0; i
< count
; i
++ )
2715 delete m_typeinfo
[i
];
2719 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2720 wxGridCellRenderer
* renderer
,
2721 wxGridCellEditor
* editor
)
2723 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2725 // is it already registered?
2726 int loc
= FindRegisteredDataType(typeName
);
2727 if ( loc
!= wxNOT_FOUND
)
2729 delete m_typeinfo
[loc
];
2730 m_typeinfo
[loc
] = info
;
2734 m_typeinfo
.Add(info
);
2738 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2740 size_t count
= m_typeinfo
.GetCount();
2741 for ( size_t i
= 0; i
< count
; i
++ )
2743 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2752 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2754 int index
= FindRegisteredDataType(typeName
);
2755 if ( index
== wxNOT_FOUND
)
2757 // check whether this is one of the standard ones, in which case
2758 // register it "on the fly"
2760 if ( typeName
== wxGRID_VALUE_STRING
)
2762 RegisterDataType(wxGRID_VALUE_STRING
,
2763 new wxGridCellStringRenderer
,
2764 new wxGridCellTextEditor
);
2766 #endif // wxUSE_TEXTCTRL
2768 if ( typeName
== wxGRID_VALUE_BOOL
)
2770 RegisterDataType(wxGRID_VALUE_BOOL
,
2771 new wxGridCellBoolRenderer
,
2772 new wxGridCellBoolEditor
);
2774 #endif // wxUSE_CHECKBOX
2776 if ( typeName
== wxGRID_VALUE_NUMBER
)
2778 RegisterDataType(wxGRID_VALUE_NUMBER
,
2779 new wxGridCellNumberRenderer
,
2780 new wxGridCellNumberEditor
);
2782 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2784 RegisterDataType(wxGRID_VALUE_FLOAT
,
2785 new wxGridCellFloatRenderer
,
2786 new wxGridCellFloatEditor
);
2788 #endif // wxUSE_TEXTCTRL
2790 if ( typeName
== wxGRID_VALUE_CHOICE
)
2792 RegisterDataType(wxGRID_VALUE_CHOICE
,
2793 new wxGridCellStringRenderer
,
2794 new wxGridCellChoiceEditor
);
2796 #endif // wxUSE_COMBOBOX
2801 // we get here only if just added the entry for this type, so return
2803 index
= m_typeinfo
.GetCount() - 1;
2809 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2811 int index
= FindDataType(typeName
);
2812 if ( index
== wxNOT_FOUND
)
2814 // the first part of the typename is the "real" type, anything after ':'
2815 // are the parameters for the renderer
2816 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2817 if ( index
== wxNOT_FOUND
)
2822 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2823 wxGridCellRenderer
*rendererOld
= renderer
;
2824 renderer
= renderer
->Clone();
2825 rendererOld
->DecRef();
2827 wxGridCellEditor
*editor
= GetEditor(index
);
2828 wxGridCellEditor
*editorOld
= editor
;
2829 editor
= editor
->Clone();
2830 editorOld
->DecRef();
2832 // do it even if there are no parameters to reset them to defaults
2833 wxString params
= typeName
.AfterFirst(_T(':'));
2834 renderer
->SetParameters(params
);
2835 editor
->SetParameters(params
);
2837 // register the new typename
2838 RegisterDataType(typeName
, renderer
, editor
);
2840 // we just registered it, it's the last one
2841 index
= m_typeinfo
.GetCount() - 1;
2847 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2849 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2855 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2857 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2863 // ----------------------------------------------------------------------------
2865 // ----------------------------------------------------------------------------
2867 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2870 wxGridTableBase::wxGridTableBase()
2872 m_view
= (wxGrid
*) NULL
;
2873 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2876 wxGridTableBase::~wxGridTableBase()
2878 delete m_attrProvider
;
2881 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2883 delete m_attrProvider
;
2884 m_attrProvider
= attrProvider
;
2887 bool wxGridTableBase::CanHaveAttributes()
2889 if ( ! GetAttrProvider() )
2891 // use the default attr provider by default
2892 SetAttrProvider(new wxGridCellAttrProvider
);
2897 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2899 if ( m_attrProvider
)
2900 return m_attrProvider
->GetAttr(row
, col
, kind
);
2902 return (wxGridCellAttr
*)NULL
;
2905 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2907 if ( m_attrProvider
)
2909 attr
->SetKind(wxGridCellAttr::Cell
);
2910 m_attrProvider
->SetAttr(attr
, row
, col
);
2914 // as we take ownership of the pointer and don't store it, we must
2920 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2922 if ( m_attrProvider
)
2924 attr
->SetKind(wxGridCellAttr::Row
);
2925 m_attrProvider
->SetRowAttr(attr
, row
);
2929 // as we take ownership of the pointer and don't store it, we must
2935 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2937 if ( m_attrProvider
)
2939 attr
->SetKind(wxGridCellAttr::Col
);
2940 m_attrProvider
->SetColAttr(attr
, col
);
2944 // as we take ownership of the pointer and don't store it, we must
2950 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2951 size_t WXUNUSED(numRows
) )
2953 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2958 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2960 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2965 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2966 size_t WXUNUSED(numRows
) )
2968 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2973 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2974 size_t WXUNUSED(numCols
) )
2976 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2981 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2983 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2988 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2989 size_t WXUNUSED(numCols
) )
2991 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2997 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3000 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3001 // how much it makes sense to us geeks.
3005 wxString
wxGridTableBase::GetColLabelValue( int col
)
3007 // default col labels are:
3008 // cols 0 to 25 : A-Z
3009 // cols 26 to 675 : AA-ZZ
3014 for ( n
= 1; ; n
++ )
3016 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3018 if ( col
< 0 ) break;
3021 // reverse the string...
3023 for ( i
= 0; i
< n
; i
++ )
3032 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3034 return wxGRID_VALUE_STRING
;
3037 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3038 const wxString
& typeName
)
3040 return typeName
== wxGRID_VALUE_STRING
;
3043 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3045 return CanGetValueAs(row
, col
, typeName
);
3048 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3053 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3058 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3063 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3064 long WXUNUSED(value
) )
3068 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3069 double WXUNUSED(value
) )
3073 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3074 bool WXUNUSED(value
) )
3079 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3080 const wxString
& WXUNUSED(typeName
) )
3085 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3086 const wxString
& WXUNUSED(typeName
),
3087 void* WXUNUSED(value
) )
3091 //////////////////////////////////////////////////////////////////////
3093 // Message class for the grid table to send requests and notifications
3097 wxGridTableMessage::wxGridTableMessage()
3099 m_table
= (wxGridTableBase
*) NULL
;
3105 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3106 int commandInt1
, int commandInt2
)
3110 m_comInt1
= commandInt1
;
3111 m_comInt2
= commandInt2
;
3116 //////////////////////////////////////////////////////////////////////
3118 // A basic grid table for string data. An object of this class will
3119 // created by wxGrid if you don't specify an alternative table class.
3122 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3124 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3126 wxGridStringTable::wxGridStringTable()
3131 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3134 m_data
.Alloc( numRows
);
3137 sa
.Alloc( numCols
);
3138 sa
.Add( wxEmptyString
, numCols
);
3140 m_data
.Add( sa
, numRows
);
3143 wxGridStringTable::~wxGridStringTable()
3147 int wxGridStringTable::GetNumberRows()
3149 return m_data
.GetCount();
3152 int wxGridStringTable::GetNumberCols()
3154 if ( m_data
.GetCount() > 0 )
3155 return m_data
[0].GetCount();
3160 wxString
wxGridStringTable::GetValue( int row
, int col
)
3162 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3164 _T("invalid row or column index in wxGridStringTable") );
3166 return m_data
[row
][col
];
3169 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3171 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3172 _T("invalid row or column index in wxGridStringTable") );
3174 m_data
[row
][col
] = value
;
3177 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3179 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3181 _T("invalid row or column index in wxGridStringTable") );
3183 return (m_data
[row
][col
] == wxEmptyString
);
3186 void wxGridStringTable::Clear()
3189 int numRows
, numCols
;
3191 numRows
= m_data
.GetCount();
3194 numCols
= m_data
[0].GetCount();
3196 for ( row
= 0; row
< numRows
; row
++ )
3198 for ( col
= 0; col
< numCols
; col
++ )
3200 m_data
[row
][col
] = wxEmptyString
;
3207 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3209 size_t curNumRows
= m_data
.GetCount();
3210 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3211 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3213 if ( pos
>= curNumRows
)
3215 return AppendRows( numRows
);
3219 sa
.Alloc( curNumCols
);
3220 sa
.Add( wxEmptyString
, curNumCols
);
3221 m_data
.Insert( sa
, pos
, numRows
);
3224 wxGridTableMessage
msg( this,
3225 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3229 GetView()->ProcessTableMessage( msg
);
3235 bool wxGridStringTable::AppendRows( size_t numRows
)
3237 size_t curNumRows
= m_data
.GetCount();
3238 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3239 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3242 if ( curNumCols
> 0 )
3244 sa
.Alloc( curNumCols
);
3245 sa
.Add( wxEmptyString
, curNumCols
);
3248 m_data
.Add( sa
, numRows
);
3252 wxGridTableMessage
msg( this,
3253 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3256 GetView()->ProcessTableMessage( msg
);
3262 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3264 size_t curNumRows
= m_data
.GetCount();
3266 if ( pos
>= curNumRows
)
3268 wxFAIL_MSG( wxString::Format
3270 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3272 (unsigned long)numRows
,
3273 (unsigned long)curNumRows
3279 if ( numRows
> curNumRows
- pos
)
3281 numRows
= curNumRows
- pos
;
3284 if ( numRows
>= curNumRows
)
3290 m_data
.RemoveAt( pos
, numRows
);
3294 wxGridTableMessage
msg( this,
3295 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3299 GetView()->ProcessTableMessage( msg
);
3305 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3309 size_t curNumRows
= m_data
.GetCount();
3310 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3311 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3313 if ( pos
>= curNumCols
)
3315 return AppendCols( numCols
);
3318 for ( row
= 0; row
< curNumRows
; row
++ )
3320 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3322 m_data
[row
].Insert( wxEmptyString
, col
);
3327 wxGridTableMessage
msg( this,
3328 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3332 GetView()->ProcessTableMessage( msg
);
3338 bool wxGridStringTable::AppendCols( size_t numCols
)
3342 size_t curNumRows
= m_data
.GetCount();
3346 // TODO: something better than this ?
3348 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3353 for ( row
= 0; row
< curNumRows
; row
++ )
3355 m_data
[row
].Add( wxEmptyString
, numCols
);
3360 wxGridTableMessage
msg( this,
3361 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3364 GetView()->ProcessTableMessage( msg
);
3370 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3374 size_t curNumRows
= m_data
.GetCount();
3375 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3376 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3378 if ( pos
>= curNumCols
)
3380 wxFAIL_MSG( wxString::Format
3382 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3384 (unsigned long)numCols
,
3385 (unsigned long)curNumCols
3390 if ( numCols
> curNumCols
- pos
)
3392 numCols
= curNumCols
- pos
;
3395 for ( row
= 0; row
< curNumRows
; row
++ )
3397 if ( numCols
>= curNumCols
)
3399 m_data
[row
].Clear();
3403 m_data
[row
].RemoveAt( pos
, numCols
);
3408 wxGridTableMessage
msg( this,
3409 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3413 GetView()->ProcessTableMessage( msg
);
3419 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3421 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3423 // using default label
3425 return wxGridTableBase::GetRowLabelValue( row
);
3429 return m_rowLabels
[ row
];
3433 wxString
wxGridStringTable::GetColLabelValue( int col
)
3435 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3437 // using default label
3439 return wxGridTableBase::GetColLabelValue( col
);
3443 return m_colLabels
[ col
];
3447 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3449 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3451 int n
= m_rowLabels
.GetCount();
3453 for ( i
= n
; i
<= row
; i
++ )
3455 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3459 m_rowLabels
[row
] = value
;
3462 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3464 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3466 int n
= m_colLabels
.GetCount();
3468 for ( i
= n
; i
<= col
; i
++ )
3470 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3474 m_colLabels
[col
] = value
;
3479 //////////////////////////////////////////////////////////////////////
3480 //////////////////////////////////////////////////////////////////////
3482 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3484 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3485 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3486 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3487 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3488 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3489 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3492 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3494 const wxPoint
&pos
, const wxSize
&size
)
3495 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3500 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3504 // NO - don't do this because it will set both the x and y origin
3505 // coords to match the parent scrolled window and we just want to
3506 // set the y coord - MB
3508 // m_owner->PrepareDC( dc );
3511 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3512 dc
.SetDeviceOrigin( 0, -y
);
3514 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3515 m_owner
->DrawRowLabels( dc
, rows
);
3519 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3521 m_owner
->ProcessRowLabelMouseEvent( event
);
3525 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3527 m_owner
->GetEventHandler()->ProcessEvent(event
);
3531 // This seems to be required for wxMotif otherwise the mouse
3532 // cursor must be in the cell edit control to get key events
3534 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3536 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3539 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3541 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3546 //////////////////////////////////////////////////////////////////////
3548 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3550 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3551 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3552 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3553 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3554 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3555 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3558 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3560 const wxPoint
&pos
, const wxSize
&size
)
3561 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3566 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3570 // NO - don't do this because it will set both the x and y origin
3571 // coords to match the parent scrolled window and we just want to
3572 // set the x coord - MB
3574 // m_owner->PrepareDC( dc );
3577 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3578 dc
.SetDeviceOrigin( -x
, 0 );
3580 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3581 m_owner
->DrawColLabels( dc
, cols
);
3585 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3587 m_owner
->ProcessColLabelMouseEvent( event
);
3590 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3592 m_owner
->GetEventHandler()->ProcessEvent(event
);
3596 // This seems to be required for wxMotif otherwise the mouse
3597 // cursor must be in the cell edit control to get key events
3599 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3601 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3604 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3606 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3611 //////////////////////////////////////////////////////////////////////
3613 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3615 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3616 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3617 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3618 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3619 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3620 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3623 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3625 const wxPoint
&pos
, const wxSize
&size
)
3626 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3631 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3635 int client_height
= 0;
3636 int client_width
= 0;
3637 GetClientSize( &client_width
, &client_height
);
3639 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3640 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3641 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3642 dc
.DrawLine( 0, 0, client_width
, 0 );
3643 dc
.DrawLine( 0, 0, 0, client_height
);
3645 dc
.SetPen( *wxWHITE_PEN
);
3646 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3647 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3651 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3653 m_owner
->ProcessCornerLabelMouseEvent( event
);
3657 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3659 m_owner
->GetEventHandler()->ProcessEvent(event
);
3662 // This seems to be required for wxMotif otherwise the mouse
3663 // cursor must be in the cell edit control to get key events
3665 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3667 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3670 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3672 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3677 //////////////////////////////////////////////////////////////////////
3679 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3681 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3682 EVT_PAINT( wxGridWindow::OnPaint
)
3683 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3684 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3685 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3686 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3687 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3688 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3689 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3692 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3693 wxGridRowLabelWindow
*rowLblWin
,
3694 wxGridColLabelWindow
*colLblWin
,
3697 const wxSize
&size
)
3698 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3699 wxT("grid window") )
3703 m_rowLabelWin
= rowLblWin
;
3704 m_colLabelWin
= colLblWin
;
3708 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3710 wxPaintDC
dc( this );
3711 m_owner
->PrepareDC( dc
);
3712 wxRegion reg
= GetUpdateRegion();
3713 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3714 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3715 #if WXGRID_DRAW_LINES
3716 m_owner
->DrawAllGridLines( dc
, reg
);
3718 m_owner
->DrawGridSpace( dc
);
3719 m_owner
->DrawHighlight( dc
, DirtyCells
);
3723 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3725 wxWindow::ScrollWindow( dx
, dy
, rect
);
3726 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3727 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3731 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3733 m_owner
->ProcessGridCellMouseEvent( event
);
3736 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3738 m_owner
->GetEventHandler()->ProcessEvent(event
);
3741 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3742 // cursor must be in the cell edit control to get key events
3744 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3746 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3749 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3751 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3754 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3758 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3760 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3764 //////////////////////////////////////////////////////////////////////
3766 // Internal Helper function for computing row or column from some
3767 // (unscrolled) coordinate value, using either
3768 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3769 // of m_rowBottoms/m_ColRights to speed up the search!
3771 // Internal helper macros for simpler use of that function
3773 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3774 const wxArrayInt
& BorderArray
, int nMax
,
3777 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3778 m_minAcceptableColWidth, \
3779 m_colRights, m_numCols, true)
3780 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3781 m_minAcceptableRowHeight, \
3782 m_rowBottoms, m_numRows, true)
3783 /////////////////////////////////////////////////////////////////////
3785 #if wxUSE_EXTENDED_RTTI
3786 WX_DEFINE_FLAGS( wxGridStyle
)
3788 wxBEGIN_FLAGS( wxGridStyle
)
3789 // new style border flags, we put them first to
3790 // use them for streaming out
3791 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3792 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3793 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3794 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3795 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3796 wxFLAGS_MEMBER(wxBORDER_NONE
)
3798 // old style border flags
3799 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3800 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3801 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3802 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3803 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3804 wxFLAGS_MEMBER(wxBORDER
)
3806 // standard window styles
3807 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3808 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3809 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3810 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3811 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3812 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3813 wxFLAGS_MEMBER(wxVSCROLL
)
3814 wxFLAGS_MEMBER(wxHSCROLL
)
3816 wxEND_FLAGS( wxGridStyle
)
3818 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3820 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3821 wxHIDE_PROPERTY( Children
)
3822 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3823 wxEND_PROPERTIES_TABLE()
3825 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3826 wxEND_HANDLERS_TABLE()
3828 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3831 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3834 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3837 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3838 EVT_PAINT( wxGrid::OnPaint
)
3839 EVT_SIZE( wxGrid::OnSize
)
3840 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3841 EVT_KEY_UP( wxGrid::OnKeyUp
)
3842 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3847 // in order to make sure that a size event is not
3848 // trigerred in a unfinished state
3849 m_cornerLabelWin
= NULL
;
3850 m_rowLabelWin
= NULL
;
3851 m_colLabelWin
= NULL
;
3855 wxGrid::wxGrid( wxWindow
*parent
,
3860 const wxString
& name
)
3861 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3862 m_colMinWidths(GRID_HASH_SIZE
),
3863 m_rowMinHeights(GRID_HASH_SIZE
)
3866 SetBestFittingSize(size
);
3869 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3870 const wxPoint
& pos
, const wxSize
& size
,
3871 long style
, const wxString
& name
)
3873 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3874 style
| wxWANTS_CHARS
, name
))
3877 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3878 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3881 SetBestFittingSize(size
);
3889 // Must do this or ~wxScrollHelper will pop the wrong event handler
3890 SetTargetWindow(this);
3892 wxSafeDecRef(m_defaultCellAttr
);
3894 #ifdef DEBUG_ATTR_CACHE
3895 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3896 wxPrintf(_T("wxGrid attribute cache statistics: "
3897 "total: %u, hits: %u (%u%%)\n"),
3898 total
, gs_nAttrCacheHits
,
3899 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3905 delete m_typeRegistry
;
3911 // ----- internal init and update functions
3914 // NOTE: If using the default visual attributes works everywhere then this can
3915 // be removed as well as the #else cases below.
3916 #define _USE_VISATTR 0
3919 #include "wx/listbox.h"
3922 void wxGrid::Create()
3924 m_created
= false; // set to true by CreateGrid
3926 m_table
= (wxGridTableBase
*) NULL
;
3929 m_cellEditCtrlEnabled
= false;
3931 m_defaultCellAttr
= new wxGridCellAttr();
3933 // Set default cell attributes
3934 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3935 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3936 m_defaultCellAttr
->SetFont(GetFont());
3937 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3938 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3939 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3942 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3943 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3945 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3946 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3949 m_defaultCellAttr
->SetTextColour(
3950 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3951 m_defaultCellAttr
->SetBackgroundColour(
3952 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3957 m_currentCellCoords
= wxGridNoCellCoords
;
3959 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3960 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3962 // create the type registry
3963 m_typeRegistry
= new wxGridTypeRegistry
;
3966 // subwindow components that make up the wxGrid
3967 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3972 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3977 m_colLabelWin
= new wxGridColLabelWindow( this,
3982 m_gridWin
= new wxGridWindow( this,
3989 SetTargetWindow( m_gridWin
);
3992 wxColour gfg
= gva
.colFg
;
3993 wxColour gbg
= gva
.colBg
;
3994 wxColour lfg
= lva
.colFg
;
3995 wxColour lbg
= lva
.colBg
;
3997 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3998 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
3999 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4000 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4002 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4003 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4004 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4005 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4006 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4007 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4009 m_gridWin
->SetOwnForegroundColour(gfg
);
4010 m_gridWin
->SetOwnBackgroundColour(gbg
);
4016 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4017 wxGrid::wxGridSelectionModes selmode
)
4019 wxCHECK_MSG( !m_created
,
4021 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4023 m_numRows
= numRows
;
4024 m_numCols
= numCols
;
4026 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4027 m_table
->SetView( this );
4029 m_selection
= new wxGridSelection( this, selmode
);
4038 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4040 wxCHECK_RET( m_created
,
4041 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4043 m_selection
->SetSelectionMode( selmode
);
4046 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4048 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4049 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4051 return m_selection
->GetSelectionMode();
4054 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4055 wxGrid::wxGridSelectionModes selmode
)
4059 // stop all processing
4064 wxGridTableBase
*t
=m_table
;
4077 m_numRows
= table
->GetNumberRows();
4078 m_numCols
= table
->GetNumberCols();
4081 m_table
->SetView( this );
4084 m_selection
= new wxGridSelection( this, selmode
);
4097 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4098 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4100 if ( m_rowLabelWin
)
4102 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4106 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4109 m_labelTextColour
= wxColour( _T("BLACK") );
4112 m_attrCache
.row
= -1;
4113 m_attrCache
.col
= -1;
4114 m_attrCache
.attr
= NULL
;
4116 // TODO: something better than this ?
4118 m_labelFont
= this->GetFont();
4119 m_labelFont
.SetWeight( wxBOLD
);
4121 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4122 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4124 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4125 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4126 m_colLabelTextOrientation
= wxHORIZONTAL
;
4128 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4129 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4131 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4132 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4134 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4135 m_defaultRowHeight
+= 8;
4137 m_defaultRowHeight
+= 4;
4140 m_gridLineColour
= wxColour( 192,192,192 );
4141 m_gridLinesEnabled
= true;
4142 m_cellHighlightColour
= *wxBLACK
;
4143 m_cellHighlightPenWidth
= 2;
4144 m_cellHighlightROPenWidth
= 1;
4146 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4147 m_winCapture
= (wxWindow
*)NULL
;
4148 m_canDragRowSize
= true;
4149 m_canDragColSize
= true;
4150 m_canDragGridSize
= true;
4151 m_canDragCell
= false;
4153 m_dragRowOrCol
= -1;
4154 m_isDragging
= false;
4155 m_startDragPos
= wxDefaultPosition
;
4157 m_waitForSlowClick
= false;
4159 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4160 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4162 m_currentCellCoords
= wxGridNoCellCoords
;
4164 m_selectingTopLeft
= wxGridNoCellCoords
;
4165 m_selectingBottomRight
= wxGridNoCellCoords
;
4166 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4167 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4169 m_editable
= true; // default for whole grid
4171 m_inOnKeyDown
= false;
4177 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4178 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4181 // ----------------------------------------------------------------------------
4182 // the idea is to call these functions only when necessary because they create
4183 // quite big arrays which eat memory mostly unnecessary - in particular, if
4184 // default widths/heights are used for all rows/columns, we may not use these
4187 // with some extra code, it should be possible to only store the
4188 // widths/heights different from default ones but this will be done later...
4189 // ----------------------------------------------------------------------------
4191 void wxGrid::InitRowHeights()
4193 m_rowHeights
.Empty();
4194 m_rowBottoms
.Empty();
4196 m_rowHeights
.Alloc( m_numRows
);
4197 m_rowBottoms
.Alloc( m_numRows
);
4201 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4203 for ( int i
= 0; i
< m_numRows
; i
++ )
4205 rowBottom
+= m_defaultRowHeight
;
4206 m_rowBottoms
.Add( rowBottom
);
4210 void wxGrid::InitColWidths()
4212 m_colWidths
.Empty();
4213 m_colRights
.Empty();
4215 m_colWidths
.Alloc( m_numCols
);
4216 m_colRights
.Alloc( m_numCols
);
4219 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4221 for ( int i
= 0; i
< m_numCols
; i
++ )
4223 colRight
+= m_defaultColWidth
;
4224 m_colRights
.Add( colRight
);
4228 int wxGrid::GetColWidth(int col
) const
4230 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4233 int wxGrid::GetColLeft(int col
) const
4235 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4236 : m_colRights
[col
] - m_colWidths
[col
];
4239 int wxGrid::GetColRight(int col
) const
4241 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4245 int wxGrid::GetRowHeight(int row
) const
4247 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4250 int wxGrid::GetRowTop(int row
) const
4252 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4253 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4256 int wxGrid::GetRowBottom(int row
) const
4258 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4259 : m_rowBottoms
[row
];
4262 void wxGrid::CalcDimensions()
4265 GetClientSize( &cw
, &ch
);
4267 if ( m_rowLabelWin
->IsShown() )
4268 cw
-= m_rowLabelWidth
;
4269 if ( m_colLabelWin
->IsShown() )
4270 ch
-= m_colLabelHeight
;
4273 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4274 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4276 // take into account editor if shown
4277 if( IsCellEditControlShown() )
4280 int r
= m_currentCellCoords
.GetRow();
4281 int c
= m_currentCellCoords
.GetCol();
4282 int x
= GetColLeft(c
);
4283 int y
= GetRowTop(r
);
4285 // how big is the editor
4286 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4287 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4288 editor
->GetControl()->GetSize(&w2
, &h2
);
4291 if( w2
> w
) w
= w2
;
4292 if( h2
> h
) h
= h2
;
4297 // preserve (more or less) the previous position
4299 GetViewStart( &x
, &y
);
4301 // ensure the position is valid for the new scroll ranges
4303 x
= wxMax( w
- 1, 0 );
4305 y
= wxMax( h
- 1, 0 );
4307 // do set scrollbar parameters
4308 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4309 GetScrollX(w
), GetScrollY(h
), x
, y
,
4310 GetBatchCount() != 0);
4312 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4313 // still must reposition the children
4318 void wxGrid::CalcWindowSizes()
4320 // escape if the window is has not been fully created yet
4322 if ( m_cornerLabelWin
== NULL
)
4326 GetClientSize( &cw
, &ch
);
4328 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4329 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4331 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4332 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4334 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4335 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4337 if ( m_gridWin
&& m_gridWin
->IsShown() )
4338 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4342 // this is called when the grid table sends a message to say that it
4343 // has been redimensioned
4345 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4348 bool result
= false;
4350 // Clear the attribute cache as the attribute might refer to a different
4351 // cell than stored in the cache after adding/removing rows/columns.
4353 // By the same reasoning, the editor should be dismissed if columns are
4354 // added or removed. And for consistency, it should IMHO always be
4355 // removed, not only if the cell "underneath" it actually changes.
4356 // For now, I intentionally do not save the editor's content as the
4357 // cell it might want to save that stuff to might no longer exist.
4358 HideCellEditControl();
4360 // if we were using the default widths/heights so far, we must change them
4362 if ( m_colWidths
.IsEmpty() )
4367 if ( m_rowHeights
.IsEmpty() )
4373 switch ( msg
.GetId() )
4375 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4377 size_t pos
= msg
.GetCommandInt();
4378 int numRows
= msg
.GetCommandInt2();
4380 m_numRows
+= numRows
;
4382 if ( !m_rowHeights
.IsEmpty() )
4384 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4385 m_rowBottoms
.Insert( 0, pos
, numRows
);
4388 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4390 for ( i
= pos
; i
< m_numRows
; i
++ )
4392 bottom
+= m_rowHeights
[i
];
4393 m_rowBottoms
[i
] = bottom
;
4396 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4398 // if we have just inserted cols into an empty grid the current
4399 // cell will be undefined...
4401 SetCurrentCell( 0, 0 );
4405 m_selection
->UpdateRows( pos
, numRows
);
4406 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4408 attrProvider
->UpdateAttrRows( pos
, numRows
);
4410 if ( !GetBatchCount() )
4413 m_rowLabelWin
->Refresh();
4419 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4421 int numRows
= msg
.GetCommandInt();
4422 int oldNumRows
= m_numRows
;
4423 m_numRows
+= numRows
;
4425 if ( !m_rowHeights
.IsEmpty() )
4427 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4428 m_rowBottoms
.Add( 0, numRows
);
4431 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4433 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4435 bottom
+= m_rowHeights
[i
];
4436 m_rowBottoms
[i
] = bottom
;
4439 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4441 // if we have just inserted cols into an empty grid the current
4442 // cell will be undefined...
4444 SetCurrentCell( 0, 0 );
4446 if ( !GetBatchCount() )
4449 m_rowLabelWin
->Refresh();
4455 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4457 size_t pos
= msg
.GetCommandInt();
4458 int numRows
= msg
.GetCommandInt2();
4459 m_numRows
-= numRows
;
4461 if ( !m_rowHeights
.IsEmpty() )
4463 m_rowHeights
.RemoveAt( pos
, numRows
);
4464 m_rowBottoms
.RemoveAt( pos
, numRows
);
4467 for ( i
= 0; i
< m_numRows
; i
++ )
4469 h
+= m_rowHeights
[i
];
4470 m_rowBottoms
[i
] = h
;
4475 m_currentCellCoords
= wxGridNoCellCoords
;
4479 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4480 m_currentCellCoords
.Set( 0, 0 );
4484 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4485 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4487 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4488 // ifdef'd out following patch from Paul Gammans
4490 // No need to touch column attributes, unless we
4491 // removed _all_ rows, in this case, we remove
4492 // all column attributes.
4493 // I hate to do this here, but the
4494 // needed data is not available inside UpdateAttrRows.
4495 if ( !GetNumberRows() )
4496 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4499 if ( !GetBatchCount() )
4502 m_rowLabelWin
->Refresh();
4508 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4510 size_t pos
= msg
.GetCommandInt();
4511 int numCols
= msg
.GetCommandInt2();
4512 m_numCols
+= numCols
;
4514 if ( !m_colWidths
.IsEmpty() )
4516 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4517 m_colRights
.Insert( 0, pos
, numCols
);
4520 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4522 for ( i
= pos
; i
< m_numCols
; i
++ )
4524 right
+= m_colWidths
[i
];
4525 m_colRights
[i
] = right
;
4528 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4530 // if we have just inserted cols into an empty grid the current
4531 // cell will be undefined...
4533 SetCurrentCell( 0, 0 );
4537 m_selection
->UpdateCols( pos
, numCols
);
4538 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4540 attrProvider
->UpdateAttrCols( pos
, numCols
);
4541 if ( !GetBatchCount() )
4544 m_colLabelWin
->Refresh();
4551 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4553 int numCols
= msg
.GetCommandInt();
4554 int oldNumCols
= m_numCols
;
4555 m_numCols
+= numCols
;
4556 if ( !m_colWidths
.IsEmpty() )
4558 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4559 m_colRights
.Add( 0, numCols
);
4562 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4564 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4566 right
+= m_colWidths
[i
];
4567 m_colRights
[i
] = right
;
4570 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4572 // if we have just inserted cols into an empty grid the current
4573 // cell will be undefined...
4575 SetCurrentCell( 0, 0 );
4577 if ( !GetBatchCount() )
4580 m_colLabelWin
->Refresh();
4586 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4588 size_t pos
= msg
.GetCommandInt();
4589 int numCols
= msg
.GetCommandInt2();
4590 m_numCols
-= numCols
;
4592 if ( !m_colWidths
.IsEmpty() )
4594 m_colWidths
.RemoveAt( pos
, numCols
);
4595 m_colRights
.RemoveAt( pos
, numCols
);
4598 for ( i
= 0; i
< m_numCols
; i
++ )
4600 w
+= m_colWidths
[i
];
4606 m_currentCellCoords
= wxGridNoCellCoords
;
4610 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4611 m_currentCellCoords
.Set( 0, 0 );
4615 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4616 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4618 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4619 // ifdef'd out following patch from Paul Gammans
4621 // No need to touch row attributes, unless we
4622 // removed _all_ columns, in this case, we remove
4623 // all row attributes.
4624 // I hate to do this here, but the
4625 // needed data is not available inside UpdateAttrCols.
4626 if ( !GetNumberCols() )
4627 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4630 if ( !GetBatchCount() )
4633 m_colLabelWin
->Refresh();
4640 if (result
&& !GetBatchCount() )
4641 m_gridWin
->Refresh();
4646 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4648 wxRegionIterator
iter( reg
);
4651 wxArrayInt rowlabels
;
4658 // TODO: remove this when we can...
4659 // There is a bug in wxMotif that gives garbage update
4660 // rectangles if you jump-scroll a long way by clicking the
4661 // scrollbar with middle button. This is a work-around
4663 #if defined(__WXMOTIF__)
4665 m_gridWin
->GetClientSize( &cw
, &ch
);
4666 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4667 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4670 // logical bounds of update region
4673 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4674 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4676 // find the row labels within these bounds
4679 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4681 if ( GetRowBottom(row
) < top
)
4684 if ( GetRowTop(row
) > bottom
)
4687 rowlabels
.Add( row
);
4697 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4699 wxRegionIterator
iter( reg
);
4702 wxArrayInt colLabels
;
4709 // TODO: remove this when we can...
4710 // There is a bug in wxMotif that gives garbage update
4711 // rectangles if you jump-scroll a long way by clicking the
4712 // scrollbar with middle button. This is a work-around
4714 #if defined(__WXMOTIF__)
4716 m_gridWin
->GetClientSize( &cw
, &ch
);
4717 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4718 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4721 // logical bounds of update region
4724 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4725 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4727 // find the cells within these bounds
4730 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4732 if ( GetColRight(col
) < left
)
4735 if ( GetColLeft(col
) > right
)
4738 colLabels
.Add( col
);
4747 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4749 wxRegionIterator
iter( reg
);
4752 wxGridCellCoordsArray cellsExposed
;
4754 int left
, top
, right
, bottom
;
4759 // TODO: remove this when we can...
4760 // There is a bug in wxMotif that gives garbage update
4761 // rectangles if you jump-scroll a long way by clicking the
4762 // scrollbar with middle button. This is a work-around
4764 #if defined(__WXMOTIF__)
4766 m_gridWin
->GetClientSize( &cw
, &ch
);
4767 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4768 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4769 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4770 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4773 // logical bounds of update region
4775 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4776 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4778 // find the cells within these bounds
4781 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4783 if ( GetRowBottom(row
) <= top
)
4786 if ( GetRowTop(row
) > bottom
)
4789 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4791 if ( GetColRight(col
) <= left
)
4794 if ( GetColLeft(col
) > right
)
4797 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4804 return cellsExposed
;
4808 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4811 wxPoint
pos( event
.GetPosition() );
4812 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4814 if ( event
.Dragging() )
4818 m_isDragging
= true;
4819 m_rowLabelWin
->CaptureMouse();
4822 if ( event
.LeftIsDown() )
4824 switch( m_cursorMode
)
4826 case WXGRID_CURSOR_RESIZE_ROW
:
4828 int cw
, ch
, left
, dummy
;
4829 m_gridWin
->GetClientSize( &cw
, &ch
);
4830 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4832 wxClientDC
dc( m_gridWin
);
4835 GetRowTop(m_dragRowOrCol
) +
4836 GetRowMinimalHeight(m_dragRowOrCol
) );
4837 dc
.SetLogicalFunction(wxINVERT
);
4838 if ( m_dragLastPos
>= 0 )
4840 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4842 dc
.DrawLine( left
, y
, left
+cw
, y
);
4847 case WXGRID_CURSOR_SELECT_ROW
:
4848 if ( (row
= YToRow( y
)) >= 0 )
4852 m_selection
->SelectRow( row
,
4853 event
.ControlDown(),
4860 // default label to suppress warnings about "enumeration value
4861 // 'xxx' not handled in switch
4869 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4874 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4875 m_isDragging
= false;
4878 // ------------ Entering or leaving the window
4880 if ( event
.Entering() || event
.Leaving() )
4882 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4886 // ------------ Left button pressed
4888 else if ( event
.LeftDown() )
4890 // don't send a label click event for a hit on the
4891 // edge of the row label - this is probably the user
4892 // wanting to resize the row
4894 if ( YToEdgeOfRow(y
) < 0 )
4898 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4900 if ( !event
.ShiftDown() && !event
.ControlDown() )
4904 if ( event
.ShiftDown() )
4906 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4909 GetNumberCols() - 1,
4910 event
.ControlDown(),
4917 m_selection
->SelectRow( row
,
4918 event
.ControlDown(),
4925 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4930 // starting to drag-resize a row
4932 if ( CanDragRowSize() )
4933 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4938 // ------------ Left double click
4940 else if (event
.LeftDClick() )
4942 int row
= YToEdgeOfRow(y
);
4947 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4949 // no default action at the moment
4954 // adjust row height depending on label text
4955 AutoSizeRowLabelSize( row
);
4957 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4963 // ------------ Left button released
4965 else if ( event
.LeftUp() )
4967 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4969 DoEndDragResizeRow();
4971 // Note: we are ending the event *after* doing
4972 // default processing in this case
4974 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4977 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4982 // ------------ Right button down
4984 else if ( event
.RightDown() )
4988 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4990 // no default action at the moment
4995 // ------------ Right double click
4997 else if ( event
.RightDClick() )
5001 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5003 // no default action at the moment
5008 // ------------ No buttons down and mouse moving
5010 else if ( event
.Moving() )
5012 m_dragRowOrCol
= YToEdgeOfRow( y
);
5013 if ( m_dragRowOrCol
>= 0 )
5015 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5017 // don't capture the mouse yet
5018 if ( CanDragRowSize() )
5019 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5022 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5024 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5030 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5033 wxPoint
pos( event
.GetPosition() );
5034 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5036 if ( event
.Dragging() )
5040 m_isDragging
= true;
5041 m_colLabelWin
->CaptureMouse();
5044 if ( event
.LeftIsDown() )
5046 switch( m_cursorMode
)
5048 case WXGRID_CURSOR_RESIZE_COL
:
5050 int cw
, ch
, dummy
, top
;
5051 m_gridWin
->GetClientSize( &cw
, &ch
);
5052 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5054 wxClientDC
dc( m_gridWin
);
5057 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5058 GetColMinimalWidth(m_dragRowOrCol
));
5059 dc
.SetLogicalFunction(wxINVERT
);
5060 if ( m_dragLastPos
>= 0 )
5062 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5064 dc
.DrawLine( x
, top
, x
, top
+ch
);
5069 case WXGRID_CURSOR_SELECT_COL
:
5070 if ( (col
= XToCol( x
)) >= 0 )
5074 m_selection
->SelectCol( col
,
5075 event
.ControlDown(),
5082 // default label to suppress warnings about "enumeration value
5083 // 'xxx' not handled in switch
5091 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5096 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5097 m_isDragging
= false;
5100 // ------------ Entering or leaving the window
5102 if ( event
.Entering() || event
.Leaving() )
5104 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5108 // ------------ Left button pressed
5110 else if ( event
.LeftDown() )
5112 // don't send a label click event for a hit on the
5113 // edge of the col label - this is probably the user
5114 // wanting to resize the col
5116 if ( XToEdgeOfCol(x
) < 0 )
5120 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5122 if ( !event
.ShiftDown() && !event
.ControlDown() )
5126 if ( event
.ShiftDown() )
5128 m_selection
->SelectBlock( 0,
5129 m_currentCellCoords
.GetCol(),
5130 GetNumberRows() - 1, col
,
5131 event
.ControlDown(),
5138 m_selection
->SelectCol( col
,
5139 event
.ControlDown(),
5146 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5151 // starting to drag-resize a col
5153 if ( CanDragColSize() )
5154 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5159 // ------------ Left double click
5161 if ( event
.LeftDClick() )
5163 int col
= XToEdgeOfCol(x
);
5168 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5170 // no default action at the moment
5175 // adjust column width depending on label text
5176 AutoSizeColLabelSize( col
);
5178 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5184 // ------------ Left button released
5186 else if ( event
.LeftUp() )
5188 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5190 DoEndDragResizeCol();
5192 // Note: we are ending the event *after* doing
5193 // default processing in this case
5195 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5198 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5203 // ------------ Right button down
5205 else if ( event
.RightDown() )
5209 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5211 // no default action at the moment
5216 // ------------ Right double click
5218 else if ( event
.RightDClick() )
5222 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5224 // no default action at the moment
5229 // ------------ No buttons down and mouse moving
5231 else if ( event
.Moving() )
5233 m_dragRowOrCol
= XToEdgeOfCol( x
);
5234 if ( m_dragRowOrCol
>= 0 )
5236 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5238 // don't capture the cursor yet
5239 if ( CanDragColSize() )
5240 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5243 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5245 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5251 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5253 if ( event
.LeftDown() )
5255 // indicate corner label by having both row and
5258 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5264 else if ( event
.LeftDClick() )
5266 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5269 else if ( event
.RightDown() )
5271 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5273 // no default action at the moment
5277 else if ( event
.RightDClick() )
5279 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5281 // no default action at the moment
5286 void wxGrid::ChangeCursorMode(CursorMode mode
,
5291 static const wxChar
*cursorModes
[] =
5300 wxLogTrace(_T("grid"),
5301 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5302 win
== m_colLabelWin
? _T("colLabelWin")
5303 : win
? _T("rowLabelWin")
5305 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5306 #endif // __WXDEBUG__
5308 if ( mode
== m_cursorMode
&&
5309 win
== m_winCapture
&&
5310 captureMouse
== (m_winCapture
!= NULL
))
5315 // by default use the grid itself
5321 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5322 m_winCapture
= (wxWindow
*)NULL
;
5325 m_cursorMode
= mode
;
5327 switch ( m_cursorMode
)
5329 case WXGRID_CURSOR_RESIZE_ROW
:
5330 win
->SetCursor( m_rowResizeCursor
);
5333 case WXGRID_CURSOR_RESIZE_COL
:
5334 win
->SetCursor( m_colResizeCursor
);
5338 win
->SetCursor( *wxSTANDARD_CURSOR
);
5341 // we need to capture mouse when resizing
5342 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5343 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5345 if ( captureMouse
&& resize
)
5347 win
->CaptureMouse();
5352 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5355 wxPoint
pos( event
.GetPosition() );
5356 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5358 wxGridCellCoords coords
;
5359 XYToCell( x
, y
, coords
);
5361 int cell_rows
, cell_cols
;
5362 bool isFirstDrag
= !m_isDragging
;
5363 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5364 if ((cell_rows
< 0) || (cell_cols
< 0))
5366 coords
.SetRow(coords
.GetRow() + cell_rows
);
5367 coords
.SetCol(coords
.GetCol() + cell_cols
);
5370 if ( event
.Dragging() )
5372 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5374 // Don't start doing anything until the mouse has been drug at
5375 // least 3 pixels in any direction...
5378 if (m_startDragPos
== wxDefaultPosition
)
5380 m_startDragPos
= pos
;
5383 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5387 m_isDragging
= true;
5388 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5390 // Hide the edit control, so it
5391 // won't interfer with drag-shrinking.
5392 if ( IsCellEditControlShown() )
5394 HideCellEditControl();
5395 SaveEditControlValue();
5398 // Have we captured the mouse yet?
5401 m_winCapture
= m_gridWin
;
5402 m_winCapture
->CaptureMouse();
5405 if ( coords
!= wxGridNoCellCoords
)
5407 if ( event
.ControlDown() )
5409 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5410 m_selectingKeyboard
= coords
;
5411 HighlightBlock ( m_selectingKeyboard
, coords
);
5413 else if ( CanDragCell() )
5417 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5418 m_selectingKeyboard
= coords
;
5420 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5428 if ( !IsSelection() )
5430 HighlightBlock( coords
, coords
);
5434 HighlightBlock( m_currentCellCoords
, coords
);
5438 if (! IsVisible(coords
))
5440 MakeCellVisible(coords
);
5441 // TODO: need to introduce a delay or something here. The
5442 // scrolling is way to fast, at least on MSW - also on GTK.
5446 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5448 int cw
, ch
, left
, dummy
;
5449 m_gridWin
->GetClientSize( &cw
, &ch
);
5450 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5452 wxClientDC
dc( m_gridWin
);
5454 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5455 GetRowMinimalHeight(m_dragRowOrCol
) );
5456 dc
.SetLogicalFunction(wxINVERT
);
5457 if ( m_dragLastPos
>= 0 )
5459 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5461 dc
.DrawLine( left
, y
, left
+cw
, y
);
5464 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5466 int cw
, ch
, dummy
, top
;
5467 m_gridWin
->GetClientSize( &cw
, &ch
);
5468 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5470 wxClientDC
dc( m_gridWin
);
5472 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5473 GetColMinimalWidth(m_dragRowOrCol
) );
5474 dc
.SetLogicalFunction(wxINVERT
);
5475 if ( m_dragLastPos
>= 0 )
5477 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5479 dc
.DrawLine( x
, top
, x
, top
+ch
);
5486 m_isDragging
= false;
5487 m_startDragPos
= wxDefaultPosition
;
5489 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5490 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5493 if ( event
.Entering() || event
.Leaving() )
5495 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5496 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5501 // ------------ Left button pressed
5503 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5505 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5510 if ( !event
.ControlDown() )
5512 if ( event
.ShiftDown() )
5516 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5517 m_currentCellCoords
.GetCol(),
5520 event
.ControlDown(),
5526 else if ( XToEdgeOfCol(x
) < 0 &&
5527 YToEdgeOfRow(y
) < 0 )
5529 DisableCellEditControl();
5530 MakeCellVisible( coords
);
5532 if ( event
.ControlDown() )
5536 m_selection
->ToggleCellSelection( coords
.GetRow(),
5538 event
.ControlDown(),
5543 m_selectingTopLeft
= wxGridNoCellCoords
;
5544 m_selectingBottomRight
= wxGridNoCellCoords
;
5545 m_selectingKeyboard
= coords
;
5549 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5550 SetCurrentCell( coords
);
5553 if ( m_selection
->GetSelectionMode() !=
5554 wxGrid::wxGridSelectCells
)
5556 HighlightBlock( coords
, coords
);
5565 // ------------ Left double click
5567 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5569 DisableCellEditControl();
5571 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5573 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5578 // we want double click to select a cell and start editing
5579 // (i.e. to behave in same way as sequence of two slow clicks):
5580 m_waitForSlowClick
= true;
5587 // ------------ Left button released
5589 else if ( event
.LeftUp() )
5591 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5595 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5596 m_winCapture
= NULL
;
5599 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5602 EnableCellEditControl();
5604 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5605 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5606 editor
->StartingClick();
5610 m_waitForSlowClick
= false;
5612 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5613 m_selectingBottomRight
!= wxGridNoCellCoords
)
5617 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5618 m_selectingTopLeft
.GetCol(),
5619 m_selectingBottomRight
.GetRow(),
5620 m_selectingBottomRight
.GetCol(),
5621 event
.ControlDown(),
5627 m_selectingTopLeft
= wxGridNoCellCoords
;
5628 m_selectingBottomRight
= wxGridNoCellCoords
;
5630 // Show the edit control, if it has been hidden for
5632 ShowCellEditControl();
5635 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5637 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5638 DoEndDragResizeRow();
5640 // Note: we are ending the event *after* doing
5641 // default processing in this case
5643 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5645 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5647 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5648 DoEndDragResizeCol();
5650 // Note: we are ending the event *after* doing
5651 // default processing in this case
5653 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5660 // ------------ Right button down
5662 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5664 DisableCellEditControl();
5665 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5670 // no default action at the moment
5675 // ------------ Right double click
5677 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5679 DisableCellEditControl();
5680 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5685 // no default action at the moment
5689 // ------------ Moving and no button action
5691 else if ( event
.Moving() && !event
.IsButton() )
5693 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5695 // out of grid cell area
5696 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5700 int dragRow
= YToEdgeOfRow( y
);
5701 int dragCol
= XToEdgeOfCol( x
);
5703 // Dragging on the corner of a cell to resize in both
5704 // directions is not implemented yet...
5706 if ( dragRow
>= 0 && dragCol
>= 0 )
5708 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5714 m_dragRowOrCol
= dragRow
;
5716 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5718 if ( CanDragRowSize() && CanDragGridSize() )
5719 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5724 m_dragRowOrCol
= dragCol
;
5732 m_dragRowOrCol
= dragCol
;
5734 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5736 if ( CanDragColSize() && CanDragGridSize() )
5737 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5743 // Neither on a row or col edge
5745 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5747 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5753 void wxGrid::DoEndDragResizeRow()
5755 if ( m_dragLastPos
>= 0 )
5757 // erase the last line and resize the row
5759 int cw
, ch
, left
, dummy
;
5760 m_gridWin
->GetClientSize( &cw
, &ch
);
5761 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5763 wxClientDC
dc( m_gridWin
);
5765 dc
.SetLogicalFunction( wxINVERT
);
5766 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5767 HideCellEditControl();
5768 SaveEditControlValue();
5770 int rowTop
= GetRowTop(m_dragRowOrCol
);
5771 SetRowSize( m_dragRowOrCol
,
5772 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5774 if ( !GetBatchCount() )
5776 // Only needed to get the correct rect.y:
5777 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5779 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5780 rect
.width
= m_rowLabelWidth
;
5781 rect
.height
= ch
- rect
.y
;
5782 m_rowLabelWin
->Refresh( true, &rect
);
5784 // if there is a multicell block, paint all of it
5787 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5788 int leftCol
= XToCol(left
);
5789 int rightCol
= internalXToCol(left
+cw
);
5792 for (i
=leftCol
; i
<rightCol
; i
++)
5794 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5795 if (cell_rows
< subtract_rows
)
5796 subtract_rows
= cell_rows
;
5798 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5799 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5800 rect
.height
= ch
- rect
.y
;
5803 m_gridWin
->Refresh( false, &rect
);
5806 ShowCellEditControl();
5811 void wxGrid::DoEndDragResizeCol()
5813 if ( m_dragLastPos
>= 0 )
5815 // erase the last line and resize the col
5817 int cw
, ch
, dummy
, top
;
5818 m_gridWin
->GetClientSize( &cw
, &ch
);
5819 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5821 wxClientDC
dc( m_gridWin
);
5823 dc
.SetLogicalFunction( wxINVERT
);
5824 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5825 HideCellEditControl();
5826 SaveEditControlValue();
5828 int colLeft
= GetColLeft(m_dragRowOrCol
);
5829 SetColSize( m_dragRowOrCol
,
5830 wxMax( m_dragLastPos
- colLeft
,
5831 GetColMinimalWidth(m_dragRowOrCol
) ) );
5833 if ( !GetBatchCount() )
5835 // Only needed to get the correct rect.x:
5836 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5838 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5839 rect
.width
= cw
- rect
.x
;
5840 rect
.height
= m_colLabelHeight
;
5841 m_colLabelWin
->Refresh( true, &rect
);
5843 // if there is a multicell block, paint all of it
5846 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5847 int topRow
= YToRow(top
);
5848 int bottomRow
= internalYToRow(top
+cw
);
5851 for (i
=topRow
; i
<bottomRow
; i
++)
5853 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5854 if (cell_cols
< subtract_cols
)
5855 subtract_cols
= cell_cols
;
5857 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5858 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5859 rect
.width
= cw
- rect
.x
;
5862 m_gridWin
->Refresh( false, &rect
);
5865 ShowCellEditControl();
5872 // ------ interaction with data model
5874 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5876 switch ( msg
.GetId() )
5878 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5879 return GetModelValues();
5881 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5882 return SetModelValues();
5884 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5885 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5886 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5887 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5888 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5889 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5890 return Redimension( msg
);
5899 // The behaviour of this function depends on the grid table class
5900 // Clear() function. For the default wxGridStringTable class the
5901 // behavious is to replace all cell contents with wxEmptyString but
5902 // not to change the number of rows or cols.
5904 void wxGrid::ClearGrid()
5908 if (IsCellEditControlEnabled())
5909 DisableCellEditControl();
5912 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5917 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5919 // TODO: something with updateLabels flag
5923 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5929 if (IsCellEditControlEnabled())
5930 DisableCellEditControl();
5932 bool done
= m_table
->InsertRows( pos
, numRows
);
5935 // the table will have sent the results of the insert row
5936 // operation to this view object as a grid table message
5942 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5944 // TODO: something with updateLabels flag
5948 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5954 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5956 // the table will have sent the results of the append row
5957 // operation to this view object as a grid table message
5963 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5965 // TODO: something with updateLabels flag
5969 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5975 if (IsCellEditControlEnabled())
5976 DisableCellEditControl();
5978 bool done
= m_table
->DeleteRows( pos
, numRows
);
5980 // the table will have sent the results of the delete row
5981 // operation to this view object as a grid table message
5987 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5989 // TODO: something with updateLabels flag
5993 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5999 if (IsCellEditControlEnabled())
6000 DisableCellEditControl();
6002 bool done
= m_table
->InsertCols( pos
, numCols
);
6004 // the table will have sent the results of the insert col
6005 // operation to this view object as a grid table message
6011 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6013 // TODO: something with updateLabels flag
6017 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6023 bool done
= m_table
->AppendCols( numCols
);
6025 // the table will have sent the results of the append col
6026 // operation to this view object as a grid table message
6032 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6034 // TODO: something with updateLabels flag
6038 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6044 if (IsCellEditControlEnabled())
6045 DisableCellEditControl();
6047 bool done
= m_table
->DeleteCols( pos
, numCols
);
6049 // the table will have sent the results of the delete col
6050 // operation to this view object as a grid table message
6058 // ----- event handlers
6061 // Generate a grid event based on a mouse event and
6062 // return the result of ProcessEvent()
6064 int wxGrid::SendEvent( const wxEventType type
,
6066 wxMouseEvent
& mouseEv
)
6071 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6073 int rowOrCol
= (row
== -1 ? col
: row
);
6075 wxGridSizeEvent
gridEvt( GetId(),
6079 mouseEv
.GetX() + GetRowLabelSize(),
6080 mouseEv
.GetY() + GetColLabelSize(),
6081 mouseEv
.ControlDown(),
6082 mouseEv
.ShiftDown(),
6084 mouseEv
.MetaDown() );
6086 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6087 vetoed
= !gridEvt
.IsAllowed();
6089 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6091 // Right now, it should _never_ end up here!
6092 wxGridRangeSelectEvent
gridEvt( GetId(),
6096 m_selectingBottomRight
,
6098 mouseEv
.ControlDown(),
6099 mouseEv
.ShiftDown(),
6101 mouseEv
.MetaDown() );
6103 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6104 vetoed
= !gridEvt
.IsAllowed();
6108 wxGridEvent
gridEvt( GetId(),
6112 mouseEv
.GetX() + GetRowLabelSize(),
6113 mouseEv
.GetY() + GetColLabelSize(),
6115 mouseEv
.ControlDown(),
6116 mouseEv
.ShiftDown(),
6118 mouseEv
.MetaDown() );
6119 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6120 vetoed
= !gridEvt
.IsAllowed();
6123 // A Veto'd event may not be `claimed' so test this first
6124 if (vetoed
) return -1;
6125 return claimed
? 1 : 0;
6129 // Generate a grid event of specified type and return the result
6130 // of ProcessEvent().
6132 int wxGrid::SendEvent( const wxEventType type
,
6138 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6140 int rowOrCol
= (row
== -1 ? col
: row
);
6142 wxGridSizeEvent
gridEvt( GetId(),
6147 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6148 vetoed
= !gridEvt
.IsAllowed();
6152 wxGridEvent
gridEvt( GetId(),
6157 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6158 vetoed
= !gridEvt
.IsAllowed();
6161 // A Veto'd event may not be `claimed' so test this first
6162 if (vetoed
) return -1;
6163 return claimed
? 1 : 0;
6167 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6169 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6172 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6174 // Don't do anything if between Begin/EndBatch...
6175 // EndBatch() will do all this on the last nested one anyway.
6176 if (! GetBatchCount())
6178 // Refresh to get correct scrolled position:
6179 wxScrolledWindow::Refresh(eraseb
,rect
);
6183 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6184 int width_label
, width_cell
, height_label
, height_cell
;
6187 //Copy rectangle can get scroll offsets..
6188 rect_x
= rect
->GetX();
6189 rect_y
= rect
->GetY();
6190 rectWidth
= rect
->GetWidth();
6191 rectHeight
= rect
->GetHeight();
6193 width_label
= m_rowLabelWidth
- rect_x
;
6194 if (width_label
> rectWidth
) width_label
= rectWidth
;
6196 height_label
= m_colLabelHeight
- rect_y
;
6197 if (height_label
> rectHeight
) height_label
= rectHeight
;
6199 if (rect_x
> m_rowLabelWidth
)
6201 x
= rect_x
- m_rowLabelWidth
;
6202 width_cell
= rectWidth
;
6207 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6210 if (rect_y
> m_colLabelHeight
)
6212 y
= rect_y
- m_colLabelHeight
;
6213 height_cell
= rectHeight
;
6218 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6221 // Paint corner label part intersecting rect.
6222 if ( width_label
> 0 && height_label
> 0 )
6224 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6225 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6228 // Paint col labels part intersecting rect.
6229 if ( width_cell
> 0 && height_label
> 0 )
6231 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6232 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6235 // Paint row labels part intersecting rect.
6236 if ( width_label
> 0 && height_cell
> 0 )
6238 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6239 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6242 // Paint cell area part intersecting rect.
6243 if ( width_cell
> 0 && height_cell
> 0 )
6245 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6246 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6251 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6252 m_colLabelWin
->Refresh(eraseb
, NULL
);
6253 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6254 m_gridWin
->Refresh(eraseb
, NULL
);
6259 void wxGrid::OnSize( wxSizeEvent
& event
)
6261 // position the child windows
6264 // don't call CalcDimensions() from here, the base class handles the size
6270 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6272 if ( m_inOnKeyDown
)
6274 // shouldn't be here - we are going round in circles...
6276 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6279 m_inOnKeyDown
= true;
6281 // propagate the event up and see if it gets processed
6283 wxWindow
*parent
= GetParent();
6284 wxKeyEvent
keyEvt( event
);
6285 keyEvt
.SetEventObject( parent
);
6287 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6290 // try local handlers
6292 switch ( event
.GetKeyCode() )
6295 if ( event
.ControlDown() )
6297 MoveCursorUpBlock( event
.ShiftDown() );
6301 MoveCursorUp( event
.ShiftDown() );
6306 if ( event
.ControlDown() )
6308 MoveCursorDownBlock( event
.ShiftDown() );
6312 MoveCursorDown( event
.ShiftDown() );
6317 if ( event
.ControlDown() )
6319 MoveCursorLeftBlock( event
.ShiftDown() );
6323 MoveCursorLeft( event
.ShiftDown() );
6328 if ( event
.ControlDown() )
6330 MoveCursorRightBlock( event
.ShiftDown() );
6334 MoveCursorRight( event
.ShiftDown() );
6339 case WXK_NUMPAD_ENTER
:
6340 if ( event
.ControlDown() )
6342 event
.Skip(); // to let the edit control have the return
6346 if ( GetGridCursorRow() < GetNumberRows()-1 )
6348 MoveCursorDown( event
.ShiftDown() );
6352 // at the bottom of a column
6353 DisableCellEditControl();
6363 if (event
.ShiftDown())
6365 if ( GetGridCursorCol() > 0 )
6367 MoveCursorLeft( false );
6372 DisableCellEditControl();
6377 if ( GetGridCursorCol() < GetNumberCols()-1 )
6379 MoveCursorRight( false );
6384 DisableCellEditControl();
6390 if ( event
.ControlDown() )
6392 MakeCellVisible( 0, 0 );
6393 SetCurrentCell( 0, 0 );
6402 if ( event
.ControlDown() )
6404 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6405 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6422 if ( event
.ControlDown() )
6426 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6427 m_currentCellCoords
.GetCol(),
6428 event
.ControlDown(),
6435 if ( !IsEditable() )
6437 MoveCursorRight( false );
6440 // Otherwise fall through to default
6443 // is it possible to edit the current cell at all?
6444 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6446 // yes, now check whether the cells editor accepts the key
6447 int row
= m_currentCellCoords
.GetRow();
6448 int col
= m_currentCellCoords
.GetCol();
6449 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6450 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6452 // <F2> is special and will always start editing, for
6453 // other keys - ask the editor itself
6454 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6455 || editor
->IsAcceptedKey(event
) )
6457 // ensure cell is visble
6458 MakeCellVisible(row
, col
);
6459 EnableCellEditControl();
6461 // a problem can arise if the cell is not completely
6462 // visible (even after calling MakeCellVisible the
6463 // control is not created and calling StartingKey will
6465 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6477 // let others process char events with modifiers or all
6478 // char events for readonly cells
6485 m_inOnKeyDown
= false;
6488 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6490 // try local handlers
6492 if ( event
.GetKeyCode() == WXK_SHIFT
)
6494 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6495 m_selectingBottomRight
!= wxGridNoCellCoords
)
6499 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6500 m_selectingTopLeft
.GetCol(),
6501 m_selectingBottomRight
.GetRow(),
6502 m_selectingBottomRight
.GetCol(),
6503 event
.ControlDown(),
6510 m_selectingTopLeft
= wxGridNoCellCoords
;
6511 m_selectingBottomRight
= wxGridNoCellCoords
;
6512 m_selectingKeyboard
= wxGridNoCellCoords
;
6516 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6520 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6522 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6524 // the event has been intercepted - do nothing
6528 wxClientDC
dc(m_gridWin
);
6531 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6533 DisableCellEditControl();
6535 if ( IsVisible( m_currentCellCoords
, false ) )
6538 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6539 if ( !m_gridLinesEnabled
)
6547 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6549 // Otherwise refresh redraws the highlight!
6550 m_currentCellCoords
= coords
;
6552 DrawGridCellArea(dc
,cells
);
6553 DrawAllGridLines( dc
, r
);
6557 m_currentCellCoords
= coords
;
6559 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6560 DrawCellHighlight(dc
, attr
);
6565 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6568 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6572 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6575 rightCol
= GetNumberCols() - 1;
6577 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6580 bottomRow
= GetNumberRows() - 1;
6584 if ( topRow
> bottomRow
)
6591 if ( leftCol
> rightCol
)
6598 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6599 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6601 // First the case that we selected a completely new area
6602 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6603 m_selectingBottomRight
== wxGridNoCellCoords
)
6606 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6607 wxGridCellCoords ( bottomRow
, rightCol
) );
6608 m_gridWin
->Refresh( false, &rect
);
6610 // Now handle changing an existing selection area.
6611 else if ( m_selectingTopLeft
!= updateTopLeft
||
6612 m_selectingBottomRight
!= updateBottomRight
)
6614 // Compute two optimal update rectangles:
6615 // Either one rectangle is a real subset of the
6616 // other, or they are (almost) disjoint!
6618 bool need_refresh
[4];
6622 need_refresh
[3] = false;
6625 // Store intermediate values
6626 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6627 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6628 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6629 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6631 // Determine the outer/inner coordinates.
6632 if (oldLeft
> leftCol
)
6638 if (oldTop
> topRow
)
6644 if (oldRight
< rightCol
)
6647 oldRight
= rightCol
;
6650 if (oldBottom
< bottomRow
)
6653 oldBottom
= bottomRow
;
6657 // Now, either the stuff marked old is the outer
6658 // rectangle or we don't have a situation where one
6659 // is contained in the other.
6661 if ( oldLeft
< leftCol
)
6663 // Refresh the newly selected or deselected
6664 // area to the left of the old or new selection.
6665 need_refresh
[0] = true;
6666 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6668 wxGridCellCoords ( oldBottom
,
6672 if ( oldTop
< topRow
)
6674 // Refresh the newly selected or deselected
6675 // area above the old or new selection.
6676 need_refresh
[1] = true;
6677 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6679 wxGridCellCoords ( topRow
- 1,
6683 if ( oldRight
> rightCol
)
6685 // Refresh the newly selected or deselected
6686 // area to the right of the old or new selection.
6687 need_refresh
[2] = true;
6688 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6690 wxGridCellCoords ( oldBottom
,
6694 if ( oldBottom
> bottomRow
)
6696 // Refresh the newly selected or deselected
6697 // area below the old or new selection.
6698 need_refresh
[3] = true;
6699 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6701 wxGridCellCoords ( oldBottom
,
6705 // various Refresh() calls
6706 for (i
= 0; i
< 4; i
++ )
6707 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6708 m_gridWin
->Refresh( false, &(rect
[i
]) );
6711 m_selectingTopLeft
= updateTopLeft
;
6712 m_selectingBottomRight
= updateBottomRight
;
6716 // ------ functions to get/send data (see also public functions)
6719 bool wxGrid::GetModelValues()
6721 // Hide the editor, so it won't hide a changed value.
6722 HideCellEditControl();
6726 // all we need to do is repaint the grid
6728 m_gridWin
->Refresh();
6736 bool wxGrid::SetModelValues()
6740 // Disable the editor, so it won't hide a changed value.
6741 // Do we also want to save the current value of the editor first?
6743 DisableCellEditControl();
6747 for ( row
= 0; row
< m_numRows
; row
++ )
6749 for ( col
= 0; col
< m_numCols
; col
++ )
6751 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6763 // Note - this function only draws cells that are in the list of
6764 // exposed cells (usually set from the update region by
6765 // CalcExposedCells)
6767 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6769 if ( !m_numRows
|| !m_numCols
) return;
6771 int i
, numCells
= cells
.GetCount();
6772 int row
, col
, cell_rows
, cell_cols
;
6773 wxGridCellCoordsArray redrawCells
;
6775 for ( i
= numCells
-1; i
>= 0; i
-- )
6777 row
= cells
[i
].GetRow();
6778 col
= cells
[i
].GetCol();
6779 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6781 // If this cell is part of a multicell block, find owner for repaint
6782 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6784 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6785 bool marked
= false;
6786 for ( int j
= 0; j
< numCells
; j
++ )
6788 if ( cell
== cells
[j
] )
6796 int count
= redrawCells
.GetCount();
6797 for (int j
= 0; j
< count
; j
++)
6799 if ( cell
== redrawCells
[j
] )
6805 if (!marked
) redrawCells
.Add( cell
);
6807 continue; // don't bother drawing this cell
6810 // If this cell is empty, find cell to left that might want to overflow
6811 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6813 for ( int l
= 0; l
< cell_rows
; l
++ )
6815 // find a cell in this row to left alreay marked for repaint
6817 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6818 if ((redrawCells
[k
].GetCol() < left
) &&
6819 (redrawCells
[k
].GetRow() == row
))
6820 left
=redrawCells
[k
].GetCol();
6822 if (left
== col
) left
= 0; // oh well
6824 for (int j
= col
-1; j
>= left
; j
--)
6826 if (!m_table
->IsEmptyCell(row
+l
, j
))
6828 if (GetCellOverflow(row
+l
, j
))
6830 wxGridCellCoords
cell(row
+l
, j
);
6831 bool marked
= false;
6833 for (int k
= 0; k
< numCells
; k
++)
6835 if ( cell
== cells
[k
] )
6843 int count
= redrawCells
.GetCount();
6844 for (int k
= 0; k
< count
; k
++)
6846 if ( cell
== redrawCells
[k
] )
6852 if (!marked
) redrawCells
.Add( cell
);
6860 DrawCell( dc
, cells
[i
] );
6863 numCells
= redrawCells
.GetCount();
6865 for ( i
= numCells
- 1; i
>= 0; i
-- )
6867 DrawCell( dc
, redrawCells
[i
] );
6872 void wxGrid::DrawGridSpace( wxDC
& dc
)
6875 m_gridWin
->GetClientSize( &cw
, &ch
);
6878 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6880 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6881 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6883 if ( right
> rightCol
|| bottom
> bottomRow
)
6886 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6888 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6889 dc
.SetPen( *wxTRANSPARENT_PEN
);
6891 if ( right
> rightCol
)
6893 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6896 if ( bottom
> bottomRow
)
6898 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6904 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6906 int row
= coords
.GetRow();
6907 int col
= coords
.GetCol();
6909 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6912 // we draw the cell border ourselves
6913 #if !WXGRID_DRAW_LINES
6914 if ( m_gridLinesEnabled
)
6915 DrawCellBorder( dc
, coords
);
6918 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6920 bool isCurrent
= coords
== m_currentCellCoords
;
6922 wxRect rect
= CellToRect( row
, col
);
6924 // if the editor is shown, we should use it and not the renderer
6925 // Note: However, only if it is really _shown_, i.e. not hidden!
6926 if ( isCurrent
&& IsCellEditControlShown() )
6928 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6929 editor
->PaintBackground(rect
, attr
);
6934 // but all the rest is drawn by the cell renderer and hence may be
6936 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6937 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6944 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6946 int row
= m_currentCellCoords
.GetRow();
6947 int col
= m_currentCellCoords
.GetCol();
6949 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6952 wxRect rect
= CellToRect(row
, col
);
6954 // hmmm... what could we do here to show that the cell is disabled?
6955 // for now, I just draw a thinner border than for the other ones, but
6956 // it doesn't look really good
6958 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6962 // The center of th drawn line is where the position/width/height of
6963 // the rectangle is actually at, (on wxMSW atr least,) so we will
6964 // reduce the size of the rectangle to compensate for the thickness of
6965 // the line. If this is too strange on non wxMSW platforms then
6966 // please #ifdef this appropriately.
6967 rect
.x
+= penWidth
/2;
6968 rect
.y
+= penWidth
/2;
6969 rect
.width
-= penWidth
-1;
6970 rect
.height
-= penWidth
-1;
6973 // Now draw the rectangle
6974 // use the cellHighlightColour if the cell is inside a selection, this
6975 // will ensure the cell is always visible.
6976 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6977 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6978 dc
.DrawRectangle(rect
);
6982 // VZ: my experiments with 3d borders...
6984 // how to properly set colours for arbitrary bg?
6985 wxCoord x1
= rect
.x
,
6987 x2
= rect
.x
+ rect
.width
-1,
6988 y2
= rect
.y
+ rect
.height
-1;
6990 dc
.SetPen(*wxWHITE_PEN
);
6991 dc
.DrawLine(x1
, y1
, x2
, y1
);
6992 dc
.DrawLine(x1
, y1
, x1
, y2
);
6994 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6995 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6997 dc
.SetPen(*wxBLACK_PEN
);
6998 dc
.DrawLine(x1
, y2
, x2
, y2
);
6999 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7004 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7006 int row
= coords
.GetRow();
7007 int col
= coords
.GetCol();
7008 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7011 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7013 wxRect rect
= CellToRect( row
, col
);
7015 // right hand border
7017 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7018 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7022 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7023 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7026 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7028 // This if block was previously in wxGrid::OnPaint but that doesn't
7029 // seem to get called under wxGTK - MB
7031 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7032 m_numRows
&& m_numCols
)
7034 m_currentCellCoords
.Set(0, 0);
7037 if ( IsCellEditControlShown() )
7039 // don't show highlight when the edit control is shown
7043 // if the active cell was repainted, repaint its highlight too because it
7044 // might have been damaged by the grid lines
7045 size_t count
= cells
.GetCount();
7046 for ( size_t n
= 0; n
< count
; n
++ )
7048 if ( cells
[n
] == m_currentCellCoords
)
7050 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7051 DrawCellHighlight(dc
, attr
);
7059 // TODO: remove this ???
7060 // This is used to redraw all grid lines e.g. when the grid line colour
7063 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7065 #if !WXGRID_DRAW_LINES
7069 if ( !m_gridLinesEnabled
||
7071 !m_numCols
) return;
7073 int top
, bottom
, left
, right
;
7075 #if 0 //#ifndef __WXGTK__
7079 m_gridWin
->GetClientSize(&cw
, &ch
);
7081 // virtual coords of visible area
7083 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7084 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7089 reg
.GetBox(x
, y
, w
, h
);
7090 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7091 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7095 m_gridWin
->GetClientSize(&cw
, &ch
);
7096 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7097 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7100 // avoid drawing grid lines past the last row and col
7102 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7103 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7105 // no gridlines inside multicells, clip them out
7106 int leftCol
= internalXToCol(left
);
7107 int topRow
= internalYToRow(top
);
7108 int rightCol
= internalXToCol(right
);
7109 int bottomRow
= internalYToRow(bottom
);
7110 wxRegion
clippedcells(0, 0, cw
, ch
);
7113 int i
, j
, cell_rows
, cell_cols
;
7116 for (j
=topRow
; j
<bottomRow
; j
++)
7118 for (i
=leftCol
; i
<rightCol
; i
++)
7120 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7121 if ((cell_rows
> 1) || (cell_cols
> 1))
7123 rect
= CellToRect(j
,i
);
7124 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7125 clippedcells
.Subtract(rect
);
7127 else if ((cell_rows
< 0) || (cell_cols
< 0))
7129 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7130 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7131 clippedcells
.Subtract(rect
);
7135 dc
.SetClippingRegion( clippedcells
);
7137 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7139 // horizontal grid lines
7141 // already declared above - int i;
7142 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7144 int bot
= GetRowBottom(i
) - 1;
7153 dc
.DrawLine( left
, bot
, right
, bot
);
7158 // vertical grid lines
7160 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7162 int colRight
= GetColRight(i
) - 1;
7163 if ( colRight
> right
)
7168 if ( colRight
>= left
)
7170 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7173 dc
.DestroyClippingRegion();
7177 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7179 if ( !m_numRows
) return;
7182 size_t numLabels
= rows
.GetCount();
7184 for ( i
= 0; i
< numLabels
; i
++ )
7186 DrawRowLabel( dc
, rows
[i
] );
7191 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7193 if ( GetRowHeight(row
) <= 0 )
7196 int rowTop
= GetRowTop(row
),
7197 rowBottom
= GetRowBottom(row
) - 1;
7199 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7200 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7201 m_rowLabelWidth
-1, rowBottom
);
7203 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7205 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7207 dc
.SetPen( *wxWHITE_PEN
);
7208 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7209 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7211 dc
.SetBackgroundMode( wxTRANSPARENT
);
7212 dc
.SetTextForeground( GetLabelTextColour() );
7213 dc
.SetFont( GetLabelFont() );
7216 GetRowLabelAlignment( &hAlign
, &vAlign
);
7220 rect
.SetY( GetRowTop(row
) + 2 );
7221 rect
.SetWidth( m_rowLabelWidth
- 4 );
7222 rect
.SetHeight( GetRowHeight(row
) - 4 );
7223 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7227 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7229 if ( !m_numCols
) return;
7232 size_t numLabels
= cols
.GetCount();
7234 for ( i
= 0; i
< numLabels
; i
++ )
7236 DrawColLabel( dc
, cols
[i
] );
7241 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7243 if ( GetColWidth(col
) <= 0 )
7246 int colLeft
= GetColLeft(col
),
7247 colRight
= GetColRight(col
) - 1;
7249 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7250 dc
.DrawLine( colRight
, 0,
7251 colRight
, m_colLabelHeight
-1 );
7253 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7255 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7256 colRight
+1, m_colLabelHeight
-1 );
7258 dc
.SetPen( *wxWHITE_PEN
);
7259 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7260 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7262 dc
.SetBackgroundMode( wxTRANSPARENT
);
7263 dc
.SetTextForeground( GetLabelTextColour() );
7264 dc
.SetFont( GetLabelFont() );
7266 int hAlign
, vAlign
, orient
;
7267 GetColLabelAlignment( &hAlign
, &vAlign
);
7268 orient
= GetColLabelTextOrientation();
7271 rect
.SetX( colLeft
+ 2 );
7273 rect
.SetWidth( GetColWidth(col
) - 4 );
7274 rect
.SetHeight( m_colLabelHeight
- 4 );
7275 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7278 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7279 const wxString
& value
,
7283 int textOrientation
)
7285 wxArrayString lines
;
7287 StringToLines( value
, lines
);
7290 //Forward to new API.
7291 DrawTextRectangle( dc
,
7300 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7301 const wxArrayString
& lines
,
7305 int textOrientation
)
7307 long textWidth
, textHeight
;
7308 long lineWidth
, lineHeight
;
7311 dc
.SetClippingRegion( rect
);
7313 nLines
= lines
.GetCount();
7317 float x
= 0.0, y
= 0.0;
7319 if( textOrientation
== wxHORIZONTAL
)
7320 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7322 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7326 case wxALIGN_BOTTOM
:
7327 if( textOrientation
== wxHORIZONTAL
)
7328 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7330 x
= rect
.x
+ rect
.width
- textWidth
;
7333 case wxALIGN_CENTRE
:
7334 if( textOrientation
== wxHORIZONTAL
)
7335 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7337 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7342 if( textOrientation
== wxHORIZONTAL
)
7349 // Align each line of a multi-line label
7350 for( l
= 0; l
< nLines
; l
++ )
7352 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7354 switch( horizAlign
)
7357 if( textOrientation
== wxHORIZONTAL
)
7358 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7360 y
= rect
.y
+ lineWidth
+ 1;
7363 case wxALIGN_CENTRE
:
7364 if( textOrientation
== wxHORIZONTAL
)
7365 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7367 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7372 if( textOrientation
== wxHORIZONTAL
)
7375 y
= rect
.y
+ rect
.height
- 1;
7379 if( textOrientation
== wxHORIZONTAL
)
7381 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7386 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7391 dc
.DestroyClippingRegion();
7395 // Split multi line text up into an array of strings. Any existing
7396 // contents of the string array are preserved.
7398 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7402 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7403 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7405 while ( startPos
< (int)tVal
.Length() )
7407 pos
= tVal
.Mid(startPos
).Find( eol
);
7412 else if ( pos
== 0 )
7414 lines
.Add( wxEmptyString
);
7418 lines
.Add( value
.Mid(startPos
, pos
) );
7422 if ( startPos
< (int)value
.Length() )
7424 lines
.Add( value
.Mid( startPos
) );
7429 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7430 const wxArrayString
& lines
,
7431 long *width
, long *height
)
7438 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7440 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7441 w
= wxMax( w
, lineW
);
7450 // ------ Batch processing.
7452 void wxGrid::EndBatch()
7454 if ( m_batchCount
> 0 )
7457 if ( !m_batchCount
)
7460 m_rowLabelWin
->Refresh();
7461 m_colLabelWin
->Refresh();
7462 m_cornerLabelWin
->Refresh();
7463 m_gridWin
->Refresh();
7468 // Use this, rather than wxWindow::Refresh(), to force an immediate
7469 // repainting of the grid. Has no effect if you are already inside a
7470 // BeginBatch / EndBatch block.
7472 void wxGrid::ForceRefresh()
7480 // ------ Edit control functions
7484 void wxGrid::EnableEditing( bool edit
)
7486 // TODO: improve this ?
7488 if ( edit
!= m_editable
)
7490 if(!edit
) EnableCellEditControl(edit
);
7496 void wxGrid::EnableCellEditControl( bool enable
)
7501 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7502 SetCurrentCell( 0, 0 );
7504 if ( enable
!= m_cellEditCtrlEnabled
)
7508 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7511 // this should be checked by the caller!
7512 wxASSERT_MSG( CanEnableCellControl(),
7513 _T("can't enable editing for this cell!") );
7515 // do it before ShowCellEditControl()
7516 m_cellEditCtrlEnabled
= enable
;
7518 ShowCellEditControl();
7522 //FIXME:add veto support
7523 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7525 HideCellEditControl();
7526 SaveEditControlValue();
7528 // do it after HideCellEditControl()
7529 m_cellEditCtrlEnabled
= enable
;
7534 bool wxGrid::IsCurrentCellReadOnly() const
7537 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7538 bool readonly
= attr
->IsReadOnly();
7544 bool wxGrid::CanEnableCellControl() const
7546 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7547 !IsCurrentCellReadOnly();
7551 bool wxGrid::IsCellEditControlEnabled() const
7553 // the cell edit control might be disable for all cells or just for the
7554 // current one if it's read only
7555 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7558 bool wxGrid::IsCellEditControlShown() const
7560 bool isShown
= false;
7562 if ( m_cellEditCtrlEnabled
)
7564 int row
= m_currentCellCoords
.GetRow();
7565 int col
= m_currentCellCoords
.GetCol();
7566 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7567 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7572 if ( editor
->IsCreated() )
7574 isShown
= editor
->GetControl()->IsShown();
7584 void wxGrid::ShowCellEditControl()
7586 if ( IsCellEditControlEnabled() )
7588 if ( !IsVisible( m_currentCellCoords
) )
7590 m_cellEditCtrlEnabled
= false;
7595 wxRect rect
= CellToRect( m_currentCellCoords
);
7596 int row
= m_currentCellCoords
.GetRow();
7597 int col
= m_currentCellCoords
.GetCol();
7599 // if this is part of a multicell, find owner (topleft)
7600 int cell_rows
, cell_cols
;
7601 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7602 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7606 m_currentCellCoords
.SetRow( row
);
7607 m_currentCellCoords
.SetCol( col
);
7610 // convert to scrolled coords
7612 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7614 // done in PaintBackground()
7616 // erase the highlight and the cell contents because the editor
7617 // might not cover the entire cell
7618 wxClientDC
dc( m_gridWin
);
7620 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7621 dc
.SetPen(*wxTRANSPARENT_PEN
);
7622 dc
.DrawRectangle(rect
);
7625 // cell is shifted by one pixel
7626 // However, don't allow x or y to become negative
7627 // since the SetSize() method interprets that as
7634 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7635 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7636 if ( !editor
->IsCreated() )
7638 editor
->Create(m_gridWin
, wxID_ANY
,
7639 new wxGridCellEditorEvtHandler(this, editor
));
7641 wxGridEditorCreatedEvent
evt(GetId(),
7642 wxEVT_GRID_EDITOR_CREATED
,
7646 editor
->GetControl());
7647 GetEventHandler()->ProcessEvent(evt
);
7651 // resize editor to overflow into righthand cells if allowed
7652 int maxWidth
= rect
.width
;
7653 wxString value
= GetCellValue(row
, col
);
7654 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7657 GetTextExtent(value
, &maxWidth
, &y
,
7658 NULL
, NULL
, &attr
->GetFont());
7659 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7661 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7662 if (rect
.x
+maxWidth
> client_right
)
7663 maxWidth
= client_right
- rect
.x
;
7665 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7667 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7668 // may have changed earlier
7669 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7672 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7673 // looks weird going over a multicell
7674 if (m_table
->IsEmptyCell(row
,i
) &&
7675 (rect
.width
< maxWidth
) && (c_rows
== 1))
7676 rect
.width
+= GetColWidth(i
);
7680 if (rect
.GetRight() > client_right
)
7681 rect
.SetRight(client_right
-1);
7684 editor
->SetCellAttr(attr
);
7685 editor
->SetSize( rect
);
7686 editor
->Show( true, attr
);
7688 // recalc dimensions in case we need to
7689 // expand the scrolled window to account for editor
7692 editor
->BeginEdit(row
, col
, this);
7693 editor
->SetCellAttr(NULL
);
7702 void wxGrid::HideCellEditControl()
7704 if ( IsCellEditControlEnabled() )
7706 int row
= m_currentCellCoords
.GetRow();
7707 int col
= m_currentCellCoords
.GetCol();
7709 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7710 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7711 editor
->Show( false );
7714 m_gridWin
->SetFocus();
7715 // refresh whole row to the right
7716 wxRect
rect( CellToRect(row
, col
) );
7717 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7718 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7719 m_gridWin
->Refresh( false, &rect
);
7724 void wxGrid::SaveEditControlValue()
7726 if ( IsCellEditControlEnabled() )
7728 int row
= m_currentCellCoords
.GetRow();
7729 int col
= m_currentCellCoords
.GetCol();
7731 wxString oldval
= GetCellValue(row
,col
);
7733 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7734 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7735 bool changed
= editor
->EndEdit(row
, col
, this);
7742 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7743 m_currentCellCoords
.GetRow(),
7744 m_currentCellCoords
.GetCol() ) < 0 ) {
7746 // Event has been vetoed, set the data back.
7747 SetCellValue(row
,col
,oldval
);
7755 // ------ Grid location functions
7756 // Note that all of these functions work with the logical coordinates of
7757 // grid cells and labels so you will need to convert from device
7758 // coordinates for mouse events etc.
7761 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7763 int row
= YToRow(y
);
7764 int col
= XToCol(x
);
7766 if ( row
== -1 || col
== -1 )
7768 coords
= wxGridNoCellCoords
;
7772 coords
.Set( row
, col
);
7777 // Internal Helper function for computing row or column from some
7778 // (unscrolled) coordinate value, using either
7779 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7780 // of m_rowBottoms/m_ColRights to speed up the search!
7782 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7783 const wxArrayInt
& BorderArray
, int nMax
,
7788 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7794 size_t i_max
= coord
/ defaultDist
,
7797 if (BorderArray
.IsEmpty())
7799 if((int) i_max
< nMax
)
7801 return clipToMinMax
? nMax
- 1 : -1;
7804 if ( i_max
>= BorderArray
.GetCount())
7805 i_max
= BorderArray
.GetCount() - 1;
7808 if ( coord
>= BorderArray
[i_max
])
7812 i_max
= coord
/ minDist
;
7814 i_max
= BorderArray
.GetCount() - 1;
7816 if ( i_max
>= BorderArray
.GetCount())
7817 i_max
= BorderArray
.GetCount() - 1;
7819 if ( coord
>= BorderArray
[i_max
])
7820 return clipToMinMax
? (int)i_max
: -1;
7821 if ( coord
< BorderArray
[0] )
7824 while ( i_max
- i_min
> 0 )
7826 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7827 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7828 if (coord
>= BorderArray
[ i_max
- 1])
7832 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7833 if (coord
< BorderArray
[median
])
7841 int wxGrid::YToRow( int y
)
7843 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7844 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7848 int wxGrid::XToCol( int x
)
7850 return CoordToRowOrCol(x
, m_defaultColWidth
,
7851 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7855 // return the row number that that the y coord is near the edge of, or
7856 // -1 if not near an edge
7858 int wxGrid::YToEdgeOfRow( int y
)
7861 i
= internalYToRow(y
);
7863 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7865 // We know that we are in row i, test whether we are
7866 // close enough to lower or upper border, respectively.
7867 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7869 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7877 // return the col number that that the x coord is near the edge of, or
7878 // -1 if not near an edge
7880 int wxGrid::XToEdgeOfCol( int x
)
7883 i
= internalXToCol(x
);
7885 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7887 // We know that we are in column i, test whether we are
7888 // close enough to right or left border, respectively.
7889 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7891 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7899 wxRect
wxGrid::CellToRect( int row
, int col
)
7901 wxRect
rect( -1, -1, -1, -1 );
7903 if ( row
>= 0 && row
< m_numRows
&&
7904 col
>= 0 && col
< m_numCols
)
7906 int i
, cell_rows
, cell_cols
;
7907 rect
.width
= rect
.height
= 0;
7908 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7909 // if negative then find multicell owner
7910 if (cell_rows
< 0) row
+= cell_rows
;
7911 if (cell_cols
< 0) col
+= cell_cols
;
7912 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7914 rect
.x
= GetColLeft(col
);
7915 rect
.y
= GetRowTop(row
);
7916 for (i
=col
; i
<col
+cell_cols
; i
++)
7917 rect
.width
+= GetColWidth(i
);
7918 for (i
=row
; i
<row
+cell_rows
; i
++)
7919 rect
.height
+= GetRowHeight(i
);
7922 // if grid lines are enabled, then the area of the cell is a bit smaller
7923 if (m_gridLinesEnabled
) {
7931 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7933 // get the cell rectangle in logical coords
7935 wxRect
r( CellToRect( row
, col
) );
7937 // convert to device coords
7939 int left
, top
, right
, bottom
;
7940 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7941 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7943 // check against the client area of the grid window
7946 m_gridWin
->GetClientSize( &cw
, &ch
);
7948 if ( wholeCellVisible
)
7950 // is the cell wholly visible ?
7952 return ( left
>= 0 && right
<= cw
&&
7953 top
>= 0 && bottom
<= ch
);
7957 // is the cell partly visible ?
7959 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7960 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7965 // make the specified cell location visible by doing a minimal amount
7968 void wxGrid::MakeCellVisible( int row
, int col
)
7972 int xpos
= -1, ypos
= -1;
7974 if ( row
>= 0 && row
< m_numRows
&&
7975 col
>= 0 && col
< m_numCols
)
7977 // get the cell rectangle in logical coords
7979 wxRect
r( CellToRect( row
, col
) );
7981 // convert to device coords
7983 int left
, top
, right
, bottom
;
7984 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7985 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7988 m_gridWin
->GetClientSize( &cw
, &ch
);
7994 else if ( bottom
> ch
)
7996 int h
= r
.GetHeight();
7998 for ( i
= row
-1; i
>= 0; i
-- )
8000 int rowHeight
= GetRowHeight(i
);
8001 if ( h
+ rowHeight
> ch
)
8008 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8009 // have rounding errors (this is important, because if we do, we
8010 // might not scroll at all and some cells won't be redrawn)
8012 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8014 ypos
+= GRID_SCROLL_LINE_Y
;
8021 else if ( right
> cw
)
8023 // position the view so that the cell is on the right
8025 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8026 xpos
= x0
+ (right
- cw
);
8028 // see comment for ypos above
8029 xpos
+= GRID_SCROLL_LINE_X
;
8032 if ( xpos
!= -1 || ypos
!= -1 )
8035 xpos
/= GRID_SCROLL_LINE_X
;
8037 ypos
/= GRID_SCROLL_LINE_Y
;
8038 Scroll( xpos
, ypos
);
8046 // ------ Grid cursor movement functions
8049 bool wxGrid::MoveCursorUp( bool expandSelection
)
8051 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8052 m_currentCellCoords
.GetRow() >= 0 )
8054 if ( expandSelection
)
8056 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8057 m_selectingKeyboard
= m_currentCellCoords
;
8058 if ( m_selectingKeyboard
.GetRow() > 0 )
8060 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8061 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8062 m_selectingKeyboard
.GetCol() );
8063 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8066 else if ( m_currentCellCoords
.GetRow() > 0 )
8069 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8070 m_currentCellCoords
.GetCol() );
8071 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8072 m_currentCellCoords
.GetCol() );
8083 bool wxGrid::MoveCursorDown( bool expandSelection
)
8085 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8086 m_currentCellCoords
.GetRow() < m_numRows
)
8088 if ( expandSelection
)
8090 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8091 m_selectingKeyboard
= m_currentCellCoords
;
8092 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8094 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8095 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8096 m_selectingKeyboard
.GetCol() );
8097 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8100 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8103 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8104 m_currentCellCoords
.GetCol() );
8105 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8106 m_currentCellCoords
.GetCol() );
8117 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8119 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8120 m_currentCellCoords
.GetCol() >= 0 )
8122 if ( expandSelection
)
8124 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8125 m_selectingKeyboard
= m_currentCellCoords
;
8126 if ( m_selectingKeyboard
.GetCol() > 0 )
8128 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8129 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8130 m_selectingKeyboard
.GetCol() );
8131 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8134 else if ( m_currentCellCoords
.GetCol() > 0 )
8137 MakeCellVisible( m_currentCellCoords
.GetRow(),
8138 m_currentCellCoords
.GetCol() - 1 );
8139 SetCurrentCell( m_currentCellCoords
.GetRow(),
8140 m_currentCellCoords
.GetCol() - 1 );
8151 bool wxGrid::MoveCursorRight( bool expandSelection
)
8153 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8154 m_currentCellCoords
.GetCol() < m_numCols
)
8156 if ( expandSelection
)
8158 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8159 m_selectingKeyboard
= m_currentCellCoords
;
8160 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8162 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8163 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8164 m_selectingKeyboard
.GetCol() );
8165 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8168 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8171 MakeCellVisible( m_currentCellCoords
.GetRow(),
8172 m_currentCellCoords
.GetCol() + 1 );
8173 SetCurrentCell( m_currentCellCoords
.GetRow(),
8174 m_currentCellCoords
.GetCol() + 1 );
8185 bool wxGrid::MovePageUp()
8187 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8189 int row
= m_currentCellCoords
.GetRow();
8193 m_gridWin
->GetClientSize( &cw
, &ch
);
8195 int y
= GetRowTop(row
);
8196 int newRow
= internalYToRow( y
- ch
+ 1 );
8198 if ( newRow
== row
)
8200 //row > 0 , so newrow can never be less than 0 here.
8204 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8205 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8213 bool wxGrid::MovePageDown()
8215 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8217 int row
= m_currentCellCoords
.GetRow();
8218 if ( (row
+1) < m_numRows
)
8221 m_gridWin
->GetClientSize( &cw
, &ch
);
8223 int y
= GetRowTop(row
);
8224 int newRow
= internalYToRow( y
+ ch
);
8225 if ( newRow
== row
)
8227 // row < m_numRows , so newrow can't overflow here.
8231 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8232 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8240 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8243 m_currentCellCoords
!= wxGridNoCellCoords
&&
8244 m_currentCellCoords
.GetRow() > 0 )
8246 int row
= m_currentCellCoords
.GetRow();
8247 int col
= m_currentCellCoords
.GetCol();
8249 if ( m_table
->IsEmptyCell(row
, col
) )
8251 // starting in an empty cell: find the next block of
8257 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8260 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8262 // starting at the top of a block: find the next block
8268 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8273 // starting within a block: find the top of the block
8278 if ( m_table
->IsEmptyCell(row
, col
) )
8286 MakeCellVisible( row
, col
);
8287 if ( expandSelection
)
8289 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8290 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8295 SetCurrentCell( row
, col
);
8303 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8306 m_currentCellCoords
!= wxGridNoCellCoords
&&
8307 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8309 int row
= m_currentCellCoords
.GetRow();
8310 int col
= m_currentCellCoords
.GetCol();
8312 if ( m_table
->IsEmptyCell(row
, col
) )
8314 // starting in an empty cell: find the next block of
8317 while ( row
< m_numRows
-1 )
8320 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8323 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8325 // starting at the bottom of a block: find the next block
8328 while ( row
< m_numRows
-1 )
8331 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8336 // starting within a block: find the bottom of the block
8338 while ( row
< m_numRows
-1 )
8341 if ( m_table
->IsEmptyCell(row
, col
) )
8349 MakeCellVisible( row
, col
);
8350 if ( expandSelection
)
8352 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8353 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8358 SetCurrentCell( row
, col
);
8367 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8370 m_currentCellCoords
!= wxGridNoCellCoords
&&
8371 m_currentCellCoords
.GetCol() > 0 )
8373 int row
= m_currentCellCoords
.GetRow();
8374 int col
= m_currentCellCoords
.GetCol();
8376 if ( m_table
->IsEmptyCell(row
, col
) )
8378 // starting in an empty cell: find the next block of
8384 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8387 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8389 // starting at the left of a block: find the next block
8395 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8400 // starting within a block: find the left of the block
8405 if ( m_table
->IsEmptyCell(row
, col
) )
8413 MakeCellVisible( row
, col
);
8414 if ( expandSelection
)
8416 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8417 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8422 SetCurrentCell( row
, col
);
8431 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8434 m_currentCellCoords
!= wxGridNoCellCoords
&&
8435 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8437 int row
= m_currentCellCoords
.GetRow();
8438 int col
= m_currentCellCoords
.GetCol();
8440 if ( m_table
->IsEmptyCell(row
, col
) )
8442 // starting in an empty cell: find the next block of
8445 while ( col
< m_numCols
-1 )
8448 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8451 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8453 // starting at the right of a block: find the next block
8456 while ( col
< m_numCols
-1 )
8459 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8464 // starting within a block: find the right of the block
8466 while ( col
< m_numCols
-1 )
8469 if ( m_table
->IsEmptyCell(row
, col
) )
8477 MakeCellVisible( row
, col
);
8478 if ( expandSelection
)
8480 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8481 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8486 SetCurrentCell( row
, col
);
8498 // ------ Label values and formatting
8501 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8503 *horiz
= m_rowLabelHorizAlign
;
8504 *vert
= m_rowLabelVertAlign
;
8507 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8509 *horiz
= m_colLabelHorizAlign
;
8510 *vert
= m_colLabelVertAlign
;
8513 int wxGrid::GetColLabelTextOrientation()
8515 return m_colLabelTextOrientation
;
8518 wxString
wxGrid::GetRowLabelValue( int row
)
8522 return m_table
->GetRowLabelValue( row
);
8532 wxString
wxGrid::GetColLabelValue( int col
)
8536 return m_table
->GetColLabelValue( col
);
8547 void wxGrid::SetRowLabelSize( int width
)
8549 width
= wxMax( width
, 0 );
8550 if ( width
!= m_rowLabelWidth
)
8554 m_rowLabelWin
->Show( false );
8555 m_cornerLabelWin
->Show( false );
8557 else if ( m_rowLabelWidth
== 0 )
8559 m_rowLabelWin
->Show( true );
8560 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8563 m_rowLabelWidth
= width
;
8565 wxScrolledWindow::Refresh( true );
8570 void wxGrid::SetColLabelSize( int height
)
8572 height
= wxMax( height
, 0 );
8573 if ( height
!= m_colLabelHeight
)
8577 m_colLabelWin
->Show( false );
8578 m_cornerLabelWin
->Show( false );
8580 else if ( m_colLabelHeight
== 0 )
8582 m_colLabelWin
->Show( true );
8583 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8586 m_colLabelHeight
= height
;
8588 wxScrolledWindow::Refresh( true );
8593 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8595 if ( m_labelBackgroundColour
!= colour
)
8597 m_labelBackgroundColour
= colour
;
8598 m_rowLabelWin
->SetBackgroundColour( colour
);
8599 m_colLabelWin
->SetBackgroundColour( colour
);
8600 m_cornerLabelWin
->SetBackgroundColour( colour
);
8602 if ( !GetBatchCount() )
8604 m_rowLabelWin
->Refresh();
8605 m_colLabelWin
->Refresh();
8606 m_cornerLabelWin
->Refresh();
8611 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8613 if ( m_labelTextColour
!= colour
)
8615 m_labelTextColour
= colour
;
8616 if ( !GetBatchCount() )
8618 m_rowLabelWin
->Refresh();
8619 m_colLabelWin
->Refresh();
8624 void wxGrid::SetLabelFont( const wxFont
& font
)
8627 if ( !GetBatchCount() )
8629 m_rowLabelWin
->Refresh();
8630 m_colLabelWin
->Refresh();
8634 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8636 // allow old (incorrect) defs to be used
8639 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8640 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8641 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8646 case wxTOP
: vert
= wxALIGN_TOP
; break;
8647 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8648 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8651 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8653 m_rowLabelHorizAlign
= horiz
;
8656 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8658 m_rowLabelVertAlign
= vert
;
8661 if ( !GetBatchCount() )
8663 m_rowLabelWin
->Refresh();
8667 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8669 // allow old (incorrect) defs to be used
8672 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8673 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8674 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8679 case wxTOP
: vert
= wxALIGN_TOP
; break;
8680 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8681 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8684 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8686 m_colLabelHorizAlign
= horiz
;
8689 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8691 m_colLabelVertAlign
= vert
;
8694 if ( !GetBatchCount() )
8696 m_colLabelWin
->Refresh();
8700 // Note: under MSW, the default column label font must be changed because it
8701 // does not support vertical printing
8703 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8704 // pGrid->SetLabelFont(font);
8705 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8707 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8709 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8711 m_colLabelTextOrientation
= textOrientation
;
8714 if ( !GetBatchCount() )
8716 m_colLabelWin
->Refresh();
8720 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8724 m_table
->SetRowLabelValue( row
, s
);
8725 if ( !GetBatchCount() )
8727 wxRect rect
= CellToRect( row
, 0);
8728 if ( rect
.height
> 0 )
8730 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8732 rect
.width
= m_rowLabelWidth
;
8733 m_rowLabelWin
->Refresh( true, &rect
);
8739 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8743 m_table
->SetColLabelValue( col
, s
);
8744 if ( !GetBatchCount() )
8746 wxRect rect
= CellToRect( 0, col
);
8747 if ( rect
.width
> 0 )
8749 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8751 rect
.height
= m_colLabelHeight
;
8752 m_colLabelWin
->Refresh( true, &rect
);
8758 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8760 if ( m_gridLineColour
!= colour
)
8762 m_gridLineColour
= colour
;
8764 wxClientDC
dc( m_gridWin
);
8766 DrawAllGridLines( dc
, wxRegion() );
8771 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8773 if ( m_cellHighlightColour
!= colour
)
8775 m_cellHighlightColour
= colour
;
8777 wxClientDC
dc( m_gridWin
);
8779 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8780 DrawCellHighlight(dc
, attr
);
8785 void wxGrid::SetCellHighlightPenWidth(int width
)
8787 if (m_cellHighlightPenWidth
!= width
) {
8788 m_cellHighlightPenWidth
= width
;
8790 // Just redrawing the cell highlight is not enough since that won't
8791 // make any visible change if the the thickness is getting smaller.
8792 int row
= m_currentCellCoords
.GetRow();
8793 int col
= m_currentCellCoords
.GetCol();
8794 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8796 wxRect rect
= CellToRect(row
, col
);
8797 m_gridWin
->Refresh(true, &rect
);
8801 void wxGrid::SetCellHighlightROPenWidth(int width
)
8803 if (m_cellHighlightROPenWidth
!= width
) {
8804 m_cellHighlightROPenWidth
= width
;
8806 // Just redrawing the cell highlight is not enough since that won't
8807 // make any visible change if the the thickness is getting smaller.
8808 int row
= m_currentCellCoords
.GetRow();
8809 int col
= m_currentCellCoords
.GetCol();
8810 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8812 wxRect rect
= CellToRect(row
, col
);
8813 m_gridWin
->Refresh(true, &rect
);
8817 void wxGrid::EnableGridLines( bool enable
)
8819 if ( enable
!= m_gridLinesEnabled
)
8821 m_gridLinesEnabled
= enable
;
8823 if ( !GetBatchCount() )
8827 wxClientDC
dc( m_gridWin
);
8829 DrawAllGridLines( dc
, wxRegion() );
8833 m_gridWin
->Refresh();
8840 int wxGrid::GetDefaultRowSize()
8842 return m_defaultRowHeight
;
8845 int wxGrid::GetRowSize( int row
)
8847 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8849 return GetRowHeight(row
);
8852 int wxGrid::GetDefaultColSize()
8854 return m_defaultColWidth
;
8857 int wxGrid::GetColSize( int col
)
8859 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8861 return GetColWidth(col
);
8864 // ============================================================================
8865 // access to the grid attributes: each of them has a default value in the grid
8866 // itself and may be overidden on a per-cell basis
8867 // ============================================================================
8869 // ----------------------------------------------------------------------------
8870 // setting default attributes
8871 // ----------------------------------------------------------------------------
8873 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8875 m_defaultCellAttr
->SetBackgroundColour(col
);
8877 m_gridWin
->SetBackgroundColour(col
);
8881 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8883 m_defaultCellAttr
->SetTextColour(col
);
8886 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8888 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8891 void wxGrid::SetDefaultCellOverflow( bool allow
)
8893 m_defaultCellAttr
->SetOverflow(allow
);
8896 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8898 m_defaultCellAttr
->SetFont(font
);
8902 // For editors and renderers the type registry takes precedence over the
8903 // default attr, so we need to register the new editor/renderer for the string
8904 // data type in order to make setting a default editor/renderer appear to
8907 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8909 RegisterDataType(wxGRID_VALUE_STRING
,
8911 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
8914 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8916 RegisterDataType(wxGRID_VALUE_STRING
,
8917 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
8921 // ----------------------------------------------------------------------------
8922 // access to the default attrbiutes
8923 // ----------------------------------------------------------------------------
8925 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8927 return m_defaultCellAttr
->GetBackgroundColour();
8930 wxColour
wxGrid::GetDefaultCellTextColour()
8932 return m_defaultCellAttr
->GetTextColour();
8935 wxFont
wxGrid::GetDefaultCellFont()
8937 return m_defaultCellAttr
->GetFont();
8940 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8942 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8945 bool wxGrid::GetDefaultCellOverflow()
8947 return m_defaultCellAttr
->GetOverflow();
8950 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8952 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8955 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8957 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8960 // ----------------------------------------------------------------------------
8961 // access to cell attributes
8962 // ----------------------------------------------------------------------------
8964 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8966 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8967 wxColour colour
= attr
->GetBackgroundColour();
8972 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8974 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8975 wxColour colour
= attr
->GetTextColour();
8980 wxFont
wxGrid::GetCellFont( int row
, int col
)
8982 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8983 wxFont font
= attr
->GetFont();
8988 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8990 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8991 attr
->GetAlignment(horiz
, vert
);
8995 bool wxGrid::GetCellOverflow( int row
, int col
)
8997 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8998 bool allow
= attr
->GetOverflow();
9003 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9005 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9006 attr
->GetSize( num_rows
, num_cols
);
9010 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9012 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9013 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9019 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9021 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9022 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9028 bool wxGrid::IsReadOnly(int row
, int col
) const
9030 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9031 bool isReadOnly
= attr
->IsReadOnly();
9036 // ----------------------------------------------------------------------------
9037 // attribute support: cache, automatic provider creation, ...
9038 // ----------------------------------------------------------------------------
9040 bool wxGrid::CanHaveAttributes()
9047 return m_table
->CanHaveAttributes();
9050 void wxGrid::ClearAttrCache()
9052 if ( m_attrCache
.row
!= -1 )
9054 wxSafeDecRef(m_attrCache
.attr
);
9055 m_attrCache
.attr
= NULL
;
9056 m_attrCache
.row
= -1;
9060 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9064 wxGrid
*self
= (wxGrid
*)this; // const_cast
9066 self
->ClearAttrCache();
9067 self
->m_attrCache
.row
= row
;
9068 self
->m_attrCache
.col
= col
;
9069 self
->m_attrCache
.attr
= attr
;
9074 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9076 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9078 *attr
= m_attrCache
.attr
;
9079 wxSafeIncRef(m_attrCache
.attr
);
9081 #ifdef DEBUG_ATTR_CACHE
9082 gs_nAttrCacheHits
++;
9089 #ifdef DEBUG_ATTR_CACHE
9090 gs_nAttrCacheMisses
++;
9096 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9098 wxGridCellAttr
*attr
= NULL
;
9099 // Additional test to avoid looking at the cache e.g. for
9100 // wxNoCellCoords, as this will confuse memory management.
9103 if ( !LookupAttr(row
, col
, &attr
) )
9105 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9106 : (wxGridCellAttr
*)NULL
;
9107 CacheAttr(row
, col
, attr
);
9112 attr
->SetDefAttr(m_defaultCellAttr
);
9116 attr
= m_defaultCellAttr
;
9123 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9125 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9126 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9128 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9129 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9131 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9134 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9136 // artificially inc the ref count to match DecRef() in caller
9138 m_table
->SetAttr(attr
, row
, col
);
9144 // ----------------------------------------------------------------------------
9145 // setting column attributes (wrappers around SetColAttr)
9146 // ----------------------------------------------------------------------------
9148 void wxGrid::SetColFormatBool(int col
)
9150 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9153 void wxGrid::SetColFormatNumber(int col
)
9155 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9158 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9160 wxString typeName
= wxGRID_VALUE_FLOAT
;
9161 if ( (width
!= -1) || (precision
!= -1) )
9163 typeName
<< _T(':') << width
<< _T(',') << precision
;
9166 SetColFormatCustom(col
, typeName
);
9169 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9171 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9173 attr
= new wxGridCellAttr
;
9174 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9175 attr
->SetRenderer(renderer
);
9177 SetColAttr(col
, attr
);
9181 // ----------------------------------------------------------------------------
9182 // setting cell attributes: this is forwarded to the table
9183 // ----------------------------------------------------------------------------
9185 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9187 if ( CanHaveAttributes() )
9189 m_table
->SetAttr(attr
, row
, col
);
9198 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9200 if ( CanHaveAttributes() )
9202 m_table
->SetRowAttr(attr
, row
);
9211 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9213 if ( CanHaveAttributes() )
9215 m_table
->SetColAttr(attr
, col
);
9224 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9226 if ( CanHaveAttributes() )
9228 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9229 attr
->SetBackgroundColour(colour
);
9234 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9236 if ( CanHaveAttributes() )
9238 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9239 attr
->SetTextColour(colour
);
9244 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9246 if ( CanHaveAttributes() )
9248 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9249 attr
->SetFont(font
);
9254 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9256 if ( CanHaveAttributes() )
9258 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9259 attr
->SetAlignment(horiz
, vert
);
9264 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9266 if ( CanHaveAttributes() )
9268 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9269 attr
->SetOverflow(allow
);
9274 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9276 if ( CanHaveAttributes() )
9278 int cell_rows
, cell_cols
;
9280 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9281 attr
->GetSize(&cell_rows
, &cell_cols
);
9282 attr
->SetSize(num_rows
, num_cols
);
9285 // Cannot set the size of a cell to 0 or negative values
9286 // While it is perfectly legal to do that, this function cannot
9287 // handle all the possibilies, do it by hand by getting the CellAttr.
9288 // You can only set the size of a cell to 1,1 or greater with this fn
9289 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9290 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9291 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9292 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9294 // if this was already a multicell then "turn off" the other cells first
9295 if ((cell_rows
> 1) || (cell_rows
> 1))
9298 for (j
=row
; j
<row
+cell_rows
; j
++)
9300 for (i
=col
; i
<col
+cell_cols
; i
++)
9302 if ((i
!= col
) || (j
!= row
))
9304 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9305 attr_stub
->SetSize( 1, 1 );
9306 attr_stub
->DecRef();
9312 // mark the cells that will be covered by this cell to
9313 // negative or zero values to point back at this cell
9314 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9317 for (j
=row
; j
<row
+num_rows
; j
++)
9319 for (i
=col
; i
<col
+num_cols
; i
++)
9321 if ((i
!= col
) || (j
!= row
))
9323 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9324 attr_stub
->SetSize( row
-j
, col
-i
);
9325 attr_stub
->DecRef();
9333 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9335 if ( CanHaveAttributes() )
9337 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9338 attr
->SetRenderer(renderer
);
9343 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9345 if ( CanHaveAttributes() )
9347 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9348 attr
->SetEditor(editor
);
9353 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9355 if ( CanHaveAttributes() )
9357 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9358 attr
->SetReadOnly(isReadOnly
);
9363 // ----------------------------------------------------------------------------
9364 // Data type registration
9365 // ----------------------------------------------------------------------------
9367 void wxGrid::RegisterDataType(const wxString
& typeName
,
9368 wxGridCellRenderer
* renderer
,
9369 wxGridCellEditor
* editor
)
9371 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9375 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9377 wxString typeName
= m_table
->GetTypeName(row
, col
);
9378 return GetDefaultEditorForType(typeName
);
9381 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9383 wxString typeName
= m_table
->GetTypeName(row
, col
);
9384 return GetDefaultRendererForType(typeName
);
9388 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9390 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9391 if ( index
== wxNOT_FOUND
)
9393 wxFAIL_MSG(wxT("Unknown data type name"));
9398 return m_typeRegistry
->GetEditor(index
);
9402 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9404 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9405 if ( index
== wxNOT_FOUND
)
9407 wxFAIL_MSG(wxT("Unknown data type name"));
9412 return m_typeRegistry
->GetRenderer(index
);
9416 // ----------------------------------------------------------------------------
9418 // ----------------------------------------------------------------------------
9420 void wxGrid::EnableDragRowSize( bool enable
)
9422 m_canDragRowSize
= enable
;
9426 void wxGrid::EnableDragColSize( bool enable
)
9428 m_canDragColSize
= enable
;
9431 void wxGrid::EnableDragGridSize( bool enable
)
9433 m_canDragGridSize
= enable
;
9436 void wxGrid::EnableDragCell( bool enable
)
9438 m_canDragCell
= enable
;
9441 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9443 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9445 if ( resizeExistingRows
)
9447 // since we are resizing all rows to the default row size,
9448 // we can simply clear the row heights and row bottoms
9449 // arrays (which also allows us to take advantage of
9450 // some speed optimisations)
9451 m_rowHeights
.Empty();
9452 m_rowBottoms
.Empty();
9453 if ( !GetBatchCount() )
9458 void wxGrid::SetRowSize( int row
, int height
)
9460 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9462 // See comment in SetColSize
9463 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9465 if ( m_rowHeights
.IsEmpty() )
9467 // need to really create the array
9471 int h
= wxMax( 0, height
);
9472 int diff
= h
- m_rowHeights
[row
];
9474 m_rowHeights
[row
] = h
;
9476 for ( i
= row
; i
< m_numRows
; i
++ )
9478 m_rowBottoms
[i
] += diff
;
9480 if ( !GetBatchCount() )
9484 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9486 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9488 if ( resizeExistingCols
)
9490 // since we are resizing all columns to the default column size,
9491 // we can simply clear the col widths and col rights
9492 // arrays (which also allows us to take advantage of
9493 // some speed optimisations)
9494 m_colWidths
.Empty();
9495 m_colRights
.Empty();
9496 if ( !GetBatchCount() )
9501 void wxGrid::SetColSize( int col
, int width
)
9503 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9505 // should we check that it's bigger than GetColMinimalWidth(col) here?
9507 // No, because it is reasonable to assume the library user know's
9508 // what he is doing. However whe should test against the weaker
9509 // constariant of minimalAcceptableWidth, as this breaks rendering
9511 // This test then fixes sf.net bug #645734
9513 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9515 if ( m_colWidths
.IsEmpty() )
9517 // need to really create the array
9521 // if < 0 calc new width from label
9525 wxArrayString lines
;
9526 wxClientDC
dc(m_colLabelWin
);
9527 dc
.SetFont(GetLabelFont());
9528 StringToLines(GetColLabelValue(col
), lines
);
9529 GetTextBoxSize(dc
, lines
, &w
, &h
);
9532 int w
= wxMax( 0, width
);
9533 int diff
= w
- m_colWidths
[col
];
9534 m_colWidths
[col
] = w
;
9537 for ( i
= col
; i
< m_numCols
; i
++ )
9539 m_colRights
[i
] += diff
;
9541 if ( !GetBatchCount() )
9546 void wxGrid::SetColMinimalWidth( int col
, int width
)
9548 if (width
> GetColMinimalAcceptableWidth()) {
9549 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9550 m_colMinWidths
[key
] = width
;
9554 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9556 if (width
> GetRowMinimalAcceptableHeight()) {
9557 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9558 m_rowMinHeights
[key
] = width
;
9562 int wxGrid::GetColMinimalWidth(int col
) const
9564 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9565 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9566 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9569 int wxGrid::GetRowMinimalHeight(int row
) const
9571 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9572 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9573 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9576 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9578 // We do allow a width of 0 since this gives us
9579 // an easy way to temporarily hidding columns.
9582 m_minAcceptableColWidth
= width
;
9585 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9587 // We do allow a height of 0 since this gives us
9588 // an easy way to temporarily hidding rows.
9591 m_minAcceptableRowHeight
= height
;
9594 int wxGrid::GetColMinimalAcceptableWidth() const
9596 return m_minAcceptableColWidth
;
9599 int wxGrid::GetRowMinimalAcceptableHeight() const
9601 return m_minAcceptableRowHeight
;
9604 // ----------------------------------------------------------------------------
9606 // ----------------------------------------------------------------------------
9608 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9610 wxClientDC
dc(m_gridWin
);
9612 //Cancel editting of cell
9613 HideCellEditControl();
9614 SaveEditControlValue();
9616 // init both of them to avoid compiler warnings, even if weo nly need one
9624 wxCoord extent
, extentMax
= 0;
9625 int max
= column
? m_numRows
: m_numCols
;
9626 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9633 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9634 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9637 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9638 extent
= column
? size
.x
: size
.y
;
9639 if ( extent
> extentMax
)
9650 // now also compare with the column label extent
9652 dc
.SetFont( GetLabelFont() );
9656 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9657 if( GetColLabelTextOrientation() == wxVERTICAL
)
9661 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9663 extent
= column
? w
: h
;
9664 if ( extent
> extentMax
)
9671 // empty column - give default extent (notice that if extentMax is less
9672 // than default extent but != 0, it's ok)
9673 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9679 // leave some space around text
9690 SetColSize(col
, extentMax
);
9691 if ( !GetBatchCount() )
9694 m_gridWin
->GetClientSize( &cw
, &ch
);
9695 wxRect
rect ( CellToRect( 0, col
) );
9697 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9698 rect
.width
= cw
- rect
.x
;
9699 rect
.height
= m_colLabelHeight
;
9700 m_colLabelWin
->Refresh( true, &rect
);
9705 SetRowSize(row
, extentMax
);
9706 if ( !GetBatchCount() )
9709 m_gridWin
->GetClientSize( &cw
, &ch
);
9710 wxRect
rect ( CellToRect( row
, 0 ) );
9712 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9713 rect
.width
= m_rowLabelWidth
;
9714 rect
.height
= ch
- rect
.y
;
9715 m_rowLabelWin
->Refresh( true, &rect
);
9721 SetColMinimalWidth(col
, extentMax
);
9723 SetRowMinimalHeight(row
, extentMax
);
9727 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9729 int width
= m_rowLabelWidth
;
9734 for ( int col
= 0; col
< m_numCols
; col
++ )
9738 AutoSizeColumn(col
, setAsMin
);
9741 width
+= GetColWidth(col
);
9750 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9752 int height
= m_colLabelHeight
;
9757 for ( int row
= 0; row
< m_numRows
; row
++ )
9761 AutoSizeRow(row
, setAsMin
);
9764 height
+= GetRowHeight(row
);
9773 void wxGrid::AutoSize()
9777 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9779 // round up the size to a multiple of scroll step - this ensures that we
9780 // won't get the scrollbars if we're sized exactly to this width
9781 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9783 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9784 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9786 // distribute the extra space between the columns/rows to avoid having
9787 // extra white space
9789 // Remove the extra m_extraWidth + 1 added above
9790 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9791 if ( diff
&& m_numCols
)
9793 // try to resize the columns uniformly
9794 wxCoord diffPerCol
= diff
/ m_numCols
;
9797 for ( int col
= 0; col
< m_numCols
; col
++ )
9799 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9803 // add remaining amount to the last columns
9804 diff
-= diffPerCol
* m_numCols
;
9807 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9809 SetColSize(col
, GetColWidth(col
) + 1);
9815 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9816 if ( diff
&& m_numRows
)
9818 // try to resize the columns uniformly
9819 wxCoord diffPerRow
= diff
/ m_numRows
;
9822 for ( int row
= 0; row
< m_numRows
; row
++ )
9824 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9828 // add remaining amount to the last rows
9829 diff
-= diffPerRow
* m_numRows
;
9832 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9834 SetRowSize(row
, GetRowHeight(row
) + 1);
9841 SetClientSize(sizeFit
);
9844 void wxGrid::AutoSizeRowLabelSize( int row
)
9846 wxArrayString lines
;
9849 // Hide the edit control, so it
9850 // won't interfer with drag-shrinking.
9851 if( IsCellEditControlShown() )
9853 HideCellEditControl();
9854 SaveEditControlValue();
9857 // autosize row height depending on label text
9858 StringToLines( GetRowLabelValue( row
), lines
);
9859 wxClientDC
dc( m_rowLabelWin
);
9860 GetTextBoxSize( dc
, lines
, &w
, &h
);
9861 if( h
< m_defaultRowHeight
)
9862 h
= m_defaultRowHeight
;
9867 void wxGrid::AutoSizeColLabelSize( int col
)
9869 wxArrayString lines
;
9872 // Hide the edit control, so it
9873 // won't interfer with drag-shrinking.
9874 if( IsCellEditControlShown() )
9876 HideCellEditControl();
9877 SaveEditControlValue();
9880 // autosize column width depending on label text
9881 StringToLines( GetColLabelValue( col
), lines
);
9882 wxClientDC
dc( m_colLabelWin
);
9883 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9884 GetTextBoxSize( dc
, lines
, &w
, &h
);
9886 GetTextBoxSize( dc
, lines
, &h
, &w
);
9887 if( w
< m_defaultColWidth
)
9888 w
= m_defaultColWidth
;
9893 wxSize
wxGrid::DoGetBestSize() const
9895 // don't set sizes, only calculate them
9896 wxGrid
*self
= (wxGrid
*)this; // const_cast
9899 width
= self
->SetOrCalcColumnSizes(true);
9900 height
= self
->SetOrCalcRowSizes(true);
9902 if (!width
) width
=100;
9903 if (!height
) height
=80;
9905 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9906 // of the scrollbars, is there any magic incantaion for that?
9908 GetScrollPixelsPerUnit(&xpu
, &ypu
);
9910 width
+= 1 + xpu
- (width
% xpu
);
9912 height
+= 1 + ypu
- (height
% ypu
);
9914 // limit to 1/4 of the screen size
9915 int maxwidth
, maxheight
;
9916 wxDisplaySize( & maxwidth
, & maxheight
);
9919 if ( width
> maxwidth
) width
= maxwidth
;
9920 if ( height
> maxheight
) height
= maxheight
;
9923 wxSize
best(width
, height
);
9924 // NOTE: This size should be cached, but first we need to add calls to
9925 // InvalidateBestSize everywhere that could change the results of this
9927 // CacheBestSize(size);
9937 wxPen
& wxGrid::GetDividerPen() const
9942 // ----------------------------------------------------------------------------
9943 // cell value accessor functions
9944 // ----------------------------------------------------------------------------
9946 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9950 m_table
->SetValue( row
, col
, s
);
9951 if ( !GetBatchCount() )
9954 wxRect
rect( CellToRect( row
, col
) );
9956 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9957 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9958 m_gridWin
->Refresh( false, &rect
);
9961 if ( m_currentCellCoords
.GetRow() == row
&&
9962 m_currentCellCoords
.GetCol() == col
&&
9963 IsCellEditControlShown())
9964 // Note: If we are using IsCellEditControlEnabled,
9965 // this interacts badly with calling SetCellValue from
9966 // an EVT_GRID_CELL_CHANGE handler.
9968 HideCellEditControl();
9969 ShowCellEditControl(); // will reread data from table
9976 // ------ Block, row and col selection
9979 void wxGrid::SelectRow( int row
, bool addToSelected
)
9981 if ( IsSelection() && !addToSelected
)
9985 m_selection
->SelectRow( row
, false, addToSelected
);
9989 void wxGrid::SelectCol( int col
, bool addToSelected
)
9991 if ( IsSelection() && !addToSelected
)
9995 m_selection
->SelectCol( col
, false, addToSelected
);
9999 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10000 bool addToSelected
)
10002 if ( IsSelection() && !addToSelected
)
10006 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10007 false, addToSelected
);
10011 void wxGrid::SelectAll()
10013 if ( m_numRows
> 0 && m_numCols
> 0 )
10016 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10021 // ------ Cell, row and col deselection
10024 void wxGrid::DeselectRow( int row
)
10026 if ( !m_selection
)
10029 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10031 if ( m_selection
->IsInSelection(row
, 0 ) )
10032 m_selection
->ToggleCellSelection( row
, 0);
10036 int nCols
= GetNumberCols();
10037 for ( int i
= 0; i
< nCols
; i
++ )
10039 if ( m_selection
->IsInSelection(row
, i
) )
10040 m_selection
->ToggleCellSelection( row
, i
);
10045 void wxGrid::DeselectCol( int col
)
10047 if ( !m_selection
)
10050 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10052 if ( m_selection
->IsInSelection(0, col
) )
10053 m_selection
->ToggleCellSelection( 0, col
);
10057 int nRows
= GetNumberRows();
10058 for ( int i
= 0; i
< nRows
; i
++ )
10060 if ( m_selection
->IsInSelection(i
, col
) )
10061 m_selection
->ToggleCellSelection(i
, col
);
10066 void wxGrid::DeselectCell( int row
, int col
)
10068 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10069 m_selection
->ToggleCellSelection(row
, col
);
10072 bool wxGrid::IsSelection()
10074 return ( m_selection
&& (m_selection
->IsSelection() ||
10075 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10076 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10079 bool wxGrid::IsInSelection( int row
, int col
) const
10081 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10082 ( row
>= m_selectingTopLeft
.GetRow() &&
10083 col
>= m_selectingTopLeft
.GetCol() &&
10084 row
<= m_selectingBottomRight
.GetRow() &&
10085 col
<= m_selectingBottomRight
.GetCol() )) );
10088 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10090 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10091 return m_selection
->m_cellSelection
;
10093 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10095 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10096 return m_selection
->m_blockSelectionTopLeft
;
10098 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10100 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10101 return m_selection
->m_blockSelectionBottomRight
;
10103 wxArrayInt
wxGrid::GetSelectedRows() const
10105 if (!m_selection
) { wxArrayInt a
; return a
; }
10106 return m_selection
->m_rowSelection
;
10108 wxArrayInt
wxGrid::GetSelectedCols() const
10110 if (!m_selection
) { wxArrayInt a
; return a
; }
10111 return m_selection
->m_colSelection
;
10115 void wxGrid::ClearSelection()
10117 m_selectingTopLeft
= wxGridNoCellCoords
;
10118 m_selectingBottomRight
= wxGridNoCellCoords
;
10120 m_selection
->ClearSelection();
10124 // This function returns the rectangle that encloses the given block
10125 // in device coords clipped to the client size of the grid window.
10127 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10128 const wxGridCellCoords
&bottomRight
)
10130 wxRect
rect( wxGridNoCellRect
);
10133 cellRect
= CellToRect( topLeft
);
10134 if ( cellRect
!= wxGridNoCellRect
)
10140 rect
= wxRect( 0, 0, 0, 0 );
10143 cellRect
= CellToRect( bottomRight
);
10144 if ( cellRect
!= wxGridNoCellRect
)
10150 return wxGridNoCellRect
;
10154 int left
= rect
.GetLeft();
10155 int top
= rect
.GetTop();
10156 int right
= rect
.GetRight();
10157 int bottom
= rect
.GetBottom();
10159 int leftCol
= topLeft
.GetCol();
10160 int topRow
= topLeft
.GetRow();
10161 int rightCol
= bottomRight
.GetCol();
10162 int bottomRow
= bottomRight
.GetRow();
10180 topRow
= bottomRow
;
10185 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10187 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10189 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10191 cellRect
= CellToRect( j
, i
);
10193 if (cellRect
.x
< left
)
10195 if (cellRect
.y
< top
)
10197 if (cellRect
.x
+ cellRect
.width
> right
)
10198 right
= cellRect
.x
+ cellRect
.width
;
10199 if (cellRect
.y
+ cellRect
.height
> bottom
)
10200 bottom
= cellRect
.y
+ cellRect
.height
;
10202 else i
= rightCol
; // jump over inner cells.
10206 // convert to scrolled coords
10208 CalcScrolledPosition( left
, top
, &left
, &top
);
10209 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10212 m_gridWin
->GetClientSize( &cw
, &ch
);
10214 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10215 return wxRect( 0, 0, 0, 0);
10217 rect
.SetLeft( wxMax(0, left
) );
10218 rect
.SetTop( wxMax(0, top
) );
10219 rect
.SetRight( wxMin(cw
, right
) );
10220 rect
.SetBottom( wxMin(ch
, bottom
) );
10226 // ------ Grid event classes
10229 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10231 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10232 int row
, int col
, int x
, int y
, bool sel
,
10233 bool control
, bool shift
, bool alt
, bool meta
)
10234 : wxNotifyEvent( type
, id
)
10241 m_control
= control
;
10246 SetEventObject(obj
);
10250 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10252 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10253 int rowOrCol
, int x
, int y
,
10254 bool control
, bool shift
, bool alt
, bool meta
)
10255 : wxNotifyEvent( type
, id
)
10257 m_rowOrCol
= rowOrCol
;
10260 m_control
= control
;
10265 SetEventObject(obj
);
10269 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10271 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10272 const wxGridCellCoords
& topLeft
,
10273 const wxGridCellCoords
& bottomRight
,
10274 bool sel
, bool control
,
10275 bool shift
, bool alt
, bool meta
)
10276 : wxNotifyEvent( type
, id
)
10278 m_topLeft
= topLeft
;
10279 m_bottomRight
= bottomRight
;
10281 m_control
= control
;
10286 SetEventObject(obj
);
10290 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10292 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10293 wxObject
* obj
, int row
,
10294 int col
, wxControl
* ctrl
)
10295 : wxCommandEvent(type
, id
)
10297 SetEventObject(obj
);
10303 #endif // wxUSE_GRID