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
192 m_rowLabelWin
= NULL
;
193 m_colLabelWin
= 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 it's a F-Key or other special key then it shouldn't start the
576 if (event
.GetKeyCode() >= WXK_START
)
580 // if the unicode key code is not really a unicode character (it may
581 // be a function key or etc., the platforms appear to always give us a
582 // small value in this case) then fallback to the ASCII key code but
583 // don't do anything for function keys or etc.
584 key
= event
.GetUnicodeKey();
587 key
= event
.GetKeyCode();
588 keyOk
= (key
<= 127);
591 key
= event
.GetKeyCode();
592 keyOk
= (key
<= 255);
598 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
603 void wxGridCellEditor::StartingClick()
609 // ----------------------------------------------------------------------------
610 // wxGridCellTextEditor
611 // ----------------------------------------------------------------------------
613 wxGridCellTextEditor::wxGridCellTextEditor()
618 void wxGridCellTextEditor::Create(wxWindow
* parent
,
620 wxEvtHandler
* evtHandler
)
622 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
623 wxDefaultPosition
, wxDefaultSize
624 #if defined(__WXMSW__)
625 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
629 // set max length allowed in the textctrl, if the parameter was set
632 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
635 wxGridCellEditor::Create(parent
, id
, evtHandler
);
638 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
639 wxGridCellAttr
* WXUNUSED(attr
))
641 // as we fill the entire client area,
642 // don't do anything here to minimize flicker
645 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
647 wxRect
rect(rectOrig
);
649 // Make the edit control large enough to allow for internal margins
651 // TODO: remove this if the text ctrl sizing is improved esp. for unix
653 #if defined(__WXGTK__)
662 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
664 // MB: treat MSW separately here otherwise the caret doesn't show
665 // when the editor is in the first row.
666 #if defined(__WXMSW__)
669 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
672 #if defined(__WXMOTIF__)
677 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
678 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
679 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
680 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
683 wxGridCellEditor::SetSize(rect
);
686 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
688 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
690 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
692 DoBeginEdit(m_startValue
);
695 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
697 Text()->SetValue(startValue
);
698 Text()->SetInsertionPointEnd();
699 Text()->SetSelection(-1, -1);
703 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
705 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
707 bool changed
= false;
708 wxString value
= Text()->GetValue();
709 if (value
!= m_startValue
)
713 grid
->GetTable()->SetValue(row
, col
, value
);
715 m_startValue
= wxEmptyString
;
717 // No point in setting the text of the hidden control
718 //Text()->SetValue(m_startValue);
723 void wxGridCellTextEditor::Reset()
725 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
727 DoReset(m_startValue
);
730 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
732 Text()->SetValue(startValue
);
733 Text()->SetInsertionPointEnd();
736 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
738 return wxGridCellEditor::IsAcceptedKey(event
);
741 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
743 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
744 // longer an appropriate way to get the character into the text control.
745 // Do it ourselves instead. We know that if we get this far that we have
746 // a valid character, so not a whole lot of testing needs to be done.
748 wxTextCtrl
* tc
= Text();
753 ch
= event
.GetUnicodeKey();
755 ch
= (wxChar
)event
.GetKeyCode();
757 ch
= (wxChar
)event
.GetKeyCode();
763 // delete the character at the cursor
764 pos
= tc
->GetInsertionPoint();
765 if (pos
< tc
->GetLastPosition())
766 tc
->Remove(pos
, pos
+ 1);
770 // delete the character before the cursor
771 pos
= tc
->GetInsertionPoint();
773 tc
->Remove(pos
- 1, pos
);
782 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
783 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
785 #if defined(__WXMOTIF__) || defined(__WXGTK__)
786 // wxMotif needs a little extra help...
787 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
788 wxString
s( Text()->GetValue() );
789 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
791 Text()->SetInsertionPoint( pos
);
793 // the other ports can handle a Return key press
799 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
809 if ( params
.ToLong(&tmp
) )
811 m_maxChars
= (size_t)tmp
;
815 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
820 // return the value in the text control
821 wxString
wxGridCellTextEditor::GetValue() const
823 return Text()->GetValue();
826 // ----------------------------------------------------------------------------
827 // wxGridCellNumberEditor
828 // ----------------------------------------------------------------------------
830 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
836 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
838 wxEvtHandler
* evtHandler
)
843 // create a spin ctrl
844 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
845 wxDefaultPosition
, wxDefaultSize
,
849 wxGridCellEditor::Create(parent
, id
, evtHandler
);
854 // just a text control
855 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
858 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
863 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
865 // first get the value
866 wxGridTableBase
*table
= grid
->GetTable();
867 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
869 m_valueOld
= table
->GetValueAsLong(row
, col
);
874 wxString sValue
= table
->GetValue(row
, col
);
875 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
877 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
885 Spin()->SetValue((int)m_valueOld
);
891 DoBeginEdit(GetString());
895 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
905 value
= Spin()->GetValue();
906 changed
= value
!= m_valueOld
;
908 text
= wxString::Format(wxT("%ld"), value
);
913 text
= Text()->GetValue();
914 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
919 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
920 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
922 grid
->GetTable()->SetValue(row
, col
, text
);
928 void wxGridCellNumberEditor::Reset()
933 Spin()->SetValue((int)m_valueOld
);
938 DoReset(GetString());
942 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
944 if ( wxGridCellEditor::IsAcceptedKey(event
) )
946 int keycode
= event
.GetKeyCode();
947 if ( (keycode
< 128) &&
948 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
957 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
959 int keycode
= event
.GetKeyCode();
962 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
964 wxGridCellTextEditor::StartingKey(event
);
973 if ( wxIsdigit(keycode
) )
975 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
976 spin
->SetValue(keycode
- '0');
977 spin
->SetSelection(1,1);
986 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
997 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1001 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1005 // skip the error message below
1010 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1014 // return the value in the spin control if it is there (the text control otherwise)
1015 wxString
wxGridCellNumberEditor::GetValue() const
1022 long value
= Spin()->GetValue();
1023 s
.Printf(wxT("%ld"), value
);
1028 s
= Text()->GetValue();
1034 // ----------------------------------------------------------------------------
1035 // wxGridCellFloatEditor
1036 // ----------------------------------------------------------------------------
1038 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1041 m_precision
= precision
;
1044 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1046 wxEvtHandler
* evtHandler
)
1048 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1050 #if wxUSE_VALIDATORS
1051 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1055 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1057 // first get the value
1058 wxGridTableBase
*table
= grid
->GetTable();
1059 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1061 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1066 wxString sValue
= table
->GetValue(row
, col
);
1067 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1069 wxFAIL_MSG( _T("this cell doesn't have float value") );
1074 DoBeginEdit(GetString());
1077 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1081 wxString
text(Text()->GetValue());
1083 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1084 !wxIsSameDouble(value
, m_valueOld
) )
1086 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1087 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1089 grid
->GetTable()->SetValue(row
, col
, text
);
1097 void wxGridCellFloatEditor::Reset()
1099 DoReset(GetString());
1102 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1104 int keycode
= event
.GetKeyCode();
1106 tmpbuf
[0] = (char) keycode
;
1108 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1111 bool is_decimal_point
= ( strbuf
==
1112 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1114 bool is_decimal_point
= ( strbuf
== _T(".") );
1117 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1118 || is_decimal_point
)
1120 wxGridCellTextEditor::StartingKey(event
);
1122 // skip Skip() below
1129 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1140 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1144 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1146 m_precision
= (int)tmp
;
1148 // skip the error message below
1153 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1157 wxString
wxGridCellFloatEditor::GetString() const
1160 if ( m_precision
== -1 && m_width
!= -1)
1162 // default precision
1163 fmt
.Printf(_T("%%%d.f"), m_width
);
1165 else if ( m_precision
!= -1 && m_width
== -1)
1168 fmt
.Printf(_T("%%.%df"), m_precision
);
1170 else if ( m_precision
!= -1 && m_width
!= -1 )
1172 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1176 // default width/precision
1180 return wxString::Format(fmt
, m_valueOld
);
1183 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1185 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1187 int keycode
= event
.GetKeyCode();
1188 printf("%d\n", keycode
);
1189 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1191 tmpbuf
[0] = (char) keycode
;
1193 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1196 bool is_decimal_point
=
1197 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1198 wxLOCALE_CAT_NUMBER
) );
1200 bool is_decimal_point
= ( strbuf
== _T(".") );
1203 if ( (keycode
< 128) &&
1204 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1205 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1214 #endif // wxUSE_TEXTCTRL
1218 // ----------------------------------------------------------------------------
1219 // wxGridCellBoolEditor
1220 // ----------------------------------------------------------------------------
1222 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1224 wxEvtHandler
* evtHandler
)
1226 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1227 wxDefaultPosition
, wxDefaultSize
,
1230 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1233 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1235 bool resize
= false;
1236 wxSize size
= m_control
->GetSize();
1237 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1239 // check if the checkbox is not too big/small for this cell
1240 wxSize sizeBest
= m_control
->GetBestSize();
1241 if ( !(size
== sizeBest
) )
1243 // reset to default size if it had been made smaller
1249 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1251 // leave 1 pixel margin
1252 size
.x
= size
.y
= minSize
- 2;
1259 m_control
->SetSize(size
);
1262 // position it in the centre of the rectangle (TODO: support alignment?)
1264 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1265 // the checkbox without label still has some space to the right in wxGTK,
1266 // so shift it to the right
1268 #elif defined(__WXMSW__)
1269 // here too, but in other way
1274 int hAlign
= wxALIGN_CENTRE
;
1275 int vAlign
= wxALIGN_CENTRE
;
1277 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1280 if (hAlign
== wxALIGN_LEFT
)
1288 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1290 else if (hAlign
== wxALIGN_RIGHT
)
1292 x
= r
.x
+ r
.width
- size
.x
- 2;
1293 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1295 else if (hAlign
== wxALIGN_CENTRE
)
1297 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1298 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1301 m_control
->Move(x
, y
);
1304 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1306 m_control
->Show(show
);
1310 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1311 CBox()->SetBackgroundColour(colBg
);
1315 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1317 wxASSERT_MSG(m_control
,
1318 wxT("The wxGridCellEditor must be created first!"));
1320 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1322 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1326 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1327 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1330 CBox()->SetValue(m_startValue
);
1334 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1337 wxASSERT_MSG(m_control
,
1338 wxT("The wxGridCellEditor must be created first!"));
1340 bool changed
= false;
1341 bool value
= CBox()->GetValue();
1342 if ( value
!= m_startValue
)
1347 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1348 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1350 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1356 void wxGridCellBoolEditor::Reset()
1358 wxASSERT_MSG(m_control
,
1359 wxT("The wxGridCellEditor must be created first!"));
1361 CBox()->SetValue(m_startValue
);
1364 void wxGridCellBoolEditor::StartingClick()
1366 CBox()->SetValue(!CBox()->GetValue());
1369 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1371 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1373 int keycode
= event
.GetKeyCode();
1386 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1388 int keycode
= event
.GetKeyCode();
1392 CBox()->SetValue(!CBox()->GetValue());
1396 CBox()->SetValue(true);
1400 CBox()->SetValue(false);
1406 // return the value as "1" for true and the empty string for false
1407 wxString
wxGridCellBoolEditor::GetValue() const
1409 bool bSet
= CBox()->GetValue();
1410 return bSet
? _T("1") : wxEmptyString
;
1413 #endif // wxUSE_CHECKBOX
1417 // ----------------------------------------------------------------------------
1418 // wxGridCellChoiceEditor
1419 // ----------------------------------------------------------------------------
1421 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1423 : m_choices(choices
),
1424 m_allowOthers(allowOthers
) { }
1426 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1427 const wxString choices
[],
1429 : m_allowOthers(allowOthers
)
1433 m_choices
.Alloc(count
);
1434 for ( size_t n
= 0; n
< count
; n
++ )
1436 m_choices
.Add(choices
[n
]);
1441 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1443 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1444 editor
->m_allowOthers
= m_allowOthers
;
1445 editor
->m_choices
= m_choices
;
1450 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1452 wxEvtHandler
* evtHandler
)
1454 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1455 wxDefaultPosition
, wxDefaultSize
,
1457 m_allowOthers
? 0 : wxCB_READONLY
);
1459 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1462 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1463 wxGridCellAttr
* attr
)
1465 // as we fill the entire client area, don't do anything here to minimize
1468 // TODO: It doesn't actually fill the client area since the height of a
1469 // combo always defaults to the standard. Until someone has time to
1470 // figure out the right rectangle to paint, just do it the normal way.
1471 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1474 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1476 wxASSERT_MSG(m_control
,
1477 wxT("The wxGridCellEditor must be created first!"));
1479 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1481 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1483 // Don't immediately end if we get a kill focus event within BeginEdit
1485 evtHandler
->SetInSetFocus(true);
1487 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1491 Combo()->SetValue(m_startValue
);
1495 // find the right position, or default to the first if not found
1496 int pos
= Combo()->FindString(m_startValue
);
1497 if (pos
== wxNOT_FOUND
)
1499 Combo()->SetSelection(pos
);
1502 Combo()->SetInsertionPointEnd();
1503 Combo()->SetFocus();
1507 // When dropping down the menu, a kill focus event
1508 // happens after this point, so we can't reset the flag yet.
1509 #if !defined(__WXGTK20__)
1510 evtHandler
->SetInSetFocus(false);
1515 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1518 wxString value
= Combo()->GetValue();
1519 if ( value
== m_startValue
)
1522 grid
->GetTable()->SetValue(row
, col
, value
);
1527 void wxGridCellChoiceEditor::Reset()
1529 Combo()->SetValue(m_startValue
);
1530 Combo()->SetInsertionPointEnd();
1533 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1543 wxStringTokenizer
tk(params
, _T(','));
1544 while ( tk
.HasMoreTokens() )
1546 m_choices
.Add(tk
.GetNextToken());
1550 // return the value in the text control
1551 wxString
wxGridCellChoiceEditor::GetValue() const
1553 return Combo()->GetValue();
1556 #endif // wxUSE_COMBOBOX
1558 // ----------------------------------------------------------------------------
1559 // wxGridCellEditorEvtHandler
1560 // ----------------------------------------------------------------------------
1562 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1564 // Don't disable the cell if we're just starting to edit it
1569 m_grid
->DisableCellEditControl();
1574 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1576 switch ( event
.GetKeyCode() )
1580 m_grid
->DisableCellEditControl();
1584 m_grid
->GetEventHandler()->ProcessEvent( event
);
1588 case WXK_NUMPAD_ENTER
:
1589 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1590 m_editor
->HandleReturn(event
);
1599 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1601 int row
= m_grid
->GetGridCursorRow();
1602 int col
= m_grid
->GetGridCursorCol();
1603 wxRect rect
= m_grid
->CellToRect( row
, col
);
1605 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1607 // if cell width is smaller than grid client area, cell is wholly visible
1608 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1610 switch ( event
.GetKeyCode() )
1615 case WXK_NUMPAD_ENTER
:
1620 if ( wholeCellVisible
)
1622 // no special processing needed...
1627 // do special processing for partly visible cell...
1629 // get the widths of all cells previous to this one
1631 for ( int i
= 0; i
< col
; i
++ )
1633 colXPos
+= m_grid
->GetColSize(i
);
1636 int xUnit
= 1, yUnit
= 1;
1637 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1640 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1644 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1652 if ( wholeCellVisible
)
1654 // no special processing needed...
1659 // do special processing for partly visible cell...
1662 wxString value
= m_grid
->GetCellValue(row
, col
);
1663 if ( wxEmptyString
!= value
)
1665 // get width of cell CONTENTS (text)
1667 wxFont font
= m_grid
->GetCellFont(row
, col
);
1668 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1670 // try to RIGHT align the text by scrolling
1671 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1673 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1674 // otherwise the last part of the cell content might be hidden below the scroll bar
1675 // FIXME: maybe there is a more suitable correction?
1676 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1677 if ( textWidth
< 0 )
1683 // get the widths of all cells previous to this one
1685 for ( int i
= 0; i
< col
; i
++ )
1687 colXPos
+= m_grid
->GetColSize(i
);
1690 // and add the (modified) text width of the cell contents
1691 // as we'd like to see the last part of the cell contents
1692 colXPos
+= textWidth
;
1694 int xUnit
= 1, yUnit
= 1;
1695 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1696 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1707 // ----------------------------------------------------------------------------
1708 // wxGridCellWorker is an (almost) empty common base class for
1709 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1710 // ----------------------------------------------------------------------------
1712 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1717 wxGridCellWorker::~wxGridCellWorker()
1721 // ============================================================================
1723 // ============================================================================
1725 // ----------------------------------------------------------------------------
1726 // wxGridCellRenderer
1727 // ----------------------------------------------------------------------------
1729 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1730 wxGridCellAttr
& attr
,
1733 int WXUNUSED(row
), int WXUNUSED(col
),
1736 dc
.SetBackgroundMode( wxSOLID
);
1738 // grey out fields if the grid is disabled
1739 if ( grid
.IsEnabled() )
1743 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1747 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1752 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1755 dc
.SetPen( *wxTRANSPARENT_PEN
);
1756 dc
.DrawRectangle(rect
);
1759 // ----------------------------------------------------------------------------
1760 // wxGridCellStringRenderer
1761 // ----------------------------------------------------------------------------
1763 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1764 const wxGridCellAttr
& attr
,
1768 dc
.SetBackgroundMode( wxTRANSPARENT
);
1770 // TODO some special colours for attr.IsReadOnly() case?
1772 // different coloured text when the grid is disabled
1773 if ( grid
.IsEnabled() )
1777 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1778 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1782 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1783 dc
.SetTextForeground( attr
.GetTextColour() );
1788 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1789 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1792 dc
.SetFont( attr
.GetFont() );
1795 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1797 const wxString
& text
)
1799 wxCoord x
= 0, y
= 0, max_x
= 0;
1800 dc
.SetFont(attr
.GetFont());
1801 wxStringTokenizer
tk(text
, _T('\n'));
1802 while ( tk
.HasMoreTokens() )
1804 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1805 max_x
= wxMax(max_x
, x
);
1808 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1810 return wxSize(max_x
, y
);
1813 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1814 wxGridCellAttr
& attr
,
1818 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1821 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1822 wxGridCellAttr
& attr
,
1824 const wxRect
& rectCell
,
1828 wxRect rect
= rectCell
;
1831 // erase only this cells background, overflow cells should have been erased
1832 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1835 attr
.GetAlignment(&hAlign
, &vAlign
);
1837 int overflowCols
= 0;
1839 if (attr
.GetOverflow())
1841 int cols
= grid
.GetNumberCols();
1842 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1843 int cell_rows
, cell_cols
;
1844 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1845 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1847 int i
, c_cols
, c_rows
;
1848 for (i
= col
+cell_cols
; i
< cols
; i
++)
1850 bool is_empty
= true;
1851 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1853 // check w/ anchor cell for multicell block
1854 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1857 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1866 rect
.width
+= grid
.GetColSize(i
);
1874 if (rect
.width
>= best_width
)
1878 overflowCols
= i
- col
- cell_cols
+ 1;
1879 if (overflowCols
>= cols
)
1880 overflowCols
= cols
- 1;
1883 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1885 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1887 clip
.x
+= rectCell
.width
;
1888 // draw each overflow cell individually
1889 int col_end
= col
+ cell_cols
+ overflowCols
;
1890 if (col_end
>= grid
.GetNumberCols())
1891 col_end
= grid
.GetNumberCols() - 1;
1892 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1894 clip
.width
= grid
.GetColSize(i
) - 1;
1895 dc
.DestroyClippingRegion();
1896 dc
.SetClippingRegion(clip
);
1898 SetTextColoursAndFont(grid
, attr
, dc
,
1899 grid
.IsInSelection(row
,i
));
1901 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1902 rect
, hAlign
, vAlign
);
1903 clip
.x
+= grid
.GetColSize(i
) - 1;
1909 dc
.DestroyClippingRegion();
1913 // now we only have to draw the text
1914 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1916 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1917 rect
, hAlign
, vAlign
);
1920 // ----------------------------------------------------------------------------
1921 // wxGridCellNumberRenderer
1922 // ----------------------------------------------------------------------------
1924 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1926 wxGridTableBase
*table
= grid
.GetTable();
1928 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1930 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1934 text
= table
->GetValue(row
, col
);
1940 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1941 wxGridCellAttr
& attr
,
1943 const wxRect
& rectCell
,
1947 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1949 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1951 // draw the text right aligned by default
1953 attr
.GetAlignment(&hAlign
, &vAlign
);
1954 hAlign
= wxALIGN_RIGHT
;
1956 wxRect rect
= rectCell
;
1959 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1962 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1963 wxGridCellAttr
& attr
,
1967 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1970 // ----------------------------------------------------------------------------
1971 // wxGridCellFloatRenderer
1972 // ----------------------------------------------------------------------------
1974 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1977 SetPrecision(precision
);
1980 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1982 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1983 renderer
->m_width
= m_width
;
1984 renderer
->m_precision
= m_precision
;
1985 renderer
->m_format
= m_format
;
1990 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1992 wxGridTableBase
*table
= grid
.GetTable();
1997 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1999 val
= table
->GetValueAsDouble(row
, col
);
2004 text
= table
->GetValue(row
, col
);
2005 hasDouble
= text
.ToDouble(&val
);
2012 if ( m_width
== -1 )
2014 if ( m_precision
== -1 )
2016 // default width/precision
2017 m_format
= _T("%f");
2021 m_format
.Printf(_T("%%.%df"), m_precision
);
2024 else if ( m_precision
== -1 )
2026 // default precision
2027 m_format
.Printf(_T("%%%d.f"), m_width
);
2031 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2035 text
.Printf(m_format
, val
);
2038 //else: text already contains the string
2043 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2044 wxGridCellAttr
& attr
,
2046 const wxRect
& rectCell
,
2050 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2052 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2054 // draw the text right aligned by default
2056 attr
.GetAlignment(&hAlign
, &vAlign
);
2057 hAlign
= wxALIGN_RIGHT
;
2059 wxRect rect
= rectCell
;
2062 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2065 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2066 wxGridCellAttr
& attr
,
2070 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2073 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2077 // reset to defaults
2083 wxString tmp
= params
.BeforeFirst(_T(','));
2087 if ( tmp
.ToLong(&width
) )
2089 SetWidth((int)width
);
2093 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2097 tmp
= params
.AfterFirst(_T(','));
2101 if ( tmp
.ToLong(&precision
) )
2103 SetPrecision((int)precision
);
2107 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2113 // ----------------------------------------------------------------------------
2114 // wxGridCellBoolRenderer
2115 // ----------------------------------------------------------------------------
2117 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2119 // FIXME these checkbox size calculations are really ugly...
2121 // between checkmark and box
2122 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2124 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2125 wxGridCellAttr
& WXUNUSED(attr
),
2130 // compute it only once (no locks for MT safeness in GUI thread...)
2131 if ( !ms_sizeCheckMark
.x
)
2133 // get checkbox size
2134 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2135 wxSize size
= checkbox
->GetBestSize();
2136 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2138 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2139 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2140 checkSize
-= size
.y
/ 2;
2145 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2148 return ms_sizeCheckMark
;
2151 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2152 wxGridCellAttr
& attr
,
2158 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2160 // draw a check mark in the centre (ignoring alignment - TODO)
2161 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2163 // don't draw outside the cell
2164 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2165 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2167 // and even leave (at least) 1 pixel margin
2168 size
.x
= size
.y
= minSize
- 2;
2171 // draw a border around checkmark
2173 attr
.GetAlignment(&hAlign
, &vAlign
);
2176 if (hAlign
== wxALIGN_CENTRE
)
2178 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2179 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2180 rectBorder
.width
= size
.x
;
2181 rectBorder
.height
= size
.y
;
2183 else if (hAlign
== wxALIGN_LEFT
)
2185 rectBorder
.x
= rect
.x
+ 2;
2186 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2187 rectBorder
.width
= size
.x
;
2188 rectBorder
.height
= size
.y
;
2190 else if (hAlign
== wxALIGN_RIGHT
)
2192 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2193 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2194 rectBorder
.width
= size
.x
;
2195 rectBorder
.height
= size
.y
;
2199 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2201 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2205 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2206 value
= !( !cellval
|| (cellval
== wxT("0")) );
2211 wxRect rectMark
= rectBorder
;
2214 // MSW DrawCheckMark() is weird (and should probably be changed...)
2215 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2219 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2222 dc
.SetTextForeground(attr
.GetTextColour());
2223 dc
.DrawCheckMark(rectMark
);
2226 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2227 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2228 dc
.DrawRectangle(rectBorder
);
2231 // ----------------------------------------------------------------------------
2233 // ----------------------------------------------------------------------------
2235 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2239 m_isReadOnly
= Unset
;
2244 m_attrkind
= wxGridCellAttr::Cell
;
2246 m_sizeRows
= m_sizeCols
= 1;
2247 m_overflow
= UnsetOverflow
;
2249 SetDefAttr(attrDefault
);
2252 wxGridCellAttr
*wxGridCellAttr::Clone() const
2254 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2256 if ( HasTextColour() )
2257 attr
->SetTextColour(GetTextColour());
2258 if ( HasBackgroundColour() )
2259 attr
->SetBackgroundColour(GetBackgroundColour());
2261 attr
->SetFont(GetFont());
2262 if ( HasAlignment() )
2263 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2265 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2269 attr
->SetRenderer(m_renderer
);
2270 m_renderer
->IncRef();
2274 attr
->SetEditor(m_editor
);
2279 attr
->SetReadOnly();
2281 attr
->SetKind( m_attrkind
);
2286 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2288 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2289 SetTextColour(mergefrom
->GetTextColour());
2290 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2291 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2292 if ( !HasFont() && mergefrom
->HasFont() )
2293 SetFont(mergefrom
->GetFont());
2294 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2297 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2298 SetAlignment(hAlign
, vAlign
);
2300 if ( !HasSize() && mergefrom
->HasSize() )
2301 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2303 // Directly access member functions as GetRender/Editor don't just return
2304 // m_renderer/m_editor
2306 // Maybe add support for merge of Render and Editor?
2307 if (!HasRenderer() && mergefrom
->HasRenderer() )
2309 m_renderer
= mergefrom
->m_renderer
;
2310 m_renderer
->IncRef();
2312 if ( !HasEditor() && mergefrom
->HasEditor() )
2314 m_editor
= mergefrom
->m_editor
;
2317 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2318 SetReadOnly(mergefrom
->IsReadOnly());
2320 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2321 SetOverflow(mergefrom
->GetOverflow());
2323 SetDefAttr(mergefrom
->m_defGridAttr
);
2326 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2328 // The size of a cell is normally 1,1
2330 // If this cell is larger (2,2) then this is the top left cell
2331 // the other cells that will be covered (lower right cells) must be
2332 // set to negative or zero values such that
2333 // row + num_rows of the covered cell points to the larger cell (this cell)
2334 // same goes for the col + num_cols.
2336 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2338 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2339 !((num_rows
<= 0) && (num_cols
> 0)) ||
2340 !((num_rows
== 0) && (num_cols
== 0))),
2341 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2343 m_sizeRows
= num_rows
;
2344 m_sizeCols
= num_cols
;
2347 const wxColour
& wxGridCellAttr::GetTextColour() const
2349 if (HasTextColour())
2353 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2355 return m_defGridAttr
->GetTextColour();
2359 wxFAIL_MSG(wxT("Missing default cell attribute"));
2360 return wxNullColour
;
2364 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2366 if (HasBackgroundColour())
2370 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2372 return m_defGridAttr
->GetBackgroundColour();
2376 wxFAIL_MSG(wxT("Missing default cell attribute"));
2377 return wxNullColour
;
2381 const wxFont
& wxGridCellAttr::GetFont() const
2387 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2389 return m_defGridAttr
->GetFont();
2393 wxFAIL_MSG(wxT("Missing default cell attribute"));
2398 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2407 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2409 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2413 wxFAIL_MSG(wxT("Missing default cell attribute"));
2417 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2420 *num_rows
= m_sizeRows
;
2422 *num_cols
= m_sizeCols
;
2425 // GetRenderer and GetEditor use a slightly different decision path about
2426 // which attribute to use. If a non-default attr object has one then it is
2427 // used, otherwise the default editor or renderer is fetched from the grid and
2428 // used. It should be the default for the data type of the cell. If it is
2429 // NULL (because the table has a type that the grid does not have in its
2430 // registry), then the grid's default editor or renderer is used.
2432 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2434 wxGridCellRenderer
*renderer
= NULL
;
2436 if ( m_renderer
&& this != m_defGridAttr
)
2438 // use the cells renderer if it has one
2439 renderer
= m_renderer
;
2442 else // no non-default cell renderer
2444 // get default renderer for the data type
2447 // GetDefaultRendererForCell() will do IncRef() for us
2448 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2451 if ( renderer
== NULL
)
2453 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2455 // if we still don't have one then use the grid default
2456 // (no need for IncRef() here neither)
2457 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2459 else // default grid attr
2461 // use m_renderer which we had decided not to use initially
2462 renderer
= m_renderer
;
2469 // we're supposed to always find something
2470 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2475 // same as above, except for s/renderer/editor/g
2476 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2478 wxGridCellEditor
*editor
= NULL
;
2480 if ( m_editor
&& this != m_defGridAttr
)
2482 // use the cells editor if it has one
2486 else // no non default cell editor
2488 // get default editor for the data type
2491 // GetDefaultEditorForCell() will do IncRef() for us
2492 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2495 if ( editor
== NULL
)
2497 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2499 // if we still don't have one then use the grid default
2500 // (no need for IncRef() here neither)
2501 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2503 else // default grid attr
2505 // use m_editor which we had decided not to use initially
2513 // we're supposed to always find something
2514 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2519 // ----------------------------------------------------------------------------
2520 // wxGridCellAttrData
2521 // ----------------------------------------------------------------------------
2523 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2525 int n
= FindIndex(row
, col
);
2526 if ( n
== wxNOT_FOUND
)
2528 // add the attribute
2529 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2533 // free the old attribute
2534 m_attrs
[(size_t)n
].attr
->DecRef();
2538 // change the attribute
2539 m_attrs
[(size_t)n
].attr
= attr
;
2543 // remove this attribute
2544 m_attrs
.RemoveAt((size_t)n
);
2549 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2551 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2553 int n
= FindIndex(row
, col
);
2554 if ( n
!= wxNOT_FOUND
)
2556 attr
= m_attrs
[(size_t)n
].attr
;
2563 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2565 size_t count
= m_attrs
.GetCount();
2566 for ( size_t n
= 0; n
< count
; n
++ )
2568 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2569 wxCoord row
= coords
.GetRow();
2570 if ((size_t)row
>= pos
)
2574 // If rows inserted, include row counter where necessary
2575 coords
.SetRow(row
+ numRows
);
2577 else if (numRows
< 0)
2579 // If rows deleted ...
2580 if ((size_t)row
>= pos
- numRows
)
2582 // ...either decrement row counter (if row still exists)...
2583 coords
.SetRow(row
+ numRows
);
2587 // ...or remove the attribute
2588 // No need to DecRef the attribute itself since this is
2589 // done be wxGridCellWithAttr's destructor!
2590 m_attrs
.RemoveAt(n
);
2599 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2601 size_t count
= m_attrs
.GetCount();
2602 for ( size_t n
= 0; n
< count
; n
++ )
2604 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2605 wxCoord col
= coords
.GetCol();
2606 if ( (size_t)col
>= pos
)
2610 // If rows inserted, include row counter where necessary
2611 coords
.SetCol(col
+ numCols
);
2613 else if (numCols
< 0)
2615 // If rows deleted ...
2616 if ((size_t)col
>= pos
- numCols
)
2618 // ...either decrement row counter (if row still exists)...
2619 coords
.SetCol(col
+ numCols
);
2623 // ...or remove the attribute
2624 // No need to DecRef the attribute itself since this is
2625 // done be wxGridCellWithAttr's destructor!
2626 m_attrs
.RemoveAt(n
);
2635 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2637 size_t count
= m_attrs
.GetCount();
2638 for ( size_t n
= 0; n
< count
; n
++ )
2640 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2641 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2650 // ----------------------------------------------------------------------------
2651 // wxGridRowOrColAttrData
2652 // ----------------------------------------------------------------------------
2654 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2656 size_t count
= m_attrs
.Count();
2657 for ( size_t n
= 0; n
< count
; n
++ )
2659 m_attrs
[n
]->DecRef();
2663 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2665 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2667 int n
= m_rowsOrCols
.Index(rowOrCol
);
2668 if ( n
!= wxNOT_FOUND
)
2670 attr
= m_attrs
[(size_t)n
];
2677 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2679 int i
= m_rowsOrCols
.Index(rowOrCol
);
2680 if ( i
== wxNOT_FOUND
)
2682 // add the attribute
2683 m_rowsOrCols
.Add(rowOrCol
);
2688 size_t n
= (size_t)i
;
2691 // change the attribute
2692 m_attrs
[n
]->DecRef();
2697 // remove this attribute
2698 m_attrs
[n
]->DecRef();
2699 m_rowsOrCols
.RemoveAt(n
);
2700 m_attrs
.RemoveAt(n
);
2705 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2707 size_t count
= m_attrs
.GetCount();
2708 for ( size_t n
= 0; n
< count
; n
++ )
2710 int & rowOrCol
= m_rowsOrCols
[n
];
2711 if ( (size_t)rowOrCol
>= pos
)
2713 if ( numRowsOrCols
> 0 )
2715 // If rows inserted, include row counter where necessary
2716 rowOrCol
+= numRowsOrCols
;
2718 else if ( numRowsOrCols
< 0)
2720 // If rows deleted, either decrement row counter (if row still exists)
2721 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2722 rowOrCol
+= numRowsOrCols
;
2725 m_rowsOrCols
.RemoveAt(n
);
2726 m_attrs
[n
]->DecRef();
2727 m_attrs
.RemoveAt(n
);
2736 // ----------------------------------------------------------------------------
2737 // wxGridCellAttrProvider
2738 // ----------------------------------------------------------------------------
2740 wxGridCellAttrProvider::wxGridCellAttrProvider()
2742 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2745 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2750 void wxGridCellAttrProvider::InitData()
2752 m_data
= new wxGridCellAttrProviderData
;
2755 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2756 wxGridCellAttr::wxAttrKind kind
) const
2758 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2763 case (wxGridCellAttr::Any
):
2764 // Get cached merge attributes.
2765 // Currently not used as no cache implemented as not mutable
2766 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2769 // Basically implement old version.
2770 // Also check merge cache, so we don't have to re-merge every time..
2771 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2772 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2773 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2775 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2777 // Two or more are non NULL
2778 attr
= new wxGridCellAttr
;
2779 attr
->SetKind(wxGridCellAttr::Merged
);
2781 // Order is important..
2784 attr
->MergeWith(attrcell
);
2789 attr
->MergeWith(attrcol
);
2794 attr
->MergeWith(attrrow
);
2798 // store merge attr if cache implemented
2800 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2804 // one or none is non null return it or null.
2823 case (wxGridCellAttr::Cell
):
2824 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2827 case (wxGridCellAttr::Col
):
2828 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2831 case (wxGridCellAttr::Row
):
2832 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2837 // (wxGridCellAttr::Default):
2838 // (wxGridCellAttr::Merged):
2846 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2852 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2855 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2860 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2863 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2868 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2871 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2875 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2877 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2881 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2885 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2887 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2891 // ----------------------------------------------------------------------------
2892 // wxGridTypeRegistry
2893 // ----------------------------------------------------------------------------
2895 wxGridTypeRegistry::~wxGridTypeRegistry()
2897 size_t count
= m_typeinfo
.Count();
2898 for ( size_t i
= 0; i
< count
; i
++ )
2899 delete m_typeinfo
[i
];
2902 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2903 wxGridCellRenderer
* renderer
,
2904 wxGridCellEditor
* editor
)
2906 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2908 // is it already registered?
2909 int loc
= FindRegisteredDataType(typeName
);
2910 if ( loc
!= wxNOT_FOUND
)
2912 delete m_typeinfo
[loc
];
2913 m_typeinfo
[loc
] = info
;
2917 m_typeinfo
.Add(info
);
2921 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2923 size_t count
= m_typeinfo
.GetCount();
2924 for ( size_t i
= 0; i
< count
; i
++ )
2926 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2935 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2937 int index
= FindRegisteredDataType(typeName
);
2938 if ( index
== wxNOT_FOUND
)
2940 // check whether this is one of the standard ones, in which case
2941 // register it "on the fly"
2943 if ( typeName
== wxGRID_VALUE_STRING
)
2945 RegisterDataType(wxGRID_VALUE_STRING
,
2946 new wxGridCellStringRenderer
,
2947 new wxGridCellTextEditor
);
2950 #endif // wxUSE_TEXTCTRL
2952 if ( typeName
== wxGRID_VALUE_BOOL
)
2954 RegisterDataType(wxGRID_VALUE_BOOL
,
2955 new wxGridCellBoolRenderer
,
2956 new wxGridCellBoolEditor
);
2959 #endif // wxUSE_CHECKBOX
2961 if ( typeName
== wxGRID_VALUE_NUMBER
)
2963 RegisterDataType(wxGRID_VALUE_NUMBER
,
2964 new wxGridCellNumberRenderer
,
2965 new wxGridCellNumberEditor
);
2967 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2969 RegisterDataType(wxGRID_VALUE_FLOAT
,
2970 new wxGridCellFloatRenderer
,
2971 new wxGridCellFloatEditor
);
2974 #endif // wxUSE_TEXTCTRL
2976 if ( typeName
== wxGRID_VALUE_CHOICE
)
2978 RegisterDataType(wxGRID_VALUE_CHOICE
,
2979 new wxGridCellStringRenderer
,
2980 new wxGridCellChoiceEditor
);
2983 #endif // wxUSE_COMBOBOX
2988 // we get here only if just added the entry for this type, so return
2990 index
= m_typeinfo
.GetCount() - 1;
2996 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2998 int index
= FindDataType(typeName
);
2999 if ( index
== wxNOT_FOUND
)
3001 // the first part of the typename is the "real" type, anything after ':'
3002 // are the parameters for the renderer
3003 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3004 if ( index
== wxNOT_FOUND
)
3009 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3010 wxGridCellRenderer
*rendererOld
= renderer
;
3011 renderer
= renderer
->Clone();
3012 rendererOld
->DecRef();
3014 wxGridCellEditor
*editor
= GetEditor(index
);
3015 wxGridCellEditor
*editorOld
= editor
;
3016 editor
= editor
->Clone();
3017 editorOld
->DecRef();
3019 // do it even if there are no parameters to reset them to defaults
3020 wxString params
= typeName
.AfterFirst(_T(':'));
3021 renderer
->SetParameters(params
);
3022 editor
->SetParameters(params
);
3024 // register the new typename
3025 RegisterDataType(typeName
, renderer
, editor
);
3027 // we just registered it, it's the last one
3028 index
= m_typeinfo
.GetCount() - 1;
3034 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3036 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3043 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3045 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3052 // ----------------------------------------------------------------------------
3054 // ----------------------------------------------------------------------------
3056 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3058 wxGridTableBase::wxGridTableBase()
3060 m_view
= (wxGrid
*) NULL
;
3061 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3064 wxGridTableBase::~wxGridTableBase()
3066 delete m_attrProvider
;
3069 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3071 delete m_attrProvider
;
3072 m_attrProvider
= attrProvider
;
3075 bool wxGridTableBase::CanHaveAttributes()
3077 if ( ! GetAttrProvider() )
3079 // use the default attr provider by default
3080 SetAttrProvider(new wxGridCellAttrProvider
);
3086 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3088 if ( m_attrProvider
)
3089 return m_attrProvider
->GetAttr(row
, col
, kind
);
3091 return (wxGridCellAttr
*)NULL
;
3094 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3096 if ( m_attrProvider
)
3098 attr
->SetKind(wxGridCellAttr::Cell
);
3099 m_attrProvider
->SetAttr(attr
, row
, col
);
3103 // as we take ownership of the pointer and don't store it, we must
3109 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3111 if ( m_attrProvider
)
3113 attr
->SetKind(wxGridCellAttr::Row
);
3114 m_attrProvider
->SetRowAttr(attr
, row
);
3118 // as we take ownership of the pointer and don't store it, we must
3124 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3126 if ( m_attrProvider
)
3128 attr
->SetKind(wxGridCellAttr::Col
);
3129 m_attrProvider
->SetColAttr(attr
, col
);
3133 // as we take ownership of the pointer and don't store it, we must
3139 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3140 size_t WXUNUSED(numRows
) )
3142 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3147 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3149 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3154 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3155 size_t WXUNUSED(numRows
) )
3157 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3162 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3163 size_t WXUNUSED(numCols
) )
3165 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3170 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3172 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3177 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3178 size_t WXUNUSED(numCols
) )
3180 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3185 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3189 // RD: Starting the rows at zero confuses users,
3190 // no matter how much it makes sense to us geeks.
3196 wxString
wxGridTableBase::GetColLabelValue( int col
)
3198 // default col labels are:
3199 // cols 0 to 25 : A-Z
3200 // cols 26 to 675 : AA-ZZ
3205 for ( n
= 1; ; n
++ )
3207 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3213 // reverse the string...
3215 for ( i
= 0; i
< n
; i
++ )
3223 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3225 return wxGRID_VALUE_STRING
;
3228 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3229 const wxString
& typeName
)
3231 return typeName
== wxGRID_VALUE_STRING
;
3234 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3236 return CanGetValueAs(row
, col
, typeName
);
3239 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3244 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3249 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3254 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3255 long WXUNUSED(value
) )
3259 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3260 double WXUNUSED(value
) )
3264 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3265 bool WXUNUSED(value
) )
3269 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3270 const wxString
& WXUNUSED(typeName
) )
3275 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3276 const wxString
& WXUNUSED(typeName
),
3277 void* WXUNUSED(value
) )
3281 //////////////////////////////////////////////////////////////////////
3283 // Message class for the grid table to send requests and notifications
3287 wxGridTableMessage::wxGridTableMessage()
3289 m_table
= (wxGridTableBase
*) NULL
;
3295 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3296 int commandInt1
, int commandInt2
)
3300 m_comInt1
= commandInt1
;
3301 m_comInt2
= commandInt2
;
3304 //////////////////////////////////////////////////////////////////////
3306 // A basic grid table for string data. An object of this class will
3307 // created by wxGrid if you don't specify an alternative table class.
3310 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3312 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3314 wxGridStringTable::wxGridStringTable()
3319 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3322 m_data
.Alloc( numRows
);
3325 sa
.Alloc( numCols
);
3326 sa
.Add( wxEmptyString
, numCols
);
3328 m_data
.Add( sa
, numRows
);
3331 wxGridStringTable::~wxGridStringTable()
3335 int wxGridStringTable::GetNumberRows()
3337 return m_data
.GetCount();
3340 int wxGridStringTable::GetNumberCols()
3342 if ( m_data
.GetCount() > 0 )
3343 return m_data
[0].GetCount();
3348 wxString
wxGridStringTable::GetValue( int row
, int col
)
3350 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3352 _T("invalid row or column index in wxGridStringTable") );
3354 return m_data
[row
][col
];
3357 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3359 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3360 _T("invalid row or column index in wxGridStringTable") );
3362 m_data
[row
][col
] = value
;
3365 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3367 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3369 _T("invalid row or column index in wxGridStringTable") );
3371 return (m_data
[row
][col
] == wxEmptyString
);
3374 void wxGridStringTable::Clear()
3377 int numRows
, numCols
;
3379 numRows
= m_data
.GetCount();
3382 numCols
= m_data
[0].GetCount();
3384 for ( row
= 0; row
< numRows
; row
++ )
3386 for ( col
= 0; col
< numCols
; col
++ )
3388 m_data
[row
][col
] = wxEmptyString
;
3394 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3396 size_t curNumRows
= m_data
.GetCount();
3397 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3398 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3400 if ( pos
>= curNumRows
)
3402 return AppendRows( numRows
);
3406 sa
.Alloc( curNumCols
);
3407 sa
.Add( wxEmptyString
, curNumCols
);
3408 m_data
.Insert( sa
, pos
, numRows
);
3412 wxGridTableMessage
msg( this,
3413 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3417 GetView()->ProcessTableMessage( msg
);
3423 bool wxGridStringTable::AppendRows( size_t numRows
)
3425 size_t curNumRows
= m_data
.GetCount();
3426 size_t curNumCols
= ( curNumRows
> 0
3427 ? m_data
[0].GetCount()
3428 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3431 if ( curNumCols
> 0 )
3433 sa
.Alloc( curNumCols
);
3434 sa
.Add( wxEmptyString
, curNumCols
);
3437 m_data
.Add( sa
, numRows
);
3441 wxGridTableMessage
msg( this,
3442 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3445 GetView()->ProcessTableMessage( msg
);
3451 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3453 size_t curNumRows
= m_data
.GetCount();
3455 if ( pos
>= curNumRows
)
3457 wxFAIL_MSG( wxString::Format
3459 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3461 (unsigned long)numRows
,
3462 (unsigned long)curNumRows
3468 if ( numRows
> curNumRows
- pos
)
3470 numRows
= curNumRows
- pos
;
3473 if ( numRows
>= curNumRows
)
3479 m_data
.RemoveAt( pos
, numRows
);
3484 wxGridTableMessage
msg( this,
3485 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3489 GetView()->ProcessTableMessage( msg
);
3495 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3499 size_t curNumRows
= m_data
.GetCount();
3500 size_t curNumCols
= ( curNumRows
> 0
3501 ? m_data
[0].GetCount()
3502 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3504 if ( pos
>= curNumCols
)
3506 return AppendCols( numCols
);
3509 for ( row
= 0; row
< curNumRows
; row
++ )
3511 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3513 m_data
[row
].Insert( wxEmptyString
, col
);
3519 wxGridTableMessage
msg( this,
3520 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3524 GetView()->ProcessTableMessage( msg
);
3530 bool wxGridStringTable::AppendCols( size_t numCols
)
3534 size_t curNumRows
= m_data
.GetCount();
3539 // TODO: something better than this ?
3541 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3546 for ( row
= 0; row
< curNumRows
; row
++ )
3548 m_data
[row
].Add( wxEmptyString
, numCols
);
3553 wxGridTableMessage
msg( this,
3554 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3557 GetView()->ProcessTableMessage( msg
);
3563 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3567 size_t curNumRows
= m_data
.GetCount();
3568 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3569 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3571 if ( pos
>= curNumCols
)
3573 wxFAIL_MSG( wxString::Format
3575 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3577 (unsigned long)numCols
,
3578 (unsigned long)curNumCols
3583 if ( numCols
> curNumCols
- pos
)
3585 numCols
= curNumCols
- pos
;
3588 for ( row
= 0; row
< curNumRows
; row
++ )
3590 if ( numCols
>= curNumCols
)
3592 m_data
[row
].Clear();
3596 m_data
[row
].RemoveAt( pos
, numCols
);
3602 wxGridTableMessage
msg( this,
3603 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3607 GetView()->ProcessTableMessage( msg
);
3613 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3615 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3617 // using default label
3619 return wxGridTableBase::GetRowLabelValue( row
);
3623 return m_rowLabels
[row
];
3627 wxString
wxGridStringTable::GetColLabelValue( int col
)
3629 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3631 // using default label
3633 return wxGridTableBase::GetColLabelValue( col
);
3637 return m_colLabels
[col
];
3641 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3643 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3645 int n
= m_rowLabels
.GetCount();
3648 for ( i
= n
; i
<= row
; i
++ )
3650 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3654 m_rowLabels
[row
] = value
;
3657 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3659 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3661 int n
= m_colLabels
.GetCount();
3664 for ( i
= n
; i
<= col
; i
++ )
3666 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3670 m_colLabels
[col
] = value
;
3674 //////////////////////////////////////////////////////////////////////
3675 //////////////////////////////////////////////////////////////////////
3677 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3679 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3680 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3681 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3682 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3683 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3684 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3685 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3688 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3690 const wxPoint
&pos
, const wxSize
&size
)
3691 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3696 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3700 // NO - don't do this because it will set both the x and y origin
3701 // coords to match the parent scrolled window and we just want to
3702 // set the y coord - MB
3704 // m_owner->PrepareDC( dc );
3707 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3708 dc
.SetDeviceOrigin( 0, -y
);
3710 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3711 m_owner
->DrawRowLabels( dc
, rows
);
3714 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3716 m_owner
->ProcessRowLabelMouseEvent( event
);
3719 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3721 m_owner
->GetEventHandler()->ProcessEvent( event
);
3724 // This seems to be required for wxMotif otherwise the mouse
3725 // cursor must be in the cell edit control to get key events
3727 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3729 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3733 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3735 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3739 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3741 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3745 //////////////////////////////////////////////////////////////////////
3747 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3749 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3750 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3751 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3752 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3753 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3754 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3755 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3758 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3760 const wxPoint
&pos
, const wxSize
&size
)
3761 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3766 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3770 // NO - don't do this because it will set both the x and y origin
3771 // coords to match the parent scrolled window and we just want to
3772 // set the x coord - MB
3774 // m_owner->PrepareDC( dc );
3777 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3778 dc
.SetDeviceOrigin( -x
, 0 );
3780 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3781 m_owner
->DrawColLabels( dc
, cols
);
3784 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3786 m_owner
->ProcessColLabelMouseEvent( event
);
3789 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3791 m_owner
->GetEventHandler()->ProcessEvent( event
);
3794 // This seems to be required for wxMotif otherwise the mouse
3795 // cursor must be in the cell edit control to get key events
3797 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3799 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3803 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3805 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3809 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3811 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3815 //////////////////////////////////////////////////////////////////////
3817 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3819 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3820 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3821 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3822 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3823 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3824 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3825 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3828 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3830 const wxPoint
&pos
, const wxSize
&size
)
3831 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3836 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3840 int client_height
= 0;
3841 int client_width
= 0;
3842 GetClientSize( &client_width
, &client_height
);
3844 // VZ: any reason for this ifdef? (FIXME)
3849 rect
.SetWidth( client_width
- 2 );
3850 rect
.SetHeight( client_height
- 2 );
3852 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3854 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
), 1, wxSOLID
) );
3855 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3856 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3857 dc
.DrawLine( 0, 0, client_width
, 0 );
3858 dc
.DrawLine( 0, 0, 0, client_height
);
3860 dc
.SetPen( *wxWHITE_PEN
);
3861 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3862 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3866 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3868 m_owner
->ProcessCornerLabelMouseEvent( event
);
3871 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3873 m_owner
->GetEventHandler()->ProcessEvent(event
);
3876 // This seems to be required for wxMotif otherwise the mouse
3877 // cursor must be in the cell edit control to get key events
3879 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3881 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3885 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3887 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3891 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3893 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3897 //////////////////////////////////////////////////////////////////////
3899 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3901 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3902 EVT_PAINT( wxGridWindow::OnPaint
)
3903 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3904 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3905 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3906 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3907 EVT_CHAR( wxGridWindow::OnChar
)
3908 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3909 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3910 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3913 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3914 wxGridRowLabelWindow
*rowLblWin
,
3915 wxGridColLabelWindow
*colLblWin
,
3918 const wxSize
&size
)
3920 parent
, id
, pos
, size
,
3921 wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
| wxFULL_REPAINT_ON_RESIZE
,
3922 wxT("grid window") )
3925 m_rowLabelWin
= rowLblWin
;
3926 m_colLabelWin
= colLblWin
;
3929 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3931 wxPaintDC
dc( this );
3932 m_owner
->PrepareDC( dc
);
3933 wxRegion reg
= GetUpdateRegion();
3934 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3935 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3937 #if WXGRID_DRAW_LINES
3938 m_owner
->DrawAllGridLines( dc
, reg
);
3941 m_owner
->DrawGridSpace( dc
);
3942 m_owner
->DrawHighlight( dc
, dirtyCells
);
3945 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3947 wxWindow::ScrollWindow( dx
, dy
, rect
);
3948 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3949 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3952 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3954 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3957 m_owner
->ProcessGridCellMouseEvent( event
);
3960 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3962 m_owner
->GetEventHandler()->ProcessEvent( event
);
3965 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3966 // cursor must be in the cell edit control to get key events
3968 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3970 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3974 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3976 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3980 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3982 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3986 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3990 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3992 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3996 //////////////////////////////////////////////////////////////////////
3998 // Internal Helper function for computing row or column from some
3999 // (unscrolled) coordinate value, using either
4000 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4001 // of m_rowBottoms/m_ColRights to speed up the search!
4003 // Internal helper macros for simpler use of that function
4005 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4006 const wxArrayInt
& BorderArray
, int nMax
,
4009 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
4010 m_minAcceptableColWidth, \
4011 m_colRights, m_numCols, true)
4012 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4013 m_minAcceptableRowHeight, \
4014 m_rowBottoms, m_numRows, true)
4016 /////////////////////////////////////////////////////////////////////
4018 #if wxUSE_EXTENDED_RTTI
4019 WX_DEFINE_FLAGS( wxGridStyle
)
4021 wxBEGIN_FLAGS( wxGridStyle
)
4022 // new style border flags, we put them first to
4023 // use them for streaming out
4024 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4025 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4026 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4027 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4028 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4029 wxFLAGS_MEMBER(wxBORDER_NONE
)
4031 // old style border flags
4032 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4033 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4034 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4035 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4036 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4037 wxFLAGS_MEMBER(wxBORDER
)
4039 // standard window styles
4040 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4041 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4042 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4043 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4044 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4045 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4046 wxFLAGS_MEMBER(wxVSCROLL
)
4047 wxFLAGS_MEMBER(wxHSCROLL
)
4049 wxEND_FLAGS( wxGridStyle
)
4051 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4053 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4054 wxHIDE_PROPERTY( Children
)
4055 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4056 wxEND_PROPERTIES_TABLE()
4058 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4059 wxEND_HANDLERS_TABLE()
4061 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4064 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4067 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4070 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4071 EVT_PAINT( wxGrid::OnPaint
)
4072 EVT_SIZE( wxGrid::OnSize
)
4073 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4074 EVT_KEY_UP( wxGrid::OnKeyUp
)
4075 EVT_CHAR ( wxGrid::OnChar
)
4076 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4081 // in order to make sure that a size event is not
4082 // trigerred in a unfinished state
4083 m_cornerLabelWin
= NULL
;
4084 m_rowLabelWin
= NULL
;
4085 m_colLabelWin
= NULL
;
4089 wxGrid::wxGrid( wxWindow
*parent
,
4094 const wxString
& name
)
4095 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4096 m_colMinWidths(GRID_HASH_SIZE
),
4097 m_rowMinHeights(GRID_HASH_SIZE
)
4100 SetBestFittingSize(size
);
4103 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4104 const wxPoint
& pos
, const wxSize
& size
,
4105 long style
, const wxString
& name
)
4107 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4108 style
| wxWANTS_CHARS
, name
))
4111 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4112 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4115 SetBestFittingSize(size
);
4122 // Must do this or ~wxScrollHelper will pop the wrong event handler
4123 SetTargetWindow(this);
4125 wxSafeDecRef(m_defaultCellAttr
);
4127 #ifdef DEBUG_ATTR_CACHE
4128 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4129 wxPrintf(_T("wxGrid attribute cache statistics: "
4130 "total: %u, hits: %u (%u%%)\n"),
4131 total
, gs_nAttrCacheHits
,
4132 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4138 delete m_typeRegistry
;
4143 // ----- internal init and update functions
4146 // NOTE: If using the default visual attributes works everywhere then this can
4147 // be removed as well as the #else cases below.
4148 #define _USE_VISATTR 0
4151 #include "wx/listbox.h"
4154 void wxGrid::Create()
4156 // set to true by CreateGrid
4159 // create the type registry
4160 m_typeRegistry
= new wxGridTypeRegistry
;
4163 m_table
= (wxGridTableBase
*) NULL
;
4166 m_cellEditCtrlEnabled
= false;
4168 m_defaultCellAttr
= new wxGridCellAttr();
4170 // Set default cell attributes
4171 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4172 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4173 m_defaultCellAttr
->SetFont(GetFont());
4174 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4175 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4176 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4179 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4180 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4182 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4183 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4186 m_defaultCellAttr
->SetTextColour(
4187 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4188 m_defaultCellAttr
->SetBackgroundColour(
4189 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4194 m_currentCellCoords
= wxGridNoCellCoords
;
4196 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4197 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4199 // subwindow components that make up the wxGrid
4200 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4205 m_colLabelWin
= new wxGridColLabelWindow( this,
4210 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4215 m_gridWin
= new wxGridWindow( this,
4222 SetTargetWindow( m_gridWin
);
4225 wxColour gfg
= gva
.colFg
;
4226 wxColour gbg
= gva
.colBg
;
4227 wxColour lfg
= lva
.colFg
;
4228 wxColour lbg
= lva
.colBg
;
4230 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4231 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4232 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4233 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4236 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4237 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4238 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4239 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4240 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4241 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4243 m_gridWin
->SetOwnForegroundColour(gfg
);
4244 m_gridWin
->SetOwnBackgroundColour(gbg
);
4249 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4250 wxGrid::wxGridSelectionModes selmode
)
4252 wxCHECK_MSG( !m_created
,
4254 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4256 m_numRows
= numRows
;
4257 m_numCols
= numCols
;
4259 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4260 m_table
->SetView( this );
4262 m_selection
= new wxGridSelection( this, selmode
);
4271 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4273 wxCHECK_RET( m_created
,
4274 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4276 m_selection
->SetSelectionMode( selmode
);
4279 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4281 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4282 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4284 return m_selection
->GetSelectionMode();
4287 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4288 wxGrid::wxGridSelectionModes selmode
)
4292 // stop all processing
4297 wxGridTableBase
*t
= m_table
;
4312 m_numRows
= table
->GetNumberRows();
4313 m_numCols
= table
->GetNumberCols();
4316 m_table
->SetView( this );
4317 m_ownTable
= takeOwnership
;
4318 m_selection
= new wxGridSelection( this, selmode
);
4330 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4331 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4333 if ( m_rowLabelWin
)
4335 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4339 m_labelBackgroundColour
= *wxWHITE
;
4342 m_labelTextColour
= *wxBLACK
;
4345 m_attrCache
.row
= -1;
4346 m_attrCache
.col
= -1;
4347 m_attrCache
.attr
= NULL
;
4349 // TODO: something better than this ?
4351 m_labelFont
= this->GetFont();
4352 m_labelFont
.SetWeight( wxBOLD
);
4354 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4355 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4357 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4358 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4359 m_colLabelTextOrientation
= wxHORIZONTAL
;
4361 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4362 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4364 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4365 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4367 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4368 m_defaultRowHeight
+= 8;
4370 m_defaultRowHeight
+= 4;
4373 m_gridLineColour
= wxColour( 192,192,192 );
4374 m_gridLinesEnabled
= true;
4375 m_cellHighlightColour
= *wxBLACK
;
4376 m_cellHighlightPenWidth
= 2;
4377 m_cellHighlightROPenWidth
= 1;
4379 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4380 m_winCapture
= (wxWindow
*)NULL
;
4381 m_canDragRowSize
= true;
4382 m_canDragColSize
= true;
4383 m_canDragGridSize
= true;
4384 m_canDragCell
= false;
4386 m_dragRowOrCol
= -1;
4387 m_isDragging
= false;
4388 m_startDragPos
= wxDefaultPosition
;
4390 m_waitForSlowClick
= false;
4392 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4393 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4395 m_currentCellCoords
= wxGridNoCellCoords
;
4397 m_selectingTopLeft
= wxGridNoCellCoords
;
4398 m_selectingBottomRight
= wxGridNoCellCoords
;
4399 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4400 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4402 m_editable
= true; // default for whole grid
4404 m_inOnKeyDown
= false;
4410 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4411 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4414 // ----------------------------------------------------------------------------
4415 // the idea is to call these functions only when necessary because they create
4416 // quite big arrays which eat memory mostly unnecessary - in particular, if
4417 // default widths/heights are used for all rows/columns, we may not use these
4420 // with some extra code, it should be possible to only store the
4421 // widths/heights different from default ones but this will be done later...
4422 // ----------------------------------------------------------------------------
4424 void wxGrid::InitRowHeights()
4426 m_rowHeights
.Empty();
4427 m_rowBottoms
.Empty();
4429 m_rowHeights
.Alloc( m_numRows
);
4430 m_rowBottoms
.Alloc( m_numRows
);
4434 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4436 for ( int i
= 0; i
< m_numRows
; i
++ )
4438 rowBottom
+= m_defaultRowHeight
;
4439 m_rowBottoms
.Add( rowBottom
);
4443 void wxGrid::InitColWidths()
4445 m_colWidths
.Empty();
4446 m_colRights
.Empty();
4448 m_colWidths
.Alloc( m_numCols
);
4449 m_colRights
.Alloc( m_numCols
);
4452 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4454 for ( int i
= 0; i
< m_numCols
; i
++ )
4456 colRight
+= m_defaultColWidth
;
4457 m_colRights
.Add( colRight
);
4461 int wxGrid::GetColWidth(int col
) const
4463 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4466 int wxGrid::GetColLeft(int col
) const
4468 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4469 : m_colRights
[col
] - m_colWidths
[col
];
4472 int wxGrid::GetColRight(int col
) const
4474 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4478 int wxGrid::GetRowHeight(int row
) const
4480 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4483 int wxGrid::GetRowTop(int row
) const
4485 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4486 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4489 int wxGrid::GetRowBottom(int row
) const
4491 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4492 : m_rowBottoms
[row
];
4495 void wxGrid::CalcDimensions()
4498 GetClientSize( &cw
, &ch
);
4500 if ( m_rowLabelWin
->IsShown() )
4501 cw
-= m_rowLabelWidth
;
4502 if ( m_colLabelWin
->IsShown() )
4503 ch
-= m_colLabelHeight
;
4506 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4507 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4509 // take into account editor if shown
4510 if ( IsCellEditControlShown() )
4513 int r
= m_currentCellCoords
.GetRow();
4514 int c
= m_currentCellCoords
.GetCol();
4515 int x
= GetColLeft(c
);
4516 int y
= GetRowTop(r
);
4518 // how big is the editor
4519 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4520 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4521 editor
->GetControl()->GetSize(&w2
, &h2
);
4532 // preserve (more or less) the previous position
4534 GetViewStart( &x
, &y
);
4536 // ensure the position is valid for the new scroll ranges
4538 x
= wxMax( w
- 1, 0 );
4540 y
= wxMax( h
- 1, 0 );
4542 // do set scrollbar parameters
4543 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4544 GetScrollX(w
), GetScrollY(h
), x
, y
,
4545 GetBatchCount() != 0);
4547 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4548 // still must reposition the children
4552 void wxGrid::CalcWindowSizes()
4554 // escape if the window is has not been fully created yet
4556 if ( m_cornerLabelWin
== NULL
)
4560 GetClientSize( &cw
, &ch
);
4562 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4563 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4565 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4566 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4568 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4569 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4571 if ( m_gridWin
&& m_gridWin
->IsShown() )
4572 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4575 // this is called when the grid table sends a message
4576 // to indicate that it has been redimensioned
4578 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4581 bool result
= false;
4583 // Clear the attribute cache as the attribute might refer to a different
4584 // cell than stored in the cache after adding/removing rows/columns.
4587 // By the same reasoning, the editor should be dismissed if columns are
4588 // added or removed. And for consistency, it should IMHO always be
4589 // removed, not only if the cell "underneath" it actually changes.
4590 // For now, I intentionally do not save the editor's content as the
4591 // cell it might want to save that stuff to might no longer exist.
4592 HideCellEditControl();
4595 // if we were using the default widths/heights so far, we must change them
4597 if ( m_colWidths
.IsEmpty() )
4602 if ( m_rowHeights
.IsEmpty() )
4608 switch ( msg
.GetId() )
4610 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4612 size_t pos
= msg
.GetCommandInt();
4613 int numRows
= msg
.GetCommandInt2();
4615 m_numRows
+= numRows
;
4617 if ( !m_rowHeights
.IsEmpty() )
4619 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4620 m_rowBottoms
.Insert( 0, pos
, numRows
);
4624 bottom
= m_rowBottoms
[pos
- 1];
4626 for ( i
= pos
; i
< m_numRows
; i
++ )
4628 bottom
+= m_rowHeights
[i
];
4629 m_rowBottoms
[i
] = bottom
;
4633 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4635 // if we have just inserted cols into an empty grid the current
4636 // cell will be undefined...
4638 SetCurrentCell( 0, 0 );
4642 m_selection
->UpdateRows( pos
, numRows
);
4643 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4645 attrProvider
->UpdateAttrRows( pos
, numRows
);
4647 if ( !GetBatchCount() )
4650 m_rowLabelWin
->Refresh();
4656 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4658 int numRows
= msg
.GetCommandInt();
4659 int oldNumRows
= m_numRows
;
4660 m_numRows
+= numRows
;
4662 if ( !m_rowHeights
.IsEmpty() )
4664 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4665 m_rowBottoms
.Add( 0, numRows
);
4668 if ( oldNumRows
> 0 )
4669 bottom
= m_rowBottoms
[oldNumRows
- 1];
4671 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4673 bottom
+= m_rowHeights
[i
];
4674 m_rowBottoms
[i
] = bottom
;
4678 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4680 // if we have just inserted cols into an empty grid the current
4681 // cell will be undefined...
4683 SetCurrentCell( 0, 0 );
4686 if ( !GetBatchCount() )
4689 m_rowLabelWin
->Refresh();
4695 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4697 size_t pos
= msg
.GetCommandInt();
4698 int numRows
= msg
.GetCommandInt2();
4699 m_numRows
-= numRows
;
4701 if ( !m_rowHeights
.IsEmpty() )
4703 m_rowHeights
.RemoveAt( pos
, numRows
);
4704 m_rowBottoms
.RemoveAt( pos
, numRows
);
4707 for ( i
= 0; i
< m_numRows
; i
++ )
4709 h
+= m_rowHeights
[i
];
4710 m_rowBottoms
[i
] = h
;
4716 m_currentCellCoords
= wxGridNoCellCoords
;
4720 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4721 m_currentCellCoords
.Set( 0, 0 );
4725 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4726 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4729 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4731 // ifdef'd out following patch from Paul Gammans
4733 // No need to touch column attributes, unless we
4734 // removed _all_ rows, in this case, we remove
4735 // all column attributes.
4736 // I hate to do this here, but the
4737 // needed data is not available inside UpdateAttrRows.
4738 if ( !GetNumberRows() )
4739 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4743 if ( !GetBatchCount() )
4746 m_rowLabelWin
->Refresh();
4752 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4754 size_t pos
= msg
.GetCommandInt();
4755 int numCols
= msg
.GetCommandInt2();
4756 m_numCols
+= numCols
;
4758 if ( !m_colWidths
.IsEmpty() )
4760 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4761 m_colRights
.Insert( 0, pos
, numCols
);
4765 right
= m_colRights
[pos
- 1];
4767 for ( i
= pos
; i
< m_numCols
; i
++ )
4769 right
+= m_colWidths
[i
];
4770 m_colRights
[i
] = right
;
4774 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4776 // if we have just inserted cols into an empty grid the current
4777 // cell will be undefined...
4779 SetCurrentCell( 0, 0 );
4783 m_selection
->UpdateCols( pos
, numCols
);
4784 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4786 attrProvider
->UpdateAttrCols( pos
, numCols
);
4787 if ( !GetBatchCount() )
4790 m_colLabelWin
->Refresh();
4796 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4798 int numCols
= msg
.GetCommandInt();
4799 int oldNumCols
= m_numCols
;
4800 m_numCols
+= numCols
;
4801 if ( !m_colWidths
.IsEmpty() )
4803 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4804 m_colRights
.Add( 0, numCols
);
4807 if ( oldNumCols
> 0 )
4808 right
= m_colRights
[oldNumCols
- 1];
4810 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4812 right
+= m_colWidths
[i
];
4813 m_colRights
[i
] = right
;
4817 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4819 // if we have just inserted cols into an empty grid the current
4820 // cell will be undefined...
4822 SetCurrentCell( 0, 0 );
4824 if ( !GetBatchCount() )
4827 m_colLabelWin
->Refresh();
4833 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4835 size_t pos
= msg
.GetCommandInt();
4836 int numCols
= msg
.GetCommandInt2();
4837 m_numCols
-= numCols
;
4839 if ( !m_colWidths
.IsEmpty() )
4841 m_colWidths
.RemoveAt( pos
, numCols
);
4842 m_colRights
.RemoveAt( pos
, numCols
);
4845 for ( i
= 0; i
< m_numCols
; i
++ )
4847 w
+= m_colWidths
[i
];
4854 m_currentCellCoords
= wxGridNoCellCoords
;
4858 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4859 m_currentCellCoords
.Set( 0, 0 );
4863 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4864 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4867 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4869 // ifdef'd out following patch from Paul Gammans
4871 // No need to touch row attributes, unless we
4872 // removed _all_ columns, in this case, we remove
4873 // all row attributes.
4874 // I hate to do this here, but the
4875 // needed data is not available inside UpdateAttrCols.
4876 if ( !GetNumberCols() )
4877 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4881 if ( !GetBatchCount() )
4884 m_colLabelWin
->Refresh();
4891 if (result
&& !GetBatchCount() )
4892 m_gridWin
->Refresh();
4897 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4899 wxRegionIterator
iter( reg
);
4902 wxArrayInt rowlabels
;
4909 // TODO: remove this when we can...
4910 // There is a bug in wxMotif that gives garbage update
4911 // rectangles if you jump-scroll a long way by clicking the
4912 // scrollbar with middle button. This is a work-around
4914 #if defined(__WXMOTIF__)
4916 m_gridWin
->GetClientSize( &cw
, &ch
);
4917 if ( r
.GetTop() > ch
)
4919 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4922 // logical bounds of update region
4925 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4926 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4928 // find the row labels within these bounds
4931 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4933 if ( GetRowBottom(row
) < top
)
4936 if ( GetRowTop(row
) > bottom
)
4939 rowlabels
.Add( row
);
4948 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4950 wxRegionIterator
iter( reg
);
4953 wxArrayInt colLabels
;
4960 // TODO: remove this when we can...
4961 // There is a bug in wxMotif that gives garbage update
4962 // rectangles if you jump-scroll a long way by clicking the
4963 // scrollbar with middle button. This is a work-around
4965 #if defined(__WXMOTIF__)
4967 m_gridWin
->GetClientSize( &cw
, &ch
);
4968 if ( r
.GetLeft() > cw
)
4970 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4973 // logical bounds of update region
4976 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4977 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4979 // find the cells within these bounds
4982 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4984 if ( GetColRight(col
) < left
)
4987 if ( GetColLeft(col
) > right
)
4990 colLabels
.Add( col
);
4999 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
5001 wxRegionIterator
iter( reg
);
5004 wxGridCellCoordsArray cellsExposed
;
5006 int left
, top
, right
, bottom
;
5011 // TODO: remove this when we can...
5012 // There is a bug in wxMotif that gives garbage update
5013 // rectangles if you jump-scroll a long way by clicking the
5014 // scrollbar with middle button. This is a work-around
5016 #if defined(__WXMOTIF__)
5018 m_gridWin
->GetClientSize( &cw
, &ch
);
5019 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5020 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5021 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5022 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5025 // logical bounds of update region
5027 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5028 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5030 // find the cells within these bounds
5033 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5035 if ( GetRowBottom(row
) <= top
)
5038 if ( GetRowTop(row
) > bottom
)
5041 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
5043 if ( GetColRight(col
) <= left
)
5046 if ( GetColLeft(col
) > right
)
5049 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5056 return cellsExposed
;
5060 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5063 wxPoint
pos( event
.GetPosition() );
5064 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5066 if ( event
.Dragging() )
5070 m_isDragging
= true;
5071 m_rowLabelWin
->CaptureMouse();
5074 if ( event
.LeftIsDown() )
5076 switch ( m_cursorMode
)
5078 case WXGRID_CURSOR_RESIZE_ROW
:
5080 int cw
, ch
, left
, dummy
;
5081 m_gridWin
->GetClientSize( &cw
, &ch
);
5082 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5084 wxClientDC
dc( m_gridWin
);
5087 GetRowTop(m_dragRowOrCol
) +
5088 GetRowMinimalHeight(m_dragRowOrCol
) );
5089 dc
.SetLogicalFunction(wxINVERT
);
5090 if ( m_dragLastPos
>= 0 )
5092 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5094 dc
.DrawLine( left
, y
, left
+cw
, y
);
5099 case WXGRID_CURSOR_SELECT_ROW
:
5101 if ( (row
= YToRow( y
)) >= 0 )
5105 m_selection
->SelectRow( row
,
5106 event
.ControlDown(),
5115 // default label to suppress warnings about "enumeration value
5116 // 'xxx' not handled in switch
5124 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5129 if (m_rowLabelWin
->HasCapture())
5130 m_rowLabelWin
->ReleaseMouse();
5131 m_isDragging
= false;
5134 // ------------ Entering or leaving the window
5136 if ( event
.Entering() || event
.Leaving() )
5138 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5141 // ------------ Left button pressed
5143 else if ( event
.LeftDown() )
5145 // don't send a label click event for a hit on the
5146 // edge of the row label - this is probably the user
5147 // wanting to resize the row
5149 if ( YToEdgeOfRow(y
) < 0 )
5153 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5155 if ( !event
.ShiftDown() && !event
.CmdDown() )
5159 if ( event
.ShiftDown() )
5161 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5164 GetNumberCols() - 1,
5165 event
.ControlDown(),
5172 m_selection
->SelectRow( row
,
5173 event
.ControlDown(),
5180 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5185 // starting to drag-resize a row
5186 if ( CanDragRowSize() )
5187 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5191 // ------------ Left double click
5193 else if (event
.LeftDClick() )
5195 row
= YToEdgeOfRow(y
);
5200 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5202 // no default action at the moment
5207 // adjust row height depending on label text
5208 AutoSizeRowLabelSize( row
);
5210 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5215 // ------------ Left button released
5217 else if ( event
.LeftUp() )
5219 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5221 DoEndDragResizeRow();
5223 // Note: we are ending the event *after* doing
5224 // default processing in this case
5226 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5229 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5233 // ------------ Right button down
5235 else if ( event
.RightDown() )
5239 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5241 // no default action at the moment
5245 // ------------ Right double click
5247 else if ( event
.RightDClick() )
5251 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5253 // no default action at the moment
5257 // ------------ No buttons down and mouse moving
5259 else if ( event
.Moving() )
5261 m_dragRowOrCol
= YToEdgeOfRow( y
);
5262 if ( m_dragRowOrCol
>= 0 )
5264 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5266 // don't capture the mouse yet
5267 if ( CanDragRowSize() )
5268 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5271 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5273 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5278 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5281 wxPoint
pos( event
.GetPosition() );
5282 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5284 if ( event
.Dragging() )
5288 m_isDragging
= true;
5289 m_colLabelWin
->CaptureMouse();
5292 if ( event
.LeftIsDown() )
5294 switch ( m_cursorMode
)
5296 case WXGRID_CURSOR_RESIZE_COL
:
5298 int cw
, ch
, dummy
, top
;
5299 m_gridWin
->GetClientSize( &cw
, &ch
);
5300 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5302 wxClientDC
dc( m_gridWin
);
5305 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5306 GetColMinimalWidth(m_dragRowOrCol
));
5307 dc
.SetLogicalFunction(wxINVERT
);
5308 if ( m_dragLastPos
>= 0 )
5310 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5312 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5317 case WXGRID_CURSOR_SELECT_COL
:
5319 if ( (col
= XToCol( x
)) >= 0 )
5323 m_selection
->SelectCol( col
,
5324 event
.ControlDown(),
5333 // default label to suppress warnings about "enumeration value
5334 // 'xxx' not handled in switch
5342 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5347 if (m_colLabelWin
->HasCapture())
5348 m_colLabelWin
->ReleaseMouse();
5349 m_isDragging
= false;
5352 // ------------ Entering or leaving the window
5354 if ( event
.Entering() || event
.Leaving() )
5356 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5359 // ------------ Left button pressed
5361 else if ( event
.LeftDown() )
5363 // don't send a label click event for a hit on the
5364 // edge of the col label - this is probably the user
5365 // wanting to resize the col
5367 if ( XToEdgeOfCol(x
) < 0 )
5371 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5373 if ( !event
.ShiftDown() && !event
.CmdDown() )
5377 if ( event
.ShiftDown() )
5379 m_selection
->SelectBlock( 0,
5380 m_currentCellCoords
.GetCol(),
5381 GetNumberRows() - 1, col
,
5382 event
.ControlDown(),
5389 m_selection
->SelectCol( col
,
5390 event
.ControlDown(),
5397 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5402 // starting to drag-resize a col
5404 if ( CanDragColSize() )
5405 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5409 // ------------ Left double click
5411 if ( event
.LeftDClick() )
5413 col
= XToEdgeOfCol(x
);
5418 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5420 // no default action at the moment
5425 // adjust column width depending on label text
5426 AutoSizeColLabelSize( col
);
5428 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5433 // ------------ Left button released
5435 else if ( event
.LeftUp() )
5437 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5439 DoEndDragResizeCol();
5441 // Note: we are ending the event *after* doing
5442 // default processing in this case
5444 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5447 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5451 // ------------ Right button down
5453 else if ( event
.RightDown() )
5457 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5459 // no default action at the moment
5463 // ------------ Right double click
5465 else if ( event
.RightDClick() )
5469 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5471 // no default action at the moment
5475 // ------------ No buttons down and mouse moving
5477 else if ( event
.Moving() )
5479 m_dragRowOrCol
= XToEdgeOfCol( x
);
5480 if ( m_dragRowOrCol
>= 0 )
5482 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5484 // don't capture the cursor yet
5485 if ( CanDragColSize() )
5486 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5489 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5491 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5496 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5498 if ( event
.LeftDown() )
5500 // indicate corner label by having both row and
5503 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5508 else if ( event
.LeftDClick() )
5510 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5512 else if ( event
.RightDown() )
5514 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5516 // no default action at the moment
5519 else if ( event
.RightDClick() )
5521 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5523 // no default action at the moment
5528 void wxGrid::ChangeCursorMode(CursorMode mode
,
5533 static const wxChar
*cursorModes
[] =
5542 wxLogTrace(_T("grid"),
5543 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5544 win
== m_colLabelWin
? _T("colLabelWin")
5545 : win
? _T("rowLabelWin")
5547 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5550 if ( mode
== m_cursorMode
&&
5551 win
== m_winCapture
&&
5552 captureMouse
== (m_winCapture
!= NULL
))
5557 // by default use the grid itself
5563 if (m_winCapture
->HasCapture())
5564 m_winCapture
->ReleaseMouse();
5565 m_winCapture
= (wxWindow
*)NULL
;
5568 m_cursorMode
= mode
;
5570 switch ( m_cursorMode
)
5572 case WXGRID_CURSOR_RESIZE_ROW
:
5573 win
->SetCursor( m_rowResizeCursor
);
5576 case WXGRID_CURSOR_RESIZE_COL
:
5577 win
->SetCursor( m_colResizeCursor
);
5581 win
->SetCursor( *wxSTANDARD_CURSOR
);
5585 // we need to capture mouse when resizing
5586 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5587 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5589 if ( captureMouse
&& resize
)
5591 win
->CaptureMouse();
5596 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5599 wxPoint
pos( event
.GetPosition() );
5600 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5602 wxGridCellCoords coords
;
5603 XYToCell( x
, y
, coords
);
5605 int cell_rows
, cell_cols
;
5606 bool isFirstDrag
= !m_isDragging
;
5607 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5608 if ((cell_rows
< 0) || (cell_cols
< 0))
5610 coords
.SetRow(coords
.GetRow() + cell_rows
);
5611 coords
.SetCol(coords
.GetCol() + cell_cols
);
5614 if ( event
.Dragging() )
5616 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5618 // Don't start doing anything until the mouse has been dragged at
5619 // least 3 pixels in any direction...
5622 if (m_startDragPos
== wxDefaultPosition
)
5624 m_startDragPos
= pos
;
5627 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5631 m_isDragging
= true;
5632 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5634 // Hide the edit control, so it
5635 // won't interfere with drag-shrinking.
5636 if ( IsCellEditControlShown() )
5638 HideCellEditControl();
5639 SaveEditControlValue();
5642 // Have we captured the mouse yet?
5645 m_winCapture
= m_gridWin
;
5646 m_winCapture
->CaptureMouse();
5649 if ( coords
!= wxGridNoCellCoords
)
5651 if ( event
.CmdDown() )
5653 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5654 m_selectingKeyboard
= coords
;
5655 HighlightBlock( m_selectingKeyboard
, coords
);
5657 else if ( CanDragCell() )
5661 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5662 m_selectingKeyboard
= coords
;
5664 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5672 if ( !IsSelection() )
5674 HighlightBlock( coords
, coords
);
5678 HighlightBlock( m_currentCellCoords
, coords
);
5682 if (! IsVisible(coords
))
5684 MakeCellVisible(coords
);
5685 // TODO: need to introduce a delay or something here. The
5686 // scrolling is way to fast, at least on MSW - also on GTK.
5690 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5692 int cw
, ch
, left
, dummy
;
5693 m_gridWin
->GetClientSize( &cw
, &ch
);
5694 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5696 wxClientDC
dc( m_gridWin
);
5698 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5699 GetRowMinimalHeight(m_dragRowOrCol
) );
5700 dc
.SetLogicalFunction(wxINVERT
);
5701 if ( m_dragLastPos
>= 0 )
5703 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5705 dc
.DrawLine( left
, y
, left
+cw
, y
);
5708 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5710 int cw
, ch
, dummy
, top
;
5711 m_gridWin
->GetClientSize( &cw
, &ch
);
5712 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5714 wxClientDC
dc( m_gridWin
);
5716 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5717 GetColMinimalWidth(m_dragRowOrCol
) );
5718 dc
.SetLogicalFunction(wxINVERT
);
5719 if ( m_dragLastPos
>= 0 )
5721 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5723 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5730 m_isDragging
= false;
5731 m_startDragPos
= wxDefaultPosition
;
5733 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5734 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5737 if ( event
.Entering() || event
.Leaving() )
5739 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5740 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5745 // ------------ Left button pressed
5747 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5749 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5754 if ( !event
.CmdDown() )
5756 if ( event
.ShiftDown() )
5760 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5761 m_currentCellCoords
.GetCol(),
5764 event
.ControlDown(),
5770 else if ( XToEdgeOfCol(x
) < 0 &&
5771 YToEdgeOfRow(y
) < 0 )
5773 DisableCellEditControl();
5774 MakeCellVisible( coords
);
5776 if ( event
.CmdDown() )
5780 m_selection
->ToggleCellSelection( coords
.GetRow(),
5782 event
.ControlDown(),
5787 m_selectingTopLeft
= wxGridNoCellCoords
;
5788 m_selectingBottomRight
= wxGridNoCellCoords
;
5789 m_selectingKeyboard
= coords
;
5793 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5794 SetCurrentCell( coords
);
5797 if ( m_selection
->GetSelectionMode() !=
5798 wxGrid::wxGridSelectCells
)
5800 HighlightBlock( coords
, coords
);
5808 // ------------ Left double click
5810 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5812 DisableCellEditControl();
5814 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5816 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5821 // we want double click to select a cell and start editing
5822 // (i.e. to behave in same way as sequence of two slow clicks):
5823 m_waitForSlowClick
= true;
5828 // ------------ Left button released
5830 else if ( event
.LeftUp() )
5832 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5836 if (m_winCapture
->HasCapture())
5837 m_winCapture
->ReleaseMouse();
5838 m_winCapture
= NULL
;
5841 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
5844 EnableCellEditControl();
5846 wxGridCellAttr
*attr
= GetCellAttr(coords
);
5847 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5848 editor
->StartingClick();
5852 m_waitForSlowClick
= false;
5854 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5855 m_selectingBottomRight
!= wxGridNoCellCoords
)
5859 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5860 m_selectingTopLeft
.GetCol(),
5861 m_selectingBottomRight
.GetRow(),
5862 m_selectingBottomRight
.GetCol(),
5863 event
.ControlDown(),
5869 m_selectingTopLeft
= wxGridNoCellCoords
;
5870 m_selectingBottomRight
= wxGridNoCellCoords
;
5872 // Show the edit control, if it has been hidden for
5874 ShowCellEditControl();
5877 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5879 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5880 DoEndDragResizeRow();
5882 // Note: we are ending the event *after* doing
5883 // default processing in this case
5885 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5887 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5889 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5890 DoEndDragResizeCol();
5892 // Note: we are ending the event *after* doing
5893 // default processing in this case
5895 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5901 // ------------ Right button down
5903 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5905 DisableCellEditControl();
5906 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5911 // no default action at the moment
5915 // ------------ Right double click
5917 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5919 DisableCellEditControl();
5920 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5925 // no default action at the moment
5929 // ------------ Moving and no button action
5931 else if ( event
.Moving() && !event
.IsButton() )
5933 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5935 // out of grid cell area
5936 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5940 int dragRow
= YToEdgeOfRow( y
);
5941 int dragCol
= XToEdgeOfCol( x
);
5943 // Dragging on the corner of a cell to resize in both
5944 // directions is not implemented yet...
5946 if ( dragRow
>= 0 && dragCol
>= 0 )
5948 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5954 m_dragRowOrCol
= dragRow
;
5956 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5958 if ( CanDragRowSize() && CanDragGridSize() )
5959 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5962 else if ( dragCol
>= 0 )
5964 m_dragRowOrCol
= dragCol
;
5966 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5968 if ( CanDragColSize() && CanDragGridSize() )
5969 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5972 else // Neither on a row or col edge
5974 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5976 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5982 void wxGrid::DoEndDragResizeRow()
5984 if ( m_dragLastPos
>= 0 )
5986 // erase the last line and resize the row
5988 int cw
, ch
, left
, dummy
;
5989 m_gridWin
->GetClientSize( &cw
, &ch
);
5990 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5992 wxClientDC
dc( m_gridWin
);
5994 dc
.SetLogicalFunction( wxINVERT
);
5995 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
5996 HideCellEditControl();
5997 SaveEditControlValue();
5999 int rowTop
= GetRowTop(m_dragRowOrCol
);
6000 SetRowSize( m_dragRowOrCol
,
6001 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6003 if ( !GetBatchCount() )
6005 // Only needed to get the correct rect.y:
6006 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6008 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6009 rect
.width
= m_rowLabelWidth
;
6010 rect
.height
= ch
- rect
.y
;
6011 m_rowLabelWin
->Refresh( true, &rect
);
6014 // if there is a multicell block, paint all of it
6017 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6018 int leftCol
= XToCol(left
);
6019 int rightCol
= internalXToCol(left
+ cw
);
6022 for (i
=leftCol
; i
<rightCol
; i
++)
6024 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6025 if (cell_rows
< subtract_rows
)
6026 subtract_rows
= cell_rows
;
6028 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6029 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6030 rect
.height
= ch
- rect
.y
;
6033 m_gridWin
->Refresh( false, &rect
);
6036 ShowCellEditControl();
6041 void wxGrid::DoEndDragResizeCol()
6043 if ( m_dragLastPos
>= 0 )
6045 // erase the last line and resize the col
6047 int cw
, ch
, dummy
, top
;
6048 m_gridWin
->GetClientSize( &cw
, &ch
);
6049 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6051 wxClientDC
dc( m_gridWin
);
6053 dc
.SetLogicalFunction( wxINVERT
);
6054 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6055 HideCellEditControl();
6056 SaveEditControlValue();
6058 int colLeft
= GetColLeft(m_dragRowOrCol
);
6059 SetColSize( m_dragRowOrCol
,
6060 wxMax( m_dragLastPos
- colLeft
,
6061 GetColMinimalWidth(m_dragRowOrCol
) ) );
6063 if ( !GetBatchCount() )
6065 // Only needed to get the correct rect.x:
6066 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6068 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6069 rect
.width
= cw
- rect
.x
;
6070 rect
.height
= m_colLabelHeight
;
6071 m_colLabelWin
->Refresh( true, &rect
);
6074 // if there is a multicell block, paint all of it
6077 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6078 int topRow
= YToRow(top
);
6079 int bottomRow
= internalYToRow(top
+ cw
);
6082 for (i
=topRow
; i
<bottomRow
; i
++)
6084 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6085 if (cell_cols
< subtract_cols
)
6086 subtract_cols
= cell_cols
;
6089 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6090 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6091 rect
.width
= cw
- rect
.x
;
6095 m_gridWin
->Refresh( false, &rect
);
6098 ShowCellEditControl();
6103 // ------ interaction with data model
6105 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6107 switch ( msg
.GetId() )
6109 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6110 return GetModelValues();
6112 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6113 return SetModelValues();
6115 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6116 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6117 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6118 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6119 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6120 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6121 return Redimension( msg
);
6128 // The behaviour of this function depends on the grid table class
6129 // Clear() function. For the default wxGridStringTable class the
6130 // behavious is to replace all cell contents with wxEmptyString but
6131 // not to change the number of rows or cols.
6133 void wxGrid::ClearGrid()
6137 if (IsCellEditControlEnabled())
6138 DisableCellEditControl();
6141 if (!GetBatchCount())
6142 m_gridWin
->Refresh();
6146 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6148 // TODO: something with updateLabels flag
6152 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6158 if (IsCellEditControlEnabled())
6159 DisableCellEditControl();
6161 bool done
= m_table
->InsertRows( pos
, numRows
);
6164 // the table will have sent the results of the insert row
6165 // operation to this view object as a grid table message
6171 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6173 // TODO: something with updateLabels flag
6177 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6183 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6186 // the table will have sent the results of the append row
6187 // operation to this view object as a grid table message
6193 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6195 // TODO: something with updateLabels flag
6199 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6205 if (IsCellEditControlEnabled())
6206 DisableCellEditControl();
6208 bool done
= m_table
->DeleteRows( pos
, numRows
);
6210 // the table will have sent the results of the delete row
6211 // operation to this view object as a grid table message
6217 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6219 // TODO: something with updateLabels flag
6223 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6229 if (IsCellEditControlEnabled())
6230 DisableCellEditControl();
6232 bool done
= m_table
->InsertCols( pos
, numCols
);
6234 // the table will have sent the results of the insert col
6235 // operation to this view object as a grid table message
6241 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6243 // TODO: something with updateLabels flag
6247 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6253 bool done
= m_table
->AppendCols( numCols
);
6255 // the table will have sent the results of the append col
6256 // operation to this view object as a grid table message
6262 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6264 // TODO: something with updateLabels flag
6268 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6274 if (IsCellEditControlEnabled())
6275 DisableCellEditControl();
6277 bool done
= m_table
->DeleteCols( pos
, numCols
);
6279 // the table will have sent the results of the delete col
6280 // operation to this view object as a grid table message
6287 // ----- event handlers
6290 // Generate a grid event based on a mouse event and
6291 // return the result of ProcessEvent()
6293 int wxGrid::SendEvent( const wxEventType type
,
6295 wxMouseEvent
& mouseEv
)
6297 bool claimed
, vetoed
;
6299 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6301 int rowOrCol
= (row
== -1 ? col
: row
);
6303 wxGridSizeEvent
gridEvt( GetId(),
6307 mouseEv
.GetX() + GetRowLabelSize(),
6308 mouseEv
.GetY() + GetColLabelSize(),
6309 mouseEv
.ControlDown(),
6310 mouseEv
.ShiftDown(),
6312 mouseEv
.MetaDown() );
6314 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6315 vetoed
= !gridEvt
.IsAllowed();
6317 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6319 // Right now, it should _never_ end up here!
6320 wxGridRangeSelectEvent
gridEvt( GetId(),
6324 m_selectingBottomRight
,
6326 mouseEv
.ControlDown(),
6327 mouseEv
.ShiftDown(),
6329 mouseEv
.MetaDown() );
6331 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6332 vetoed
= !gridEvt
.IsAllowed();
6336 wxGridEvent
gridEvt( GetId(),
6340 mouseEv
.GetX() + GetRowLabelSize(),
6341 mouseEv
.GetY() + GetColLabelSize(),
6343 mouseEv
.ControlDown(),
6344 mouseEv
.ShiftDown(),
6346 mouseEv
.MetaDown() );
6347 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6348 vetoed
= !gridEvt
.IsAllowed();
6351 // A Veto'd event may not be `claimed' so test this first
6355 return claimed
? 1 : 0;
6358 // Generate a grid event of specified type and return the result
6359 // of ProcessEvent().
6361 int wxGrid::SendEvent( const wxEventType type
,
6364 bool claimed
, vetoed
;
6366 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6368 int rowOrCol
= (row
== -1 ? col
: row
);
6370 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6372 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6373 vetoed
= !gridEvt
.IsAllowed();
6377 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6379 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6380 vetoed
= !gridEvt
.IsAllowed();
6383 // A Veto'd event may not be `claimed' so test this first
6387 return claimed
? 1 : 0;
6390 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6392 // needed to prevent zillions of paint events on MSW
6396 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6398 // Don't do anything if between Begin/EndBatch...
6399 // EndBatch() will do all this on the last nested one anyway.
6400 if (! GetBatchCount())
6402 // Refresh to get correct scrolled position:
6403 wxScrolledWindow::Refresh(eraseb
, rect
);
6407 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6408 int width_label
, width_cell
, height_label
, height_cell
;
6411 // Copy rectangle can get scroll offsets..
6412 rect_x
= rect
->GetX();
6413 rect_y
= rect
->GetY();
6414 rectWidth
= rect
->GetWidth();
6415 rectHeight
= rect
->GetHeight();
6417 width_label
= m_rowLabelWidth
- rect_x
;
6418 if (width_label
> rectWidth
)
6419 width_label
= rectWidth
;
6421 height_label
= m_colLabelHeight
- rect_y
;
6422 if (height_label
> rectHeight
)
6423 height_label
= rectHeight
;
6425 if (rect_x
> m_rowLabelWidth
)
6427 x
= rect_x
- m_rowLabelWidth
;
6428 width_cell
= rectWidth
;
6433 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6436 if (rect_y
> m_colLabelHeight
)
6438 y
= rect_y
- m_colLabelHeight
;
6439 height_cell
= rectHeight
;
6444 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6447 // Paint corner label part intersecting rect.
6448 if ( width_label
> 0 && height_label
> 0 )
6450 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6451 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6454 // Paint col labels part intersecting rect.
6455 if ( width_cell
> 0 && height_label
> 0 )
6457 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6458 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6461 // Paint row labels part intersecting rect.
6462 if ( width_label
> 0 && height_cell
> 0 )
6464 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6465 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6468 // Paint cell area part intersecting rect.
6469 if ( width_cell
> 0 && height_cell
> 0 )
6471 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6472 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6477 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6478 m_colLabelWin
->Refresh(eraseb
, NULL
);
6479 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6480 m_gridWin
->Refresh(eraseb
, NULL
);
6485 void wxGrid::OnSize( wxSizeEvent
& event
)
6487 // position the child windows
6490 // don't call CalcDimensions() from here, the base class handles the size
6495 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6497 if ( m_inOnKeyDown
)
6499 // shouldn't be here - we are going round in circles...
6501 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6504 m_inOnKeyDown
= true;
6506 // propagate the event up and see if it gets processed
6507 wxWindow
*parent
= GetParent();
6508 wxKeyEvent
keyEvt( event
);
6509 keyEvt
.SetEventObject( parent
);
6511 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6513 // try local handlers
6514 switch ( event
.GetKeyCode() )
6517 if ( event
.ControlDown() )
6518 MoveCursorUpBlock( event
.ShiftDown() );
6520 MoveCursorUp( event
.ShiftDown() );
6524 if ( event
.ControlDown() )
6525 MoveCursorDownBlock( event
.ShiftDown() );
6527 MoveCursorDown( event
.ShiftDown() );
6531 if ( event
.ControlDown() )
6532 MoveCursorLeftBlock( event
.ShiftDown() );
6534 MoveCursorLeft( event
.ShiftDown() );
6538 if ( event
.ControlDown() )
6539 MoveCursorRightBlock( event
.ShiftDown() );
6541 MoveCursorRight( event
.ShiftDown() );
6545 case WXK_NUMPAD_ENTER
:
6546 if ( event
.ControlDown() )
6548 event
.Skip(); // to let the edit control have the return
6552 if ( GetGridCursorRow() < GetNumberRows()-1 )
6554 MoveCursorDown( event
.ShiftDown() );
6558 // at the bottom of a column
6559 DisableCellEditControl();
6569 if (event
.ShiftDown())
6571 if ( GetGridCursorCol() > 0 )
6573 MoveCursorLeft( false );
6578 DisableCellEditControl();
6583 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6585 MoveCursorRight( false );
6590 DisableCellEditControl();
6596 if ( event
.ControlDown() )
6598 MakeCellVisible( 0, 0 );
6599 SetCurrentCell( 0, 0 );
6608 if ( event
.ControlDown() )
6610 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
6611 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
6628 if ( event
.ControlDown() )
6632 m_selection
->ToggleCellSelection(
6633 m_currentCellCoords
.GetRow(),
6634 m_currentCellCoords
.GetCol(),
6635 event
.ControlDown(),
6643 if ( !IsEditable() )
6644 MoveCursorRight( false );
6655 m_inOnKeyDown
= false;
6658 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6660 // try local handlers
6662 if ( event
.GetKeyCode() == WXK_SHIFT
)
6664 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6665 m_selectingBottomRight
!= wxGridNoCellCoords
)
6669 m_selection
->SelectBlock(
6670 m_selectingTopLeft
.GetRow(),
6671 m_selectingTopLeft
.GetCol(),
6672 m_selectingBottomRight
.GetRow(),
6673 m_selectingBottomRight
.GetCol(),
6674 event
.ControlDown(),
6681 m_selectingTopLeft
= wxGridNoCellCoords
;
6682 m_selectingBottomRight
= wxGridNoCellCoords
;
6683 m_selectingKeyboard
= wxGridNoCellCoords
;
6687 void wxGrid::OnChar( wxKeyEvent
& event
)
6689 // is it possible to edit the current cell at all?
6690 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6692 // yes, now check whether the cells editor accepts the key
6693 int row
= m_currentCellCoords
.GetRow();
6694 int col
= m_currentCellCoords
.GetCol();
6695 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6696 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6698 // <F2> is special and will always start editing, for
6699 // other keys - ask the editor itself
6700 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6701 || editor
->IsAcceptedKey(event
) )
6703 // ensure cell is visble
6704 MakeCellVisible(row
, col
);
6705 EnableCellEditControl();
6707 // a problem can arise if the cell is not completely
6708 // visible (even after calling MakeCellVisible the
6709 // control is not created and calling StartingKey will
6711 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6712 editor
->StartingKey(event
);
6728 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6732 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6734 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6736 // the event has been intercepted - do nothing
6740 wxClientDC
dc( m_gridWin
);
6743 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6745 DisableCellEditControl();
6747 if ( IsVisible( m_currentCellCoords
, false ) )
6750 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
6751 if ( !m_gridLinesEnabled
)
6759 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6761 // Otherwise refresh redraws the highlight!
6762 m_currentCellCoords
= coords
;
6764 DrawGridCellArea( dc
, cells
);
6765 DrawAllGridLines( dc
, r
);
6769 m_currentCellCoords
= coords
;
6771 wxGridCellAttr
*attr
= GetCellAttr( coords
);
6772 DrawCellHighlight( dc
, attr
);
6776 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6779 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6783 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6786 rightCol
= GetNumberCols() - 1;
6788 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6791 bottomRow
= GetNumberRows() - 1;
6795 if ( topRow
> bottomRow
)
6802 if ( leftCol
> rightCol
)
6809 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6810 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6812 // First the case that we selected a completely new area
6813 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6814 m_selectingBottomRight
== wxGridNoCellCoords
)
6817 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6818 wxGridCellCoords ( bottomRow
, rightCol
) );
6819 m_gridWin
->Refresh( false, &rect
);
6822 // Now handle changing an existing selection area.
6823 else if ( m_selectingTopLeft
!= updateTopLeft
||
6824 m_selectingBottomRight
!= updateBottomRight
)
6826 // Compute two optimal update rectangles:
6827 // Either one rectangle is a real subset of the
6828 // other, or they are (almost) disjoint!
6830 bool need_refresh
[4];
6834 need_refresh
[3] = false;
6837 // Store intermediate values
6838 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6839 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6840 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6841 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6843 // Determine the outer/inner coordinates.
6844 if (oldLeft
> leftCol
)
6850 if (oldTop
> topRow
)
6856 if (oldRight
< rightCol
)
6859 oldRight
= rightCol
;
6862 if (oldBottom
< bottomRow
)
6865 oldBottom
= bottomRow
;
6869 // Now, either the stuff marked old is the outer
6870 // rectangle or we don't have a situation where one
6871 // is contained in the other.
6873 if ( oldLeft
< leftCol
)
6875 // Refresh the newly selected or deselected
6876 // area to the left of the old or new selection.
6877 need_refresh
[0] = true;
6878 rect
[0] = BlockToDeviceRect(
6879 wxGridCellCoords( oldTop
, oldLeft
),
6880 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
6883 if ( oldTop
< topRow
)
6885 // Refresh the newly selected or deselected
6886 // area above the old or new selection.
6887 need_refresh
[1] = true;
6888 rect
[1] = BlockToDeviceRect(
6889 wxGridCellCoords( oldTop
, leftCol
),
6890 wxGridCellCoords( topRow
- 1, rightCol
) );
6893 if ( oldRight
> rightCol
)
6895 // Refresh the newly selected or deselected
6896 // area to the right of the old or new selection.
6897 need_refresh
[2] = true;
6898 rect
[2] = BlockToDeviceRect(
6899 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
6900 wxGridCellCoords( oldBottom
, oldRight
) );
6903 if ( oldBottom
> bottomRow
)
6905 // Refresh the newly selected or deselected
6906 // area below the old or new selection.
6907 need_refresh
[3] = true;
6908 rect
[3] = BlockToDeviceRect(
6909 wxGridCellCoords( bottomRow
+ 1, leftCol
),
6910 wxGridCellCoords( oldBottom
, rightCol
) );
6913 // various Refresh() calls
6914 for (i
= 0; i
< 4; i
++ )
6915 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6916 m_gridWin
->Refresh( false, &(rect
[i
]) );
6920 m_selectingTopLeft
= updateTopLeft
;
6921 m_selectingBottomRight
= updateBottomRight
;
6925 // ------ functions to get/send data (see also public functions)
6928 bool wxGrid::GetModelValues()
6930 // Hide the editor, so it won't hide a changed value.
6931 HideCellEditControl();
6935 // all we need to do is repaint the grid
6937 m_gridWin
->Refresh();
6944 bool wxGrid::SetModelValues()
6948 // Disable the editor, so it won't hide a changed value.
6949 // Do we also want to save the current value of the editor first?
6951 DisableCellEditControl();
6955 for ( row
= 0; row
< m_numRows
; row
++ )
6957 for ( col
= 0; col
< m_numCols
; col
++ )
6959 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6969 // Note - this function only draws cells that are in the list of
6970 // exposed cells (usually set from the update region by
6971 // CalcExposedCells)
6973 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6975 if ( !m_numRows
|| !m_numCols
)
6978 int i
, numCells
= cells
.GetCount();
6979 int row
, col
, cell_rows
, cell_cols
;
6980 wxGridCellCoordsArray redrawCells
;
6982 for ( i
= numCells
- 1; i
>= 0; i
-- )
6984 row
= cells
[i
].GetRow();
6985 col
= cells
[i
].GetCol();
6986 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6988 // If this cell is part of a multicell block, find owner for repaint
6989 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6991 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
6992 bool marked
= false;
6993 for ( int j
= 0; j
< numCells
; j
++ )
6995 if ( cell
== cells
[j
] )
7004 int count
= redrawCells
.GetCount();
7005 for (int j
= 0; j
< count
; j
++)
7007 if ( cell
== redrawCells
[j
] )
7015 redrawCells
.Add( cell
);
7018 // don't bother drawing this cell
7022 // If this cell is empty, find cell to left that might want to overflow
7023 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7025 for ( int l
= 0; l
< cell_rows
; l
++ )
7027 // find a cell in this row to leave already marked for repaint
7029 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7030 if ((redrawCells
[k
].GetCol() < left
) &&
7031 (redrawCells
[k
].GetRow() == row
))
7033 left
= redrawCells
[k
].GetCol();
7037 left
= 0; // oh well
7039 for (int j
= col
- 1; j
>= left
; j
--)
7041 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7043 if (GetCellOverflow(row
+ l
, j
))
7045 wxGridCellCoords
cell(row
+ l
, j
);
7046 bool marked
= false;
7048 for (int k
= 0; k
< numCells
; k
++)
7050 if ( cell
== cells
[k
] )
7059 int count
= redrawCells
.GetCount();
7060 for (int k
= 0; k
< count
; k
++)
7062 if ( cell
== redrawCells
[k
] )
7069 redrawCells
.Add( cell
);
7078 DrawCell( dc
, cells
[i
] );
7081 numCells
= redrawCells
.GetCount();
7083 for ( i
= numCells
- 1; i
>= 0; i
-- )
7085 DrawCell( dc
, redrawCells
[i
] );
7089 void wxGrid::DrawGridSpace( wxDC
& dc
)
7092 m_gridWin
->GetClientSize( &cw
, &ch
);
7095 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7097 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
7098 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7100 if ( right
> rightCol
|| bottom
> bottomRow
)
7103 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7105 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7106 dc
.SetPen( *wxTRANSPARENT_PEN
);
7108 if ( right
> rightCol
)
7110 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7113 if ( bottom
> bottomRow
)
7115 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7120 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7122 int row
= coords
.GetRow();
7123 int col
= coords
.GetCol();
7125 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7128 // we draw the cell border ourselves
7129 #if !WXGRID_DRAW_LINES
7130 if ( m_gridLinesEnabled
)
7131 DrawCellBorder( dc
, coords
);
7134 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7136 bool isCurrent
= coords
== m_currentCellCoords
;
7138 wxRect rect
= CellToRect( row
, col
);
7140 // if the editor is shown, we should use it and not the renderer
7141 // Note: However, only if it is really _shown_, i.e. not hidden!
7142 if ( isCurrent
&& IsCellEditControlShown() )
7144 // NB: this "#if..." is temporary and fixes a problem where the
7145 // edit control is erased by this code after being rendered.
7146 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7147 // implicitly, causing this out-of order render.
7148 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7149 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7150 editor
->PaintBackground(rect
, attr
);
7156 // but all the rest is drawn by the cell renderer and hence may be customized
7157 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7158 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7165 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7167 int row
= m_currentCellCoords
.GetRow();
7168 int col
= m_currentCellCoords
.GetCol();
7170 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7173 wxRect rect
= CellToRect(row
, col
);
7175 // hmmm... what could we do here to show that the cell is disabled?
7176 // for now, I just draw a thinner border than for the other ones, but
7177 // it doesn't look really good
7179 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7183 // The center of the drawn line is where the position/width/height of
7184 // the rectangle is actually at (on wxMSW at least), so the
7185 // size of the rectangle is reduced to compensate for the thickness of
7186 // the line. If this is too strange on non-wxMSW platforms then
7187 // please #ifdef this appropriately.
7188 rect
.x
+= penWidth
/ 2;
7189 rect
.y
+= penWidth
/ 2;
7190 rect
.width
-= penWidth
- 1;
7191 rect
.height
-= penWidth
- 1;
7193 // Now draw the rectangle
7194 // use the cellHighlightColour if the cell is inside a selection, this
7195 // will ensure the cell is always visible.
7196 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7197 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7198 dc
.DrawRectangle(rect
);
7202 // VZ: my experiments with 3D borders...
7204 // how to properly set colours for arbitrary bg?
7205 wxCoord x1
= rect
.x
,
7207 x2
= rect
.x
+ rect
.width
- 1,
7208 y2
= rect
.y
+ rect
.height
- 1;
7210 dc
.SetPen(*wxWHITE_PEN
);
7211 dc
.DrawLine(x1
, y1
, x2
, y1
);
7212 dc
.DrawLine(x1
, y1
, x1
, y2
);
7214 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7215 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7217 dc
.SetPen(*wxBLACK_PEN
);
7218 dc
.DrawLine(x1
, y2
, x2
, y2
);
7219 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7223 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7225 int row
= coords
.GetRow();
7226 int col
= coords
.GetCol();
7227 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7230 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7232 wxRect rect
= CellToRect( row
, col
);
7234 // right hand border
7235 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7236 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7239 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7240 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7243 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7245 // This if block was previously in wxGrid::OnPaint but that doesn't
7246 // seem to get called under wxGTK - MB
7248 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7249 m_numRows
&& m_numCols
)
7251 m_currentCellCoords
.Set(0, 0);
7254 if ( IsCellEditControlShown() )
7256 // don't show highlight when the edit control is shown
7260 // if the active cell was repainted, repaint its highlight too because it
7261 // might have been damaged by the grid lines
7262 size_t count
= cells
.GetCount();
7263 for ( size_t n
= 0; n
< count
; n
++ )
7265 if ( cells
[n
] == m_currentCellCoords
)
7267 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7268 DrawCellHighlight(dc
, attr
);
7276 // TODO: remove this ???
7277 // This is used to redraw all grid lines e.g. when the grid line colour
7280 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7282 #if !WXGRID_DRAW_LINES
7286 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7289 int top
, bottom
, left
, right
;
7291 #if 0 //#ifndef __WXGTK__
7295 m_gridWin
->GetClientSize(&cw
, &ch
);
7297 // virtual coords of visible area
7299 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7300 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7305 reg
.GetBox(x
, y
, w
, h
);
7306 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7307 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7311 m_gridWin
->GetClientSize(&cw
, &ch
);
7312 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7313 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7316 // avoid drawing grid lines past the last row and col
7318 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7319 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7321 // no gridlines inside multicells, clip them out
7322 int leftCol
= internalXToCol(left
);
7323 int topRow
= internalYToRow(top
);
7324 int rightCol
= internalXToCol(right
);
7325 int bottomRow
= internalYToRow(bottom
);
7328 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7329 wxRegion
clippedcells(0, 0, cw
, ch
);
7331 int i
, j
, cell_rows
, cell_cols
;
7334 for (j
=topRow
; j
<bottomRow
; j
++)
7336 for (i
=leftCol
; i
<rightCol
; i
++)
7338 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7339 if ((cell_rows
> 1) || (cell_cols
> 1))
7341 rect
= CellToRect(j
,i
);
7342 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7343 clippedcells
.Subtract(rect
);
7345 else if ((cell_rows
< 0) || (cell_cols
< 0))
7347 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7348 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7349 clippedcells
.Subtract(rect
);
7354 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7356 int i
, j
, cell_rows
, cell_cols
;
7359 for (j
=topRow
; j
<bottomRow
; j
++)
7361 for (i
=leftCol
; i
<rightCol
; i
++)
7363 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7364 if ((cell_rows
> 1) || (cell_cols
> 1))
7366 rect
= CellToRect(j
, i
);
7367 clippedcells
.Subtract(rect
);
7369 else if ((cell_rows
< 0) || (cell_cols
< 0))
7371 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7372 clippedcells
.Subtract(rect
);
7378 dc
.SetClippingRegion( clippedcells
);
7380 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7382 // horizontal grid lines
7384 // already declared above - int i;
7385 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7387 int bot
= GetRowBottom(i
) - 1;
7396 dc
.DrawLine( left
, bot
, right
, bot
);
7400 // vertical grid lines
7402 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7404 int colRight
= GetColRight(i
) - 1;
7405 if ( colRight
> right
)
7410 if ( colRight
>= left
)
7412 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7416 dc
.DestroyClippingRegion();
7419 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7425 size_t numLabels
= rows
.GetCount();
7427 for ( i
= 0; i
< numLabels
; i
++ )
7429 DrawRowLabel( dc
, rows
[i
] );
7433 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7435 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7442 rect
.SetY( GetRowTop(row
) + 1 );
7443 rect
.SetWidth( m_rowLabelWidth
- 2 );
7444 rect
.SetHeight( GetRowHeight(row
) - 2 );
7446 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7448 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7450 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7452 int rowTop
= GetRowTop(row
),
7453 rowBottom
= GetRowBottom(row
) - 1;
7455 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
), 1, wxSOLID
) );
7456 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7457 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7458 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7460 dc
.SetPen( *wxWHITE_PEN
);
7461 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7462 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7465 dc
.SetBackgroundMode( wxTRANSPARENT
);
7466 dc
.SetTextForeground( GetLabelTextColour() );
7467 dc
.SetFont( GetLabelFont() );
7470 GetRowLabelAlignment( &hAlign
, &vAlign
);
7473 rect
.SetY( GetRowTop(row
) + 2 );
7474 rect
.SetWidth( m_rowLabelWidth
- 4 );
7475 rect
.SetHeight( GetRowHeight(row
) - 4 );
7476 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7479 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7485 size_t numLabels
= cols
.GetCount();
7487 for ( i
= 0; i
< numLabels
; i
++ )
7489 DrawColLabel( dc
, cols
[i
] );
7493 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7495 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7498 int colLeft
= GetColLeft(col
);
7503 rect
.SetX( colLeft
+ 1 );
7505 rect
.SetWidth( GetColWidth(col
) - 2 );
7506 rect
.SetHeight( m_colLabelHeight
- 2 );
7508 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7510 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7512 int colRight
= GetColRight(col
) - 1;
7514 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
), 1, wxSOLID
) );
7515 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7516 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7517 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7518 colRight
+ 1, m_colLabelHeight
- 1 );
7520 dc
.SetPen( *wxWHITE_PEN
);
7521 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7522 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7525 dc
.SetBackgroundMode( wxTRANSPARENT
);
7526 dc
.SetTextForeground( GetLabelTextColour() );
7527 dc
.SetFont( GetLabelFont() );
7529 int hAlign
, vAlign
, orient
;
7530 GetColLabelAlignment( &hAlign
, &vAlign
);
7531 orient
= GetColLabelTextOrientation();
7533 rect
.SetX( colLeft
+ 2 );
7535 rect
.SetWidth( GetColWidth(col
) - 4 );
7536 rect
.SetHeight( m_colLabelHeight
- 4 );
7537 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7540 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7541 const wxString
& value
,
7545 int textOrientation
)
7547 wxArrayString lines
;
7549 StringToLines( value
, lines
);
7551 // Forward to new API.
7552 DrawTextRectangle( dc
,
7560 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
7561 // add textOrientation support
7562 void wxGrid::DrawTextRectangle(wxDC
& dc
,
7563 const wxArrayString
& lines
,
7567 int textOrientation
)
7569 if ( lines
.empty() )
7572 wxDCClipper
clip(dc
, rect
);
7577 if ( textOrientation
== wxHORIZONTAL
)
7578 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
7580 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7584 switch ( vertAlign
)
7586 case wxALIGN_BOTTOM
:
7587 if ( textOrientation
== wxHORIZONTAL
)
7588 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7590 x
= rect
.x
+ rect
.width
- textWidth
;
7593 case wxALIGN_CENTRE
:
7594 if ( textOrientation
== wxHORIZONTAL
)
7595 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
7597 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
7602 if ( textOrientation
== wxHORIZONTAL
)
7609 // Align each line of a multi-line label
7610 size_t nLines
= lines
.GetCount();
7611 for ( size_t l
= 0; l
< nLines
; l
++ )
7613 const wxString
& line
= lines
[l
];
7617 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
7623 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
7625 switch ( horizAlign
)
7628 if ( textOrientation
== wxHORIZONTAL
)
7629 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7631 y
= rect
.y
+ lineWidth
+ 1;
7634 case wxALIGN_CENTRE
:
7635 if ( textOrientation
== wxHORIZONTAL
)
7636 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
7638 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
7643 if ( textOrientation
== wxHORIZONTAL
)
7646 y
= rect
.y
+ rect
.height
- 1;
7650 if ( textOrientation
== wxHORIZONTAL
)
7652 dc
.DrawText( line
, x
, y
);
7657 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
7663 // Split multi-line text up into an array of strings.
7664 // Any existing contents of the string array are preserved.
7666 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7670 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7671 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7673 while ( startPos
< (int)tVal
.length() )
7675 pos
= tVal
.Mid(startPos
).Find( eol
);
7680 else if ( pos
== 0 )
7682 lines
.Add( wxEmptyString
);
7686 lines
.Add( value
.Mid(startPos
, pos
) );
7689 startPos
+= pos
+ 1;
7692 if ( startPos
< (int)value
.length() )
7694 lines
.Add( value
.Mid( startPos
) );
7698 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
7699 const wxArrayString
& lines
,
7700 long *width
, long *height
)
7704 long lineW
= 0, lineH
= 0;
7707 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7709 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7710 w
= wxMax( w
, lineW
);
7719 // ------ Batch processing.
7721 void wxGrid::EndBatch()
7723 if ( m_batchCount
> 0 )
7726 if ( !m_batchCount
)
7729 m_rowLabelWin
->Refresh();
7730 m_colLabelWin
->Refresh();
7731 m_cornerLabelWin
->Refresh();
7732 m_gridWin
->Refresh();
7737 // Use this, rather than wxWindow::Refresh(), to force an immediate
7738 // repainting of the grid. Has no effect if you are already inside a
7739 // BeginBatch / EndBatch block.
7741 void wxGrid::ForceRefresh()
7747 bool wxGrid::Enable(bool enable
)
7749 if ( !wxScrolledWindow::Enable(enable
) )
7752 // redraw in the new state
7753 m_gridWin
->Refresh();
7759 // ------ Edit control functions
7762 void wxGrid::EnableEditing( bool edit
)
7764 // TODO: improve this ?
7766 if ( edit
!= m_editable
)
7769 EnableCellEditControl(edit
);
7774 void wxGrid::EnableCellEditControl( bool enable
)
7779 if ( enable
!= m_cellEditCtrlEnabled
)
7783 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7786 // this should be checked by the caller!
7787 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
7789 // do it before ShowCellEditControl()
7790 m_cellEditCtrlEnabled
= enable
;
7792 ShowCellEditControl();
7796 //FIXME:add veto support
7797 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7799 HideCellEditControl();
7800 SaveEditControlValue();
7802 // do it after HideCellEditControl()
7803 m_cellEditCtrlEnabled
= enable
;
7808 bool wxGrid::IsCurrentCellReadOnly() const
7811 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7812 bool readonly
= attr
->IsReadOnly();
7818 bool wxGrid::CanEnableCellControl() const
7820 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7821 !IsCurrentCellReadOnly();
7824 bool wxGrid::IsCellEditControlEnabled() const
7826 // the cell edit control might be disable for all cells or just for the
7827 // current one if it's read only
7828 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7831 bool wxGrid::IsCellEditControlShown() const
7833 bool isShown
= false;
7835 if ( m_cellEditCtrlEnabled
)
7837 int row
= m_currentCellCoords
.GetRow();
7838 int col
= m_currentCellCoords
.GetCol();
7839 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7840 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7845 if ( editor
->IsCreated() )
7847 isShown
= editor
->GetControl()->IsShown();
7857 void wxGrid::ShowCellEditControl()
7859 if ( IsCellEditControlEnabled() )
7861 if ( !IsVisible( m_currentCellCoords
, false ) )
7863 m_cellEditCtrlEnabled
= false;
7868 wxRect rect
= CellToRect( m_currentCellCoords
);
7869 int row
= m_currentCellCoords
.GetRow();
7870 int col
= m_currentCellCoords
.GetCol();
7872 // if this is part of a multicell, find owner (topleft)
7873 int cell_rows
, cell_cols
;
7874 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7875 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7879 m_currentCellCoords
.SetRow( row
);
7880 m_currentCellCoords
.SetCol( col
);
7883 // convert to scrolled coords
7884 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7890 // performed in PaintBackground()
7892 // erase the highlight and the cell contents because the editor
7893 // might not cover the entire cell
7894 wxClientDC
dc( m_gridWin
);
7896 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7897 dc
.SetPen(*wxTRANSPARENT_PEN
);
7898 dc
.DrawRectangle(rect
);
7901 // cell is shifted by one pixel
7902 // However, don't allow x or y to become negative
7903 // since the SetSize() method interprets that as
7910 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7911 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7912 if ( !editor
->IsCreated() )
7914 editor
->Create(m_gridWin
, wxID_ANY
,
7915 new wxGridCellEditorEvtHandler(this, editor
));
7917 wxGridEditorCreatedEvent
evt(GetId(),
7918 wxEVT_GRID_EDITOR_CREATED
,
7922 editor
->GetControl());
7923 GetEventHandler()->ProcessEvent(evt
);
7926 // resize editor to overflow into righthand cells if allowed
7927 int maxWidth
= rect
.width
;
7928 wxString value
= GetCellValue(row
, col
);
7929 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7932 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
7933 if (maxWidth
< rect
.width
)
7934 maxWidth
= rect
.width
;
7937 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7938 if (rect
.x
+ maxWidth
> client_right
)
7939 maxWidth
= client_right
- rect
.x
;
7941 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7943 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7944 // may have changed earlier
7945 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
7948 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7950 // looks weird going over a multicell
7951 if (m_table
->IsEmptyCell( row
, i
) &&
7952 (rect
.width
< maxWidth
) && (c_rows
== 1))
7954 rect
.width
+= GetColWidth( i
);
7960 if (rect
.GetRight() > client_right
)
7961 rect
.SetRight( client_right
- 1 );
7964 editor
->SetCellAttr( attr
);
7965 editor
->SetSize( rect
);
7967 editor
->GetControl()->Move(
7968 editor
->GetControl()->GetPosition().x
+ nXMove
,
7969 editor
->GetControl()->GetPosition().y
);
7970 editor
->Show( true, attr
);
7972 // recalc dimensions in case we need to
7973 // expand the scrolled window to account for editor
7976 editor
->BeginEdit(row
, col
, this);
7977 editor
->SetCellAttr(NULL
);
7985 void wxGrid::HideCellEditControl()
7987 if ( IsCellEditControlEnabled() )
7989 int row
= m_currentCellCoords
.GetRow();
7990 int col
= m_currentCellCoords
.GetCol();
7992 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7993 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7994 editor
->Show( false );
7998 m_gridWin
->SetFocus();
8000 // refresh whole row to the right
8001 wxRect
rect( CellToRect(row
, col
) );
8002 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8003 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8006 // ensure that the pixels under the focus ring get refreshed as well
8007 rect
.Inflate(10, 10);
8010 m_gridWin
->Refresh( false, &rect
);
8014 void wxGrid::SaveEditControlValue()
8016 if ( IsCellEditControlEnabled() )
8018 int row
= m_currentCellCoords
.GetRow();
8019 int col
= m_currentCellCoords
.GetCol();
8021 wxString oldval
= GetCellValue(row
, col
);
8023 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8024 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8025 bool changed
= editor
->EndEdit(row
, col
, this);
8032 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8033 m_currentCellCoords
.GetRow(),
8034 m_currentCellCoords
.GetCol() ) < 0 )
8036 // Event has been vetoed, set the data back.
8037 SetCellValue(row
, col
, oldval
);
8044 // ------ Grid location functions
8045 // Note that all of these functions work with the logical coordinates of
8046 // grid cells and labels so you will need to convert from device
8047 // coordinates for mouse events etc.
8050 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
8052 int row
= YToRow(y
);
8053 int col
= XToCol(x
);
8055 if ( row
== -1 || col
== -1 )
8057 coords
= wxGridNoCellCoords
;
8061 coords
.Set( row
, col
);
8065 // Internal Helper function for computing row or column from some
8066 // (unscrolled) coordinate value, using either
8067 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8068 // of m_rowBottoms/m_ColRights to speed up the search!
8070 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8071 const wxArrayInt
& BorderArray
, int nMax
,
8075 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8080 size_t i_max
= coord
/ defaultDist
,
8083 if (BorderArray
.IsEmpty())
8085 if ((int) i_max
< nMax
)
8087 return clipToMinMax
? nMax
- 1 : -1;
8090 if ( i_max
>= BorderArray
.GetCount())
8092 i_max
= BorderArray
.GetCount() - 1;
8096 if ( coord
>= BorderArray
[i_max
])
8100 i_max
= coord
/ minDist
;
8102 i_max
= BorderArray
.GetCount() - 1;
8105 if ( i_max
>= BorderArray
.GetCount())
8106 i_max
= BorderArray
.GetCount() - 1;
8109 if ( coord
>= BorderArray
[i_max
])
8110 return clipToMinMax
? (int)i_max
: -1;
8111 if ( coord
< BorderArray
[0] )
8114 while ( i_max
- i_min
> 0 )
8116 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8117 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8118 if (coord
>= BorderArray
[ i_max
- 1])
8122 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8123 if (coord
< BorderArray
[median
])
8132 int wxGrid::YToRow( int y
)
8134 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8135 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8138 int wxGrid::XToCol( int x
)
8140 return CoordToRowOrCol(x
, m_defaultColWidth
,
8141 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8144 // return the row number that that the y coord is near the edge of, or
8145 // -1 if not near an edge
8147 int wxGrid::YToEdgeOfRow( int y
)
8150 i
= internalYToRow(y
);
8152 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8154 // We know that we are in row i, test whether we are
8155 // close enough to lower or upper border, respectively.
8156 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8158 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8165 // return the col number that that the x coord is near the edge of, or
8166 // -1 if not near an edge
8168 int wxGrid::XToEdgeOfCol( int x
)
8171 i
= internalXToCol(x
);
8173 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8175 // We know that we are in column i; test whether we are
8176 // close enough to right or left border, respectively.
8177 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8179 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8186 wxRect
wxGrid::CellToRect( int row
, int col
)
8188 wxRect
rect( -1, -1, -1, -1 );
8190 if ( row
>= 0 && row
< m_numRows
&&
8191 col
>= 0 && col
< m_numCols
)
8193 int i
, cell_rows
, cell_cols
;
8194 rect
.width
= rect
.height
= 0;
8195 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8196 // if negative then find multicell owner
8201 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8203 rect
.x
= GetColLeft(col
);
8204 rect
.y
= GetRowTop(row
);
8205 for (i
=col
; i
< col
+ cell_cols
; i
++)
8206 rect
.width
+= GetColWidth(i
);
8207 for (i
=row
; i
< row
+ cell_rows
; i
++)
8208 rect
.height
+= GetRowHeight(i
);
8211 // if grid lines are enabled, then the area of the cell is a bit smaller
8212 if (m_gridLinesEnabled
)
8221 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8223 // get the cell rectangle in logical coords
8225 wxRect
r( CellToRect( row
, col
) );
8227 // convert to device coords
8229 int left
, top
, right
, bottom
;
8230 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8231 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8233 // check against the client area of the grid window
8235 m_gridWin
->GetClientSize( &cw
, &ch
);
8237 if ( wholeCellVisible
)
8239 // is the cell wholly visible ?
8240 return ( left
>= 0 && right
<= cw
&&
8241 top
>= 0 && bottom
<= ch
);
8245 // is the cell partly visible ?
8247 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8248 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8252 // make the specified cell location visible by doing a minimal amount
8255 void wxGrid::MakeCellVisible( int row
, int col
)
8258 int xpos
= -1, ypos
= -1;
8260 if ( row
>= 0 && row
< m_numRows
&&
8261 col
>= 0 && col
< m_numCols
)
8263 // get the cell rectangle in logical coords
8264 wxRect
r( CellToRect( row
, col
) );
8266 // convert to device coords
8267 int left
, top
, right
, bottom
;
8268 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8269 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8272 m_gridWin
->GetClientSize( &cw
, &ch
);
8278 else if ( bottom
> ch
)
8280 int h
= r
.GetHeight();
8282 for ( i
= row
- 1; i
>= 0; i
-- )
8284 int rowHeight
= GetRowHeight(i
);
8285 if ( h
+ rowHeight
> ch
)
8292 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8293 // have rounding errors (this is important, because if we do,
8294 // we might not scroll at all and some cells won't be redrawn)
8296 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8297 // so just add a full scroll unit...
8298 ypos
+= m_scrollLineY
;
8301 // special handling for wide cells - show always left part of the cell!
8302 // Otherwise, e.g. when stepping from row to row, it would jump between
8303 // left and right part of the cell on every step!
8305 if ( left
< 0 || (right
- left
) >= cw
)
8309 else if ( right
> cw
)
8311 // position the view so that the cell is on the right
8313 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8314 xpos
= x0
+ (right
- cw
);
8316 // see comment for ypos above
8317 xpos
+= m_scrollLineX
;
8320 if ( xpos
!= -1 || ypos
!= -1 )
8323 xpos
/= m_scrollLineX
;
8325 ypos
/= m_scrollLineY
;
8326 Scroll( xpos
, ypos
);
8333 // ------ Grid cursor movement functions
8336 bool wxGrid::MoveCursorUp( bool expandSelection
)
8338 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8339 m_currentCellCoords
.GetRow() >= 0 )
8341 if ( expandSelection
)
8343 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8344 m_selectingKeyboard
= m_currentCellCoords
;
8345 if ( m_selectingKeyboard
.GetRow() > 0 )
8347 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8348 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8349 m_selectingKeyboard
.GetCol() );
8350 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8353 else if ( m_currentCellCoords
.GetRow() > 0 )
8355 int row
= m_currentCellCoords
.GetRow() - 1;
8356 int col
= m_currentCellCoords
.GetCol();
8358 MakeCellVisible( row
, col
);
8359 SetCurrentCell( row
, col
);
8370 bool wxGrid::MoveCursorDown( bool expandSelection
)
8372 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8373 m_currentCellCoords
.GetRow() < m_numRows
)
8375 if ( expandSelection
)
8377 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8378 m_selectingKeyboard
= m_currentCellCoords
;
8379 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8381 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8382 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8383 m_selectingKeyboard
.GetCol() );
8384 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8387 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8389 int row
= m_currentCellCoords
.GetRow() + 1;
8390 int col
= m_currentCellCoords
.GetCol();
8392 MakeCellVisible( row
, col
);
8393 SetCurrentCell( row
, col
);
8404 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8406 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8407 m_currentCellCoords
.GetCol() >= 0 )
8409 if ( expandSelection
)
8411 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8412 m_selectingKeyboard
= m_currentCellCoords
;
8413 if ( m_selectingKeyboard
.GetCol() > 0 )
8415 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8416 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8417 m_selectingKeyboard
.GetCol() );
8418 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8421 else if ( m_currentCellCoords
.GetCol() > 0 )
8423 int row
= m_currentCellCoords
.GetRow();
8424 int col
= m_currentCellCoords
.GetCol() - 1;
8426 MakeCellVisible( row
, col
);
8427 SetCurrentCell( row
, col
);
8438 bool wxGrid::MoveCursorRight( bool expandSelection
)
8440 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8441 m_currentCellCoords
.GetCol() < m_numCols
)
8443 if ( expandSelection
)
8445 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8446 m_selectingKeyboard
= m_currentCellCoords
;
8447 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8449 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8450 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8451 m_selectingKeyboard
.GetCol() );
8452 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8455 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8457 int row
= m_currentCellCoords
.GetRow();
8458 int col
= m_currentCellCoords
.GetCol() + 1;
8460 MakeCellVisible( row
, col
);
8461 SetCurrentCell( row
, col
);
8472 bool wxGrid::MovePageUp()
8474 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8477 int row
= m_currentCellCoords
.GetRow();
8481 m_gridWin
->GetClientSize( &cw
, &ch
);
8483 int y
= GetRowTop(row
);
8484 int newRow
= internalYToRow( y
- ch
+ 1 );
8486 if ( newRow
== row
)
8488 // row > 0, so newRow can never be less than 0 here.
8492 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8493 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8501 bool wxGrid::MovePageDown()
8503 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8506 int row
= m_currentCellCoords
.GetRow();
8507 if ( (row
+ 1) < m_numRows
)
8510 m_gridWin
->GetClientSize( &cw
, &ch
);
8512 int y
= GetRowTop(row
);
8513 int newRow
= internalYToRow( y
+ ch
);
8514 if ( newRow
== row
)
8516 // row < m_numRows, so newRow can't overflow here.
8520 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8521 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8529 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8532 m_currentCellCoords
!= wxGridNoCellCoords
&&
8533 m_currentCellCoords
.GetRow() > 0 )
8535 int row
= m_currentCellCoords
.GetRow();
8536 int col
= m_currentCellCoords
.GetCol();
8538 if ( m_table
->IsEmptyCell(row
, col
) )
8540 // starting in an empty cell: find the next block of
8546 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8550 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
8552 // starting at the top of a block: find the next block
8558 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8564 // starting within a block: find the top of the block
8569 if ( m_table
->IsEmptyCell(row
, col
) )
8577 MakeCellVisible( row
, col
);
8578 if ( expandSelection
)
8580 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8581 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8586 SetCurrentCell( row
, col
);
8595 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8598 m_currentCellCoords
!= wxGridNoCellCoords
&&
8599 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8601 int row
= m_currentCellCoords
.GetRow();
8602 int col
= m_currentCellCoords
.GetCol();
8604 if ( m_table
->IsEmptyCell(row
, col
) )
8606 // starting in an empty cell: find the next block of
8609 while ( row
< m_numRows
- 1 )
8612 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8616 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
8618 // starting at the bottom of a block: find the next block
8621 while ( row
< m_numRows
- 1 )
8624 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8630 // starting within a block: find the bottom of the block
8632 while ( row
< m_numRows
- 1 )
8635 if ( m_table
->IsEmptyCell(row
, col
) )
8643 MakeCellVisible( row
, col
);
8644 if ( expandSelection
)
8646 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8647 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8652 SetCurrentCell( row
, col
);
8661 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8664 m_currentCellCoords
!= wxGridNoCellCoords
&&
8665 m_currentCellCoords
.GetCol() > 0 )
8667 int row
= m_currentCellCoords
.GetRow();
8668 int col
= m_currentCellCoords
.GetCol();
8670 if ( m_table
->IsEmptyCell(row
, col
) )
8672 // starting in an empty cell: find the next block of
8678 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8682 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
8684 // starting at the left of a block: find the next block
8690 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8696 // starting within a block: find the left of the block
8701 if ( m_table
->IsEmptyCell(row
, col
) )
8709 MakeCellVisible( row
, col
);
8710 if ( expandSelection
)
8712 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8713 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8718 SetCurrentCell( row
, col
);
8727 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8730 m_currentCellCoords
!= wxGridNoCellCoords
&&
8731 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8733 int row
= m_currentCellCoords
.GetRow();
8734 int col
= m_currentCellCoords
.GetCol();
8736 if ( m_table
->IsEmptyCell(row
, col
) )
8738 // starting in an empty cell: find the next block of
8741 while ( col
< m_numCols
- 1 )
8744 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8748 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
8750 // starting at the right of a block: find the next block
8753 while ( col
< m_numCols
- 1 )
8756 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8762 // starting within a block: find the right of the block
8764 while ( col
< m_numCols
- 1 )
8767 if ( m_table
->IsEmptyCell(row
, col
) )
8775 MakeCellVisible( row
, col
);
8776 if ( expandSelection
)
8778 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8779 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8784 SetCurrentCell( row
, col
);
8794 // ------ Label values and formatting
8797 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8800 *horiz
= m_rowLabelHorizAlign
;
8802 *vert
= m_rowLabelVertAlign
;
8805 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8808 *horiz
= m_colLabelHorizAlign
;
8810 *vert
= m_colLabelVertAlign
;
8813 int wxGrid::GetColLabelTextOrientation()
8815 return m_colLabelTextOrientation
;
8818 wxString
wxGrid::GetRowLabelValue( int row
)
8822 return m_table
->GetRowLabelValue( row
);
8832 wxString
wxGrid::GetColLabelValue( int col
)
8836 return m_table
->GetColLabelValue( col
);
8846 void wxGrid::SetRowLabelSize( int width
)
8848 width
= wxMax( width
, 0 );
8849 if ( width
!= m_rowLabelWidth
)
8853 m_rowLabelWin
->Show( false );
8854 m_cornerLabelWin
->Show( false );
8856 else if ( m_rowLabelWidth
== 0 )
8858 m_rowLabelWin
->Show( true );
8859 if ( m_colLabelHeight
> 0 )
8860 m_cornerLabelWin
->Show( true );
8863 m_rowLabelWidth
= width
;
8865 wxScrolledWindow::Refresh( true );
8869 void wxGrid::SetColLabelSize( int height
)
8871 height
= wxMax( height
, 0 );
8872 if ( height
!= m_colLabelHeight
)
8876 m_colLabelWin
->Show( false );
8877 m_cornerLabelWin
->Show( false );
8879 else if ( m_colLabelHeight
== 0 )
8881 m_colLabelWin
->Show( true );
8882 if ( m_rowLabelWidth
> 0 )
8883 m_cornerLabelWin
->Show( true );
8886 m_colLabelHeight
= height
;
8888 wxScrolledWindow::Refresh( true );
8892 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8894 if ( m_labelBackgroundColour
!= colour
)
8896 m_labelBackgroundColour
= colour
;
8897 m_rowLabelWin
->SetBackgroundColour( colour
);
8898 m_colLabelWin
->SetBackgroundColour( colour
);
8899 m_cornerLabelWin
->SetBackgroundColour( colour
);
8901 if ( !GetBatchCount() )
8903 m_rowLabelWin
->Refresh();
8904 m_colLabelWin
->Refresh();
8905 m_cornerLabelWin
->Refresh();
8910 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8912 if ( m_labelTextColour
!= colour
)
8914 m_labelTextColour
= colour
;
8915 if ( !GetBatchCount() )
8917 m_rowLabelWin
->Refresh();
8918 m_colLabelWin
->Refresh();
8923 void wxGrid::SetLabelFont( const wxFont
& font
)
8926 if ( !GetBatchCount() )
8928 m_rowLabelWin
->Refresh();
8929 m_colLabelWin
->Refresh();
8933 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8935 // allow old (incorrect) defs to be used
8938 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8939 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8940 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8945 case wxTOP
: vert
= wxALIGN_TOP
; break;
8946 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8947 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8950 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8952 m_rowLabelHorizAlign
= horiz
;
8955 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8957 m_rowLabelVertAlign
= vert
;
8960 if ( !GetBatchCount() )
8962 m_rowLabelWin
->Refresh();
8966 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8968 // allow old (incorrect) defs to be used
8971 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8972 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8973 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8978 case wxTOP
: vert
= wxALIGN_TOP
; break;
8979 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8980 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8983 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8985 m_colLabelHorizAlign
= horiz
;
8988 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8990 m_colLabelVertAlign
= vert
;
8993 if ( !GetBatchCount() )
8995 m_colLabelWin
->Refresh();
8999 // Note: under MSW, the default column label font must be changed because it
9000 // does not support vertical printing
9002 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9003 // pGrid->SetLabelFont(font);
9004 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9006 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9008 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9009 m_colLabelTextOrientation
= textOrientation
;
9011 if ( !GetBatchCount() )
9012 m_colLabelWin
->Refresh();
9015 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9019 m_table
->SetRowLabelValue( row
, s
);
9020 if ( !GetBatchCount() )
9022 wxRect rect
= CellToRect( row
, 0 );
9023 if ( rect
.height
> 0 )
9025 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9027 rect
.width
= m_rowLabelWidth
;
9028 m_rowLabelWin
->Refresh( true, &rect
);
9034 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9038 m_table
->SetColLabelValue( col
, s
);
9039 if ( !GetBatchCount() )
9041 wxRect rect
= CellToRect( 0, col
);
9042 if ( rect
.width
> 0 )
9044 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9046 rect
.height
= m_colLabelHeight
;
9047 m_colLabelWin
->Refresh( true, &rect
);
9053 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9055 if ( m_gridLineColour
!= colour
)
9057 m_gridLineColour
= colour
;
9059 wxClientDC
dc( m_gridWin
);
9061 DrawAllGridLines( dc
, wxRegion() );
9065 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9067 if ( m_cellHighlightColour
!= colour
)
9069 m_cellHighlightColour
= colour
;
9071 wxClientDC
dc( m_gridWin
);
9073 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9074 DrawCellHighlight(dc
, attr
);
9079 void wxGrid::SetCellHighlightPenWidth(int width
)
9081 if (m_cellHighlightPenWidth
!= width
)
9083 m_cellHighlightPenWidth
= width
;
9085 // Just redrawing the cell highlight is not enough since that won't
9086 // make any visible change if the the thickness is getting smaller.
9087 int row
= m_currentCellCoords
.GetRow();
9088 int col
= m_currentCellCoords
.GetCol();
9089 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9092 wxRect rect
= CellToRect(row
, col
);
9093 m_gridWin
->Refresh(true, &rect
);
9097 void wxGrid::SetCellHighlightROPenWidth(int width
)
9099 if (m_cellHighlightROPenWidth
!= width
)
9101 m_cellHighlightROPenWidth
= width
;
9103 // Just redrawing the cell highlight is not enough since that won't
9104 // make any visible change if the the thickness is getting smaller.
9105 int row
= m_currentCellCoords
.GetRow();
9106 int col
= m_currentCellCoords
.GetCol();
9107 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9110 wxRect rect
= CellToRect(row
, col
);
9111 m_gridWin
->Refresh(true, &rect
);
9115 void wxGrid::EnableGridLines( bool enable
)
9117 if ( enable
!= m_gridLinesEnabled
)
9119 m_gridLinesEnabled
= enable
;
9121 if ( !GetBatchCount() )
9125 wxClientDC
dc( m_gridWin
);
9127 DrawAllGridLines( dc
, wxRegion() );
9131 m_gridWin
->Refresh();
9137 int wxGrid::GetDefaultRowSize()
9139 return m_defaultRowHeight
;
9142 int wxGrid::GetRowSize( int row
)
9144 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9146 return GetRowHeight(row
);
9149 int wxGrid::GetDefaultColSize()
9151 return m_defaultColWidth
;
9154 int wxGrid::GetColSize( int col
)
9156 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9158 return GetColWidth(col
);
9161 // ============================================================================
9162 // access to the grid attributes: each of them has a default value in the grid
9163 // itself and may be overidden on a per-cell basis
9164 // ============================================================================
9166 // ----------------------------------------------------------------------------
9167 // setting default attributes
9168 // ----------------------------------------------------------------------------
9170 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9172 m_defaultCellAttr
->SetBackgroundColour(col
);
9174 m_gridWin
->SetBackgroundColour(col
);
9178 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9180 m_defaultCellAttr
->SetTextColour(col
);
9183 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9185 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9188 void wxGrid::SetDefaultCellOverflow( bool allow
)
9190 m_defaultCellAttr
->SetOverflow(allow
);
9193 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9195 m_defaultCellAttr
->SetFont(font
);
9198 // For editors and renderers the type registry takes precedence over the
9199 // default attr, so we need to register the new editor/renderer for the string
9200 // data type in order to make setting a default editor/renderer appear to
9203 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9205 RegisterDataType(wxGRID_VALUE_STRING
,
9207 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9210 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9212 RegisterDataType(wxGRID_VALUE_STRING
,
9213 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9217 // ----------------------------------------------------------------------------
9218 // access to the default attrbiutes
9219 // ----------------------------------------------------------------------------
9221 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9223 return m_defaultCellAttr
->GetBackgroundColour();
9226 wxColour
wxGrid::GetDefaultCellTextColour()
9228 return m_defaultCellAttr
->GetTextColour();
9231 wxFont
wxGrid::GetDefaultCellFont()
9233 return m_defaultCellAttr
->GetFont();
9236 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9238 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9241 bool wxGrid::GetDefaultCellOverflow()
9243 return m_defaultCellAttr
->GetOverflow();
9246 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9248 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9251 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9253 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9256 // ----------------------------------------------------------------------------
9257 // access to cell attributes
9258 // ----------------------------------------------------------------------------
9260 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9262 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9263 wxColour colour
= attr
->GetBackgroundColour();
9269 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9271 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9272 wxColour colour
= attr
->GetTextColour();
9278 wxFont
wxGrid::GetCellFont( int row
, int col
)
9280 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9281 wxFont font
= attr
->GetFont();
9287 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9289 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9290 attr
->GetAlignment(horiz
, vert
);
9294 bool wxGrid::GetCellOverflow( int row
, int col
)
9296 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9297 bool allow
= attr
->GetOverflow();
9303 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9305 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9306 attr
->GetSize( num_rows
, num_cols
);
9310 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9312 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9313 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9319 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9321 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9322 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9328 bool wxGrid::IsReadOnly(int row
, int col
) const
9330 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9331 bool isReadOnly
= attr
->IsReadOnly();
9337 // ----------------------------------------------------------------------------
9338 // attribute support: cache, automatic provider creation, ...
9339 // ----------------------------------------------------------------------------
9341 bool wxGrid::CanHaveAttributes()
9348 return m_table
->CanHaveAttributes();
9351 void wxGrid::ClearAttrCache()
9353 if ( m_attrCache
.row
!= -1 )
9355 wxSafeDecRef(m_attrCache
.attr
);
9356 m_attrCache
.attr
= NULL
;
9357 m_attrCache
.row
= -1;
9361 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9365 wxGrid
*self
= (wxGrid
*)this; // const_cast
9367 self
->ClearAttrCache();
9368 self
->m_attrCache
.row
= row
;
9369 self
->m_attrCache
.col
= col
;
9370 self
->m_attrCache
.attr
= attr
;
9375 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9377 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9379 *attr
= m_attrCache
.attr
;
9380 wxSafeIncRef(m_attrCache
.attr
);
9382 #ifdef DEBUG_ATTR_CACHE
9383 gs_nAttrCacheHits
++;
9390 #ifdef DEBUG_ATTR_CACHE
9391 gs_nAttrCacheMisses
++;
9398 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9400 wxGridCellAttr
*attr
= NULL
;
9401 // Additional test to avoid looking at the cache e.g. for
9402 // wxNoCellCoords, as this will confuse memory management.
9405 if ( !LookupAttr(row
, col
, &attr
) )
9407 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9408 : (wxGridCellAttr
*)NULL
;
9409 CacheAttr(row
, col
, attr
);
9415 attr
->SetDefAttr(m_defaultCellAttr
);
9419 attr
= m_defaultCellAttr
;
9426 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9428 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9429 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9431 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9432 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9434 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9437 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9439 // artificially inc the ref count to match DecRef() in caller
9441 m_table
->SetAttr(attr
, row
, col
);
9447 // ----------------------------------------------------------------------------
9448 // setting column attributes (wrappers around SetColAttr)
9449 // ----------------------------------------------------------------------------
9451 void wxGrid::SetColFormatBool(int col
)
9453 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9456 void wxGrid::SetColFormatNumber(int col
)
9458 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9461 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9463 wxString typeName
= wxGRID_VALUE_FLOAT
;
9464 if ( (width
!= -1) || (precision
!= -1) )
9466 typeName
<< _T(':') << width
<< _T(',') << precision
;
9469 SetColFormatCustom(col
, typeName
);
9472 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9474 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9476 attr
= new wxGridCellAttr
;
9477 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9478 attr
->SetRenderer(renderer
);
9480 SetColAttr(col
, attr
);
9484 // ----------------------------------------------------------------------------
9485 // setting cell attributes: this is forwarded to the table
9486 // ----------------------------------------------------------------------------
9488 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9490 if ( CanHaveAttributes() )
9492 m_table
->SetAttr(attr
, row
, col
);
9501 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9503 if ( CanHaveAttributes() )
9505 m_table
->SetRowAttr(attr
, row
);
9514 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9516 if ( CanHaveAttributes() )
9518 m_table
->SetColAttr(attr
, col
);
9527 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9529 if ( CanHaveAttributes() )
9531 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9532 attr
->SetBackgroundColour(colour
);
9537 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9539 if ( CanHaveAttributes() )
9541 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9542 attr
->SetTextColour(colour
);
9547 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9549 if ( CanHaveAttributes() )
9551 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9552 attr
->SetFont(font
);
9557 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9559 if ( CanHaveAttributes() )
9561 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9562 attr
->SetAlignment(horiz
, vert
);
9567 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9569 if ( CanHaveAttributes() )
9571 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9572 attr
->SetOverflow(allow
);
9577 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9579 if ( CanHaveAttributes() )
9581 int cell_rows
, cell_cols
;
9583 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9584 attr
->GetSize(&cell_rows
, &cell_cols
);
9585 attr
->SetSize(num_rows
, num_cols
);
9588 // Cannot set the size of a cell to 0 or negative values
9589 // While it is perfectly legal to do that, this function cannot
9590 // handle all the possibilies, do it by hand by getting the CellAttr.
9591 // You can only set the size of a cell to 1,1 or greater with this fn
9592 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9593 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9594 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9595 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9597 // if this was already a multicell then "turn off" the other cells first
9598 if ((cell_rows
> 1) || (cell_rows
> 1))
9601 for (j
=row
; j
< row
+ cell_rows
; j
++)
9603 for (i
=col
; i
< col
+ cell_cols
; i
++)
9605 if ((i
!= col
) || (j
!= row
))
9607 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9608 attr_stub
->SetSize( 1, 1 );
9609 attr_stub
->DecRef();
9615 // mark the cells that will be covered by this cell to
9616 // negative or zero values to point back at this cell
9617 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9620 for (j
=row
; j
< row
+ num_rows
; j
++)
9622 for (i
=col
; i
< col
+ num_cols
; i
++)
9624 if ((i
!= col
) || (j
!= row
))
9626 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9627 attr_stub
->SetSize( row
- j
, col
- i
);
9628 attr_stub
->DecRef();
9636 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9638 if ( CanHaveAttributes() )
9640 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9641 attr
->SetRenderer(renderer
);
9646 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9648 if ( CanHaveAttributes() )
9650 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9651 attr
->SetEditor(editor
);
9656 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9658 if ( CanHaveAttributes() )
9660 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9661 attr
->SetReadOnly(isReadOnly
);
9666 // ----------------------------------------------------------------------------
9667 // Data type registration
9668 // ----------------------------------------------------------------------------
9670 void wxGrid::RegisterDataType(const wxString
& typeName
,
9671 wxGridCellRenderer
* renderer
,
9672 wxGridCellEditor
* editor
)
9674 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9678 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9680 wxString typeName
= m_table
->GetTypeName(row
, col
);
9681 return GetDefaultEditorForType(typeName
);
9684 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9686 wxString typeName
= m_table
->GetTypeName(row
, col
);
9687 return GetDefaultRendererForType(typeName
);
9690 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9692 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9693 if ( index
== wxNOT_FOUND
)
9697 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9698 wxFAIL_MSG(errStr
.c_str());
9703 return m_typeRegistry
->GetEditor(index
);
9706 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9708 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9709 if ( index
== wxNOT_FOUND
)
9713 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9714 wxFAIL_MSG(errStr
.c_str());
9719 return m_typeRegistry
->GetRenderer(index
);
9722 // ----------------------------------------------------------------------------
9724 // ----------------------------------------------------------------------------
9726 void wxGrid::EnableDragRowSize( bool enable
)
9728 m_canDragRowSize
= enable
;
9731 void wxGrid::EnableDragColSize( bool enable
)
9733 m_canDragColSize
= enable
;
9736 void wxGrid::EnableDragGridSize( bool enable
)
9738 m_canDragGridSize
= enable
;
9741 void wxGrid::EnableDragCell( bool enable
)
9743 m_canDragCell
= enable
;
9746 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9748 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9750 if ( resizeExistingRows
)
9752 // since we are resizing all rows to the default row size,
9753 // we can simply clear the row heights and row bottoms
9754 // arrays (which also allows us to take advantage of
9755 // some speed optimisations)
9756 m_rowHeights
.Empty();
9757 m_rowBottoms
.Empty();
9758 if ( !GetBatchCount() )
9763 void wxGrid::SetRowSize( int row
, int height
)
9765 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9767 // See comment in SetColSize
9768 if ( height
< GetRowMinimalAcceptableHeight())
9771 if ( m_rowHeights
.IsEmpty() )
9773 // need to really create the array
9777 int h
= wxMax( 0, height
);
9778 int diff
= h
- m_rowHeights
[row
];
9780 m_rowHeights
[row
] = h
;
9782 for ( i
= row
; i
< m_numRows
; i
++ )
9784 m_rowBottoms
[i
] += diff
;
9787 if ( !GetBatchCount() )
9791 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9793 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9795 if ( resizeExistingCols
)
9797 // since we are resizing all columns to the default column size,
9798 // we can simply clear the col widths and col rights
9799 // arrays (which also allows us to take advantage of
9800 // some speed optimisations)
9801 m_colWidths
.Empty();
9802 m_colRights
.Empty();
9803 if ( !GetBatchCount() )
9808 void wxGrid::SetColSize( int col
, int width
)
9810 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9812 // should we check that it's bigger than GetColMinimalWidth(col) here?
9814 // No, because it is reasonable to assume the library user know's
9815 // what he is doing. However whe should test against the weaker
9816 // constraint of minimalAcceptableWidth, as this breaks rendering
9818 // This test then fixes sf.net bug #645734
9820 if ( width
< GetColMinimalAcceptableWidth() )
9823 if ( m_colWidths
.IsEmpty() )
9825 // need to really create the array
9829 // if < 0 then calculate new width from label
9833 wxArrayString lines
;
9834 wxClientDC
dc(m_colLabelWin
);
9835 dc
.SetFont(GetLabelFont());
9836 StringToLines(GetColLabelValue(col
), lines
);
9837 GetTextBoxSize(dc
, lines
, &w
, &h
);
9841 int w
= wxMax( 0, width
);
9842 int diff
= w
- m_colWidths
[col
];
9843 m_colWidths
[col
] = w
;
9846 for ( i
= col
; i
< m_numCols
; i
++ )
9848 m_colRights
[i
] += diff
;
9851 if ( !GetBatchCount() )
9855 void wxGrid::SetColMinimalWidth( int col
, int width
)
9857 if (width
> GetColMinimalAcceptableWidth())
9859 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9860 m_colMinWidths
[key
] = width
;
9864 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9866 if (width
> GetRowMinimalAcceptableHeight())
9868 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9869 m_rowMinHeights
[key
] = width
;
9873 int wxGrid::GetColMinimalWidth(int col
) const
9875 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9876 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9878 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9881 int wxGrid::GetRowMinimalHeight(int row
) const
9883 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9884 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9886 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9889 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9891 // We do allow a width of 0 since this gives us
9892 // an easy way to temporarily hiding columns.
9894 m_minAcceptableColWidth
= width
;
9897 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9899 // We do allow a height of 0 since this gives us
9900 // an easy way to temporarily hiding rows.
9902 m_minAcceptableRowHeight
= height
;
9905 int wxGrid::GetColMinimalAcceptableWidth() const
9907 return m_minAcceptableColWidth
;
9910 int wxGrid::GetRowMinimalAcceptableHeight() const
9912 return m_minAcceptableRowHeight
;
9915 // ----------------------------------------------------------------------------
9917 // ----------------------------------------------------------------------------
9919 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9921 wxClientDC
dc(m_gridWin
);
9923 // cancel editing of cell
9924 HideCellEditControl();
9925 SaveEditControlValue();
9927 // init both of them to avoid compiler warnings, even if we only need one
9935 wxCoord extent
, extentMax
= 0;
9936 int max
= column
? m_numRows
: m_numCols
;
9937 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9944 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9945 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
9948 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9949 extent
= column
? size
.x
: size
.y
;
9950 if ( extent
> extentMax
)
9959 // now also compare with the column label extent
9961 dc
.SetFont( GetLabelFont() );
9965 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9966 if ( GetColLabelTextOrientation() == wxVERTICAL
)
9970 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9972 extent
= column
? w
: h
;
9973 if ( extent
> extentMax
)
9978 // empty column - give default extent (notice that if extentMax is less
9979 // than default extent but != 0, it's OK)
9980 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9985 // leave some space around text
9993 SetColSize( col
, extentMax
);
9994 if ( !GetBatchCount() )
9997 m_gridWin
->GetClientSize( &cw
, &ch
);
9998 wxRect
rect ( CellToRect( 0, col
) );
10000 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10001 rect
.width
= cw
- rect
.x
;
10002 rect
.height
= m_colLabelHeight
;
10003 m_colLabelWin
->Refresh( true, &rect
);
10008 SetRowSize(row
, extentMax
);
10009 if ( !GetBatchCount() )
10012 m_gridWin
->GetClientSize( &cw
, &ch
);
10013 wxRect
rect( CellToRect( row
, 0 ) );
10015 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10016 rect
.width
= m_rowLabelWidth
;
10017 rect
.height
= ch
- rect
.y
;
10018 m_rowLabelWin
->Refresh( true, &rect
);
10025 SetColMinimalWidth(col
, extentMax
);
10027 SetRowMinimalHeight(row
, extentMax
);
10031 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10033 int width
= m_rowLabelWidth
;
10038 for ( int col
= 0; col
< m_numCols
; col
++ )
10041 AutoSizeColumn(col
, setAsMin
);
10043 width
+= GetColWidth(col
);
10052 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10054 int height
= m_colLabelHeight
;
10059 for ( int row
= 0; row
< m_numRows
; row
++ )
10062 AutoSizeRow(row
, setAsMin
);
10064 height
+= GetRowHeight(row
);
10073 void wxGrid::AutoSize()
10077 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
10079 // round up the size to a multiple of scroll step - this ensures that we
10080 // won't get the scrollbars if we're sized exactly to this width
10081 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10084 GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
10085 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10087 // distribute the extra space between the columns/rows to avoid having
10088 // extra white space
10090 // Remove the extra m_extraWidth + 1 added above
10091 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10092 if ( diff
&& m_numCols
)
10094 // try to resize the columns uniformly
10095 wxCoord diffPerCol
= diff
/ m_numCols
;
10098 for ( int col
= 0; col
< m_numCols
; col
++ )
10100 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10104 // add remaining amount to the last columns
10105 diff
-= diffPerCol
* m_numCols
;
10108 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10110 SetColSize(col
, GetColWidth(col
) + 1);
10116 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10117 if ( diff
&& m_numRows
)
10119 // try to resize the columns uniformly
10120 wxCoord diffPerRow
= diff
/ m_numRows
;
10123 for ( int row
= 0; row
< m_numRows
; row
++ )
10125 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10129 // add remaining amount to the last rows
10130 diff
-= diffPerRow
* m_numRows
;
10133 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10135 SetRowSize(row
, GetRowHeight(row
) + 1);
10142 SetClientSize(sizeFit
);
10145 void wxGrid::AutoSizeRowLabelSize( int row
)
10147 wxArrayString lines
;
10150 // Hide the edit control, so it
10151 // won't interfere with drag-shrinking.
10152 if ( IsCellEditControlShown() )
10154 HideCellEditControl();
10155 SaveEditControlValue();
10158 // autosize row height depending on label text
10159 StringToLines( GetRowLabelValue( row
), lines
);
10160 wxClientDC
dc( m_rowLabelWin
);
10161 GetTextBoxSize( dc
, lines
, &w
, &h
);
10162 if ( h
< m_defaultRowHeight
)
10163 h
= m_defaultRowHeight
;
10164 SetRowSize(row
, h
);
10168 void wxGrid::AutoSizeColLabelSize( int col
)
10170 wxArrayString lines
;
10173 // Hide the edit control, so it
10174 // won't interfere with drag-shrinking.
10175 if ( IsCellEditControlShown() )
10177 HideCellEditControl();
10178 SaveEditControlValue();
10181 // autosize column width depending on label text
10182 StringToLines( GetColLabelValue( col
), lines
);
10183 wxClientDC
dc( m_colLabelWin
);
10184 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10185 GetTextBoxSize( dc
, lines
, &w
, &h
);
10187 GetTextBoxSize( dc
, lines
, &h
, &w
);
10188 if ( w
< m_defaultColWidth
)
10189 w
= m_defaultColWidth
;
10190 SetColSize(col
, w
);
10194 wxSize
wxGrid::DoGetBestSize() const
10196 // don't set sizes, only calculate them
10197 wxGrid
*self
= (wxGrid
*)this; // const_cast
10200 width
= self
->SetOrCalcColumnSizes(true);
10201 height
= self
->SetOrCalcRowSizes(true);
10208 // Round up to a multiple the scroll rate
10209 // NOTE: this still doesn't get rid of the scrollbars;
10210 // is there any magic incantation for that?
10212 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10214 width
+= 1 + xpu
- (width
% xpu
);
10216 height
+= 1 + ypu
- (height
% ypu
);
10218 // limit to 1/4 of the screen size
10219 int maxwidth
, maxheight
;
10220 wxDisplaySize( &maxwidth
, &maxheight
);
10223 if ( width
> maxwidth
)
10225 if ( height
> maxheight
)
10226 height
= maxheight
;
10228 wxSize
best(width
, height
);
10230 // NOTE: This size should be cached, but first we need to add calls to
10231 // InvalidateBestSize everywhere that could change the results of this
10233 // CacheBestSize(size);
10243 wxPen
& wxGrid::GetDividerPen() const
10248 // ----------------------------------------------------------------------------
10249 // cell value accessor functions
10250 // ----------------------------------------------------------------------------
10252 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10256 m_table
->SetValue( row
, col
, s
);
10257 if ( !GetBatchCount() )
10260 wxRect
rect( CellToRect( row
, col
) );
10262 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10263 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10264 m_gridWin
->Refresh( false, &rect
);
10267 if ( m_currentCellCoords
.GetRow() == row
&&
10268 m_currentCellCoords
.GetCol() == col
&&
10269 IsCellEditControlShown())
10270 // Note: If we are using IsCellEditControlEnabled,
10271 // this interacts badly with calling SetCellValue from
10272 // an EVT_GRID_CELL_CHANGE handler.
10274 HideCellEditControl();
10275 ShowCellEditControl(); // will reread data from table
10280 // ----------------------------------------------------------------------------
10281 // block, row and column selection
10282 // ----------------------------------------------------------------------------
10284 void wxGrid::SelectRow( int row
, bool addToSelected
)
10286 if ( IsSelection() && !addToSelected
)
10290 m_selection
->SelectRow( row
, false, addToSelected
);
10293 void wxGrid::SelectCol( int col
, bool addToSelected
)
10295 if ( IsSelection() && !addToSelected
)
10299 m_selection
->SelectCol( col
, false, addToSelected
);
10302 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10303 bool addToSelected
)
10305 if ( IsSelection() && !addToSelected
)
10309 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10310 false, addToSelected
);
10313 void wxGrid::SelectAll()
10315 if ( m_numRows
> 0 && m_numCols
> 0 )
10318 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10322 // ----------------------------------------------------------------------------
10323 // cell, row and col deselection
10324 // ----------------------------------------------------------------------------
10326 void wxGrid::DeselectRow( int row
)
10328 if ( !m_selection
)
10331 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10333 if ( m_selection
->IsInSelection(row
, 0 ) )
10334 m_selection
->ToggleCellSelection(row
, 0);
10338 int nCols
= GetNumberCols();
10339 for ( int i
= 0; i
< nCols
; i
++ )
10341 if ( m_selection
->IsInSelection(row
, i
) )
10342 m_selection
->ToggleCellSelection(row
, i
);
10347 void wxGrid::DeselectCol( int col
)
10349 if ( !m_selection
)
10352 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10354 if ( m_selection
->IsInSelection(0, col
) )
10355 m_selection
->ToggleCellSelection(0, col
);
10359 int nRows
= GetNumberRows();
10360 for ( int i
= 0; i
< nRows
; i
++ )
10362 if ( m_selection
->IsInSelection(i
, col
) )
10363 m_selection
->ToggleCellSelection(i
, col
);
10368 void wxGrid::DeselectCell( int row
, int col
)
10370 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10371 m_selection
->ToggleCellSelection(row
, col
);
10374 bool wxGrid::IsSelection()
10376 return ( m_selection
&& (m_selection
->IsSelection() ||
10377 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10378 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10381 bool wxGrid::IsInSelection( int row
, int col
) const
10383 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10384 ( row
>= m_selectingTopLeft
.GetRow() &&
10385 col
>= m_selectingTopLeft
.GetCol() &&
10386 row
<= m_selectingBottomRight
.GetRow() &&
10387 col
<= m_selectingBottomRight
.GetCol() )) );
10390 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10394 wxGridCellCoordsArray a
;
10398 return m_selection
->m_cellSelection
;
10401 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10405 wxGridCellCoordsArray a
;
10409 return m_selection
->m_blockSelectionTopLeft
;
10412 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10416 wxGridCellCoordsArray a
;
10420 return m_selection
->m_blockSelectionBottomRight
;
10423 wxArrayInt
wxGrid::GetSelectedRows() const
10431 return m_selection
->m_rowSelection
;
10434 wxArrayInt
wxGrid::GetSelectedCols() const
10442 return m_selection
->m_colSelection
;
10445 void wxGrid::ClearSelection()
10447 m_selectingTopLeft
= wxGridNoCellCoords
;
10448 m_selectingBottomRight
= wxGridNoCellCoords
;
10450 m_selection
->ClearSelection();
10453 // This function returns the rectangle that encloses the given block
10454 // in device coords clipped to the client size of the grid window.
10456 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10457 const wxGridCellCoords
&bottomRight
)
10459 wxRect
rect( wxGridNoCellRect
);
10462 cellRect
= CellToRect( topLeft
);
10463 if ( cellRect
!= wxGridNoCellRect
)
10469 rect
= wxRect(0, 0, 0, 0);
10472 cellRect
= CellToRect( bottomRight
);
10473 if ( cellRect
!= wxGridNoCellRect
)
10479 return wxGridNoCellRect
;
10483 int left
= rect
.GetLeft();
10484 int top
= rect
.GetTop();
10485 int right
= rect
.GetRight();
10486 int bottom
= rect
.GetBottom();
10488 int leftCol
= topLeft
.GetCol();
10489 int topRow
= topLeft
.GetRow();
10490 int rightCol
= bottomRight
.GetCol();
10491 int bottomRow
= bottomRight
.GetRow();
10499 leftCol
= rightCol
;
10509 topRow
= bottomRow
;
10513 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10515 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10517 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10519 cellRect
= CellToRect( j
, i
);
10521 if (cellRect
.x
< left
)
10523 if (cellRect
.y
< top
)
10525 if (cellRect
.x
+ cellRect
.width
> right
)
10526 right
= cellRect
.x
+ cellRect
.width
;
10527 if (cellRect
.y
+ cellRect
.height
> bottom
)
10528 bottom
= cellRect
.y
+ cellRect
.height
;
10532 i
= rightCol
; // jump over inner cells.
10537 // convert to scrolled coords
10539 CalcScrolledPosition( left
, top
, &left
, &top
);
10540 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10543 m_gridWin
->GetClientSize( &cw
, &ch
);
10545 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10546 return wxRect(0,0,0,0);
10548 rect
.SetLeft( wxMax(0, left
) );
10549 rect
.SetTop( wxMax(0, top
) );
10550 rect
.SetRight( wxMin(cw
, right
) );
10551 rect
.SetBottom( wxMin(ch
, bottom
) );
10556 // ----------------------------------------------------------------------------
10557 // grid event classes
10558 // ----------------------------------------------------------------------------
10560 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10562 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10563 int row
, int col
, int x
, int y
, bool sel
,
10564 bool control
, bool shift
, bool alt
, bool meta
)
10565 : wxNotifyEvent( type
, id
)
10572 m_control
= control
;
10577 SetEventObject(obj
);
10581 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10583 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10584 int rowOrCol
, int x
, int y
,
10585 bool control
, bool shift
, bool alt
, bool meta
)
10586 : wxNotifyEvent( type
, id
)
10588 m_rowOrCol
= rowOrCol
;
10591 m_control
= control
;
10596 SetEventObject(obj
);
10600 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10602 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10603 const wxGridCellCoords
& topLeft
,
10604 const wxGridCellCoords
& bottomRight
,
10605 bool sel
, bool control
,
10606 bool shift
, bool alt
, bool meta
)
10607 : wxNotifyEvent( type
, id
)
10609 m_topLeft
= topLeft
;
10610 m_bottomRight
= bottomRight
;
10612 m_control
= control
;
10617 SetEventObject(obj
);
10621 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10623 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10624 wxObject
* obj
, int row
,
10625 int col
, wxControl
* ctrl
)
10626 : wxCommandEvent(type
, id
)
10628 SetEventObject(obj
);
10634 #endif // wxUSE_GRID