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"
44 #include "wx/textfile.h"
45 #include "wx/spinctrl.h"
46 #include "wx/tokenzr.h"
47 #include "wx/renderer.h"
50 #include "wx/generic/gridsel.h"
52 #if defined(__WXMOTIF__)
53 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
55 #define WXUNUSED_MOTIF(identifier) identifier
58 #if defined(__WXGTK__)
59 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
61 #define WXUNUSED_GTK(identifier) identifier
64 // Required for wxIs... functions
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
72 class WXDLLIMPEXP_ADV
);
74 struct wxGridCellWithAttr
76 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
77 : coords(row
, col
), attr(attr_
)
86 wxGridCellCoords coords
;
90 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
91 // without rewriting the macros, which require a public copy constructor.
94 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
95 class WXDLLIMPEXP_ADV
);
97 #include "wx/arrimpl.cpp"
99 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
100 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
121 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
122 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
131 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
132 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
133 const wxPoint
&pos
, const wxSize
&size
);
138 void OnPaint( wxPaintEvent
& event
);
139 void OnMouseEvent( wxMouseEvent
& event
);
140 void OnMouseWheel( wxMouseEvent
& event
);
141 void OnKeyDown( wxKeyEvent
& event
);
142 void OnKeyUp( wxKeyEvent
& );
143 void OnChar( wxKeyEvent
& );
145 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
146 DECLARE_EVENT_TABLE()
147 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
151 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
154 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
155 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
156 const wxPoint
&pos
, const wxSize
&size
);
161 void OnPaint( wxPaintEvent
&event
);
162 void OnMouseEvent( wxMouseEvent
& event
);
163 void OnMouseWheel( wxMouseEvent
& event
);
164 void OnKeyDown( wxKeyEvent
& event
);
165 void OnKeyUp( wxKeyEvent
& );
166 void OnChar( wxKeyEvent
& );
168 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
169 DECLARE_EVENT_TABLE()
170 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
174 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
177 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
178 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
179 const wxPoint
&pos
, const wxSize
&size
);
184 void OnMouseEvent( wxMouseEvent
& event
);
185 void OnMouseWheel( wxMouseEvent
& event
);
186 void OnKeyDown( wxKeyEvent
& event
);
187 void OnKeyUp( wxKeyEvent
& );
188 void OnChar( wxKeyEvent
& );
189 void OnPaint( wxPaintEvent
& event
);
191 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
192 DECLARE_EVENT_TABLE()
193 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
196 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
201 m_owner
= (wxGrid
*)NULL
;
202 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
203 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
206 wxGridWindow( wxGrid
*parent
,
207 wxGridRowLabelWindow
*rowLblWin
,
208 wxGridColLabelWindow
*colLblWin
,
209 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
212 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
214 wxGrid
* GetOwner() { return m_owner
; }
218 wxGridRowLabelWindow
*m_rowLabelWin
;
219 wxGridColLabelWindow
*m_colLabelWin
;
221 void OnPaint( wxPaintEvent
&event
);
222 void OnMouseWheel( wxMouseEvent
& event
);
223 void OnMouseEvent( wxMouseEvent
& event
);
224 void OnKeyDown( wxKeyEvent
& );
225 void OnKeyUp( wxKeyEvent
& );
226 void OnChar( wxKeyEvent
& );
227 void OnEraseBackground( wxEraseEvent
& );
228 void OnFocus( wxFocusEvent
& );
230 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
231 DECLARE_EVENT_TABLE()
232 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
)) &&
1089 !wxIsSameDouble(value
, m_valueOld
) )
1091 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1092 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1094 grid
->GetTable()->SetValue(row
, col
, text
);
1101 void wxGridCellFloatEditor::Reset()
1103 DoReset(GetString());
1106 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1108 int keycode
= event
.GetKeyCode();
1110 tmpbuf
[0] = (char) keycode
;
1112 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1114 bool is_decimal_point
= ( strbuf
==
1115 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1117 bool is_decimal_point
= ( strbuf
== _T(".") );
1119 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1120 || is_decimal_point
)
1122 wxGridCellTextEditor::StartingKey(event
);
1124 // skip Skip() below
1131 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1142 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1146 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1148 m_precision
= (int)tmp
;
1150 // skip the error message below
1155 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1159 wxString
wxGridCellFloatEditor::GetString() const
1162 if ( m_width
== -1 )
1164 // default width/precision
1167 else if ( m_precision
== -1 )
1169 // default precision
1170 fmt
.Printf(_T("%%%d.f"), m_width
);
1174 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1177 return wxString::Format(fmt
, m_valueOld
);
1180 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1182 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1184 int keycode
= event
.GetKeyCode();
1185 printf("%d\n", keycode
);
1186 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1188 tmpbuf
[0] = (char) keycode
;
1190 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1192 bool is_decimal_point
=
1193 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1194 wxLOCALE_CAT_NUMBER
) );
1196 bool is_decimal_point
= ( strbuf
== _T(".") );
1198 if ( (keycode
< 128) &&
1199 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1200 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1207 #endif // wxUSE_TEXTCTRL
1211 // ----------------------------------------------------------------------------
1212 // wxGridCellBoolEditor
1213 // ----------------------------------------------------------------------------
1215 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1217 wxEvtHandler
* evtHandler
)
1219 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1220 wxDefaultPosition
, wxDefaultSize
,
1223 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1226 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1228 bool resize
= false;
1229 wxSize size
= m_control
->GetSize();
1230 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1232 // check if the checkbox is not too big/small for this cell
1233 wxSize sizeBest
= m_control
->GetBestSize();
1234 if ( !(size
== sizeBest
) )
1236 // reset to default size if it had been made smaller
1242 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1244 // leave 1 pixel margin
1245 size
.x
= size
.y
= minSize
- 2;
1252 m_control
->SetSize(size
);
1255 // position it in the centre of the rectangle (TODO: support alignment?)
1257 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1258 // the checkbox without label still has some space to the right in wxGTK,
1259 // so shift it to the right
1261 #elif defined(__WXMSW__)
1262 // here too, but in other way
1267 int hAlign
= wxALIGN_CENTRE
;
1268 int vAlign
= wxALIGN_CENTRE
;
1270 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1273 if (hAlign
== wxALIGN_LEFT
)
1279 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1281 else if (hAlign
== wxALIGN_RIGHT
)
1283 x
= r
.x
+ r
.width
- size
.x
- 2;
1284 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1286 else if (hAlign
== wxALIGN_CENTRE
)
1288 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1289 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1292 m_control
->Move(x
, y
);
1295 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1297 m_control
->Show(show
);
1301 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1302 CBox()->SetBackgroundColour(colBg
);
1306 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1308 wxASSERT_MSG(m_control
,
1309 wxT("The wxGridCellEditor must be Created first!"));
1311 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1312 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1315 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1316 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1318 CBox()->SetValue(m_startValue
);
1322 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1325 wxASSERT_MSG(m_control
,
1326 wxT("The wxGridCellEditor must be Created first!"));
1328 bool changed
= false;
1329 bool value
= CBox()->GetValue();
1330 if ( value
!= m_startValue
)
1335 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1336 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1338 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1344 void wxGridCellBoolEditor::Reset()
1346 wxASSERT_MSG(m_control
,
1347 wxT("The wxGridCellEditor must be Created first!"));
1349 CBox()->SetValue(m_startValue
);
1352 void wxGridCellBoolEditor::StartingClick()
1354 CBox()->SetValue(!CBox()->GetValue());
1357 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1359 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1361 int keycode
= event
.GetKeyCode();
1374 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1376 int keycode
= event
.GetKeyCode();
1380 CBox()->SetValue(!CBox()->GetValue());
1384 CBox()->SetValue(true);
1388 CBox()->SetValue(false);
1394 // return the value as "1" for true and the empty string for false
1395 wxString
wxGridCellBoolEditor::GetValue() const
1397 bool bSet
= CBox()->GetValue();
1398 return bSet
? _T("1") : wxEmptyString
;
1401 #endif // wxUSE_CHECKBOX
1405 // ----------------------------------------------------------------------------
1406 // wxGridCellChoiceEditor
1407 // ----------------------------------------------------------------------------
1409 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1411 : m_choices(choices
),
1412 m_allowOthers(allowOthers
) { }
1414 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1415 const wxString choices
[],
1417 : m_allowOthers(allowOthers
)
1421 m_choices
.Alloc(count
);
1422 for ( size_t n
= 0; n
< count
; n
++ )
1424 m_choices
.Add(choices
[n
]);
1429 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1431 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1432 editor
->m_allowOthers
= m_allowOthers
;
1433 editor
->m_choices
= m_choices
;
1438 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1440 wxEvtHandler
* evtHandler
)
1442 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1443 wxDefaultPosition
, wxDefaultSize
,
1445 m_allowOthers
? 0 : wxCB_READONLY
);
1447 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1450 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1451 wxGridCellAttr
* attr
)
1453 // as we fill the entire client area, don't do anything here to minimize
1456 // TODO: It doesn't actually fill the client area since the height of a
1457 // combo always defaults to the standard... Until someone has time to
1458 // figure out the right rectangle to paint, just do it the normal way...
1459 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1462 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1464 wxASSERT_MSG(m_control
,
1465 wxT("The wxGridCellEditor must be Created first!"));
1467 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1469 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1471 // Don't immediately end if we get a kill focus event within BeginEdit
1473 evtHandler
->SetInSetFocus(true);
1475 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1478 Combo()->SetValue(m_startValue
);
1481 // find the right position, or default to the first if not found
1482 int pos
= Combo()->FindString(m_startValue
);
1483 if (pos
== wxNOT_FOUND
)
1485 Combo()->SetSelection(pos
);
1487 Combo()->SetInsertionPointEnd();
1488 Combo()->SetFocus();
1491 evtHandler
->SetInSetFocus(false);
1494 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1497 wxString value
= Combo()->GetValue();
1498 if ( value
== m_startValue
)
1501 grid
->GetTable()->SetValue(row
, col
, value
);
1506 void wxGridCellChoiceEditor::Reset()
1508 Combo()->SetValue(m_startValue
);
1509 Combo()->SetInsertionPointEnd();
1512 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1522 wxStringTokenizer
tk(params
, _T(','));
1523 while ( tk
.HasMoreTokens() )
1525 m_choices
.Add(tk
.GetNextToken());
1529 // return the value in the text control
1530 wxString
wxGridCellChoiceEditor::GetValue() const
1532 return Combo()->GetValue();
1535 #endif // wxUSE_COMBOBOX
1537 // ----------------------------------------------------------------------------
1538 // wxGridCellEditorEvtHandler
1539 // ----------------------------------------------------------------------------
1541 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1543 // Don't disable the cell if we're just starting to edit it
1548 m_grid
->DisableCellEditControl();
1553 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1555 switch ( event
.GetKeyCode() )
1559 m_grid
->DisableCellEditControl();
1563 m_grid
->GetEventHandler()->ProcessEvent( event
);
1567 case WXK_NUMPAD_ENTER
:
1568 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1569 m_editor
->HandleReturn(event
);
1577 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1579 switch ( event
.GetKeyCode() )
1584 case WXK_NUMPAD_ENTER
:
1592 // ----------------------------------------------------------------------------
1593 // wxGridCellWorker is an (almost) empty common base class for
1594 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1595 // ----------------------------------------------------------------------------
1597 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1602 wxGridCellWorker::~wxGridCellWorker()
1606 // ============================================================================
1608 // ============================================================================
1610 // ----------------------------------------------------------------------------
1611 // wxGridCellRenderer
1612 // ----------------------------------------------------------------------------
1614 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1615 wxGridCellAttr
& attr
,
1618 int WXUNUSED(row
), int WXUNUSED(col
),
1621 dc
.SetBackgroundMode( wxSOLID
);
1623 // grey out fields if the grid is disabled
1624 if ( grid
.IsEnabled() )
1628 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1632 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1637 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1640 dc
.SetPen( *wxTRANSPARENT_PEN
);
1641 dc
.DrawRectangle(rect
);
1644 // ----------------------------------------------------------------------------
1645 // wxGridCellStringRenderer
1646 // ----------------------------------------------------------------------------
1648 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1649 const wxGridCellAttr
& attr
,
1653 dc
.SetBackgroundMode( wxTRANSPARENT
);
1655 // TODO some special colours for attr.IsReadOnly() case?
1657 // different coloured text when the grid is disabled
1658 if ( grid
.IsEnabled() )
1662 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1663 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1667 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1668 dc
.SetTextForeground( attr
.GetTextColour() );
1673 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1674 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1677 dc
.SetFont( attr
.GetFont() );
1680 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1682 const wxString
& text
)
1684 wxCoord x
= 0, y
= 0, max_x
= 0;
1685 dc
.SetFont(attr
.GetFont());
1686 wxStringTokenizer
tk(text
, _T('\n'));
1687 while ( tk
.HasMoreTokens() )
1689 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1690 max_x
= wxMax(max_x
, x
);
1693 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1695 return wxSize(max_x
, y
);
1698 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1699 wxGridCellAttr
& attr
,
1703 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1706 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1707 wxGridCellAttr
& attr
,
1709 const wxRect
& rectCell
,
1713 wxRect rect
= rectCell
;
1716 // erase only this cells background, overflow cells should have been erased
1717 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1720 attr
.GetAlignment(&hAlign
, &vAlign
);
1722 int overflowCols
= 0;
1724 if (attr
.GetOverflow())
1726 int cols
= grid
.GetNumberCols();
1727 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1728 int cell_rows
, cell_cols
;
1729 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1730 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1732 int i
, c_cols
, c_rows
;
1733 for (i
= col
+cell_cols
; i
< cols
; i
++)
1735 bool is_empty
= true;
1736 for (int j
=row
; j
<row
+cell_rows
; j
++)
1738 // check w/ anchor cell for multicell block
1739 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1740 if (c_rows
> 0) c_rows
= 0;
1741 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1748 rect
.width
+= grid
.GetColSize(i
);
1754 if (rect
.width
>= best_width
)
1757 overflowCols
= i
- col
- cell_cols
+ 1;
1758 if (overflowCols
>= cols
)
1759 overflowCols
= cols
- 1;
1762 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1764 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1766 clip
.x
+= rectCell
.width
;
1767 // draw each overflow cell individually
1768 int col_end
= col
+cell_cols
+overflowCols
;
1769 if (col_end
>= grid
.GetNumberCols())
1770 col_end
= grid
.GetNumberCols() - 1;
1771 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1773 clip
.width
= grid
.GetColSize(i
) - 1;
1774 dc
.DestroyClippingRegion();
1775 dc
.SetClippingRegion(clip
);
1777 SetTextColoursAndFont(grid
, attr
, dc
,
1778 grid
.IsInSelection(row
,i
));
1780 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1781 rect
, hAlign
, vAlign
);
1782 clip
.x
+= grid
.GetColSize(i
) - 1;
1788 dc
.DestroyClippingRegion();
1792 // now we only have to draw the text
1793 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1795 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1796 rect
, hAlign
, vAlign
);
1799 // ----------------------------------------------------------------------------
1800 // wxGridCellNumberRenderer
1801 // ----------------------------------------------------------------------------
1803 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1805 wxGridTableBase
*table
= grid
.GetTable();
1807 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1809 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1813 text
= table
->GetValue(row
, col
);
1819 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1820 wxGridCellAttr
& attr
,
1822 const wxRect
& rectCell
,
1826 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1828 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1830 // draw the text right aligned by default
1832 attr
.GetAlignment(&hAlign
, &vAlign
);
1833 hAlign
= wxALIGN_RIGHT
;
1835 wxRect rect
= rectCell
;
1838 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1841 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1842 wxGridCellAttr
& attr
,
1846 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1849 // ----------------------------------------------------------------------------
1850 // wxGridCellFloatRenderer
1851 // ----------------------------------------------------------------------------
1853 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1856 SetPrecision(precision
);
1859 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1861 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1862 renderer
->m_width
= m_width
;
1863 renderer
->m_precision
= m_precision
;
1864 renderer
->m_format
= m_format
;
1869 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1871 wxGridTableBase
*table
= grid
.GetTable();
1876 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1878 val
= table
->GetValueAsDouble(row
, col
);
1883 text
= table
->GetValue(row
, col
);
1884 hasDouble
= text
.ToDouble(&val
);
1891 if ( m_width
== -1 )
1893 if ( m_precision
== -1 )
1895 // default width/precision
1896 m_format
= _T("%f");
1900 m_format
.Printf(_T("%%.%df"), m_precision
);
1903 else if ( m_precision
== -1 )
1905 // default precision
1906 m_format
.Printf(_T("%%%d.f"), m_width
);
1910 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1914 text
.Printf(m_format
, val
);
1917 //else: text already contains the string
1922 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1923 wxGridCellAttr
& attr
,
1925 const wxRect
& rectCell
,
1929 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1931 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1933 // draw the text right aligned by default
1935 attr
.GetAlignment(&hAlign
, &vAlign
);
1936 hAlign
= wxALIGN_RIGHT
;
1938 wxRect rect
= rectCell
;
1941 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1944 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1945 wxGridCellAttr
& attr
,
1949 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1952 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1956 // reset to defaults
1962 wxString tmp
= params
.BeforeFirst(_T(','));
1966 if ( tmp
.ToLong(&width
) )
1968 SetWidth((int)width
);
1972 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1975 tmp
= params
.AfterFirst(_T(','));
1979 if ( tmp
.ToLong(&precision
) )
1981 SetPrecision((int)precision
);
1985 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1992 // ----------------------------------------------------------------------------
1993 // wxGridCellBoolRenderer
1994 // ----------------------------------------------------------------------------
1996 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1998 // FIXME these checkbox size calculations are really ugly...
2000 // between checkmark and box
2001 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2003 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2004 wxGridCellAttr
& WXUNUSED(attr
),
2009 // compute it only once (no locks for MT safeness in GUI thread...)
2010 if ( !ms_sizeCheckMark
.x
)
2012 // get checkbox size
2013 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2014 wxSize size
= checkbox
->GetBestSize();
2015 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
2017 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2018 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2019 checkSize
-= size
.y
/ 2;
2024 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2027 return ms_sizeCheckMark
;
2030 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2031 wxGridCellAttr
& attr
,
2037 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2039 // draw a check mark in the centre (ignoring alignment - TODO)
2040 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2042 // don't draw outside the cell
2043 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2044 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2046 // and even leave (at least) 1 pixel margin
2047 size
.x
= size
.y
= minSize
- 2;
2050 // draw a border around checkmark
2052 attr
.GetAlignment(& hAlign
, &vAlign
);
2055 if (hAlign
== wxALIGN_CENTRE
)
2057 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2058 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2059 rectBorder
.width
= size
.x
;
2060 rectBorder
.height
= size
.y
;
2062 else if (hAlign
== wxALIGN_LEFT
)
2064 rectBorder
.x
= rect
.x
+ 2;
2065 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2066 rectBorder
.width
= size
.x
;
2067 rectBorder
.height
= size
.y
;
2069 else if (hAlign
== wxALIGN_RIGHT
)
2071 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2072 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2073 rectBorder
.width
= size
.x
;
2074 rectBorder
.height
= size
.y
;
2078 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2079 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2082 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2083 value
= !( !cellval
|| (cellval
== wxT("0")) );
2088 wxRect rectMark
= rectBorder
;
2090 // MSW DrawCheckMark() is weird (and should probably be changed...)
2091 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2095 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2098 dc
.SetTextForeground(attr
.GetTextColour());
2099 dc
.DrawCheckMark(rectMark
);
2102 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2103 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2104 dc
.DrawRectangle(rectBorder
);
2107 // ----------------------------------------------------------------------------
2109 // ----------------------------------------------------------------------------
2111 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2115 m_isReadOnly
= Unset
;
2120 m_attrkind
= wxGridCellAttr::Cell
;
2122 m_sizeRows
= m_sizeCols
= 1;
2123 m_overflow
= UnsetOverflow
;
2125 SetDefAttr(attrDefault
);
2128 wxGridCellAttr
*wxGridCellAttr::Clone() const
2130 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2132 if ( HasTextColour() )
2133 attr
->SetTextColour(GetTextColour());
2134 if ( HasBackgroundColour() )
2135 attr
->SetBackgroundColour(GetBackgroundColour());
2137 attr
->SetFont(GetFont());
2138 if ( HasAlignment() )
2139 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2141 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2145 attr
->SetRenderer(m_renderer
);
2146 m_renderer
->IncRef();
2150 attr
->SetEditor(m_editor
);
2155 attr
->SetReadOnly();
2157 attr
->SetKind( m_attrkind
);
2162 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2164 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2165 SetTextColour(mergefrom
->GetTextColour());
2166 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2167 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2168 if ( !HasFont() && mergefrom
->HasFont() )
2169 SetFont(mergefrom
->GetFont());
2170 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2173 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2174 SetAlignment(hAlign
, vAlign
);
2176 if ( !HasSize() && mergefrom
->HasSize() )
2177 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2179 // Directly access member functions as GetRender/Editor don't just return
2180 // m_renderer/m_editor
2182 // Maybe add support for merge of Render and Editor?
2183 if (!HasRenderer() && mergefrom
->HasRenderer() )
2185 m_renderer
= mergefrom
->m_renderer
;
2186 m_renderer
->IncRef();
2188 if ( !HasEditor() && mergefrom
->HasEditor() )
2190 m_editor
= mergefrom
->m_editor
;
2193 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2194 SetReadOnly(mergefrom
->IsReadOnly());
2196 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2197 SetOverflow(mergefrom
->GetOverflow());
2199 SetDefAttr(mergefrom
->m_defGridAttr
);
2202 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2204 // The size of a cell is normally 1,1
2206 // If this cell is larger (2,2) then this is the top left cell
2207 // the other cells that will be covered (lower right cells) must be
2208 // set to negative or zero values such that
2209 // row + num_rows of the covered cell points to the larger cell (this cell)
2210 // same goes for the col + num_cols.
2212 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2214 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2215 !((num_rows
<=0)&&(num_cols
>0)) ||
2216 !((num_rows
==0)&&(num_cols
==0))),
2217 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2219 m_sizeRows
= num_rows
;
2220 m_sizeCols
= num_cols
;
2223 const wxColour
& wxGridCellAttr::GetTextColour() const
2225 if (HasTextColour())
2229 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2231 return m_defGridAttr
->GetTextColour();
2235 wxFAIL_MSG(wxT("Missing default cell attribute"));
2236 return wxNullColour
;
2241 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2243 if (HasBackgroundColour())
2245 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2246 return m_defGridAttr
->GetBackgroundColour();
2249 wxFAIL_MSG(wxT("Missing default cell attribute"));
2250 return wxNullColour
;
2255 const wxFont
& wxGridCellAttr::GetFont() const
2259 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2260 return m_defGridAttr
->GetFont();
2263 wxFAIL_MSG(wxT("Missing default cell attribute"));
2269 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2278 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2279 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2282 wxFAIL_MSG(wxT("Missing default cell attribute"));
2286 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2289 *num_rows
= m_sizeRows
;
2291 *num_cols
= m_sizeCols
;
2294 // GetRenderer and GetEditor use a slightly different decision path about
2295 // which attribute to use. If a non-default attr object has one then it is
2296 // used, otherwise the default editor or renderer is fetched from the grid and
2297 // used. It should be the default for the data type of the cell. If it is
2298 // NULL (because the table has a type that the grid does not have in its
2299 // registry,) then the grid's default editor or renderer is used.
2301 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2303 wxGridCellRenderer
*renderer
;
2305 if ( m_renderer
&& this != m_defGridAttr
)
2307 // use the cells renderer if it has one
2308 renderer
= m_renderer
;
2311 else // no non default cell renderer
2313 // get default renderer for the data type
2316 // GetDefaultRendererForCell() will do IncRef() for us
2317 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2326 if (m_defGridAttr
&& this != m_defGridAttr
)
2328 // if we still don't have one then use the grid default
2329 // (no need for IncRef() here neither)
2330 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2332 else // default grid attr
2334 // use m_renderer which we had decided not to use initially
2335 renderer
= m_renderer
;
2342 // we're supposed to always find something
2343 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2348 // same as above, except for s/renderer/editor/g
2349 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2351 wxGridCellEditor
*editor
;
2353 if ( m_editor
&& this != m_defGridAttr
)
2355 // use the cells editor if it has one
2359 else // no non default cell editor
2361 // get default editor for the data type
2364 // GetDefaultEditorForCell() will do IncRef() for us
2365 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2374 if ( m_defGridAttr
&& this != m_defGridAttr
)
2376 // if we still don't have one then use the grid default
2377 // (no need for IncRef() here neither)
2378 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2380 else // default grid attr
2382 // use m_editor which we had decided not to use initially
2390 // we're supposed to always find something
2391 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2396 // ----------------------------------------------------------------------------
2397 // wxGridCellAttrData
2398 // ----------------------------------------------------------------------------
2400 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2402 int n
= FindIndex(row
, col
);
2403 if ( n
== wxNOT_FOUND
)
2405 // add the attribute
2406 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2410 // free the old attribute
2411 m_attrs
[(size_t)n
].attr
->DecRef();
2415 // change the attribute
2416 m_attrs
[(size_t)n
].attr
= attr
;
2420 // remove this attribute
2421 m_attrs
.RemoveAt((size_t)n
);
2426 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2428 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2430 int n
= FindIndex(row
, col
);
2431 if ( n
!= wxNOT_FOUND
)
2433 attr
= m_attrs
[(size_t)n
].attr
;
2440 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2442 size_t count
= m_attrs
.GetCount();
2443 for ( size_t n
= 0; n
< count
; n
++ )
2445 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2446 wxCoord row
= coords
.GetRow();
2447 if ((size_t)row
>= pos
)
2451 // If rows inserted, include row counter where necessary
2452 coords
.SetRow(row
+ numRows
);
2454 else if (numRows
< 0)
2456 // If rows deleted ...
2457 if ((size_t)row
>= pos
- numRows
)
2459 // ...either decrement row counter (if row still exists)...
2460 coords
.SetRow(row
+ numRows
);
2464 // ...or remove the attribute
2465 // No need to DecRef the attribute itself since this is
2466 // done be wxGridCellWithAttr's destructor!
2467 m_attrs
.RemoveAt(n
);
2476 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2478 size_t count
= m_attrs
.GetCount();
2479 for ( size_t n
= 0; n
< count
; n
++ )
2481 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2482 wxCoord col
= coords
.GetCol();
2483 if ( (size_t)col
>= pos
)
2487 // If rows inserted, include row counter where necessary
2488 coords
.SetCol(col
+ numCols
);
2490 else if (numCols
< 0)
2492 // If rows deleted ...
2493 if ((size_t)col
>= pos
- numCols
)
2495 // ...either decrement row counter (if row still exists)...
2496 coords
.SetCol(col
+ numCols
);
2500 // ...or remove the attribute
2501 // No need to DecRef the attribute itself since this is
2502 // done be wxGridCellWithAttr's destructor!
2503 m_attrs
.RemoveAt(n
);
2511 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2513 size_t count
= m_attrs
.GetCount();
2514 for ( size_t n
= 0; n
< count
; n
++ )
2516 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2517 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2526 // ----------------------------------------------------------------------------
2527 // wxGridRowOrColAttrData
2528 // ----------------------------------------------------------------------------
2530 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2532 size_t count
= m_attrs
.Count();
2533 for ( size_t n
= 0; n
< count
; n
++ )
2535 m_attrs
[n
]->DecRef();
2539 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2541 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2543 int n
= m_rowsOrCols
.Index(rowOrCol
);
2544 if ( n
!= wxNOT_FOUND
)
2546 attr
= m_attrs
[(size_t)n
];
2553 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2555 int i
= m_rowsOrCols
.Index(rowOrCol
);
2556 if ( i
== wxNOT_FOUND
)
2558 // add the attribute
2559 m_rowsOrCols
.Add(rowOrCol
);
2564 size_t n
= (size_t)i
;
2567 // change the attribute
2568 m_attrs
[n
]->DecRef();
2573 // remove this attribute
2574 m_attrs
[n
]->DecRef();
2575 m_rowsOrCols
.RemoveAt(n
);
2576 m_attrs
.RemoveAt(n
);
2581 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2583 size_t count
= m_attrs
.GetCount();
2584 for ( size_t n
= 0; n
< count
; n
++ )
2586 int & rowOrCol
= m_rowsOrCols
[n
];
2587 if ( (size_t)rowOrCol
>= pos
)
2589 if ( numRowsOrCols
> 0 )
2591 // If rows inserted, include row counter where necessary
2592 rowOrCol
+= numRowsOrCols
;
2594 else if ( numRowsOrCols
< 0)
2596 // If rows deleted, either decrement row counter (if row still exists)
2597 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2598 rowOrCol
+= numRowsOrCols
;
2601 m_rowsOrCols
.RemoveAt(n
);
2602 m_attrs
[n
]->DecRef();
2603 m_attrs
.RemoveAt(n
);
2612 // ----------------------------------------------------------------------------
2613 // wxGridCellAttrProvider
2614 // ----------------------------------------------------------------------------
2616 wxGridCellAttrProvider::wxGridCellAttrProvider()
2618 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2621 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2626 void wxGridCellAttrProvider::InitData()
2628 m_data
= new wxGridCellAttrProviderData
;
2631 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2632 wxGridCellAttr::wxAttrKind kind
) const
2634 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2639 case (wxGridCellAttr::Any
):
2640 //Get cached merge attributes.
2641 // Currenlty not used as no cache implemented as not mutiable
2642 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2645 //Basically implement old version.
2646 //Also check merge cache, so we don't have to re-merge every time..
2647 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2648 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2649 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2651 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2653 // Two or more are non NULL
2654 attr
= new wxGridCellAttr
;
2655 attr
->SetKind(wxGridCellAttr::Merged
);
2660 attr
->MergeWith(attrcell
);
2665 attr
->MergeWith(attrcol
);
2670 attr
->MergeWith(attrrow
);
2673 //store merge attr if cache implemented
2675 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2679 // one or none is non null return it or null.
2698 case (wxGridCellAttr::Cell
):
2699 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2702 case (wxGridCellAttr::Col
):
2703 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2706 case (wxGridCellAttr::Row
):
2707 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2712 // (wxGridCellAttr::Default):
2713 // (wxGridCellAttr::Merged):
2720 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2726 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2729 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2734 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2737 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2742 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2745 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2749 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2751 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2755 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2759 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2761 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2765 // ----------------------------------------------------------------------------
2766 // wxGridTypeRegistry
2767 // ----------------------------------------------------------------------------
2769 wxGridTypeRegistry::~wxGridTypeRegistry()
2771 size_t count
= m_typeinfo
.Count();
2772 for ( size_t i
= 0; i
< count
; i
++ )
2773 delete m_typeinfo
[i
];
2777 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2778 wxGridCellRenderer
* renderer
,
2779 wxGridCellEditor
* editor
)
2781 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2783 // is it already registered?
2784 int loc
= FindRegisteredDataType(typeName
);
2785 if ( loc
!= wxNOT_FOUND
)
2787 delete m_typeinfo
[loc
];
2788 m_typeinfo
[loc
] = info
;
2792 m_typeinfo
.Add(info
);
2796 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2798 size_t count
= m_typeinfo
.GetCount();
2799 for ( size_t i
= 0; i
< count
; i
++ )
2801 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2810 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2812 int index
= FindRegisteredDataType(typeName
);
2813 if ( index
== wxNOT_FOUND
)
2815 // check whether this is one of the standard ones, in which case
2816 // register it "on the fly"
2818 if ( typeName
== wxGRID_VALUE_STRING
)
2820 RegisterDataType(wxGRID_VALUE_STRING
,
2821 new wxGridCellStringRenderer
,
2822 new wxGridCellTextEditor
);
2825 #endif // wxUSE_TEXTCTRL
2827 if ( typeName
== wxGRID_VALUE_BOOL
)
2829 RegisterDataType(wxGRID_VALUE_BOOL
,
2830 new wxGridCellBoolRenderer
,
2831 new wxGridCellBoolEditor
);
2834 #endif // wxUSE_CHECKBOX
2836 if ( typeName
== wxGRID_VALUE_NUMBER
)
2838 RegisterDataType(wxGRID_VALUE_NUMBER
,
2839 new wxGridCellNumberRenderer
,
2840 new wxGridCellNumberEditor
);
2842 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2844 RegisterDataType(wxGRID_VALUE_FLOAT
,
2845 new wxGridCellFloatRenderer
,
2846 new wxGridCellFloatEditor
);
2849 #endif // wxUSE_TEXTCTRL
2851 if ( typeName
== wxGRID_VALUE_CHOICE
)
2853 RegisterDataType(wxGRID_VALUE_CHOICE
,
2854 new wxGridCellStringRenderer
,
2855 new wxGridCellChoiceEditor
);
2858 #endif // wxUSE_COMBOBOX
2863 // we get here only if just added the entry for this type, so return
2865 index
= m_typeinfo
.GetCount() - 1;
2871 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2873 int index
= FindDataType(typeName
);
2874 if ( index
== wxNOT_FOUND
)
2876 // the first part of the typename is the "real" type, anything after ':'
2877 // are the parameters for the renderer
2878 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2879 if ( index
== wxNOT_FOUND
)
2884 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2885 wxGridCellRenderer
*rendererOld
= renderer
;
2886 renderer
= renderer
->Clone();
2887 rendererOld
->DecRef();
2889 wxGridCellEditor
*editor
= GetEditor(index
);
2890 wxGridCellEditor
*editorOld
= editor
;
2891 editor
= editor
->Clone();
2892 editorOld
->DecRef();
2894 // do it even if there are no parameters to reset them to defaults
2895 wxString params
= typeName
.AfterFirst(_T(':'));
2896 renderer
->SetParameters(params
);
2897 editor
->SetParameters(params
);
2899 // register the new typename
2900 RegisterDataType(typeName
, renderer
, editor
);
2902 // we just registered it, it's the last one
2903 index
= m_typeinfo
.GetCount() - 1;
2909 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2911 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2917 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2919 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2925 // ----------------------------------------------------------------------------
2927 // ----------------------------------------------------------------------------
2929 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2932 wxGridTableBase::wxGridTableBase()
2934 m_view
= (wxGrid
*) NULL
;
2935 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2938 wxGridTableBase::~wxGridTableBase()
2940 delete m_attrProvider
;
2943 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2945 delete m_attrProvider
;
2946 m_attrProvider
= attrProvider
;
2949 bool wxGridTableBase::CanHaveAttributes()
2951 if ( ! GetAttrProvider() )
2953 // use the default attr provider by default
2954 SetAttrProvider(new wxGridCellAttrProvider
);
2959 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2961 if ( m_attrProvider
)
2962 return m_attrProvider
->GetAttr(row
, col
, kind
);
2964 return (wxGridCellAttr
*)NULL
;
2968 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2970 if ( m_attrProvider
)
2972 attr
->SetKind(wxGridCellAttr::Cell
);
2973 m_attrProvider
->SetAttr(attr
, row
, col
);
2977 // as we take ownership of the pointer and don't store it, we must
2983 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2985 if ( m_attrProvider
)
2987 attr
->SetKind(wxGridCellAttr::Row
);
2988 m_attrProvider
->SetRowAttr(attr
, row
);
2992 // as we take ownership of the pointer and don't store it, we must
2998 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3000 if ( m_attrProvider
)
3002 attr
->SetKind(wxGridCellAttr::Col
);
3003 m_attrProvider
->SetColAttr(attr
, col
);
3007 // as we take ownership of the pointer and don't store it, we must
3013 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3014 size_t WXUNUSED(numRows
) )
3016 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3021 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3023 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3028 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3029 size_t WXUNUSED(numRows
) )
3031 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3036 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3037 size_t WXUNUSED(numCols
) )
3039 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3044 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3046 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3051 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3052 size_t WXUNUSED(numCols
) )
3054 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3059 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3062 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3063 // how much it makes sense to us geeks.
3067 wxString
wxGridTableBase::GetColLabelValue( int col
)
3069 // default col labels are:
3070 // cols 0 to 25 : A-Z
3071 // cols 26 to 675 : AA-ZZ
3076 for ( n
= 1; ; n
++ )
3078 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3084 // reverse the string...
3086 for ( i
= 0; i
< n
; i
++ )
3094 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3096 return wxGRID_VALUE_STRING
;
3099 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3100 const wxString
& typeName
)
3102 return typeName
== wxGRID_VALUE_STRING
;
3105 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3107 return CanGetValueAs(row
, col
, typeName
);
3110 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3115 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3120 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3125 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3126 long WXUNUSED(value
) )
3130 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3131 double WXUNUSED(value
) )
3135 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3136 bool WXUNUSED(value
) )
3141 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3142 const wxString
& WXUNUSED(typeName
) )
3147 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3148 const wxString
& WXUNUSED(typeName
),
3149 void* WXUNUSED(value
) )
3153 //////////////////////////////////////////////////////////////////////
3155 // Message class for the grid table to send requests and notifications
3159 wxGridTableMessage::wxGridTableMessage()
3161 m_table
= (wxGridTableBase
*) NULL
;
3167 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3168 int commandInt1
, int commandInt2
)
3172 m_comInt1
= commandInt1
;
3173 m_comInt2
= commandInt2
;
3178 //////////////////////////////////////////////////////////////////////
3180 // A basic grid table for string data. An object of this class will
3181 // created by wxGrid if you don't specify an alternative table class.
3184 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3186 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3188 wxGridStringTable::wxGridStringTable()
3193 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3196 m_data
.Alloc( numRows
);
3199 sa
.Alloc( numCols
);
3200 sa
.Add( wxEmptyString
, numCols
);
3202 m_data
.Add( sa
, numRows
);
3205 wxGridStringTable::~wxGridStringTable()
3209 int wxGridStringTable::GetNumberRows()
3211 return m_data
.GetCount();
3214 int wxGridStringTable::GetNumberCols()
3216 if ( m_data
.GetCount() > 0 )
3217 return m_data
[0].GetCount();
3222 wxString
wxGridStringTable::GetValue( int row
, int col
)
3224 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3226 _T("invalid row or column index in wxGridStringTable") );
3228 return m_data
[row
][col
];
3231 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3233 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3234 _T("invalid row or column index in wxGridStringTable") );
3236 m_data
[row
][col
] = value
;
3239 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3241 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3243 _T("invalid row or column index in wxGridStringTable") );
3245 return (m_data
[row
][col
] == wxEmptyString
);
3248 void wxGridStringTable::Clear()
3251 int numRows
, numCols
;
3253 numRows
= m_data
.GetCount();
3256 numCols
= m_data
[0].GetCount();
3258 for ( row
= 0; row
< numRows
; row
++ )
3260 for ( col
= 0; col
< numCols
; col
++ )
3262 m_data
[row
][col
] = wxEmptyString
;
3269 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3271 size_t curNumRows
= m_data
.GetCount();
3272 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3273 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3275 if ( pos
>= curNumRows
)
3277 return AppendRows( numRows
);
3281 sa
.Alloc( curNumCols
);
3282 sa
.Add( wxEmptyString
, curNumCols
);
3283 m_data
.Insert( sa
, pos
, numRows
);
3286 wxGridTableMessage
msg( this,
3287 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3291 GetView()->ProcessTableMessage( msg
);
3297 bool wxGridStringTable::AppendRows( size_t numRows
)
3299 size_t curNumRows
= m_data
.GetCount();
3300 size_t curNumCols
= ( curNumRows
> 0
3301 ? m_data
[0].GetCount()
3302 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3305 if ( curNumCols
> 0 )
3307 sa
.Alloc( curNumCols
);
3308 sa
.Add( wxEmptyString
, curNumCols
);
3311 m_data
.Add( sa
, numRows
);
3315 wxGridTableMessage
msg( this,
3316 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3319 GetView()->ProcessTableMessage( msg
);
3325 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3327 size_t curNumRows
= m_data
.GetCount();
3329 if ( pos
>= curNumRows
)
3331 wxFAIL_MSG( wxString::Format
3333 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3335 (unsigned long)numRows
,
3336 (unsigned long)curNumRows
3342 if ( numRows
> curNumRows
- pos
)
3344 numRows
= curNumRows
- pos
;
3347 if ( numRows
>= curNumRows
)
3353 m_data
.RemoveAt( pos
, numRows
);
3358 wxGridTableMessage
msg( this,
3359 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3363 GetView()->ProcessTableMessage( msg
);
3369 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3373 size_t curNumRows
= m_data
.GetCount();
3374 size_t curNumCols
= ( curNumRows
> 0
3375 ? m_data
[0].GetCount()
3376 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3378 if ( pos
>= curNumCols
)
3380 return AppendCols( numCols
);
3383 for ( row
= 0; row
< curNumRows
; row
++ )
3385 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3387 m_data
[row
].Insert( wxEmptyString
, col
);
3393 wxGridTableMessage
msg( this,
3394 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3398 GetView()->ProcessTableMessage( msg
);
3404 bool wxGridStringTable::AppendCols( size_t numCols
)
3408 size_t curNumRows
= m_data
.GetCount();
3412 // TODO: something better than this ?
3414 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3419 for ( row
= 0; row
< curNumRows
; row
++ )
3421 m_data
[row
].Add( wxEmptyString
, numCols
);
3426 wxGridTableMessage
msg( this,
3427 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3430 GetView()->ProcessTableMessage( msg
);
3436 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3440 size_t curNumRows
= m_data
.GetCount();
3441 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3442 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3444 if ( pos
>= curNumCols
)
3446 wxFAIL_MSG( wxString::Format
3448 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3450 (unsigned long)numCols
,
3451 (unsigned long)curNumCols
3456 if ( numCols
> curNumCols
- pos
)
3458 numCols
= curNumCols
- pos
;
3461 for ( row
= 0; row
< curNumRows
; row
++ )
3463 if ( numCols
>= curNumCols
)
3465 m_data
[row
].Clear();
3469 m_data
[row
].RemoveAt( pos
, numCols
);
3475 wxGridTableMessage
msg( this,
3476 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3480 GetView()->ProcessTableMessage( msg
);
3486 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3488 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3490 // using default label
3492 return wxGridTableBase::GetRowLabelValue( row
);
3496 return m_rowLabels
[ row
];
3500 wxString
wxGridStringTable::GetColLabelValue( int col
)
3502 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3504 // using default label
3506 return wxGridTableBase::GetColLabelValue( col
);
3510 return m_colLabels
[ col
];
3514 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3516 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3518 int n
= m_rowLabels
.GetCount();
3520 for ( i
= n
; i
<= row
; i
++ )
3522 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3526 m_rowLabels
[row
] = value
;
3529 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3531 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3533 int n
= m_colLabels
.GetCount();
3535 for ( i
= n
; i
<= col
; i
++ )
3537 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3541 m_colLabels
[col
] = value
;
3545 //////////////////////////////////////////////////////////////////////
3546 //////////////////////////////////////////////////////////////////////
3548 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3550 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3551 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3552 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3553 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3554 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3555 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3556 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3559 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3561 const wxPoint
&pos
, const wxSize
&size
)
3562 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3567 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3571 // NO - don't do this because it will set both the x and y origin
3572 // coords to match the parent scrolled window and we just want to
3573 // set the y coord - MB
3575 // m_owner->PrepareDC( dc );
3578 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3579 dc
.SetDeviceOrigin( 0, -y
);
3581 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3582 m_owner
->DrawRowLabels( dc
, rows
);
3585 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3587 m_owner
->ProcessRowLabelMouseEvent( event
);
3590 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3592 m_owner
->GetEventHandler()->ProcessEvent(event
);
3595 // This seems to be required for wxMotif otherwise the mouse
3596 // cursor must be in the cell edit control to get key events
3598 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3600 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3603 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3605 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3608 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3610 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3614 //////////////////////////////////////////////////////////////////////
3616 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3618 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3619 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3620 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3621 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3622 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3623 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3624 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3627 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3629 const wxPoint
&pos
, const wxSize
&size
)
3630 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3635 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3639 // NO - don't do this because it will set both the x and y origin
3640 // coords to match the parent scrolled window and we just want to
3641 // set the x coord - MB
3643 // m_owner->PrepareDC( dc );
3646 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3647 dc
.SetDeviceOrigin( -x
, 0 );
3649 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3650 m_owner
->DrawColLabels( dc
, cols
);
3653 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3655 m_owner
->ProcessColLabelMouseEvent( event
);
3658 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3660 m_owner
->GetEventHandler()->ProcessEvent(event
);
3663 // This seems to be required for wxMotif otherwise the mouse
3664 // cursor must be in the cell edit control to get key events
3666 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3668 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3672 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3674 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3678 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3680 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3685 //////////////////////////////////////////////////////////////////////
3687 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3689 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3690 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3691 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3692 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3693 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3694 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3695 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3698 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3700 const wxPoint
&pos
, const wxSize
&size
)
3701 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3706 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3710 int client_height
= 0;
3711 int client_width
= 0;
3712 GetClientSize( &client_width
, &client_height
);
3714 // VZ: any reason for this ifdef? (FIXME)
3719 rect
.SetWidth( client_width
- 2 );
3720 rect
.SetHeight( client_height
- 2 );
3722 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3724 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3725 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3726 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3727 dc
.DrawLine( 0, 0, client_width
, 0 );
3728 dc
.DrawLine( 0, 0, 0, client_height
);
3730 dc
.SetPen( *wxWHITE_PEN
);
3731 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3732 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3733 #endif // __WXGTK__/!__WXGTK__
3736 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3738 m_owner
->ProcessCornerLabelMouseEvent( event
);
3741 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3743 m_owner
->GetEventHandler()->ProcessEvent(event
);
3746 // This seems to be required for wxMotif otherwise the mouse
3747 // cursor must be in the cell edit control to get key events
3749 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3751 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3754 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3756 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3759 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3761 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3765 //////////////////////////////////////////////////////////////////////
3767 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3769 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3770 EVT_PAINT( wxGridWindow::OnPaint
)
3771 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3772 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3773 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3774 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3775 EVT_CHAR ( wxGridWindow::OnChar
)
3776 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3777 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3778 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3781 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3782 wxGridRowLabelWindow
*rowLblWin
,
3783 wxGridColLabelWindow
*colLblWin
,
3786 const wxSize
&size
)
3787 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3788 wxT("grid window") )
3791 m_rowLabelWin
= rowLblWin
;
3792 m_colLabelWin
= colLblWin
;
3795 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3797 wxPaintDC
dc( this );
3798 m_owner
->PrepareDC( dc
);
3799 wxRegion reg
= GetUpdateRegion();
3800 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3801 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3802 #if WXGRID_DRAW_LINES
3803 m_owner
->DrawAllGridLines( dc
, reg
);
3805 m_owner
->DrawGridSpace( dc
);
3806 m_owner
->DrawHighlight( dc
, DirtyCells
);
3809 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3811 wxWindow::ScrollWindow( dx
, dy
, rect
);
3812 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3813 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3816 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3818 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3821 m_owner
->ProcessGridCellMouseEvent( event
);
3824 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3826 m_owner
->GetEventHandler()->ProcessEvent(event
);
3829 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3830 // cursor must be in the cell edit control to get key events
3832 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3834 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3838 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3840 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3844 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3846 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3850 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3854 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3856 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3860 //////////////////////////////////////////////////////////////////////
3862 // Internal Helper function for computing row or column from some
3863 // (unscrolled) coordinate value, using either
3864 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3865 // of m_rowBottoms/m_ColRights to speed up the search!
3867 // Internal helper macros for simpler use of that function
3869 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3870 const wxArrayInt
& BorderArray
, int nMax
,
3873 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3874 m_minAcceptableColWidth, \
3875 m_colRights, m_numCols, true)
3876 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3877 m_minAcceptableRowHeight, \
3878 m_rowBottoms, m_numRows, true)
3879 /////////////////////////////////////////////////////////////////////
3881 #if wxUSE_EXTENDED_RTTI
3882 WX_DEFINE_FLAGS( wxGridStyle
)
3884 wxBEGIN_FLAGS( wxGridStyle
)
3885 // new style border flags, we put them first to
3886 // use them for streaming out
3887 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3888 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3889 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3890 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3891 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3892 wxFLAGS_MEMBER(wxBORDER_NONE
)
3894 // old style border flags
3895 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3896 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3897 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3898 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3899 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3900 wxFLAGS_MEMBER(wxBORDER
)
3902 // standard window styles
3903 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3904 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3905 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3906 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3907 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3908 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3909 wxFLAGS_MEMBER(wxVSCROLL
)
3910 wxFLAGS_MEMBER(wxHSCROLL
)
3912 wxEND_FLAGS( wxGridStyle
)
3914 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3916 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3917 wxHIDE_PROPERTY( Children
)
3918 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3919 wxEND_PROPERTIES_TABLE()
3921 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3922 wxEND_HANDLERS_TABLE()
3924 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3927 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3930 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3933 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3934 EVT_PAINT( wxGrid::OnPaint
)
3935 EVT_SIZE( wxGrid::OnSize
)
3936 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3937 EVT_KEY_UP( wxGrid::OnKeyUp
)
3938 EVT_CHAR ( wxGrid::OnChar
)
3939 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3944 // in order to make sure that a size event is not
3945 // trigerred in a unfinished state
3946 m_cornerLabelWin
= NULL
;
3947 m_rowLabelWin
= NULL
;
3948 m_colLabelWin
= NULL
;
3952 wxGrid::wxGrid( wxWindow
*parent
,
3957 const wxString
& name
)
3958 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3959 m_colMinWidths(GRID_HASH_SIZE
),
3960 m_rowMinHeights(GRID_HASH_SIZE
)
3963 SetBestFittingSize(size
);
3966 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3967 const wxPoint
& pos
, const wxSize
& size
,
3968 long style
, const wxString
& name
)
3970 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3971 style
| wxWANTS_CHARS
, name
))
3974 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3975 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3978 SetBestFittingSize(size
);
3986 // Must do this or ~wxScrollHelper will pop the wrong event handler
3987 SetTargetWindow(this);
3989 wxSafeDecRef(m_defaultCellAttr
);
3991 #ifdef DEBUG_ATTR_CACHE
3992 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3993 wxPrintf(_T("wxGrid attribute cache statistics: "
3994 "total: %u, hits: %u (%u%%)\n"),
3995 total
, gs_nAttrCacheHits
,
3996 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4002 delete m_typeRegistry
;
4008 // ----- internal init and update functions
4011 // NOTE: If using the default visual attributes works everywhere then this can
4012 // be removed as well as the #else cases below.
4013 #define _USE_VISATTR 0
4016 #include "wx/listbox.h"
4019 void wxGrid::Create()
4021 m_created
= false; // set to true by CreateGrid
4023 m_table
= (wxGridTableBase
*) NULL
;
4026 m_cellEditCtrlEnabled
= false;
4028 m_defaultCellAttr
= new wxGridCellAttr();
4030 // Set default cell attributes
4031 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4032 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4033 m_defaultCellAttr
->SetFont(GetFont());
4034 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4035 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4036 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4039 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4040 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4042 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4043 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4046 m_defaultCellAttr
->SetTextColour(
4047 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4048 m_defaultCellAttr
->SetBackgroundColour(
4049 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4054 m_currentCellCoords
= wxGridNoCellCoords
;
4056 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4057 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4059 // create the type registry
4060 m_typeRegistry
= new wxGridTypeRegistry
;
4063 // subwindow components that make up the wxGrid
4064 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4069 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4074 m_colLabelWin
= new wxGridColLabelWindow( this,
4079 m_gridWin
= new wxGridWindow( this,
4086 SetTargetWindow( m_gridWin
);
4089 wxColour gfg
= gva
.colFg
;
4090 wxColour gbg
= gva
.colBg
;
4091 wxColour lfg
= lva
.colFg
;
4092 wxColour lbg
= lva
.colBg
;
4094 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4095 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4096 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4097 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4099 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4100 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4101 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4102 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4103 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4104 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4106 m_gridWin
->SetOwnForegroundColour(gfg
);
4107 m_gridWin
->SetOwnBackgroundColour(gbg
);
4113 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4114 wxGrid::wxGridSelectionModes selmode
)
4116 wxCHECK_MSG( !m_created
,
4118 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4120 m_numRows
= numRows
;
4121 m_numCols
= numCols
;
4123 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4124 m_table
->SetView( this );
4126 m_selection
= new wxGridSelection( this, selmode
);
4135 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4137 wxCHECK_RET( m_created
,
4138 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4140 m_selection
->SetSelectionMode( selmode
);
4143 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4145 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4146 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4148 return m_selection
->GetSelectionMode();
4151 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4152 wxGrid::wxGridSelectionModes selmode
)
4156 // stop all processing
4161 wxGridTableBase
*t
=m_table
;
4174 m_numRows
= table
->GetNumberRows();
4175 m_numCols
= table
->GetNumberCols();
4178 m_table
->SetView( this );
4181 m_selection
= new wxGridSelection( this, selmode
);
4194 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4195 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4197 if ( m_rowLabelWin
)
4199 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4203 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4206 m_labelTextColour
= wxColour( _T("BLACK") );
4209 m_attrCache
.row
= -1;
4210 m_attrCache
.col
= -1;
4211 m_attrCache
.attr
= NULL
;
4213 // TODO: something better than this ?
4215 m_labelFont
= this->GetFont();
4216 m_labelFont
.SetWeight( wxBOLD
);
4218 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4219 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4221 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4222 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4223 m_colLabelTextOrientation
= wxHORIZONTAL
;
4225 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4226 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4228 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4229 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4231 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4232 m_defaultRowHeight
+= 8;
4234 m_defaultRowHeight
+= 4;
4237 m_gridLineColour
= wxColour( 192,192,192 );
4238 m_gridLinesEnabled
= true;
4239 m_cellHighlightColour
= *wxBLACK
;
4240 m_cellHighlightPenWidth
= 2;
4241 m_cellHighlightROPenWidth
= 1;
4243 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4244 m_winCapture
= (wxWindow
*)NULL
;
4245 m_canDragRowSize
= true;
4246 m_canDragColSize
= true;
4247 m_canDragGridSize
= true;
4248 m_canDragCell
= false;
4250 m_dragRowOrCol
= -1;
4251 m_isDragging
= false;
4252 m_startDragPos
= wxDefaultPosition
;
4254 m_waitForSlowClick
= false;
4256 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4257 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4259 m_currentCellCoords
= wxGridNoCellCoords
;
4261 m_selectingTopLeft
= wxGridNoCellCoords
;
4262 m_selectingBottomRight
= wxGridNoCellCoords
;
4263 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4264 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4266 m_editable
= true; // default for whole grid
4268 m_inOnKeyDown
= false;
4274 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4275 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4278 // ----------------------------------------------------------------------------
4279 // the idea is to call these functions only when necessary because they create
4280 // quite big arrays which eat memory mostly unnecessary - in particular, if
4281 // default widths/heights are used for all rows/columns, we may not use these
4284 // with some extra code, it should be possible to only store the
4285 // widths/heights different from default ones but this will be done later...
4286 // ----------------------------------------------------------------------------
4288 void wxGrid::InitRowHeights()
4290 m_rowHeights
.Empty();
4291 m_rowBottoms
.Empty();
4293 m_rowHeights
.Alloc( m_numRows
);
4294 m_rowBottoms
.Alloc( m_numRows
);
4298 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4300 for ( int i
= 0; i
< m_numRows
; i
++ )
4302 rowBottom
+= m_defaultRowHeight
;
4303 m_rowBottoms
.Add( rowBottom
);
4307 void wxGrid::InitColWidths()
4309 m_colWidths
.Empty();
4310 m_colRights
.Empty();
4312 m_colWidths
.Alloc( m_numCols
);
4313 m_colRights
.Alloc( m_numCols
);
4316 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4318 for ( int i
= 0; i
< m_numCols
; i
++ )
4320 colRight
+= m_defaultColWidth
;
4321 m_colRights
.Add( colRight
);
4325 int wxGrid::GetColWidth(int col
) const
4327 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4330 int wxGrid::GetColLeft(int col
) const
4332 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4333 : m_colRights
[col
] - m_colWidths
[col
];
4336 int wxGrid::GetColRight(int col
) const
4338 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4342 int wxGrid::GetRowHeight(int row
) const
4344 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4347 int wxGrid::GetRowTop(int row
) const
4349 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4350 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4353 int wxGrid::GetRowBottom(int row
) const
4355 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4356 : m_rowBottoms
[row
];
4359 void wxGrid::CalcDimensions()
4362 GetClientSize( &cw
, &ch
);
4364 if ( m_rowLabelWin
->IsShown() )
4365 cw
-= m_rowLabelWidth
;
4366 if ( m_colLabelWin
->IsShown() )
4367 ch
-= m_colLabelHeight
;
4370 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4371 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4373 // take into account editor if shown
4374 if ( IsCellEditControlShown() )
4377 int r
= m_currentCellCoords
.GetRow();
4378 int c
= m_currentCellCoords
.GetCol();
4379 int x
= GetColLeft(c
);
4380 int y
= GetRowTop(r
);
4382 // how big is the editor
4383 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4384 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4385 editor
->GetControl()->GetSize(&w2
, &h2
);
4396 // preserve (more or less) the previous position
4398 GetViewStart( &x
, &y
);
4400 // ensure the position is valid for the new scroll ranges
4402 x
= wxMax( w
- 1, 0 );
4404 y
= wxMax( h
- 1, 0 );
4406 // do set scrollbar parameters
4407 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4408 GetScrollX(w
), GetScrollY(h
), x
, y
,
4409 GetBatchCount() != 0);
4411 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4412 // still must reposition the children
4417 void wxGrid::CalcWindowSizes()
4419 // escape if the window is has not been fully created yet
4421 if ( m_cornerLabelWin
== NULL
)
4425 GetClientSize( &cw
, &ch
);
4427 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4428 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4430 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4431 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4433 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4434 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4436 if ( m_gridWin
&& m_gridWin
->IsShown() )
4437 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4441 // this is called when the grid table sends a message to say that it
4442 // has been redimensioned
4444 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4447 bool result
= false;
4449 // Clear the attribute cache as the attribute might refer to a different
4450 // cell than stored in the cache after adding/removing rows/columns.
4452 // By the same reasoning, the editor should be dismissed if columns are
4453 // added or removed. And for consistency, it should IMHO always be
4454 // removed, not only if the cell "underneath" it actually changes.
4455 // For now, I intentionally do not save the editor's content as the
4456 // cell it might want to save that stuff to might no longer exist.
4457 HideCellEditControl();
4459 // if we were using the default widths/heights so far, we must change them
4461 if ( m_colWidths
.IsEmpty() )
4466 if ( m_rowHeights
.IsEmpty() )
4472 switch ( msg
.GetId() )
4474 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4476 size_t pos
= msg
.GetCommandInt();
4477 int numRows
= msg
.GetCommandInt2();
4479 m_numRows
+= numRows
;
4481 if ( !m_rowHeights
.IsEmpty() )
4483 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4484 m_rowBottoms
.Insert( 0, pos
, numRows
);
4487 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4489 for ( i
= pos
; i
< m_numRows
; i
++ )
4491 bottom
+= m_rowHeights
[i
];
4492 m_rowBottoms
[i
] = bottom
;
4495 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4497 // if we have just inserted cols into an empty grid the current
4498 // cell will be undefined...
4500 SetCurrentCell( 0, 0 );
4504 m_selection
->UpdateRows( pos
, numRows
);
4505 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4507 attrProvider
->UpdateAttrRows( pos
, numRows
);
4509 if ( !GetBatchCount() )
4512 m_rowLabelWin
->Refresh();
4518 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4520 int numRows
= msg
.GetCommandInt();
4521 int oldNumRows
= m_numRows
;
4522 m_numRows
+= numRows
;
4524 if ( !m_rowHeights
.IsEmpty() )
4526 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4527 m_rowBottoms
.Add( 0, numRows
);
4530 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4532 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4534 bottom
+= m_rowHeights
[i
];
4535 m_rowBottoms
[i
] = bottom
;
4538 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4540 // if we have just inserted cols into an empty grid the current
4541 // cell will be undefined...
4543 SetCurrentCell( 0, 0 );
4545 if ( !GetBatchCount() )
4548 m_rowLabelWin
->Refresh();
4554 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4556 size_t pos
= msg
.GetCommandInt();
4557 int numRows
= msg
.GetCommandInt2();
4558 m_numRows
-= numRows
;
4560 if ( !m_rowHeights
.IsEmpty() )
4562 m_rowHeights
.RemoveAt( pos
, numRows
);
4563 m_rowBottoms
.RemoveAt( pos
, numRows
);
4566 for ( i
= 0; i
< m_numRows
; i
++ )
4568 h
+= m_rowHeights
[i
];
4569 m_rowBottoms
[i
] = h
;
4574 m_currentCellCoords
= wxGridNoCellCoords
;
4578 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4579 m_currentCellCoords
.Set( 0, 0 );
4583 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4584 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4587 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4588 // ifdef'd out following patch from Paul Gammans
4590 // No need to touch column attributes, unless we
4591 // removed _all_ rows, in this case, we remove
4592 // all column attributes.
4593 // I hate to do this here, but the
4594 // needed data is not available inside UpdateAttrRows.
4595 if ( !GetNumberRows() )
4596 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4599 if ( !GetBatchCount() )
4602 m_rowLabelWin
->Refresh();
4608 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4610 size_t pos
= msg
.GetCommandInt();
4611 int numCols
= msg
.GetCommandInt2();
4612 m_numCols
+= numCols
;
4614 if ( !m_colWidths
.IsEmpty() )
4616 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4617 m_colRights
.Insert( 0, pos
, numCols
);
4620 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4622 for ( i
= pos
; i
< m_numCols
; i
++ )
4624 right
+= m_colWidths
[i
];
4625 m_colRights
[i
] = right
;
4628 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4630 // if we have just inserted cols into an empty grid the current
4631 // cell will be undefined...
4633 SetCurrentCell( 0, 0 );
4637 m_selection
->UpdateCols( pos
, numCols
);
4638 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4640 attrProvider
->UpdateAttrCols( pos
, numCols
);
4641 if ( !GetBatchCount() )
4644 m_colLabelWin
->Refresh();
4651 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4653 int numCols
= msg
.GetCommandInt();
4654 int oldNumCols
= m_numCols
;
4655 m_numCols
+= numCols
;
4656 if ( !m_colWidths
.IsEmpty() )
4658 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4659 m_colRights
.Add( 0, numCols
);
4662 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4664 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4666 right
+= m_colWidths
[i
];
4667 m_colRights
[i
] = right
;
4670 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4672 // if we have just inserted cols into an empty grid the current
4673 // cell will be undefined...
4675 SetCurrentCell( 0, 0 );
4677 if ( !GetBatchCount() )
4680 m_colLabelWin
->Refresh();
4686 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4688 size_t pos
= msg
.GetCommandInt();
4689 int numCols
= msg
.GetCommandInt2();
4690 m_numCols
-= numCols
;
4692 if ( !m_colWidths
.IsEmpty() )
4694 m_colWidths
.RemoveAt( pos
, numCols
);
4695 m_colRights
.RemoveAt( pos
, numCols
);
4698 for ( i
= 0; i
< m_numCols
; i
++ )
4700 w
+= m_colWidths
[i
];
4706 m_currentCellCoords
= wxGridNoCellCoords
;
4710 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4711 m_currentCellCoords
.Set( 0, 0 );
4715 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4716 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4719 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4720 // ifdef'd out following patch from Paul Gammans
4722 // No need to touch row attributes, unless we
4723 // removed _all_ columns, in this case, we remove
4724 // all row attributes.
4725 // I hate to do this here, but the
4726 // needed data is not available inside UpdateAttrCols.
4727 if ( !GetNumberCols() )
4728 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4731 if ( !GetBatchCount() )
4734 m_colLabelWin
->Refresh();
4741 if (result
&& !GetBatchCount() )
4742 m_gridWin
->Refresh();
4747 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4749 wxRegionIterator
iter( reg
);
4752 wxArrayInt rowlabels
;
4759 // TODO: remove this when we can...
4760 // There is a bug in wxMotif that gives garbage update
4761 // rectangles if you jump-scroll a long way by clicking the
4762 // scrollbar with middle button. This is a work-around
4764 #if defined(__WXMOTIF__)
4766 m_gridWin
->GetClientSize( &cw
, &ch
);
4767 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4768 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4771 // logical bounds of update region
4774 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4775 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4777 // find the row labels within these bounds
4780 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4782 if ( GetRowBottom(row
) < top
)
4785 if ( GetRowTop(row
) > bottom
)
4788 rowlabels
.Add( row
);
4798 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4800 wxRegionIterator
iter( reg
);
4803 wxArrayInt colLabels
;
4810 // TODO: remove this when we can...
4811 // There is a bug in wxMotif that gives garbage update
4812 // rectangles if you jump-scroll a long way by clicking the
4813 // scrollbar with middle button. This is a work-around
4815 #if defined(__WXMOTIF__)
4817 m_gridWin
->GetClientSize( &cw
, &ch
);
4818 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4819 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4822 // logical bounds of update region
4825 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4826 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4828 // find the cells within these bounds
4831 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4833 if ( GetColRight(col
) < left
)
4836 if ( GetColLeft(col
) > right
)
4839 colLabels
.Add( col
);
4848 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4850 wxRegionIterator
iter( reg
);
4853 wxGridCellCoordsArray cellsExposed
;
4855 int left
, top
, right
, bottom
;
4860 // TODO: remove this when we can...
4861 // There is a bug in wxMotif that gives garbage update
4862 // rectangles if you jump-scroll a long way by clicking the
4863 // scrollbar with middle button. This is a work-around
4865 #if defined(__WXMOTIF__)
4867 m_gridWin
->GetClientSize( &cw
, &ch
);
4868 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4869 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4870 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4871 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4874 // logical bounds of update region
4876 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4877 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4879 // find the cells within these bounds
4882 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4884 if ( GetRowBottom(row
) <= top
)
4887 if ( GetRowTop(row
) > bottom
)
4890 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4892 if ( GetColRight(col
) <= left
)
4895 if ( GetColLeft(col
) > right
)
4898 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4905 return cellsExposed
;
4909 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4912 wxPoint
pos( event
.GetPosition() );
4913 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4915 if ( event
.Dragging() )
4919 m_isDragging
= true;
4920 m_rowLabelWin
->CaptureMouse();
4923 if ( event
.LeftIsDown() )
4925 switch( m_cursorMode
)
4927 case WXGRID_CURSOR_RESIZE_ROW
:
4929 int cw
, ch
, left
, dummy
;
4930 m_gridWin
->GetClientSize( &cw
, &ch
);
4931 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4933 wxClientDC
dc( m_gridWin
);
4936 GetRowTop(m_dragRowOrCol
) +
4937 GetRowMinimalHeight(m_dragRowOrCol
) );
4938 dc
.SetLogicalFunction(wxINVERT
);
4939 if ( m_dragLastPos
>= 0 )
4941 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4943 dc
.DrawLine( left
, y
, left
+cw
, y
);
4948 case WXGRID_CURSOR_SELECT_ROW
:
4950 if ( (row
= YToRow( y
)) >= 0 )
4954 m_selection
->SelectRow( row
,
4955 event
.ControlDown(),
4964 // default label to suppress warnings about "enumeration value
4965 // 'xxx' not handled in switch
4973 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4978 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4979 m_isDragging
= false;
4982 // ------------ Entering or leaving the window
4984 if ( event
.Entering() || event
.Leaving() )
4986 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4990 // ------------ Left button pressed
4992 else if ( event
.LeftDown() )
4994 // don't send a label click event for a hit on the
4995 // edge of the row label - this is probably the user
4996 // wanting to resize the row
4998 if ( YToEdgeOfRow(y
) < 0 )
5002 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5004 if ( !event
.ShiftDown() && !event
.ControlDown() )
5008 if ( event
.ShiftDown() )
5010 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5013 GetNumberCols() - 1,
5014 event
.ControlDown(),
5021 m_selection
->SelectRow( row
,
5022 event
.ControlDown(),
5029 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5034 // starting to drag-resize a row
5036 if ( CanDragRowSize() )
5037 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5042 // ------------ Left double click
5044 else if (event
.LeftDClick() )
5046 row
= YToEdgeOfRow(y
);
5051 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5053 // no default action at the moment
5058 // adjust row height depending on label text
5059 AutoSizeRowLabelSize( row
);
5061 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5067 // ------------ Left button released
5069 else if ( event
.LeftUp() )
5071 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5073 DoEndDragResizeRow();
5075 // Note: we are ending the event *after* doing
5076 // default processing in this case
5078 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5081 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5086 // ------------ Right button down
5088 else if ( event
.RightDown() )
5092 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5094 // no default action at the moment
5099 // ------------ Right double click
5101 else if ( event
.RightDClick() )
5105 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5107 // no default action at the moment
5112 // ------------ No buttons down and mouse moving
5114 else if ( event
.Moving() )
5116 m_dragRowOrCol
= YToEdgeOfRow( y
);
5117 if ( m_dragRowOrCol
>= 0 )
5119 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5121 // don't capture the mouse yet
5122 if ( CanDragRowSize() )
5123 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5126 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5128 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5134 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5137 wxPoint
pos( event
.GetPosition() );
5138 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5140 if ( event
.Dragging() )
5144 m_isDragging
= true;
5145 m_colLabelWin
->CaptureMouse();
5148 if ( event
.LeftIsDown() )
5150 switch( m_cursorMode
)
5152 case WXGRID_CURSOR_RESIZE_COL
:
5154 int cw
, ch
, dummy
, top
;
5155 m_gridWin
->GetClientSize( &cw
, &ch
);
5156 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5158 wxClientDC
dc( m_gridWin
);
5161 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5162 GetColMinimalWidth(m_dragRowOrCol
));
5163 dc
.SetLogicalFunction(wxINVERT
);
5164 if ( m_dragLastPos
>= 0 )
5166 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5168 dc
.DrawLine( x
, top
, x
, top
+ch
);
5173 case WXGRID_CURSOR_SELECT_COL
:
5175 if ( (col
= XToCol( x
)) >= 0 )
5179 m_selection
->SelectCol( col
,
5180 event
.ControlDown(),
5189 // default label to suppress warnings about "enumeration value
5190 // 'xxx' not handled in switch
5198 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5203 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5204 m_isDragging
= false;
5207 // ------------ Entering or leaving the window
5209 if ( event
.Entering() || event
.Leaving() )
5211 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5215 // ------------ Left button pressed
5217 else if ( event
.LeftDown() )
5219 // don't send a label click event for a hit on the
5220 // edge of the col label - this is probably the user
5221 // wanting to resize the col
5223 if ( XToEdgeOfCol(x
) < 0 )
5227 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5229 if ( !event
.ShiftDown() && !event
.ControlDown() )
5233 if ( event
.ShiftDown() )
5235 m_selection
->SelectBlock( 0,
5236 m_currentCellCoords
.GetCol(),
5237 GetNumberRows() - 1, col
,
5238 event
.ControlDown(),
5245 m_selection
->SelectCol( col
,
5246 event
.ControlDown(),
5253 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5258 // starting to drag-resize a col
5260 if ( CanDragColSize() )
5261 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5266 // ------------ Left double click
5268 if ( event
.LeftDClick() )
5270 col
= XToEdgeOfCol(x
);
5275 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5277 // no default action at the moment
5282 // adjust column width depending on label text
5283 AutoSizeColLabelSize( col
);
5285 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5291 // ------------ Left button released
5293 else if ( event
.LeftUp() )
5295 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5297 DoEndDragResizeCol();
5299 // Note: we are ending the event *after* doing
5300 // default processing in this case
5302 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5305 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5310 // ------------ Right button down
5312 else if ( event
.RightDown() )
5316 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5318 // no default action at the moment
5323 // ------------ Right double click
5325 else if ( event
.RightDClick() )
5329 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5331 // no default action at the moment
5336 // ------------ No buttons down and mouse moving
5338 else if ( event
.Moving() )
5340 m_dragRowOrCol
= XToEdgeOfCol( x
);
5341 if ( m_dragRowOrCol
>= 0 )
5343 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5345 // don't capture the cursor yet
5346 if ( CanDragColSize() )
5347 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5350 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5352 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5358 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5360 if ( event
.LeftDown() )
5362 // indicate corner label by having both row and
5365 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5371 else if ( event
.LeftDClick() )
5373 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5376 else if ( event
.RightDown() )
5378 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5380 // no default action at the moment
5384 else if ( event
.RightDClick() )
5386 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5388 // no default action at the moment
5393 void wxGrid::ChangeCursorMode(CursorMode mode
,
5398 static const wxChar
*cursorModes
[] =
5407 wxLogTrace(_T("grid"),
5408 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5409 win
== m_colLabelWin
? _T("colLabelWin")
5410 : win
? _T("rowLabelWin")
5412 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5413 #endif // __WXDEBUG__
5415 if ( mode
== m_cursorMode
&&
5416 win
== m_winCapture
&&
5417 captureMouse
== (m_winCapture
!= NULL
))
5422 // by default use the grid itself
5428 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5429 m_winCapture
= (wxWindow
*)NULL
;
5432 m_cursorMode
= mode
;
5434 switch ( m_cursorMode
)
5436 case WXGRID_CURSOR_RESIZE_ROW
:
5437 win
->SetCursor( m_rowResizeCursor
);
5440 case WXGRID_CURSOR_RESIZE_COL
:
5441 win
->SetCursor( m_colResizeCursor
);
5445 win
->SetCursor( *wxSTANDARD_CURSOR
);
5448 // we need to capture mouse when resizing
5449 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5450 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5452 if ( captureMouse
&& resize
)
5454 win
->CaptureMouse();
5459 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5462 wxPoint
pos( event
.GetPosition() );
5463 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5465 wxGridCellCoords coords
;
5466 XYToCell( x
, y
, coords
);
5468 int cell_rows
, cell_cols
;
5469 bool isFirstDrag
= !m_isDragging
;
5470 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5471 if ((cell_rows
< 0) || (cell_cols
< 0))
5473 coords
.SetRow(coords
.GetRow() + cell_rows
);
5474 coords
.SetCol(coords
.GetCol() + cell_cols
);
5477 if ( event
.Dragging() )
5479 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5481 // Don't start doing anything until the mouse has been drug at
5482 // least 3 pixels in any direction...
5485 if (m_startDragPos
== wxDefaultPosition
)
5487 m_startDragPos
= pos
;
5490 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5494 m_isDragging
= true;
5495 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5497 // Hide the edit control, so it
5498 // won't interfere with drag-shrinking.
5499 if ( IsCellEditControlShown() )
5501 HideCellEditControl();
5502 SaveEditControlValue();
5505 // Have we captured the mouse yet?
5508 m_winCapture
= m_gridWin
;
5509 m_winCapture
->CaptureMouse();
5512 if ( coords
!= wxGridNoCellCoords
)
5514 if ( event
.ControlDown() )
5516 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5517 m_selectingKeyboard
= coords
;
5518 HighlightBlock ( m_selectingKeyboard
, coords
);
5520 else if ( CanDragCell() )
5524 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5525 m_selectingKeyboard
= coords
;
5527 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5535 if ( !IsSelection() )
5537 HighlightBlock( coords
, coords
);
5541 HighlightBlock( m_currentCellCoords
, coords
);
5545 if (! IsVisible(coords
))
5547 MakeCellVisible(coords
);
5548 // TODO: need to introduce a delay or something here. The
5549 // scrolling is way to fast, at least on MSW - also on GTK.
5553 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5555 int cw
, ch
, left
, dummy
;
5556 m_gridWin
->GetClientSize( &cw
, &ch
);
5557 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5559 wxClientDC
dc( m_gridWin
);
5561 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5562 GetRowMinimalHeight(m_dragRowOrCol
) );
5563 dc
.SetLogicalFunction(wxINVERT
);
5564 if ( m_dragLastPos
>= 0 )
5566 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5568 dc
.DrawLine( left
, y
, left
+cw
, y
);
5571 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5573 int cw
, ch
, dummy
, top
;
5574 m_gridWin
->GetClientSize( &cw
, &ch
);
5575 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5577 wxClientDC
dc( m_gridWin
);
5579 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5580 GetColMinimalWidth(m_dragRowOrCol
) );
5581 dc
.SetLogicalFunction(wxINVERT
);
5582 if ( m_dragLastPos
>= 0 )
5584 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5586 dc
.DrawLine( x
, top
, x
, top
+ch
);
5593 m_isDragging
= false;
5594 m_startDragPos
= wxDefaultPosition
;
5596 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5597 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5600 if ( event
.Entering() || event
.Leaving() )
5602 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5603 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5608 // ------------ Left button pressed
5610 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5612 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5617 if ( !event
.ControlDown() )
5619 if ( event
.ShiftDown() )
5623 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5624 m_currentCellCoords
.GetCol(),
5627 event
.ControlDown(),
5633 else if ( XToEdgeOfCol(x
) < 0 &&
5634 YToEdgeOfRow(y
) < 0 )
5636 DisableCellEditControl();
5637 MakeCellVisible( coords
);
5639 if ( event
.ControlDown() )
5643 m_selection
->ToggleCellSelection( coords
.GetRow(),
5645 event
.ControlDown(),
5650 m_selectingTopLeft
= wxGridNoCellCoords
;
5651 m_selectingBottomRight
= wxGridNoCellCoords
;
5652 m_selectingKeyboard
= coords
;
5656 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5657 SetCurrentCell( coords
);
5660 if ( m_selection
->GetSelectionMode() !=
5661 wxGrid::wxGridSelectCells
)
5663 HighlightBlock( coords
, coords
);
5672 // ------------ Left double click
5674 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5676 DisableCellEditControl();
5678 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5680 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5685 // we want double click to select a cell and start editing
5686 // (i.e. to behave in same way as sequence of two slow clicks):
5687 m_waitForSlowClick
= true;
5694 // ------------ Left button released
5696 else if ( event
.LeftUp() )
5698 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5702 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5703 m_winCapture
= NULL
;
5706 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5709 EnableCellEditControl();
5711 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5712 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5713 editor
->StartingClick();
5717 m_waitForSlowClick
= false;
5719 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5720 m_selectingBottomRight
!= wxGridNoCellCoords
)
5724 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5725 m_selectingTopLeft
.GetCol(),
5726 m_selectingBottomRight
.GetRow(),
5727 m_selectingBottomRight
.GetCol(),
5728 event
.ControlDown(),
5734 m_selectingTopLeft
= wxGridNoCellCoords
;
5735 m_selectingBottomRight
= wxGridNoCellCoords
;
5737 // Show the edit control, if it has been hidden for
5739 ShowCellEditControl();
5742 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5744 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5745 DoEndDragResizeRow();
5747 // Note: we are ending the event *after* doing
5748 // default processing in this case
5750 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5752 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5754 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5755 DoEndDragResizeCol();
5757 // Note: we are ending the event *after* doing
5758 // default processing in this case
5760 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5767 // ------------ Right button down
5769 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5771 DisableCellEditControl();
5772 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5777 // no default action at the moment
5782 // ------------ Right double click
5784 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5786 DisableCellEditControl();
5787 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5792 // no default action at the moment
5796 // ------------ Moving and no button action
5798 else if ( event
.Moving() && !event
.IsButton() )
5800 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5802 // out of grid cell area
5803 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5807 int dragRow
= YToEdgeOfRow( y
);
5808 int dragCol
= XToEdgeOfCol( x
);
5810 // Dragging on the corner of a cell to resize in both
5811 // directions is not implemented yet...
5813 if ( dragRow
>= 0 && dragCol
>= 0 )
5815 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5821 m_dragRowOrCol
= dragRow
;
5823 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5825 if ( CanDragRowSize() && CanDragGridSize() )
5826 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5831 m_dragRowOrCol
= dragCol
;
5839 m_dragRowOrCol
= dragCol
;
5841 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5843 if ( CanDragColSize() && CanDragGridSize() )
5844 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5850 // Neither on a row or col edge
5852 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5854 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5860 void wxGrid::DoEndDragResizeRow()
5862 if ( m_dragLastPos
>= 0 )
5864 // erase the last line and resize the row
5866 int cw
, ch
, left
, dummy
;
5867 m_gridWin
->GetClientSize( &cw
, &ch
);
5868 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5870 wxClientDC
dc( m_gridWin
);
5872 dc
.SetLogicalFunction( wxINVERT
);
5873 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5874 HideCellEditControl();
5875 SaveEditControlValue();
5877 int rowTop
= GetRowTop(m_dragRowOrCol
);
5878 SetRowSize( m_dragRowOrCol
,
5879 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5881 if ( !GetBatchCount() )
5883 // Only needed to get the correct rect.y:
5884 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5886 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5887 rect
.width
= m_rowLabelWidth
;
5888 rect
.height
= ch
- rect
.y
;
5889 m_rowLabelWin
->Refresh( true, &rect
);
5891 // if there is a multicell block, paint all of it
5894 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5895 int leftCol
= XToCol(left
);
5896 int rightCol
= internalXToCol(left
+cw
);
5899 for (i
=leftCol
; i
<rightCol
; i
++)
5901 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5902 if (cell_rows
< subtract_rows
)
5903 subtract_rows
= cell_rows
;
5905 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5906 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5907 rect
.height
= ch
- rect
.y
;
5910 m_gridWin
->Refresh( false, &rect
);
5913 ShowCellEditControl();
5918 void wxGrid::DoEndDragResizeCol()
5920 if ( m_dragLastPos
>= 0 )
5922 // erase the last line and resize the col
5924 int cw
, ch
, dummy
, top
;
5925 m_gridWin
->GetClientSize( &cw
, &ch
);
5926 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5928 wxClientDC
dc( m_gridWin
);
5930 dc
.SetLogicalFunction( wxINVERT
);
5931 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5932 HideCellEditControl();
5933 SaveEditControlValue();
5935 int colLeft
= GetColLeft(m_dragRowOrCol
);
5936 SetColSize( m_dragRowOrCol
,
5937 wxMax( m_dragLastPos
- colLeft
,
5938 GetColMinimalWidth(m_dragRowOrCol
) ) );
5940 if ( !GetBatchCount() )
5942 // Only needed to get the correct rect.x:
5943 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5945 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5946 rect
.width
= cw
- rect
.x
;
5947 rect
.height
= m_colLabelHeight
;
5948 m_colLabelWin
->Refresh( true, &rect
);
5950 // if there is a multicell block, paint all of it
5953 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5954 int topRow
= YToRow(top
);
5955 int bottomRow
= internalYToRow(top
+cw
);
5958 for (i
=topRow
; i
<bottomRow
; i
++)
5960 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5961 if (cell_cols
< subtract_cols
)
5962 subtract_cols
= cell_cols
;
5964 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5965 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5966 rect
.width
= cw
- rect
.x
;
5969 m_gridWin
->Refresh( false, &rect
);
5972 ShowCellEditControl();
5979 // ------ interaction with data model
5981 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5983 switch ( msg
.GetId() )
5985 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5986 return GetModelValues();
5988 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5989 return SetModelValues();
5991 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5992 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5993 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5994 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5995 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5996 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5997 return Redimension( msg
);
6006 // The behaviour of this function depends on the grid table class
6007 // Clear() function. For the default wxGridStringTable class the
6008 // behavious is to replace all cell contents with wxEmptyString but
6009 // not to change the number of rows or cols.
6011 void wxGrid::ClearGrid()
6015 if (IsCellEditControlEnabled())
6016 DisableCellEditControl();
6019 if ( !GetBatchCount() ) m_gridWin
->Refresh();
6024 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6026 // TODO: something with updateLabels flag
6030 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6036 if (IsCellEditControlEnabled())
6037 DisableCellEditControl();
6039 bool done
= m_table
->InsertRows( pos
, numRows
);
6042 // the table will have sent the results of the insert row
6043 // operation to this view object as a grid table message
6049 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6051 // TODO: something with updateLabels flag
6055 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6061 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6063 // the table will have sent the results of the append row
6064 // operation to this view object as a grid table message
6070 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6072 // TODO: something with updateLabels flag
6076 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6082 if (IsCellEditControlEnabled())
6083 DisableCellEditControl();
6085 bool done
= m_table
->DeleteRows( pos
, numRows
);
6087 // the table will have sent the results of the delete row
6088 // operation to this view object as a grid table message
6094 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6096 // TODO: something with updateLabels flag
6100 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6106 if (IsCellEditControlEnabled())
6107 DisableCellEditControl();
6109 bool done
= m_table
->InsertCols( pos
, numCols
);
6111 // the table will have sent the results of the insert col
6112 // operation to this view object as a grid table message
6118 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6120 // TODO: something with updateLabels flag
6124 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6130 bool done
= m_table
->AppendCols( numCols
);
6132 // the table will have sent the results of the append col
6133 // operation to this view object as a grid table message
6139 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6141 // TODO: something with updateLabels flag
6145 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6151 if (IsCellEditControlEnabled())
6152 DisableCellEditControl();
6154 bool done
= m_table
->DeleteCols( pos
, numCols
);
6156 // the table will have sent the results of the delete col
6157 // operation to this view object as a grid table message
6165 // ----- event handlers
6168 // Generate a grid event based on a mouse event and
6169 // return the result of ProcessEvent()
6171 int wxGrid::SendEvent( const wxEventType type
,
6173 wxMouseEvent
& mouseEv
)
6178 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6180 int rowOrCol
= (row
== -1 ? col
: row
);
6182 wxGridSizeEvent
gridEvt( GetId(),
6186 mouseEv
.GetX() + GetRowLabelSize(),
6187 mouseEv
.GetY() + GetColLabelSize(),
6188 mouseEv
.ControlDown(),
6189 mouseEv
.ShiftDown(),
6191 mouseEv
.MetaDown() );
6193 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6194 vetoed
= !gridEvt
.IsAllowed();
6196 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6198 // Right now, it should _never_ end up here!
6199 wxGridRangeSelectEvent
gridEvt( GetId(),
6203 m_selectingBottomRight
,
6205 mouseEv
.ControlDown(),
6206 mouseEv
.ShiftDown(),
6208 mouseEv
.MetaDown() );
6210 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6211 vetoed
= !gridEvt
.IsAllowed();
6215 wxGridEvent
gridEvt( GetId(),
6219 mouseEv
.GetX() + GetRowLabelSize(),
6220 mouseEv
.GetY() + GetColLabelSize(),
6222 mouseEv
.ControlDown(),
6223 mouseEv
.ShiftDown(),
6225 mouseEv
.MetaDown() );
6226 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6227 vetoed
= !gridEvt
.IsAllowed();
6230 // A Veto'd event may not be `claimed' so test this first
6234 return claimed
? 1 : 0;
6238 // Generate a grid event of specified type and return the result
6239 // of ProcessEvent().
6241 int wxGrid::SendEvent( const wxEventType type
,
6247 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6249 int rowOrCol
= (row
== -1 ? col
: row
);
6251 wxGridSizeEvent
gridEvt( GetId(),
6256 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6257 vetoed
= !gridEvt
.IsAllowed();
6261 wxGridEvent
gridEvt( GetId(),
6266 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6267 vetoed
= !gridEvt
.IsAllowed();
6270 // A Veto'd event may not be `claimed' so test this first
6274 return claimed
? 1 : 0;
6277 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6279 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6282 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6284 // Don't do anything if between Begin/EndBatch...
6285 // EndBatch() will do all this on the last nested one anyway.
6286 if (! GetBatchCount())
6288 // Refresh to get correct scrolled position:
6289 wxScrolledWindow::Refresh(eraseb
, rect
);
6293 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6294 int width_label
, width_cell
, height_label
, height_cell
;
6297 //Copy rectangle can get scroll offsets..
6298 rect_x
= rect
->GetX();
6299 rect_y
= rect
->GetY();
6300 rectWidth
= rect
->GetWidth();
6301 rectHeight
= rect
->GetHeight();
6303 width_label
= m_rowLabelWidth
- rect_x
;
6304 if (width_label
> rectWidth
)
6305 width_label
= rectWidth
;
6307 height_label
= m_colLabelHeight
- rect_y
;
6308 if (height_label
> rectHeight
) height_label
= rectHeight
;
6310 if (rect_x
> m_rowLabelWidth
)
6312 x
= rect_x
- m_rowLabelWidth
;
6313 width_cell
= rectWidth
;
6318 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6321 if (rect_y
> m_colLabelHeight
)
6323 y
= rect_y
- m_colLabelHeight
;
6324 height_cell
= rectHeight
;
6329 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6332 // Paint corner label part intersecting rect.
6333 if ( width_label
> 0 && height_label
> 0 )
6335 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6336 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6339 // Paint col labels part intersecting rect.
6340 if ( width_cell
> 0 && height_label
> 0 )
6342 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6343 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6346 // Paint row labels part intersecting rect.
6347 if ( width_label
> 0 && height_cell
> 0 )
6349 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6350 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6353 // Paint cell area part intersecting rect.
6354 if ( width_cell
> 0 && height_cell
> 0 )
6356 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6357 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6362 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6363 m_colLabelWin
->Refresh(eraseb
, NULL
);
6364 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6365 m_gridWin
->Refresh(eraseb
, NULL
);
6370 void wxGrid::OnSize( wxSizeEvent
& event
)
6372 // position the child windows
6375 // don't call CalcDimensions() from here, the base class handles the size
6381 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6383 if ( m_inOnKeyDown
)
6385 // shouldn't be here - we are going round in circles...
6387 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6390 m_inOnKeyDown
= true;
6392 // propagate the event up and see if it gets processed
6394 wxWindow
*parent
= GetParent();
6395 wxKeyEvent
keyEvt( event
);
6396 keyEvt
.SetEventObject( parent
);
6398 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6401 // try local handlers
6403 switch ( event
.GetKeyCode() )
6406 if ( event
.ControlDown() )
6408 MoveCursorUpBlock( event
.ShiftDown() );
6412 MoveCursorUp( event
.ShiftDown() );
6417 if ( event
.ControlDown() )
6419 MoveCursorDownBlock( event
.ShiftDown() );
6423 MoveCursorDown( event
.ShiftDown() );
6428 if ( event
.ControlDown() )
6430 MoveCursorLeftBlock( event
.ShiftDown() );
6434 MoveCursorLeft( event
.ShiftDown() );
6439 if ( event
.ControlDown() )
6441 MoveCursorRightBlock( event
.ShiftDown() );
6445 MoveCursorRight( event
.ShiftDown() );
6450 case WXK_NUMPAD_ENTER
:
6451 if ( event
.ControlDown() )
6453 event
.Skip(); // to let the edit control have the return
6457 if ( GetGridCursorRow() < GetNumberRows()-1 )
6459 MoveCursorDown( event
.ShiftDown() );
6463 // at the bottom of a column
6464 DisableCellEditControl();
6474 if (event
.ShiftDown())
6476 if ( GetGridCursorCol() > 0 )
6478 MoveCursorLeft( false );
6483 DisableCellEditControl();
6488 if ( GetGridCursorCol() < GetNumberCols()-1 )
6490 MoveCursorRight( false );
6495 DisableCellEditControl();
6501 if ( event
.ControlDown() )
6503 MakeCellVisible( 0, 0 );
6504 SetCurrentCell( 0, 0 );
6513 if ( event
.ControlDown() )
6515 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6516 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6533 if ( event
.ControlDown() )
6537 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6538 m_currentCellCoords
.GetCol(),
6539 event
.ControlDown(),
6546 if ( !IsEditable() )
6548 MoveCursorRight( false );
6551 // Otherwise fall through to default
6559 m_inOnKeyDown
= false;
6562 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6564 // try local handlers
6566 if ( event
.GetKeyCode() == WXK_SHIFT
)
6568 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6569 m_selectingBottomRight
!= wxGridNoCellCoords
)
6573 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6574 m_selectingTopLeft
.GetCol(),
6575 m_selectingBottomRight
.GetRow(),
6576 m_selectingBottomRight
.GetCol(),
6577 event
.ControlDown(),
6584 m_selectingTopLeft
= wxGridNoCellCoords
;
6585 m_selectingBottomRight
= wxGridNoCellCoords
;
6586 m_selectingKeyboard
= wxGridNoCellCoords
;
6590 void wxGrid::OnChar( wxKeyEvent
& event
)
6592 // is it possible to edit the current cell at all?
6593 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6595 // yes, now check whether the cells editor accepts the key
6596 int row
= m_currentCellCoords
.GetRow();
6597 int col
= m_currentCellCoords
.GetCol();
6598 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6599 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6601 // <F2> is special and will always start editing, for
6602 // other keys - ask the editor itself
6603 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6604 || editor
->IsAcceptedKey(event
) )
6606 // ensure cell is visble
6607 MakeCellVisible(row
, col
);
6608 EnableCellEditControl();
6610 // a problem can arise if the cell is not completely
6611 // visible (even after calling MakeCellVisible the
6612 // control is not created and calling StartingKey will
6614 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6615 editor
->StartingKey(event
);
6632 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6636 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6638 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6640 // the event has been intercepted - do nothing
6644 wxClientDC
dc(m_gridWin
);
6647 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6649 DisableCellEditControl();
6651 if ( IsVisible( m_currentCellCoords
, false ) )
6654 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6655 if ( !m_gridLinesEnabled
)
6663 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6665 // Otherwise refresh redraws the highlight!
6666 m_currentCellCoords
= coords
;
6668 DrawGridCellArea(dc
,cells
);
6669 DrawAllGridLines( dc
, r
);
6673 m_currentCellCoords
= coords
;
6675 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6676 DrawCellHighlight(dc
, attr
);
6681 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6684 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6688 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6691 rightCol
= GetNumberCols() - 1;
6693 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6696 bottomRow
= GetNumberRows() - 1;
6700 if ( topRow
> bottomRow
)
6707 if ( leftCol
> rightCol
)
6714 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6715 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6717 // First the case that we selected a completely new area
6718 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6719 m_selectingBottomRight
== wxGridNoCellCoords
)
6722 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6723 wxGridCellCoords ( bottomRow
, rightCol
) );
6724 m_gridWin
->Refresh( false, &rect
);
6726 // Now handle changing an existing selection area.
6727 else if ( m_selectingTopLeft
!= updateTopLeft
||
6728 m_selectingBottomRight
!= updateBottomRight
)
6730 // Compute two optimal update rectangles:
6731 // Either one rectangle is a real subset of the
6732 // other, or they are (almost) disjoint!
6734 bool need_refresh
[4];
6738 need_refresh
[3] = false;
6741 // Store intermediate values
6742 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6743 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6744 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6745 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6747 // Determine the outer/inner coordinates.
6748 if (oldLeft
> leftCol
)
6754 if (oldTop
> topRow
)
6760 if (oldRight
< rightCol
)
6763 oldRight
= rightCol
;
6766 if (oldBottom
< bottomRow
)
6769 oldBottom
= bottomRow
;
6773 // Now, either the stuff marked old is the outer
6774 // rectangle or we don't have a situation where one
6775 // is contained in the other.
6777 if ( oldLeft
< leftCol
)
6779 // Refresh the newly selected or deselected
6780 // area to the left of the old or new selection.
6781 need_refresh
[0] = true;
6782 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6784 wxGridCellCoords ( oldBottom
,
6788 if ( oldTop
< topRow
)
6790 // Refresh the newly selected or deselected
6791 // area above the old or new selection.
6792 need_refresh
[1] = true;
6793 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6795 wxGridCellCoords ( topRow
- 1,
6799 if ( oldRight
> rightCol
)
6801 // Refresh the newly selected or deselected
6802 // area to the right of the old or new selection.
6803 need_refresh
[2] = true;
6804 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6806 wxGridCellCoords ( oldBottom
,
6810 if ( oldBottom
> bottomRow
)
6812 // Refresh the newly selected or deselected
6813 // area below the old or new selection.
6814 need_refresh
[3] = true;
6815 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6817 wxGridCellCoords ( oldBottom
,
6821 // various Refresh() calls
6822 for (i
= 0; i
< 4; i
++ )
6823 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6824 m_gridWin
->Refresh( false, &(rect
[i
]) );
6827 m_selectingTopLeft
= updateTopLeft
;
6828 m_selectingBottomRight
= updateBottomRight
;
6832 // ------ functions to get/send data (see also public functions)
6835 bool wxGrid::GetModelValues()
6837 // Hide the editor, so it won't hide a changed value.
6838 HideCellEditControl();
6842 // all we need to do is repaint the grid
6844 m_gridWin
->Refresh();
6852 bool wxGrid::SetModelValues()
6856 // Disable the editor, so it won't hide a changed value.
6857 // Do we also want to save the current value of the editor first?
6859 DisableCellEditControl();
6863 for ( row
= 0; row
< m_numRows
; row
++ )
6865 for ( col
= 0; col
< m_numCols
; col
++ )
6867 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6879 // Note - this function only draws cells that are in the list of
6880 // exposed cells (usually set from the update region by
6881 // CalcExposedCells)
6883 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6885 if ( !m_numRows
|| !m_numCols
)
6888 int i
, numCells
= cells
.GetCount();
6889 int row
, col
, cell_rows
, cell_cols
;
6890 wxGridCellCoordsArray redrawCells
;
6892 for ( i
= numCells
-1; i
>= 0; i
-- )
6894 row
= cells
[i
].GetRow();
6895 col
= cells
[i
].GetCol();
6896 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6898 // If this cell is part of a multicell block, find owner for repaint
6899 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6901 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6902 bool marked
= false;
6903 for ( int j
= 0; j
< numCells
; j
++ )
6905 if ( cell
== cells
[j
] )
6913 int count
= redrawCells
.GetCount();
6914 for (int j
= 0; j
< count
; j
++)
6916 if ( cell
== redrawCells
[j
] )
6923 redrawCells
.Add( cell
);
6925 continue; // don't bother drawing this cell
6928 // If this cell is empty, find cell to left that might want to overflow
6929 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6931 for ( int l
= 0; l
< cell_rows
; l
++ )
6933 // find a cell in this row to left alreay marked for repaint
6935 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6936 if ((redrawCells
[k
].GetCol() < left
) &&
6937 (redrawCells
[k
].GetRow() == row
))
6938 left
= redrawCells
[k
].GetCol();
6941 left
= 0; // oh well
6943 for (int j
= col
-1; j
>= left
; j
--)
6945 if (!m_table
->IsEmptyCell(row
+l
, j
))
6947 if (GetCellOverflow(row
+l
, j
))
6949 wxGridCellCoords
cell(row
+l
, j
);
6950 bool marked
= false;
6952 for (int k
= 0; k
< numCells
; k
++)
6954 if ( cell
== cells
[k
] )
6963 int count
= redrawCells
.GetCount();
6964 for (int k
= 0; k
< count
; k
++)
6966 if ( cell
== redrawCells
[k
] )
6973 redrawCells
.Add( cell
);
6982 DrawCell( dc
, cells
[i
] );
6985 numCells
= redrawCells
.GetCount();
6987 for ( i
= numCells
- 1; i
>= 0; i
-- )
6989 DrawCell( dc
, redrawCells
[i
] );
6994 void wxGrid::DrawGridSpace( wxDC
& dc
)
6997 m_gridWin
->GetClientSize( &cw
, &ch
);
7000 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7002 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
7003 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
7005 if ( right
> rightCol
|| bottom
> bottomRow
)
7008 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7010 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7011 dc
.SetPen( *wxTRANSPARENT_PEN
);
7013 if ( right
> rightCol
)
7015 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7018 if ( bottom
> bottomRow
)
7020 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7026 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7028 int row
= coords
.GetRow();
7029 int col
= coords
.GetCol();
7031 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7034 // we draw the cell border ourselves
7035 #if !WXGRID_DRAW_LINES
7036 if ( m_gridLinesEnabled
)
7037 DrawCellBorder( dc
, coords
);
7040 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7042 bool isCurrent
= coords
== m_currentCellCoords
;
7044 wxRect rect
= CellToRect( row
, col
);
7046 // if the editor is shown, we should use it and not the renderer
7047 // Note: However, only if it is really _shown_, i.e. not hidden!
7048 if ( isCurrent
&& IsCellEditControlShown() )
7050 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7051 editor
->PaintBackground(rect
, attr
);
7056 // but all the rest is drawn by the cell renderer and hence may be
7058 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7059 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7066 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7068 int row
= m_currentCellCoords
.GetRow();
7069 int col
= m_currentCellCoords
.GetCol();
7071 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7074 wxRect rect
= CellToRect(row
, col
);
7076 // hmmm... what could we do here to show that the cell is disabled?
7077 // for now, I just draw a thinner border than for the other ones, but
7078 // it doesn't look really good
7080 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7084 // The center of th drawn line is where the position/width/height of
7085 // the rectangle is actually at, (on wxMSW atr least,) so we will
7086 // reduce the size of the rectangle to compensate for the thickness of
7087 // the line. If this is too strange on non wxMSW platforms then
7088 // please #ifdef this appropriately.
7089 rect
.x
+= penWidth
/ 2;
7090 rect
.y
+= penWidth
/ 2;
7091 rect
.width
-= penWidth
- 1;
7092 rect
.height
-= penWidth
- 1;
7095 // Now draw the rectangle
7096 // use the cellHighlightColour if the cell is inside a selection, this
7097 // will ensure the cell is always visible.
7098 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7099 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7100 dc
.DrawRectangle(rect
);
7104 // VZ: my experiments with 3d borders...
7106 // how to properly set colours for arbitrary bg?
7107 wxCoord x1
= rect
.x
,
7109 x2
= rect
.x
+ rect
.width
- 1,
7110 y2
= rect
.y
+ rect
.height
- 1;
7112 dc
.SetPen(*wxWHITE_PEN
);
7113 dc
.DrawLine(x1
, y1
, x2
, y1
);
7114 dc
.DrawLine(x1
, y1
, x1
, y2
);
7116 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7117 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7119 dc
.SetPen(*wxBLACK_PEN
);
7120 dc
.DrawLine(x1
, y2
, x2
, y2
);
7121 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7126 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7128 int row
= coords
.GetRow();
7129 int col
= coords
.GetCol();
7130 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7133 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7135 wxRect rect
= CellToRect( row
, col
);
7137 // right hand border
7139 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7140 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7144 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7145 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7148 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7150 // This if block was previously in wxGrid::OnPaint but that doesn't
7151 // seem to get called under wxGTK - MB
7153 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7154 m_numRows
&& m_numCols
)
7156 m_currentCellCoords
.Set(0, 0);
7159 if ( IsCellEditControlShown() )
7161 // don't show highlight when the edit control is shown
7165 // if the active cell was repainted, repaint its highlight too because it
7166 // might have been damaged by the grid lines
7167 size_t count
= cells
.GetCount();
7168 for ( size_t n
= 0; n
< count
; n
++ )
7170 if ( cells
[n
] == m_currentCellCoords
)
7172 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7173 DrawCellHighlight(dc
, attr
);
7181 // TODO: remove this ???
7182 // This is used to redraw all grid lines e.g. when the grid line colour
7185 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7187 #if !WXGRID_DRAW_LINES
7191 if ( !m_gridLinesEnabled
||
7196 int top
, bottom
, left
, right
;
7198 #if 0 //#ifndef __WXGTK__
7202 m_gridWin
->GetClientSize(&cw
, &ch
);
7204 // virtual coords of visible area
7206 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7207 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7212 reg
.GetBox(x
, y
, w
, h
);
7213 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7214 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7218 m_gridWin
->GetClientSize(&cw
, &ch
);
7219 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7220 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7223 // avoid drawing grid lines past the last row and col
7225 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7226 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7228 // no gridlines inside multicells, clip them out
7229 int leftCol
= internalXToCol(left
);
7230 int topRow
= internalYToRow(top
);
7231 int rightCol
= internalXToCol(right
);
7232 int bottomRow
= internalYToRow(bottom
);
7235 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7236 wxRegion
clippedcells(0, 0, cw
, ch
);
7238 int i
, j
, cell_rows
, cell_cols
;
7241 for (j
=topRow
; j
<bottomRow
; j
++)
7243 for (i
=leftCol
; i
<rightCol
; i
++)
7245 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7246 if ((cell_rows
> 1) || (cell_cols
> 1))
7248 rect
= CellToRect(j
,i
);
7249 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7250 clippedcells
.Subtract(rect
);
7252 else if ((cell_rows
< 0) || (cell_cols
< 0))
7254 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7255 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7256 clippedcells
.Subtract(rect
);
7261 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7263 int i
, j
, cell_rows
, cell_cols
;
7266 for (j
=topRow
; j
<bottomRow
; j
++)
7268 for (i
=leftCol
; i
<rightCol
; i
++)
7270 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7271 if ((cell_rows
> 1) || (cell_cols
> 1))
7273 rect
= CellToRect(j
,i
);
7274 clippedcells
.Subtract(rect
);
7276 else if ((cell_rows
< 0) || (cell_cols
< 0))
7278 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7279 clippedcells
.Subtract(rect
);
7284 dc
.SetClippingRegion( clippedcells
);
7286 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7288 // horizontal grid lines
7290 // already declared above - int i;
7291 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7293 int bot
= GetRowBottom(i
) - 1;
7302 dc
.DrawLine( left
, bot
, right
, bot
);
7306 // vertical grid lines
7308 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7310 int colRight
= GetColRight(i
) - 1;
7311 if ( colRight
> right
)
7316 if ( colRight
>= left
)
7318 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7321 dc
.DestroyClippingRegion();
7324 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7330 size_t numLabels
= rows
.GetCount();
7332 for ( i
= 0; i
< numLabels
; i
++ )
7334 DrawRowLabel( dc
, rows
[i
] );
7338 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7340 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7346 rect
.SetY( GetRowTop(row
) + 1 );
7347 rect
.SetWidth( m_rowLabelWidth
- 2 );
7348 rect
.SetHeight( GetRowHeight(row
) - 2 );
7350 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7352 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7354 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7356 int rowTop
= GetRowTop(row
),
7357 rowBottom
= GetRowBottom(row
) - 1;
7359 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7360 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7361 m_rowLabelWidth
-1, rowBottom
);
7363 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7365 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7367 dc
.SetPen( *wxWHITE_PEN
);
7368 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7369 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7371 dc
.SetBackgroundMode( wxTRANSPARENT
);
7372 dc
.SetTextForeground( GetLabelTextColour() );
7373 dc
.SetFont( GetLabelFont() );
7376 GetRowLabelAlignment( &hAlign
, &vAlign
);
7379 rect
.SetY( GetRowTop(row
) + 2 );
7380 rect
.SetWidth( m_rowLabelWidth
- 4 );
7381 rect
.SetHeight( GetRowHeight(row
) - 4 );
7382 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7385 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7391 size_t numLabels
= cols
.GetCount();
7393 for ( i
= 0; i
< numLabels
; i
++ )
7395 DrawColLabel( dc
, cols
[i
] );
7399 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7401 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7404 int colLeft
= GetColLeft(col
);
7408 rect
.SetX( colLeft
+ 1 );
7410 rect
.SetWidth( GetColWidth(col
) - 2 );
7411 rect
.SetHeight( m_colLabelHeight
- 2 );
7413 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7415 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7417 int colRight
= GetColRight(col
) - 1;
7419 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7420 dc
.DrawLine( colRight
, 0,
7421 colRight
, m_colLabelHeight
-1 );
7423 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7425 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7426 colRight
+1, m_colLabelHeight
-1 );
7428 dc
.SetPen( *wxWHITE_PEN
);
7429 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7430 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7432 dc
.SetBackgroundMode( wxTRANSPARENT
);
7433 dc
.SetTextForeground( GetLabelTextColour() );
7434 dc
.SetFont( GetLabelFont() );
7436 int hAlign
, vAlign
, orient
;
7437 GetColLabelAlignment( &hAlign
, &vAlign
);
7438 orient
= GetColLabelTextOrientation();
7440 rect
.SetX( colLeft
+ 2 );
7442 rect
.SetWidth( GetColWidth(col
) - 4 );
7443 rect
.SetHeight( m_colLabelHeight
- 4 );
7444 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7447 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7448 const wxString
& value
,
7452 int textOrientation
)
7454 wxArrayString lines
;
7456 StringToLines( value
, lines
);
7458 //Forward to new API.
7459 DrawTextRectangle( dc
,
7468 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7469 const wxArrayString
& lines
,
7473 int textOrientation
)
7475 long textWidth
, textHeight
;
7476 long lineWidth
, lineHeight
;
7479 dc
.SetClippingRegion( rect
);
7481 nLines
= lines
.GetCount();
7485 float x
= 0.0, y
= 0.0;
7487 if ( textOrientation
== wxHORIZONTAL
)
7488 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7490 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7494 case wxALIGN_BOTTOM
:
7495 if ( textOrientation
== wxHORIZONTAL
)
7496 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7498 x
= rect
.x
+ rect
.width
- textWidth
;
7501 case wxALIGN_CENTRE
:
7502 if ( textOrientation
== wxHORIZONTAL
)
7503 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7505 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7510 if ( textOrientation
== wxHORIZONTAL
)
7517 // Align each line of a multi-line label
7518 for ( l
= 0; l
< nLines
; l
++ )
7520 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7522 switch( horizAlign
)
7525 if ( textOrientation
== wxHORIZONTAL
)
7526 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7528 y
= rect
.y
+ lineWidth
+ 1;
7531 case wxALIGN_CENTRE
:
7532 if ( textOrientation
== wxHORIZONTAL
)
7533 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7535 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7540 if ( textOrientation
== wxHORIZONTAL
)
7543 y
= rect
.y
+ rect
.height
- 1;
7547 if ( textOrientation
== wxHORIZONTAL
)
7549 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7554 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7560 dc
.DestroyClippingRegion();
7564 // Split multi line text up into an array of strings. Any existing
7565 // contents of the string array are preserved.
7567 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7571 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7572 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7574 while ( startPos
< (int)tVal
.Length() )
7576 pos
= tVal
.Mid(startPos
).Find( eol
);
7581 else if ( pos
== 0 )
7583 lines
.Add( wxEmptyString
);
7587 lines
.Add( value
.Mid(startPos
, pos
) );
7589 startPos
+= pos
+ 1;
7592 if ( startPos
< (int)value
.Length() )
7594 lines
.Add( value
.Mid( startPos
) );
7599 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
7600 const wxArrayString
& lines
,
7601 long *width
, long *height
)
7608 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7610 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7611 w
= wxMax( w
, lineW
);
7620 // ------ Batch processing.
7622 void wxGrid::EndBatch()
7624 if ( m_batchCount
> 0 )
7627 if ( !m_batchCount
)
7630 m_rowLabelWin
->Refresh();
7631 m_colLabelWin
->Refresh();
7632 m_cornerLabelWin
->Refresh();
7633 m_gridWin
->Refresh();
7638 // Use this, rather than wxWindow::Refresh(), to force an immediate
7639 // repainting of the grid. Has no effect if you are already inside a
7640 // BeginBatch / EndBatch block.
7642 void wxGrid::ForceRefresh()
7648 bool wxGrid::Enable(bool enable
)
7650 if ( !wxScrolledWindow::Enable(enable
) )
7653 // redraw in the new state
7654 m_gridWin
->Refresh();
7661 // ------ Edit control functions
7664 void wxGrid::EnableEditing( bool edit
)
7666 // TODO: improve this ?
7668 if ( edit
!= m_editable
)
7671 EnableCellEditControl(edit
);
7676 void wxGrid::EnableCellEditControl( bool enable
)
7681 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7682 SetCurrentCell( 0, 0 );
7684 if ( enable
!= m_cellEditCtrlEnabled
)
7688 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7691 // this should be checked by the caller!
7692 wxASSERT_MSG( CanEnableCellControl(),
7693 _T("can't enable editing for this cell!") );
7695 // do it before ShowCellEditControl()
7696 m_cellEditCtrlEnabled
= enable
;
7698 ShowCellEditControl();
7702 //FIXME:add veto support
7703 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7705 HideCellEditControl();
7706 SaveEditControlValue();
7708 // do it after HideCellEditControl()
7709 m_cellEditCtrlEnabled
= enable
;
7714 bool wxGrid::IsCurrentCellReadOnly() const
7717 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7718 bool readonly
= attr
->IsReadOnly();
7724 bool wxGrid::CanEnableCellControl() const
7726 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7727 !IsCurrentCellReadOnly();
7731 bool wxGrid::IsCellEditControlEnabled() const
7733 // the cell edit control might be disable for all cells or just for the
7734 // current one if it's read only
7735 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7738 bool wxGrid::IsCellEditControlShown() const
7740 bool isShown
= false;
7742 if ( m_cellEditCtrlEnabled
)
7744 int row
= m_currentCellCoords
.GetRow();
7745 int col
= m_currentCellCoords
.GetCol();
7746 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7747 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7752 if ( editor
->IsCreated() )
7754 isShown
= editor
->GetControl()->IsShown();
7764 void wxGrid::ShowCellEditControl()
7766 if ( IsCellEditControlEnabled() )
7768 if ( !IsVisible( m_currentCellCoords
) )
7770 m_cellEditCtrlEnabled
= false;
7775 wxRect rect
= CellToRect( m_currentCellCoords
);
7776 int row
= m_currentCellCoords
.GetRow();
7777 int col
= m_currentCellCoords
.GetCol();
7779 // if this is part of a multicell, find owner (topleft)
7780 int cell_rows
, cell_cols
;
7781 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7782 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7786 m_currentCellCoords
.SetRow( row
);
7787 m_currentCellCoords
.SetCol( col
);
7790 // convert to scrolled coords
7792 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7794 // done in PaintBackground()
7796 // erase the highlight and the cell contents because the editor
7797 // might not cover the entire cell
7798 wxClientDC
dc( m_gridWin
);
7800 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7801 dc
.SetPen(*wxTRANSPARENT_PEN
);
7802 dc
.DrawRectangle(rect
);
7805 // cell is shifted by one pixel
7806 // However, don't allow x or y to become negative
7807 // since the SetSize() method interprets that as
7814 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7815 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7816 if ( !editor
->IsCreated() )
7818 editor
->Create(m_gridWin
, wxID_ANY
,
7819 new wxGridCellEditorEvtHandler(this, editor
));
7821 wxGridEditorCreatedEvent
evt(GetId(),
7822 wxEVT_GRID_EDITOR_CREATED
,
7826 editor
->GetControl());
7827 GetEventHandler()->ProcessEvent(evt
);
7831 // resize editor to overflow into righthand cells if allowed
7832 int maxWidth
= rect
.width
;
7833 wxString value
= GetCellValue(row
, col
);
7834 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7837 GetTextExtent(value
, &maxWidth
, &y
,
7838 NULL
, NULL
, &attr
->GetFont());
7839 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7842 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7843 if (rect
.x
+maxWidth
> client_right
)
7844 maxWidth
= client_right
- rect
.x
;
7846 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7848 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7849 // may have changed earlier
7850 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7853 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7854 // looks weird going over a multicell
7855 if (m_table
->IsEmptyCell(row
,i
) &&
7856 (rect
.width
< maxWidth
) && (c_rows
== 1))
7857 rect
.width
+= GetColWidth(i
);
7862 if (rect
.GetRight() > client_right
)
7863 rect
.SetRight(client_right
- 1);
7866 editor
->SetCellAttr(attr
);
7867 editor
->SetSize( rect
);
7868 editor
->Show( true, attr
);
7870 // recalc dimensions in case we need to
7871 // expand the scrolled window to account for editor
7874 editor
->BeginEdit(row
, col
, this);
7875 editor
->SetCellAttr(NULL
);
7883 void wxGrid::HideCellEditControl()
7885 if ( IsCellEditControlEnabled() )
7887 int row
= m_currentCellCoords
.GetRow();
7888 int col
= m_currentCellCoords
.GetCol();
7890 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7891 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7892 editor
->Show( false );
7896 m_gridWin
->SetFocus();
7898 // refresh whole row to the right
7899 wxRect
rect( CellToRect(row
, col
) );
7900 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7901 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7903 // ensure that the pixels under the focus ring get refreshed as well
7904 rect
.Inflate(10, 10);
7906 m_gridWin
->Refresh( false, &rect
);
7910 void wxGrid::SaveEditControlValue()
7912 if ( IsCellEditControlEnabled() )
7914 int row
= m_currentCellCoords
.GetRow();
7915 int col
= m_currentCellCoords
.GetCol();
7917 wxString oldval
= GetCellValue(row
, col
);
7919 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7920 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7921 bool changed
= editor
->EndEdit(row
, col
, this);
7928 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7929 m_currentCellCoords
.GetRow(),
7930 m_currentCellCoords
.GetCol() ) < 0 )
7932 // Event has been vetoed, set the data back.
7933 SetCellValue(row
, col
, oldval
);
7941 // ------ Grid location functions
7942 // Note that all of these functions work with the logical coordinates of
7943 // grid cells and labels so you will need to convert from device
7944 // coordinates for mouse events etc.
7947 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7949 int row
= YToRow(y
);
7950 int col
= XToCol(x
);
7952 if ( row
== -1 || col
== -1 )
7954 coords
= wxGridNoCellCoords
;
7958 coords
.Set( row
, col
);
7963 // Internal Helper function for computing row or column from some
7964 // (unscrolled) coordinate value, using either
7965 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7966 // of m_rowBottoms/m_ColRights to speed up the search!
7968 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7969 const wxArrayInt
& BorderArray
, int nMax
,
7973 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7978 size_t i_max
= coord
/ defaultDist
,
7981 if (BorderArray
.IsEmpty())
7983 if ((int) i_max
< nMax
)
7985 return clipToMinMax
? nMax
- 1 : -1;
7988 if ( i_max
>= BorderArray
.GetCount())
7989 i_max
= BorderArray
.GetCount() - 1;
7992 if ( coord
>= BorderArray
[i_max
])
7996 i_max
= coord
/ minDist
;
7998 i_max
= BorderArray
.GetCount() - 1;
8001 if ( i_max
>= BorderArray
.GetCount())
8002 i_max
= BorderArray
.GetCount() - 1;
8005 if ( coord
>= BorderArray
[i_max
])
8006 return clipToMinMax
? (int)i_max
: -1;
8007 if ( coord
< BorderArray
[0] )
8010 while ( i_max
- i_min
> 0 )
8012 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8013 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8014 if (coord
>= BorderArray
[ i_max
- 1])
8018 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8019 if (coord
< BorderArray
[median
])
8028 int wxGrid::YToRow( int y
)
8030 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8031 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8034 int wxGrid::XToCol( int x
)
8036 return CoordToRowOrCol(x
, m_defaultColWidth
,
8037 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8040 // return the row number that that the y coord is near the edge of, or
8041 // -1 if not near an edge
8043 int wxGrid::YToEdgeOfRow( int y
)
8046 i
= internalYToRow(y
);
8048 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8050 // We know that we are in row i, test whether we are
8051 // close enough to lower or upper border, respectively.
8052 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8054 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8061 // return the col number that that the x coord is near the edge of, or
8062 // -1 if not near an edge
8064 int wxGrid::XToEdgeOfCol( int x
)
8067 i
= internalXToCol(x
);
8069 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8071 // We know that we are in column i, test whether we are
8072 // close enough to right or left border, respectively.
8073 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8075 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8082 wxRect
wxGrid::CellToRect( int row
, int col
)
8084 wxRect
rect( -1, -1, -1, -1 );
8086 if ( row
>= 0 && row
< m_numRows
&&
8087 col
>= 0 && col
< m_numCols
)
8089 int i
, cell_rows
, cell_cols
;
8090 rect
.width
= rect
.height
= 0;
8091 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8092 // if negative then find multicell owner
8093 if (cell_rows
< 0) row
+= cell_rows
;
8094 if (cell_cols
< 0) col
+= cell_cols
;
8095 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8097 rect
.x
= GetColLeft(col
);
8098 rect
.y
= GetRowTop(row
);
8099 for (i
=col
; i
<col
+cell_cols
; i
++)
8100 rect
.width
+= GetColWidth(i
);
8101 for (i
=row
; i
<row
+cell_rows
; i
++)
8102 rect
.height
+= GetRowHeight(i
);
8105 // if grid lines are enabled, then the area of the cell is a bit smaller
8106 if (m_gridLinesEnabled
)
8115 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8117 // get the cell rectangle in logical coords
8119 wxRect
r( CellToRect( row
, col
) );
8121 // convert to device coords
8123 int left
, top
, right
, bottom
;
8124 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8125 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8127 // check against the client area of the grid window
8130 m_gridWin
->GetClientSize( &cw
, &ch
);
8132 if ( wholeCellVisible
)
8134 // is the cell wholly visible ?
8136 return ( left
>= 0 && right
<= cw
&&
8137 top
>= 0 && bottom
<= ch
);
8141 // is the cell partly visible ?
8143 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8144 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8148 // make the specified cell location visible by doing a minimal amount
8151 void wxGrid::MakeCellVisible( int row
, int col
)
8154 int xpos
= -1, ypos
= -1;
8156 if ( row
>= 0 && row
< m_numRows
&&
8157 col
>= 0 && col
< m_numCols
)
8159 // get the cell rectangle in logical coords
8161 wxRect
r( CellToRect( row
, col
) );
8163 // convert to device coords
8165 int left
, top
, right
, bottom
;
8166 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8167 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8170 m_gridWin
->GetClientSize( &cw
, &ch
);
8176 else if ( bottom
> ch
)
8178 int h
= r
.GetHeight();
8180 for ( i
= row
-1; i
>= 0; i
-- )
8182 int rowHeight
= GetRowHeight(i
);
8183 if ( h
+ rowHeight
> ch
)
8190 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8191 // have rounding errors (this is important, because if we do, we
8192 // might not scroll at all and some cells won't be redrawn)
8194 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8196 ypos
+= m_scrollLineY
;
8203 else if ( right
> cw
)
8205 // position the view so that the cell is on the right
8207 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8208 xpos
= x0
+ (right
- cw
);
8210 // see comment for ypos above
8211 xpos
+= m_scrollLineX
;
8214 if ( xpos
!= -1 || ypos
!= -1 )
8217 xpos
/= m_scrollLineX
;
8219 ypos
/= m_scrollLineY
;
8220 Scroll( xpos
, ypos
);
8228 // ------ Grid cursor movement functions
8231 bool wxGrid::MoveCursorUp( bool expandSelection
)
8233 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8234 m_currentCellCoords
.GetRow() >= 0 )
8236 if ( expandSelection
)
8238 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8239 m_selectingKeyboard
= m_currentCellCoords
;
8240 if ( m_selectingKeyboard
.GetRow() > 0 )
8242 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8243 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8244 m_selectingKeyboard
.GetCol() );
8245 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8248 else if ( m_currentCellCoords
.GetRow() > 0 )
8251 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8252 m_currentCellCoords
.GetCol() );
8253 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8254 m_currentCellCoords
.GetCol() );
8266 bool wxGrid::MoveCursorDown( bool expandSelection
)
8268 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8269 m_currentCellCoords
.GetRow() < m_numRows
)
8271 if ( expandSelection
)
8273 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8274 m_selectingKeyboard
= m_currentCellCoords
;
8275 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8277 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8278 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8279 m_selectingKeyboard
.GetCol() );
8280 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8283 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8286 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8287 m_currentCellCoords
.GetCol() );
8288 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8289 m_currentCellCoords
.GetCol() );
8300 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8302 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8303 m_currentCellCoords
.GetCol() >= 0 )
8305 if ( expandSelection
)
8307 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8308 m_selectingKeyboard
= m_currentCellCoords
;
8309 if ( m_selectingKeyboard
.GetCol() > 0 )
8311 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8312 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8313 m_selectingKeyboard
.GetCol() );
8314 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8317 else if ( m_currentCellCoords
.GetCol() > 0 )
8320 MakeCellVisible( m_currentCellCoords
.GetRow(),
8321 m_currentCellCoords
.GetCol() - 1 );
8322 SetCurrentCell( m_currentCellCoords
.GetRow(),
8323 m_currentCellCoords
.GetCol() - 1 );
8334 bool wxGrid::MoveCursorRight( bool expandSelection
)
8336 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8337 m_currentCellCoords
.GetCol() < m_numCols
)
8339 if ( expandSelection
)
8341 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8342 m_selectingKeyboard
= m_currentCellCoords
;
8343 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8345 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8346 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8347 m_selectingKeyboard
.GetCol() );
8348 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8351 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8354 MakeCellVisible( m_currentCellCoords
.GetRow(),
8355 m_currentCellCoords
.GetCol() + 1 );
8356 SetCurrentCell( m_currentCellCoords
.GetRow(),
8357 m_currentCellCoords
.GetCol() + 1 );
8368 bool wxGrid::MovePageUp()
8370 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8373 int row
= m_currentCellCoords
.GetRow();
8377 m_gridWin
->GetClientSize( &cw
, &ch
);
8379 int y
= GetRowTop(row
);
8380 int newRow
= internalYToRow( y
- ch
+ 1 );
8382 if ( newRow
== row
)
8384 //row > 0 , so newrow can never be less than 0 here.
8388 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8389 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8397 bool wxGrid::MovePageDown()
8399 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8402 int row
= m_currentCellCoords
.GetRow();
8403 if ( (row
+1) < m_numRows
)
8406 m_gridWin
->GetClientSize( &cw
, &ch
);
8408 int y
= GetRowTop(row
);
8409 int newRow
= internalYToRow( y
+ ch
);
8410 if ( newRow
== row
)
8412 // row < m_numRows , so newrow can't overflow here.
8416 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8417 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8425 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8428 m_currentCellCoords
!= wxGridNoCellCoords
&&
8429 m_currentCellCoords
.GetRow() > 0 )
8431 int row
= m_currentCellCoords
.GetRow();
8432 int col
= m_currentCellCoords
.GetCol();
8434 if ( m_table
->IsEmptyCell(row
, col
) )
8436 // starting in an empty cell: find the next block of
8442 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8446 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8448 // starting at the top of a block: find the next block
8454 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8460 // starting within a block: find the top of the block
8465 if ( m_table
->IsEmptyCell(row
, col
) )
8473 MakeCellVisible( row
, col
);
8474 if ( expandSelection
)
8476 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8477 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8482 SetCurrentCell( row
, col
);
8491 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8494 m_currentCellCoords
!= wxGridNoCellCoords
&&
8495 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8497 int row
= m_currentCellCoords
.GetRow();
8498 int col
= m_currentCellCoords
.GetCol();
8500 if ( m_table
->IsEmptyCell(row
, col
) )
8502 // starting in an empty cell: find the next block of
8505 while ( row
< m_numRows
-1 )
8508 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8512 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8514 // starting at the bottom of a block: find the next block
8517 while ( row
< m_numRows
-1 )
8520 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8526 // starting within a block: find the bottom of the block
8528 while ( row
< m_numRows
-1 )
8531 if ( m_table
->IsEmptyCell(row
, col
) )
8539 MakeCellVisible( row
, col
);
8540 if ( expandSelection
)
8542 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8543 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8548 SetCurrentCell( row
, col
);
8557 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8560 m_currentCellCoords
!= wxGridNoCellCoords
&&
8561 m_currentCellCoords
.GetCol() > 0 )
8563 int row
= m_currentCellCoords
.GetRow();
8564 int col
= m_currentCellCoords
.GetCol();
8566 if ( m_table
->IsEmptyCell(row
, col
) )
8568 // starting in an empty cell: find the next block of
8574 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8578 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8580 // starting at the left of a block: find the next block
8586 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8592 // starting within a block: find the left of the block
8597 if ( m_table
->IsEmptyCell(row
, col
) )
8605 MakeCellVisible( row
, col
);
8606 if ( expandSelection
)
8608 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8609 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8614 SetCurrentCell( row
, col
);
8623 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8626 m_currentCellCoords
!= wxGridNoCellCoords
&&
8627 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8629 int row
= m_currentCellCoords
.GetRow();
8630 int col
= m_currentCellCoords
.GetCol();
8632 if ( m_table
->IsEmptyCell(row
, col
) )
8634 // starting in an empty cell: find the next block of
8637 while ( col
< m_numCols
-1 )
8640 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8644 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8646 // starting at the right of a block: find the next block
8649 while ( col
< m_numCols
-1 )
8652 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8658 // starting within a block: find the right of the block
8660 while ( col
< m_numCols
-1 )
8663 if ( m_table
->IsEmptyCell(row
, col
) )
8671 MakeCellVisible( row
, col
);
8672 if ( expandSelection
)
8674 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8675 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8680 SetCurrentCell( row
, col
);
8692 // ------ Label values and formatting
8695 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8697 *horiz
= m_rowLabelHorizAlign
;
8698 *vert
= m_rowLabelVertAlign
;
8701 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8703 *horiz
= m_colLabelHorizAlign
;
8704 *vert
= m_colLabelVertAlign
;
8707 int wxGrid::GetColLabelTextOrientation()
8709 return m_colLabelTextOrientation
;
8712 wxString
wxGrid::GetRowLabelValue( int row
)
8716 return m_table
->GetRowLabelValue( row
);
8726 wxString
wxGrid::GetColLabelValue( int col
)
8730 return m_table
->GetColLabelValue( col
);
8741 void wxGrid::SetRowLabelSize( int width
)
8743 width
= wxMax( width
, 0 );
8744 if ( width
!= m_rowLabelWidth
)
8748 m_rowLabelWin
->Show( false );
8749 m_cornerLabelWin
->Show( false );
8751 else if ( m_rowLabelWidth
== 0 )
8753 m_rowLabelWin
->Show( true );
8754 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8757 m_rowLabelWidth
= width
;
8759 wxScrolledWindow::Refresh( true );
8764 void wxGrid::SetColLabelSize( int height
)
8766 height
= wxMax( height
, 0 );
8767 if ( height
!= m_colLabelHeight
)
8771 m_colLabelWin
->Show( false );
8772 m_cornerLabelWin
->Show( false );
8774 else if ( m_colLabelHeight
== 0 )
8776 m_colLabelWin
->Show( true );
8777 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8780 m_colLabelHeight
= height
;
8782 wxScrolledWindow::Refresh( true );
8787 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8789 if ( m_labelBackgroundColour
!= colour
)
8791 m_labelBackgroundColour
= colour
;
8792 m_rowLabelWin
->SetBackgroundColour( colour
);
8793 m_colLabelWin
->SetBackgroundColour( colour
);
8794 m_cornerLabelWin
->SetBackgroundColour( colour
);
8796 if ( !GetBatchCount() )
8798 m_rowLabelWin
->Refresh();
8799 m_colLabelWin
->Refresh();
8800 m_cornerLabelWin
->Refresh();
8805 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8807 if ( m_labelTextColour
!= colour
)
8809 m_labelTextColour
= colour
;
8810 if ( !GetBatchCount() )
8812 m_rowLabelWin
->Refresh();
8813 m_colLabelWin
->Refresh();
8818 void wxGrid::SetLabelFont( const wxFont
& font
)
8821 if ( !GetBatchCount() )
8823 m_rowLabelWin
->Refresh();
8824 m_colLabelWin
->Refresh();
8828 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8830 // allow old (incorrect) defs to be used
8833 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8834 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8835 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8840 case wxTOP
: vert
= wxALIGN_TOP
; break;
8841 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8842 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8845 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8847 m_rowLabelHorizAlign
= horiz
;
8850 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8852 m_rowLabelVertAlign
= vert
;
8855 if ( !GetBatchCount() )
8857 m_rowLabelWin
->Refresh();
8861 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8863 // allow old (incorrect) defs to be used
8866 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8867 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8868 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8873 case wxTOP
: vert
= wxALIGN_TOP
; break;
8874 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8875 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8878 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8880 m_colLabelHorizAlign
= horiz
;
8883 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8885 m_colLabelVertAlign
= vert
;
8888 if ( !GetBatchCount() )
8890 m_colLabelWin
->Refresh();
8894 // Note: under MSW, the default column label font must be changed because it
8895 // does not support vertical printing
8897 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8898 // pGrid->SetLabelFont(font);
8899 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8901 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8903 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8905 m_colLabelTextOrientation
= textOrientation
;
8908 if ( !GetBatchCount() )
8910 m_colLabelWin
->Refresh();
8914 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8918 m_table
->SetRowLabelValue( row
, s
);
8919 if ( !GetBatchCount() )
8921 wxRect rect
= CellToRect( row
, 0);
8922 if ( rect
.height
> 0 )
8924 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8926 rect
.width
= m_rowLabelWidth
;
8927 m_rowLabelWin
->Refresh( true, &rect
);
8933 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8937 m_table
->SetColLabelValue( col
, s
);
8938 if ( !GetBatchCount() )
8940 wxRect rect
= CellToRect( 0, col
);
8941 if ( rect
.width
> 0 )
8943 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8945 rect
.height
= m_colLabelHeight
;
8946 m_colLabelWin
->Refresh( true, &rect
);
8952 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8954 if ( m_gridLineColour
!= colour
)
8956 m_gridLineColour
= colour
;
8958 wxClientDC
dc( m_gridWin
);
8960 DrawAllGridLines( dc
, wxRegion() );
8965 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8967 if ( m_cellHighlightColour
!= colour
)
8969 m_cellHighlightColour
= colour
;
8971 wxClientDC
dc( m_gridWin
);
8973 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8974 DrawCellHighlight(dc
, attr
);
8979 void wxGrid::SetCellHighlightPenWidth(int width
)
8981 if (m_cellHighlightPenWidth
!= width
)
8983 m_cellHighlightPenWidth
= width
;
8985 // Just redrawing the cell highlight is not enough since that won't
8986 // make any visible change if the the thickness is getting smaller.
8987 int row
= m_currentCellCoords
.GetRow();
8988 int col
= m_currentCellCoords
.GetCol();
8989 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8991 wxRect rect
= CellToRect(row
, col
);
8992 m_gridWin
->Refresh(true, &rect
);
8996 void wxGrid::SetCellHighlightROPenWidth(int width
)
8998 if (m_cellHighlightROPenWidth
!= width
)
9000 m_cellHighlightROPenWidth
= width
;
9002 // Just redrawing the cell highlight is not enough since that won't
9003 // make any visible change if the the thickness is getting smaller.
9004 int row
= m_currentCellCoords
.GetRow();
9005 int col
= m_currentCellCoords
.GetCol();
9006 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9008 wxRect rect
= CellToRect(row
, col
);
9009 m_gridWin
->Refresh(true, &rect
);
9013 void wxGrid::EnableGridLines( bool enable
)
9015 if ( enable
!= m_gridLinesEnabled
)
9017 m_gridLinesEnabled
= enable
;
9019 if ( !GetBatchCount() )
9023 wxClientDC
dc( m_gridWin
);
9025 DrawAllGridLines( dc
, wxRegion() );
9029 m_gridWin
->Refresh();
9036 int wxGrid::GetDefaultRowSize()
9038 return m_defaultRowHeight
;
9041 int wxGrid::GetRowSize( int row
)
9043 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9045 return GetRowHeight(row
);
9048 int wxGrid::GetDefaultColSize()
9050 return m_defaultColWidth
;
9053 int wxGrid::GetColSize( int col
)
9055 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9057 return GetColWidth(col
);
9060 // ============================================================================
9061 // access to the grid attributes: each of them has a default value in the grid
9062 // itself and may be overidden on a per-cell basis
9063 // ============================================================================
9065 // ----------------------------------------------------------------------------
9066 // setting default attributes
9067 // ----------------------------------------------------------------------------
9069 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9071 m_defaultCellAttr
->SetBackgroundColour(col
);
9073 m_gridWin
->SetBackgroundColour(col
);
9077 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9079 m_defaultCellAttr
->SetTextColour(col
);
9082 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9084 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9087 void wxGrid::SetDefaultCellOverflow( bool allow
)
9089 m_defaultCellAttr
->SetOverflow(allow
);
9092 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9094 m_defaultCellAttr
->SetFont(font
);
9098 // For editors and renderers the type registry takes precedence over the
9099 // default attr, so we need to register the new editor/renderer for the string
9100 // data type in order to make setting a default editor/renderer appear to
9103 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9105 RegisterDataType(wxGRID_VALUE_STRING
,
9107 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9110 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9112 RegisterDataType(wxGRID_VALUE_STRING
,
9113 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9117 // ----------------------------------------------------------------------------
9118 // access to the default attrbiutes
9119 // ----------------------------------------------------------------------------
9121 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9123 return m_defaultCellAttr
->GetBackgroundColour();
9126 wxColour
wxGrid::GetDefaultCellTextColour()
9128 return m_defaultCellAttr
->GetTextColour();
9131 wxFont
wxGrid::GetDefaultCellFont()
9133 return m_defaultCellAttr
->GetFont();
9136 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9138 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9141 bool wxGrid::GetDefaultCellOverflow()
9143 return m_defaultCellAttr
->GetOverflow();
9146 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9148 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9151 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9153 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9156 // ----------------------------------------------------------------------------
9157 // access to cell attributes
9158 // ----------------------------------------------------------------------------
9160 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9162 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9163 wxColour colour
= attr
->GetBackgroundColour();
9168 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9170 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9171 wxColour colour
= attr
->GetTextColour();
9176 wxFont
wxGrid::GetCellFont( int row
, int col
)
9178 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9179 wxFont font
= attr
->GetFont();
9184 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9186 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9187 attr
->GetAlignment(horiz
, vert
);
9191 bool wxGrid::GetCellOverflow( int row
, int col
)
9193 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9194 bool allow
= attr
->GetOverflow();
9200 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9202 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9203 attr
->GetSize( num_rows
, num_cols
);
9207 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9209 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9210 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9216 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9218 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9219 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9225 bool wxGrid::IsReadOnly(int row
, int col
) const
9227 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9228 bool isReadOnly
= attr
->IsReadOnly();
9234 // ----------------------------------------------------------------------------
9235 // attribute support: cache, automatic provider creation, ...
9236 // ----------------------------------------------------------------------------
9238 bool wxGrid::CanHaveAttributes()
9245 return m_table
->CanHaveAttributes();
9248 void wxGrid::ClearAttrCache()
9250 if ( m_attrCache
.row
!= -1 )
9252 wxSafeDecRef(m_attrCache
.attr
);
9253 m_attrCache
.attr
= NULL
;
9254 m_attrCache
.row
= -1;
9258 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9262 wxGrid
*self
= (wxGrid
*)this; // const_cast
9264 self
->ClearAttrCache();
9265 self
->m_attrCache
.row
= row
;
9266 self
->m_attrCache
.col
= col
;
9267 self
->m_attrCache
.attr
= attr
;
9272 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9274 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9276 *attr
= m_attrCache
.attr
;
9277 wxSafeIncRef(m_attrCache
.attr
);
9279 #ifdef DEBUG_ATTR_CACHE
9280 gs_nAttrCacheHits
++;
9287 #ifdef DEBUG_ATTR_CACHE
9288 gs_nAttrCacheMisses
++;
9295 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9297 wxGridCellAttr
*attr
= NULL
;
9298 // Additional test to avoid looking at the cache e.g. for
9299 // wxNoCellCoords, as this will confuse memory management.
9302 if ( !LookupAttr(row
, col
, &attr
) )
9304 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9305 : (wxGridCellAttr
*)NULL
;
9306 CacheAttr(row
, col
, attr
);
9312 attr
->SetDefAttr(m_defaultCellAttr
);
9316 attr
= m_defaultCellAttr
;
9323 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9325 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9326 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9328 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9329 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9331 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9334 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9336 // artificially inc the ref count to match DecRef() in caller
9338 m_table
->SetAttr(attr
, row
, col
);
9344 // ----------------------------------------------------------------------------
9345 // setting column attributes (wrappers around SetColAttr)
9346 // ----------------------------------------------------------------------------
9348 void wxGrid::SetColFormatBool(int col
)
9350 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9353 void wxGrid::SetColFormatNumber(int col
)
9355 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9358 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9360 wxString typeName
= wxGRID_VALUE_FLOAT
;
9361 if ( (width
!= -1) || (precision
!= -1) )
9363 typeName
<< _T(':') << width
<< _T(',') << precision
;
9366 SetColFormatCustom(col
, typeName
);
9369 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9371 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9373 attr
= new wxGridCellAttr
;
9374 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9375 attr
->SetRenderer(renderer
);
9377 SetColAttr(col
, attr
);
9381 // ----------------------------------------------------------------------------
9382 // setting cell attributes: this is forwarded to the table
9383 // ----------------------------------------------------------------------------
9385 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9387 if ( CanHaveAttributes() )
9389 m_table
->SetAttr(attr
, row
, col
);
9398 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9400 if ( CanHaveAttributes() )
9402 m_table
->SetRowAttr(attr
, row
);
9411 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9413 if ( CanHaveAttributes() )
9415 m_table
->SetColAttr(attr
, col
);
9424 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9426 if ( CanHaveAttributes() )
9428 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9429 attr
->SetBackgroundColour(colour
);
9434 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9436 if ( CanHaveAttributes() )
9438 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9439 attr
->SetTextColour(colour
);
9444 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9446 if ( CanHaveAttributes() )
9448 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9449 attr
->SetFont(font
);
9454 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9456 if ( CanHaveAttributes() )
9458 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9459 attr
->SetAlignment(horiz
, vert
);
9464 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9466 if ( CanHaveAttributes() )
9468 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9469 attr
->SetOverflow(allow
);
9474 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9476 if ( CanHaveAttributes() )
9478 int cell_rows
, cell_cols
;
9480 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9481 attr
->GetSize(&cell_rows
, &cell_cols
);
9482 attr
->SetSize(num_rows
, num_cols
);
9485 // Cannot set the size of a cell to 0 or negative values
9486 // While it is perfectly legal to do that, this function cannot
9487 // handle all the possibilies, do it by hand by getting the CellAttr.
9488 // You can only set the size of a cell to 1,1 or greater with this fn
9489 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9490 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9491 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9492 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9494 // if this was already a multicell then "turn off" the other cells first
9495 if ((cell_rows
> 1) || (cell_rows
> 1))
9498 for (j
=row
; j
<row
+cell_rows
; j
++)
9500 for (i
=col
; i
<col
+cell_cols
; i
++)
9502 if ((i
!= col
) || (j
!= row
))
9504 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9505 attr_stub
->SetSize( 1, 1 );
9506 attr_stub
->DecRef();
9512 // mark the cells that will be covered by this cell to
9513 // negative or zero values to point back at this cell
9514 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9517 for (j
=row
; j
<row
+num_rows
; j
++)
9519 for (i
=col
; i
<col
+num_cols
; i
++)
9521 if ((i
!= col
) || (j
!= row
))
9523 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9524 attr_stub
->SetSize( row
-j
, col
-i
);
9525 attr_stub
->DecRef();
9533 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9535 if ( CanHaveAttributes() )
9537 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9538 attr
->SetRenderer(renderer
);
9543 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9545 if ( CanHaveAttributes() )
9547 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9548 attr
->SetEditor(editor
);
9553 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9555 if ( CanHaveAttributes() )
9557 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9558 attr
->SetReadOnly(isReadOnly
);
9563 // ----------------------------------------------------------------------------
9564 // Data type registration
9565 // ----------------------------------------------------------------------------
9567 void wxGrid::RegisterDataType(const wxString
& typeName
,
9568 wxGridCellRenderer
* renderer
,
9569 wxGridCellEditor
* editor
)
9571 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9575 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9577 wxString typeName
= m_table
->GetTypeName(row
, col
);
9578 return GetDefaultEditorForType(typeName
);
9581 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9583 wxString typeName
= m_table
->GetTypeName(row
, col
);
9584 return GetDefaultRendererForType(typeName
);
9588 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9590 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9591 if ( index
== wxNOT_FOUND
)
9595 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9596 wxFAIL_MSG(errStr
.c_str());
9601 return m_typeRegistry
->GetEditor(index
);
9605 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9607 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9608 if ( index
== wxNOT_FOUND
)
9612 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9613 wxFAIL_MSG(errStr
.c_str());
9618 return m_typeRegistry
->GetRenderer(index
);
9622 // ----------------------------------------------------------------------------
9624 // ----------------------------------------------------------------------------
9626 void wxGrid::EnableDragRowSize( bool enable
)
9628 m_canDragRowSize
= enable
;
9631 void wxGrid::EnableDragColSize( bool enable
)
9633 m_canDragColSize
= enable
;
9636 void wxGrid::EnableDragGridSize( bool enable
)
9638 m_canDragGridSize
= enable
;
9641 void wxGrid::EnableDragCell( bool enable
)
9643 m_canDragCell
= enable
;
9646 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9648 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9650 if ( resizeExistingRows
)
9652 // since we are resizing all rows to the default row size,
9653 // we can simply clear the row heights and row bottoms
9654 // arrays (which also allows us to take advantage of
9655 // some speed optimisations)
9656 m_rowHeights
.Empty();
9657 m_rowBottoms
.Empty();
9658 if ( !GetBatchCount() )
9663 void wxGrid::SetRowSize( int row
, int height
)
9665 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9667 // See comment in SetColSize
9668 if ( height
< GetRowMinimalAcceptableHeight())
9671 if ( m_rowHeights
.IsEmpty() )
9673 // need to really create the array
9677 int h
= wxMax( 0, height
);
9678 int diff
= h
- m_rowHeights
[row
];
9680 m_rowHeights
[row
] = h
;
9682 for ( i
= row
; i
< m_numRows
; i
++ )
9684 m_rowBottoms
[i
] += diff
;
9686 if ( !GetBatchCount() )
9690 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9692 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9694 if ( resizeExistingCols
)
9696 // since we are resizing all columns to the default column size,
9697 // we can simply clear the col widths and col rights
9698 // arrays (which also allows us to take advantage of
9699 // some speed optimisations)
9700 m_colWidths
.Empty();
9701 m_colRights
.Empty();
9702 if ( !GetBatchCount() )
9707 void wxGrid::SetColSize( int col
, int width
)
9709 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9711 // should we check that it's bigger than GetColMinimalWidth(col) here?
9713 // No, because it is reasonable to assume the library user know's
9714 // what he is doing. However whe should test against the weaker
9715 // constariant of minimalAcceptableWidth, as this breaks rendering
9717 // This test then fixes sf.net bug #645734
9719 if ( width
< GetColMinimalAcceptableWidth())
9722 if ( m_colWidths
.IsEmpty() )
9724 // need to really create the array
9728 // if < 0 calc new width from label
9732 wxArrayString lines
;
9733 wxClientDC
dc(m_colLabelWin
);
9734 dc
.SetFont(GetLabelFont());
9735 StringToLines(GetColLabelValue(col
), lines
);
9736 GetTextBoxSize(dc
, lines
, &w
, &h
);
9739 int w
= wxMax( 0, width
);
9740 int diff
= w
- m_colWidths
[col
];
9741 m_colWidths
[col
] = w
;
9744 for ( i
= col
; i
< m_numCols
; i
++ )
9746 m_colRights
[i
] += diff
;
9748 if ( !GetBatchCount() )
9752 void wxGrid::SetColMinimalWidth( int col
, int width
)
9754 if (width
> GetColMinimalAcceptableWidth())
9756 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9757 m_colMinWidths
[key
] = width
;
9761 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9763 if (width
> GetRowMinimalAcceptableHeight())
9765 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9766 m_rowMinHeights
[key
] = width
;
9770 int wxGrid::GetColMinimalWidth(int col
) const
9772 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9773 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9774 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9777 int wxGrid::GetRowMinimalHeight(int row
) const
9779 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9780 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9781 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9784 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9786 // We do allow a width of 0 since this gives us
9787 // an easy way to temporarily hidding columns.
9790 m_minAcceptableColWidth
= width
;
9793 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9795 // We do allow a height of 0 since this gives us
9796 // an easy way to temporarily hidding rows.
9799 m_minAcceptableRowHeight
= height
;
9802 int wxGrid::GetColMinimalAcceptableWidth() const
9804 return m_minAcceptableColWidth
;
9807 int wxGrid::GetRowMinimalAcceptableHeight() const
9809 return m_minAcceptableRowHeight
;
9812 // ----------------------------------------------------------------------------
9814 // ----------------------------------------------------------------------------
9816 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9818 wxClientDC
dc(m_gridWin
);
9820 //Cancel editting of cell
9821 HideCellEditControl();
9822 SaveEditControlValue();
9824 // init both of them to avoid compiler warnings, even if weo nly need one
9832 wxCoord extent
, extentMax
= 0;
9833 int max
= column
? m_numRows
: m_numCols
;
9834 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9841 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9842 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9845 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9846 extent
= column
? size
.x
: size
.y
;
9847 if ( extent
> extentMax
)
9858 // now also compare with the column label extent
9860 dc
.SetFont( GetLabelFont() );
9864 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9865 if ( GetColLabelTextOrientation() == wxVERTICAL
)
9869 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9871 extent
= column
? w
: h
;
9872 if ( extent
> extentMax
)
9879 // empty column - give default extent (notice that if extentMax is less
9880 // than default extent but != 0, it's ok)
9881 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9887 // leave some space around text
9898 SetColSize(col
, extentMax
);
9899 if ( !GetBatchCount() )
9902 m_gridWin
->GetClientSize( &cw
, &ch
);
9903 wxRect
rect ( CellToRect( 0, col
) );
9905 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9906 rect
.width
= cw
- rect
.x
;
9907 rect
.height
= m_colLabelHeight
;
9908 m_colLabelWin
->Refresh( true, &rect
);
9913 SetRowSize(row
, extentMax
);
9914 if ( !GetBatchCount() )
9917 m_gridWin
->GetClientSize( &cw
, &ch
);
9918 wxRect
rect ( CellToRect( row
, 0 ) );
9920 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9921 rect
.width
= m_rowLabelWidth
;
9922 rect
.height
= ch
- rect
.y
;
9923 m_rowLabelWin
->Refresh( true, &rect
);
9929 SetColMinimalWidth(col
, extentMax
);
9931 SetRowMinimalHeight(row
, extentMax
);
9935 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9937 int width
= m_rowLabelWidth
;
9942 for ( int col
= 0; col
< m_numCols
; col
++ )
9946 AutoSizeColumn(col
, setAsMin
);
9949 width
+= GetColWidth(col
);
9958 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9960 int height
= m_colLabelHeight
;
9965 for ( int row
= 0; row
< m_numRows
; row
++ )
9969 AutoSizeRow(row
, setAsMin
);
9972 height
+= GetRowHeight(row
);
9981 void wxGrid::AutoSize()
9985 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9987 // round up the size to a multiple of scroll step - this ensures that we
9988 // won't get the scrollbars if we're sized exactly to this width
9989 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9991 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
9992 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
9994 // distribute the extra space between the columns/rows to avoid having
9995 // extra white space
9997 // Remove the extra m_extraWidth + 1 added above
9998 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9999 if ( diff
&& m_numCols
)
10001 // try to resize the columns uniformly
10002 wxCoord diffPerCol
= diff
/ m_numCols
;
10005 for ( int col
= 0; col
< m_numCols
; col
++ )
10007 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10011 // add remaining amount to the last columns
10012 diff
-= diffPerCol
* m_numCols
;
10015 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10017 SetColSize(col
, GetColWidth(col
) + 1);
10023 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10024 if ( diff
&& m_numRows
)
10026 // try to resize the columns uniformly
10027 wxCoord diffPerRow
= diff
/ m_numRows
;
10030 for ( int row
= 0; row
< m_numRows
; row
++ )
10032 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10036 // add remaining amount to the last rows
10037 diff
-= diffPerRow
* m_numRows
;
10040 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10042 SetRowSize(row
, GetRowHeight(row
) + 1);
10049 SetClientSize(sizeFit
);
10052 void wxGrid::AutoSizeRowLabelSize( int row
)
10054 wxArrayString lines
;
10057 // Hide the edit control, so it
10058 // won't interfere with drag-shrinking.
10059 if ( IsCellEditControlShown() )
10061 HideCellEditControl();
10062 SaveEditControlValue();
10065 // autosize row height depending on label text
10066 StringToLines( GetRowLabelValue( row
), lines
);
10067 wxClientDC
dc( m_rowLabelWin
);
10068 GetTextBoxSize( dc
, lines
, &w
, &h
);
10069 if ( h
< m_defaultRowHeight
)
10070 h
= m_defaultRowHeight
;
10071 SetRowSize(row
, h
);
10075 void wxGrid::AutoSizeColLabelSize( int col
)
10077 wxArrayString lines
;
10080 // Hide the edit control, so it
10081 // won't interfer with drag-shrinking.
10082 if ( IsCellEditControlShown() )
10084 HideCellEditControl();
10085 SaveEditControlValue();
10088 // autosize column width depending on label text
10089 StringToLines( GetColLabelValue( col
), lines
);
10090 wxClientDC
dc( m_colLabelWin
);
10091 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10092 GetTextBoxSize( dc
, lines
, &w
, &h
);
10094 GetTextBoxSize( dc
, lines
, &h
, &w
);
10095 if ( w
< m_defaultColWidth
)
10096 w
= m_defaultColWidth
;
10097 SetColSize(col
, w
);
10101 wxSize
wxGrid::DoGetBestSize() const
10103 // don't set sizes, only calculate them
10104 wxGrid
*self
= (wxGrid
*)this; // const_cast
10107 width
= self
->SetOrCalcColumnSizes(true);
10108 height
= self
->SetOrCalcRowSizes(true);
10115 // Round up to a multiple the scroll rate
10116 // NOTE: this still doesn't get rid of the scrollbars;
10117 // is there any magic incantation for that?
10119 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10121 width
+= 1 + xpu
- (width
% xpu
);
10123 height
+= 1 + ypu
- (height
% ypu
);
10125 // limit to 1/4 of the screen size
10126 int maxwidth
, maxheight
;
10127 wxDisplaySize( &maxwidth
, &maxheight
);
10130 if ( width
> maxwidth
)
10132 if ( height
> maxheight
)
10133 height
= maxheight
;
10135 wxSize
best(width
, height
);
10136 // NOTE: This size should be cached, but first we need to add calls to
10137 // InvalidateBestSize everywhere that could change the results of this
10139 // CacheBestSize(size);
10148 wxPen
& wxGrid::GetDividerPen() const
10153 // ----------------------------------------------------------------------------
10154 // cell value accessor functions
10155 // ----------------------------------------------------------------------------
10157 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10161 m_table
->SetValue( row
, col
, s
);
10162 if ( !GetBatchCount() )
10165 wxRect
rect( CellToRect( row
, col
) );
10167 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10168 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10169 m_gridWin
->Refresh( false, &rect
);
10172 if ( m_currentCellCoords
.GetRow() == row
&&
10173 m_currentCellCoords
.GetCol() == col
&&
10174 IsCellEditControlShown())
10175 // Note: If we are using IsCellEditControlEnabled,
10176 // this interacts badly with calling SetCellValue from
10177 // an EVT_GRID_CELL_CHANGE handler.
10179 HideCellEditControl();
10180 ShowCellEditControl(); // will reread data from table
10187 // ------ Block, row and col selection
10190 void wxGrid::SelectRow( int row
, bool addToSelected
)
10192 if ( IsSelection() && !addToSelected
)
10196 m_selection
->SelectRow( row
, false, addToSelected
);
10199 void wxGrid::SelectCol( int col
, bool addToSelected
)
10201 if ( IsSelection() && !addToSelected
)
10205 m_selection
->SelectCol( col
, false, addToSelected
);
10208 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10209 bool addToSelected
)
10211 if ( IsSelection() && !addToSelected
)
10215 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10216 false, addToSelected
);
10219 void wxGrid::SelectAll()
10221 if ( m_numRows
> 0 && m_numCols
> 0 )
10224 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10229 // ------ Cell, row and col deselection
10232 void wxGrid::DeselectRow( int row
)
10234 if ( !m_selection
)
10237 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10239 if ( m_selection
->IsInSelection(row
, 0 ) )
10240 m_selection
->ToggleCellSelection( row
, 0);
10244 int nCols
= GetNumberCols();
10245 for ( int i
= 0; i
< nCols
; i
++ )
10247 if ( m_selection
->IsInSelection(row
, i
) )
10248 m_selection
->ToggleCellSelection( row
, i
);
10253 void wxGrid::DeselectCol( int col
)
10255 if ( !m_selection
)
10258 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10260 if ( m_selection
->IsInSelection(0, col
) )
10261 m_selection
->ToggleCellSelection( 0, col
);
10265 int nRows
= GetNumberRows();
10266 for ( int i
= 0; i
< nRows
; i
++ )
10268 if ( m_selection
->IsInSelection(i
, col
) )
10269 m_selection
->ToggleCellSelection(i
, col
);
10274 void wxGrid::DeselectCell( int row
, int col
)
10276 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10277 m_selection
->ToggleCellSelection(row
, col
);
10280 bool wxGrid::IsSelection()
10282 return ( m_selection
&& (m_selection
->IsSelection() ||
10283 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10284 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10287 bool wxGrid::IsInSelection( int row
, int col
) const
10289 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10290 ( row
>= m_selectingTopLeft
.GetRow() &&
10291 col
>= m_selectingTopLeft
.GetCol() &&
10292 row
<= m_selectingBottomRight
.GetRow() &&
10293 col
<= m_selectingBottomRight
.GetCol() )) );
10296 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10300 wxGridCellCoordsArray a
;
10304 return m_selection
->m_cellSelection
;
10307 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10311 wxGridCellCoordsArray a
;
10315 return m_selection
->m_blockSelectionTopLeft
;
10318 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10322 wxGridCellCoordsArray a
;
10326 return m_selection
->m_blockSelectionBottomRight
;
10329 wxArrayInt
wxGrid::GetSelectedRows() const
10337 return m_selection
->m_rowSelection
;
10340 wxArrayInt
wxGrid::GetSelectedCols() const
10348 return m_selection
->m_colSelection
;
10351 void wxGrid::ClearSelection()
10353 m_selectingTopLeft
= wxGridNoCellCoords
;
10354 m_selectingBottomRight
= wxGridNoCellCoords
;
10356 m_selection
->ClearSelection();
10360 // This function returns the rectangle that encloses the given block
10361 // in device coords clipped to the client size of the grid window.
10363 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10364 const wxGridCellCoords
&bottomRight
)
10366 wxRect
rect( wxGridNoCellRect
);
10369 cellRect
= CellToRect( topLeft
);
10370 if ( cellRect
!= wxGridNoCellRect
)
10376 rect
= wxRect(0,0,0,0);
10379 cellRect
= CellToRect( bottomRight
);
10380 if ( cellRect
!= wxGridNoCellRect
)
10386 return wxGridNoCellRect
;
10390 int left
= rect
.GetLeft();
10391 int top
= rect
.GetTop();
10392 int right
= rect
.GetRight();
10393 int bottom
= rect
.GetBottom();
10395 int leftCol
= topLeft
.GetCol();
10396 int topRow
= topLeft
.GetRow();
10397 int rightCol
= bottomRight
.GetCol();
10398 int bottomRow
= bottomRight
.GetRow();
10406 leftCol
= rightCol
;
10416 topRow
= bottomRow
;
10421 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10423 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10425 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10427 cellRect
= CellToRect( j
, i
);
10429 if (cellRect
.x
< left
)
10431 if (cellRect
.y
< top
)
10433 if (cellRect
.x
+ cellRect
.width
> right
)
10434 right
= cellRect
.x
+ cellRect
.width
;
10435 if (cellRect
.y
+ cellRect
.height
> bottom
)
10436 bottom
= cellRect
.y
+ cellRect
.height
;
10440 i
= rightCol
; // jump over inner cells.
10445 // convert to scrolled coords
10447 CalcScrolledPosition( left
, top
, &left
, &top
);
10448 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10451 m_gridWin
->GetClientSize( &cw
, &ch
);
10453 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10454 return wxRect(0,0,0,0);
10456 rect
.SetLeft( wxMax(0, left
) );
10457 rect
.SetTop( wxMax(0, top
) );
10458 rect
.SetRight( wxMin(cw
, right
) );
10459 rect
.SetBottom( wxMin(ch
, bottom
) );
10465 // ------ Grid event classes
10468 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10470 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10471 int row
, int col
, int x
, int y
, bool sel
,
10472 bool control
, bool shift
, bool alt
, bool meta
)
10473 : wxNotifyEvent( type
, id
)
10480 m_control
= control
;
10485 SetEventObject(obj
);
10489 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10491 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10492 int rowOrCol
, int x
, int y
,
10493 bool control
, bool shift
, bool alt
, bool meta
)
10494 : wxNotifyEvent( type
, id
)
10496 m_rowOrCol
= rowOrCol
;
10499 m_control
= control
;
10504 SetEventObject(obj
);
10508 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10510 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10511 const wxGridCellCoords
& topLeft
,
10512 const wxGridCellCoords
& bottomRight
,
10513 bool sel
, bool control
,
10514 bool shift
, bool alt
, bool meta
)
10515 : wxNotifyEvent( type
, id
)
10517 m_topLeft
= topLeft
;
10518 m_bottomRight
= bottomRight
;
10520 m_control
= control
;
10525 SetEventObject(obj
);
10529 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10531 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10532 wxObject
* obj
, int row
,
10533 int col
, wxControl
* ctrl
)
10534 : wxCommandEvent(type
, id
)
10536 SetEventObject(obj
);
10542 #endif // wxUSE_GRID