1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/dcclient.h"
24 #include "wx/settings.h"
26 #include "wx/textctrl.h"
27 #include "wx/checkbox.h"
28 #include "wx/combobox.h"
29 #include "wx/valtext.h"
34 #include "wx/textfile.h"
35 #include "wx/spinctrl.h"
36 #include "wx/tokenzr.h"
37 #include "wx/renderer.h"
40 #include "wx/generic/gridsel.h"
42 #if defined(__WXMOTIF__)
43 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
45 #define WXUNUSED_MOTIF(identifier) identifier
48 #if defined(__WXGTK__)
49 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
51 #define WXUNUSED_GTK(identifier) identifier
54 // Required for wxIs... functions
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
62 class WXDLLIMPEXP_ADV
);
64 struct wxGridCellWithAttr
66 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
67 : coords(row
, col
), attr(attr_
)
76 wxGridCellCoords coords
;
80 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
81 // without rewriting the macros, which require a public copy constructor.
84 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
85 class WXDLLIMPEXP_ADV
);
87 #include "wx/arrimpl.cpp"
89 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
90 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
97 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
98 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
99 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
100 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
101 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
102 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
103 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
121 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
122 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
123 const wxPoint
&pos
, const wxSize
&size
);
128 void OnPaint( wxPaintEvent
& event
);
129 void OnMouseEvent( wxMouseEvent
& event
);
130 void OnMouseWheel( wxMouseEvent
& event
);
131 void OnKeyDown( wxKeyEvent
& event
);
132 void OnKeyUp( wxKeyEvent
& );
133 void OnChar( wxKeyEvent
& );
135 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
136 DECLARE_EVENT_TABLE()
137 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
141 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
144 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
145 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
146 const wxPoint
&pos
, const wxSize
&size
);
151 void OnPaint( wxPaintEvent
& event
);
152 void OnMouseEvent( wxMouseEvent
& event
);
153 void OnMouseWheel( wxMouseEvent
& event
);
154 void OnKeyDown( wxKeyEvent
& event
);
155 void OnKeyUp( wxKeyEvent
& );
156 void OnChar( wxKeyEvent
& );
158 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
159 DECLARE_EVENT_TABLE()
160 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
164 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
167 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
168 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
169 const wxPoint
&pos
, const wxSize
&size
);
174 void OnMouseEvent( wxMouseEvent
& event
);
175 void OnMouseWheel( wxMouseEvent
& event
);
176 void OnKeyDown( wxKeyEvent
& event
);
177 void OnKeyUp( wxKeyEvent
& );
178 void OnChar( wxKeyEvent
& );
179 void OnPaint( wxPaintEvent
& event
);
181 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
182 DECLARE_EVENT_TABLE()
183 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
186 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
191 m_owner
= (wxGrid
*)NULL
;
192 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
193 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
196 wxGridWindow( wxGrid
*parent
,
197 wxGridRowLabelWindow
*rowLblWin
,
198 wxGridColLabelWindow
*colLblWin
,
199 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
202 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
204 wxGrid
* GetOwner() { return m_owner
; }
208 wxGridRowLabelWindow
*m_rowLabelWin
;
209 wxGridColLabelWindow
*m_colLabelWin
;
211 void OnPaint( wxPaintEvent
&event
);
212 void OnMouseWheel( wxMouseEvent
& event
);
213 void OnMouseEvent( wxMouseEvent
& event
);
214 void OnKeyDown( wxKeyEvent
& );
215 void OnKeyUp( wxKeyEvent
& );
216 void OnChar( wxKeyEvent
& );
217 void OnEraseBackground( wxEraseEvent
& );
218 void OnFocus( wxFocusEvent
& );
220 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
221 DECLARE_EVENT_TABLE()
222 DECLARE_NO_COPY_CLASS(wxGridWindow
)
226 class wxGridCellEditorEvtHandler
: public wxEvtHandler
229 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
236 void OnKillFocus(wxFocusEvent
& event
);
237 void OnKeyDown(wxKeyEvent
& event
);
238 void OnChar(wxKeyEvent
& event
);
240 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
244 wxGridCellEditor
*m_editor
;
246 // Work around the fact that a focus kill event can be sent to
247 // a combobox within a set focus event.
250 DECLARE_EVENT_TABLE()
251 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
252 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
256 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
258 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
259 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
260 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
261 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
265 // ----------------------------------------------------------------------------
266 // the internal data representation used by wxGridCellAttrProvider
267 // ----------------------------------------------------------------------------
269 // this class stores attributes set for cells
270 class WXDLLIMPEXP_ADV wxGridCellAttrData
273 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
274 wxGridCellAttr
*GetAttr(int row
, int col
) const;
275 void UpdateAttrRows( size_t pos
, int numRows
);
276 void UpdateAttrCols( size_t pos
, int numCols
);
279 // searches for the attr for given cell, returns wxNOT_FOUND if not found
280 int FindIndex(int row
, int col
) const;
282 wxGridCellWithAttrArray m_attrs
;
285 // this class stores attributes set for rows or columns
286 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
289 // empty ctor to suppress warnings
290 wxGridRowOrColAttrData() {}
291 ~wxGridRowOrColAttrData();
293 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
294 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
295 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
298 wxArrayInt m_rowsOrCols
;
299 wxArrayAttrs m_attrs
;
302 // NB: this is just a wrapper around 3 objects: one which stores cell
303 // attributes, and 2 others for row/col ones
304 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
307 wxGridCellAttrData m_cellAttrs
;
308 wxGridRowOrColAttrData m_rowAttrs
,
313 // ----------------------------------------------------------------------------
314 // data structures used for the data type registry
315 // ----------------------------------------------------------------------------
317 struct wxGridDataTypeInfo
319 wxGridDataTypeInfo(const wxString
& typeName
,
320 wxGridCellRenderer
* renderer
,
321 wxGridCellEditor
* editor
)
322 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
325 ~wxGridDataTypeInfo()
327 wxSafeDecRef(m_renderer
);
328 wxSafeDecRef(m_editor
);
332 wxGridCellRenderer
* m_renderer
;
333 wxGridCellEditor
* m_editor
;
335 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
339 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
340 class WXDLLIMPEXP_ADV
);
343 class WXDLLIMPEXP_ADV wxGridTypeRegistry
346 wxGridTypeRegistry() {}
347 ~wxGridTypeRegistry();
349 void RegisterDataType(const wxString
& typeName
,
350 wxGridCellRenderer
* renderer
,
351 wxGridCellEditor
* editor
);
353 // find one of already registered data types
354 int FindRegisteredDataType(const wxString
& typeName
);
356 // try to FindRegisteredDataType(), if this fails and typeName is one of
357 // standard typenames, register it and return its index
358 int FindDataType(const wxString
& typeName
);
360 // try to FindDataType(), if it fails see if it is not one of already
361 // registered data types with some params in which case clone the
362 // registered data type and set params for it
363 int FindOrCloneDataType(const wxString
& typeName
);
365 wxGridCellRenderer
* GetRenderer(int index
);
366 wxGridCellEditor
* GetEditor(int index
);
369 wxGridDataTypeInfoArray m_typeinfo
;
373 // ----------------------------------------------------------------------------
374 // conditional compilation
375 // ----------------------------------------------------------------------------
377 #ifndef WXGRID_DRAW_LINES
378 #define WXGRID_DRAW_LINES 1
381 // ----------------------------------------------------------------------------
383 // ----------------------------------------------------------------------------
385 //#define DEBUG_ATTR_CACHE
386 #ifdef DEBUG_ATTR_CACHE
387 static size_t gs_nAttrCacheHits
= 0;
388 static size_t gs_nAttrCacheMisses
= 0;
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
395 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
396 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
399 // TODO: this doesn't work at all, grid cells have different sizes and approx
400 // calculations don't work as because of the size mismatch scrollbars
401 // sometimes fail to be shown when they should be or vice versa
403 // The scroll bars may be a little flakey once in a while, but that is
404 // surely much less horrible than having scroll lines of only 1!!!
407 // Well, it's still seriously broken so it might be better but needs
410 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
411 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
413 // the size of hash tables used a bit everywhere (the max number of elements
414 // in these hash tables is the number of rows/columns)
415 static const int GRID_HASH_SIZE
= 100;
418 // ----------------------------------------------------------------------------
420 // ----------------------------------------------------------------------------
422 static inline int GetScrollX(int x
)
424 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
427 static inline int GetScrollY(int y
)
429 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
433 // ============================================================================
435 // ============================================================================
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 wxGridCellEditor::wxGridCellEditor()
447 wxGridCellEditor::~wxGridCellEditor()
452 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
453 wxWindowID
WXUNUSED(id
),
454 wxEvtHandler
* evtHandler
)
457 m_control
->PushEventHandler(evtHandler
);
460 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
461 wxGridCellAttr
*attr
)
463 // erase the background because we might not fill the cell
464 wxClientDC
dc(m_control
->GetParent());
465 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
467 gridWindow
->GetOwner()->PrepareDC(dc
);
469 dc
.SetPen(*wxTRANSPARENT_PEN
);
470 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
471 dc
.DrawRectangle(rectCell
);
473 // redraw the control we just painted over
474 m_control
->Refresh();
477 void wxGridCellEditor::Destroy()
481 m_control
->PopEventHandler( true /* delete it*/ );
483 m_control
->Destroy();
488 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
490 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
492 m_control
->Show(show
);
496 // set the colours/fonts if we have any
499 m_colFgOld
= m_control
->GetForegroundColour();
500 m_control
->SetForegroundColour(attr
->GetTextColour());
502 m_colBgOld
= m_control
->GetBackgroundColour();
503 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
505 // Workaround for GTK+1 font setting problem on some platforms
506 #if !defined(__WXGTK__) || defined(__WXGTK20__)
507 m_fontOld
= m_control
->GetFont();
508 m_control
->SetFont(attr
->GetFont());
511 // can't do anything more in the base class version, the other
512 // attributes may only be used by the derived classes
517 // restore the standard colours fonts
518 if ( m_colFgOld
.Ok() )
520 m_control
->SetForegroundColour(m_colFgOld
);
521 m_colFgOld
= wxNullColour
;
524 if ( m_colBgOld
.Ok() )
526 m_control
->SetBackgroundColour(m_colBgOld
);
527 m_colBgOld
= wxNullColour
;
530 // Workaround for GTK+1 font setting problem on some platforms
531 #if !defined(__WXGTK__) || defined(__WXGTK20__)
532 if ( m_fontOld
.Ok() )
534 m_control
->SetFont(m_fontOld
);
535 m_fontOld
= wxNullFont
;
541 void wxGridCellEditor::SetSize(const wxRect
& rect
)
543 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
545 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
548 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
553 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
555 bool ctrl
= event
.ControlDown();
556 bool alt
= event
.AltDown();
559 // On the Mac the Alt key is more like shift and is used for entry of
560 // valid characters, so check for Ctrl and Meta instead.
561 alt
= event
.MetaDown();
564 // Assume it's not a valid char if ctrl or alt is down, but if both are
565 // down then it may be because of an AltGr key combination, so let them
566 // through in that case.
567 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
574 // if the unicode key code is not really a unicode character (it may
575 // be a function key or etc., the platforms appear to always give us a
576 // small value in this case) then fallback to the ASCII key code but
577 // don't do anything for function keys or etc.
578 key
= event
.GetUnicodeKey();
581 key
= event
.GetKeyCode();
582 keyOk
= (key
<= 127);
585 key
= event
.GetKeyCode();
586 keyOk
= (key
<= 255);
592 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
597 void wxGridCellEditor::StartingClick()
603 // ----------------------------------------------------------------------------
604 // wxGridCellTextEditor
605 // ----------------------------------------------------------------------------
607 wxGridCellTextEditor::wxGridCellTextEditor()
612 void wxGridCellTextEditor::Create(wxWindow
* parent
,
614 wxEvtHandler
* evtHandler
)
616 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
617 wxDefaultPosition
, wxDefaultSize
618 #if defined(__WXMSW__)
619 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
623 // set max length allowed in the textctrl, if the parameter was set
626 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
629 wxGridCellEditor::Create(parent
, id
, evtHandler
);
632 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
633 wxGridCellAttr
* WXUNUSED(attr
))
635 // as we fill the entire client area,
636 // don't do anything here to minimize flicker
639 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
641 wxRect
rect(rectOrig
);
643 // Make the edit control large enough to allow for internal margins
645 // TODO: remove this if the text ctrl sizing is improved esp. for unix
647 #if defined(__WXGTK__)
656 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
658 // MB: treat MSW separately here otherwise the caret doesn't show
659 // when the editor is in the first row.
660 #if defined(__WXMSW__)
663 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
666 #if defined(__WXMOTIF__)
671 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
672 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
673 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
674 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
677 wxGridCellEditor::SetSize(rect
);
680 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
682 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
684 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
686 DoBeginEdit(m_startValue
);
689 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
691 Text()->SetValue(startValue
);
692 Text()->SetInsertionPointEnd();
693 Text()->SetSelection(-1, -1);
697 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
699 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
701 bool changed
= false;
702 wxString value
= Text()->GetValue();
703 if (value
!= m_startValue
)
707 grid
->GetTable()->SetValue(row
, col
, value
);
709 m_startValue
= wxEmptyString
;
711 // No point in setting the text of the hidden control
712 //Text()->SetValue(m_startValue);
717 void wxGridCellTextEditor::Reset()
719 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
721 DoReset(m_startValue
);
724 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
726 Text()->SetValue(startValue
);
727 Text()->SetInsertionPointEnd();
730 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
732 return wxGridCellEditor::IsAcceptedKey(event
);
735 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
737 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
738 // longer an appropriate way to get the character into the text control.
739 // Do it ourselves instead. We know that if we get this far that we have
740 // a valid character, so not a whole lot of testing needs to be done.
742 wxTextCtrl
* tc
= Text();
747 ch
= event
.GetUnicodeKey();
749 ch
= (wxChar
)event
.GetKeyCode();
751 ch
= (wxChar
)event
.GetKeyCode();
757 // delete the character at the cursor
758 pos
= tc
->GetInsertionPoint();
759 if (pos
< tc
->GetLastPosition())
760 tc
->Remove(pos
, pos
+ 1);
764 // delete the character before the cursor
765 pos
= tc
->GetInsertionPoint();
767 tc
->Remove(pos
- 1, pos
);
776 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
777 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
779 #if defined(__WXMOTIF__) || defined(__WXGTK__)
780 // wxMotif needs a little extra help...
781 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
782 wxString
s( Text()->GetValue() );
783 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
785 Text()->SetInsertionPoint( pos
);
787 // the other ports can handle a Return key press
793 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
803 if ( !params
.ToLong(&tmp
) )
805 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
809 m_maxChars
= (size_t)tmp
;
814 // return the value in the text control
815 wxString
wxGridCellTextEditor::GetValue() const
817 return Text()->GetValue();
820 // ----------------------------------------------------------------------------
821 // wxGridCellNumberEditor
822 // ----------------------------------------------------------------------------
824 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
830 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
832 wxEvtHandler
* evtHandler
)
837 // create a spin ctrl
838 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
839 wxDefaultPosition
, wxDefaultSize
,
843 wxGridCellEditor::Create(parent
, id
, evtHandler
);
848 // just a text control
849 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
852 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
853 #endif // wxUSE_VALIDATORS
857 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
859 // first get the value
860 wxGridTableBase
*table
= grid
->GetTable();
861 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
863 m_valueOld
= table
->GetValueAsLong(row
, col
);
868 wxString sValue
= table
->GetValue(row
, col
);
869 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
871 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
879 Spin()->SetValue((int)m_valueOld
);
885 DoBeginEdit(GetString());
889 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
899 value
= Spin()->GetValue();
900 changed
= value
!= m_valueOld
;
902 text
= wxString::Format(wxT("%ld"), value
);
907 text
= Text()->GetValue();
908 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
913 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
914 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
916 grid
->GetTable()->SetValue(row
, col
, text
);
922 void wxGridCellNumberEditor::Reset()
927 Spin()->SetValue((int)m_valueOld
);
932 DoReset(GetString());
936 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
938 if ( wxGridCellEditor::IsAcceptedKey(event
) )
940 int keycode
= event
.GetKeyCode();
941 if ( (keycode
< 128) &&
942 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
951 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
953 int keycode
= event
.GetKeyCode();
956 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
958 wxGridCellTextEditor::StartingKey(event
);
967 if ( wxIsdigit(keycode
) )
969 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
970 spin
->SetValue(keycode
- '0');
971 spin
->SetSelection(1,1);
980 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
991 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
995 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
999 // skip the error message below
1004 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1008 // return the value in the spin control if it is there (the text control otherwise)
1009 wxString
wxGridCellNumberEditor::GetValue() const
1016 long value
= Spin()->GetValue();
1017 s
.Printf(wxT("%ld"), value
);
1022 s
= Text()->GetValue();
1028 // ----------------------------------------------------------------------------
1029 // wxGridCellFloatEditor
1030 // ----------------------------------------------------------------------------
1032 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1035 m_precision
= precision
;
1038 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1040 wxEvtHandler
* evtHandler
)
1042 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1044 #if wxUSE_VALIDATORS
1045 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1046 #endif // wxUSE_VALIDATORS
1049 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1051 // first get the value
1052 wxGridTableBase
*table
= grid
->GetTable();
1053 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1055 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1060 wxString sValue
= table
->GetValue(row
, col
);
1061 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1063 wxFAIL_MSG( _T("this cell doesn't have float value") );
1068 DoBeginEdit(GetString());
1071 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1075 wxString
text(Text()->GetValue());
1077 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1078 !wxIsSameDouble(value
, m_valueOld
) )
1080 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1081 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1083 grid
->GetTable()->SetValue(row
, col
, text
);
1091 void wxGridCellFloatEditor::Reset()
1093 DoReset(GetString());
1096 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1098 int keycode
= event
.GetKeyCode();
1100 tmpbuf
[0] = (char) keycode
;
1102 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1105 bool is_decimal_point
= ( strbuf
==
1106 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1108 bool is_decimal_point
= ( strbuf
== _T(".") );
1111 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1112 || is_decimal_point
)
1114 wxGridCellTextEditor::StartingKey(event
);
1116 // skip Skip() below
1123 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1134 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1138 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1140 m_precision
= (int)tmp
;
1142 // skip the error message below
1147 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1151 wxString
wxGridCellFloatEditor::GetString() const
1154 if ( m_precision
== -1 && m_width
!= -1)
1156 // default precision
1157 fmt
.Printf(_T("%%%d.f"), m_width
);
1159 else if ( m_precision
!= -1 && m_width
== -1)
1162 fmt
.Printf(_T("%%.%df"), m_precision
);
1164 else if ( m_precision
!= -1 && m_width
!= -1 )
1166 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1170 // default width/precision
1174 return wxString::Format(fmt
, m_valueOld
);
1177 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1179 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1181 int keycode
= event
.GetKeyCode();
1182 printf("%d\n", keycode
);
1183 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1185 tmpbuf
[0] = (char) keycode
;
1187 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1190 bool is_decimal_point
=
1191 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1192 wxLOCALE_CAT_NUMBER
) );
1194 bool is_decimal_point
= ( strbuf
== _T(".") );
1197 if ( (keycode
< 128) &&
1198 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1199 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1208 #endif // wxUSE_TEXTCTRL
1212 // ----------------------------------------------------------------------------
1213 // wxGridCellBoolEditor
1214 // ----------------------------------------------------------------------------
1216 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1218 wxEvtHandler
* evtHandler
)
1220 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1221 wxDefaultPosition
, wxDefaultSize
,
1224 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1227 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1229 bool resize
= false;
1230 wxSize size
= m_control
->GetSize();
1231 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1233 // check if the checkbox is not too big/small for this cell
1234 wxSize sizeBest
= m_control
->GetBestSize();
1235 if ( !(size
== sizeBest
) )
1237 // reset to default size if it had been made smaller
1243 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1245 // leave 1 pixel margin
1246 size
.x
= size
.y
= minSize
- 2;
1253 m_control
->SetSize(size
);
1256 // position it in the centre of the rectangle (TODO: support alignment?)
1258 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1259 // the checkbox without label still has some space to the right in wxGTK,
1260 // so shift it to the right
1262 #elif defined(__WXMSW__)
1263 // here too, but in other way
1268 int hAlign
= wxALIGN_CENTRE
;
1269 int vAlign
= wxALIGN_CENTRE
;
1271 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1274 if (hAlign
== wxALIGN_LEFT
)
1282 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1284 else if (hAlign
== wxALIGN_RIGHT
)
1286 x
= r
.x
+ r
.width
- size
.x
- 2;
1287 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1289 else if (hAlign
== wxALIGN_CENTRE
)
1291 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1292 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1295 m_control
->Move(x
, y
);
1298 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1300 m_control
->Show(show
);
1304 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1305 CBox()->SetBackgroundColour(colBg
);
1309 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1311 wxASSERT_MSG(m_control
,
1312 wxT("The wxGridCellEditor must be Created first!"));
1314 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1316 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1320 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1321 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1324 CBox()->SetValue(m_startValue
);
1328 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1331 wxASSERT_MSG(m_control
,
1332 wxT("The wxGridCellEditor must be Created first!"));
1334 bool changed
= false;
1335 bool value
= CBox()->GetValue();
1336 if ( value
!= m_startValue
)
1341 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1342 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1344 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1350 void wxGridCellBoolEditor::Reset()
1352 wxASSERT_MSG(m_control
,
1353 wxT("The wxGridCellEditor must be Created first!"));
1355 CBox()->SetValue(m_startValue
);
1358 void wxGridCellBoolEditor::StartingClick()
1360 CBox()->SetValue(!CBox()->GetValue());
1363 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1365 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1367 int keycode
= event
.GetKeyCode();
1380 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1382 int keycode
= event
.GetKeyCode();
1386 CBox()->SetValue(!CBox()->GetValue());
1390 CBox()->SetValue(true);
1394 CBox()->SetValue(false);
1400 // return the value as "1" for true and the empty string for false
1401 wxString
wxGridCellBoolEditor::GetValue() const
1403 bool bSet
= CBox()->GetValue();
1404 return bSet
? _T("1") : wxEmptyString
;
1407 #endif // wxUSE_CHECKBOX
1411 // ----------------------------------------------------------------------------
1412 // wxGridCellChoiceEditor
1413 // ----------------------------------------------------------------------------
1415 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1417 : m_choices(choices
),
1418 m_allowOthers(allowOthers
) { }
1420 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1421 const wxString choices
[],
1423 : m_allowOthers(allowOthers
)
1427 m_choices
.Alloc(count
);
1428 for ( size_t n
= 0; n
< count
; n
++ )
1430 m_choices
.Add(choices
[n
]);
1435 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1437 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1438 editor
->m_allowOthers
= m_allowOthers
;
1439 editor
->m_choices
= m_choices
;
1444 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1446 wxEvtHandler
* evtHandler
)
1448 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1449 wxDefaultPosition
, wxDefaultSize
,
1451 m_allowOthers
? 0 : wxCB_READONLY
);
1453 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1456 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1457 wxGridCellAttr
* attr
)
1459 // as we fill the entire client area, don't do anything here to minimize
1462 // TODO: It doesn't actually fill the client area since the height of a
1463 // combo always defaults to the standard... Until someone has time to
1464 // figure out the right rectangle to paint, just do it the normal way...
1465 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1468 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1470 wxASSERT_MSG(m_control
,
1471 wxT("The wxGridCellEditor must be Created first!"));
1473 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1475 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1477 // Don't immediately end if we get a kill focus event within BeginEdit
1479 evtHandler
->SetInSetFocus(true);
1481 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1485 Combo()->SetValue(m_startValue
);
1489 // find the right position, or default to the first if not found
1490 int pos
= Combo()->FindString(m_startValue
);
1491 if (pos
== wxNOT_FOUND
)
1493 Combo()->SetSelection(pos
);
1496 Combo()->SetInsertionPointEnd();
1497 Combo()->SetFocus();
1501 // When dropping down the menu, a kill focus event
1502 // happens after this point, so we can't reset the flag yet.
1503 #if !defined(__WXGTK20__)
1504 evtHandler
->SetInSetFocus(false);
1509 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1512 wxString value
= Combo()->GetValue();
1513 if ( value
== m_startValue
)
1516 grid
->GetTable()->SetValue(row
, col
, value
);
1521 void wxGridCellChoiceEditor::Reset()
1523 Combo()->SetValue(m_startValue
);
1524 Combo()->SetInsertionPointEnd();
1527 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1537 wxStringTokenizer
tk(params
, _T(','));
1538 while ( tk
.HasMoreTokens() )
1540 m_choices
.Add(tk
.GetNextToken());
1544 // return the value in the text control
1545 wxString
wxGridCellChoiceEditor::GetValue() const
1547 return Combo()->GetValue();
1550 #endif // wxUSE_COMBOBOX
1552 // ----------------------------------------------------------------------------
1553 // wxGridCellEditorEvtHandler
1554 // ----------------------------------------------------------------------------
1556 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1558 // Don't disable the cell if we're just starting to edit it
1563 m_grid
->DisableCellEditControl();
1568 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1570 switch ( event
.GetKeyCode() )
1574 m_grid
->DisableCellEditControl();
1578 m_grid
->GetEventHandler()->ProcessEvent( event
);
1582 case WXK_NUMPAD_ENTER
:
1583 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1584 m_editor
->HandleReturn(event
);
1593 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1595 int row
= m_grid
->GetGridCursorRow();
1596 int col
= m_grid
->GetGridCursorCol();
1597 wxRect rect
= m_grid
->CellToRect( row
, col
);
1599 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1601 // if cell width is smaller than grid client area, cell is wholly visible
1602 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1604 switch ( event
.GetKeyCode() )
1609 case WXK_NUMPAD_ENTER
:
1614 if ( wholeCellVisible
)
1616 // no special processing needed...
1621 // do special processing for partly visible cell...
1623 // get the widths of all cells previous to this one
1625 for ( int i
= 0; i
< col
; i
++ )
1627 colXPos
+= m_grid
->GetColSize(i
);
1630 int xUnit
= 1, yUnit
= 1;
1631 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1634 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1638 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1645 if ( wholeCellVisible
)
1647 // no special processing needed...
1652 // do special processing for partly visible cell...
1655 wxString value
= m_grid
->GetCellValue(row
, col
);
1656 if ( wxEmptyString
!= value
)
1658 // get width of cell CONTENTS (text)
1660 wxFont font
= m_grid
->GetCellFont(row
, col
);
1661 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1663 // try to RIGHT align the text by scrolling
1664 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1666 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1667 // otherwise the last part of the cell content might be hidden below the scroll bar
1668 // FIXME: maybe there is a more suitable correction?
1669 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1670 if ( textWidth
< 0 )
1676 // get the widths of all cells previous to this one
1678 for ( int i
= 0; i
< col
; i
++ )
1680 colXPos
+= m_grid
->GetColSize(i
);
1683 // and add the (modified) text width of the cell contents
1684 // as we'd like to see the last part of the cell contents
1685 colXPos
+= textWidth
;
1687 int xUnit
= 1, yUnit
= 1;
1688 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1689 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1699 // ----------------------------------------------------------------------------
1700 // wxGridCellWorker is an (almost) empty common base class for
1701 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1702 // ----------------------------------------------------------------------------
1704 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1709 wxGridCellWorker::~wxGridCellWorker()
1713 // ============================================================================
1715 // ============================================================================
1717 // ----------------------------------------------------------------------------
1718 // wxGridCellRenderer
1719 // ----------------------------------------------------------------------------
1721 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1722 wxGridCellAttr
& attr
,
1725 int WXUNUSED(row
), int WXUNUSED(col
),
1728 dc
.SetBackgroundMode( wxSOLID
);
1730 // grey out fields if the grid is disabled
1731 if ( grid
.IsEnabled() )
1735 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1739 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1744 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1747 dc
.SetPen( *wxTRANSPARENT_PEN
);
1748 dc
.DrawRectangle(rect
);
1751 // ----------------------------------------------------------------------------
1752 // wxGridCellStringRenderer
1753 // ----------------------------------------------------------------------------
1755 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1756 const wxGridCellAttr
& attr
,
1760 dc
.SetBackgroundMode( wxTRANSPARENT
);
1762 // TODO some special colours for attr.IsReadOnly() case?
1764 // different coloured text when the grid is disabled
1765 if ( grid
.IsEnabled() )
1769 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1770 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1774 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1775 dc
.SetTextForeground( attr
.GetTextColour() );
1780 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1781 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1784 dc
.SetFont( attr
.GetFont() );
1787 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1789 const wxString
& text
)
1791 wxCoord x
= 0, y
= 0, max_x
= 0;
1792 dc
.SetFont(attr
.GetFont());
1793 wxStringTokenizer
tk(text
, _T('\n'));
1794 while ( tk
.HasMoreTokens() )
1796 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1797 max_x
= wxMax(max_x
, x
);
1800 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1802 return wxSize(max_x
, y
);
1805 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1806 wxGridCellAttr
& attr
,
1810 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1813 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1814 wxGridCellAttr
& attr
,
1816 const wxRect
& rectCell
,
1820 wxRect rect
= rectCell
;
1823 // erase only this cells background, overflow cells should have been erased
1824 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1827 attr
.GetAlignment(&hAlign
, &vAlign
);
1829 int overflowCols
= 0;
1831 if (attr
.GetOverflow())
1833 int cols
= grid
.GetNumberCols();
1834 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1835 int cell_rows
, cell_cols
;
1836 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1837 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1839 int i
, c_cols
, c_rows
;
1840 for (i
= col
+cell_cols
; i
< cols
; i
++)
1842 bool is_empty
= true;
1843 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1845 // check w/ anchor cell for multicell block
1846 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1849 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1857 rect
.width
+= grid
.GetColSize(i
);
1864 if (rect
.width
>= best_width
)
1868 overflowCols
= i
- col
- cell_cols
+ 1;
1869 if (overflowCols
>= cols
)
1870 overflowCols
= cols
- 1;
1873 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1875 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1877 clip
.x
+= rectCell
.width
;
1878 // draw each overflow cell individually
1879 int col_end
= col
+cell_cols
+ overflowCols
;
1880 if (col_end
>= grid
.GetNumberCols())
1881 col_end
= grid
.GetNumberCols() - 1;
1882 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1884 clip
.width
= grid
.GetColSize(i
) - 1;
1885 dc
.DestroyClippingRegion();
1886 dc
.SetClippingRegion(clip
);
1888 SetTextColoursAndFont(grid
, attr
, dc
,
1889 grid
.IsInSelection(row
,i
));
1891 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1892 rect
, hAlign
, vAlign
);
1893 clip
.x
+= grid
.GetColSize(i
) - 1;
1899 dc
.DestroyClippingRegion();
1903 // now we only have to draw the text
1904 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1906 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1907 rect
, hAlign
, vAlign
);
1910 // ----------------------------------------------------------------------------
1911 // wxGridCellNumberRenderer
1912 // ----------------------------------------------------------------------------
1914 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1916 wxGridTableBase
*table
= grid
.GetTable();
1918 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1920 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1924 text
= table
->GetValue(row
, col
);
1930 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1931 wxGridCellAttr
& attr
,
1933 const wxRect
& rectCell
,
1937 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1939 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1941 // draw the text right aligned by default
1943 attr
.GetAlignment(&hAlign
, &vAlign
);
1944 hAlign
= wxALIGN_RIGHT
;
1946 wxRect rect
= rectCell
;
1949 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1952 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1953 wxGridCellAttr
& attr
,
1957 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1960 // ----------------------------------------------------------------------------
1961 // wxGridCellFloatRenderer
1962 // ----------------------------------------------------------------------------
1964 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1967 SetPrecision(precision
);
1970 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1972 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1973 renderer
->m_width
= m_width
;
1974 renderer
->m_precision
= m_precision
;
1975 renderer
->m_format
= m_format
;
1980 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1982 wxGridTableBase
*table
= grid
.GetTable();
1987 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1989 val
= table
->GetValueAsDouble(row
, col
);
1994 text
= table
->GetValue(row
, col
);
1995 hasDouble
= text
.ToDouble(&val
);
2002 if ( m_width
== -1 )
2004 if ( m_precision
== -1 )
2006 // default width/precision
2007 m_format
= _T("%f");
2011 m_format
.Printf(_T("%%.%df"), m_precision
);
2014 else if ( m_precision
== -1 )
2016 // default precision
2017 m_format
.Printf(_T("%%%d.f"), m_width
);
2021 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2025 text
.Printf(m_format
, val
);
2028 //else: text already contains the string
2033 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2034 wxGridCellAttr
& attr
,
2036 const wxRect
& rectCell
,
2040 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2042 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2044 // draw the text right aligned by default
2046 attr
.GetAlignment(&hAlign
, &vAlign
);
2047 hAlign
= wxALIGN_RIGHT
;
2049 wxRect rect
= rectCell
;
2052 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2055 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2056 wxGridCellAttr
& attr
,
2060 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2063 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2067 // reset to defaults
2073 wxString tmp
= params
.BeforeFirst(_T(','));
2077 if ( tmp
.ToLong(&width
) )
2079 SetWidth((int)width
);
2083 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2087 tmp
= params
.AfterFirst(_T(','));
2091 if ( tmp
.ToLong(&precision
) )
2093 SetPrecision((int)precision
);
2097 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2103 // ----------------------------------------------------------------------------
2104 // wxGridCellBoolRenderer
2105 // ----------------------------------------------------------------------------
2107 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2109 // FIXME these checkbox size calculations are really ugly...
2111 // between checkmark and box
2112 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2114 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2115 wxGridCellAttr
& WXUNUSED(attr
),
2120 // compute it only once (no locks for MT safeness in GUI thread...)
2121 if ( !ms_sizeCheckMark
.x
)
2123 // get checkbox size
2124 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2125 wxSize size
= checkbox
->GetBestSize();
2126 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2128 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2129 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2130 checkSize
-= size
.y
/ 2;
2135 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2138 return ms_sizeCheckMark
;
2141 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2142 wxGridCellAttr
& attr
,
2148 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2150 // draw a check mark in the centre (ignoring alignment - TODO)
2151 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2153 // don't draw outside the cell
2154 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2155 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2157 // and even leave (at least) 1 pixel margin
2158 size
.x
= size
.y
= minSize
- 2;
2161 // draw a border around checkmark
2163 attr
.GetAlignment(& hAlign
, &vAlign
);
2166 if (hAlign
== wxALIGN_CENTRE
)
2168 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2169 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2170 rectBorder
.width
= size
.x
;
2171 rectBorder
.height
= size
.y
;
2173 else if (hAlign
== wxALIGN_LEFT
)
2175 rectBorder
.x
= rect
.x
+ 2;
2176 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2177 rectBorder
.width
= size
.x
;
2178 rectBorder
.height
= size
.y
;
2180 else if (hAlign
== wxALIGN_RIGHT
)
2182 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2183 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2184 rectBorder
.width
= size
.x
;
2185 rectBorder
.height
= size
.y
;
2189 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2190 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2193 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2194 value
= !( !cellval
|| (cellval
== wxT("0")) );
2199 wxRect rectMark
= rectBorder
;
2202 // MSW DrawCheckMark() is weird (and should probably be changed...)
2203 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2207 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2210 dc
.SetTextForeground(attr
.GetTextColour());
2211 dc
.DrawCheckMark(rectMark
);
2214 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2215 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2216 dc
.DrawRectangle(rectBorder
);
2219 // ----------------------------------------------------------------------------
2221 // ----------------------------------------------------------------------------
2223 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2227 m_isReadOnly
= Unset
;
2232 m_attrkind
= wxGridCellAttr::Cell
;
2234 m_sizeRows
= m_sizeCols
= 1;
2235 m_overflow
= UnsetOverflow
;
2237 SetDefAttr(attrDefault
);
2240 wxGridCellAttr
*wxGridCellAttr::Clone() const
2242 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2244 if ( HasTextColour() )
2245 attr
->SetTextColour(GetTextColour());
2246 if ( HasBackgroundColour() )
2247 attr
->SetBackgroundColour(GetBackgroundColour());
2249 attr
->SetFont(GetFont());
2250 if ( HasAlignment() )
2251 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2253 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2257 attr
->SetRenderer(m_renderer
);
2258 m_renderer
->IncRef();
2262 attr
->SetEditor(m_editor
);
2267 attr
->SetReadOnly();
2269 attr
->SetKind( m_attrkind
);
2274 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2276 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2277 SetTextColour(mergefrom
->GetTextColour());
2278 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2279 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2280 if ( !HasFont() && mergefrom
->HasFont() )
2281 SetFont(mergefrom
->GetFont());
2282 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2285 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2286 SetAlignment(hAlign
, vAlign
);
2288 if ( !HasSize() && mergefrom
->HasSize() )
2289 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2291 // Directly access member functions as GetRender/Editor don't just return
2292 // m_renderer/m_editor
2294 // Maybe add support for merge of Render and Editor?
2295 if (!HasRenderer() && mergefrom
->HasRenderer() )
2297 m_renderer
= mergefrom
->m_renderer
;
2298 m_renderer
->IncRef();
2300 if ( !HasEditor() && mergefrom
->HasEditor() )
2302 m_editor
= mergefrom
->m_editor
;
2305 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2306 SetReadOnly(mergefrom
->IsReadOnly());
2308 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2309 SetOverflow(mergefrom
->GetOverflow());
2311 SetDefAttr(mergefrom
->m_defGridAttr
);
2314 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2316 // The size of a cell is normally 1,1
2318 // If this cell is larger (2,2) then this is the top left cell
2319 // the other cells that will be covered (lower right cells) must be
2320 // set to negative or zero values such that
2321 // row + num_rows of the covered cell points to the larger cell (this cell)
2322 // same goes for the col + num_cols.
2324 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2326 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2327 !((num_rows
<= 0) && (num_cols
> 0)) ||
2328 !((num_rows
== 0) && (num_cols
== 0))),
2329 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2331 m_sizeRows
= num_rows
;
2332 m_sizeCols
= num_cols
;
2335 const wxColour
& wxGridCellAttr::GetTextColour() const
2337 if (HasTextColour())
2341 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2343 return m_defGridAttr
->GetTextColour();
2347 wxFAIL_MSG(wxT("Missing default cell attribute"));
2348 return wxNullColour
;
2352 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2354 if (HasBackgroundColour())
2358 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2360 return m_defGridAttr
->GetBackgroundColour();
2364 wxFAIL_MSG(wxT("Missing default cell attribute"));
2365 return wxNullColour
;
2369 const wxFont
& wxGridCellAttr::GetFont() const
2375 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2377 return m_defGridAttr
->GetFont();
2381 wxFAIL_MSG(wxT("Missing default cell attribute"));
2386 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2395 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2396 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2399 wxFAIL_MSG(wxT("Missing default cell attribute"));
2403 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2406 *num_rows
= m_sizeRows
;
2408 *num_cols
= m_sizeCols
;
2411 // GetRenderer and GetEditor use a slightly different decision path about
2412 // which attribute to use. If a non-default attr object has one then it is
2413 // used, otherwise the default editor or renderer is fetched from the grid and
2414 // used. It should be the default for the data type of the cell. If it is
2415 // NULL (because the table has a type that the grid does not have in its
2416 // registry,) then the grid's default editor or renderer is used.
2418 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2420 wxGridCellRenderer
*renderer
;
2422 if ( m_renderer
&& this != m_defGridAttr
)
2424 // use the cells renderer if it has one
2425 renderer
= m_renderer
;
2428 else // no non-default cell renderer
2430 // get default renderer for the data type
2433 // GetDefaultRendererForCell() will do IncRef() for us
2434 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2443 if (m_defGridAttr
&& this != m_defGridAttr
)
2445 // if we still don't have one then use the grid default
2446 // (no need for IncRef() here neither)
2447 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2449 else // default grid attr
2451 // use m_renderer which we had decided not to use initially
2452 renderer
= m_renderer
;
2459 // we're supposed to always find something
2460 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2465 // same as above, except for s/renderer/editor/g
2466 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2468 wxGridCellEditor
*editor
;
2470 if ( m_editor
&& this != m_defGridAttr
)
2472 // use the cells editor if it has one
2476 else // no non default cell editor
2478 // get default editor for the data type
2481 // GetDefaultEditorForCell() will do IncRef() for us
2482 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2491 if ( m_defGridAttr
&& this != m_defGridAttr
)
2493 // if we still don't have one then use the grid default
2494 // (no need for IncRef() here neither)
2495 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2497 else // default grid attr
2499 // use m_editor which we had decided not to use initially
2507 // we're supposed to always find something
2508 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2513 // ----------------------------------------------------------------------------
2514 // wxGridCellAttrData
2515 // ----------------------------------------------------------------------------
2517 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2519 int n
= FindIndex(row
, col
);
2520 if ( n
== wxNOT_FOUND
)
2522 // add the attribute
2523 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2527 // free the old attribute
2528 m_attrs
[(size_t)n
].attr
->DecRef();
2532 // change the attribute
2533 m_attrs
[(size_t)n
].attr
= attr
;
2537 // remove this attribute
2538 m_attrs
.RemoveAt((size_t)n
);
2543 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2545 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2547 int n
= FindIndex(row
, col
);
2548 if ( n
!= wxNOT_FOUND
)
2550 attr
= m_attrs
[(size_t)n
].attr
;
2557 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2559 size_t count
= m_attrs
.GetCount();
2560 for ( size_t n
= 0; n
< count
; n
++ )
2562 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2563 wxCoord row
= coords
.GetRow();
2564 if ((size_t)row
>= pos
)
2568 // If rows inserted, include row counter where necessary
2569 coords
.SetRow(row
+ numRows
);
2571 else if (numRows
< 0)
2573 // If rows deleted ...
2574 if ((size_t)row
>= pos
- numRows
)
2576 // ...either decrement row counter (if row still exists)...
2577 coords
.SetRow(row
+ numRows
);
2581 // ...or remove the attribute
2582 // No need to DecRef the attribute itself since this is
2583 // done be wxGridCellWithAttr's destructor!
2584 m_attrs
.RemoveAt(n
);
2593 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2595 size_t count
= m_attrs
.GetCount();
2596 for ( size_t n
= 0; n
< count
; n
++ )
2598 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2599 wxCoord col
= coords
.GetCol();
2600 if ( (size_t)col
>= pos
)
2604 // If rows inserted, include row counter where necessary
2605 coords
.SetCol(col
+ numCols
);
2607 else if (numCols
< 0)
2609 // If rows deleted ...
2610 if ((size_t)col
>= pos
- numCols
)
2612 // ...either decrement row counter (if row still exists)...
2613 coords
.SetCol(col
+ numCols
);
2617 // ...or remove the attribute
2618 // No need to DecRef the attribute itself since this is
2619 // done be wxGridCellWithAttr's destructor!
2620 m_attrs
.RemoveAt(n
);
2629 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2631 size_t count
= m_attrs
.GetCount();
2632 for ( size_t n
= 0; n
< count
; n
++ )
2634 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2635 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2644 // ----------------------------------------------------------------------------
2645 // wxGridRowOrColAttrData
2646 // ----------------------------------------------------------------------------
2648 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2650 size_t count
= m_attrs
.Count();
2651 for ( size_t n
= 0; n
< count
; n
++ )
2653 m_attrs
[n
]->DecRef();
2657 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2659 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2661 int n
= m_rowsOrCols
.Index(rowOrCol
);
2662 if ( n
!= wxNOT_FOUND
)
2664 attr
= m_attrs
[(size_t)n
];
2671 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2673 int i
= m_rowsOrCols
.Index(rowOrCol
);
2674 if ( i
== wxNOT_FOUND
)
2676 // add the attribute
2677 m_rowsOrCols
.Add(rowOrCol
);
2682 size_t n
= (size_t)i
;
2685 // change the attribute
2686 m_attrs
[n
]->DecRef();
2691 // remove this attribute
2692 m_attrs
[n
]->DecRef();
2693 m_rowsOrCols
.RemoveAt(n
);
2694 m_attrs
.RemoveAt(n
);
2699 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2701 size_t count
= m_attrs
.GetCount();
2702 for ( size_t n
= 0; n
< count
; n
++ )
2704 int & rowOrCol
= m_rowsOrCols
[n
];
2705 if ( (size_t)rowOrCol
>= pos
)
2707 if ( numRowsOrCols
> 0 )
2709 // If rows inserted, include row counter where necessary
2710 rowOrCol
+= numRowsOrCols
;
2712 else if ( numRowsOrCols
< 0)
2714 // If rows deleted, either decrement row counter (if row still exists)
2715 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2716 rowOrCol
+= numRowsOrCols
;
2719 m_rowsOrCols
.RemoveAt(n
);
2720 m_attrs
[n
]->DecRef();
2721 m_attrs
.RemoveAt(n
);
2730 // ----------------------------------------------------------------------------
2731 // wxGridCellAttrProvider
2732 // ----------------------------------------------------------------------------
2734 wxGridCellAttrProvider::wxGridCellAttrProvider()
2736 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2739 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2744 void wxGridCellAttrProvider::InitData()
2746 m_data
= new wxGridCellAttrProviderData
;
2749 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2750 wxGridCellAttr::wxAttrKind kind
) const
2752 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2757 case (wxGridCellAttr::Any
):
2758 // Get cached merge attributes.
2759 // Currently not used as no cache implemented as not mutable
2760 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2763 // Basically implement old version.
2764 // Also check merge cache, so we don't have to re-merge every time..
2765 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2766 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2767 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2769 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2771 // Two or more are non NULL
2772 attr
= new wxGridCellAttr
;
2773 attr
->SetKind(wxGridCellAttr::Merged
);
2775 // Order is important..
2778 attr
->MergeWith(attrcell
);
2783 attr
->MergeWith(attrcol
);
2788 attr
->MergeWith(attrrow
);
2792 // store merge attr if cache implemented
2794 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2798 // one or none is non null return it or null.
2817 case (wxGridCellAttr::Cell
):
2818 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2821 case (wxGridCellAttr::Col
):
2822 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2825 case (wxGridCellAttr::Row
):
2826 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2831 // (wxGridCellAttr::Default):
2832 // (wxGridCellAttr::Merged):
2840 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2846 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2849 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2854 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2857 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2862 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2865 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2869 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2871 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2875 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2879 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2881 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2885 // ----------------------------------------------------------------------------
2886 // wxGridTypeRegistry
2887 // ----------------------------------------------------------------------------
2889 wxGridTypeRegistry::~wxGridTypeRegistry()
2891 size_t count
= m_typeinfo
.Count();
2892 for ( size_t i
= 0; i
< count
; i
++ )
2893 delete m_typeinfo
[i
];
2896 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2897 wxGridCellRenderer
* renderer
,
2898 wxGridCellEditor
* editor
)
2900 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2902 // is it already registered?
2903 int loc
= FindRegisteredDataType(typeName
);
2904 if ( loc
!= wxNOT_FOUND
)
2906 delete m_typeinfo
[loc
];
2907 m_typeinfo
[loc
] = info
;
2911 m_typeinfo
.Add(info
);
2915 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2917 size_t count
= m_typeinfo
.GetCount();
2918 for ( size_t i
= 0; i
< count
; i
++ )
2920 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2929 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2931 int index
= FindRegisteredDataType(typeName
);
2932 if ( index
== wxNOT_FOUND
)
2934 // check whether this is one of the standard ones, in which case
2935 // register it "on the fly"
2937 if ( typeName
== wxGRID_VALUE_STRING
)
2939 RegisterDataType(wxGRID_VALUE_STRING
,
2940 new wxGridCellStringRenderer
,
2941 new wxGridCellTextEditor
);
2944 #endif // wxUSE_TEXTCTRL
2946 if ( typeName
== wxGRID_VALUE_BOOL
)
2948 RegisterDataType(wxGRID_VALUE_BOOL
,
2949 new wxGridCellBoolRenderer
,
2950 new wxGridCellBoolEditor
);
2953 #endif // wxUSE_CHECKBOX
2955 if ( typeName
== wxGRID_VALUE_NUMBER
)
2957 RegisterDataType(wxGRID_VALUE_NUMBER
,
2958 new wxGridCellNumberRenderer
,
2959 new wxGridCellNumberEditor
);
2961 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2963 RegisterDataType(wxGRID_VALUE_FLOAT
,
2964 new wxGridCellFloatRenderer
,
2965 new wxGridCellFloatEditor
);
2968 #endif // wxUSE_TEXTCTRL
2970 if ( typeName
== wxGRID_VALUE_CHOICE
)
2972 RegisterDataType(wxGRID_VALUE_CHOICE
,
2973 new wxGridCellStringRenderer
,
2974 new wxGridCellChoiceEditor
);
2977 #endif // wxUSE_COMBOBOX
2982 // we get here only if just added the entry for this type, so return
2984 index
= m_typeinfo
.GetCount() - 1;
2990 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2992 int index
= FindDataType(typeName
);
2993 if ( index
== wxNOT_FOUND
)
2995 // the first part of the typename is the "real" type, anything after ':'
2996 // are the parameters for the renderer
2997 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2998 if ( index
== wxNOT_FOUND
)
3003 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3004 wxGridCellRenderer
*rendererOld
= renderer
;
3005 renderer
= renderer
->Clone();
3006 rendererOld
->DecRef();
3008 wxGridCellEditor
*editor
= GetEditor(index
);
3009 wxGridCellEditor
*editorOld
= editor
;
3010 editor
= editor
->Clone();
3011 editorOld
->DecRef();
3013 // do it even if there are no parameters to reset them to defaults
3014 wxString params
= typeName
.AfterFirst(_T(':'));
3015 renderer
->SetParameters(params
);
3016 editor
->SetParameters(params
);
3018 // register the new typename
3019 RegisterDataType(typeName
, renderer
, editor
);
3021 // we just registered it, it's the last one
3022 index
= m_typeinfo
.GetCount() - 1;
3028 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3030 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3037 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3039 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3046 // ----------------------------------------------------------------------------
3048 // ----------------------------------------------------------------------------
3050 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3052 wxGridTableBase::wxGridTableBase()
3054 m_view
= (wxGrid
*) NULL
;
3055 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3058 wxGridTableBase::~wxGridTableBase()
3060 delete m_attrProvider
;
3063 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3065 delete m_attrProvider
;
3066 m_attrProvider
= attrProvider
;
3069 bool wxGridTableBase::CanHaveAttributes()
3071 if ( ! GetAttrProvider() )
3073 // use the default attr provider by default
3074 SetAttrProvider(new wxGridCellAttrProvider
);
3080 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3082 if ( m_attrProvider
)
3083 return m_attrProvider
->GetAttr(row
, col
, kind
);
3085 return (wxGridCellAttr
*)NULL
;
3088 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3090 if ( m_attrProvider
)
3092 attr
->SetKind(wxGridCellAttr::Cell
);
3093 m_attrProvider
->SetAttr(attr
, row
, col
);
3097 // as we take ownership of the pointer and don't store it, we must
3103 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3105 if ( m_attrProvider
)
3107 attr
->SetKind(wxGridCellAttr::Row
);
3108 m_attrProvider
->SetRowAttr(attr
, row
);
3112 // as we take ownership of the pointer and don't store it, we must
3118 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3120 if ( m_attrProvider
)
3122 attr
->SetKind(wxGridCellAttr::Col
);
3123 m_attrProvider
->SetColAttr(attr
, col
);
3127 // as we take ownership of the pointer and don't store it, we must
3133 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3134 size_t WXUNUSED(numRows
) )
3136 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3141 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3143 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3148 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3149 size_t WXUNUSED(numRows
) )
3151 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3156 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3157 size_t WXUNUSED(numCols
) )
3159 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3164 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3166 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3171 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3172 size_t WXUNUSED(numCols
) )
3174 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3179 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3183 // RD: Starting the rows at zero confuses users,
3184 // no matter how much it makes sense to us geeks.
3190 wxString
wxGridTableBase::GetColLabelValue( int col
)
3192 // default col labels are:
3193 // cols 0 to 25 : A-Z
3194 // cols 26 to 675 : AA-ZZ
3199 for ( n
= 1; ; n
++ )
3201 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3207 // reverse the string...
3209 for ( i
= 0; i
< n
; i
++ )
3217 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3219 return wxGRID_VALUE_STRING
;
3222 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3223 const wxString
& typeName
)
3225 return typeName
== wxGRID_VALUE_STRING
;
3228 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3230 return CanGetValueAs(row
, col
, typeName
);
3233 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3238 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3243 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3248 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3249 long WXUNUSED(value
) )
3253 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3254 double WXUNUSED(value
) )
3258 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3259 bool WXUNUSED(value
) )
3263 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3264 const wxString
& WXUNUSED(typeName
) )
3269 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3270 const wxString
& WXUNUSED(typeName
),
3271 void* WXUNUSED(value
) )
3275 //////////////////////////////////////////////////////////////////////
3277 // Message class for the grid table to send requests and notifications
3281 wxGridTableMessage::wxGridTableMessage()
3283 m_table
= (wxGridTableBase
*) NULL
;
3289 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3290 int commandInt1
, int commandInt2
)
3294 m_comInt1
= commandInt1
;
3295 m_comInt2
= commandInt2
;
3298 //////////////////////////////////////////////////////////////////////
3300 // A basic grid table for string data. An object of this class will
3301 // created by wxGrid if you don't specify an alternative table class.
3304 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3306 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3308 wxGridStringTable::wxGridStringTable()
3313 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3316 m_data
.Alloc( numRows
);
3319 sa
.Alloc( numCols
);
3320 sa
.Add( wxEmptyString
, numCols
);
3322 m_data
.Add( sa
, numRows
);
3325 wxGridStringTable::~wxGridStringTable()
3329 int wxGridStringTable::GetNumberRows()
3331 return m_data
.GetCount();
3334 int wxGridStringTable::GetNumberCols()
3336 if ( m_data
.GetCount() > 0 )
3337 return m_data
[0].GetCount();
3342 wxString
wxGridStringTable::GetValue( int row
, int col
)
3344 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3346 _T("invalid row or column index in wxGridStringTable") );
3348 return m_data
[row
][col
];
3351 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3353 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3354 _T("invalid row or column index in wxGridStringTable") );
3356 m_data
[row
][col
] = value
;
3359 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3361 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3363 _T("invalid row or column index in wxGridStringTable") );
3365 return (m_data
[row
][col
] == wxEmptyString
);
3368 void wxGridStringTable::Clear()
3371 int numRows
, numCols
;
3373 numRows
= m_data
.GetCount();
3376 numCols
= m_data
[0].GetCount();
3378 for ( row
= 0; row
< numRows
; row
++ )
3380 for ( col
= 0; col
< numCols
; col
++ )
3382 m_data
[row
][col
] = wxEmptyString
;
3388 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3390 size_t curNumRows
= m_data
.GetCount();
3391 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3392 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3394 if ( pos
>= curNumRows
)
3396 return AppendRows( numRows
);
3400 sa
.Alloc( curNumCols
);
3401 sa
.Add( wxEmptyString
, curNumCols
);
3402 m_data
.Insert( sa
, pos
, numRows
);
3406 wxGridTableMessage
msg( this,
3407 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3411 GetView()->ProcessTableMessage( msg
);
3417 bool wxGridStringTable::AppendRows( size_t numRows
)
3419 size_t curNumRows
= m_data
.GetCount();
3420 size_t curNumCols
= ( curNumRows
> 0
3421 ? m_data
[0].GetCount()
3422 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3425 if ( curNumCols
> 0 )
3427 sa
.Alloc( curNumCols
);
3428 sa
.Add( wxEmptyString
, curNumCols
);
3431 m_data
.Add( sa
, numRows
);
3435 wxGridTableMessage
msg( this,
3436 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3439 GetView()->ProcessTableMessage( msg
);
3445 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3447 size_t curNumRows
= m_data
.GetCount();
3449 if ( pos
>= curNumRows
)
3451 wxFAIL_MSG( wxString::Format
3453 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3455 (unsigned long)numRows
,
3456 (unsigned long)curNumRows
3462 if ( numRows
> curNumRows
- pos
)
3464 numRows
= curNumRows
- pos
;
3467 if ( numRows
>= curNumRows
)
3473 m_data
.RemoveAt( pos
, numRows
);
3478 wxGridTableMessage
msg( this,
3479 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3483 GetView()->ProcessTableMessage( msg
);
3489 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3493 size_t curNumRows
= m_data
.GetCount();
3494 size_t curNumCols
= ( curNumRows
> 0
3495 ? m_data
[0].GetCount()
3496 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3498 if ( pos
>= curNumCols
)
3500 return AppendCols( numCols
);
3503 for ( row
= 0; row
< curNumRows
; row
++ )
3505 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3507 m_data
[row
].Insert( wxEmptyString
, col
);
3513 wxGridTableMessage
msg( this,
3514 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3518 GetView()->ProcessTableMessage( msg
);
3524 bool wxGridStringTable::AppendCols( size_t numCols
)
3528 size_t curNumRows
= m_data
.GetCount();
3533 // TODO: something better than this ?
3535 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3540 for ( row
= 0; row
< curNumRows
; row
++ )
3542 m_data
[row
].Add( wxEmptyString
, numCols
);
3547 wxGridTableMessage
msg( this,
3548 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3551 GetView()->ProcessTableMessage( msg
);
3557 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3561 size_t curNumRows
= m_data
.GetCount();
3562 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3563 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3565 if ( pos
>= curNumCols
)
3567 wxFAIL_MSG( wxString::Format
3569 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3571 (unsigned long)numCols
,
3572 (unsigned long)curNumCols
3577 if ( numCols
> curNumCols
- pos
)
3579 numCols
= curNumCols
- pos
;
3582 for ( row
= 0; row
< curNumRows
; row
++ )
3584 if ( numCols
>= curNumCols
)
3586 m_data
[row
].Clear();
3590 m_data
[row
].RemoveAt( pos
, numCols
);
3596 wxGridTableMessage
msg( this,
3597 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3601 GetView()->ProcessTableMessage( msg
);
3607 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3609 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3611 // using default label
3613 return wxGridTableBase::GetRowLabelValue( row
);
3617 return m_rowLabels
[row
];
3621 wxString
wxGridStringTable::GetColLabelValue( int col
)
3623 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3625 // using default label
3627 return wxGridTableBase::GetColLabelValue( col
);
3631 return m_colLabels
[col
];
3635 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3637 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3639 int n
= m_rowLabels
.GetCount();
3642 for ( i
= n
; i
<= row
; i
++ )
3644 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3648 m_rowLabels
[row
] = value
;
3651 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3653 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3655 int n
= m_colLabels
.GetCount();
3658 for ( i
= n
; i
<= col
; i
++ )
3660 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3664 m_colLabels
[col
] = value
;
3668 //////////////////////////////////////////////////////////////////////
3669 //////////////////////////////////////////////////////////////////////
3671 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3673 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3674 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3675 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3676 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3677 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3678 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3679 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3682 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3684 const wxPoint
&pos
, const wxSize
&size
)
3685 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3690 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3694 // NO - don't do this because it will set both the x and y origin
3695 // coords to match the parent scrolled window and we just want to
3696 // set the y coord - MB
3698 // m_owner->PrepareDC( dc );
3701 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3702 dc
.SetDeviceOrigin( 0, -y
);
3704 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3705 m_owner
->DrawRowLabels( dc
, rows
);
3708 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3710 m_owner
->ProcessRowLabelMouseEvent( event
);
3713 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3715 m_owner
->GetEventHandler()->ProcessEvent( event
);
3718 // This seems to be required for wxMotif otherwise the mouse
3719 // cursor must be in the cell edit control to get key events
3721 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3723 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3727 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3729 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3733 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3735 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3739 //////////////////////////////////////////////////////////////////////
3741 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3743 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3744 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3745 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3746 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3747 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3748 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3749 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3752 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3754 const wxPoint
&pos
, const wxSize
&size
)
3755 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3760 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3764 // NO - don't do this because it will set both the x and y origin
3765 // coords to match the parent scrolled window and we just want to
3766 // set the x coord - MB
3768 // m_owner->PrepareDC( dc );
3771 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3772 dc
.SetDeviceOrigin( -x
, 0 );
3774 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3775 m_owner
->DrawColLabels( dc
, cols
);
3778 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3780 m_owner
->ProcessColLabelMouseEvent( event
);
3783 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3785 m_owner
->GetEventHandler()->ProcessEvent( event
);
3788 // This seems to be required for wxMotif otherwise the mouse
3789 // cursor must be in the cell edit control to get key events
3791 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3793 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3797 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3799 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3803 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3805 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3809 //////////////////////////////////////////////////////////////////////
3811 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3813 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3814 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3815 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3816 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3817 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3818 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3819 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3822 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3824 const wxPoint
&pos
, const wxSize
&size
)
3825 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3830 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3834 int client_height
= 0;
3835 int client_width
= 0;
3836 GetClientSize( &client_width
, &client_height
);
3838 // VZ: any reason for this ifdef? (FIXME)
3843 rect
.SetWidth( client_width
- 2 );
3844 rect
.SetHeight( client_height
- 2 );
3846 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3848 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
), 1, wxSOLID
) );
3849 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3850 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3851 dc
.DrawLine( 0, 0, client_width
, 0 );
3852 dc
.DrawLine( 0, 0, 0, client_height
);
3854 dc
.SetPen( *wxWHITE_PEN
);
3855 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3856 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3860 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3862 m_owner
->ProcessCornerLabelMouseEvent( event
);
3865 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3867 m_owner
->GetEventHandler()->ProcessEvent(event
);
3870 // This seems to be required for wxMotif otherwise the mouse
3871 // cursor must be in the cell edit control to get key events
3873 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3875 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3879 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3881 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3885 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3887 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3891 //////////////////////////////////////////////////////////////////////
3893 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3895 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3896 EVT_PAINT( wxGridWindow::OnPaint
)
3897 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3898 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3899 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3900 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3901 EVT_CHAR( wxGridWindow::OnChar
)
3902 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3903 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3904 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3907 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3908 wxGridRowLabelWindow
*rowLblWin
,
3909 wxGridColLabelWindow
*colLblWin
,
3912 const wxSize
&size
)
3914 parent
, id
, pos
, size
,
3915 wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
| wxFULL_REPAINT_ON_RESIZE
,
3916 wxT("grid window") )
3919 m_rowLabelWin
= rowLblWin
;
3920 m_colLabelWin
= colLblWin
;
3923 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3925 wxPaintDC
dc( this );
3926 m_owner
->PrepareDC( dc
);
3927 wxRegion reg
= GetUpdateRegion();
3928 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3929 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3931 #if WXGRID_DRAW_LINES
3932 m_owner
->DrawAllGridLines( dc
, reg
);
3935 m_owner
->DrawGridSpace( dc
);
3936 m_owner
->DrawHighlight( dc
, dirtyCells
);
3939 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3941 wxWindow::ScrollWindow( dx
, dy
, rect
);
3942 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3943 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3946 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3948 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3951 m_owner
->ProcessGridCellMouseEvent( event
);
3954 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3956 m_owner
->GetEventHandler()->ProcessEvent( event
);
3959 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3960 // cursor must be in the cell edit control to get key events
3962 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3964 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3968 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3970 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3974 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3976 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3980 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3984 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3986 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3990 //////////////////////////////////////////////////////////////////////
3992 // Internal Helper function for computing row or column from some
3993 // (unscrolled) coordinate value, using either
3994 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3995 // of m_rowBottoms/m_ColRights to speed up the search!
3997 // Internal helper macros for simpler use of that function
3999 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4000 const wxArrayInt
& BorderArray
, int nMax
,
4003 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
4004 m_minAcceptableColWidth, \
4005 m_colRights, m_numCols, true)
4006 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4007 m_minAcceptableRowHeight, \
4008 m_rowBottoms, m_numRows, true)
4010 /////////////////////////////////////////////////////////////////////
4012 #if wxUSE_EXTENDED_RTTI
4013 WX_DEFINE_FLAGS( wxGridStyle
)
4015 wxBEGIN_FLAGS( wxGridStyle
)
4016 // new style border flags, we put them first to
4017 // use them for streaming out
4018 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4019 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4020 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4021 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4022 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4023 wxFLAGS_MEMBER(wxBORDER_NONE
)
4025 // old style border flags
4026 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4027 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4028 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4029 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4030 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4031 wxFLAGS_MEMBER(wxBORDER
)
4033 // standard window styles
4034 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4035 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4036 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4037 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4038 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4039 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4040 wxFLAGS_MEMBER(wxVSCROLL
)
4041 wxFLAGS_MEMBER(wxHSCROLL
)
4043 wxEND_FLAGS( wxGridStyle
)
4045 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4047 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4048 wxHIDE_PROPERTY( Children
)
4049 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4050 wxEND_PROPERTIES_TABLE()
4052 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4053 wxEND_HANDLERS_TABLE()
4055 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4058 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4061 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4064 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4065 EVT_PAINT( wxGrid::OnPaint
)
4066 EVT_SIZE( wxGrid::OnSize
)
4067 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4068 EVT_KEY_UP( wxGrid::OnKeyUp
)
4069 EVT_CHAR ( wxGrid::OnChar
)
4070 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4075 // in order to make sure that a size event is not
4076 // trigerred in a unfinished state
4077 m_cornerLabelWin
= NULL
;
4078 m_rowLabelWin
= NULL
;
4079 m_colLabelWin
= NULL
;
4083 wxGrid::wxGrid( wxWindow
*parent
,
4088 const wxString
& name
)
4089 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4090 m_colMinWidths(GRID_HASH_SIZE
),
4091 m_rowMinHeights(GRID_HASH_SIZE
)
4094 SetBestFittingSize(size
);
4097 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4098 const wxPoint
& pos
, const wxSize
& size
,
4099 long style
, const wxString
& name
)
4101 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4102 style
| wxWANTS_CHARS
, name
))
4105 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4106 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4109 SetBestFittingSize(size
);
4116 // Must do this or ~wxScrollHelper will pop the wrong event handler
4117 SetTargetWindow(this);
4119 wxSafeDecRef(m_defaultCellAttr
);
4121 #ifdef DEBUG_ATTR_CACHE
4122 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4123 wxPrintf(_T("wxGrid attribute cache statistics: "
4124 "total: %u, hits: %u (%u%%)\n"),
4125 total
, gs_nAttrCacheHits
,
4126 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4132 delete m_typeRegistry
;
4137 // ----- internal init and update functions
4140 // NOTE: If using the default visual attributes works everywhere then this can
4141 // be removed as well as the #else cases below.
4142 #define _USE_VISATTR 0
4145 #include "wx/listbox.h"
4148 void wxGrid::Create()
4150 m_created
= false; // set to true by CreateGrid
4152 m_table
= (wxGridTableBase
*) NULL
;
4155 m_cellEditCtrlEnabled
= false;
4157 m_defaultCellAttr
= new wxGridCellAttr();
4159 // Set default cell attributes
4160 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4161 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4162 m_defaultCellAttr
->SetFont(GetFont());
4163 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4164 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4165 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4168 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4169 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4171 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4172 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4175 m_defaultCellAttr
->SetTextColour(
4176 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4177 m_defaultCellAttr
->SetBackgroundColour(
4178 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4183 m_currentCellCoords
= wxGridNoCellCoords
;
4185 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4186 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4188 // create the type registry
4189 m_typeRegistry
= new wxGridTypeRegistry
;
4192 // subwindow components that make up the wxGrid
4193 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4198 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4203 m_colLabelWin
= new wxGridColLabelWindow( this,
4208 m_gridWin
= new wxGridWindow( this,
4215 SetTargetWindow( m_gridWin
);
4218 wxColour gfg
= gva
.colFg
;
4219 wxColour gbg
= gva
.colBg
;
4220 wxColour lfg
= lva
.colFg
;
4221 wxColour lbg
= lva
.colBg
;
4223 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4224 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4225 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4226 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4229 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4230 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4231 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4232 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4233 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4234 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4236 m_gridWin
->SetOwnForegroundColour(gfg
);
4237 m_gridWin
->SetOwnBackgroundColour(gbg
);
4242 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4243 wxGrid::wxGridSelectionModes selmode
)
4245 wxCHECK_MSG( !m_created
,
4247 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4249 m_numRows
= numRows
;
4250 m_numCols
= numCols
;
4252 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4253 m_table
->SetView( this );
4255 m_selection
= new wxGridSelection( this, selmode
);
4264 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4266 wxCHECK_RET( m_created
,
4267 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4269 m_selection
->SetSelectionMode( selmode
);
4272 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4274 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4275 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4277 return m_selection
->GetSelectionMode();
4280 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4281 wxGrid::wxGridSelectionModes selmode
)
4285 // stop all processing
4290 wxGridTableBase
*t
= m_table
;
4305 m_numRows
= table
->GetNumberRows();
4306 m_numCols
= table
->GetNumberCols();
4309 m_table
->SetView( this );
4310 m_ownTable
= takeOwnership
;
4311 m_selection
= new wxGridSelection( this, selmode
);
4323 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4324 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4326 if ( m_rowLabelWin
)
4328 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4332 m_labelBackgroundColour
= wxColour( wxT("WHITE") );
4335 m_labelTextColour
= wxColour( wxT("BLACK") );
4338 m_attrCache
.row
= -1;
4339 m_attrCache
.col
= -1;
4340 m_attrCache
.attr
= NULL
;
4342 // TODO: something better than this ?
4344 m_labelFont
= this->GetFont();
4345 m_labelFont
.SetWeight( wxBOLD
);
4347 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4348 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4350 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4351 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4352 m_colLabelTextOrientation
= wxHORIZONTAL
;
4354 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4355 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4357 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4358 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4360 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4361 m_defaultRowHeight
+= 8;
4363 m_defaultRowHeight
+= 4;
4366 m_gridLineColour
= wxColour( 192,192,192 );
4367 m_gridLinesEnabled
= true;
4368 m_cellHighlightColour
= *wxBLACK
;
4369 m_cellHighlightPenWidth
= 2;
4370 m_cellHighlightROPenWidth
= 1;
4372 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4373 m_winCapture
= (wxWindow
*)NULL
;
4374 m_canDragRowSize
= true;
4375 m_canDragColSize
= true;
4376 m_canDragGridSize
= true;
4377 m_canDragCell
= false;
4379 m_dragRowOrCol
= -1;
4380 m_isDragging
= false;
4381 m_startDragPos
= wxDefaultPosition
;
4383 m_waitForSlowClick
= false;
4385 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4386 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4388 m_currentCellCoords
= wxGridNoCellCoords
;
4390 m_selectingTopLeft
= wxGridNoCellCoords
;
4391 m_selectingBottomRight
= wxGridNoCellCoords
;
4392 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4393 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4395 m_editable
= true; // default for whole grid
4397 m_inOnKeyDown
= false;
4403 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4404 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4407 // ----------------------------------------------------------------------------
4408 // the idea is to call these functions only when necessary because they create
4409 // quite big arrays which eat memory mostly unnecessary - in particular, if
4410 // default widths/heights are used for all rows/columns, we may not use these
4413 // with some extra code, it should be possible to only store the
4414 // widths/heights different from default ones but this will be done later...
4415 // ----------------------------------------------------------------------------
4417 void wxGrid::InitRowHeights()
4419 m_rowHeights
.Empty();
4420 m_rowBottoms
.Empty();
4422 m_rowHeights
.Alloc( m_numRows
);
4423 m_rowBottoms
.Alloc( m_numRows
);
4427 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4429 for ( int i
= 0; i
< m_numRows
; i
++ )
4431 rowBottom
+= m_defaultRowHeight
;
4432 m_rowBottoms
.Add( rowBottom
);
4436 void wxGrid::InitColWidths()
4438 m_colWidths
.Empty();
4439 m_colRights
.Empty();
4441 m_colWidths
.Alloc( m_numCols
);
4442 m_colRights
.Alloc( m_numCols
);
4445 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4447 for ( int i
= 0; i
< m_numCols
; i
++ )
4449 colRight
+= m_defaultColWidth
;
4450 m_colRights
.Add( colRight
);
4454 int wxGrid::GetColWidth(int col
) const
4456 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4459 int wxGrid::GetColLeft(int col
) const
4461 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4462 : m_colRights
[col
] - m_colWidths
[col
];
4465 int wxGrid::GetColRight(int col
) const
4467 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4471 int wxGrid::GetRowHeight(int row
) const
4473 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4476 int wxGrid::GetRowTop(int row
) const
4478 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4479 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4482 int wxGrid::GetRowBottom(int row
) const
4484 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4485 : m_rowBottoms
[row
];
4488 void wxGrid::CalcDimensions()
4491 GetClientSize( &cw
, &ch
);
4493 if ( m_rowLabelWin
->IsShown() )
4494 cw
-= m_rowLabelWidth
;
4495 if ( m_colLabelWin
->IsShown() )
4496 ch
-= m_colLabelHeight
;
4499 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4500 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4502 // take into account editor if shown
4503 if ( IsCellEditControlShown() )
4506 int r
= m_currentCellCoords
.GetRow();
4507 int c
= m_currentCellCoords
.GetCol();
4508 int x
= GetColLeft(c
);
4509 int y
= GetRowTop(r
);
4511 // how big is the editor
4512 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4513 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4514 editor
->GetControl()->GetSize(&w2
, &h2
);
4525 // preserve (more or less) the previous position
4527 GetViewStart( &x
, &y
);
4529 // ensure the position is valid for the new scroll ranges
4531 x
= wxMax( w
- 1, 0 );
4533 y
= wxMax( h
- 1, 0 );
4535 // do set scrollbar parameters
4536 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4537 GetScrollX(w
), GetScrollY(h
), x
, y
,
4538 GetBatchCount() != 0);
4540 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4541 // still must reposition the children
4546 void wxGrid::CalcWindowSizes()
4548 // escape if the window is has not been fully created yet
4550 if ( m_cornerLabelWin
== NULL
)
4554 GetClientSize( &cw
, &ch
);
4556 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4557 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4559 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4560 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4562 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4563 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4565 if ( m_gridWin
&& m_gridWin
->IsShown() )
4566 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4570 // this is called when the grid table sends a message to say that it
4571 // has been redimensioned
4573 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4576 bool result
= false;
4578 // Clear the attribute cache as the attribute might refer to a different
4579 // cell than stored in the cache after adding/removing rows/columns.
4582 // By the same reasoning, the editor should be dismissed if columns are
4583 // added or removed. And for consistency, it should IMHO always be
4584 // removed, not only if the cell "underneath" it actually changes.
4585 // For now, I intentionally do not save the editor's content as the
4586 // cell it might want to save that stuff to might no longer exist.
4587 HideCellEditControl();
4590 // if we were using the default widths/heights so far, we must change them
4592 if ( m_colWidths
.IsEmpty() )
4597 if ( m_rowHeights
.IsEmpty() )
4603 switch ( msg
.GetId() )
4605 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4607 size_t pos
= msg
.GetCommandInt();
4608 int numRows
= msg
.GetCommandInt2();
4610 m_numRows
+= numRows
;
4612 if ( !m_rowHeights
.IsEmpty() )
4614 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4615 m_rowBottoms
.Insert( 0, pos
, numRows
);
4619 bottom
= m_rowBottoms
[pos
- 1];
4621 for ( i
= pos
; i
< m_numRows
; i
++ )
4623 bottom
+= m_rowHeights
[i
];
4624 m_rowBottoms
[i
] = bottom
;
4628 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4630 // if we have just inserted cols into an empty grid the current
4631 // cell will be undefined...
4633 SetCurrentCell( 0, 0 );
4637 m_selection
->UpdateRows( pos
, numRows
);
4638 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4640 attrProvider
->UpdateAttrRows( pos
, numRows
);
4642 if ( !GetBatchCount() )
4645 m_rowLabelWin
->Refresh();
4651 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4653 int numRows
= msg
.GetCommandInt();
4654 int oldNumRows
= m_numRows
;
4655 m_numRows
+= numRows
;
4657 if ( !m_rowHeights
.IsEmpty() )
4659 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4660 m_rowBottoms
.Add( 0, numRows
);
4663 if ( oldNumRows
> 0 )
4664 bottom
= m_rowBottoms
[oldNumRows
- 1];
4666 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4668 bottom
+= m_rowHeights
[i
];
4669 m_rowBottoms
[i
] = bottom
;
4673 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4675 // if we have just inserted cols into an empty grid the current
4676 // cell will be undefined...
4678 SetCurrentCell( 0, 0 );
4681 if ( !GetBatchCount() )
4684 m_rowLabelWin
->Refresh();
4690 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4692 size_t pos
= msg
.GetCommandInt();
4693 int numRows
= msg
.GetCommandInt2();
4694 m_numRows
-= numRows
;
4696 if ( !m_rowHeights
.IsEmpty() )
4698 m_rowHeights
.RemoveAt( pos
, numRows
);
4699 m_rowBottoms
.RemoveAt( pos
, numRows
);
4702 for ( i
= 0; i
< m_numRows
; i
++ )
4704 h
+= m_rowHeights
[i
];
4705 m_rowBottoms
[i
] = h
;
4710 m_currentCellCoords
= wxGridNoCellCoords
;
4714 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4715 m_currentCellCoords
.Set( 0, 0 );
4719 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4720 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4723 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4724 // ifdef'd out following patch from Paul Gammans
4726 // No need to touch column attributes, unless we
4727 // removed _all_ rows, in this case, we remove
4728 // all column attributes.
4729 // I hate to do this here, but the
4730 // needed data is not available inside UpdateAttrRows.
4731 if ( !GetNumberRows() )
4732 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4736 if ( !GetBatchCount() )
4739 m_rowLabelWin
->Refresh();
4745 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4747 size_t pos
= msg
.GetCommandInt();
4748 int numCols
= msg
.GetCommandInt2();
4749 m_numCols
+= numCols
;
4751 if ( !m_colWidths
.IsEmpty() )
4753 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4754 m_colRights
.Insert( 0, pos
, numCols
);
4758 right
= m_colRights
[pos
- 1];
4760 for ( i
= pos
; i
< m_numCols
; i
++ )
4762 right
+= m_colWidths
[i
];
4763 m_colRights
[i
] = right
;
4767 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4769 // if we have just inserted cols into an empty grid the current
4770 // cell will be undefined...
4772 SetCurrentCell( 0, 0 );
4776 m_selection
->UpdateCols( pos
, numCols
);
4777 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4779 attrProvider
->UpdateAttrCols( pos
, numCols
);
4780 if ( !GetBatchCount() )
4783 m_colLabelWin
->Refresh();
4790 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4792 int numCols
= msg
.GetCommandInt();
4793 int oldNumCols
= m_numCols
;
4794 m_numCols
+= numCols
;
4795 if ( !m_colWidths
.IsEmpty() )
4797 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4798 m_colRights
.Add( 0, numCols
);
4801 if ( oldNumCols
> 0 )
4802 right
= m_colRights
[oldNumCols
- 1];
4804 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4806 right
+= m_colWidths
[i
];
4807 m_colRights
[i
] = right
;
4811 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4813 // if we have just inserted cols into an empty grid the current
4814 // cell will be undefined...
4816 SetCurrentCell( 0, 0 );
4818 if ( !GetBatchCount() )
4821 m_colLabelWin
->Refresh();
4827 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4829 size_t pos
= msg
.GetCommandInt();
4830 int numCols
= msg
.GetCommandInt2();
4831 m_numCols
-= numCols
;
4833 if ( !m_colWidths
.IsEmpty() )
4835 m_colWidths
.RemoveAt( pos
, numCols
);
4836 m_colRights
.RemoveAt( pos
, numCols
);
4839 for ( i
= 0; i
< m_numCols
; i
++ )
4841 w
+= m_colWidths
[i
];
4848 m_currentCellCoords
= wxGridNoCellCoords
;
4852 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4853 m_currentCellCoords
.Set( 0, 0 );
4857 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4858 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4861 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4863 // ifdef'd out following patch from Paul Gammans
4865 // No need to touch row attributes, unless we
4866 // removed _all_ columns, in this case, we remove
4867 // all row attributes.
4868 // I hate to do this here, but the
4869 // needed data is not available inside UpdateAttrCols.
4870 if ( !GetNumberCols() )
4871 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4875 if ( !GetBatchCount() )
4878 m_colLabelWin
->Refresh();
4885 if (result
&& !GetBatchCount() )
4886 m_gridWin
->Refresh();
4891 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4893 wxRegionIterator
iter( reg
);
4896 wxArrayInt rowlabels
;
4903 // TODO: remove this when we can...
4904 // There is a bug in wxMotif that gives garbage update
4905 // rectangles if you jump-scroll a long way by clicking the
4906 // scrollbar with middle button. This is a work-around
4908 #if defined(__WXMOTIF__)
4910 m_gridWin
->GetClientSize( &cw
, &ch
);
4911 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4912 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4915 // logical bounds of update region
4918 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4919 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4921 // find the row labels within these bounds
4924 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4926 if ( GetRowBottom(row
) < top
)
4929 if ( GetRowTop(row
) > bottom
)
4932 rowlabels
.Add( row
);
4941 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4943 wxRegionIterator
iter( reg
);
4946 wxArrayInt colLabels
;
4953 // TODO: remove this when we can...
4954 // There is a bug in wxMotif that gives garbage update
4955 // rectangles if you jump-scroll a long way by clicking the
4956 // scrollbar with middle button. This is a work-around
4958 #if defined(__WXMOTIF__)
4960 m_gridWin
->GetClientSize( &cw
, &ch
);
4961 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4962 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4965 // logical bounds of update region
4968 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4969 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4971 // find the cells within these bounds
4974 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4976 if ( GetColRight(col
) < left
)
4979 if ( GetColLeft(col
) > right
)
4982 colLabels
.Add( col
);
4991 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4993 wxRegionIterator
iter( reg
);
4996 wxGridCellCoordsArray cellsExposed
;
4998 int left
, top
, right
, bottom
;
5003 // TODO: remove this when we can...
5004 // There is a bug in wxMotif that gives garbage update
5005 // rectangles if you jump-scroll a long way by clicking the
5006 // scrollbar with middle button. This is a work-around
5008 #if defined(__WXMOTIF__)
5010 m_gridWin
->GetClientSize( &cw
, &ch
);
5011 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5012 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5013 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5014 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5017 // logical bounds of update region
5019 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5020 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5022 // find the cells within these bounds
5025 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5027 if ( GetRowBottom(row
) <= top
)
5030 if ( GetRowTop(row
) > bottom
)
5033 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
5035 if ( GetColRight(col
) <= left
)
5038 if ( GetColLeft(col
) > right
)
5041 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5048 return cellsExposed
;
5052 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5055 wxPoint
pos( event
.GetPosition() );
5056 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5058 if ( event
.Dragging() )
5062 m_isDragging
= true;
5063 m_rowLabelWin
->CaptureMouse();
5066 if ( event
.LeftIsDown() )
5068 switch ( m_cursorMode
)
5070 case WXGRID_CURSOR_RESIZE_ROW
:
5072 int cw
, ch
, left
, dummy
;
5073 m_gridWin
->GetClientSize( &cw
, &ch
);
5074 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5076 wxClientDC
dc( m_gridWin
);
5079 GetRowTop(m_dragRowOrCol
) +
5080 GetRowMinimalHeight(m_dragRowOrCol
) );
5081 dc
.SetLogicalFunction(wxINVERT
);
5082 if ( m_dragLastPos
>= 0 )
5084 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5086 dc
.DrawLine( left
, y
, left
+cw
, y
);
5091 case WXGRID_CURSOR_SELECT_ROW
:
5093 if ( (row
= YToRow( y
)) >= 0 )
5097 m_selection
->SelectRow( row
,
5098 event
.ControlDown(),
5107 // default label to suppress warnings about "enumeration value
5108 // 'xxx' not handled in switch
5116 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5121 if (m_rowLabelWin
->HasCapture())
5122 m_rowLabelWin
->ReleaseMouse();
5123 m_isDragging
= false;
5126 // ------------ Entering or leaving the window
5128 if ( event
.Entering() || event
.Leaving() )
5130 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5133 // ------------ Left button pressed
5135 else if ( event
.LeftDown() )
5137 // don't send a label click event for a hit on the
5138 // edge of the row label - this is probably the user
5139 // wanting to resize the row
5141 if ( YToEdgeOfRow(y
) < 0 )
5145 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5147 if ( !event
.ShiftDown() && !event
.CmdDown() )
5151 if ( event
.ShiftDown() )
5153 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5156 GetNumberCols() - 1,
5157 event
.ControlDown(),
5164 m_selection
->SelectRow( row
,
5165 event
.ControlDown(),
5172 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5177 // starting to drag-resize a row
5178 if ( CanDragRowSize() )
5179 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5183 // ------------ Left double click
5185 else if (event
.LeftDClick() )
5187 row
= YToEdgeOfRow(y
);
5192 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5194 // no default action at the moment
5199 // adjust row height depending on label text
5200 AutoSizeRowLabelSize( row
);
5202 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5207 // ------------ Left button released
5209 else if ( event
.LeftUp() )
5211 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5213 DoEndDragResizeRow();
5215 // Note: we are ending the event *after* doing
5216 // default processing in this case
5218 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5221 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5225 // ------------ Right button down
5227 else if ( event
.RightDown() )
5231 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5233 // no default action at the moment
5237 // ------------ Right double click
5239 else if ( event
.RightDClick() )
5243 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5245 // no default action at the moment
5249 // ------------ No buttons down and mouse moving
5251 else if ( event
.Moving() )
5253 m_dragRowOrCol
= YToEdgeOfRow( y
);
5254 if ( m_dragRowOrCol
>= 0 )
5256 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5258 // don't capture the mouse yet
5259 if ( CanDragRowSize() )
5260 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5263 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5265 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5270 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5273 wxPoint
pos( event
.GetPosition() );
5274 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5276 if ( event
.Dragging() )
5280 m_isDragging
= true;
5281 m_colLabelWin
->CaptureMouse();
5284 if ( event
.LeftIsDown() )
5286 switch ( m_cursorMode
)
5288 case WXGRID_CURSOR_RESIZE_COL
:
5290 int cw
, ch
, dummy
, top
;
5291 m_gridWin
->GetClientSize( &cw
, &ch
);
5292 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5294 wxClientDC
dc( m_gridWin
);
5297 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5298 GetColMinimalWidth(m_dragRowOrCol
));
5299 dc
.SetLogicalFunction(wxINVERT
);
5300 if ( m_dragLastPos
>= 0 )
5302 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5304 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5309 case WXGRID_CURSOR_SELECT_COL
:
5311 if ( (col
= XToCol( x
)) >= 0 )
5315 m_selection
->SelectCol( col
,
5316 event
.ControlDown(),
5325 // default label to suppress warnings about "enumeration value
5326 // 'xxx' not handled in switch
5334 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5339 if (m_colLabelWin
->HasCapture())
5340 m_colLabelWin
->ReleaseMouse();
5341 m_isDragging
= false;
5344 // ------------ Entering or leaving the window
5346 if ( event
.Entering() || event
.Leaving() )
5348 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5351 // ------------ Left button pressed
5353 else if ( event
.LeftDown() )
5355 // don't send a label click event for a hit on the
5356 // edge of the col label - this is probably the user
5357 // wanting to resize the col
5359 if ( XToEdgeOfCol(x
) < 0 )
5363 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5365 if ( !event
.ShiftDown() && !event
.CmdDown() )
5369 if ( event
.ShiftDown() )
5371 m_selection
->SelectBlock( 0,
5372 m_currentCellCoords
.GetCol(),
5373 GetNumberRows() - 1, col
,
5374 event
.ControlDown(),
5381 m_selection
->SelectCol( col
,
5382 event
.ControlDown(),
5389 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5394 // starting to drag-resize a col
5396 if ( CanDragColSize() )
5397 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5401 // ------------ Left double click
5403 if ( event
.LeftDClick() )
5405 col
= XToEdgeOfCol(x
);
5410 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5412 // no default action at the moment
5417 // adjust column width depending on label text
5418 AutoSizeColLabelSize( col
);
5420 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5425 // ------------ Left button released
5427 else if ( event
.LeftUp() )
5429 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5431 DoEndDragResizeCol();
5433 // Note: we are ending the event *after* doing
5434 // default processing in this case
5436 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5439 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5443 // ------------ Right button down
5445 else if ( event
.RightDown() )
5449 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5451 // no default action at the moment
5455 // ------------ Right double click
5457 else if ( event
.RightDClick() )
5461 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5463 // no default action at the moment
5467 // ------------ No buttons down and mouse moving
5469 else if ( event
.Moving() )
5471 m_dragRowOrCol
= XToEdgeOfCol( x
);
5472 if ( m_dragRowOrCol
>= 0 )
5474 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5476 // don't capture the cursor yet
5477 if ( CanDragColSize() )
5478 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5481 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5483 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5488 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5490 if ( event
.LeftDown() )
5492 // indicate corner label by having both row and
5495 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5500 else if ( event
.LeftDClick() )
5502 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5504 else if ( event
.RightDown() )
5506 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5508 // no default action at the moment
5511 else if ( event
.RightDClick() )
5513 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5515 // no default action at the moment
5520 void wxGrid::ChangeCursorMode(CursorMode mode
,
5525 static const wxChar
*cursorModes
[] =
5534 wxLogTrace(_T("grid"),
5535 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5536 win
== m_colLabelWin
? _T("colLabelWin")
5537 : win
? _T("rowLabelWin")
5539 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5542 if ( mode
== m_cursorMode
&&
5543 win
== m_winCapture
&&
5544 captureMouse
== (m_winCapture
!= NULL
))
5549 // by default use the grid itself
5555 if (m_winCapture
->HasCapture())
5556 m_winCapture
->ReleaseMouse();
5557 m_winCapture
= (wxWindow
*)NULL
;
5560 m_cursorMode
= mode
;
5562 switch ( m_cursorMode
)
5564 case WXGRID_CURSOR_RESIZE_ROW
:
5565 win
->SetCursor( m_rowResizeCursor
);
5568 case WXGRID_CURSOR_RESIZE_COL
:
5569 win
->SetCursor( m_colResizeCursor
);
5573 win
->SetCursor( *wxSTANDARD_CURSOR
);
5577 // we need to capture mouse when resizing
5578 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5579 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5581 if ( captureMouse
&& resize
)
5583 win
->CaptureMouse();
5588 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5591 wxPoint
pos( event
.GetPosition() );
5592 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5594 wxGridCellCoords coords
;
5595 XYToCell( x
, y
, coords
);
5597 int cell_rows
, cell_cols
;
5598 bool isFirstDrag
= !m_isDragging
;
5599 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5600 if ((cell_rows
< 0) || (cell_cols
< 0))
5602 coords
.SetRow(coords
.GetRow() + cell_rows
);
5603 coords
.SetCol(coords
.GetCol() + cell_cols
);
5606 if ( event
.Dragging() )
5608 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5610 // Don't start doing anything until the mouse has been dragged at
5611 // least 3 pixels in any direction...
5614 if (m_startDragPos
== wxDefaultPosition
)
5616 m_startDragPos
= pos
;
5619 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5623 m_isDragging
= true;
5624 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5626 // Hide the edit control, so it
5627 // won't interfere with drag-shrinking.
5628 if ( IsCellEditControlShown() )
5630 HideCellEditControl();
5631 SaveEditControlValue();
5634 // Have we captured the mouse yet?
5637 m_winCapture
= m_gridWin
;
5638 m_winCapture
->CaptureMouse();
5641 if ( coords
!= wxGridNoCellCoords
)
5643 if ( event
.CmdDown() )
5645 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5646 m_selectingKeyboard
= coords
;
5647 HighlightBlock( m_selectingKeyboard
, coords
);
5649 else if ( CanDragCell() )
5653 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5654 m_selectingKeyboard
= coords
;
5656 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5664 if ( !IsSelection() )
5666 HighlightBlock( coords
, coords
);
5670 HighlightBlock( m_currentCellCoords
, coords
);
5674 if (! IsVisible(coords
))
5676 MakeCellVisible(coords
);
5677 // TODO: need to introduce a delay or something here. The
5678 // scrolling is way to fast, at least on MSW - also on GTK.
5682 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5684 int cw
, ch
, left
, dummy
;
5685 m_gridWin
->GetClientSize( &cw
, &ch
);
5686 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5688 wxClientDC
dc( m_gridWin
);
5690 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5691 GetRowMinimalHeight(m_dragRowOrCol
) );
5692 dc
.SetLogicalFunction(wxINVERT
);
5693 if ( m_dragLastPos
>= 0 )
5695 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5697 dc
.DrawLine( left
, y
, left
+cw
, y
);
5700 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5702 int cw
, ch
, dummy
, top
;
5703 m_gridWin
->GetClientSize( &cw
, &ch
);
5704 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5706 wxClientDC
dc( m_gridWin
);
5708 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5709 GetColMinimalWidth(m_dragRowOrCol
) );
5710 dc
.SetLogicalFunction(wxINVERT
);
5711 if ( m_dragLastPos
>= 0 )
5713 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5715 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5722 m_isDragging
= false;
5723 m_startDragPos
= wxDefaultPosition
;
5725 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5726 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5729 if ( event
.Entering() || event
.Leaving() )
5731 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5732 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5737 // ------------ Left button pressed
5739 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5741 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5746 if ( !event
.CmdDown() )
5748 if ( event
.ShiftDown() )
5752 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5753 m_currentCellCoords
.GetCol(),
5756 event
.ControlDown(),
5762 else if ( XToEdgeOfCol(x
) < 0 &&
5763 YToEdgeOfRow(y
) < 0 )
5765 DisableCellEditControl();
5766 MakeCellVisible( coords
);
5768 if ( event
.CmdDown() )
5772 m_selection
->ToggleCellSelection( coords
.GetRow(),
5774 event
.ControlDown(),
5779 m_selectingTopLeft
= wxGridNoCellCoords
;
5780 m_selectingBottomRight
= wxGridNoCellCoords
;
5781 m_selectingKeyboard
= coords
;
5785 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5786 SetCurrentCell( coords
);
5789 if ( m_selection
->GetSelectionMode() !=
5790 wxGrid::wxGridSelectCells
)
5792 HighlightBlock( coords
, coords
);
5800 // ------------ Left double click
5802 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5804 DisableCellEditControl();
5806 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5808 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5813 // we want double click to select a cell and start editing
5814 // (i.e. to behave in same way as sequence of two slow clicks):
5815 m_waitForSlowClick
= true;
5820 // ------------ Left button released
5822 else if ( event
.LeftUp() )
5824 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5828 if (m_winCapture
->HasCapture())
5829 m_winCapture
->ReleaseMouse();
5830 m_winCapture
= NULL
;
5833 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
5836 EnableCellEditControl();
5838 wxGridCellAttr
*attr
= GetCellAttr(coords
);
5839 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5840 editor
->StartingClick();
5844 m_waitForSlowClick
= false;
5846 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5847 m_selectingBottomRight
!= wxGridNoCellCoords
)
5851 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5852 m_selectingTopLeft
.GetCol(),
5853 m_selectingBottomRight
.GetRow(),
5854 m_selectingBottomRight
.GetCol(),
5855 event
.ControlDown(),
5861 m_selectingTopLeft
= wxGridNoCellCoords
;
5862 m_selectingBottomRight
= wxGridNoCellCoords
;
5864 // Show the edit control, if it has been hidden for
5866 ShowCellEditControl();
5869 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5871 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5872 DoEndDragResizeRow();
5874 // Note: we are ending the event *after* doing
5875 // default processing in this case
5877 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5879 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5881 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5882 DoEndDragResizeCol();
5884 // Note: we are ending the event *after* doing
5885 // default processing in this case
5887 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5893 // ------------ Right button down
5895 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5897 DisableCellEditControl();
5898 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5903 // no default action at the moment
5907 // ------------ Right double click
5909 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5911 DisableCellEditControl();
5912 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5917 // no default action at the moment
5921 // ------------ Moving and no button action
5923 else if ( event
.Moving() && !event
.IsButton() )
5925 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5927 // out of grid cell area
5928 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5932 int dragRow
= YToEdgeOfRow( y
);
5933 int dragCol
= XToEdgeOfCol( x
);
5935 // Dragging on the corner of a cell to resize in both
5936 // directions is not implemented yet...
5938 if ( dragRow
>= 0 && dragCol
>= 0 )
5940 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5946 m_dragRowOrCol
= dragRow
;
5948 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5950 if ( CanDragRowSize() && CanDragGridSize() )
5951 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5954 else if ( dragCol
>= 0 )
5956 m_dragRowOrCol
= dragCol
;
5958 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5960 if ( CanDragColSize() && CanDragGridSize() )
5961 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5964 else // Neither on a row or col edge
5966 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5968 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5974 void wxGrid::DoEndDragResizeRow()
5976 if ( m_dragLastPos
>= 0 )
5978 // erase the last line and resize the row
5980 int cw
, ch
, left
, dummy
;
5981 m_gridWin
->GetClientSize( &cw
, &ch
);
5982 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5984 wxClientDC
dc( m_gridWin
);
5986 dc
.SetLogicalFunction( wxINVERT
);
5987 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
5988 HideCellEditControl();
5989 SaveEditControlValue();
5991 int rowTop
= GetRowTop(m_dragRowOrCol
);
5992 SetRowSize( m_dragRowOrCol
,
5993 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5995 if ( !GetBatchCount() )
5997 // Only needed to get the correct rect.y:
5998 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6000 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6001 rect
.width
= m_rowLabelWidth
;
6002 rect
.height
= ch
- rect
.y
;
6003 m_rowLabelWin
->Refresh( true, &rect
);
6006 // if there is a multicell block, paint all of it
6009 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6010 int leftCol
= XToCol(left
);
6011 int rightCol
= internalXToCol(left
+ cw
);
6014 for (i
=leftCol
; i
<rightCol
; i
++)
6016 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6017 if (cell_rows
< subtract_rows
)
6018 subtract_rows
= cell_rows
;
6020 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6021 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6022 rect
.height
= ch
- rect
.y
;
6025 m_gridWin
->Refresh( false, &rect
);
6028 ShowCellEditControl();
6033 void wxGrid::DoEndDragResizeCol()
6035 if ( m_dragLastPos
>= 0 )
6037 // erase the last line and resize the col
6039 int cw
, ch
, dummy
, top
;
6040 m_gridWin
->GetClientSize( &cw
, &ch
);
6041 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6043 wxClientDC
dc( m_gridWin
);
6045 dc
.SetLogicalFunction( wxINVERT
);
6046 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6047 HideCellEditControl();
6048 SaveEditControlValue();
6050 int colLeft
= GetColLeft(m_dragRowOrCol
);
6051 SetColSize( m_dragRowOrCol
,
6052 wxMax( m_dragLastPos
- colLeft
,
6053 GetColMinimalWidth(m_dragRowOrCol
) ) );
6055 if ( !GetBatchCount() )
6057 // Only needed to get the correct rect.x:
6058 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6060 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6061 rect
.width
= cw
- rect
.x
;
6062 rect
.height
= m_colLabelHeight
;
6063 m_colLabelWin
->Refresh( true, &rect
);
6066 // if there is a multicell block, paint all of it
6069 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6070 int topRow
= YToRow(top
);
6071 int bottomRow
= internalYToRow(top
+ cw
);
6074 for (i
=topRow
; i
<bottomRow
; i
++)
6076 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6077 if (cell_cols
< subtract_cols
)
6078 subtract_cols
= cell_cols
;
6081 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6082 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6083 rect
.width
= cw
- rect
.x
;
6087 m_gridWin
->Refresh( false, &rect
);
6090 ShowCellEditControl();
6095 // ------ interaction with data model
6097 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6099 switch ( msg
.GetId() )
6101 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6102 return GetModelValues();
6104 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6105 return SetModelValues();
6107 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6108 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6109 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6110 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6111 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6112 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6113 return Redimension( msg
);
6120 // The behaviour of this function depends on the grid table class
6121 // Clear() function. For the default wxGridStringTable class the
6122 // behavious is to replace all cell contents with wxEmptyString but
6123 // not to change the number of rows or cols.
6125 void wxGrid::ClearGrid()
6129 if (IsCellEditControlEnabled())
6130 DisableCellEditControl();
6133 if (!GetBatchCount())
6134 m_gridWin
->Refresh();
6138 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6140 // TODO: something with updateLabels flag
6144 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6150 if (IsCellEditControlEnabled())
6151 DisableCellEditControl();
6153 bool done
= m_table
->InsertRows( pos
, numRows
);
6156 // the table will have sent the results of the insert row
6157 // operation to this view object as a grid table message
6163 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6165 // TODO: something with updateLabels flag
6169 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6175 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6178 // the table will have sent the results of the append row
6179 // operation to this view object as a grid table message
6185 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6187 // TODO: something with updateLabels flag
6191 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6197 if (IsCellEditControlEnabled())
6198 DisableCellEditControl();
6200 bool done
= m_table
->DeleteRows( pos
, numRows
);
6202 // the table will have sent the results of the delete row
6203 // operation to this view object as a grid table message
6209 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6211 // TODO: something with updateLabels flag
6215 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6221 if (IsCellEditControlEnabled())
6222 DisableCellEditControl();
6224 bool done
= m_table
->InsertCols( pos
, numCols
);
6226 // the table will have sent the results of the insert col
6227 // operation to this view object as a grid table message
6233 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6235 // TODO: something with updateLabels flag
6239 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6245 bool done
= m_table
->AppendCols( numCols
);
6247 // the table will have sent the results of the append col
6248 // operation to this view object as a grid table message
6254 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6256 // TODO: something with updateLabels flag
6260 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6266 if (IsCellEditControlEnabled())
6267 DisableCellEditControl();
6269 bool done
= m_table
->DeleteCols( pos
, numCols
);
6271 // the table will have sent the results of the delete col
6272 // operation to this view object as a grid table message
6279 // ----- event handlers
6282 // Generate a grid event based on a mouse event and
6283 // return the result of ProcessEvent()
6285 int wxGrid::SendEvent( const wxEventType type
,
6287 wxMouseEvent
& mouseEv
)
6289 bool claimed
, vetoed
;
6291 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6293 int rowOrCol
= (row
== -1 ? col
: row
);
6295 wxGridSizeEvent
gridEvt( GetId(),
6299 mouseEv
.GetX() + GetRowLabelSize(),
6300 mouseEv
.GetY() + GetColLabelSize(),
6301 mouseEv
.ControlDown(),
6302 mouseEv
.ShiftDown(),
6304 mouseEv
.MetaDown() );
6306 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6307 vetoed
= !gridEvt
.IsAllowed();
6309 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6311 // Right now, it should _never_ end up here!
6312 wxGridRangeSelectEvent
gridEvt( GetId(),
6316 m_selectingBottomRight
,
6318 mouseEv
.ControlDown(),
6319 mouseEv
.ShiftDown(),
6321 mouseEv
.MetaDown() );
6323 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6324 vetoed
= !gridEvt
.IsAllowed();
6328 wxGridEvent
gridEvt( GetId(),
6332 mouseEv
.GetX() + GetRowLabelSize(),
6333 mouseEv
.GetY() + GetColLabelSize(),
6335 mouseEv
.ControlDown(),
6336 mouseEv
.ShiftDown(),
6338 mouseEv
.MetaDown() );
6339 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6340 vetoed
= !gridEvt
.IsAllowed();
6343 // A Veto'd event may not be `claimed' so test this first
6347 return claimed
? 1 : 0;
6350 // Generate a grid event of specified type and return the result
6351 // of ProcessEvent().
6353 int wxGrid::SendEvent( const wxEventType type
,
6356 bool claimed
, vetoed
;
6358 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6360 int rowOrCol
= (row
== -1 ? col
: row
);
6362 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6364 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6365 vetoed
= !gridEvt
.IsAllowed();
6369 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6371 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6372 vetoed
= !gridEvt
.IsAllowed();
6375 // A Veto'd event may not be `claimed' so test this first
6379 return claimed
? 1 : 0;
6382 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6384 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6387 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6389 // Don't do anything if between Begin/EndBatch...
6390 // EndBatch() will do all this on the last nested one anyway.
6391 if (! GetBatchCount())
6393 // Refresh to get correct scrolled position:
6394 wxScrolledWindow::Refresh(eraseb
, rect
);
6398 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6399 int width_label
, width_cell
, height_label
, height_cell
;
6402 // Copy rectangle can get scroll offsets..
6403 rect_x
= rect
->GetX();
6404 rect_y
= rect
->GetY();
6405 rectWidth
= rect
->GetWidth();
6406 rectHeight
= rect
->GetHeight();
6408 width_label
= m_rowLabelWidth
- rect_x
;
6409 if (width_label
> rectWidth
)
6410 width_label
= rectWidth
;
6412 height_label
= m_colLabelHeight
- rect_y
;
6413 if (height_label
> rectHeight
)
6414 height_label
= rectHeight
;
6416 if (rect_x
> m_rowLabelWidth
)
6418 x
= rect_x
- m_rowLabelWidth
;
6419 width_cell
= rectWidth
;
6424 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6427 if (rect_y
> m_colLabelHeight
)
6429 y
= rect_y
- m_colLabelHeight
;
6430 height_cell
= rectHeight
;
6435 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6438 // Paint corner label part intersecting rect.
6439 if ( width_label
> 0 && height_label
> 0 )
6441 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6442 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6445 // Paint col labels part intersecting rect.
6446 if ( width_cell
> 0 && height_label
> 0 )
6448 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6449 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6452 // Paint row labels part intersecting rect.
6453 if ( width_label
> 0 && height_cell
> 0 )
6455 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6456 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6459 // Paint cell area part intersecting rect.
6460 if ( width_cell
> 0 && height_cell
> 0 )
6462 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6463 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6468 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6469 m_colLabelWin
->Refresh(eraseb
, NULL
);
6470 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6471 m_gridWin
->Refresh(eraseb
, NULL
);
6476 void wxGrid::OnSize( wxSizeEvent
& event
)
6478 // position the child windows
6481 // don't call CalcDimensions() from here, the base class handles the size
6486 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6488 if ( m_inOnKeyDown
)
6490 // shouldn't be here - we are going round in circles...
6492 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6495 m_inOnKeyDown
= true;
6497 // propagate the event up and see if it gets processed
6498 wxWindow
*parent
= GetParent();
6499 wxKeyEvent
keyEvt( event
);
6500 keyEvt
.SetEventObject( parent
);
6502 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6504 // try local handlers
6505 switch ( event
.GetKeyCode() )
6508 if ( event
.ControlDown() )
6509 MoveCursorUpBlock( event
.ShiftDown() );
6511 MoveCursorUp( event
.ShiftDown() );
6515 if ( event
.ControlDown() )
6516 MoveCursorDownBlock( event
.ShiftDown() );
6518 MoveCursorDown( event
.ShiftDown() );
6522 if ( event
.ControlDown() )
6523 MoveCursorLeftBlock( event
.ShiftDown() );
6525 MoveCursorLeft( event
.ShiftDown() );
6529 if ( event
.ControlDown() )
6530 MoveCursorRightBlock( event
.ShiftDown() );
6532 MoveCursorRight( event
.ShiftDown() );
6536 case WXK_NUMPAD_ENTER
:
6537 if ( event
.ControlDown() )
6539 event
.Skip(); // to let the edit control have the return
6543 if ( GetGridCursorRow() < GetNumberRows()-1 )
6545 MoveCursorDown( event
.ShiftDown() );
6549 // at the bottom of a column
6550 DisableCellEditControl();
6560 if (event
.ShiftDown())
6562 if ( GetGridCursorCol() > 0 )
6564 MoveCursorLeft( false );
6569 DisableCellEditControl();
6574 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6576 MoveCursorRight( false );
6581 DisableCellEditControl();
6587 if ( event
.ControlDown() )
6589 MakeCellVisible( 0, 0 );
6590 SetCurrentCell( 0, 0 );
6599 if ( event
.ControlDown() )
6601 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
6602 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
6619 if ( event
.ControlDown() )
6623 m_selection
->ToggleCellSelection(
6624 m_currentCellCoords
.GetRow(),
6625 m_currentCellCoords
.GetCol(),
6626 event
.ControlDown(),
6634 if ( !IsEditable() )
6635 MoveCursorRight( false );
6646 m_inOnKeyDown
= false;
6649 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6651 // try local handlers
6653 if ( event
.GetKeyCode() == WXK_SHIFT
)
6655 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6656 m_selectingBottomRight
!= wxGridNoCellCoords
)
6660 m_selection
->SelectBlock(
6661 m_selectingTopLeft
.GetRow(),
6662 m_selectingTopLeft
.GetCol(),
6663 m_selectingBottomRight
.GetRow(),
6664 m_selectingBottomRight
.GetCol(),
6665 event
.ControlDown(),
6672 m_selectingTopLeft
= wxGridNoCellCoords
;
6673 m_selectingBottomRight
= wxGridNoCellCoords
;
6674 m_selectingKeyboard
= wxGridNoCellCoords
;
6678 void wxGrid::OnChar( wxKeyEvent
& event
)
6680 // is it possible to edit the current cell at all?
6681 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6683 // yes, now check whether the cells editor accepts the key
6684 int row
= m_currentCellCoords
.GetRow();
6685 int col
= m_currentCellCoords
.GetCol();
6686 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6687 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6689 // <F2> is special and will always start editing, for
6690 // other keys - ask the editor itself
6691 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6692 || editor
->IsAcceptedKey(event
) )
6694 // ensure cell is visble
6695 MakeCellVisible(row
, col
);
6696 EnableCellEditControl();
6698 // a problem can arise if the cell is not completely
6699 // visible (even after calling MakeCellVisible the
6700 // control is not created and calling StartingKey will
6702 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6703 editor
->StartingKey(event
);
6719 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6723 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6725 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6727 // the event has been intercepted - do nothing
6731 wxClientDC
dc( m_gridWin
);
6734 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6736 DisableCellEditControl();
6738 if ( IsVisible( m_currentCellCoords
, false ) )
6741 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
6742 if ( !m_gridLinesEnabled
)
6750 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6752 // Otherwise refresh redraws the highlight!
6753 m_currentCellCoords
= coords
;
6755 DrawGridCellArea( dc
, cells
);
6756 DrawAllGridLines( dc
, r
);
6760 m_currentCellCoords
= coords
;
6762 wxGridCellAttr
*attr
= GetCellAttr( coords
);
6763 DrawCellHighlight( dc
, attr
);
6767 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6770 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6774 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6777 rightCol
= GetNumberCols() - 1;
6779 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6782 bottomRow
= GetNumberRows() - 1;
6786 if ( topRow
> bottomRow
)
6793 if ( leftCol
> rightCol
)
6800 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6801 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6803 // First the case that we selected a completely new area
6804 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6805 m_selectingBottomRight
== wxGridNoCellCoords
)
6808 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6809 wxGridCellCoords ( bottomRow
, rightCol
) );
6810 m_gridWin
->Refresh( false, &rect
);
6813 // Now handle changing an existing selection area.
6814 else if ( m_selectingTopLeft
!= updateTopLeft
||
6815 m_selectingBottomRight
!= updateBottomRight
)
6817 // Compute two optimal update rectangles:
6818 // Either one rectangle is a real subset of the
6819 // other, or they are (almost) disjoint!
6821 bool need_refresh
[4];
6825 need_refresh
[3] = false;
6828 // Store intermediate values
6829 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6830 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6831 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6832 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6834 // Determine the outer/inner coordinates.
6835 if (oldLeft
> leftCol
)
6841 if (oldTop
> topRow
)
6847 if (oldRight
< rightCol
)
6850 oldRight
= rightCol
;
6853 if (oldBottom
< bottomRow
)
6856 oldBottom
= bottomRow
;
6860 // Now, either the stuff marked old is the outer
6861 // rectangle or we don't have a situation where one
6862 // is contained in the other.
6864 if ( oldLeft
< leftCol
)
6866 // Refresh the newly selected or deselected
6867 // area to the left of the old or new selection.
6868 need_refresh
[0] = true;
6869 rect
[0] = BlockToDeviceRect(
6870 wxGridCellCoords( oldTop
, oldLeft
),
6871 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
6874 if ( oldTop
< topRow
)
6876 // Refresh the newly selected or deselected
6877 // area above the old or new selection.
6878 need_refresh
[1] = true;
6879 rect
[1] = BlockToDeviceRect(
6880 wxGridCellCoords( oldTop
, leftCol
),
6881 wxGridCellCoords( topRow
- 1, rightCol
) );
6884 if ( oldRight
> rightCol
)
6886 // Refresh the newly selected or deselected
6887 // area to the right of the old or new selection.
6888 need_refresh
[2] = true;
6889 rect
[2] = BlockToDeviceRect(
6890 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
6891 wxGridCellCoords( oldBottom
, oldRight
) );
6894 if ( oldBottom
> bottomRow
)
6896 // Refresh the newly selected or deselected
6897 // area below the old or new selection.
6898 need_refresh
[3] = true;
6899 rect
[3] = BlockToDeviceRect(
6900 wxGridCellCoords( bottomRow
+ 1, leftCol
),
6901 wxGridCellCoords( oldBottom
, rightCol
) );
6904 // various Refresh() calls
6905 for (i
= 0; i
< 4; i
++ )
6906 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6907 m_gridWin
->Refresh( false, &(rect
[i
]) );
6911 m_selectingTopLeft
= updateTopLeft
;
6912 m_selectingBottomRight
= updateBottomRight
;
6916 // ------ functions to get/send data (see also public functions)
6919 bool wxGrid::GetModelValues()
6921 // Hide the editor, so it won't hide a changed value.
6922 HideCellEditControl();
6926 // all we need to do is repaint the grid
6928 m_gridWin
->Refresh();
6935 bool wxGrid::SetModelValues()
6939 // Disable the editor, so it won't hide a changed value.
6940 // Do we also want to save the current value of the editor first?
6942 DisableCellEditControl();
6946 for ( row
= 0; row
< m_numRows
; row
++ )
6948 for ( col
= 0; col
< m_numCols
; col
++ )
6950 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6960 // Note - this function only draws cells that are in the list of
6961 // exposed cells (usually set from the update region by
6962 // CalcExposedCells)
6964 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6966 if ( !m_numRows
|| !m_numCols
)
6969 int i
, numCells
= cells
.GetCount();
6970 int row
, col
, cell_rows
, cell_cols
;
6971 wxGridCellCoordsArray redrawCells
;
6973 for ( i
= numCells
- 1; i
>= 0; i
-- )
6975 row
= cells
[i
].GetRow();
6976 col
= cells
[i
].GetCol();
6977 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6979 // If this cell is part of a multicell block, find owner for repaint
6980 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6982 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
6983 bool marked
= false;
6984 for ( int j
= 0; j
< numCells
; j
++ )
6986 if ( cell
== cells
[j
] )
6995 int count
= redrawCells
.GetCount();
6996 for (int j
= 0; j
< count
; j
++)
6998 if ( cell
== redrawCells
[j
] )
7006 redrawCells
.Add( cell
);
7009 // don't bother drawing this cell
7013 // If this cell is empty, find cell to left that might want to overflow
7014 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7016 for ( int l
= 0; l
< cell_rows
; l
++ )
7018 // find a cell in this row to left alreay marked for repaint
7020 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7021 if ((redrawCells
[k
].GetCol() < left
) &&
7022 (redrawCells
[k
].GetRow() == row
))
7024 left
= redrawCells
[k
].GetCol();
7028 left
= 0; // oh well
7030 for (int j
= col
- 1; j
>= left
; j
--)
7032 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7034 if (GetCellOverflow(row
+ l
, j
))
7036 wxGridCellCoords
cell(row
+ l
, j
);
7037 bool marked
= false;
7039 for (int k
= 0; k
< numCells
; k
++)
7041 if ( cell
== cells
[k
] )
7050 int count
= redrawCells
.GetCount();
7051 for (int k
= 0; k
< count
; k
++)
7053 if ( cell
== redrawCells
[k
] )
7060 redrawCells
.Add( cell
);
7069 DrawCell( dc
, cells
[i
] );
7072 numCells
= redrawCells
.GetCount();
7074 for ( i
= numCells
- 1; i
>= 0; i
-- )
7076 DrawCell( dc
, redrawCells
[i
] );
7080 void wxGrid::DrawGridSpace( wxDC
& dc
)
7083 m_gridWin
->GetClientSize( &cw
, &ch
);
7086 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7088 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
7089 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7091 if ( right
> rightCol
|| bottom
> bottomRow
)
7094 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7096 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7097 dc
.SetPen( *wxTRANSPARENT_PEN
);
7099 if ( right
> rightCol
)
7101 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7104 if ( bottom
> bottomRow
)
7106 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7111 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7113 int row
= coords
.GetRow();
7114 int col
= coords
.GetCol();
7116 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7119 // we draw the cell border ourselves
7120 #if !WXGRID_DRAW_LINES
7121 if ( m_gridLinesEnabled
)
7122 DrawCellBorder( dc
, coords
);
7125 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7127 bool isCurrent
= coords
== m_currentCellCoords
;
7129 wxRect rect
= CellToRect( row
, col
);
7131 // if the editor is shown, we should use it and not the renderer
7132 // Note: However, only if it is really _shown_, i.e. not hidden!
7133 if ( isCurrent
&& IsCellEditControlShown() )
7135 // NB: this "#if..." is temporary and fixes a problem where the
7136 // edit control is erased by this code after being rendered.
7137 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7138 // implicitly, causing this out-of order render.
7139 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7140 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7141 editor
->PaintBackground(rect
, attr
);
7147 // but all the rest is drawn by the cell renderer and hence may be customized
7148 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7149 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7156 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7158 int row
= m_currentCellCoords
.GetRow();
7159 int col
= m_currentCellCoords
.GetCol();
7161 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7164 wxRect rect
= CellToRect(row
, col
);
7166 // hmmm... what could we do here to show that the cell is disabled?
7167 // for now, I just draw a thinner border than for the other ones, but
7168 // it doesn't look really good
7170 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7174 // The center of th drawn line is where the position/width/height of
7175 // the rectangle is actually at, (on wxMSW at least,) so we will
7176 // reduce the size of the rectangle to compensate for the thickness of
7177 // the line. If this is too strange on non wxMSW platforms then
7178 // please #ifdef this appropriately.
7179 rect
.x
+= penWidth
/ 2;
7180 rect
.y
+= penWidth
/ 2;
7181 rect
.width
-= penWidth
- 1;
7182 rect
.height
-= penWidth
- 1;
7184 // Now draw the rectangle
7185 // use the cellHighlightColour if the cell is inside a selection, this
7186 // will ensure the cell is always visible.
7187 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7188 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7189 dc
.DrawRectangle(rect
);
7193 // VZ: my experiments with 3D borders...
7195 // how to properly set colours for arbitrary bg?
7196 wxCoord x1
= rect
.x
,
7198 x2
= rect
.x
+ rect
.width
- 1,
7199 y2
= rect
.y
+ rect
.height
- 1;
7201 dc
.SetPen(*wxWHITE_PEN
);
7202 dc
.DrawLine(x1
, y1
, x2
, y1
);
7203 dc
.DrawLine(x1
, y1
, x1
, y2
);
7205 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7206 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7208 dc
.SetPen(*wxBLACK_PEN
);
7209 dc
.DrawLine(x1
, y2
, x2
, y2
);
7210 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7214 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7216 int row
= coords
.GetRow();
7217 int col
= coords
.GetCol();
7218 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7221 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7223 wxRect rect
= CellToRect( row
, col
);
7225 // right hand border
7226 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7227 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7230 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7231 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7234 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7236 // This if block was previously in wxGrid::OnPaint but that doesn't
7237 // seem to get called under wxGTK - MB
7239 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7240 m_numRows
&& m_numCols
)
7242 m_currentCellCoords
.Set(0, 0);
7245 if ( IsCellEditControlShown() )
7247 // don't show highlight when the edit control is shown
7251 // if the active cell was repainted, repaint its highlight too because it
7252 // might have been damaged by the grid lines
7253 size_t count
= cells
.GetCount();
7254 for ( size_t n
= 0; n
< count
; n
++ )
7256 if ( cells
[n
] == m_currentCellCoords
)
7258 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7259 DrawCellHighlight(dc
, attr
);
7267 // TODO: remove this ???
7268 // This is used to redraw all grid lines e.g. when the grid line colour
7271 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7273 #if !WXGRID_DRAW_LINES
7277 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7280 int top
, bottom
, left
, right
;
7282 #if 0 //#ifndef __WXGTK__
7286 m_gridWin
->GetClientSize(&cw
, &ch
);
7288 // virtual coords of visible area
7290 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7291 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7296 reg
.GetBox(x
, y
, w
, h
);
7297 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7298 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7302 m_gridWin
->GetClientSize(&cw
, &ch
);
7303 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7304 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7307 // avoid drawing grid lines past the last row and col
7309 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7310 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7312 // no gridlines inside multicells, clip them out
7313 int leftCol
= internalXToCol(left
);
7314 int topRow
= internalYToRow(top
);
7315 int rightCol
= internalXToCol(right
);
7316 int bottomRow
= internalYToRow(bottom
);
7319 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7320 wxRegion
clippedcells(0, 0, cw
, ch
);
7322 int i
, j
, cell_rows
, cell_cols
;
7325 for (j
=topRow
; j
<bottomRow
; j
++)
7327 for (i
=leftCol
; i
<rightCol
; i
++)
7329 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7330 if ((cell_rows
> 1) || (cell_cols
> 1))
7332 rect
= CellToRect(j
,i
);
7333 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7334 clippedcells
.Subtract(rect
);
7336 else if ((cell_rows
< 0) || (cell_cols
< 0))
7338 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7339 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7340 clippedcells
.Subtract(rect
);
7345 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7347 int i
, j
, cell_rows
, cell_cols
;
7350 for (j
=topRow
; j
<bottomRow
; j
++)
7352 for (i
=leftCol
; i
<rightCol
; i
++)
7354 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7355 if ((cell_rows
> 1) || (cell_cols
> 1))
7357 rect
= CellToRect(j
, i
);
7358 clippedcells
.Subtract(rect
);
7360 else if ((cell_rows
< 0) || (cell_cols
< 0))
7362 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7363 clippedcells
.Subtract(rect
);
7369 dc
.SetClippingRegion( clippedcells
);
7371 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7373 // horizontal grid lines
7375 // already declared above - int i;
7376 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7378 int bot
= GetRowBottom(i
) - 1;
7387 dc
.DrawLine( left
, bot
, right
, bot
);
7391 // vertical grid lines
7393 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7395 int colRight
= GetColRight(i
) - 1;
7396 if ( colRight
> right
)
7401 if ( colRight
>= left
)
7403 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7407 dc
.DestroyClippingRegion();
7410 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7416 size_t numLabels
= rows
.GetCount();
7418 for ( i
= 0; i
< numLabels
; i
++ )
7420 DrawRowLabel( dc
, rows
[i
] );
7424 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7426 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7433 rect
.SetY( GetRowTop(row
) + 1 );
7434 rect
.SetWidth( m_rowLabelWidth
- 2 );
7435 rect
.SetHeight( GetRowHeight(row
) - 2 );
7437 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7439 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7441 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7443 int rowTop
= GetRowTop(row
),
7444 rowBottom
= GetRowBottom(row
) - 1;
7446 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
), 1, wxSOLID
) );
7447 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7448 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7449 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7451 dc
.SetPen( *wxWHITE_PEN
);
7452 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7453 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7456 dc
.SetBackgroundMode( wxTRANSPARENT
);
7457 dc
.SetTextForeground( GetLabelTextColour() );
7458 dc
.SetFont( GetLabelFont() );
7461 GetRowLabelAlignment( &hAlign
, &vAlign
);
7464 rect
.SetY( GetRowTop(row
) + 2 );
7465 rect
.SetWidth( m_rowLabelWidth
- 4 );
7466 rect
.SetHeight( GetRowHeight(row
) - 4 );
7467 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7470 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7476 size_t numLabels
= cols
.GetCount();
7478 for ( i
= 0; i
< numLabels
; i
++ )
7480 DrawColLabel( dc
, cols
[i
] );
7484 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7486 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7489 int colLeft
= GetColLeft(col
);
7494 rect
.SetX( colLeft
+ 1 );
7496 rect
.SetWidth( GetColWidth(col
) - 2 );
7497 rect
.SetHeight( m_colLabelHeight
- 2 );
7499 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7501 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7503 int colRight
= GetColRight(col
) - 1;
7505 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
), 1, wxSOLID
) );
7506 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7507 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7508 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7509 colRight
+ 1, m_colLabelHeight
- 1 );
7511 dc
.SetPen( *wxWHITE_PEN
);
7512 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7513 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7516 dc
.SetBackgroundMode( wxTRANSPARENT
);
7517 dc
.SetTextForeground( GetLabelTextColour() );
7518 dc
.SetFont( GetLabelFont() );
7520 int hAlign
, vAlign
, orient
;
7521 GetColLabelAlignment( &hAlign
, &vAlign
);
7522 orient
= GetColLabelTextOrientation();
7524 rect
.SetX( colLeft
+ 2 );
7526 rect
.SetWidth( GetColWidth(col
) - 4 );
7527 rect
.SetHeight( m_colLabelHeight
- 4 );
7528 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7531 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7532 const wxString
& value
,
7536 int textOrientation
)
7538 wxArrayString lines
;
7540 StringToLines( value
, lines
);
7542 // Forward to new API.
7543 DrawTextRectangle( dc
,
7551 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7552 const wxArrayString
& lines
,
7556 int textOrientation
)
7558 long textWidth
= 0, textHeight
= 0;
7559 long lineWidth
= 0, lineHeight
= 0;
7562 dc
.SetClippingRegion( rect
);
7564 nLines
= lines
.GetCount();
7568 float x
= 0.0, y
= 0.0;
7570 if ( textOrientation
== wxHORIZONTAL
)
7571 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
7573 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7575 switch ( vertAlign
)
7577 case wxALIGN_BOTTOM
:
7578 if ( textOrientation
== wxHORIZONTAL
)
7579 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7581 x
= rect
.x
+ rect
.width
- textWidth
;
7584 case wxALIGN_CENTRE
:
7585 if ( textOrientation
== wxHORIZONTAL
)
7586 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
7588 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
7593 if ( textOrientation
== wxHORIZONTAL
)
7600 // Align each line of a multi-line label
7601 for ( l
= 0; l
< nLines
; l
++ )
7603 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7605 switch ( horizAlign
)
7608 if ( textOrientation
== wxHORIZONTAL
)
7609 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7611 y
= rect
.y
+ lineWidth
+ 1;
7614 case wxALIGN_CENTRE
:
7615 if ( textOrientation
== wxHORIZONTAL
)
7616 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
7618 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
7623 if ( textOrientation
== wxHORIZONTAL
)
7626 y
= rect
.y
+ rect
.height
- 1;
7630 if ( textOrientation
== wxHORIZONTAL
)
7632 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7637 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7643 dc
.DestroyClippingRegion();
7646 // Split multi-line text up into an array of strings.
7647 // Any existing contents of the string array are preserved.
7649 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7653 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7654 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7656 while ( startPos
< (int)tVal
.length() )
7658 pos
= tVal
.Mid(startPos
).Find( eol
);
7663 else if ( pos
== 0 )
7665 lines
.Add( wxEmptyString
);
7669 lines
.Add( value
.Mid(startPos
, pos
) );
7672 startPos
+= pos
+ 1;
7675 if ( startPos
< (int)value
.length() )
7677 lines
.Add( value
.Mid( startPos
) );
7681 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
7682 const wxArrayString
& lines
,
7683 long *width
, long *height
)
7687 long lineW
= 0, lineH
= 0;
7690 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7692 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7693 w
= wxMax( w
, lineW
);
7702 // ------ Batch processing.
7704 void wxGrid::EndBatch()
7706 if ( m_batchCount
> 0 )
7709 if ( !m_batchCount
)
7712 m_rowLabelWin
->Refresh();
7713 m_colLabelWin
->Refresh();
7714 m_cornerLabelWin
->Refresh();
7715 m_gridWin
->Refresh();
7720 // Use this, rather than wxWindow::Refresh(), to force an immediate
7721 // repainting of the grid. Has no effect if you are already inside a
7722 // BeginBatch / EndBatch block.
7724 void wxGrid::ForceRefresh()
7730 bool wxGrid::Enable(bool enable
)
7732 if ( !wxScrolledWindow::Enable(enable
) )
7735 // redraw in the new state
7736 m_gridWin
->Refresh();
7742 // ------ Edit control functions
7745 void wxGrid::EnableEditing( bool edit
)
7747 // TODO: improve this ?
7749 if ( edit
!= m_editable
)
7752 EnableCellEditControl(edit
);
7757 void wxGrid::EnableCellEditControl( bool enable
)
7762 if ( enable
!= m_cellEditCtrlEnabled
)
7766 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7769 // this should be checked by the caller!
7770 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
7772 // do it before ShowCellEditControl()
7773 m_cellEditCtrlEnabled
= enable
;
7775 ShowCellEditControl();
7779 //FIXME:add veto support
7780 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7782 HideCellEditControl();
7783 SaveEditControlValue();
7785 // do it after HideCellEditControl()
7786 m_cellEditCtrlEnabled
= enable
;
7791 bool wxGrid::IsCurrentCellReadOnly() const
7794 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7795 bool readonly
= attr
->IsReadOnly();
7801 bool wxGrid::CanEnableCellControl() const
7803 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7804 !IsCurrentCellReadOnly();
7807 bool wxGrid::IsCellEditControlEnabled() const
7809 // the cell edit control might be disable for all cells or just for the
7810 // current one if it's read only
7811 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7814 bool wxGrid::IsCellEditControlShown() const
7816 bool isShown
= false;
7818 if ( m_cellEditCtrlEnabled
)
7820 int row
= m_currentCellCoords
.GetRow();
7821 int col
= m_currentCellCoords
.GetCol();
7822 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7823 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7828 if ( editor
->IsCreated() )
7830 isShown
= editor
->GetControl()->IsShown();
7840 void wxGrid::ShowCellEditControl()
7842 if ( IsCellEditControlEnabled() )
7844 if ( !IsVisible( m_currentCellCoords
, false ) )
7846 m_cellEditCtrlEnabled
= false;
7851 wxRect rect
= CellToRect( m_currentCellCoords
);
7852 int row
= m_currentCellCoords
.GetRow();
7853 int col
= m_currentCellCoords
.GetCol();
7855 // if this is part of a multicell, find owner (topleft)
7856 int cell_rows
, cell_cols
;
7857 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7858 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7862 m_currentCellCoords
.SetRow( row
);
7863 m_currentCellCoords
.SetCol( col
);
7866 // convert to scrolled coords
7867 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7873 // performed in PaintBackground()
7875 // erase the highlight and the cell contents because the editor
7876 // might not cover the entire cell
7877 wxClientDC
dc( m_gridWin
);
7879 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7880 dc
.SetPen(*wxTRANSPARENT_PEN
);
7881 dc
.DrawRectangle(rect
);
7884 // cell is shifted by one pixel
7885 // However, don't allow x or y to become negative
7886 // since the SetSize() method interprets that as
7893 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7894 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7895 if ( !editor
->IsCreated() )
7897 editor
->Create(m_gridWin
, wxID_ANY
,
7898 new wxGridCellEditorEvtHandler(this, editor
));
7900 wxGridEditorCreatedEvent
evt(GetId(),
7901 wxEVT_GRID_EDITOR_CREATED
,
7905 editor
->GetControl());
7906 GetEventHandler()->ProcessEvent(evt
);
7909 // resize editor to overflow into righthand cells if allowed
7910 int maxWidth
= rect
.width
;
7911 wxString value
= GetCellValue(row
, col
);
7912 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7915 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
7916 if (maxWidth
< rect
.width
)
7917 maxWidth
= rect
.width
;
7920 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7921 if (rect
.x
+ maxWidth
> client_right
)
7922 maxWidth
= client_right
- rect
.x
;
7924 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7926 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7927 // may have changed earlier
7928 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
7931 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7933 // looks weird going over a multicell
7934 if (m_table
->IsEmptyCell( row
, i
) &&
7935 (rect
.width
< maxWidth
) && (c_rows
== 1))
7937 rect
.width
+= GetColWidth( i
);
7943 if (rect
.GetRight() > client_right
)
7944 rect
.SetRight( client_right
- 1 );
7947 editor
->SetCellAttr( attr
);
7948 editor
->SetSize( rect
);
7949 editor
->GetControl()->Move(
7950 editor
->GetControl()->GetPosition().x
+ nXMove
,
7951 editor
->GetControl()->GetPosition().y
);
7952 editor
->Show( true, attr
);
7955 for (int i
= 0; i
< m_currentCellCoords
.GetCol(); i
++)
7957 colXPos
+= GetColSize( i
);
7960 int xUnit
= 1, yUnit
= 1;
7961 GetScrollPixelsPerUnit( &xUnit
, &yUnit
);
7962 if (m_currentCellCoords
.GetCol() != 0)
7963 Scroll( colXPos
/ xUnit
- 1, GetScrollPos( wxVERTICAL
) );
7965 Scroll( colXPos
/ xUnit
, GetScrollPos( wxVERTICAL
) );
7967 // recalc dimensions in case we need to
7968 // expand the scrolled window to account for editor
7971 editor
->BeginEdit(row
, col
, this);
7972 editor
->SetCellAttr(NULL
);
7980 void wxGrid::HideCellEditControl()
7982 if ( IsCellEditControlEnabled() )
7984 int row
= m_currentCellCoords
.GetRow();
7985 int col
= m_currentCellCoords
.GetCol();
7987 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7988 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7989 editor
->Show( false );
7993 m_gridWin
->SetFocus();
7995 // refresh whole row to the right
7996 wxRect
rect( CellToRect(row
, col
) );
7997 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7998 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8001 // ensure that the pixels under the focus ring get refreshed as well
8002 rect
.Inflate(10, 10);
8005 m_gridWin
->Refresh( false, &rect
);
8009 void wxGrid::SaveEditControlValue()
8011 if ( IsCellEditControlEnabled() )
8013 int row
= m_currentCellCoords
.GetRow();
8014 int col
= m_currentCellCoords
.GetCol();
8016 wxString oldval
= GetCellValue(row
, col
);
8018 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8019 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8020 bool changed
= editor
->EndEdit(row
, col
, this);
8027 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8028 m_currentCellCoords
.GetRow(),
8029 m_currentCellCoords
.GetCol() ) < 0 )
8031 // Event has been vetoed, set the data back.
8032 SetCellValue(row
, col
, oldval
);
8039 // ------ Grid location functions
8040 // Note that all of these functions work with the logical coordinates of
8041 // grid cells and labels so you will need to convert from device
8042 // coordinates for mouse events etc.
8045 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
8047 int row
= YToRow(y
);
8048 int col
= XToCol(x
);
8050 if ( row
== -1 || col
== -1 )
8052 coords
= wxGridNoCellCoords
;
8056 coords
.Set( row
, col
);
8060 // Internal Helper function for computing row or column from some
8061 // (unscrolled) coordinate value, using either
8062 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8063 // of m_rowBottoms/m_ColRights to speed up the search!
8065 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8066 const wxArrayInt
& BorderArray
, int nMax
,
8070 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8075 size_t i_max
= coord
/ defaultDist
,
8078 if (BorderArray
.IsEmpty())
8080 if ((int) i_max
< nMax
)
8082 return clipToMinMax
? nMax
- 1 : -1;
8085 if ( i_max
>= BorderArray
.GetCount())
8087 i_max
= BorderArray
.GetCount() - 1;
8091 if ( coord
>= BorderArray
[i_max
])
8095 i_max
= coord
/ minDist
;
8097 i_max
= BorderArray
.GetCount() - 1;
8100 if ( i_max
>= BorderArray
.GetCount())
8101 i_max
= BorderArray
.GetCount() - 1;
8104 if ( coord
>= BorderArray
[i_max
])
8105 return clipToMinMax
? (int)i_max
: -1;
8106 if ( coord
< BorderArray
[0] )
8109 while ( i_max
- i_min
> 0 )
8111 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8112 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8113 if (coord
>= BorderArray
[ i_max
- 1])
8117 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8118 if (coord
< BorderArray
[median
])
8127 int wxGrid::YToRow( int y
)
8129 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8130 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8133 int wxGrid::XToCol( int x
)
8135 return CoordToRowOrCol(x
, m_defaultColWidth
,
8136 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8139 // return the row number that that the y coord is near the edge of, or
8140 // -1 if not near an edge
8142 int wxGrid::YToEdgeOfRow( int y
)
8145 i
= internalYToRow(y
);
8147 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8149 // We know that we are in row i, test whether we are
8150 // close enough to lower or upper border, respectively.
8151 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8153 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8160 // return the col number that that the x coord is near the edge of, or
8161 // -1 if not near an edge
8163 int wxGrid::XToEdgeOfCol( int x
)
8166 i
= internalXToCol(x
);
8168 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8170 // We know that we are in column i; test whether we are
8171 // close enough to right or left border, respectively.
8172 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8174 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8181 wxRect
wxGrid::CellToRect( int row
, int col
)
8183 wxRect
rect( -1, -1, -1, -1 );
8185 if ( row
>= 0 && row
< m_numRows
&&
8186 col
>= 0 && col
< m_numCols
)
8188 int i
, cell_rows
, cell_cols
;
8189 rect
.width
= rect
.height
= 0;
8190 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8191 // if negative then find multicell owner
8196 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8198 rect
.x
= GetColLeft(col
);
8199 rect
.y
= GetRowTop(row
);
8200 for (i
=col
; i
< col
+ cell_cols
; i
++)
8201 rect
.width
+= GetColWidth(i
);
8202 for (i
=row
; i
< row
+ cell_rows
; i
++)
8203 rect
.height
+= GetRowHeight(i
);
8206 // if grid lines are enabled, then the area of the cell is a bit smaller
8207 if (m_gridLinesEnabled
)
8216 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8218 // get the cell rectangle in logical coords
8220 wxRect
r( CellToRect( row
, col
) );
8222 // convert to device coords
8224 int left
, top
, right
, bottom
;
8225 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8226 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8228 // check against the client area of the grid window
8230 m_gridWin
->GetClientSize( &cw
, &ch
);
8232 if ( wholeCellVisible
)
8234 // is the cell wholly visible ?
8235 return ( left
>= 0 && right
<= cw
&&
8236 top
>= 0 && bottom
<= ch
);
8240 // is the cell partly visible ?
8242 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8243 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8247 // make the specified cell location visible by doing a minimal amount
8250 void wxGrid::MakeCellVisible( int row
, int col
)
8253 int xpos
= -1, ypos
= -1;
8255 if ( row
>= 0 && row
< m_numRows
&&
8256 col
>= 0 && col
< m_numCols
)
8258 // get the cell rectangle in logical coords
8259 wxRect
r( CellToRect( row
, col
) );
8261 // convert to device coords
8262 int left
, top
, right
, bottom
;
8263 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8264 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8267 m_gridWin
->GetClientSize( &cw
, &ch
);
8273 else if ( bottom
> ch
)
8275 int h
= r
.GetHeight();
8277 for ( i
= row
- 1; i
>= 0; i
-- )
8279 int rowHeight
= GetRowHeight(i
);
8280 if ( h
+ rowHeight
> ch
)
8287 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8288 // have rounding errors (this is important, because if we do, we
8289 // might not scroll at all and some cells won't be redrawn)
8291 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8293 ypos
+= m_scrollLineY
;
8296 // special handling for wide cells - show always left part of the cell!
8297 // Otherwise, e.g. when stepping from row to row, it would jump between
8298 // left and right part of the cell on every step!
8300 if ( left
< 0 || (right
- left
) >= cw
)
8304 else if ( right
> cw
)
8306 // position the view so that the cell is on the right
8308 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8309 xpos
= x0
+ (right
- cw
);
8311 // see comment for ypos above
8312 xpos
+= m_scrollLineX
;
8315 if ( xpos
!= -1 || ypos
!= -1 )
8318 xpos
/= m_scrollLineX
;
8320 ypos
/= m_scrollLineY
;
8321 Scroll( xpos
, ypos
);
8328 // ------ Grid cursor movement functions
8331 bool wxGrid::MoveCursorUp( bool expandSelection
)
8333 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8334 m_currentCellCoords
.GetRow() >= 0 )
8336 if ( expandSelection
)
8338 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8339 m_selectingKeyboard
= m_currentCellCoords
;
8340 if ( m_selectingKeyboard
.GetRow() > 0 )
8342 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8343 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8344 m_selectingKeyboard
.GetCol() );
8345 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8348 else if ( m_currentCellCoords
.GetRow() > 0 )
8350 int row
= m_currentCellCoords
.GetRow() - 1;
8351 int col
= m_currentCellCoords
.GetCol();
8353 MakeCellVisible( row
, col
);
8354 SetCurrentCell( row
, col
);
8365 bool wxGrid::MoveCursorDown( bool expandSelection
)
8367 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8368 m_currentCellCoords
.GetRow() < m_numRows
)
8370 if ( expandSelection
)
8372 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8373 m_selectingKeyboard
= m_currentCellCoords
;
8374 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8376 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8377 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8378 m_selectingKeyboard
.GetCol() );
8379 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8382 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8384 int row
= m_currentCellCoords
.GetRow() + 1;
8385 int col
= m_currentCellCoords
.GetCol();
8387 MakeCellVisible( row
, col
);
8388 SetCurrentCell( row
, col
);
8399 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8401 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8402 m_currentCellCoords
.GetCol() >= 0 )
8404 if ( expandSelection
)
8406 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8407 m_selectingKeyboard
= m_currentCellCoords
;
8408 if ( m_selectingKeyboard
.GetCol() > 0 )
8410 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8411 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8412 m_selectingKeyboard
.GetCol() );
8413 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8416 else if ( m_currentCellCoords
.GetCol() > 0 )
8418 int row
= m_currentCellCoords
.GetRow();
8419 int col
= m_currentCellCoords
.GetCol() - 1;
8421 MakeCellVisible( row
, col
);
8422 SetCurrentCell( row
, col
);
8433 bool wxGrid::MoveCursorRight( bool expandSelection
)
8435 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8436 m_currentCellCoords
.GetCol() < m_numCols
)
8438 if ( expandSelection
)
8440 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8441 m_selectingKeyboard
= m_currentCellCoords
;
8442 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8444 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8445 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8446 m_selectingKeyboard
.GetCol() );
8447 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8450 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8452 int row
= m_currentCellCoords
.GetRow();
8453 int col
= m_currentCellCoords
.GetCol() + 1;
8455 MakeCellVisible( row
, col
);
8456 SetCurrentCell( row
, col
);
8467 bool wxGrid::MovePageUp()
8469 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8472 int row
= m_currentCellCoords
.GetRow();
8476 m_gridWin
->GetClientSize( &cw
, &ch
);
8478 int y
= GetRowTop(row
);
8479 int newRow
= internalYToRow( y
- ch
+ 1 );
8481 if ( newRow
== row
)
8483 //row > 0 , so newrow can never be less than 0 here.
8487 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8488 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8496 bool wxGrid::MovePageDown()
8498 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8501 int row
= m_currentCellCoords
.GetRow();
8502 if ( (row
+ 1) < m_numRows
)
8505 m_gridWin
->GetClientSize( &cw
, &ch
);
8507 int y
= GetRowTop(row
);
8508 int newRow
= internalYToRow( y
+ ch
);
8509 if ( newRow
== row
)
8511 // row < m_numRows , so newrow can't overflow here.
8515 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8516 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8524 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8527 m_currentCellCoords
!= wxGridNoCellCoords
&&
8528 m_currentCellCoords
.GetRow() > 0 )
8530 int row
= m_currentCellCoords
.GetRow();
8531 int col
= m_currentCellCoords
.GetCol();
8533 if ( m_table
->IsEmptyCell(row
, col
) )
8535 // starting in an empty cell: find the next block of
8541 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8545 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
8547 // starting at the top of a block: find the next block
8553 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8559 // starting within a block: find the top of the block
8564 if ( m_table
->IsEmptyCell(row
, col
) )
8572 MakeCellVisible( row
, col
);
8573 if ( expandSelection
)
8575 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8576 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8581 SetCurrentCell( row
, col
);
8590 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8593 m_currentCellCoords
!= wxGridNoCellCoords
&&
8594 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8596 int row
= m_currentCellCoords
.GetRow();
8597 int col
= m_currentCellCoords
.GetCol();
8599 if ( m_table
->IsEmptyCell(row
, col
) )
8601 // starting in an empty cell: find the next block of
8604 while ( row
< m_numRows
- 1 )
8607 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8611 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
8613 // starting at the bottom of a block: find the next block
8616 while ( row
< m_numRows
- 1 )
8619 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8625 // starting within a block: find the bottom of the block
8627 while ( row
< m_numRows
- 1 )
8630 if ( m_table
->IsEmptyCell(row
, col
) )
8638 MakeCellVisible( row
, col
);
8639 if ( expandSelection
)
8641 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8642 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8647 SetCurrentCell( row
, col
);
8656 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8659 m_currentCellCoords
!= wxGridNoCellCoords
&&
8660 m_currentCellCoords
.GetCol() > 0 )
8662 int row
= m_currentCellCoords
.GetRow();
8663 int col
= m_currentCellCoords
.GetCol();
8665 if ( m_table
->IsEmptyCell(row
, col
) )
8667 // starting in an empty cell: find the next block of
8673 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8677 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
8679 // starting at the left of a block: find the next block
8685 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8691 // starting within a block: find the left of the block
8696 if ( m_table
->IsEmptyCell(row
, col
) )
8704 MakeCellVisible( row
, col
);
8705 if ( expandSelection
)
8707 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8708 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8713 SetCurrentCell( row
, col
);
8722 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8725 m_currentCellCoords
!= wxGridNoCellCoords
&&
8726 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8728 int row
= m_currentCellCoords
.GetRow();
8729 int col
= m_currentCellCoords
.GetCol();
8731 if ( m_table
->IsEmptyCell(row
, col
) )
8733 // starting in an empty cell: find the next block of
8736 while ( col
< m_numCols
- 1 )
8739 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8743 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
8745 // starting at the right of a block: find the next block
8748 while ( col
< m_numCols
- 1 )
8751 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8757 // starting within a block: find the right of the block
8759 while ( col
< m_numCols
- 1 )
8762 if ( m_table
->IsEmptyCell(row
, col
) )
8770 MakeCellVisible( row
, col
);
8771 if ( expandSelection
)
8773 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8774 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8779 SetCurrentCell( row
, col
);
8789 // ------ Label values and formatting
8792 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8795 *horiz
= m_rowLabelHorizAlign
;
8797 *vert
= m_rowLabelVertAlign
;
8800 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8803 *horiz
= m_colLabelHorizAlign
;
8805 *vert
= m_colLabelVertAlign
;
8808 int wxGrid::GetColLabelTextOrientation()
8810 return m_colLabelTextOrientation
;
8813 wxString
wxGrid::GetRowLabelValue( int row
)
8817 return m_table
->GetRowLabelValue( row
);
8827 wxString
wxGrid::GetColLabelValue( int col
)
8831 return m_table
->GetColLabelValue( col
);
8841 void wxGrid::SetRowLabelSize( int width
)
8843 width
= wxMax( width
, 0 );
8844 if ( width
!= m_rowLabelWidth
)
8848 m_rowLabelWin
->Show( false );
8849 m_cornerLabelWin
->Show( false );
8851 else if ( m_rowLabelWidth
== 0 )
8853 m_rowLabelWin
->Show( true );
8854 if ( m_colLabelHeight
> 0 )
8855 m_cornerLabelWin
->Show( true );
8858 m_rowLabelWidth
= width
;
8860 wxScrolledWindow::Refresh( true );
8864 void wxGrid::SetColLabelSize( int height
)
8866 height
= wxMax( height
, 0 );
8867 if ( height
!= m_colLabelHeight
)
8871 m_colLabelWin
->Show( false );
8872 m_cornerLabelWin
->Show( false );
8874 else if ( m_colLabelHeight
== 0 )
8876 m_colLabelWin
->Show( true );
8877 if ( m_rowLabelWidth
> 0 )
8878 m_cornerLabelWin
->Show( true );
8881 m_colLabelHeight
= height
;
8883 wxScrolledWindow::Refresh( true );
8887 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8889 if ( m_labelBackgroundColour
!= colour
)
8891 m_labelBackgroundColour
= colour
;
8892 m_rowLabelWin
->SetBackgroundColour( colour
);
8893 m_colLabelWin
->SetBackgroundColour( colour
);
8894 m_cornerLabelWin
->SetBackgroundColour( colour
);
8896 if ( !GetBatchCount() )
8898 m_rowLabelWin
->Refresh();
8899 m_colLabelWin
->Refresh();
8900 m_cornerLabelWin
->Refresh();
8905 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8907 if ( m_labelTextColour
!= colour
)
8909 m_labelTextColour
= colour
;
8910 if ( !GetBatchCount() )
8912 m_rowLabelWin
->Refresh();
8913 m_colLabelWin
->Refresh();
8918 void wxGrid::SetLabelFont( const wxFont
& font
)
8921 if ( !GetBatchCount() )
8923 m_rowLabelWin
->Refresh();
8924 m_colLabelWin
->Refresh();
8928 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8930 // allow old (incorrect) defs to be used
8933 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8934 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8935 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8940 case wxTOP
: vert
= wxALIGN_TOP
; break;
8941 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8942 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8945 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8947 m_rowLabelHorizAlign
= horiz
;
8950 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8952 m_rowLabelVertAlign
= vert
;
8955 if ( !GetBatchCount() )
8957 m_rowLabelWin
->Refresh();
8961 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8963 // allow old (incorrect) defs to be used
8966 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8967 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8968 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8973 case wxTOP
: vert
= wxALIGN_TOP
; break;
8974 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8975 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8978 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8980 m_colLabelHorizAlign
= horiz
;
8983 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8985 m_colLabelVertAlign
= vert
;
8988 if ( !GetBatchCount() )
8990 m_colLabelWin
->Refresh();
8994 // Note: under MSW, the default column label font must be changed because it
8995 // does not support vertical printing
8997 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8998 // pGrid->SetLabelFont(font);
8999 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9001 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9003 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9004 m_colLabelTextOrientation
= textOrientation
;
9006 if ( !GetBatchCount() )
9007 m_colLabelWin
->Refresh();
9010 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9014 m_table
->SetRowLabelValue( row
, s
);
9015 if ( !GetBatchCount() )
9017 wxRect rect
= CellToRect( row
, 0 );
9018 if ( rect
.height
> 0 )
9020 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9022 rect
.width
= m_rowLabelWidth
;
9023 m_rowLabelWin
->Refresh( true, &rect
);
9029 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9033 m_table
->SetColLabelValue( col
, s
);
9034 if ( !GetBatchCount() )
9036 wxRect rect
= CellToRect( 0, col
);
9037 if ( rect
.width
> 0 )
9039 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9041 rect
.height
= m_colLabelHeight
;
9042 m_colLabelWin
->Refresh( true, &rect
);
9048 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9050 if ( m_gridLineColour
!= colour
)
9052 m_gridLineColour
= colour
;
9054 wxClientDC
dc( m_gridWin
);
9056 DrawAllGridLines( dc
, wxRegion() );
9060 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9062 if ( m_cellHighlightColour
!= colour
)
9064 m_cellHighlightColour
= colour
;
9066 wxClientDC
dc( m_gridWin
);
9068 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9069 DrawCellHighlight(dc
, attr
);
9074 void wxGrid::SetCellHighlightPenWidth(int width
)
9076 if (m_cellHighlightPenWidth
!= width
)
9078 m_cellHighlightPenWidth
= width
;
9080 // Just redrawing the cell highlight is not enough since that won't
9081 // make any visible change if the the thickness is getting smaller.
9082 int row
= m_currentCellCoords
.GetRow();
9083 int col
= m_currentCellCoords
.GetCol();
9084 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9087 wxRect rect
= CellToRect(row
, col
);
9088 m_gridWin
->Refresh(true, &rect
);
9092 void wxGrid::SetCellHighlightROPenWidth(int width
)
9094 if (m_cellHighlightROPenWidth
!= width
)
9096 m_cellHighlightROPenWidth
= width
;
9098 // Just redrawing the cell highlight is not enough since that won't
9099 // make any visible change if the the thickness is getting smaller.
9100 int row
= m_currentCellCoords
.GetRow();
9101 int col
= m_currentCellCoords
.GetCol();
9102 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9105 wxRect rect
= CellToRect(row
, col
);
9106 m_gridWin
->Refresh(true, &rect
);
9110 void wxGrid::EnableGridLines( bool enable
)
9112 if ( enable
!= m_gridLinesEnabled
)
9114 m_gridLinesEnabled
= enable
;
9116 if ( !GetBatchCount() )
9120 wxClientDC
dc( m_gridWin
);
9122 DrawAllGridLines( dc
, wxRegion() );
9126 m_gridWin
->Refresh();
9132 int wxGrid::GetDefaultRowSize()
9134 return m_defaultRowHeight
;
9137 int wxGrid::GetRowSize( int row
)
9139 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9141 return GetRowHeight(row
);
9144 int wxGrid::GetDefaultColSize()
9146 return m_defaultColWidth
;
9149 int wxGrid::GetColSize( int col
)
9151 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9153 return GetColWidth(col
);
9156 // ============================================================================
9157 // access to the grid attributes: each of them has a default value in the grid
9158 // itself and may be overidden on a per-cell basis
9159 // ============================================================================
9161 // ----------------------------------------------------------------------------
9162 // setting default attributes
9163 // ----------------------------------------------------------------------------
9165 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9167 m_defaultCellAttr
->SetBackgroundColour(col
);
9169 m_gridWin
->SetBackgroundColour(col
);
9173 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9175 m_defaultCellAttr
->SetTextColour(col
);
9178 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9180 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9183 void wxGrid::SetDefaultCellOverflow( bool allow
)
9185 m_defaultCellAttr
->SetOverflow(allow
);
9188 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9190 m_defaultCellAttr
->SetFont(font
);
9193 // For editors and renderers the type registry takes precedence over the
9194 // default attr, so we need to register the new editor/renderer for the string
9195 // data type in order to make setting a default editor/renderer appear to
9198 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9200 RegisterDataType(wxGRID_VALUE_STRING
,
9202 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9205 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9207 RegisterDataType(wxGRID_VALUE_STRING
,
9208 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9212 // ----------------------------------------------------------------------------
9213 // access to the default attrbiutes
9214 // ----------------------------------------------------------------------------
9216 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9218 return m_defaultCellAttr
->GetBackgroundColour();
9221 wxColour
wxGrid::GetDefaultCellTextColour()
9223 return m_defaultCellAttr
->GetTextColour();
9226 wxFont
wxGrid::GetDefaultCellFont()
9228 return m_defaultCellAttr
->GetFont();
9231 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9233 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9236 bool wxGrid::GetDefaultCellOverflow()
9238 return m_defaultCellAttr
->GetOverflow();
9241 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9243 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9246 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9248 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9251 // ----------------------------------------------------------------------------
9252 // access to cell attributes
9253 // ----------------------------------------------------------------------------
9255 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9257 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9258 wxColour colour
= attr
->GetBackgroundColour();
9264 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9266 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9267 wxColour colour
= attr
->GetTextColour();
9273 wxFont
wxGrid::GetCellFont( int row
, int col
)
9275 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9276 wxFont font
= attr
->GetFont();
9282 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9284 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9285 attr
->GetAlignment(horiz
, vert
);
9289 bool wxGrid::GetCellOverflow( int row
, int col
)
9291 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9292 bool allow
= attr
->GetOverflow();
9298 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9300 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9301 attr
->GetSize( num_rows
, num_cols
);
9305 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9307 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9308 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9314 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9316 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9317 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9323 bool wxGrid::IsReadOnly(int row
, int col
) const
9325 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9326 bool isReadOnly
= attr
->IsReadOnly();
9332 // ----------------------------------------------------------------------------
9333 // attribute support: cache, automatic provider creation, ...
9334 // ----------------------------------------------------------------------------
9336 bool wxGrid::CanHaveAttributes()
9343 return m_table
->CanHaveAttributes();
9346 void wxGrid::ClearAttrCache()
9348 if ( m_attrCache
.row
!= -1 )
9350 wxSafeDecRef(m_attrCache
.attr
);
9351 m_attrCache
.attr
= NULL
;
9352 m_attrCache
.row
= -1;
9356 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9360 wxGrid
*self
= (wxGrid
*)this; // const_cast
9362 self
->ClearAttrCache();
9363 self
->m_attrCache
.row
= row
;
9364 self
->m_attrCache
.col
= col
;
9365 self
->m_attrCache
.attr
= attr
;
9370 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9372 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9374 *attr
= m_attrCache
.attr
;
9375 wxSafeIncRef(m_attrCache
.attr
);
9377 #ifdef DEBUG_ATTR_CACHE
9378 gs_nAttrCacheHits
++;
9385 #ifdef DEBUG_ATTR_CACHE
9386 gs_nAttrCacheMisses
++;
9393 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9395 wxGridCellAttr
*attr
= NULL
;
9396 // Additional test to avoid looking at the cache e.g. for
9397 // wxNoCellCoords, as this will confuse memory management.
9400 if ( !LookupAttr(row
, col
, &attr
) )
9402 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9403 : (wxGridCellAttr
*)NULL
;
9404 CacheAttr(row
, col
, attr
);
9410 attr
->SetDefAttr(m_defaultCellAttr
);
9414 attr
= m_defaultCellAttr
;
9421 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9423 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9424 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9426 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9427 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9429 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9432 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9434 // artificially inc the ref count to match DecRef() in caller
9436 m_table
->SetAttr(attr
, row
, col
);
9442 // ----------------------------------------------------------------------------
9443 // setting column attributes (wrappers around SetColAttr)
9444 // ----------------------------------------------------------------------------
9446 void wxGrid::SetColFormatBool(int col
)
9448 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9451 void wxGrid::SetColFormatNumber(int col
)
9453 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9456 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9458 wxString typeName
= wxGRID_VALUE_FLOAT
;
9459 if ( (width
!= -1) || (precision
!= -1) )
9461 typeName
<< _T(':') << width
<< _T(',') << precision
;
9464 SetColFormatCustom(col
, typeName
);
9467 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9469 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9471 attr
= new wxGridCellAttr
;
9472 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9473 attr
->SetRenderer(renderer
);
9475 SetColAttr(col
, attr
);
9479 // ----------------------------------------------------------------------------
9480 // setting cell attributes: this is forwarded to the table
9481 // ----------------------------------------------------------------------------
9483 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9485 if ( CanHaveAttributes() )
9487 m_table
->SetAttr(attr
, row
, col
);
9496 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9498 if ( CanHaveAttributes() )
9500 m_table
->SetRowAttr(attr
, row
);
9509 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9511 if ( CanHaveAttributes() )
9513 m_table
->SetColAttr(attr
, col
);
9522 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9524 if ( CanHaveAttributes() )
9526 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9527 attr
->SetBackgroundColour(colour
);
9532 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9534 if ( CanHaveAttributes() )
9536 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9537 attr
->SetTextColour(colour
);
9542 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9544 if ( CanHaveAttributes() )
9546 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9547 attr
->SetFont(font
);
9552 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9554 if ( CanHaveAttributes() )
9556 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9557 attr
->SetAlignment(horiz
, vert
);
9562 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9564 if ( CanHaveAttributes() )
9566 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9567 attr
->SetOverflow(allow
);
9572 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9574 if ( CanHaveAttributes() )
9576 int cell_rows
, cell_cols
;
9578 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9579 attr
->GetSize(&cell_rows
, &cell_cols
);
9580 attr
->SetSize(num_rows
, num_cols
);
9583 // Cannot set the size of a cell to 0 or negative values
9584 // While it is perfectly legal to do that, this function cannot
9585 // handle all the possibilies, do it by hand by getting the CellAttr.
9586 // You can only set the size of a cell to 1,1 or greater with this fn
9587 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9588 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9589 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9590 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9592 // if this was already a multicell then "turn off" the other cells first
9593 if ((cell_rows
> 1) || (cell_rows
> 1))
9596 for (j
=row
; j
< row
+ cell_rows
; j
++)
9598 for (i
=col
; i
< col
+ cell_cols
; i
++)
9600 if ((i
!= col
) || (j
!= row
))
9602 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9603 attr_stub
->SetSize( 1, 1 );
9604 attr_stub
->DecRef();
9610 // mark the cells that will be covered by this cell to
9611 // negative or zero values to point back at this cell
9612 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9615 for (j
=row
; j
< row
+ num_rows
; j
++)
9617 for (i
=col
; i
< col
+ num_cols
; i
++)
9619 if ((i
!= col
) || (j
!= row
))
9621 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9622 attr_stub
->SetSize( row
- j
, col
- i
);
9623 attr_stub
->DecRef();
9631 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9633 if ( CanHaveAttributes() )
9635 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9636 attr
->SetRenderer(renderer
);
9641 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9643 if ( CanHaveAttributes() )
9645 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9646 attr
->SetEditor(editor
);
9651 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9653 if ( CanHaveAttributes() )
9655 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9656 attr
->SetReadOnly(isReadOnly
);
9661 // ----------------------------------------------------------------------------
9662 // Data type registration
9663 // ----------------------------------------------------------------------------
9665 void wxGrid::RegisterDataType(const wxString
& typeName
,
9666 wxGridCellRenderer
* renderer
,
9667 wxGridCellEditor
* editor
)
9669 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9673 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9675 wxString typeName
= m_table
->GetTypeName(row
, col
);
9676 return GetDefaultEditorForType(typeName
);
9679 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9681 wxString typeName
= m_table
->GetTypeName(row
, col
);
9682 return GetDefaultRendererForType(typeName
);
9685 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9687 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9688 if ( index
== wxNOT_FOUND
)
9692 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9693 wxFAIL_MSG(errStr
.c_str());
9698 return m_typeRegistry
->GetEditor(index
);
9701 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9703 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9704 if ( index
== wxNOT_FOUND
)
9708 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9709 wxFAIL_MSG(errStr
.c_str());
9714 return m_typeRegistry
->GetRenderer(index
);
9717 // ----------------------------------------------------------------------------
9719 // ----------------------------------------------------------------------------
9721 void wxGrid::EnableDragRowSize( bool enable
)
9723 m_canDragRowSize
= enable
;
9726 void wxGrid::EnableDragColSize( bool enable
)
9728 m_canDragColSize
= enable
;
9731 void wxGrid::EnableDragGridSize( bool enable
)
9733 m_canDragGridSize
= enable
;
9736 void wxGrid::EnableDragCell( bool enable
)
9738 m_canDragCell
= enable
;
9741 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9743 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9745 if ( resizeExistingRows
)
9747 // since we are resizing all rows to the default row size,
9748 // we can simply clear the row heights and row bottoms
9749 // arrays (which also allows us to take advantage of
9750 // some speed optimisations)
9751 m_rowHeights
.Empty();
9752 m_rowBottoms
.Empty();
9753 if ( !GetBatchCount() )
9758 void wxGrid::SetRowSize( int row
, int height
)
9760 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9762 // See comment in SetColSize
9763 if ( height
< GetRowMinimalAcceptableHeight())
9766 if ( m_rowHeights
.IsEmpty() )
9768 // need to really create the array
9772 int h
= wxMax( 0, height
);
9773 int diff
= h
- m_rowHeights
[row
];
9775 m_rowHeights
[row
] = h
;
9777 for ( i
= row
; i
< m_numRows
; i
++ )
9779 m_rowBottoms
[i
] += diff
;
9782 if ( !GetBatchCount() )
9786 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9788 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9790 if ( resizeExistingCols
)
9792 // since we are resizing all columns to the default column size,
9793 // we can simply clear the col widths and col rights
9794 // arrays (which also allows us to take advantage of
9795 // some speed optimisations)
9796 m_colWidths
.Empty();
9797 m_colRights
.Empty();
9798 if ( !GetBatchCount() )
9803 void wxGrid::SetColSize( int col
, int width
)
9805 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9807 // should we check that it's bigger than GetColMinimalWidth(col) here?
9809 // No, because it is reasonable to assume the library user know's
9810 // what he is doing. However whe should test against the weaker
9811 // constraint of minimalAcceptableWidth, as this breaks rendering
9813 // This test then fixes sf.net bug #645734
9815 if ( width
< GetColMinimalAcceptableWidth() )
9818 if ( m_colWidths
.IsEmpty() )
9820 // need to really create the array
9824 // if < 0 calc new width from label
9828 wxArrayString lines
;
9829 wxClientDC
dc(m_colLabelWin
);
9830 dc
.SetFont(GetLabelFont());
9831 StringToLines(GetColLabelValue(col
), lines
);
9832 GetTextBoxSize(dc
, lines
, &w
, &h
);
9836 int w
= wxMax( 0, width
);
9837 int diff
= w
- m_colWidths
[col
];
9838 m_colWidths
[col
] = w
;
9841 for ( i
= col
; i
< m_numCols
; i
++ )
9843 m_colRights
[i
] += diff
;
9846 if ( !GetBatchCount() )
9850 void wxGrid::SetColMinimalWidth( int col
, int width
)
9852 if (width
> GetColMinimalAcceptableWidth())
9854 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9855 m_colMinWidths
[key
] = width
;
9859 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9861 if (width
> GetRowMinimalAcceptableHeight())
9863 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9864 m_rowMinHeights
[key
] = width
;
9868 int wxGrid::GetColMinimalWidth(int col
) const
9870 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9871 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9873 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9876 int wxGrid::GetRowMinimalHeight(int row
) const
9878 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9879 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9881 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9884 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9886 // We do allow a width of 0 since this gives us
9887 // an easy way to temporarily hiding columns.
9889 m_minAcceptableColWidth
= width
;
9892 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9894 // We do allow a height of 0 since this gives us
9895 // an easy way to temporarily hiding rows.
9897 m_minAcceptableRowHeight
= height
;
9900 int wxGrid::GetColMinimalAcceptableWidth() const
9902 return m_minAcceptableColWidth
;
9905 int wxGrid::GetRowMinimalAcceptableHeight() const
9907 return m_minAcceptableRowHeight
;
9910 // ----------------------------------------------------------------------------
9912 // ----------------------------------------------------------------------------
9914 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9916 wxClientDC
dc(m_gridWin
);
9918 // cancel editing of cell
9919 HideCellEditControl();
9920 SaveEditControlValue();
9922 // init both of them to avoid compiler warnings, even if we only need one
9930 wxCoord extent
, extentMax
= 0;
9931 int max
= column
? m_numRows
: m_numCols
;
9932 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9939 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9940 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
9943 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9944 extent
= column
? size
.x
: size
.y
;
9945 if ( extent
> extentMax
)
9956 // now also compare with the column label extent
9958 dc
.SetFont( GetLabelFont() );
9962 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9963 if ( GetColLabelTextOrientation() == wxVERTICAL
)
9967 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9969 extent
= column
? w
: h
;
9970 if ( extent
> extentMax
)
9977 // empty column - give default extent (notice that if extentMax is less
9978 // than default extent but != 0, it's OK)
9979 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9984 // leave some space around text
9992 SetColSize( col
, extentMax
);
9993 if ( !GetBatchCount() )
9996 m_gridWin
->GetClientSize( &cw
, &ch
);
9997 wxRect
rect ( CellToRect( 0, col
) );
9999 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10000 rect
.width
= cw
- rect
.x
;
10001 rect
.height
= m_colLabelHeight
;
10002 m_colLabelWin
->Refresh( true, &rect
);
10007 SetRowSize(row
, extentMax
);
10008 if ( !GetBatchCount() )
10011 m_gridWin
->GetClientSize( &cw
, &ch
);
10012 wxRect
rect( CellToRect( row
, 0 ) );
10014 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10015 rect
.width
= m_rowLabelWidth
;
10016 rect
.height
= ch
- rect
.y
;
10017 m_rowLabelWin
->Refresh( true, &rect
);
10024 SetColMinimalWidth(col
, extentMax
);
10026 SetRowMinimalHeight(row
, extentMax
);
10030 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10032 int width
= m_rowLabelWidth
;
10037 for ( int col
= 0; col
< m_numCols
; col
++ )
10041 AutoSizeColumn(col
, setAsMin
);
10044 width
+= GetColWidth(col
);
10053 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10055 int height
= m_colLabelHeight
;
10060 for ( int row
= 0; row
< m_numRows
; row
++ )
10063 AutoSizeRow(row
, setAsMin
);
10065 height
+= GetRowHeight(row
);
10074 void wxGrid::AutoSize()
10078 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
10080 // round up the size to a multiple of scroll step - this ensures that we
10081 // won't get the scrollbars if we're sized exactly to this width
10082 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10085 GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
10086 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10088 // distribute the extra space between the columns/rows to avoid having
10089 // extra white space
10091 // Remove the extra m_extraWidth + 1 added above
10092 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10093 if ( diff
&& m_numCols
)
10095 // try to resize the columns uniformly
10096 wxCoord diffPerCol
= diff
/ m_numCols
;
10099 for ( int col
= 0; col
< m_numCols
; col
++ )
10101 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10105 // add remaining amount to the last columns
10106 diff
-= diffPerCol
* m_numCols
;
10109 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10111 SetColSize(col
, GetColWidth(col
) + 1);
10117 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10118 if ( diff
&& m_numRows
)
10120 // try to resize the columns uniformly
10121 wxCoord diffPerRow
= diff
/ m_numRows
;
10124 for ( int row
= 0; row
< m_numRows
; row
++ )
10126 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10130 // add remaining amount to the last rows
10131 diff
-= diffPerRow
* m_numRows
;
10134 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10136 SetRowSize(row
, GetRowHeight(row
) + 1);
10143 SetClientSize(sizeFit
);
10146 void wxGrid::AutoSizeRowLabelSize( int row
)
10148 wxArrayString lines
;
10151 // Hide the edit control, so it
10152 // won't interfere with drag-shrinking.
10153 if ( IsCellEditControlShown() )
10155 HideCellEditControl();
10156 SaveEditControlValue();
10159 // autosize row height depending on label text
10160 StringToLines( GetRowLabelValue( row
), lines
);
10161 wxClientDC
dc( m_rowLabelWin
);
10162 GetTextBoxSize( dc
, lines
, &w
, &h
);
10163 if ( h
< m_defaultRowHeight
)
10164 h
= m_defaultRowHeight
;
10165 SetRowSize(row
, h
);
10169 void wxGrid::AutoSizeColLabelSize( int col
)
10171 wxArrayString lines
;
10174 // Hide the edit control, so it
10175 // won't interfer with drag-shrinking.
10176 if ( IsCellEditControlShown() )
10178 HideCellEditControl();
10179 SaveEditControlValue();
10182 // autosize column width depending on label text
10183 StringToLines( GetColLabelValue( col
), lines
);
10184 wxClientDC
dc( m_colLabelWin
);
10185 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10186 GetTextBoxSize( dc
, lines
, &w
, &h
);
10188 GetTextBoxSize( dc
, lines
, &h
, &w
);
10189 if ( w
< m_defaultColWidth
)
10190 w
= m_defaultColWidth
;
10191 SetColSize(col
, w
);
10195 wxSize
wxGrid::DoGetBestSize() const
10197 // don't set sizes, only calculate them
10198 wxGrid
*self
= (wxGrid
*)this; // const_cast
10201 width
= self
->SetOrCalcColumnSizes(true);
10202 height
= self
->SetOrCalcRowSizes(true);
10209 // Round up to a multiple the scroll rate
10210 // NOTE: this still doesn't get rid of the scrollbars;
10211 // is there any magic incantation for that?
10213 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10215 width
+= 1 + xpu
- (width
% xpu
);
10217 height
+= 1 + ypu
- (height
% ypu
);
10219 // limit to 1/4 of the screen size
10220 int maxwidth
, maxheight
;
10221 wxDisplaySize( &maxwidth
, &maxheight
);
10224 if ( width
> maxwidth
)
10226 if ( height
> maxheight
)
10227 height
= maxheight
;
10229 wxSize
best(width
, height
);
10231 // NOTE: This size should be cached, but first we need to add calls to
10232 // InvalidateBestSize everywhere that could change the results of this
10234 // CacheBestSize(size);
10244 wxPen
& wxGrid::GetDividerPen() const
10249 // ----------------------------------------------------------------------------
10250 // cell value accessor functions
10251 // ----------------------------------------------------------------------------
10253 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10257 m_table
->SetValue( row
, col
, s
);
10258 if ( !GetBatchCount() )
10261 wxRect
rect( CellToRect( row
, col
) );
10263 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10264 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10265 m_gridWin
->Refresh( false, &rect
);
10268 if ( m_currentCellCoords
.GetRow() == row
&&
10269 m_currentCellCoords
.GetCol() == col
&&
10270 IsCellEditControlShown())
10271 // Note: If we are using IsCellEditControlEnabled,
10272 // this interacts badly with calling SetCellValue from
10273 // an EVT_GRID_CELL_CHANGE handler.
10275 HideCellEditControl();
10276 ShowCellEditControl(); // will reread data from table
10281 // ----------------------------------------------------------------------------
10282 // block, row and column selection
10283 // ----------------------------------------------------------------------------
10285 void wxGrid::SelectRow( int row
, bool addToSelected
)
10287 if ( IsSelection() && !addToSelected
)
10291 m_selection
->SelectRow( row
, false, addToSelected
);
10294 void wxGrid::SelectCol( int col
, bool addToSelected
)
10296 if ( IsSelection() && !addToSelected
)
10300 m_selection
->SelectCol( col
, false, addToSelected
);
10303 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10304 bool addToSelected
)
10306 if ( IsSelection() && !addToSelected
)
10310 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10311 false, addToSelected
);
10314 void wxGrid::SelectAll()
10316 if ( m_numRows
> 0 && m_numCols
> 0 )
10319 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10323 // ----------------------------------------------------------------------------
10324 // cell, row and col deselection
10325 // ----------------------------------------------------------------------------
10327 void wxGrid::DeselectRow( int row
)
10329 if ( !m_selection
)
10332 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10334 if ( m_selection
->IsInSelection(row
, 0 ) )
10335 m_selection
->ToggleCellSelection(row
, 0);
10339 int nCols
= GetNumberCols();
10340 for ( int i
= 0; i
< nCols
; i
++ )
10342 if ( m_selection
->IsInSelection(row
, i
) )
10343 m_selection
->ToggleCellSelection(row
, i
);
10348 void wxGrid::DeselectCol( int col
)
10350 if ( !m_selection
)
10353 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10355 if ( m_selection
->IsInSelection(0, col
) )
10356 m_selection
->ToggleCellSelection(0, col
);
10360 int nRows
= GetNumberRows();
10361 for ( int i
= 0; i
< nRows
; i
++ )
10363 if ( m_selection
->IsInSelection(i
, col
) )
10364 m_selection
->ToggleCellSelection(i
, col
);
10369 void wxGrid::DeselectCell( int row
, int col
)
10371 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10372 m_selection
->ToggleCellSelection(row
, col
);
10375 bool wxGrid::IsSelection()
10377 return ( m_selection
&& (m_selection
->IsSelection() ||
10378 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10379 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10382 bool wxGrid::IsInSelection( int row
, int col
) const
10384 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10385 ( row
>= m_selectingTopLeft
.GetRow() &&
10386 col
>= m_selectingTopLeft
.GetCol() &&
10387 row
<= m_selectingBottomRight
.GetRow() &&
10388 col
<= m_selectingBottomRight
.GetCol() )) );
10391 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10395 wxGridCellCoordsArray a
;
10399 return m_selection
->m_cellSelection
;
10402 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10406 wxGridCellCoordsArray a
;
10410 return m_selection
->m_blockSelectionTopLeft
;
10413 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10417 wxGridCellCoordsArray a
;
10421 return m_selection
->m_blockSelectionBottomRight
;
10424 wxArrayInt
wxGrid::GetSelectedRows() const
10432 return m_selection
->m_rowSelection
;
10435 wxArrayInt
wxGrid::GetSelectedCols() const
10443 return m_selection
->m_colSelection
;
10446 void wxGrid::ClearSelection()
10448 m_selectingTopLeft
= wxGridNoCellCoords
;
10449 m_selectingBottomRight
= wxGridNoCellCoords
;
10451 m_selection
->ClearSelection();
10454 // This function returns the rectangle that encloses the given block
10455 // in device coords clipped to the client size of the grid window.
10457 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10458 const wxGridCellCoords
&bottomRight
)
10460 wxRect
rect( wxGridNoCellRect
);
10463 cellRect
= CellToRect( topLeft
);
10464 if ( cellRect
!= wxGridNoCellRect
)
10470 rect
= wxRect(0, 0, 0, 0);
10473 cellRect
= CellToRect( bottomRight
);
10474 if ( cellRect
!= wxGridNoCellRect
)
10480 return wxGridNoCellRect
;
10484 int left
= rect
.GetLeft();
10485 int top
= rect
.GetTop();
10486 int right
= rect
.GetRight();
10487 int bottom
= rect
.GetBottom();
10489 int leftCol
= topLeft
.GetCol();
10490 int topRow
= topLeft
.GetRow();
10491 int rightCol
= bottomRight
.GetCol();
10492 int bottomRow
= bottomRight
.GetRow();
10500 leftCol
= rightCol
;
10510 topRow
= bottomRow
;
10514 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10516 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10518 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10520 cellRect
= CellToRect( j
, i
);
10522 if (cellRect
.x
< left
)
10524 if (cellRect
.y
< top
)
10526 if (cellRect
.x
+ cellRect
.width
> right
)
10527 right
= cellRect
.x
+ cellRect
.width
;
10528 if (cellRect
.y
+ cellRect
.height
> bottom
)
10529 bottom
= cellRect
.y
+ cellRect
.height
;
10533 i
= rightCol
; // jump over inner cells.
10538 // convert to scrolled coords
10540 CalcScrolledPosition( left
, top
, &left
, &top
);
10541 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10544 m_gridWin
->GetClientSize( &cw
, &ch
);
10546 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10547 return wxRect(0,0,0,0);
10549 rect
.SetLeft( wxMax(0, left
) );
10550 rect
.SetTop( wxMax(0, top
) );
10551 rect
.SetRight( wxMin(cw
, right
) );
10552 rect
.SetBottom( wxMin(ch
, bottom
) );
10557 // ----------------------------------------------------------------------------
10558 // grid event classes
10559 // ----------------------------------------------------------------------------
10561 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10563 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10564 int row
, int col
, int x
, int y
, bool sel
,
10565 bool control
, bool shift
, bool alt
, bool meta
)
10566 : wxNotifyEvent( type
, id
)
10573 m_control
= control
;
10578 SetEventObject(obj
);
10582 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10584 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10585 int rowOrCol
, int x
, int y
,
10586 bool control
, bool shift
, bool alt
, bool meta
)
10587 : wxNotifyEvent( type
, id
)
10589 m_rowOrCol
= rowOrCol
;
10592 m_control
= control
;
10597 SetEventObject(obj
);
10601 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10603 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10604 const wxGridCellCoords
& topLeft
,
10605 const wxGridCellCoords
& bottomRight
,
10606 bool sel
, bool control
,
10607 bool shift
, bool alt
, bool meta
)
10608 : wxNotifyEvent( type
, id
)
10610 m_topLeft
= topLeft
;
10611 m_bottomRight
= bottomRight
;
10613 m_control
= control
;
10618 SetEventObject(obj
);
10622 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10624 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10625 wxObject
* obj
, int row
,
10626 int col
, wxControl
* ctrl
)
10627 : wxCommandEvent(type
, id
)
10629 SetEventObject(obj
);
10635 #endif // wxUSE_GRID