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"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
51 #include "wx/generic/gridsel.h"
53 #if defined(__WXMOTIF__)
54 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
56 #define WXUNUSED_MOTIF(identifier) identifier
59 #if defined(__WXGTK__)
60 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
62 #define WXUNUSED_GTK(identifier) identifier
65 // Required for wxIs... functions
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
73 class WXDLLIMPEXP_ADV
);
75 struct wxGridCellWithAttr
77 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
78 : coords(row
, col
), attr(attr_
)
87 wxGridCellCoords coords
;
91 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
92 // without rewriting the macros, which require a public copy constructor.
95 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
96 class WXDLLIMPEXP_ADV
);
98 #include "wx/arrimpl.cpp"
100 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
101 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
121 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
122 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
131 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
132 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
133 const wxPoint
&pos
, const wxSize
&size
);
138 void OnPaint( wxPaintEvent
& event
);
139 void OnMouseEvent( wxMouseEvent
& event
);
140 void OnMouseWheel( wxMouseEvent
& event
);
141 void OnKeyDown( wxKeyEvent
& event
);
142 void OnKeyUp( wxKeyEvent
& );
144 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
145 DECLARE_EVENT_TABLE()
146 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
150 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
153 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
154 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
155 const wxPoint
&pos
, const wxSize
&size
);
160 void OnPaint( wxPaintEvent
&event
);
161 void OnMouseEvent( wxMouseEvent
& event
);
162 void OnMouseWheel( wxMouseEvent
& event
);
163 void OnKeyDown( wxKeyEvent
& event
);
164 void OnKeyUp( wxKeyEvent
& );
166 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
167 DECLARE_EVENT_TABLE()
168 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
172 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
175 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
176 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
177 const wxPoint
&pos
, const wxSize
&size
);
182 void OnMouseEvent( wxMouseEvent
& event
);
183 void OnMouseWheel( wxMouseEvent
& event
);
184 void OnKeyDown( wxKeyEvent
& event
);
185 void OnKeyUp( wxKeyEvent
& );
186 void OnPaint( wxPaintEvent
& event
);
188 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
189 DECLARE_EVENT_TABLE()
190 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
193 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
198 m_owner
= (wxGrid
*)NULL
;
199 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
200 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
203 wxGridWindow( wxGrid
*parent
,
204 wxGridRowLabelWindow
*rowLblWin
,
205 wxGridColLabelWindow
*colLblWin
,
206 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
209 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
211 wxGrid
* GetOwner() { return m_owner
; }
215 wxGridRowLabelWindow
*m_rowLabelWin
;
216 wxGridColLabelWindow
*m_colLabelWin
;
218 void OnPaint( wxPaintEvent
&event
);
219 void OnMouseWheel( wxMouseEvent
& event
);
220 void OnMouseEvent( wxMouseEvent
& event
);
221 void OnKeyDown( wxKeyEvent
& );
222 void OnKeyUp( wxKeyEvent
& );
223 void OnEraseBackground( wxEraseEvent
& );
224 void OnFocus( wxFocusEvent
& );
226 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
227 DECLARE_EVENT_TABLE()
228 DECLARE_NO_COPY_CLASS(wxGridWindow
)
233 class wxGridCellEditorEvtHandler
: public wxEvtHandler
236 wxGridCellEditorEvtHandler()
237 : m_grid(0), m_editor(0)
239 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
240 : m_grid(grid
), m_editor(editor
)
243 void OnKeyDown(wxKeyEvent
& event
);
244 void OnChar(wxKeyEvent
& event
);
248 wxGridCellEditor
* m_editor
;
249 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
250 DECLARE_EVENT_TABLE()
251 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
255 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
256 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
257 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
258 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
263 // ----------------------------------------------------------------------------
264 // the internal data representation used by wxGridCellAttrProvider
265 // ----------------------------------------------------------------------------
267 // this class stores attributes set for cells
268 class WXDLLIMPEXP_ADV wxGridCellAttrData
271 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
272 wxGridCellAttr
*GetAttr(int row
, int col
) const;
273 void UpdateAttrRows( size_t pos
, int numRows
);
274 void UpdateAttrCols( size_t pos
, int numCols
);
277 // searches for the attr for given cell, returns wxNOT_FOUND if not found
278 int FindIndex(int row
, int col
) const;
280 wxGridCellWithAttrArray m_attrs
;
283 // this class stores attributes set for rows or columns
284 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
287 // empty ctor to suppress warnings
288 wxGridRowOrColAttrData() { }
289 ~wxGridRowOrColAttrData();
291 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
292 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
293 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
296 wxArrayInt m_rowsOrCols
;
297 wxArrayAttrs m_attrs
;
300 // NB: this is just a wrapper around 3 objects: one which stores cell
301 // attributes, and 2 others for row/col ones
302 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
305 wxGridCellAttrData m_cellAttrs
;
306 wxGridRowOrColAttrData m_rowAttrs
,
311 // ----------------------------------------------------------------------------
312 // data structures used for the data type registry
313 // ----------------------------------------------------------------------------
315 struct wxGridDataTypeInfo
317 wxGridDataTypeInfo(const wxString
& typeName
,
318 wxGridCellRenderer
* renderer
,
319 wxGridCellEditor
* editor
)
320 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
323 ~wxGridDataTypeInfo()
325 wxSafeDecRef(m_renderer
);
326 wxSafeDecRef(m_editor
);
330 wxGridCellRenderer
* m_renderer
;
331 wxGridCellEditor
* m_editor
;
333 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
337 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
338 class WXDLLIMPEXP_ADV
);
341 class WXDLLIMPEXP_ADV wxGridTypeRegistry
344 wxGridTypeRegistry() {}
345 ~wxGridTypeRegistry();
347 void RegisterDataType(const wxString
& typeName
,
348 wxGridCellRenderer
* renderer
,
349 wxGridCellEditor
* editor
);
351 // find one of already registered data types
352 int FindRegisteredDataType(const wxString
& typeName
);
354 // try to FindRegisteredDataType(), if this fails and typeName is one of
355 // standard typenames, register it and return its index
356 int FindDataType(const wxString
& typeName
);
358 // try to FindDataType(), if it fails see if it is not one of already
359 // registered data types with some params in which case clone the
360 // registered data type and set params for it
361 int FindOrCloneDataType(const wxString
& typeName
);
363 wxGridCellRenderer
* GetRenderer(int index
);
364 wxGridCellEditor
* GetEditor(int index
);
367 wxGridDataTypeInfoArray m_typeinfo
;
370 // ----------------------------------------------------------------------------
371 // conditional compilation
372 // ----------------------------------------------------------------------------
374 #ifndef WXGRID_DRAW_LINES
375 #define WXGRID_DRAW_LINES 1
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 //#define DEBUG_ATTR_CACHE
383 #ifdef DEBUG_ATTR_CACHE
384 static size_t gs_nAttrCacheHits
= 0;
385 static size_t gs_nAttrCacheMisses
= 0;
386 #endif // DEBUG_ATTR_CACHE
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
393 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
396 // TODO: this doesn't work at all, grid cells have different sizes and approx
397 // calculations don't work as because of the size mismatch scrollbars
398 // sometimes fail to be shown when they should be or vice versa
400 // The scroll bars may be a little flakey once in a while, but that is
401 // surely much less horrible than having scroll lines of only 1!!!
404 // Well, it's still seriously broken so it might be better but needs
407 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
408 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
410 // the size of hash tables used a bit everywhere (the max number of elements
411 // in these hash tables is the number of rows/columns)
412 static const int GRID_HASH_SIZE
= 100;
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
418 static inline int GetScrollX(int x
)
420 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
423 static inline int GetScrollY(int y
)
425 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
428 // ============================================================================
430 // ============================================================================
432 // ----------------------------------------------------------------------------
434 // ----------------------------------------------------------------------------
436 wxGridCellEditor::wxGridCellEditor()
443 wxGridCellEditor::~wxGridCellEditor()
448 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
449 wxWindowID
WXUNUSED(id
),
450 wxEvtHandler
* evtHandler
)
453 m_control
->PushEventHandler(evtHandler
);
456 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
457 wxGridCellAttr
*attr
)
459 // erase the background because we might not fill the cell
460 wxClientDC
dc(m_control
->GetParent());
461 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
463 gridWindow
->GetOwner()->PrepareDC(dc
);
465 dc
.SetPen(*wxTRANSPARENT_PEN
);
466 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
467 dc
.DrawRectangle(rectCell
);
469 // redraw the control we just painted over
470 m_control
->Refresh();
473 void wxGridCellEditor::Destroy()
477 m_control
->PopEventHandler(TRUE
/* delete it*/);
479 m_control
->Destroy();
484 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
486 wxASSERT_MSG(m_control
,
487 wxT("The wxGridCellEditor must be Created first!"));
488 m_control
->Show(show
);
492 // set the colours/fonts if we have any
495 m_colFgOld
= m_control
->GetForegroundColour();
496 m_control
->SetForegroundColour(attr
->GetTextColour());
498 m_colBgOld
= m_control
->GetBackgroundColour();
499 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
501 m_fontOld
= m_control
->GetFont();
502 m_control
->SetFont(attr
->GetFont());
504 // can't do anything more in the base class version, the other
505 // attributes may only be used by the derived classes
510 // restore the standard colours fonts
511 if ( m_colFgOld
.Ok() )
513 m_control
->SetForegroundColour(m_colFgOld
);
514 m_colFgOld
= wxNullColour
;
517 if ( m_colBgOld
.Ok() )
519 m_control
->SetBackgroundColour(m_colBgOld
);
520 m_colBgOld
= wxNullColour
;
523 if ( m_fontOld
.Ok() )
525 m_control
->SetFont(m_fontOld
);
526 m_fontOld
= wxNullFont
;
531 void wxGridCellEditor::SetSize(const wxRect
& rect
)
533 wxASSERT_MSG(m_control
,
534 wxT("The wxGridCellEditor must be Created first!"));
535 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
538 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
543 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
545 // accept the simple key presses, not anything with Ctrl/Alt/Meta
546 return !(event
.ControlDown() || event
.AltDown());
549 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
554 void wxGridCellEditor::StartingClick()
560 // ----------------------------------------------------------------------------
561 // wxGridCellTextEditor
562 // ----------------------------------------------------------------------------
564 wxGridCellTextEditor::wxGridCellTextEditor()
569 void wxGridCellTextEditor::Create(wxWindow
* parent
,
571 wxEvtHandler
* evtHandler
)
573 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
574 wxDefaultPosition
, wxDefaultSize
575 #if defined(__WXMSW__)
576 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
580 // set max length allowed in the textctrl, if the parameter was set
583 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
586 wxGridCellEditor::Create(parent
, id
, evtHandler
);
589 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
590 wxGridCellAttr
* WXUNUSED(attr
))
592 // as we fill the entire client area, don't do anything here to minimize
596 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
598 wxRect
rect(rectOrig
);
600 // Make the edit control large enough to allow for internal
603 // TODO: remove this if the text ctrl sizing is improved esp. for
606 #if defined(__WXGTK__)
615 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
617 // MB: treat MSW separately here otherwise the caret doesn't show
618 // when the editor is in the first row.
619 #if defined(__WXMSW__)
622 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
625 #if defined(__WXMOTIF__)
629 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
630 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
631 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
632 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
635 wxGridCellEditor::SetSize(rect
);
638 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
640 wxASSERT_MSG(m_control
,
641 wxT("The wxGridCellEditor must be Created first!"));
643 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
645 DoBeginEdit(m_startValue
);
648 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
650 Text()->SetValue(startValue
);
651 Text()->SetInsertionPointEnd();
652 Text()->SetSelection(-1,-1);
656 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
659 wxASSERT_MSG(m_control
,
660 wxT("The wxGridCellEditor must be Created first!"));
662 bool changed
= FALSE
;
663 wxString value
= Text()->GetValue();
664 if (value
!= m_startValue
)
668 grid
->GetTable()->SetValue(row
, col
, value
);
670 m_startValue
= wxEmptyString
;
671 // No point in setting the text of the hidden control
672 //Text()->SetValue(m_startValue);
678 void wxGridCellTextEditor::Reset()
680 wxASSERT_MSG(m_control
,
681 wxT("The wxGridCellEditor must be Created first!"));
683 DoReset(m_startValue
);
686 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
688 Text()->SetValue(startValue
);
689 Text()->SetInsertionPointEnd();
692 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
694 if ( wxGridCellEditor::IsAcceptedKey(event
) )
696 int keycode
= event
.GetKeyCode();
710 case WXK_NUMPAD_MULTIPLY
:
714 case WXK_NUMPAD_SUBTRACT
:
716 case WXK_NUMPAD_DECIMAL
:
718 case WXK_NUMPAD_DIVIDE
:
722 // accept 8 bit chars too if isprint() agrees
723 if ( (keycode
< 255) && (wxIsprint(keycode
)) )
731 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
733 if ( !Text()->EmulateKeyPress(event
) )
739 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
740 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
742 #if defined(__WXMOTIF__) || defined(__WXGTK__)
743 // wxMotif needs a little extra help...
744 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
745 wxString
s( Text()->GetValue() );
746 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
748 Text()->SetInsertionPoint( pos
);
750 // the other ports can handle a Return key press
756 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
766 if ( !params
.ToLong(&tmp
) )
768 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
772 m_maxChars
= (size_t)tmp
;
777 // return the value in the text control
778 wxString
wxGridCellTextEditor::GetValue() const
780 return Text()->GetValue();
783 // ----------------------------------------------------------------------------
784 // wxGridCellNumberEditor
785 // ----------------------------------------------------------------------------
787 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
793 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
795 wxEvtHandler
* evtHandler
)
799 // create a spin ctrl
800 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
801 wxDefaultPosition
, wxDefaultSize
,
805 wxGridCellEditor::Create(parent
, id
, evtHandler
);
809 // just a text control
810 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
813 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
814 #endif // wxUSE_VALIDATORS
818 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
820 // first get the value
821 wxGridTableBase
*table
= grid
->GetTable();
822 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
824 m_valueOld
= table
->GetValueAsLong(row
, col
);
829 wxString sValue
= table
->GetValue(row
, col
);
830 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
832 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
839 Spin()->SetValue((int)m_valueOld
);
844 DoBeginEdit(GetString());
848 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
857 value
= Spin()->GetValue();
858 changed
= value
!= m_valueOld
;
860 text
= wxString::Format(wxT("%ld"), value
);
864 text
= Text()->GetValue();
865 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
870 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
871 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
873 grid
->GetTable()->SetValue(row
, col
, text
);
879 void wxGridCellNumberEditor::Reset()
883 Spin()->SetValue((int)m_valueOld
);
887 DoReset(GetString());
891 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
893 if ( wxGridCellEditor::IsAcceptedKey(event
) )
895 int keycode
= event
.GetKeyCode();
911 case WXK_NUMPAD_SUBTRACT
:
917 if ( (keycode
< 128) && wxIsdigit(keycode
) )
925 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
929 int keycode
= event
.GetKeyCode();
930 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
931 || keycode
== WXK_NUMPAD0
932 || keycode
== WXK_NUMPAD1
933 || keycode
== WXK_NUMPAD2
934 || keycode
== WXK_NUMPAD3
935 || keycode
== WXK_NUMPAD4
936 || keycode
== WXK_NUMPAD5
937 || keycode
== WXK_NUMPAD6
938 || keycode
== WXK_NUMPAD7
939 || keycode
== WXK_NUMPAD8
940 || keycode
== WXK_NUMPAD9
941 || keycode
== WXK_ADD
942 || keycode
== WXK_NUMPAD_ADD
943 || keycode
== WXK_SUBTRACT
944 || keycode
== WXK_NUMPAD_SUBTRACT
)
946 wxGridCellTextEditor::StartingKey(event
);
956 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
967 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
971 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
975 // skip the error message below
980 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
984 // return the value in the spin control if it is there (the text control otherwise)
985 wxString
wxGridCellNumberEditor::GetValue() const
991 long value
= Spin()->GetValue();
992 s
.Printf(wxT("%ld"), value
);
996 s
= Text()->GetValue();
1001 // ----------------------------------------------------------------------------
1002 // wxGridCellFloatEditor
1003 // ----------------------------------------------------------------------------
1005 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1008 m_precision
= precision
;
1011 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1013 wxEvtHandler
* evtHandler
)
1015 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1017 #if wxUSE_VALIDATORS
1018 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1019 #endif // wxUSE_VALIDATORS
1022 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1024 // first get the value
1025 wxGridTableBase
*table
= grid
->GetTable();
1026 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1028 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1033 wxString sValue
= table
->GetValue(row
, col
);
1034 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1036 wxFAIL_MSG( _T("this cell doesn't have float value") );
1041 DoBeginEdit(GetString());
1044 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1048 wxString
text(Text()->GetValue());
1050 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1052 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1053 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1055 grid
->GetTable()->SetValue(row
, col
, text
);
1062 void wxGridCellFloatEditor::Reset()
1064 DoReset(GetString());
1067 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1069 int keycode
= event
.GetKeyCode();
1070 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1071 || keycode
== WXK_NUMPAD0
1072 || keycode
== WXK_NUMPAD1
1073 || keycode
== WXK_NUMPAD2
1074 || keycode
== WXK_NUMPAD3
1075 || keycode
== WXK_NUMPAD4
1076 || keycode
== WXK_NUMPAD5
1077 || keycode
== WXK_NUMPAD6
1078 || keycode
== WXK_NUMPAD7
1079 || keycode
== WXK_NUMPAD8
1080 || keycode
== WXK_NUMPAD9
1081 || keycode
== WXK_ADD
1082 || keycode
== WXK_NUMPAD_ADD
1083 || keycode
== WXK_SUBTRACT
1084 || keycode
== WXK_NUMPAD_SUBTRACT
)
1086 wxGridCellTextEditor::StartingKey(event
);
1088 // skip Skip() below
1095 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1106 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1110 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1112 m_precision
= (int)tmp
;
1114 // skip the error message below
1119 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1123 wxString
wxGridCellFloatEditor::GetString() const
1126 if ( m_width
== -1 )
1128 // default width/precision
1131 else if ( m_precision
== -1 )
1133 // default precision
1134 fmt
.Printf(_T("%%%d.f"), m_width
);
1138 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1141 return wxString::Format(fmt
, m_valueOld
);
1144 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1146 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1148 int keycode
= event
.GetKeyCode();
1162 case WXK_NUMPAD_ADD
:
1164 case WXK_NUMPAD_SUBTRACT
:
1166 case WXK_NUMPAD_DECIMAL
:
1170 // additionally accept 'e' as in '1e+6'
1171 if ( (keycode
< 128) &&
1172 (wxIsdigit(keycode
) || tolower(keycode
) == 'e') )
1180 #endif // wxUSE_TEXTCTRL
1184 // ----------------------------------------------------------------------------
1185 // wxGridCellBoolEditor
1186 // ----------------------------------------------------------------------------
1188 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1190 wxEvtHandler
* evtHandler
)
1192 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1193 wxDefaultPosition
, wxDefaultSize
,
1196 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1199 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1201 bool resize
= FALSE
;
1202 wxSize size
= m_control
->GetSize();
1203 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1205 // check if the checkbox is not too big/small for this cell
1206 wxSize sizeBest
= m_control
->GetBestSize();
1207 if ( !(size
== sizeBest
) )
1209 // reset to default size if it had been made smaller
1215 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1217 // leave 1 pixel margin
1218 size
.x
= size
.y
= minSize
- 2;
1225 m_control
->SetSize(size
);
1228 // position it in the centre of the rectangle (TODO: support alignment?)
1230 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1231 // the checkbox without label still has some space to the right in wxGTK,
1232 // so shift it to the right
1234 #elif defined(__WXMSW__)
1235 // here too, but in other way
1240 int hAlign
= wxALIGN_CENTRE
;
1241 int vAlign
= wxALIGN_CENTRE
;
1243 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1246 if (hAlign
== wxALIGN_LEFT
)
1252 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1254 else if (hAlign
== wxALIGN_RIGHT
)
1256 x
= r
.x
+ r
.width
- size
.x
- 2;
1257 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1259 else if (hAlign
== wxALIGN_CENTRE
)
1261 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1262 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1265 m_control
->Move(x
, y
);
1268 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1270 m_control
->Show(show
);
1274 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1275 CBox()->SetBackgroundColour(colBg
);
1279 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1281 wxASSERT_MSG(m_control
,
1282 wxT("The wxGridCellEditor must be Created first!"));
1284 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1285 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1288 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1289 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1291 CBox()->SetValue(m_startValue
);
1295 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1298 wxASSERT_MSG(m_control
,
1299 wxT("The wxGridCellEditor must be Created first!"));
1301 bool changed
= FALSE
;
1302 bool value
= CBox()->GetValue();
1303 if ( value
!= m_startValue
)
1308 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1309 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1311 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1317 void wxGridCellBoolEditor::Reset()
1319 wxASSERT_MSG(m_control
,
1320 wxT("The wxGridCellEditor must be Created first!"));
1322 CBox()->SetValue(m_startValue
);
1325 void wxGridCellBoolEditor::StartingClick()
1327 CBox()->SetValue(!CBox()->GetValue());
1330 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1332 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1334 int keycode
= event
.GetKeyCode();
1338 case WXK_NUMPAD_MULTIPLY
:
1340 case WXK_NUMPAD_ADD
:
1342 case WXK_NUMPAD_SUBTRACT
:
1353 // return the value as "1" for true and the empty string for false
1354 wxString
wxGridCellBoolEditor::GetValue() const
1356 bool bSet
= CBox()->GetValue();
1357 return bSet
? _T("1") : wxEmptyString
;
1360 #endif // wxUSE_CHECKBOX
1364 // ----------------------------------------------------------------------------
1365 // wxGridCellChoiceEditor
1366 // ----------------------------------------------------------------------------
1368 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1370 : m_choices(choices
),
1371 m_allowOthers(allowOthers
) { }
1373 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1374 const wxString choices
[],
1376 : m_allowOthers(allowOthers
)
1380 m_choices
.Alloc(count
);
1381 for ( size_t n
= 0; n
< count
; n
++ )
1383 m_choices
.Add(choices
[n
]);
1388 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1390 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1391 editor
->m_allowOthers
= m_allowOthers
;
1392 editor
->m_choices
= m_choices
;
1397 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1399 wxEvtHandler
* evtHandler
)
1401 size_t count
= m_choices
.GetCount();
1402 wxString
*choices
= new wxString
[count
];
1403 for ( size_t n
= 0; n
< count
; n
++ )
1405 choices
[n
] = m_choices
[n
];
1408 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1409 wxDefaultPosition
, wxDefaultSize
,
1411 m_allowOthers
? 0 : wxCB_READONLY
);
1415 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1418 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1419 wxGridCellAttr
* attr
)
1421 // as we fill the entire client area, don't do anything here to minimize
1424 // TODO: It doesn't actually fill the client area since the height of a
1425 // combo always defaults to the standard... Until someone has time to
1426 // figure out the right rectangle to paint, just do it the normal way...
1427 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1430 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1432 wxASSERT_MSG(m_control
,
1433 wxT("The wxGridCellEditor must be Created first!"));
1435 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1438 Combo()->SetValue(m_startValue
);
1441 // find the right position, or default to the first if not found
1442 int pos
= Combo()->FindString(m_startValue
);
1445 Combo()->SetSelection(pos
);
1447 Combo()->SetInsertionPointEnd();
1448 Combo()->SetFocus();
1451 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1454 wxString value
= Combo()->GetValue();
1455 bool changed
= value
!= m_startValue
;
1458 grid
->GetTable()->SetValue(row
, col
, value
);
1460 m_startValue
= wxEmptyString
;
1462 Combo()->SetValue(m_startValue
);
1464 Combo()->SetSelection(0);
1469 void wxGridCellChoiceEditor::Reset()
1471 Combo()->SetValue(m_startValue
);
1472 Combo()->SetInsertionPointEnd();
1475 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1485 wxStringTokenizer
tk(params
, _T(','));
1486 while ( tk
.HasMoreTokens() )
1488 m_choices
.Add(tk
.GetNextToken());
1492 // return the value in the text control
1493 wxString
wxGridCellChoiceEditor::GetValue() const
1495 return Combo()->GetValue();
1498 #endif // wxUSE_COMBOBOX
1500 // ----------------------------------------------------------------------------
1501 // wxGridCellEditorEvtHandler
1502 // ----------------------------------------------------------------------------
1504 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1506 switch ( event
.GetKeyCode() )
1510 m_grid
->DisableCellEditControl();
1514 m_grid
->GetEventHandler()->ProcessEvent( event
);
1518 case WXK_NUMPAD_ENTER
:
1519 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1520 m_editor
->HandleReturn(event
);
1529 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1531 switch ( event
.GetKeyCode() )
1536 case WXK_NUMPAD_ENTER
:
1544 // ----------------------------------------------------------------------------
1545 // wxGridCellWorker is an (almost) empty common base class for
1546 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1547 // ----------------------------------------------------------------------------
1549 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1554 wxGridCellWorker::~wxGridCellWorker()
1558 // ============================================================================
1560 // ============================================================================
1562 // ----------------------------------------------------------------------------
1563 // wxGridCellRenderer
1564 // ----------------------------------------------------------------------------
1566 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1567 wxGridCellAttr
& attr
,
1570 int WXUNUSED(row
), int WXUNUSED(col
),
1573 dc
.SetBackgroundMode( wxSOLID
);
1575 // grey out fields if the grid is disabled
1576 if( grid
.IsEnabled() )
1580 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1584 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1589 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1592 dc
.SetPen( *wxTRANSPARENT_PEN
);
1593 dc
.DrawRectangle(rect
);
1596 // ----------------------------------------------------------------------------
1597 // wxGridCellStringRenderer
1598 // ----------------------------------------------------------------------------
1600 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1601 wxGridCellAttr
& attr
,
1605 dc
.SetBackgroundMode( wxTRANSPARENT
);
1607 // TODO some special colours for attr.IsReadOnly() case?
1609 // different coloured text when the grid is disabled
1610 if( grid
.IsEnabled() )
1614 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1615 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1619 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1620 dc
.SetTextForeground( attr
.GetTextColour() );
1625 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1626 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1629 dc
.SetFont( attr
.GetFont() );
1632 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1634 const wxString
& text
)
1636 wxCoord x
= 0, y
= 0, max_x
= 0;
1637 dc
.SetFont(attr
.GetFont());
1638 wxStringTokenizer
tk(text
, _T('\n'));
1639 while ( tk
.HasMoreTokens() )
1641 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1642 max_x
= wxMax(max_x
, x
);
1645 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1647 return wxSize(max_x
, y
);
1650 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1651 wxGridCellAttr
& attr
,
1655 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1658 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1659 wxGridCellAttr
& attr
,
1661 const wxRect
& rectCell
,
1665 wxRect rect
= rectCell
;
1668 // erase only this cells background, overflow cells should have been erased
1669 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1672 attr
.GetAlignment(&hAlign
, &vAlign
);
1674 int overflowCols
= 0;
1676 if (attr
.GetOverflow())
1678 int cols
= grid
.GetNumberCols();
1679 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1680 int cell_rows
, cell_cols
;
1681 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1682 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1684 int i
, c_cols
, c_rows
;
1685 for (i
= col
+cell_cols
; i
< cols
; i
++)
1687 bool is_empty
= TRUE
;
1688 for (int j
=row
; j
<row
+cell_rows
; j
++)
1690 // check w/ anchor cell for multicell block
1691 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1692 if (c_rows
> 0) c_rows
= 0;
1693 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1700 rect
.width
+= grid
.GetColSize(i
);
1706 if (rect
.width
>= best_width
) break;
1708 overflowCols
= i
- col
- cell_cols
+ 1;
1709 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1712 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1714 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1716 clip
.x
+= rectCell
.width
;
1717 // draw each overflow cell individually
1718 int col_end
= col
+cell_cols
+overflowCols
;
1719 if (col_end
>= grid
.GetNumberCols())
1720 col_end
= grid
.GetNumberCols() - 1;
1721 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1723 clip
.width
= grid
.GetColSize(i
) - 1;
1724 dc
.DestroyClippingRegion();
1725 dc
.SetClippingRegion(clip
);
1727 SetTextColoursAndFont(grid
, attr
, dc
,
1728 grid
.IsInSelection(row
,i
));
1730 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1731 rect
, hAlign
, vAlign
);
1732 clip
.x
+= grid
.GetColSize(i
) - 1;
1738 dc
.DestroyClippingRegion();
1742 // now we only have to draw the text
1743 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1745 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1746 rect
, hAlign
, vAlign
);
1749 // ----------------------------------------------------------------------------
1750 // wxGridCellNumberRenderer
1751 // ----------------------------------------------------------------------------
1753 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1755 wxGridTableBase
*table
= grid
.GetTable();
1757 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1759 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1763 text
= table
->GetValue(row
, col
);
1769 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1770 wxGridCellAttr
& attr
,
1772 const wxRect
& rectCell
,
1776 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1778 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1780 // draw the text right aligned by default
1782 attr
.GetAlignment(&hAlign
, &vAlign
);
1783 hAlign
= wxALIGN_RIGHT
;
1785 wxRect rect
= rectCell
;
1788 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1791 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1792 wxGridCellAttr
& attr
,
1796 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1799 // ----------------------------------------------------------------------------
1800 // wxGridCellFloatRenderer
1801 // ----------------------------------------------------------------------------
1803 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1806 SetPrecision(precision
);
1809 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1811 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1812 renderer
->m_width
= m_width
;
1813 renderer
->m_precision
= m_precision
;
1814 renderer
->m_format
= m_format
;
1819 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1821 wxGridTableBase
*table
= grid
.GetTable();
1826 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1828 val
= table
->GetValueAsDouble(row
, col
);
1833 text
= table
->GetValue(row
, col
);
1834 hasDouble
= text
.ToDouble(&val
);
1841 if ( m_width
== -1 )
1843 if ( m_precision
== -1 )
1845 // default width/precision
1846 m_format
= _T("%f");
1850 m_format
.Printf(_T("%%.%df"), m_precision
);
1853 else if ( m_precision
== -1 )
1855 // default precision
1856 m_format
.Printf(_T("%%%d.f"), m_width
);
1860 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1864 text
.Printf(m_format
, val
);
1867 //else: text already contains the string
1872 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1873 wxGridCellAttr
& attr
,
1875 const wxRect
& rectCell
,
1879 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1881 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1883 // draw the text right aligned by default
1885 attr
.GetAlignment(&hAlign
, &vAlign
);
1886 hAlign
= wxALIGN_RIGHT
;
1888 wxRect rect
= rectCell
;
1891 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1894 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1895 wxGridCellAttr
& attr
,
1899 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1902 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1906 // reset to defaults
1912 wxString tmp
= params
.BeforeFirst(_T(','));
1916 if ( tmp
.ToLong(&width
) )
1918 SetWidth((int)width
);
1922 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1926 tmp
= params
.AfterFirst(_T(','));
1930 if ( tmp
.ToLong(&precision
) )
1932 SetPrecision((int)precision
);
1936 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1944 // ----------------------------------------------------------------------------
1945 // wxGridCellBoolRenderer
1946 // ----------------------------------------------------------------------------
1948 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1950 // FIXME these checkbox size calculations are really ugly...
1952 // between checkmark and box
1953 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1955 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1956 wxGridCellAttr
& WXUNUSED(attr
),
1961 // compute it only once (no locks for MT safeness in GUI thread...)
1962 if ( !ms_sizeCheckMark
.x
)
1964 // get checkbox size
1965 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1966 wxSize size
= checkbox
->GetBestSize();
1967 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1969 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1970 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1971 checkSize
-= size
.y
/ 2;
1976 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1979 return ms_sizeCheckMark
;
1982 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1983 wxGridCellAttr
& attr
,
1989 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1991 // draw a check mark in the centre (ignoring alignment - TODO)
1992 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1994 // don't draw outside the cell
1995 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1996 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1998 // and even leave (at least) 1 pixel margin
1999 size
.x
= size
.y
= minSize
- 2;
2002 // draw a border around checkmark
2004 attr
.GetAlignment(& hAlign
, &vAlign
);
2007 if (hAlign
== wxALIGN_CENTRE
)
2009 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2010 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2011 rectBorder
.width
= size
.x
;
2012 rectBorder
.height
= size
.y
;
2014 else if (hAlign
== wxALIGN_LEFT
)
2016 rectBorder
.x
= rect
.x
+ 2;
2017 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2018 rectBorder
.width
= size
.x
;
2019 rectBorder
.height
= size
.y
;
2021 else if (hAlign
== wxALIGN_RIGHT
)
2023 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2024 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2025 rectBorder
.width
= size
.x
;
2026 rectBorder
.height
= size
.y
;
2030 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2031 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2034 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2035 value
= !( !cellval
|| (cellval
== wxT("0")) );
2040 wxRect rectMark
= rectBorder
;
2042 // MSW DrawCheckMark() is weird (and should probably be changed...)
2043 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2047 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2050 dc
.SetTextForeground(attr
.GetTextColour());
2051 dc
.DrawCheckMark(rectMark
);
2054 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2055 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2056 dc
.DrawRectangle(rectBorder
);
2059 // ----------------------------------------------------------------------------
2061 // ----------------------------------------------------------------------------
2063 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2067 m_isReadOnly
= Unset
;
2072 m_attrkind
= wxGridCellAttr::Cell
;
2074 m_sizeRows
= m_sizeCols
= 1;
2075 m_overflow
= UnsetOverflow
;
2077 SetDefAttr(attrDefault
);
2080 wxGridCellAttr
*wxGridCellAttr::Clone() const
2082 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2084 if ( HasTextColour() )
2085 attr
->SetTextColour(GetTextColour());
2086 if ( HasBackgroundColour() )
2087 attr
->SetBackgroundColour(GetBackgroundColour());
2089 attr
->SetFont(GetFont());
2090 if ( HasAlignment() )
2091 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2093 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2097 attr
->SetRenderer(m_renderer
);
2098 m_renderer
->IncRef();
2102 attr
->SetEditor(m_editor
);
2107 attr
->SetReadOnly();
2109 attr
->SetKind( m_attrkind
);
2114 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2116 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2117 SetTextColour(mergefrom
->GetTextColour());
2118 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2119 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2120 if ( !HasFont() && mergefrom
->HasFont() )
2121 SetFont(mergefrom
->GetFont());
2122 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2124 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2125 SetAlignment(hAlign
, vAlign
);
2128 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2130 // Directly access member functions as GetRender/Editor don't just return
2131 // m_renderer/m_editor
2133 // Maybe add support for merge of Render and Editor?
2134 if (!HasRenderer() && mergefrom
->HasRenderer() )
2136 m_renderer
= mergefrom
->m_renderer
;
2137 m_renderer
->IncRef();
2139 if ( !HasEditor() && mergefrom
->HasEditor() )
2141 m_editor
= mergefrom
->m_editor
;
2144 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2145 SetReadOnly(mergefrom
->IsReadOnly());
2147 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2148 SetOverflow(mergefrom
->GetOverflow());
2150 SetDefAttr(mergefrom
->m_defGridAttr
);
2153 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2155 // The size of a cell is normally 1,1
2157 // If this cell is larger (2,2) then this is the top left cell
2158 // the other cells that will be covered (lower right cells) must be
2159 // set to negative or zero values such that
2160 // row + num_rows of the covered cell points to the larger cell (this cell)
2161 // same goes for the col + num_cols.
2163 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2165 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2166 !((num_rows
<=0)&&(num_cols
>0)) ||
2167 !((num_rows
==0)&&(num_cols
==0))),
2168 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2170 m_sizeRows
= num_rows
;
2171 m_sizeCols
= num_cols
;
2174 const wxColour
& wxGridCellAttr::GetTextColour() const
2176 if (HasTextColour())
2180 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2182 return m_defGridAttr
->GetTextColour();
2186 wxFAIL_MSG(wxT("Missing default cell attribute"));
2187 return wxNullColour
;
2192 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2194 if (HasBackgroundColour())
2196 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2197 return m_defGridAttr
->GetBackgroundColour();
2200 wxFAIL_MSG(wxT("Missing default cell attribute"));
2201 return wxNullColour
;
2206 const wxFont
& wxGridCellAttr::GetFont() const
2210 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2211 return m_defGridAttr
->GetFont();
2214 wxFAIL_MSG(wxT("Missing default cell attribute"));
2220 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2224 if ( hAlign
) *hAlign
= m_hAlign
;
2225 if ( vAlign
) *vAlign
= m_vAlign
;
2227 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2228 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2231 wxFAIL_MSG(wxT("Missing default cell attribute"));
2235 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2237 if ( num_rows
) *num_rows
= m_sizeRows
;
2238 if ( num_cols
) *num_cols
= m_sizeCols
;
2241 // GetRenderer and GetEditor use a slightly different decision path about
2242 // which attribute to use. If a non-default attr object has one then it is
2243 // used, otherwise the default editor or renderer is fetched from the grid and
2244 // used. It should be the default for the data type of the cell. If it is
2245 // NULL (because the table has a type that the grid does not have in its
2246 // registry,) then the grid's default editor or renderer is used.
2248 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2250 wxGridCellRenderer
*renderer
;
2252 if ( m_renderer
&& this != m_defGridAttr
)
2254 // use the cells renderer if it has one
2255 renderer
= m_renderer
;
2258 else // no non default cell renderer
2260 // get default renderer for the data type
2263 // GetDefaultRendererForCell() will do IncRef() for us
2264 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2273 if (m_defGridAttr
&& this != m_defGridAttr
)
2275 // if we still don't have one then use the grid default
2276 // (no need for IncRef() here neither)
2277 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2279 else // default grid attr
2281 // use m_renderer which we had decided not to use initially
2282 renderer
= m_renderer
;
2289 // we're supposed to always find something
2290 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2295 // same as above, except for s/renderer/editor/g
2296 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2298 wxGridCellEditor
*editor
;
2300 if ( m_editor
&& this != m_defGridAttr
)
2302 // use the cells editor if it has one
2306 else // no non default cell editor
2308 // get default editor for the data type
2311 // GetDefaultEditorForCell() will do IncRef() for us
2312 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2321 if ( m_defGridAttr
&& this != m_defGridAttr
)
2323 // if we still don't have one then use the grid default
2324 // (no need for IncRef() here neither)
2325 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2327 else // default grid attr
2329 // use m_editor which we had decided not to use initially
2337 // we're supposed to always find something
2338 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2343 // ----------------------------------------------------------------------------
2344 // wxGridCellAttrData
2345 // ----------------------------------------------------------------------------
2347 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2349 int n
= FindIndex(row
, col
);
2350 if ( n
== wxNOT_FOUND
)
2352 // add the attribute
2353 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2357 // free the old attribute
2358 m_attrs
[(size_t)n
].attr
->DecRef();
2362 // change the attribute
2363 m_attrs
[(size_t)n
].attr
= attr
;
2367 // remove this attribute
2368 m_attrs
.RemoveAt((size_t)n
);
2373 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2375 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2377 int n
= FindIndex(row
, col
);
2378 if ( n
!= wxNOT_FOUND
)
2380 attr
= m_attrs
[(size_t)n
].attr
;
2387 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2389 size_t count
= m_attrs
.GetCount();
2390 for ( size_t n
= 0; n
< count
; n
++ )
2392 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2393 wxCoord row
= coords
.GetRow();
2394 if ((size_t)row
>= pos
)
2398 // If rows inserted, include row counter where necessary
2399 coords
.SetRow(row
+ numRows
);
2401 else if (numRows
< 0)
2403 // If rows deleted ...
2404 if ((size_t)row
>= pos
- numRows
)
2406 // ...either decrement row counter (if row still exists)...
2407 coords
.SetRow(row
+ numRows
);
2411 // ...or remove the attribute
2412 m_attrs
.RemoveAt((size_t)n
);
2420 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2422 size_t count
= m_attrs
.GetCount();
2423 for ( size_t n
= 0; n
< count
; n
++ )
2425 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2426 wxCoord col
= coords
.GetCol();
2427 if ( (size_t)col
>= pos
)
2431 // If rows inserted, include row counter where necessary
2432 coords
.SetCol(col
+ numCols
);
2434 else if (numCols
< 0)
2436 // If rows deleted ...
2437 if ((size_t)col
>= pos
- numCols
)
2439 // ...either decrement row counter (if row still exists)...
2440 coords
.SetCol(col
+ numCols
);
2444 // ...or remove the attribute
2445 m_attrs
.RemoveAt((size_t)n
);
2453 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2455 size_t count
= m_attrs
.GetCount();
2456 for ( size_t n
= 0; n
< count
; n
++ )
2458 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2459 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2468 // ----------------------------------------------------------------------------
2469 // wxGridRowOrColAttrData
2470 // ----------------------------------------------------------------------------
2472 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2474 size_t count
= m_attrs
.Count();
2475 for ( size_t n
= 0; n
< count
; n
++ )
2477 m_attrs
[n
]->DecRef();
2481 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2483 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2485 int n
= m_rowsOrCols
.Index(rowOrCol
);
2486 if ( n
!= wxNOT_FOUND
)
2488 attr
= m_attrs
[(size_t)n
];
2495 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2497 int i
= m_rowsOrCols
.Index(rowOrCol
);
2498 if ( i
== wxNOT_FOUND
)
2500 // add the attribute
2501 m_rowsOrCols
.Add(rowOrCol
);
2506 size_t n
= (size_t)i
;
2509 // change the attribute
2510 m_attrs
[n
]->DecRef();
2515 // remove this attribute
2516 m_attrs
[n
]->DecRef();
2517 m_rowsOrCols
.RemoveAt(n
);
2518 m_attrs
.RemoveAt(n
);
2523 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2525 size_t count
= m_attrs
.GetCount();
2526 for ( size_t n
= 0; n
< count
; n
++ )
2528 int & rowOrCol
= m_rowsOrCols
[n
];
2529 if ( (size_t)rowOrCol
>= pos
)
2531 if ( numRowsOrCols
> 0 )
2533 // If rows inserted, include row counter where necessary
2534 rowOrCol
+= numRowsOrCols
;
2536 else if ( numRowsOrCols
< 0)
2538 // If rows deleted, either decrement row counter (if row still exists)
2539 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2540 rowOrCol
+= numRowsOrCols
;
2543 m_rowsOrCols
.RemoveAt((size_t)n
);
2544 m_attrs
.RemoveAt((size_t)n
);
2552 // ----------------------------------------------------------------------------
2553 // wxGridCellAttrProvider
2554 // ----------------------------------------------------------------------------
2556 wxGridCellAttrProvider::wxGridCellAttrProvider()
2558 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2561 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2566 void wxGridCellAttrProvider::InitData()
2568 m_data
= new wxGridCellAttrProviderData
;
2571 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2572 wxGridCellAttr::wxAttrKind kind
) const
2574 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2579 case (wxGridCellAttr::Any
):
2580 //Get cached merge attributes.
2581 // Currenlty not used as no cache implemented as not mutiable
2582 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2585 //Basicaly implement old version.
2586 //Also check merge cache, so we don't have to re-merge every time..
2587 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2588 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2589 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2591 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2592 // Two or move are non NULL
2593 attr
= new wxGridCellAttr
;
2594 attr
->SetKind(wxGridCellAttr::Merged
);
2598 attr
->MergeWith(attrcell
);
2602 attr
->MergeWith(attrcol
);
2606 attr
->MergeWith(attrrow
);
2609 //store merge attr if cache implemented
2611 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2615 // one or none is non null return it or null.
2616 if(attrrow
) attr
= attrrow
;
2617 if(attrcol
) attr
= attrcol
;
2618 if(attrcell
) attr
= attrcell
;
2622 case (wxGridCellAttr::Cell
):
2623 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2625 case (wxGridCellAttr::Col
):
2626 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2628 case (wxGridCellAttr::Row
):
2629 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2633 // (wxGridCellAttr::Default):
2634 // (wxGridCellAttr::Merged):
2641 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2647 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2650 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2655 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2658 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2663 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2666 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2670 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2672 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2676 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2680 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2682 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2686 // ----------------------------------------------------------------------------
2687 // wxGridTypeRegistry
2688 // ----------------------------------------------------------------------------
2690 wxGridTypeRegistry::~wxGridTypeRegistry()
2692 size_t count
= m_typeinfo
.Count();
2693 for ( size_t i
= 0; i
< count
; i
++ )
2694 delete m_typeinfo
[i
];
2698 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2699 wxGridCellRenderer
* renderer
,
2700 wxGridCellEditor
* editor
)
2702 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2704 // is it already registered?
2705 int loc
= FindRegisteredDataType(typeName
);
2706 if ( loc
!= wxNOT_FOUND
)
2708 delete m_typeinfo
[loc
];
2709 m_typeinfo
[loc
] = info
;
2713 m_typeinfo
.Add(info
);
2717 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2719 size_t count
= m_typeinfo
.GetCount();
2720 for ( size_t i
= 0; i
< count
; i
++ )
2722 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2731 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2733 int index
= FindRegisteredDataType(typeName
);
2734 if ( index
== wxNOT_FOUND
)
2736 // check whether this is one of the standard ones, in which case
2737 // register it "on the fly"
2739 if ( typeName
== wxGRID_VALUE_STRING
)
2741 RegisterDataType(wxGRID_VALUE_STRING
,
2742 new wxGridCellStringRenderer
,
2743 new wxGridCellTextEditor
);
2745 #endif // wxUSE_TEXTCTRL
2747 if ( typeName
== wxGRID_VALUE_BOOL
)
2749 RegisterDataType(wxGRID_VALUE_BOOL
,
2750 new wxGridCellBoolRenderer
,
2751 new wxGridCellBoolEditor
);
2753 #endif // wxUSE_CHECKBOX
2755 if ( typeName
== wxGRID_VALUE_NUMBER
)
2757 RegisterDataType(wxGRID_VALUE_NUMBER
,
2758 new wxGridCellNumberRenderer
,
2759 new wxGridCellNumberEditor
);
2761 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2763 RegisterDataType(wxGRID_VALUE_FLOAT
,
2764 new wxGridCellFloatRenderer
,
2765 new wxGridCellFloatEditor
);
2767 #endif // wxUSE_TEXTCTRL
2769 if ( typeName
== wxGRID_VALUE_CHOICE
)
2771 RegisterDataType(wxGRID_VALUE_CHOICE
,
2772 new wxGridCellStringRenderer
,
2773 new wxGridCellChoiceEditor
);
2775 #endif // wxUSE_COMBOBOX
2780 // we get here only if just added the entry for this type, so return
2782 index
= m_typeinfo
.GetCount() - 1;
2788 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2790 int index
= FindDataType(typeName
);
2791 if ( index
== wxNOT_FOUND
)
2793 // the first part of the typename is the "real" type, anything after ':'
2794 // are the parameters for the renderer
2795 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2796 if ( index
== wxNOT_FOUND
)
2801 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2802 wxGridCellRenderer
*rendererOld
= renderer
;
2803 renderer
= renderer
->Clone();
2804 rendererOld
->DecRef();
2806 wxGridCellEditor
*editor
= GetEditor(index
);
2807 wxGridCellEditor
*editorOld
= editor
;
2808 editor
= editor
->Clone();
2809 editorOld
->DecRef();
2811 // do it even if there are no parameters to reset them to defaults
2812 wxString params
= typeName
.AfterFirst(_T(':'));
2813 renderer
->SetParameters(params
);
2814 editor
->SetParameters(params
);
2816 // register the new typename
2817 RegisterDataType(typeName
, renderer
, editor
);
2819 // we just registered it, it's the last one
2820 index
= m_typeinfo
.GetCount() - 1;
2826 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2828 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2834 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2836 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2842 // ----------------------------------------------------------------------------
2844 // ----------------------------------------------------------------------------
2846 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2849 wxGridTableBase::wxGridTableBase()
2851 m_view
= (wxGrid
*) NULL
;
2852 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2855 wxGridTableBase::~wxGridTableBase()
2857 delete m_attrProvider
;
2860 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2862 delete m_attrProvider
;
2863 m_attrProvider
= attrProvider
;
2866 bool wxGridTableBase::CanHaveAttributes()
2868 if ( ! GetAttrProvider() )
2870 // use the default attr provider by default
2871 SetAttrProvider(new wxGridCellAttrProvider
);
2876 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2878 if ( m_attrProvider
)
2879 return m_attrProvider
->GetAttr(row
, col
, kind
);
2881 return (wxGridCellAttr
*)NULL
;
2884 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2886 if ( m_attrProvider
)
2888 attr
->SetKind(wxGridCellAttr::Cell
);
2889 m_attrProvider
->SetAttr(attr
, row
, col
);
2893 // as we take ownership of the pointer and don't store it, we must
2899 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2901 if ( m_attrProvider
)
2903 attr
->SetKind(wxGridCellAttr::Row
);
2904 m_attrProvider
->SetRowAttr(attr
, row
);
2908 // as we take ownership of the pointer and don't store it, we must
2914 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2916 if ( m_attrProvider
)
2918 attr
->SetKind(wxGridCellAttr::Col
);
2919 m_attrProvider
->SetColAttr(attr
, col
);
2923 // as we take ownership of the pointer and don't store it, we must
2929 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2930 size_t WXUNUSED(numRows
) )
2932 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2937 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2939 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2944 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2945 size_t WXUNUSED(numRows
) )
2947 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2952 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2953 size_t WXUNUSED(numCols
) )
2955 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2960 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2962 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2967 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2968 size_t WXUNUSED(numCols
) )
2970 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2976 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2979 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2980 // how much it makes sense to us geeks.
2984 wxString
wxGridTableBase::GetColLabelValue( int col
)
2986 // default col labels are:
2987 // cols 0 to 25 : A-Z
2988 // cols 26 to 675 : AA-ZZ
2993 for ( n
= 1; ; n
++ )
2995 s
+= (_T('A') + (wxChar
)( col%26
));
2997 if ( col
< 0 ) break;
3000 // reverse the string...
3002 for ( i
= 0; i
< n
; i
++ )
3011 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3013 return wxGRID_VALUE_STRING
;
3016 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3017 const wxString
& typeName
)
3019 return typeName
== wxGRID_VALUE_STRING
;
3022 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3024 return CanGetValueAs(row
, col
, typeName
);
3027 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3032 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3037 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3042 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3043 long WXUNUSED(value
) )
3047 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3048 double WXUNUSED(value
) )
3052 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3053 bool WXUNUSED(value
) )
3058 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3059 const wxString
& WXUNUSED(typeName
) )
3064 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3065 const wxString
& WXUNUSED(typeName
),
3066 void* WXUNUSED(value
) )
3070 //////////////////////////////////////////////////////////////////////
3072 // Message class for the grid table to send requests and notifications
3076 wxGridTableMessage::wxGridTableMessage()
3078 m_table
= (wxGridTableBase
*) NULL
;
3084 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3085 int commandInt1
, int commandInt2
)
3089 m_comInt1
= commandInt1
;
3090 m_comInt2
= commandInt2
;
3095 //////////////////////////////////////////////////////////////////////
3097 // A basic grid table for string data. An object of this class will
3098 // created by wxGrid if you don't specify an alternative table class.
3101 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3103 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3105 wxGridStringTable::wxGridStringTable()
3110 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3113 m_data
.Alloc( numRows
);
3116 sa
.Alloc( numCols
);
3117 sa
.Add( wxEmptyString
, numCols
);
3119 m_data
.Add( sa
, numRows
);
3122 wxGridStringTable::~wxGridStringTable()
3126 int wxGridStringTable::GetNumberRows()
3128 return m_data
.GetCount();
3131 int wxGridStringTable::GetNumberCols()
3133 if ( m_data
.GetCount() > 0 )
3134 return m_data
[0].GetCount();
3139 wxString
wxGridStringTable::GetValue( int row
, int col
)
3141 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3143 _T("invalid row or column index in wxGridStringTable") );
3145 return m_data
[row
][col
];
3148 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3150 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3151 _T("invalid row or column index in wxGridStringTable") );
3153 m_data
[row
][col
] = value
;
3156 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3158 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3160 _T("invalid row or column index in wxGridStringTable") );
3162 return (m_data
[row
][col
] == wxEmptyString
);
3165 void wxGridStringTable::Clear()
3168 int numRows
, numCols
;
3170 numRows
= m_data
.GetCount();
3173 numCols
= m_data
[0].GetCount();
3175 for ( row
= 0; row
< numRows
; row
++ )
3177 for ( col
= 0; col
< numCols
; col
++ )
3179 m_data
[row
][col
] = wxEmptyString
;
3186 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3188 size_t curNumRows
= m_data
.GetCount();
3189 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3190 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3192 if ( pos
>= curNumRows
)
3194 return AppendRows( numRows
);
3198 sa
.Alloc( curNumCols
);
3199 sa
.Add( wxEmptyString
, curNumCols
);
3200 m_data
.Insert( sa
, pos
, numRows
);
3203 wxGridTableMessage
msg( this,
3204 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3208 GetView()->ProcessTableMessage( msg
);
3214 bool wxGridStringTable::AppendRows( size_t numRows
)
3216 size_t curNumRows
= m_data
.GetCount();
3217 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3218 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3221 if ( curNumCols
> 0 )
3223 sa
.Alloc( curNumCols
);
3224 sa
.Add( wxEmptyString
, curNumCols
);
3227 m_data
.Add( sa
, numRows
);
3231 wxGridTableMessage
msg( this,
3232 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3235 GetView()->ProcessTableMessage( msg
);
3241 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3243 size_t curNumRows
= m_data
.GetCount();
3245 if ( pos
>= curNumRows
)
3247 wxFAIL_MSG( wxString::Format
3249 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3251 (unsigned long)numRows
,
3252 (unsigned long)curNumRows
3258 if ( numRows
> curNumRows
- pos
)
3260 numRows
= curNumRows
- pos
;
3263 if ( numRows
>= curNumRows
)
3269 m_data
.RemoveAt( pos
, numRows
);
3273 wxGridTableMessage
msg( this,
3274 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3278 GetView()->ProcessTableMessage( msg
);
3284 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3288 size_t curNumRows
= m_data
.GetCount();
3289 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3290 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3292 if ( pos
>= curNumCols
)
3294 return AppendCols( numCols
);
3297 for ( row
= 0; row
< curNumRows
; row
++ )
3299 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3301 m_data
[row
].Insert( wxEmptyString
, col
);
3306 wxGridTableMessage
msg( this,
3307 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3311 GetView()->ProcessTableMessage( msg
);
3317 bool wxGridStringTable::AppendCols( size_t numCols
)
3321 size_t curNumRows
= m_data
.GetCount();
3325 // TODO: something better than this ?
3327 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3332 for ( row
= 0; row
< curNumRows
; row
++ )
3334 m_data
[row
].Add( wxEmptyString
, numCols
);
3339 wxGridTableMessage
msg( this,
3340 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3343 GetView()->ProcessTableMessage( msg
);
3349 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3353 size_t curNumRows
= m_data
.GetCount();
3354 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3355 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3357 if ( pos
>= curNumCols
)
3359 wxFAIL_MSG( wxString::Format
3361 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3363 (unsigned long)numCols
,
3364 (unsigned long)curNumCols
3369 if ( numCols
> curNumCols
- pos
)
3371 numCols
= curNumCols
- pos
;
3374 for ( row
= 0; row
< curNumRows
; row
++ )
3376 if ( numCols
>= curNumCols
)
3378 m_data
[row
].Clear();
3382 m_data
[row
].RemoveAt( pos
, numCols
);
3387 wxGridTableMessage
msg( this,
3388 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3392 GetView()->ProcessTableMessage( msg
);
3398 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3400 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3402 // using default label
3404 return wxGridTableBase::GetRowLabelValue( row
);
3408 return m_rowLabels
[ row
];
3412 wxString
wxGridStringTable::GetColLabelValue( int col
)
3414 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3416 // using default label
3418 return wxGridTableBase::GetColLabelValue( col
);
3422 return m_colLabels
[ col
];
3426 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3428 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3430 int n
= m_rowLabels
.GetCount();
3432 for ( i
= n
; i
<= row
; i
++ )
3434 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3438 m_rowLabels
[row
] = value
;
3441 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3443 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3445 int n
= m_colLabels
.GetCount();
3447 for ( i
= n
; i
<= col
; i
++ )
3449 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3453 m_colLabels
[col
] = value
;
3458 //////////////////////////////////////////////////////////////////////
3459 //////////////////////////////////////////////////////////////////////
3461 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3463 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3464 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3465 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3466 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3467 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3468 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3471 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3473 const wxPoint
&pos
, const wxSize
&size
)
3474 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3479 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3483 // NO - don't do this because it will set both the x and y origin
3484 // coords to match the parent scrolled window and we just want to
3485 // set the y coord - MB
3487 // m_owner->PrepareDC( dc );
3490 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3491 dc
.SetDeviceOrigin( 0, -y
);
3493 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3494 m_owner
->DrawRowLabels( dc
, rows
);
3498 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3500 m_owner
->ProcessRowLabelMouseEvent( event
);
3504 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3506 m_owner
->GetEventHandler()->ProcessEvent(event
);
3510 // This seems to be required for wxMotif otherwise the mouse
3511 // cursor must be in the cell edit control to get key events
3513 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3515 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3518 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3520 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3525 //////////////////////////////////////////////////////////////////////
3527 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3529 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3530 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3531 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3532 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3533 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3534 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3537 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3539 const wxPoint
&pos
, const wxSize
&size
)
3540 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3545 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3549 // NO - don't do this because it will set both the x and y origin
3550 // coords to match the parent scrolled window and we just want to
3551 // set the x coord - MB
3553 // m_owner->PrepareDC( dc );
3556 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3557 dc
.SetDeviceOrigin( -x
, 0 );
3559 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3560 m_owner
->DrawColLabels( dc
, cols
);
3564 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3566 m_owner
->ProcessColLabelMouseEvent( event
);
3569 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3571 m_owner
->GetEventHandler()->ProcessEvent(event
);
3575 // This seems to be required for wxMotif otherwise the mouse
3576 // cursor must be in the cell edit control to get key events
3578 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3580 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3583 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3585 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3590 //////////////////////////////////////////////////////////////////////
3592 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3594 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3595 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3596 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3597 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3598 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3599 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3602 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3604 const wxPoint
&pos
, const wxSize
&size
)
3605 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3610 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3614 int client_height
= 0;
3615 int client_width
= 0;
3616 GetClientSize( &client_width
, &client_height
);
3618 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3619 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3620 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3621 dc
.DrawLine( 0, 0, client_width
, 0 );
3622 dc
.DrawLine( 0, 0, 0, client_height
);
3624 dc
.SetPen( *wxWHITE_PEN
);
3625 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3626 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3630 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3632 m_owner
->ProcessCornerLabelMouseEvent( event
);
3636 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3638 m_owner
->GetEventHandler()->ProcessEvent(event
);
3641 // This seems to be required for wxMotif otherwise the mouse
3642 // cursor must be in the cell edit control to get key events
3644 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3646 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3649 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3651 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3656 //////////////////////////////////////////////////////////////////////
3658 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3660 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3661 EVT_PAINT( wxGridWindow::OnPaint
)
3662 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3663 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3664 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3665 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3666 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3667 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3668 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3671 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3672 wxGridRowLabelWindow
*rowLblWin
,
3673 wxGridColLabelWindow
*colLblWin
,
3676 const wxSize
&size
)
3677 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
,
3678 wxT("grid window") )
3682 m_rowLabelWin
= rowLblWin
;
3683 m_colLabelWin
= colLblWin
;
3684 SetBackgroundColour(_T("WHITE"));
3688 wxGridWindow::~wxGridWindow()
3693 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3695 wxPaintDC
dc( this );
3696 m_owner
->PrepareDC( dc
);
3697 wxRegion reg
= GetUpdateRegion();
3698 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3699 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3700 #if WXGRID_DRAW_LINES
3701 m_owner
->DrawAllGridLines( dc
, reg
);
3703 m_owner
->DrawGridSpace( dc
);
3704 m_owner
->DrawHighlight( dc
, DirtyCells
);
3708 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3710 wxWindow::ScrollWindow( dx
, dy
, rect
);
3711 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3712 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3716 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3718 m_owner
->ProcessGridCellMouseEvent( event
);
3721 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3723 m_owner
->GetEventHandler()->ProcessEvent(event
);
3726 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3727 // cursor must be in the cell edit control to get key events
3729 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3731 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3734 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3736 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3739 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3743 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3745 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3749 //////////////////////////////////////////////////////////////////////
3751 // Internal Helper function for computing row or column from some
3752 // (unscrolled) coordinate value, using either
3753 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3754 // of m_rowBottoms/m_ColRights to speed up the search!
3756 // Internal helper macros for simpler use of that function
3758 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3759 const wxArrayInt
& BorderArray
, int nMax
,
3762 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3763 m_minAcceptableColWidth, \
3764 m_colRights, m_numCols, TRUE)
3765 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3766 m_minAcceptableRowHeight, \
3767 m_rowBottoms, m_numRows, TRUE)
3768 /////////////////////////////////////////////////////////////////////
3770 #if wxUSE_EXTENDED_RTTI
3771 WX_DEFINE_FLAGS( wxGridStyle
)
3773 wxBEGIN_FLAGS( wxGridStyle
)
3774 // new style border flags, we put them first to
3775 // use them for streaming out
3776 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3777 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3778 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3779 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3780 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3781 wxFLAGS_MEMBER(wxBORDER_NONE
)
3783 // old style border flags
3784 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3785 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3786 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3787 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3788 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3789 wxFLAGS_MEMBER(wxBORDER
)
3791 // standard window styles
3792 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3793 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3794 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3795 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3796 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3797 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3798 wxFLAGS_MEMBER(wxVSCROLL
)
3799 wxFLAGS_MEMBER(wxHSCROLL
)
3801 wxEND_FLAGS( wxGridStyle
)
3803 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3805 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3806 wxHIDE_PROPERTY( Children
)
3807 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3808 wxEND_PROPERTIES_TABLE()
3810 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3811 wxEND_HANDLERS_TABLE()
3813 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3816 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
3819 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3822 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3823 EVT_PAINT( wxGrid::OnPaint
)
3824 EVT_SIZE( wxGrid::OnSize
)
3825 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3826 EVT_KEY_UP( wxGrid::OnKeyUp
)
3827 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3832 // in order to make sure that a size event is not
3833 // trigerred in a unfinished state
3834 m_cornerLabelWin
= NULL
;
3835 m_rowLabelWin
= NULL
;
3836 m_colLabelWin
= NULL
;
3840 wxGrid::wxGrid( wxWindow
*parent
,
3845 const wxString
& name
)
3846 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3847 m_colMinWidths(GRID_HASH_SIZE
),
3848 m_rowMinHeights(GRID_HASH_SIZE
)
3853 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3854 const wxPoint
& pos
, const wxSize
& size
,
3855 long style
, const wxString
& name
)
3857 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3858 style
| wxWANTS_CHARS
, name
))
3861 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3862 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3873 // Must do this or ~wxScrollHelper will pop the wrong event handler
3874 SetTargetWindow(this);
3876 wxSafeDecRef(m_defaultCellAttr
);
3878 #ifdef DEBUG_ATTR_CACHE
3879 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3880 wxPrintf(_T("wxGrid attribute cache statistics: "
3881 "total: %u, hits: %u (%u%%)\n"),
3882 total
, gs_nAttrCacheHits
,
3883 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3889 delete m_typeRegistry
;
3895 // ----- internal init and update functions
3898 void wxGrid::Create()
3900 m_created
= FALSE
; // set to TRUE by CreateGrid
3902 m_table
= (wxGridTableBase
*) NULL
;
3905 m_cellEditCtrlEnabled
= FALSE
;
3907 m_defaultCellAttr
= new wxGridCellAttr();
3909 // Set default cell attributes
3910 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3911 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3912 m_defaultCellAttr
->SetFont(GetFont());
3913 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3914 m_defaultCellAttr
->SetTextColour(
3915 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3916 m_defaultCellAttr
->SetBackgroundColour(
3917 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3918 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3919 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3924 m_currentCellCoords
= wxGridNoCellCoords
;
3926 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3927 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3929 // create the type registry
3930 m_typeRegistry
= new wxGridTypeRegistry
;
3933 // subwindow components that make up the wxGrid
3934 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3939 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3944 m_colLabelWin
= new wxGridColLabelWindow( this,
3949 m_gridWin
= new wxGridWindow( this,
3956 SetTargetWindow( m_gridWin
);
3962 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3963 wxGrid::wxGridSelectionModes selmode
)
3965 wxCHECK_MSG( !m_created
,
3967 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3969 m_numRows
= numRows
;
3970 m_numCols
= numCols
;
3972 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3973 m_table
->SetView( this );
3975 m_selection
= new wxGridSelection( this, selmode
);
3984 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3986 wxCHECK_RET( m_created
,
3987 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3989 m_selection
->SetSelectionMode( selmode
);
3992 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3994 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3995 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3997 return m_selection
->GetSelectionMode();
4000 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4001 wxGrid::wxGridSelectionModes selmode
)
4005 // stop all processing
4010 wxGridTableBase
*t
=m_table
;
4023 m_numRows
= table
->GetNumberRows();
4024 m_numCols
= table
->GetNumberCols();
4027 m_table
->SetView( this );
4030 m_selection
= new wxGridSelection( this, selmode
);
4043 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4044 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4046 if ( m_rowLabelWin
)
4048 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4052 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4055 m_labelTextColour
= wxColour( _T("BLACK") );
4058 m_attrCache
.row
= -1;
4059 m_attrCache
.col
= -1;
4060 m_attrCache
.attr
= NULL
;
4062 // TODO: something better than this ?
4064 m_labelFont
= this->GetFont();
4065 m_labelFont
.SetWeight( wxBOLD
);
4067 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4068 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4070 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4071 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4072 m_colLabelTextOrientation
= wxHORIZONTAL
;
4074 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4075 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4077 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4078 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4080 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4081 m_defaultRowHeight
+= 8;
4083 m_defaultRowHeight
+= 4;
4086 m_gridLineColour
= wxColour( 192,192,192 );
4087 m_gridLinesEnabled
= TRUE
;
4088 m_cellHighlightColour
= *wxBLACK
;
4089 m_cellHighlightPenWidth
= 2;
4090 m_cellHighlightROPenWidth
= 1;
4092 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4093 m_winCapture
= (wxWindow
*)NULL
;
4094 m_canDragRowSize
= TRUE
;
4095 m_canDragColSize
= TRUE
;
4096 m_canDragGridSize
= TRUE
;
4098 m_dragRowOrCol
= -1;
4099 m_isDragging
= FALSE
;
4100 m_startDragPos
= wxDefaultPosition
;
4102 m_waitForSlowClick
= FALSE
;
4104 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4105 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4107 m_currentCellCoords
= wxGridNoCellCoords
;
4109 m_selectingTopLeft
= wxGridNoCellCoords
;
4110 m_selectingBottomRight
= wxGridNoCellCoords
;
4111 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4112 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4114 m_editable
= TRUE
; // default for whole grid
4116 m_inOnKeyDown
= FALSE
;
4123 // ----------------------------------------------------------------------------
4124 // the idea is to call these functions only when necessary because they create
4125 // quite big arrays which eat memory mostly unnecessary - in particular, if
4126 // default widths/heights are used for all rows/columns, we may not use these
4129 // with some extra code, it should be possible to only store the
4130 // widths/heights different from default ones but this will be done later...
4131 // ----------------------------------------------------------------------------
4133 void wxGrid::InitRowHeights()
4135 m_rowHeights
.Empty();
4136 m_rowBottoms
.Empty();
4138 m_rowHeights
.Alloc( m_numRows
);
4139 m_rowBottoms
.Alloc( m_numRows
);
4143 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4145 for ( int i
= 0; i
< m_numRows
; i
++ )
4147 rowBottom
+= m_defaultRowHeight
;
4148 m_rowBottoms
.Add( rowBottom
);
4152 void wxGrid::InitColWidths()
4154 m_colWidths
.Empty();
4155 m_colRights
.Empty();
4157 m_colWidths
.Alloc( m_numCols
);
4158 m_colRights
.Alloc( m_numCols
);
4161 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4163 for ( int i
= 0; i
< m_numCols
; i
++ )
4165 colRight
+= m_defaultColWidth
;
4166 m_colRights
.Add( colRight
);
4170 int wxGrid::GetColWidth(int col
) const
4172 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4175 int wxGrid::GetColLeft(int col
) const
4177 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4178 : m_colRights
[col
] - m_colWidths
[col
];
4181 int wxGrid::GetColRight(int col
) const
4183 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4187 int wxGrid::GetRowHeight(int row
) const
4189 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4192 int wxGrid::GetRowTop(int row
) const
4194 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4195 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4198 int wxGrid::GetRowBottom(int row
) const
4200 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4201 : m_rowBottoms
[row
];
4204 void wxGrid::CalcDimensions()
4207 GetClientSize( &cw
, &ch
);
4209 if ( m_rowLabelWin
->IsShown() )
4210 cw
-= m_rowLabelWidth
;
4211 if ( m_colLabelWin
->IsShown() )
4212 ch
-= m_colLabelHeight
;
4215 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4216 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4218 // take into account editor if shown
4219 if( IsCellEditControlShown() )
4222 int r
= m_currentCellCoords
.GetRow();
4223 int c
= m_currentCellCoords
.GetCol();
4224 int x
= GetColLeft(c
);
4225 int y
= GetRowTop(r
);
4227 // how big is the editor
4228 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4229 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4230 editor
->GetControl()->GetSize(&w2
, &h2
);
4233 if( w2
> w
) w
= w2
;
4234 if( h2
> h
) h
= h2
;
4239 // preserve (more or less) the previous position
4241 GetViewStart( &x
, &y
);
4243 // ensure the position is valid for the new scroll ranges
4245 x
= wxMax( w
- 1, 0 );
4247 y
= wxMax( h
- 1, 0 );
4249 // do set scrollbar parameters
4250 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4251 GetScrollX(w
), GetScrollY(h
), x
, y
,
4252 GetBatchCount() != 0);
4254 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4255 // still must reposition the children
4260 void wxGrid::CalcWindowSizes()
4262 // escape if the window is has not been fully created yet
4264 if ( m_cornerLabelWin
== NULL
)
4268 GetClientSize( &cw
, &ch
);
4270 if ( m_cornerLabelWin
->IsShown() )
4271 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4273 if ( m_colLabelWin
->IsShown() )
4274 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4276 if ( m_rowLabelWin
->IsShown() )
4277 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4279 if ( m_gridWin
->IsShown() )
4280 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4284 // this is called when the grid table sends a message to say that it
4285 // has been redimensioned
4287 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4290 bool result
= FALSE
;
4292 // Clear the attribute cache as the attribute might refer to a different
4293 // cell than stored in the cache after adding/removing rows/columns.
4295 // By the same reasoning, the editor should be dismissed if columns are
4296 // added or removed. And for consistency, it should IMHO always be
4297 // removed, not only if the cell "underneath" it actually changes.
4298 // For now, I intentionally do not save the editor's content as the
4299 // cell it might want to save that stuff to might no longer exist.
4300 HideCellEditControl();
4302 // if we were using the default widths/heights so far, we must change them
4304 if ( m_colWidths
.IsEmpty() )
4309 if ( m_rowHeights
.IsEmpty() )
4315 switch ( msg
.GetId() )
4317 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4319 size_t pos
= msg
.GetCommandInt();
4320 int numRows
= msg
.GetCommandInt2();
4322 m_numRows
+= numRows
;
4324 if ( !m_rowHeights
.IsEmpty() )
4326 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4327 m_rowBottoms
.Insert( 0, pos
, numRows
);
4330 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4332 for ( i
= pos
; i
< m_numRows
; i
++ )
4334 bottom
+= m_rowHeights
[i
];
4335 m_rowBottoms
[i
] = bottom
;
4338 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4340 // if we have just inserted cols into an empty grid the current
4341 // cell will be undefined...
4343 SetCurrentCell( 0, 0 );
4347 m_selection
->UpdateRows( pos
, numRows
);
4348 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4350 attrProvider
->UpdateAttrRows( pos
, numRows
);
4352 if ( !GetBatchCount() )
4355 m_rowLabelWin
->Refresh();
4361 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4363 int numRows
= msg
.GetCommandInt();
4364 int oldNumRows
= m_numRows
;
4365 m_numRows
+= numRows
;
4367 if ( !m_rowHeights
.IsEmpty() )
4369 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4370 m_rowBottoms
.Add( 0, numRows
);
4373 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4375 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4377 bottom
+= m_rowHeights
[i
];
4378 m_rowBottoms
[i
] = bottom
;
4381 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4383 // if we have just inserted cols into an empty grid the current
4384 // cell will be undefined...
4386 SetCurrentCell( 0, 0 );
4388 if ( !GetBatchCount() )
4391 m_rowLabelWin
->Refresh();
4397 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4399 size_t pos
= msg
.GetCommandInt();
4400 int numRows
= msg
.GetCommandInt2();
4401 m_numRows
-= numRows
;
4403 if ( !m_rowHeights
.IsEmpty() )
4405 m_rowHeights
.RemoveAt( pos
, numRows
);
4406 m_rowBottoms
.RemoveAt( pos
, numRows
);
4409 for ( i
= 0; i
< m_numRows
; i
++ )
4411 h
+= m_rowHeights
[i
];
4412 m_rowBottoms
[i
] = h
;
4417 m_currentCellCoords
= wxGridNoCellCoords
;
4421 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4422 m_currentCellCoords
.Set( 0, 0 );
4426 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4427 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4429 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4430 // ifdef'd out following patch from Paul Gammans
4432 // No need to touch column attributes, unless we
4433 // removed _all_ rows, in this case, we remove
4434 // all column attributes.
4435 // I hate to do this here, but the
4436 // needed data is not available inside UpdateAttrRows.
4437 if ( !GetNumberRows() )
4438 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4441 if ( !GetBatchCount() )
4444 m_rowLabelWin
->Refresh();
4450 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4452 size_t pos
= msg
.GetCommandInt();
4453 int numCols
= msg
.GetCommandInt2();
4454 m_numCols
+= numCols
;
4456 if ( !m_colWidths
.IsEmpty() )
4458 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4459 m_colRights
.Insert( 0, pos
, numCols
);
4462 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4464 for ( i
= pos
; i
< m_numCols
; i
++ )
4466 right
+= m_colWidths
[i
];
4467 m_colRights
[i
] = right
;
4470 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4472 // if we have just inserted cols into an empty grid the current
4473 // cell will be undefined...
4475 SetCurrentCell( 0, 0 );
4479 m_selection
->UpdateCols( pos
, numCols
);
4480 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4482 attrProvider
->UpdateAttrCols( pos
, numCols
);
4483 if ( !GetBatchCount() )
4486 m_colLabelWin
->Refresh();
4493 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4495 int numCols
= msg
.GetCommandInt();
4496 int oldNumCols
= m_numCols
;
4497 m_numCols
+= numCols
;
4498 if ( !m_colWidths
.IsEmpty() )
4500 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4501 m_colRights
.Add( 0, numCols
);
4504 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4506 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4508 right
+= m_colWidths
[i
];
4509 m_colRights
[i
] = right
;
4512 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4514 // if we have just inserted cols into an empty grid the current
4515 // cell will be undefined...
4517 SetCurrentCell( 0, 0 );
4519 if ( !GetBatchCount() )
4522 m_colLabelWin
->Refresh();
4528 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4530 size_t pos
= msg
.GetCommandInt();
4531 int numCols
= msg
.GetCommandInt2();
4532 m_numCols
-= numCols
;
4534 if ( !m_colWidths
.IsEmpty() )
4536 m_colWidths
.RemoveAt( pos
, numCols
);
4537 m_colRights
.RemoveAt( pos
, numCols
);
4540 for ( i
= 0; i
< m_numCols
; i
++ )
4542 w
+= m_colWidths
[i
];
4548 m_currentCellCoords
= wxGridNoCellCoords
;
4552 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4553 m_currentCellCoords
.Set( 0, 0 );
4557 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4558 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4560 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4561 // ifdef'd out following patch from Paul Gammans
4563 // No need to touch row attributes, unless we
4564 // removed _all_ columns, in this case, we remove
4565 // all row attributes.
4566 // I hate to do this here, but the
4567 // needed data is not available inside UpdateAttrCols.
4568 if ( !GetNumberCols() )
4569 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4572 if ( !GetBatchCount() )
4575 m_colLabelWin
->Refresh();
4582 if (result
&& !GetBatchCount() )
4583 m_gridWin
->Refresh();
4588 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4590 wxRegionIterator
iter( reg
);
4593 wxArrayInt rowlabels
;
4600 // TODO: remove this when we can...
4601 // There is a bug in wxMotif that gives garbage update
4602 // rectangles if you jump-scroll a long way by clicking the
4603 // scrollbar with middle button. This is a work-around
4605 #if defined(__WXMOTIF__)
4607 m_gridWin
->GetClientSize( &cw
, &ch
);
4608 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4609 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4612 // logical bounds of update region
4615 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4616 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4618 // find the row labels within these bounds
4621 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4623 if ( GetRowBottom(row
) < top
)
4626 if ( GetRowTop(row
) > bottom
)
4629 rowlabels
.Add( row
);
4639 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4641 wxRegionIterator
iter( reg
);
4644 wxArrayInt colLabels
;
4651 // TODO: remove this when we can...
4652 // There is a bug in wxMotif that gives garbage update
4653 // rectangles if you jump-scroll a long way by clicking the
4654 // scrollbar with middle button. This is a work-around
4656 #if defined(__WXMOTIF__)
4658 m_gridWin
->GetClientSize( &cw
, &ch
);
4659 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4660 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4663 // logical bounds of update region
4666 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4667 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4669 // find the cells within these bounds
4672 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4674 if ( GetColRight(col
) < left
)
4677 if ( GetColLeft(col
) > right
)
4680 colLabels
.Add( col
);
4689 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4691 wxRegionIterator
iter( reg
);
4694 wxGridCellCoordsArray cellsExposed
;
4696 int left
, top
, right
, bottom
;
4701 // TODO: remove this when we can...
4702 // There is a bug in wxMotif that gives garbage update
4703 // rectangles if you jump-scroll a long way by clicking the
4704 // scrollbar with middle button. This is a work-around
4706 #if defined(__WXMOTIF__)
4708 m_gridWin
->GetClientSize( &cw
, &ch
);
4709 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4710 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4711 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4712 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4715 // logical bounds of update region
4717 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4718 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4720 // find the cells within these bounds
4723 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4725 if ( GetRowBottom(row
) <= top
)
4728 if ( GetRowTop(row
) > bottom
)
4731 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4733 if ( GetColRight(col
) <= left
)
4736 if ( GetColLeft(col
) > right
)
4739 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4746 return cellsExposed
;
4750 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4753 wxPoint
pos( event
.GetPosition() );
4754 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4756 if ( event
.Dragging() )
4760 m_isDragging
= TRUE
;
4761 m_rowLabelWin
->CaptureMouse();
4764 if ( event
.LeftIsDown() )
4766 switch( m_cursorMode
)
4768 case WXGRID_CURSOR_RESIZE_ROW
:
4770 int cw
, ch
, left
, dummy
;
4771 m_gridWin
->GetClientSize( &cw
, &ch
);
4772 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4774 wxClientDC
dc( m_gridWin
);
4777 GetRowTop(m_dragRowOrCol
) +
4778 GetRowMinimalHeight(m_dragRowOrCol
) );
4779 dc
.SetLogicalFunction(wxINVERT
);
4780 if ( m_dragLastPos
>= 0 )
4782 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4784 dc
.DrawLine( left
, y
, left
+cw
, y
);
4789 case WXGRID_CURSOR_SELECT_ROW
:
4790 if ( (row
= YToRow( y
)) >= 0 )
4794 m_selection
->SelectRow( row
,
4795 event
.ControlDown(),
4802 // default label to suppress warnings about "enumeration value
4803 // 'xxx' not handled in switch
4811 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4816 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4817 m_isDragging
= FALSE
;
4820 // ------------ Entering or leaving the window
4822 if ( event
.Entering() || event
.Leaving() )
4824 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4828 // ------------ Left button pressed
4830 else if ( event
.LeftDown() )
4832 // don't send a label click event for a hit on the
4833 // edge of the row label - this is probably the user
4834 // wanting to resize the row
4836 if ( YToEdgeOfRow(y
) < 0 )
4840 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4842 if ( !event
.ShiftDown() && !event
.ControlDown() )
4846 if ( event
.ShiftDown() )
4848 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4851 GetNumberCols() - 1,
4852 event
.ControlDown(),
4859 m_selection
->SelectRow( row
,
4860 event
.ControlDown(),
4867 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4872 // starting to drag-resize a row
4874 if ( CanDragRowSize() )
4875 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4880 // ------------ Left double click
4882 else if (event
.LeftDClick() )
4884 int row
= YToEdgeOfRow(y
);
4889 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4891 // no default action at the moment
4896 // adjust row height depending on label text
4897 AutoSizeRowLabelSize( row
);
4899 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4905 // ------------ Left button released
4907 else if ( event
.LeftUp() )
4909 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4911 DoEndDragResizeRow();
4913 // Note: we are ending the event *after* doing
4914 // default processing in this case
4916 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4919 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4924 // ------------ Right button down
4926 else if ( event
.RightDown() )
4930 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4932 // no default action at the moment
4937 // ------------ Right double click
4939 else if ( event
.RightDClick() )
4943 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4945 // no default action at the moment
4950 // ------------ No buttons down and mouse moving
4952 else if ( event
.Moving() )
4954 m_dragRowOrCol
= YToEdgeOfRow( y
);
4955 if ( m_dragRowOrCol
>= 0 )
4957 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4959 // don't capture the mouse yet
4960 if ( CanDragRowSize() )
4961 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4964 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4966 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4972 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4975 wxPoint
pos( event
.GetPosition() );
4976 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4978 if ( event
.Dragging() )
4982 m_isDragging
= TRUE
;
4983 m_colLabelWin
->CaptureMouse();
4986 if ( event
.LeftIsDown() )
4988 switch( m_cursorMode
)
4990 case WXGRID_CURSOR_RESIZE_COL
:
4992 int cw
, ch
, dummy
, top
;
4993 m_gridWin
->GetClientSize( &cw
, &ch
);
4994 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4996 wxClientDC
dc( m_gridWin
);
4999 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5000 GetColMinimalWidth(m_dragRowOrCol
));
5001 dc
.SetLogicalFunction(wxINVERT
);
5002 if ( m_dragLastPos
>= 0 )
5004 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5006 dc
.DrawLine( x
, top
, x
, top
+ch
);
5011 case WXGRID_CURSOR_SELECT_COL
:
5012 if ( (col
= XToCol( x
)) >= 0 )
5016 m_selection
->SelectCol( col
,
5017 event
.ControlDown(),
5024 // default label to suppress warnings about "enumeration value
5025 // 'xxx' not handled in switch
5033 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5038 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5039 m_isDragging
= FALSE
;
5042 // ------------ Entering or leaving the window
5044 if ( event
.Entering() || event
.Leaving() )
5046 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5050 // ------------ Left button pressed
5052 else if ( event
.LeftDown() )
5054 // don't send a label click event for a hit on the
5055 // edge of the col label - this is probably the user
5056 // wanting to resize the col
5058 if ( XToEdgeOfCol(x
) < 0 )
5062 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5064 if ( !event
.ShiftDown() && !event
.ControlDown() )
5068 if ( event
.ShiftDown() )
5070 m_selection
->SelectBlock( 0,
5071 m_currentCellCoords
.GetCol(),
5072 GetNumberRows() - 1, col
,
5073 event
.ControlDown(),
5080 m_selection
->SelectCol( col
,
5081 event
.ControlDown(),
5088 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5093 // starting to drag-resize a col
5095 if ( CanDragColSize() )
5096 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5101 // ------------ Left double click
5103 if ( event
.LeftDClick() )
5105 int col
= XToEdgeOfCol(x
);
5110 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5112 // no default action at the moment
5117 // adjust column width depending on label text
5118 AutoSizeColLabelSize( col
);
5120 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5126 // ------------ Left button released
5128 else if ( event
.LeftUp() )
5130 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5132 DoEndDragResizeCol();
5134 // Note: we are ending the event *after* doing
5135 // default processing in this case
5137 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5140 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5145 // ------------ Right button down
5147 else if ( event
.RightDown() )
5151 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5153 // no default action at the moment
5158 // ------------ Right double click
5160 else if ( event
.RightDClick() )
5164 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5166 // no default action at the moment
5171 // ------------ No buttons down and mouse moving
5173 else if ( event
.Moving() )
5175 m_dragRowOrCol
= XToEdgeOfCol( x
);
5176 if ( m_dragRowOrCol
>= 0 )
5178 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5180 // don't capture the cursor yet
5181 if ( CanDragColSize() )
5182 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
5185 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5187 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
5193 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5195 if ( event
.LeftDown() )
5197 // indicate corner label by having both row and
5200 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5206 else if ( event
.LeftDClick() )
5208 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5211 else if ( event
.RightDown() )
5213 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5215 // no default action at the moment
5219 else if ( event
.RightDClick() )
5221 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5223 // no default action at the moment
5228 void wxGrid::ChangeCursorMode(CursorMode mode
,
5233 static const wxChar
*cursorModes
[] =
5242 wxLogTrace(_T("grid"),
5243 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5244 win
== m_colLabelWin
? _T("colLabelWin")
5245 : win
? _T("rowLabelWin")
5247 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5248 #endif // __WXDEBUG__
5250 if ( mode
== m_cursorMode
&&
5251 win
== m_winCapture
&&
5252 captureMouse
== (m_winCapture
!= NULL
))
5257 // by default use the grid itself
5263 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5264 m_winCapture
= (wxWindow
*)NULL
;
5267 m_cursorMode
= mode
;
5269 switch ( m_cursorMode
)
5271 case WXGRID_CURSOR_RESIZE_ROW
:
5272 win
->SetCursor( m_rowResizeCursor
);
5275 case WXGRID_CURSOR_RESIZE_COL
:
5276 win
->SetCursor( m_colResizeCursor
);
5280 win
->SetCursor( *wxSTANDARD_CURSOR
);
5283 // we need to capture mouse when resizing
5284 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5285 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5287 if ( captureMouse
&& resize
)
5289 win
->CaptureMouse();
5294 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5297 wxPoint
pos( event
.GetPosition() );
5298 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5300 wxGridCellCoords coords
;
5301 XYToCell( x
, y
, coords
);
5303 int cell_rows
, cell_cols
;
5304 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5305 if ((cell_rows
< 0) || (cell_cols
< 0))
5307 coords
.SetRow(coords
.GetRow() + cell_rows
);
5308 coords
.SetCol(coords
.GetCol() + cell_cols
);
5311 if ( event
.Dragging() )
5313 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5315 // Don't start doing anything until the mouse has been drug at
5316 // least 3 pixels in any direction...
5319 if (m_startDragPos
== wxDefaultPosition
)
5321 m_startDragPos
= pos
;
5324 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5328 m_isDragging
= TRUE
;
5329 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5331 // Hide the edit control, so it
5332 // won't interfer with drag-shrinking.
5333 if ( IsCellEditControlShown() )
5335 HideCellEditControl();
5336 SaveEditControlValue();
5339 // Have we captured the mouse yet?
5342 m_winCapture
= m_gridWin
;
5343 m_winCapture
->CaptureMouse();
5346 if ( coords
!= wxGridNoCellCoords
)
5348 if ( event
.ControlDown() )
5350 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5351 m_selectingKeyboard
= coords
;
5352 HighlightBlock ( m_selectingKeyboard
, coords
);
5356 if ( !IsSelection() )
5358 HighlightBlock( coords
, coords
);
5362 HighlightBlock( m_currentCellCoords
, coords
);
5366 if (! IsVisible(coords
))
5368 MakeCellVisible(coords
);
5369 // TODO: need to introduce a delay or something here. The
5370 // scrolling is way to fast, at least on MSW - also on GTK.
5374 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5376 int cw
, ch
, left
, dummy
;
5377 m_gridWin
->GetClientSize( &cw
, &ch
);
5378 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5380 wxClientDC
dc( m_gridWin
);
5382 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5383 GetRowMinimalHeight(m_dragRowOrCol
) );
5384 dc
.SetLogicalFunction(wxINVERT
);
5385 if ( m_dragLastPos
>= 0 )
5387 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5389 dc
.DrawLine( left
, y
, left
+cw
, y
);
5392 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5394 int cw
, ch
, dummy
, top
;
5395 m_gridWin
->GetClientSize( &cw
, &ch
);
5396 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5398 wxClientDC
dc( m_gridWin
);
5400 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5401 GetColMinimalWidth(m_dragRowOrCol
) );
5402 dc
.SetLogicalFunction(wxINVERT
);
5403 if ( m_dragLastPos
>= 0 )
5405 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5407 dc
.DrawLine( x
, top
, x
, top
+ch
);
5414 m_isDragging
= FALSE
;
5415 m_startDragPos
= wxDefaultPosition
;
5417 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5418 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5421 if ( event
.Entering() || event
.Leaving() )
5423 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5424 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5429 // ------------ Left button pressed
5431 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5433 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5438 if ( !event
.ControlDown() )
5440 if ( event
.ShiftDown() )
5444 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5445 m_currentCellCoords
.GetCol(),
5448 event
.ControlDown(),
5454 else if ( XToEdgeOfCol(x
) < 0 &&
5455 YToEdgeOfRow(y
) < 0 )
5457 DisableCellEditControl();
5458 MakeCellVisible( coords
);
5460 if ( event
.ControlDown() )
5464 m_selection
->ToggleCellSelection( coords
.GetRow(),
5466 event
.ControlDown(),
5471 m_selectingTopLeft
= wxGridNoCellCoords
;
5472 m_selectingBottomRight
= wxGridNoCellCoords
;
5473 m_selectingKeyboard
= coords
;
5477 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5478 SetCurrentCell( coords
);
5481 if ( m_selection
->GetSelectionMode() !=
5482 wxGrid::wxGridSelectCells
)
5484 HighlightBlock( coords
, coords
);
5493 // ------------ Left double click
5495 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5497 DisableCellEditControl();
5499 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5501 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5509 // ------------ Left button released
5511 else if ( event
.LeftUp() )
5513 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5517 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5518 m_winCapture
= NULL
;
5521 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5524 EnableCellEditControl();
5526 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5527 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5528 editor
->StartingClick();
5532 m_waitForSlowClick
= FALSE
;
5534 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5535 m_selectingBottomRight
!= wxGridNoCellCoords
)
5539 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5540 m_selectingTopLeft
.GetCol(),
5541 m_selectingBottomRight
.GetRow(),
5542 m_selectingBottomRight
.GetCol(),
5543 event
.ControlDown(),
5549 m_selectingTopLeft
= wxGridNoCellCoords
;
5550 m_selectingBottomRight
= wxGridNoCellCoords
;
5552 // Show the edit control, if it has been hidden for
5554 ShowCellEditControl();
5557 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5559 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5560 DoEndDragResizeRow();
5562 // Note: we are ending the event *after* doing
5563 // default processing in this case
5565 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5567 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5569 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5570 DoEndDragResizeCol();
5572 // Note: we are ending the event *after* doing
5573 // default processing in this case
5575 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5582 // ------------ Right button down
5584 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5586 DisableCellEditControl();
5587 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5592 // no default action at the moment
5597 // ------------ Right double click
5599 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5601 DisableCellEditControl();
5602 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5607 // no default action at the moment
5611 // ------------ Moving and no button action
5613 else if ( event
.Moving() && !event
.IsButton() )
5615 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5617 // out of grid cell area
5618 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5622 int dragRow
= YToEdgeOfRow( y
);
5623 int dragCol
= XToEdgeOfCol( x
);
5625 // Dragging on the corner of a cell to resize in both
5626 // directions is not implemented yet...
5628 if ( dragRow
>= 0 && dragCol
>= 0 )
5630 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5636 m_dragRowOrCol
= dragRow
;
5638 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5640 if ( CanDragRowSize() && CanDragGridSize() )
5641 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5646 m_dragRowOrCol
= dragCol
;
5654 m_dragRowOrCol
= dragCol
;
5656 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5658 if ( CanDragColSize() && CanDragGridSize() )
5659 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5665 // Neither on a row or col edge
5667 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5669 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5675 void wxGrid::DoEndDragResizeRow()
5677 if ( m_dragLastPos
>= 0 )
5679 // erase the last line and resize the row
5681 int cw
, ch
, left
, dummy
;
5682 m_gridWin
->GetClientSize( &cw
, &ch
);
5683 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5685 wxClientDC
dc( m_gridWin
);
5687 dc
.SetLogicalFunction( wxINVERT
);
5688 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5689 HideCellEditControl();
5690 SaveEditControlValue();
5692 int rowTop
= GetRowTop(m_dragRowOrCol
);
5693 SetRowSize( m_dragRowOrCol
,
5694 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5696 if ( !GetBatchCount() )
5698 // Only needed to get the correct rect.y:
5699 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5701 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5702 rect
.width
= m_rowLabelWidth
;
5703 rect
.height
= ch
- rect
.y
;
5704 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5706 // if there is a multicell block, paint all of it
5709 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5710 int leftCol
= XToCol(left
);
5711 int rightCol
= internalXToCol(left
+cw
);
5714 for (i
=leftCol
; i
<rightCol
; i
++)
5716 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5717 if (cell_rows
< subtract_rows
)
5718 subtract_rows
= cell_rows
;
5720 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5721 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5722 rect
.height
= ch
- rect
.y
;
5725 m_gridWin
->Refresh( FALSE
, &rect
);
5728 ShowCellEditControl();
5733 void wxGrid::DoEndDragResizeCol()
5735 if ( m_dragLastPos
>= 0 )
5737 // erase the last line and resize the col
5739 int cw
, ch
, dummy
, top
;
5740 m_gridWin
->GetClientSize( &cw
, &ch
);
5741 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5743 wxClientDC
dc( m_gridWin
);
5745 dc
.SetLogicalFunction( wxINVERT
);
5746 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5747 HideCellEditControl();
5748 SaveEditControlValue();
5750 int colLeft
= GetColLeft(m_dragRowOrCol
);
5751 SetColSize( m_dragRowOrCol
,
5752 wxMax( m_dragLastPos
- colLeft
,
5753 GetColMinimalWidth(m_dragRowOrCol
) ) );
5755 if ( !GetBatchCount() )
5757 // Only needed to get the correct rect.x:
5758 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5760 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5761 rect
.width
= cw
- rect
.x
;
5762 rect
.height
= m_colLabelHeight
;
5763 m_colLabelWin
->Refresh( TRUE
, &rect
);
5765 // if there is a multicell block, paint all of it
5768 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5769 int topRow
= YToRow(top
);
5770 int bottomRow
= internalYToRow(top
+cw
);
5773 for (i
=topRow
; i
<bottomRow
; i
++)
5775 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5776 if (cell_cols
< subtract_cols
)
5777 subtract_cols
= cell_cols
;
5779 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5780 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5781 rect
.width
= cw
- rect
.x
;
5784 m_gridWin
->Refresh( FALSE
, &rect
);
5787 ShowCellEditControl();
5794 // ------ interaction with data model
5796 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5798 switch ( msg
.GetId() )
5800 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5801 return GetModelValues();
5803 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5804 return SetModelValues();
5806 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5807 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5808 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5809 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5810 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5811 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5812 return Redimension( msg
);
5821 // The behaviour of this function depends on the grid table class
5822 // Clear() function. For the default wxGridStringTable class the
5823 // behavious is to replace all cell contents with wxEmptyString but
5824 // not to change the number of rows or cols.
5826 void wxGrid::ClearGrid()
5830 if (IsCellEditControlEnabled())
5831 DisableCellEditControl();
5834 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5839 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5841 // TODO: something with updateLabels flag
5845 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5851 if (IsCellEditControlEnabled())
5852 DisableCellEditControl();
5854 bool done
= m_table
->InsertRows( pos
, numRows
);
5857 // the table will have sent the results of the insert row
5858 // operation to this view object as a grid table message
5864 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5866 // TODO: something with updateLabels flag
5870 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5876 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5878 // the table will have sent the results of the append row
5879 // operation to this view object as a grid table message
5885 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5887 // TODO: something with updateLabels flag
5891 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5897 if (IsCellEditControlEnabled())
5898 DisableCellEditControl();
5900 bool done
= m_table
->DeleteRows( pos
, numRows
);
5902 // the table will have sent the results of the delete row
5903 // operation to this view object as a grid table message
5909 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5911 // TODO: something with updateLabels flag
5915 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5921 if (IsCellEditControlEnabled())
5922 DisableCellEditControl();
5924 bool done
= m_table
->InsertCols( pos
, numCols
);
5926 // the table will have sent the results of the insert col
5927 // operation to this view object as a grid table message
5933 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5935 // TODO: something with updateLabels flag
5939 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5945 bool done
= m_table
->AppendCols( numCols
);
5947 // the table will have sent the results of the append col
5948 // operation to this view object as a grid table message
5954 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5956 // TODO: something with updateLabels flag
5960 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5966 if (IsCellEditControlEnabled())
5967 DisableCellEditControl();
5969 bool done
= m_table
->DeleteCols( pos
, numCols
);
5971 // the table will have sent the results of the delete col
5972 // operation to this view object as a grid table message
5980 // ----- event handlers
5983 // Generate a grid event based on a mouse event and
5984 // return the result of ProcessEvent()
5986 int wxGrid::SendEvent( const wxEventType type
,
5988 wxMouseEvent
& mouseEv
)
5993 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5995 int rowOrCol
= (row
== -1 ? col
: row
);
5997 wxGridSizeEvent
gridEvt( GetId(),
6001 mouseEv
.GetX() + GetRowLabelSize(),
6002 mouseEv
.GetY() + GetColLabelSize(),
6003 mouseEv
.ControlDown(),
6004 mouseEv
.ShiftDown(),
6006 mouseEv
.MetaDown() );
6008 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6009 vetoed
= !gridEvt
.IsAllowed();
6011 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6013 // Right now, it should _never_ end up here!
6014 wxGridRangeSelectEvent
gridEvt( GetId(),
6018 m_selectingBottomRight
,
6020 mouseEv
.ControlDown(),
6021 mouseEv
.ShiftDown(),
6023 mouseEv
.MetaDown() );
6025 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6026 vetoed
= !gridEvt
.IsAllowed();
6030 wxGridEvent
gridEvt( GetId(),
6034 mouseEv
.GetX() + GetRowLabelSize(),
6035 mouseEv
.GetY() + GetColLabelSize(),
6037 mouseEv
.ControlDown(),
6038 mouseEv
.ShiftDown(),
6040 mouseEv
.MetaDown() );
6041 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6042 vetoed
= !gridEvt
.IsAllowed();
6045 // A Veto'd event may not be `claimed' so test this first
6046 if (vetoed
) return -1;
6047 return claimed
? 1 : 0;
6051 // Generate a grid event of specified type and return the result
6052 // of ProcessEvent().
6054 int wxGrid::SendEvent( const wxEventType type
,
6060 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6062 int rowOrCol
= (row
== -1 ? col
: row
);
6064 wxGridSizeEvent
gridEvt( GetId(),
6069 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6070 vetoed
= !gridEvt
.IsAllowed();
6074 wxGridEvent
gridEvt( GetId(),
6079 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6080 vetoed
= !gridEvt
.IsAllowed();
6083 // A Veto'd event may not be `claimed' so test this first
6084 if (vetoed
) return -1;
6085 return claimed
? 1 : 0;
6089 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6091 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6094 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6096 // Don't do anything if between Begin/EndBatch...
6097 // EndBatch() will do all this on the last nested one anyway.
6098 if (! GetBatchCount())
6100 // Refresh to get correct scrolled position:
6101 wxScrolledWindow::Refresh(eraseb
,rect
);
6105 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6106 int width_label
, width_cell
, height_label
, height_cell
;
6109 //Copy rectangle can get scroll offsets..
6110 rect_x
= rect
->GetX();
6111 rect_y
= rect
->GetY();
6112 rectWidth
= rect
->GetWidth();
6113 rectHeight
= rect
->GetHeight();
6115 width_label
= m_rowLabelWidth
- rect_x
;
6116 if (width_label
> rectWidth
) width_label
= rectWidth
;
6118 height_label
= m_colLabelHeight
- rect_y
;
6119 if (height_label
> rectHeight
) height_label
= rectHeight
;
6121 if (rect_x
> m_rowLabelWidth
)
6123 x
= rect_x
- m_rowLabelWidth
;
6124 width_cell
= rectWidth
;
6129 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6132 if (rect_y
> m_colLabelHeight
)
6134 y
= rect_y
- m_colLabelHeight
;
6135 height_cell
= rectHeight
;
6140 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6143 // Paint corner label part intersecting rect.
6144 if ( width_label
> 0 && height_label
> 0 )
6146 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6147 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6150 // Paint col labels part intersecting rect.
6151 if ( width_cell
> 0 && height_label
> 0 )
6153 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6154 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6157 // Paint row labels part intersecting rect.
6158 if ( width_label
> 0 && height_cell
> 0 )
6160 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6161 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6164 // Paint cell area part intersecting rect.
6165 if ( width_cell
> 0 && height_cell
> 0 )
6167 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6168 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6173 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6174 m_colLabelWin
->Refresh(eraseb
, NULL
);
6175 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6176 m_gridWin
->Refresh(eraseb
, NULL
);
6181 void wxGrid::OnSize( wxSizeEvent
& event
)
6183 // position the child windows
6186 // don't call CalcDimensions() from here, the base class handles the size
6192 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6194 if ( m_inOnKeyDown
)
6196 // shouldn't be here - we are going round in circles...
6198 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6201 m_inOnKeyDown
= TRUE
;
6203 // propagate the event up and see if it gets processed
6205 wxWindow
*parent
= GetParent();
6206 wxKeyEvent
keyEvt( event
);
6207 keyEvt
.SetEventObject( parent
);
6209 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6212 // try local handlers
6214 switch ( event
.GetKeyCode() )
6217 if ( event
.ControlDown() )
6219 MoveCursorUpBlock( event
.ShiftDown() );
6223 MoveCursorUp( event
.ShiftDown() );
6228 if ( event
.ControlDown() )
6230 MoveCursorDownBlock( event
.ShiftDown() );
6234 MoveCursorDown( event
.ShiftDown() );
6239 if ( event
.ControlDown() )
6241 MoveCursorLeftBlock( event
.ShiftDown() );
6245 MoveCursorLeft( event
.ShiftDown() );
6250 if ( event
.ControlDown() )
6252 MoveCursorRightBlock( event
.ShiftDown() );
6256 MoveCursorRight( event
.ShiftDown() );
6261 case WXK_NUMPAD_ENTER
:
6262 if ( event
.ControlDown() )
6264 event
.Skip(); // to let the edit control have the return
6268 if ( GetGridCursorRow() < GetNumberRows()-1 )
6270 MoveCursorDown( event
.ShiftDown() );
6274 // at the bottom of a column
6275 HideCellEditControl();
6276 SaveEditControlValue();
6286 if (event
.ShiftDown())
6288 if ( GetGridCursorCol() > 0 )
6290 MoveCursorLeft( FALSE
);
6295 HideCellEditControl();
6296 SaveEditControlValue();
6301 if ( GetGridCursorCol() < GetNumberCols()-1 )
6303 MoveCursorRight( FALSE
);
6308 HideCellEditControl();
6309 SaveEditControlValue();
6315 if ( event
.ControlDown() )
6317 MakeCellVisible( 0, 0 );
6318 SetCurrentCell( 0, 0 );
6327 if ( event
.ControlDown() )
6329 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6330 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6347 if ( event
.ControlDown() )
6351 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6352 m_currentCellCoords
.GetCol(),
6353 event
.ControlDown(),
6360 if ( !IsEditable() )
6362 MoveCursorRight( FALSE
);
6365 // Otherwise fall through to default
6368 // is it possible to edit the current cell at all?
6369 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6371 // yes, now check whether the cells editor accepts the key
6372 int row
= m_currentCellCoords
.GetRow();
6373 int col
= m_currentCellCoords
.GetCol();
6374 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6375 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6377 // <F2> is special and will always start editing, for
6378 // other keys - ask the editor itself
6379 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6380 || editor
->IsAcceptedKey(event
) )
6382 // ensure cell is visble
6383 MakeCellVisible(row
, col
);
6384 EnableCellEditControl();
6386 // a problem can arise if the cell is not completely
6387 // visible (even after calling MakeCellVisible the
6388 // control is not created and calling StartingKey will
6390 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6402 // let others process char events with modifiers or all
6403 // char events for readonly cells
6410 m_inOnKeyDown
= FALSE
;
6413 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6415 // try local handlers
6417 if ( event
.GetKeyCode() == WXK_SHIFT
)
6419 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6420 m_selectingBottomRight
!= wxGridNoCellCoords
)
6424 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6425 m_selectingTopLeft
.GetCol(),
6426 m_selectingBottomRight
.GetRow(),
6427 m_selectingBottomRight
.GetCol(),
6428 event
.ControlDown(),
6435 m_selectingTopLeft
= wxGridNoCellCoords
;
6436 m_selectingBottomRight
= wxGridNoCellCoords
;
6437 m_selectingKeyboard
= wxGridNoCellCoords
;
6441 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6445 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6447 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6449 // the event has been intercepted - do nothing
6453 wxClientDC
dc(m_gridWin
);
6456 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6458 HideCellEditControl();
6459 DisableCellEditControl();
6461 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6464 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6465 if ( !m_gridLinesEnabled
)
6473 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6475 // Otherwise refresh redraws the highlight!
6476 m_currentCellCoords
= coords
;
6478 DrawGridCellArea(dc
,cells
);
6479 DrawAllGridLines( dc
, r
);
6483 m_currentCellCoords
= coords
;
6485 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6486 DrawCellHighlight(dc
, attr
);
6491 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6494 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6498 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6501 rightCol
= GetNumberCols() - 1;
6503 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6506 bottomRow
= GetNumberRows() - 1;
6510 if ( topRow
> bottomRow
)
6517 if ( leftCol
> rightCol
)
6524 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6525 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6527 // First the case that we selected a completely new area
6528 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6529 m_selectingBottomRight
== wxGridNoCellCoords
)
6532 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6533 wxGridCellCoords ( bottomRow
, rightCol
) );
6534 m_gridWin
->Refresh( FALSE
, &rect
);
6536 // Now handle changing an existing selection area.
6537 else if ( m_selectingTopLeft
!= updateTopLeft
||
6538 m_selectingBottomRight
!= updateBottomRight
)
6540 // Compute two optimal update rectangles:
6541 // Either one rectangle is a real subset of the
6542 // other, or they are (almost) disjoint!
6544 bool need_refresh
[4];
6548 need_refresh
[3] = FALSE
;
6551 // Store intermediate values
6552 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6553 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6554 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6555 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6557 // Determine the outer/inner coordinates.
6558 if (oldLeft
> leftCol
)
6564 if (oldTop
> topRow
)
6570 if (oldRight
< rightCol
)
6573 oldRight
= rightCol
;
6576 if (oldBottom
< bottomRow
)
6579 oldBottom
= bottomRow
;
6583 // Now, either the stuff marked old is the outer
6584 // rectangle or we don't have a situation where one
6585 // is contained in the other.
6587 if ( oldLeft
< leftCol
)
6589 // Refresh the newly selected or deselected
6590 // area to the left of the old or new selection.
6591 need_refresh
[0] = TRUE
;
6592 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6594 wxGridCellCoords ( oldBottom
,
6598 if ( oldTop
< topRow
)
6600 // Refresh the newly selected or deselected
6601 // area above the old or new selection.
6602 need_refresh
[1] = TRUE
;
6603 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6605 wxGridCellCoords ( topRow
- 1,
6609 if ( oldRight
> rightCol
)
6611 // Refresh the newly selected or deselected
6612 // area to the right of the old or new selection.
6613 need_refresh
[2] = TRUE
;
6614 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6616 wxGridCellCoords ( oldBottom
,
6620 if ( oldBottom
> bottomRow
)
6622 // Refresh the newly selected or deselected
6623 // area below the old or new selection.
6624 need_refresh
[3] = TRUE
;
6625 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6627 wxGridCellCoords ( oldBottom
,
6631 // various Refresh() calls
6632 for (i
= 0; i
< 4; i
++ )
6633 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6634 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6637 m_selectingTopLeft
= updateTopLeft
;
6638 m_selectingBottomRight
= updateBottomRight
;
6642 // ------ functions to get/send data (see also public functions)
6645 bool wxGrid::GetModelValues()
6647 // Hide the editor, so it won't hide a changed value.
6648 HideCellEditControl();
6652 // all we need to do is repaint the grid
6654 m_gridWin
->Refresh();
6662 bool wxGrid::SetModelValues()
6666 // Disable the editor, so it won't hide a changed value.
6667 // Do we also want to save the current value of the editor first?
6669 DisableCellEditControl();
6673 for ( row
= 0; row
< m_numRows
; row
++ )
6675 for ( col
= 0; col
< m_numCols
; col
++ )
6677 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6689 // Note - this function only draws cells that are in the list of
6690 // exposed cells (usually set from the update region by
6691 // CalcExposedCells)
6693 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6695 if ( !m_numRows
|| !m_numCols
) return;
6697 int i
, numCells
= cells
.GetCount();
6698 int row
, col
, cell_rows
, cell_cols
;
6699 wxGridCellCoordsArray redrawCells
;
6701 for ( i
= numCells
-1; i
>= 0; i
-- )
6703 row
= cells
[i
].GetRow();
6704 col
= cells
[i
].GetCol();
6705 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6707 // If this cell is part of a multicell block, find owner for repaint
6708 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6710 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6711 bool marked
= FALSE
;
6712 for ( int j
= 0; j
< numCells
; j
++ )
6714 if ( cell
== cells
[j
] )
6722 int count
= redrawCells
.GetCount();
6723 for (int j
= 0; j
< count
; j
++)
6725 if ( cell
== redrawCells
[j
] )
6731 if (!marked
) redrawCells
.Add( cell
);
6733 continue; // don't bother drawing this cell
6736 // If this cell is empty, find cell to left that might want to overflow
6737 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6739 for ( int l
= 0; l
< cell_rows
; l
++ )
6741 // find a cell in this row to left alreay marked for repaint
6743 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6744 if ((redrawCells
[k
].GetCol() < left
) &&
6745 (redrawCells
[k
].GetRow() == row
))
6746 left
=redrawCells
[k
].GetCol();
6748 if (left
== col
) left
= 0; // oh well
6750 for (int j
= col
-1; j
>= left
; j
--)
6752 if (!m_table
->IsEmptyCell(row
+l
, j
))
6754 if (GetCellOverflow(row
+l
, j
))
6756 wxGridCellCoords
cell(row
+l
, j
);
6757 bool marked
= FALSE
;
6759 for (int k
= 0; k
< numCells
; k
++)
6761 if ( cell
== cells
[k
] )
6769 int count
= redrawCells
.GetCount();
6770 for (int k
= 0; k
< count
; k
++)
6772 if ( cell
== redrawCells
[k
] )
6778 if (!marked
) redrawCells
.Add( cell
);
6786 DrawCell( dc
, cells
[i
] );
6789 numCells
= redrawCells
.GetCount();
6791 for ( i
= numCells
- 1; i
>= 0; i
-- )
6793 DrawCell( dc
, redrawCells
[i
] );
6798 void wxGrid::DrawGridSpace( wxDC
& dc
)
6801 m_gridWin
->GetClientSize( &cw
, &ch
);
6804 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6806 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6807 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6809 if ( right
> rightCol
|| bottom
> bottomRow
)
6812 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6814 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6815 dc
.SetPen( *wxTRANSPARENT_PEN
);
6817 if ( right
> rightCol
)
6819 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6822 if ( bottom
> bottomRow
)
6824 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6830 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6832 int row
= coords
.GetRow();
6833 int col
= coords
.GetCol();
6835 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6838 // we draw the cell border ourselves
6839 #if !WXGRID_DRAW_LINES
6840 if ( m_gridLinesEnabled
)
6841 DrawCellBorder( dc
, coords
);
6844 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6846 bool isCurrent
= coords
== m_currentCellCoords
;
6848 wxRect rect
= CellToRect( row
, col
);
6850 // if the editor is shown, we should use it and not the renderer
6851 // Note: However, only if it is really _shown_, i.e. not hidden!
6852 if ( isCurrent
&& IsCellEditControlShown() )
6854 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6855 editor
->PaintBackground(rect
, attr
);
6860 // but all the rest is drawn by the cell renderer and hence may be
6862 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6863 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6870 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6872 int row
= m_currentCellCoords
.GetRow();
6873 int col
= m_currentCellCoords
.GetCol();
6875 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6878 wxRect rect
= CellToRect(row
, col
);
6880 // hmmm... what could we do here to show that the cell is disabled?
6881 // for now, I just draw a thinner border than for the other ones, but
6882 // it doesn't look really good
6884 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6888 // The center of th drawn line is where the position/width/height of
6889 // the rectangle is actually at, (on wxMSW atr least,) so we will
6890 // reduce the size of the rectangle to compensate for the thickness of
6891 // the line. If this is too strange on non wxMSW platforms then
6892 // please #ifdef this appropriately.
6893 rect
.x
+= penWidth
/2;
6894 rect
.y
+= penWidth
/2;
6895 rect
.width
-= penWidth
-1;
6896 rect
.height
-= penWidth
-1;
6899 // Now draw the rectangle
6900 // use the cellHighlightColour if the cell is inside a selection, this
6901 // will ensure the cell is always visible.
6902 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6903 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6904 dc
.DrawRectangle(rect
);
6908 // VZ: my experiments with 3d borders...
6910 // how to properly set colours for arbitrary bg?
6911 wxCoord x1
= rect
.x
,
6913 x2
= rect
.x
+ rect
.width
-1,
6914 y2
= rect
.y
+ rect
.height
-1;
6916 dc
.SetPen(*wxWHITE_PEN
);
6917 dc
.DrawLine(x1
, y1
, x2
, y1
);
6918 dc
.DrawLine(x1
, y1
, x1
, y2
);
6920 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6921 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6923 dc
.SetPen(*wxBLACK_PEN
);
6924 dc
.DrawLine(x1
, y2
, x2
, y2
);
6925 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6930 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6932 int row
= coords
.GetRow();
6933 int col
= coords
.GetCol();
6934 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6937 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6939 wxRect rect
= CellToRect( row
, col
);
6941 // right hand border
6943 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6944 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6948 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6949 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6952 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6954 // This if block was previously in wxGrid::OnPaint but that doesn't
6955 // seem to get called under wxGTK - MB
6957 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6958 m_numRows
&& m_numCols
)
6960 m_currentCellCoords
.Set(0, 0);
6963 if ( IsCellEditControlShown() )
6965 // don't show highlight when the edit control is shown
6969 // if the active cell was repainted, repaint its highlight too because it
6970 // might have been damaged by the grid lines
6971 size_t count
= cells
.GetCount();
6972 for ( size_t n
= 0; n
< count
; n
++ )
6974 if ( cells
[n
] == m_currentCellCoords
)
6976 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6977 DrawCellHighlight(dc
, attr
);
6985 // TODO: remove this ???
6986 // This is used to redraw all grid lines e.g. when the grid line colour
6989 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6991 #if !WXGRID_DRAW_LINES
6995 if ( !m_gridLinesEnabled
||
6997 !m_numCols
) return;
6999 int top
, bottom
, left
, right
;
7001 #if 0 //#ifndef __WXGTK__
7005 m_gridWin
->GetClientSize(&cw
, &ch
);
7007 // virtual coords of visible area
7009 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7010 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7015 reg
.GetBox(x
, y
, w
, h
);
7016 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7017 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7021 m_gridWin
->GetClientSize(&cw
, &ch
);
7022 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7023 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7026 // avoid drawing grid lines past the last row and col
7028 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7029 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7031 // no gridlines inside multicells, clip them out
7032 int leftCol
= internalXToCol(left
);
7033 int topRow
= internalYToRow(top
);
7034 int rightCol
= internalXToCol(right
);
7035 int bottomRow
= internalYToRow(bottom
);
7036 wxRegion
clippedcells(0, 0, cw
, ch
);
7039 int i
, j
, cell_rows
, cell_cols
;
7042 for (j
=topRow
; j
<bottomRow
; j
++)
7044 for (i
=leftCol
; i
<rightCol
; i
++)
7046 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7047 if ((cell_rows
> 1) || (cell_cols
> 1))
7049 rect
= CellToRect(j
,i
);
7050 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7051 clippedcells
.Subtract(rect
);
7053 else if ((cell_rows
< 0) || (cell_cols
< 0))
7055 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7056 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7057 clippedcells
.Subtract(rect
);
7061 dc
.SetClippingRegion( clippedcells
);
7063 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7065 // horizontal grid lines
7067 // already declared above - int i;
7068 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7070 int bot
= GetRowBottom(i
) - 1;
7079 dc
.DrawLine( left
, bot
, right
, bot
);
7084 // vertical grid lines
7086 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7088 int colRight
= GetColRight(i
) - 1;
7089 if ( colRight
> right
)
7094 if ( colRight
>= left
)
7096 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7099 dc
.DestroyClippingRegion();
7103 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7105 if ( !m_numRows
) return;
7108 size_t numLabels
= rows
.GetCount();
7110 for ( i
= 0; i
< numLabels
; i
++ )
7112 DrawRowLabel( dc
, rows
[i
] );
7117 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7119 if ( GetRowHeight(row
) <= 0 )
7122 int rowTop
= GetRowTop(row
),
7123 rowBottom
= GetRowBottom(row
) - 1;
7125 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7126 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7127 m_rowLabelWidth
-1, rowBottom
);
7129 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7131 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7133 dc
.SetPen( *wxWHITE_PEN
);
7134 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7135 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7137 dc
.SetBackgroundMode( wxTRANSPARENT
);
7138 dc
.SetTextForeground( GetLabelTextColour() );
7139 dc
.SetFont( GetLabelFont() );
7142 GetRowLabelAlignment( &hAlign
, &vAlign
);
7146 rect
.SetY( GetRowTop(row
) + 2 );
7147 rect
.SetWidth( m_rowLabelWidth
- 4 );
7148 rect
.SetHeight( GetRowHeight(row
) - 4 );
7149 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7153 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7155 if ( !m_numCols
) return;
7158 size_t numLabels
= cols
.GetCount();
7160 for ( i
= 0; i
< numLabels
; i
++ )
7162 DrawColLabel( dc
, cols
[i
] );
7167 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7169 if ( GetColWidth(col
) <= 0 )
7172 int colLeft
= GetColLeft(col
),
7173 colRight
= GetColRight(col
) - 1;
7175 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7176 dc
.DrawLine( colRight
, 0,
7177 colRight
, m_colLabelHeight
-1 );
7179 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7181 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7182 colRight
+1, m_colLabelHeight
-1 );
7184 dc
.SetPen( *wxWHITE_PEN
);
7185 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7186 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7188 dc
.SetBackgroundMode( wxTRANSPARENT
);
7189 dc
.SetTextForeground( GetLabelTextColour() );
7190 dc
.SetFont( GetLabelFont() );
7192 int hAlign
, vAlign
, orient
;
7193 GetColLabelAlignment( &hAlign
, &vAlign
);
7194 orient
= GetColLabelTextOrientation();
7197 rect
.SetX( colLeft
+ 2 );
7199 rect
.SetWidth( GetColWidth(col
) - 4 );
7200 rect
.SetHeight( m_colLabelHeight
- 4 );
7201 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7204 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7205 const wxString
& value
,
7209 int textOrientation
)
7211 wxArrayString lines
;
7213 StringToLines( value
, lines
);
7216 //Forward to new API.
7217 DrawTextRectangle( dc
,
7226 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7227 const wxArrayString
& lines
,
7231 int textOrientation
)
7233 long textWidth
, textHeight
;
7234 long lineWidth
, lineHeight
;
7237 dc
.SetClippingRegion( rect
);
7239 nLines
= lines
.GetCount();
7243 float x
= 0.0, y
= 0.0;
7245 if( textOrientation
== wxHORIZONTAL
)
7246 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7248 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7252 case wxALIGN_BOTTOM
:
7253 if( textOrientation
== wxHORIZONTAL
)
7254 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7256 x
= rect
.x
+ rect
.width
- textWidth
;
7259 case wxALIGN_CENTRE
:
7260 if( textOrientation
== wxHORIZONTAL
)
7261 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7263 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7268 if( textOrientation
== wxHORIZONTAL
)
7275 // Align each line of a multi-line label
7276 for( l
= 0; l
< nLines
; l
++ )
7278 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7280 switch( horizAlign
)
7283 if( textOrientation
== wxHORIZONTAL
)
7284 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7286 y
= rect
.y
+ lineWidth
+ 1;
7289 case wxALIGN_CENTRE
:
7290 if( textOrientation
== wxHORIZONTAL
)
7291 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7293 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7298 if( textOrientation
== wxHORIZONTAL
)
7301 y
= rect
.y
+ rect
.height
- 1;
7305 if( textOrientation
== wxHORIZONTAL
)
7307 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7312 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7317 dc
.DestroyClippingRegion();
7321 // Split multi line text up into an array of strings. Any existing
7322 // contents of the string array are preserved.
7324 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7328 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7329 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7331 while ( startPos
< (int)tVal
.Length() )
7333 pos
= tVal
.Mid(startPos
).Find( eol
);
7338 else if ( pos
== 0 )
7340 lines
.Add( wxEmptyString
);
7344 lines
.Add( value
.Mid(startPos
, pos
) );
7348 if ( startPos
< (int)value
.Length() )
7350 lines
.Add( value
.Mid( startPos
) );
7355 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7356 const wxArrayString
& lines
,
7357 long *width
, long *height
)
7364 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7366 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7367 w
= wxMax( w
, lineW
);
7376 // ------ Batch processing.
7378 void wxGrid::EndBatch()
7380 if ( m_batchCount
> 0 )
7383 if ( !m_batchCount
)
7386 m_rowLabelWin
->Refresh();
7387 m_colLabelWin
->Refresh();
7388 m_cornerLabelWin
->Refresh();
7389 m_gridWin
->Refresh();
7394 // Use this, rather than wxWindow::Refresh(), to force an immediate
7395 // repainting of the grid. Has no effect if you are already inside a
7396 // BeginBatch / EndBatch block.
7398 void wxGrid::ForceRefresh()
7406 // ------ Edit control functions
7410 void wxGrid::EnableEditing( bool edit
)
7412 // TODO: improve this ?
7414 if ( edit
!= m_editable
)
7416 if(!edit
) EnableCellEditControl(edit
);
7422 void wxGrid::EnableCellEditControl( bool enable
)
7427 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7428 SetCurrentCell( 0, 0 );
7430 if ( enable
!= m_cellEditCtrlEnabled
)
7434 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7437 // this should be checked by the caller!
7438 wxASSERT_MSG( CanEnableCellControl(),
7439 _T("can't enable editing for this cell!") );
7441 // do it before ShowCellEditControl()
7442 m_cellEditCtrlEnabled
= enable
;
7444 ShowCellEditControl();
7448 //FIXME:add veto support
7449 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7451 HideCellEditControl();
7452 SaveEditControlValue();
7454 // do it after HideCellEditControl()
7455 m_cellEditCtrlEnabled
= enable
;
7460 bool wxGrid::IsCurrentCellReadOnly() const
7463 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7464 bool readonly
= attr
->IsReadOnly();
7470 bool wxGrid::CanEnableCellControl() const
7472 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7473 !IsCurrentCellReadOnly();
7477 bool wxGrid::IsCellEditControlEnabled() const
7479 // the cell edit control might be disable for all cells or just for the
7480 // current one if it's read only
7481 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7484 bool wxGrid::IsCellEditControlShown() const
7486 bool isShown
= FALSE
;
7488 if ( m_cellEditCtrlEnabled
)
7490 int row
= m_currentCellCoords
.GetRow();
7491 int col
= m_currentCellCoords
.GetCol();
7492 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7493 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7498 if ( editor
->IsCreated() )
7500 isShown
= editor
->GetControl()->IsShown();
7510 void wxGrid::ShowCellEditControl()
7512 if ( IsCellEditControlEnabled() )
7514 if ( !IsVisible( m_currentCellCoords
) )
7516 m_cellEditCtrlEnabled
= FALSE
;
7521 wxRect rect
= CellToRect( m_currentCellCoords
);
7522 int row
= m_currentCellCoords
.GetRow();
7523 int col
= m_currentCellCoords
.GetCol();
7525 // if this is part of a multicell, find owner (topleft)
7526 int cell_rows
, cell_cols
;
7527 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7528 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7532 m_currentCellCoords
.SetRow( row
);
7533 m_currentCellCoords
.SetCol( col
);
7536 // convert to scrolled coords
7538 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7540 // done in PaintBackground()
7542 // erase the highlight and the cell contents because the editor
7543 // might not cover the entire cell
7544 wxClientDC
dc( m_gridWin
);
7546 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7547 dc
.SetPen(*wxTRANSPARENT_PEN
);
7548 dc
.DrawRectangle(rect
);
7551 // cell is shifted by one pixel
7552 // However, don't allow x or y to become negative
7553 // since the SetSize() method interprets that as
7560 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7561 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7562 if ( !editor
->IsCreated() )
7564 editor
->Create(m_gridWin
, -1,
7565 new wxGridCellEditorEvtHandler(this, editor
));
7567 wxGridEditorCreatedEvent
evt(GetId(),
7568 wxEVT_GRID_EDITOR_CREATED
,
7572 editor
->GetControl());
7573 GetEventHandler()->ProcessEvent(evt
);
7577 // resize editor to overflow into righthand cells if allowed
7578 int maxWidth
= rect
.width
;
7579 wxString value
= GetCellValue(row
, col
);
7580 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7583 GetTextExtent(value
, &maxWidth
, &y
,
7584 NULL
, NULL
, &attr
->GetFont());
7585 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7587 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7588 if (rect
.x
+maxWidth
> client_right
)
7589 maxWidth
= client_right
- rect
.x
;
7591 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7593 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7594 // may have changed earlier
7595 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7598 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7599 // looks weird going over a multicell
7600 if (m_table
->IsEmptyCell(row
,i
) &&
7601 (rect
.width
< maxWidth
) && (c_rows
== 1))
7602 rect
.width
+= GetColWidth(i
);
7606 if (rect
.GetRight() > client_right
)
7607 rect
.SetRight(client_right
-1);
7610 editor
->SetCellAttr(attr
);
7611 editor
->SetSize( rect
);
7612 editor
->Show( TRUE
, attr
);
7614 // recalc dimensions in case we need to
7615 // expand the scrolled window to account for editor
7618 editor
->BeginEdit(row
, col
, this);
7619 editor
->SetCellAttr(NULL
);
7628 void wxGrid::HideCellEditControl()
7630 if ( IsCellEditControlEnabled() )
7632 int row
= m_currentCellCoords
.GetRow();
7633 int col
= m_currentCellCoords
.GetCol();
7635 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7636 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7637 editor
->Show( FALSE
);
7640 m_gridWin
->SetFocus();
7641 // refresh whole row to the right
7642 wxRect
rect( CellToRect(row
, col
) );
7643 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7644 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7645 m_gridWin
->Refresh( FALSE
, &rect
);
7650 void wxGrid::SaveEditControlValue()
7652 if ( IsCellEditControlEnabled() )
7654 int row
= m_currentCellCoords
.GetRow();
7655 int col
= m_currentCellCoords
.GetCol();
7657 wxString oldval
= GetCellValue(row
,col
);
7659 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7660 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7661 bool changed
= editor
->EndEdit(row
, col
, this);
7668 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7669 m_currentCellCoords
.GetRow(),
7670 m_currentCellCoords
.GetCol() ) < 0 ) {
7672 // Event has been vetoed, set the data back.
7673 SetCellValue(row
,col
,oldval
);
7681 // ------ Grid location functions
7682 // Note that all of these functions work with the logical coordinates of
7683 // grid cells and labels so you will need to convert from device
7684 // coordinates for mouse events etc.
7687 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7689 int row
= YToRow(y
);
7690 int col
= XToCol(x
);
7692 if ( row
== -1 || col
== -1 )
7694 coords
= wxGridNoCellCoords
;
7698 coords
.Set( row
, col
);
7703 // Internal Helper function for computing row or column from some
7704 // (unscrolled) coordinate value, using either
7705 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7706 // of m_rowBottoms/m_ColRights to speed up the search!
7708 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7709 const wxArrayInt
& BorderArray
, int nMax
,
7714 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7720 size_t i_max
= coord
/ defaultDist
,
7723 if (BorderArray
.IsEmpty())
7725 if((int) i_max
< nMax
)
7727 return clipToMinMax
? nMax
- 1 : -1;
7730 if ( i_max
>= BorderArray
.GetCount())
7731 i_max
= BorderArray
.GetCount() - 1;
7734 if ( coord
>= BorderArray
[i_max
])
7738 i_max
= coord
/ minDist
;
7740 i_max
= BorderArray
.GetCount() - 1;
7742 if ( i_max
>= BorderArray
.GetCount())
7743 i_max
= BorderArray
.GetCount() - 1;
7745 if ( coord
>= BorderArray
[i_max
])
7746 return clipToMinMax
? (int)i_max
: -1;
7747 if ( coord
< BorderArray
[0] )
7750 while ( i_max
- i_min
> 0 )
7752 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7753 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7754 if (coord
>= BorderArray
[ i_max
- 1])
7758 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7759 if (coord
< BorderArray
[median
])
7767 int wxGrid::YToRow( int y
)
7769 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7770 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, FALSE
);
7774 int wxGrid::XToCol( int x
)
7776 return CoordToRowOrCol(x
, m_defaultColWidth
,
7777 m_minAcceptableColWidth
, m_colRights
, m_numCols
, FALSE
);
7781 // return the row number that that the y coord is near the edge of, or
7782 // -1 if not near an edge
7784 int wxGrid::YToEdgeOfRow( int y
)
7787 i
= internalYToRow(y
);
7789 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7791 // We know that we are in row i, test whether we are
7792 // close enough to lower or upper border, respectively.
7793 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7795 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7803 // return the col number that that the x coord is near the edge of, or
7804 // -1 if not near an edge
7806 int wxGrid::XToEdgeOfCol( int x
)
7809 i
= internalXToCol(x
);
7811 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7813 // We know that we are in column i, test whether we are
7814 // close enough to right or left border, respectively.
7815 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7817 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7825 wxRect
wxGrid::CellToRect( int row
, int col
)
7827 wxRect
rect( -1, -1, -1, -1 );
7829 if ( row
>= 0 && row
< m_numRows
&&
7830 col
>= 0 && col
< m_numCols
)
7832 int i
, cell_rows
, cell_cols
;
7833 rect
.width
= rect
.height
= 0;
7834 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7835 // if negative then find multicell owner
7836 if (cell_rows
< 0) row
+= cell_rows
;
7837 if (cell_cols
< 0) col
+= cell_cols
;
7838 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7840 rect
.x
= GetColLeft(col
);
7841 rect
.y
= GetRowTop(row
);
7842 for (i
=col
; i
<col
+cell_cols
; i
++)
7843 rect
.width
+= GetColWidth(i
);
7844 for (i
=row
; i
<row
+cell_rows
; i
++)
7845 rect
.height
+= GetRowHeight(i
);
7848 // if grid lines are enabled, then the area of the cell is a bit smaller
7849 if (m_gridLinesEnabled
) {
7857 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7859 // get the cell rectangle in logical coords
7861 wxRect
r( CellToRect( row
, col
) );
7863 // convert to device coords
7865 int left
, top
, right
, bottom
;
7866 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7867 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7869 // check against the client area of the grid window
7872 m_gridWin
->GetClientSize( &cw
, &ch
);
7874 if ( wholeCellVisible
)
7876 // is the cell wholly visible ?
7878 return ( left
>= 0 && right
<= cw
&&
7879 top
>= 0 && bottom
<= ch
);
7883 // is the cell partly visible ?
7885 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7886 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7891 // make the specified cell location visible by doing a minimal amount
7894 void wxGrid::MakeCellVisible( int row
, int col
)
7898 int xpos
= -1, ypos
= -1;
7900 if ( row
>= 0 && row
< m_numRows
&&
7901 col
>= 0 && col
< m_numCols
)
7903 // get the cell rectangle in logical coords
7905 wxRect
r( CellToRect( row
, col
) );
7907 // convert to device coords
7909 int left
, top
, right
, bottom
;
7910 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7911 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7914 m_gridWin
->GetClientSize( &cw
, &ch
);
7920 else if ( bottom
> ch
)
7922 int h
= r
.GetHeight();
7924 for ( i
= row
-1; i
>= 0; i
-- )
7926 int rowHeight
= GetRowHeight(i
);
7927 if ( h
+ rowHeight
> ch
)
7934 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7935 // have rounding errors (this is important, because if we do, we
7936 // might not scroll at all and some cells won't be redrawn)
7938 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7940 ypos
+= GRID_SCROLL_LINE_Y
;
7947 else if ( right
> cw
)
7949 // position the view so that the cell is on the right
7951 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
7952 xpos
= x0
+ (right
- cw
);
7954 // see comment for ypos above
7955 xpos
+= GRID_SCROLL_LINE_X
;
7958 if ( xpos
!= -1 || ypos
!= -1 )
7961 xpos
/= GRID_SCROLL_LINE_X
;
7963 ypos
/= GRID_SCROLL_LINE_Y
;
7964 Scroll( xpos
, ypos
);
7972 // ------ Grid cursor movement functions
7975 bool wxGrid::MoveCursorUp( bool expandSelection
)
7977 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7978 m_currentCellCoords
.GetRow() >= 0 )
7980 if ( expandSelection
)
7982 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7983 m_selectingKeyboard
= m_currentCellCoords
;
7984 if ( m_selectingKeyboard
.GetRow() > 0 )
7986 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7987 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7988 m_selectingKeyboard
.GetCol() );
7989 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7992 else if ( m_currentCellCoords
.GetRow() > 0 )
7995 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7996 m_currentCellCoords
.GetCol() );
7997 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7998 m_currentCellCoords
.GetCol() );
8009 bool wxGrid::MoveCursorDown( bool expandSelection
)
8011 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8012 m_currentCellCoords
.GetRow() < m_numRows
)
8014 if ( expandSelection
)
8016 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8017 m_selectingKeyboard
= m_currentCellCoords
;
8018 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8020 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8021 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8022 m_selectingKeyboard
.GetCol() );
8023 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8026 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8029 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8030 m_currentCellCoords
.GetCol() );
8031 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8032 m_currentCellCoords
.GetCol() );
8043 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8045 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8046 m_currentCellCoords
.GetCol() >= 0 )
8048 if ( expandSelection
)
8050 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8051 m_selectingKeyboard
= m_currentCellCoords
;
8052 if ( m_selectingKeyboard
.GetCol() > 0 )
8054 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8055 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8056 m_selectingKeyboard
.GetCol() );
8057 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8060 else if ( m_currentCellCoords
.GetCol() > 0 )
8063 MakeCellVisible( m_currentCellCoords
.GetRow(),
8064 m_currentCellCoords
.GetCol() - 1 );
8065 SetCurrentCell( m_currentCellCoords
.GetRow(),
8066 m_currentCellCoords
.GetCol() - 1 );
8077 bool wxGrid::MoveCursorRight( bool expandSelection
)
8079 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8080 m_currentCellCoords
.GetCol() < m_numCols
)
8082 if ( expandSelection
)
8084 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8085 m_selectingKeyboard
= m_currentCellCoords
;
8086 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8088 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8089 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8090 m_selectingKeyboard
.GetCol() );
8091 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8094 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8097 MakeCellVisible( m_currentCellCoords
.GetRow(),
8098 m_currentCellCoords
.GetCol() + 1 );
8099 SetCurrentCell( m_currentCellCoords
.GetRow(),
8100 m_currentCellCoords
.GetCol() + 1 );
8111 bool wxGrid::MovePageUp()
8113 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8115 int row
= m_currentCellCoords
.GetRow();
8119 m_gridWin
->GetClientSize( &cw
, &ch
);
8121 int y
= GetRowTop(row
);
8122 int newRow
= internalYToRow( y
- ch
+ 1 );
8124 if ( newRow
== row
)
8126 //row > 0 , so newrow can never be less than 0 here.
8130 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8131 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8139 bool wxGrid::MovePageDown()
8141 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8143 int row
= m_currentCellCoords
.GetRow();
8144 if ( (row
+1) < m_numRows
)
8147 m_gridWin
->GetClientSize( &cw
, &ch
);
8149 int y
= GetRowTop(row
);
8150 int newRow
= internalYToRow( y
+ ch
);
8151 if ( newRow
== row
)
8153 // row < m_numRows , so newrow can't overflow here.
8157 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8158 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8166 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8169 m_currentCellCoords
!= wxGridNoCellCoords
&&
8170 m_currentCellCoords
.GetRow() > 0 )
8172 int row
= m_currentCellCoords
.GetRow();
8173 int col
= m_currentCellCoords
.GetCol();
8175 if ( m_table
->IsEmptyCell(row
, col
) )
8177 // starting in an empty cell: find the next block of
8183 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8186 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8188 // starting at the top of a block: find the next block
8194 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8199 // starting within a block: find the top of the block
8204 if ( m_table
->IsEmptyCell(row
, col
) )
8212 MakeCellVisible( row
, col
);
8213 if ( expandSelection
)
8215 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8216 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8221 SetCurrentCell( row
, col
);
8229 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8232 m_currentCellCoords
!= wxGridNoCellCoords
&&
8233 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8235 int row
= m_currentCellCoords
.GetRow();
8236 int col
= m_currentCellCoords
.GetCol();
8238 if ( m_table
->IsEmptyCell(row
, col
) )
8240 // starting in an empty cell: find the next block of
8243 while ( row
< m_numRows
-1 )
8246 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8249 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8251 // starting at the bottom of a block: find the next block
8254 while ( row
< m_numRows
-1 )
8257 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8262 // starting within a block: find the bottom of the block
8264 while ( row
< m_numRows
-1 )
8267 if ( m_table
->IsEmptyCell(row
, col
) )
8275 MakeCellVisible( row
, col
);
8276 if ( expandSelection
)
8278 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8279 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8284 SetCurrentCell( row
, col
);
8293 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8296 m_currentCellCoords
!= wxGridNoCellCoords
&&
8297 m_currentCellCoords
.GetCol() > 0 )
8299 int row
= m_currentCellCoords
.GetRow();
8300 int col
= m_currentCellCoords
.GetCol();
8302 if ( m_table
->IsEmptyCell(row
, col
) )
8304 // starting in an empty cell: find the next block of
8310 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8313 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8315 // starting at the left of a block: find the next block
8321 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8326 // starting within a block: find the left of the block
8331 if ( m_table
->IsEmptyCell(row
, col
) )
8339 MakeCellVisible( row
, col
);
8340 if ( expandSelection
)
8342 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8343 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8348 SetCurrentCell( row
, col
);
8357 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8360 m_currentCellCoords
!= wxGridNoCellCoords
&&
8361 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8363 int row
= m_currentCellCoords
.GetRow();
8364 int col
= m_currentCellCoords
.GetCol();
8366 if ( m_table
->IsEmptyCell(row
, col
) )
8368 // starting in an empty cell: find the next block of
8371 while ( col
< m_numCols
-1 )
8374 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8377 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8379 // starting at the right of a block: find the next block
8382 while ( col
< m_numCols
-1 )
8385 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8390 // starting within a block: find the right of the block
8392 while ( col
< m_numCols
-1 )
8395 if ( m_table
->IsEmptyCell(row
, col
) )
8403 MakeCellVisible( row
, col
);
8404 if ( expandSelection
)
8406 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8407 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8412 SetCurrentCell( row
, col
);
8424 // ------ Label values and formatting
8427 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8429 *horiz
= m_rowLabelHorizAlign
;
8430 *vert
= m_rowLabelVertAlign
;
8433 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8435 *horiz
= m_colLabelHorizAlign
;
8436 *vert
= m_colLabelVertAlign
;
8439 int wxGrid::GetColLabelTextOrientation()
8441 return m_colLabelTextOrientation
;
8444 wxString
wxGrid::GetRowLabelValue( int row
)
8448 return m_table
->GetRowLabelValue( row
);
8458 wxString
wxGrid::GetColLabelValue( int col
)
8462 return m_table
->GetColLabelValue( col
);
8473 void wxGrid::SetRowLabelSize( int width
)
8475 width
= wxMax( width
, 0 );
8476 if ( width
!= m_rowLabelWidth
)
8480 m_rowLabelWin
->Show( FALSE
);
8481 m_cornerLabelWin
->Show( FALSE
);
8483 else if ( m_rowLabelWidth
== 0 )
8485 m_rowLabelWin
->Show( TRUE
);
8486 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8489 m_rowLabelWidth
= width
;
8491 wxScrolledWindow::Refresh( TRUE
);
8496 void wxGrid::SetColLabelSize( int height
)
8498 height
= wxMax( height
, 0 );
8499 if ( height
!= m_colLabelHeight
)
8503 m_colLabelWin
->Show( FALSE
);
8504 m_cornerLabelWin
->Show( FALSE
);
8506 else if ( m_colLabelHeight
== 0 )
8508 m_colLabelWin
->Show( TRUE
);
8509 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8512 m_colLabelHeight
= height
;
8514 wxScrolledWindow::Refresh( TRUE
);
8519 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8521 if ( m_labelBackgroundColour
!= colour
)
8523 m_labelBackgroundColour
= colour
;
8524 m_rowLabelWin
->SetBackgroundColour( colour
);
8525 m_colLabelWin
->SetBackgroundColour( colour
);
8526 m_cornerLabelWin
->SetBackgroundColour( colour
);
8528 if ( !GetBatchCount() )
8530 m_rowLabelWin
->Refresh();
8531 m_colLabelWin
->Refresh();
8532 m_cornerLabelWin
->Refresh();
8537 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8539 if ( m_labelTextColour
!= colour
)
8541 m_labelTextColour
= colour
;
8542 if ( !GetBatchCount() )
8544 m_rowLabelWin
->Refresh();
8545 m_colLabelWin
->Refresh();
8550 void wxGrid::SetLabelFont( const wxFont
& font
)
8553 if ( !GetBatchCount() )
8555 m_rowLabelWin
->Refresh();
8556 m_colLabelWin
->Refresh();
8560 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8562 // allow old (incorrect) defs to be used
8565 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8566 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8567 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8572 case wxTOP
: vert
= wxALIGN_TOP
; break;
8573 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8574 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8577 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8579 m_rowLabelHorizAlign
= horiz
;
8582 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8584 m_rowLabelVertAlign
= vert
;
8587 if ( !GetBatchCount() )
8589 m_rowLabelWin
->Refresh();
8593 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8595 // allow old (incorrect) defs to be used
8598 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8599 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8600 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8605 case wxTOP
: vert
= wxALIGN_TOP
; break;
8606 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8607 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8610 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8612 m_colLabelHorizAlign
= horiz
;
8615 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8617 m_colLabelVertAlign
= vert
;
8620 if ( !GetBatchCount() )
8622 m_colLabelWin
->Refresh();
8626 // Note: under MSW, the default column label font must be changed because it
8627 // does not support vertical printing
8629 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8630 // pGrid->SetLabelFont(font);
8631 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8633 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8635 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8637 m_colLabelTextOrientation
= textOrientation
;
8640 if ( !GetBatchCount() )
8642 m_colLabelWin
->Refresh();
8646 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8650 m_table
->SetRowLabelValue( row
, s
);
8651 if ( !GetBatchCount() )
8653 wxRect rect
= CellToRect( row
, 0);
8654 if ( rect
.height
> 0 )
8656 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8658 rect
.width
= m_rowLabelWidth
;
8659 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8665 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8669 m_table
->SetColLabelValue( col
, s
);
8670 if ( !GetBatchCount() )
8672 wxRect rect
= CellToRect( 0, col
);
8673 if ( rect
.width
> 0 )
8675 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8677 rect
.height
= m_colLabelHeight
;
8678 m_colLabelWin
->Refresh( TRUE
, &rect
);
8684 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8686 if ( m_gridLineColour
!= colour
)
8688 m_gridLineColour
= colour
;
8690 wxClientDC
dc( m_gridWin
);
8692 DrawAllGridLines( dc
, wxRegion() );
8697 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8699 if ( m_cellHighlightColour
!= colour
)
8701 m_cellHighlightColour
= colour
;
8703 wxClientDC
dc( m_gridWin
);
8705 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8706 DrawCellHighlight(dc
, attr
);
8711 void wxGrid::SetCellHighlightPenWidth(int width
)
8713 if (m_cellHighlightPenWidth
!= width
) {
8714 m_cellHighlightPenWidth
= width
;
8716 // Just redrawing the cell highlight is not enough since that won't
8717 // make any visible change if the the thickness is getting smaller.
8718 int row
= m_currentCellCoords
.GetRow();
8719 int col
= m_currentCellCoords
.GetCol();
8720 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8722 wxRect rect
= CellToRect(row
, col
);
8723 m_gridWin
->Refresh(TRUE
, &rect
);
8727 void wxGrid::SetCellHighlightROPenWidth(int width
)
8729 if (m_cellHighlightROPenWidth
!= width
) {
8730 m_cellHighlightROPenWidth
= width
;
8732 // Just redrawing the cell highlight is not enough since that won't
8733 // make any visible change if the the thickness is getting smaller.
8734 int row
= m_currentCellCoords
.GetRow();
8735 int col
= m_currentCellCoords
.GetCol();
8736 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8738 wxRect rect
= CellToRect(row
, col
);
8739 m_gridWin
->Refresh(TRUE
, &rect
);
8743 void wxGrid::EnableGridLines( bool enable
)
8745 if ( enable
!= m_gridLinesEnabled
)
8747 m_gridLinesEnabled
= enable
;
8749 if ( !GetBatchCount() )
8753 wxClientDC
dc( m_gridWin
);
8755 DrawAllGridLines( dc
, wxRegion() );
8759 m_gridWin
->Refresh();
8766 int wxGrid::GetDefaultRowSize()
8768 return m_defaultRowHeight
;
8771 int wxGrid::GetRowSize( int row
)
8773 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8775 return GetRowHeight(row
);
8778 int wxGrid::GetDefaultColSize()
8780 return m_defaultColWidth
;
8783 int wxGrid::GetColSize( int col
)
8785 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8787 return GetColWidth(col
);
8790 // ============================================================================
8791 // access to the grid attributes: each of them has a default value in the grid
8792 // itself and may be overidden on a per-cell basis
8793 // ============================================================================
8795 // ----------------------------------------------------------------------------
8796 // setting default attributes
8797 // ----------------------------------------------------------------------------
8799 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8801 m_defaultCellAttr
->SetBackgroundColour(col
);
8803 m_gridWin
->SetBackgroundColour(col
);
8807 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8809 m_defaultCellAttr
->SetTextColour(col
);
8812 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8814 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8817 void wxGrid::SetDefaultCellOverflow( bool allow
)
8819 m_defaultCellAttr
->SetOverflow(allow
);
8822 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8824 m_defaultCellAttr
->SetFont(font
);
8827 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8829 m_defaultCellAttr
->SetRenderer(renderer
);
8832 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8834 m_defaultCellAttr
->SetEditor(editor
);
8837 // ----------------------------------------------------------------------------
8838 // access to the default attrbiutes
8839 // ----------------------------------------------------------------------------
8841 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8843 return m_defaultCellAttr
->GetBackgroundColour();
8846 wxColour
wxGrid::GetDefaultCellTextColour()
8848 return m_defaultCellAttr
->GetTextColour();
8851 wxFont
wxGrid::GetDefaultCellFont()
8853 return m_defaultCellAttr
->GetFont();
8856 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8858 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8861 bool wxGrid::GetDefaultCellOverflow()
8863 return m_defaultCellAttr
->GetOverflow();
8866 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8868 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8871 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8873 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8876 // ----------------------------------------------------------------------------
8877 // access to cell attributes
8878 // ----------------------------------------------------------------------------
8880 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8882 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8883 wxColour colour
= attr
->GetBackgroundColour();
8888 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8890 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8891 wxColour colour
= attr
->GetTextColour();
8896 wxFont
wxGrid::GetCellFont( int row
, int col
)
8898 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8899 wxFont font
= attr
->GetFont();
8904 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8906 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8907 attr
->GetAlignment(horiz
, vert
);
8911 bool wxGrid::GetCellOverflow( int row
, int col
)
8913 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8914 bool allow
= attr
->GetOverflow();
8919 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8921 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8922 attr
->GetSize( num_rows
, num_cols
);
8926 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8928 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8929 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8935 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8937 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8938 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8944 bool wxGrid::IsReadOnly(int row
, int col
) const
8946 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8947 bool isReadOnly
= attr
->IsReadOnly();
8952 // ----------------------------------------------------------------------------
8953 // attribute support: cache, automatic provider creation, ...
8954 // ----------------------------------------------------------------------------
8956 bool wxGrid::CanHaveAttributes()
8963 return m_table
->CanHaveAttributes();
8966 void wxGrid::ClearAttrCache()
8968 if ( m_attrCache
.row
!= -1 )
8970 wxSafeDecRef(m_attrCache
.attr
);
8971 m_attrCache
.attr
= NULL
;
8972 m_attrCache
.row
= -1;
8976 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8980 wxGrid
*self
= (wxGrid
*)this; // const_cast
8982 self
->ClearAttrCache();
8983 self
->m_attrCache
.row
= row
;
8984 self
->m_attrCache
.col
= col
;
8985 self
->m_attrCache
.attr
= attr
;
8990 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8992 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8994 *attr
= m_attrCache
.attr
;
8995 wxSafeIncRef(m_attrCache
.attr
);
8997 #ifdef DEBUG_ATTR_CACHE
8998 gs_nAttrCacheHits
++;
9005 #ifdef DEBUG_ATTR_CACHE
9006 gs_nAttrCacheMisses
++;
9012 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9014 wxGridCellAttr
*attr
= NULL
;
9015 // Additional test to avoid looking at the cache e.g. for
9016 // wxNoCellCoords, as this will confuse memory management.
9019 if ( !LookupAttr(row
, col
, &attr
) )
9021 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9022 : (wxGridCellAttr
*)NULL
;
9023 CacheAttr(row
, col
, attr
);
9028 attr
->SetDefAttr(m_defaultCellAttr
);
9032 attr
= m_defaultCellAttr
;
9039 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9041 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9043 wxCHECK_MSG( m_table
, attr
,
9044 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
9046 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9049 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9051 // artificially inc the ref count to match DecRef() in caller
9053 m_table
->SetAttr(attr
, row
, col
);
9059 // ----------------------------------------------------------------------------
9060 // setting column attributes (wrappers around SetColAttr)
9061 // ----------------------------------------------------------------------------
9063 void wxGrid::SetColFormatBool(int col
)
9065 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9068 void wxGrid::SetColFormatNumber(int col
)
9070 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9073 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9075 wxString typeName
= wxGRID_VALUE_FLOAT
;
9076 if ( (width
!= -1) || (precision
!= -1) )
9078 typeName
<< _T(':') << width
<< _T(',') << precision
;
9081 SetColFormatCustom(col
, typeName
);
9084 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9086 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9088 attr
= new wxGridCellAttr
;
9089 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9090 attr
->SetRenderer(renderer
);
9092 SetColAttr(col
, attr
);
9096 // ----------------------------------------------------------------------------
9097 // setting cell attributes: this is forwarded to the table
9098 // ----------------------------------------------------------------------------
9100 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9102 if ( CanHaveAttributes() )
9104 m_table
->SetAttr(attr
, row
, col
);
9113 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9115 if ( CanHaveAttributes() )
9117 m_table
->SetRowAttr(attr
, row
);
9126 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9128 if ( CanHaveAttributes() )
9130 m_table
->SetColAttr(attr
, col
);
9139 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9141 if ( CanHaveAttributes() )
9143 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9144 attr
->SetBackgroundColour(colour
);
9149 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9151 if ( CanHaveAttributes() )
9153 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9154 attr
->SetTextColour(colour
);
9159 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9161 if ( CanHaveAttributes() )
9163 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9164 attr
->SetFont(font
);
9169 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9171 if ( CanHaveAttributes() )
9173 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9174 attr
->SetAlignment(horiz
, vert
);
9179 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9181 if ( CanHaveAttributes() )
9183 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9184 attr
->SetOverflow(allow
);
9189 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9191 if ( CanHaveAttributes() )
9193 int cell_rows
, cell_cols
;
9195 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9196 attr
->GetSize(&cell_rows
, &cell_cols
);
9197 attr
->SetSize(num_rows
, num_cols
);
9200 // Cannot set the size of a cell to 0 or negative values
9201 // While it is perfectly legal to do that, this function cannot
9202 // handle all the possibilies, do it by hand by getting the CellAttr.
9203 // You can only set the size of a cell to 1,1 or greater with this fn
9204 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9205 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9206 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9207 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9209 // if this was already a multicell then "turn off" the other cells first
9210 if ((cell_rows
> 1) || (cell_rows
> 1))
9213 for (j
=row
; j
<row
+cell_rows
; j
++)
9215 for (i
=col
; i
<col
+cell_cols
; i
++)
9217 if ((i
!= col
) || (j
!= row
))
9219 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9220 attr_stub
->SetSize( 1, 1 );
9221 attr_stub
->DecRef();
9227 // mark the cells that will be covered by this cell to
9228 // negative or zero values to point back at this cell
9229 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9232 for (j
=row
; j
<row
+num_rows
; j
++)
9234 for (i
=col
; i
<col
+num_cols
; i
++)
9236 if ((i
!= col
) || (j
!= row
))
9238 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9239 attr_stub
->SetSize( row
-j
, col
-i
);
9240 attr_stub
->DecRef();
9248 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9250 if ( CanHaveAttributes() )
9252 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9253 attr
->SetRenderer(renderer
);
9258 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9260 if ( CanHaveAttributes() )
9262 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9263 attr
->SetEditor(editor
);
9268 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9270 if ( CanHaveAttributes() )
9272 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9273 attr
->SetReadOnly(isReadOnly
);
9278 // ----------------------------------------------------------------------------
9279 // Data type registration
9280 // ----------------------------------------------------------------------------
9282 void wxGrid::RegisterDataType(const wxString
& typeName
,
9283 wxGridCellRenderer
* renderer
,
9284 wxGridCellEditor
* editor
)
9286 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9290 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9292 wxString typeName
= m_table
->GetTypeName(row
, col
);
9293 return GetDefaultEditorForType(typeName
);
9296 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9298 wxString typeName
= m_table
->GetTypeName(row
, col
);
9299 return GetDefaultRendererForType(typeName
);
9303 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9305 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9306 if ( index
== wxNOT_FOUND
)
9308 wxFAIL_MSG(wxT("Unknown data type name"));
9313 return m_typeRegistry
->GetEditor(index
);
9317 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9319 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9320 if ( index
== wxNOT_FOUND
)
9322 wxFAIL_MSG(wxT("Unknown data type name"));
9327 return m_typeRegistry
->GetRenderer(index
);
9331 // ----------------------------------------------------------------------------
9333 // ----------------------------------------------------------------------------
9335 void wxGrid::EnableDragRowSize( bool enable
)
9337 m_canDragRowSize
= enable
;
9341 void wxGrid::EnableDragColSize( bool enable
)
9343 m_canDragColSize
= enable
;
9346 void wxGrid::EnableDragGridSize( bool enable
)
9348 m_canDragGridSize
= enable
;
9352 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9354 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9356 if ( resizeExistingRows
)
9358 // since we are resizing all rows to the default row size,
9359 // we can simply clear the row heights and row bottoms
9360 // arrays (which also allows us to take advantage of
9361 // some speed optimisations)
9362 m_rowHeights
.Empty();
9363 m_rowBottoms
.Empty();
9364 if ( !GetBatchCount() )
9369 void wxGrid::SetRowSize( int row
, int height
)
9371 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9373 // See comment in SetColSize
9374 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9376 if ( m_rowHeights
.IsEmpty() )
9378 // need to really create the array
9382 int h
= wxMax( 0, height
);
9383 int diff
= h
- m_rowHeights
[row
];
9385 m_rowHeights
[row
] = h
;
9387 for ( i
= row
; i
< m_numRows
; i
++ )
9389 m_rowBottoms
[i
] += diff
;
9391 if ( !GetBatchCount() )
9395 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9397 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9399 if ( resizeExistingCols
)
9401 // since we are resizing all columns to the default column size,
9402 // we can simply clear the col widths and col rights
9403 // arrays (which also allows us to take advantage of
9404 // some speed optimisations)
9405 m_colWidths
.Empty();
9406 m_colRights
.Empty();
9407 if ( !GetBatchCount() )
9412 void wxGrid::SetColSize( int col
, int width
)
9414 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9416 // should we check that it's bigger than GetColMinimalWidth(col) here?
9418 // No, because it is reasonable to assume the library user know's
9419 // what he is doing. However whe should test against the weaker
9420 // constariant of minimalAcceptableWidth, as this breaks rendering
9422 // This test then fixes sf.net bug #645734
9424 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9426 if ( m_colWidths
.IsEmpty() )
9428 // need to really create the array
9432 // if < 0 calc new width from label
9436 wxArrayString lines
;
9437 wxClientDC
dc(m_colLabelWin
);
9438 dc
.SetFont(GetLabelFont());
9439 StringToLines(GetColLabelValue(col
), lines
);
9440 GetTextBoxSize(dc
, lines
, &w
, &h
);
9443 int w
= wxMax( 0, width
);
9444 int diff
= w
- m_colWidths
[col
];
9445 m_colWidths
[col
] = w
;
9448 for ( i
= col
; i
< m_numCols
; i
++ )
9450 m_colRights
[i
] += diff
;
9452 if ( !GetBatchCount() )
9457 void wxGrid::SetColMinimalWidth( int col
, int width
)
9459 if (width
> GetColMinimalAcceptableWidth()) {
9460 m_colMinWidths
[col
] = width
;
9464 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9466 if (width
> GetRowMinimalAcceptableHeight()) {
9467 m_rowMinHeights
[row
] = width
;
9471 int wxGrid::GetColMinimalWidth(int col
) const
9473 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(col
);
9474 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9477 int wxGrid::GetRowMinimalHeight(int row
) const
9479 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(row
);
9480 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9483 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9485 // We do allow a width of 0 since this gives us
9486 // an easy way to temporarily hidding columns.
9489 m_minAcceptableColWidth
= width
;
9492 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9494 // We do allow a height of 0 since this gives us
9495 // an easy way to temporarily hidding rows.
9498 m_minAcceptableRowHeight
= height
;
9501 int wxGrid::GetColMinimalAcceptableWidth() const
9503 return m_minAcceptableColWidth
;
9506 int wxGrid::GetRowMinimalAcceptableHeight() const
9508 return m_minAcceptableRowHeight
;
9511 // ----------------------------------------------------------------------------
9513 // ----------------------------------------------------------------------------
9515 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9517 wxClientDC
dc(m_gridWin
);
9519 // init both of them to avoid compiler warnings, even if weo nly need one
9527 wxCoord extent
, extentMax
= 0;
9528 int max
= column
? m_numRows
: m_numCols
;
9529 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9536 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9537 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9540 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9541 extent
= column
? size
.x
: size
.y
;
9542 if ( extent
> extentMax
)
9553 // now also compare with the column label extent
9555 dc
.SetFont( GetLabelFont() );
9559 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9560 if( GetColLabelTextOrientation() == wxVERTICAL
)
9564 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9566 extent
= column
? w
: h
;
9567 if ( extent
> extentMax
)
9574 // empty column - give default extent (notice that if extentMax is less
9575 // than default extent but != 0, it's ok)
9576 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9582 // leave some space around text
9593 SetColSize(col
, extentMax
);
9594 if ( !GetBatchCount() )
9597 m_gridWin
->GetClientSize( &cw
, &ch
);
9598 wxRect
rect ( CellToRect( 0, col
) );
9600 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9601 rect
.width
= cw
- rect
.x
;
9602 rect
.height
= m_colLabelHeight
;
9603 m_colLabelWin
->Refresh( TRUE
, &rect
);
9608 SetRowSize(row
, extentMax
);
9609 if ( !GetBatchCount() )
9612 m_gridWin
->GetClientSize( &cw
, &ch
);
9613 wxRect
rect ( CellToRect( row
, 0 ) );
9615 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9616 rect
.width
= m_rowLabelWidth
;
9617 rect
.height
= ch
- rect
.y
;
9618 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9624 SetColMinimalWidth(col
, extentMax
);
9626 SetRowMinimalHeight(row
, extentMax
);
9630 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9632 int width
= m_rowLabelWidth
;
9637 for ( int col
= 0; col
< m_numCols
; col
++ )
9641 AutoSizeColumn(col
, setAsMin
);
9644 width
+= GetColWidth(col
);
9653 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9655 int height
= m_colLabelHeight
;
9660 for ( int row
= 0; row
< m_numRows
; row
++ )
9664 AutoSizeRow(row
, setAsMin
);
9667 height
+= GetRowHeight(row
);
9676 void wxGrid::AutoSize()
9680 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9682 // round up the size to a multiple of scroll step - this ensures that we
9683 // won't get the scrollbars if we're sized exactly to this width
9684 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9686 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9687 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9689 // distribute the extra space between the columns/rows to avoid having
9690 // extra white space
9692 // Remove the extra m_extraWidth + 1 added above
9693 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9694 if ( diff
&& m_numCols
)
9696 // try to resize the columns uniformly
9697 wxCoord diffPerCol
= diff
/ m_numCols
;
9700 for ( int col
= 0; col
< m_numCols
; col
++ )
9702 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9706 // add remaining amount to the last columns
9707 diff
-= diffPerCol
* m_numCols
;
9710 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9712 SetColSize(col
, GetColWidth(col
) + 1);
9718 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9719 if ( diff
&& m_numRows
)
9721 // try to resize the columns uniformly
9722 wxCoord diffPerRow
= diff
/ m_numRows
;
9725 for ( int row
= 0; row
< m_numRows
; row
++ )
9727 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9731 // add remaining amount to the last rows
9732 diff
-= diffPerRow
* m_numRows
;
9735 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9737 SetRowSize(row
, GetRowHeight(row
) + 1);
9744 SetClientSize(sizeFit
);
9747 void wxGrid::AutoSizeRowLabelSize( int row
)
9749 wxArrayString lines
;
9752 // Hide the edit control, so it
9753 // won't interfer with drag-shrinking.
9754 if( IsCellEditControlShown() )
9756 HideCellEditControl();
9757 SaveEditControlValue();
9760 // autosize row height depending on label text
9761 StringToLines( GetRowLabelValue( row
), lines
);
9762 wxClientDC
dc( m_rowLabelWin
);
9763 GetTextBoxSize( dc
, lines
, &w
, &h
);
9764 if( h
< m_defaultRowHeight
)
9765 h
= m_defaultRowHeight
;
9770 void wxGrid::AutoSizeColLabelSize( int col
)
9772 wxArrayString lines
;
9775 // Hide the edit control, so it
9776 // won't interfer with drag-shrinking.
9777 if( IsCellEditControlShown() )
9779 HideCellEditControl();
9780 SaveEditControlValue();
9783 // autosize column width depending on label text
9784 StringToLines( GetColLabelValue( col
), lines
);
9785 wxClientDC
dc( m_colLabelWin
);
9786 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9787 GetTextBoxSize( dc
, lines
, &w
, &h
);
9789 GetTextBoxSize( dc
, lines
, &h
, &w
);
9790 if( w
< m_defaultColWidth
)
9791 w
= m_defaultColWidth
;
9796 wxSize
wxGrid::DoGetBestSize() const
9798 // don't set sizes, only calculate them
9799 wxGrid
*self
= (wxGrid
*)this; // const_cast
9802 width
= self
->SetOrCalcColumnSizes(TRUE
);
9803 height
= self
->SetOrCalcRowSizes(TRUE
);
9805 int maxwidth
, maxheight
;
9806 wxDisplaySize( & maxwidth
, & maxheight
);
9808 if ( width
> maxwidth
) width
= maxwidth
;
9809 if ( height
> maxheight
) height
= maxheight
;
9811 return wxSize( width
, height
);
9820 wxPen
& wxGrid::GetDividerPen() const
9825 // ----------------------------------------------------------------------------
9826 // cell value accessor functions
9827 // ----------------------------------------------------------------------------
9829 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9833 m_table
->SetValue( row
, col
, s
);
9834 if ( !GetBatchCount() )
9837 wxRect
rect( CellToRect( row
, col
) );
9839 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9840 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9841 m_gridWin
->Refresh( FALSE
, &rect
);
9844 if ( m_currentCellCoords
.GetRow() == row
&&
9845 m_currentCellCoords
.GetCol() == col
&&
9846 IsCellEditControlShown())
9847 // Note: If we are using IsCellEditControlEnabled,
9848 // this interacts badly with calling SetCellValue from
9849 // an EVT_GRID_CELL_CHANGE handler.
9851 HideCellEditControl();
9852 ShowCellEditControl(); // will reread data from table
9859 // ------ Block, row and col selection
9862 void wxGrid::SelectRow( int row
, bool addToSelected
)
9864 if ( IsSelection() && !addToSelected
)
9868 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9872 void wxGrid::SelectCol( int col
, bool addToSelected
)
9874 if ( IsSelection() && !addToSelected
)
9878 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9882 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9883 bool addToSelected
)
9885 if ( IsSelection() && !addToSelected
)
9889 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9890 FALSE
, addToSelected
);
9894 void wxGrid::SelectAll()
9896 if ( m_numRows
> 0 && m_numCols
> 0 )
9899 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9904 // ------ Cell, row and col deselection
9907 void wxGrid::DeselectRow( int row
)
9912 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9914 if ( m_selection
->IsInSelection(row
, 0 ) )
9915 m_selection
->ToggleCellSelection( row
, 0);
9919 int nCols
= GetNumberCols();
9920 for ( int i
= 0; i
< nCols
; i
++ )
9922 if ( m_selection
->IsInSelection(row
, i
) )
9923 m_selection
->ToggleCellSelection( row
, i
);
9928 void wxGrid::DeselectCol( int col
)
9933 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9935 if ( m_selection
->IsInSelection(0, col
) )
9936 m_selection
->ToggleCellSelection( 0, col
);
9940 int nRows
= GetNumberRows();
9941 for ( int i
= 0; i
< nRows
; i
++ )
9943 if ( m_selection
->IsInSelection(i
, col
) )
9944 m_selection
->ToggleCellSelection(i
, col
);
9949 void wxGrid::DeselectCell( int row
, int col
)
9951 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9952 m_selection
->ToggleCellSelection(row
, col
);
9955 bool wxGrid::IsSelection()
9957 return ( m_selection
&& (m_selection
->IsSelection() ||
9958 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9959 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9962 bool wxGrid::IsInSelection( int row
, int col
) const
9964 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9965 ( row
>= m_selectingTopLeft
.GetRow() &&
9966 col
>= m_selectingTopLeft
.GetCol() &&
9967 row
<= m_selectingBottomRight
.GetRow() &&
9968 col
<= m_selectingBottomRight
.GetCol() )) );
9971 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9973 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9974 return m_selection
->m_cellSelection
;
9976 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9978 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9979 return m_selection
->m_blockSelectionTopLeft
;
9981 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9983 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9984 return m_selection
->m_blockSelectionBottomRight
;
9986 wxArrayInt
wxGrid::GetSelectedRows() const
9988 if (!m_selection
) { wxArrayInt a
; return a
; }
9989 return m_selection
->m_rowSelection
;
9991 wxArrayInt
wxGrid::GetSelectedCols() const
9993 if (!m_selection
) { wxArrayInt a
; return a
; }
9994 return m_selection
->m_colSelection
;
9998 void wxGrid::ClearSelection()
10000 m_selectingTopLeft
= wxGridNoCellCoords
;
10001 m_selectingBottomRight
= wxGridNoCellCoords
;
10003 m_selection
->ClearSelection();
10007 // This function returns the rectangle that encloses the given block
10008 // in device coords clipped to the client size of the grid window.
10010 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10011 const wxGridCellCoords
&bottomRight
)
10013 wxRect
rect( wxGridNoCellRect
);
10016 cellRect
= CellToRect( topLeft
);
10017 if ( cellRect
!= wxGridNoCellRect
)
10023 rect
= wxRect( 0, 0, 0, 0 );
10026 cellRect
= CellToRect( bottomRight
);
10027 if ( cellRect
!= wxGridNoCellRect
)
10033 return wxGridNoCellRect
;
10037 int left
= rect
.GetLeft();
10038 int top
= rect
.GetTop();
10039 int right
= rect
.GetRight();
10040 int bottom
= rect
.GetBottom();
10042 int leftCol
= topLeft
.GetCol();
10043 int topRow
= topLeft
.GetRow();
10044 int rightCol
= bottomRight
.GetCol();
10045 int bottomRow
= bottomRight
.GetRow();
10063 topRow
= bottomRow
;
10068 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10070 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10072 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10074 cellRect
= CellToRect( j
, i
);
10076 if (cellRect
.x
< left
)
10078 if (cellRect
.y
< top
)
10080 if (cellRect
.x
+ cellRect
.width
> right
)
10081 right
= cellRect
.x
+ cellRect
.width
;
10082 if (cellRect
.y
+ cellRect
.height
> bottom
)
10083 bottom
= cellRect
.y
+ cellRect
.height
;
10085 else i
= rightCol
; // jump over inner cells.
10089 // convert to scrolled coords
10091 CalcScrolledPosition( left
, top
, &left
, &top
);
10092 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10095 m_gridWin
->GetClientSize( &cw
, &ch
);
10097 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10098 return wxRect( 0, 0, 0, 0);
10100 rect
.SetLeft( wxMax(0, left
) );
10101 rect
.SetTop( wxMax(0, top
) );
10102 rect
.SetRight( wxMin(cw
, right
) );
10103 rect
.SetBottom( wxMin(ch
, bottom
) );
10109 // ------ Grid event classes
10112 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10114 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10115 int row
, int col
, int x
, int y
, bool sel
,
10116 bool control
, bool shift
, bool alt
, bool meta
)
10117 : wxNotifyEvent( type
, id
)
10124 m_control
= control
;
10129 SetEventObject(obj
);
10133 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10135 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10136 int rowOrCol
, int x
, int y
,
10137 bool control
, bool shift
, bool alt
, bool meta
)
10138 : wxNotifyEvent( type
, id
)
10140 m_rowOrCol
= rowOrCol
;
10143 m_control
= control
;
10148 SetEventObject(obj
);
10152 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10154 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10155 const wxGridCellCoords
& topLeft
,
10156 const wxGridCellCoords
& bottomRight
,
10157 bool sel
, bool control
,
10158 bool shift
, bool alt
, bool meta
)
10159 : wxNotifyEvent( type
, id
)
10161 m_topLeft
= topLeft
;
10162 m_bottomRight
= bottomRight
;
10164 m_control
= control
;
10169 SetEventObject(obj
);
10173 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10175 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10176 wxObject
* obj
, int row
,
10177 int col
, wxControl
* ctrl
)
10178 : wxCommandEvent(type
, id
)
10180 SetEventObject(obj
);
10186 #endif // wxUSE_GRID