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_NO_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
& );
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_NO_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 Text()->SetValue(m_startValue
);
677 void wxGridCellTextEditor::Reset()
679 wxASSERT_MSG(m_control
,
680 wxT("The wxGridCellEditor must be Created first!"));
682 DoReset(m_startValue
);
685 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
687 Text()->SetValue(startValue
);
688 Text()->SetInsertionPointEnd();
691 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
693 if ( wxGridCellEditor::IsAcceptedKey(event
) )
695 int keycode
= event
.GetKeyCode();
709 case WXK_NUMPAD_MULTIPLY
:
713 case WXK_NUMPAD_SUBTRACT
:
715 case WXK_NUMPAD_DECIMAL
:
717 case WXK_NUMPAD_DIVIDE
:
721 // accept 8 bit chars too if isprint() agrees
722 if ( (keycode
< 255) && (wxIsprint(keycode
)) )
730 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
732 if ( !Text()->EmulateKeyPress(event
) )
738 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
739 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
741 #if defined(__WXMOTIF__) || defined(__WXGTK__)
742 // wxMotif needs a little extra help...
743 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
744 wxString
s( Text()->GetValue() );
745 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
747 Text()->SetInsertionPoint( pos
);
749 // the other ports can handle a Return key press
755 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
765 if ( !params
.ToLong(&tmp
) )
767 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
771 m_maxChars
= (size_t)tmp
;
776 // return the value in the text control
777 wxString
wxGridCellTextEditor::GetValue() const
779 return Text()->GetValue();
782 // ----------------------------------------------------------------------------
783 // wxGridCellNumberEditor
784 // ----------------------------------------------------------------------------
786 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
792 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
794 wxEvtHandler
* evtHandler
)
798 // create a spin ctrl
799 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
800 wxDefaultPosition
, wxDefaultSize
,
804 wxGridCellEditor::Create(parent
, id
, evtHandler
);
808 // just a text control
809 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
812 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
813 #endif // wxUSE_VALIDATORS
817 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
819 // first get the value
820 wxGridTableBase
*table
= grid
->GetTable();
821 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
823 m_valueOld
= table
->GetValueAsLong(row
, col
);
828 wxString sValue
= table
->GetValue(row
, col
);
829 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
831 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
838 Spin()->SetValue((int)m_valueOld
);
843 DoBeginEdit(GetString());
847 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
856 value
= Spin()->GetValue();
857 changed
= value
!= m_valueOld
;
859 text
= wxString::Format(wxT("%ld"), value
);
863 text
= Text()->GetValue();
864 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
869 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
870 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
872 grid
->GetTable()->SetValue(row
, col
, text
);
878 void wxGridCellNumberEditor::Reset()
882 Spin()->SetValue((int)m_valueOld
);
886 DoReset(GetString());
890 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
892 if ( wxGridCellEditor::IsAcceptedKey(event
) )
894 int keycode
= event
.GetKeyCode();
910 case WXK_NUMPAD_SUBTRACT
:
916 if ( (keycode
< 128) && wxIsdigit(keycode
) )
924 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
928 int keycode
= event
.GetKeyCode();
929 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
930 || keycode
== WXK_NUMPAD0
931 || keycode
== WXK_NUMPAD1
932 || keycode
== WXK_NUMPAD2
933 || keycode
== WXK_NUMPAD3
934 || keycode
== WXK_NUMPAD4
935 || keycode
== WXK_NUMPAD5
936 || keycode
== WXK_NUMPAD6
937 || keycode
== WXK_NUMPAD7
938 || keycode
== WXK_NUMPAD8
939 || keycode
== WXK_NUMPAD9
940 || keycode
== WXK_ADD
941 || keycode
== WXK_NUMPAD_ADD
942 || keycode
== WXK_SUBTRACT
943 || keycode
== WXK_NUMPAD_SUBTRACT
)
945 wxGridCellTextEditor::StartingKey(event
);
955 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
966 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
970 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
974 // skip the error message below
979 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
983 // return the value in the spin control if it is there (the text control otherwise)
984 wxString
wxGridCellNumberEditor::GetValue() const
990 long value
= Spin()->GetValue();
991 s
.Printf(wxT("%ld"), value
);
995 s
= Text()->GetValue();
1000 // ----------------------------------------------------------------------------
1001 // wxGridCellFloatEditor
1002 // ----------------------------------------------------------------------------
1004 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1007 m_precision
= precision
;
1010 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1012 wxEvtHandler
* evtHandler
)
1014 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1016 #if wxUSE_VALIDATORS
1017 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1018 #endif // wxUSE_VALIDATORS
1021 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1023 // first get the value
1024 wxGridTableBase
*table
= grid
->GetTable();
1025 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1027 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1032 wxString sValue
= table
->GetValue(row
, col
);
1033 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1035 wxFAIL_MSG( _T("this cell doesn't have float value") );
1040 DoBeginEdit(GetString());
1043 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1047 wxString
text(Text()->GetValue());
1049 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1051 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1052 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1054 grid
->GetTable()->SetValue(row
, col
, text
);
1061 void wxGridCellFloatEditor::Reset()
1063 DoReset(GetString());
1066 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1068 int keycode
= event
.GetKeyCode();
1069 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1070 || keycode
== WXK_NUMPAD0
1071 || keycode
== WXK_NUMPAD1
1072 || keycode
== WXK_NUMPAD2
1073 || keycode
== WXK_NUMPAD3
1074 || keycode
== WXK_NUMPAD4
1075 || keycode
== WXK_NUMPAD5
1076 || keycode
== WXK_NUMPAD6
1077 || keycode
== WXK_NUMPAD7
1078 || keycode
== WXK_NUMPAD8
1079 || keycode
== WXK_NUMPAD9
1080 || keycode
== WXK_ADD
1081 || keycode
== WXK_NUMPAD_ADD
1082 || keycode
== WXK_SUBTRACT
1083 || keycode
== WXK_NUMPAD_SUBTRACT
)
1085 wxGridCellTextEditor::StartingKey(event
);
1087 // skip Skip() below
1094 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1105 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1109 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1111 m_precision
= (int)tmp
;
1113 // skip the error message below
1118 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1122 wxString
wxGridCellFloatEditor::GetString() const
1125 if ( m_width
== -1 )
1127 // default width/precision
1130 else if ( m_precision
== -1 )
1132 // default precision
1133 fmt
.Printf(_T("%%%d.f"), m_width
);
1137 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1140 return wxString::Format(fmt
, m_valueOld
);
1143 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1145 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1147 int keycode
= event
.GetKeyCode();
1161 case WXK_NUMPAD_ADD
:
1163 case WXK_NUMPAD_SUBTRACT
:
1165 case WXK_NUMPAD_DECIMAL
:
1169 // additionally accept 'e' as in '1e+6'
1170 if ( (keycode
< 128) &&
1171 (wxIsdigit(keycode
) || tolower(keycode
) == 'e') )
1179 #endif // wxUSE_TEXTCTRL
1183 // ----------------------------------------------------------------------------
1184 // wxGridCellBoolEditor
1185 // ----------------------------------------------------------------------------
1187 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1189 wxEvtHandler
* evtHandler
)
1191 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1192 wxDefaultPosition
, wxDefaultSize
,
1195 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1198 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1200 bool resize
= FALSE
;
1201 wxSize size
= m_control
->GetSize();
1202 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1204 // check if the checkbox is not too big/small for this cell
1205 wxSize sizeBest
= m_control
->GetBestSize();
1206 if ( !(size
== sizeBest
) )
1208 // reset to default size if it had been made smaller
1214 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1216 // leave 1 pixel margin
1217 size
.x
= size
.y
= minSize
- 2;
1224 m_control
->SetSize(size
);
1227 // position it in the centre of the rectangle (TODO: support alignment?)
1229 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1230 // the checkbox without label still has some space to the right in wxGTK,
1231 // so shift it to the right
1233 #elif defined(__WXMSW__)
1234 // here too, but in other way
1239 int hAlign
= wxALIGN_CENTRE
;
1240 int vAlign
= wxALIGN_CENTRE
;
1242 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1245 if (hAlign
== wxALIGN_LEFT
)
1251 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1253 else if (hAlign
== wxALIGN_RIGHT
)
1255 x
= r
.x
+ r
.width
- size
.x
- 2;
1256 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1258 else if (hAlign
== wxALIGN_CENTRE
)
1260 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1261 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1264 m_control
->Move(x
, y
);
1267 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1269 m_control
->Show(show
);
1273 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1274 CBox()->SetBackgroundColour(colBg
);
1278 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1280 wxASSERT_MSG(m_control
,
1281 wxT("The wxGridCellEditor must be Created first!"));
1283 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1284 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1287 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1288 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1290 CBox()->SetValue(m_startValue
);
1294 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1297 wxASSERT_MSG(m_control
,
1298 wxT("The wxGridCellEditor must be Created first!"));
1300 bool changed
= FALSE
;
1301 bool value
= CBox()->GetValue();
1302 if ( value
!= m_startValue
)
1307 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1308 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1310 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1316 void wxGridCellBoolEditor::Reset()
1318 wxASSERT_MSG(m_control
,
1319 wxT("The wxGridCellEditor must be Created first!"));
1321 CBox()->SetValue(m_startValue
);
1324 void wxGridCellBoolEditor::StartingClick()
1326 CBox()->SetValue(!CBox()->GetValue());
1329 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1331 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1333 int keycode
= event
.GetKeyCode();
1337 case WXK_NUMPAD_MULTIPLY
:
1339 case WXK_NUMPAD_ADD
:
1341 case WXK_NUMPAD_SUBTRACT
:
1352 // return the value as "1" for true and the empty string for false
1353 wxString
wxGridCellBoolEditor::GetValue() const
1355 bool bSet
= CBox()->GetValue();
1356 return bSet
? _T("1") : wxEmptyString
;
1359 #endif // wxUSE_CHECKBOX
1363 // ----------------------------------------------------------------------------
1364 // wxGridCellChoiceEditor
1365 // ----------------------------------------------------------------------------
1367 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1368 const wxString choices
[],
1370 : m_allowOthers(allowOthers
)
1374 m_choices
.Alloc(count
);
1375 for ( size_t n
= 0; n
< count
; n
++ )
1377 m_choices
.Add(choices
[n
]);
1382 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1384 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1385 editor
->m_allowOthers
= m_allowOthers
;
1386 editor
->m_choices
= m_choices
;
1391 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1393 wxEvtHandler
* evtHandler
)
1395 size_t count
= m_choices
.GetCount();
1396 wxString
*choices
= new wxString
[count
];
1397 for ( size_t n
= 0; n
< count
; n
++ )
1399 choices
[n
] = m_choices
[n
];
1402 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1403 wxDefaultPosition
, wxDefaultSize
,
1405 m_allowOthers
? 0 : wxCB_READONLY
);
1409 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1412 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1413 wxGridCellAttr
* attr
)
1415 // as we fill the entire client area, don't do anything here to minimize
1418 // TODO: It doesn't actually fill the client area since the height of a
1419 // combo always defaults to the standard... Until someone has time to
1420 // figure out the right rectangle to paint, just do it the normal way...
1421 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1424 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1426 wxASSERT_MSG(m_control
,
1427 wxT("The wxGridCellEditor must be Created first!"));
1429 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1432 Combo()->SetValue(m_startValue
);
1435 // find the right position, or default to the first if not found
1436 int pos
= Combo()->FindString(m_startValue
);
1439 Combo()->SetSelection(pos
);
1441 Combo()->SetInsertionPointEnd();
1442 Combo()->SetFocus();
1445 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1448 wxString value
= Combo()->GetValue();
1449 bool changed
= value
!= m_startValue
;
1452 grid
->GetTable()->SetValue(row
, col
, value
);
1454 m_startValue
= wxEmptyString
;
1456 Combo()->SetValue(m_startValue
);
1458 Combo()->SetSelection(0);
1463 void wxGridCellChoiceEditor::Reset()
1465 Combo()->SetValue(m_startValue
);
1466 Combo()->SetInsertionPointEnd();
1469 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1479 wxStringTokenizer
tk(params
, _T(','));
1480 while ( tk
.HasMoreTokens() )
1482 m_choices
.Add(tk
.GetNextToken());
1486 // return the value in the text control
1487 wxString
wxGridCellChoiceEditor::GetValue() const
1489 return Combo()->GetValue();
1492 #endif // wxUSE_COMBOBOX
1494 // ----------------------------------------------------------------------------
1495 // wxGridCellEditorEvtHandler
1496 // ----------------------------------------------------------------------------
1498 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1500 switch ( event
.GetKeyCode() )
1504 m_grid
->DisableCellEditControl();
1508 m_grid
->GetEventHandler()->ProcessEvent( event
);
1512 case WXK_NUMPAD_ENTER
:
1513 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1514 m_editor
->HandleReturn(event
);
1523 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1525 switch ( event
.GetKeyCode() )
1530 case WXK_NUMPAD_ENTER
:
1538 // ----------------------------------------------------------------------------
1539 // wxGridCellWorker is an (almost) empty common base class for
1540 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1541 // ----------------------------------------------------------------------------
1543 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1548 wxGridCellWorker::~wxGridCellWorker()
1552 // ============================================================================
1554 // ============================================================================
1556 // ----------------------------------------------------------------------------
1557 // wxGridCellRenderer
1558 // ----------------------------------------------------------------------------
1560 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1561 wxGridCellAttr
& attr
,
1564 int WXUNUSED(row
), int WXUNUSED(col
),
1567 dc
.SetBackgroundMode( wxSOLID
);
1569 // grey out fields if the grid is disabled
1570 if( grid
.IsEnabled() )
1574 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1578 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1583 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1586 dc
.SetPen( *wxTRANSPARENT_PEN
);
1587 dc
.DrawRectangle(rect
);
1590 // ----------------------------------------------------------------------------
1591 // wxGridCellStringRenderer
1592 // ----------------------------------------------------------------------------
1594 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1595 wxGridCellAttr
& attr
,
1599 dc
.SetBackgroundMode( wxTRANSPARENT
);
1601 // TODO some special colours for attr.IsReadOnly() case?
1603 // different coloured text when the grid is disabled
1604 if( grid
.IsEnabled() )
1608 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1609 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1613 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1614 dc
.SetTextForeground( attr
.GetTextColour() );
1619 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1620 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1623 dc
.SetFont( attr
.GetFont() );
1626 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1628 const wxString
& text
)
1630 wxCoord x
= 0, y
= 0, max_x
= 0;
1631 dc
.SetFont(attr
.GetFont());
1632 wxStringTokenizer
tk(text
, _T('\n'));
1633 while ( tk
.HasMoreTokens() )
1635 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1636 max_x
= wxMax(max_x
, x
);
1639 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1641 return wxSize(max_x
, y
);
1644 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1645 wxGridCellAttr
& attr
,
1649 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1652 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1653 wxGridCellAttr
& attr
,
1655 const wxRect
& rectCell
,
1659 wxRect rect
= rectCell
;
1662 // erase only this cells background, overflow cells should have been erased
1663 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1666 attr
.GetAlignment(&hAlign
, &vAlign
);
1668 int overflowCols
= 0;
1670 if (attr
.GetOverflow())
1672 int cols
= grid
.GetNumberCols();
1673 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1674 int cell_rows
, cell_cols
;
1675 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1676 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1678 int i
, c_cols
, c_rows
;
1679 for (i
= col
+cell_cols
; i
< cols
; i
++)
1681 bool is_empty
= TRUE
;
1682 for (int j
=row
; j
<row
+cell_rows
; j
++)
1684 // check w/ anchor cell for multicell block
1685 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1686 if (c_rows
> 0) c_rows
= 0;
1687 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1694 rect
.width
+= grid
.GetColSize(i
);
1700 if (rect
.width
>= best_width
) break;
1702 overflowCols
= i
- col
- cell_cols
+ 1;
1703 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1706 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1708 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1710 clip
.x
+= rectCell
.width
;
1711 // draw each overflow cell individually
1712 int col_end
= col
+cell_cols
+overflowCols
;
1713 if (col_end
>= grid
.GetNumberCols())
1714 col_end
= grid
.GetNumberCols() - 1;
1715 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1717 clip
.width
= grid
.GetColSize(i
) - 1;
1718 dc
.DestroyClippingRegion();
1719 dc
.SetClippingRegion(clip
);
1721 SetTextColoursAndFont(grid
, attr
, dc
,
1722 grid
.IsInSelection(row
,i
));
1724 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1725 rect
, hAlign
, vAlign
);
1726 clip
.x
+= grid
.GetColSize(i
) - 1;
1732 dc
.DestroyClippingRegion();
1736 // now we only have to draw the text
1737 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1739 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1740 rect
, hAlign
, vAlign
);
1743 // ----------------------------------------------------------------------------
1744 // wxGridCellNumberRenderer
1745 // ----------------------------------------------------------------------------
1747 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1749 wxGridTableBase
*table
= grid
.GetTable();
1751 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1753 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1757 text
= table
->GetValue(row
, col
);
1763 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1764 wxGridCellAttr
& attr
,
1766 const wxRect
& rectCell
,
1770 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1772 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1774 // draw the text right aligned by default
1776 attr
.GetAlignment(&hAlign
, &vAlign
);
1777 hAlign
= wxALIGN_RIGHT
;
1779 wxRect rect
= rectCell
;
1782 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1785 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1786 wxGridCellAttr
& attr
,
1790 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1793 // ----------------------------------------------------------------------------
1794 // wxGridCellFloatRenderer
1795 // ----------------------------------------------------------------------------
1797 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1800 SetPrecision(precision
);
1803 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1805 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1806 renderer
->m_width
= m_width
;
1807 renderer
->m_precision
= m_precision
;
1808 renderer
->m_format
= m_format
;
1813 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1815 wxGridTableBase
*table
= grid
.GetTable();
1820 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1822 val
= table
->GetValueAsDouble(row
, col
);
1827 text
= table
->GetValue(row
, col
);
1828 hasDouble
= text
.ToDouble(&val
);
1835 if ( m_width
== -1 )
1837 if ( m_precision
== -1 )
1839 // default width/precision
1840 m_format
= _T("%f");
1844 m_format
.Printf(_T("%%.%df"), m_precision
);
1847 else if ( m_precision
== -1 )
1849 // default precision
1850 m_format
.Printf(_T("%%%d.f"), m_width
);
1854 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1858 text
.Printf(m_format
, val
);
1861 //else: text already contains the string
1866 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1867 wxGridCellAttr
& attr
,
1869 const wxRect
& rectCell
,
1873 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1875 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1877 // draw the text right aligned by default
1879 attr
.GetAlignment(&hAlign
, &vAlign
);
1880 hAlign
= wxALIGN_RIGHT
;
1882 wxRect rect
= rectCell
;
1885 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1888 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1889 wxGridCellAttr
& attr
,
1893 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1896 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1900 // reset to defaults
1906 wxString tmp
= params
.BeforeFirst(_T(','));
1910 if ( tmp
.ToLong(&width
) )
1912 SetWidth((int)width
);
1916 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1920 tmp
= params
.AfterFirst(_T(','));
1924 if ( tmp
.ToLong(&precision
) )
1926 SetPrecision((int)precision
);
1930 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1938 // ----------------------------------------------------------------------------
1939 // wxGridCellBoolRenderer
1940 // ----------------------------------------------------------------------------
1942 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1944 // FIXME these checkbox size calculations are really ugly...
1946 // between checkmark and box
1947 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1949 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1950 wxGridCellAttr
& WXUNUSED(attr
),
1955 // compute it only once (no locks for MT safeness in GUI thread...)
1956 if ( !ms_sizeCheckMark
.x
)
1958 // get checkbox size
1959 wxCoord checkSize
= 0;
1960 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1961 wxSize size
= checkbox
->GetBestSize();
1962 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1964 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1965 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1966 checkSize
-= size
.y
/ 2;
1971 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1974 return ms_sizeCheckMark
;
1977 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1978 wxGridCellAttr
& attr
,
1984 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1986 // draw a check mark in the centre (ignoring alignment - TODO)
1987 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1989 // don't draw outside the cell
1990 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1991 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1993 // and even leave (at least) 1 pixel margin
1994 size
.x
= size
.y
= minSize
- 2;
1997 // draw a border around checkmark
1999 attr
.GetAlignment(& hAlign
, &vAlign
);
2002 if (hAlign
== wxALIGN_CENTRE
)
2004 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2005 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2006 rectBorder
.width
= size
.x
;
2007 rectBorder
.height
= size
.y
;
2009 else if (hAlign
== wxALIGN_LEFT
)
2011 rectBorder
.x
= rect
.x
+ 2;
2012 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2013 rectBorder
.width
= size
.x
;
2014 rectBorder
.height
= size
.y
;
2016 else if (hAlign
== wxALIGN_RIGHT
)
2018 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2019 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2020 rectBorder
.width
= size
.x
;
2021 rectBorder
.height
= size
.y
;
2025 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2026 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2029 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2030 value
= !( !cellval
|| (cellval
== wxT("0")) );
2035 wxRect rectMark
= rectBorder
;
2037 // MSW DrawCheckMark() is weird (and should probably be changed...)
2038 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2042 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2045 dc
.SetTextForeground(attr
.GetTextColour());
2046 dc
.DrawCheckMark(rectMark
);
2049 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2050 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2051 dc
.DrawRectangle(rectBorder
);
2054 // ----------------------------------------------------------------------------
2056 // ----------------------------------------------------------------------------
2058 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2062 m_isReadOnly
= Unset
;
2067 m_attrkind
= wxGridCellAttr::Cell
;
2069 m_sizeRows
= m_sizeCols
= 1;
2070 m_overflow
= UnsetOverflow
;
2072 SetDefAttr(attrDefault
);
2075 wxGridCellAttr
*wxGridCellAttr::Clone() const
2077 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2079 if ( HasTextColour() )
2080 attr
->SetTextColour(GetTextColour());
2081 if ( HasBackgroundColour() )
2082 attr
->SetBackgroundColour(GetBackgroundColour());
2084 attr
->SetFont(GetFont());
2085 if ( HasAlignment() )
2086 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2088 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2092 attr
->SetRenderer(m_renderer
);
2093 m_renderer
->IncRef();
2097 attr
->SetEditor(m_editor
);
2102 attr
->SetReadOnly();
2104 attr
->SetKind( m_attrkind
);
2109 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2111 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2112 SetTextColour(mergefrom
->GetTextColour());
2113 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2114 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2115 if ( !HasFont() && mergefrom
->HasFont() )
2116 SetFont(mergefrom
->GetFont());
2117 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2119 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2120 SetAlignment(hAlign
, vAlign
);
2123 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2125 // Directly access member functions as GetRender/Editor don't just return
2126 // m_renderer/m_editor
2128 // Maybe add support for merge of Render and Editor?
2129 if (!HasRenderer() && mergefrom
->HasRenderer() )
2131 m_renderer
= mergefrom
->m_renderer
;
2132 m_renderer
->IncRef();
2134 if ( !HasEditor() && mergefrom
->HasEditor() )
2136 m_editor
= mergefrom
->m_editor
;
2139 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2140 SetReadOnly(mergefrom
->IsReadOnly());
2142 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2143 SetOverflow(mergefrom
->GetOverflow());
2145 SetDefAttr(mergefrom
->m_defGridAttr
);
2148 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2150 // The size of a cell is normally 1,1
2152 // If this cell is larger (2,2) then this is the top left cell
2153 // the other cells that will be covered (lower right cells) must be
2154 // set to negative or zero values such that
2155 // row + num_rows of the covered cell points to the larger cell (this cell)
2156 // same goes for the col + num_cols.
2158 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2160 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2161 !((num_rows
<=0)&&(num_cols
>0)) ||
2162 !((num_rows
==0)&&(num_cols
==0))),
2163 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2165 m_sizeRows
= num_rows
;
2166 m_sizeCols
= num_cols
;
2169 const wxColour
& wxGridCellAttr::GetTextColour() const
2171 if (HasTextColour())
2175 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2177 return m_defGridAttr
->GetTextColour();
2181 wxFAIL_MSG(wxT("Missing default cell attribute"));
2182 return wxNullColour
;
2187 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2189 if (HasBackgroundColour())
2191 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2192 return m_defGridAttr
->GetBackgroundColour();
2195 wxFAIL_MSG(wxT("Missing default cell attribute"));
2196 return wxNullColour
;
2201 const wxFont
& wxGridCellAttr::GetFont() const
2205 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2206 return m_defGridAttr
->GetFont();
2209 wxFAIL_MSG(wxT("Missing default cell attribute"));
2215 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2219 if ( hAlign
) *hAlign
= m_hAlign
;
2220 if ( vAlign
) *vAlign
= m_vAlign
;
2222 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2223 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2226 wxFAIL_MSG(wxT("Missing default cell attribute"));
2230 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2232 if ( num_rows
) *num_rows
= m_sizeRows
;
2233 if ( num_cols
) *num_cols
= m_sizeCols
;
2236 // GetRenderer and GetEditor use a slightly different decision path about
2237 // which attribute to use. If a non-default attr object has one then it is
2238 // used, otherwise the default editor or renderer is fetched from the grid and
2239 // used. It should be the default for the data type of the cell. If it is
2240 // NULL (because the table has a type that the grid does not have in its
2241 // registry,) then the grid's default editor or renderer is used.
2243 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2245 wxGridCellRenderer
*renderer
;
2247 if ( m_renderer
&& this != m_defGridAttr
)
2249 // use the cells renderer if it has one
2250 renderer
= m_renderer
;
2253 else // no non default cell renderer
2255 // get default renderer for the data type
2258 // GetDefaultRendererForCell() will do IncRef() for us
2259 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2268 if (m_defGridAttr
&& this != m_defGridAttr
)
2270 // if we still don't have one then use the grid default
2271 // (no need for IncRef() here neither)
2272 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2274 else // default grid attr
2276 // use m_renderer which we had decided not to use initially
2277 renderer
= m_renderer
;
2284 // we're supposed to always find something
2285 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2290 // same as above, except for s/renderer/editor/g
2291 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2293 wxGridCellEditor
*editor
;
2295 if ( m_editor
&& this != m_defGridAttr
)
2297 // use the cells editor if it has one
2301 else // no non default cell editor
2303 // get default editor for the data type
2306 // GetDefaultEditorForCell() will do IncRef() for us
2307 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2316 if ( m_defGridAttr
&& this != m_defGridAttr
)
2318 // if we still don't have one then use the grid default
2319 // (no need for IncRef() here neither)
2320 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2322 else // default grid attr
2324 // use m_editor which we had decided not to use initially
2332 // we're supposed to always find something
2333 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2338 // ----------------------------------------------------------------------------
2339 // wxGridCellAttrData
2340 // ----------------------------------------------------------------------------
2342 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2344 int n
= FindIndex(row
, col
);
2345 if ( n
== wxNOT_FOUND
)
2347 // add the attribute
2348 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2352 // free the old attribute
2353 m_attrs
[(size_t)n
].attr
->DecRef();
2357 // change the attribute
2358 m_attrs
[(size_t)n
].attr
= attr
;
2362 // remove this attribute
2363 m_attrs
.RemoveAt((size_t)n
);
2368 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2370 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2372 int n
= FindIndex(row
, col
);
2373 if ( n
!= wxNOT_FOUND
)
2375 attr
= m_attrs
[(size_t)n
].attr
;
2382 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2384 size_t count
= m_attrs
.GetCount();
2385 for ( size_t n
= 0; n
< count
; n
++ )
2387 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2388 wxCoord row
= coords
.GetRow();
2389 if ((size_t)row
>= pos
)
2393 // If rows inserted, include row counter where necessary
2394 coords
.SetRow(row
+ numRows
);
2396 else if (numRows
< 0)
2398 // If rows deleted ...
2399 if ((size_t)row
>= pos
- numRows
)
2401 // ...either decrement row counter (if row still exists)...
2402 coords
.SetRow(row
+ numRows
);
2406 // ...or remove the attribute
2407 m_attrs
.RemoveAt((size_t)n
);
2415 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2417 size_t count
= m_attrs
.GetCount();
2418 for ( size_t n
= 0; n
< count
; n
++ )
2420 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2421 wxCoord col
= coords
.GetCol();
2422 if ( (size_t)col
>= pos
)
2426 // If rows inserted, include row counter where necessary
2427 coords
.SetCol(col
+ numCols
);
2429 else if (numCols
< 0)
2431 // If rows deleted ...
2432 if ((size_t)col
>= pos
- numCols
)
2434 // ...either decrement row counter (if row still exists)...
2435 coords
.SetCol(col
+ numCols
);
2439 // ...or remove the attribute
2440 m_attrs
.RemoveAt((size_t)n
);
2448 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2450 size_t count
= m_attrs
.GetCount();
2451 for ( size_t n
= 0; n
< count
; n
++ )
2453 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2454 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2463 // ----------------------------------------------------------------------------
2464 // wxGridRowOrColAttrData
2465 // ----------------------------------------------------------------------------
2467 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2469 size_t count
= m_attrs
.Count();
2470 for ( size_t n
= 0; n
< count
; n
++ )
2472 m_attrs
[n
]->DecRef();
2476 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2478 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2480 int n
= m_rowsOrCols
.Index(rowOrCol
);
2481 if ( n
!= wxNOT_FOUND
)
2483 attr
= m_attrs
[(size_t)n
];
2490 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2492 int i
= m_rowsOrCols
.Index(rowOrCol
);
2493 if ( i
== wxNOT_FOUND
)
2495 // add the attribute
2496 m_rowsOrCols
.Add(rowOrCol
);
2501 size_t n
= (size_t)i
;
2504 // change the attribute
2505 m_attrs
[n
]->DecRef();
2510 // remove this attribute
2511 m_attrs
[n
]->DecRef();
2512 m_rowsOrCols
.RemoveAt(n
);
2513 m_attrs
.RemoveAt(n
);
2518 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2520 size_t count
= m_attrs
.GetCount();
2521 for ( size_t n
= 0; n
< count
; n
++ )
2523 int & rowOrCol
= m_rowsOrCols
[n
];
2524 if ( (size_t)rowOrCol
>= pos
)
2526 if ( numRowsOrCols
> 0 )
2528 // If rows inserted, include row counter where necessary
2529 rowOrCol
+= numRowsOrCols
;
2531 else if ( numRowsOrCols
< 0)
2533 // If rows deleted, either decrement row counter (if row still exists)
2534 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2535 rowOrCol
+= numRowsOrCols
;
2538 m_rowsOrCols
.RemoveAt((size_t)n
);
2539 m_attrs
.RemoveAt((size_t)n
);
2547 // ----------------------------------------------------------------------------
2548 // wxGridCellAttrProvider
2549 // ----------------------------------------------------------------------------
2551 wxGridCellAttrProvider::wxGridCellAttrProvider()
2553 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2556 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2561 void wxGridCellAttrProvider::InitData()
2563 m_data
= new wxGridCellAttrProviderData
;
2566 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2567 wxGridCellAttr::wxAttrKind kind
) const
2569 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2574 case (wxGridCellAttr::Any
):
2575 //Get cached merge attributes.
2576 // Currenlty not used as no cache implemented as not mutiable
2577 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2580 //Basicaly implement old version.
2581 //Also check merge cache, so we don't have to re-merge every time..
2582 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2583 *attrrow
= (wxGridCellAttr
*)NULL
,
2584 *attrcol
= (wxGridCellAttr
*)NULL
;
2586 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2587 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2588 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2590 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2591 // Two or move are non NULL
2592 attr
= new wxGridCellAttr
;
2593 attr
->SetKind(wxGridCellAttr::Merged
);
2597 attr
->MergeWith(attrcell
);
2601 attr
->MergeWith(attrcol
);
2605 attr
->MergeWith(attrrow
);
2608 //store merge attr if cache implemented
2610 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2614 // one or none is non null return it or null.
2615 if(attrrow
) attr
= attrrow
;
2616 if(attrcol
) attr
= attrcol
;
2617 if(attrcell
) attr
= attrcell
;
2621 case (wxGridCellAttr::Cell
):
2622 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2624 case (wxGridCellAttr::Col
):
2625 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2627 case (wxGridCellAttr::Row
):
2628 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2632 // (wxGridCellAttr::Default):
2633 // (wxGridCellAttr::Merged):
2640 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2646 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2649 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2654 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2657 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2662 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2665 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2669 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2671 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2675 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2679 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2681 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2685 // ----------------------------------------------------------------------------
2686 // wxGridTypeRegistry
2687 // ----------------------------------------------------------------------------
2689 wxGridTypeRegistry::~wxGridTypeRegistry()
2691 size_t count
= m_typeinfo
.Count();
2692 for ( size_t i
= 0; i
< count
; i
++ )
2693 delete m_typeinfo
[i
];
2697 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2698 wxGridCellRenderer
* renderer
,
2699 wxGridCellEditor
* editor
)
2701 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2703 // is it already registered?
2704 int loc
= FindRegisteredDataType(typeName
);
2705 if ( loc
!= wxNOT_FOUND
)
2707 delete m_typeinfo
[loc
];
2708 m_typeinfo
[loc
] = info
;
2712 m_typeinfo
.Add(info
);
2716 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2718 size_t count
= m_typeinfo
.GetCount();
2719 for ( size_t i
= 0; i
< count
; i
++ )
2721 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2730 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2732 int index
= FindRegisteredDataType(typeName
);
2733 if ( index
== wxNOT_FOUND
)
2735 // check whether this is one of the standard ones, in which case
2736 // register it "on the fly"
2738 if ( typeName
== wxGRID_VALUE_STRING
)
2740 RegisterDataType(wxGRID_VALUE_STRING
,
2741 new wxGridCellStringRenderer
,
2742 new wxGridCellTextEditor
);
2744 #endif // wxUSE_TEXTCTRL
2746 if ( typeName
== wxGRID_VALUE_BOOL
)
2748 RegisterDataType(wxGRID_VALUE_BOOL
,
2749 new wxGridCellBoolRenderer
,
2750 new wxGridCellBoolEditor
);
2752 #endif // wxUSE_CHECKBOX
2754 if ( typeName
== wxGRID_VALUE_NUMBER
)
2756 RegisterDataType(wxGRID_VALUE_NUMBER
,
2757 new wxGridCellNumberRenderer
,
2758 new wxGridCellNumberEditor
);
2760 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2762 RegisterDataType(wxGRID_VALUE_FLOAT
,
2763 new wxGridCellFloatRenderer
,
2764 new wxGridCellFloatEditor
);
2766 #endif // wxUSE_TEXTCTRL
2768 if ( typeName
== wxGRID_VALUE_CHOICE
)
2770 RegisterDataType(wxGRID_VALUE_CHOICE
,
2771 new wxGridCellStringRenderer
,
2772 new wxGridCellChoiceEditor
);
2774 #endif // wxUSE_COMBOBOX
2779 // we get here only if just added the entry for this type, so return
2781 index
= m_typeinfo
.GetCount() - 1;
2787 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2789 int index
= FindDataType(typeName
);
2790 if ( index
== wxNOT_FOUND
)
2792 // the first part of the typename is the "real" type, anything after ':'
2793 // are the parameters for the renderer
2794 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2795 if ( index
== wxNOT_FOUND
)
2800 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2801 wxGridCellRenderer
*rendererOld
= renderer
;
2802 renderer
= renderer
->Clone();
2803 rendererOld
->DecRef();
2805 wxGridCellEditor
*editor
= GetEditor(index
);
2806 wxGridCellEditor
*editorOld
= editor
;
2807 editor
= editor
->Clone();
2808 editorOld
->DecRef();
2810 // do it even if there are no parameters to reset them to defaults
2811 wxString params
= typeName
.AfterFirst(_T(':'));
2812 renderer
->SetParameters(params
);
2813 editor
->SetParameters(params
);
2815 // register the new typename
2816 RegisterDataType(typeName
, renderer
, editor
);
2818 // we just registered it, it's the last one
2819 index
= m_typeinfo
.GetCount() - 1;
2825 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2827 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2833 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2835 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2841 // ----------------------------------------------------------------------------
2843 // ----------------------------------------------------------------------------
2845 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2848 wxGridTableBase::wxGridTableBase()
2850 m_view
= (wxGrid
*) NULL
;
2851 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2854 wxGridTableBase::~wxGridTableBase()
2856 delete m_attrProvider
;
2859 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2861 delete m_attrProvider
;
2862 m_attrProvider
= attrProvider
;
2865 bool wxGridTableBase::CanHaveAttributes()
2867 if ( ! GetAttrProvider() )
2869 // use the default attr provider by default
2870 SetAttrProvider(new wxGridCellAttrProvider
);
2875 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2877 if ( m_attrProvider
)
2878 return m_attrProvider
->GetAttr(row
, col
, kind
);
2880 return (wxGridCellAttr
*)NULL
;
2883 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2885 if ( m_attrProvider
)
2887 attr
->SetKind(wxGridCellAttr::Cell
);
2888 m_attrProvider
->SetAttr(attr
, row
, col
);
2892 // as we take ownership of the pointer and don't store it, we must
2898 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2900 if ( m_attrProvider
)
2902 attr
->SetKind(wxGridCellAttr::Row
);
2903 m_attrProvider
->SetRowAttr(attr
, row
);
2907 // as we take ownership of the pointer and don't store it, we must
2913 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2915 if ( m_attrProvider
)
2917 attr
->SetKind(wxGridCellAttr::Col
);
2918 m_attrProvider
->SetColAttr(attr
, col
);
2922 // as we take ownership of the pointer and don't store it, we must
2928 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2929 size_t WXUNUSED(numRows
) )
2931 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2936 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2938 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2943 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2944 size_t WXUNUSED(numRows
) )
2946 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2951 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2952 size_t WXUNUSED(numCols
) )
2954 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2959 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2961 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2966 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2967 size_t WXUNUSED(numCols
) )
2969 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2975 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2978 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2979 // how much it makes sense to us geeks.
2983 wxString
wxGridTableBase::GetColLabelValue( int col
)
2985 // default col labels are:
2986 // cols 0 to 25 : A-Z
2987 // cols 26 to 675 : AA-ZZ
2992 for ( n
= 1; ; n
++ )
2994 s
+= (_T('A') + (wxChar
)( col%26
));
2996 if ( col
< 0 ) break;
2999 // reverse the string...
3001 for ( i
= 0; i
< n
; i
++ )
3010 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3012 return wxGRID_VALUE_STRING
;
3015 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3016 const wxString
& typeName
)
3018 return typeName
== wxGRID_VALUE_STRING
;
3021 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3023 return CanGetValueAs(row
, col
, typeName
);
3026 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3031 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3036 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3041 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3042 long WXUNUSED(value
) )
3046 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3047 double WXUNUSED(value
) )
3051 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3052 bool WXUNUSED(value
) )
3057 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3058 const wxString
& WXUNUSED(typeName
) )
3063 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3064 const wxString
& WXUNUSED(typeName
),
3065 void* WXUNUSED(value
) )
3069 //////////////////////////////////////////////////////////////////////
3071 // Message class for the grid table to send requests and notifications
3075 wxGridTableMessage::wxGridTableMessage()
3077 m_table
= (wxGridTableBase
*) NULL
;
3083 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3084 int commandInt1
, int commandInt2
)
3088 m_comInt1
= commandInt1
;
3089 m_comInt2
= commandInt2
;
3094 //////////////////////////////////////////////////////////////////////
3096 // A basic grid table for string data. An object of this class will
3097 // created by wxGrid if you don't specify an alternative table class.
3100 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3102 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3104 wxGridStringTable::wxGridStringTable()
3109 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3112 m_data
.Alloc( numRows
);
3115 sa
.Alloc( numCols
);
3116 sa
.Add( wxEmptyString
, numCols
);
3118 m_data
.Add( sa
, numRows
);
3121 wxGridStringTable::~wxGridStringTable()
3125 int wxGridStringTable::GetNumberRows()
3127 return m_data
.GetCount();
3130 int wxGridStringTable::GetNumberCols()
3132 if ( m_data
.GetCount() > 0 )
3133 return m_data
[0].GetCount();
3138 wxString
wxGridStringTable::GetValue( int row
, int col
)
3140 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3142 _T("invalid row or column index in wxGridStringTable") );
3144 return m_data
[row
][col
];
3147 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3149 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3150 _T("invalid row or column index in wxGridStringTable") );
3152 m_data
[row
][col
] = value
;
3155 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3157 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3159 _T("invalid row or column index in wxGridStringTable") );
3161 return (m_data
[row
][col
] == wxEmptyString
);
3164 void wxGridStringTable::Clear()
3167 int numRows
, numCols
;
3169 numRows
= m_data
.GetCount();
3172 numCols
= m_data
[0].GetCount();
3174 for ( row
= 0; row
< numRows
; row
++ )
3176 for ( col
= 0; col
< numCols
; col
++ )
3178 m_data
[row
][col
] = wxEmptyString
;
3185 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3187 size_t curNumRows
= m_data
.GetCount();
3188 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3189 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3191 if ( pos
>= curNumRows
)
3193 return AppendRows( numRows
);
3197 sa
.Alloc( curNumCols
);
3198 sa
.Add( wxEmptyString
, curNumCols
);
3199 m_data
.Insert( sa
, pos
, numRows
);
3202 wxGridTableMessage
msg( this,
3203 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3207 GetView()->ProcessTableMessage( msg
);
3213 bool wxGridStringTable::AppendRows( size_t numRows
)
3215 size_t curNumRows
= m_data
.GetCount();
3216 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3217 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3220 if ( curNumCols
> 0 )
3222 sa
.Alloc( curNumCols
);
3223 sa
.Add( wxEmptyString
, curNumCols
);
3226 m_data
.Add( sa
, numRows
);
3230 wxGridTableMessage
msg( this,
3231 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3234 GetView()->ProcessTableMessage( msg
);
3240 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3242 size_t curNumRows
= m_data
.GetCount();
3244 if ( pos
>= curNumRows
)
3246 wxFAIL_MSG( wxString::Format
3248 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3250 (unsigned long)numRows
,
3251 (unsigned long)curNumRows
3257 if ( numRows
> curNumRows
- pos
)
3259 numRows
= curNumRows
- pos
;
3262 if ( numRows
>= curNumRows
)
3268 m_data
.RemoveAt( pos
, numRows
);
3272 wxGridTableMessage
msg( this,
3273 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3277 GetView()->ProcessTableMessage( msg
);
3283 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3287 size_t curNumRows
= m_data
.GetCount();
3288 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3289 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3291 if ( pos
>= curNumCols
)
3293 return AppendCols( numCols
);
3296 for ( row
= 0; row
< curNumRows
; row
++ )
3298 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3300 m_data
[row
].Insert( wxEmptyString
, col
);
3305 wxGridTableMessage
msg( this,
3306 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3310 GetView()->ProcessTableMessage( msg
);
3316 bool wxGridStringTable::AppendCols( size_t numCols
)
3320 size_t curNumRows
= m_data
.GetCount();
3324 // TODO: something better than this ?
3326 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3331 for ( row
= 0; row
< curNumRows
; row
++ )
3333 m_data
[row
].Add( wxEmptyString
, numCols
);
3338 wxGridTableMessage
msg( this,
3339 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3342 GetView()->ProcessTableMessage( msg
);
3348 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3352 size_t curNumRows
= m_data
.GetCount();
3353 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3354 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3356 if ( pos
>= curNumCols
)
3358 wxFAIL_MSG( wxString::Format
3360 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3362 (unsigned long)numCols
,
3363 (unsigned long)curNumCols
3368 if ( numCols
> curNumCols
- pos
)
3370 numCols
= curNumCols
- pos
;
3373 for ( row
= 0; row
< curNumRows
; row
++ )
3375 if ( numCols
>= curNumCols
)
3377 m_data
[row
].Clear();
3381 m_data
[row
].RemoveAt( pos
, numCols
);
3386 wxGridTableMessage
msg( this,
3387 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3391 GetView()->ProcessTableMessage( msg
);
3397 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3399 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3401 // using default label
3403 return wxGridTableBase::GetRowLabelValue( row
);
3407 return m_rowLabels
[ row
];
3411 wxString
wxGridStringTable::GetColLabelValue( int col
)
3413 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3415 // using default label
3417 return wxGridTableBase::GetColLabelValue( col
);
3421 return m_colLabels
[ col
];
3425 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3427 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3429 int n
= m_rowLabels
.GetCount();
3431 for ( i
= n
; i
<= row
; i
++ )
3433 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3437 m_rowLabels
[row
] = value
;
3440 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3442 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3444 int n
= m_colLabels
.GetCount();
3446 for ( i
= n
; i
<= col
; i
++ )
3448 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3452 m_colLabels
[col
] = value
;
3457 //////////////////////////////////////////////////////////////////////
3458 //////////////////////////////////////////////////////////////////////
3460 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3462 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3463 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3464 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3465 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3466 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3467 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3470 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3472 const wxPoint
&pos
, const wxSize
&size
)
3473 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3478 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3482 // NO - don't do this because it will set both the x and y origin
3483 // coords to match the parent scrolled window and we just want to
3484 // set the y coord - MB
3486 // m_owner->PrepareDC( dc );
3489 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3490 dc
.SetDeviceOrigin( 0, -y
);
3492 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3493 m_owner
->DrawRowLabels( dc
, rows
);
3497 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3499 m_owner
->ProcessRowLabelMouseEvent( event
);
3503 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3505 m_owner
->GetEventHandler()->ProcessEvent(event
);
3509 // This seems to be required for wxMotif otherwise the mouse
3510 // cursor must be in the cell edit control to get key events
3512 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3514 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3517 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3519 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3524 //////////////////////////////////////////////////////////////////////
3526 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3528 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3529 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3530 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3531 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3532 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3533 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3536 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3538 const wxPoint
&pos
, const wxSize
&size
)
3539 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3544 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3548 // NO - don't do this because it will set both the x and y origin
3549 // coords to match the parent scrolled window and we just want to
3550 // set the x coord - MB
3552 // m_owner->PrepareDC( dc );
3555 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3556 dc
.SetDeviceOrigin( -x
, 0 );
3558 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3559 m_owner
->DrawColLabels( dc
, cols
);
3563 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3565 m_owner
->ProcessColLabelMouseEvent( event
);
3568 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3570 m_owner
->GetEventHandler()->ProcessEvent(event
);
3574 // This seems to be required for wxMotif otherwise the mouse
3575 // cursor must be in the cell edit control to get key events
3577 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3579 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3582 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3584 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3589 //////////////////////////////////////////////////////////////////////
3591 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3593 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3594 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3595 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3596 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3597 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3598 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3601 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3603 const wxPoint
&pos
, const wxSize
&size
)
3604 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3609 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3613 int client_height
= 0;
3614 int client_width
= 0;
3615 GetClientSize( &client_width
, &client_height
);
3617 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3618 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3619 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3620 dc
.DrawLine( 0, 0, client_width
, 0 );
3621 dc
.DrawLine( 0, 0, 0, client_height
);
3623 dc
.SetPen( *wxWHITE_PEN
);
3624 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3625 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3629 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3631 m_owner
->ProcessCornerLabelMouseEvent( event
);
3635 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3637 m_owner
->GetEventHandler()->ProcessEvent(event
);
3640 // This seems to be required for wxMotif otherwise the mouse
3641 // cursor must be in the cell edit control to get key events
3643 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3645 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3648 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3650 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3655 //////////////////////////////////////////////////////////////////////
3657 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3659 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3660 EVT_PAINT( wxGridWindow::OnPaint
)
3661 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3662 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3663 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3664 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3665 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3668 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3669 wxGridRowLabelWindow
*rowLblWin
,
3670 wxGridColLabelWindow
*colLblWin
,
3673 const wxSize
&size
)
3674 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
,
3675 wxT("grid window") )
3679 m_rowLabelWin
= rowLblWin
;
3680 m_colLabelWin
= colLblWin
;
3681 SetBackgroundColour(_T("WHITE"));
3685 wxGridWindow::~wxGridWindow()
3690 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3692 wxPaintDC
dc( this );
3693 m_owner
->PrepareDC( dc
);
3694 wxRegion reg
= GetUpdateRegion();
3695 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3696 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3697 #if WXGRID_DRAW_LINES
3698 m_owner
->DrawAllGridLines( dc
, reg
);
3700 m_owner
->DrawGridSpace( dc
);
3701 m_owner
->DrawHighlight( dc
, DirtyCells
);
3705 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3707 wxWindow::ScrollWindow( dx
, dy
, rect
);
3708 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3709 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3713 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3715 m_owner
->ProcessGridCellMouseEvent( event
);
3718 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3720 m_owner
->GetEventHandler()->ProcessEvent(event
);
3723 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3724 // cursor must be in the cell edit control to get key events
3726 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3728 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3731 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3733 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3736 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3741 //////////////////////////////////////////////////////////////////////
3743 // Internal Helper function for computing row or column from some
3744 // (unscrolled) coordinate value, using either
3745 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3746 // of m_rowBottoms/m_ColRights to speed up the search!
3748 // Internal helper macros for simpler use of that function
3750 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3751 const wxArrayInt
& BorderArray
, int nMax
,
3754 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3755 m_minAcceptableColWidth, \
3756 m_colRights, m_numCols, TRUE)
3757 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3758 m_minAcceptableRowHeight, \
3759 m_rowBottoms, m_numRows, TRUE)
3760 /////////////////////////////////////////////////////////////////////
3762 #if wxUSE_EXTENDED_RTTI
3763 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3765 WX_BEGIN_PROPERTIES_TABLE(wxGrid
)
3766 WX_HIDE_PROPERTY( Children
)
3767 WX_END_PROPERTIES_TABLE()
3769 WX_BEGIN_HANDLERS_TABLE(wxGrid
)
3770 WX_END_HANDLERS_TABLE()
3772 WX_CONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3775 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
3778 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3781 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3782 EVT_PAINT( wxGrid::OnPaint
)
3783 EVT_SIZE( wxGrid::OnSize
)
3784 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3785 EVT_KEY_UP( wxGrid::OnKeyUp
)
3786 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3791 // in order to make sure that a size event is not
3792 // trigerred in a unfinished state
3793 m_cornerLabelWin
= NULL
;
3794 m_rowLabelWin
= NULL
;
3795 m_colLabelWin
= NULL
;
3799 wxGrid::wxGrid( wxWindow
*parent
,
3804 const wxString
& name
)
3805 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3806 m_colMinWidths(GRID_HASH_SIZE
),
3807 m_rowMinHeights(GRID_HASH_SIZE
)
3812 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3813 const wxPoint
& pos
, const wxSize
& size
,
3814 long style
, const wxString
& name
)
3816 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3817 style
| wxWANTS_CHARS
, name
))
3820 m_colMinWidths
=GRID_HASH_SIZE
;
3821 m_rowMinHeights
= GRID_HASH_SIZE
;
3832 // Must do this or ~wxScrollHelper will pop the wrong event handler
3833 SetTargetWindow(this);
3835 wxSafeDecRef(m_defaultCellAttr
);
3837 #ifdef DEBUG_ATTR_CACHE
3838 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3839 wxPrintf(_T("wxGrid attribute cache statistics: "
3840 "total: %u, hits: %u (%u%%)\n"),
3841 total
, gs_nAttrCacheHits
,
3842 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3848 delete m_typeRegistry
;
3854 // ----- internal init and update functions
3857 void wxGrid::Create()
3859 m_created
= FALSE
; // set to TRUE by CreateGrid
3861 m_table
= (wxGridTableBase
*) NULL
;
3864 m_cellEditCtrlEnabled
= FALSE
;
3866 m_defaultCellAttr
= new wxGridCellAttr();
3868 // Set default cell attributes
3869 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3870 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3871 m_defaultCellAttr
->SetFont(GetFont());
3872 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3873 m_defaultCellAttr
->SetTextColour(
3874 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3875 m_defaultCellAttr
->SetBackgroundColour(
3876 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3877 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3878 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3883 m_currentCellCoords
= wxGridNoCellCoords
;
3885 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3886 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3888 // create the type registry
3889 m_typeRegistry
= new wxGridTypeRegistry
;
3892 // subwindow components that make up the wxGrid
3893 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3898 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3903 m_colLabelWin
= new wxGridColLabelWindow( this,
3908 m_gridWin
= new wxGridWindow( this,
3915 SetTargetWindow( m_gridWin
);
3921 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3922 wxGrid::wxGridSelectionModes selmode
)
3924 wxCHECK_MSG( !m_created
,
3926 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3928 m_numRows
= numRows
;
3929 m_numCols
= numCols
;
3931 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3932 m_table
->SetView( this );
3934 m_selection
= new wxGridSelection( this, selmode
);
3943 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3945 wxCHECK_RET( m_created
,
3946 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3948 m_selection
->SetSelectionMode( selmode
);
3951 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3953 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3954 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3956 return m_selection
->GetSelectionMode();
3959 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3960 wxGrid::wxGridSelectionModes selmode
)
3964 // stop all processing
3969 wxGridTableBase
*t
=m_table
;
3982 m_numRows
= table
->GetNumberRows();
3983 m_numCols
= table
->GetNumberCols();
3986 m_table
->SetView( this );
3989 m_selection
= new wxGridSelection( this, selmode
);
4002 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4003 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4005 if ( m_rowLabelWin
)
4007 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4011 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4014 m_labelTextColour
= wxColour( _T("BLACK") );
4017 m_attrCache
.row
= -1;
4018 m_attrCache
.col
= -1;
4019 m_attrCache
.attr
= NULL
;
4021 // TODO: something better than this ?
4023 m_labelFont
= this->GetFont();
4024 m_labelFont
.SetWeight( wxBOLD
);
4026 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4027 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4029 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4030 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4031 m_colLabelTextOrientation
= wxHORIZONTAL
;
4033 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4034 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4036 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4037 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4039 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4040 m_defaultRowHeight
+= 8;
4042 m_defaultRowHeight
+= 4;
4045 m_gridLineColour
= wxColour( 192,192,192 );
4046 m_gridLinesEnabled
= TRUE
;
4047 m_cellHighlightColour
= *wxBLACK
;
4048 m_cellHighlightPenWidth
= 2;
4049 m_cellHighlightROPenWidth
= 1;
4051 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4052 m_winCapture
= (wxWindow
*)NULL
;
4053 m_canDragRowSize
= TRUE
;
4054 m_canDragColSize
= TRUE
;
4055 m_canDragGridSize
= TRUE
;
4057 m_dragRowOrCol
= -1;
4058 m_isDragging
= FALSE
;
4059 m_startDragPos
= wxDefaultPosition
;
4061 m_waitForSlowClick
= FALSE
;
4063 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4064 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4066 m_currentCellCoords
= wxGridNoCellCoords
;
4068 m_selectingTopLeft
= wxGridNoCellCoords
;
4069 m_selectingBottomRight
= wxGridNoCellCoords
;
4070 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4071 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4073 m_editable
= TRUE
; // default for whole grid
4075 m_inOnKeyDown
= FALSE
;
4082 // ----------------------------------------------------------------------------
4083 // the idea is to call these functions only when necessary because they create
4084 // quite big arrays which eat memory mostly unnecessary - in particular, if
4085 // default widths/heights are used for all rows/columns, we may not use these
4088 // with some extra code, it should be possible to only store the
4089 // widths/heights different from default ones but this will be done later...
4090 // ----------------------------------------------------------------------------
4092 void wxGrid::InitRowHeights()
4094 m_rowHeights
.Empty();
4095 m_rowBottoms
.Empty();
4097 m_rowHeights
.Alloc( m_numRows
);
4098 m_rowBottoms
.Alloc( m_numRows
);
4102 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4104 for ( int i
= 0; i
< m_numRows
; i
++ )
4106 rowBottom
+= m_defaultRowHeight
;
4107 m_rowBottoms
.Add( rowBottom
);
4111 void wxGrid::InitColWidths()
4113 m_colWidths
.Empty();
4114 m_colRights
.Empty();
4116 m_colWidths
.Alloc( m_numCols
);
4117 m_colRights
.Alloc( m_numCols
);
4120 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4122 for ( int i
= 0; i
< m_numCols
; i
++ )
4124 colRight
+= m_defaultColWidth
;
4125 m_colRights
.Add( colRight
);
4129 int wxGrid::GetColWidth(int col
) const
4131 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4134 int wxGrid::GetColLeft(int col
) const
4136 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4137 : m_colRights
[col
] - m_colWidths
[col
];
4140 int wxGrid::GetColRight(int col
) const
4142 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4146 int wxGrid::GetRowHeight(int row
) const
4148 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4151 int wxGrid::GetRowTop(int row
) const
4153 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4154 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4157 int wxGrid::GetRowBottom(int row
) const
4159 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4160 : m_rowBottoms
[row
];
4163 void wxGrid::CalcDimensions()
4166 GetClientSize( &cw
, &ch
);
4168 if ( m_rowLabelWin
->IsShown() )
4169 cw
-= m_rowLabelWidth
;
4170 if ( m_colLabelWin
->IsShown() )
4171 ch
-= m_colLabelHeight
;
4174 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4175 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4177 // take into account editor if shown
4178 if( IsCellEditControlShown() )
4181 int r
= m_currentCellCoords
.GetRow();
4182 int c
= m_currentCellCoords
.GetCol();
4183 int x
= GetColLeft(c
);
4184 int y
= GetRowTop(r
);
4186 // how big is the editor
4187 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4188 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4189 editor
->GetControl()->GetSize(&w2
, &h2
);
4192 if( w2
> w
) w
= w2
;
4193 if( h2
> h
) h
= h2
;
4198 // preserve (more or less) the previous position
4200 GetViewStart( &x
, &y
);
4202 // maybe we don't need scrollbars at all?
4204 // also adjust the position to be valid for the new scroll rangs
4225 // do set scrollbar parameters
4226 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4227 GetScrollX(w
), GetScrollY(h
), x
, y
,
4228 GetBatchCount() != 0);
4230 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4231 // still must reposition the children
4236 void wxGrid::CalcWindowSizes()
4238 // escape if the window is has not been fully created yet
4240 if ( m_cornerLabelWin
== NULL
)
4244 GetClientSize( &cw
, &ch
);
4246 if ( m_cornerLabelWin
->IsShown() )
4247 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4249 if ( m_colLabelWin
->IsShown() )
4250 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4252 if ( m_rowLabelWin
->IsShown() )
4253 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4255 if ( m_gridWin
->IsShown() )
4256 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4260 // this is called when the grid table sends a message to say that it
4261 // has been redimensioned
4263 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4266 bool result
= FALSE
;
4268 // Clear the attribute cache as the attribute might refer to a different
4269 // cell than stored in the cache after adding/removing rows/columns.
4271 // By the same reasoning, the editor should be dismissed if columns are
4272 // added or removed. And for consistency, it should IMHO always be
4273 // removed, not only if the cell "underneath" it actually changes.
4274 // For now, I intentionally do not save the editor's content as the
4275 // cell it might want to save that stuff to might no longer exist.
4276 HideCellEditControl();
4278 // if we were using the default widths/heights so far, we must change them
4280 if ( m_colWidths
.IsEmpty() )
4285 if ( m_rowHeights
.IsEmpty() )
4291 switch ( msg
.GetId() )
4293 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4295 size_t pos
= msg
.GetCommandInt();
4296 int numRows
= msg
.GetCommandInt2();
4298 m_numRows
+= numRows
;
4300 if ( !m_rowHeights
.IsEmpty() )
4302 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4303 m_rowBottoms
.Insert( 0, pos
, numRows
);
4306 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4308 for ( i
= pos
; i
< m_numRows
; i
++ )
4310 bottom
+= m_rowHeights
[i
];
4311 m_rowBottoms
[i
] = bottom
;
4314 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4316 // if we have just inserted cols into an empty grid the current
4317 // cell will be undefined...
4319 SetCurrentCell( 0, 0 );
4323 m_selection
->UpdateRows( pos
, numRows
);
4324 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4326 attrProvider
->UpdateAttrRows( pos
, numRows
);
4328 if ( !GetBatchCount() )
4331 m_rowLabelWin
->Refresh();
4337 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4339 int numRows
= msg
.GetCommandInt();
4340 int oldNumRows
= m_numRows
;
4341 m_numRows
+= numRows
;
4343 if ( !m_rowHeights
.IsEmpty() )
4345 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4346 m_rowBottoms
.Add( 0, numRows
);
4349 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4351 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4353 bottom
+= m_rowHeights
[i
];
4354 m_rowBottoms
[i
] = bottom
;
4357 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4359 // if we have just inserted cols into an empty grid the current
4360 // cell will be undefined...
4362 SetCurrentCell( 0, 0 );
4364 if ( !GetBatchCount() )
4367 m_rowLabelWin
->Refresh();
4373 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4375 size_t pos
= msg
.GetCommandInt();
4376 int numRows
= msg
.GetCommandInt2();
4377 m_numRows
-= numRows
;
4379 if ( !m_rowHeights
.IsEmpty() )
4381 m_rowHeights
.RemoveAt( pos
, numRows
);
4382 m_rowBottoms
.RemoveAt( pos
, numRows
);
4385 for ( i
= 0; i
< m_numRows
; i
++ )
4387 h
+= m_rowHeights
[i
];
4388 m_rowBottoms
[i
] = h
;
4393 m_currentCellCoords
= wxGridNoCellCoords
;
4397 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4398 m_currentCellCoords
.Set( 0, 0 );
4402 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4403 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4405 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4406 // ifdef'd out following patch from Paul Gammans
4408 // No need to touch column attributes, unless we
4409 // removed _all_ rows, in this case, we remove
4410 // all column attributes.
4411 // I hate to do this here, but the
4412 // needed data is not available inside UpdateAttrRows.
4413 if ( !GetNumberRows() )
4414 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4417 if ( !GetBatchCount() )
4420 m_rowLabelWin
->Refresh();
4426 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4428 size_t pos
= msg
.GetCommandInt();
4429 int numCols
= msg
.GetCommandInt2();
4430 m_numCols
+= numCols
;
4432 if ( !m_colWidths
.IsEmpty() )
4434 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4435 m_colRights
.Insert( 0, pos
, numCols
);
4438 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4440 for ( i
= pos
; i
< m_numCols
; i
++ )
4442 right
+= m_colWidths
[i
];
4443 m_colRights
[i
] = right
;
4446 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4448 // if we have just inserted cols into an empty grid the current
4449 // cell will be undefined...
4451 SetCurrentCell( 0, 0 );
4455 m_selection
->UpdateCols( pos
, numCols
);
4456 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4458 attrProvider
->UpdateAttrCols( pos
, numCols
);
4459 if ( !GetBatchCount() )
4462 m_colLabelWin
->Refresh();
4469 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4471 int numCols
= msg
.GetCommandInt();
4472 int oldNumCols
= m_numCols
;
4473 m_numCols
+= numCols
;
4474 if ( !m_colWidths
.IsEmpty() )
4476 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4477 m_colRights
.Add( 0, numCols
);
4480 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4482 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4484 right
+= m_colWidths
[i
];
4485 m_colRights
[i
] = right
;
4488 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4490 // if we have just inserted cols into an empty grid the current
4491 // cell will be undefined...
4493 SetCurrentCell( 0, 0 );
4495 if ( !GetBatchCount() )
4498 m_colLabelWin
->Refresh();
4504 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4506 size_t pos
= msg
.GetCommandInt();
4507 int numCols
= msg
.GetCommandInt2();
4508 m_numCols
-= numCols
;
4510 if ( !m_colWidths
.IsEmpty() )
4512 m_colWidths
.RemoveAt( pos
, numCols
);
4513 m_colRights
.RemoveAt( pos
, numCols
);
4516 for ( i
= 0; i
< m_numCols
; i
++ )
4518 w
+= m_colWidths
[i
];
4524 m_currentCellCoords
= wxGridNoCellCoords
;
4528 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4529 m_currentCellCoords
.Set( 0, 0 );
4533 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4534 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4536 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4537 // ifdef'd out following patch from Paul Gammans
4539 // No need to touch row attributes, unless we
4540 // removed _all_ columns, in this case, we remove
4541 // all row attributes.
4542 // I hate to do this here, but the
4543 // needed data is not available inside UpdateAttrCols.
4544 if ( !GetNumberCols() )
4545 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4548 if ( !GetBatchCount() )
4551 m_colLabelWin
->Refresh();
4558 if (result
&& !GetBatchCount() )
4559 m_gridWin
->Refresh();
4564 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4566 wxRegionIterator
iter( reg
);
4569 wxArrayInt rowlabels
;
4576 // TODO: remove this when we can...
4577 // There is a bug in wxMotif that gives garbage update
4578 // rectangles if you jump-scroll a long way by clicking the
4579 // scrollbar with middle button. This is a work-around
4581 #if defined(__WXMOTIF__)
4583 m_gridWin
->GetClientSize( &cw
, &ch
);
4584 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4585 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4588 // logical bounds of update region
4591 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4592 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4594 // find the row labels within these bounds
4597 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4599 if ( GetRowBottom(row
) < top
)
4602 if ( GetRowTop(row
) > bottom
)
4605 rowlabels
.Add( row
);
4615 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4617 wxRegionIterator
iter( reg
);
4620 wxArrayInt colLabels
;
4627 // TODO: remove this when we can...
4628 // There is a bug in wxMotif that gives garbage update
4629 // rectangles if you jump-scroll a long way by clicking the
4630 // scrollbar with middle button. This is a work-around
4632 #if defined(__WXMOTIF__)
4634 m_gridWin
->GetClientSize( &cw
, &ch
);
4635 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4636 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4639 // logical bounds of update region
4642 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4643 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4645 // find the cells within these bounds
4648 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4650 if ( GetColRight(col
) < left
)
4653 if ( GetColLeft(col
) > right
)
4656 colLabels
.Add( col
);
4665 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4667 wxRegionIterator
iter( reg
);
4670 wxGridCellCoordsArray cellsExposed
;
4672 int left
, top
, right
, bottom
;
4677 // TODO: remove this when we can...
4678 // There is a bug in wxMotif that gives garbage update
4679 // rectangles if you jump-scroll a long way by clicking the
4680 // scrollbar with middle button. This is a work-around
4682 #if defined(__WXMOTIF__)
4684 m_gridWin
->GetClientSize( &cw
, &ch
);
4685 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4686 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4687 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4688 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4691 // logical bounds of update region
4693 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4694 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4696 // find the cells within these bounds
4699 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4701 if ( GetRowBottom(row
) <= top
)
4704 if ( GetRowTop(row
) > bottom
)
4707 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4709 if ( GetColRight(col
) <= left
)
4712 if ( GetColLeft(col
) > right
)
4715 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4722 return cellsExposed
;
4726 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4729 wxPoint
pos( event
.GetPosition() );
4730 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4732 if ( event
.Dragging() )
4736 m_isDragging
= TRUE
;
4737 m_rowLabelWin
->CaptureMouse();
4740 if ( event
.LeftIsDown() )
4742 switch( m_cursorMode
)
4744 case WXGRID_CURSOR_RESIZE_ROW
:
4746 int cw
, ch
, left
, dummy
;
4747 m_gridWin
->GetClientSize( &cw
, &ch
);
4748 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4750 wxClientDC
dc( m_gridWin
);
4753 GetRowTop(m_dragRowOrCol
) +
4754 GetRowMinimalHeight(m_dragRowOrCol
) );
4755 dc
.SetLogicalFunction(wxINVERT
);
4756 if ( m_dragLastPos
>= 0 )
4758 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4760 dc
.DrawLine( left
, y
, left
+cw
, y
);
4765 case WXGRID_CURSOR_SELECT_ROW
:
4766 if ( (row
= YToRow( y
)) >= 0 )
4770 m_selection
->SelectRow( row
,
4771 event
.ControlDown(),
4778 // default label to suppress warnings about "enumeration value
4779 // 'xxx' not handled in switch
4787 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4792 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4793 m_isDragging
= FALSE
;
4796 // ------------ Entering or leaving the window
4798 if ( event
.Entering() || event
.Leaving() )
4800 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4804 // ------------ Left button pressed
4806 else if ( event
.LeftDown() )
4808 // don't send a label click event for a hit on the
4809 // edge of the row label - this is probably the user
4810 // wanting to resize the row
4812 if ( YToEdgeOfRow(y
) < 0 )
4816 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4818 if ( !event
.ShiftDown() && !event
.ControlDown() )
4822 if ( event
.ShiftDown() )
4824 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4827 GetNumberCols() - 1,
4828 event
.ControlDown(),
4835 m_selection
->SelectRow( row
,
4836 event
.ControlDown(),
4843 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4848 // starting to drag-resize a row
4850 if ( CanDragRowSize() )
4851 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4856 // ------------ Left double click
4858 else if (event
.LeftDClick() )
4860 int row
= YToEdgeOfRow(y
);
4865 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4867 // no default action at the moment
4872 // adjust row height depending on label text
4873 AutoSizeRowLabelSize( row
);
4875 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4881 // ------------ Left button released
4883 else if ( event
.LeftUp() )
4885 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4887 DoEndDragResizeRow();
4889 // Note: we are ending the event *after* doing
4890 // default processing in this case
4892 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4895 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4900 // ------------ Right button down
4902 else if ( event
.RightDown() )
4906 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4908 // no default action at the moment
4913 // ------------ Right double click
4915 else if ( event
.RightDClick() )
4919 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4921 // no default action at the moment
4926 // ------------ No buttons down and mouse moving
4928 else if ( event
.Moving() )
4930 m_dragRowOrCol
= YToEdgeOfRow( y
);
4931 if ( m_dragRowOrCol
>= 0 )
4933 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4935 // don't capture the mouse yet
4936 if ( CanDragRowSize() )
4937 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4940 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4942 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4948 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4951 wxPoint
pos( event
.GetPosition() );
4952 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4954 if ( event
.Dragging() )
4958 m_isDragging
= TRUE
;
4959 m_colLabelWin
->CaptureMouse();
4962 if ( event
.LeftIsDown() )
4964 switch( m_cursorMode
)
4966 case WXGRID_CURSOR_RESIZE_COL
:
4968 int cw
, ch
, dummy
, top
;
4969 m_gridWin
->GetClientSize( &cw
, &ch
);
4970 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4972 wxClientDC
dc( m_gridWin
);
4975 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4976 GetColMinimalWidth(m_dragRowOrCol
));
4977 dc
.SetLogicalFunction(wxINVERT
);
4978 if ( m_dragLastPos
>= 0 )
4980 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4982 dc
.DrawLine( x
, top
, x
, top
+ch
);
4987 case WXGRID_CURSOR_SELECT_COL
:
4988 if ( (col
= XToCol( x
)) >= 0 )
4992 m_selection
->SelectCol( col
,
4993 event
.ControlDown(),
5000 // default label to suppress warnings about "enumeration value
5001 // 'xxx' not handled in switch
5009 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5014 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5015 m_isDragging
= FALSE
;
5018 // ------------ Entering or leaving the window
5020 if ( event
.Entering() || event
.Leaving() )
5022 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5026 // ------------ Left button pressed
5028 else if ( event
.LeftDown() )
5030 // don't send a label click event for a hit on the
5031 // edge of the col label - this is probably the user
5032 // wanting to resize the col
5034 if ( XToEdgeOfCol(x
) < 0 )
5038 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5040 if ( !event
.ShiftDown() && !event
.ControlDown() )
5044 if ( event
.ShiftDown() )
5046 m_selection
->SelectBlock( 0,
5047 m_currentCellCoords
.GetCol(),
5048 GetNumberRows() - 1, col
,
5049 event
.ControlDown(),
5056 m_selection
->SelectCol( col
,
5057 event
.ControlDown(),
5064 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5069 // starting to drag-resize a col
5071 if ( CanDragColSize() )
5072 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5077 // ------------ Left double click
5079 if ( event
.LeftDClick() )
5081 int col
= XToEdgeOfCol(x
);
5086 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5088 // no default action at the moment
5093 // adjust column width depending on label text
5094 AutoSizeColLabelSize( col
);
5096 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5102 // ------------ Left button released
5104 else if ( event
.LeftUp() )
5106 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5108 DoEndDragResizeCol();
5110 // Note: we are ending the event *after* doing
5111 // default processing in this case
5113 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5116 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5121 // ------------ Right button down
5123 else if ( event
.RightDown() )
5127 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5129 // no default action at the moment
5134 // ------------ Right double click
5136 else if ( event
.RightDClick() )
5140 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5142 // no default action at the moment
5147 // ------------ No buttons down and mouse moving
5149 else if ( event
.Moving() )
5151 m_dragRowOrCol
= XToEdgeOfCol( x
);
5152 if ( m_dragRowOrCol
>= 0 )
5154 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5156 // don't capture the cursor yet
5157 if ( CanDragColSize() )
5158 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
5161 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5163 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
5169 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5171 if ( event
.LeftDown() )
5173 // indicate corner label by having both row and
5176 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5182 else if ( event
.LeftDClick() )
5184 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5187 else if ( event
.RightDown() )
5189 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5191 // no default action at the moment
5195 else if ( event
.RightDClick() )
5197 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5199 // no default action at the moment
5204 void wxGrid::ChangeCursorMode(CursorMode mode
,
5209 static const wxChar
*cursorModes
[] =
5218 wxLogTrace(_T("grid"),
5219 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5220 win
== m_colLabelWin
? _T("colLabelWin")
5221 : win
? _T("rowLabelWin")
5223 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5224 #endif // __WXDEBUG__
5226 if ( mode
== m_cursorMode
&&
5227 win
== m_winCapture
&&
5228 captureMouse
== (m_winCapture
!= NULL
))
5233 // by default use the grid itself
5239 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5240 m_winCapture
= (wxWindow
*)NULL
;
5243 m_cursorMode
= mode
;
5245 switch ( m_cursorMode
)
5247 case WXGRID_CURSOR_RESIZE_ROW
:
5248 win
->SetCursor( m_rowResizeCursor
);
5251 case WXGRID_CURSOR_RESIZE_COL
:
5252 win
->SetCursor( m_colResizeCursor
);
5256 win
->SetCursor( *wxSTANDARD_CURSOR
);
5259 // we need to capture mouse when resizing
5260 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5261 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5263 if ( captureMouse
&& resize
)
5265 win
->CaptureMouse();
5270 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5273 wxPoint
pos( event
.GetPosition() );
5274 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5276 wxGridCellCoords coords
;
5277 XYToCell( x
, y
, coords
);
5279 int cell_rows
, cell_cols
;
5280 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5281 if ((cell_rows
< 0) || (cell_cols
< 0))
5283 coords
.SetRow(coords
.GetRow() + cell_rows
);
5284 coords
.SetCol(coords
.GetCol() + cell_cols
);
5287 if ( event
.Dragging() )
5289 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5291 // Don't start doing anything until the mouse has been drug at
5292 // least 3 pixels in any direction...
5295 if (m_startDragPos
== wxDefaultPosition
)
5297 m_startDragPos
= pos
;
5300 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5304 m_isDragging
= TRUE
;
5305 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5307 // Hide the edit control, so it
5308 // won't interfer with drag-shrinking.
5309 if ( IsCellEditControlShown() )
5311 HideCellEditControl();
5312 SaveEditControlValue();
5315 // Have we captured the mouse yet?
5318 m_winCapture
= m_gridWin
;
5319 m_winCapture
->CaptureMouse();
5322 if ( coords
!= wxGridNoCellCoords
)
5324 if ( event
.ControlDown() )
5326 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5327 m_selectingKeyboard
= coords
;
5328 HighlightBlock ( m_selectingKeyboard
, coords
);
5332 if ( !IsSelection() )
5334 HighlightBlock( coords
, coords
);
5338 HighlightBlock( m_currentCellCoords
, coords
);
5342 if (! IsVisible(coords
))
5344 MakeCellVisible(coords
);
5345 // TODO: need to introduce a delay or something here. The
5346 // scrolling is way to fast, at least on MSW - also on GTK.
5350 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5352 int cw
, ch
, left
, dummy
;
5353 m_gridWin
->GetClientSize( &cw
, &ch
);
5354 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5356 wxClientDC
dc( m_gridWin
);
5358 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5359 GetRowMinimalHeight(m_dragRowOrCol
) );
5360 dc
.SetLogicalFunction(wxINVERT
);
5361 if ( m_dragLastPos
>= 0 )
5363 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5365 dc
.DrawLine( left
, y
, left
+cw
, y
);
5368 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5370 int cw
, ch
, dummy
, top
;
5371 m_gridWin
->GetClientSize( &cw
, &ch
);
5372 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5374 wxClientDC
dc( m_gridWin
);
5376 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5377 GetColMinimalWidth(m_dragRowOrCol
) );
5378 dc
.SetLogicalFunction(wxINVERT
);
5379 if ( m_dragLastPos
>= 0 )
5381 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5383 dc
.DrawLine( x
, top
, x
, top
+ch
);
5390 m_isDragging
= FALSE
;
5391 m_startDragPos
= wxDefaultPosition
;
5393 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5394 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5397 if ( event
.Entering() || event
.Leaving() )
5399 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5400 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5405 // ------------ Left button pressed
5407 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5409 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5414 if ( !event
.ControlDown() )
5416 if ( event
.ShiftDown() )
5420 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5421 m_currentCellCoords
.GetCol(),
5424 event
.ControlDown(),
5430 else if ( XToEdgeOfCol(x
) < 0 &&
5431 YToEdgeOfRow(y
) < 0 )
5433 DisableCellEditControl();
5434 MakeCellVisible( coords
);
5436 if ( event
.ControlDown() )
5440 m_selection
->ToggleCellSelection( coords
.GetRow(),
5442 event
.ControlDown(),
5447 m_selectingTopLeft
= wxGridNoCellCoords
;
5448 m_selectingBottomRight
= wxGridNoCellCoords
;
5449 m_selectingKeyboard
= coords
;
5453 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5454 SetCurrentCell( coords
);
5457 if ( m_selection
->GetSelectionMode() !=
5458 wxGrid::wxGridSelectCells
)
5460 HighlightBlock( coords
, coords
);
5469 // ------------ Left double click
5471 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5473 DisableCellEditControl();
5475 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5477 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5485 // ------------ Left button released
5487 else if ( event
.LeftUp() )
5489 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5493 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5494 m_winCapture
= NULL
;
5497 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5500 EnableCellEditControl();
5502 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5503 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5504 editor
->StartingClick();
5508 m_waitForSlowClick
= FALSE
;
5510 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5511 m_selectingBottomRight
!= wxGridNoCellCoords
)
5515 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5516 m_selectingTopLeft
.GetCol(),
5517 m_selectingBottomRight
.GetRow(),
5518 m_selectingBottomRight
.GetCol(),
5519 event
.ControlDown(),
5525 m_selectingTopLeft
= wxGridNoCellCoords
;
5526 m_selectingBottomRight
= wxGridNoCellCoords
;
5528 // Show the edit control, if it has been hidden for
5530 ShowCellEditControl();
5533 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5535 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5536 DoEndDragResizeRow();
5538 // Note: we are ending the event *after* doing
5539 // default processing in this case
5541 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5543 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5545 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5546 DoEndDragResizeCol();
5548 // Note: we are ending the event *after* doing
5549 // default processing in this case
5551 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5558 // ------------ Right button down
5560 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5562 DisableCellEditControl();
5563 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5568 // no default action at the moment
5573 // ------------ Right double click
5575 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5577 DisableCellEditControl();
5578 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5583 // no default action at the moment
5587 // ------------ Moving and no button action
5589 else if ( event
.Moving() && !event
.IsButton() )
5591 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5593 // out of grid cell area
5594 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5598 int dragRow
= YToEdgeOfRow( y
);
5599 int dragCol
= XToEdgeOfCol( x
);
5601 // Dragging on the corner of a cell to resize in both
5602 // directions is not implemented yet...
5604 if ( dragRow
>= 0 && dragCol
>= 0 )
5606 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5612 m_dragRowOrCol
= dragRow
;
5614 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5616 if ( CanDragRowSize() && CanDragGridSize() )
5617 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5622 m_dragRowOrCol
= dragCol
;
5630 m_dragRowOrCol
= dragCol
;
5632 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5634 if ( CanDragColSize() && CanDragGridSize() )
5635 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5641 // Neither on a row or col edge
5643 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5645 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5651 void wxGrid::DoEndDragResizeRow()
5653 if ( m_dragLastPos
>= 0 )
5655 // erase the last line and resize the row
5657 int cw
, ch
, left
, dummy
;
5658 m_gridWin
->GetClientSize( &cw
, &ch
);
5659 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5661 wxClientDC
dc( m_gridWin
);
5663 dc
.SetLogicalFunction( wxINVERT
);
5664 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5665 HideCellEditControl();
5666 SaveEditControlValue();
5668 int rowTop
= GetRowTop(m_dragRowOrCol
);
5669 SetRowSize( m_dragRowOrCol
,
5670 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5672 if ( !GetBatchCount() )
5674 // Only needed to get the correct rect.y:
5675 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5677 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5678 rect
.width
= m_rowLabelWidth
;
5679 rect
.height
= ch
- rect
.y
;
5680 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5682 // if there is a multicell block, paint all of it
5685 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5686 int leftCol
= XToCol(left
);
5687 int rightCol
= internalXToCol(left
+cw
);
5690 for (i
=leftCol
; i
<rightCol
; i
++)
5692 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5693 if (cell_rows
< subtract_rows
)
5694 subtract_rows
= cell_rows
;
5696 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5697 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5698 rect
.height
= ch
- rect
.y
;
5701 m_gridWin
->Refresh( FALSE
, &rect
);
5704 ShowCellEditControl();
5709 void wxGrid::DoEndDragResizeCol()
5711 if ( m_dragLastPos
>= 0 )
5713 // erase the last line and resize the col
5715 int cw
, ch
, dummy
, top
;
5716 m_gridWin
->GetClientSize( &cw
, &ch
);
5717 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5719 wxClientDC
dc( m_gridWin
);
5721 dc
.SetLogicalFunction( wxINVERT
);
5722 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5723 HideCellEditControl();
5724 SaveEditControlValue();
5726 int colLeft
= GetColLeft(m_dragRowOrCol
);
5727 SetColSize( m_dragRowOrCol
,
5728 wxMax( m_dragLastPos
- colLeft
,
5729 GetColMinimalWidth(m_dragRowOrCol
) ) );
5731 if ( !GetBatchCount() )
5733 // Only needed to get the correct rect.x:
5734 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5736 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5737 rect
.width
= cw
- rect
.x
;
5738 rect
.height
= m_colLabelHeight
;
5739 m_colLabelWin
->Refresh( TRUE
, &rect
);
5741 // if there is a multicell block, paint all of it
5744 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5745 int topRow
= YToRow(top
);
5746 int bottomRow
= internalYToRow(top
+cw
);
5749 for (i
=topRow
; i
<bottomRow
; i
++)
5751 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5752 if (cell_cols
< subtract_cols
)
5753 subtract_cols
= cell_cols
;
5755 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5756 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5757 rect
.width
= cw
- rect
.x
;
5760 m_gridWin
->Refresh( FALSE
, &rect
);
5763 ShowCellEditControl();
5770 // ------ interaction with data model
5772 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5774 switch ( msg
.GetId() )
5776 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5777 return GetModelValues();
5779 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5780 return SetModelValues();
5782 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5783 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5784 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5785 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5786 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5787 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5788 return Redimension( msg
);
5797 // The behaviour of this function depends on the grid table class
5798 // Clear() function. For the default wxGridStringTable class the
5799 // behavious is to replace all cell contents with wxEmptyString but
5800 // not to change the number of rows or cols.
5802 void wxGrid::ClearGrid()
5806 if (IsCellEditControlEnabled())
5807 DisableCellEditControl();
5810 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5815 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5817 // TODO: something with updateLabels flag
5821 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5827 if (IsCellEditControlEnabled())
5828 DisableCellEditControl();
5830 bool done
= m_table
->InsertRows( pos
, numRows
);
5833 // the table will have sent the results of the insert row
5834 // operation to this view object as a grid table message
5840 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5842 // TODO: something with updateLabels flag
5846 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5852 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5854 // the table will have sent the results of the append row
5855 // operation to this view object as a grid table message
5861 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5863 // TODO: something with updateLabels flag
5867 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5873 if (IsCellEditControlEnabled())
5874 DisableCellEditControl();
5876 bool done
= m_table
->DeleteRows( pos
, numRows
);
5878 // the table will have sent the results of the delete row
5879 // operation to this view object as a grid table message
5885 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5887 // TODO: something with updateLabels flag
5891 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5897 if (IsCellEditControlEnabled())
5898 DisableCellEditControl();
5900 bool done
= m_table
->InsertCols( pos
, numCols
);
5902 // the table will have sent the results of the insert col
5903 // operation to this view object as a grid table message
5909 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5911 // TODO: something with updateLabels flag
5915 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5921 bool done
= m_table
->AppendCols( numCols
);
5923 // the table will have sent the results of the append col
5924 // operation to this view object as a grid table message
5930 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5932 // TODO: something with updateLabels flag
5936 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5942 if (IsCellEditControlEnabled())
5943 DisableCellEditControl();
5945 bool done
= m_table
->DeleteCols( pos
, numCols
);
5947 // the table will have sent the results of the delete col
5948 // operation to this view object as a grid table message
5956 // ----- event handlers
5959 // Generate a grid event based on a mouse event and
5960 // return the result of ProcessEvent()
5962 int wxGrid::SendEvent( const wxEventType type
,
5964 wxMouseEvent
& mouseEv
)
5969 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5971 int rowOrCol
= (row
== -1 ? col
: row
);
5973 wxGridSizeEvent
gridEvt( GetId(),
5977 mouseEv
.GetX() + GetRowLabelSize(),
5978 mouseEv
.GetY() + GetColLabelSize(),
5979 mouseEv
.ControlDown(),
5980 mouseEv
.ShiftDown(),
5982 mouseEv
.MetaDown() );
5984 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5985 vetoed
= !gridEvt
.IsAllowed();
5987 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5989 // Right now, it should _never_ end up here!
5990 wxGridRangeSelectEvent
gridEvt( GetId(),
5994 m_selectingBottomRight
,
5996 mouseEv
.ControlDown(),
5997 mouseEv
.ShiftDown(),
5999 mouseEv
.MetaDown() );
6001 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6002 vetoed
= !gridEvt
.IsAllowed();
6006 wxGridEvent
gridEvt( GetId(),
6010 mouseEv
.GetX() + GetRowLabelSize(),
6011 mouseEv
.GetY() + GetColLabelSize(),
6013 mouseEv
.ControlDown(),
6014 mouseEv
.ShiftDown(),
6016 mouseEv
.MetaDown() );
6017 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6018 vetoed
= !gridEvt
.IsAllowed();
6021 // A Veto'd event may not be `claimed' so test this first
6022 if (vetoed
) return -1;
6023 return claimed
? 1 : 0;
6027 // Generate a grid event of specified type and return the result
6028 // of ProcessEvent().
6030 int wxGrid::SendEvent( const wxEventType type
,
6036 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6038 int rowOrCol
= (row
== -1 ? col
: row
);
6040 wxGridSizeEvent
gridEvt( GetId(),
6045 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6046 vetoed
= !gridEvt
.IsAllowed();
6050 wxGridEvent
gridEvt( GetId(),
6055 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6056 vetoed
= !gridEvt
.IsAllowed();
6059 // A Veto'd event may not be `claimed' so test this first
6060 if (vetoed
) return -1;
6061 return claimed
? 1 : 0;
6065 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6067 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6070 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6072 // Don't do anything if between Begin/EndBatch...
6073 // EndBatch() will do all this on the last nested one anyway.
6074 if (! GetBatchCount())
6076 // Refresh to get correct scrolled position:
6077 wxScrolledWindow::Refresh(eraseb
,rect
);
6081 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6082 int width_label
, width_cell
, height_label
, height_cell
;
6085 //Copy rectangle can get scroll offsets..
6086 rect_x
= rect
->GetX();
6087 rect_y
= rect
->GetY();
6088 rectWidth
= rect
->GetWidth();
6089 rectHeight
= rect
->GetHeight();
6091 width_label
= m_rowLabelWidth
- rect_x
;
6092 if (width_label
> rectWidth
) width_label
= rectWidth
;
6094 height_label
= m_colLabelHeight
- rect_y
;
6095 if (height_label
> rectHeight
) height_label
= rectHeight
;
6097 if (rect_x
> m_rowLabelWidth
)
6099 x
= rect_x
- m_rowLabelWidth
;
6100 width_cell
= rectWidth
;
6105 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6108 if (rect_y
> m_colLabelHeight
)
6110 y
= rect_y
- m_colLabelHeight
;
6111 height_cell
= rectHeight
;
6116 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6119 // Paint corner label part intersecting rect.
6120 if ( width_label
> 0 && height_label
> 0 )
6122 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6123 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6126 // Paint col labels part intersecting rect.
6127 if ( width_cell
> 0 && height_label
> 0 )
6129 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6130 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6133 // Paint row labels part intersecting rect.
6134 if ( width_label
> 0 && height_cell
> 0 )
6136 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6137 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6140 // Paint cell area part intersecting rect.
6141 if ( width_cell
> 0 && height_cell
> 0 )
6143 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6144 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6149 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6150 m_colLabelWin
->Refresh(eraseb
, NULL
);
6151 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6152 m_gridWin
->Refresh(eraseb
, NULL
);
6157 void wxGrid::OnSize( wxSizeEvent
& event
)
6159 // position the child windows
6162 // don't call CalcDimensions() from here, the base class handles the size
6168 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6170 if ( m_inOnKeyDown
)
6172 // shouldn't be here - we are going round in circles...
6174 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6177 m_inOnKeyDown
= TRUE
;
6179 // propagate the event up and see if it gets processed
6181 wxWindow
*parent
= GetParent();
6182 wxKeyEvent
keyEvt( event
);
6183 keyEvt
.SetEventObject( parent
);
6185 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6188 // try local handlers
6190 switch ( event
.GetKeyCode() )
6193 if ( event
.ControlDown() )
6195 MoveCursorUpBlock( event
.ShiftDown() );
6199 MoveCursorUp( event
.ShiftDown() );
6204 if ( event
.ControlDown() )
6206 MoveCursorDownBlock( event
.ShiftDown() );
6210 MoveCursorDown( event
.ShiftDown() );
6215 if ( event
.ControlDown() )
6217 MoveCursorLeftBlock( event
.ShiftDown() );
6221 MoveCursorLeft( event
.ShiftDown() );
6226 if ( event
.ControlDown() )
6228 MoveCursorRightBlock( event
.ShiftDown() );
6232 MoveCursorRight( event
.ShiftDown() );
6237 case WXK_NUMPAD_ENTER
:
6238 if ( event
.ControlDown() )
6240 event
.Skip(); // to let the edit control have the return
6244 if ( GetGridCursorRow() < GetNumberRows()-1 )
6246 MoveCursorDown( event
.ShiftDown() );
6250 // at the bottom of a column
6251 HideCellEditControl();
6252 SaveEditControlValue();
6262 if (event
.ShiftDown())
6264 if ( GetGridCursorCol() > 0 )
6266 MoveCursorLeft( FALSE
);
6271 HideCellEditControl();
6272 SaveEditControlValue();
6277 if ( GetGridCursorCol() < GetNumberCols()-1 )
6279 MoveCursorRight( FALSE
);
6284 HideCellEditControl();
6285 SaveEditControlValue();
6291 if ( event
.ControlDown() )
6293 MakeCellVisible( 0, 0 );
6294 SetCurrentCell( 0, 0 );
6303 if ( event
.ControlDown() )
6305 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6306 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6323 if ( event
.ControlDown() )
6327 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6328 m_currentCellCoords
.GetCol(),
6329 event
.ControlDown(),
6336 if ( !IsEditable() )
6338 MoveCursorRight( FALSE
);
6341 // Otherwise fall through to default
6344 // is it possible to edit the current cell at all?
6345 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6347 // yes, now check whether the cells editor accepts the key
6348 int row
= m_currentCellCoords
.GetRow();
6349 int col
= m_currentCellCoords
.GetCol();
6350 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6351 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6353 // <F2> is special and will always start editing, for
6354 // other keys - ask the editor itself
6355 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6356 || editor
->IsAcceptedKey(event
) )
6358 // ensure cell is visble
6359 MakeCellVisible(row
, col
);
6360 EnableCellEditControl();
6362 // a problem can arise if the cell is not completely
6363 // visible (even after calling MakeCellVisible the
6364 // control is not created and calling StartingKey will
6366 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6378 // let others process char events with modifiers or all
6379 // char events for readonly cells
6386 m_inOnKeyDown
= FALSE
;
6389 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6391 // try local handlers
6393 if ( event
.GetKeyCode() == WXK_SHIFT
)
6395 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6396 m_selectingBottomRight
!= wxGridNoCellCoords
)
6400 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6401 m_selectingTopLeft
.GetCol(),
6402 m_selectingBottomRight
.GetRow(),
6403 m_selectingBottomRight
.GetCol(),
6404 event
.ControlDown(),
6411 m_selectingTopLeft
= wxGridNoCellCoords
;
6412 m_selectingBottomRight
= wxGridNoCellCoords
;
6413 m_selectingKeyboard
= wxGridNoCellCoords
;
6417 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6421 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6423 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6425 // the event has been intercepted - do nothing
6429 wxClientDC
dc(m_gridWin
);
6432 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6434 HideCellEditControl();
6435 DisableCellEditControl();
6437 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6440 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6441 if ( !m_gridLinesEnabled
)
6449 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6451 // Otherwise refresh redraws the highlight!
6452 m_currentCellCoords
= coords
;
6454 DrawGridCellArea(dc
,cells
);
6455 DrawAllGridLines( dc
, r
);
6459 m_currentCellCoords
= coords
;
6461 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6462 DrawCellHighlight(dc
, attr
);
6467 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6470 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6474 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6477 rightCol
= GetNumberCols() - 1;
6479 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6482 bottomRow
= GetNumberRows() - 1;
6486 if ( topRow
> bottomRow
)
6493 if ( leftCol
> rightCol
)
6500 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6501 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6503 // First the case that we selected a completely new area
6504 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6505 m_selectingBottomRight
== wxGridNoCellCoords
)
6508 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6509 wxGridCellCoords ( bottomRow
, rightCol
) );
6510 m_gridWin
->Refresh( FALSE
, &rect
);
6512 // Now handle changing an existing selection area.
6513 else if ( m_selectingTopLeft
!= updateTopLeft
||
6514 m_selectingBottomRight
!= updateBottomRight
)
6516 // Compute two optimal update rectangles:
6517 // Either one rectangle is a real subset of the
6518 // other, or they are (almost) disjoint!
6520 bool need_refresh
[4];
6524 need_refresh
[3] = FALSE
;
6527 // Store intermediate values
6528 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6529 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6530 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6531 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6533 // Determine the outer/inner coordinates.
6534 if (oldLeft
> leftCol
)
6540 if (oldTop
> topRow
)
6546 if (oldRight
< rightCol
)
6549 oldRight
= rightCol
;
6552 if (oldBottom
< bottomRow
)
6555 oldBottom
= bottomRow
;
6559 // Now, either the stuff marked old is the outer
6560 // rectangle or we don't have a situation where one
6561 // is contained in the other.
6563 if ( oldLeft
< leftCol
)
6565 // Refresh the newly selected or deselected
6566 // area to the left of the old or new selection.
6567 need_refresh
[0] = TRUE
;
6568 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6570 wxGridCellCoords ( oldBottom
,
6574 if ( oldTop
< topRow
)
6576 // Refresh the newly selected or deselected
6577 // area above the old or new selection.
6578 need_refresh
[1] = TRUE
;
6579 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6581 wxGridCellCoords ( topRow
- 1,
6585 if ( oldRight
> rightCol
)
6587 // Refresh the newly selected or deselected
6588 // area to the right of the old or new selection.
6589 need_refresh
[2] = TRUE
;
6590 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6592 wxGridCellCoords ( oldBottom
,
6596 if ( oldBottom
> bottomRow
)
6598 // Refresh the newly selected or deselected
6599 // area below the old or new selection.
6600 need_refresh
[3] = TRUE
;
6601 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6603 wxGridCellCoords ( oldBottom
,
6607 // various Refresh() calls
6608 for (i
= 0; i
< 4; i
++ )
6609 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6610 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6613 m_selectingTopLeft
= updateTopLeft
;
6614 m_selectingBottomRight
= updateBottomRight
;
6618 // ------ functions to get/send data (see also public functions)
6621 bool wxGrid::GetModelValues()
6623 // Hide the editor, so it won't hide a changed value.
6624 HideCellEditControl();
6628 // all we need to do is repaint the grid
6630 m_gridWin
->Refresh();
6638 bool wxGrid::SetModelValues()
6642 // Disable the editor, so it won't hide a changed value.
6643 // Do we also want to save the current value of the editor first?
6645 DisableCellEditControl();
6649 for ( row
= 0; row
< m_numRows
; row
++ )
6651 for ( col
= 0; col
< m_numCols
; col
++ )
6653 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6665 // Note - this function only draws cells that are in the list of
6666 // exposed cells (usually set from the update region by
6667 // CalcExposedCells)
6669 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6671 if ( !m_numRows
|| !m_numCols
) return;
6673 int i
, numCells
= cells
.GetCount();
6674 int row
, col
, cell_rows
, cell_cols
;
6675 wxGridCellCoordsArray redrawCells
;
6677 for ( i
= numCells
-1; i
>= 0; i
-- )
6679 row
= cells
[i
].GetRow();
6680 col
= cells
[i
].GetCol();
6681 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6683 // If this cell is part of a multicell block, find owner for repaint
6684 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6686 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6687 bool marked
= FALSE
;
6688 for ( int j
= 0; j
< numCells
; j
++ )
6690 if ( cell
== cells
[j
] )
6698 int count
= redrawCells
.GetCount();
6699 for (int j
= 0; j
< count
; j
++)
6701 if ( cell
== redrawCells
[j
] )
6707 if (!marked
) redrawCells
.Add( cell
);
6709 continue; // don't bother drawing this cell
6712 // If this cell is empty, find cell to left that might want to overflow
6713 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6715 for ( int l
= 0; l
< cell_rows
; l
++ )
6717 // find a cell in this row to left alreay marked for repaint
6719 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6720 if ((redrawCells
[k
].GetCol() < left
) &&
6721 (redrawCells
[k
].GetRow() == row
))
6722 left
=redrawCells
[k
].GetCol();
6724 if (left
== col
) left
= 0; // oh well
6726 for (int j
= col
-1; j
>= left
; j
--)
6728 if (!m_table
->IsEmptyCell(row
+l
, j
))
6730 if (GetCellOverflow(row
+l
, j
))
6732 wxGridCellCoords
cell(row
+l
, j
);
6733 bool marked
= FALSE
;
6735 for (int k
= 0; k
< numCells
; k
++)
6737 if ( cell
== cells
[k
] )
6745 int count
= redrawCells
.GetCount();
6746 for (int k
= 0; k
< count
; k
++)
6748 if ( cell
== redrawCells
[k
] )
6754 if (!marked
) redrawCells
.Add( cell
);
6762 DrawCell( dc
, cells
[i
] );
6765 numCells
= redrawCells
.GetCount();
6767 for ( i
= numCells
- 1; i
>= 0; i
-- )
6769 DrawCell( dc
, redrawCells
[i
] );
6774 void wxGrid::DrawGridSpace( wxDC
& dc
)
6777 m_gridWin
->GetClientSize( &cw
, &ch
);
6780 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6782 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6783 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6785 if ( right
> rightCol
|| bottom
> bottomRow
)
6788 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6790 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6791 dc
.SetPen( *wxTRANSPARENT_PEN
);
6793 if ( right
> rightCol
)
6795 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6798 if ( bottom
> bottomRow
)
6800 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6806 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6808 int row
= coords
.GetRow();
6809 int col
= coords
.GetCol();
6811 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6814 // we draw the cell border ourselves
6815 #if !WXGRID_DRAW_LINES
6816 if ( m_gridLinesEnabled
)
6817 DrawCellBorder( dc
, coords
);
6820 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6822 bool isCurrent
= coords
== m_currentCellCoords
;
6824 wxRect rect
= CellToRect( row
, col
);
6826 // if the editor is shown, we should use it and not the renderer
6827 // Note: However, only if it is really _shown_, i.e. not hidden!
6828 if ( isCurrent
&& IsCellEditControlShown() )
6830 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6831 editor
->PaintBackground(rect
, attr
);
6836 // but all the rest is drawn by the cell renderer and hence may be
6838 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6839 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6846 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6848 int row
= m_currentCellCoords
.GetRow();
6849 int col
= m_currentCellCoords
.GetCol();
6851 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6854 wxRect rect
= CellToRect(row
, col
);
6856 // hmmm... what could we do here to show that the cell is disabled?
6857 // for now, I just draw a thinner border than for the other ones, but
6858 // it doesn't look really good
6860 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6864 // The center of th drawn line is where the position/width/height of
6865 // the rectangle is actually at, (on wxMSW atr least,) so we will
6866 // reduce the size of the rectangle to compensate for the thickness of
6867 // the line. If this is too strange on non wxMSW platforms then
6868 // please #ifdef this appropriately.
6869 rect
.x
+= penWidth
/2;
6870 rect
.y
+= penWidth
/2;
6871 rect
.width
-= penWidth
-1;
6872 rect
.height
-= penWidth
-1;
6875 // Now draw the rectangle
6876 // use the cellHighlightColour if the cell is inside a selection, this
6877 // will ensure the cell is always visible.
6878 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6879 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6880 dc
.DrawRectangle(rect
);
6884 // VZ: my experiments with 3d borders...
6886 // how to properly set colours for arbitrary bg?
6887 wxCoord x1
= rect
.x
,
6889 x2
= rect
.x
+ rect
.width
-1,
6890 y2
= rect
.y
+ rect
.height
-1;
6892 dc
.SetPen(*wxWHITE_PEN
);
6893 dc
.DrawLine(x1
, y1
, x2
, y1
);
6894 dc
.DrawLine(x1
, y1
, x1
, y2
);
6896 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6897 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6899 dc
.SetPen(*wxBLACK_PEN
);
6900 dc
.DrawLine(x1
, y2
, x2
, y2
);
6901 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6906 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6908 int row
= coords
.GetRow();
6909 int col
= coords
.GetCol();
6910 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6913 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6915 wxRect rect
= CellToRect( row
, col
);
6917 // right hand border
6919 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6920 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6924 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6925 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6928 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6930 // This if block was previously in wxGrid::OnPaint but that doesn't
6931 // seem to get called under wxGTK - MB
6933 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6934 m_numRows
&& m_numCols
)
6936 m_currentCellCoords
.Set(0, 0);
6939 if ( IsCellEditControlShown() )
6941 // don't show highlight when the edit control is shown
6945 // if the active cell was repainted, repaint its highlight too because it
6946 // might have been damaged by the grid lines
6947 size_t count
= cells
.GetCount();
6948 for ( size_t n
= 0; n
< count
; n
++ )
6950 if ( cells
[n
] == m_currentCellCoords
)
6952 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6953 DrawCellHighlight(dc
, attr
);
6961 // TODO: remove this ???
6962 // This is used to redraw all grid lines e.g. when the grid line colour
6965 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6967 #if !WXGRID_DRAW_LINES
6971 if ( !m_gridLinesEnabled
||
6973 !m_numCols
) return;
6975 int top
, bottom
, left
, right
;
6977 #if 0 //#ifndef __WXGTK__
6981 m_gridWin
->GetClientSize(&cw
, &ch
);
6983 // virtual coords of visible area
6985 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6986 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6991 reg
.GetBox(x
, y
, w
, h
);
6992 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6993 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6997 m_gridWin
->GetClientSize(&cw
, &ch
);
6998 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6999 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7002 // avoid drawing grid lines past the last row and col
7004 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7005 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7007 // no gridlines inside multicells, clip them out
7008 int leftCol
= internalXToCol(left
);
7009 int topRow
= internalYToRow(top
);
7010 int rightCol
= internalXToCol(right
);
7011 int bottomRow
= internalYToRow(bottom
);
7012 wxRegion
clippedcells(0, 0, cw
, ch
);
7015 int i
, j
, cell_rows
, cell_cols
;
7018 for (j
=topRow
; j
<bottomRow
; j
++)
7020 for (i
=leftCol
; i
<rightCol
; i
++)
7022 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7023 if ((cell_rows
> 1) || (cell_cols
> 1))
7025 rect
= CellToRect(j
,i
);
7026 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7027 clippedcells
.Subtract(rect
);
7029 else if ((cell_rows
< 0) || (cell_cols
< 0))
7031 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7032 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7033 clippedcells
.Subtract(rect
);
7037 dc
.SetClippingRegion( clippedcells
);
7039 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7041 // horizontal grid lines
7043 // already declared above - int i;
7044 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7046 int bot
= GetRowBottom(i
) - 1;
7055 dc
.DrawLine( left
, bot
, right
, bot
);
7060 // vertical grid lines
7062 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7064 int colRight
= GetColRight(i
) - 1;
7065 if ( colRight
> right
)
7070 if ( colRight
>= left
)
7072 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7075 dc
.DestroyClippingRegion();
7079 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7081 if ( !m_numRows
) return;
7084 size_t numLabels
= rows
.GetCount();
7086 for ( i
= 0; i
< numLabels
; i
++ )
7088 DrawRowLabel( dc
, rows
[i
] );
7093 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7095 if ( GetRowHeight(row
) <= 0 )
7098 int rowTop
= GetRowTop(row
),
7099 rowBottom
= GetRowBottom(row
) - 1;
7101 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7102 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7103 m_rowLabelWidth
-1, rowBottom
);
7105 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7107 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7109 dc
.SetPen( *wxWHITE_PEN
);
7110 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7111 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7113 dc
.SetBackgroundMode( wxTRANSPARENT
);
7114 dc
.SetTextForeground( GetLabelTextColour() );
7115 dc
.SetFont( GetLabelFont() );
7118 GetRowLabelAlignment( &hAlign
, &vAlign
);
7122 rect
.SetY( GetRowTop(row
) + 2 );
7123 rect
.SetWidth( m_rowLabelWidth
- 4 );
7124 rect
.SetHeight( GetRowHeight(row
) - 4 );
7125 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7129 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7131 if ( !m_numCols
) return;
7134 size_t numLabels
= cols
.GetCount();
7136 for ( i
= 0; i
< numLabels
; i
++ )
7138 DrawColLabel( dc
, cols
[i
] );
7143 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7145 if ( GetColWidth(col
) <= 0 )
7148 int colLeft
= GetColLeft(col
),
7149 colRight
= GetColRight(col
) - 1;
7151 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7152 dc
.DrawLine( colRight
, 0,
7153 colRight
, m_colLabelHeight
-1 );
7155 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7157 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7158 colRight
+1, m_colLabelHeight
-1 );
7160 dc
.SetPen( *wxWHITE_PEN
);
7161 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7162 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7164 dc
.SetBackgroundMode( wxTRANSPARENT
);
7165 dc
.SetTextForeground( GetLabelTextColour() );
7166 dc
.SetFont( GetLabelFont() );
7168 int hAlign
, vAlign
, orient
;
7169 GetColLabelAlignment( &hAlign
, &vAlign
);
7170 orient
= GetColLabelTextOrientation();
7173 rect
.SetX( colLeft
+ 2 );
7175 rect
.SetWidth( GetColWidth(col
) - 4 );
7176 rect
.SetHeight( m_colLabelHeight
- 4 );
7177 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7180 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7181 const wxString
& value
,
7185 int textOrientation
)
7187 wxArrayString lines
;
7189 StringToLines( value
, lines
);
7192 //Forward to new API.
7193 DrawTextRectangle( dc
,
7202 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7203 const wxArrayString
& lines
,
7207 int textOrientation
)
7209 long textWidth
, textHeight
;
7210 long lineWidth
, lineHeight
;
7213 dc
.SetClippingRegion( rect
);
7215 nLines
= lines
.GetCount();
7219 float x
= 0.0, y
= 0.0;
7221 if( textOrientation
== wxHORIZONTAL
)
7222 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7224 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7228 case wxALIGN_BOTTOM
:
7229 if( textOrientation
== wxHORIZONTAL
)
7230 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7232 x
= rect
.x
+ rect
.width
- textWidth
;
7235 case wxALIGN_CENTRE
:
7236 if( textOrientation
== wxHORIZONTAL
)
7237 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7239 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7244 if( textOrientation
== wxHORIZONTAL
)
7251 // Align each line of a multi-line label
7252 for( l
= 0; l
< nLines
; l
++ )
7254 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7256 switch( horizAlign
)
7259 if( textOrientation
== wxHORIZONTAL
)
7260 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7262 y
= rect
.y
+ lineWidth
+ 1;
7265 case wxALIGN_CENTRE
:
7266 if( textOrientation
== wxHORIZONTAL
)
7267 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7269 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7274 if( textOrientation
== wxHORIZONTAL
)
7277 y
= rect
.y
+ rect
.height
- 1;
7281 if( textOrientation
== wxHORIZONTAL
)
7283 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7288 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7293 dc
.DestroyClippingRegion();
7297 // Split multi line text up into an array of strings. Any existing
7298 // contents of the string array are preserved.
7300 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7304 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7305 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7307 while ( startPos
< (int)tVal
.Length() )
7309 pos
= tVal
.Mid(startPos
).Find( eol
);
7314 else if ( pos
== 0 )
7316 lines
.Add( wxEmptyString
);
7320 lines
.Add( value
.Mid(startPos
, pos
) );
7324 if ( startPos
< (int)value
.Length() )
7326 lines
.Add( value
.Mid( startPos
) );
7331 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7332 const wxArrayString
& lines
,
7333 long *width
, long *height
)
7340 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7342 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7343 w
= wxMax( w
, lineW
);
7352 // ------ Batch processing.
7354 void wxGrid::EndBatch()
7356 if ( m_batchCount
> 0 )
7359 if ( !m_batchCount
)
7362 m_rowLabelWin
->Refresh();
7363 m_colLabelWin
->Refresh();
7364 m_cornerLabelWin
->Refresh();
7365 m_gridWin
->Refresh();
7370 // Use this, rather than wxWindow::Refresh(), to force an immediate
7371 // repainting of the grid. Has no effect if you are already inside a
7372 // BeginBatch / EndBatch block.
7374 void wxGrid::ForceRefresh()
7382 // ------ Edit control functions
7386 void wxGrid::EnableEditing( bool edit
)
7388 // TODO: improve this ?
7390 if ( edit
!= m_editable
)
7392 if(!edit
) EnableCellEditControl(edit
);
7398 void wxGrid::EnableCellEditControl( bool enable
)
7403 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7404 SetCurrentCell( 0, 0 );
7406 if ( enable
!= m_cellEditCtrlEnabled
)
7410 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7413 // this should be checked by the caller!
7414 wxASSERT_MSG( CanEnableCellControl(),
7415 _T("can't enable editing for this cell!") );
7417 // do it before ShowCellEditControl()
7418 m_cellEditCtrlEnabled
= enable
;
7420 ShowCellEditControl();
7424 //FIXME:add veto support
7425 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7427 HideCellEditControl();
7428 SaveEditControlValue();
7430 // do it after HideCellEditControl()
7431 m_cellEditCtrlEnabled
= enable
;
7436 bool wxGrid::IsCurrentCellReadOnly() const
7439 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7440 bool readonly
= attr
->IsReadOnly();
7446 bool wxGrid::CanEnableCellControl() const
7448 return m_editable
&& !IsCurrentCellReadOnly();
7451 bool wxGrid::IsCellEditControlEnabled() const
7453 // the cell edit control might be disable for all cells or just for the
7454 // current one if it's read only
7455 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7458 bool wxGrid::IsCellEditControlShown() const
7460 bool isShown
= FALSE
;
7462 if ( m_cellEditCtrlEnabled
)
7464 int row
= m_currentCellCoords
.GetRow();
7465 int col
= m_currentCellCoords
.GetCol();
7466 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7467 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7472 if ( editor
->IsCreated() )
7474 isShown
= editor
->GetControl()->IsShown();
7484 void wxGrid::ShowCellEditControl()
7486 if ( IsCellEditControlEnabled() )
7488 if ( !IsVisible( m_currentCellCoords
) )
7490 m_cellEditCtrlEnabled
= FALSE
;
7495 wxRect rect
= CellToRect( m_currentCellCoords
);
7496 int row
= m_currentCellCoords
.GetRow();
7497 int col
= m_currentCellCoords
.GetCol();
7499 // if this is part of a multicell, find owner (topleft)
7500 int cell_rows
, cell_cols
;
7501 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7502 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7506 m_currentCellCoords
.SetRow( row
);
7507 m_currentCellCoords
.SetCol( col
);
7510 // convert to scrolled coords
7512 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7514 // done in PaintBackground()
7516 // erase the highlight and the cell contents because the editor
7517 // might not cover the entire cell
7518 wxClientDC
dc( m_gridWin
);
7520 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7521 dc
.SetPen(*wxTRANSPARENT_PEN
);
7522 dc
.DrawRectangle(rect
);
7525 // cell is shifted by one pixel
7526 // However, don't allow x or y to become negative
7527 // since the SetSize() method interprets that as
7534 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7535 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7536 if ( !editor
->IsCreated() )
7538 editor
->Create(m_gridWin
, -1,
7539 new wxGridCellEditorEvtHandler(this, editor
));
7541 wxGridEditorCreatedEvent
evt(GetId(),
7542 wxEVT_GRID_EDITOR_CREATED
,
7546 editor
->GetControl());
7547 GetEventHandler()->ProcessEvent(evt
);
7551 // resize editor to overflow into righthand cells if allowed
7552 int maxWidth
= rect
.width
;
7553 wxString value
= GetCellValue(row
, col
);
7554 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7557 GetTextExtent(value
, &maxWidth
, &y
,
7558 NULL
, NULL
, &attr
->GetFont());
7559 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7561 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7562 if (rect
.x
+maxWidth
> client_right
)
7563 maxWidth
= client_right
- rect
.x
;
7565 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7567 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7568 // may have changed earlier
7569 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7572 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7573 // looks weird going over a multicell
7574 if (m_table
->IsEmptyCell(row
,i
) &&
7575 (rect
.width
< maxWidth
) && (c_rows
== 1))
7576 rect
.width
+= GetColWidth(i
);
7580 if (rect
.GetRight() > client_right
)
7581 rect
.SetRight(client_right
-1);
7584 editor
->SetCellAttr(attr
);
7585 editor
->SetSize( rect
);
7586 editor
->Show( TRUE
, attr
);
7588 // recalc dimensions in case we need to
7589 // expand the scrolled window to account for editor
7592 editor
->BeginEdit(row
, col
, this);
7593 editor
->SetCellAttr(NULL
);
7602 void wxGrid::HideCellEditControl()
7604 if ( IsCellEditControlEnabled() )
7606 int row
= m_currentCellCoords
.GetRow();
7607 int col
= m_currentCellCoords
.GetCol();
7609 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7610 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7611 editor
->Show( FALSE
);
7614 m_gridWin
->SetFocus();
7615 // refresh whole row to the right
7616 wxRect
rect( CellToRect(row
, col
) );
7617 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7618 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7619 m_gridWin
->Refresh( FALSE
, &rect
);
7624 void wxGrid::SaveEditControlValue()
7626 if ( IsCellEditControlEnabled() )
7628 int row
= m_currentCellCoords
.GetRow();
7629 int col
= m_currentCellCoords
.GetCol();
7631 wxString oldval
= GetCellValue(row
,col
);
7633 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7634 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7635 bool changed
= editor
->EndEdit(row
, col
, this);
7642 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7643 m_currentCellCoords
.GetRow(),
7644 m_currentCellCoords
.GetCol() ) < 0 ) {
7646 // Event has been vetoed, set the data back.
7647 SetCellValue(row
,col
,oldval
);
7655 // ------ Grid location functions
7656 // Note that all of these functions work with the logical coordinates of
7657 // grid cells and labels so you will need to convert from device
7658 // coordinates for mouse events etc.
7661 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7663 int row
= YToRow(y
);
7664 int col
= XToCol(x
);
7666 if ( row
== -1 || col
== -1 )
7668 coords
= wxGridNoCellCoords
;
7672 coords
.Set( row
, col
);
7677 // Internal Helper function for computing row or column from some
7678 // (unscrolled) coordinate value, using either
7679 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7680 // of m_rowBottoms/m_ColRights to speed up the search!
7682 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7683 const wxArrayInt
& BorderArray
, int nMax
,
7688 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7694 size_t i_max
= coord
/ defaultDist
,
7697 if (BorderArray
.IsEmpty())
7699 if((int) i_max
< nMax
)
7701 return clipToMinMax
? nMax
- 1 : -1;
7704 if ( i_max
>= BorderArray
.GetCount())
7705 i_max
= BorderArray
.GetCount() - 1;
7708 if ( coord
>= BorderArray
[i_max
])
7711 i_max
= coord
/ minDist
;
7713 if ( i_max
>= BorderArray
.GetCount())
7714 i_max
= BorderArray
.GetCount() - 1;
7716 if ( coord
>= BorderArray
[i_max
])
7717 return clipToMinMax
? (int)i_max
: -1;
7718 if ( coord
< BorderArray
[0] )
7721 while ( i_max
- i_min
> 0 )
7723 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7724 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7725 if (coord
>= BorderArray
[ i_max
- 1])
7729 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7730 if (coord
< BorderArray
[median
])
7738 int wxGrid::YToRow( int y
)
7740 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7741 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, FALSE
);
7745 int wxGrid::XToCol( int x
)
7747 return CoordToRowOrCol(x
, m_defaultColWidth
,
7748 m_minAcceptableColWidth
, m_colRights
, m_numCols
, FALSE
);
7752 // return the row number that that the y coord is near the edge of, or
7753 // -1 if not near an edge
7755 int wxGrid::YToEdgeOfRow( int y
)
7758 i
= internalYToRow(y
);
7760 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7762 // We know that we are in row i, test whether we are
7763 // close enough to lower or upper border, respectively.
7764 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7766 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7774 // return the col number that that the x coord is near the edge of, or
7775 // -1 if not near an edge
7777 int wxGrid::XToEdgeOfCol( int x
)
7780 i
= internalXToCol(x
);
7782 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7784 // We know that we are in column i, test whether we are
7785 // close enough to right or left border, respectively.
7786 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7788 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7796 wxRect
wxGrid::CellToRect( int row
, int col
)
7798 wxRect
rect( -1, -1, -1, -1 );
7800 if ( row
>= 0 && row
< m_numRows
&&
7801 col
>= 0 && col
< m_numCols
)
7803 int i
, cell_rows
, cell_cols
;
7804 rect
.width
= rect
.height
= 0;
7805 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7806 // if negative then find multicell owner
7807 if (cell_rows
< 0) row
+= cell_rows
;
7808 if (cell_cols
< 0) col
+= cell_cols
;
7809 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7811 rect
.x
= GetColLeft(col
);
7812 rect
.y
= GetRowTop(row
);
7813 for (i
=col
; i
<col
+cell_cols
; i
++)
7814 rect
.width
+= GetColWidth(i
);
7815 for (i
=row
; i
<row
+cell_rows
; i
++)
7816 rect
.height
+= GetRowHeight(i
);
7819 // if grid lines are enabled, then the area of the cell is a bit smaller
7820 if (m_gridLinesEnabled
) {
7828 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7830 // get the cell rectangle in logical coords
7832 wxRect
r( CellToRect( row
, col
) );
7834 // convert to device coords
7836 int left
, top
, right
, bottom
;
7837 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7838 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7840 // check against the client area of the grid window
7843 m_gridWin
->GetClientSize( &cw
, &ch
);
7845 if ( wholeCellVisible
)
7847 // is the cell wholly visible ?
7849 return ( left
>= 0 && right
<= cw
&&
7850 top
>= 0 && bottom
<= ch
);
7854 // is the cell partly visible ?
7856 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7857 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7862 // make the specified cell location visible by doing a minimal amount
7865 void wxGrid::MakeCellVisible( int row
, int col
)
7869 int xpos
= -1, ypos
= -1;
7871 if ( row
>= 0 && row
< m_numRows
&&
7872 col
>= 0 && col
< m_numCols
)
7874 // get the cell rectangle in logical coords
7876 wxRect
r( CellToRect( row
, col
) );
7878 // convert to device coords
7880 int left
, top
, right
, bottom
;
7881 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7882 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7885 m_gridWin
->GetClientSize( &cw
, &ch
);
7891 else if ( bottom
> ch
)
7893 int h
= r
.GetHeight();
7895 for ( i
= row
-1; i
>= 0; i
-- )
7897 int rowHeight
= GetRowHeight(i
);
7898 if ( h
+ rowHeight
> ch
)
7905 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7906 // have rounding errors (this is important, because if we do, we
7907 // might not scroll at all and some cells won't be redrawn)
7909 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7911 ypos
+= GRID_SCROLL_LINE_Y
;
7918 else if ( right
> cw
)
7920 // position the view so that the cell is on the right
7922 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
7923 xpos
= x0
+ (right
- cw
);
7925 // see comment for ypos above
7926 xpos
+= GRID_SCROLL_LINE_X
;
7929 if ( xpos
!= -1 || ypos
!= -1 )
7932 xpos
/= GRID_SCROLL_LINE_X
;
7934 ypos
/= GRID_SCROLL_LINE_Y
;
7935 Scroll( xpos
, ypos
);
7943 // ------ Grid cursor movement functions
7946 bool wxGrid::MoveCursorUp( bool expandSelection
)
7948 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7949 m_currentCellCoords
.GetRow() >= 0 )
7951 if ( expandSelection
)
7953 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7954 m_selectingKeyboard
= m_currentCellCoords
;
7955 if ( m_selectingKeyboard
.GetRow() > 0 )
7957 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7958 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7959 m_selectingKeyboard
.GetCol() );
7960 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7963 else if ( m_currentCellCoords
.GetRow() > 0 )
7966 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7967 m_currentCellCoords
.GetCol() );
7968 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7969 m_currentCellCoords
.GetCol() );
7980 bool wxGrid::MoveCursorDown( bool expandSelection
)
7982 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7983 m_currentCellCoords
.GetRow() < m_numRows
)
7985 if ( expandSelection
)
7987 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7988 m_selectingKeyboard
= m_currentCellCoords
;
7989 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7991 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7992 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7993 m_selectingKeyboard
.GetCol() );
7994 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7997 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8000 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8001 m_currentCellCoords
.GetCol() );
8002 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8003 m_currentCellCoords
.GetCol() );
8014 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8016 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8017 m_currentCellCoords
.GetCol() >= 0 )
8019 if ( expandSelection
)
8021 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8022 m_selectingKeyboard
= m_currentCellCoords
;
8023 if ( m_selectingKeyboard
.GetCol() > 0 )
8025 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8026 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8027 m_selectingKeyboard
.GetCol() );
8028 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8031 else if ( m_currentCellCoords
.GetCol() > 0 )
8034 MakeCellVisible( m_currentCellCoords
.GetRow(),
8035 m_currentCellCoords
.GetCol() - 1 );
8036 SetCurrentCell( m_currentCellCoords
.GetRow(),
8037 m_currentCellCoords
.GetCol() - 1 );
8048 bool wxGrid::MoveCursorRight( bool expandSelection
)
8050 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8051 m_currentCellCoords
.GetCol() < m_numCols
)
8053 if ( expandSelection
)
8055 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8056 m_selectingKeyboard
= m_currentCellCoords
;
8057 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8059 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8060 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8061 m_selectingKeyboard
.GetCol() );
8062 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8065 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8068 MakeCellVisible( m_currentCellCoords
.GetRow(),
8069 m_currentCellCoords
.GetCol() + 1 );
8070 SetCurrentCell( m_currentCellCoords
.GetRow(),
8071 m_currentCellCoords
.GetCol() + 1 );
8082 bool wxGrid::MovePageUp()
8084 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8086 int row
= m_currentCellCoords
.GetRow();
8090 m_gridWin
->GetClientSize( &cw
, &ch
);
8092 int y
= GetRowTop(row
);
8093 int newRow
= internalYToRow( y
- ch
+ 1 );
8095 if ( newRow
== row
)
8097 //row > 0 , so newrow can never be less than 0 here.
8101 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8102 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8110 bool wxGrid::MovePageDown()
8112 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8114 int row
= m_currentCellCoords
.GetRow();
8115 if ( (row
+1) < m_numRows
)
8118 m_gridWin
->GetClientSize( &cw
, &ch
);
8120 int y
= GetRowTop(row
);
8121 int newRow
= internalYToRow( y
+ ch
);
8122 if ( newRow
== row
)
8124 // row < m_numRows , so newrow can't overflow here.
8128 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8129 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8137 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8140 m_currentCellCoords
!= wxGridNoCellCoords
&&
8141 m_currentCellCoords
.GetRow() > 0 )
8143 int row
= m_currentCellCoords
.GetRow();
8144 int col
= m_currentCellCoords
.GetCol();
8146 if ( m_table
->IsEmptyCell(row
, col
) )
8148 // starting in an empty cell: find the next block of
8154 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8157 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8159 // starting at the top of a block: find the next block
8165 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8170 // starting within a block: find the top of the block
8175 if ( m_table
->IsEmptyCell(row
, col
) )
8183 MakeCellVisible( row
, col
);
8184 if ( expandSelection
)
8186 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8187 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8192 SetCurrentCell( row
, col
);
8200 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8203 m_currentCellCoords
!= wxGridNoCellCoords
&&
8204 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8206 int row
= m_currentCellCoords
.GetRow();
8207 int col
= m_currentCellCoords
.GetCol();
8209 if ( m_table
->IsEmptyCell(row
, col
) )
8211 // starting in an empty cell: find the next block of
8214 while ( row
< m_numRows
-1 )
8217 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8220 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8222 // starting at the bottom of a block: find the next block
8225 while ( row
< m_numRows
-1 )
8228 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8233 // starting within a block: find the bottom of the block
8235 while ( row
< m_numRows
-1 )
8238 if ( m_table
->IsEmptyCell(row
, col
) )
8246 MakeCellVisible( row
, col
);
8247 if ( expandSelection
)
8249 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8250 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8255 SetCurrentCell( row
, col
);
8264 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8267 m_currentCellCoords
!= wxGridNoCellCoords
&&
8268 m_currentCellCoords
.GetCol() > 0 )
8270 int row
= m_currentCellCoords
.GetRow();
8271 int col
= m_currentCellCoords
.GetCol();
8273 if ( m_table
->IsEmptyCell(row
, col
) )
8275 // starting in an empty cell: find the next block of
8281 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8284 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8286 // starting at the left of a block: find the next block
8292 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8297 // starting within a block: find the left of the block
8302 if ( m_table
->IsEmptyCell(row
, col
) )
8310 MakeCellVisible( row
, col
);
8311 if ( expandSelection
)
8313 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8314 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8319 SetCurrentCell( row
, col
);
8328 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8331 m_currentCellCoords
!= wxGridNoCellCoords
&&
8332 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8334 int row
= m_currentCellCoords
.GetRow();
8335 int col
= m_currentCellCoords
.GetCol();
8337 if ( m_table
->IsEmptyCell(row
, col
) )
8339 // starting in an empty cell: find the next block of
8342 while ( col
< m_numCols
-1 )
8345 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8348 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8350 // starting at the right of a block: find the next block
8353 while ( col
< m_numCols
-1 )
8356 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8361 // starting within a block: find the right of the block
8363 while ( col
< m_numCols
-1 )
8366 if ( m_table
->IsEmptyCell(row
, col
) )
8374 MakeCellVisible( row
, col
);
8375 if ( expandSelection
)
8377 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8378 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8383 SetCurrentCell( row
, col
);
8395 // ------ Label values and formatting
8398 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8400 *horiz
= m_rowLabelHorizAlign
;
8401 *vert
= m_rowLabelVertAlign
;
8404 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8406 *horiz
= m_colLabelHorizAlign
;
8407 *vert
= m_colLabelVertAlign
;
8410 int wxGrid::GetColLabelTextOrientation()
8412 return m_colLabelTextOrientation
;
8415 wxString
wxGrid::GetRowLabelValue( int row
)
8419 return m_table
->GetRowLabelValue( row
);
8429 wxString
wxGrid::GetColLabelValue( int col
)
8433 return m_table
->GetColLabelValue( col
);
8444 void wxGrid::SetRowLabelSize( int width
)
8446 width
= wxMax( width
, 0 );
8447 if ( width
!= m_rowLabelWidth
)
8451 m_rowLabelWin
->Show( FALSE
);
8452 m_cornerLabelWin
->Show( FALSE
);
8454 else if ( m_rowLabelWidth
== 0 )
8456 m_rowLabelWin
->Show( TRUE
);
8457 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8460 m_rowLabelWidth
= width
;
8462 wxScrolledWindow::Refresh( TRUE
);
8467 void wxGrid::SetColLabelSize( int height
)
8469 height
= wxMax( height
, 0 );
8470 if ( height
!= m_colLabelHeight
)
8474 m_colLabelWin
->Show( FALSE
);
8475 m_cornerLabelWin
->Show( FALSE
);
8477 else if ( m_colLabelHeight
== 0 )
8479 m_colLabelWin
->Show( TRUE
);
8480 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8483 m_colLabelHeight
= height
;
8485 wxScrolledWindow::Refresh( TRUE
);
8490 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8492 if ( m_labelBackgroundColour
!= colour
)
8494 m_labelBackgroundColour
= colour
;
8495 m_rowLabelWin
->SetBackgroundColour( colour
);
8496 m_colLabelWin
->SetBackgroundColour( colour
);
8497 m_cornerLabelWin
->SetBackgroundColour( colour
);
8499 if ( !GetBatchCount() )
8501 m_rowLabelWin
->Refresh();
8502 m_colLabelWin
->Refresh();
8503 m_cornerLabelWin
->Refresh();
8508 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8510 if ( m_labelTextColour
!= colour
)
8512 m_labelTextColour
= colour
;
8513 if ( !GetBatchCount() )
8515 m_rowLabelWin
->Refresh();
8516 m_colLabelWin
->Refresh();
8521 void wxGrid::SetLabelFont( const wxFont
& font
)
8524 if ( !GetBatchCount() )
8526 m_rowLabelWin
->Refresh();
8527 m_colLabelWin
->Refresh();
8531 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8533 // allow old (incorrect) defs to be used
8536 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8537 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8538 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8543 case wxTOP
: vert
= wxALIGN_TOP
; break;
8544 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8545 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8548 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8550 m_rowLabelHorizAlign
= horiz
;
8553 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8555 m_rowLabelVertAlign
= vert
;
8558 if ( !GetBatchCount() )
8560 m_rowLabelWin
->Refresh();
8564 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8566 // allow old (incorrect) defs to be used
8569 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8570 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8571 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8576 case wxTOP
: vert
= wxALIGN_TOP
; break;
8577 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8578 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8581 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8583 m_colLabelHorizAlign
= horiz
;
8586 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8588 m_colLabelVertAlign
= vert
;
8591 if ( !GetBatchCount() )
8593 m_colLabelWin
->Refresh();
8597 // Note: under MSW, the default column label font must be changed because it
8598 // does not support vertical printing
8600 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8601 // pGrid->SetLabelFont(font);
8602 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8604 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8606 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8608 m_colLabelTextOrientation
= textOrientation
;
8611 if ( !GetBatchCount() )
8613 m_colLabelWin
->Refresh();
8617 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8621 m_table
->SetRowLabelValue( row
, s
);
8622 if ( !GetBatchCount() )
8624 wxRect rect
= CellToRect( row
, 0);
8625 if ( rect
.height
> 0 )
8627 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8629 rect
.width
= m_rowLabelWidth
;
8630 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8636 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8640 m_table
->SetColLabelValue( col
, s
);
8641 if ( !GetBatchCount() )
8643 wxRect rect
= CellToRect( 0, col
);
8644 if ( rect
.width
> 0 )
8646 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8648 rect
.height
= m_colLabelHeight
;
8649 m_colLabelWin
->Refresh( TRUE
, &rect
);
8655 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8657 if ( m_gridLineColour
!= colour
)
8659 m_gridLineColour
= colour
;
8661 wxClientDC
dc( m_gridWin
);
8663 DrawAllGridLines( dc
, wxRegion() );
8668 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8670 if ( m_cellHighlightColour
!= colour
)
8672 m_cellHighlightColour
= colour
;
8674 wxClientDC
dc( m_gridWin
);
8676 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8677 DrawCellHighlight(dc
, attr
);
8682 void wxGrid::SetCellHighlightPenWidth(int width
)
8684 if (m_cellHighlightPenWidth
!= width
) {
8685 m_cellHighlightPenWidth
= width
;
8687 // Just redrawing the cell highlight is not enough since that won't
8688 // make any visible change if the the thickness is getting smaller.
8689 int row
= m_currentCellCoords
.GetRow();
8690 int col
= m_currentCellCoords
.GetCol();
8691 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8693 wxRect rect
= CellToRect(row
, col
);
8694 m_gridWin
->Refresh(TRUE
, &rect
);
8698 void wxGrid::SetCellHighlightROPenWidth(int width
)
8700 if (m_cellHighlightROPenWidth
!= width
) {
8701 m_cellHighlightROPenWidth
= width
;
8703 // Just redrawing the cell highlight is not enough since that won't
8704 // make any visible change if the the thickness is getting smaller.
8705 int row
= m_currentCellCoords
.GetRow();
8706 int col
= m_currentCellCoords
.GetCol();
8707 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8709 wxRect rect
= CellToRect(row
, col
);
8710 m_gridWin
->Refresh(TRUE
, &rect
);
8714 void wxGrid::EnableGridLines( bool enable
)
8716 if ( enable
!= m_gridLinesEnabled
)
8718 m_gridLinesEnabled
= enable
;
8720 if ( !GetBatchCount() )
8724 wxClientDC
dc( m_gridWin
);
8726 DrawAllGridLines( dc
, wxRegion() );
8730 m_gridWin
->Refresh();
8737 int wxGrid::GetDefaultRowSize()
8739 return m_defaultRowHeight
;
8742 int wxGrid::GetRowSize( int row
)
8744 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8746 return GetRowHeight(row
);
8749 int wxGrid::GetDefaultColSize()
8751 return m_defaultColWidth
;
8754 int wxGrid::GetColSize( int col
)
8756 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8758 return GetColWidth(col
);
8761 // ============================================================================
8762 // access to the grid attributes: each of them has a default value in the grid
8763 // itself and may be overidden on a per-cell basis
8764 // ============================================================================
8766 // ----------------------------------------------------------------------------
8767 // setting default attributes
8768 // ----------------------------------------------------------------------------
8770 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8772 m_defaultCellAttr
->SetBackgroundColour(col
);
8774 m_gridWin
->SetBackgroundColour(col
);
8778 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8780 m_defaultCellAttr
->SetTextColour(col
);
8783 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8785 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8788 void wxGrid::SetDefaultCellOverflow( bool allow
)
8790 m_defaultCellAttr
->SetOverflow(allow
);
8793 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8795 m_defaultCellAttr
->SetFont(font
);
8798 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8800 m_defaultCellAttr
->SetRenderer(renderer
);
8803 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8805 m_defaultCellAttr
->SetEditor(editor
);
8808 // ----------------------------------------------------------------------------
8809 // access to the default attrbiutes
8810 // ----------------------------------------------------------------------------
8812 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8814 return m_defaultCellAttr
->GetBackgroundColour();
8817 wxColour
wxGrid::GetDefaultCellTextColour()
8819 return m_defaultCellAttr
->GetTextColour();
8822 wxFont
wxGrid::GetDefaultCellFont()
8824 return m_defaultCellAttr
->GetFont();
8827 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8829 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8832 bool wxGrid::GetDefaultCellOverflow()
8834 return m_defaultCellAttr
->GetOverflow();
8837 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8839 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8842 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8844 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8847 // ----------------------------------------------------------------------------
8848 // access to cell attributes
8849 // ----------------------------------------------------------------------------
8851 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8853 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8854 wxColour colour
= attr
->GetBackgroundColour();
8859 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8861 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8862 wxColour colour
= attr
->GetTextColour();
8867 wxFont
wxGrid::GetCellFont( int row
, int col
)
8869 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8870 wxFont font
= attr
->GetFont();
8875 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8877 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8878 attr
->GetAlignment(horiz
, vert
);
8882 bool wxGrid::GetCellOverflow( int row
, int col
)
8884 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8885 bool allow
= attr
->GetOverflow();
8890 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8892 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8893 attr
->GetSize( num_rows
, num_cols
);
8897 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8899 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8900 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8906 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8908 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8909 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8915 bool wxGrid::IsReadOnly(int row
, int col
) const
8917 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8918 bool isReadOnly
= attr
->IsReadOnly();
8923 // ----------------------------------------------------------------------------
8924 // attribute support: cache, automatic provider creation, ...
8925 // ----------------------------------------------------------------------------
8927 bool wxGrid::CanHaveAttributes()
8934 return m_table
->CanHaveAttributes();
8937 void wxGrid::ClearAttrCache()
8939 if ( m_attrCache
.row
!= -1 )
8941 wxSafeDecRef(m_attrCache
.attr
);
8942 m_attrCache
.attr
= NULL
;
8943 m_attrCache
.row
= -1;
8947 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8951 wxGrid
*self
= (wxGrid
*)this; // const_cast
8953 self
->ClearAttrCache();
8954 self
->m_attrCache
.row
= row
;
8955 self
->m_attrCache
.col
= col
;
8956 self
->m_attrCache
.attr
= attr
;
8961 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8963 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8965 *attr
= m_attrCache
.attr
;
8966 wxSafeIncRef(m_attrCache
.attr
);
8968 #ifdef DEBUG_ATTR_CACHE
8969 gs_nAttrCacheHits
++;
8976 #ifdef DEBUG_ATTR_CACHE
8977 gs_nAttrCacheMisses
++;
8983 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8985 wxGridCellAttr
*attr
= NULL
;
8986 // Additional test to avoid looking at the cache e.g. for
8987 // wxNoCellCoords, as this will confuse memory management.
8990 if ( !LookupAttr(row
, col
, &attr
) )
8992 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8993 : (wxGridCellAttr
*)NULL
;
8994 CacheAttr(row
, col
, attr
);
8999 attr
->SetDefAttr(m_defaultCellAttr
);
9003 attr
= m_defaultCellAttr
;
9010 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9012 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9014 wxCHECK_MSG( m_table
, attr
,
9015 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
9017 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9020 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9022 // artificially inc the ref count to match DecRef() in caller
9024 m_table
->SetAttr(attr
, row
, col
);
9030 // ----------------------------------------------------------------------------
9031 // setting column attributes (wrappers around SetColAttr)
9032 // ----------------------------------------------------------------------------
9034 void wxGrid::SetColFormatBool(int col
)
9036 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9039 void wxGrid::SetColFormatNumber(int col
)
9041 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9044 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9046 wxString typeName
= wxGRID_VALUE_FLOAT
;
9047 if ( (width
!= -1) || (precision
!= -1) )
9049 typeName
<< _T(':') << width
<< _T(',') << precision
;
9052 SetColFormatCustom(col
, typeName
);
9055 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9057 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9059 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9061 attr
= new wxGridCellAttr
;
9062 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9063 attr
->SetRenderer(renderer
);
9065 SetColAttr(col
, attr
);
9069 // ----------------------------------------------------------------------------
9070 // setting cell attributes: this is forwarded to the table
9071 // ----------------------------------------------------------------------------
9073 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9075 if ( CanHaveAttributes() )
9077 m_table
->SetAttr(attr
, row
, col
);
9086 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9088 if ( CanHaveAttributes() )
9090 m_table
->SetRowAttr(attr
, row
);
9099 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9101 if ( CanHaveAttributes() )
9103 m_table
->SetColAttr(attr
, col
);
9112 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9114 if ( CanHaveAttributes() )
9116 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9117 attr
->SetBackgroundColour(colour
);
9122 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9124 if ( CanHaveAttributes() )
9126 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9127 attr
->SetTextColour(colour
);
9132 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9134 if ( CanHaveAttributes() )
9136 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9137 attr
->SetFont(font
);
9142 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9144 if ( CanHaveAttributes() )
9146 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9147 attr
->SetAlignment(horiz
, vert
);
9152 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9154 if ( CanHaveAttributes() )
9156 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9157 attr
->SetOverflow(allow
);
9162 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9164 if ( CanHaveAttributes() )
9166 int cell_rows
, cell_cols
;
9168 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9169 attr
->GetSize(&cell_rows
, &cell_cols
);
9170 attr
->SetSize(num_rows
, num_cols
);
9173 // Cannot set the size of a cell to 0 or negative values
9174 // While it is perfectly legal to do that, this function cannot
9175 // handle all the possibilies, do it by hand by getting the CellAttr.
9176 // You can only set the size of a cell to 1,1 or greater with this fn
9177 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9178 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9179 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9180 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9182 // if this was already a multicell then "turn off" the other cells first
9183 if ((cell_rows
> 1) || (cell_rows
> 1))
9186 for (j
=row
; j
<row
+cell_rows
; j
++)
9188 for (i
=col
; i
<col
+cell_cols
; i
++)
9190 if ((i
!= col
) || (j
!= row
))
9192 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9193 attr_stub
->SetSize( 1, 1 );
9194 attr_stub
->DecRef();
9200 // mark the cells that will be covered by this cell to
9201 // negative or zero values to point back at this cell
9202 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9205 for (j
=row
; j
<row
+num_rows
; j
++)
9207 for (i
=col
; i
<col
+num_cols
; i
++)
9209 if ((i
!= col
) || (j
!= row
))
9211 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9212 attr_stub
->SetSize( row
-j
, col
-i
);
9213 attr_stub
->DecRef();
9221 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9223 if ( CanHaveAttributes() )
9225 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9226 attr
->SetRenderer(renderer
);
9231 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9233 if ( CanHaveAttributes() )
9235 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9236 attr
->SetEditor(editor
);
9241 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9243 if ( CanHaveAttributes() )
9245 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9246 attr
->SetReadOnly(isReadOnly
);
9251 // ----------------------------------------------------------------------------
9252 // Data type registration
9253 // ----------------------------------------------------------------------------
9255 void wxGrid::RegisterDataType(const wxString
& typeName
,
9256 wxGridCellRenderer
* renderer
,
9257 wxGridCellEditor
* editor
)
9259 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9263 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9265 wxString typeName
= m_table
->GetTypeName(row
, col
);
9266 return GetDefaultEditorForType(typeName
);
9269 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9271 wxString typeName
= m_table
->GetTypeName(row
, col
);
9272 return GetDefaultRendererForType(typeName
);
9276 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9278 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9279 if ( index
== wxNOT_FOUND
)
9281 wxFAIL_MSG(wxT("Unknown data type name"));
9286 return m_typeRegistry
->GetEditor(index
);
9290 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9292 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9293 if ( index
== wxNOT_FOUND
)
9295 wxFAIL_MSG(wxT("Unknown data type name"));
9300 return m_typeRegistry
->GetRenderer(index
);
9304 // ----------------------------------------------------------------------------
9306 // ----------------------------------------------------------------------------
9308 void wxGrid::EnableDragRowSize( bool enable
)
9310 m_canDragRowSize
= enable
;
9314 void wxGrid::EnableDragColSize( bool enable
)
9316 m_canDragColSize
= enable
;
9319 void wxGrid::EnableDragGridSize( bool enable
)
9321 m_canDragGridSize
= enable
;
9325 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9327 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9329 if ( resizeExistingRows
)
9331 // since we are resizing all rows to the default row size,
9332 // we can simply clear the row heights and row bottoms
9333 // arrays (which also allows us to take advantage of
9334 // some speed optimisations)
9335 m_rowHeights
.Empty();
9336 m_rowBottoms
.Empty();
9337 if ( !GetBatchCount() )
9342 void wxGrid::SetRowSize( int row
, int height
)
9344 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9346 // See comment in SetColSize
9347 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9349 if ( m_rowHeights
.IsEmpty() )
9351 // need to really create the array
9355 int h
= wxMax( 0, height
);
9356 int diff
= h
- m_rowHeights
[row
];
9358 m_rowHeights
[row
] = h
;
9360 for ( i
= row
; i
< m_numRows
; i
++ )
9362 m_rowBottoms
[i
] += diff
;
9364 if ( !GetBatchCount() )
9368 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9370 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9372 if ( resizeExistingCols
)
9374 // since we are resizing all columns to the default column size,
9375 // we can simply clear the col widths and col rights
9376 // arrays (which also allows us to take advantage of
9377 // some speed optimisations)
9378 m_colWidths
.Empty();
9379 m_colRights
.Empty();
9380 if ( !GetBatchCount() )
9385 void wxGrid::SetColSize( int col
, int width
)
9387 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9389 // should we check that it's bigger than GetColMinimalWidth(col) here?
9391 // No, because it is reasonable to assume the library user know's
9392 // what he is doing. However whe should test against the weaker
9393 // constariant of minimalAcceptableWidth, as this breaks rendering
9395 // This test then fixes sf.net bug #645734
9397 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9399 if ( m_colWidths
.IsEmpty() )
9401 // need to really create the array
9405 // if < 0 calc new width from label
9409 wxArrayString lines
;
9410 wxClientDC
dc(m_colLabelWin
);
9411 dc
.SetFont(GetLabelFont());
9412 StringToLines(GetColLabelValue(col
), lines
);
9413 GetTextBoxSize(dc
, lines
, &w
, &h
);
9416 int w
= wxMax( 0, width
);
9417 int diff
= w
- m_colWidths
[col
];
9418 m_colWidths
[col
] = w
;
9421 for ( i
= col
; i
< m_numCols
; i
++ )
9423 m_colRights
[i
] += diff
;
9425 if ( !GetBatchCount() )
9430 void wxGrid::SetColMinimalWidth( int col
, int width
)
9432 if (width
> GetColMinimalAcceptableWidth()) {
9433 m_colMinWidths
[col
] = width
;
9437 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9439 if (width
> GetRowMinimalAcceptableHeight()) {
9440 m_rowMinHeights
[row
] = width
;
9444 int wxGrid::GetColMinimalWidth(int col
) const
9446 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(col
);
9447 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9450 int wxGrid::GetRowMinimalHeight(int row
) const
9452 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(row
);
9453 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9456 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9460 m_minAcceptableColWidth
= width
;
9463 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9467 m_minAcceptableRowHeight
= height
;
9470 int wxGrid::GetColMinimalAcceptableWidth() const
9472 return m_minAcceptableColWidth
;
9475 int wxGrid::GetRowMinimalAcceptableHeight() const
9477 return m_minAcceptableRowHeight
;
9480 // ----------------------------------------------------------------------------
9482 // ----------------------------------------------------------------------------
9484 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9486 wxClientDC
dc(m_gridWin
);
9488 // init both of them to avoid compiler warnings, even if weo nly need one
9496 wxCoord extent
, extentMax
= 0;
9497 int max
= column
? m_numRows
: m_numCols
;
9498 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9505 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9506 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9509 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9510 extent
= column
? size
.x
: size
.y
;
9511 if ( extent
> extentMax
)
9522 // now also compare with the column label extent
9524 dc
.SetFont( GetLabelFont() );
9528 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9529 if( GetColLabelTextOrientation() == wxVERTICAL
)
9533 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9535 extent
= column
? w
: h
;
9536 if ( extent
> extentMax
)
9543 // empty column - give default extent (notice that if extentMax is less
9544 // than default extent but != 0, it's ok)
9545 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9551 // leave some space around text
9562 SetColSize(col
, extentMax
);
9563 if ( !GetBatchCount() )
9566 m_gridWin
->GetClientSize( &cw
, &ch
);
9567 wxRect
rect ( CellToRect( 0, col
) );
9569 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9570 rect
.width
= cw
- rect
.x
;
9571 rect
.height
= m_colLabelHeight
;
9572 m_colLabelWin
->Refresh( TRUE
, &rect
);
9577 SetRowSize(row
, extentMax
);
9578 if ( !GetBatchCount() )
9581 m_gridWin
->GetClientSize( &cw
, &ch
);
9582 wxRect
rect ( CellToRect( row
, 0 ) );
9584 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9585 rect
.width
= m_rowLabelWidth
;
9586 rect
.height
= ch
- rect
.y
;
9587 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9593 SetColMinimalWidth(col
, extentMax
);
9595 SetRowMinimalHeight(row
, extentMax
);
9599 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9601 int width
= m_rowLabelWidth
;
9606 for ( int col
= 0; col
< m_numCols
; col
++ )
9610 AutoSizeColumn(col
, setAsMin
);
9613 width
+= GetColWidth(col
);
9622 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9624 int height
= m_colLabelHeight
;
9629 for ( int row
= 0; row
< m_numRows
; row
++ )
9633 AutoSizeRow(row
, setAsMin
);
9636 height
+= GetRowHeight(row
);
9645 void wxGrid::AutoSize()
9649 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9651 // round up the size to a multiple of scroll step - this ensures that we
9652 // won't get the scrollbars if we're sized exactly to this width
9653 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9655 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9656 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9658 // distribute the extra space between the columns/rows to avoid having
9659 // extra white space
9661 // Remove the extra m_extraWidth + 1 added above
9662 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9663 if ( diff
&& m_numCols
)
9665 // try to resize the columns uniformly
9666 wxCoord diffPerCol
= diff
/ m_numCols
;
9669 for ( int col
= 0; col
< m_numCols
; col
++ )
9671 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9675 // add remaining amount to the last columns
9676 diff
-= diffPerCol
* m_numCols
;
9679 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9681 SetColSize(col
, GetColWidth(col
) + 1);
9687 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9688 if ( diff
&& m_numRows
)
9690 // try to resize the columns uniformly
9691 wxCoord diffPerRow
= diff
/ m_numRows
;
9694 for ( int row
= 0; row
< m_numRows
; row
++ )
9696 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9700 // add remaining amount to the last rows
9701 diff
-= diffPerRow
* m_numRows
;
9704 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9706 SetRowSize(row
, GetRowHeight(row
) + 1);
9713 SetClientSize(sizeFit
);
9716 void wxGrid::AutoSizeRowLabelSize( int row
)
9718 wxArrayString lines
;
9721 // Hide the edit control, so it
9722 // won't interfer with drag-shrinking.
9723 if( IsCellEditControlShown() )
9725 HideCellEditControl();
9726 SaveEditControlValue();
9729 // autosize row height depending on label text
9730 StringToLines( GetRowLabelValue( row
), lines
);
9731 wxClientDC
dc( m_rowLabelWin
);
9732 GetTextBoxSize( dc
, lines
, &w
, &h
);
9733 if( h
< m_defaultRowHeight
)
9734 h
= m_defaultRowHeight
;
9739 void wxGrid::AutoSizeColLabelSize( int col
)
9741 wxArrayString lines
;
9744 // Hide the edit control, so it
9745 // won't interfer with drag-shrinking.
9746 if( IsCellEditControlShown() )
9748 HideCellEditControl();
9749 SaveEditControlValue();
9752 // autosize column width depending on label text
9753 StringToLines( GetColLabelValue( col
), lines
);
9754 wxClientDC
dc( m_colLabelWin
);
9755 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9756 GetTextBoxSize( dc
, lines
, &w
, &h
);
9758 GetTextBoxSize( dc
, lines
, &h
, &w
);
9759 if( w
< m_defaultColWidth
)
9760 w
= m_defaultColWidth
;
9765 wxSize
wxGrid::DoGetBestSize() const
9767 // don't set sizes, only calculate them
9768 wxGrid
*self
= (wxGrid
*)this; // const_cast
9771 width
= self
->SetOrCalcColumnSizes(TRUE
);
9772 height
= self
->SetOrCalcRowSizes(TRUE
);
9774 int maxwidth
, maxheight
;
9775 wxDisplaySize( & maxwidth
, & maxheight
);
9777 if ( width
> maxwidth
) width
= maxwidth
;
9778 if ( height
> maxheight
) height
= maxheight
;
9780 return wxSize( width
, height
);
9789 wxPen
& wxGrid::GetDividerPen() const
9794 // ----------------------------------------------------------------------------
9795 // cell value accessor functions
9796 // ----------------------------------------------------------------------------
9798 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9802 m_table
->SetValue( row
, col
, s
);
9803 if ( !GetBatchCount() )
9806 wxRect
rect( CellToRect( row
, col
) );
9808 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9809 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9810 m_gridWin
->Refresh( FALSE
, &rect
);
9813 if ( m_currentCellCoords
.GetRow() == row
&&
9814 m_currentCellCoords
.GetCol() == col
&&
9815 IsCellEditControlShown())
9816 // Note: If we are using IsCellEditControlEnabled,
9817 // this interacts badly with calling SetCellValue from
9818 // an EVT_GRID_CELL_CHANGE handler.
9820 HideCellEditControl();
9821 ShowCellEditControl(); // will reread data from table
9828 // ------ Block, row and col selection
9831 void wxGrid::SelectRow( int row
, bool addToSelected
)
9833 if ( IsSelection() && !addToSelected
)
9837 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9841 void wxGrid::SelectCol( int col
, bool addToSelected
)
9843 if ( IsSelection() && !addToSelected
)
9847 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9851 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9852 bool addToSelected
)
9854 if ( IsSelection() && !addToSelected
)
9858 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9859 FALSE
, addToSelected
);
9863 void wxGrid::SelectAll()
9865 if ( m_numRows
> 0 && m_numCols
> 0 )
9868 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9873 // ------ Cell, row and col deselection
9876 void wxGrid::DeselectRow( int row
)
9881 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9883 if ( m_selection
->IsInSelection(row
, 0 ) )
9884 m_selection
->ToggleCellSelection( row
, 0);
9888 int nCols
= GetNumberCols();
9889 for ( int i
= 0; i
< nCols
; i
++ )
9891 if ( m_selection
->IsInSelection(row
, i
) )
9892 m_selection
->ToggleCellSelection( row
, i
);
9897 void wxGrid::DeselectCol( int col
)
9902 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9904 if ( m_selection
->IsInSelection(0, col
) )
9905 m_selection
->ToggleCellSelection( 0, col
);
9909 int nRows
= GetNumberRows();
9910 for ( int i
= 0; i
< nRows
; i
++ )
9912 if ( m_selection
->IsInSelection(i
, col
) )
9913 m_selection
->ToggleCellSelection(i
, col
);
9918 void wxGrid::DeselectCell( int row
, int col
)
9920 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9921 m_selection
->ToggleCellSelection(row
, col
);
9924 bool wxGrid::IsSelection()
9926 return ( m_selection
&& (m_selection
->IsSelection() ||
9927 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9928 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9931 bool wxGrid::IsInSelection( int row
, int col
) const
9933 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9934 ( row
>= m_selectingTopLeft
.GetRow() &&
9935 col
>= m_selectingTopLeft
.GetCol() &&
9936 row
<= m_selectingBottomRight
.GetRow() &&
9937 col
<= m_selectingBottomRight
.GetCol() )) );
9940 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9942 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9943 return m_selection
->m_cellSelection
;
9945 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9947 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9948 return m_selection
->m_blockSelectionTopLeft
;
9950 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9952 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9953 return m_selection
->m_blockSelectionBottomRight
;
9955 wxArrayInt
wxGrid::GetSelectedRows() const
9957 if (!m_selection
) { wxArrayInt a
; return a
; }
9958 return m_selection
->m_rowSelection
;
9960 wxArrayInt
wxGrid::GetSelectedCols() const
9962 if (!m_selection
) { wxArrayInt a
; return a
; }
9963 return m_selection
->m_colSelection
;
9967 void wxGrid::ClearSelection()
9969 m_selectingTopLeft
= wxGridNoCellCoords
;
9970 m_selectingBottomRight
= wxGridNoCellCoords
;
9972 m_selection
->ClearSelection();
9976 // This function returns the rectangle that encloses the given block
9977 // in device coords clipped to the client size of the grid window.
9979 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9980 const wxGridCellCoords
&bottomRight
)
9982 wxRect
rect( wxGridNoCellRect
);
9985 cellRect
= CellToRect( topLeft
);
9986 if ( cellRect
!= wxGridNoCellRect
)
9992 rect
= wxRect( 0, 0, 0, 0 );
9995 cellRect
= CellToRect( bottomRight
);
9996 if ( cellRect
!= wxGridNoCellRect
)
10002 return wxGridNoCellRect
;
10006 int left
= rect
.GetLeft();
10007 int top
= rect
.GetTop();
10008 int right
= rect
.GetRight();
10009 int bottom
= rect
.GetBottom();
10011 int leftCol
= topLeft
.GetCol();
10012 int topRow
= topLeft
.GetRow();
10013 int rightCol
= bottomRight
.GetCol();
10014 int bottomRow
= bottomRight
.GetRow();
10032 topRow
= bottomRow
;
10037 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10039 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10041 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10043 cellRect
= CellToRect( j
, i
);
10045 if (cellRect
.x
< left
)
10047 if (cellRect
.y
< top
)
10049 if (cellRect
.x
+ cellRect
.width
> right
)
10050 right
= cellRect
.x
+ cellRect
.width
;
10051 if (cellRect
.y
+ cellRect
.height
> bottom
)
10052 bottom
= cellRect
.y
+ cellRect
.height
;
10054 else i
= rightCol
; // jump over inner cells.
10058 // convert to scrolled coords
10060 CalcScrolledPosition( left
, top
, &left
, &top
);
10061 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10064 m_gridWin
->GetClientSize( &cw
, &ch
);
10066 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10067 return wxRect( 0, 0, 0, 0);
10069 rect
.SetLeft( wxMax(0, left
) );
10070 rect
.SetTop( wxMax(0, top
) );
10071 rect
.SetRight( wxMin(cw
, right
) );
10072 rect
.SetBottom( wxMin(ch
, bottom
) );
10078 // ------ Grid event classes
10081 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10083 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10084 int row
, int col
, int x
, int y
, bool sel
,
10085 bool control
, bool shift
, bool alt
, bool meta
)
10086 : wxNotifyEvent( type
, id
)
10093 m_control
= control
;
10098 SetEventObject(obj
);
10102 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10104 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10105 int rowOrCol
, int x
, int y
,
10106 bool control
, bool shift
, bool alt
, bool meta
)
10107 : wxNotifyEvent( type
, id
)
10109 m_rowOrCol
= rowOrCol
;
10112 m_control
= control
;
10117 SetEventObject(obj
);
10121 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10123 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10124 const wxGridCellCoords
& topLeft
,
10125 const wxGridCellCoords
& bottomRight
,
10126 bool sel
, bool control
,
10127 bool shift
, bool alt
, bool meta
)
10128 : wxNotifyEvent( type
, id
)
10130 m_topLeft
= topLeft
;
10131 m_bottomRight
= bottomRight
;
10133 m_control
= control
;
10138 SetEventObject(obj
);
10142 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10144 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10145 wxObject
* obj
, int row
,
10146 int col
, wxControl
* ctrl
)
10147 : wxCommandEvent(type
, id
)
10149 SetEventObject(obj
);
10155 #endif // wxUSE_GRID