1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilatixon, includes "wx/wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/dcclient.h"
34 #include "wx/settings.h"
36 #include "wx/textctrl.h"
37 #include "wx/checkbox.h"
38 #include "wx/combobox.h"
39 #include "wx/valtext.h"
43 #include "wx/textfile.h"
44 #include "wx/spinctrl.h"
45 #include "wx/tokenzr.h"
46 #include "wx/renderer.h"
49 #include "wx/generic/gridsel.h"
51 #if defined(__WXMOTIF__)
52 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
54 #define WXUNUSED_MOTIF(identifier) identifier
57 #if defined(__WXGTK__)
58 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
60 #define WXUNUSED_GTK(identifier) identifier
63 // Required for wxIs... functions
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
71 class WXDLLIMPEXP_ADV
);
73 struct wxGridCellWithAttr
75 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
76 : coords(row
, col
), attr(attr_
)
85 wxGridCellCoords coords
;
89 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
90 // without rewriting the macros, which require a public copy constructor.
93 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
94 class WXDLLIMPEXP_ADV
);
96 #include "wx/arrimpl.cpp"
98 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
99 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
121 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
130 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
131 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
132 const wxPoint
&pos
, const wxSize
&size
);
137 void OnPaint( wxPaintEvent
& event
);
138 void OnMouseEvent( wxMouseEvent
& event
);
139 void OnMouseWheel( wxMouseEvent
& event
);
140 void OnKeyDown( wxKeyEvent
& event
);
141 void OnKeyUp( wxKeyEvent
& );
142 void OnChar( wxKeyEvent
& );
144 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
145 DECLARE_EVENT_TABLE()
146 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
150 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
153 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
154 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
155 const wxPoint
&pos
, const wxSize
&size
);
160 void OnPaint( wxPaintEvent
&event
);
161 void OnMouseEvent( wxMouseEvent
& event
);
162 void OnMouseWheel( wxMouseEvent
& event
);
163 void OnKeyDown( wxKeyEvent
& event
);
164 void OnKeyUp( wxKeyEvent
& );
165 void OnChar( wxKeyEvent
& );
167 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
168 DECLARE_EVENT_TABLE()
169 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
173 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
176 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
177 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
178 const wxPoint
&pos
, const wxSize
&size
);
183 void OnMouseEvent( wxMouseEvent
& event
);
184 void OnMouseWheel( wxMouseEvent
& event
);
185 void OnKeyDown( wxKeyEvent
& event
);
186 void OnKeyUp( wxKeyEvent
& );
187 void OnChar( wxKeyEvent
& );
188 void OnPaint( wxPaintEvent
& event
);
190 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
191 DECLARE_EVENT_TABLE()
192 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
195 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
200 m_owner
= (wxGrid
*)NULL
;
201 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
202 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
205 wxGridWindow( wxGrid
*parent
,
206 wxGridRowLabelWindow
*rowLblWin
,
207 wxGridColLabelWindow
*colLblWin
,
208 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
211 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
213 wxGrid
* GetOwner() { return m_owner
; }
217 wxGridRowLabelWindow
*m_rowLabelWin
;
218 wxGridColLabelWindow
*m_colLabelWin
;
220 void OnPaint( wxPaintEvent
&event
);
221 void OnMouseWheel( wxMouseEvent
& event
);
222 void OnMouseEvent( wxMouseEvent
& event
);
223 void OnKeyDown( wxKeyEvent
& );
224 void OnKeyUp( wxKeyEvent
& );
225 void OnChar( wxKeyEvent
& );
226 void OnEraseBackground( wxEraseEvent
& );
227 void OnFocus( wxFocusEvent
& );
229 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
230 DECLARE_EVENT_TABLE()
231 DECLARE_NO_COPY_CLASS(wxGridWindow
)
236 class wxGridCellEditorEvtHandler
: public wxEvtHandler
239 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
246 void OnKillFocus(wxFocusEvent
& event
);
247 void OnKeyDown(wxKeyEvent
& event
);
248 void OnChar(wxKeyEvent
& event
);
250 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
254 wxGridCellEditor
* m_editor
;
256 // Work around the fact that a focus kill event can be sent to
257 // a combobox within a set focus event.
260 DECLARE_EVENT_TABLE()
261 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
262 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
266 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
268 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
269 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
270 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
271 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
276 // ----------------------------------------------------------------------------
277 // the internal data representation used by wxGridCellAttrProvider
278 // ----------------------------------------------------------------------------
280 // this class stores attributes set for cells
281 class WXDLLIMPEXP_ADV wxGridCellAttrData
284 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
285 wxGridCellAttr
*GetAttr(int row
, int col
) const;
286 void UpdateAttrRows( size_t pos
, int numRows
);
287 void UpdateAttrCols( size_t pos
, int numCols
);
290 // searches for the attr for given cell, returns wxNOT_FOUND if not found
291 int FindIndex(int row
, int col
) const;
293 wxGridCellWithAttrArray m_attrs
;
296 // this class stores attributes set for rows or columns
297 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
300 // empty ctor to suppress warnings
301 wxGridRowOrColAttrData() { }
302 ~wxGridRowOrColAttrData();
304 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
305 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
306 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
309 wxArrayInt m_rowsOrCols
;
310 wxArrayAttrs m_attrs
;
313 // NB: this is just a wrapper around 3 objects: one which stores cell
314 // attributes, and 2 others for row/col ones
315 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
318 wxGridCellAttrData m_cellAttrs
;
319 wxGridRowOrColAttrData m_rowAttrs
,
324 // ----------------------------------------------------------------------------
325 // data structures used for the data type registry
326 // ----------------------------------------------------------------------------
328 struct wxGridDataTypeInfo
330 wxGridDataTypeInfo(const wxString
& typeName
,
331 wxGridCellRenderer
* renderer
,
332 wxGridCellEditor
* editor
)
333 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
336 ~wxGridDataTypeInfo()
338 wxSafeDecRef(m_renderer
);
339 wxSafeDecRef(m_editor
);
343 wxGridCellRenderer
* m_renderer
;
344 wxGridCellEditor
* m_editor
;
346 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
350 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
351 class WXDLLIMPEXP_ADV
);
354 class WXDLLIMPEXP_ADV wxGridTypeRegistry
357 wxGridTypeRegistry() {}
358 ~wxGridTypeRegistry();
360 void RegisterDataType(const wxString
& typeName
,
361 wxGridCellRenderer
* renderer
,
362 wxGridCellEditor
* editor
);
364 // find one of already registered data types
365 int FindRegisteredDataType(const wxString
& typeName
);
367 // try to FindRegisteredDataType(), if this fails and typeName is one of
368 // standard typenames, register it and return its index
369 int FindDataType(const wxString
& typeName
);
371 // try to FindDataType(), if it fails see if it is not one of already
372 // registered data types with some params in which case clone the
373 // registered data type and set params for it
374 int FindOrCloneDataType(const wxString
& typeName
);
376 wxGridCellRenderer
* GetRenderer(int index
);
377 wxGridCellEditor
* GetEditor(int index
);
380 wxGridDataTypeInfoArray m_typeinfo
;
383 // ----------------------------------------------------------------------------
384 // conditional compilation
385 // ----------------------------------------------------------------------------
387 #ifndef WXGRID_DRAW_LINES
388 #define WXGRID_DRAW_LINES 1
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
395 //#define DEBUG_ATTR_CACHE
396 #ifdef DEBUG_ATTR_CACHE
397 static size_t gs_nAttrCacheHits
= 0;
398 static size_t gs_nAttrCacheMisses
= 0;
399 #endif // DEBUG_ATTR_CACHE
401 // ----------------------------------------------------------------------------
403 // ----------------------------------------------------------------------------
405 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
406 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
409 // TODO: this doesn't work at all, grid cells have different sizes and approx
410 // calculations don't work as because of the size mismatch scrollbars
411 // sometimes fail to be shown when they should be or vice versa
413 // The scroll bars may be a little flakey once in a while, but that is
414 // surely much less horrible than having scroll lines of only 1!!!
417 // Well, it's still seriously broken so it might be better but needs
420 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
421 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
423 // the size of hash tables used a bit everywhere (the max number of elements
424 // in these hash tables is the number of rows/columns)
425 static const int GRID_HASH_SIZE
= 100;
428 // ----------------------------------------------------------------------------
430 // ----------------------------------------------------------------------------
432 static inline int GetScrollX(int x
)
434 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
437 static inline int GetScrollY(int y
)
439 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
443 // ============================================================================
445 // ============================================================================
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
451 wxGridCellEditor::wxGridCellEditor()
458 wxGridCellEditor::~wxGridCellEditor()
463 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
464 wxWindowID
WXUNUSED(id
),
465 wxEvtHandler
* evtHandler
)
468 m_control
->PushEventHandler(evtHandler
);
471 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
472 wxGridCellAttr
*attr
)
474 // erase the background because we might not fill the cell
475 wxClientDC
dc(m_control
->GetParent());
476 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
478 gridWindow
->GetOwner()->PrepareDC(dc
);
480 dc
.SetPen(*wxTRANSPARENT_PEN
);
481 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
482 dc
.DrawRectangle(rectCell
);
484 // redraw the control we just painted over
485 m_control
->Refresh();
488 void wxGridCellEditor::Destroy()
492 m_control
->PopEventHandler(true /* delete it*/);
494 m_control
->Destroy();
499 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
501 wxASSERT_MSG(m_control
,
502 wxT("The wxGridCellEditor must be Created first!"));
503 m_control
->Show(show
);
507 // set the colours/fonts if we have any
510 m_colFgOld
= m_control
->GetForegroundColour();
511 m_control
->SetForegroundColour(attr
->GetTextColour());
513 m_colBgOld
= m_control
->GetBackgroundColour();
514 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
516 // Workaround for GTK+1 font setting problem on some platforms
517 #if !defined(__WXGTK__) || defined(__WXGTK20__)
518 m_fontOld
= m_control
->GetFont();
519 m_control
->SetFont(attr
->GetFont());
521 // can't do anything more in the base class version, the other
522 // attributes may only be used by the derived classes
527 // restore the standard colours fonts
528 if ( m_colFgOld
.Ok() )
530 m_control
->SetForegroundColour(m_colFgOld
);
531 m_colFgOld
= wxNullColour
;
534 if ( m_colBgOld
.Ok() )
536 m_control
->SetBackgroundColour(m_colBgOld
);
537 m_colBgOld
= wxNullColour
;
539 // Workaround for GTK+1 font setting problem on some platforms
540 #if !defined(__WXGTK__) || defined(__WXGTK20__)
541 if ( m_fontOld
.Ok() )
543 m_control
->SetFont(m_fontOld
);
544 m_fontOld
= wxNullFont
;
550 void wxGridCellEditor::SetSize(const wxRect
& rect
)
552 wxASSERT_MSG(m_control
,
553 wxT("The wxGridCellEditor must be Created first!"));
554 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
557 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
562 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
564 bool ctrl
= event
.ControlDown();
565 bool alt
= event
.AltDown();
567 // On the Mac the Alt key is more like shift and is used for entry of
568 // valid characters, so check for Ctrl and Meta instead.
569 alt
= event
.MetaDown();
572 // Assume it's not a valid char if ctrl or alt is down, but if both are
573 // down then it may be because of an AltGr key combination, so let them
574 // through in that case.
575 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
579 int key
= event
.GetUnicodeKey();
582 // if the unicode key code is not really a unicode character (it may
583 // be a function key or etc., the platforms appear to always give us a
584 // small value in this case) then fallback to the ascii key code but
585 // don't do anything for function keys or etc.
588 key
= event
.GetKeyCode();
589 keyOk
= (key
<= 127);
592 #else // !wxUSE_UNICODE
593 int key
= event
.GetKeyCode();
597 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
600 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
605 void wxGridCellEditor::StartingClick()
611 // ----------------------------------------------------------------------------
612 // wxGridCellTextEditor
613 // ----------------------------------------------------------------------------
615 wxGridCellTextEditor::wxGridCellTextEditor()
620 void wxGridCellTextEditor::Create(wxWindow
* parent
,
622 wxEvtHandler
* evtHandler
)
624 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
625 wxDefaultPosition
, wxDefaultSize
626 #if defined(__WXMSW__)
627 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
631 // set max length allowed in the textctrl, if the parameter was set
634 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
637 wxGridCellEditor::Create(parent
, id
, evtHandler
);
640 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
641 wxGridCellAttr
* WXUNUSED(attr
))
643 // as we fill the entire client area, don't do anything here to minimize
647 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
649 wxRect
rect(rectOrig
);
651 // Make the edit control large enough to allow for internal
654 // TODO: remove this if the text ctrl sizing is improved esp. for
657 #if defined(__WXGTK__)
666 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
668 // MB: treat MSW separately here otherwise the caret doesn't show
669 // when the editor is in the first row.
670 #if defined(__WXMSW__)
673 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
676 #if defined(__WXMOTIF__)
680 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
681 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
682 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
683 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
686 wxGridCellEditor::SetSize(rect
);
689 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
691 wxASSERT_MSG(m_control
,
692 wxT("The wxGridCellEditor must be Created first!"));
694 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
696 DoBeginEdit(m_startValue
);
699 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
701 Text()->SetValue(startValue
);
702 Text()->SetInsertionPointEnd();
703 Text()->SetSelection(-1,-1);
707 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
710 wxASSERT_MSG(m_control
,
711 wxT("The wxGridCellEditor must be Created first!"));
713 bool changed
= false;
714 wxString value
= Text()->GetValue();
715 if (value
!= m_startValue
)
719 grid
->GetTable()->SetValue(row
, col
, value
);
721 m_startValue
= wxEmptyString
;
722 // No point in setting the text of the hidden control
723 //Text()->SetValue(m_startValue);
729 void wxGridCellTextEditor::Reset()
731 wxASSERT_MSG(m_control
,
732 wxT("The wxGridCellEditor must be Created first!"));
734 DoReset(m_startValue
);
737 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
739 Text()->SetValue(startValue
);
740 Text()->SetInsertionPointEnd();
743 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
745 return wxGridCellEditor::IsAcceptedKey(event
);
748 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
750 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
751 // longer an appropriate way to get the character into the text control.
752 // Do it ourselves instead. We know that if we get this far that we have
753 // a valid character, so not a whole lot of testing needs to be done.
755 wxTextCtrl
* tc
= Text();
760 ch
= event
.GetUnicodeKey();
762 ch
= (wxChar
)event
.GetKeyCode();
764 ch
= (wxChar
)event
.GetKeyCode();
769 // delete the character at the cursor
770 pos
= tc
->GetInsertionPoint();
771 if (pos
< tc
->GetLastPosition())
772 tc
->Remove(pos
, pos
+1);
776 // delete the character before the cursor
777 pos
= tc
->GetInsertionPoint();
779 tc
->Remove(pos
-1, pos
);
788 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
789 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
791 #if defined(__WXMOTIF__) || defined(__WXGTK__)
792 // wxMotif needs a little extra help...
793 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
794 wxString
s( Text()->GetValue() );
795 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
797 Text()->SetInsertionPoint( pos
);
799 // the other ports can handle a Return key press
805 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
815 if ( !params
.ToLong(&tmp
) )
817 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
821 m_maxChars
= (size_t)tmp
;
826 // return the value in the text control
827 wxString
wxGridCellTextEditor::GetValue() const
829 return Text()->GetValue();
832 // ----------------------------------------------------------------------------
833 // wxGridCellNumberEditor
834 // ----------------------------------------------------------------------------
836 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
842 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
844 wxEvtHandler
* evtHandler
)
849 // create a spin ctrl
850 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
851 wxDefaultPosition
, wxDefaultSize
,
855 wxGridCellEditor::Create(parent
, id
, evtHandler
);
860 // just a text control
861 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
864 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
865 #endif // wxUSE_VALIDATORS
869 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
871 // first get the value
872 wxGridTableBase
*table
= grid
->GetTable();
873 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
875 m_valueOld
= table
->GetValueAsLong(row
, col
);
880 wxString sValue
= table
->GetValue(row
, col
);
881 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
883 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
891 Spin()->SetValue((int)m_valueOld
);
897 DoBeginEdit(GetString());
901 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
911 value
= Spin()->GetValue();
912 changed
= value
!= m_valueOld
;
914 text
= wxString::Format(wxT("%ld"), value
);
919 text
= Text()->GetValue();
920 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
925 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
926 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
928 grid
->GetTable()->SetValue(row
, col
, text
);
934 void wxGridCellNumberEditor::Reset()
939 Spin()->SetValue((int)m_valueOld
);
944 DoReset(GetString());
948 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
950 if ( wxGridCellEditor::IsAcceptedKey(event
) )
952 int keycode
= event
.GetKeyCode();
953 if ( (keycode
< 128) &&
954 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
963 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
965 int keycode
= event
.GetKeyCode();
968 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
970 wxGridCellTextEditor::StartingKey(event
);
979 if ( wxIsdigit(keycode
) )
981 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
982 spin
->SetValue(keycode
- '0');
983 spin
->SetSelection(1,1);
991 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1002 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1006 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1010 // skip the error message below
1015 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1019 // return the value in the spin control if it is there (the text control otherwise)
1020 wxString
wxGridCellNumberEditor::GetValue() const
1027 long value
= Spin()->GetValue();
1028 s
.Printf(wxT("%ld"), value
);
1033 s
= Text()->GetValue();
1039 // ----------------------------------------------------------------------------
1040 // wxGridCellFloatEditor
1041 // ----------------------------------------------------------------------------
1043 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1046 m_precision
= precision
;
1049 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1051 wxEvtHandler
* evtHandler
)
1053 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1055 #if wxUSE_VALIDATORS
1056 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1057 #endif // wxUSE_VALIDATORS
1060 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1062 // first get the value
1063 wxGridTableBase
*table
= grid
->GetTable();
1064 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1066 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1071 wxString sValue
= table
->GetValue(row
, col
);
1072 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1074 wxFAIL_MSG( _T("this cell doesn't have float value") );
1079 DoBeginEdit(GetString());
1082 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1086 wxString
text(Text()->GetValue());
1088 if ( (text
.empty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1090 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1091 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1093 grid
->GetTable()->SetValue(row
, col
, text
);
1100 void wxGridCellFloatEditor::Reset()
1102 DoReset(GetString());
1105 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1107 int keycode
= event
.GetKeyCode();
1109 tmpbuf
[0] = (char) keycode
;
1111 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1113 bool is_decimal_point
= ( strbuf
==
1114 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1116 bool is_decimal_point
= ( strbuf
== _T(".") );
1118 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1119 || is_decimal_point
)
1121 wxGridCellTextEditor::StartingKey(event
);
1123 // skip Skip() below
1130 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1141 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1145 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1147 m_precision
= (int)tmp
;
1149 // skip the error message below
1154 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1158 wxString
wxGridCellFloatEditor::GetString() const
1161 if ( m_width
== -1 )
1163 // default width/precision
1166 else if ( m_precision
== -1 )
1168 // default precision
1169 fmt
.Printf(_T("%%%d.f"), m_width
);
1173 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1176 return wxString::Format(fmt
, m_valueOld
);
1179 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1181 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1183 int keycode
= event
.GetKeyCode();
1184 printf("%d\n", keycode
);
1185 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1187 tmpbuf
[0] = (char) keycode
;
1189 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1191 bool is_decimal_point
=
1192 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1193 wxLOCALE_CAT_NUMBER
) );
1195 bool is_decimal_point
= ( strbuf
== _T(".") );
1197 if ( (keycode
< 128) &&
1198 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1199 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1206 #endif // wxUSE_TEXTCTRL
1210 // ----------------------------------------------------------------------------
1211 // wxGridCellBoolEditor
1212 // ----------------------------------------------------------------------------
1214 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1216 wxEvtHandler
* evtHandler
)
1218 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1219 wxDefaultPosition
, wxDefaultSize
,
1222 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1225 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1227 bool resize
= false;
1228 wxSize size
= m_control
->GetSize();
1229 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1231 // check if the checkbox is not too big/small for this cell
1232 wxSize sizeBest
= m_control
->GetBestSize();
1233 if ( !(size
== sizeBest
) )
1235 // reset to default size if it had been made smaller
1241 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1243 // leave 1 pixel margin
1244 size
.x
= size
.y
= minSize
- 2;
1251 m_control
->SetSize(size
);
1254 // position it in the centre of the rectangle (TODO: support alignment?)
1256 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1257 // the checkbox without label still has some space to the right in wxGTK,
1258 // so shift it to the right
1260 #elif defined(__WXMSW__)
1261 // here too, but in other way
1266 int hAlign
= wxALIGN_CENTRE
;
1267 int vAlign
= wxALIGN_CENTRE
;
1269 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1272 if (hAlign
== wxALIGN_LEFT
)
1278 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1280 else if (hAlign
== wxALIGN_RIGHT
)
1282 x
= r
.x
+ r
.width
- size
.x
- 2;
1283 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1285 else if (hAlign
== wxALIGN_CENTRE
)
1287 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1288 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1291 m_control
->Move(x
, y
);
1294 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1296 m_control
->Show(show
);
1300 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1301 CBox()->SetBackgroundColour(colBg
);
1305 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1307 wxASSERT_MSG(m_control
,
1308 wxT("The wxGridCellEditor must be Created first!"));
1310 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1311 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1314 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1315 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1317 CBox()->SetValue(m_startValue
);
1321 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1324 wxASSERT_MSG(m_control
,
1325 wxT("The wxGridCellEditor must be Created first!"));
1327 bool changed
= false;
1328 bool value
= CBox()->GetValue();
1329 if ( value
!= m_startValue
)
1334 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1335 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1337 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1343 void wxGridCellBoolEditor::Reset()
1345 wxASSERT_MSG(m_control
,
1346 wxT("The wxGridCellEditor must be Created first!"));
1348 CBox()->SetValue(m_startValue
);
1351 void wxGridCellBoolEditor::StartingClick()
1353 CBox()->SetValue(!CBox()->GetValue());
1356 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1358 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1360 int keycode
= event
.GetKeyCode();
1373 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1375 int keycode
= event
.GetKeyCode();
1379 CBox()->SetValue(!CBox()->GetValue());
1383 CBox()->SetValue(true);
1387 CBox()->SetValue(false);
1393 // return the value as "1" for true and the empty string for false
1394 wxString
wxGridCellBoolEditor::GetValue() const
1396 bool bSet
= CBox()->GetValue();
1397 return bSet
? _T("1") : wxEmptyString
;
1400 #endif // wxUSE_CHECKBOX
1404 // ----------------------------------------------------------------------------
1405 // wxGridCellChoiceEditor
1406 // ----------------------------------------------------------------------------
1408 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1410 : m_choices(choices
),
1411 m_allowOthers(allowOthers
) { }
1413 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1414 const wxString choices
[],
1416 : m_allowOthers(allowOthers
)
1420 m_choices
.Alloc(count
);
1421 for ( size_t n
= 0; n
< count
; n
++ )
1423 m_choices
.Add(choices
[n
]);
1428 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1430 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1431 editor
->m_allowOthers
= m_allowOthers
;
1432 editor
->m_choices
= m_choices
;
1437 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1439 wxEvtHandler
* evtHandler
)
1441 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1442 wxDefaultPosition
, wxDefaultSize
,
1444 m_allowOthers
? 0 : wxCB_READONLY
);
1446 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1449 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1450 wxGridCellAttr
* attr
)
1452 // as we fill the entire client area, don't do anything here to minimize
1455 // TODO: It doesn't actually fill the client area since the height of a
1456 // combo always defaults to the standard... Until someone has time to
1457 // figure out the right rectangle to paint, just do it the normal way...
1458 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1461 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1463 wxASSERT_MSG(m_control
,
1464 wxT("The wxGridCellEditor must be Created first!"));
1466 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1468 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1470 // Don't immediately end if we get a kill focus event within BeginEdit
1472 evtHandler
->SetInSetFocus(true);
1474 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1477 Combo()->SetValue(m_startValue
);
1480 // find the right position, or default to the first if not found
1481 int pos
= Combo()->FindString(m_startValue
);
1482 if (pos
== wxNOT_FOUND
)
1484 Combo()->SetSelection(pos
);
1486 Combo()->SetInsertionPointEnd();
1487 Combo()->SetFocus();
1490 evtHandler
->SetInSetFocus(false);
1493 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1496 wxString value
= Combo()->GetValue();
1497 if ( value
== m_startValue
)
1500 grid
->GetTable()->SetValue(row
, col
, value
);
1505 void wxGridCellChoiceEditor::Reset()
1507 Combo()->SetValue(m_startValue
);
1508 Combo()->SetInsertionPointEnd();
1511 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1521 wxStringTokenizer
tk(params
, _T(','));
1522 while ( tk
.HasMoreTokens() )
1524 m_choices
.Add(tk
.GetNextToken());
1528 // return the value in the text control
1529 wxString
wxGridCellChoiceEditor::GetValue() const
1531 return Combo()->GetValue();
1534 #endif // wxUSE_COMBOBOX
1536 // ----------------------------------------------------------------------------
1537 // wxGridCellEditorEvtHandler
1538 // ----------------------------------------------------------------------------
1540 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1542 // Don't disable the cell if we're just starting to edit it
1547 m_grid
->DisableCellEditControl();
1552 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1554 switch ( event
.GetKeyCode() )
1558 m_grid
->DisableCellEditControl();
1562 m_grid
->GetEventHandler()->ProcessEvent( event
);
1566 case WXK_NUMPAD_ENTER
:
1567 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1568 m_editor
->HandleReturn(event
);
1576 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1578 switch ( event
.GetKeyCode() )
1583 case WXK_NUMPAD_ENTER
:
1591 // ----------------------------------------------------------------------------
1592 // wxGridCellWorker is an (almost) empty common base class for
1593 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1594 // ----------------------------------------------------------------------------
1596 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1601 wxGridCellWorker::~wxGridCellWorker()
1605 // ============================================================================
1607 // ============================================================================
1609 // ----------------------------------------------------------------------------
1610 // wxGridCellRenderer
1611 // ----------------------------------------------------------------------------
1613 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1614 wxGridCellAttr
& attr
,
1617 int WXUNUSED(row
), int WXUNUSED(col
),
1620 dc
.SetBackgroundMode( wxSOLID
);
1622 // grey out fields if the grid is disabled
1623 if( grid
.IsEnabled() )
1627 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1631 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1636 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1639 dc
.SetPen( *wxTRANSPARENT_PEN
);
1640 dc
.DrawRectangle(rect
);
1643 // ----------------------------------------------------------------------------
1644 // wxGridCellStringRenderer
1645 // ----------------------------------------------------------------------------
1647 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1648 wxGridCellAttr
& attr
,
1652 dc
.SetBackgroundMode( wxTRANSPARENT
);
1654 // TODO some special colours for attr.IsReadOnly() case?
1656 // different coloured text when the grid is disabled
1657 if( grid
.IsEnabled() )
1661 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1662 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1666 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1667 dc
.SetTextForeground( attr
.GetTextColour() );
1672 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1673 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1676 dc
.SetFont( attr
.GetFont() );
1679 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1681 const wxString
& text
)
1683 wxCoord x
= 0, y
= 0, max_x
= 0;
1684 dc
.SetFont(attr
.GetFont());
1685 wxStringTokenizer
tk(text
, _T('\n'));
1686 while ( tk
.HasMoreTokens() )
1688 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1689 max_x
= wxMax(max_x
, x
);
1692 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1694 return wxSize(max_x
, y
);
1697 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1698 wxGridCellAttr
& attr
,
1702 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1705 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1706 wxGridCellAttr
& attr
,
1708 const wxRect
& rectCell
,
1712 wxRect rect
= rectCell
;
1715 // erase only this cells background, overflow cells should have been erased
1716 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1719 attr
.GetAlignment(&hAlign
, &vAlign
);
1721 int overflowCols
= 0;
1723 if (attr
.GetOverflow())
1725 int cols
= grid
.GetNumberCols();
1726 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1727 int cell_rows
, cell_cols
;
1728 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1729 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1731 int i
, c_cols
, c_rows
;
1732 for (i
= col
+cell_cols
; i
< cols
; i
++)
1734 bool is_empty
= true;
1735 for (int j
=row
; j
<row
+cell_rows
; j
++)
1737 // check w/ anchor cell for multicell block
1738 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1739 if (c_rows
> 0) c_rows
= 0;
1740 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1747 rect
.width
+= grid
.GetColSize(i
);
1753 if (rect
.width
>= best_width
) break;
1755 overflowCols
= i
- col
- cell_cols
+ 1;
1756 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1759 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1761 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1763 clip
.x
+= rectCell
.width
;
1764 // draw each overflow cell individually
1765 int col_end
= col
+cell_cols
+overflowCols
;
1766 if (col_end
>= grid
.GetNumberCols())
1767 col_end
= grid
.GetNumberCols() - 1;
1768 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1770 clip
.width
= grid
.GetColSize(i
) - 1;
1771 dc
.DestroyClippingRegion();
1772 dc
.SetClippingRegion(clip
);
1774 SetTextColoursAndFont(grid
, attr
, dc
,
1775 grid
.IsInSelection(row
,i
));
1777 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1778 rect
, hAlign
, vAlign
);
1779 clip
.x
+= grid
.GetColSize(i
) - 1;
1785 dc
.DestroyClippingRegion();
1789 // now we only have to draw the text
1790 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1792 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1793 rect
, hAlign
, vAlign
);
1796 // ----------------------------------------------------------------------------
1797 // wxGridCellNumberRenderer
1798 // ----------------------------------------------------------------------------
1800 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1802 wxGridTableBase
*table
= grid
.GetTable();
1804 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1806 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1810 text
= table
->GetValue(row
, col
);
1816 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1817 wxGridCellAttr
& attr
,
1819 const wxRect
& rectCell
,
1823 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1825 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1827 // draw the text right aligned by default
1829 attr
.GetAlignment(&hAlign
, &vAlign
);
1830 hAlign
= wxALIGN_RIGHT
;
1832 wxRect rect
= rectCell
;
1835 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1838 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1839 wxGridCellAttr
& attr
,
1843 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1846 // ----------------------------------------------------------------------------
1847 // wxGridCellFloatRenderer
1848 // ----------------------------------------------------------------------------
1850 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1853 SetPrecision(precision
);
1856 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1858 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1859 renderer
->m_width
= m_width
;
1860 renderer
->m_precision
= m_precision
;
1861 renderer
->m_format
= m_format
;
1866 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1868 wxGridTableBase
*table
= grid
.GetTable();
1873 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1875 val
= table
->GetValueAsDouble(row
, col
);
1880 text
= table
->GetValue(row
, col
);
1881 hasDouble
= text
.ToDouble(&val
);
1888 if ( m_width
== -1 )
1890 if ( m_precision
== -1 )
1892 // default width/precision
1893 m_format
= _T("%f");
1897 m_format
.Printf(_T("%%.%df"), m_precision
);
1900 else if ( m_precision
== -1 )
1902 // default precision
1903 m_format
.Printf(_T("%%%d.f"), m_width
);
1907 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1911 text
.Printf(m_format
, val
);
1914 //else: text already contains the string
1919 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1920 wxGridCellAttr
& attr
,
1922 const wxRect
& rectCell
,
1926 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1928 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1930 // draw the text right aligned by default
1932 attr
.GetAlignment(&hAlign
, &vAlign
);
1933 hAlign
= wxALIGN_RIGHT
;
1935 wxRect rect
= rectCell
;
1938 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1941 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1942 wxGridCellAttr
& attr
,
1946 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1949 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1953 // reset to defaults
1959 wxString tmp
= params
.BeforeFirst(_T(','));
1963 if ( tmp
.ToLong(&width
) )
1965 SetWidth((int)width
);
1969 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1972 tmp
= params
.AfterFirst(_T(','));
1976 if ( tmp
.ToLong(&precision
) )
1978 SetPrecision((int)precision
);
1982 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1989 // ----------------------------------------------------------------------------
1990 // wxGridCellBoolRenderer
1991 // ----------------------------------------------------------------------------
1993 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1995 // FIXME these checkbox size calculations are really ugly...
1997 // between checkmark and box
1998 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2000 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2001 wxGridCellAttr
& WXUNUSED(attr
),
2006 // compute it only once (no locks for MT safeness in GUI thread...)
2007 if ( !ms_sizeCheckMark
.x
)
2009 // get checkbox size
2010 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2011 wxSize size
= checkbox
->GetBestSize();
2012 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
2014 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2015 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2016 checkSize
-= size
.y
/ 2;
2021 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2024 return ms_sizeCheckMark
;
2027 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2028 wxGridCellAttr
& attr
,
2034 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2036 // draw a check mark in the centre (ignoring alignment - TODO)
2037 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2039 // don't draw outside the cell
2040 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2041 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2043 // and even leave (at least) 1 pixel margin
2044 size
.x
= size
.y
= minSize
- 2;
2047 // draw a border around checkmark
2049 attr
.GetAlignment(& hAlign
, &vAlign
);
2052 if (hAlign
== wxALIGN_CENTRE
)
2054 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2055 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2056 rectBorder
.width
= size
.x
;
2057 rectBorder
.height
= size
.y
;
2059 else if (hAlign
== wxALIGN_LEFT
)
2061 rectBorder
.x
= rect
.x
+ 2;
2062 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2063 rectBorder
.width
= size
.x
;
2064 rectBorder
.height
= size
.y
;
2066 else if (hAlign
== wxALIGN_RIGHT
)
2068 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2069 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2070 rectBorder
.width
= size
.x
;
2071 rectBorder
.height
= size
.y
;
2075 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2076 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2079 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2080 value
= !( !cellval
|| (cellval
== wxT("0")) );
2085 wxRect rectMark
= rectBorder
;
2087 // MSW DrawCheckMark() is weird (and should probably be changed...)
2088 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2092 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2095 dc
.SetTextForeground(attr
.GetTextColour());
2096 dc
.DrawCheckMark(rectMark
);
2099 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2100 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2101 dc
.DrawRectangle(rectBorder
);
2104 // ----------------------------------------------------------------------------
2106 // ----------------------------------------------------------------------------
2108 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2112 m_isReadOnly
= Unset
;
2117 m_attrkind
= wxGridCellAttr::Cell
;
2119 m_sizeRows
= m_sizeCols
= 1;
2120 m_overflow
= UnsetOverflow
;
2122 SetDefAttr(attrDefault
);
2125 wxGridCellAttr
*wxGridCellAttr::Clone() const
2127 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2129 if ( HasTextColour() )
2130 attr
->SetTextColour(GetTextColour());
2131 if ( HasBackgroundColour() )
2132 attr
->SetBackgroundColour(GetBackgroundColour());
2134 attr
->SetFont(GetFont());
2135 if ( HasAlignment() )
2136 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2138 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2142 attr
->SetRenderer(m_renderer
);
2143 m_renderer
->IncRef();
2147 attr
->SetEditor(m_editor
);
2152 attr
->SetReadOnly();
2154 attr
->SetKind( m_attrkind
);
2159 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2161 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2162 SetTextColour(mergefrom
->GetTextColour());
2163 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2164 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2165 if ( !HasFont() && mergefrom
->HasFont() )
2166 SetFont(mergefrom
->GetFont());
2167 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2169 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2170 SetAlignment(hAlign
, vAlign
);
2172 if ( !HasSize() && mergefrom
->HasSize() )
2173 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2175 // Directly access member functions as GetRender/Editor don't just return
2176 // m_renderer/m_editor
2178 // Maybe add support for merge of Render and Editor?
2179 if (!HasRenderer() && mergefrom
->HasRenderer() )
2181 m_renderer
= mergefrom
->m_renderer
;
2182 m_renderer
->IncRef();
2184 if ( !HasEditor() && mergefrom
->HasEditor() )
2186 m_editor
= mergefrom
->m_editor
;
2189 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2190 SetReadOnly(mergefrom
->IsReadOnly());
2192 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2193 SetOverflow(mergefrom
->GetOverflow());
2195 SetDefAttr(mergefrom
->m_defGridAttr
);
2198 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2200 // The size of a cell is normally 1,1
2202 // If this cell is larger (2,2) then this is the top left cell
2203 // the other cells that will be covered (lower right cells) must be
2204 // set to negative or zero values such that
2205 // row + num_rows of the covered cell points to the larger cell (this cell)
2206 // same goes for the col + num_cols.
2208 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2210 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2211 !((num_rows
<=0)&&(num_cols
>0)) ||
2212 !((num_rows
==0)&&(num_cols
==0))),
2213 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2215 m_sizeRows
= num_rows
;
2216 m_sizeCols
= num_cols
;
2219 const wxColour
& wxGridCellAttr::GetTextColour() const
2221 if (HasTextColour())
2225 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2227 return m_defGridAttr
->GetTextColour();
2231 wxFAIL_MSG(wxT("Missing default cell attribute"));
2232 return wxNullColour
;
2237 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2239 if (HasBackgroundColour())
2241 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2242 return m_defGridAttr
->GetBackgroundColour();
2245 wxFAIL_MSG(wxT("Missing default cell attribute"));
2246 return wxNullColour
;
2251 const wxFont
& wxGridCellAttr::GetFont() const
2255 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2256 return m_defGridAttr
->GetFont();
2259 wxFAIL_MSG(wxT("Missing default cell attribute"));
2265 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2269 if ( hAlign
) *hAlign
= m_hAlign
;
2270 if ( vAlign
) *vAlign
= m_vAlign
;
2272 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2273 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2276 wxFAIL_MSG(wxT("Missing default cell attribute"));
2280 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2282 if ( num_rows
) *num_rows
= m_sizeRows
;
2283 if ( num_cols
) *num_cols
= m_sizeCols
;
2286 // GetRenderer and GetEditor use a slightly different decision path about
2287 // which attribute to use. If a non-default attr object has one then it is
2288 // used, otherwise the default editor or renderer is fetched from the grid and
2289 // used. It should be the default for the data type of the cell. If it is
2290 // NULL (because the table has a type that the grid does not have in its
2291 // registry,) then the grid's default editor or renderer is used.
2293 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2295 wxGridCellRenderer
*renderer
;
2297 if ( m_renderer
&& this != m_defGridAttr
)
2299 // use the cells renderer if it has one
2300 renderer
= m_renderer
;
2303 else // no non default cell renderer
2305 // get default renderer for the data type
2308 // GetDefaultRendererForCell() will do IncRef() for us
2309 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2318 if (m_defGridAttr
&& this != m_defGridAttr
)
2320 // if we still don't have one then use the grid default
2321 // (no need for IncRef() here neither)
2322 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2324 else // default grid attr
2326 // use m_renderer which we had decided not to use initially
2327 renderer
= m_renderer
;
2334 // we're supposed to always find something
2335 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2340 // same as above, except for s/renderer/editor/g
2341 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2343 wxGridCellEditor
*editor
;
2345 if ( m_editor
&& this != m_defGridAttr
)
2347 // use the cells editor if it has one
2351 else // no non default cell editor
2353 // get default editor for the data type
2356 // GetDefaultEditorForCell() will do IncRef() for us
2357 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2366 if ( m_defGridAttr
&& this != m_defGridAttr
)
2368 // if we still don't have one then use the grid default
2369 // (no need for IncRef() here neither)
2370 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2372 else // default grid attr
2374 // use m_editor which we had decided not to use initially
2382 // we're supposed to always find something
2383 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2388 // ----------------------------------------------------------------------------
2389 // wxGridCellAttrData
2390 // ----------------------------------------------------------------------------
2392 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2394 int n
= FindIndex(row
, col
);
2395 if ( n
== wxNOT_FOUND
)
2397 // add the attribute
2398 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2402 // free the old attribute
2403 m_attrs
[(size_t)n
].attr
->DecRef();
2407 // change the attribute
2408 m_attrs
[(size_t)n
].attr
= attr
;
2412 // remove this attribute
2413 m_attrs
.RemoveAt((size_t)n
);
2418 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2420 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2422 int n
= FindIndex(row
, col
);
2423 if ( n
!= wxNOT_FOUND
)
2425 attr
= m_attrs
[(size_t)n
].attr
;
2432 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2434 size_t count
= m_attrs
.GetCount();
2435 for ( size_t n
= 0; n
< count
; n
++ )
2437 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2438 wxCoord row
= coords
.GetRow();
2439 if ((size_t)row
>= pos
)
2443 // If rows inserted, include row counter where necessary
2444 coords
.SetRow(row
+ numRows
);
2446 else if (numRows
< 0)
2448 // If rows deleted ...
2449 if ((size_t)row
>= pos
- numRows
)
2451 // ...either decrement row counter (if row still exists)...
2452 coords
.SetRow(row
+ numRows
);
2456 // ...or remove the attribute
2457 // No need to DecRef the attribute itself since this is
2458 // done be wxGridCellWithAttr's destructor!
2459 m_attrs
.RemoveAt(n
);
2467 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2469 size_t count
= m_attrs
.GetCount();
2470 for ( size_t n
= 0; n
< count
; n
++ )
2472 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2473 wxCoord col
= coords
.GetCol();
2474 if ( (size_t)col
>= pos
)
2478 // If rows inserted, include row counter where necessary
2479 coords
.SetCol(col
+ numCols
);
2481 else if (numCols
< 0)
2483 // If rows deleted ...
2484 if ((size_t)col
>= pos
- numCols
)
2486 // ...either decrement row counter (if row still exists)...
2487 coords
.SetCol(col
+ numCols
);
2491 // ...or remove the attribute
2492 // No need to DecRef the attribute itself since this is
2493 // done be wxGridCellWithAttr's destructor!
2494 m_attrs
.RemoveAt(n
);
2502 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2504 size_t count
= m_attrs
.GetCount();
2505 for ( size_t n
= 0; n
< count
; n
++ )
2507 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2508 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2517 // ----------------------------------------------------------------------------
2518 // wxGridRowOrColAttrData
2519 // ----------------------------------------------------------------------------
2521 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2523 size_t count
= m_attrs
.Count();
2524 for ( size_t n
= 0; n
< count
; n
++ )
2526 m_attrs
[n
]->DecRef();
2530 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2532 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2534 int n
= m_rowsOrCols
.Index(rowOrCol
);
2535 if ( n
!= wxNOT_FOUND
)
2537 attr
= m_attrs
[(size_t)n
];
2544 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2546 int i
= m_rowsOrCols
.Index(rowOrCol
);
2547 if ( i
== wxNOT_FOUND
)
2549 // add the attribute
2550 m_rowsOrCols
.Add(rowOrCol
);
2555 size_t n
= (size_t)i
;
2558 // change the attribute
2559 m_attrs
[n
]->DecRef();
2564 // remove this attribute
2565 m_attrs
[n
]->DecRef();
2566 m_rowsOrCols
.RemoveAt(n
);
2567 m_attrs
.RemoveAt(n
);
2572 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2574 size_t count
= m_attrs
.GetCount();
2575 for ( size_t n
= 0; n
< count
; n
++ )
2577 int & rowOrCol
= m_rowsOrCols
[n
];
2578 if ( (size_t)rowOrCol
>= pos
)
2580 if ( numRowsOrCols
> 0 )
2582 // If rows inserted, include row counter where necessary
2583 rowOrCol
+= numRowsOrCols
;
2585 else if ( numRowsOrCols
< 0)
2587 // If rows deleted, either decrement row counter (if row still exists)
2588 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2589 rowOrCol
+= numRowsOrCols
;
2592 m_rowsOrCols
.RemoveAt(n
);
2593 m_attrs
[n
]->DecRef();
2594 m_attrs
.RemoveAt(n
);
2602 // ----------------------------------------------------------------------------
2603 // wxGridCellAttrProvider
2604 // ----------------------------------------------------------------------------
2606 wxGridCellAttrProvider::wxGridCellAttrProvider()
2608 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2611 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2616 void wxGridCellAttrProvider::InitData()
2618 m_data
= new wxGridCellAttrProviderData
;
2621 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2622 wxGridCellAttr::wxAttrKind kind
) const
2624 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2629 case (wxGridCellAttr::Any
):
2630 //Get cached merge attributes.
2631 // Currenlty not used as no cache implemented as not mutiable
2632 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2635 //Basicaly implement old version.
2636 //Also check merge cache, so we don't have to re-merge every time..
2637 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2638 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2639 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2641 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2642 // Two or more are non NULL
2643 attr
= new wxGridCellAttr
;
2644 attr
->SetKind(wxGridCellAttr::Merged
);
2648 attr
->MergeWith(attrcell
);
2652 attr
->MergeWith(attrcol
);
2656 attr
->MergeWith(attrrow
);
2659 //store merge attr if cache implemented
2661 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2665 // one or none is non null return it or null.
2666 if(attrrow
) attr
= attrrow
;
2682 case (wxGridCellAttr::Cell
):
2683 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2685 case (wxGridCellAttr::Col
):
2686 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2688 case (wxGridCellAttr::Row
):
2689 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2693 // (wxGridCellAttr::Default):
2694 // (wxGridCellAttr::Merged):
2701 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2707 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2710 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2715 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2718 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2723 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2726 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2730 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2732 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2736 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2740 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2742 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2746 // ----------------------------------------------------------------------------
2747 // wxGridTypeRegistry
2748 // ----------------------------------------------------------------------------
2750 wxGridTypeRegistry::~wxGridTypeRegistry()
2752 size_t count
= m_typeinfo
.Count();
2753 for ( size_t i
= 0; i
< count
; i
++ )
2754 delete m_typeinfo
[i
];
2758 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2759 wxGridCellRenderer
* renderer
,
2760 wxGridCellEditor
* editor
)
2762 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2764 // is it already registered?
2765 int loc
= FindRegisteredDataType(typeName
);
2766 if ( loc
!= wxNOT_FOUND
)
2768 delete m_typeinfo
[loc
];
2769 m_typeinfo
[loc
] = info
;
2773 m_typeinfo
.Add(info
);
2777 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2779 size_t count
= m_typeinfo
.GetCount();
2780 for ( size_t i
= 0; i
< count
; i
++ )
2782 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2791 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2793 int index
= FindRegisteredDataType(typeName
);
2794 if ( index
== wxNOT_FOUND
)
2796 // check whether this is one of the standard ones, in which case
2797 // register it "on the fly"
2799 if ( typeName
== wxGRID_VALUE_STRING
)
2801 RegisterDataType(wxGRID_VALUE_STRING
,
2802 new wxGridCellStringRenderer
,
2803 new wxGridCellTextEditor
);
2805 #endif // wxUSE_TEXTCTRL
2807 if ( typeName
== wxGRID_VALUE_BOOL
)
2809 RegisterDataType(wxGRID_VALUE_BOOL
,
2810 new wxGridCellBoolRenderer
,
2811 new wxGridCellBoolEditor
);
2813 #endif // wxUSE_CHECKBOX
2815 if ( typeName
== wxGRID_VALUE_NUMBER
)
2817 RegisterDataType(wxGRID_VALUE_NUMBER
,
2818 new wxGridCellNumberRenderer
,
2819 new wxGridCellNumberEditor
);
2821 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2823 RegisterDataType(wxGRID_VALUE_FLOAT
,
2824 new wxGridCellFloatRenderer
,
2825 new wxGridCellFloatEditor
);
2827 #endif // wxUSE_TEXTCTRL
2829 if ( typeName
== wxGRID_VALUE_CHOICE
)
2831 RegisterDataType(wxGRID_VALUE_CHOICE
,
2832 new wxGridCellStringRenderer
,
2833 new wxGridCellChoiceEditor
);
2835 #endif // wxUSE_COMBOBOX
2840 // we get here only if just added the entry for this type, so return
2842 index
= m_typeinfo
.GetCount() - 1;
2848 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2850 int index
= FindDataType(typeName
);
2851 if ( index
== wxNOT_FOUND
)
2853 // the first part of the typename is the "real" type, anything after ':'
2854 // are the parameters for the renderer
2855 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2856 if ( index
== wxNOT_FOUND
)
2861 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2862 wxGridCellRenderer
*rendererOld
= renderer
;
2863 renderer
= renderer
->Clone();
2864 rendererOld
->DecRef();
2866 wxGridCellEditor
*editor
= GetEditor(index
);
2867 wxGridCellEditor
*editorOld
= editor
;
2868 editor
= editor
->Clone();
2869 editorOld
->DecRef();
2871 // do it even if there are no parameters to reset them to defaults
2872 wxString params
= typeName
.AfterFirst(_T(':'));
2873 renderer
->SetParameters(params
);
2874 editor
->SetParameters(params
);
2876 // register the new typename
2877 RegisterDataType(typeName
, renderer
, editor
);
2879 // we just registered it, it's the last one
2880 index
= m_typeinfo
.GetCount() - 1;
2886 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2888 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2894 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2896 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2902 // ----------------------------------------------------------------------------
2904 // ----------------------------------------------------------------------------
2906 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2909 wxGridTableBase::wxGridTableBase()
2911 m_view
= (wxGrid
*) NULL
;
2912 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2915 wxGridTableBase::~wxGridTableBase()
2917 delete m_attrProvider
;
2920 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2922 delete m_attrProvider
;
2923 m_attrProvider
= attrProvider
;
2926 bool wxGridTableBase::CanHaveAttributes()
2928 if ( ! GetAttrProvider() )
2930 // use the default attr provider by default
2931 SetAttrProvider(new wxGridCellAttrProvider
);
2936 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2938 if ( m_attrProvider
)
2939 return m_attrProvider
->GetAttr(row
, col
, kind
);
2941 return (wxGridCellAttr
*)NULL
;
2945 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2947 if ( m_attrProvider
)
2949 attr
->SetKind(wxGridCellAttr::Cell
);
2950 m_attrProvider
->SetAttr(attr
, row
, col
);
2954 // as we take ownership of the pointer and don't store it, we must
2960 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2962 if ( m_attrProvider
)
2964 attr
->SetKind(wxGridCellAttr::Row
);
2965 m_attrProvider
->SetRowAttr(attr
, row
);
2969 // as we take ownership of the pointer and don't store it, we must
2975 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2977 if ( m_attrProvider
)
2979 attr
->SetKind(wxGridCellAttr::Col
);
2980 m_attrProvider
->SetColAttr(attr
, col
);
2984 // as we take ownership of the pointer and don't store it, we must
2990 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2991 size_t WXUNUSED(numRows
) )
2993 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2998 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3000 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3005 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3006 size_t WXUNUSED(numRows
) )
3008 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3013 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3014 size_t WXUNUSED(numCols
) )
3016 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3021 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3023 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3028 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3029 size_t WXUNUSED(numCols
) )
3031 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3037 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3040 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3041 // how much it makes sense to us geeks.
3045 wxString
wxGridTableBase::GetColLabelValue( int col
)
3047 // default col labels are:
3048 // cols 0 to 25 : A-Z
3049 // cols 26 to 675 : AA-ZZ
3054 for ( n
= 1; ; n
++ )
3056 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3058 if ( col
< 0 ) break;
3061 // reverse the string...
3063 for ( i
= 0; i
< n
; i
++ )
3072 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3074 return wxGRID_VALUE_STRING
;
3077 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3078 const wxString
& typeName
)
3080 return typeName
== wxGRID_VALUE_STRING
;
3083 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3085 return CanGetValueAs(row
, col
, typeName
);
3088 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3093 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3098 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3103 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3104 long WXUNUSED(value
) )
3108 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3109 double WXUNUSED(value
) )
3113 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3114 bool WXUNUSED(value
) )
3119 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3120 const wxString
& WXUNUSED(typeName
) )
3125 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3126 const wxString
& WXUNUSED(typeName
),
3127 void* WXUNUSED(value
) )
3131 //////////////////////////////////////////////////////////////////////
3133 // Message class for the grid table to send requests and notifications
3137 wxGridTableMessage::wxGridTableMessage()
3139 m_table
= (wxGridTableBase
*) NULL
;
3145 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3146 int commandInt1
, int commandInt2
)
3150 m_comInt1
= commandInt1
;
3151 m_comInt2
= commandInt2
;
3156 //////////////////////////////////////////////////////////////////////
3158 // A basic grid table for string data. An object of this class will
3159 // created by wxGrid if you don't specify an alternative table class.
3162 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3164 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3166 wxGridStringTable::wxGridStringTable()
3171 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3174 m_data
.Alloc( numRows
);
3177 sa
.Alloc( numCols
);
3178 sa
.Add( wxEmptyString
, numCols
);
3180 m_data
.Add( sa
, numRows
);
3183 wxGridStringTable::~wxGridStringTable()
3187 int wxGridStringTable::GetNumberRows()
3189 return m_data
.GetCount();
3192 int wxGridStringTable::GetNumberCols()
3194 if ( m_data
.GetCount() > 0 )
3195 return m_data
[0].GetCount();
3200 wxString
wxGridStringTable::GetValue( int row
, int col
)
3202 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3204 _T("invalid row or column index in wxGridStringTable") );
3206 return m_data
[row
][col
];
3209 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3211 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3212 _T("invalid row or column index in wxGridStringTable") );
3214 m_data
[row
][col
] = value
;
3217 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3219 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3221 _T("invalid row or column index in wxGridStringTable") );
3223 return (m_data
[row
][col
] == wxEmptyString
);
3226 void wxGridStringTable::Clear()
3229 int numRows
, numCols
;
3231 numRows
= m_data
.GetCount();
3234 numCols
= m_data
[0].GetCount();
3236 for ( row
= 0; row
< numRows
; row
++ )
3238 for ( col
= 0; col
< numCols
; col
++ )
3240 m_data
[row
][col
] = wxEmptyString
;
3247 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3249 size_t curNumRows
= m_data
.GetCount();
3250 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3251 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3253 if ( pos
>= curNumRows
)
3255 return AppendRows( numRows
);
3259 sa
.Alloc( curNumCols
);
3260 sa
.Add( wxEmptyString
, curNumCols
);
3261 m_data
.Insert( sa
, pos
, numRows
);
3264 wxGridTableMessage
msg( this,
3265 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3269 GetView()->ProcessTableMessage( msg
);
3275 bool wxGridStringTable::AppendRows( size_t numRows
)
3277 size_t curNumRows
= m_data
.GetCount();
3278 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3279 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3282 if ( curNumCols
> 0 )
3284 sa
.Alloc( curNumCols
);
3285 sa
.Add( wxEmptyString
, curNumCols
);
3288 m_data
.Add( sa
, numRows
);
3292 wxGridTableMessage
msg( this,
3293 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3296 GetView()->ProcessTableMessage( msg
);
3302 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3304 size_t curNumRows
= m_data
.GetCount();
3306 if ( pos
>= curNumRows
)
3308 wxFAIL_MSG( wxString::Format
3310 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3312 (unsigned long)numRows
,
3313 (unsigned long)curNumRows
3319 if ( numRows
> curNumRows
- pos
)
3321 numRows
= curNumRows
- pos
;
3324 if ( numRows
>= curNumRows
)
3330 m_data
.RemoveAt( pos
, numRows
);
3334 wxGridTableMessage
msg( this,
3335 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3339 GetView()->ProcessTableMessage( msg
);
3345 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3349 size_t curNumRows
= m_data
.GetCount();
3350 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3351 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3353 if ( pos
>= curNumCols
)
3355 return AppendCols( numCols
);
3358 for ( row
= 0; row
< curNumRows
; row
++ )
3360 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3362 m_data
[row
].Insert( wxEmptyString
, col
);
3367 wxGridTableMessage
msg( this,
3368 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3372 GetView()->ProcessTableMessage( msg
);
3378 bool wxGridStringTable::AppendCols( size_t numCols
)
3382 size_t curNumRows
= m_data
.GetCount();
3386 // TODO: something better than this ?
3388 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3393 for ( row
= 0; row
< curNumRows
; row
++ )
3395 m_data
[row
].Add( wxEmptyString
, numCols
);
3400 wxGridTableMessage
msg( this,
3401 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3404 GetView()->ProcessTableMessage( msg
);
3410 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3414 size_t curNumRows
= m_data
.GetCount();
3415 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3416 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3418 if ( pos
>= curNumCols
)
3420 wxFAIL_MSG( wxString::Format
3422 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3424 (unsigned long)numCols
,
3425 (unsigned long)curNumCols
3430 if ( numCols
> curNumCols
- pos
)
3432 numCols
= curNumCols
- pos
;
3435 for ( row
= 0; row
< curNumRows
; row
++ )
3437 if ( numCols
>= curNumCols
)
3439 m_data
[row
].Clear();
3443 m_data
[row
].RemoveAt( pos
, numCols
);
3448 wxGridTableMessage
msg( this,
3449 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3453 GetView()->ProcessTableMessage( msg
);
3459 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3461 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3463 // using default label
3465 return wxGridTableBase::GetRowLabelValue( row
);
3469 return m_rowLabels
[ row
];
3473 wxString
wxGridStringTable::GetColLabelValue( int col
)
3475 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3477 // using default label
3479 return wxGridTableBase::GetColLabelValue( col
);
3483 return m_colLabels
[ col
];
3487 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3489 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3491 int n
= m_rowLabels
.GetCount();
3493 for ( i
= n
; i
<= row
; i
++ )
3495 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3499 m_rowLabels
[row
] = value
;
3502 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3504 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3506 int n
= m_colLabels
.GetCount();
3508 for ( i
= n
; i
<= col
; i
++ )
3510 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3514 m_colLabels
[col
] = value
;
3519 //////////////////////////////////////////////////////////////////////
3520 //////////////////////////////////////////////////////////////////////
3522 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3524 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3525 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3526 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3527 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3528 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3529 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3530 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3533 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3535 const wxPoint
&pos
, const wxSize
&size
)
3536 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3541 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3545 // NO - don't do this because it will set both the x and y origin
3546 // coords to match the parent scrolled window and we just want to
3547 // set the y coord - MB
3549 // m_owner->PrepareDC( dc );
3552 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3553 dc
.SetDeviceOrigin( 0, -y
);
3555 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3556 m_owner
->DrawRowLabels( dc
, rows
);
3560 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3562 m_owner
->ProcessRowLabelMouseEvent( event
);
3566 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3568 m_owner
->GetEventHandler()->ProcessEvent(event
);
3572 // This seems to be required for wxMotif otherwise the mouse
3573 // cursor must be in the cell edit control to get key events
3575 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3577 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3580 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3582 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3585 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3587 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3592 //////////////////////////////////////////////////////////////////////
3594 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3596 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3597 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3598 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3599 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3600 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3601 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3602 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3605 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3607 const wxPoint
&pos
, const wxSize
&size
)
3608 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3613 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3617 // NO - don't do this because it will set both the x and y origin
3618 // coords to match the parent scrolled window and we just want to
3619 // set the x coord - MB
3621 // m_owner->PrepareDC( dc );
3624 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3625 dc
.SetDeviceOrigin( -x
, 0 );
3627 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3628 m_owner
->DrawColLabels( dc
, cols
);
3632 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3634 m_owner
->ProcessColLabelMouseEvent( event
);
3637 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3639 m_owner
->GetEventHandler()->ProcessEvent(event
);
3643 // This seems to be required for wxMotif otherwise the mouse
3644 // cursor must be in the cell edit control to get key events
3646 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3648 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3651 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3653 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3656 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3658 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3662 //////////////////////////////////////////////////////////////////////
3664 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3666 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3667 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3668 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3669 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3670 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3671 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3672 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3675 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3677 const wxPoint
&pos
, const wxSize
&size
)
3678 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3683 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3687 int client_height
= 0;
3688 int client_width
= 0;
3689 GetClientSize( &client_width
, &client_height
);
3691 // VZ: any reason for this ifdef? (FIXME)
3696 rect
.SetWidth( client_width
- 2 );
3697 rect
.SetHeight( client_height
- 2 );
3699 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3701 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3702 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3703 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3704 dc
.DrawLine( 0, 0, client_width
, 0 );
3705 dc
.DrawLine( 0, 0, 0, client_height
);
3707 dc
.SetPen( *wxWHITE_PEN
);
3708 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3709 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3710 #endif // __WXGTK__/!__WXGTK__
3714 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3716 m_owner
->ProcessCornerLabelMouseEvent( event
);
3720 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3722 m_owner
->GetEventHandler()->ProcessEvent(event
);
3725 // This seems to be required for wxMotif otherwise the mouse
3726 // cursor must be in the cell edit control to get key events
3728 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3730 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3733 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3735 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3738 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3740 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3744 //////////////////////////////////////////////////////////////////////
3746 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3748 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3749 EVT_PAINT( wxGridWindow::OnPaint
)
3750 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3751 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3752 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3753 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3754 EVT_CHAR ( wxGridWindow::OnChar
)
3755 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3756 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3757 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3760 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3761 wxGridRowLabelWindow
*rowLblWin
,
3762 wxGridColLabelWindow
*colLblWin
,
3765 const wxSize
&size
)
3766 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3767 wxT("grid window") )
3771 m_rowLabelWin
= rowLblWin
;
3772 m_colLabelWin
= colLblWin
;
3776 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3778 wxPaintDC
dc( this );
3779 m_owner
->PrepareDC( dc
);
3780 wxRegion reg
= GetUpdateRegion();
3781 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3782 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3783 #if WXGRID_DRAW_LINES
3784 m_owner
->DrawAllGridLines( dc
, reg
);
3786 m_owner
->DrawGridSpace( dc
);
3787 m_owner
->DrawHighlight( dc
, DirtyCells
);
3791 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3793 wxWindow::ScrollWindow( dx
, dy
, rect
);
3794 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3795 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3799 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3801 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3804 m_owner
->ProcessGridCellMouseEvent( event
);
3807 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3809 m_owner
->GetEventHandler()->ProcessEvent(event
);
3812 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3813 // cursor must be in the cell edit control to get key events
3815 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3817 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3820 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3822 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3825 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3827 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3830 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3834 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3836 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3840 //////////////////////////////////////////////////////////////////////
3842 // Internal Helper function for computing row or column from some
3843 // (unscrolled) coordinate value, using either
3844 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3845 // of m_rowBottoms/m_ColRights to speed up the search!
3847 // Internal helper macros for simpler use of that function
3849 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3850 const wxArrayInt
& BorderArray
, int nMax
,
3853 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3854 m_minAcceptableColWidth, \
3855 m_colRights, m_numCols, true)
3856 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3857 m_minAcceptableRowHeight, \
3858 m_rowBottoms, m_numRows, true)
3859 /////////////////////////////////////////////////////////////////////
3861 #if wxUSE_EXTENDED_RTTI
3862 WX_DEFINE_FLAGS( wxGridStyle
)
3864 wxBEGIN_FLAGS( wxGridStyle
)
3865 // new style border flags, we put them first to
3866 // use them for streaming out
3867 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3868 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3869 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3870 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3871 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3872 wxFLAGS_MEMBER(wxBORDER_NONE
)
3874 // old style border flags
3875 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3876 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3877 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3878 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3879 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3880 wxFLAGS_MEMBER(wxBORDER
)
3882 // standard window styles
3883 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3884 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3885 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3886 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3887 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3888 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3889 wxFLAGS_MEMBER(wxVSCROLL
)
3890 wxFLAGS_MEMBER(wxHSCROLL
)
3892 wxEND_FLAGS( wxGridStyle
)
3894 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3896 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3897 wxHIDE_PROPERTY( Children
)
3898 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3899 wxEND_PROPERTIES_TABLE()
3901 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3902 wxEND_HANDLERS_TABLE()
3904 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3907 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3910 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3913 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3914 EVT_PAINT( wxGrid::OnPaint
)
3915 EVT_SIZE( wxGrid::OnSize
)
3916 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3917 EVT_KEY_UP( wxGrid::OnKeyUp
)
3918 EVT_CHAR ( wxGrid::OnChar
)
3919 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3924 // in order to make sure that a size event is not
3925 // trigerred in a unfinished state
3926 m_cornerLabelWin
= NULL
;
3927 m_rowLabelWin
= NULL
;
3928 m_colLabelWin
= NULL
;
3932 wxGrid::wxGrid( wxWindow
*parent
,
3937 const wxString
& name
)
3938 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3939 m_colMinWidths(GRID_HASH_SIZE
),
3940 m_rowMinHeights(GRID_HASH_SIZE
)
3943 SetBestFittingSize(size
);
3946 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3947 const wxPoint
& pos
, const wxSize
& size
,
3948 long style
, const wxString
& name
)
3950 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3951 style
| wxWANTS_CHARS
, name
))
3954 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3955 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3958 SetBestFittingSize(size
);
3966 // Must do this or ~wxScrollHelper will pop the wrong event handler
3967 SetTargetWindow(this);
3969 wxSafeDecRef(m_defaultCellAttr
);
3971 #ifdef DEBUG_ATTR_CACHE
3972 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3973 wxPrintf(_T("wxGrid attribute cache statistics: "
3974 "total: %u, hits: %u (%u%%)\n"),
3975 total
, gs_nAttrCacheHits
,
3976 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3982 delete m_typeRegistry
;
3988 // ----- internal init and update functions
3991 // NOTE: If using the default visual attributes works everywhere then this can
3992 // be removed as well as the #else cases below.
3993 #define _USE_VISATTR 0
3996 #include "wx/listbox.h"
3999 void wxGrid::Create()
4001 m_created
= false; // set to true by CreateGrid
4003 m_table
= (wxGridTableBase
*) NULL
;
4006 m_cellEditCtrlEnabled
= false;
4008 m_defaultCellAttr
= new wxGridCellAttr();
4010 // Set default cell attributes
4011 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4012 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4013 m_defaultCellAttr
->SetFont(GetFont());
4014 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4015 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4016 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4019 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4020 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4022 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4023 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4026 m_defaultCellAttr
->SetTextColour(
4027 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4028 m_defaultCellAttr
->SetBackgroundColour(
4029 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4034 m_currentCellCoords
= wxGridNoCellCoords
;
4036 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4037 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4039 // create the type registry
4040 m_typeRegistry
= new wxGridTypeRegistry
;
4043 // subwindow components that make up the wxGrid
4044 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4049 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4054 m_colLabelWin
= new wxGridColLabelWindow( this,
4059 m_gridWin
= new wxGridWindow( this,
4066 SetTargetWindow( m_gridWin
);
4069 wxColour gfg
= gva
.colFg
;
4070 wxColour gbg
= gva
.colBg
;
4071 wxColour lfg
= lva
.colFg
;
4072 wxColour lbg
= lva
.colBg
;
4074 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4075 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4076 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4077 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4079 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4080 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4081 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4082 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4083 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4084 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4086 m_gridWin
->SetOwnForegroundColour(gfg
);
4087 m_gridWin
->SetOwnBackgroundColour(gbg
);
4093 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4094 wxGrid::wxGridSelectionModes selmode
)
4096 wxCHECK_MSG( !m_created
,
4098 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4100 m_numRows
= numRows
;
4101 m_numCols
= numCols
;
4103 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4104 m_table
->SetView( this );
4106 m_selection
= new wxGridSelection( this, selmode
);
4115 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4117 wxCHECK_RET( m_created
,
4118 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4120 m_selection
->SetSelectionMode( selmode
);
4123 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4125 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4126 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4128 return m_selection
->GetSelectionMode();
4131 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4132 wxGrid::wxGridSelectionModes selmode
)
4136 // stop all processing
4141 wxGridTableBase
*t
=m_table
;
4154 m_numRows
= table
->GetNumberRows();
4155 m_numCols
= table
->GetNumberCols();
4158 m_table
->SetView( this );
4161 m_selection
= new wxGridSelection( this, selmode
);
4174 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4175 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4177 if ( m_rowLabelWin
)
4179 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4183 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4186 m_labelTextColour
= wxColour( _T("BLACK") );
4189 m_attrCache
.row
= -1;
4190 m_attrCache
.col
= -1;
4191 m_attrCache
.attr
= NULL
;
4193 // TODO: something better than this ?
4195 m_labelFont
= this->GetFont();
4196 m_labelFont
.SetWeight( wxBOLD
);
4198 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4199 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4201 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4202 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4203 m_colLabelTextOrientation
= wxHORIZONTAL
;
4205 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4206 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4208 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4209 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4211 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4212 m_defaultRowHeight
+= 8;
4214 m_defaultRowHeight
+= 4;
4217 m_gridLineColour
= wxColour( 192,192,192 );
4218 m_gridLinesEnabled
= true;
4219 m_cellHighlightColour
= *wxBLACK
;
4220 m_cellHighlightPenWidth
= 2;
4221 m_cellHighlightROPenWidth
= 1;
4223 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4224 m_winCapture
= (wxWindow
*)NULL
;
4225 m_canDragRowSize
= true;
4226 m_canDragColSize
= true;
4227 m_canDragGridSize
= true;
4228 m_canDragCell
= false;
4230 m_dragRowOrCol
= -1;
4231 m_isDragging
= false;
4232 m_startDragPos
= wxDefaultPosition
;
4234 m_waitForSlowClick
= false;
4236 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4237 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4239 m_currentCellCoords
= wxGridNoCellCoords
;
4241 m_selectingTopLeft
= wxGridNoCellCoords
;
4242 m_selectingBottomRight
= wxGridNoCellCoords
;
4243 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4244 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4246 m_editable
= true; // default for whole grid
4248 m_inOnKeyDown
= false;
4254 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4255 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4258 // ----------------------------------------------------------------------------
4259 // the idea is to call these functions only when necessary because they create
4260 // quite big arrays which eat memory mostly unnecessary - in particular, if
4261 // default widths/heights are used for all rows/columns, we may not use these
4264 // with some extra code, it should be possible to only store the
4265 // widths/heights different from default ones but this will be done later...
4266 // ----------------------------------------------------------------------------
4268 void wxGrid::InitRowHeights()
4270 m_rowHeights
.Empty();
4271 m_rowBottoms
.Empty();
4273 m_rowHeights
.Alloc( m_numRows
);
4274 m_rowBottoms
.Alloc( m_numRows
);
4278 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4280 for ( int i
= 0; i
< m_numRows
; i
++ )
4282 rowBottom
+= m_defaultRowHeight
;
4283 m_rowBottoms
.Add( rowBottom
);
4287 void wxGrid::InitColWidths()
4289 m_colWidths
.Empty();
4290 m_colRights
.Empty();
4292 m_colWidths
.Alloc( m_numCols
);
4293 m_colRights
.Alloc( m_numCols
);
4296 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4298 for ( int i
= 0; i
< m_numCols
; i
++ )
4300 colRight
+= m_defaultColWidth
;
4301 m_colRights
.Add( colRight
);
4305 int wxGrid::GetColWidth(int col
) const
4307 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4310 int wxGrid::GetColLeft(int col
) const
4312 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4313 : m_colRights
[col
] - m_colWidths
[col
];
4316 int wxGrid::GetColRight(int col
) const
4318 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4322 int wxGrid::GetRowHeight(int row
) const
4324 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4327 int wxGrid::GetRowTop(int row
) const
4329 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4330 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4333 int wxGrid::GetRowBottom(int row
) const
4335 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4336 : m_rowBottoms
[row
];
4339 void wxGrid::CalcDimensions()
4342 GetClientSize( &cw
, &ch
);
4344 if ( m_rowLabelWin
->IsShown() )
4345 cw
-= m_rowLabelWidth
;
4346 if ( m_colLabelWin
->IsShown() )
4347 ch
-= m_colLabelHeight
;
4350 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4351 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4353 // take into account editor if shown
4354 if( IsCellEditControlShown() )
4357 int r
= m_currentCellCoords
.GetRow();
4358 int c
= m_currentCellCoords
.GetCol();
4359 int x
= GetColLeft(c
);
4360 int y
= GetRowTop(r
);
4362 // how big is the editor
4363 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4364 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4365 editor
->GetControl()->GetSize(&w2
, &h2
);
4368 if( w2
> w
) w
= w2
;
4369 if( h2
> h
) h
= h2
;
4374 // preserve (more or less) the previous position
4376 GetViewStart( &x
, &y
);
4378 // ensure the position is valid for the new scroll ranges
4380 x
= wxMax( w
- 1, 0 );
4382 y
= wxMax( h
- 1, 0 );
4384 // do set scrollbar parameters
4385 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4386 GetScrollX(w
), GetScrollY(h
), x
, y
,
4387 GetBatchCount() != 0);
4389 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4390 // still must reposition the children
4395 void wxGrid::CalcWindowSizes()
4397 // escape if the window is has not been fully created yet
4399 if ( m_cornerLabelWin
== NULL
)
4403 GetClientSize( &cw
, &ch
);
4405 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4406 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4408 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4409 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4411 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4412 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4414 if ( m_gridWin
&& m_gridWin
->IsShown() )
4415 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4419 // this is called when the grid table sends a message to say that it
4420 // has been redimensioned
4422 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4425 bool result
= false;
4427 // Clear the attribute cache as the attribute might refer to a different
4428 // cell than stored in the cache after adding/removing rows/columns.
4430 // By the same reasoning, the editor should be dismissed if columns are
4431 // added or removed. And for consistency, it should IMHO always be
4432 // removed, not only if the cell "underneath" it actually changes.
4433 // For now, I intentionally do not save the editor's content as the
4434 // cell it might want to save that stuff to might no longer exist.
4435 HideCellEditControl();
4437 // if we were using the default widths/heights so far, we must change them
4439 if ( m_colWidths
.IsEmpty() )
4444 if ( m_rowHeights
.IsEmpty() )
4450 switch ( msg
.GetId() )
4452 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4454 size_t pos
= msg
.GetCommandInt();
4455 int numRows
= msg
.GetCommandInt2();
4457 m_numRows
+= numRows
;
4459 if ( !m_rowHeights
.IsEmpty() )
4461 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4462 m_rowBottoms
.Insert( 0, pos
, numRows
);
4465 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4467 for ( i
= pos
; i
< m_numRows
; i
++ )
4469 bottom
+= m_rowHeights
[i
];
4470 m_rowBottoms
[i
] = bottom
;
4473 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4475 // if we have just inserted cols into an empty grid the current
4476 // cell will be undefined...
4478 SetCurrentCell( 0, 0 );
4482 m_selection
->UpdateRows( pos
, numRows
);
4483 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4485 attrProvider
->UpdateAttrRows( pos
, numRows
);
4487 if ( !GetBatchCount() )
4490 m_rowLabelWin
->Refresh();
4496 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4498 int numRows
= msg
.GetCommandInt();
4499 int oldNumRows
= m_numRows
;
4500 m_numRows
+= numRows
;
4502 if ( !m_rowHeights
.IsEmpty() )
4504 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4505 m_rowBottoms
.Add( 0, numRows
);
4508 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4510 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4512 bottom
+= m_rowHeights
[i
];
4513 m_rowBottoms
[i
] = bottom
;
4516 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4518 // if we have just inserted cols into an empty grid the current
4519 // cell will be undefined...
4521 SetCurrentCell( 0, 0 );
4523 if ( !GetBatchCount() )
4526 m_rowLabelWin
->Refresh();
4532 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4534 size_t pos
= msg
.GetCommandInt();
4535 int numRows
= msg
.GetCommandInt2();
4536 m_numRows
-= numRows
;
4538 if ( !m_rowHeights
.IsEmpty() )
4540 m_rowHeights
.RemoveAt( pos
, numRows
);
4541 m_rowBottoms
.RemoveAt( pos
, numRows
);
4544 for ( i
= 0; i
< m_numRows
; i
++ )
4546 h
+= m_rowHeights
[i
];
4547 m_rowBottoms
[i
] = h
;
4552 m_currentCellCoords
= wxGridNoCellCoords
;
4556 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4557 m_currentCellCoords
.Set( 0, 0 );
4561 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4562 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4564 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4565 // ifdef'd out following patch from Paul Gammans
4567 // No need to touch column attributes, unless we
4568 // removed _all_ rows, in this case, we remove
4569 // all column attributes.
4570 // I hate to do this here, but the
4571 // needed data is not available inside UpdateAttrRows.
4572 if ( !GetNumberRows() )
4573 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4576 if ( !GetBatchCount() )
4579 m_rowLabelWin
->Refresh();
4585 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4587 size_t pos
= msg
.GetCommandInt();
4588 int numCols
= msg
.GetCommandInt2();
4589 m_numCols
+= numCols
;
4591 if ( !m_colWidths
.IsEmpty() )
4593 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4594 m_colRights
.Insert( 0, pos
, numCols
);
4597 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4599 for ( i
= pos
; i
< m_numCols
; i
++ )
4601 right
+= m_colWidths
[i
];
4602 m_colRights
[i
] = right
;
4605 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4607 // if we have just inserted cols into an empty grid the current
4608 // cell will be undefined...
4610 SetCurrentCell( 0, 0 );
4614 m_selection
->UpdateCols( pos
, numCols
);
4615 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4617 attrProvider
->UpdateAttrCols( pos
, numCols
);
4618 if ( !GetBatchCount() )
4621 m_colLabelWin
->Refresh();
4628 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4630 int numCols
= msg
.GetCommandInt();
4631 int oldNumCols
= m_numCols
;
4632 m_numCols
+= numCols
;
4633 if ( !m_colWidths
.IsEmpty() )
4635 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4636 m_colRights
.Add( 0, numCols
);
4639 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4641 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4643 right
+= m_colWidths
[i
];
4644 m_colRights
[i
] = right
;
4647 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4649 // if we have just inserted cols into an empty grid the current
4650 // cell will be undefined...
4652 SetCurrentCell( 0, 0 );
4654 if ( !GetBatchCount() )
4657 m_colLabelWin
->Refresh();
4663 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4665 size_t pos
= msg
.GetCommandInt();
4666 int numCols
= msg
.GetCommandInt2();
4667 m_numCols
-= numCols
;
4669 if ( !m_colWidths
.IsEmpty() )
4671 m_colWidths
.RemoveAt( pos
, numCols
);
4672 m_colRights
.RemoveAt( pos
, numCols
);
4675 for ( i
= 0; i
< m_numCols
; i
++ )
4677 w
+= m_colWidths
[i
];
4683 m_currentCellCoords
= wxGridNoCellCoords
;
4687 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4688 m_currentCellCoords
.Set( 0, 0 );
4692 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4693 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4695 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4696 // ifdef'd out following patch from Paul Gammans
4698 // No need to touch row attributes, unless we
4699 // removed _all_ columns, in this case, we remove
4700 // all row attributes.
4701 // I hate to do this here, but the
4702 // needed data is not available inside UpdateAttrCols.
4703 if ( !GetNumberCols() )
4704 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4707 if ( !GetBatchCount() )
4710 m_colLabelWin
->Refresh();
4717 if (result
&& !GetBatchCount() )
4718 m_gridWin
->Refresh();
4723 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4725 wxRegionIterator
iter( reg
);
4728 wxArrayInt rowlabels
;
4735 // TODO: remove this when we can...
4736 // There is a bug in wxMotif that gives garbage update
4737 // rectangles if you jump-scroll a long way by clicking the
4738 // scrollbar with middle button. This is a work-around
4740 #if defined(__WXMOTIF__)
4742 m_gridWin
->GetClientSize( &cw
, &ch
);
4743 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4744 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4747 // logical bounds of update region
4750 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4751 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4753 // find the row labels within these bounds
4756 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4758 if ( GetRowBottom(row
) < top
)
4761 if ( GetRowTop(row
) > bottom
)
4764 rowlabels
.Add( row
);
4774 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4776 wxRegionIterator
iter( reg
);
4779 wxArrayInt colLabels
;
4786 // TODO: remove this when we can...
4787 // There is a bug in wxMotif that gives garbage update
4788 // rectangles if you jump-scroll a long way by clicking the
4789 // scrollbar with middle button. This is a work-around
4791 #if defined(__WXMOTIF__)
4793 m_gridWin
->GetClientSize( &cw
, &ch
);
4794 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4795 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4798 // logical bounds of update region
4801 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4802 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4804 // find the cells within these bounds
4807 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4809 if ( GetColRight(col
) < left
)
4812 if ( GetColLeft(col
) > right
)
4815 colLabels
.Add( col
);
4824 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4826 wxRegionIterator
iter( reg
);
4829 wxGridCellCoordsArray cellsExposed
;
4831 int left
, top
, right
, bottom
;
4836 // TODO: remove this when we can...
4837 // There is a bug in wxMotif that gives garbage update
4838 // rectangles if you jump-scroll a long way by clicking the
4839 // scrollbar with middle button. This is a work-around
4841 #if defined(__WXMOTIF__)
4843 m_gridWin
->GetClientSize( &cw
, &ch
);
4844 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4845 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4846 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4847 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4850 // logical bounds of update region
4852 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4853 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4855 // find the cells within these bounds
4858 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4860 if ( GetRowBottom(row
) <= top
)
4863 if ( GetRowTop(row
) > bottom
)
4866 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4868 if ( GetColRight(col
) <= left
)
4871 if ( GetColLeft(col
) > right
)
4874 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4881 return cellsExposed
;
4885 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4888 wxPoint
pos( event
.GetPosition() );
4889 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4891 if ( event
.Dragging() )
4895 m_isDragging
= true;
4896 m_rowLabelWin
->CaptureMouse();
4899 if ( event
.LeftIsDown() )
4901 switch( m_cursorMode
)
4903 case WXGRID_CURSOR_RESIZE_ROW
:
4905 int cw
, ch
, left
, dummy
;
4906 m_gridWin
->GetClientSize( &cw
, &ch
);
4907 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4909 wxClientDC
dc( m_gridWin
);
4912 GetRowTop(m_dragRowOrCol
) +
4913 GetRowMinimalHeight(m_dragRowOrCol
) );
4914 dc
.SetLogicalFunction(wxINVERT
);
4915 if ( m_dragLastPos
>= 0 )
4917 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4919 dc
.DrawLine( left
, y
, left
+cw
, y
);
4924 case WXGRID_CURSOR_SELECT_ROW
:
4926 if ( (row
= YToRow( y
)) >= 0 )
4930 m_selection
->SelectRow( row
,
4931 event
.ControlDown(),
4940 // default label to suppress warnings about "enumeration value
4941 // 'xxx' not handled in switch
4949 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4954 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4955 m_isDragging
= false;
4958 // ------------ Entering or leaving the window
4960 if ( event
.Entering() || event
.Leaving() )
4962 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4966 // ------------ Left button pressed
4968 else if ( event
.LeftDown() )
4970 // don't send a label click event for a hit on the
4971 // edge of the row label - this is probably the user
4972 // wanting to resize the row
4974 if ( YToEdgeOfRow(y
) < 0 )
4978 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4980 if ( !event
.ShiftDown() && !event
.ControlDown() )
4984 if ( event
.ShiftDown() )
4986 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4989 GetNumberCols() - 1,
4990 event
.ControlDown(),
4997 m_selection
->SelectRow( row
,
4998 event
.ControlDown(),
5005 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5010 // starting to drag-resize a row
5012 if ( CanDragRowSize() )
5013 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5018 // ------------ Left double click
5020 else if (event
.LeftDClick() )
5022 int row
= YToEdgeOfRow(y
);
5027 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5029 // no default action at the moment
5034 // adjust row height depending on label text
5035 AutoSizeRowLabelSize( row
);
5037 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5043 // ------------ Left button released
5045 else if ( event
.LeftUp() )
5047 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5049 DoEndDragResizeRow();
5051 // Note: we are ending the event *after* doing
5052 // default processing in this case
5054 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5057 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5062 // ------------ Right button down
5064 else if ( event
.RightDown() )
5068 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5070 // no default action at the moment
5075 // ------------ Right double click
5077 else if ( event
.RightDClick() )
5081 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5083 // no default action at the moment
5088 // ------------ No buttons down and mouse moving
5090 else if ( event
.Moving() )
5092 m_dragRowOrCol
= YToEdgeOfRow( y
);
5093 if ( m_dragRowOrCol
>= 0 )
5095 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5097 // don't capture the mouse yet
5098 if ( CanDragRowSize() )
5099 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5102 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5104 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5110 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5113 wxPoint
pos( event
.GetPosition() );
5114 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5116 if ( event
.Dragging() )
5120 m_isDragging
= true;
5121 m_colLabelWin
->CaptureMouse();
5124 if ( event
.LeftIsDown() )
5126 switch( m_cursorMode
)
5128 case WXGRID_CURSOR_RESIZE_COL
:
5130 int cw
, ch
, dummy
, top
;
5131 m_gridWin
->GetClientSize( &cw
, &ch
);
5132 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5134 wxClientDC
dc( m_gridWin
);
5137 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5138 GetColMinimalWidth(m_dragRowOrCol
));
5139 dc
.SetLogicalFunction(wxINVERT
);
5140 if ( m_dragLastPos
>= 0 )
5142 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5144 dc
.DrawLine( x
, top
, x
, top
+ch
);
5149 case WXGRID_CURSOR_SELECT_COL
:
5151 if ( (col
= XToCol( x
)) >= 0 )
5155 m_selection
->SelectCol( col
,
5156 event
.ControlDown(),
5165 // default label to suppress warnings about "enumeration value
5166 // 'xxx' not handled in switch
5174 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5179 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5180 m_isDragging
= false;
5183 // ------------ Entering or leaving the window
5185 if ( event
.Entering() || event
.Leaving() )
5187 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5191 // ------------ Left button pressed
5193 else if ( event
.LeftDown() )
5195 // don't send a label click event for a hit on the
5196 // edge of the col label - this is probably the user
5197 // wanting to resize the col
5199 if ( XToEdgeOfCol(x
) < 0 )
5203 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5205 if ( !event
.ShiftDown() && !event
.ControlDown() )
5209 if ( event
.ShiftDown() )
5211 m_selection
->SelectBlock( 0,
5212 m_currentCellCoords
.GetCol(),
5213 GetNumberRows() - 1, col
,
5214 event
.ControlDown(),
5221 m_selection
->SelectCol( col
,
5222 event
.ControlDown(),
5229 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5234 // starting to drag-resize a col
5236 if ( CanDragColSize() )
5237 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5242 // ------------ Left double click
5244 if ( event
.LeftDClick() )
5246 int col
= XToEdgeOfCol(x
);
5251 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5253 // no default action at the moment
5258 // adjust column width depending on label text
5259 AutoSizeColLabelSize( col
);
5261 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5267 // ------------ Left button released
5269 else if ( event
.LeftUp() )
5271 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5273 DoEndDragResizeCol();
5275 // Note: we are ending the event *after* doing
5276 // default processing in this case
5278 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5281 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5286 // ------------ Right button down
5288 else if ( event
.RightDown() )
5292 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5294 // no default action at the moment
5299 // ------------ Right double click
5301 else if ( event
.RightDClick() )
5305 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5307 // no default action at the moment
5312 // ------------ No buttons down and mouse moving
5314 else if ( event
.Moving() )
5316 m_dragRowOrCol
= XToEdgeOfCol( x
);
5317 if ( m_dragRowOrCol
>= 0 )
5319 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5321 // don't capture the cursor yet
5322 if ( CanDragColSize() )
5323 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5326 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5328 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5334 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5336 if ( event
.LeftDown() )
5338 // indicate corner label by having both row and
5341 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5347 else if ( event
.LeftDClick() )
5349 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5352 else if ( event
.RightDown() )
5354 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5356 // no default action at the moment
5360 else if ( event
.RightDClick() )
5362 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5364 // no default action at the moment
5369 void wxGrid::ChangeCursorMode(CursorMode mode
,
5374 static const wxChar
*cursorModes
[] =
5383 wxLogTrace(_T("grid"),
5384 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5385 win
== m_colLabelWin
? _T("colLabelWin")
5386 : win
? _T("rowLabelWin")
5388 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5389 #endif // __WXDEBUG__
5391 if ( mode
== m_cursorMode
&&
5392 win
== m_winCapture
&&
5393 captureMouse
== (m_winCapture
!= NULL
))
5398 // by default use the grid itself
5404 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5405 m_winCapture
= (wxWindow
*)NULL
;
5408 m_cursorMode
= mode
;
5410 switch ( m_cursorMode
)
5412 case WXGRID_CURSOR_RESIZE_ROW
:
5413 win
->SetCursor( m_rowResizeCursor
);
5416 case WXGRID_CURSOR_RESIZE_COL
:
5417 win
->SetCursor( m_colResizeCursor
);
5421 win
->SetCursor( *wxSTANDARD_CURSOR
);
5424 // we need to capture mouse when resizing
5425 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5426 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5428 if ( captureMouse
&& resize
)
5430 win
->CaptureMouse();
5435 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5438 wxPoint
pos( event
.GetPosition() );
5439 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5441 wxGridCellCoords coords
;
5442 XYToCell( x
, y
, coords
);
5444 int cell_rows
, cell_cols
;
5445 bool isFirstDrag
= !m_isDragging
;
5446 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5447 if ((cell_rows
< 0) || (cell_cols
< 0))
5449 coords
.SetRow(coords
.GetRow() + cell_rows
);
5450 coords
.SetCol(coords
.GetCol() + cell_cols
);
5453 if ( event
.Dragging() )
5455 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5457 // Don't start doing anything until the mouse has been drug at
5458 // least 3 pixels in any direction...
5461 if (m_startDragPos
== wxDefaultPosition
)
5463 m_startDragPos
= pos
;
5466 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5470 m_isDragging
= true;
5471 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5473 // Hide the edit control, so it
5474 // won't interfer with drag-shrinking.
5475 if ( IsCellEditControlShown() )
5477 HideCellEditControl();
5478 SaveEditControlValue();
5481 // Have we captured the mouse yet?
5484 m_winCapture
= m_gridWin
;
5485 m_winCapture
->CaptureMouse();
5488 if ( coords
!= wxGridNoCellCoords
)
5490 if ( event
.ControlDown() )
5492 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5493 m_selectingKeyboard
= coords
;
5494 HighlightBlock ( m_selectingKeyboard
, coords
);
5496 else if ( CanDragCell() )
5500 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5501 m_selectingKeyboard
= coords
;
5503 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5511 if ( !IsSelection() )
5513 HighlightBlock( coords
, coords
);
5517 HighlightBlock( m_currentCellCoords
, coords
);
5521 if (! IsVisible(coords
))
5523 MakeCellVisible(coords
);
5524 // TODO: need to introduce a delay or something here. The
5525 // scrolling is way to fast, at least on MSW - also on GTK.
5529 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5531 int cw
, ch
, left
, dummy
;
5532 m_gridWin
->GetClientSize( &cw
, &ch
);
5533 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5535 wxClientDC
dc( m_gridWin
);
5537 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5538 GetRowMinimalHeight(m_dragRowOrCol
) );
5539 dc
.SetLogicalFunction(wxINVERT
);
5540 if ( m_dragLastPos
>= 0 )
5542 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5544 dc
.DrawLine( left
, y
, left
+cw
, y
);
5547 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5549 int cw
, ch
, dummy
, top
;
5550 m_gridWin
->GetClientSize( &cw
, &ch
);
5551 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5553 wxClientDC
dc( m_gridWin
);
5555 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5556 GetColMinimalWidth(m_dragRowOrCol
) );
5557 dc
.SetLogicalFunction(wxINVERT
);
5558 if ( m_dragLastPos
>= 0 )
5560 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5562 dc
.DrawLine( x
, top
, x
, top
+ch
);
5569 m_isDragging
= false;
5570 m_startDragPos
= wxDefaultPosition
;
5572 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5573 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5576 if ( event
.Entering() || event
.Leaving() )
5578 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5579 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5584 // ------------ Left button pressed
5586 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5588 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5593 if ( !event
.ControlDown() )
5595 if ( event
.ShiftDown() )
5599 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5600 m_currentCellCoords
.GetCol(),
5603 event
.ControlDown(),
5609 else if ( XToEdgeOfCol(x
) < 0 &&
5610 YToEdgeOfRow(y
) < 0 )
5612 DisableCellEditControl();
5613 MakeCellVisible( coords
);
5615 if ( event
.ControlDown() )
5619 m_selection
->ToggleCellSelection( coords
.GetRow(),
5621 event
.ControlDown(),
5626 m_selectingTopLeft
= wxGridNoCellCoords
;
5627 m_selectingBottomRight
= wxGridNoCellCoords
;
5628 m_selectingKeyboard
= coords
;
5632 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5633 SetCurrentCell( coords
);
5636 if ( m_selection
->GetSelectionMode() !=
5637 wxGrid::wxGridSelectCells
)
5639 HighlightBlock( coords
, coords
);
5648 // ------------ Left double click
5650 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5652 DisableCellEditControl();
5654 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5656 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5661 // we want double click to select a cell and start editing
5662 // (i.e. to behave in same way as sequence of two slow clicks):
5663 m_waitForSlowClick
= true;
5670 // ------------ Left button released
5672 else if ( event
.LeftUp() )
5674 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5678 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5679 m_winCapture
= NULL
;
5682 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5685 EnableCellEditControl();
5687 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5688 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5689 editor
->StartingClick();
5693 m_waitForSlowClick
= false;
5695 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5696 m_selectingBottomRight
!= wxGridNoCellCoords
)
5700 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5701 m_selectingTopLeft
.GetCol(),
5702 m_selectingBottomRight
.GetRow(),
5703 m_selectingBottomRight
.GetCol(),
5704 event
.ControlDown(),
5710 m_selectingTopLeft
= wxGridNoCellCoords
;
5711 m_selectingBottomRight
= wxGridNoCellCoords
;
5713 // Show the edit control, if it has been hidden for
5715 ShowCellEditControl();
5718 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5720 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5721 DoEndDragResizeRow();
5723 // Note: we are ending the event *after* doing
5724 // default processing in this case
5726 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5728 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5730 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5731 DoEndDragResizeCol();
5733 // Note: we are ending the event *after* doing
5734 // default processing in this case
5736 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5743 // ------------ Right button down
5745 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5747 DisableCellEditControl();
5748 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5753 // no default action at the moment
5758 // ------------ Right double click
5760 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5762 DisableCellEditControl();
5763 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5768 // no default action at the moment
5772 // ------------ Moving and no button action
5774 else if ( event
.Moving() && !event
.IsButton() )
5776 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5778 // out of grid cell area
5779 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5783 int dragRow
= YToEdgeOfRow( y
);
5784 int dragCol
= XToEdgeOfCol( x
);
5786 // Dragging on the corner of a cell to resize in both
5787 // directions is not implemented yet...
5789 if ( dragRow
>= 0 && dragCol
>= 0 )
5791 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5797 m_dragRowOrCol
= dragRow
;
5799 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5801 if ( CanDragRowSize() && CanDragGridSize() )
5802 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5807 m_dragRowOrCol
= dragCol
;
5815 m_dragRowOrCol
= dragCol
;
5817 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5819 if ( CanDragColSize() && CanDragGridSize() )
5820 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5826 // Neither on a row or col edge
5828 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5830 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5836 void wxGrid::DoEndDragResizeRow()
5838 if ( m_dragLastPos
>= 0 )
5840 // erase the last line and resize the row
5842 int cw
, ch
, left
, dummy
;
5843 m_gridWin
->GetClientSize( &cw
, &ch
);
5844 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5846 wxClientDC
dc( m_gridWin
);
5848 dc
.SetLogicalFunction( wxINVERT
);
5849 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5850 HideCellEditControl();
5851 SaveEditControlValue();
5853 int rowTop
= GetRowTop(m_dragRowOrCol
);
5854 SetRowSize( m_dragRowOrCol
,
5855 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5857 if ( !GetBatchCount() )
5859 // Only needed to get the correct rect.y:
5860 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5862 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5863 rect
.width
= m_rowLabelWidth
;
5864 rect
.height
= ch
- rect
.y
;
5865 m_rowLabelWin
->Refresh( true, &rect
);
5867 // if there is a multicell block, paint all of it
5870 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5871 int leftCol
= XToCol(left
);
5872 int rightCol
= internalXToCol(left
+cw
);
5875 for (i
=leftCol
; i
<rightCol
; i
++)
5877 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5878 if (cell_rows
< subtract_rows
)
5879 subtract_rows
= cell_rows
;
5881 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5882 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5883 rect
.height
= ch
- rect
.y
;
5886 m_gridWin
->Refresh( false, &rect
);
5889 ShowCellEditControl();
5894 void wxGrid::DoEndDragResizeCol()
5896 if ( m_dragLastPos
>= 0 )
5898 // erase the last line and resize the col
5900 int cw
, ch
, dummy
, top
;
5901 m_gridWin
->GetClientSize( &cw
, &ch
);
5902 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5904 wxClientDC
dc( m_gridWin
);
5906 dc
.SetLogicalFunction( wxINVERT
);
5907 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5908 HideCellEditControl();
5909 SaveEditControlValue();
5911 int colLeft
= GetColLeft(m_dragRowOrCol
);
5912 SetColSize( m_dragRowOrCol
,
5913 wxMax( m_dragLastPos
- colLeft
,
5914 GetColMinimalWidth(m_dragRowOrCol
) ) );
5916 if ( !GetBatchCount() )
5918 // Only needed to get the correct rect.x:
5919 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5921 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5922 rect
.width
= cw
- rect
.x
;
5923 rect
.height
= m_colLabelHeight
;
5924 m_colLabelWin
->Refresh( true, &rect
);
5926 // if there is a multicell block, paint all of it
5929 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5930 int topRow
= YToRow(top
);
5931 int bottomRow
= internalYToRow(top
+cw
);
5934 for (i
=topRow
; i
<bottomRow
; i
++)
5936 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5937 if (cell_cols
< subtract_cols
)
5938 subtract_cols
= cell_cols
;
5940 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5941 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5942 rect
.width
= cw
- rect
.x
;
5945 m_gridWin
->Refresh( false, &rect
);
5948 ShowCellEditControl();
5955 // ------ interaction with data model
5957 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5959 switch ( msg
.GetId() )
5961 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5962 return GetModelValues();
5964 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5965 return SetModelValues();
5967 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5968 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5969 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5970 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5971 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5972 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5973 return Redimension( msg
);
5982 // The behaviour of this function depends on the grid table class
5983 // Clear() function. For the default wxGridStringTable class the
5984 // behavious is to replace all cell contents with wxEmptyString but
5985 // not to change the number of rows or cols.
5987 void wxGrid::ClearGrid()
5991 if (IsCellEditControlEnabled())
5992 DisableCellEditControl();
5995 if ( !GetBatchCount() ) m_gridWin
->Refresh();
6000 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6002 // TODO: something with updateLabels flag
6006 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6012 if (IsCellEditControlEnabled())
6013 DisableCellEditControl();
6015 bool done
= m_table
->InsertRows( pos
, numRows
);
6018 // the table will have sent the results of the insert row
6019 // operation to this view object as a grid table message
6025 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6027 // TODO: something with updateLabels flag
6031 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6037 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6039 // the table will have sent the results of the append row
6040 // operation to this view object as a grid table message
6046 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6048 // TODO: something with updateLabels flag
6052 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6058 if (IsCellEditControlEnabled())
6059 DisableCellEditControl();
6061 bool done
= m_table
->DeleteRows( pos
, numRows
);
6063 // the table will have sent the results of the delete row
6064 // operation to this view object as a grid table message
6070 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6072 // TODO: something with updateLabels flag
6076 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6082 if (IsCellEditControlEnabled())
6083 DisableCellEditControl();
6085 bool done
= m_table
->InsertCols( pos
, numCols
);
6087 // the table will have sent the results of the insert col
6088 // operation to this view object as a grid table message
6094 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6096 // TODO: something with updateLabels flag
6100 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6106 bool done
= m_table
->AppendCols( numCols
);
6108 // the table will have sent the results of the append col
6109 // operation to this view object as a grid table message
6115 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6117 // TODO: something with updateLabels flag
6121 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6127 if (IsCellEditControlEnabled())
6128 DisableCellEditControl();
6130 bool done
= m_table
->DeleteCols( pos
, numCols
);
6132 // the table will have sent the results of the delete col
6133 // operation to this view object as a grid table message
6141 // ----- event handlers
6144 // Generate a grid event based on a mouse event and
6145 // return the result of ProcessEvent()
6147 int wxGrid::SendEvent( const wxEventType type
,
6149 wxMouseEvent
& mouseEv
)
6154 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6156 int rowOrCol
= (row
== -1 ? col
: row
);
6158 wxGridSizeEvent
gridEvt( GetId(),
6162 mouseEv
.GetX() + GetRowLabelSize(),
6163 mouseEv
.GetY() + GetColLabelSize(),
6164 mouseEv
.ControlDown(),
6165 mouseEv
.ShiftDown(),
6167 mouseEv
.MetaDown() );
6169 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6170 vetoed
= !gridEvt
.IsAllowed();
6172 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6174 // Right now, it should _never_ end up here!
6175 wxGridRangeSelectEvent
gridEvt( GetId(),
6179 m_selectingBottomRight
,
6181 mouseEv
.ControlDown(),
6182 mouseEv
.ShiftDown(),
6184 mouseEv
.MetaDown() );
6186 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6187 vetoed
= !gridEvt
.IsAllowed();
6191 wxGridEvent
gridEvt( GetId(),
6195 mouseEv
.GetX() + GetRowLabelSize(),
6196 mouseEv
.GetY() + GetColLabelSize(),
6198 mouseEv
.ControlDown(),
6199 mouseEv
.ShiftDown(),
6201 mouseEv
.MetaDown() );
6202 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6203 vetoed
= !gridEvt
.IsAllowed();
6206 // A Veto'd event may not be `claimed' so test this first
6207 if (vetoed
) return -1;
6208 return claimed
? 1 : 0;
6212 // Generate a grid event of specified type and return the result
6213 // of ProcessEvent().
6215 int wxGrid::SendEvent( const wxEventType type
,
6221 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6223 int rowOrCol
= (row
== -1 ? col
: row
);
6225 wxGridSizeEvent
gridEvt( GetId(),
6230 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6231 vetoed
= !gridEvt
.IsAllowed();
6235 wxGridEvent
gridEvt( GetId(),
6240 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6241 vetoed
= !gridEvt
.IsAllowed();
6244 // A Veto'd event may not be `claimed' so test this first
6245 if (vetoed
) return -1;
6246 return claimed
? 1 : 0;
6250 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6252 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6255 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6257 // Don't do anything if between Begin/EndBatch...
6258 // EndBatch() will do all this on the last nested one anyway.
6259 if (! GetBatchCount())
6261 // Refresh to get correct scrolled position:
6262 wxScrolledWindow::Refresh(eraseb
,rect
);
6266 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6267 int width_label
, width_cell
, height_label
, height_cell
;
6270 //Copy rectangle can get scroll offsets..
6271 rect_x
= rect
->GetX();
6272 rect_y
= rect
->GetY();
6273 rectWidth
= rect
->GetWidth();
6274 rectHeight
= rect
->GetHeight();
6276 width_label
= m_rowLabelWidth
- rect_x
;
6277 if (width_label
> rectWidth
) width_label
= rectWidth
;
6279 height_label
= m_colLabelHeight
- rect_y
;
6280 if (height_label
> rectHeight
) height_label
= rectHeight
;
6282 if (rect_x
> m_rowLabelWidth
)
6284 x
= rect_x
- m_rowLabelWidth
;
6285 width_cell
= rectWidth
;
6290 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6293 if (rect_y
> m_colLabelHeight
)
6295 y
= rect_y
- m_colLabelHeight
;
6296 height_cell
= rectHeight
;
6301 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6304 // Paint corner label part intersecting rect.
6305 if ( width_label
> 0 && height_label
> 0 )
6307 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6308 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6311 // Paint col labels part intersecting rect.
6312 if ( width_cell
> 0 && height_label
> 0 )
6314 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6315 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6318 // Paint row labels part intersecting rect.
6319 if ( width_label
> 0 && height_cell
> 0 )
6321 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6322 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6325 // Paint cell area part intersecting rect.
6326 if ( width_cell
> 0 && height_cell
> 0 )
6328 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6329 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6334 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6335 m_colLabelWin
->Refresh(eraseb
, NULL
);
6336 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6337 m_gridWin
->Refresh(eraseb
, NULL
);
6342 void wxGrid::OnSize( wxSizeEvent
& event
)
6344 // position the child windows
6347 // don't call CalcDimensions() from here, the base class handles the size
6353 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6355 if ( m_inOnKeyDown
)
6357 // shouldn't be here - we are going round in circles...
6359 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6362 m_inOnKeyDown
= true;
6364 // propagate the event up and see if it gets processed
6366 wxWindow
*parent
= GetParent();
6367 wxKeyEvent
keyEvt( event
);
6368 keyEvt
.SetEventObject( parent
);
6370 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6373 // try local handlers
6375 switch ( event
.GetKeyCode() )
6378 if ( event
.ControlDown() )
6380 MoveCursorUpBlock( event
.ShiftDown() );
6384 MoveCursorUp( event
.ShiftDown() );
6389 if ( event
.ControlDown() )
6391 MoveCursorDownBlock( event
.ShiftDown() );
6395 MoveCursorDown( event
.ShiftDown() );
6400 if ( event
.ControlDown() )
6402 MoveCursorLeftBlock( event
.ShiftDown() );
6406 MoveCursorLeft( event
.ShiftDown() );
6411 if ( event
.ControlDown() )
6413 MoveCursorRightBlock( event
.ShiftDown() );
6417 MoveCursorRight( event
.ShiftDown() );
6422 case WXK_NUMPAD_ENTER
:
6423 if ( event
.ControlDown() )
6425 event
.Skip(); // to let the edit control have the return
6429 if ( GetGridCursorRow() < GetNumberRows()-1 )
6431 MoveCursorDown( event
.ShiftDown() );
6435 // at the bottom of a column
6436 DisableCellEditControl();
6446 if (event
.ShiftDown())
6448 if ( GetGridCursorCol() > 0 )
6450 MoveCursorLeft( false );
6455 DisableCellEditControl();
6460 if ( GetGridCursorCol() < GetNumberCols()-1 )
6462 MoveCursorRight( false );
6467 DisableCellEditControl();
6473 if ( event
.ControlDown() )
6475 MakeCellVisible( 0, 0 );
6476 SetCurrentCell( 0, 0 );
6485 if ( event
.ControlDown() )
6487 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6488 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6505 if ( event
.ControlDown() )
6509 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6510 m_currentCellCoords
.GetCol(),
6511 event
.ControlDown(),
6518 if ( !IsEditable() )
6520 MoveCursorRight( false );
6523 // Otherwise fall through to default
6531 m_inOnKeyDown
= false;
6534 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6536 // try local handlers
6538 if ( event
.GetKeyCode() == WXK_SHIFT
)
6540 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6541 m_selectingBottomRight
!= wxGridNoCellCoords
)
6545 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6546 m_selectingTopLeft
.GetCol(),
6547 m_selectingBottomRight
.GetRow(),
6548 m_selectingBottomRight
.GetCol(),
6549 event
.ControlDown(),
6556 m_selectingTopLeft
= wxGridNoCellCoords
;
6557 m_selectingBottomRight
= wxGridNoCellCoords
;
6558 m_selectingKeyboard
= wxGridNoCellCoords
;
6562 void wxGrid::OnChar( wxKeyEvent
& event
)
6564 // is it possible to edit the current cell at all?
6565 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6567 // yes, now check whether the cells editor accepts the key
6568 int row
= m_currentCellCoords
.GetRow();
6569 int col
= m_currentCellCoords
.GetCol();
6570 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6571 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6573 // <F2> is special and will always start editing, for
6574 // other keys - ask the editor itself
6575 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6576 || editor
->IsAcceptedKey(event
) )
6578 // ensure cell is visble
6579 MakeCellVisible(row
, col
);
6580 EnableCellEditControl();
6582 // a problem can arise if the cell is not completely
6583 // visible (even after calling MakeCellVisible the
6584 // control is not created and calling StartingKey will
6586 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6587 editor
->StartingKey(event
);
6604 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6608 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6610 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6612 // the event has been intercepted - do nothing
6616 wxClientDC
dc(m_gridWin
);
6619 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6621 DisableCellEditControl();
6623 if ( IsVisible( m_currentCellCoords
, false ) )
6626 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6627 if ( !m_gridLinesEnabled
)
6635 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6637 // Otherwise refresh redraws the highlight!
6638 m_currentCellCoords
= coords
;
6640 DrawGridCellArea(dc
,cells
);
6641 DrawAllGridLines( dc
, r
);
6645 m_currentCellCoords
= coords
;
6647 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6648 DrawCellHighlight(dc
, attr
);
6653 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6656 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6660 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6663 rightCol
= GetNumberCols() - 1;
6665 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6668 bottomRow
= GetNumberRows() - 1;
6672 if ( topRow
> bottomRow
)
6679 if ( leftCol
> rightCol
)
6686 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6687 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6689 // First the case that we selected a completely new area
6690 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6691 m_selectingBottomRight
== wxGridNoCellCoords
)
6694 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6695 wxGridCellCoords ( bottomRow
, rightCol
) );
6696 m_gridWin
->Refresh( false, &rect
);
6698 // Now handle changing an existing selection area.
6699 else if ( m_selectingTopLeft
!= updateTopLeft
||
6700 m_selectingBottomRight
!= updateBottomRight
)
6702 // Compute two optimal update rectangles:
6703 // Either one rectangle is a real subset of the
6704 // other, or they are (almost) disjoint!
6706 bool need_refresh
[4];
6710 need_refresh
[3] = false;
6713 // Store intermediate values
6714 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6715 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6716 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6717 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6719 // Determine the outer/inner coordinates.
6720 if (oldLeft
> leftCol
)
6726 if (oldTop
> topRow
)
6732 if (oldRight
< rightCol
)
6735 oldRight
= rightCol
;
6738 if (oldBottom
< bottomRow
)
6741 oldBottom
= bottomRow
;
6745 // Now, either the stuff marked old is the outer
6746 // rectangle or we don't have a situation where one
6747 // is contained in the other.
6749 if ( oldLeft
< leftCol
)
6751 // Refresh the newly selected or deselected
6752 // area to the left of the old or new selection.
6753 need_refresh
[0] = true;
6754 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6756 wxGridCellCoords ( oldBottom
,
6760 if ( oldTop
< topRow
)
6762 // Refresh the newly selected or deselected
6763 // area above the old or new selection.
6764 need_refresh
[1] = true;
6765 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6767 wxGridCellCoords ( topRow
- 1,
6771 if ( oldRight
> rightCol
)
6773 // Refresh the newly selected or deselected
6774 // area to the right of the old or new selection.
6775 need_refresh
[2] = true;
6776 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6778 wxGridCellCoords ( oldBottom
,
6782 if ( oldBottom
> bottomRow
)
6784 // Refresh the newly selected or deselected
6785 // area below the old or new selection.
6786 need_refresh
[3] = true;
6787 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6789 wxGridCellCoords ( oldBottom
,
6793 // various Refresh() calls
6794 for (i
= 0; i
< 4; i
++ )
6795 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6796 m_gridWin
->Refresh( false, &(rect
[i
]) );
6799 m_selectingTopLeft
= updateTopLeft
;
6800 m_selectingBottomRight
= updateBottomRight
;
6804 // ------ functions to get/send data (see also public functions)
6807 bool wxGrid::GetModelValues()
6809 // Hide the editor, so it won't hide a changed value.
6810 HideCellEditControl();
6814 // all we need to do is repaint the grid
6816 m_gridWin
->Refresh();
6824 bool wxGrid::SetModelValues()
6828 // Disable the editor, so it won't hide a changed value.
6829 // Do we also want to save the current value of the editor first?
6831 DisableCellEditControl();
6835 for ( row
= 0; row
< m_numRows
; row
++ )
6837 for ( col
= 0; col
< m_numCols
; col
++ )
6839 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6851 // Note - this function only draws cells that are in the list of
6852 // exposed cells (usually set from the update region by
6853 // CalcExposedCells)
6855 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6857 if ( !m_numRows
|| !m_numCols
) return;
6859 int i
, numCells
= cells
.GetCount();
6860 int row
, col
, cell_rows
, cell_cols
;
6861 wxGridCellCoordsArray redrawCells
;
6863 for ( i
= numCells
-1; i
>= 0; i
-- )
6865 row
= cells
[i
].GetRow();
6866 col
= cells
[i
].GetCol();
6867 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6869 // If this cell is part of a multicell block, find owner for repaint
6870 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6872 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6873 bool marked
= false;
6874 for ( int j
= 0; j
< numCells
; j
++ )
6876 if ( cell
== cells
[j
] )
6884 int count
= redrawCells
.GetCount();
6885 for (int j
= 0; j
< count
; j
++)
6887 if ( cell
== redrawCells
[j
] )
6893 if (!marked
) redrawCells
.Add( cell
);
6895 continue; // don't bother drawing this cell
6898 // If this cell is empty, find cell to left that might want to overflow
6899 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6901 for ( int l
= 0; l
< cell_rows
; l
++ )
6903 // find a cell in this row to left alreay marked for repaint
6905 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6906 if ((redrawCells
[k
].GetCol() < left
) &&
6907 (redrawCells
[k
].GetRow() == row
))
6908 left
=redrawCells
[k
].GetCol();
6910 if (left
== col
) left
= 0; // oh well
6912 for (int j
= col
-1; j
>= left
; j
--)
6914 if (!m_table
->IsEmptyCell(row
+l
, j
))
6916 if (GetCellOverflow(row
+l
, j
))
6918 wxGridCellCoords
cell(row
+l
, j
);
6919 bool marked
= false;
6921 for (int k
= 0; k
< numCells
; k
++)
6923 if ( cell
== cells
[k
] )
6931 int count
= redrawCells
.GetCount();
6932 for (int k
= 0; k
< count
; k
++)
6934 if ( cell
== redrawCells
[k
] )
6940 if (!marked
) redrawCells
.Add( cell
);
6948 DrawCell( dc
, cells
[i
] );
6951 numCells
= redrawCells
.GetCount();
6953 for ( i
= numCells
- 1; i
>= 0; i
-- )
6955 DrawCell( dc
, redrawCells
[i
] );
6960 void wxGrid::DrawGridSpace( wxDC
& dc
)
6963 m_gridWin
->GetClientSize( &cw
, &ch
);
6966 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6968 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6969 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6971 if ( right
> rightCol
|| bottom
> bottomRow
)
6974 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6976 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6977 dc
.SetPen( *wxTRANSPARENT_PEN
);
6979 if ( right
> rightCol
)
6981 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6984 if ( bottom
> bottomRow
)
6986 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6992 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6994 int row
= coords
.GetRow();
6995 int col
= coords
.GetCol();
6997 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7000 // we draw the cell border ourselves
7001 #if !WXGRID_DRAW_LINES
7002 if ( m_gridLinesEnabled
)
7003 DrawCellBorder( dc
, coords
);
7006 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7008 bool isCurrent
= coords
== m_currentCellCoords
;
7010 wxRect rect
= CellToRect( row
, col
);
7012 // if the editor is shown, we should use it and not the renderer
7013 // Note: However, only if it is really _shown_, i.e. not hidden!
7014 if ( isCurrent
&& IsCellEditControlShown() )
7016 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7017 editor
->PaintBackground(rect
, attr
);
7022 // but all the rest is drawn by the cell renderer and hence may be
7024 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7025 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7032 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7034 int row
= m_currentCellCoords
.GetRow();
7035 int col
= m_currentCellCoords
.GetCol();
7037 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7040 wxRect rect
= CellToRect(row
, col
);
7042 // hmmm... what could we do here to show that the cell is disabled?
7043 // for now, I just draw a thinner border than for the other ones, but
7044 // it doesn't look really good
7046 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7050 // The center of th drawn line is where the position/width/height of
7051 // the rectangle is actually at, (on wxMSW atr least,) so we will
7052 // reduce the size of the rectangle to compensate for the thickness of
7053 // the line. If this is too strange on non wxMSW platforms then
7054 // please #ifdef this appropriately.
7055 rect
.x
+= penWidth
/2;
7056 rect
.y
+= penWidth
/2;
7057 rect
.width
-= penWidth
-1;
7058 rect
.height
-= penWidth
-1;
7061 // Now draw the rectangle
7062 // use the cellHighlightColour if the cell is inside a selection, this
7063 // will ensure the cell is always visible.
7064 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
7065 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7066 dc
.DrawRectangle(rect
);
7070 // VZ: my experiments with 3d borders...
7072 // how to properly set colours for arbitrary bg?
7073 wxCoord x1
= rect
.x
,
7075 x2
= rect
.x
+ rect
.width
-1,
7076 y2
= rect
.y
+ rect
.height
-1;
7078 dc
.SetPen(*wxWHITE_PEN
);
7079 dc
.DrawLine(x1
, y1
, x2
, y1
);
7080 dc
.DrawLine(x1
, y1
, x1
, y2
);
7082 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7083 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7085 dc
.SetPen(*wxBLACK_PEN
);
7086 dc
.DrawLine(x1
, y2
, x2
, y2
);
7087 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7092 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7094 int row
= coords
.GetRow();
7095 int col
= coords
.GetCol();
7096 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7099 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7101 wxRect rect
= CellToRect( row
, col
);
7103 // right hand border
7105 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7106 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7110 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7111 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7114 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7116 // This if block was previously in wxGrid::OnPaint but that doesn't
7117 // seem to get called under wxGTK - MB
7119 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7120 m_numRows
&& m_numCols
)
7122 m_currentCellCoords
.Set(0, 0);
7125 if ( IsCellEditControlShown() )
7127 // don't show highlight when the edit control is shown
7131 // if the active cell was repainted, repaint its highlight too because it
7132 // might have been damaged by the grid lines
7133 size_t count
= cells
.GetCount();
7134 for ( size_t n
= 0; n
< count
; n
++ )
7136 if ( cells
[n
] == m_currentCellCoords
)
7138 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7139 DrawCellHighlight(dc
, attr
);
7147 // TODO: remove this ???
7148 // This is used to redraw all grid lines e.g. when the grid line colour
7151 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7153 #if !WXGRID_DRAW_LINES
7157 if ( !m_gridLinesEnabled
||
7159 !m_numCols
) return;
7161 int top
, bottom
, left
, right
;
7163 #if 0 //#ifndef __WXGTK__
7167 m_gridWin
->GetClientSize(&cw
, &ch
);
7169 // virtual coords of visible area
7171 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7172 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7177 reg
.GetBox(x
, y
, w
, h
);
7178 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7179 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7183 m_gridWin
->GetClientSize(&cw
, &ch
);
7184 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7185 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7188 // avoid drawing grid lines past the last row and col
7190 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7191 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7193 // no gridlines inside multicells, clip them out
7194 int leftCol
= internalXToCol(left
);
7195 int topRow
= internalYToRow(top
);
7196 int rightCol
= internalXToCol(right
);
7197 int bottomRow
= internalYToRow(bottom
);
7200 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7201 wxRegion
clippedcells(0, 0, cw
, ch
);
7203 int i
, j
, cell_rows
, cell_cols
;
7206 for (j
=topRow
; j
<bottomRow
; j
++)
7208 for (i
=leftCol
; i
<rightCol
; i
++)
7210 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7211 if ((cell_rows
> 1) || (cell_cols
> 1))
7213 rect
= CellToRect(j
,i
);
7214 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7215 clippedcells
.Subtract(rect
);
7217 else if ((cell_rows
< 0) || (cell_cols
< 0))
7219 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7220 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7221 clippedcells
.Subtract(rect
);
7226 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7228 int i
, j
, cell_rows
, cell_cols
;
7231 for (j
=topRow
; j
<bottomRow
; j
++)
7233 for (i
=leftCol
; i
<rightCol
; i
++)
7235 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7236 if ((cell_rows
> 1) || (cell_cols
> 1))
7238 rect
= CellToRect(j
,i
);
7239 clippedcells
.Subtract(rect
);
7241 else if ((cell_rows
< 0) || (cell_cols
< 0))
7243 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7244 clippedcells
.Subtract(rect
);
7249 dc
.SetClippingRegion( clippedcells
);
7251 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7253 // horizontal grid lines
7255 // already declared above - int i;
7256 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7258 int bot
= GetRowBottom(i
) - 1;
7267 dc
.DrawLine( left
, bot
, right
, bot
);
7272 // vertical grid lines
7274 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7276 int colRight
= GetColRight(i
) - 1;
7277 if ( colRight
> right
)
7282 if ( colRight
>= left
)
7284 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7287 dc
.DestroyClippingRegion();
7291 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7293 if ( !m_numRows
) return;
7296 size_t numLabels
= rows
.GetCount();
7298 for ( i
= 0; i
< numLabels
; i
++ )
7300 DrawRowLabel( dc
, rows
[i
] );
7305 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7307 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7313 rect
.SetY( GetRowTop(row
) + 1 );
7314 rect
.SetWidth( m_rowLabelWidth
- 2 );
7315 rect
.SetHeight( GetRowHeight(row
) - 2 );
7317 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7319 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7321 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7323 int rowTop
= GetRowTop(row
),
7324 rowBottom
= GetRowBottom(row
) - 1;
7326 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7327 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7328 m_rowLabelWidth
-1, rowBottom
);
7330 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7332 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7334 dc
.SetPen( *wxWHITE_PEN
);
7335 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7336 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7338 dc
.SetBackgroundMode( wxTRANSPARENT
);
7339 dc
.SetTextForeground( GetLabelTextColour() );
7340 dc
.SetFont( GetLabelFont() );
7343 GetRowLabelAlignment( &hAlign
, &vAlign
);
7346 rect
.SetY( GetRowTop(row
) + 2 );
7347 rect
.SetWidth( m_rowLabelWidth
- 4 );
7348 rect
.SetHeight( GetRowHeight(row
) - 4 );
7349 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7353 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7355 if ( !m_numCols
) return;
7358 size_t numLabels
= cols
.GetCount();
7360 for ( i
= 0; i
< numLabels
; i
++ )
7362 DrawColLabel( dc
, cols
[i
] );
7367 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7369 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7372 int colLeft
= GetColLeft(col
);
7376 rect
.SetX( colLeft
+ 1 );
7378 rect
.SetWidth( GetColWidth(col
) - 2 );
7379 rect
.SetHeight( m_colLabelHeight
- 2 );
7381 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7383 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7385 int colRight
= GetColRight(col
) - 1;
7387 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7388 dc
.DrawLine( colRight
, 0,
7389 colRight
, m_colLabelHeight
-1 );
7391 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7393 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7394 colRight
+1, m_colLabelHeight
-1 );
7396 dc
.SetPen( *wxWHITE_PEN
);
7397 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7398 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7400 dc
.SetBackgroundMode( wxTRANSPARENT
);
7401 dc
.SetTextForeground( GetLabelTextColour() );
7402 dc
.SetFont( GetLabelFont() );
7404 int hAlign
, vAlign
, orient
;
7405 GetColLabelAlignment( &hAlign
, &vAlign
);
7406 orient
= GetColLabelTextOrientation();
7408 rect
.SetX( colLeft
+ 2 );
7410 rect
.SetWidth( GetColWidth(col
) - 4 );
7411 rect
.SetHeight( m_colLabelHeight
- 4 );
7412 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7415 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7416 const wxString
& value
,
7420 int textOrientation
)
7422 wxArrayString lines
;
7424 StringToLines( value
, lines
);
7427 //Forward to new API.
7428 DrawTextRectangle( dc
,
7437 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7438 const wxArrayString
& lines
,
7442 int textOrientation
)
7444 long textWidth
, textHeight
;
7445 long lineWidth
, lineHeight
;
7448 dc
.SetClippingRegion( rect
);
7450 nLines
= lines
.GetCount();
7454 float x
= 0.0, y
= 0.0;
7456 if( textOrientation
== wxHORIZONTAL
)
7457 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7459 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7463 case wxALIGN_BOTTOM
:
7464 if( textOrientation
== wxHORIZONTAL
)
7465 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7467 x
= rect
.x
+ rect
.width
- textWidth
;
7470 case wxALIGN_CENTRE
:
7471 if( textOrientation
== wxHORIZONTAL
)
7472 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7474 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7479 if( textOrientation
== wxHORIZONTAL
)
7486 // Align each line of a multi-line label
7487 for( l
= 0; l
< nLines
; l
++ )
7489 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7491 switch( horizAlign
)
7494 if( textOrientation
== wxHORIZONTAL
)
7495 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7497 y
= rect
.y
+ lineWidth
+ 1;
7500 case wxALIGN_CENTRE
:
7501 if( textOrientation
== wxHORIZONTAL
)
7502 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7504 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7509 if( textOrientation
== wxHORIZONTAL
)
7512 y
= rect
.y
+ rect
.height
- 1;
7516 if( textOrientation
== wxHORIZONTAL
)
7518 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7523 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7528 dc
.DestroyClippingRegion();
7532 // Split multi line text up into an array of strings. Any existing
7533 // contents of the string array are preserved.
7535 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7539 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7540 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7542 while ( startPos
< (int)tVal
.Length() )
7544 pos
= tVal
.Mid(startPos
).Find( eol
);
7549 else if ( pos
== 0 )
7551 lines
.Add( wxEmptyString
);
7555 lines
.Add( value
.Mid(startPos
, pos
) );
7559 if ( startPos
< (int)value
.Length() )
7561 lines
.Add( value
.Mid( startPos
) );
7566 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7567 const wxArrayString
& lines
,
7568 long *width
, long *height
)
7575 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7577 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7578 w
= wxMax( w
, lineW
);
7587 // ------ Batch processing.
7589 void wxGrid::EndBatch()
7591 if ( m_batchCount
> 0 )
7594 if ( !m_batchCount
)
7597 m_rowLabelWin
->Refresh();
7598 m_colLabelWin
->Refresh();
7599 m_cornerLabelWin
->Refresh();
7600 m_gridWin
->Refresh();
7605 // Use this, rather than wxWindow::Refresh(), to force an immediate
7606 // repainting of the grid. Has no effect if you are already inside a
7607 // BeginBatch / EndBatch block.
7609 void wxGrid::ForceRefresh()
7615 bool wxGrid::Enable(bool enable
)
7617 if ( !wxScrolledWindow::Enable(enable
) )
7620 // redraw in the new state
7621 m_gridWin
->Refresh();
7627 // ------ Edit control functions
7631 void wxGrid::EnableEditing( bool edit
)
7633 // TODO: improve this ?
7635 if ( edit
!= m_editable
)
7637 if(!edit
) EnableCellEditControl(edit
);
7643 void wxGrid::EnableCellEditControl( bool enable
)
7648 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7649 SetCurrentCell( 0, 0 );
7651 if ( enable
!= m_cellEditCtrlEnabled
)
7655 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7658 // this should be checked by the caller!
7659 wxASSERT_MSG( CanEnableCellControl(),
7660 _T("can't enable editing for this cell!") );
7662 // do it before ShowCellEditControl()
7663 m_cellEditCtrlEnabled
= enable
;
7665 ShowCellEditControl();
7669 //FIXME:add veto support
7670 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7672 HideCellEditControl();
7673 SaveEditControlValue();
7675 // do it after HideCellEditControl()
7676 m_cellEditCtrlEnabled
= enable
;
7681 bool wxGrid::IsCurrentCellReadOnly() const
7684 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7685 bool readonly
= attr
->IsReadOnly();
7691 bool wxGrid::CanEnableCellControl() const
7693 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7694 !IsCurrentCellReadOnly();
7698 bool wxGrid::IsCellEditControlEnabled() const
7700 // the cell edit control might be disable for all cells or just for the
7701 // current one if it's read only
7702 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7705 bool wxGrid::IsCellEditControlShown() const
7707 bool isShown
= false;
7709 if ( m_cellEditCtrlEnabled
)
7711 int row
= m_currentCellCoords
.GetRow();
7712 int col
= m_currentCellCoords
.GetCol();
7713 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7714 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7719 if ( editor
->IsCreated() )
7721 isShown
= editor
->GetControl()->IsShown();
7731 void wxGrid::ShowCellEditControl()
7733 if ( IsCellEditControlEnabled() )
7735 if ( !IsVisible( m_currentCellCoords
) )
7737 m_cellEditCtrlEnabled
= false;
7742 wxRect rect
= CellToRect( m_currentCellCoords
);
7743 int row
= m_currentCellCoords
.GetRow();
7744 int col
= m_currentCellCoords
.GetCol();
7746 // if this is part of a multicell, find owner (topleft)
7747 int cell_rows
, cell_cols
;
7748 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7749 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7753 m_currentCellCoords
.SetRow( row
);
7754 m_currentCellCoords
.SetCol( col
);
7757 // convert to scrolled coords
7759 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7761 // done in PaintBackground()
7763 // erase the highlight and the cell contents because the editor
7764 // might not cover the entire cell
7765 wxClientDC
dc( m_gridWin
);
7767 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7768 dc
.SetPen(*wxTRANSPARENT_PEN
);
7769 dc
.DrawRectangle(rect
);
7772 // cell is shifted by one pixel
7773 // However, don't allow x or y to become negative
7774 // since the SetSize() method interprets that as
7781 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7782 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7783 if ( !editor
->IsCreated() )
7785 editor
->Create(m_gridWin
, wxID_ANY
,
7786 new wxGridCellEditorEvtHandler(this, editor
));
7788 wxGridEditorCreatedEvent
evt(GetId(),
7789 wxEVT_GRID_EDITOR_CREATED
,
7793 editor
->GetControl());
7794 GetEventHandler()->ProcessEvent(evt
);
7798 // resize editor to overflow into righthand cells if allowed
7799 int maxWidth
= rect
.width
;
7800 wxString value
= GetCellValue(row
, col
);
7801 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7804 GetTextExtent(value
, &maxWidth
, &y
,
7805 NULL
, NULL
, &attr
->GetFont());
7806 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7808 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7809 if (rect
.x
+maxWidth
> client_right
)
7810 maxWidth
= client_right
- rect
.x
;
7812 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7814 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7815 // may have changed earlier
7816 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7819 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7820 // looks weird going over a multicell
7821 if (m_table
->IsEmptyCell(row
,i
) &&
7822 (rect
.width
< maxWidth
) && (c_rows
== 1))
7823 rect
.width
+= GetColWidth(i
);
7827 if (rect
.GetRight() > client_right
)
7828 rect
.SetRight(client_right
-1);
7831 editor
->SetCellAttr(attr
);
7832 editor
->SetSize( rect
);
7833 editor
->Show( true, attr
);
7835 // recalc dimensions in case we need to
7836 // expand the scrolled window to account for editor
7839 editor
->BeginEdit(row
, col
, this);
7840 editor
->SetCellAttr(NULL
);
7849 void wxGrid::HideCellEditControl()
7851 if ( IsCellEditControlEnabled() )
7853 int row
= m_currentCellCoords
.GetRow();
7854 int col
= m_currentCellCoords
.GetCol();
7856 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7857 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7858 editor
->Show( false );
7862 m_gridWin
->SetFocus();
7864 // refresh whole row to the right
7865 wxRect
rect( CellToRect(row
, col
) );
7866 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7867 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7869 // ensure that the pixels under the focus ring get refreshed as well
7870 rect
.Inflate(10,10);
7872 m_gridWin
->Refresh( false, &rect
);
7877 void wxGrid::SaveEditControlValue()
7879 if ( IsCellEditControlEnabled() )
7881 int row
= m_currentCellCoords
.GetRow();
7882 int col
= m_currentCellCoords
.GetCol();
7884 wxString oldval
= GetCellValue(row
,col
);
7886 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7887 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7888 bool changed
= editor
->EndEdit(row
, col
, this);
7895 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7896 m_currentCellCoords
.GetRow(),
7897 m_currentCellCoords
.GetCol() ) < 0 ) {
7899 // Event has been vetoed, set the data back.
7900 SetCellValue(row
,col
,oldval
);
7908 // ------ Grid location functions
7909 // Note that all of these functions work with the logical coordinates of
7910 // grid cells and labels so you will need to convert from device
7911 // coordinates for mouse events etc.
7914 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7916 int row
= YToRow(y
);
7917 int col
= XToCol(x
);
7919 if ( row
== -1 || col
== -1 )
7921 coords
= wxGridNoCellCoords
;
7925 coords
.Set( row
, col
);
7930 // Internal Helper function for computing row or column from some
7931 // (unscrolled) coordinate value, using either
7932 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7933 // of m_rowBottoms/m_ColRights to speed up the search!
7935 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7936 const wxArrayInt
& BorderArray
, int nMax
,
7941 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7947 size_t i_max
= coord
/ defaultDist
,
7950 if (BorderArray
.IsEmpty())
7952 if((int) i_max
< nMax
)
7954 return clipToMinMax
? nMax
- 1 : -1;
7957 if ( i_max
>= BorderArray
.GetCount())
7958 i_max
= BorderArray
.GetCount() - 1;
7961 if ( coord
>= BorderArray
[i_max
])
7965 i_max
= coord
/ minDist
;
7967 i_max
= BorderArray
.GetCount() - 1;
7969 if ( i_max
>= BorderArray
.GetCount())
7970 i_max
= BorderArray
.GetCount() - 1;
7972 if ( coord
>= BorderArray
[i_max
])
7973 return clipToMinMax
? (int)i_max
: -1;
7974 if ( coord
< BorderArray
[0] )
7977 while ( i_max
- i_min
> 0 )
7979 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7980 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7981 if (coord
>= BorderArray
[ i_max
- 1])
7985 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7986 if (coord
< BorderArray
[median
])
7994 int wxGrid::YToRow( int y
)
7996 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7997 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8001 int wxGrid::XToCol( int x
)
8003 return CoordToRowOrCol(x
, m_defaultColWidth
,
8004 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8008 // return the row number that that the y coord is near the edge of, or
8009 // -1 if not near an edge
8011 int wxGrid::YToEdgeOfRow( int y
)
8014 i
= internalYToRow(y
);
8016 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8018 // We know that we are in row i, test whether we are
8019 // close enough to lower or upper border, respectively.
8020 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8022 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8030 // return the col number that that the x coord is near the edge of, or
8031 // -1 if not near an edge
8033 int wxGrid::XToEdgeOfCol( int x
)
8036 i
= internalXToCol(x
);
8038 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8040 // We know that we are in column i, test whether we are
8041 // close enough to right or left border, respectively.
8042 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8044 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8052 wxRect
wxGrid::CellToRect( int row
, int col
)
8054 wxRect
rect( -1, -1, -1, -1 );
8056 if ( row
>= 0 && row
< m_numRows
&&
8057 col
>= 0 && col
< m_numCols
)
8059 int i
, cell_rows
, cell_cols
;
8060 rect
.width
= rect
.height
= 0;
8061 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8062 // if negative then find multicell owner
8063 if (cell_rows
< 0) row
+= cell_rows
;
8064 if (cell_cols
< 0) col
+= cell_cols
;
8065 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8067 rect
.x
= GetColLeft(col
);
8068 rect
.y
= GetRowTop(row
);
8069 for (i
=col
; i
<col
+cell_cols
; i
++)
8070 rect
.width
+= GetColWidth(i
);
8071 for (i
=row
; i
<row
+cell_rows
; i
++)
8072 rect
.height
+= GetRowHeight(i
);
8075 // if grid lines are enabled, then the area of the cell is a bit smaller
8076 if (m_gridLinesEnabled
) {
8084 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8086 // get the cell rectangle in logical coords
8088 wxRect
r( CellToRect( row
, col
) );
8090 // convert to device coords
8092 int left
, top
, right
, bottom
;
8093 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8094 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8096 // check against the client area of the grid window
8099 m_gridWin
->GetClientSize( &cw
, &ch
);
8101 if ( wholeCellVisible
)
8103 // is the cell wholly visible ?
8105 return ( left
>= 0 && right
<= cw
&&
8106 top
>= 0 && bottom
<= ch
);
8110 // is the cell partly visible ?
8112 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8113 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8118 // make the specified cell location visible by doing a minimal amount
8121 void wxGrid::MakeCellVisible( int row
, int col
)
8125 int xpos
= -1, ypos
= -1;
8127 if ( row
>= 0 && row
< m_numRows
&&
8128 col
>= 0 && col
< m_numCols
)
8130 // get the cell rectangle in logical coords
8132 wxRect
r( CellToRect( row
, col
) );
8134 // convert to device coords
8136 int left
, top
, right
, bottom
;
8137 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8138 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8141 m_gridWin
->GetClientSize( &cw
, &ch
);
8147 else if ( bottom
> ch
)
8149 int h
= r
.GetHeight();
8151 for ( i
= row
-1; i
>= 0; i
-- )
8153 int rowHeight
= GetRowHeight(i
);
8154 if ( h
+ rowHeight
> ch
)
8161 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8162 // have rounding errors (this is important, because if we do, we
8163 // might not scroll at all and some cells won't be redrawn)
8165 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8167 ypos
+= m_scrollLineY
;
8174 else if ( right
> cw
)
8176 // position the view so that the cell is on the right
8178 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8179 xpos
= x0
+ (right
- cw
);
8181 // see comment for ypos above
8182 xpos
+= m_scrollLineX
;
8185 if ( xpos
!= -1 || ypos
!= -1 )
8188 xpos
/= m_scrollLineX
;
8190 ypos
/= m_scrollLineY
;
8191 Scroll( xpos
, ypos
);
8199 // ------ Grid cursor movement functions
8202 bool wxGrid::MoveCursorUp( bool expandSelection
)
8204 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8205 m_currentCellCoords
.GetRow() >= 0 )
8207 if ( expandSelection
)
8209 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8210 m_selectingKeyboard
= m_currentCellCoords
;
8211 if ( m_selectingKeyboard
.GetRow() > 0 )
8213 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8214 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8215 m_selectingKeyboard
.GetCol() );
8216 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8219 else if ( m_currentCellCoords
.GetRow() > 0 )
8222 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8223 m_currentCellCoords
.GetCol() );
8224 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8225 m_currentCellCoords
.GetCol() );
8236 bool wxGrid::MoveCursorDown( bool expandSelection
)
8238 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8239 m_currentCellCoords
.GetRow() < m_numRows
)
8241 if ( expandSelection
)
8243 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8244 m_selectingKeyboard
= m_currentCellCoords
;
8245 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8247 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8248 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8249 m_selectingKeyboard
.GetCol() );
8250 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8253 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8256 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8257 m_currentCellCoords
.GetCol() );
8258 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8259 m_currentCellCoords
.GetCol() );
8270 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8272 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8273 m_currentCellCoords
.GetCol() >= 0 )
8275 if ( expandSelection
)
8277 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8278 m_selectingKeyboard
= m_currentCellCoords
;
8279 if ( m_selectingKeyboard
.GetCol() > 0 )
8281 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8282 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8283 m_selectingKeyboard
.GetCol() );
8284 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8287 else if ( m_currentCellCoords
.GetCol() > 0 )
8290 MakeCellVisible( m_currentCellCoords
.GetRow(),
8291 m_currentCellCoords
.GetCol() - 1 );
8292 SetCurrentCell( m_currentCellCoords
.GetRow(),
8293 m_currentCellCoords
.GetCol() - 1 );
8304 bool wxGrid::MoveCursorRight( bool expandSelection
)
8306 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8307 m_currentCellCoords
.GetCol() < m_numCols
)
8309 if ( expandSelection
)
8311 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8312 m_selectingKeyboard
= m_currentCellCoords
;
8313 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8315 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8316 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8317 m_selectingKeyboard
.GetCol() );
8318 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8321 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8324 MakeCellVisible( m_currentCellCoords
.GetRow(),
8325 m_currentCellCoords
.GetCol() + 1 );
8326 SetCurrentCell( m_currentCellCoords
.GetRow(),
8327 m_currentCellCoords
.GetCol() + 1 );
8338 bool wxGrid::MovePageUp()
8340 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8342 int row
= m_currentCellCoords
.GetRow();
8346 m_gridWin
->GetClientSize( &cw
, &ch
);
8348 int y
= GetRowTop(row
);
8349 int newRow
= internalYToRow( y
- ch
+ 1 );
8351 if ( newRow
== row
)
8353 //row > 0 , so newrow can never be less than 0 here.
8357 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8358 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8366 bool wxGrid::MovePageDown()
8368 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8370 int row
= m_currentCellCoords
.GetRow();
8371 if ( (row
+1) < m_numRows
)
8374 m_gridWin
->GetClientSize( &cw
, &ch
);
8376 int y
= GetRowTop(row
);
8377 int newRow
= internalYToRow( y
+ ch
);
8378 if ( newRow
== row
)
8380 // row < m_numRows , so newrow can't overflow here.
8384 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8385 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8393 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8396 m_currentCellCoords
!= wxGridNoCellCoords
&&
8397 m_currentCellCoords
.GetRow() > 0 )
8399 int row
= m_currentCellCoords
.GetRow();
8400 int col
= m_currentCellCoords
.GetCol();
8402 if ( m_table
->IsEmptyCell(row
, col
) )
8404 // starting in an empty cell: find the next block of
8410 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8413 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8415 // starting at the top of a block: find the next block
8421 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8426 // starting within a block: find the top of the block
8431 if ( m_table
->IsEmptyCell(row
, col
) )
8439 MakeCellVisible( row
, col
);
8440 if ( expandSelection
)
8442 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8443 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8448 SetCurrentCell( row
, col
);
8456 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8459 m_currentCellCoords
!= wxGridNoCellCoords
&&
8460 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8462 int row
= m_currentCellCoords
.GetRow();
8463 int col
= m_currentCellCoords
.GetCol();
8465 if ( m_table
->IsEmptyCell(row
, col
) )
8467 // starting in an empty cell: find the next block of
8470 while ( row
< m_numRows
-1 )
8473 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8476 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8478 // starting at the bottom of a block: find the next block
8481 while ( row
< m_numRows
-1 )
8484 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8489 // starting within a block: find the bottom of the block
8491 while ( row
< m_numRows
-1 )
8494 if ( m_table
->IsEmptyCell(row
, col
) )
8502 MakeCellVisible( row
, col
);
8503 if ( expandSelection
)
8505 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8506 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8511 SetCurrentCell( row
, col
);
8520 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8523 m_currentCellCoords
!= wxGridNoCellCoords
&&
8524 m_currentCellCoords
.GetCol() > 0 )
8526 int row
= m_currentCellCoords
.GetRow();
8527 int col
= m_currentCellCoords
.GetCol();
8529 if ( m_table
->IsEmptyCell(row
, col
) )
8531 // starting in an empty cell: find the next block of
8537 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8540 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8542 // starting at the left of a block: find the next block
8548 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8553 // starting within a block: find the left of the block
8558 if ( m_table
->IsEmptyCell(row
, col
) )
8566 MakeCellVisible( row
, col
);
8567 if ( expandSelection
)
8569 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8570 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8575 SetCurrentCell( row
, col
);
8584 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8587 m_currentCellCoords
!= wxGridNoCellCoords
&&
8588 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8590 int row
= m_currentCellCoords
.GetRow();
8591 int col
= m_currentCellCoords
.GetCol();
8593 if ( m_table
->IsEmptyCell(row
, col
) )
8595 // starting in an empty cell: find the next block of
8598 while ( col
< m_numCols
-1 )
8601 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8604 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8606 // starting at the right of a block: find the next block
8609 while ( col
< m_numCols
-1 )
8612 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8617 // starting within a block: find the right of the block
8619 while ( col
< m_numCols
-1 )
8622 if ( m_table
->IsEmptyCell(row
, col
) )
8630 MakeCellVisible( row
, col
);
8631 if ( expandSelection
)
8633 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8634 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8639 SetCurrentCell( row
, col
);
8651 // ------ Label values and formatting
8654 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8656 *horiz
= m_rowLabelHorizAlign
;
8657 *vert
= m_rowLabelVertAlign
;
8660 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8662 *horiz
= m_colLabelHorizAlign
;
8663 *vert
= m_colLabelVertAlign
;
8666 int wxGrid::GetColLabelTextOrientation()
8668 return m_colLabelTextOrientation
;
8671 wxString
wxGrid::GetRowLabelValue( int row
)
8675 return m_table
->GetRowLabelValue( row
);
8685 wxString
wxGrid::GetColLabelValue( int col
)
8689 return m_table
->GetColLabelValue( col
);
8700 void wxGrid::SetRowLabelSize( int width
)
8702 width
= wxMax( width
, 0 );
8703 if ( width
!= m_rowLabelWidth
)
8707 m_rowLabelWin
->Show( false );
8708 m_cornerLabelWin
->Show( false );
8710 else if ( m_rowLabelWidth
== 0 )
8712 m_rowLabelWin
->Show( true );
8713 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8716 m_rowLabelWidth
= width
;
8718 wxScrolledWindow::Refresh( true );
8723 void wxGrid::SetColLabelSize( int height
)
8725 height
= wxMax( height
, 0 );
8726 if ( height
!= m_colLabelHeight
)
8730 m_colLabelWin
->Show( false );
8731 m_cornerLabelWin
->Show( false );
8733 else if ( m_colLabelHeight
== 0 )
8735 m_colLabelWin
->Show( true );
8736 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8739 m_colLabelHeight
= height
;
8741 wxScrolledWindow::Refresh( true );
8746 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8748 if ( m_labelBackgroundColour
!= colour
)
8750 m_labelBackgroundColour
= colour
;
8751 m_rowLabelWin
->SetBackgroundColour( colour
);
8752 m_colLabelWin
->SetBackgroundColour( colour
);
8753 m_cornerLabelWin
->SetBackgroundColour( colour
);
8755 if ( !GetBatchCount() )
8757 m_rowLabelWin
->Refresh();
8758 m_colLabelWin
->Refresh();
8759 m_cornerLabelWin
->Refresh();
8764 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8766 if ( m_labelTextColour
!= colour
)
8768 m_labelTextColour
= colour
;
8769 if ( !GetBatchCount() )
8771 m_rowLabelWin
->Refresh();
8772 m_colLabelWin
->Refresh();
8777 void wxGrid::SetLabelFont( const wxFont
& font
)
8780 if ( !GetBatchCount() )
8782 m_rowLabelWin
->Refresh();
8783 m_colLabelWin
->Refresh();
8787 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8789 // allow old (incorrect) defs to be used
8792 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8793 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8794 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8799 case wxTOP
: vert
= wxALIGN_TOP
; break;
8800 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8801 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8804 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8806 m_rowLabelHorizAlign
= horiz
;
8809 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8811 m_rowLabelVertAlign
= vert
;
8814 if ( !GetBatchCount() )
8816 m_rowLabelWin
->Refresh();
8820 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8822 // allow old (incorrect) defs to be used
8825 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8826 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8827 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8832 case wxTOP
: vert
= wxALIGN_TOP
; break;
8833 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8834 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8837 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8839 m_colLabelHorizAlign
= horiz
;
8842 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8844 m_colLabelVertAlign
= vert
;
8847 if ( !GetBatchCount() )
8849 m_colLabelWin
->Refresh();
8853 // Note: under MSW, the default column label font must be changed because it
8854 // does not support vertical printing
8856 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8857 // pGrid->SetLabelFont(font);
8858 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8860 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8862 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8864 m_colLabelTextOrientation
= textOrientation
;
8867 if ( !GetBatchCount() )
8869 m_colLabelWin
->Refresh();
8873 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8877 m_table
->SetRowLabelValue( row
, s
);
8878 if ( !GetBatchCount() )
8880 wxRect rect
= CellToRect( row
, 0);
8881 if ( rect
.height
> 0 )
8883 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8885 rect
.width
= m_rowLabelWidth
;
8886 m_rowLabelWin
->Refresh( true, &rect
);
8892 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8896 m_table
->SetColLabelValue( col
, s
);
8897 if ( !GetBatchCount() )
8899 wxRect rect
= CellToRect( 0, col
);
8900 if ( rect
.width
> 0 )
8902 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8904 rect
.height
= m_colLabelHeight
;
8905 m_colLabelWin
->Refresh( true, &rect
);
8911 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8913 if ( m_gridLineColour
!= colour
)
8915 m_gridLineColour
= colour
;
8917 wxClientDC
dc( m_gridWin
);
8919 DrawAllGridLines( dc
, wxRegion() );
8924 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8926 if ( m_cellHighlightColour
!= colour
)
8928 m_cellHighlightColour
= colour
;
8930 wxClientDC
dc( m_gridWin
);
8932 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8933 DrawCellHighlight(dc
, attr
);
8938 void wxGrid::SetCellHighlightPenWidth(int width
)
8940 if (m_cellHighlightPenWidth
!= width
) {
8941 m_cellHighlightPenWidth
= width
;
8943 // Just redrawing the cell highlight is not enough since that won't
8944 // make any visible change if the the thickness is getting smaller.
8945 int row
= m_currentCellCoords
.GetRow();
8946 int col
= m_currentCellCoords
.GetCol();
8947 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8949 wxRect rect
= CellToRect(row
, col
);
8950 m_gridWin
->Refresh(true, &rect
);
8954 void wxGrid::SetCellHighlightROPenWidth(int width
)
8956 if (m_cellHighlightROPenWidth
!= width
) {
8957 m_cellHighlightROPenWidth
= width
;
8959 // Just redrawing the cell highlight is not enough since that won't
8960 // make any visible change if the the thickness is getting smaller.
8961 int row
= m_currentCellCoords
.GetRow();
8962 int col
= m_currentCellCoords
.GetCol();
8963 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8965 wxRect rect
= CellToRect(row
, col
);
8966 m_gridWin
->Refresh(true, &rect
);
8970 void wxGrid::EnableGridLines( bool enable
)
8972 if ( enable
!= m_gridLinesEnabled
)
8974 m_gridLinesEnabled
= enable
;
8976 if ( !GetBatchCount() )
8980 wxClientDC
dc( m_gridWin
);
8982 DrawAllGridLines( dc
, wxRegion() );
8986 m_gridWin
->Refresh();
8993 int wxGrid::GetDefaultRowSize()
8995 return m_defaultRowHeight
;
8998 int wxGrid::GetRowSize( int row
)
9000 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9002 return GetRowHeight(row
);
9005 int wxGrid::GetDefaultColSize()
9007 return m_defaultColWidth
;
9010 int wxGrid::GetColSize( int col
)
9012 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9014 return GetColWidth(col
);
9017 // ============================================================================
9018 // access to the grid attributes: each of them has a default value in the grid
9019 // itself and may be overidden on a per-cell basis
9020 // ============================================================================
9022 // ----------------------------------------------------------------------------
9023 // setting default attributes
9024 // ----------------------------------------------------------------------------
9026 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9028 m_defaultCellAttr
->SetBackgroundColour(col
);
9030 m_gridWin
->SetBackgroundColour(col
);
9034 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9036 m_defaultCellAttr
->SetTextColour(col
);
9039 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9041 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9044 void wxGrid::SetDefaultCellOverflow( bool allow
)
9046 m_defaultCellAttr
->SetOverflow(allow
);
9049 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9051 m_defaultCellAttr
->SetFont(font
);
9055 // For editors and renderers the type registry takes precedence over the
9056 // default attr, so we need to register the new editor/renderer for the string
9057 // data type in order to make setting a default editor/renderer appear to
9060 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9062 RegisterDataType(wxGRID_VALUE_STRING
,
9064 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9067 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9069 RegisterDataType(wxGRID_VALUE_STRING
,
9070 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9074 // ----------------------------------------------------------------------------
9075 // access to the default attrbiutes
9076 // ----------------------------------------------------------------------------
9078 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9080 return m_defaultCellAttr
->GetBackgroundColour();
9083 wxColour
wxGrid::GetDefaultCellTextColour()
9085 return m_defaultCellAttr
->GetTextColour();
9088 wxFont
wxGrid::GetDefaultCellFont()
9090 return m_defaultCellAttr
->GetFont();
9093 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9095 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9098 bool wxGrid::GetDefaultCellOverflow()
9100 return m_defaultCellAttr
->GetOverflow();
9103 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9105 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9108 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9110 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
9113 // ----------------------------------------------------------------------------
9114 // access to cell attributes
9115 // ----------------------------------------------------------------------------
9117 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9119 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9120 wxColour colour
= attr
->GetBackgroundColour();
9125 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9127 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9128 wxColour colour
= attr
->GetTextColour();
9133 wxFont
wxGrid::GetCellFont( int row
, int col
)
9135 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9136 wxFont font
= attr
->GetFont();
9141 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9143 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9144 attr
->GetAlignment(horiz
, vert
);
9148 bool wxGrid::GetCellOverflow( int row
, int col
)
9150 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9151 bool allow
= attr
->GetOverflow();
9156 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9158 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9159 attr
->GetSize( num_rows
, num_cols
);
9163 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9165 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9166 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9172 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9174 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9175 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9181 bool wxGrid::IsReadOnly(int row
, int col
) const
9183 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9184 bool isReadOnly
= attr
->IsReadOnly();
9189 // ----------------------------------------------------------------------------
9190 // attribute support: cache, automatic provider creation, ...
9191 // ----------------------------------------------------------------------------
9193 bool wxGrid::CanHaveAttributes()
9200 return m_table
->CanHaveAttributes();
9203 void wxGrid::ClearAttrCache()
9205 if ( m_attrCache
.row
!= -1 )
9207 wxSafeDecRef(m_attrCache
.attr
);
9208 m_attrCache
.attr
= NULL
;
9209 m_attrCache
.row
= -1;
9213 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9217 wxGrid
*self
= (wxGrid
*)this; // const_cast
9219 self
->ClearAttrCache();
9220 self
->m_attrCache
.row
= row
;
9221 self
->m_attrCache
.col
= col
;
9222 self
->m_attrCache
.attr
= attr
;
9227 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9229 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9231 *attr
= m_attrCache
.attr
;
9232 wxSafeIncRef(m_attrCache
.attr
);
9234 #ifdef DEBUG_ATTR_CACHE
9235 gs_nAttrCacheHits
++;
9242 #ifdef DEBUG_ATTR_CACHE
9243 gs_nAttrCacheMisses
++;
9249 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9251 wxGridCellAttr
*attr
= NULL
;
9252 // Additional test to avoid looking at the cache e.g. for
9253 // wxNoCellCoords, as this will confuse memory management.
9256 if ( !LookupAttr(row
, col
, &attr
) )
9258 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9259 : (wxGridCellAttr
*)NULL
;
9260 CacheAttr(row
, col
, attr
);
9265 attr
->SetDefAttr(m_defaultCellAttr
);
9269 attr
= m_defaultCellAttr
;
9276 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9278 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9279 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9281 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9282 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9284 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9287 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9289 // artificially inc the ref count to match DecRef() in caller
9291 m_table
->SetAttr(attr
, row
, col
);
9297 // ----------------------------------------------------------------------------
9298 // setting column attributes (wrappers around SetColAttr)
9299 // ----------------------------------------------------------------------------
9301 void wxGrid::SetColFormatBool(int col
)
9303 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9306 void wxGrid::SetColFormatNumber(int col
)
9308 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9311 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9313 wxString typeName
= wxGRID_VALUE_FLOAT
;
9314 if ( (width
!= -1) || (precision
!= -1) )
9316 typeName
<< _T(':') << width
<< _T(',') << precision
;
9319 SetColFormatCustom(col
, typeName
);
9322 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9324 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9326 attr
= new wxGridCellAttr
;
9327 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9328 attr
->SetRenderer(renderer
);
9330 SetColAttr(col
, attr
);
9334 // ----------------------------------------------------------------------------
9335 // setting cell attributes: this is forwarded to the table
9336 // ----------------------------------------------------------------------------
9338 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9340 if ( CanHaveAttributes() )
9342 m_table
->SetAttr(attr
, row
, col
);
9351 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9353 if ( CanHaveAttributes() )
9355 m_table
->SetRowAttr(attr
, row
);
9364 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9366 if ( CanHaveAttributes() )
9368 m_table
->SetColAttr(attr
, col
);
9377 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9379 if ( CanHaveAttributes() )
9381 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9382 attr
->SetBackgroundColour(colour
);
9387 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9389 if ( CanHaveAttributes() )
9391 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9392 attr
->SetTextColour(colour
);
9397 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9399 if ( CanHaveAttributes() )
9401 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9402 attr
->SetFont(font
);
9407 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9409 if ( CanHaveAttributes() )
9411 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9412 attr
->SetAlignment(horiz
, vert
);
9417 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9419 if ( CanHaveAttributes() )
9421 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9422 attr
->SetOverflow(allow
);
9427 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9429 if ( CanHaveAttributes() )
9431 int cell_rows
, cell_cols
;
9433 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9434 attr
->GetSize(&cell_rows
, &cell_cols
);
9435 attr
->SetSize(num_rows
, num_cols
);
9438 // Cannot set the size of a cell to 0 or negative values
9439 // While it is perfectly legal to do that, this function cannot
9440 // handle all the possibilies, do it by hand by getting the CellAttr.
9441 // You can only set the size of a cell to 1,1 or greater with this fn
9442 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9443 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9444 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9445 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9447 // if this was already a multicell then "turn off" the other cells first
9448 if ((cell_rows
> 1) || (cell_rows
> 1))
9451 for (j
=row
; j
<row
+cell_rows
; j
++)
9453 for (i
=col
; i
<col
+cell_cols
; i
++)
9455 if ((i
!= col
) || (j
!= row
))
9457 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9458 attr_stub
->SetSize( 1, 1 );
9459 attr_stub
->DecRef();
9465 // mark the cells that will be covered by this cell to
9466 // negative or zero values to point back at this cell
9467 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9470 for (j
=row
; j
<row
+num_rows
; j
++)
9472 for (i
=col
; i
<col
+num_cols
; i
++)
9474 if ((i
!= col
) || (j
!= row
))
9476 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9477 attr_stub
->SetSize( row
-j
, col
-i
);
9478 attr_stub
->DecRef();
9486 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9488 if ( CanHaveAttributes() )
9490 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9491 attr
->SetRenderer(renderer
);
9496 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9498 if ( CanHaveAttributes() )
9500 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9501 attr
->SetEditor(editor
);
9506 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9508 if ( CanHaveAttributes() )
9510 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9511 attr
->SetReadOnly(isReadOnly
);
9516 // ----------------------------------------------------------------------------
9517 // Data type registration
9518 // ----------------------------------------------------------------------------
9520 void wxGrid::RegisterDataType(const wxString
& typeName
,
9521 wxGridCellRenderer
* renderer
,
9522 wxGridCellEditor
* editor
)
9524 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9528 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9530 wxString typeName
= m_table
->GetTypeName(row
, col
);
9531 return GetDefaultEditorForType(typeName
);
9534 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9536 wxString typeName
= m_table
->GetTypeName(row
, col
);
9537 return GetDefaultRendererForType(typeName
);
9541 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9543 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9544 if ( index
== wxNOT_FOUND
)
9546 wxFAIL_MSG(wxT("Unknown data type name"));
9551 return m_typeRegistry
->GetEditor(index
);
9555 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9557 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9558 if ( index
== wxNOT_FOUND
)
9560 wxFAIL_MSG(wxT("Unknown data type name"));
9565 return m_typeRegistry
->GetRenderer(index
);
9569 // ----------------------------------------------------------------------------
9571 // ----------------------------------------------------------------------------
9573 void wxGrid::EnableDragRowSize( bool enable
)
9575 m_canDragRowSize
= enable
;
9579 void wxGrid::EnableDragColSize( bool enable
)
9581 m_canDragColSize
= enable
;
9584 void wxGrid::EnableDragGridSize( bool enable
)
9586 m_canDragGridSize
= enable
;
9589 void wxGrid::EnableDragCell( bool enable
)
9591 m_canDragCell
= enable
;
9594 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9596 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9598 if ( resizeExistingRows
)
9600 // since we are resizing all rows to the default row size,
9601 // we can simply clear the row heights and row bottoms
9602 // arrays (which also allows us to take advantage of
9603 // some speed optimisations)
9604 m_rowHeights
.Empty();
9605 m_rowBottoms
.Empty();
9606 if ( !GetBatchCount() )
9611 void wxGrid::SetRowSize( int row
, int height
)
9613 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9615 // See comment in SetColSize
9616 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9618 if ( m_rowHeights
.IsEmpty() )
9620 // need to really create the array
9624 int h
= wxMax( 0, height
);
9625 int diff
= h
- m_rowHeights
[row
];
9627 m_rowHeights
[row
] = h
;
9629 for ( i
= row
; i
< m_numRows
; i
++ )
9631 m_rowBottoms
[i
] += diff
;
9633 if ( !GetBatchCount() )
9637 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9639 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9641 if ( resizeExistingCols
)
9643 // since we are resizing all columns to the default column size,
9644 // we can simply clear the col widths and col rights
9645 // arrays (which also allows us to take advantage of
9646 // some speed optimisations)
9647 m_colWidths
.Empty();
9648 m_colRights
.Empty();
9649 if ( !GetBatchCount() )
9654 void wxGrid::SetColSize( int col
, int width
)
9656 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9658 // should we check that it's bigger than GetColMinimalWidth(col) here?
9660 // No, because it is reasonable to assume the library user know's
9661 // what he is doing. However whe should test against the weaker
9662 // constariant of minimalAcceptableWidth, as this breaks rendering
9664 // This test then fixes sf.net bug #645734
9666 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9668 if ( m_colWidths
.IsEmpty() )
9670 // need to really create the array
9674 // if < 0 calc new width from label
9678 wxArrayString lines
;
9679 wxClientDC
dc(m_colLabelWin
);
9680 dc
.SetFont(GetLabelFont());
9681 StringToLines(GetColLabelValue(col
), lines
);
9682 GetTextBoxSize(dc
, lines
, &w
, &h
);
9685 int w
= wxMax( 0, width
);
9686 int diff
= w
- m_colWidths
[col
];
9687 m_colWidths
[col
] = w
;
9690 for ( i
= col
; i
< m_numCols
; i
++ )
9692 m_colRights
[i
] += diff
;
9694 if ( !GetBatchCount() )
9699 void wxGrid::SetColMinimalWidth( int col
, int width
)
9701 if (width
> GetColMinimalAcceptableWidth()) {
9702 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9703 m_colMinWidths
[key
] = width
;
9707 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9709 if (width
> GetRowMinimalAcceptableHeight()) {
9710 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9711 m_rowMinHeights
[key
] = width
;
9715 int wxGrid::GetColMinimalWidth(int col
) const
9717 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9718 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9719 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9722 int wxGrid::GetRowMinimalHeight(int row
) const
9724 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9725 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9726 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9729 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9731 // We do allow a width of 0 since this gives us
9732 // an easy way to temporarily hidding columns.
9735 m_minAcceptableColWidth
= width
;
9738 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9740 // We do allow a height of 0 since this gives us
9741 // an easy way to temporarily hidding rows.
9744 m_minAcceptableRowHeight
= height
;
9747 int wxGrid::GetColMinimalAcceptableWidth() const
9749 return m_minAcceptableColWidth
;
9752 int wxGrid::GetRowMinimalAcceptableHeight() const
9754 return m_minAcceptableRowHeight
;
9757 // ----------------------------------------------------------------------------
9759 // ----------------------------------------------------------------------------
9761 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9763 wxClientDC
dc(m_gridWin
);
9765 //Cancel editting of cell
9766 HideCellEditControl();
9767 SaveEditControlValue();
9769 // init both of them to avoid compiler warnings, even if weo nly need one
9777 wxCoord extent
, extentMax
= 0;
9778 int max
= column
? m_numRows
: m_numCols
;
9779 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9786 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9787 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9790 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9791 extent
= column
? size
.x
: size
.y
;
9792 if ( extent
> extentMax
)
9803 // now also compare with the column label extent
9805 dc
.SetFont( GetLabelFont() );
9809 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9810 if( GetColLabelTextOrientation() == wxVERTICAL
)
9814 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9816 extent
= column
? w
: h
;
9817 if ( extent
> extentMax
)
9824 // empty column - give default extent (notice that if extentMax is less
9825 // than default extent but != 0, it's ok)
9826 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9832 // leave some space around text
9843 SetColSize(col
, extentMax
);
9844 if ( !GetBatchCount() )
9847 m_gridWin
->GetClientSize( &cw
, &ch
);
9848 wxRect
rect ( CellToRect( 0, col
) );
9850 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9851 rect
.width
= cw
- rect
.x
;
9852 rect
.height
= m_colLabelHeight
;
9853 m_colLabelWin
->Refresh( true, &rect
);
9858 SetRowSize(row
, extentMax
);
9859 if ( !GetBatchCount() )
9862 m_gridWin
->GetClientSize( &cw
, &ch
);
9863 wxRect
rect ( CellToRect( row
, 0 ) );
9865 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9866 rect
.width
= m_rowLabelWidth
;
9867 rect
.height
= ch
- rect
.y
;
9868 m_rowLabelWin
->Refresh( true, &rect
);
9874 SetColMinimalWidth(col
, extentMax
);
9876 SetRowMinimalHeight(row
, extentMax
);
9880 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9882 int width
= m_rowLabelWidth
;
9887 for ( int col
= 0; col
< m_numCols
; col
++ )
9891 AutoSizeColumn(col
, setAsMin
);
9894 width
+= GetColWidth(col
);
9903 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9905 int height
= m_colLabelHeight
;
9910 for ( int row
= 0; row
< m_numRows
; row
++ )
9914 AutoSizeRow(row
, setAsMin
);
9917 height
+= GetRowHeight(row
);
9926 void wxGrid::AutoSize()
9930 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9932 // round up the size to a multiple of scroll step - this ensures that we
9933 // won't get the scrollbars if we're sized exactly to this width
9934 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9936 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
9937 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
9939 // distribute the extra space between the columns/rows to avoid having
9940 // extra white space
9942 // Remove the extra m_extraWidth + 1 added above
9943 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9944 if ( diff
&& m_numCols
)
9946 // try to resize the columns uniformly
9947 wxCoord diffPerCol
= diff
/ m_numCols
;
9950 for ( int col
= 0; col
< m_numCols
; col
++ )
9952 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9956 // add remaining amount to the last columns
9957 diff
-= diffPerCol
* m_numCols
;
9960 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9962 SetColSize(col
, GetColWidth(col
) + 1);
9968 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9969 if ( diff
&& m_numRows
)
9971 // try to resize the columns uniformly
9972 wxCoord diffPerRow
= diff
/ m_numRows
;
9975 for ( int row
= 0; row
< m_numRows
; row
++ )
9977 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9981 // add remaining amount to the last rows
9982 diff
-= diffPerRow
* m_numRows
;
9985 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9987 SetRowSize(row
, GetRowHeight(row
) + 1);
9994 SetClientSize(sizeFit
);
9997 void wxGrid::AutoSizeRowLabelSize( int row
)
9999 wxArrayString lines
;
10002 // Hide the edit control, so it
10003 // won't interfer with drag-shrinking.
10004 if( IsCellEditControlShown() )
10006 HideCellEditControl();
10007 SaveEditControlValue();
10010 // autosize row height depending on label text
10011 StringToLines( GetRowLabelValue( row
), lines
);
10012 wxClientDC
dc( m_rowLabelWin
);
10013 GetTextBoxSize( dc
, lines
, &w
, &h
);
10014 if( h
< m_defaultRowHeight
)
10015 h
= m_defaultRowHeight
;
10016 SetRowSize(row
, h
);
10020 void wxGrid::AutoSizeColLabelSize( int col
)
10022 wxArrayString lines
;
10025 // Hide the edit control, so it
10026 // won't interfer with drag-shrinking.
10027 if( IsCellEditControlShown() )
10029 HideCellEditControl();
10030 SaveEditControlValue();
10033 // autosize column width depending on label text
10034 StringToLines( GetColLabelValue( col
), lines
);
10035 wxClientDC
dc( m_colLabelWin
);
10036 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
10037 GetTextBoxSize( dc
, lines
, &w
, &h
);
10039 GetTextBoxSize( dc
, lines
, &h
, &w
);
10040 if( w
< m_defaultColWidth
)
10041 w
= m_defaultColWidth
;
10042 SetColSize(col
, w
);
10046 wxSize
wxGrid::DoGetBestSize() const
10048 // don't set sizes, only calculate them
10049 wxGrid
*self
= (wxGrid
*)this; // const_cast
10052 width
= self
->SetOrCalcColumnSizes(true);
10053 height
= self
->SetOrCalcRowSizes(true);
10055 if (!width
) width
=100;
10056 if (!height
) height
=80;
10058 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
10059 // of the scrollbars, is there any magic incantaion for that?
10061 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10063 width
+= 1 + xpu
- (width
% xpu
);
10065 height
+= 1 + ypu
- (height
% ypu
);
10067 // limit to 1/4 of the screen size
10068 int maxwidth
, maxheight
;
10069 wxDisplaySize( & maxwidth
, & maxheight
);
10072 if ( width
> maxwidth
) width
= maxwidth
;
10073 if ( height
> maxheight
) height
= maxheight
;
10076 wxSize
best(width
, height
);
10077 // NOTE: This size should be cached, but first we need to add calls to
10078 // InvalidateBestSize everywhere that could change the results of this
10080 // CacheBestSize(size);
10090 wxPen
& wxGrid::GetDividerPen() const
10095 // ----------------------------------------------------------------------------
10096 // cell value accessor functions
10097 // ----------------------------------------------------------------------------
10099 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10103 m_table
->SetValue( row
, col
, s
);
10104 if ( !GetBatchCount() )
10107 wxRect
rect( CellToRect( row
, col
) );
10109 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10110 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10111 m_gridWin
->Refresh( false, &rect
);
10114 if ( m_currentCellCoords
.GetRow() == row
&&
10115 m_currentCellCoords
.GetCol() == col
&&
10116 IsCellEditControlShown())
10117 // Note: If we are using IsCellEditControlEnabled,
10118 // this interacts badly with calling SetCellValue from
10119 // an EVT_GRID_CELL_CHANGE handler.
10121 HideCellEditControl();
10122 ShowCellEditControl(); // will reread data from table
10129 // ------ Block, row and col selection
10132 void wxGrid::SelectRow( int row
, bool addToSelected
)
10134 if ( IsSelection() && !addToSelected
)
10138 m_selection
->SelectRow( row
, false, addToSelected
);
10142 void wxGrid::SelectCol( int col
, bool addToSelected
)
10144 if ( IsSelection() && !addToSelected
)
10148 m_selection
->SelectCol( col
, false, addToSelected
);
10152 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10153 bool addToSelected
)
10155 if ( IsSelection() && !addToSelected
)
10159 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10160 false, addToSelected
);
10164 void wxGrid::SelectAll()
10166 if ( m_numRows
> 0 && m_numCols
> 0 )
10169 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10174 // ------ Cell, row and col deselection
10177 void wxGrid::DeselectRow( int row
)
10179 if ( !m_selection
)
10182 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10184 if ( m_selection
->IsInSelection(row
, 0 ) )
10185 m_selection
->ToggleCellSelection( row
, 0);
10189 int nCols
= GetNumberCols();
10190 for ( int i
= 0; i
< nCols
; i
++ )
10192 if ( m_selection
->IsInSelection(row
, i
) )
10193 m_selection
->ToggleCellSelection( row
, i
);
10198 void wxGrid::DeselectCol( int col
)
10200 if ( !m_selection
)
10203 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10205 if ( m_selection
->IsInSelection(0, col
) )
10206 m_selection
->ToggleCellSelection( 0, col
);
10210 int nRows
= GetNumberRows();
10211 for ( int i
= 0; i
< nRows
; i
++ )
10213 if ( m_selection
->IsInSelection(i
, col
) )
10214 m_selection
->ToggleCellSelection(i
, col
);
10219 void wxGrid::DeselectCell( int row
, int col
)
10221 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10222 m_selection
->ToggleCellSelection(row
, col
);
10225 bool wxGrid::IsSelection()
10227 return ( m_selection
&& (m_selection
->IsSelection() ||
10228 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10229 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10232 bool wxGrid::IsInSelection( int row
, int col
) const
10234 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10235 ( row
>= m_selectingTopLeft
.GetRow() &&
10236 col
>= m_selectingTopLeft
.GetCol() &&
10237 row
<= m_selectingBottomRight
.GetRow() &&
10238 col
<= m_selectingBottomRight
.GetCol() )) );
10241 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10243 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10244 return m_selection
->m_cellSelection
;
10246 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10248 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10249 return m_selection
->m_blockSelectionTopLeft
;
10251 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10253 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10254 return m_selection
->m_blockSelectionBottomRight
;
10256 wxArrayInt
wxGrid::GetSelectedRows() const
10258 if (!m_selection
) { wxArrayInt a
; return a
; }
10259 return m_selection
->m_rowSelection
;
10261 wxArrayInt
wxGrid::GetSelectedCols() const
10263 if (!m_selection
) { wxArrayInt a
; return a
; }
10264 return m_selection
->m_colSelection
;
10268 void wxGrid::ClearSelection()
10270 m_selectingTopLeft
= wxGridNoCellCoords
;
10271 m_selectingBottomRight
= wxGridNoCellCoords
;
10273 m_selection
->ClearSelection();
10277 // This function returns the rectangle that encloses the given block
10278 // in device coords clipped to the client size of the grid window.
10280 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10281 const wxGridCellCoords
&bottomRight
)
10283 wxRect
rect( wxGridNoCellRect
);
10286 cellRect
= CellToRect( topLeft
);
10287 if ( cellRect
!= wxGridNoCellRect
)
10293 rect
= wxRect(0,0,0,0);
10296 cellRect
= CellToRect( bottomRight
);
10297 if ( cellRect
!= wxGridNoCellRect
)
10303 return wxGridNoCellRect
;
10307 int left
= rect
.GetLeft();
10308 int top
= rect
.GetTop();
10309 int right
= rect
.GetRight();
10310 int bottom
= rect
.GetBottom();
10312 int leftCol
= topLeft
.GetCol();
10313 int topRow
= topLeft
.GetRow();
10314 int rightCol
= bottomRight
.GetCol();
10315 int bottomRow
= bottomRight
.GetRow();
10333 topRow
= bottomRow
;
10338 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10340 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10342 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10344 cellRect
= CellToRect( j
, i
);
10346 if (cellRect
.x
< left
)
10348 if (cellRect
.y
< top
)
10350 if (cellRect
.x
+ cellRect
.width
> right
)
10351 right
= cellRect
.x
+ cellRect
.width
;
10352 if (cellRect
.y
+ cellRect
.height
> bottom
)
10353 bottom
= cellRect
.y
+ cellRect
.height
;
10355 else i
= rightCol
; // jump over inner cells.
10359 // convert to scrolled coords
10361 CalcScrolledPosition( left
, top
, &left
, &top
);
10362 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10365 m_gridWin
->GetClientSize( &cw
, &ch
);
10367 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10368 return wxRect(0,0,0,0);
10370 rect
.SetLeft( wxMax(0, left
) );
10371 rect
.SetTop( wxMax(0, top
) );
10372 rect
.SetRight( wxMin(cw
, right
) );
10373 rect
.SetBottom( wxMin(ch
, bottom
) );
10379 // ------ Grid event classes
10382 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10384 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10385 int row
, int col
, int x
, int y
, bool sel
,
10386 bool control
, bool shift
, bool alt
, bool meta
)
10387 : wxNotifyEvent( type
, id
)
10394 m_control
= control
;
10399 SetEventObject(obj
);
10403 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10405 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10406 int rowOrCol
, int x
, int y
,
10407 bool control
, bool shift
, bool alt
, bool meta
)
10408 : wxNotifyEvent( type
, id
)
10410 m_rowOrCol
= rowOrCol
;
10413 m_control
= control
;
10418 SetEventObject(obj
);
10422 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10424 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10425 const wxGridCellCoords
& topLeft
,
10426 const wxGridCellCoords
& bottomRight
,
10427 bool sel
, bool control
,
10428 bool shift
, bool alt
, bool meta
)
10429 : wxNotifyEvent( type
, id
)
10431 m_topLeft
= topLeft
;
10432 m_bottomRight
= bottomRight
;
10434 m_control
= control
;
10439 SetEventObject(obj
);
10443 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10445 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10446 wxObject
* obj
, int row
,
10447 int col
, wxControl
* ctrl
)
10448 : wxCommandEvent(type
, id
)
10450 SetEventObject(obj
);
10456 #endif // wxUSE_GRID