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();
1492 // When dropping down the menu, a kill focus event
1493 // happens after this point, so we can't reset the flag yet.
1494 #if !defined(__WXGTK20__)
1495 evtHandler
->SetInSetFocus(false);
1500 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1503 wxString value
= Combo()->GetValue();
1504 if ( value
== m_startValue
)
1507 grid
->GetTable()->SetValue(row
, col
, value
);
1512 void wxGridCellChoiceEditor::Reset()
1514 Combo()->SetValue(m_startValue
);
1515 Combo()->SetInsertionPointEnd();
1518 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1528 wxStringTokenizer
tk(params
, _T(','));
1529 while ( tk
.HasMoreTokens() )
1531 m_choices
.Add(tk
.GetNextToken());
1535 // return the value in the text control
1536 wxString
wxGridCellChoiceEditor::GetValue() const
1538 return Combo()->GetValue();
1541 #endif // wxUSE_COMBOBOX
1543 // ----------------------------------------------------------------------------
1544 // wxGridCellEditorEvtHandler
1545 // ----------------------------------------------------------------------------
1547 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1549 // Don't disable the cell if we're just starting to edit it
1554 m_grid
->DisableCellEditControl();
1559 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1561 switch ( event
.GetKeyCode() )
1565 m_grid
->DisableCellEditControl();
1569 m_grid
->GetEventHandler()->ProcessEvent( event
);
1573 case WXK_NUMPAD_ENTER
:
1574 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1575 m_editor
->HandleReturn(event
);
1583 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1585 switch ( event
.GetKeyCode() )
1590 case WXK_NUMPAD_ENTER
:
1598 // ----------------------------------------------------------------------------
1599 // wxGridCellWorker is an (almost) empty common base class for
1600 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1601 // ----------------------------------------------------------------------------
1603 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1608 wxGridCellWorker::~wxGridCellWorker()
1612 // ============================================================================
1614 // ============================================================================
1616 // ----------------------------------------------------------------------------
1617 // wxGridCellRenderer
1618 // ----------------------------------------------------------------------------
1620 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1621 wxGridCellAttr
& attr
,
1624 int WXUNUSED(row
), int WXUNUSED(col
),
1627 dc
.SetBackgroundMode( wxSOLID
);
1629 // grey out fields if the grid is disabled
1630 if ( grid
.IsEnabled() )
1634 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1638 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1643 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1646 dc
.SetPen( *wxTRANSPARENT_PEN
);
1647 dc
.DrawRectangle(rect
);
1650 // ----------------------------------------------------------------------------
1651 // wxGridCellStringRenderer
1652 // ----------------------------------------------------------------------------
1654 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1655 const wxGridCellAttr
& attr
,
1659 dc
.SetBackgroundMode( wxTRANSPARENT
);
1661 // TODO some special colours for attr.IsReadOnly() case?
1663 // different coloured text when the grid is disabled
1664 if ( grid
.IsEnabled() )
1668 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1669 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1673 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1674 dc
.SetTextForeground( attr
.GetTextColour() );
1679 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1680 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1683 dc
.SetFont( attr
.GetFont() );
1686 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1688 const wxString
& text
)
1690 wxCoord x
= 0, y
= 0, max_x
= 0;
1691 dc
.SetFont(attr
.GetFont());
1692 wxStringTokenizer
tk(text
, _T('\n'));
1693 while ( tk
.HasMoreTokens() )
1695 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1696 max_x
= wxMax(max_x
, x
);
1699 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1701 return wxSize(max_x
, y
);
1704 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1705 wxGridCellAttr
& attr
,
1709 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1712 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1713 wxGridCellAttr
& attr
,
1715 const wxRect
& rectCell
,
1719 wxRect rect
= rectCell
;
1722 // erase only this cells background, overflow cells should have been erased
1723 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1726 attr
.GetAlignment(&hAlign
, &vAlign
);
1728 int overflowCols
= 0;
1730 if (attr
.GetOverflow())
1732 int cols
= grid
.GetNumberCols();
1733 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1734 int cell_rows
, cell_cols
;
1735 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1736 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1738 int i
, c_cols
, c_rows
;
1739 for (i
= col
+cell_cols
; i
< cols
; i
++)
1741 bool is_empty
= true;
1742 for (int j
=row
; j
<row
+cell_rows
; j
++)
1744 // check w/ anchor cell for multicell block
1745 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1746 if (c_rows
> 0) c_rows
= 0;
1747 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1754 rect
.width
+= grid
.GetColSize(i
);
1760 if (rect
.width
>= best_width
)
1763 overflowCols
= i
- col
- cell_cols
+ 1;
1764 if (overflowCols
>= cols
)
1765 overflowCols
= cols
- 1;
1768 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1770 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1772 clip
.x
+= rectCell
.width
;
1773 // draw each overflow cell individually
1774 int col_end
= col
+cell_cols
+overflowCols
;
1775 if (col_end
>= grid
.GetNumberCols())
1776 col_end
= grid
.GetNumberCols() - 1;
1777 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1779 clip
.width
= grid
.GetColSize(i
) - 1;
1780 dc
.DestroyClippingRegion();
1781 dc
.SetClippingRegion(clip
);
1783 SetTextColoursAndFont(grid
, attr
, dc
,
1784 grid
.IsInSelection(row
,i
));
1786 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1787 rect
, hAlign
, vAlign
);
1788 clip
.x
+= grid
.GetColSize(i
) - 1;
1794 dc
.DestroyClippingRegion();
1798 // now we only have to draw the text
1799 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1801 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1802 rect
, hAlign
, vAlign
);
1805 // ----------------------------------------------------------------------------
1806 // wxGridCellNumberRenderer
1807 // ----------------------------------------------------------------------------
1809 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1811 wxGridTableBase
*table
= grid
.GetTable();
1813 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1815 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1819 text
= table
->GetValue(row
, col
);
1825 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1826 wxGridCellAttr
& attr
,
1828 const wxRect
& rectCell
,
1832 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1834 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1836 // draw the text right aligned by default
1838 attr
.GetAlignment(&hAlign
, &vAlign
);
1839 hAlign
= wxALIGN_RIGHT
;
1841 wxRect rect
= rectCell
;
1844 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1847 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1848 wxGridCellAttr
& attr
,
1852 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1855 // ----------------------------------------------------------------------------
1856 // wxGridCellFloatRenderer
1857 // ----------------------------------------------------------------------------
1859 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1862 SetPrecision(precision
);
1865 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1867 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1868 renderer
->m_width
= m_width
;
1869 renderer
->m_precision
= m_precision
;
1870 renderer
->m_format
= m_format
;
1875 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1877 wxGridTableBase
*table
= grid
.GetTable();
1882 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1884 val
= table
->GetValueAsDouble(row
, col
);
1889 text
= table
->GetValue(row
, col
);
1890 hasDouble
= text
.ToDouble(&val
);
1897 if ( m_width
== -1 )
1899 if ( m_precision
== -1 )
1901 // default width/precision
1902 m_format
= _T("%f");
1906 m_format
.Printf(_T("%%.%df"), m_precision
);
1909 else if ( m_precision
== -1 )
1911 // default precision
1912 m_format
.Printf(_T("%%%d.f"), m_width
);
1916 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1920 text
.Printf(m_format
, val
);
1923 //else: text already contains the string
1928 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1929 wxGridCellAttr
& attr
,
1931 const wxRect
& rectCell
,
1935 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1937 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1939 // draw the text right aligned by default
1941 attr
.GetAlignment(&hAlign
, &vAlign
);
1942 hAlign
= wxALIGN_RIGHT
;
1944 wxRect rect
= rectCell
;
1947 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1950 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1951 wxGridCellAttr
& attr
,
1955 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1958 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1962 // reset to defaults
1968 wxString tmp
= params
.BeforeFirst(_T(','));
1972 if ( tmp
.ToLong(&width
) )
1974 SetWidth((int)width
);
1978 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1981 tmp
= params
.AfterFirst(_T(','));
1985 if ( tmp
.ToLong(&precision
) )
1987 SetPrecision((int)precision
);
1991 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1998 // ----------------------------------------------------------------------------
1999 // wxGridCellBoolRenderer
2000 // ----------------------------------------------------------------------------
2002 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2004 // FIXME these checkbox size calculations are really ugly...
2006 // between checkmark and box
2007 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2009 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2010 wxGridCellAttr
& WXUNUSED(attr
),
2015 // compute it only once (no locks for MT safeness in GUI thread...)
2016 if ( !ms_sizeCheckMark
.x
)
2018 // get checkbox size
2019 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2020 wxSize size
= checkbox
->GetBestSize();
2021 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
2023 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2024 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2025 checkSize
-= size
.y
/ 2;
2030 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2033 return ms_sizeCheckMark
;
2036 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2037 wxGridCellAttr
& attr
,
2043 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2045 // draw a check mark in the centre (ignoring alignment - TODO)
2046 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2048 // don't draw outside the cell
2049 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2050 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2052 // and even leave (at least) 1 pixel margin
2053 size
.x
= size
.y
= minSize
- 2;
2056 // draw a border around checkmark
2058 attr
.GetAlignment(& hAlign
, &vAlign
);
2061 if (hAlign
== wxALIGN_CENTRE
)
2063 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2064 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2065 rectBorder
.width
= size
.x
;
2066 rectBorder
.height
= size
.y
;
2068 else if (hAlign
== wxALIGN_LEFT
)
2070 rectBorder
.x
= rect
.x
+ 2;
2071 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2072 rectBorder
.width
= size
.x
;
2073 rectBorder
.height
= size
.y
;
2075 else if (hAlign
== wxALIGN_RIGHT
)
2077 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2078 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2079 rectBorder
.width
= size
.x
;
2080 rectBorder
.height
= size
.y
;
2084 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2085 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2088 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2089 value
= !( !cellval
|| (cellval
== wxT("0")) );
2094 wxRect rectMark
= rectBorder
;
2096 // MSW DrawCheckMark() is weird (and should probably be changed...)
2097 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2101 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2104 dc
.SetTextForeground(attr
.GetTextColour());
2105 dc
.DrawCheckMark(rectMark
);
2108 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2109 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2110 dc
.DrawRectangle(rectBorder
);
2113 // ----------------------------------------------------------------------------
2115 // ----------------------------------------------------------------------------
2117 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2121 m_isReadOnly
= Unset
;
2126 m_attrkind
= wxGridCellAttr::Cell
;
2128 m_sizeRows
= m_sizeCols
= 1;
2129 m_overflow
= UnsetOverflow
;
2131 SetDefAttr(attrDefault
);
2134 wxGridCellAttr
*wxGridCellAttr::Clone() const
2136 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2138 if ( HasTextColour() )
2139 attr
->SetTextColour(GetTextColour());
2140 if ( HasBackgroundColour() )
2141 attr
->SetBackgroundColour(GetBackgroundColour());
2143 attr
->SetFont(GetFont());
2144 if ( HasAlignment() )
2145 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2147 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2151 attr
->SetRenderer(m_renderer
);
2152 m_renderer
->IncRef();
2156 attr
->SetEditor(m_editor
);
2161 attr
->SetReadOnly();
2163 attr
->SetKind( m_attrkind
);
2168 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2170 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2171 SetTextColour(mergefrom
->GetTextColour());
2172 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2173 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2174 if ( !HasFont() && mergefrom
->HasFont() )
2175 SetFont(mergefrom
->GetFont());
2176 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2179 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2180 SetAlignment(hAlign
, vAlign
);
2182 if ( !HasSize() && mergefrom
->HasSize() )
2183 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2185 // Directly access member functions as GetRender/Editor don't just return
2186 // m_renderer/m_editor
2188 // Maybe add support for merge of Render and Editor?
2189 if (!HasRenderer() && mergefrom
->HasRenderer() )
2191 m_renderer
= mergefrom
->m_renderer
;
2192 m_renderer
->IncRef();
2194 if ( !HasEditor() && mergefrom
->HasEditor() )
2196 m_editor
= mergefrom
->m_editor
;
2199 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2200 SetReadOnly(mergefrom
->IsReadOnly());
2202 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2203 SetOverflow(mergefrom
->GetOverflow());
2205 SetDefAttr(mergefrom
->m_defGridAttr
);
2208 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2210 // The size of a cell is normally 1,1
2212 // If this cell is larger (2,2) then this is the top left cell
2213 // the other cells that will be covered (lower right cells) must be
2214 // set to negative or zero values such that
2215 // row + num_rows of the covered cell points to the larger cell (this cell)
2216 // same goes for the col + num_cols.
2218 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2220 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2221 !((num_rows
<=0)&&(num_cols
>0)) ||
2222 !((num_rows
==0)&&(num_cols
==0))),
2223 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2225 m_sizeRows
= num_rows
;
2226 m_sizeCols
= num_cols
;
2229 const wxColour
& wxGridCellAttr::GetTextColour() const
2231 if (HasTextColour())
2235 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2237 return m_defGridAttr
->GetTextColour();
2241 wxFAIL_MSG(wxT("Missing default cell attribute"));
2242 return wxNullColour
;
2247 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2249 if (HasBackgroundColour())
2251 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2252 return m_defGridAttr
->GetBackgroundColour();
2255 wxFAIL_MSG(wxT("Missing default cell attribute"));
2256 return wxNullColour
;
2261 const wxFont
& wxGridCellAttr::GetFont() const
2265 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2266 return m_defGridAttr
->GetFont();
2269 wxFAIL_MSG(wxT("Missing default cell attribute"));
2275 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2284 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2285 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2288 wxFAIL_MSG(wxT("Missing default cell attribute"));
2292 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2295 *num_rows
= m_sizeRows
;
2297 *num_cols
= m_sizeCols
;
2300 // GetRenderer and GetEditor use a slightly different decision path about
2301 // which attribute to use. If a non-default attr object has one then it is
2302 // used, otherwise the default editor or renderer is fetched from the grid and
2303 // used. It should be the default for the data type of the cell. If it is
2304 // NULL (because the table has a type that the grid does not have in its
2305 // registry,) then the grid's default editor or renderer is used.
2307 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2309 wxGridCellRenderer
*renderer
;
2311 if ( m_renderer
&& this != m_defGridAttr
)
2313 // use the cells renderer if it has one
2314 renderer
= m_renderer
;
2317 else // no non default cell renderer
2319 // get default renderer for the data type
2322 // GetDefaultRendererForCell() will do IncRef() for us
2323 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2332 if (m_defGridAttr
&& this != m_defGridAttr
)
2334 // if we still don't have one then use the grid default
2335 // (no need for IncRef() here neither)
2336 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2338 else // default grid attr
2340 // use m_renderer which we had decided not to use initially
2341 renderer
= m_renderer
;
2348 // we're supposed to always find something
2349 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2354 // same as above, except for s/renderer/editor/g
2355 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2357 wxGridCellEditor
*editor
;
2359 if ( m_editor
&& this != m_defGridAttr
)
2361 // use the cells editor if it has one
2365 else // no non default cell editor
2367 // get default editor for the data type
2370 // GetDefaultEditorForCell() will do IncRef() for us
2371 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2380 if ( m_defGridAttr
&& this != m_defGridAttr
)
2382 // if we still don't have one then use the grid default
2383 // (no need for IncRef() here neither)
2384 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2386 else // default grid attr
2388 // use m_editor which we had decided not to use initially
2396 // we're supposed to always find something
2397 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2402 // ----------------------------------------------------------------------------
2403 // wxGridCellAttrData
2404 // ----------------------------------------------------------------------------
2406 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2408 int n
= FindIndex(row
, col
);
2409 if ( n
== wxNOT_FOUND
)
2411 // add the attribute
2412 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2416 // free the old attribute
2417 m_attrs
[(size_t)n
].attr
->DecRef();
2421 // change the attribute
2422 m_attrs
[(size_t)n
].attr
= attr
;
2426 // remove this attribute
2427 m_attrs
.RemoveAt((size_t)n
);
2432 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2434 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2436 int n
= FindIndex(row
, col
);
2437 if ( n
!= wxNOT_FOUND
)
2439 attr
= m_attrs
[(size_t)n
].attr
;
2446 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2448 size_t count
= m_attrs
.GetCount();
2449 for ( size_t n
= 0; n
< count
; n
++ )
2451 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2452 wxCoord row
= coords
.GetRow();
2453 if ((size_t)row
>= pos
)
2457 // If rows inserted, include row counter where necessary
2458 coords
.SetRow(row
+ numRows
);
2460 else if (numRows
< 0)
2462 // If rows deleted ...
2463 if ((size_t)row
>= pos
- numRows
)
2465 // ...either decrement row counter (if row still exists)...
2466 coords
.SetRow(row
+ numRows
);
2470 // ...or remove the attribute
2471 // No need to DecRef the attribute itself since this is
2472 // done be wxGridCellWithAttr's destructor!
2473 m_attrs
.RemoveAt(n
);
2482 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2484 size_t count
= m_attrs
.GetCount();
2485 for ( size_t n
= 0; n
< count
; n
++ )
2487 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2488 wxCoord col
= coords
.GetCol();
2489 if ( (size_t)col
>= pos
)
2493 // If rows inserted, include row counter where necessary
2494 coords
.SetCol(col
+ numCols
);
2496 else if (numCols
< 0)
2498 // If rows deleted ...
2499 if ((size_t)col
>= pos
- numCols
)
2501 // ...either decrement row counter (if row still exists)...
2502 coords
.SetCol(col
+ numCols
);
2506 // ...or remove the attribute
2507 // No need to DecRef the attribute itself since this is
2508 // done be wxGridCellWithAttr's destructor!
2509 m_attrs
.RemoveAt(n
);
2517 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2519 size_t count
= m_attrs
.GetCount();
2520 for ( size_t n
= 0; n
< count
; n
++ )
2522 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2523 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2532 // ----------------------------------------------------------------------------
2533 // wxGridRowOrColAttrData
2534 // ----------------------------------------------------------------------------
2536 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2538 size_t count
= m_attrs
.Count();
2539 for ( size_t n
= 0; n
< count
; n
++ )
2541 m_attrs
[n
]->DecRef();
2545 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2547 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2549 int n
= m_rowsOrCols
.Index(rowOrCol
);
2550 if ( n
!= wxNOT_FOUND
)
2552 attr
= m_attrs
[(size_t)n
];
2559 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2561 int i
= m_rowsOrCols
.Index(rowOrCol
);
2562 if ( i
== wxNOT_FOUND
)
2564 // add the attribute
2565 m_rowsOrCols
.Add(rowOrCol
);
2570 size_t n
= (size_t)i
;
2573 // change the attribute
2574 m_attrs
[n
]->DecRef();
2579 // remove this attribute
2580 m_attrs
[n
]->DecRef();
2581 m_rowsOrCols
.RemoveAt(n
);
2582 m_attrs
.RemoveAt(n
);
2587 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2589 size_t count
= m_attrs
.GetCount();
2590 for ( size_t n
= 0; n
< count
; n
++ )
2592 int & rowOrCol
= m_rowsOrCols
[n
];
2593 if ( (size_t)rowOrCol
>= pos
)
2595 if ( numRowsOrCols
> 0 )
2597 // If rows inserted, include row counter where necessary
2598 rowOrCol
+= numRowsOrCols
;
2600 else if ( numRowsOrCols
< 0)
2602 // If rows deleted, either decrement row counter (if row still exists)
2603 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2604 rowOrCol
+= numRowsOrCols
;
2607 m_rowsOrCols
.RemoveAt(n
);
2608 m_attrs
[n
]->DecRef();
2609 m_attrs
.RemoveAt(n
);
2618 // ----------------------------------------------------------------------------
2619 // wxGridCellAttrProvider
2620 // ----------------------------------------------------------------------------
2622 wxGridCellAttrProvider::wxGridCellAttrProvider()
2624 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2627 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2632 void wxGridCellAttrProvider::InitData()
2634 m_data
= new wxGridCellAttrProviderData
;
2637 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2638 wxGridCellAttr::wxAttrKind kind
) const
2640 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2645 case (wxGridCellAttr::Any
):
2646 // Get cached merge attributes.
2647 // Currently not used as no cache implemented as not mutable
2648 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2651 // Basically implement old version.
2652 // Also check merge cache, so we don't have to re-merge every time..
2653 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2654 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2655 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2657 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2659 // Two or more are non NULL
2660 attr
= new wxGridCellAttr
;
2661 attr
->SetKind(wxGridCellAttr::Merged
);
2663 // Order is important..
2666 attr
->MergeWith(attrcell
);
2671 attr
->MergeWith(attrcol
);
2676 attr
->MergeWith(attrrow
);
2680 // store merge attr if cache implemented
2682 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2686 // one or none is non null return it or null.
2705 case (wxGridCellAttr::Cell
):
2706 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2709 case (wxGridCellAttr::Col
):
2710 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2713 case (wxGridCellAttr::Row
):
2714 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2719 // (wxGridCellAttr::Default):
2720 // (wxGridCellAttr::Merged):
2727 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2733 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2736 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2741 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2744 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2749 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2752 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2756 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2758 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2762 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2766 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2768 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2772 // ----------------------------------------------------------------------------
2773 // wxGridTypeRegistry
2774 // ----------------------------------------------------------------------------
2776 wxGridTypeRegistry::~wxGridTypeRegistry()
2778 size_t count
= m_typeinfo
.Count();
2779 for ( size_t i
= 0; i
< count
; i
++ )
2780 delete m_typeinfo
[i
];
2784 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2785 wxGridCellRenderer
* renderer
,
2786 wxGridCellEditor
* editor
)
2788 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2790 // is it already registered?
2791 int loc
= FindRegisteredDataType(typeName
);
2792 if ( loc
!= wxNOT_FOUND
)
2794 delete m_typeinfo
[loc
];
2795 m_typeinfo
[loc
] = info
;
2799 m_typeinfo
.Add(info
);
2803 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2805 size_t count
= m_typeinfo
.GetCount();
2806 for ( size_t i
= 0; i
< count
; i
++ )
2808 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2817 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2819 int index
= FindRegisteredDataType(typeName
);
2820 if ( index
== wxNOT_FOUND
)
2822 // check whether this is one of the standard ones, in which case
2823 // register it "on the fly"
2825 if ( typeName
== wxGRID_VALUE_STRING
)
2827 RegisterDataType(wxGRID_VALUE_STRING
,
2828 new wxGridCellStringRenderer
,
2829 new wxGridCellTextEditor
);
2832 #endif // wxUSE_TEXTCTRL
2834 if ( typeName
== wxGRID_VALUE_BOOL
)
2836 RegisterDataType(wxGRID_VALUE_BOOL
,
2837 new wxGridCellBoolRenderer
,
2838 new wxGridCellBoolEditor
);
2841 #endif // wxUSE_CHECKBOX
2843 if ( typeName
== wxGRID_VALUE_NUMBER
)
2845 RegisterDataType(wxGRID_VALUE_NUMBER
,
2846 new wxGridCellNumberRenderer
,
2847 new wxGridCellNumberEditor
);
2849 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2851 RegisterDataType(wxGRID_VALUE_FLOAT
,
2852 new wxGridCellFloatRenderer
,
2853 new wxGridCellFloatEditor
);
2856 #endif // wxUSE_TEXTCTRL
2858 if ( typeName
== wxGRID_VALUE_CHOICE
)
2860 RegisterDataType(wxGRID_VALUE_CHOICE
,
2861 new wxGridCellStringRenderer
,
2862 new wxGridCellChoiceEditor
);
2865 #endif // wxUSE_COMBOBOX
2870 // we get here only if just added the entry for this type, so return
2872 index
= m_typeinfo
.GetCount() - 1;
2878 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2880 int index
= FindDataType(typeName
);
2881 if ( index
== wxNOT_FOUND
)
2883 // the first part of the typename is the "real" type, anything after ':'
2884 // are the parameters for the renderer
2885 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2886 if ( index
== wxNOT_FOUND
)
2891 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2892 wxGridCellRenderer
*rendererOld
= renderer
;
2893 renderer
= renderer
->Clone();
2894 rendererOld
->DecRef();
2896 wxGridCellEditor
*editor
= GetEditor(index
);
2897 wxGridCellEditor
*editorOld
= editor
;
2898 editor
= editor
->Clone();
2899 editorOld
->DecRef();
2901 // do it even if there are no parameters to reset them to defaults
2902 wxString params
= typeName
.AfterFirst(_T(':'));
2903 renderer
->SetParameters(params
);
2904 editor
->SetParameters(params
);
2906 // register the new typename
2907 RegisterDataType(typeName
, renderer
, editor
);
2909 // we just registered it, it's the last one
2910 index
= m_typeinfo
.GetCount() - 1;
2916 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2918 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2924 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2926 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2932 // ----------------------------------------------------------------------------
2934 // ----------------------------------------------------------------------------
2936 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2939 wxGridTableBase::wxGridTableBase()
2941 m_view
= (wxGrid
*) NULL
;
2942 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2945 wxGridTableBase::~wxGridTableBase()
2947 delete m_attrProvider
;
2950 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2952 delete m_attrProvider
;
2953 m_attrProvider
= attrProvider
;
2956 bool wxGridTableBase::CanHaveAttributes()
2958 if ( ! GetAttrProvider() )
2960 // use the default attr provider by default
2961 SetAttrProvider(new wxGridCellAttrProvider
);
2966 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2968 if ( m_attrProvider
)
2969 return m_attrProvider
->GetAttr(row
, col
, kind
);
2971 return (wxGridCellAttr
*)NULL
;
2975 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2977 if ( m_attrProvider
)
2979 attr
->SetKind(wxGridCellAttr::Cell
);
2980 m_attrProvider
->SetAttr(attr
, row
, col
);
2984 // as we take ownership of the pointer and don't store it, we must
2990 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2992 if ( m_attrProvider
)
2994 attr
->SetKind(wxGridCellAttr::Row
);
2995 m_attrProvider
->SetRowAttr(attr
, row
);
2999 // as we take ownership of the pointer and don't store it, we must
3005 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3007 if ( m_attrProvider
)
3009 attr
->SetKind(wxGridCellAttr::Col
);
3010 m_attrProvider
->SetColAttr(attr
, col
);
3014 // as we take ownership of the pointer and don't store it, we must
3020 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3021 size_t WXUNUSED(numRows
) )
3023 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3028 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3030 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3035 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3036 size_t WXUNUSED(numRows
) )
3038 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3043 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3044 size_t WXUNUSED(numCols
) )
3046 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3051 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3053 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3058 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3059 size_t WXUNUSED(numCols
) )
3061 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3066 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3069 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3070 // how much it makes sense to us geeks.
3074 wxString
wxGridTableBase::GetColLabelValue( int col
)
3076 // default col labels are:
3077 // cols 0 to 25 : A-Z
3078 // cols 26 to 675 : AA-ZZ
3083 for ( n
= 1; ; n
++ )
3085 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3091 // reverse the string...
3093 for ( i
= 0; i
< n
; i
++ )
3101 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3103 return wxGRID_VALUE_STRING
;
3106 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3107 const wxString
& typeName
)
3109 return typeName
== wxGRID_VALUE_STRING
;
3112 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3114 return CanGetValueAs(row
, col
, typeName
);
3117 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3122 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3127 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3132 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3133 long WXUNUSED(value
) )
3137 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3138 double WXUNUSED(value
) )
3142 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3143 bool WXUNUSED(value
) )
3148 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3149 const wxString
& WXUNUSED(typeName
) )
3154 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3155 const wxString
& WXUNUSED(typeName
),
3156 void* WXUNUSED(value
) )
3160 //////////////////////////////////////////////////////////////////////
3162 // Message class for the grid table to send requests and notifications
3166 wxGridTableMessage::wxGridTableMessage()
3168 m_table
= (wxGridTableBase
*) NULL
;
3174 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3175 int commandInt1
, int commandInt2
)
3179 m_comInt1
= commandInt1
;
3180 m_comInt2
= commandInt2
;
3185 //////////////////////////////////////////////////////////////////////
3187 // A basic grid table for string data. An object of this class will
3188 // created by wxGrid if you don't specify an alternative table class.
3191 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3193 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3195 wxGridStringTable::wxGridStringTable()
3200 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3203 m_data
.Alloc( numRows
);
3206 sa
.Alloc( numCols
);
3207 sa
.Add( wxEmptyString
, numCols
);
3209 m_data
.Add( sa
, numRows
);
3212 wxGridStringTable::~wxGridStringTable()
3216 int wxGridStringTable::GetNumberRows()
3218 return m_data
.GetCount();
3221 int wxGridStringTable::GetNumberCols()
3223 if ( m_data
.GetCount() > 0 )
3224 return m_data
[0].GetCount();
3229 wxString
wxGridStringTable::GetValue( int row
, int col
)
3231 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3233 _T("invalid row or column index in wxGridStringTable") );
3235 return m_data
[row
][col
];
3238 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3240 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3241 _T("invalid row or column index in wxGridStringTable") );
3243 m_data
[row
][col
] = value
;
3246 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3248 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3250 _T("invalid row or column index in wxGridStringTable") );
3252 return (m_data
[row
][col
] == wxEmptyString
);
3255 void wxGridStringTable::Clear()
3258 int numRows
, numCols
;
3260 numRows
= m_data
.GetCount();
3263 numCols
= m_data
[0].GetCount();
3265 for ( row
= 0; row
< numRows
; row
++ )
3267 for ( col
= 0; col
< numCols
; col
++ )
3269 m_data
[row
][col
] = wxEmptyString
;
3276 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3278 size_t curNumRows
= m_data
.GetCount();
3279 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3280 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3282 if ( pos
>= curNumRows
)
3284 return AppendRows( numRows
);
3288 sa
.Alloc( curNumCols
);
3289 sa
.Add( wxEmptyString
, curNumCols
);
3290 m_data
.Insert( sa
, pos
, numRows
);
3293 wxGridTableMessage
msg( this,
3294 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3298 GetView()->ProcessTableMessage( msg
);
3304 bool wxGridStringTable::AppendRows( size_t numRows
)
3306 size_t curNumRows
= m_data
.GetCount();
3307 size_t curNumCols
= ( curNumRows
> 0
3308 ? m_data
[0].GetCount()
3309 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3312 if ( curNumCols
> 0 )
3314 sa
.Alloc( curNumCols
);
3315 sa
.Add( wxEmptyString
, curNumCols
);
3318 m_data
.Add( sa
, numRows
);
3322 wxGridTableMessage
msg( this,
3323 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3326 GetView()->ProcessTableMessage( msg
);
3332 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3334 size_t curNumRows
= m_data
.GetCount();
3336 if ( pos
>= curNumRows
)
3338 wxFAIL_MSG( wxString::Format
3340 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3342 (unsigned long)numRows
,
3343 (unsigned long)curNumRows
3349 if ( numRows
> curNumRows
- pos
)
3351 numRows
= curNumRows
- pos
;
3354 if ( numRows
>= curNumRows
)
3360 m_data
.RemoveAt( pos
, numRows
);
3365 wxGridTableMessage
msg( this,
3366 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3370 GetView()->ProcessTableMessage( msg
);
3376 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3380 size_t curNumRows
= m_data
.GetCount();
3381 size_t curNumCols
= ( curNumRows
> 0
3382 ? m_data
[0].GetCount()
3383 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3385 if ( pos
>= curNumCols
)
3387 return AppendCols( numCols
);
3390 for ( row
= 0; row
< curNumRows
; row
++ )
3392 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3394 m_data
[row
].Insert( wxEmptyString
, col
);
3400 wxGridTableMessage
msg( this,
3401 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3405 GetView()->ProcessTableMessage( msg
);
3411 bool wxGridStringTable::AppendCols( size_t numCols
)
3415 size_t curNumRows
= m_data
.GetCount();
3419 // TODO: something better than this ?
3421 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3426 for ( row
= 0; row
< curNumRows
; row
++ )
3428 m_data
[row
].Add( wxEmptyString
, numCols
);
3433 wxGridTableMessage
msg( this,
3434 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3437 GetView()->ProcessTableMessage( msg
);
3443 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3447 size_t curNumRows
= m_data
.GetCount();
3448 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3449 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3451 if ( pos
>= curNumCols
)
3453 wxFAIL_MSG( wxString::Format
3455 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3457 (unsigned long)numCols
,
3458 (unsigned long)curNumCols
3463 if ( numCols
> curNumCols
- pos
)
3465 numCols
= curNumCols
- pos
;
3468 for ( row
= 0; row
< curNumRows
; row
++ )
3470 if ( numCols
>= curNumCols
)
3472 m_data
[row
].Clear();
3476 m_data
[row
].RemoveAt( pos
, numCols
);
3482 wxGridTableMessage
msg( this,
3483 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3487 GetView()->ProcessTableMessage( msg
);
3493 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3495 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3497 // using default label
3499 return wxGridTableBase::GetRowLabelValue( row
);
3503 return m_rowLabels
[ row
];
3507 wxString
wxGridStringTable::GetColLabelValue( int col
)
3509 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3511 // using default label
3513 return wxGridTableBase::GetColLabelValue( col
);
3517 return m_colLabels
[ col
];
3521 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3523 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3525 int n
= m_rowLabels
.GetCount();
3527 for ( i
= n
; i
<= row
; i
++ )
3529 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3533 m_rowLabels
[row
] = value
;
3536 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3538 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3540 int n
= m_colLabels
.GetCount();
3542 for ( i
= n
; i
<= col
; i
++ )
3544 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3548 m_colLabels
[col
] = value
;
3552 //////////////////////////////////////////////////////////////////////
3553 //////////////////////////////////////////////////////////////////////
3555 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3557 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3558 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3559 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3560 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3561 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3562 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3563 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3566 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3568 const wxPoint
&pos
, const wxSize
&size
)
3569 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3574 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3578 // NO - don't do this because it will set both the x and y origin
3579 // coords to match the parent scrolled window and we just want to
3580 // set the y coord - MB
3582 // m_owner->PrepareDC( dc );
3585 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3586 dc
.SetDeviceOrigin( 0, -y
);
3588 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3589 m_owner
->DrawRowLabels( dc
, rows
);
3592 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3594 m_owner
->ProcessRowLabelMouseEvent( event
);
3597 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3599 m_owner
->GetEventHandler()->ProcessEvent(event
);
3602 // This seems to be required for wxMotif otherwise the mouse
3603 // cursor must be in the cell edit control to get key events
3605 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3607 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3610 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3612 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3615 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3617 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3621 //////////////////////////////////////////////////////////////////////
3623 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3625 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3626 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3627 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3628 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3629 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3630 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3631 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3634 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3636 const wxPoint
&pos
, const wxSize
&size
)
3637 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3642 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3646 // NO - don't do this because it will set both the x and y origin
3647 // coords to match the parent scrolled window and we just want to
3648 // set the x coord - MB
3650 // m_owner->PrepareDC( dc );
3653 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3654 dc
.SetDeviceOrigin( -x
, 0 );
3656 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3657 m_owner
->DrawColLabels( dc
, cols
);
3660 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3662 m_owner
->ProcessColLabelMouseEvent( event
);
3665 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3667 m_owner
->GetEventHandler()->ProcessEvent(event
);
3670 // This seems to be required for wxMotif otherwise the mouse
3671 // cursor must be in the cell edit control to get key events
3673 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3675 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3679 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3681 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3685 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3687 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3692 //////////////////////////////////////////////////////////////////////
3694 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3696 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3697 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3698 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3699 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3700 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3701 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3702 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3705 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3707 const wxPoint
&pos
, const wxSize
&size
)
3708 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3713 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3717 int client_height
= 0;
3718 int client_width
= 0;
3719 GetClientSize( &client_width
, &client_height
);
3721 // VZ: any reason for this ifdef? (FIXME)
3726 rect
.SetWidth( client_width
- 2 );
3727 rect
.SetHeight( client_height
- 2 );
3729 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3731 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3732 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3733 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3734 dc
.DrawLine( 0, 0, client_width
, 0 );
3735 dc
.DrawLine( 0, 0, 0, client_height
);
3737 dc
.SetPen( *wxWHITE_PEN
);
3738 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3739 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3740 #endif // __WXGTK__/!__WXGTK__
3743 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3745 m_owner
->ProcessCornerLabelMouseEvent( event
);
3748 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3750 m_owner
->GetEventHandler()->ProcessEvent(event
);
3753 // This seems to be required for wxMotif otherwise the mouse
3754 // cursor must be in the cell edit control to get key events
3756 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3758 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3761 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3763 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3766 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3768 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3772 //////////////////////////////////////////////////////////////////////
3774 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3776 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3777 EVT_PAINT( wxGridWindow::OnPaint
)
3778 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3779 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3780 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3781 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3782 EVT_CHAR ( wxGridWindow::OnChar
)
3783 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3784 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3785 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3788 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3789 wxGridRowLabelWindow
*rowLblWin
,
3790 wxGridColLabelWindow
*colLblWin
,
3793 const wxSize
&size
)
3794 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3795 wxT("grid window") )
3798 m_rowLabelWin
= rowLblWin
;
3799 m_colLabelWin
= colLblWin
;
3802 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3804 wxPaintDC
dc( this );
3805 m_owner
->PrepareDC( dc
);
3806 wxRegion reg
= GetUpdateRegion();
3807 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3808 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3809 #if WXGRID_DRAW_LINES
3810 m_owner
->DrawAllGridLines( dc
, reg
);
3812 m_owner
->DrawGridSpace( dc
);
3813 m_owner
->DrawHighlight( dc
, DirtyCells
);
3816 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3818 wxWindow::ScrollWindow( dx
, dy
, rect
);
3819 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3820 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3823 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3825 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3828 m_owner
->ProcessGridCellMouseEvent( event
);
3831 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3833 m_owner
->GetEventHandler()->ProcessEvent(event
);
3836 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3837 // cursor must be in the cell edit control to get key events
3839 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3841 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3845 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3847 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3851 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3853 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3857 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3861 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3863 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3867 //////////////////////////////////////////////////////////////////////
3869 // Internal Helper function for computing row or column from some
3870 // (unscrolled) coordinate value, using either
3871 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3872 // of m_rowBottoms/m_ColRights to speed up the search!
3874 // Internal helper macros for simpler use of that function
3876 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3877 const wxArrayInt
& BorderArray
, int nMax
,
3880 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3881 m_minAcceptableColWidth, \
3882 m_colRights, m_numCols, true)
3883 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3884 m_minAcceptableRowHeight, \
3885 m_rowBottoms, m_numRows, true)
3886 /////////////////////////////////////////////////////////////////////
3888 #if wxUSE_EXTENDED_RTTI
3889 WX_DEFINE_FLAGS( wxGridStyle
)
3891 wxBEGIN_FLAGS( wxGridStyle
)
3892 // new style border flags, we put them first to
3893 // use them for streaming out
3894 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3895 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3896 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3897 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3898 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3899 wxFLAGS_MEMBER(wxBORDER_NONE
)
3901 // old style border flags
3902 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3903 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3904 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3905 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3906 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3907 wxFLAGS_MEMBER(wxBORDER
)
3909 // standard window styles
3910 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3911 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3912 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3913 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3914 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3915 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3916 wxFLAGS_MEMBER(wxVSCROLL
)
3917 wxFLAGS_MEMBER(wxHSCROLL
)
3919 wxEND_FLAGS( wxGridStyle
)
3921 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3923 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3924 wxHIDE_PROPERTY( Children
)
3925 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3926 wxEND_PROPERTIES_TABLE()
3928 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3929 wxEND_HANDLERS_TABLE()
3931 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3934 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3937 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3940 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3941 EVT_PAINT( wxGrid::OnPaint
)
3942 EVT_SIZE( wxGrid::OnSize
)
3943 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3944 EVT_KEY_UP( wxGrid::OnKeyUp
)
3945 EVT_CHAR ( wxGrid::OnChar
)
3946 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3951 // in order to make sure that a size event is not
3952 // trigerred in a unfinished state
3953 m_cornerLabelWin
= NULL
;
3954 m_rowLabelWin
= NULL
;
3955 m_colLabelWin
= NULL
;
3959 wxGrid::wxGrid( wxWindow
*parent
,
3964 const wxString
& name
)
3965 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3966 m_colMinWidths(GRID_HASH_SIZE
),
3967 m_rowMinHeights(GRID_HASH_SIZE
)
3970 SetBestFittingSize(size
);
3973 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3974 const wxPoint
& pos
, const wxSize
& size
,
3975 long style
, const wxString
& name
)
3977 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3978 style
| wxWANTS_CHARS
, name
))
3981 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3982 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3985 SetBestFittingSize(size
);
3993 // Must do this or ~wxScrollHelper will pop the wrong event handler
3994 SetTargetWindow(this);
3996 wxSafeDecRef(m_defaultCellAttr
);
3998 #ifdef DEBUG_ATTR_CACHE
3999 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4000 wxPrintf(_T("wxGrid attribute cache statistics: "
4001 "total: %u, hits: %u (%u%%)\n"),
4002 total
, gs_nAttrCacheHits
,
4003 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4009 delete m_typeRegistry
;
4015 // ----- internal init and update functions
4018 // NOTE: If using the default visual attributes works everywhere then this can
4019 // be removed as well as the #else cases below.
4020 #define _USE_VISATTR 0
4023 #include "wx/listbox.h"
4026 void wxGrid::Create()
4028 m_created
= false; // set to true by CreateGrid
4030 m_table
= (wxGridTableBase
*) NULL
;
4033 m_cellEditCtrlEnabled
= false;
4035 m_defaultCellAttr
= new wxGridCellAttr();
4037 // Set default cell attributes
4038 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4039 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4040 m_defaultCellAttr
->SetFont(GetFont());
4041 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4042 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4043 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4046 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4047 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4049 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4050 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4053 m_defaultCellAttr
->SetTextColour(
4054 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4055 m_defaultCellAttr
->SetBackgroundColour(
4056 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4061 m_currentCellCoords
= wxGridNoCellCoords
;
4063 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4064 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4066 // create the type registry
4067 m_typeRegistry
= new wxGridTypeRegistry
;
4070 // subwindow components that make up the wxGrid
4071 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4076 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4081 m_colLabelWin
= new wxGridColLabelWindow( this,
4086 m_gridWin
= new wxGridWindow( this,
4093 SetTargetWindow( m_gridWin
);
4096 wxColour gfg
= gva
.colFg
;
4097 wxColour gbg
= gva
.colBg
;
4098 wxColour lfg
= lva
.colFg
;
4099 wxColour lbg
= lva
.colBg
;
4101 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4102 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4103 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4104 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4106 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4107 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4108 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4109 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4110 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4111 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4113 m_gridWin
->SetOwnForegroundColour(gfg
);
4114 m_gridWin
->SetOwnBackgroundColour(gbg
);
4120 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4121 wxGrid::wxGridSelectionModes selmode
)
4123 wxCHECK_MSG( !m_created
,
4125 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4127 m_numRows
= numRows
;
4128 m_numCols
= numCols
;
4130 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4131 m_table
->SetView( this );
4133 m_selection
= new wxGridSelection( this, selmode
);
4142 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4144 wxCHECK_RET( m_created
,
4145 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4147 m_selection
->SetSelectionMode( selmode
);
4150 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4152 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4153 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4155 return m_selection
->GetSelectionMode();
4158 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4159 wxGrid::wxGridSelectionModes selmode
)
4163 // stop all processing
4168 wxGridTableBase
*t
=m_table
;
4181 m_numRows
= table
->GetNumberRows();
4182 m_numCols
= table
->GetNumberCols();
4185 m_table
->SetView( this );
4186 m_ownTable
= takeOwnership
;
4187 m_selection
= new wxGridSelection( this, selmode
);
4200 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4201 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4203 if ( m_rowLabelWin
)
4205 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4209 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4212 m_labelTextColour
= wxColour( _T("BLACK") );
4215 m_attrCache
.row
= -1;
4216 m_attrCache
.col
= -1;
4217 m_attrCache
.attr
= NULL
;
4219 // TODO: something better than this ?
4221 m_labelFont
= this->GetFont();
4222 m_labelFont
.SetWeight( wxBOLD
);
4224 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4225 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4227 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4228 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4229 m_colLabelTextOrientation
= wxHORIZONTAL
;
4231 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4232 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4234 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4235 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4237 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4238 m_defaultRowHeight
+= 8;
4240 m_defaultRowHeight
+= 4;
4243 m_gridLineColour
= wxColour( 192,192,192 );
4244 m_gridLinesEnabled
= true;
4245 m_cellHighlightColour
= *wxBLACK
;
4246 m_cellHighlightPenWidth
= 2;
4247 m_cellHighlightROPenWidth
= 1;
4249 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4250 m_winCapture
= (wxWindow
*)NULL
;
4251 m_canDragRowSize
= true;
4252 m_canDragColSize
= true;
4253 m_canDragGridSize
= true;
4254 m_canDragCell
= false;
4256 m_dragRowOrCol
= -1;
4257 m_isDragging
= false;
4258 m_startDragPos
= wxDefaultPosition
;
4260 m_waitForSlowClick
= false;
4262 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4263 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4265 m_currentCellCoords
= wxGridNoCellCoords
;
4267 m_selectingTopLeft
= wxGridNoCellCoords
;
4268 m_selectingBottomRight
= wxGridNoCellCoords
;
4269 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4270 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4272 m_editable
= true; // default for whole grid
4274 m_inOnKeyDown
= false;
4280 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4281 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4284 // ----------------------------------------------------------------------------
4285 // the idea is to call these functions only when necessary because they create
4286 // quite big arrays which eat memory mostly unnecessary - in particular, if
4287 // default widths/heights are used for all rows/columns, we may not use these
4290 // with some extra code, it should be possible to only store the
4291 // widths/heights different from default ones but this will be done later...
4292 // ----------------------------------------------------------------------------
4294 void wxGrid::InitRowHeights()
4296 m_rowHeights
.Empty();
4297 m_rowBottoms
.Empty();
4299 m_rowHeights
.Alloc( m_numRows
);
4300 m_rowBottoms
.Alloc( m_numRows
);
4304 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4306 for ( int i
= 0; i
< m_numRows
; i
++ )
4308 rowBottom
+= m_defaultRowHeight
;
4309 m_rowBottoms
.Add( rowBottom
);
4313 void wxGrid::InitColWidths()
4315 m_colWidths
.Empty();
4316 m_colRights
.Empty();
4318 m_colWidths
.Alloc( m_numCols
);
4319 m_colRights
.Alloc( m_numCols
);
4322 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4324 for ( int i
= 0; i
< m_numCols
; i
++ )
4326 colRight
+= m_defaultColWidth
;
4327 m_colRights
.Add( colRight
);
4331 int wxGrid::GetColWidth(int col
) const
4333 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4336 int wxGrid::GetColLeft(int col
) const
4338 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4339 : m_colRights
[col
] - m_colWidths
[col
];
4342 int wxGrid::GetColRight(int col
) const
4344 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4348 int wxGrid::GetRowHeight(int row
) const
4350 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4353 int wxGrid::GetRowTop(int row
) const
4355 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4356 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4359 int wxGrid::GetRowBottom(int row
) const
4361 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4362 : m_rowBottoms
[row
];
4365 void wxGrid::CalcDimensions()
4368 GetClientSize( &cw
, &ch
);
4370 if ( m_rowLabelWin
->IsShown() )
4371 cw
-= m_rowLabelWidth
;
4372 if ( m_colLabelWin
->IsShown() )
4373 ch
-= m_colLabelHeight
;
4376 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4377 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4379 // take into account editor if shown
4380 if ( IsCellEditControlShown() )
4383 int r
= m_currentCellCoords
.GetRow();
4384 int c
= m_currentCellCoords
.GetCol();
4385 int x
= GetColLeft(c
);
4386 int y
= GetRowTop(r
);
4388 // how big is the editor
4389 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4390 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4391 editor
->GetControl()->GetSize(&w2
, &h2
);
4402 // preserve (more or less) the previous position
4404 GetViewStart( &x
, &y
);
4406 // ensure the position is valid for the new scroll ranges
4408 x
= wxMax( w
- 1, 0 );
4410 y
= wxMax( h
- 1, 0 );
4412 // do set scrollbar parameters
4413 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4414 GetScrollX(w
), GetScrollY(h
), x
, y
,
4415 GetBatchCount() != 0);
4417 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4418 // still must reposition the children
4423 void wxGrid::CalcWindowSizes()
4425 // escape if the window is has not been fully created yet
4427 if ( m_cornerLabelWin
== NULL
)
4431 GetClientSize( &cw
, &ch
);
4433 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4434 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4436 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4437 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4439 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4440 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4442 if ( m_gridWin
&& m_gridWin
->IsShown() )
4443 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4447 // this is called when the grid table sends a message to say that it
4448 // has been redimensioned
4450 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4453 bool result
= false;
4455 // Clear the attribute cache as the attribute might refer to a different
4456 // cell than stored in the cache after adding/removing rows/columns.
4458 // By the same reasoning, the editor should be dismissed if columns are
4459 // added or removed. And for consistency, it should IMHO always be
4460 // removed, not only if the cell "underneath" it actually changes.
4461 // For now, I intentionally do not save the editor's content as the
4462 // cell it might want to save that stuff to might no longer exist.
4463 HideCellEditControl();
4465 // if we were using the default widths/heights so far, we must change them
4467 if ( m_colWidths
.IsEmpty() )
4472 if ( m_rowHeights
.IsEmpty() )
4478 switch ( msg
.GetId() )
4480 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4482 size_t pos
= msg
.GetCommandInt();
4483 int numRows
= msg
.GetCommandInt2();
4485 m_numRows
+= numRows
;
4487 if ( !m_rowHeights
.IsEmpty() )
4489 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4490 m_rowBottoms
.Insert( 0, pos
, numRows
);
4493 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4495 for ( i
= pos
; i
< m_numRows
; i
++ )
4497 bottom
+= m_rowHeights
[i
];
4498 m_rowBottoms
[i
] = bottom
;
4501 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4503 // if we have just inserted cols into an empty grid the current
4504 // cell will be undefined...
4506 SetCurrentCell( 0, 0 );
4510 m_selection
->UpdateRows( pos
, numRows
);
4511 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4513 attrProvider
->UpdateAttrRows( pos
, numRows
);
4515 if ( !GetBatchCount() )
4518 m_rowLabelWin
->Refresh();
4524 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4526 int numRows
= msg
.GetCommandInt();
4527 int oldNumRows
= m_numRows
;
4528 m_numRows
+= numRows
;
4530 if ( !m_rowHeights
.IsEmpty() )
4532 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4533 m_rowBottoms
.Add( 0, numRows
);
4536 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4538 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4540 bottom
+= m_rowHeights
[i
];
4541 m_rowBottoms
[i
] = bottom
;
4544 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4546 // if we have just inserted cols into an empty grid the current
4547 // cell will be undefined...
4549 SetCurrentCell( 0, 0 );
4551 if ( !GetBatchCount() )
4554 m_rowLabelWin
->Refresh();
4560 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4562 size_t pos
= msg
.GetCommandInt();
4563 int numRows
= msg
.GetCommandInt2();
4564 m_numRows
-= numRows
;
4566 if ( !m_rowHeights
.IsEmpty() )
4568 m_rowHeights
.RemoveAt( pos
, numRows
);
4569 m_rowBottoms
.RemoveAt( pos
, numRows
);
4572 for ( i
= 0; i
< m_numRows
; i
++ )
4574 h
+= m_rowHeights
[i
];
4575 m_rowBottoms
[i
] = h
;
4580 m_currentCellCoords
= wxGridNoCellCoords
;
4584 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4585 m_currentCellCoords
.Set( 0, 0 );
4589 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4590 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4593 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4594 // ifdef'd out following patch from Paul Gammans
4596 // No need to touch column attributes, unless we
4597 // removed _all_ rows, in this case, we remove
4598 // all column attributes.
4599 // I hate to do this here, but the
4600 // needed data is not available inside UpdateAttrRows.
4601 if ( !GetNumberRows() )
4602 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4605 if ( !GetBatchCount() )
4608 m_rowLabelWin
->Refresh();
4614 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4616 size_t pos
= msg
.GetCommandInt();
4617 int numCols
= msg
.GetCommandInt2();
4618 m_numCols
+= numCols
;
4620 if ( !m_colWidths
.IsEmpty() )
4622 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4623 m_colRights
.Insert( 0, pos
, numCols
);
4626 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4628 for ( i
= pos
; i
< m_numCols
; i
++ )
4630 right
+= m_colWidths
[i
];
4631 m_colRights
[i
] = right
;
4634 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4636 // if we have just inserted cols into an empty grid the current
4637 // cell will be undefined...
4639 SetCurrentCell( 0, 0 );
4643 m_selection
->UpdateCols( pos
, numCols
);
4644 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4646 attrProvider
->UpdateAttrCols( pos
, numCols
);
4647 if ( !GetBatchCount() )
4650 m_colLabelWin
->Refresh();
4657 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4659 int numCols
= msg
.GetCommandInt();
4660 int oldNumCols
= m_numCols
;
4661 m_numCols
+= numCols
;
4662 if ( !m_colWidths
.IsEmpty() )
4664 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4665 m_colRights
.Add( 0, numCols
);
4668 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4670 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4672 right
+= m_colWidths
[i
];
4673 m_colRights
[i
] = right
;
4676 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4678 // if we have just inserted cols into an empty grid the current
4679 // cell will be undefined...
4681 SetCurrentCell( 0, 0 );
4683 if ( !GetBatchCount() )
4686 m_colLabelWin
->Refresh();
4692 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4694 size_t pos
= msg
.GetCommandInt();
4695 int numCols
= msg
.GetCommandInt2();
4696 m_numCols
-= numCols
;
4698 if ( !m_colWidths
.IsEmpty() )
4700 m_colWidths
.RemoveAt( pos
, numCols
);
4701 m_colRights
.RemoveAt( pos
, numCols
);
4704 for ( i
= 0; i
< m_numCols
; i
++ )
4706 w
+= m_colWidths
[i
];
4712 m_currentCellCoords
= wxGridNoCellCoords
;
4716 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4717 m_currentCellCoords
.Set( 0, 0 );
4721 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4722 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4725 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4726 // ifdef'd out following patch from Paul Gammans
4728 // No need to touch row attributes, unless we
4729 // removed _all_ columns, in this case, we remove
4730 // all row attributes.
4731 // I hate to do this here, but the
4732 // needed data is not available inside UpdateAttrCols.
4733 if ( !GetNumberCols() )
4734 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4737 if ( !GetBatchCount() )
4740 m_colLabelWin
->Refresh();
4747 if (result
&& !GetBatchCount() )
4748 m_gridWin
->Refresh();
4753 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4755 wxRegionIterator
iter( reg
);
4758 wxArrayInt rowlabels
;
4765 // TODO: remove this when we can...
4766 // There is a bug in wxMotif that gives garbage update
4767 // rectangles if you jump-scroll a long way by clicking the
4768 // scrollbar with middle button. This is a work-around
4770 #if defined(__WXMOTIF__)
4772 m_gridWin
->GetClientSize( &cw
, &ch
);
4773 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4774 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4777 // logical bounds of update region
4780 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4781 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4783 // find the row labels within these bounds
4786 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4788 if ( GetRowBottom(row
) < top
)
4791 if ( GetRowTop(row
) > bottom
)
4794 rowlabels
.Add( row
);
4804 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4806 wxRegionIterator
iter( reg
);
4809 wxArrayInt colLabels
;
4816 // TODO: remove this when we can...
4817 // There is a bug in wxMotif that gives garbage update
4818 // rectangles if you jump-scroll a long way by clicking the
4819 // scrollbar with middle button. This is a work-around
4821 #if defined(__WXMOTIF__)
4823 m_gridWin
->GetClientSize( &cw
, &ch
);
4824 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4825 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4828 // logical bounds of update region
4831 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4832 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4834 // find the cells within these bounds
4837 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4839 if ( GetColRight(col
) < left
)
4842 if ( GetColLeft(col
) > right
)
4845 colLabels
.Add( col
);
4854 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4856 wxRegionIterator
iter( reg
);
4859 wxGridCellCoordsArray cellsExposed
;
4861 int left
, top
, right
, bottom
;
4866 // TODO: remove this when we can...
4867 // There is a bug in wxMotif that gives garbage update
4868 // rectangles if you jump-scroll a long way by clicking the
4869 // scrollbar with middle button. This is a work-around
4871 #if defined(__WXMOTIF__)
4873 m_gridWin
->GetClientSize( &cw
, &ch
);
4874 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4875 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4876 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4877 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4880 // logical bounds of update region
4882 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4883 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4885 // find the cells within these bounds
4888 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4890 if ( GetRowBottom(row
) <= top
)
4893 if ( GetRowTop(row
) > bottom
)
4896 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4898 if ( GetColRight(col
) <= left
)
4901 if ( GetColLeft(col
) > right
)
4904 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4911 return cellsExposed
;
4915 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4918 wxPoint
pos( event
.GetPosition() );
4919 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4921 if ( event
.Dragging() )
4925 m_isDragging
= true;
4926 m_rowLabelWin
->CaptureMouse();
4929 if ( event
.LeftIsDown() )
4931 switch ( m_cursorMode
)
4933 case WXGRID_CURSOR_RESIZE_ROW
:
4935 int cw
, ch
, left
, dummy
;
4936 m_gridWin
->GetClientSize( &cw
, &ch
);
4937 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4939 wxClientDC
dc( m_gridWin
);
4942 GetRowTop(m_dragRowOrCol
) +
4943 GetRowMinimalHeight(m_dragRowOrCol
) );
4944 dc
.SetLogicalFunction(wxINVERT
);
4945 if ( m_dragLastPos
>= 0 )
4947 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4949 dc
.DrawLine( left
, y
, left
+cw
, y
);
4954 case WXGRID_CURSOR_SELECT_ROW
:
4956 if ( (row
= YToRow( y
)) >= 0 )
4960 m_selection
->SelectRow( row
,
4961 event
.ControlDown(),
4970 // default label to suppress warnings about "enumeration value
4971 // 'xxx' not handled in switch
4979 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4984 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4985 m_isDragging
= false;
4988 // ------------ Entering or leaving the window
4990 if ( event
.Entering() || event
.Leaving() )
4992 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4996 // ------------ Left button pressed
4998 else if ( event
.LeftDown() )
5000 // don't send a label click event for a hit on the
5001 // edge of the row label - this is probably the user
5002 // wanting to resize the row
5004 if ( YToEdgeOfRow(y
) < 0 )
5008 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5010 if ( !event
.ShiftDown() && !event
.ControlDown() )
5014 if ( event
.ShiftDown() )
5016 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5019 GetNumberCols() - 1,
5020 event
.ControlDown(),
5027 m_selection
->SelectRow( row
,
5028 event
.ControlDown(),
5035 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5040 // starting to drag-resize a row
5042 if ( CanDragRowSize() )
5043 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5048 // ------------ Left double click
5050 else if (event
.LeftDClick() )
5052 row
= YToEdgeOfRow(y
);
5057 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5059 // no default action at the moment
5064 // adjust row height depending on label text
5065 AutoSizeRowLabelSize( row
);
5067 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5073 // ------------ Left button released
5075 else if ( event
.LeftUp() )
5077 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5079 DoEndDragResizeRow();
5081 // Note: we are ending the event *after* doing
5082 // default processing in this case
5084 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5087 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5092 // ------------ Right button down
5094 else if ( event
.RightDown() )
5098 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5100 // no default action at the moment
5105 // ------------ Right double click
5107 else if ( event
.RightDClick() )
5111 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5113 // no default action at the moment
5118 // ------------ No buttons down and mouse moving
5120 else if ( event
.Moving() )
5122 m_dragRowOrCol
= YToEdgeOfRow( y
);
5123 if ( m_dragRowOrCol
>= 0 )
5125 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5127 // don't capture the mouse yet
5128 if ( CanDragRowSize() )
5129 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5132 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5134 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5140 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5143 wxPoint
pos( event
.GetPosition() );
5144 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5146 if ( event
.Dragging() )
5150 m_isDragging
= true;
5151 m_colLabelWin
->CaptureMouse();
5154 if ( event
.LeftIsDown() )
5156 switch ( m_cursorMode
)
5158 case WXGRID_CURSOR_RESIZE_COL
:
5160 int cw
, ch
, dummy
, top
;
5161 m_gridWin
->GetClientSize( &cw
, &ch
);
5162 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5164 wxClientDC
dc( m_gridWin
);
5167 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5168 GetColMinimalWidth(m_dragRowOrCol
));
5169 dc
.SetLogicalFunction(wxINVERT
);
5170 if ( m_dragLastPos
>= 0 )
5172 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5174 dc
.DrawLine( x
, top
, x
, top
+ch
);
5179 case WXGRID_CURSOR_SELECT_COL
:
5181 if ( (col
= XToCol( x
)) >= 0 )
5185 m_selection
->SelectCol( col
,
5186 event
.ControlDown(),
5195 // default label to suppress warnings about "enumeration value
5196 // 'xxx' not handled in switch
5204 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5209 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5210 m_isDragging
= false;
5213 // ------------ Entering or leaving the window
5215 if ( event
.Entering() || event
.Leaving() )
5217 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5221 // ------------ Left button pressed
5223 else if ( event
.LeftDown() )
5225 // don't send a label click event for a hit on the
5226 // edge of the col label - this is probably the user
5227 // wanting to resize the col
5229 if ( XToEdgeOfCol(x
) < 0 )
5233 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5235 if ( !event
.ShiftDown() && !event
.ControlDown() )
5239 if ( event
.ShiftDown() )
5241 m_selection
->SelectBlock( 0,
5242 m_currentCellCoords
.GetCol(),
5243 GetNumberRows() - 1, col
,
5244 event
.ControlDown(),
5251 m_selection
->SelectCol( col
,
5252 event
.ControlDown(),
5259 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5264 // starting to drag-resize a col
5266 if ( CanDragColSize() )
5267 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5272 // ------------ Left double click
5274 if ( event
.LeftDClick() )
5276 col
= XToEdgeOfCol(x
);
5281 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5283 // no default action at the moment
5288 // adjust column width depending on label text
5289 AutoSizeColLabelSize( col
);
5291 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5297 // ------------ Left button released
5299 else if ( event
.LeftUp() )
5301 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5303 DoEndDragResizeCol();
5305 // Note: we are ending the event *after* doing
5306 // default processing in this case
5308 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5311 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5316 // ------------ Right button down
5318 else if ( event
.RightDown() )
5322 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5324 // no default action at the moment
5329 // ------------ Right double click
5331 else if ( event
.RightDClick() )
5335 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5337 // no default action at the moment
5342 // ------------ No buttons down and mouse moving
5344 else if ( event
.Moving() )
5346 m_dragRowOrCol
= XToEdgeOfCol( x
);
5347 if ( m_dragRowOrCol
>= 0 )
5349 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5351 // don't capture the cursor yet
5352 if ( CanDragColSize() )
5353 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5356 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5358 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5364 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5366 if ( event
.LeftDown() )
5368 // indicate corner label by having both row and
5371 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5377 else if ( event
.LeftDClick() )
5379 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5382 else if ( event
.RightDown() )
5384 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5386 // no default action at the moment
5390 else if ( event
.RightDClick() )
5392 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5394 // no default action at the moment
5399 void wxGrid::ChangeCursorMode(CursorMode mode
,
5404 static const wxChar
*cursorModes
[] =
5413 wxLogTrace(_T("grid"),
5414 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5415 win
== m_colLabelWin
? _T("colLabelWin")
5416 : win
? _T("rowLabelWin")
5418 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5419 #endif // __WXDEBUG__
5421 if ( mode
== m_cursorMode
&&
5422 win
== m_winCapture
&&
5423 captureMouse
== (m_winCapture
!= NULL
))
5428 // by default use the grid itself
5434 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5435 m_winCapture
= (wxWindow
*)NULL
;
5438 m_cursorMode
= mode
;
5440 switch ( m_cursorMode
)
5442 case WXGRID_CURSOR_RESIZE_ROW
:
5443 win
->SetCursor( m_rowResizeCursor
);
5446 case WXGRID_CURSOR_RESIZE_COL
:
5447 win
->SetCursor( m_colResizeCursor
);
5451 win
->SetCursor( *wxSTANDARD_CURSOR
);
5454 // we need to capture mouse when resizing
5455 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5456 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5458 if ( captureMouse
&& resize
)
5460 win
->CaptureMouse();
5465 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5468 wxPoint
pos( event
.GetPosition() );
5469 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5471 wxGridCellCoords coords
;
5472 XYToCell( x
, y
, coords
);
5474 int cell_rows
, cell_cols
;
5475 bool isFirstDrag
= !m_isDragging
;
5476 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5477 if ((cell_rows
< 0) || (cell_cols
< 0))
5479 coords
.SetRow(coords
.GetRow() + cell_rows
);
5480 coords
.SetCol(coords
.GetCol() + cell_cols
);
5483 if ( event
.Dragging() )
5485 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5487 // Don't start doing anything until the mouse has been dragged at
5488 // least 3 pixels in any direction...
5491 if (m_startDragPos
== wxDefaultPosition
)
5493 m_startDragPos
= pos
;
5496 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5500 m_isDragging
= true;
5501 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5503 // Hide the edit control, so it
5504 // won't interfere with drag-shrinking.
5505 if ( IsCellEditControlShown() )
5507 HideCellEditControl();
5508 SaveEditControlValue();
5511 // Have we captured the mouse yet?
5514 m_winCapture
= m_gridWin
;
5515 m_winCapture
->CaptureMouse();
5518 if ( coords
!= wxGridNoCellCoords
)
5520 if ( event
.ControlDown() )
5522 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5523 m_selectingKeyboard
= coords
;
5524 HighlightBlock ( m_selectingKeyboard
, coords
);
5526 else if ( CanDragCell() )
5530 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5531 m_selectingKeyboard
= coords
;
5533 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5541 if ( !IsSelection() )
5543 HighlightBlock( coords
, coords
);
5547 HighlightBlock( m_currentCellCoords
, coords
);
5551 if (! IsVisible(coords
))
5553 MakeCellVisible(coords
);
5554 // TODO: need to introduce a delay or something here. The
5555 // scrolling is way to fast, at least on MSW - also on GTK.
5559 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5561 int cw
, ch
, left
, dummy
;
5562 m_gridWin
->GetClientSize( &cw
, &ch
);
5563 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5565 wxClientDC
dc( m_gridWin
);
5567 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5568 GetRowMinimalHeight(m_dragRowOrCol
) );
5569 dc
.SetLogicalFunction(wxINVERT
);
5570 if ( m_dragLastPos
>= 0 )
5572 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5574 dc
.DrawLine( left
, y
, left
+cw
, y
);
5577 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5579 int cw
, ch
, dummy
, top
;
5580 m_gridWin
->GetClientSize( &cw
, &ch
);
5581 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5583 wxClientDC
dc( m_gridWin
);
5585 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5586 GetColMinimalWidth(m_dragRowOrCol
) );
5587 dc
.SetLogicalFunction(wxINVERT
);
5588 if ( m_dragLastPos
>= 0 )
5590 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5592 dc
.DrawLine( x
, top
, x
, top
+ch
);
5599 m_isDragging
= false;
5600 m_startDragPos
= wxDefaultPosition
;
5602 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5603 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5606 if ( event
.Entering() || event
.Leaving() )
5608 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5609 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5614 // ------------ Left button pressed
5616 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5618 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5623 if ( !event
.ControlDown() )
5625 if ( event
.ShiftDown() )
5629 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5630 m_currentCellCoords
.GetCol(),
5633 event
.ControlDown(),
5639 else if ( XToEdgeOfCol(x
) < 0 &&
5640 YToEdgeOfRow(y
) < 0 )
5642 DisableCellEditControl();
5643 MakeCellVisible( coords
);
5645 if ( event
.ControlDown() )
5649 m_selection
->ToggleCellSelection( coords
.GetRow(),
5651 event
.ControlDown(),
5656 m_selectingTopLeft
= wxGridNoCellCoords
;
5657 m_selectingBottomRight
= wxGridNoCellCoords
;
5658 m_selectingKeyboard
= coords
;
5662 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5663 SetCurrentCell( coords
);
5666 if ( m_selection
->GetSelectionMode() !=
5667 wxGrid::wxGridSelectCells
)
5669 HighlightBlock( coords
, coords
);
5678 // ------------ Left double click
5680 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5682 DisableCellEditControl();
5684 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5686 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5691 // we want double click to select a cell and start editing
5692 // (i.e. to behave in same way as sequence of two slow clicks):
5693 m_waitForSlowClick
= true;
5700 // ------------ Left button released
5702 else if ( event
.LeftUp() )
5704 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5708 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5709 m_winCapture
= NULL
;
5712 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5715 EnableCellEditControl();
5717 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5718 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5719 editor
->StartingClick();
5723 m_waitForSlowClick
= false;
5725 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5726 m_selectingBottomRight
!= wxGridNoCellCoords
)
5730 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5731 m_selectingTopLeft
.GetCol(),
5732 m_selectingBottomRight
.GetRow(),
5733 m_selectingBottomRight
.GetCol(),
5734 event
.ControlDown(),
5740 m_selectingTopLeft
= wxGridNoCellCoords
;
5741 m_selectingBottomRight
= wxGridNoCellCoords
;
5743 // Show the edit control, if it has been hidden for
5745 ShowCellEditControl();
5748 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5750 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5751 DoEndDragResizeRow();
5753 // Note: we are ending the event *after* doing
5754 // default processing in this case
5756 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5758 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5760 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5761 DoEndDragResizeCol();
5763 // Note: we are ending the event *after* doing
5764 // default processing in this case
5766 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5773 // ------------ Right button down
5775 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5777 DisableCellEditControl();
5778 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5783 // no default action at the moment
5788 // ------------ Right double click
5790 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5792 DisableCellEditControl();
5793 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5798 // no default action at the moment
5802 // ------------ Moving and no button action
5804 else if ( event
.Moving() && !event
.IsButton() )
5806 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5808 // out of grid cell area
5809 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5813 int dragRow
= YToEdgeOfRow( y
);
5814 int dragCol
= XToEdgeOfCol( x
);
5816 // Dragging on the corner of a cell to resize in both
5817 // directions is not implemented yet...
5819 if ( dragRow
>= 0 && dragCol
>= 0 )
5821 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5827 m_dragRowOrCol
= dragRow
;
5829 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5831 if ( CanDragRowSize() && CanDragGridSize() )
5832 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5837 m_dragRowOrCol
= dragCol
;
5845 m_dragRowOrCol
= dragCol
;
5847 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5849 if ( CanDragColSize() && CanDragGridSize() )
5850 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5856 // Neither on a row or col edge
5858 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5860 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5866 void wxGrid::DoEndDragResizeRow()
5868 if ( m_dragLastPos
>= 0 )
5870 // erase the last line and resize the row
5872 int cw
, ch
, left
, dummy
;
5873 m_gridWin
->GetClientSize( &cw
, &ch
);
5874 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5876 wxClientDC
dc( m_gridWin
);
5878 dc
.SetLogicalFunction( wxINVERT
);
5879 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5880 HideCellEditControl();
5881 SaveEditControlValue();
5883 int rowTop
= GetRowTop(m_dragRowOrCol
);
5884 SetRowSize( m_dragRowOrCol
,
5885 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5887 if ( !GetBatchCount() )
5889 // Only needed to get the correct rect.y:
5890 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5892 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5893 rect
.width
= m_rowLabelWidth
;
5894 rect
.height
= ch
- rect
.y
;
5895 m_rowLabelWin
->Refresh( true, &rect
);
5897 // if there is a multicell block, paint all of it
5900 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5901 int leftCol
= XToCol(left
);
5902 int rightCol
= internalXToCol(left
+cw
);
5905 for (i
=leftCol
; i
<rightCol
; i
++)
5907 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5908 if (cell_rows
< subtract_rows
)
5909 subtract_rows
= cell_rows
;
5911 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5912 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5913 rect
.height
= ch
- rect
.y
;
5916 m_gridWin
->Refresh( false, &rect
);
5919 ShowCellEditControl();
5924 void wxGrid::DoEndDragResizeCol()
5926 if ( m_dragLastPos
>= 0 )
5928 // erase the last line and resize the col
5930 int cw
, ch
, dummy
, top
;
5931 m_gridWin
->GetClientSize( &cw
, &ch
);
5932 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5934 wxClientDC
dc( m_gridWin
);
5936 dc
.SetLogicalFunction( wxINVERT
);
5937 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5938 HideCellEditControl();
5939 SaveEditControlValue();
5941 int colLeft
= GetColLeft(m_dragRowOrCol
);
5942 SetColSize( m_dragRowOrCol
,
5943 wxMax( m_dragLastPos
- colLeft
,
5944 GetColMinimalWidth(m_dragRowOrCol
) ) );
5946 if ( !GetBatchCount() )
5948 // Only needed to get the correct rect.x:
5949 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5951 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5952 rect
.width
= cw
- rect
.x
;
5953 rect
.height
= m_colLabelHeight
;
5954 m_colLabelWin
->Refresh( true, &rect
);
5956 // if there is a multicell block, paint all of it
5959 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5960 int topRow
= YToRow(top
);
5961 int bottomRow
= internalYToRow(top
+cw
);
5964 for (i
=topRow
; i
<bottomRow
; i
++)
5966 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5967 if (cell_cols
< subtract_cols
)
5968 subtract_cols
= cell_cols
;
5970 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5971 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5972 rect
.width
= cw
- rect
.x
;
5975 m_gridWin
->Refresh( false, &rect
);
5978 ShowCellEditControl();
5985 // ------ interaction with data model
5987 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5989 switch ( msg
.GetId() )
5991 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5992 return GetModelValues();
5994 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5995 return SetModelValues();
5997 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5998 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5999 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6000 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6001 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6002 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6003 return Redimension( msg
);
6012 // The behaviour of this function depends on the grid table class
6013 // Clear() function. For the default wxGridStringTable class the
6014 // behavious is to replace all cell contents with wxEmptyString but
6015 // not to change the number of rows or cols.
6017 void wxGrid::ClearGrid()
6021 if (IsCellEditControlEnabled())
6022 DisableCellEditControl();
6025 if ( !GetBatchCount() ) m_gridWin
->Refresh();
6030 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6032 // TODO: something with updateLabels flag
6036 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6042 if (IsCellEditControlEnabled())
6043 DisableCellEditControl();
6045 bool done
= m_table
->InsertRows( pos
, numRows
);
6048 // the table will have sent the results of the insert row
6049 // operation to this view object as a grid table message
6055 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6057 // TODO: something with updateLabels flag
6061 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6067 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6069 // the table will have sent the results of the append row
6070 // operation to this view object as a grid table message
6076 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6078 // TODO: something with updateLabels flag
6082 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6088 if (IsCellEditControlEnabled())
6089 DisableCellEditControl();
6091 bool done
= m_table
->DeleteRows( pos
, numRows
);
6093 // the table will have sent the results of the delete row
6094 // operation to this view object as a grid table message
6100 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6102 // TODO: something with updateLabels flag
6106 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6112 if (IsCellEditControlEnabled())
6113 DisableCellEditControl();
6115 bool done
= m_table
->InsertCols( pos
, numCols
);
6117 // the table will have sent the results of the insert col
6118 // operation to this view object as a grid table message
6124 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6126 // TODO: something with updateLabels flag
6130 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6136 bool done
= m_table
->AppendCols( numCols
);
6138 // the table will have sent the results of the append col
6139 // operation to this view object as a grid table message
6145 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6147 // TODO: something with updateLabels flag
6151 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6157 if (IsCellEditControlEnabled())
6158 DisableCellEditControl();
6160 bool done
= m_table
->DeleteCols( pos
, numCols
);
6162 // the table will have sent the results of the delete col
6163 // operation to this view object as a grid table message
6171 // ----- event handlers
6174 // Generate a grid event based on a mouse event and
6175 // return the result of ProcessEvent()
6177 int wxGrid::SendEvent( const wxEventType type
,
6179 wxMouseEvent
& mouseEv
)
6184 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6186 int rowOrCol
= (row
== -1 ? col
: row
);
6188 wxGridSizeEvent
gridEvt( GetId(),
6192 mouseEv
.GetX() + GetRowLabelSize(),
6193 mouseEv
.GetY() + GetColLabelSize(),
6194 mouseEv
.ControlDown(),
6195 mouseEv
.ShiftDown(),
6197 mouseEv
.MetaDown() );
6199 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6200 vetoed
= !gridEvt
.IsAllowed();
6202 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6204 // Right now, it should _never_ end up here!
6205 wxGridRangeSelectEvent
gridEvt( GetId(),
6209 m_selectingBottomRight
,
6211 mouseEv
.ControlDown(),
6212 mouseEv
.ShiftDown(),
6214 mouseEv
.MetaDown() );
6216 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6217 vetoed
= !gridEvt
.IsAllowed();
6221 wxGridEvent
gridEvt( GetId(),
6225 mouseEv
.GetX() + GetRowLabelSize(),
6226 mouseEv
.GetY() + GetColLabelSize(),
6228 mouseEv
.ControlDown(),
6229 mouseEv
.ShiftDown(),
6231 mouseEv
.MetaDown() );
6232 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6233 vetoed
= !gridEvt
.IsAllowed();
6236 // A Veto'd event may not be `claimed' so test this first
6240 return claimed
? 1 : 0;
6244 // Generate a grid event of specified type and return the result
6245 // of ProcessEvent().
6247 int wxGrid::SendEvent( const wxEventType type
,
6253 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6255 int rowOrCol
= (row
== -1 ? col
: row
);
6257 wxGridSizeEvent
gridEvt( GetId(),
6262 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6263 vetoed
= !gridEvt
.IsAllowed();
6267 wxGridEvent
gridEvt( GetId(),
6272 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6273 vetoed
= !gridEvt
.IsAllowed();
6276 // A Veto'd event may not be `claimed' so test this first
6280 return claimed
? 1 : 0;
6283 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6285 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6288 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6290 // Don't do anything if between Begin/EndBatch...
6291 // EndBatch() will do all this on the last nested one anyway.
6292 if (! GetBatchCount())
6294 // Refresh to get correct scrolled position:
6295 wxScrolledWindow::Refresh(eraseb
, rect
);
6299 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6300 int width_label
, width_cell
, height_label
, height_cell
;
6303 //Copy rectangle can get scroll offsets..
6304 rect_x
= rect
->GetX();
6305 rect_y
= rect
->GetY();
6306 rectWidth
= rect
->GetWidth();
6307 rectHeight
= rect
->GetHeight();
6309 width_label
= m_rowLabelWidth
- rect_x
;
6310 if (width_label
> rectWidth
)
6311 width_label
= rectWidth
;
6313 height_label
= m_colLabelHeight
- rect_y
;
6314 if (height_label
> rectHeight
) height_label
= rectHeight
;
6316 if (rect_x
> m_rowLabelWidth
)
6318 x
= rect_x
- m_rowLabelWidth
;
6319 width_cell
= rectWidth
;
6324 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6327 if (rect_y
> m_colLabelHeight
)
6329 y
= rect_y
- m_colLabelHeight
;
6330 height_cell
= rectHeight
;
6335 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6338 // Paint corner label part intersecting rect.
6339 if ( width_label
> 0 && height_label
> 0 )
6341 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6342 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6345 // Paint col labels part intersecting rect.
6346 if ( width_cell
> 0 && height_label
> 0 )
6348 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6349 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6352 // Paint row labels part intersecting rect.
6353 if ( width_label
> 0 && height_cell
> 0 )
6355 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6356 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6359 // Paint cell area part intersecting rect.
6360 if ( width_cell
> 0 && height_cell
> 0 )
6362 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6363 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6368 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6369 m_colLabelWin
->Refresh(eraseb
, NULL
);
6370 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6371 m_gridWin
->Refresh(eraseb
, NULL
);
6376 void wxGrid::OnSize( wxSizeEvent
& event
)
6378 // position the child windows
6381 // don't call CalcDimensions() from here, the base class handles the size
6387 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6389 if ( m_inOnKeyDown
)
6391 // shouldn't be here - we are going round in circles...
6393 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6396 m_inOnKeyDown
= true;
6398 // propagate the event up and see if it gets processed
6400 wxWindow
*parent
= GetParent();
6401 wxKeyEvent
keyEvt( event
);
6402 keyEvt
.SetEventObject( parent
);
6404 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6407 // try local handlers
6409 switch ( event
.GetKeyCode() )
6412 if ( event
.ControlDown() )
6414 MoveCursorUpBlock( event
.ShiftDown() );
6418 MoveCursorUp( event
.ShiftDown() );
6423 if ( event
.ControlDown() )
6425 MoveCursorDownBlock( event
.ShiftDown() );
6429 MoveCursorDown( event
.ShiftDown() );
6434 if ( event
.ControlDown() )
6436 MoveCursorLeftBlock( event
.ShiftDown() );
6440 MoveCursorLeft( event
.ShiftDown() );
6445 if ( event
.ControlDown() )
6447 MoveCursorRightBlock( event
.ShiftDown() );
6451 MoveCursorRight( event
.ShiftDown() );
6456 case WXK_NUMPAD_ENTER
:
6457 if ( event
.ControlDown() )
6459 event
.Skip(); // to let the edit control have the return
6463 if ( GetGridCursorRow() < GetNumberRows()-1 )
6465 MoveCursorDown( event
.ShiftDown() );
6469 // at the bottom of a column
6470 DisableCellEditControl();
6480 if (event
.ShiftDown())
6482 if ( GetGridCursorCol() > 0 )
6484 MoveCursorLeft( false );
6489 DisableCellEditControl();
6494 if ( GetGridCursorCol() < GetNumberCols()-1 )
6496 MoveCursorRight( false );
6501 DisableCellEditControl();
6507 if ( event
.ControlDown() )
6509 MakeCellVisible( 0, 0 );
6510 SetCurrentCell( 0, 0 );
6519 if ( event
.ControlDown() )
6521 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6522 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6539 if ( event
.ControlDown() )
6543 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6544 m_currentCellCoords
.GetCol(),
6545 event
.ControlDown(),
6552 if ( !IsEditable() )
6554 MoveCursorRight( false );
6557 // Otherwise fall through to default
6565 m_inOnKeyDown
= false;
6568 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6570 // try local handlers
6572 if ( event
.GetKeyCode() == WXK_SHIFT
)
6574 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6575 m_selectingBottomRight
!= wxGridNoCellCoords
)
6579 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6580 m_selectingTopLeft
.GetCol(),
6581 m_selectingBottomRight
.GetRow(),
6582 m_selectingBottomRight
.GetCol(),
6583 event
.ControlDown(),
6590 m_selectingTopLeft
= wxGridNoCellCoords
;
6591 m_selectingBottomRight
= wxGridNoCellCoords
;
6592 m_selectingKeyboard
= wxGridNoCellCoords
;
6596 void wxGrid::OnChar( wxKeyEvent
& event
)
6598 // is it possible to edit the current cell at all?
6599 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6601 // yes, now check whether the cells editor accepts the key
6602 int row
= m_currentCellCoords
.GetRow();
6603 int col
= m_currentCellCoords
.GetCol();
6604 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6605 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6607 // <F2> is special and will always start editing, for
6608 // other keys - ask the editor itself
6609 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6610 || editor
->IsAcceptedKey(event
) )
6612 // ensure cell is visble
6613 MakeCellVisible(row
, col
);
6614 EnableCellEditControl();
6616 // a problem can arise if the cell is not completely
6617 // visible (even after calling MakeCellVisible the
6618 // control is not created and calling StartingKey will
6620 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6621 editor
->StartingKey(event
);
6638 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6642 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6644 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6646 // the event has been intercepted - do nothing
6650 wxClientDC
dc(m_gridWin
);
6653 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6655 DisableCellEditControl();
6657 if ( IsVisible( m_currentCellCoords
, false ) )
6660 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6661 if ( !m_gridLinesEnabled
)
6669 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6671 // Otherwise refresh redraws the highlight!
6672 m_currentCellCoords
= coords
;
6674 DrawGridCellArea(dc
,cells
);
6675 DrawAllGridLines( dc
, r
);
6679 m_currentCellCoords
= coords
;
6681 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6682 DrawCellHighlight(dc
, attr
);
6687 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6690 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6694 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6697 rightCol
= GetNumberCols() - 1;
6699 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6702 bottomRow
= GetNumberRows() - 1;
6706 if ( topRow
> bottomRow
)
6713 if ( leftCol
> rightCol
)
6720 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6721 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6723 // First the case that we selected a completely new area
6724 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6725 m_selectingBottomRight
== wxGridNoCellCoords
)
6728 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6729 wxGridCellCoords ( bottomRow
, rightCol
) );
6730 m_gridWin
->Refresh( false, &rect
);
6732 // Now handle changing an existing selection area.
6733 else if ( m_selectingTopLeft
!= updateTopLeft
||
6734 m_selectingBottomRight
!= updateBottomRight
)
6736 // Compute two optimal update rectangles:
6737 // Either one rectangle is a real subset of the
6738 // other, or they are (almost) disjoint!
6740 bool need_refresh
[4];
6744 need_refresh
[3] = false;
6747 // Store intermediate values
6748 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6749 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6750 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6751 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6753 // Determine the outer/inner coordinates.
6754 if (oldLeft
> leftCol
)
6760 if (oldTop
> topRow
)
6766 if (oldRight
< rightCol
)
6769 oldRight
= rightCol
;
6772 if (oldBottom
< bottomRow
)
6775 oldBottom
= bottomRow
;
6779 // Now, either the stuff marked old is the outer
6780 // rectangle or we don't have a situation where one
6781 // is contained in the other.
6783 if ( oldLeft
< leftCol
)
6785 // Refresh the newly selected or deselected
6786 // area to the left of the old or new selection.
6787 need_refresh
[0] = true;
6788 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6790 wxGridCellCoords ( oldBottom
,
6794 if ( oldTop
< topRow
)
6796 // Refresh the newly selected or deselected
6797 // area above the old or new selection.
6798 need_refresh
[1] = true;
6799 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6801 wxGridCellCoords ( topRow
- 1,
6805 if ( oldRight
> rightCol
)
6807 // Refresh the newly selected or deselected
6808 // area to the right of the old or new selection.
6809 need_refresh
[2] = true;
6810 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6812 wxGridCellCoords ( oldBottom
,
6816 if ( oldBottom
> bottomRow
)
6818 // Refresh the newly selected or deselected
6819 // area below the old or new selection.
6820 need_refresh
[3] = true;
6821 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6823 wxGridCellCoords ( oldBottom
,
6827 // various Refresh() calls
6828 for (i
= 0; i
< 4; i
++ )
6829 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6830 m_gridWin
->Refresh( false, &(rect
[i
]) );
6833 m_selectingTopLeft
= updateTopLeft
;
6834 m_selectingBottomRight
= updateBottomRight
;
6838 // ------ functions to get/send data (see also public functions)
6841 bool wxGrid::GetModelValues()
6843 // Hide the editor, so it won't hide a changed value.
6844 HideCellEditControl();
6848 // all we need to do is repaint the grid
6850 m_gridWin
->Refresh();
6858 bool wxGrid::SetModelValues()
6862 // Disable the editor, so it won't hide a changed value.
6863 // Do we also want to save the current value of the editor first?
6865 DisableCellEditControl();
6869 for ( row
= 0; row
< m_numRows
; row
++ )
6871 for ( col
= 0; col
< m_numCols
; col
++ )
6873 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6885 // Note - this function only draws cells that are in the list of
6886 // exposed cells (usually set from the update region by
6887 // CalcExposedCells)
6889 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6891 if ( !m_numRows
|| !m_numCols
)
6894 int i
, numCells
= cells
.GetCount();
6895 int row
, col
, cell_rows
, cell_cols
;
6896 wxGridCellCoordsArray redrawCells
;
6898 for ( i
= numCells
-1; i
>= 0; i
-- )
6900 row
= cells
[i
].GetRow();
6901 col
= cells
[i
].GetCol();
6902 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6904 // If this cell is part of a multicell block, find owner for repaint
6905 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6907 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6908 bool marked
= false;
6909 for ( int j
= 0; j
< numCells
; j
++ )
6911 if ( cell
== cells
[j
] )
6919 int count
= redrawCells
.GetCount();
6920 for (int j
= 0; j
< count
; j
++)
6922 if ( cell
== redrawCells
[j
] )
6929 redrawCells
.Add( cell
);
6931 continue; // don't bother drawing this cell
6934 // If this cell is empty, find cell to left that might want to overflow
6935 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6937 for ( int l
= 0; l
< cell_rows
; l
++ )
6939 // find a cell in this row to left alreay marked for repaint
6941 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6942 if ((redrawCells
[k
].GetCol() < left
) &&
6943 (redrawCells
[k
].GetRow() == row
))
6944 left
= redrawCells
[k
].GetCol();
6947 left
= 0; // oh well
6949 for (int j
= col
-1; j
>= left
; j
--)
6951 if (!m_table
->IsEmptyCell(row
+l
, j
))
6953 if (GetCellOverflow(row
+l
, j
))
6955 wxGridCellCoords
cell(row
+l
, j
);
6956 bool marked
= false;
6958 for (int k
= 0; k
< numCells
; k
++)
6960 if ( cell
== cells
[k
] )
6969 int count
= redrawCells
.GetCount();
6970 for (int k
= 0; k
< count
; k
++)
6972 if ( cell
== redrawCells
[k
] )
6979 redrawCells
.Add( cell
);
6988 DrawCell( dc
, cells
[i
] );
6991 numCells
= redrawCells
.GetCount();
6993 for ( i
= numCells
- 1; i
>= 0; i
-- )
6995 DrawCell( dc
, redrawCells
[i
] );
7000 void wxGrid::DrawGridSpace( wxDC
& dc
)
7003 m_gridWin
->GetClientSize( &cw
, &ch
);
7006 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7008 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
7009 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
7011 if ( right
> rightCol
|| bottom
> bottomRow
)
7014 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7016 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7017 dc
.SetPen( *wxTRANSPARENT_PEN
);
7019 if ( right
> rightCol
)
7021 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7024 if ( bottom
> bottomRow
)
7026 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7032 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7034 int row
= coords
.GetRow();
7035 int col
= coords
.GetCol();
7037 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7040 // we draw the cell border ourselves
7041 #if !WXGRID_DRAW_LINES
7042 if ( m_gridLinesEnabled
)
7043 DrawCellBorder( dc
, coords
);
7046 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7048 bool isCurrent
= coords
== m_currentCellCoords
;
7050 wxRect rect
= CellToRect( row
, col
);
7052 // if the editor is shown, we should use it and not the renderer
7053 // Note: However, only if it is really _shown_, i.e. not hidden!
7054 if ( isCurrent
&& IsCellEditControlShown() )
7056 // OSAF NB: this "#if..." is temporary and fixes a problem where the
7057 // edit control is erased by this code after being rendered.
7058 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7059 // implicitly, causing this out-of order render.
7060 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7061 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7062 editor
->PaintBackground(rect
, attr
);
7068 // but all the rest is drawn by the cell renderer and hence may be
7070 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7071 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7078 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7080 int row
= m_currentCellCoords
.GetRow();
7081 int col
= m_currentCellCoords
.GetCol();
7083 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7086 wxRect rect
= CellToRect(row
, col
);
7088 // hmmm... what could we do here to show that the cell is disabled?
7089 // for now, I just draw a thinner border than for the other ones, but
7090 // it doesn't look really good
7092 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7096 // The center of th drawn line is where the position/width/height of
7097 // the rectangle is actually at, (on wxMSW atr least,) so we will
7098 // reduce the size of the rectangle to compensate for the thickness of
7099 // the line. If this is too strange on non wxMSW platforms then
7100 // please #ifdef this appropriately.
7101 rect
.x
+= penWidth
/ 2;
7102 rect
.y
+= penWidth
/ 2;
7103 rect
.width
-= penWidth
- 1;
7104 rect
.height
-= penWidth
- 1;
7107 // Now draw the rectangle
7108 // use the cellHighlightColour if the cell is inside a selection, this
7109 // will ensure the cell is always visible.
7110 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7111 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7112 dc
.DrawRectangle(rect
);
7116 // VZ: my experiments with 3d borders...
7118 // how to properly set colours for arbitrary bg?
7119 wxCoord x1
= rect
.x
,
7121 x2
= rect
.x
+ rect
.width
- 1,
7122 y2
= rect
.y
+ rect
.height
- 1;
7124 dc
.SetPen(*wxWHITE_PEN
);
7125 dc
.DrawLine(x1
, y1
, x2
, y1
);
7126 dc
.DrawLine(x1
, y1
, x1
, y2
);
7128 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7129 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7131 dc
.SetPen(*wxBLACK_PEN
);
7132 dc
.DrawLine(x1
, y2
, x2
, y2
);
7133 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7138 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7140 int row
= coords
.GetRow();
7141 int col
= coords
.GetCol();
7142 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7145 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7147 wxRect rect
= CellToRect( row
, col
);
7149 // right hand border
7151 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7152 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7156 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7157 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7160 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7162 // This if block was previously in wxGrid::OnPaint but that doesn't
7163 // seem to get called under wxGTK - MB
7165 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7166 m_numRows
&& m_numCols
)
7168 m_currentCellCoords
.Set(0, 0);
7171 if ( IsCellEditControlShown() )
7173 // don't show highlight when the edit control is shown
7177 // if the active cell was repainted, repaint its highlight too because it
7178 // might have been damaged by the grid lines
7179 size_t count
= cells
.GetCount();
7180 for ( size_t n
= 0; n
< count
; n
++ )
7182 if ( cells
[n
] == m_currentCellCoords
)
7184 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7185 DrawCellHighlight(dc
, attr
);
7193 // TODO: remove this ???
7194 // This is used to redraw all grid lines e.g. when the grid line colour
7197 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7199 #if !WXGRID_DRAW_LINES
7203 if ( !m_gridLinesEnabled
||
7208 int top
, bottom
, left
, right
;
7210 #if 0 //#ifndef __WXGTK__
7214 m_gridWin
->GetClientSize(&cw
, &ch
);
7216 // virtual coords of visible area
7218 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7219 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7224 reg
.GetBox(x
, y
, w
, h
);
7225 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7226 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7230 m_gridWin
->GetClientSize(&cw
, &ch
);
7231 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7232 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7235 // avoid drawing grid lines past the last row and col
7237 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7238 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7240 // no gridlines inside multicells, clip them out
7241 int leftCol
= internalXToCol(left
);
7242 int topRow
= internalYToRow(top
);
7243 int rightCol
= internalXToCol(right
);
7244 int bottomRow
= internalYToRow(bottom
);
7247 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7248 wxRegion
clippedcells(0, 0, cw
, ch
);
7250 int i
, j
, cell_rows
, cell_cols
;
7253 for (j
=topRow
; j
<bottomRow
; j
++)
7255 for (i
=leftCol
; i
<rightCol
; i
++)
7257 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7258 if ((cell_rows
> 1) || (cell_cols
> 1))
7260 rect
= CellToRect(j
,i
);
7261 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7262 clippedcells
.Subtract(rect
);
7264 else if ((cell_rows
< 0) || (cell_cols
< 0))
7266 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7267 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7268 clippedcells
.Subtract(rect
);
7273 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7275 int i
, j
, cell_rows
, cell_cols
;
7278 for (j
=topRow
; j
<bottomRow
; j
++)
7280 for (i
=leftCol
; i
<rightCol
; i
++)
7282 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7283 if ((cell_rows
> 1) || (cell_cols
> 1))
7285 rect
= CellToRect(j
,i
);
7286 clippedcells
.Subtract(rect
);
7288 else if ((cell_rows
< 0) || (cell_cols
< 0))
7290 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7291 clippedcells
.Subtract(rect
);
7296 dc
.SetClippingRegion( clippedcells
);
7298 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7300 // horizontal grid lines
7302 // already declared above - int i;
7303 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7305 int bot
= GetRowBottom(i
) - 1;
7314 dc
.DrawLine( left
, bot
, right
, bot
);
7318 // vertical grid lines
7320 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7322 int colRight
= GetColRight(i
) - 1;
7323 if ( colRight
> right
)
7328 if ( colRight
>= left
)
7330 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7333 dc
.DestroyClippingRegion();
7336 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7342 size_t numLabels
= rows
.GetCount();
7344 for ( i
= 0; i
< numLabels
; i
++ )
7346 DrawRowLabel( dc
, rows
[i
] );
7350 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7352 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7358 rect
.SetY( GetRowTop(row
) + 1 );
7359 rect
.SetWidth( m_rowLabelWidth
- 2 );
7360 rect
.SetHeight( GetRowHeight(row
) - 2 );
7362 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7364 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7366 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7368 int rowTop
= GetRowTop(row
),
7369 rowBottom
= GetRowBottom(row
) - 1;
7371 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7372 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7373 m_rowLabelWidth
-1, rowBottom
);
7375 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7377 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7379 dc
.SetPen( *wxWHITE_PEN
);
7380 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7381 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7383 dc
.SetBackgroundMode( wxTRANSPARENT
);
7384 dc
.SetTextForeground( GetLabelTextColour() );
7385 dc
.SetFont( GetLabelFont() );
7388 GetRowLabelAlignment( &hAlign
, &vAlign
);
7391 rect
.SetY( GetRowTop(row
) + 2 );
7392 rect
.SetWidth( m_rowLabelWidth
- 4 );
7393 rect
.SetHeight( GetRowHeight(row
) - 4 );
7394 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7397 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7403 size_t numLabels
= cols
.GetCount();
7405 for ( i
= 0; i
< numLabels
; i
++ )
7407 DrawColLabel( dc
, cols
[i
] );
7411 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7413 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7416 int colLeft
= GetColLeft(col
);
7420 rect
.SetX( colLeft
+ 1 );
7422 rect
.SetWidth( GetColWidth(col
) - 2 );
7423 rect
.SetHeight( m_colLabelHeight
- 2 );
7425 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7427 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7429 int colRight
= GetColRight(col
) - 1;
7431 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7432 dc
.DrawLine( colRight
, 0,
7433 colRight
, m_colLabelHeight
-1 );
7435 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7437 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7438 colRight
+1, m_colLabelHeight
-1 );
7440 dc
.SetPen( *wxWHITE_PEN
);
7441 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7442 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7444 dc
.SetBackgroundMode( wxTRANSPARENT
);
7445 dc
.SetTextForeground( GetLabelTextColour() );
7446 dc
.SetFont( GetLabelFont() );
7448 int hAlign
, vAlign
, orient
;
7449 GetColLabelAlignment( &hAlign
, &vAlign
);
7450 orient
= GetColLabelTextOrientation();
7452 rect
.SetX( colLeft
+ 2 );
7454 rect
.SetWidth( GetColWidth(col
) - 4 );
7455 rect
.SetHeight( m_colLabelHeight
- 4 );
7456 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7459 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7460 const wxString
& value
,
7464 int textOrientation
)
7466 wxArrayString lines
;
7468 StringToLines( value
, lines
);
7470 //Forward to new API.
7471 DrawTextRectangle( dc
,
7480 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7481 const wxArrayString
& lines
,
7485 int textOrientation
)
7487 long textWidth
= 0, textHeight
= 0;
7488 long lineWidth
= 0, lineHeight
= 0;
7491 dc
.SetClippingRegion( rect
);
7493 nLines
= lines
.GetCount();
7497 float x
= 0.0, y
= 0.0;
7499 if ( textOrientation
== wxHORIZONTAL
)
7500 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7502 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7504 switch ( vertAlign
)
7506 case wxALIGN_BOTTOM
:
7507 if ( textOrientation
== wxHORIZONTAL
)
7508 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7510 x
= rect
.x
+ rect
.width
- textWidth
;
7513 case wxALIGN_CENTRE
:
7514 if ( textOrientation
== wxHORIZONTAL
)
7515 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7517 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7522 if ( textOrientation
== wxHORIZONTAL
)
7529 // Align each line of a multi-line label
7530 for ( l
= 0; l
< nLines
; l
++ )
7532 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7534 switch ( horizAlign
)
7537 if ( textOrientation
== wxHORIZONTAL
)
7538 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7540 y
= rect
.y
+ lineWidth
+ 1;
7543 case wxALIGN_CENTRE
:
7544 if ( textOrientation
== wxHORIZONTAL
)
7545 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7547 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7552 if ( textOrientation
== wxHORIZONTAL
)
7555 y
= rect
.y
+ rect
.height
- 1;
7559 if ( textOrientation
== wxHORIZONTAL
)
7561 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7566 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7572 dc
.DestroyClippingRegion();
7576 // Split multi line text up into an array of strings. Any existing
7577 // contents of the string array are preserved.
7579 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7583 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7584 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7586 while ( startPos
< (int)tVal
.Length() )
7588 pos
= tVal
.Mid(startPos
).Find( eol
);
7593 else if ( pos
== 0 )
7595 lines
.Add( wxEmptyString
);
7599 lines
.Add( value
.Mid(startPos
, pos
) );
7601 startPos
+= pos
+ 1;
7604 if ( startPos
< (int)value
.Length() )
7606 lines
.Add( value
.Mid( startPos
) );
7611 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
7612 const wxArrayString
& lines
,
7613 long *width
, long *height
)
7617 long lineW
= 0, lineH
= 0;
7620 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7622 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7623 w
= wxMax( w
, lineW
);
7632 // ------ Batch processing.
7634 void wxGrid::EndBatch()
7636 if ( m_batchCount
> 0 )
7639 if ( !m_batchCount
)
7642 m_rowLabelWin
->Refresh();
7643 m_colLabelWin
->Refresh();
7644 m_cornerLabelWin
->Refresh();
7645 m_gridWin
->Refresh();
7650 // Use this, rather than wxWindow::Refresh(), to force an immediate
7651 // repainting of the grid. Has no effect if you are already inside a
7652 // BeginBatch / EndBatch block.
7654 void wxGrid::ForceRefresh()
7660 bool wxGrid::Enable(bool enable
)
7662 if ( !wxScrolledWindow::Enable(enable
) )
7665 // redraw in the new state
7666 m_gridWin
->Refresh();
7673 // ------ Edit control functions
7676 void wxGrid::EnableEditing( bool edit
)
7678 // TODO: improve this ?
7680 if ( edit
!= m_editable
)
7683 EnableCellEditControl(edit
);
7688 void wxGrid::EnableCellEditControl( bool enable
)
7693 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7694 SetCurrentCell( 0, 0 );
7696 if ( enable
!= m_cellEditCtrlEnabled
)
7700 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7703 // this should be checked by the caller!
7704 wxASSERT_MSG( CanEnableCellControl(),
7705 _T("can't enable editing for this cell!") );
7707 // do it before ShowCellEditControl()
7708 m_cellEditCtrlEnabled
= enable
;
7710 ShowCellEditControl();
7714 //FIXME:add veto support
7715 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7717 HideCellEditControl();
7718 SaveEditControlValue();
7720 // do it after HideCellEditControl()
7721 m_cellEditCtrlEnabled
= enable
;
7726 bool wxGrid::IsCurrentCellReadOnly() const
7729 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7730 bool readonly
= attr
->IsReadOnly();
7736 bool wxGrid::CanEnableCellControl() const
7738 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7739 !IsCurrentCellReadOnly();
7743 bool wxGrid::IsCellEditControlEnabled() const
7745 // the cell edit control might be disable for all cells or just for the
7746 // current one if it's read only
7747 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7750 bool wxGrid::IsCellEditControlShown() const
7752 bool isShown
= false;
7754 if ( m_cellEditCtrlEnabled
)
7756 int row
= m_currentCellCoords
.GetRow();
7757 int col
= m_currentCellCoords
.GetCol();
7758 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7759 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7764 if ( editor
->IsCreated() )
7766 isShown
= editor
->GetControl()->IsShown();
7776 void wxGrid::ShowCellEditControl()
7778 if ( IsCellEditControlEnabled() )
7780 if ( !IsVisible( m_currentCellCoords
) )
7782 m_cellEditCtrlEnabled
= false;
7787 wxRect rect
= CellToRect( m_currentCellCoords
);
7788 int row
= m_currentCellCoords
.GetRow();
7789 int col
= m_currentCellCoords
.GetCol();
7791 // if this is part of a multicell, find owner (topleft)
7792 int cell_rows
, cell_cols
;
7793 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7794 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7798 m_currentCellCoords
.SetRow( row
);
7799 m_currentCellCoords
.SetCol( col
);
7802 // convert to scrolled coords
7804 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7806 // done in PaintBackground()
7808 // erase the highlight and the cell contents because the editor
7809 // might not cover the entire cell
7810 wxClientDC
dc( m_gridWin
);
7812 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7813 dc
.SetPen(*wxTRANSPARENT_PEN
);
7814 dc
.DrawRectangle(rect
);
7817 // cell is shifted by one pixel
7818 // However, don't allow x or y to become negative
7819 // since the SetSize() method interprets that as
7826 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7827 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7828 if ( !editor
->IsCreated() )
7830 editor
->Create(m_gridWin
, wxID_ANY
,
7831 new wxGridCellEditorEvtHandler(this, editor
));
7833 wxGridEditorCreatedEvent
evt(GetId(),
7834 wxEVT_GRID_EDITOR_CREATED
,
7838 editor
->GetControl());
7839 GetEventHandler()->ProcessEvent(evt
);
7843 // resize editor to overflow into righthand cells if allowed
7844 int maxWidth
= rect
.width
;
7845 wxString value
= GetCellValue(row
, col
);
7846 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7849 GetTextExtent(value
, &maxWidth
, &y
,
7850 NULL
, NULL
, &attr
->GetFont());
7851 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7854 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7855 if (rect
.x
+maxWidth
> client_right
)
7856 maxWidth
= client_right
- rect
.x
;
7858 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7860 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7861 // may have changed earlier
7862 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7865 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7866 // looks weird going over a multicell
7867 if (m_table
->IsEmptyCell(row
,i
) &&
7868 (rect
.width
< maxWidth
) && (c_rows
== 1))
7869 rect
.width
+= GetColWidth(i
);
7874 if (rect
.GetRight() > client_right
)
7875 rect
.SetRight(client_right
- 1);
7878 editor
->SetCellAttr(attr
);
7879 editor
->SetSize( rect
);
7880 editor
->Show( true, attr
);
7882 // recalc dimensions in case we need to
7883 // expand the scrolled window to account for editor
7886 editor
->BeginEdit(row
, col
, this);
7887 editor
->SetCellAttr(NULL
);
7895 void wxGrid::HideCellEditControl()
7897 if ( IsCellEditControlEnabled() )
7899 int row
= m_currentCellCoords
.GetRow();
7900 int col
= m_currentCellCoords
.GetCol();
7902 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7903 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7904 editor
->Show( false );
7908 m_gridWin
->SetFocus();
7910 // refresh whole row to the right
7911 wxRect
rect( CellToRect(row
, col
) );
7912 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7913 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7915 // ensure that the pixels under the focus ring get refreshed as well
7916 rect
.Inflate(10, 10);
7918 m_gridWin
->Refresh( false, &rect
);
7922 void wxGrid::SaveEditControlValue()
7924 if ( IsCellEditControlEnabled() )
7926 int row
= m_currentCellCoords
.GetRow();
7927 int col
= m_currentCellCoords
.GetCol();
7929 wxString oldval
= GetCellValue(row
, col
);
7931 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7932 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7933 bool changed
= editor
->EndEdit(row
, col
, this);
7940 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7941 m_currentCellCoords
.GetRow(),
7942 m_currentCellCoords
.GetCol() ) < 0 )
7944 // Event has been vetoed, set the data back.
7945 SetCellValue(row
, col
, oldval
);
7953 // ------ Grid location functions
7954 // Note that all of these functions work with the logical coordinates of
7955 // grid cells and labels so you will need to convert from device
7956 // coordinates for mouse events etc.
7959 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7961 int row
= YToRow(y
);
7962 int col
= XToCol(x
);
7964 if ( row
== -1 || col
== -1 )
7966 coords
= wxGridNoCellCoords
;
7970 coords
.Set( row
, col
);
7975 // Internal Helper function for computing row or column from some
7976 // (unscrolled) coordinate value, using either
7977 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7978 // of m_rowBottoms/m_ColRights to speed up the search!
7980 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7981 const wxArrayInt
& BorderArray
, int nMax
,
7985 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7990 size_t i_max
= coord
/ defaultDist
,
7993 if (BorderArray
.IsEmpty())
7995 if ((int) i_max
< nMax
)
7997 return clipToMinMax
? nMax
- 1 : -1;
8000 if ( i_max
>= BorderArray
.GetCount())
8001 i_max
= BorderArray
.GetCount() - 1;
8004 if ( coord
>= BorderArray
[i_max
])
8008 i_max
= coord
/ minDist
;
8010 i_max
= BorderArray
.GetCount() - 1;
8013 if ( i_max
>= BorderArray
.GetCount())
8014 i_max
= BorderArray
.GetCount() - 1;
8017 if ( coord
>= BorderArray
[i_max
])
8018 return clipToMinMax
? (int)i_max
: -1;
8019 if ( coord
< BorderArray
[0] )
8022 while ( i_max
- i_min
> 0 )
8024 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8025 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8026 if (coord
>= BorderArray
[ i_max
- 1])
8030 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8031 if (coord
< BorderArray
[median
])
8040 int wxGrid::YToRow( int y
)
8042 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8043 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8046 int wxGrid::XToCol( int x
)
8048 return CoordToRowOrCol(x
, m_defaultColWidth
,
8049 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8052 // return the row number that that the y coord is near the edge of, or
8053 // -1 if not near an edge
8055 int wxGrid::YToEdgeOfRow( int y
)
8058 i
= internalYToRow(y
);
8060 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8062 // We know that we are in row i, test whether we are
8063 // close enough to lower or upper border, respectively.
8064 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8066 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8073 // return the col number that that the x coord is near the edge of, or
8074 // -1 if not near an edge
8076 int wxGrid::XToEdgeOfCol( int x
)
8079 i
= internalXToCol(x
);
8081 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8083 // We know that we are in column i, test whether we are
8084 // close enough to right or left border, respectively.
8085 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8087 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8094 wxRect
wxGrid::CellToRect( int row
, int col
)
8096 wxRect
rect( -1, -1, -1, -1 );
8098 if ( row
>= 0 && row
< m_numRows
&&
8099 col
>= 0 && col
< m_numCols
)
8101 int i
, cell_rows
, cell_cols
;
8102 rect
.width
= rect
.height
= 0;
8103 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8104 // if negative then find multicell owner
8105 if (cell_rows
< 0) row
+= cell_rows
;
8106 if (cell_cols
< 0) col
+= cell_cols
;
8107 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8109 rect
.x
= GetColLeft(col
);
8110 rect
.y
= GetRowTop(row
);
8111 for (i
=col
; i
<col
+cell_cols
; i
++)
8112 rect
.width
+= GetColWidth(i
);
8113 for (i
=row
; i
<row
+cell_rows
; i
++)
8114 rect
.height
+= GetRowHeight(i
);
8117 // if grid lines are enabled, then the area of the cell is a bit smaller
8118 if (m_gridLinesEnabled
)
8127 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8129 // get the cell rectangle in logical coords
8131 wxRect
r( CellToRect( row
, col
) );
8133 // convert to device coords
8135 int left
, top
, right
, bottom
;
8136 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8137 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8139 // check against the client area of the grid window
8142 m_gridWin
->GetClientSize( &cw
, &ch
);
8144 if ( wholeCellVisible
)
8146 // is the cell wholly visible ?
8148 return ( left
>= 0 && right
<= cw
&&
8149 top
>= 0 && bottom
<= ch
);
8153 // is the cell partly visible ?
8155 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8156 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8160 // make the specified cell location visible by doing a minimal amount
8163 void wxGrid::MakeCellVisible( int row
, int col
)
8166 int xpos
= -1, ypos
= -1;
8168 if ( row
>= 0 && row
< m_numRows
&&
8169 col
>= 0 && col
< m_numCols
)
8171 // get the cell rectangle in logical coords
8173 wxRect
r( CellToRect( row
, col
) );
8175 // convert to device coords
8177 int left
, top
, right
, bottom
;
8178 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8179 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8182 m_gridWin
->GetClientSize( &cw
, &ch
);
8188 else if ( bottom
> ch
)
8190 int h
= r
.GetHeight();
8192 for ( i
= row
-1; i
>= 0; i
-- )
8194 int rowHeight
= GetRowHeight(i
);
8195 if ( h
+ rowHeight
> ch
)
8202 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8203 // have rounding errors (this is important, because if we do, we
8204 // might not scroll at all and some cells won't be redrawn)
8206 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8208 ypos
+= m_scrollLineY
;
8215 else if ( right
> cw
)
8217 // position the view so that the cell is on the right
8219 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8220 xpos
= x0
+ (right
- cw
);
8222 // see comment for ypos above
8223 xpos
+= m_scrollLineX
;
8226 if ( xpos
!= -1 || ypos
!= -1 )
8229 xpos
/= m_scrollLineX
;
8231 ypos
/= m_scrollLineY
;
8232 Scroll( xpos
, ypos
);
8240 // ------ Grid cursor movement functions
8243 bool wxGrid::MoveCursorUp( bool expandSelection
)
8245 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8246 m_currentCellCoords
.GetRow() >= 0 )
8248 if ( expandSelection
)
8250 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8251 m_selectingKeyboard
= m_currentCellCoords
;
8252 if ( m_selectingKeyboard
.GetRow() > 0 )
8254 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8255 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8256 m_selectingKeyboard
.GetCol() );
8257 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8260 else if ( m_currentCellCoords
.GetRow() > 0 )
8262 int row
= m_currentCellCoords
.GetRow() - 1;
8263 int col
= m_currentCellCoords
.GetCol();
8265 MakeCellVisible( row
, col
);
8266 SetCurrentCell( row
, col
);
8278 bool wxGrid::MoveCursorDown( bool expandSelection
)
8280 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8281 m_currentCellCoords
.GetRow() < m_numRows
)
8283 if ( expandSelection
)
8285 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8286 m_selectingKeyboard
= m_currentCellCoords
;
8287 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8289 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8290 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8291 m_selectingKeyboard
.GetCol() );
8292 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8295 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8297 int row
= m_currentCellCoords
.GetRow() + 1;
8298 int col
= m_currentCellCoords
.GetCol();
8300 MakeCellVisible( row
, col
);
8301 SetCurrentCell( row
, col
);
8312 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8314 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8315 m_currentCellCoords
.GetCol() >= 0 )
8317 if ( expandSelection
)
8319 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8320 m_selectingKeyboard
= m_currentCellCoords
;
8321 if ( m_selectingKeyboard
.GetCol() > 0 )
8323 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8324 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8325 m_selectingKeyboard
.GetCol() );
8326 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8329 else if ( m_currentCellCoords
.GetCol() > 0 )
8331 int row
= m_currentCellCoords
.GetRow();
8332 int col
= m_currentCellCoords
.GetCol() - 1;
8334 MakeCellVisible( row
, col
);
8335 SetCurrentCell( row
, col
);
8346 bool wxGrid::MoveCursorRight( bool expandSelection
)
8348 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8349 m_currentCellCoords
.GetCol() < m_numCols
)
8351 if ( expandSelection
)
8353 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8354 m_selectingKeyboard
= m_currentCellCoords
;
8355 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8357 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8358 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8359 m_selectingKeyboard
.GetCol() );
8360 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8363 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8365 int row
= m_currentCellCoords
.GetRow();
8366 int col
= m_currentCellCoords
.GetCol() + 1;
8368 MakeCellVisible( row
, col
);
8369 SetCurrentCell( row
, col
);
8380 bool wxGrid::MovePageUp()
8382 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8385 int row
= m_currentCellCoords
.GetRow();
8389 m_gridWin
->GetClientSize( &cw
, &ch
);
8391 int y
= GetRowTop(row
);
8392 int newRow
= internalYToRow( y
- ch
+ 1 );
8394 if ( newRow
== row
)
8396 //row > 0 , so newrow can never be less than 0 here.
8400 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8401 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8409 bool wxGrid::MovePageDown()
8411 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8414 int row
= m_currentCellCoords
.GetRow();
8415 if ( (row
+1) < m_numRows
)
8418 m_gridWin
->GetClientSize( &cw
, &ch
);
8420 int y
= GetRowTop(row
);
8421 int newRow
= internalYToRow( y
+ ch
);
8422 if ( newRow
== row
)
8424 // row < m_numRows , so newrow can't overflow here.
8428 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8429 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8437 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8440 m_currentCellCoords
!= wxGridNoCellCoords
&&
8441 m_currentCellCoords
.GetRow() > 0 )
8443 int row
= m_currentCellCoords
.GetRow();
8444 int col
= m_currentCellCoords
.GetCol();
8446 if ( m_table
->IsEmptyCell(row
, col
) )
8448 // starting in an empty cell: find the next block of
8454 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8458 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8460 // starting at the top of a block: find the next block
8466 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8472 // starting within a block: find the top of the block
8477 if ( m_table
->IsEmptyCell(row
, col
) )
8485 MakeCellVisible( row
, col
);
8486 if ( expandSelection
)
8488 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8489 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8494 SetCurrentCell( row
, col
);
8503 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8506 m_currentCellCoords
!= wxGridNoCellCoords
&&
8507 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8509 int row
= m_currentCellCoords
.GetRow();
8510 int col
= m_currentCellCoords
.GetCol();
8512 if ( m_table
->IsEmptyCell(row
, col
) )
8514 // starting in an empty cell: find the next block of
8517 while ( row
< m_numRows
-1 )
8520 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8524 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8526 // starting at the bottom of a block: find the next block
8529 while ( row
< m_numRows
-1 )
8532 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8538 // starting within a block: find the bottom of the block
8540 while ( row
< m_numRows
-1 )
8543 if ( m_table
->IsEmptyCell(row
, col
) )
8551 MakeCellVisible( row
, col
);
8552 if ( expandSelection
)
8554 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8555 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8560 SetCurrentCell( row
, col
);
8569 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8572 m_currentCellCoords
!= wxGridNoCellCoords
&&
8573 m_currentCellCoords
.GetCol() > 0 )
8575 int row
= m_currentCellCoords
.GetRow();
8576 int col
= m_currentCellCoords
.GetCol();
8578 if ( m_table
->IsEmptyCell(row
, col
) )
8580 // starting in an empty cell: find the next block of
8586 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8590 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8592 // starting at the left of a block: find the next block
8598 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8604 // starting within a block: find the left of the block
8609 if ( m_table
->IsEmptyCell(row
, col
) )
8617 MakeCellVisible( row
, col
);
8618 if ( expandSelection
)
8620 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8621 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8626 SetCurrentCell( row
, col
);
8635 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8638 m_currentCellCoords
!= wxGridNoCellCoords
&&
8639 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8641 int row
= m_currentCellCoords
.GetRow();
8642 int col
= m_currentCellCoords
.GetCol();
8644 if ( m_table
->IsEmptyCell(row
, col
) )
8646 // starting in an empty cell: find the next block of
8649 while ( col
< m_numCols
-1 )
8652 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8656 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8658 // starting at the right of a block: find the next block
8661 while ( col
< m_numCols
-1 )
8664 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8670 // starting within a block: find the right of the block
8672 while ( col
< m_numCols
-1 )
8675 if ( m_table
->IsEmptyCell(row
, col
) )
8683 MakeCellVisible( row
, col
);
8684 if ( expandSelection
)
8686 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8687 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8692 SetCurrentCell( row
, col
);
8704 // ------ Label values and formatting
8707 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8709 *horiz
= m_rowLabelHorizAlign
;
8710 *vert
= m_rowLabelVertAlign
;
8713 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8715 *horiz
= m_colLabelHorizAlign
;
8716 *vert
= m_colLabelVertAlign
;
8719 int wxGrid::GetColLabelTextOrientation()
8721 return m_colLabelTextOrientation
;
8724 wxString
wxGrid::GetRowLabelValue( int row
)
8728 return m_table
->GetRowLabelValue( row
);
8738 wxString
wxGrid::GetColLabelValue( int col
)
8742 return m_table
->GetColLabelValue( col
);
8753 void wxGrid::SetRowLabelSize( int width
)
8755 width
= wxMax( width
, 0 );
8756 if ( width
!= m_rowLabelWidth
)
8760 m_rowLabelWin
->Show( false );
8761 m_cornerLabelWin
->Show( false );
8763 else if ( m_rowLabelWidth
== 0 )
8765 m_rowLabelWin
->Show( true );
8766 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8769 m_rowLabelWidth
= width
;
8771 wxScrolledWindow::Refresh( true );
8776 void wxGrid::SetColLabelSize( int height
)
8778 height
= wxMax( height
, 0 );
8779 if ( height
!= m_colLabelHeight
)
8783 m_colLabelWin
->Show( false );
8784 m_cornerLabelWin
->Show( false );
8786 else if ( m_colLabelHeight
== 0 )
8788 m_colLabelWin
->Show( true );
8789 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8792 m_colLabelHeight
= height
;
8794 wxScrolledWindow::Refresh( true );
8799 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8801 if ( m_labelBackgroundColour
!= colour
)
8803 m_labelBackgroundColour
= colour
;
8804 m_rowLabelWin
->SetBackgroundColour( colour
);
8805 m_colLabelWin
->SetBackgroundColour( colour
);
8806 m_cornerLabelWin
->SetBackgroundColour( colour
);
8808 if ( !GetBatchCount() )
8810 m_rowLabelWin
->Refresh();
8811 m_colLabelWin
->Refresh();
8812 m_cornerLabelWin
->Refresh();
8817 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8819 if ( m_labelTextColour
!= colour
)
8821 m_labelTextColour
= colour
;
8822 if ( !GetBatchCount() )
8824 m_rowLabelWin
->Refresh();
8825 m_colLabelWin
->Refresh();
8830 void wxGrid::SetLabelFont( const wxFont
& font
)
8833 if ( !GetBatchCount() )
8835 m_rowLabelWin
->Refresh();
8836 m_colLabelWin
->Refresh();
8840 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8842 // allow old (incorrect) defs to be used
8845 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8846 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8847 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8852 case wxTOP
: vert
= wxALIGN_TOP
; break;
8853 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8854 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8857 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8859 m_rowLabelHorizAlign
= horiz
;
8862 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8864 m_rowLabelVertAlign
= vert
;
8867 if ( !GetBatchCount() )
8869 m_rowLabelWin
->Refresh();
8873 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8875 // allow old (incorrect) defs to be used
8878 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8879 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8880 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8885 case wxTOP
: vert
= wxALIGN_TOP
; break;
8886 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8887 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8890 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8892 m_colLabelHorizAlign
= horiz
;
8895 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8897 m_colLabelVertAlign
= vert
;
8900 if ( !GetBatchCount() )
8902 m_colLabelWin
->Refresh();
8906 // Note: under MSW, the default column label font must be changed because it
8907 // does not support vertical printing
8909 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8910 // pGrid->SetLabelFont(font);
8911 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8913 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8915 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8917 m_colLabelTextOrientation
= textOrientation
;
8920 if ( !GetBatchCount() )
8922 m_colLabelWin
->Refresh();
8926 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8930 m_table
->SetRowLabelValue( row
, s
);
8931 if ( !GetBatchCount() )
8933 wxRect rect
= CellToRect( row
, 0);
8934 if ( rect
.height
> 0 )
8936 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8938 rect
.width
= m_rowLabelWidth
;
8939 m_rowLabelWin
->Refresh( true, &rect
);
8945 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8949 m_table
->SetColLabelValue( col
, s
);
8950 if ( !GetBatchCount() )
8952 wxRect rect
= CellToRect( 0, col
);
8953 if ( rect
.width
> 0 )
8955 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8957 rect
.height
= m_colLabelHeight
;
8958 m_colLabelWin
->Refresh( true, &rect
);
8964 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8966 if ( m_gridLineColour
!= colour
)
8968 m_gridLineColour
= colour
;
8970 wxClientDC
dc( m_gridWin
);
8972 DrawAllGridLines( dc
, wxRegion() );
8977 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8979 if ( m_cellHighlightColour
!= colour
)
8981 m_cellHighlightColour
= colour
;
8983 wxClientDC
dc( m_gridWin
);
8985 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8986 DrawCellHighlight(dc
, attr
);
8991 void wxGrid::SetCellHighlightPenWidth(int width
)
8993 if (m_cellHighlightPenWidth
!= width
)
8995 m_cellHighlightPenWidth
= width
;
8997 // Just redrawing the cell highlight is not enough since that won't
8998 // make any visible change if the the thickness is getting smaller.
8999 int row
= m_currentCellCoords
.GetRow();
9000 int col
= m_currentCellCoords
.GetCol();
9001 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9003 wxRect rect
= CellToRect(row
, col
);
9004 m_gridWin
->Refresh(true, &rect
);
9008 void wxGrid::SetCellHighlightROPenWidth(int width
)
9010 if (m_cellHighlightROPenWidth
!= width
)
9012 m_cellHighlightROPenWidth
= width
;
9014 // Just redrawing the cell highlight is not enough since that won't
9015 // make any visible change if the the thickness is getting smaller.
9016 int row
= m_currentCellCoords
.GetRow();
9017 int col
= m_currentCellCoords
.GetCol();
9018 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9020 wxRect rect
= CellToRect(row
, col
);
9021 m_gridWin
->Refresh(true, &rect
);
9025 void wxGrid::EnableGridLines( bool enable
)
9027 if ( enable
!= m_gridLinesEnabled
)
9029 m_gridLinesEnabled
= enable
;
9031 if ( !GetBatchCount() )
9035 wxClientDC
dc( m_gridWin
);
9037 DrawAllGridLines( dc
, wxRegion() );
9041 m_gridWin
->Refresh();
9048 int wxGrid::GetDefaultRowSize()
9050 return m_defaultRowHeight
;
9053 int wxGrid::GetRowSize( int row
)
9055 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9057 return GetRowHeight(row
);
9060 int wxGrid::GetDefaultColSize()
9062 return m_defaultColWidth
;
9065 int wxGrid::GetColSize( int col
)
9067 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9069 return GetColWidth(col
);
9072 // ============================================================================
9073 // access to the grid attributes: each of them has a default value in the grid
9074 // itself and may be overidden on a per-cell basis
9075 // ============================================================================
9077 // ----------------------------------------------------------------------------
9078 // setting default attributes
9079 // ----------------------------------------------------------------------------
9081 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9083 m_defaultCellAttr
->SetBackgroundColour(col
);
9085 m_gridWin
->SetBackgroundColour(col
);
9089 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9091 m_defaultCellAttr
->SetTextColour(col
);
9094 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9096 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9099 void wxGrid::SetDefaultCellOverflow( bool allow
)
9101 m_defaultCellAttr
->SetOverflow(allow
);
9104 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9106 m_defaultCellAttr
->SetFont(font
);
9110 // For editors and renderers the type registry takes precedence over the
9111 // default attr, so we need to register the new editor/renderer for the string
9112 // data type in order to make setting a default editor/renderer appear to
9115 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9117 RegisterDataType(wxGRID_VALUE_STRING
,
9119 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9122 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9124 RegisterDataType(wxGRID_VALUE_STRING
,
9125 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9129 // ----------------------------------------------------------------------------
9130 // access to the default attrbiutes
9131 // ----------------------------------------------------------------------------
9133 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9135 return m_defaultCellAttr
->GetBackgroundColour();
9138 wxColour
wxGrid::GetDefaultCellTextColour()
9140 return m_defaultCellAttr
->GetTextColour();
9143 wxFont
wxGrid::GetDefaultCellFont()
9145 return m_defaultCellAttr
->GetFont();
9148 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9150 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9153 bool wxGrid::GetDefaultCellOverflow()
9155 return m_defaultCellAttr
->GetOverflow();
9158 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9160 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9163 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9165 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9168 // ----------------------------------------------------------------------------
9169 // access to cell attributes
9170 // ----------------------------------------------------------------------------
9172 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9174 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9175 wxColour colour
= attr
->GetBackgroundColour();
9180 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9182 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9183 wxColour colour
= attr
->GetTextColour();
9188 wxFont
wxGrid::GetCellFont( int row
, int col
)
9190 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9191 wxFont font
= attr
->GetFont();
9196 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9198 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9199 attr
->GetAlignment(horiz
, vert
);
9203 bool wxGrid::GetCellOverflow( int row
, int col
)
9205 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9206 bool allow
= attr
->GetOverflow();
9212 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9214 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9215 attr
->GetSize( num_rows
, num_cols
);
9219 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9221 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9222 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9228 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9230 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9231 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9237 bool wxGrid::IsReadOnly(int row
, int col
) const
9239 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9240 bool isReadOnly
= attr
->IsReadOnly();
9246 // ----------------------------------------------------------------------------
9247 // attribute support: cache, automatic provider creation, ...
9248 // ----------------------------------------------------------------------------
9250 bool wxGrid::CanHaveAttributes()
9257 return m_table
->CanHaveAttributes();
9260 void wxGrid::ClearAttrCache()
9262 if ( m_attrCache
.row
!= -1 )
9264 wxSafeDecRef(m_attrCache
.attr
);
9265 m_attrCache
.attr
= NULL
;
9266 m_attrCache
.row
= -1;
9270 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9274 wxGrid
*self
= (wxGrid
*)this; // const_cast
9276 self
->ClearAttrCache();
9277 self
->m_attrCache
.row
= row
;
9278 self
->m_attrCache
.col
= col
;
9279 self
->m_attrCache
.attr
= attr
;
9284 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9286 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9288 *attr
= m_attrCache
.attr
;
9289 wxSafeIncRef(m_attrCache
.attr
);
9291 #ifdef DEBUG_ATTR_CACHE
9292 gs_nAttrCacheHits
++;
9299 #ifdef DEBUG_ATTR_CACHE
9300 gs_nAttrCacheMisses
++;
9307 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9309 wxGridCellAttr
*attr
= NULL
;
9310 // Additional test to avoid looking at the cache e.g. for
9311 // wxNoCellCoords, as this will confuse memory management.
9314 if ( !LookupAttr(row
, col
, &attr
) )
9316 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9317 : (wxGridCellAttr
*)NULL
;
9318 CacheAttr(row
, col
, attr
);
9324 attr
->SetDefAttr(m_defaultCellAttr
);
9328 attr
= m_defaultCellAttr
;
9335 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9337 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9338 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9340 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9341 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9343 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9346 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9348 // artificially inc the ref count to match DecRef() in caller
9350 m_table
->SetAttr(attr
, row
, col
);
9356 // ----------------------------------------------------------------------------
9357 // setting column attributes (wrappers around SetColAttr)
9358 // ----------------------------------------------------------------------------
9360 void wxGrid::SetColFormatBool(int col
)
9362 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9365 void wxGrid::SetColFormatNumber(int col
)
9367 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9370 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9372 wxString typeName
= wxGRID_VALUE_FLOAT
;
9373 if ( (width
!= -1) || (precision
!= -1) )
9375 typeName
<< _T(':') << width
<< _T(',') << precision
;
9378 SetColFormatCustom(col
, typeName
);
9381 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9383 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9385 attr
= new wxGridCellAttr
;
9386 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9387 attr
->SetRenderer(renderer
);
9389 SetColAttr(col
, attr
);
9393 // ----------------------------------------------------------------------------
9394 // setting cell attributes: this is forwarded to the table
9395 // ----------------------------------------------------------------------------
9397 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9399 if ( CanHaveAttributes() )
9401 m_table
->SetAttr(attr
, row
, col
);
9410 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9412 if ( CanHaveAttributes() )
9414 m_table
->SetRowAttr(attr
, row
);
9423 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9425 if ( CanHaveAttributes() )
9427 m_table
->SetColAttr(attr
, col
);
9436 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9438 if ( CanHaveAttributes() )
9440 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9441 attr
->SetBackgroundColour(colour
);
9446 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9448 if ( CanHaveAttributes() )
9450 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9451 attr
->SetTextColour(colour
);
9456 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9458 if ( CanHaveAttributes() )
9460 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9461 attr
->SetFont(font
);
9466 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9468 if ( CanHaveAttributes() )
9470 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9471 attr
->SetAlignment(horiz
, vert
);
9476 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9478 if ( CanHaveAttributes() )
9480 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9481 attr
->SetOverflow(allow
);
9486 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9488 if ( CanHaveAttributes() )
9490 int cell_rows
, cell_cols
;
9492 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9493 attr
->GetSize(&cell_rows
, &cell_cols
);
9494 attr
->SetSize(num_rows
, num_cols
);
9497 // Cannot set the size of a cell to 0 or negative values
9498 // While it is perfectly legal to do that, this function cannot
9499 // handle all the possibilies, do it by hand by getting the CellAttr.
9500 // You can only set the size of a cell to 1,1 or greater with this fn
9501 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9502 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9503 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9504 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9506 // if this was already a multicell then "turn off" the other cells first
9507 if ((cell_rows
> 1) || (cell_rows
> 1))
9510 for (j
=row
; j
<row
+cell_rows
; j
++)
9512 for (i
=col
; i
<col
+cell_cols
; i
++)
9514 if ((i
!= col
) || (j
!= row
))
9516 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9517 attr_stub
->SetSize( 1, 1 );
9518 attr_stub
->DecRef();
9524 // mark the cells that will be covered by this cell to
9525 // negative or zero values to point back at this cell
9526 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9529 for (j
=row
; j
<row
+num_rows
; j
++)
9531 for (i
=col
; i
<col
+num_cols
; i
++)
9533 if ((i
!= col
) || (j
!= row
))
9535 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9536 attr_stub
->SetSize( row
-j
, col
-i
);
9537 attr_stub
->DecRef();
9545 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9547 if ( CanHaveAttributes() )
9549 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9550 attr
->SetRenderer(renderer
);
9555 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9557 if ( CanHaveAttributes() )
9559 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9560 attr
->SetEditor(editor
);
9565 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9567 if ( CanHaveAttributes() )
9569 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9570 attr
->SetReadOnly(isReadOnly
);
9575 // ----------------------------------------------------------------------------
9576 // Data type registration
9577 // ----------------------------------------------------------------------------
9579 void wxGrid::RegisterDataType(const wxString
& typeName
,
9580 wxGridCellRenderer
* renderer
,
9581 wxGridCellEditor
* editor
)
9583 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9587 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9589 wxString typeName
= m_table
->GetTypeName(row
, col
);
9590 return GetDefaultEditorForType(typeName
);
9593 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9595 wxString typeName
= m_table
->GetTypeName(row
, col
);
9596 return GetDefaultRendererForType(typeName
);
9600 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9602 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9603 if ( index
== wxNOT_FOUND
)
9607 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9608 wxFAIL_MSG(errStr
.c_str());
9613 return m_typeRegistry
->GetEditor(index
);
9617 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9619 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9620 if ( index
== wxNOT_FOUND
)
9624 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9625 wxFAIL_MSG(errStr
.c_str());
9630 return m_typeRegistry
->GetRenderer(index
);
9633 // ----------------------------------------------------------------------------
9635 // ----------------------------------------------------------------------------
9637 void wxGrid::EnableDragRowSize( bool enable
)
9639 m_canDragRowSize
= enable
;
9642 void wxGrid::EnableDragColSize( bool enable
)
9644 m_canDragColSize
= enable
;
9647 void wxGrid::EnableDragGridSize( bool enable
)
9649 m_canDragGridSize
= enable
;
9652 void wxGrid::EnableDragCell( bool enable
)
9654 m_canDragCell
= enable
;
9657 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9659 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9661 if ( resizeExistingRows
)
9663 // since we are resizing all rows to the default row size,
9664 // we can simply clear the row heights and row bottoms
9665 // arrays (which also allows us to take advantage of
9666 // some speed optimisations)
9667 m_rowHeights
.Empty();
9668 m_rowBottoms
.Empty();
9669 if ( !GetBatchCount() )
9674 void wxGrid::SetRowSize( int row
, int height
)
9676 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9678 // See comment in SetColSize
9679 if ( height
< GetRowMinimalAcceptableHeight())
9682 if ( m_rowHeights
.IsEmpty() )
9684 // need to really create the array
9688 int h
= wxMax( 0, height
);
9689 int diff
= h
- m_rowHeights
[row
];
9691 m_rowHeights
[row
] = h
;
9693 for ( i
= row
; i
< m_numRows
; i
++ )
9695 m_rowBottoms
[i
] += diff
;
9697 if ( !GetBatchCount() )
9701 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9703 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9705 if ( resizeExistingCols
)
9707 // since we are resizing all columns to the default column size,
9708 // we can simply clear the col widths and col rights
9709 // arrays (which also allows us to take advantage of
9710 // some speed optimisations)
9711 m_colWidths
.Empty();
9712 m_colRights
.Empty();
9713 if ( !GetBatchCount() )
9718 void wxGrid::SetColSize( int col
, int width
)
9720 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9722 // should we check that it's bigger than GetColMinimalWidth(col) here?
9724 // No, because it is reasonable to assume the library user know's
9725 // what he is doing. However whe should test against the weaker
9726 // constariant of minimalAcceptableWidth, as this breaks rendering
9728 // This test then fixes sf.net bug #645734
9730 if ( width
< GetColMinimalAcceptableWidth() )
9733 if ( m_colWidths
.IsEmpty() )
9735 // need to really create the array
9739 // if < 0 calc new width from label
9743 wxArrayString lines
;
9744 wxClientDC
dc(m_colLabelWin
);
9745 dc
.SetFont(GetLabelFont());
9746 StringToLines(GetColLabelValue(col
), lines
);
9747 GetTextBoxSize(dc
, lines
, &w
, &h
);
9751 int w
= wxMax( 0, width
);
9752 int diff
= w
- m_colWidths
[col
];
9753 m_colWidths
[col
] = w
;
9756 for ( i
= col
; i
< m_numCols
; i
++ )
9758 m_colRights
[i
] += diff
;
9761 if ( !GetBatchCount() )
9765 void wxGrid::SetColMinimalWidth( int col
, int width
)
9767 if (width
> GetColMinimalAcceptableWidth())
9769 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9770 m_colMinWidths
[key
] = width
;
9774 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9776 if (width
> GetRowMinimalAcceptableHeight())
9778 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9779 m_rowMinHeights
[key
] = width
;
9783 int wxGrid::GetColMinimalWidth(int col
) const
9785 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9786 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9788 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9791 int wxGrid::GetRowMinimalHeight(int row
) const
9793 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9794 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9796 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9799 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9801 // We do allow a width of 0 since this gives us
9802 // an easy way to temporarily hiding columns.
9804 m_minAcceptableColWidth
= width
;
9807 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9809 // We do allow a height of 0 since this gives us
9810 // an easy way to temporarily hiding rows.
9812 m_minAcceptableRowHeight
= height
;
9815 int wxGrid::GetColMinimalAcceptableWidth() const
9817 return m_minAcceptableColWidth
;
9820 int wxGrid::GetRowMinimalAcceptableHeight() const
9822 return m_minAcceptableRowHeight
;
9825 // ----------------------------------------------------------------------------
9827 // ----------------------------------------------------------------------------
9829 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9831 wxClientDC
dc(m_gridWin
);
9833 // cancel editing of cell
9834 HideCellEditControl();
9835 SaveEditControlValue();
9837 // init both of them to avoid compiler warnings, even if we only need one
9845 wxCoord extent
, extentMax
= 0;
9846 int max
= column
? m_numRows
: m_numCols
;
9847 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9854 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9855 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9858 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9859 extent
= column
? size
.x
: size
.y
;
9860 if ( extent
> extentMax
)
9871 // now also compare with the column label extent
9873 dc
.SetFont( GetLabelFont() );
9877 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9878 if ( GetColLabelTextOrientation() == wxVERTICAL
)
9882 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9884 extent
= column
? w
: h
;
9885 if ( extent
> extentMax
)
9892 // empty column - give default extent (notice that if extentMax is less
9893 // than default extent but != 0, it's ok)
9894 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9899 // leave some space around text
9907 SetColSize( col
, extentMax
);
9908 if ( !GetBatchCount() )
9911 m_gridWin
->GetClientSize( &cw
, &ch
);
9912 wxRect
rect ( CellToRect( 0, col
) );
9914 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9915 rect
.width
= cw
- rect
.x
;
9916 rect
.height
= m_colLabelHeight
;
9917 m_colLabelWin
->Refresh( true, &rect
);
9922 SetRowSize(row
, extentMax
);
9923 if ( !GetBatchCount() )
9926 m_gridWin
->GetClientSize( &cw
, &ch
);
9927 wxRect
rect ( CellToRect( row
, 0 ) );
9929 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9930 rect
.width
= m_rowLabelWidth
;
9931 rect
.height
= ch
- rect
.y
;
9932 m_rowLabelWin
->Refresh( true, &rect
);
9938 SetColMinimalWidth(col
, extentMax
);
9940 SetRowMinimalHeight(row
, extentMax
);
9944 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9946 int width
= m_rowLabelWidth
;
9951 for ( int col
= 0; col
< m_numCols
; col
++ )
9955 AutoSizeColumn(col
, setAsMin
);
9958 width
+= GetColWidth(col
);
9967 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9969 int height
= m_colLabelHeight
;
9974 for ( int row
= 0; row
< m_numRows
; row
++ )
9977 AutoSizeRow(row
, setAsMin
);
9979 height
+= GetRowHeight(row
);
9988 void wxGrid::AutoSize()
9992 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9994 // round up the size to a multiple of scroll step - this ensures that we
9995 // won't get the scrollbars if we're sized exactly to this width
9996 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9998 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
9999 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10001 // distribute the extra space between the columns/rows to avoid having
10002 // extra white space
10004 // Remove the extra m_extraWidth + 1 added above
10005 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10006 if ( diff
&& m_numCols
)
10008 // try to resize the columns uniformly
10009 wxCoord diffPerCol
= diff
/ m_numCols
;
10012 for ( int col
= 0; col
< m_numCols
; col
++ )
10014 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10018 // add remaining amount to the last columns
10019 diff
-= diffPerCol
* m_numCols
;
10022 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10024 SetColSize(col
, GetColWidth(col
) + 1);
10030 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10031 if ( diff
&& m_numRows
)
10033 // try to resize the columns uniformly
10034 wxCoord diffPerRow
= diff
/ m_numRows
;
10037 for ( int row
= 0; row
< m_numRows
; row
++ )
10039 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10043 // add remaining amount to the last rows
10044 diff
-= diffPerRow
* m_numRows
;
10047 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10049 SetRowSize(row
, GetRowHeight(row
) + 1);
10056 SetClientSize(sizeFit
);
10059 void wxGrid::AutoSizeRowLabelSize( int row
)
10061 wxArrayString lines
;
10064 // Hide the edit control, so it
10065 // won't interfere with drag-shrinking.
10066 if ( IsCellEditControlShown() )
10068 HideCellEditControl();
10069 SaveEditControlValue();
10072 // autosize row height depending on label text
10073 StringToLines( GetRowLabelValue( row
), lines
);
10074 wxClientDC
dc( m_rowLabelWin
);
10075 GetTextBoxSize( dc
, lines
, &w
, &h
);
10076 if ( h
< m_defaultRowHeight
)
10077 h
= m_defaultRowHeight
;
10078 SetRowSize(row
, h
);
10082 void wxGrid::AutoSizeColLabelSize( int col
)
10084 wxArrayString lines
;
10087 // Hide the edit control, so it
10088 // won't interfer with drag-shrinking.
10089 if ( IsCellEditControlShown() )
10091 HideCellEditControl();
10092 SaveEditControlValue();
10095 // autosize column width depending on label text
10096 StringToLines( GetColLabelValue( col
), lines
);
10097 wxClientDC
dc( m_colLabelWin
);
10098 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10099 GetTextBoxSize( dc
, lines
, &w
, &h
);
10101 GetTextBoxSize( dc
, lines
, &h
, &w
);
10102 if ( w
< m_defaultColWidth
)
10103 w
= m_defaultColWidth
;
10104 SetColSize(col
, w
);
10108 wxSize
wxGrid::DoGetBestSize() const
10110 // don't set sizes, only calculate them
10111 wxGrid
*self
= (wxGrid
*)this; // const_cast
10114 width
= self
->SetOrCalcColumnSizes(true);
10115 height
= self
->SetOrCalcRowSizes(true);
10122 // Round up to a multiple the scroll rate
10123 // NOTE: this still doesn't get rid of the scrollbars;
10124 // is there any magic incantation for that?
10126 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10128 width
+= 1 + xpu
- (width
% xpu
);
10130 height
+= 1 + ypu
- (height
% ypu
);
10132 // limit to 1/4 of the screen size
10133 int maxwidth
, maxheight
;
10134 wxDisplaySize( &maxwidth
, &maxheight
);
10137 if ( width
> maxwidth
)
10139 if ( height
> maxheight
)
10140 height
= maxheight
;
10142 wxSize
best(width
, height
);
10144 // NOTE: This size should be cached, but first we need to add calls to
10145 // InvalidateBestSize everywhere that could change the results of this
10147 // CacheBestSize(size);
10157 wxPen
& wxGrid::GetDividerPen() const
10162 // ----------------------------------------------------------------------------
10163 // cell value accessor functions
10164 // ----------------------------------------------------------------------------
10166 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10170 m_table
->SetValue( row
, col
, s
);
10171 if ( !GetBatchCount() )
10174 wxRect
rect( CellToRect( row
, col
) );
10176 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10177 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10178 m_gridWin
->Refresh( false, &rect
);
10181 if ( m_currentCellCoords
.GetRow() == row
&&
10182 m_currentCellCoords
.GetCol() == col
&&
10183 IsCellEditControlShown())
10184 // Note: If we are using IsCellEditControlEnabled,
10185 // this interacts badly with calling SetCellValue from
10186 // an EVT_GRID_CELL_CHANGE handler.
10188 HideCellEditControl();
10189 ShowCellEditControl(); // will reread data from table
10195 // ----------------------------------------------------------------------------
10196 // block, row and col selection
10197 // ----------------------------------------------------------------------------
10199 void wxGrid::SelectRow( int row
, bool addToSelected
)
10201 if ( IsSelection() && !addToSelected
)
10205 m_selection
->SelectRow( row
, false, addToSelected
);
10208 void wxGrid::SelectCol( int col
, bool addToSelected
)
10210 if ( IsSelection() && !addToSelected
)
10214 m_selection
->SelectCol( col
, false, addToSelected
);
10217 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10218 bool addToSelected
)
10220 if ( IsSelection() && !addToSelected
)
10224 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10225 false, addToSelected
);
10228 void wxGrid::SelectAll()
10230 if ( m_numRows
> 0 && m_numCols
> 0 )
10233 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10237 // ----------------------------------------------------------------------------
10238 // cell, row and col deselection
10239 // ----------------------------------------------------------------------------
10241 void wxGrid::DeselectRow( int row
)
10243 if ( !m_selection
)
10246 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10248 if ( m_selection
->IsInSelection(row
, 0 ) )
10249 m_selection
->ToggleCellSelection( row
, 0);
10253 int nCols
= GetNumberCols();
10254 for ( int i
= 0; i
< nCols
; i
++ )
10256 if ( m_selection
->IsInSelection(row
, i
) )
10257 m_selection
->ToggleCellSelection( row
, i
);
10262 void wxGrid::DeselectCol( int col
)
10264 if ( !m_selection
)
10267 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10269 if ( m_selection
->IsInSelection(0, col
) )
10270 m_selection
->ToggleCellSelection( 0, col
);
10274 int nRows
= GetNumberRows();
10275 for ( int i
= 0; i
< nRows
; i
++ )
10277 if ( m_selection
->IsInSelection(i
, col
) )
10278 m_selection
->ToggleCellSelection(i
, col
);
10283 void wxGrid::DeselectCell( int row
, int col
)
10285 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10286 m_selection
->ToggleCellSelection(row
, col
);
10289 bool wxGrid::IsSelection()
10291 return ( m_selection
&& (m_selection
->IsSelection() ||
10292 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10293 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10296 bool wxGrid::IsInSelection( int row
, int col
) const
10298 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10299 ( row
>= m_selectingTopLeft
.GetRow() &&
10300 col
>= m_selectingTopLeft
.GetCol() &&
10301 row
<= m_selectingBottomRight
.GetRow() &&
10302 col
<= m_selectingBottomRight
.GetCol() )) );
10305 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10309 wxGridCellCoordsArray a
;
10313 return m_selection
->m_cellSelection
;
10316 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10320 wxGridCellCoordsArray a
;
10324 return m_selection
->m_blockSelectionTopLeft
;
10327 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10331 wxGridCellCoordsArray a
;
10335 return m_selection
->m_blockSelectionBottomRight
;
10338 wxArrayInt
wxGrid::GetSelectedRows() const
10346 return m_selection
->m_rowSelection
;
10349 wxArrayInt
wxGrid::GetSelectedCols() const
10357 return m_selection
->m_colSelection
;
10360 void wxGrid::ClearSelection()
10362 m_selectingTopLeft
= wxGridNoCellCoords
;
10363 m_selectingBottomRight
= wxGridNoCellCoords
;
10365 m_selection
->ClearSelection();
10369 // This function returns the rectangle that encloses the given block
10370 // in device coords clipped to the client size of the grid window.
10372 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10373 const wxGridCellCoords
&bottomRight
)
10375 wxRect
rect( wxGridNoCellRect
);
10378 cellRect
= CellToRect( topLeft
);
10379 if ( cellRect
!= wxGridNoCellRect
)
10385 rect
= wxRect(0, 0, 0, 0);
10388 cellRect
= CellToRect( bottomRight
);
10389 if ( cellRect
!= wxGridNoCellRect
)
10395 return wxGridNoCellRect
;
10399 int left
= rect
.GetLeft();
10400 int top
= rect
.GetTop();
10401 int right
= rect
.GetRight();
10402 int bottom
= rect
.GetBottom();
10404 int leftCol
= topLeft
.GetCol();
10405 int topRow
= topLeft
.GetRow();
10406 int rightCol
= bottomRight
.GetCol();
10407 int bottomRow
= bottomRight
.GetRow();
10415 leftCol
= rightCol
;
10425 topRow
= bottomRow
;
10429 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10431 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10433 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10435 cellRect
= CellToRect( j
, i
);
10437 if (cellRect
.x
< left
)
10439 if (cellRect
.y
< top
)
10441 if (cellRect
.x
+ cellRect
.width
> right
)
10442 right
= cellRect
.x
+ cellRect
.width
;
10443 if (cellRect
.y
+ cellRect
.height
> bottom
)
10444 bottom
= cellRect
.y
+ cellRect
.height
;
10448 i
= rightCol
; // jump over inner cells.
10453 // convert to scrolled coords
10455 CalcScrolledPosition( left
, top
, &left
, &top
);
10456 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10459 m_gridWin
->GetClientSize( &cw
, &ch
);
10461 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10462 return wxRect(0,0,0,0);
10464 rect
.SetLeft( wxMax(0, left
) );
10465 rect
.SetTop( wxMax(0, top
) );
10466 rect
.SetRight( wxMin(cw
, right
) );
10467 rect
.SetBottom( wxMin(ch
, bottom
) );
10472 // ----------------------------------------------------------------------------
10473 // grid event classes
10474 // ----------------------------------------------------------------------------
10476 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10478 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10479 int row
, int col
, int x
, int y
, bool sel
,
10480 bool control
, bool shift
, bool alt
, bool meta
)
10481 : wxNotifyEvent( type
, id
)
10488 m_control
= control
;
10493 SetEventObject(obj
);
10497 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10499 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10500 int rowOrCol
, int x
, int y
,
10501 bool control
, bool shift
, bool alt
, bool meta
)
10502 : wxNotifyEvent( type
, id
)
10504 m_rowOrCol
= rowOrCol
;
10507 m_control
= control
;
10512 SetEventObject(obj
);
10516 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10518 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10519 const wxGridCellCoords
& topLeft
,
10520 const wxGridCellCoords
& bottomRight
,
10521 bool sel
, bool control
,
10522 bool shift
, bool alt
, bool meta
)
10523 : wxNotifyEvent( type
, id
)
10525 m_topLeft
= topLeft
;
10526 m_bottomRight
= bottomRight
;
10528 m_control
= control
;
10533 SetEventObject(obj
);
10537 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10539 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10540 wxObject
* obj
, int row
,
10541 int col
, wxControl
* ctrl
)
10542 : wxCommandEvent(type
, id
)
10544 SetEventObject(obj
);
10550 #endif // wxUSE_GRID