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_precision
== -1 && m_width
!= -1)
1164 // default precision
1165 fmt
.Printf(_T("%%%d.f"), m_width
);
1167 else if ( m_precision
!= -1 && m_width
== -1)
1170 fmt
.Printf(_T("%%.%df"), m_precision
);
1172 else if ( m_precision
!= -1 && m_width
!= -1 )
1174 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1178 // default width/precision
1182 return wxString::Format(fmt
, m_valueOld
);
1185 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1187 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1189 int keycode
= event
.GetKeyCode();
1190 printf("%d\n", keycode
);
1191 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1193 tmpbuf
[0] = (char) keycode
;
1195 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1197 bool is_decimal_point
=
1198 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1199 wxLOCALE_CAT_NUMBER
) );
1201 bool is_decimal_point
= ( strbuf
== _T(".") );
1203 if ( (keycode
< 128) &&
1204 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1205 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1212 #endif // wxUSE_TEXTCTRL
1216 // ----------------------------------------------------------------------------
1217 // wxGridCellBoolEditor
1218 // ----------------------------------------------------------------------------
1220 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1222 wxEvtHandler
* evtHandler
)
1224 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1225 wxDefaultPosition
, wxDefaultSize
,
1228 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1231 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1233 bool resize
= false;
1234 wxSize size
= m_control
->GetSize();
1235 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1237 // check if the checkbox is not too big/small for this cell
1238 wxSize sizeBest
= m_control
->GetBestSize();
1239 if ( !(size
== sizeBest
) )
1241 // reset to default size if it had been made smaller
1247 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1249 // leave 1 pixel margin
1250 size
.x
= size
.y
= minSize
- 2;
1257 m_control
->SetSize(size
);
1260 // position it in the centre of the rectangle (TODO: support alignment?)
1262 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1263 // the checkbox without label still has some space to the right in wxGTK,
1264 // so shift it to the right
1266 #elif defined(__WXMSW__)
1267 // here too, but in other way
1272 int hAlign
= wxALIGN_CENTRE
;
1273 int vAlign
= wxALIGN_CENTRE
;
1275 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1278 if (hAlign
== wxALIGN_LEFT
)
1284 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1286 else if (hAlign
== wxALIGN_RIGHT
)
1288 x
= r
.x
+ r
.width
- size
.x
- 2;
1289 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1291 else if (hAlign
== wxALIGN_CENTRE
)
1293 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1294 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1297 m_control
->Move(x
, y
);
1300 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1302 m_control
->Show(show
);
1306 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1307 CBox()->SetBackgroundColour(colBg
);
1311 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1313 wxASSERT_MSG(m_control
,
1314 wxT("The wxGridCellEditor must be Created first!"));
1316 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1317 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1320 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1321 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1323 CBox()->SetValue(m_startValue
);
1327 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1330 wxASSERT_MSG(m_control
,
1331 wxT("The wxGridCellEditor must be Created first!"));
1333 bool changed
= false;
1334 bool value
= CBox()->GetValue();
1335 if ( value
!= m_startValue
)
1340 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1341 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1343 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1349 void wxGridCellBoolEditor::Reset()
1351 wxASSERT_MSG(m_control
,
1352 wxT("The wxGridCellEditor must be Created first!"));
1354 CBox()->SetValue(m_startValue
);
1357 void wxGridCellBoolEditor::StartingClick()
1359 CBox()->SetValue(!CBox()->GetValue());
1362 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1364 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1366 int keycode
= event
.GetKeyCode();
1379 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1381 int keycode
= event
.GetKeyCode();
1385 CBox()->SetValue(!CBox()->GetValue());
1389 CBox()->SetValue(true);
1393 CBox()->SetValue(false);
1399 // return the value as "1" for true and the empty string for false
1400 wxString
wxGridCellBoolEditor::GetValue() const
1402 bool bSet
= CBox()->GetValue();
1403 return bSet
? _T("1") : wxEmptyString
;
1406 #endif // wxUSE_CHECKBOX
1410 // ----------------------------------------------------------------------------
1411 // wxGridCellChoiceEditor
1412 // ----------------------------------------------------------------------------
1414 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1416 : m_choices(choices
),
1417 m_allowOthers(allowOthers
) { }
1419 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1420 const wxString choices
[],
1422 : m_allowOthers(allowOthers
)
1426 m_choices
.Alloc(count
);
1427 for ( size_t n
= 0; n
< count
; n
++ )
1429 m_choices
.Add(choices
[n
]);
1434 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1436 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1437 editor
->m_allowOthers
= m_allowOthers
;
1438 editor
->m_choices
= m_choices
;
1443 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1445 wxEvtHandler
* evtHandler
)
1447 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1448 wxDefaultPosition
, wxDefaultSize
,
1450 m_allowOthers
? 0 : wxCB_READONLY
);
1452 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1455 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1456 wxGridCellAttr
* attr
)
1458 // as we fill the entire client area, don't do anything here to minimize
1461 // TODO: It doesn't actually fill the client area since the height of a
1462 // combo always defaults to the standard... Until someone has time to
1463 // figure out the right rectangle to paint, just do it the normal way...
1464 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1467 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1469 wxASSERT_MSG(m_control
,
1470 wxT("The wxGridCellEditor must be Created first!"));
1472 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1474 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1476 // Don't immediately end if we get a kill focus event within BeginEdit
1478 evtHandler
->SetInSetFocus(true);
1480 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1483 Combo()->SetValue(m_startValue
);
1486 // find the right position, or default to the first if not found
1487 int pos
= Combo()->FindString(m_startValue
);
1488 if (pos
== wxNOT_FOUND
)
1490 Combo()->SetSelection(pos
);
1492 Combo()->SetInsertionPointEnd();
1493 Combo()->SetFocus();
1497 // When dropping down the menu, a kill focus event
1498 // happens after this point, so we can't reset the flag yet.
1499 #if !defined(__WXGTK20__)
1500 evtHandler
->SetInSetFocus(false);
1505 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1508 wxString value
= Combo()->GetValue();
1509 if ( value
== m_startValue
)
1512 grid
->GetTable()->SetValue(row
, col
, value
);
1517 void wxGridCellChoiceEditor::Reset()
1519 Combo()->SetValue(m_startValue
);
1520 Combo()->SetInsertionPointEnd();
1523 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1533 wxStringTokenizer
tk(params
, _T(','));
1534 while ( tk
.HasMoreTokens() )
1536 m_choices
.Add(tk
.GetNextToken());
1540 // return the value in the text control
1541 wxString
wxGridCellChoiceEditor::GetValue() const
1543 return Combo()->GetValue();
1546 #endif // wxUSE_COMBOBOX
1548 // ----------------------------------------------------------------------------
1549 // wxGridCellEditorEvtHandler
1550 // ----------------------------------------------------------------------------
1552 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1554 // Don't disable the cell if we're just starting to edit it
1559 m_grid
->DisableCellEditControl();
1564 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1566 switch ( event
.GetKeyCode() )
1570 m_grid
->DisableCellEditControl();
1574 m_grid
->GetEventHandler()->ProcessEvent( event
);
1578 case WXK_NUMPAD_ENTER
:
1579 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1580 m_editor
->HandleReturn(event
);
1588 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1590 switch ( event
.GetKeyCode() )
1595 case WXK_NUMPAD_ENTER
:
1603 // ----------------------------------------------------------------------------
1604 // wxGridCellWorker is an (almost) empty common base class for
1605 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1606 // ----------------------------------------------------------------------------
1608 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1613 wxGridCellWorker::~wxGridCellWorker()
1617 // ============================================================================
1619 // ============================================================================
1621 // ----------------------------------------------------------------------------
1622 // wxGridCellRenderer
1623 // ----------------------------------------------------------------------------
1625 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1626 wxGridCellAttr
& attr
,
1629 int WXUNUSED(row
), int WXUNUSED(col
),
1632 dc
.SetBackgroundMode( wxSOLID
);
1634 // grey out fields if the grid is disabled
1635 if ( grid
.IsEnabled() )
1639 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1643 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1648 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1651 dc
.SetPen( *wxTRANSPARENT_PEN
);
1652 dc
.DrawRectangle(rect
);
1655 // ----------------------------------------------------------------------------
1656 // wxGridCellStringRenderer
1657 // ----------------------------------------------------------------------------
1659 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1660 const wxGridCellAttr
& attr
,
1664 dc
.SetBackgroundMode( wxTRANSPARENT
);
1666 // TODO some special colours for attr.IsReadOnly() case?
1668 // different coloured text when the grid is disabled
1669 if ( grid
.IsEnabled() )
1673 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1674 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1678 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1679 dc
.SetTextForeground( attr
.GetTextColour() );
1684 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1685 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1688 dc
.SetFont( attr
.GetFont() );
1691 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1693 const wxString
& text
)
1695 wxCoord x
= 0, y
= 0, max_x
= 0;
1696 dc
.SetFont(attr
.GetFont());
1697 wxStringTokenizer
tk(text
, _T('\n'));
1698 while ( tk
.HasMoreTokens() )
1700 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1701 max_x
= wxMax(max_x
, x
);
1704 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1706 return wxSize(max_x
, y
);
1709 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1710 wxGridCellAttr
& attr
,
1714 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1717 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1718 wxGridCellAttr
& attr
,
1720 const wxRect
& rectCell
,
1724 wxRect rect
= rectCell
;
1727 // erase only this cells background, overflow cells should have been erased
1728 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1731 attr
.GetAlignment(&hAlign
, &vAlign
);
1733 int overflowCols
= 0;
1735 if (attr
.GetOverflow())
1737 int cols
= grid
.GetNumberCols();
1738 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1739 int cell_rows
, cell_cols
;
1740 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1741 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1743 int i
, c_cols
, c_rows
;
1744 for (i
= col
+cell_cols
; i
< cols
; i
++)
1746 bool is_empty
= true;
1747 for (int j
=row
; j
<row
+cell_rows
; j
++)
1749 // check w/ anchor cell for multicell block
1750 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1751 if (c_rows
> 0) c_rows
= 0;
1752 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1759 rect
.width
+= grid
.GetColSize(i
);
1765 if (rect
.width
>= best_width
)
1768 overflowCols
= i
- col
- cell_cols
+ 1;
1769 if (overflowCols
>= cols
)
1770 overflowCols
= cols
- 1;
1773 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1775 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1777 clip
.x
+= rectCell
.width
;
1778 // draw each overflow cell individually
1779 int col_end
= col
+cell_cols
+overflowCols
;
1780 if (col_end
>= grid
.GetNumberCols())
1781 col_end
= grid
.GetNumberCols() - 1;
1782 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1784 clip
.width
= grid
.GetColSize(i
) - 1;
1785 dc
.DestroyClippingRegion();
1786 dc
.SetClippingRegion(clip
);
1788 SetTextColoursAndFont(grid
, attr
, dc
,
1789 grid
.IsInSelection(row
,i
));
1791 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1792 rect
, hAlign
, vAlign
);
1793 clip
.x
+= grid
.GetColSize(i
) - 1;
1799 dc
.DestroyClippingRegion();
1803 // now we only have to draw the text
1804 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1806 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1807 rect
, hAlign
, vAlign
);
1810 // ----------------------------------------------------------------------------
1811 // wxGridCellNumberRenderer
1812 // ----------------------------------------------------------------------------
1814 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1816 wxGridTableBase
*table
= grid
.GetTable();
1818 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1820 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1824 text
= table
->GetValue(row
, col
);
1830 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1831 wxGridCellAttr
& attr
,
1833 const wxRect
& rectCell
,
1837 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1839 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1841 // draw the text right aligned by default
1843 attr
.GetAlignment(&hAlign
, &vAlign
);
1844 hAlign
= wxALIGN_RIGHT
;
1846 wxRect rect
= rectCell
;
1849 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1852 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1853 wxGridCellAttr
& attr
,
1857 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1860 // ----------------------------------------------------------------------------
1861 // wxGridCellFloatRenderer
1862 // ----------------------------------------------------------------------------
1864 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1867 SetPrecision(precision
);
1870 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1872 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1873 renderer
->m_width
= m_width
;
1874 renderer
->m_precision
= m_precision
;
1875 renderer
->m_format
= m_format
;
1880 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1882 wxGridTableBase
*table
= grid
.GetTable();
1887 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1889 val
= table
->GetValueAsDouble(row
, col
);
1894 text
= table
->GetValue(row
, col
);
1895 hasDouble
= text
.ToDouble(&val
);
1902 if ( m_width
== -1 )
1904 if ( m_precision
== -1 )
1906 // default width/precision
1907 m_format
= _T("%f");
1911 m_format
.Printf(_T("%%.%df"), m_precision
);
1914 else if ( m_precision
== -1 )
1916 // default precision
1917 m_format
.Printf(_T("%%%d.f"), m_width
);
1921 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1925 text
.Printf(m_format
, val
);
1928 //else: text already contains the string
1933 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1934 wxGridCellAttr
& attr
,
1936 const wxRect
& rectCell
,
1940 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1942 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1944 // draw the text right aligned by default
1946 attr
.GetAlignment(&hAlign
, &vAlign
);
1947 hAlign
= wxALIGN_RIGHT
;
1949 wxRect rect
= rectCell
;
1952 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1955 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1956 wxGridCellAttr
& attr
,
1960 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1963 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1967 // reset to defaults
1973 wxString tmp
= params
.BeforeFirst(_T(','));
1977 if ( tmp
.ToLong(&width
) )
1979 SetWidth((int)width
);
1983 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1986 tmp
= params
.AfterFirst(_T(','));
1990 if ( tmp
.ToLong(&precision
) )
1992 SetPrecision((int)precision
);
1996 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2003 // ----------------------------------------------------------------------------
2004 // wxGridCellBoolRenderer
2005 // ----------------------------------------------------------------------------
2007 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2009 // FIXME these checkbox size calculations are really ugly...
2011 // between checkmark and box
2012 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2014 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2015 wxGridCellAttr
& WXUNUSED(attr
),
2020 // compute it only once (no locks for MT safeness in GUI thread...)
2021 if ( !ms_sizeCheckMark
.x
)
2023 // get checkbox size
2024 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2025 wxSize size
= checkbox
->GetBestSize();
2026 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
2028 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2029 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2030 checkSize
-= size
.y
/ 2;
2035 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2038 return ms_sizeCheckMark
;
2041 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2042 wxGridCellAttr
& attr
,
2048 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2050 // draw a check mark in the centre (ignoring alignment - TODO)
2051 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2053 // don't draw outside the cell
2054 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2055 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2057 // and even leave (at least) 1 pixel margin
2058 size
.x
= size
.y
= minSize
- 2;
2061 // draw a border around checkmark
2063 attr
.GetAlignment(& hAlign
, &vAlign
);
2066 if (hAlign
== wxALIGN_CENTRE
)
2068 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2069 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2070 rectBorder
.width
= size
.x
;
2071 rectBorder
.height
= size
.y
;
2073 else if (hAlign
== wxALIGN_LEFT
)
2075 rectBorder
.x
= rect
.x
+ 2;
2076 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2077 rectBorder
.width
= size
.x
;
2078 rectBorder
.height
= size
.y
;
2080 else if (hAlign
== wxALIGN_RIGHT
)
2082 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2083 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2084 rectBorder
.width
= size
.x
;
2085 rectBorder
.height
= size
.y
;
2089 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2090 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2093 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2094 value
= !( !cellval
|| (cellval
== wxT("0")) );
2099 wxRect rectMark
= rectBorder
;
2101 // MSW DrawCheckMark() is weird (and should probably be changed...)
2102 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2106 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2109 dc
.SetTextForeground(attr
.GetTextColour());
2110 dc
.DrawCheckMark(rectMark
);
2113 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2114 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2115 dc
.DrawRectangle(rectBorder
);
2118 // ----------------------------------------------------------------------------
2120 // ----------------------------------------------------------------------------
2122 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2126 m_isReadOnly
= Unset
;
2131 m_attrkind
= wxGridCellAttr::Cell
;
2133 m_sizeRows
= m_sizeCols
= 1;
2134 m_overflow
= UnsetOverflow
;
2136 SetDefAttr(attrDefault
);
2139 wxGridCellAttr
*wxGridCellAttr::Clone() const
2141 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2143 if ( HasTextColour() )
2144 attr
->SetTextColour(GetTextColour());
2145 if ( HasBackgroundColour() )
2146 attr
->SetBackgroundColour(GetBackgroundColour());
2148 attr
->SetFont(GetFont());
2149 if ( HasAlignment() )
2150 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2152 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2156 attr
->SetRenderer(m_renderer
);
2157 m_renderer
->IncRef();
2161 attr
->SetEditor(m_editor
);
2166 attr
->SetReadOnly();
2168 attr
->SetKind( m_attrkind
);
2173 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2175 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2176 SetTextColour(mergefrom
->GetTextColour());
2177 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2178 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2179 if ( !HasFont() && mergefrom
->HasFont() )
2180 SetFont(mergefrom
->GetFont());
2181 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2184 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2185 SetAlignment(hAlign
, vAlign
);
2187 if ( !HasSize() && mergefrom
->HasSize() )
2188 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2190 // Directly access member functions as GetRender/Editor don't just return
2191 // m_renderer/m_editor
2193 // Maybe add support for merge of Render and Editor?
2194 if (!HasRenderer() && mergefrom
->HasRenderer() )
2196 m_renderer
= mergefrom
->m_renderer
;
2197 m_renderer
->IncRef();
2199 if ( !HasEditor() && mergefrom
->HasEditor() )
2201 m_editor
= mergefrom
->m_editor
;
2204 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2205 SetReadOnly(mergefrom
->IsReadOnly());
2207 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2208 SetOverflow(mergefrom
->GetOverflow());
2210 SetDefAttr(mergefrom
->m_defGridAttr
);
2213 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2215 // The size of a cell is normally 1,1
2217 // If this cell is larger (2,2) then this is the top left cell
2218 // the other cells that will be covered (lower right cells) must be
2219 // set to negative or zero values such that
2220 // row + num_rows of the covered cell points to the larger cell (this cell)
2221 // same goes for the col + num_cols.
2223 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2225 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2226 !((num_rows
<=0)&&(num_cols
>0)) ||
2227 !((num_rows
==0)&&(num_cols
==0))),
2228 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2230 m_sizeRows
= num_rows
;
2231 m_sizeCols
= num_cols
;
2234 const wxColour
& wxGridCellAttr::GetTextColour() const
2236 if (HasTextColour())
2240 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2242 return m_defGridAttr
->GetTextColour();
2246 wxFAIL_MSG(wxT("Missing default cell attribute"));
2247 return wxNullColour
;
2252 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2254 if (HasBackgroundColour())
2256 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2257 return m_defGridAttr
->GetBackgroundColour();
2260 wxFAIL_MSG(wxT("Missing default cell attribute"));
2261 return wxNullColour
;
2266 const wxFont
& wxGridCellAttr::GetFont() const
2270 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2271 return m_defGridAttr
->GetFont();
2274 wxFAIL_MSG(wxT("Missing default cell attribute"));
2280 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2289 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2290 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2293 wxFAIL_MSG(wxT("Missing default cell attribute"));
2297 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2300 *num_rows
= m_sizeRows
;
2302 *num_cols
= m_sizeCols
;
2305 // GetRenderer and GetEditor use a slightly different decision path about
2306 // which attribute to use. If a non-default attr object has one then it is
2307 // used, otherwise the default editor or renderer is fetched from the grid and
2308 // used. It should be the default for the data type of the cell. If it is
2309 // NULL (because the table has a type that the grid does not have in its
2310 // registry,) then the grid's default editor or renderer is used.
2312 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2314 wxGridCellRenderer
*renderer
;
2316 if ( m_renderer
&& this != m_defGridAttr
)
2318 // use the cells renderer if it has one
2319 renderer
= m_renderer
;
2322 else // no non default cell renderer
2324 // get default renderer for the data type
2327 // GetDefaultRendererForCell() will do IncRef() for us
2328 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2337 if (m_defGridAttr
&& this != m_defGridAttr
)
2339 // if we still don't have one then use the grid default
2340 // (no need for IncRef() here neither)
2341 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2343 else // default grid attr
2345 // use m_renderer which we had decided not to use initially
2346 renderer
= m_renderer
;
2353 // we're supposed to always find something
2354 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2359 // same as above, except for s/renderer/editor/g
2360 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2362 wxGridCellEditor
*editor
;
2364 if ( m_editor
&& this != m_defGridAttr
)
2366 // use the cells editor if it has one
2370 else // no non default cell editor
2372 // get default editor for the data type
2375 // GetDefaultEditorForCell() will do IncRef() for us
2376 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2385 if ( m_defGridAttr
&& this != m_defGridAttr
)
2387 // if we still don't have one then use the grid default
2388 // (no need for IncRef() here neither)
2389 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2391 else // default grid attr
2393 // use m_editor which we had decided not to use initially
2401 // we're supposed to always find something
2402 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2407 // ----------------------------------------------------------------------------
2408 // wxGridCellAttrData
2409 // ----------------------------------------------------------------------------
2411 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2413 int n
= FindIndex(row
, col
);
2414 if ( n
== wxNOT_FOUND
)
2416 // add the attribute
2417 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2421 // free the old attribute
2422 m_attrs
[(size_t)n
].attr
->DecRef();
2426 // change the attribute
2427 m_attrs
[(size_t)n
].attr
= attr
;
2431 // remove this attribute
2432 m_attrs
.RemoveAt((size_t)n
);
2437 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2439 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2441 int n
= FindIndex(row
, col
);
2442 if ( n
!= wxNOT_FOUND
)
2444 attr
= m_attrs
[(size_t)n
].attr
;
2451 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2453 size_t count
= m_attrs
.GetCount();
2454 for ( size_t n
= 0; n
< count
; n
++ )
2456 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2457 wxCoord row
= coords
.GetRow();
2458 if ((size_t)row
>= pos
)
2462 // If rows inserted, include row counter where necessary
2463 coords
.SetRow(row
+ numRows
);
2465 else if (numRows
< 0)
2467 // If rows deleted ...
2468 if ((size_t)row
>= pos
- numRows
)
2470 // ...either decrement row counter (if row still exists)...
2471 coords
.SetRow(row
+ numRows
);
2475 // ...or remove the attribute
2476 // No need to DecRef the attribute itself since this is
2477 // done be wxGridCellWithAttr's destructor!
2478 m_attrs
.RemoveAt(n
);
2487 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2489 size_t count
= m_attrs
.GetCount();
2490 for ( size_t n
= 0; n
< count
; n
++ )
2492 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2493 wxCoord col
= coords
.GetCol();
2494 if ( (size_t)col
>= pos
)
2498 // If rows inserted, include row counter where necessary
2499 coords
.SetCol(col
+ numCols
);
2501 else if (numCols
< 0)
2503 // If rows deleted ...
2504 if ((size_t)col
>= pos
- numCols
)
2506 // ...either decrement row counter (if row still exists)...
2507 coords
.SetCol(col
+ numCols
);
2511 // ...or remove the attribute
2512 // No need to DecRef the attribute itself since this is
2513 // done be wxGridCellWithAttr's destructor!
2514 m_attrs
.RemoveAt(n
);
2522 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2524 size_t count
= m_attrs
.GetCount();
2525 for ( size_t n
= 0; n
< count
; n
++ )
2527 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2528 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2537 // ----------------------------------------------------------------------------
2538 // wxGridRowOrColAttrData
2539 // ----------------------------------------------------------------------------
2541 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2543 size_t count
= m_attrs
.Count();
2544 for ( size_t n
= 0; n
< count
; n
++ )
2546 m_attrs
[n
]->DecRef();
2550 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2552 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2554 int n
= m_rowsOrCols
.Index(rowOrCol
);
2555 if ( n
!= wxNOT_FOUND
)
2557 attr
= m_attrs
[(size_t)n
];
2564 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2566 int i
= m_rowsOrCols
.Index(rowOrCol
);
2567 if ( i
== wxNOT_FOUND
)
2569 // add the attribute
2570 m_rowsOrCols
.Add(rowOrCol
);
2575 size_t n
= (size_t)i
;
2578 // change the attribute
2579 m_attrs
[n
]->DecRef();
2584 // remove this attribute
2585 m_attrs
[n
]->DecRef();
2586 m_rowsOrCols
.RemoveAt(n
);
2587 m_attrs
.RemoveAt(n
);
2592 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2594 size_t count
= m_attrs
.GetCount();
2595 for ( size_t n
= 0; n
< count
; n
++ )
2597 int & rowOrCol
= m_rowsOrCols
[n
];
2598 if ( (size_t)rowOrCol
>= pos
)
2600 if ( numRowsOrCols
> 0 )
2602 // If rows inserted, include row counter where necessary
2603 rowOrCol
+= numRowsOrCols
;
2605 else if ( numRowsOrCols
< 0)
2607 // If rows deleted, either decrement row counter (if row still exists)
2608 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2609 rowOrCol
+= numRowsOrCols
;
2612 m_rowsOrCols
.RemoveAt(n
);
2613 m_attrs
[n
]->DecRef();
2614 m_attrs
.RemoveAt(n
);
2623 // ----------------------------------------------------------------------------
2624 // wxGridCellAttrProvider
2625 // ----------------------------------------------------------------------------
2627 wxGridCellAttrProvider::wxGridCellAttrProvider()
2629 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2632 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2637 void wxGridCellAttrProvider::InitData()
2639 m_data
= new wxGridCellAttrProviderData
;
2642 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2643 wxGridCellAttr::wxAttrKind kind
) const
2645 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2650 case (wxGridCellAttr::Any
):
2651 // Get cached merge attributes.
2652 // Currently not used as no cache implemented as not mutable
2653 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2656 // Basically implement old version.
2657 // Also check merge cache, so we don't have to re-merge every time..
2658 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2659 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2660 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2662 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2664 // Two or more are non NULL
2665 attr
= new wxGridCellAttr
;
2666 attr
->SetKind(wxGridCellAttr::Merged
);
2668 // Order is important..
2671 attr
->MergeWith(attrcell
);
2676 attr
->MergeWith(attrcol
);
2681 attr
->MergeWith(attrrow
);
2685 // store merge attr if cache implemented
2687 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2691 // one or none is non null return it or null.
2710 case (wxGridCellAttr::Cell
):
2711 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2714 case (wxGridCellAttr::Col
):
2715 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2718 case (wxGridCellAttr::Row
):
2719 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2724 // (wxGridCellAttr::Default):
2725 // (wxGridCellAttr::Merged):
2732 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2738 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2741 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2746 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2749 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2754 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2757 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2761 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2763 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2767 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2771 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2773 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2777 // ----------------------------------------------------------------------------
2778 // wxGridTypeRegistry
2779 // ----------------------------------------------------------------------------
2781 wxGridTypeRegistry::~wxGridTypeRegistry()
2783 size_t count
= m_typeinfo
.Count();
2784 for ( size_t i
= 0; i
< count
; i
++ )
2785 delete m_typeinfo
[i
];
2789 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2790 wxGridCellRenderer
* renderer
,
2791 wxGridCellEditor
* editor
)
2793 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2795 // is it already registered?
2796 int loc
= FindRegisteredDataType(typeName
);
2797 if ( loc
!= wxNOT_FOUND
)
2799 delete m_typeinfo
[loc
];
2800 m_typeinfo
[loc
] = info
;
2804 m_typeinfo
.Add(info
);
2808 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2810 size_t count
= m_typeinfo
.GetCount();
2811 for ( size_t i
= 0; i
< count
; i
++ )
2813 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2822 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2824 int index
= FindRegisteredDataType(typeName
);
2825 if ( index
== wxNOT_FOUND
)
2827 // check whether this is one of the standard ones, in which case
2828 // register it "on the fly"
2830 if ( typeName
== wxGRID_VALUE_STRING
)
2832 RegisterDataType(wxGRID_VALUE_STRING
,
2833 new wxGridCellStringRenderer
,
2834 new wxGridCellTextEditor
);
2837 #endif // wxUSE_TEXTCTRL
2839 if ( typeName
== wxGRID_VALUE_BOOL
)
2841 RegisterDataType(wxGRID_VALUE_BOOL
,
2842 new wxGridCellBoolRenderer
,
2843 new wxGridCellBoolEditor
);
2846 #endif // wxUSE_CHECKBOX
2848 if ( typeName
== wxGRID_VALUE_NUMBER
)
2850 RegisterDataType(wxGRID_VALUE_NUMBER
,
2851 new wxGridCellNumberRenderer
,
2852 new wxGridCellNumberEditor
);
2854 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2856 RegisterDataType(wxGRID_VALUE_FLOAT
,
2857 new wxGridCellFloatRenderer
,
2858 new wxGridCellFloatEditor
);
2861 #endif // wxUSE_TEXTCTRL
2863 if ( typeName
== wxGRID_VALUE_CHOICE
)
2865 RegisterDataType(wxGRID_VALUE_CHOICE
,
2866 new wxGridCellStringRenderer
,
2867 new wxGridCellChoiceEditor
);
2870 #endif // wxUSE_COMBOBOX
2875 // we get here only if just added the entry for this type, so return
2877 index
= m_typeinfo
.GetCount() - 1;
2883 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2885 int index
= FindDataType(typeName
);
2886 if ( index
== wxNOT_FOUND
)
2888 // the first part of the typename is the "real" type, anything after ':'
2889 // are the parameters for the renderer
2890 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2891 if ( index
== wxNOT_FOUND
)
2896 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2897 wxGridCellRenderer
*rendererOld
= renderer
;
2898 renderer
= renderer
->Clone();
2899 rendererOld
->DecRef();
2901 wxGridCellEditor
*editor
= GetEditor(index
);
2902 wxGridCellEditor
*editorOld
= editor
;
2903 editor
= editor
->Clone();
2904 editorOld
->DecRef();
2906 // do it even if there are no parameters to reset them to defaults
2907 wxString params
= typeName
.AfterFirst(_T(':'));
2908 renderer
->SetParameters(params
);
2909 editor
->SetParameters(params
);
2911 // register the new typename
2912 RegisterDataType(typeName
, renderer
, editor
);
2914 // we just registered it, it's the last one
2915 index
= m_typeinfo
.GetCount() - 1;
2921 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2923 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2929 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2931 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2937 // ----------------------------------------------------------------------------
2939 // ----------------------------------------------------------------------------
2941 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2944 wxGridTableBase::wxGridTableBase()
2946 m_view
= (wxGrid
*) NULL
;
2947 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2950 wxGridTableBase::~wxGridTableBase()
2952 delete m_attrProvider
;
2955 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2957 delete m_attrProvider
;
2958 m_attrProvider
= attrProvider
;
2961 bool wxGridTableBase::CanHaveAttributes()
2963 if ( ! GetAttrProvider() )
2965 // use the default attr provider by default
2966 SetAttrProvider(new wxGridCellAttrProvider
);
2971 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2973 if ( m_attrProvider
)
2974 return m_attrProvider
->GetAttr(row
, col
, kind
);
2976 return (wxGridCellAttr
*)NULL
;
2980 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2982 if ( m_attrProvider
)
2984 attr
->SetKind(wxGridCellAttr::Cell
);
2985 m_attrProvider
->SetAttr(attr
, row
, col
);
2989 // as we take ownership of the pointer and don't store it, we must
2995 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2997 if ( m_attrProvider
)
2999 attr
->SetKind(wxGridCellAttr::Row
);
3000 m_attrProvider
->SetRowAttr(attr
, row
);
3004 // as we take ownership of the pointer and don't store it, we must
3010 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3012 if ( m_attrProvider
)
3014 attr
->SetKind(wxGridCellAttr::Col
);
3015 m_attrProvider
->SetColAttr(attr
, col
);
3019 // as we take ownership of the pointer and don't store it, we must
3025 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3026 size_t WXUNUSED(numRows
) )
3028 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3033 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3035 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3040 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3041 size_t WXUNUSED(numRows
) )
3043 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3048 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3049 size_t WXUNUSED(numCols
) )
3051 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3056 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3058 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3063 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3064 size_t WXUNUSED(numCols
) )
3066 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3071 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3074 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3075 // how much it makes sense to us geeks.
3079 wxString
wxGridTableBase::GetColLabelValue( int col
)
3081 // default col labels are:
3082 // cols 0 to 25 : A-Z
3083 // cols 26 to 675 : AA-ZZ
3088 for ( n
= 1; ; n
++ )
3090 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3096 // reverse the string...
3098 for ( i
= 0; i
< n
; i
++ )
3106 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3108 return wxGRID_VALUE_STRING
;
3111 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3112 const wxString
& typeName
)
3114 return typeName
== wxGRID_VALUE_STRING
;
3117 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3119 return CanGetValueAs(row
, col
, typeName
);
3122 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3127 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3132 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3137 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3138 long WXUNUSED(value
) )
3142 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3143 double WXUNUSED(value
) )
3147 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3148 bool WXUNUSED(value
) )
3153 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3154 const wxString
& WXUNUSED(typeName
) )
3159 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3160 const wxString
& WXUNUSED(typeName
),
3161 void* WXUNUSED(value
) )
3165 //////////////////////////////////////////////////////////////////////
3167 // Message class for the grid table to send requests and notifications
3171 wxGridTableMessage::wxGridTableMessage()
3173 m_table
= (wxGridTableBase
*) NULL
;
3179 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3180 int commandInt1
, int commandInt2
)
3184 m_comInt1
= commandInt1
;
3185 m_comInt2
= commandInt2
;
3190 //////////////////////////////////////////////////////////////////////
3192 // A basic grid table for string data. An object of this class will
3193 // created by wxGrid if you don't specify an alternative table class.
3196 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3198 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3200 wxGridStringTable::wxGridStringTable()
3205 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3208 m_data
.Alloc( numRows
);
3211 sa
.Alloc( numCols
);
3212 sa
.Add( wxEmptyString
, numCols
);
3214 m_data
.Add( sa
, numRows
);
3217 wxGridStringTable::~wxGridStringTable()
3221 int wxGridStringTable::GetNumberRows()
3223 return m_data
.GetCount();
3226 int wxGridStringTable::GetNumberCols()
3228 if ( m_data
.GetCount() > 0 )
3229 return m_data
[0].GetCount();
3234 wxString
wxGridStringTable::GetValue( int row
, int col
)
3236 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3238 _T("invalid row or column index in wxGridStringTable") );
3240 return m_data
[row
][col
];
3243 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3245 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3246 _T("invalid row or column index in wxGridStringTable") );
3248 m_data
[row
][col
] = value
;
3251 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3253 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3255 _T("invalid row or column index in wxGridStringTable") );
3257 return (m_data
[row
][col
] == wxEmptyString
);
3260 void wxGridStringTable::Clear()
3263 int numRows
, numCols
;
3265 numRows
= m_data
.GetCount();
3268 numCols
= m_data
[0].GetCount();
3270 for ( row
= 0; row
< numRows
; row
++ )
3272 for ( col
= 0; col
< numCols
; col
++ )
3274 m_data
[row
][col
] = wxEmptyString
;
3281 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3283 size_t curNumRows
= m_data
.GetCount();
3284 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3285 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3287 if ( pos
>= curNumRows
)
3289 return AppendRows( numRows
);
3293 sa
.Alloc( curNumCols
);
3294 sa
.Add( wxEmptyString
, curNumCols
);
3295 m_data
.Insert( sa
, pos
, numRows
);
3298 wxGridTableMessage
msg( this,
3299 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3303 GetView()->ProcessTableMessage( msg
);
3309 bool wxGridStringTable::AppendRows( size_t numRows
)
3311 size_t curNumRows
= m_data
.GetCount();
3312 size_t curNumCols
= ( curNumRows
> 0
3313 ? m_data
[0].GetCount()
3314 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3317 if ( curNumCols
> 0 )
3319 sa
.Alloc( curNumCols
);
3320 sa
.Add( wxEmptyString
, curNumCols
);
3323 m_data
.Add( sa
, numRows
);
3327 wxGridTableMessage
msg( this,
3328 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3331 GetView()->ProcessTableMessage( msg
);
3337 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3339 size_t curNumRows
= m_data
.GetCount();
3341 if ( pos
>= curNumRows
)
3343 wxFAIL_MSG( wxString::Format
3345 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3347 (unsigned long)numRows
,
3348 (unsigned long)curNumRows
3354 if ( numRows
> curNumRows
- pos
)
3356 numRows
= curNumRows
- pos
;
3359 if ( numRows
>= curNumRows
)
3365 m_data
.RemoveAt( pos
, numRows
);
3370 wxGridTableMessage
msg( this,
3371 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3375 GetView()->ProcessTableMessage( msg
);
3381 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3385 size_t curNumRows
= m_data
.GetCount();
3386 size_t curNumCols
= ( curNumRows
> 0
3387 ? m_data
[0].GetCount()
3388 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3390 if ( pos
>= curNumCols
)
3392 return AppendCols( numCols
);
3395 for ( row
= 0; row
< curNumRows
; row
++ )
3397 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3399 m_data
[row
].Insert( wxEmptyString
, col
);
3405 wxGridTableMessage
msg( this,
3406 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3410 GetView()->ProcessTableMessage( msg
);
3416 bool wxGridStringTable::AppendCols( size_t numCols
)
3420 size_t curNumRows
= m_data
.GetCount();
3424 // TODO: something better than this ?
3426 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3431 for ( row
= 0; row
< curNumRows
; row
++ )
3433 m_data
[row
].Add( wxEmptyString
, numCols
);
3438 wxGridTableMessage
msg( this,
3439 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3442 GetView()->ProcessTableMessage( msg
);
3448 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3452 size_t curNumRows
= m_data
.GetCount();
3453 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3454 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3456 if ( pos
>= curNumCols
)
3458 wxFAIL_MSG( wxString::Format
3460 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3462 (unsigned long)numCols
,
3463 (unsigned long)curNumCols
3468 if ( numCols
> curNumCols
- pos
)
3470 numCols
= curNumCols
- pos
;
3473 for ( row
= 0; row
< curNumRows
; row
++ )
3475 if ( numCols
>= curNumCols
)
3477 m_data
[row
].Clear();
3481 m_data
[row
].RemoveAt( pos
, numCols
);
3487 wxGridTableMessage
msg( this,
3488 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3492 GetView()->ProcessTableMessage( msg
);
3498 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3500 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3502 // using default label
3504 return wxGridTableBase::GetRowLabelValue( row
);
3508 return m_rowLabels
[ row
];
3512 wxString
wxGridStringTable::GetColLabelValue( int col
)
3514 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3516 // using default label
3518 return wxGridTableBase::GetColLabelValue( col
);
3522 return m_colLabels
[ col
];
3526 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3528 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3530 int n
= m_rowLabels
.GetCount();
3532 for ( i
= n
; i
<= row
; i
++ )
3534 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3538 m_rowLabels
[row
] = value
;
3541 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3543 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3545 int n
= m_colLabels
.GetCount();
3547 for ( i
= n
; i
<= col
; i
++ )
3549 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3553 m_colLabels
[col
] = value
;
3557 //////////////////////////////////////////////////////////////////////
3558 //////////////////////////////////////////////////////////////////////
3560 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3562 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3563 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3564 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3565 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3566 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3567 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3568 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3571 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3573 const wxPoint
&pos
, const wxSize
&size
)
3574 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3579 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3583 // NO - don't do this because it will set both the x and y origin
3584 // coords to match the parent scrolled window and we just want to
3585 // set the y coord - MB
3587 // m_owner->PrepareDC( dc );
3590 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3591 dc
.SetDeviceOrigin( 0, -y
);
3593 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3594 m_owner
->DrawRowLabels( dc
, rows
);
3597 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3599 m_owner
->ProcessRowLabelMouseEvent( event
);
3602 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3604 m_owner
->GetEventHandler()->ProcessEvent(event
);
3607 // This seems to be required for wxMotif otherwise the mouse
3608 // cursor must be in the cell edit control to get key events
3610 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3612 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3615 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3617 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3620 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3622 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3626 //////////////////////////////////////////////////////////////////////
3628 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3630 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3631 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3632 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3633 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3634 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3635 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3636 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3639 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3641 const wxPoint
&pos
, const wxSize
&size
)
3642 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3647 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3651 // NO - don't do this because it will set both the x and y origin
3652 // coords to match the parent scrolled window and we just want to
3653 // set the x coord - MB
3655 // m_owner->PrepareDC( dc );
3658 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3659 dc
.SetDeviceOrigin( -x
, 0 );
3661 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3662 m_owner
->DrawColLabels( dc
, cols
);
3665 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3667 m_owner
->ProcessColLabelMouseEvent( event
);
3670 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3672 m_owner
->GetEventHandler()->ProcessEvent(event
);
3675 // This seems to be required for wxMotif otherwise the mouse
3676 // cursor must be in the cell edit control to get key events
3678 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3680 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3684 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3686 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3690 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3692 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3697 //////////////////////////////////////////////////////////////////////
3699 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3701 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3702 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3703 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3704 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3705 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3706 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3707 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3710 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3712 const wxPoint
&pos
, const wxSize
&size
)
3713 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3718 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3722 int client_height
= 0;
3723 int client_width
= 0;
3724 GetClientSize( &client_width
, &client_height
);
3726 // VZ: any reason for this ifdef? (FIXME)
3731 rect
.SetWidth( client_width
- 2 );
3732 rect
.SetHeight( client_height
- 2 );
3734 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3736 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3737 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3738 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3739 dc
.DrawLine( 0, 0, client_width
, 0 );
3740 dc
.DrawLine( 0, 0, 0, client_height
);
3742 dc
.SetPen( *wxWHITE_PEN
);
3743 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3744 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3745 #endif // __WXGTK__/!__WXGTK__
3748 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3750 m_owner
->ProcessCornerLabelMouseEvent( event
);
3753 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3755 m_owner
->GetEventHandler()->ProcessEvent(event
);
3758 // This seems to be required for wxMotif otherwise the mouse
3759 // cursor must be in the cell edit control to get key events
3761 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3763 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3766 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3768 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3771 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3773 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3777 //////////////////////////////////////////////////////////////////////
3779 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3781 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3782 EVT_PAINT( wxGridWindow::OnPaint
)
3783 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3784 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3785 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3786 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3787 EVT_CHAR ( wxGridWindow::OnChar
)
3788 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3789 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3790 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3793 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3794 wxGridRowLabelWindow
*rowLblWin
,
3795 wxGridColLabelWindow
*colLblWin
,
3798 const wxSize
&size
)
3799 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3800 wxT("grid window") )
3803 m_rowLabelWin
= rowLblWin
;
3804 m_colLabelWin
= colLblWin
;
3807 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3809 wxPaintDC
dc( this );
3810 m_owner
->PrepareDC( dc
);
3811 wxRegion reg
= GetUpdateRegion();
3812 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3813 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3814 #if WXGRID_DRAW_LINES
3815 m_owner
->DrawAllGridLines( dc
, reg
);
3817 m_owner
->DrawGridSpace( dc
);
3818 m_owner
->DrawHighlight( dc
, DirtyCells
);
3821 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3823 wxWindow::ScrollWindow( dx
, dy
, rect
);
3824 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3825 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3828 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3830 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3833 m_owner
->ProcessGridCellMouseEvent( event
);
3836 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3838 m_owner
->GetEventHandler()->ProcessEvent(event
);
3841 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3842 // cursor must be in the cell edit control to get key events
3844 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3846 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3850 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3852 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3856 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3858 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3862 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3866 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3868 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3872 //////////////////////////////////////////////////////////////////////
3874 // Internal Helper function for computing row or column from some
3875 // (unscrolled) coordinate value, using either
3876 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3877 // of m_rowBottoms/m_ColRights to speed up the search!
3879 // Internal helper macros for simpler use of that function
3881 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3882 const wxArrayInt
& BorderArray
, int nMax
,
3885 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3886 m_minAcceptableColWidth, \
3887 m_colRights, m_numCols, true)
3888 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3889 m_minAcceptableRowHeight, \
3890 m_rowBottoms, m_numRows, true)
3891 /////////////////////////////////////////////////////////////////////
3893 #if wxUSE_EXTENDED_RTTI
3894 WX_DEFINE_FLAGS( wxGridStyle
)
3896 wxBEGIN_FLAGS( wxGridStyle
)
3897 // new style border flags, we put them first to
3898 // use them for streaming out
3899 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3900 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3901 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3902 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3903 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3904 wxFLAGS_MEMBER(wxBORDER_NONE
)
3906 // old style border flags
3907 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3908 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3909 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3910 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3911 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3912 wxFLAGS_MEMBER(wxBORDER
)
3914 // standard window styles
3915 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3916 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3917 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3918 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3919 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3920 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3921 wxFLAGS_MEMBER(wxVSCROLL
)
3922 wxFLAGS_MEMBER(wxHSCROLL
)
3924 wxEND_FLAGS( wxGridStyle
)
3926 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3928 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3929 wxHIDE_PROPERTY( Children
)
3930 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3931 wxEND_PROPERTIES_TABLE()
3933 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3934 wxEND_HANDLERS_TABLE()
3936 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3939 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3942 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3945 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3946 EVT_PAINT( wxGrid::OnPaint
)
3947 EVT_SIZE( wxGrid::OnSize
)
3948 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3949 EVT_KEY_UP( wxGrid::OnKeyUp
)
3950 EVT_CHAR ( wxGrid::OnChar
)
3951 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3956 // in order to make sure that a size event is not
3957 // trigerred in a unfinished state
3958 m_cornerLabelWin
= NULL
;
3959 m_rowLabelWin
= NULL
;
3960 m_colLabelWin
= NULL
;
3964 wxGrid::wxGrid( wxWindow
*parent
,
3969 const wxString
& name
)
3970 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3971 m_colMinWidths(GRID_HASH_SIZE
),
3972 m_rowMinHeights(GRID_HASH_SIZE
)
3975 SetBestFittingSize(size
);
3978 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3979 const wxPoint
& pos
, const wxSize
& size
,
3980 long style
, const wxString
& name
)
3982 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3983 style
| wxWANTS_CHARS
, name
))
3986 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3987 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3990 SetBestFittingSize(size
);
3998 // Must do this or ~wxScrollHelper will pop the wrong event handler
3999 SetTargetWindow(this);
4001 wxSafeDecRef(m_defaultCellAttr
);
4003 #ifdef DEBUG_ATTR_CACHE
4004 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4005 wxPrintf(_T("wxGrid attribute cache statistics: "
4006 "total: %u, hits: %u (%u%%)\n"),
4007 total
, gs_nAttrCacheHits
,
4008 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4014 delete m_typeRegistry
;
4020 // ----- internal init and update functions
4023 // NOTE: If using the default visual attributes works everywhere then this can
4024 // be removed as well as the #else cases below.
4025 #define _USE_VISATTR 0
4028 #include "wx/listbox.h"
4031 void wxGrid::Create()
4033 m_created
= false; // set to true by CreateGrid
4035 m_table
= (wxGridTableBase
*) NULL
;
4038 m_cellEditCtrlEnabled
= false;
4040 m_defaultCellAttr
= new wxGridCellAttr();
4042 // Set default cell attributes
4043 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4044 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4045 m_defaultCellAttr
->SetFont(GetFont());
4046 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4047 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4048 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4051 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4052 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4054 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4055 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4058 m_defaultCellAttr
->SetTextColour(
4059 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4060 m_defaultCellAttr
->SetBackgroundColour(
4061 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4066 m_currentCellCoords
= wxGridNoCellCoords
;
4068 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4069 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4071 // create the type registry
4072 m_typeRegistry
= new wxGridTypeRegistry
;
4075 // subwindow components that make up the wxGrid
4076 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4081 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4086 m_colLabelWin
= new wxGridColLabelWindow( this,
4091 m_gridWin
= new wxGridWindow( this,
4098 SetTargetWindow( m_gridWin
);
4101 wxColour gfg
= gva
.colFg
;
4102 wxColour gbg
= gva
.colBg
;
4103 wxColour lfg
= lva
.colFg
;
4104 wxColour lbg
= lva
.colBg
;
4106 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4107 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4108 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4109 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4111 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4112 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4113 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4114 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4115 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4116 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4118 m_gridWin
->SetOwnForegroundColour(gfg
);
4119 m_gridWin
->SetOwnBackgroundColour(gbg
);
4125 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4126 wxGrid::wxGridSelectionModes selmode
)
4128 wxCHECK_MSG( !m_created
,
4130 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4132 m_numRows
= numRows
;
4133 m_numCols
= numCols
;
4135 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4136 m_table
->SetView( this );
4138 m_selection
= new wxGridSelection( this, selmode
);
4147 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4149 wxCHECK_RET( m_created
,
4150 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4152 m_selection
->SetSelectionMode( selmode
);
4155 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4157 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4158 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4160 return m_selection
->GetSelectionMode();
4163 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4164 wxGrid::wxGridSelectionModes selmode
)
4168 // stop all processing
4173 wxGridTableBase
*t
=m_table
;
4186 m_numRows
= table
->GetNumberRows();
4187 m_numCols
= table
->GetNumberCols();
4190 m_table
->SetView( this );
4191 m_ownTable
= takeOwnership
;
4192 m_selection
= new wxGridSelection( this, selmode
);
4205 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4206 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4208 if ( m_rowLabelWin
)
4210 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4214 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4217 m_labelTextColour
= wxColour( _T("BLACK") );
4220 m_attrCache
.row
= -1;
4221 m_attrCache
.col
= -1;
4222 m_attrCache
.attr
= NULL
;
4224 // TODO: something better than this ?
4226 m_labelFont
= this->GetFont();
4227 m_labelFont
.SetWeight( wxBOLD
);
4229 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4230 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4232 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4233 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4234 m_colLabelTextOrientation
= wxHORIZONTAL
;
4236 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4237 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4239 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4240 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4242 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4243 m_defaultRowHeight
+= 8;
4245 m_defaultRowHeight
+= 4;
4248 m_gridLineColour
= wxColour( 192,192,192 );
4249 m_gridLinesEnabled
= true;
4250 m_cellHighlightColour
= *wxBLACK
;
4251 m_cellHighlightPenWidth
= 2;
4252 m_cellHighlightROPenWidth
= 1;
4254 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4255 m_winCapture
= (wxWindow
*)NULL
;
4256 m_canDragRowSize
= true;
4257 m_canDragColSize
= true;
4258 m_canDragGridSize
= true;
4259 m_canDragCell
= false;
4261 m_dragRowOrCol
= -1;
4262 m_isDragging
= false;
4263 m_startDragPos
= wxDefaultPosition
;
4265 m_waitForSlowClick
= false;
4267 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4268 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4270 m_currentCellCoords
= wxGridNoCellCoords
;
4272 m_selectingTopLeft
= wxGridNoCellCoords
;
4273 m_selectingBottomRight
= wxGridNoCellCoords
;
4274 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4275 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4277 m_editable
= true; // default for whole grid
4279 m_inOnKeyDown
= false;
4285 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4286 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4289 // ----------------------------------------------------------------------------
4290 // the idea is to call these functions only when necessary because they create
4291 // quite big arrays which eat memory mostly unnecessary - in particular, if
4292 // default widths/heights are used for all rows/columns, we may not use these
4295 // with some extra code, it should be possible to only store the
4296 // widths/heights different from default ones but this will be done later...
4297 // ----------------------------------------------------------------------------
4299 void wxGrid::InitRowHeights()
4301 m_rowHeights
.Empty();
4302 m_rowBottoms
.Empty();
4304 m_rowHeights
.Alloc( m_numRows
);
4305 m_rowBottoms
.Alloc( m_numRows
);
4309 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4311 for ( int i
= 0; i
< m_numRows
; i
++ )
4313 rowBottom
+= m_defaultRowHeight
;
4314 m_rowBottoms
.Add( rowBottom
);
4318 void wxGrid::InitColWidths()
4320 m_colWidths
.Empty();
4321 m_colRights
.Empty();
4323 m_colWidths
.Alloc( m_numCols
);
4324 m_colRights
.Alloc( m_numCols
);
4327 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4329 for ( int i
= 0; i
< m_numCols
; i
++ )
4331 colRight
+= m_defaultColWidth
;
4332 m_colRights
.Add( colRight
);
4336 int wxGrid::GetColWidth(int col
) const
4338 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4341 int wxGrid::GetColLeft(int col
) const
4343 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4344 : m_colRights
[col
] - m_colWidths
[col
];
4347 int wxGrid::GetColRight(int col
) const
4349 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4353 int wxGrid::GetRowHeight(int row
) const
4355 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4358 int wxGrid::GetRowTop(int row
) const
4360 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4361 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4364 int wxGrid::GetRowBottom(int row
) const
4366 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4367 : m_rowBottoms
[row
];
4370 void wxGrid::CalcDimensions()
4373 GetClientSize( &cw
, &ch
);
4375 if ( m_rowLabelWin
->IsShown() )
4376 cw
-= m_rowLabelWidth
;
4377 if ( m_colLabelWin
->IsShown() )
4378 ch
-= m_colLabelHeight
;
4381 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4382 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4384 // take into account editor if shown
4385 if ( IsCellEditControlShown() )
4388 int r
= m_currentCellCoords
.GetRow();
4389 int c
= m_currentCellCoords
.GetCol();
4390 int x
= GetColLeft(c
);
4391 int y
= GetRowTop(r
);
4393 // how big is the editor
4394 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4395 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4396 editor
->GetControl()->GetSize(&w2
, &h2
);
4407 // preserve (more or less) the previous position
4409 GetViewStart( &x
, &y
);
4411 // ensure the position is valid for the new scroll ranges
4413 x
= wxMax( w
- 1, 0 );
4415 y
= wxMax( h
- 1, 0 );
4417 // do set scrollbar parameters
4418 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4419 GetScrollX(w
), GetScrollY(h
), x
, y
,
4420 GetBatchCount() != 0);
4422 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4423 // still must reposition the children
4428 void wxGrid::CalcWindowSizes()
4430 // escape if the window is has not been fully created yet
4432 if ( m_cornerLabelWin
== NULL
)
4436 GetClientSize( &cw
, &ch
);
4438 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4439 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4441 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4442 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4444 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4445 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4447 if ( m_gridWin
&& m_gridWin
->IsShown() )
4448 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4452 // this is called when the grid table sends a message to say that it
4453 // has been redimensioned
4455 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4458 bool result
= false;
4460 // Clear the attribute cache as the attribute might refer to a different
4461 // cell than stored in the cache after adding/removing rows/columns.
4463 // By the same reasoning, the editor should be dismissed if columns are
4464 // added or removed. And for consistency, it should IMHO always be
4465 // removed, not only if the cell "underneath" it actually changes.
4466 // For now, I intentionally do not save the editor's content as the
4467 // cell it might want to save that stuff to might no longer exist.
4468 HideCellEditControl();
4470 // if we were using the default widths/heights so far, we must change them
4472 if ( m_colWidths
.IsEmpty() )
4477 if ( m_rowHeights
.IsEmpty() )
4483 switch ( msg
.GetId() )
4485 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4487 size_t pos
= msg
.GetCommandInt();
4488 int numRows
= msg
.GetCommandInt2();
4490 m_numRows
+= numRows
;
4492 if ( !m_rowHeights
.IsEmpty() )
4494 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4495 m_rowBottoms
.Insert( 0, pos
, numRows
);
4498 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4500 for ( i
= pos
; i
< m_numRows
; i
++ )
4502 bottom
+= m_rowHeights
[i
];
4503 m_rowBottoms
[i
] = bottom
;
4506 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4508 // if we have just inserted cols into an empty grid the current
4509 // cell will be undefined...
4511 SetCurrentCell( 0, 0 );
4515 m_selection
->UpdateRows( pos
, numRows
);
4516 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4518 attrProvider
->UpdateAttrRows( pos
, numRows
);
4520 if ( !GetBatchCount() )
4523 m_rowLabelWin
->Refresh();
4529 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4531 int numRows
= msg
.GetCommandInt();
4532 int oldNumRows
= m_numRows
;
4533 m_numRows
+= numRows
;
4535 if ( !m_rowHeights
.IsEmpty() )
4537 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4538 m_rowBottoms
.Add( 0, numRows
);
4541 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4543 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4545 bottom
+= m_rowHeights
[i
];
4546 m_rowBottoms
[i
] = bottom
;
4549 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4551 // if we have just inserted cols into an empty grid the current
4552 // cell will be undefined...
4554 SetCurrentCell( 0, 0 );
4556 if ( !GetBatchCount() )
4559 m_rowLabelWin
->Refresh();
4565 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4567 size_t pos
= msg
.GetCommandInt();
4568 int numRows
= msg
.GetCommandInt2();
4569 m_numRows
-= numRows
;
4571 if ( !m_rowHeights
.IsEmpty() )
4573 m_rowHeights
.RemoveAt( pos
, numRows
);
4574 m_rowBottoms
.RemoveAt( pos
, numRows
);
4577 for ( i
= 0; i
< m_numRows
; i
++ )
4579 h
+= m_rowHeights
[i
];
4580 m_rowBottoms
[i
] = h
;
4585 m_currentCellCoords
= wxGridNoCellCoords
;
4589 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4590 m_currentCellCoords
.Set( 0, 0 );
4594 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4595 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4598 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4599 // ifdef'd out following patch from Paul Gammans
4601 // No need to touch column attributes, unless we
4602 // removed _all_ rows, in this case, we remove
4603 // all column attributes.
4604 // I hate to do this here, but the
4605 // needed data is not available inside UpdateAttrRows.
4606 if ( !GetNumberRows() )
4607 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4610 if ( !GetBatchCount() )
4613 m_rowLabelWin
->Refresh();
4619 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4621 size_t pos
= msg
.GetCommandInt();
4622 int numCols
= msg
.GetCommandInt2();
4623 m_numCols
+= numCols
;
4625 if ( !m_colWidths
.IsEmpty() )
4627 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4628 m_colRights
.Insert( 0, pos
, numCols
);
4631 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4633 for ( i
= pos
; i
< m_numCols
; i
++ )
4635 right
+= m_colWidths
[i
];
4636 m_colRights
[i
] = right
;
4639 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4641 // if we have just inserted cols into an empty grid the current
4642 // cell will be undefined...
4644 SetCurrentCell( 0, 0 );
4648 m_selection
->UpdateCols( pos
, numCols
);
4649 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4651 attrProvider
->UpdateAttrCols( pos
, numCols
);
4652 if ( !GetBatchCount() )
4655 m_colLabelWin
->Refresh();
4662 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4664 int numCols
= msg
.GetCommandInt();
4665 int oldNumCols
= m_numCols
;
4666 m_numCols
+= numCols
;
4667 if ( !m_colWidths
.IsEmpty() )
4669 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4670 m_colRights
.Add( 0, numCols
);
4673 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4675 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4677 right
+= m_colWidths
[i
];
4678 m_colRights
[i
] = right
;
4681 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4683 // if we have just inserted cols into an empty grid the current
4684 // cell will be undefined...
4686 SetCurrentCell( 0, 0 );
4688 if ( !GetBatchCount() )
4691 m_colLabelWin
->Refresh();
4697 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4699 size_t pos
= msg
.GetCommandInt();
4700 int numCols
= msg
.GetCommandInt2();
4701 m_numCols
-= numCols
;
4703 if ( !m_colWidths
.IsEmpty() )
4705 m_colWidths
.RemoveAt( pos
, numCols
);
4706 m_colRights
.RemoveAt( pos
, numCols
);
4709 for ( i
= 0; i
< m_numCols
; i
++ )
4711 w
+= m_colWidths
[i
];
4717 m_currentCellCoords
= wxGridNoCellCoords
;
4721 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4722 m_currentCellCoords
.Set( 0, 0 );
4726 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4727 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4730 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4731 // ifdef'd out following patch from Paul Gammans
4733 // No need to touch row attributes, unless we
4734 // removed _all_ columns, in this case, we remove
4735 // all row attributes.
4736 // I hate to do this here, but the
4737 // needed data is not available inside UpdateAttrCols.
4738 if ( !GetNumberCols() )
4739 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4742 if ( !GetBatchCount() )
4745 m_colLabelWin
->Refresh();
4752 if (result
&& !GetBatchCount() )
4753 m_gridWin
->Refresh();
4758 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4760 wxRegionIterator
iter( reg
);
4763 wxArrayInt rowlabels
;
4770 // TODO: remove this when we can...
4771 // There is a bug in wxMotif that gives garbage update
4772 // rectangles if you jump-scroll a long way by clicking the
4773 // scrollbar with middle button. This is a work-around
4775 #if defined(__WXMOTIF__)
4777 m_gridWin
->GetClientSize( &cw
, &ch
);
4778 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4779 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4782 // logical bounds of update region
4785 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4786 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4788 // find the row labels within these bounds
4791 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4793 if ( GetRowBottom(row
) < top
)
4796 if ( GetRowTop(row
) > bottom
)
4799 rowlabels
.Add( row
);
4809 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4811 wxRegionIterator
iter( reg
);
4814 wxArrayInt colLabels
;
4821 // TODO: remove this when we can...
4822 // There is a bug in wxMotif that gives garbage update
4823 // rectangles if you jump-scroll a long way by clicking the
4824 // scrollbar with middle button. This is a work-around
4826 #if defined(__WXMOTIF__)
4828 m_gridWin
->GetClientSize( &cw
, &ch
);
4829 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4830 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4833 // logical bounds of update region
4836 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4837 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4839 // find the cells within these bounds
4842 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4844 if ( GetColRight(col
) < left
)
4847 if ( GetColLeft(col
) > right
)
4850 colLabels
.Add( col
);
4859 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4861 wxRegionIterator
iter( reg
);
4864 wxGridCellCoordsArray cellsExposed
;
4866 int left
, top
, right
, bottom
;
4871 // TODO: remove this when we can...
4872 // There is a bug in wxMotif that gives garbage update
4873 // rectangles if you jump-scroll a long way by clicking the
4874 // scrollbar with middle button. This is a work-around
4876 #if defined(__WXMOTIF__)
4878 m_gridWin
->GetClientSize( &cw
, &ch
);
4879 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4880 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4881 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4882 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4885 // logical bounds of update region
4887 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4888 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4890 // find the cells within these bounds
4893 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4895 if ( GetRowBottom(row
) <= top
)
4898 if ( GetRowTop(row
) > bottom
)
4901 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4903 if ( GetColRight(col
) <= left
)
4906 if ( GetColLeft(col
) > right
)
4909 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4916 return cellsExposed
;
4920 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4923 wxPoint
pos( event
.GetPosition() );
4924 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4926 if ( event
.Dragging() )
4930 m_isDragging
= true;
4931 m_rowLabelWin
->CaptureMouse();
4934 if ( event
.LeftIsDown() )
4936 switch ( m_cursorMode
)
4938 case WXGRID_CURSOR_RESIZE_ROW
:
4940 int cw
, ch
, left
, dummy
;
4941 m_gridWin
->GetClientSize( &cw
, &ch
);
4942 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4944 wxClientDC
dc( m_gridWin
);
4947 GetRowTop(m_dragRowOrCol
) +
4948 GetRowMinimalHeight(m_dragRowOrCol
) );
4949 dc
.SetLogicalFunction(wxINVERT
);
4950 if ( m_dragLastPos
>= 0 )
4952 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4954 dc
.DrawLine( left
, y
, left
+cw
, y
);
4959 case WXGRID_CURSOR_SELECT_ROW
:
4961 if ( (row
= YToRow( y
)) >= 0 )
4965 m_selection
->SelectRow( row
,
4966 event
.ControlDown(),
4975 // default label to suppress warnings about "enumeration value
4976 // 'xxx' not handled in switch
4984 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4989 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4990 m_isDragging
= false;
4993 // ------------ Entering or leaving the window
4995 if ( event
.Entering() || event
.Leaving() )
4997 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5001 // ------------ Left button pressed
5003 else if ( event
.LeftDown() )
5005 // don't send a label click event for a hit on the
5006 // edge of the row label - this is probably the user
5007 // wanting to resize the row
5009 if ( YToEdgeOfRow(y
) < 0 )
5013 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5015 if ( !event
.ShiftDown() && !event
.ControlDown() )
5019 if ( event
.ShiftDown() )
5021 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5024 GetNumberCols() - 1,
5025 event
.ControlDown(),
5032 m_selection
->SelectRow( row
,
5033 event
.ControlDown(),
5040 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5045 // starting to drag-resize a row
5047 if ( CanDragRowSize() )
5048 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5053 // ------------ Left double click
5055 else if (event
.LeftDClick() )
5057 row
= YToEdgeOfRow(y
);
5062 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5064 // no default action at the moment
5069 // adjust row height depending on label text
5070 AutoSizeRowLabelSize( row
);
5072 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5078 // ------------ Left button released
5080 else if ( event
.LeftUp() )
5082 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5084 DoEndDragResizeRow();
5086 // Note: we are ending the event *after* doing
5087 // default processing in this case
5089 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5092 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5097 // ------------ Right button down
5099 else if ( event
.RightDown() )
5103 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5105 // no default action at the moment
5110 // ------------ Right double click
5112 else if ( event
.RightDClick() )
5116 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5118 // no default action at the moment
5123 // ------------ No buttons down and mouse moving
5125 else if ( event
.Moving() )
5127 m_dragRowOrCol
= YToEdgeOfRow( y
);
5128 if ( m_dragRowOrCol
>= 0 )
5130 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5132 // don't capture the mouse yet
5133 if ( CanDragRowSize() )
5134 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5137 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5139 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5145 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5148 wxPoint
pos( event
.GetPosition() );
5149 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5151 if ( event
.Dragging() )
5155 m_isDragging
= true;
5156 m_colLabelWin
->CaptureMouse();
5159 if ( event
.LeftIsDown() )
5161 switch ( m_cursorMode
)
5163 case WXGRID_CURSOR_RESIZE_COL
:
5165 int cw
, ch
, dummy
, top
;
5166 m_gridWin
->GetClientSize( &cw
, &ch
);
5167 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5169 wxClientDC
dc( m_gridWin
);
5172 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5173 GetColMinimalWidth(m_dragRowOrCol
));
5174 dc
.SetLogicalFunction(wxINVERT
);
5175 if ( m_dragLastPos
>= 0 )
5177 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5179 dc
.DrawLine( x
, top
, x
, top
+ch
);
5184 case WXGRID_CURSOR_SELECT_COL
:
5186 if ( (col
= XToCol( x
)) >= 0 )
5190 m_selection
->SelectCol( col
,
5191 event
.ControlDown(),
5200 // default label to suppress warnings about "enumeration value
5201 // 'xxx' not handled in switch
5209 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5214 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5215 m_isDragging
= false;
5218 // ------------ Entering or leaving the window
5220 if ( event
.Entering() || event
.Leaving() )
5222 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5226 // ------------ Left button pressed
5228 else if ( event
.LeftDown() )
5230 // don't send a label click event for a hit on the
5231 // edge of the col label - this is probably the user
5232 // wanting to resize the col
5234 if ( XToEdgeOfCol(x
) < 0 )
5238 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5240 if ( !event
.ShiftDown() && !event
.ControlDown() )
5244 if ( event
.ShiftDown() )
5246 m_selection
->SelectBlock( 0,
5247 m_currentCellCoords
.GetCol(),
5248 GetNumberRows() - 1, col
,
5249 event
.ControlDown(),
5256 m_selection
->SelectCol( col
,
5257 event
.ControlDown(),
5264 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5269 // starting to drag-resize a col
5271 if ( CanDragColSize() )
5272 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5277 // ------------ Left double click
5279 if ( event
.LeftDClick() )
5281 col
= XToEdgeOfCol(x
);
5286 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5288 // no default action at the moment
5293 // adjust column width depending on label text
5294 AutoSizeColLabelSize( col
);
5296 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5302 // ------------ Left button released
5304 else if ( event
.LeftUp() )
5306 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5308 DoEndDragResizeCol();
5310 // Note: we are ending the event *after* doing
5311 // default processing in this case
5313 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5316 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5321 // ------------ Right button down
5323 else if ( event
.RightDown() )
5327 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5329 // no default action at the moment
5334 // ------------ Right double click
5336 else if ( event
.RightDClick() )
5340 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5342 // no default action at the moment
5347 // ------------ No buttons down and mouse moving
5349 else if ( event
.Moving() )
5351 m_dragRowOrCol
= XToEdgeOfCol( x
);
5352 if ( m_dragRowOrCol
>= 0 )
5354 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5356 // don't capture the cursor yet
5357 if ( CanDragColSize() )
5358 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5361 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5363 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5369 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5371 if ( event
.LeftDown() )
5373 // indicate corner label by having both row and
5376 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5382 else if ( event
.LeftDClick() )
5384 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5387 else if ( event
.RightDown() )
5389 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5391 // no default action at the moment
5395 else if ( event
.RightDClick() )
5397 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5399 // no default action at the moment
5404 void wxGrid::ChangeCursorMode(CursorMode mode
,
5409 static const wxChar
*cursorModes
[] =
5418 wxLogTrace(_T("grid"),
5419 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5420 win
== m_colLabelWin
? _T("colLabelWin")
5421 : win
? _T("rowLabelWin")
5423 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5424 #endif // __WXDEBUG__
5426 if ( mode
== m_cursorMode
&&
5427 win
== m_winCapture
&&
5428 captureMouse
== (m_winCapture
!= NULL
))
5433 // by default use the grid itself
5439 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5440 m_winCapture
= (wxWindow
*)NULL
;
5443 m_cursorMode
= mode
;
5445 switch ( m_cursorMode
)
5447 case WXGRID_CURSOR_RESIZE_ROW
:
5448 win
->SetCursor( m_rowResizeCursor
);
5451 case WXGRID_CURSOR_RESIZE_COL
:
5452 win
->SetCursor( m_colResizeCursor
);
5456 win
->SetCursor( *wxSTANDARD_CURSOR
);
5459 // we need to capture mouse when resizing
5460 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5461 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5463 if ( captureMouse
&& resize
)
5465 win
->CaptureMouse();
5470 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5473 wxPoint
pos( event
.GetPosition() );
5474 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5476 wxGridCellCoords coords
;
5477 XYToCell( x
, y
, coords
);
5479 int cell_rows
, cell_cols
;
5480 bool isFirstDrag
= !m_isDragging
;
5481 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5482 if ((cell_rows
< 0) || (cell_cols
< 0))
5484 coords
.SetRow(coords
.GetRow() + cell_rows
);
5485 coords
.SetCol(coords
.GetCol() + cell_cols
);
5488 if ( event
.Dragging() )
5490 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5492 // Don't start doing anything until the mouse has been dragged at
5493 // least 3 pixels in any direction...
5496 if (m_startDragPos
== wxDefaultPosition
)
5498 m_startDragPos
= pos
;
5501 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5505 m_isDragging
= true;
5506 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5508 // Hide the edit control, so it
5509 // won't interfere with drag-shrinking.
5510 if ( IsCellEditControlShown() )
5512 HideCellEditControl();
5513 SaveEditControlValue();
5516 // Have we captured the mouse yet?
5519 m_winCapture
= m_gridWin
;
5520 m_winCapture
->CaptureMouse();
5523 if ( coords
!= wxGridNoCellCoords
)
5525 if ( event
.ControlDown() )
5527 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5528 m_selectingKeyboard
= coords
;
5529 HighlightBlock ( m_selectingKeyboard
, coords
);
5531 else if ( CanDragCell() )
5535 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5536 m_selectingKeyboard
= coords
;
5538 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5546 if ( !IsSelection() )
5548 HighlightBlock( coords
, coords
);
5552 HighlightBlock( m_currentCellCoords
, coords
);
5556 if (! IsVisible(coords
))
5558 MakeCellVisible(coords
);
5559 // TODO: need to introduce a delay or something here. The
5560 // scrolling is way to fast, at least on MSW - also on GTK.
5564 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5566 int cw
, ch
, left
, dummy
;
5567 m_gridWin
->GetClientSize( &cw
, &ch
);
5568 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5570 wxClientDC
dc( m_gridWin
);
5572 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5573 GetRowMinimalHeight(m_dragRowOrCol
) );
5574 dc
.SetLogicalFunction(wxINVERT
);
5575 if ( m_dragLastPos
>= 0 )
5577 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5579 dc
.DrawLine( left
, y
, left
+cw
, y
);
5582 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5584 int cw
, ch
, dummy
, top
;
5585 m_gridWin
->GetClientSize( &cw
, &ch
);
5586 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5588 wxClientDC
dc( m_gridWin
);
5590 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5591 GetColMinimalWidth(m_dragRowOrCol
) );
5592 dc
.SetLogicalFunction(wxINVERT
);
5593 if ( m_dragLastPos
>= 0 )
5595 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5597 dc
.DrawLine( x
, top
, x
, top
+ch
);
5604 m_isDragging
= false;
5605 m_startDragPos
= wxDefaultPosition
;
5607 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5608 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5611 if ( event
.Entering() || event
.Leaving() )
5613 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5614 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5619 // ------------ Left button pressed
5621 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5623 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5628 if ( !event
.ControlDown() )
5630 if ( event
.ShiftDown() )
5634 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5635 m_currentCellCoords
.GetCol(),
5638 event
.ControlDown(),
5644 else if ( XToEdgeOfCol(x
) < 0 &&
5645 YToEdgeOfRow(y
) < 0 )
5647 DisableCellEditControl();
5648 MakeCellVisible( coords
);
5650 if ( event
.ControlDown() )
5654 m_selection
->ToggleCellSelection( coords
.GetRow(),
5656 event
.ControlDown(),
5661 m_selectingTopLeft
= wxGridNoCellCoords
;
5662 m_selectingBottomRight
= wxGridNoCellCoords
;
5663 m_selectingKeyboard
= coords
;
5667 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5668 SetCurrentCell( coords
);
5671 if ( m_selection
->GetSelectionMode() !=
5672 wxGrid::wxGridSelectCells
)
5674 HighlightBlock( coords
, coords
);
5683 // ------------ Left double click
5685 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5687 DisableCellEditControl();
5689 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5691 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5696 // we want double click to select a cell and start editing
5697 // (i.e. to behave in same way as sequence of two slow clicks):
5698 m_waitForSlowClick
= true;
5705 // ------------ Left button released
5707 else if ( event
.LeftUp() )
5709 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5713 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5714 m_winCapture
= NULL
;
5717 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5720 EnableCellEditControl();
5722 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5723 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5724 editor
->StartingClick();
5728 m_waitForSlowClick
= false;
5730 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5731 m_selectingBottomRight
!= wxGridNoCellCoords
)
5735 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5736 m_selectingTopLeft
.GetCol(),
5737 m_selectingBottomRight
.GetRow(),
5738 m_selectingBottomRight
.GetCol(),
5739 event
.ControlDown(),
5745 m_selectingTopLeft
= wxGridNoCellCoords
;
5746 m_selectingBottomRight
= wxGridNoCellCoords
;
5748 // Show the edit control, if it has been hidden for
5750 ShowCellEditControl();
5753 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5755 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5756 DoEndDragResizeRow();
5758 // Note: we are ending the event *after* doing
5759 // default processing in this case
5761 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5763 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5765 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5766 DoEndDragResizeCol();
5768 // Note: we are ending the event *after* doing
5769 // default processing in this case
5771 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5778 // ------------ Right button down
5780 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5782 DisableCellEditControl();
5783 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5788 // no default action at the moment
5793 // ------------ Right double click
5795 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5797 DisableCellEditControl();
5798 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5803 // no default action at the moment
5807 // ------------ Moving and no button action
5809 else if ( event
.Moving() && !event
.IsButton() )
5811 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5813 // out of grid cell area
5814 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5818 int dragRow
= YToEdgeOfRow( y
);
5819 int dragCol
= XToEdgeOfCol( x
);
5821 // Dragging on the corner of a cell to resize in both
5822 // directions is not implemented yet...
5824 if ( dragRow
>= 0 && dragCol
>= 0 )
5826 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5832 m_dragRowOrCol
= dragRow
;
5834 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5836 if ( CanDragRowSize() && CanDragGridSize() )
5837 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5840 else if ( dragCol
>= 0 )
5842 m_dragRowOrCol
= dragCol
;
5844 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5846 if ( CanDragColSize() && CanDragGridSize() )
5847 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5850 else // Neither on a row or col edge
5852 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5854 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5861 void wxGrid::DoEndDragResizeRow()
5863 if ( m_dragLastPos
>= 0 )
5865 // erase the last line and resize the row
5867 int cw
, ch
, left
, dummy
;
5868 m_gridWin
->GetClientSize( &cw
, &ch
);
5869 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5871 wxClientDC
dc( m_gridWin
);
5873 dc
.SetLogicalFunction( wxINVERT
);
5874 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5875 HideCellEditControl();
5876 SaveEditControlValue();
5878 int rowTop
= GetRowTop(m_dragRowOrCol
);
5879 SetRowSize( m_dragRowOrCol
,
5880 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5882 if ( !GetBatchCount() )
5884 // Only needed to get the correct rect.y:
5885 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5887 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5888 rect
.width
= m_rowLabelWidth
;
5889 rect
.height
= ch
- rect
.y
;
5890 m_rowLabelWin
->Refresh( true, &rect
);
5892 // if there is a multicell block, paint all of it
5895 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5896 int leftCol
= XToCol(left
);
5897 int rightCol
= internalXToCol(left
+cw
);
5900 for (i
=leftCol
; i
<rightCol
; i
++)
5902 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5903 if (cell_rows
< subtract_rows
)
5904 subtract_rows
= cell_rows
;
5906 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5907 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5908 rect
.height
= ch
- rect
.y
;
5911 m_gridWin
->Refresh( false, &rect
);
5914 ShowCellEditControl();
5919 void wxGrid::DoEndDragResizeCol()
5921 if ( m_dragLastPos
>= 0 )
5923 // erase the last line and resize the col
5925 int cw
, ch
, dummy
, top
;
5926 m_gridWin
->GetClientSize( &cw
, &ch
);
5927 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5929 wxClientDC
dc( m_gridWin
);
5931 dc
.SetLogicalFunction( wxINVERT
);
5932 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5933 HideCellEditControl();
5934 SaveEditControlValue();
5936 int colLeft
= GetColLeft(m_dragRowOrCol
);
5937 SetColSize( m_dragRowOrCol
,
5938 wxMax( m_dragLastPos
- colLeft
,
5939 GetColMinimalWidth(m_dragRowOrCol
) ) );
5941 if ( !GetBatchCount() )
5943 // Only needed to get the correct rect.x:
5944 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5946 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5947 rect
.width
= cw
- rect
.x
;
5948 rect
.height
= m_colLabelHeight
;
5949 m_colLabelWin
->Refresh( true, &rect
);
5951 // if there is a multicell block, paint all of it
5954 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5955 int topRow
= YToRow(top
);
5956 int bottomRow
= internalYToRow(top
+cw
);
5959 for (i
=topRow
; i
<bottomRow
; i
++)
5961 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5962 if (cell_cols
< subtract_cols
)
5963 subtract_cols
= cell_cols
;
5965 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5966 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5967 rect
.width
= cw
- rect
.x
;
5970 m_gridWin
->Refresh( false, &rect
);
5973 ShowCellEditControl();
5980 // ------ interaction with data model
5982 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5984 switch ( msg
.GetId() )
5986 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5987 return GetModelValues();
5989 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5990 return SetModelValues();
5992 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5993 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5994 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5995 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5996 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5997 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5998 return Redimension( msg
);
6007 // The behaviour of this function depends on the grid table class
6008 // Clear() function. For the default wxGridStringTable class the
6009 // behavious is to replace all cell contents with wxEmptyString but
6010 // not to change the number of rows or cols.
6012 void wxGrid::ClearGrid()
6016 if (IsCellEditControlEnabled())
6017 DisableCellEditControl();
6020 if ( !GetBatchCount() ) m_gridWin
->Refresh();
6025 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6027 // TODO: something with updateLabels flag
6031 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6037 if (IsCellEditControlEnabled())
6038 DisableCellEditControl();
6040 bool done
= m_table
->InsertRows( pos
, numRows
);
6043 // the table will have sent the results of the insert row
6044 // operation to this view object as a grid table message
6050 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6052 // TODO: something with updateLabels flag
6056 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6062 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6064 // the table will have sent the results of the append row
6065 // operation to this view object as a grid table message
6071 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6073 // TODO: something with updateLabels flag
6077 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6083 if (IsCellEditControlEnabled())
6084 DisableCellEditControl();
6086 bool done
= m_table
->DeleteRows( pos
, numRows
);
6088 // the table will have sent the results of the delete row
6089 // operation to this view object as a grid table message
6095 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6097 // TODO: something with updateLabels flag
6101 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6107 if (IsCellEditControlEnabled())
6108 DisableCellEditControl();
6110 bool done
= m_table
->InsertCols( pos
, numCols
);
6112 // the table will have sent the results of the insert col
6113 // operation to this view object as a grid table message
6119 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6121 // TODO: something with updateLabels flag
6125 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6131 bool done
= m_table
->AppendCols( numCols
);
6133 // the table will have sent the results of the append col
6134 // operation to this view object as a grid table message
6140 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6142 // TODO: something with updateLabels flag
6146 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6152 if (IsCellEditControlEnabled())
6153 DisableCellEditControl();
6155 bool done
= m_table
->DeleteCols( pos
, numCols
);
6157 // the table will have sent the results of the delete col
6158 // operation to this view object as a grid table message
6166 // ----- event handlers
6169 // Generate a grid event based on a mouse event and
6170 // return the result of ProcessEvent()
6172 int wxGrid::SendEvent( const wxEventType type
,
6174 wxMouseEvent
& mouseEv
)
6179 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6181 int rowOrCol
= (row
== -1 ? col
: row
);
6183 wxGridSizeEvent
gridEvt( GetId(),
6187 mouseEv
.GetX() + GetRowLabelSize(),
6188 mouseEv
.GetY() + GetColLabelSize(),
6189 mouseEv
.ControlDown(),
6190 mouseEv
.ShiftDown(),
6192 mouseEv
.MetaDown() );
6194 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6195 vetoed
= !gridEvt
.IsAllowed();
6197 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6199 // Right now, it should _never_ end up here!
6200 wxGridRangeSelectEvent
gridEvt( GetId(),
6204 m_selectingBottomRight
,
6206 mouseEv
.ControlDown(),
6207 mouseEv
.ShiftDown(),
6209 mouseEv
.MetaDown() );
6211 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6212 vetoed
= !gridEvt
.IsAllowed();
6216 wxGridEvent
gridEvt( GetId(),
6220 mouseEv
.GetX() + GetRowLabelSize(),
6221 mouseEv
.GetY() + GetColLabelSize(),
6223 mouseEv
.ControlDown(),
6224 mouseEv
.ShiftDown(),
6226 mouseEv
.MetaDown() );
6227 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6228 vetoed
= !gridEvt
.IsAllowed();
6231 // A Veto'd event may not be `claimed' so test this first
6235 return claimed
? 1 : 0;
6239 // Generate a grid event of specified type and return the result
6240 // of ProcessEvent().
6242 int wxGrid::SendEvent( const wxEventType type
,
6248 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6250 int rowOrCol
= (row
== -1 ? col
: row
);
6252 wxGridSizeEvent
gridEvt( GetId(),
6257 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6258 vetoed
= !gridEvt
.IsAllowed();
6262 wxGridEvent
gridEvt( GetId(),
6267 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6268 vetoed
= !gridEvt
.IsAllowed();
6271 // A Veto'd event may not be `claimed' so test this first
6275 return claimed
? 1 : 0;
6278 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6280 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6283 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6285 // Don't do anything if between Begin/EndBatch...
6286 // EndBatch() will do all this on the last nested one anyway.
6287 if (! GetBatchCount())
6289 // Refresh to get correct scrolled position:
6290 wxScrolledWindow::Refresh(eraseb
, rect
);
6294 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6295 int width_label
, width_cell
, height_label
, height_cell
;
6298 //Copy rectangle can get scroll offsets..
6299 rect_x
= rect
->GetX();
6300 rect_y
= rect
->GetY();
6301 rectWidth
= rect
->GetWidth();
6302 rectHeight
= rect
->GetHeight();
6304 width_label
= m_rowLabelWidth
- rect_x
;
6305 if (width_label
> rectWidth
)
6306 width_label
= rectWidth
;
6308 height_label
= m_colLabelHeight
- rect_y
;
6309 if (height_label
> rectHeight
) height_label
= rectHeight
;
6311 if (rect_x
> m_rowLabelWidth
)
6313 x
= rect_x
- m_rowLabelWidth
;
6314 width_cell
= rectWidth
;
6319 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6322 if (rect_y
> m_colLabelHeight
)
6324 y
= rect_y
- m_colLabelHeight
;
6325 height_cell
= rectHeight
;
6330 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6333 // Paint corner label part intersecting rect.
6334 if ( width_label
> 0 && height_label
> 0 )
6336 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6337 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6340 // Paint col labels part intersecting rect.
6341 if ( width_cell
> 0 && height_label
> 0 )
6343 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6344 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6347 // Paint row labels part intersecting rect.
6348 if ( width_label
> 0 && height_cell
> 0 )
6350 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6351 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6354 // Paint cell area part intersecting rect.
6355 if ( width_cell
> 0 && height_cell
> 0 )
6357 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6358 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6363 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6364 m_colLabelWin
->Refresh(eraseb
, NULL
);
6365 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6366 m_gridWin
->Refresh(eraseb
, NULL
);
6371 void wxGrid::OnSize( wxSizeEvent
& event
)
6373 // position the child windows
6376 // don't call CalcDimensions() from here, the base class handles the size
6382 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6384 if ( m_inOnKeyDown
)
6386 // shouldn't be here - we are going round in circles...
6388 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6391 m_inOnKeyDown
= true;
6393 // propagate the event up and see if it gets processed
6395 wxWindow
*parent
= GetParent();
6396 wxKeyEvent
keyEvt( event
);
6397 keyEvt
.SetEventObject( parent
);
6399 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6402 // try local handlers
6404 switch ( event
.GetKeyCode() )
6407 if ( event
.ControlDown() )
6409 MoveCursorUpBlock( event
.ShiftDown() );
6413 MoveCursorUp( event
.ShiftDown() );
6418 if ( event
.ControlDown() )
6420 MoveCursorDownBlock( event
.ShiftDown() );
6424 MoveCursorDown( event
.ShiftDown() );
6429 if ( event
.ControlDown() )
6431 MoveCursorLeftBlock( event
.ShiftDown() );
6435 MoveCursorLeft( event
.ShiftDown() );
6440 if ( event
.ControlDown() )
6442 MoveCursorRightBlock( event
.ShiftDown() );
6446 MoveCursorRight( event
.ShiftDown() );
6451 case WXK_NUMPAD_ENTER
:
6452 if ( event
.ControlDown() )
6454 event
.Skip(); // to let the edit control have the return
6458 if ( GetGridCursorRow() < GetNumberRows()-1 )
6460 MoveCursorDown( event
.ShiftDown() );
6464 // at the bottom of a column
6465 DisableCellEditControl();
6475 if (event
.ShiftDown())
6477 if ( GetGridCursorCol() > 0 )
6479 MoveCursorLeft( false );
6484 DisableCellEditControl();
6489 if ( GetGridCursorCol() < GetNumberCols()-1 )
6491 MoveCursorRight( false );
6496 DisableCellEditControl();
6502 if ( event
.ControlDown() )
6504 MakeCellVisible( 0, 0 );
6505 SetCurrentCell( 0, 0 );
6514 if ( event
.ControlDown() )
6516 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6517 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6534 if ( event
.ControlDown() )
6538 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6539 m_currentCellCoords
.GetCol(),
6540 event
.ControlDown(),
6547 if ( !IsEditable() )
6549 MoveCursorRight( false );
6552 // Otherwise fall through to default
6560 m_inOnKeyDown
= false;
6563 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6565 // try local handlers
6567 if ( event
.GetKeyCode() == WXK_SHIFT
)
6569 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6570 m_selectingBottomRight
!= wxGridNoCellCoords
)
6574 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6575 m_selectingTopLeft
.GetCol(),
6576 m_selectingBottomRight
.GetRow(),
6577 m_selectingBottomRight
.GetCol(),
6578 event
.ControlDown(),
6585 m_selectingTopLeft
= wxGridNoCellCoords
;
6586 m_selectingBottomRight
= wxGridNoCellCoords
;
6587 m_selectingKeyboard
= wxGridNoCellCoords
;
6591 void wxGrid::OnChar( wxKeyEvent
& event
)
6593 // is it possible to edit the current cell at all?
6594 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6596 // yes, now check whether the cells editor accepts the key
6597 int row
= m_currentCellCoords
.GetRow();
6598 int col
= m_currentCellCoords
.GetCol();
6599 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6600 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6602 // <F2> is special and will always start editing, for
6603 // other keys - ask the editor itself
6604 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6605 || editor
->IsAcceptedKey(event
) )
6607 // ensure cell is visble
6608 MakeCellVisible(row
, col
);
6609 EnableCellEditControl();
6611 // a problem can arise if the cell is not completely
6612 // visible (even after calling MakeCellVisible the
6613 // control is not created and calling StartingKey will
6615 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6616 editor
->StartingKey(event
);
6633 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6637 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6639 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6641 // the event has been intercepted - do nothing
6645 wxClientDC
dc(m_gridWin
);
6648 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6650 DisableCellEditControl();
6652 if ( IsVisible( m_currentCellCoords
, false ) )
6655 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6656 if ( !m_gridLinesEnabled
)
6664 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6666 // Otherwise refresh redraws the highlight!
6667 m_currentCellCoords
= coords
;
6669 DrawGridCellArea(dc
,cells
);
6670 DrawAllGridLines( dc
, r
);
6674 m_currentCellCoords
= coords
;
6676 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6677 DrawCellHighlight(dc
, attr
);
6682 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6685 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6689 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6692 rightCol
= GetNumberCols() - 1;
6694 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6697 bottomRow
= GetNumberRows() - 1;
6701 if ( topRow
> bottomRow
)
6708 if ( leftCol
> rightCol
)
6715 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6716 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6718 // First the case that we selected a completely new area
6719 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6720 m_selectingBottomRight
== wxGridNoCellCoords
)
6723 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6724 wxGridCellCoords ( bottomRow
, rightCol
) );
6725 m_gridWin
->Refresh( false, &rect
);
6727 // Now handle changing an existing selection area.
6728 else if ( m_selectingTopLeft
!= updateTopLeft
||
6729 m_selectingBottomRight
!= updateBottomRight
)
6731 // Compute two optimal update rectangles:
6732 // Either one rectangle is a real subset of the
6733 // other, or they are (almost) disjoint!
6735 bool need_refresh
[4];
6739 need_refresh
[3] = false;
6742 // Store intermediate values
6743 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6744 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6745 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6746 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6748 // Determine the outer/inner coordinates.
6749 if (oldLeft
> leftCol
)
6755 if (oldTop
> topRow
)
6761 if (oldRight
< rightCol
)
6764 oldRight
= rightCol
;
6767 if (oldBottom
< bottomRow
)
6770 oldBottom
= bottomRow
;
6774 // Now, either the stuff marked old is the outer
6775 // rectangle or we don't have a situation where one
6776 // is contained in the other.
6778 if ( oldLeft
< leftCol
)
6780 // Refresh the newly selected or deselected
6781 // area to the left of the old or new selection.
6782 need_refresh
[0] = true;
6783 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6785 wxGridCellCoords ( oldBottom
,
6789 if ( oldTop
< topRow
)
6791 // Refresh the newly selected or deselected
6792 // area above the old or new selection.
6793 need_refresh
[1] = true;
6794 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6796 wxGridCellCoords ( topRow
- 1,
6800 if ( oldRight
> rightCol
)
6802 // Refresh the newly selected or deselected
6803 // area to the right of the old or new selection.
6804 need_refresh
[2] = true;
6805 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6807 wxGridCellCoords ( oldBottom
,
6811 if ( oldBottom
> bottomRow
)
6813 // Refresh the newly selected or deselected
6814 // area below the old or new selection.
6815 need_refresh
[3] = true;
6816 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6818 wxGridCellCoords ( oldBottom
,
6822 // various Refresh() calls
6823 for (i
= 0; i
< 4; i
++ )
6824 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6825 m_gridWin
->Refresh( false, &(rect
[i
]) );
6828 m_selectingTopLeft
= updateTopLeft
;
6829 m_selectingBottomRight
= updateBottomRight
;
6833 // ------ functions to get/send data (see also public functions)
6836 bool wxGrid::GetModelValues()
6838 // Hide the editor, so it won't hide a changed value.
6839 HideCellEditControl();
6843 // all we need to do is repaint the grid
6845 m_gridWin
->Refresh();
6853 bool wxGrid::SetModelValues()
6857 // Disable the editor, so it won't hide a changed value.
6858 // Do we also want to save the current value of the editor first?
6860 DisableCellEditControl();
6864 for ( row
= 0; row
< m_numRows
; row
++ )
6866 for ( col
= 0; col
< m_numCols
; col
++ )
6868 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6880 // Note - this function only draws cells that are in the list of
6881 // exposed cells (usually set from the update region by
6882 // CalcExposedCells)
6884 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6886 if ( !m_numRows
|| !m_numCols
)
6889 int i
, numCells
= cells
.GetCount();
6890 int row
, col
, cell_rows
, cell_cols
;
6891 wxGridCellCoordsArray redrawCells
;
6893 for ( i
= numCells
-1; i
>= 0; i
-- )
6895 row
= cells
[i
].GetRow();
6896 col
= cells
[i
].GetCol();
6897 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6899 // If this cell is part of a multicell block, find owner for repaint
6900 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6902 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6903 bool marked
= false;
6904 for ( int j
= 0; j
< numCells
; j
++ )
6906 if ( cell
== cells
[j
] )
6914 int count
= redrawCells
.GetCount();
6915 for (int j
= 0; j
< count
; j
++)
6917 if ( cell
== redrawCells
[j
] )
6924 redrawCells
.Add( cell
);
6926 continue; // don't bother drawing this cell
6929 // If this cell is empty, find cell to left that might want to overflow
6930 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6932 for ( int l
= 0; l
< cell_rows
; l
++ )
6934 // find a cell in this row to left alreay marked for repaint
6936 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6937 if ((redrawCells
[k
].GetCol() < left
) &&
6938 (redrawCells
[k
].GetRow() == row
))
6939 left
= redrawCells
[k
].GetCol();
6942 left
= 0; // oh well
6944 for (int j
= col
-1; j
>= left
; j
--)
6946 if (!m_table
->IsEmptyCell(row
+l
, j
))
6948 if (GetCellOverflow(row
+l
, j
))
6950 wxGridCellCoords
cell(row
+l
, j
);
6951 bool marked
= false;
6953 for (int k
= 0; k
< numCells
; k
++)
6955 if ( cell
== cells
[k
] )
6964 int count
= redrawCells
.GetCount();
6965 for (int k
= 0; k
< count
; k
++)
6967 if ( cell
== redrawCells
[k
] )
6974 redrawCells
.Add( cell
);
6983 DrawCell( dc
, cells
[i
] );
6986 numCells
= redrawCells
.GetCount();
6988 for ( i
= numCells
- 1; i
>= 0; i
-- )
6990 DrawCell( dc
, redrawCells
[i
] );
6995 void wxGrid::DrawGridSpace( wxDC
& dc
)
6998 m_gridWin
->GetClientSize( &cw
, &ch
);
7001 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7003 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
7004 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
7006 if ( right
> rightCol
|| bottom
> bottomRow
)
7009 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7011 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7012 dc
.SetPen( *wxTRANSPARENT_PEN
);
7014 if ( right
> rightCol
)
7016 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7019 if ( bottom
> bottomRow
)
7021 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7027 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7029 int row
= coords
.GetRow();
7030 int col
= coords
.GetCol();
7032 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7035 // we draw the cell border ourselves
7036 #if !WXGRID_DRAW_LINES
7037 if ( m_gridLinesEnabled
)
7038 DrawCellBorder( dc
, coords
);
7041 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7043 bool isCurrent
= coords
== m_currentCellCoords
;
7045 wxRect rect
= CellToRect( row
, col
);
7047 // if the editor is shown, we should use it and not the renderer
7048 // Note: However, only if it is really _shown_, i.e. not hidden!
7049 if ( isCurrent
&& IsCellEditControlShown() )
7051 // OSAF NB: this "#if..." is temporary and fixes a problem where the
7052 // edit control is erased by this code after being rendered.
7053 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7054 // implicitly, causing this out-of order render.
7055 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7056 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7057 editor
->PaintBackground(rect
, attr
);
7063 // but all the rest is drawn by the cell renderer and hence may be
7065 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7066 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7073 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7075 int row
= m_currentCellCoords
.GetRow();
7076 int col
= m_currentCellCoords
.GetCol();
7078 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7081 wxRect rect
= CellToRect(row
, col
);
7083 // hmmm... what could we do here to show that the cell is disabled?
7084 // for now, I just draw a thinner border than for the other ones, but
7085 // it doesn't look really good
7087 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7091 // The center of th drawn line is where the position/width/height of
7092 // the rectangle is actually at, (on wxMSW atr least,) so we will
7093 // reduce the size of the rectangle to compensate for the thickness of
7094 // the line. If this is too strange on non wxMSW platforms then
7095 // please #ifdef this appropriately.
7096 rect
.x
+= penWidth
/ 2;
7097 rect
.y
+= penWidth
/ 2;
7098 rect
.width
-= penWidth
- 1;
7099 rect
.height
-= penWidth
- 1;
7102 // Now draw the rectangle
7103 // use the cellHighlightColour if the cell is inside a selection, this
7104 // will ensure the cell is always visible.
7105 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7106 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7107 dc
.DrawRectangle(rect
);
7111 // VZ: my experiments with 3d borders...
7113 // how to properly set colours for arbitrary bg?
7114 wxCoord x1
= rect
.x
,
7116 x2
= rect
.x
+ rect
.width
- 1,
7117 y2
= rect
.y
+ rect
.height
- 1;
7119 dc
.SetPen(*wxWHITE_PEN
);
7120 dc
.DrawLine(x1
, y1
, x2
, y1
);
7121 dc
.DrawLine(x1
, y1
, x1
, y2
);
7123 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7124 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7126 dc
.SetPen(*wxBLACK_PEN
);
7127 dc
.DrawLine(x1
, y2
, x2
, y2
);
7128 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7133 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7135 int row
= coords
.GetRow();
7136 int col
= coords
.GetCol();
7137 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7140 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7142 wxRect rect
= CellToRect( row
, col
);
7144 // right hand border
7146 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7147 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7151 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7152 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7155 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7157 // This if block was previously in wxGrid::OnPaint but that doesn't
7158 // seem to get called under wxGTK - MB
7160 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7161 m_numRows
&& m_numCols
)
7163 m_currentCellCoords
.Set(0, 0);
7166 if ( IsCellEditControlShown() )
7168 // don't show highlight when the edit control is shown
7172 // if the active cell was repainted, repaint its highlight too because it
7173 // might have been damaged by the grid lines
7174 size_t count
= cells
.GetCount();
7175 for ( size_t n
= 0; n
< count
; n
++ )
7177 if ( cells
[n
] == m_currentCellCoords
)
7179 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7180 DrawCellHighlight(dc
, attr
);
7188 // TODO: remove this ???
7189 // This is used to redraw all grid lines e.g. when the grid line colour
7192 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7194 #if !WXGRID_DRAW_LINES
7198 if ( !m_gridLinesEnabled
||
7203 int top
, bottom
, left
, right
;
7205 #if 0 //#ifndef __WXGTK__
7209 m_gridWin
->GetClientSize(&cw
, &ch
);
7211 // virtual coords of visible area
7213 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7214 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7219 reg
.GetBox(x
, y
, w
, h
);
7220 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7221 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7225 m_gridWin
->GetClientSize(&cw
, &ch
);
7226 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7227 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7230 // avoid drawing grid lines past the last row and col
7232 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7233 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7235 // no gridlines inside multicells, clip them out
7236 int leftCol
= internalXToCol(left
);
7237 int topRow
= internalYToRow(top
);
7238 int rightCol
= internalXToCol(right
);
7239 int bottomRow
= internalYToRow(bottom
);
7242 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7243 wxRegion
clippedcells(0, 0, cw
, ch
);
7245 int i
, j
, cell_rows
, cell_cols
;
7248 for (j
=topRow
; j
<bottomRow
; j
++)
7250 for (i
=leftCol
; i
<rightCol
; i
++)
7252 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7253 if ((cell_rows
> 1) || (cell_cols
> 1))
7255 rect
= CellToRect(j
,i
);
7256 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7257 clippedcells
.Subtract(rect
);
7259 else if ((cell_rows
< 0) || (cell_cols
< 0))
7261 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7262 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7263 clippedcells
.Subtract(rect
);
7268 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7270 int i
, j
, cell_rows
, cell_cols
;
7273 for (j
=topRow
; j
<bottomRow
; j
++)
7275 for (i
=leftCol
; i
<rightCol
; i
++)
7277 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7278 if ((cell_rows
> 1) || (cell_cols
> 1))
7280 rect
= CellToRect(j
,i
);
7281 clippedcells
.Subtract(rect
);
7283 else if ((cell_rows
< 0) || (cell_cols
< 0))
7285 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7286 clippedcells
.Subtract(rect
);
7291 dc
.SetClippingRegion( clippedcells
);
7293 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7295 // horizontal grid lines
7297 // already declared above - int i;
7298 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7300 int bot
= GetRowBottom(i
) - 1;
7309 dc
.DrawLine( left
, bot
, right
, bot
);
7313 // vertical grid lines
7315 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7317 int colRight
= GetColRight(i
) - 1;
7318 if ( colRight
> right
)
7323 if ( colRight
>= left
)
7325 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7328 dc
.DestroyClippingRegion();
7331 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7337 size_t numLabels
= rows
.GetCount();
7339 for ( i
= 0; i
< numLabels
; i
++ )
7341 DrawRowLabel( dc
, rows
[i
] );
7345 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7347 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7353 rect
.SetY( GetRowTop(row
) + 1 );
7354 rect
.SetWidth( m_rowLabelWidth
- 2 );
7355 rect
.SetHeight( GetRowHeight(row
) - 2 );
7357 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7359 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7361 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7363 int rowTop
= GetRowTop(row
),
7364 rowBottom
= GetRowBottom(row
) - 1;
7366 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7367 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7368 m_rowLabelWidth
-1, rowBottom
);
7370 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7372 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7374 dc
.SetPen( *wxWHITE_PEN
);
7375 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7376 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7378 dc
.SetBackgroundMode( wxTRANSPARENT
);
7379 dc
.SetTextForeground( GetLabelTextColour() );
7380 dc
.SetFont( GetLabelFont() );
7383 GetRowLabelAlignment( &hAlign
, &vAlign
);
7386 rect
.SetY( GetRowTop(row
) + 2 );
7387 rect
.SetWidth( m_rowLabelWidth
- 4 );
7388 rect
.SetHeight( GetRowHeight(row
) - 4 );
7389 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7392 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7398 size_t numLabels
= cols
.GetCount();
7400 for ( i
= 0; i
< numLabels
; i
++ )
7402 DrawColLabel( dc
, cols
[i
] );
7406 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7408 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7411 int colLeft
= GetColLeft(col
);
7415 rect
.SetX( colLeft
+ 1 );
7417 rect
.SetWidth( GetColWidth(col
) - 2 );
7418 rect
.SetHeight( m_colLabelHeight
- 2 );
7420 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7422 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7424 int colRight
= GetColRight(col
) - 1;
7426 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7427 dc
.DrawLine( colRight
, 0,
7428 colRight
, m_colLabelHeight
-1 );
7430 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7432 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7433 colRight
+1, m_colLabelHeight
-1 );
7435 dc
.SetPen( *wxWHITE_PEN
);
7436 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7437 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7439 dc
.SetBackgroundMode( wxTRANSPARENT
);
7440 dc
.SetTextForeground( GetLabelTextColour() );
7441 dc
.SetFont( GetLabelFont() );
7443 int hAlign
, vAlign
, orient
;
7444 GetColLabelAlignment( &hAlign
, &vAlign
);
7445 orient
= GetColLabelTextOrientation();
7447 rect
.SetX( colLeft
+ 2 );
7449 rect
.SetWidth( GetColWidth(col
) - 4 );
7450 rect
.SetHeight( m_colLabelHeight
- 4 );
7451 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7454 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7455 const wxString
& value
,
7459 int textOrientation
)
7461 wxArrayString lines
;
7463 StringToLines( value
, lines
);
7465 //Forward to new API.
7466 DrawTextRectangle( dc
,
7475 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7476 const wxArrayString
& lines
,
7480 int textOrientation
)
7482 long textWidth
= 0, textHeight
= 0;
7483 long lineWidth
= 0, lineHeight
= 0;
7486 dc
.SetClippingRegion( rect
);
7488 nLines
= lines
.GetCount();
7492 float x
= 0.0, y
= 0.0;
7494 if ( textOrientation
== wxHORIZONTAL
)
7495 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7497 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7499 switch ( vertAlign
)
7501 case wxALIGN_BOTTOM
:
7502 if ( textOrientation
== wxHORIZONTAL
)
7503 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7505 x
= rect
.x
+ rect
.width
- textWidth
;
7508 case wxALIGN_CENTRE
:
7509 if ( textOrientation
== wxHORIZONTAL
)
7510 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7512 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7517 if ( textOrientation
== wxHORIZONTAL
)
7524 // Align each line of a multi-line label
7525 for ( l
= 0; l
< nLines
; l
++ )
7527 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7529 switch ( horizAlign
)
7532 if ( textOrientation
== wxHORIZONTAL
)
7533 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7535 y
= rect
.y
+ lineWidth
+ 1;
7538 case wxALIGN_CENTRE
:
7539 if ( textOrientation
== wxHORIZONTAL
)
7540 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7542 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7547 if ( textOrientation
== wxHORIZONTAL
)
7550 y
= rect
.y
+ rect
.height
- 1;
7554 if ( textOrientation
== wxHORIZONTAL
)
7556 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7561 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7567 dc
.DestroyClippingRegion();
7571 // Split multi line text up into an array of strings. Any existing
7572 // contents of the string array are preserved.
7574 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7578 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7579 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7581 while ( startPos
< (int)tVal
.Length() )
7583 pos
= tVal
.Mid(startPos
).Find( eol
);
7588 else if ( pos
== 0 )
7590 lines
.Add( wxEmptyString
);
7594 lines
.Add( value
.Mid(startPos
, pos
) );
7596 startPos
+= pos
+ 1;
7599 if ( startPos
< (int)value
.Length() )
7601 lines
.Add( value
.Mid( startPos
) );
7606 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
7607 const wxArrayString
& lines
,
7608 long *width
, long *height
)
7612 long lineW
= 0, lineH
= 0;
7615 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7617 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7618 w
= wxMax( w
, lineW
);
7627 // ------ Batch processing.
7629 void wxGrid::EndBatch()
7631 if ( m_batchCount
> 0 )
7634 if ( !m_batchCount
)
7637 m_rowLabelWin
->Refresh();
7638 m_colLabelWin
->Refresh();
7639 m_cornerLabelWin
->Refresh();
7640 m_gridWin
->Refresh();
7645 // Use this, rather than wxWindow::Refresh(), to force an immediate
7646 // repainting of the grid. Has no effect if you are already inside a
7647 // BeginBatch / EndBatch block.
7649 void wxGrid::ForceRefresh()
7655 bool wxGrid::Enable(bool enable
)
7657 if ( !wxScrolledWindow::Enable(enable
) )
7660 // redraw in the new state
7661 m_gridWin
->Refresh();
7668 // ------ Edit control functions
7671 void wxGrid::EnableEditing( bool edit
)
7673 // TODO: improve this ?
7675 if ( edit
!= m_editable
)
7678 EnableCellEditControl(edit
);
7683 void wxGrid::EnableCellEditControl( bool enable
)
7688 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7689 SetCurrentCell( 0, 0 );
7691 if ( enable
!= m_cellEditCtrlEnabled
)
7695 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7698 // this should be checked by the caller!
7699 wxASSERT_MSG( CanEnableCellControl(),
7700 _T("can't enable editing for this cell!") );
7702 // do it before ShowCellEditControl()
7703 m_cellEditCtrlEnabled
= enable
;
7705 ShowCellEditControl();
7709 //FIXME:add veto support
7710 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7712 HideCellEditControl();
7713 SaveEditControlValue();
7715 // do it after HideCellEditControl()
7716 m_cellEditCtrlEnabled
= enable
;
7721 bool wxGrid::IsCurrentCellReadOnly() const
7724 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7725 bool readonly
= attr
->IsReadOnly();
7731 bool wxGrid::CanEnableCellControl() const
7733 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7734 !IsCurrentCellReadOnly();
7738 bool wxGrid::IsCellEditControlEnabled() const
7740 // the cell edit control might be disable for all cells or just for the
7741 // current one if it's read only
7742 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7745 bool wxGrid::IsCellEditControlShown() const
7747 bool isShown
= false;
7749 if ( m_cellEditCtrlEnabled
)
7751 int row
= m_currentCellCoords
.GetRow();
7752 int col
= m_currentCellCoords
.GetCol();
7753 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7754 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7759 if ( editor
->IsCreated() )
7761 isShown
= editor
->GetControl()->IsShown();
7771 void wxGrid::ShowCellEditControl()
7773 if ( IsCellEditControlEnabled() )
7775 if ( !IsVisible( m_currentCellCoords
) )
7777 m_cellEditCtrlEnabled
= false;
7782 wxRect rect
= CellToRect( m_currentCellCoords
);
7783 int row
= m_currentCellCoords
.GetRow();
7784 int col
= m_currentCellCoords
.GetCol();
7786 // if this is part of a multicell, find owner (topleft)
7787 int cell_rows
, cell_cols
;
7788 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7789 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7793 m_currentCellCoords
.SetRow( row
);
7794 m_currentCellCoords
.SetCol( col
);
7797 // convert to scrolled coords
7799 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7801 // done in PaintBackground()
7803 // erase the highlight and the cell contents because the editor
7804 // might not cover the entire cell
7805 wxClientDC
dc( m_gridWin
);
7807 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7808 dc
.SetPen(*wxTRANSPARENT_PEN
);
7809 dc
.DrawRectangle(rect
);
7812 // cell is shifted by one pixel
7813 // However, don't allow x or y to become negative
7814 // since the SetSize() method interprets that as
7821 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7822 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7823 if ( !editor
->IsCreated() )
7825 editor
->Create(m_gridWin
, wxID_ANY
,
7826 new wxGridCellEditorEvtHandler(this, editor
));
7828 wxGridEditorCreatedEvent
evt(GetId(),
7829 wxEVT_GRID_EDITOR_CREATED
,
7833 editor
->GetControl());
7834 GetEventHandler()->ProcessEvent(evt
);
7838 // resize editor to overflow into righthand cells if allowed
7839 int maxWidth
= rect
.width
;
7840 wxString value
= GetCellValue(row
, col
);
7841 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7844 GetTextExtent(value
, &maxWidth
, &y
,
7845 NULL
, NULL
, &attr
->GetFont());
7846 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7849 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7850 if (rect
.x
+maxWidth
> client_right
)
7851 maxWidth
= client_right
- rect
.x
;
7853 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7855 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7856 // may have changed earlier
7857 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7860 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7861 // looks weird going over a multicell
7862 if (m_table
->IsEmptyCell(row
,i
) &&
7863 (rect
.width
< maxWidth
) && (c_rows
== 1))
7864 rect
.width
+= GetColWidth(i
);
7869 if (rect
.GetRight() > client_right
)
7870 rect
.SetRight(client_right
- 1);
7873 editor
->SetCellAttr(attr
);
7874 editor
->SetSize( rect
);
7875 editor
->Show( true, attr
);
7877 // recalc dimensions in case we need to
7878 // expand the scrolled window to account for editor
7881 editor
->BeginEdit(row
, col
, this);
7882 editor
->SetCellAttr(NULL
);
7890 void wxGrid::HideCellEditControl()
7892 if ( IsCellEditControlEnabled() )
7894 int row
= m_currentCellCoords
.GetRow();
7895 int col
= m_currentCellCoords
.GetCol();
7897 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7898 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7899 editor
->Show( false );
7903 m_gridWin
->SetFocus();
7905 // refresh whole row to the right
7906 wxRect
rect( CellToRect(row
, col
) );
7907 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7908 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7910 // ensure that the pixels under the focus ring get refreshed as well
7911 rect
.Inflate(10, 10);
7913 m_gridWin
->Refresh( false, &rect
);
7917 void wxGrid::SaveEditControlValue()
7919 if ( IsCellEditControlEnabled() )
7921 int row
= m_currentCellCoords
.GetRow();
7922 int col
= m_currentCellCoords
.GetCol();
7924 wxString oldval
= GetCellValue(row
, col
);
7926 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7927 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7928 bool changed
= editor
->EndEdit(row
, col
, this);
7935 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7936 m_currentCellCoords
.GetRow(),
7937 m_currentCellCoords
.GetCol() ) < 0 )
7939 // Event has been vetoed, set the data back.
7940 SetCellValue(row
, col
, oldval
);
7948 // ------ Grid location functions
7949 // Note that all of these functions work with the logical coordinates of
7950 // grid cells and labels so you will need to convert from device
7951 // coordinates for mouse events etc.
7954 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7956 int row
= YToRow(y
);
7957 int col
= XToCol(x
);
7959 if ( row
== -1 || col
== -1 )
7961 coords
= wxGridNoCellCoords
;
7965 coords
.Set( row
, col
);
7970 // Internal Helper function for computing row or column from some
7971 // (unscrolled) coordinate value, using either
7972 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7973 // of m_rowBottoms/m_ColRights to speed up the search!
7975 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7976 const wxArrayInt
& BorderArray
, int nMax
,
7980 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7985 size_t i_max
= coord
/ defaultDist
,
7988 if (BorderArray
.IsEmpty())
7990 if ((int) i_max
< nMax
)
7992 return clipToMinMax
? nMax
- 1 : -1;
7995 if ( i_max
>= BorderArray
.GetCount())
7996 i_max
= BorderArray
.GetCount() - 1;
7999 if ( coord
>= BorderArray
[i_max
])
8003 i_max
= coord
/ minDist
;
8005 i_max
= BorderArray
.GetCount() - 1;
8008 if ( i_max
>= BorderArray
.GetCount())
8009 i_max
= BorderArray
.GetCount() - 1;
8012 if ( coord
>= BorderArray
[i_max
])
8013 return clipToMinMax
? (int)i_max
: -1;
8014 if ( coord
< BorderArray
[0] )
8017 while ( i_max
- i_min
> 0 )
8019 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8020 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8021 if (coord
>= BorderArray
[ i_max
- 1])
8025 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8026 if (coord
< BorderArray
[median
])
8035 int wxGrid::YToRow( int y
)
8037 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8038 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8041 int wxGrid::XToCol( int x
)
8043 return CoordToRowOrCol(x
, m_defaultColWidth
,
8044 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8047 // return the row number that that the y coord is near the edge of, or
8048 // -1 if not near an edge
8050 int wxGrid::YToEdgeOfRow( int y
)
8053 i
= internalYToRow(y
);
8055 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8057 // We know that we are in row i, test whether we are
8058 // close enough to lower or upper border, respectively.
8059 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8061 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8068 // return the col number that that the x coord is near the edge of, or
8069 // -1 if not near an edge
8071 int wxGrid::XToEdgeOfCol( int x
)
8074 i
= internalXToCol(x
);
8076 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8078 // We know that we are in column i, test whether we are
8079 // close enough to right or left border, respectively.
8080 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8082 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8089 wxRect
wxGrid::CellToRect( int row
, int col
)
8091 wxRect
rect( -1, -1, -1, -1 );
8093 if ( row
>= 0 && row
< m_numRows
&&
8094 col
>= 0 && col
< m_numCols
)
8096 int i
, cell_rows
, cell_cols
;
8097 rect
.width
= rect
.height
= 0;
8098 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8099 // if negative then find multicell owner
8100 if (cell_rows
< 0) row
+= cell_rows
;
8101 if (cell_cols
< 0) col
+= cell_cols
;
8102 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8104 rect
.x
= GetColLeft(col
);
8105 rect
.y
= GetRowTop(row
);
8106 for (i
=col
; i
<col
+cell_cols
; i
++)
8107 rect
.width
+= GetColWidth(i
);
8108 for (i
=row
; i
<row
+cell_rows
; i
++)
8109 rect
.height
+= GetRowHeight(i
);
8112 // if grid lines are enabled, then the area of the cell is a bit smaller
8113 if (m_gridLinesEnabled
)
8122 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8124 // get the cell rectangle in logical coords
8126 wxRect
r( CellToRect( row
, col
) );
8128 // convert to device coords
8130 int left
, top
, right
, bottom
;
8131 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8132 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8134 // check against the client area of the grid window
8137 m_gridWin
->GetClientSize( &cw
, &ch
);
8139 if ( wholeCellVisible
)
8141 // is the cell wholly visible ?
8143 return ( left
>= 0 && right
<= cw
&&
8144 top
>= 0 && bottom
<= ch
);
8148 // is the cell partly visible ?
8150 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8151 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8155 // make the specified cell location visible by doing a minimal amount
8158 void wxGrid::MakeCellVisible( int row
, int col
)
8161 int xpos
= -1, ypos
= -1;
8163 if ( row
>= 0 && row
< m_numRows
&&
8164 col
>= 0 && col
< m_numCols
)
8166 // get the cell rectangle in logical coords
8168 wxRect
r( CellToRect( row
, col
) );
8170 // convert to device coords
8172 int left
, top
, right
, bottom
;
8173 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8174 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8177 m_gridWin
->GetClientSize( &cw
, &ch
);
8183 else if ( bottom
> ch
)
8185 int h
= r
.GetHeight();
8187 for ( i
= row
-1; i
>= 0; i
-- )
8189 int rowHeight
= GetRowHeight(i
);
8190 if ( h
+ rowHeight
> ch
)
8197 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8198 // have rounding errors (this is important, because if we do, we
8199 // might not scroll at all and some cells won't be redrawn)
8201 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8203 ypos
+= m_scrollLineY
;
8210 else if ( right
> cw
)
8212 // position the view so that the cell is on the right
8214 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8215 xpos
= x0
+ (right
- cw
);
8217 // see comment for ypos above
8218 xpos
+= m_scrollLineX
;
8221 if ( xpos
!= -1 || ypos
!= -1 )
8224 xpos
/= m_scrollLineX
;
8226 ypos
/= m_scrollLineY
;
8227 Scroll( xpos
, ypos
);
8235 // ------ Grid cursor movement functions
8238 bool wxGrid::MoveCursorUp( bool expandSelection
)
8240 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8241 m_currentCellCoords
.GetRow() >= 0 )
8243 if ( expandSelection
)
8245 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8246 m_selectingKeyboard
= m_currentCellCoords
;
8247 if ( m_selectingKeyboard
.GetRow() > 0 )
8249 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8250 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8251 m_selectingKeyboard
.GetCol() );
8252 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8255 else if ( m_currentCellCoords
.GetRow() > 0 )
8257 int row
= m_currentCellCoords
.GetRow() - 1;
8258 int col
= m_currentCellCoords
.GetCol();
8260 MakeCellVisible( row
, col
);
8261 SetCurrentCell( row
, col
);
8273 bool wxGrid::MoveCursorDown( bool expandSelection
)
8275 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8276 m_currentCellCoords
.GetRow() < m_numRows
)
8278 if ( expandSelection
)
8280 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8281 m_selectingKeyboard
= m_currentCellCoords
;
8282 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8284 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8285 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8286 m_selectingKeyboard
.GetCol() );
8287 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8290 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8292 int row
= m_currentCellCoords
.GetRow() + 1;
8293 int col
= m_currentCellCoords
.GetCol();
8295 MakeCellVisible( row
, col
);
8296 SetCurrentCell( row
, col
);
8307 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8309 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8310 m_currentCellCoords
.GetCol() >= 0 )
8312 if ( expandSelection
)
8314 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8315 m_selectingKeyboard
= m_currentCellCoords
;
8316 if ( m_selectingKeyboard
.GetCol() > 0 )
8318 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8319 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8320 m_selectingKeyboard
.GetCol() );
8321 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8324 else if ( m_currentCellCoords
.GetCol() > 0 )
8326 int row
= m_currentCellCoords
.GetRow();
8327 int col
= m_currentCellCoords
.GetCol() - 1;
8329 MakeCellVisible( row
, col
);
8330 SetCurrentCell( row
, col
);
8341 bool wxGrid::MoveCursorRight( bool expandSelection
)
8343 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8344 m_currentCellCoords
.GetCol() < m_numCols
)
8346 if ( expandSelection
)
8348 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8349 m_selectingKeyboard
= m_currentCellCoords
;
8350 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8352 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8353 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8354 m_selectingKeyboard
.GetCol() );
8355 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8358 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8360 int row
= m_currentCellCoords
.GetRow();
8361 int col
= m_currentCellCoords
.GetCol() + 1;
8363 MakeCellVisible( row
, col
);
8364 SetCurrentCell( row
, col
);
8375 bool wxGrid::MovePageUp()
8377 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8380 int row
= m_currentCellCoords
.GetRow();
8384 m_gridWin
->GetClientSize( &cw
, &ch
);
8386 int y
= GetRowTop(row
);
8387 int newRow
= internalYToRow( y
- ch
+ 1 );
8389 if ( newRow
== row
)
8391 //row > 0 , so newrow can never be less than 0 here.
8395 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8396 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8404 bool wxGrid::MovePageDown()
8406 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8409 int row
= m_currentCellCoords
.GetRow();
8410 if ( (row
+1) < m_numRows
)
8413 m_gridWin
->GetClientSize( &cw
, &ch
);
8415 int y
= GetRowTop(row
);
8416 int newRow
= internalYToRow( y
+ ch
);
8417 if ( newRow
== row
)
8419 // row < m_numRows , so newrow can't overflow here.
8423 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8424 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8432 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8435 m_currentCellCoords
!= wxGridNoCellCoords
&&
8436 m_currentCellCoords
.GetRow() > 0 )
8438 int row
= m_currentCellCoords
.GetRow();
8439 int col
= m_currentCellCoords
.GetCol();
8441 if ( m_table
->IsEmptyCell(row
, col
) )
8443 // starting in an empty cell: find the next block of
8449 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8453 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8455 // starting at the top of a block: find the next block
8461 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8467 // starting within a block: find the top of the block
8472 if ( m_table
->IsEmptyCell(row
, col
) )
8480 MakeCellVisible( row
, col
);
8481 if ( expandSelection
)
8483 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8484 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8489 SetCurrentCell( row
, col
);
8498 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8501 m_currentCellCoords
!= wxGridNoCellCoords
&&
8502 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8504 int row
= m_currentCellCoords
.GetRow();
8505 int col
= m_currentCellCoords
.GetCol();
8507 if ( m_table
->IsEmptyCell(row
, col
) )
8509 // starting in an empty cell: find the next block of
8512 while ( row
< m_numRows
-1 )
8515 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8519 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8521 // starting at the bottom of a block: find the next block
8524 while ( row
< m_numRows
-1 )
8527 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8533 // starting within a block: find the bottom of the block
8535 while ( row
< m_numRows
-1 )
8538 if ( m_table
->IsEmptyCell(row
, col
) )
8546 MakeCellVisible( row
, col
);
8547 if ( expandSelection
)
8549 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8550 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8555 SetCurrentCell( row
, col
);
8564 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8567 m_currentCellCoords
!= wxGridNoCellCoords
&&
8568 m_currentCellCoords
.GetCol() > 0 )
8570 int row
= m_currentCellCoords
.GetRow();
8571 int col
= m_currentCellCoords
.GetCol();
8573 if ( m_table
->IsEmptyCell(row
, col
) )
8575 // starting in an empty cell: find the next block of
8581 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8585 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8587 // starting at the left of a block: find the next block
8593 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8599 // starting within a block: find the left of the block
8604 if ( m_table
->IsEmptyCell(row
, col
) )
8612 MakeCellVisible( row
, col
);
8613 if ( expandSelection
)
8615 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8616 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8621 SetCurrentCell( row
, col
);
8630 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8633 m_currentCellCoords
!= wxGridNoCellCoords
&&
8634 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8636 int row
= m_currentCellCoords
.GetRow();
8637 int col
= m_currentCellCoords
.GetCol();
8639 if ( m_table
->IsEmptyCell(row
, col
) )
8641 // starting in an empty cell: find the next block of
8644 while ( col
< m_numCols
-1 )
8647 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8651 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8653 // starting at the right of a block: find the next block
8656 while ( col
< m_numCols
-1 )
8659 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8665 // starting within a block: find the right of the block
8667 while ( col
< m_numCols
-1 )
8670 if ( m_table
->IsEmptyCell(row
, col
) )
8678 MakeCellVisible( row
, col
);
8679 if ( expandSelection
)
8681 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8682 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8687 SetCurrentCell( row
, col
);
8699 // ------ Label values and formatting
8702 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8704 *horiz
= m_rowLabelHorizAlign
;
8705 *vert
= m_rowLabelVertAlign
;
8708 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8710 *horiz
= m_colLabelHorizAlign
;
8711 *vert
= m_colLabelVertAlign
;
8714 int wxGrid::GetColLabelTextOrientation()
8716 return m_colLabelTextOrientation
;
8719 wxString
wxGrid::GetRowLabelValue( int row
)
8723 return m_table
->GetRowLabelValue( row
);
8733 wxString
wxGrid::GetColLabelValue( int col
)
8737 return m_table
->GetColLabelValue( col
);
8748 void wxGrid::SetRowLabelSize( int width
)
8750 width
= wxMax( width
, 0 );
8751 if ( width
!= m_rowLabelWidth
)
8755 m_rowLabelWin
->Show( false );
8756 m_cornerLabelWin
->Show( false );
8758 else if ( m_rowLabelWidth
== 0 )
8760 m_rowLabelWin
->Show( true );
8761 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8764 m_rowLabelWidth
= width
;
8766 wxScrolledWindow::Refresh( true );
8771 void wxGrid::SetColLabelSize( int height
)
8773 height
= wxMax( height
, 0 );
8774 if ( height
!= m_colLabelHeight
)
8778 m_colLabelWin
->Show( false );
8779 m_cornerLabelWin
->Show( false );
8781 else if ( m_colLabelHeight
== 0 )
8783 m_colLabelWin
->Show( true );
8784 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8787 m_colLabelHeight
= height
;
8789 wxScrolledWindow::Refresh( true );
8794 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8796 if ( m_labelBackgroundColour
!= colour
)
8798 m_labelBackgroundColour
= colour
;
8799 m_rowLabelWin
->SetBackgroundColour( colour
);
8800 m_colLabelWin
->SetBackgroundColour( colour
);
8801 m_cornerLabelWin
->SetBackgroundColour( colour
);
8803 if ( !GetBatchCount() )
8805 m_rowLabelWin
->Refresh();
8806 m_colLabelWin
->Refresh();
8807 m_cornerLabelWin
->Refresh();
8812 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8814 if ( m_labelTextColour
!= colour
)
8816 m_labelTextColour
= colour
;
8817 if ( !GetBatchCount() )
8819 m_rowLabelWin
->Refresh();
8820 m_colLabelWin
->Refresh();
8825 void wxGrid::SetLabelFont( const wxFont
& font
)
8828 if ( !GetBatchCount() )
8830 m_rowLabelWin
->Refresh();
8831 m_colLabelWin
->Refresh();
8835 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8837 // allow old (incorrect) defs to be used
8840 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8841 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8842 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8847 case wxTOP
: vert
= wxALIGN_TOP
; break;
8848 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8849 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8852 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8854 m_rowLabelHorizAlign
= horiz
;
8857 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8859 m_rowLabelVertAlign
= vert
;
8862 if ( !GetBatchCount() )
8864 m_rowLabelWin
->Refresh();
8868 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8870 // allow old (incorrect) defs to be used
8873 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8874 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8875 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8880 case wxTOP
: vert
= wxALIGN_TOP
; break;
8881 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8882 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8885 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8887 m_colLabelHorizAlign
= horiz
;
8890 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8892 m_colLabelVertAlign
= vert
;
8895 if ( !GetBatchCount() )
8897 m_colLabelWin
->Refresh();
8901 // Note: under MSW, the default column label font must be changed because it
8902 // does not support vertical printing
8904 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8905 // pGrid->SetLabelFont(font);
8906 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8908 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8910 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8912 m_colLabelTextOrientation
= textOrientation
;
8915 if ( !GetBatchCount() )
8917 m_colLabelWin
->Refresh();
8921 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8925 m_table
->SetRowLabelValue( row
, s
);
8926 if ( !GetBatchCount() )
8928 wxRect rect
= CellToRect( row
, 0);
8929 if ( rect
.height
> 0 )
8931 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8933 rect
.width
= m_rowLabelWidth
;
8934 m_rowLabelWin
->Refresh( true, &rect
);
8940 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8944 m_table
->SetColLabelValue( col
, s
);
8945 if ( !GetBatchCount() )
8947 wxRect rect
= CellToRect( 0, col
);
8948 if ( rect
.width
> 0 )
8950 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8952 rect
.height
= m_colLabelHeight
;
8953 m_colLabelWin
->Refresh( true, &rect
);
8959 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8961 if ( m_gridLineColour
!= colour
)
8963 m_gridLineColour
= colour
;
8965 wxClientDC
dc( m_gridWin
);
8967 DrawAllGridLines( dc
, wxRegion() );
8972 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8974 if ( m_cellHighlightColour
!= colour
)
8976 m_cellHighlightColour
= colour
;
8978 wxClientDC
dc( m_gridWin
);
8980 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8981 DrawCellHighlight(dc
, attr
);
8986 void wxGrid::SetCellHighlightPenWidth(int width
)
8988 if (m_cellHighlightPenWidth
!= width
)
8990 m_cellHighlightPenWidth
= width
;
8992 // Just redrawing the cell highlight is not enough since that won't
8993 // make any visible change if the the thickness is getting smaller.
8994 int row
= m_currentCellCoords
.GetRow();
8995 int col
= m_currentCellCoords
.GetCol();
8996 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8998 wxRect rect
= CellToRect(row
, col
);
8999 m_gridWin
->Refresh(true, &rect
);
9003 void wxGrid::SetCellHighlightROPenWidth(int width
)
9005 if (m_cellHighlightROPenWidth
!= width
)
9007 m_cellHighlightROPenWidth
= width
;
9009 // Just redrawing the cell highlight is not enough since that won't
9010 // make any visible change if the the thickness is getting smaller.
9011 int row
= m_currentCellCoords
.GetRow();
9012 int col
= m_currentCellCoords
.GetCol();
9013 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9015 wxRect rect
= CellToRect(row
, col
);
9016 m_gridWin
->Refresh(true, &rect
);
9020 void wxGrid::EnableGridLines( bool enable
)
9022 if ( enable
!= m_gridLinesEnabled
)
9024 m_gridLinesEnabled
= enable
;
9026 if ( !GetBatchCount() )
9030 wxClientDC
dc( m_gridWin
);
9032 DrawAllGridLines( dc
, wxRegion() );
9036 m_gridWin
->Refresh();
9043 int wxGrid::GetDefaultRowSize()
9045 return m_defaultRowHeight
;
9048 int wxGrid::GetRowSize( int row
)
9050 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9052 return GetRowHeight(row
);
9055 int wxGrid::GetDefaultColSize()
9057 return m_defaultColWidth
;
9060 int wxGrid::GetColSize( int col
)
9062 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9064 return GetColWidth(col
);
9067 // ============================================================================
9068 // access to the grid attributes: each of them has a default value in the grid
9069 // itself and may be overidden on a per-cell basis
9070 // ============================================================================
9072 // ----------------------------------------------------------------------------
9073 // setting default attributes
9074 // ----------------------------------------------------------------------------
9076 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9078 m_defaultCellAttr
->SetBackgroundColour(col
);
9080 m_gridWin
->SetBackgroundColour(col
);
9084 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9086 m_defaultCellAttr
->SetTextColour(col
);
9089 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9091 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9094 void wxGrid::SetDefaultCellOverflow( bool allow
)
9096 m_defaultCellAttr
->SetOverflow(allow
);
9099 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9101 m_defaultCellAttr
->SetFont(font
);
9105 // For editors and renderers the type registry takes precedence over the
9106 // default attr, so we need to register the new editor/renderer for the string
9107 // data type in order to make setting a default editor/renderer appear to
9110 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9112 RegisterDataType(wxGRID_VALUE_STRING
,
9114 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9117 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9119 RegisterDataType(wxGRID_VALUE_STRING
,
9120 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9124 // ----------------------------------------------------------------------------
9125 // access to the default attrbiutes
9126 // ----------------------------------------------------------------------------
9128 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9130 return m_defaultCellAttr
->GetBackgroundColour();
9133 wxColour
wxGrid::GetDefaultCellTextColour()
9135 return m_defaultCellAttr
->GetTextColour();
9138 wxFont
wxGrid::GetDefaultCellFont()
9140 return m_defaultCellAttr
->GetFont();
9143 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9145 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9148 bool wxGrid::GetDefaultCellOverflow()
9150 return m_defaultCellAttr
->GetOverflow();
9153 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9155 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9158 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9160 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9163 // ----------------------------------------------------------------------------
9164 // access to cell attributes
9165 // ----------------------------------------------------------------------------
9167 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9169 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9170 wxColour colour
= attr
->GetBackgroundColour();
9175 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9177 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9178 wxColour colour
= attr
->GetTextColour();
9183 wxFont
wxGrid::GetCellFont( int row
, int col
)
9185 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9186 wxFont font
= attr
->GetFont();
9191 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9193 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9194 attr
->GetAlignment(horiz
, vert
);
9198 bool wxGrid::GetCellOverflow( int row
, int col
)
9200 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9201 bool allow
= attr
->GetOverflow();
9207 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9209 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9210 attr
->GetSize( num_rows
, num_cols
);
9214 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9216 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9217 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9223 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9225 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9226 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9232 bool wxGrid::IsReadOnly(int row
, int col
) const
9234 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9235 bool isReadOnly
= attr
->IsReadOnly();
9241 // ----------------------------------------------------------------------------
9242 // attribute support: cache, automatic provider creation, ...
9243 // ----------------------------------------------------------------------------
9245 bool wxGrid::CanHaveAttributes()
9252 return m_table
->CanHaveAttributes();
9255 void wxGrid::ClearAttrCache()
9257 if ( m_attrCache
.row
!= -1 )
9259 wxSafeDecRef(m_attrCache
.attr
);
9260 m_attrCache
.attr
= NULL
;
9261 m_attrCache
.row
= -1;
9265 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9269 wxGrid
*self
= (wxGrid
*)this; // const_cast
9271 self
->ClearAttrCache();
9272 self
->m_attrCache
.row
= row
;
9273 self
->m_attrCache
.col
= col
;
9274 self
->m_attrCache
.attr
= attr
;
9279 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9281 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9283 *attr
= m_attrCache
.attr
;
9284 wxSafeIncRef(m_attrCache
.attr
);
9286 #ifdef DEBUG_ATTR_CACHE
9287 gs_nAttrCacheHits
++;
9294 #ifdef DEBUG_ATTR_CACHE
9295 gs_nAttrCacheMisses
++;
9302 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9304 wxGridCellAttr
*attr
= NULL
;
9305 // Additional test to avoid looking at the cache e.g. for
9306 // wxNoCellCoords, as this will confuse memory management.
9309 if ( !LookupAttr(row
, col
, &attr
) )
9311 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9312 : (wxGridCellAttr
*)NULL
;
9313 CacheAttr(row
, col
, attr
);
9319 attr
->SetDefAttr(m_defaultCellAttr
);
9323 attr
= m_defaultCellAttr
;
9330 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9332 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9333 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9335 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9336 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9338 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9341 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9343 // artificially inc the ref count to match DecRef() in caller
9345 m_table
->SetAttr(attr
, row
, col
);
9351 // ----------------------------------------------------------------------------
9352 // setting column attributes (wrappers around SetColAttr)
9353 // ----------------------------------------------------------------------------
9355 void wxGrid::SetColFormatBool(int col
)
9357 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9360 void wxGrid::SetColFormatNumber(int col
)
9362 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9365 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9367 wxString typeName
= wxGRID_VALUE_FLOAT
;
9368 if ( (width
!= -1) || (precision
!= -1) )
9370 typeName
<< _T(':') << width
<< _T(',') << precision
;
9373 SetColFormatCustom(col
, typeName
);
9376 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9378 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9380 attr
= new wxGridCellAttr
;
9381 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9382 attr
->SetRenderer(renderer
);
9384 SetColAttr(col
, attr
);
9388 // ----------------------------------------------------------------------------
9389 // setting cell attributes: this is forwarded to the table
9390 // ----------------------------------------------------------------------------
9392 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9394 if ( CanHaveAttributes() )
9396 m_table
->SetAttr(attr
, row
, col
);
9405 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9407 if ( CanHaveAttributes() )
9409 m_table
->SetRowAttr(attr
, row
);
9418 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9420 if ( CanHaveAttributes() )
9422 m_table
->SetColAttr(attr
, col
);
9431 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9433 if ( CanHaveAttributes() )
9435 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9436 attr
->SetBackgroundColour(colour
);
9441 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9443 if ( CanHaveAttributes() )
9445 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9446 attr
->SetTextColour(colour
);
9451 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9453 if ( CanHaveAttributes() )
9455 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9456 attr
->SetFont(font
);
9461 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9463 if ( CanHaveAttributes() )
9465 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9466 attr
->SetAlignment(horiz
, vert
);
9471 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9473 if ( CanHaveAttributes() )
9475 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9476 attr
->SetOverflow(allow
);
9481 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9483 if ( CanHaveAttributes() )
9485 int cell_rows
, cell_cols
;
9487 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9488 attr
->GetSize(&cell_rows
, &cell_cols
);
9489 attr
->SetSize(num_rows
, num_cols
);
9492 // Cannot set the size of a cell to 0 or negative values
9493 // While it is perfectly legal to do that, this function cannot
9494 // handle all the possibilies, do it by hand by getting the CellAttr.
9495 // You can only set the size of a cell to 1,1 or greater with this fn
9496 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9497 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9498 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9499 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9501 // if this was already a multicell then "turn off" the other cells first
9502 if ((cell_rows
> 1) || (cell_rows
> 1))
9505 for (j
=row
; j
<row
+cell_rows
; j
++)
9507 for (i
=col
; i
<col
+cell_cols
; i
++)
9509 if ((i
!= col
) || (j
!= row
))
9511 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9512 attr_stub
->SetSize( 1, 1 );
9513 attr_stub
->DecRef();
9519 // mark the cells that will be covered by this cell to
9520 // negative or zero values to point back at this cell
9521 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9524 for (j
=row
; j
<row
+num_rows
; j
++)
9526 for (i
=col
; i
<col
+num_cols
; i
++)
9528 if ((i
!= col
) || (j
!= row
))
9530 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9531 attr_stub
->SetSize( row
-j
, col
-i
);
9532 attr_stub
->DecRef();
9540 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9542 if ( CanHaveAttributes() )
9544 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9545 attr
->SetRenderer(renderer
);
9550 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9552 if ( CanHaveAttributes() )
9554 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9555 attr
->SetEditor(editor
);
9560 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9562 if ( CanHaveAttributes() )
9564 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9565 attr
->SetReadOnly(isReadOnly
);
9570 // ----------------------------------------------------------------------------
9571 // Data type registration
9572 // ----------------------------------------------------------------------------
9574 void wxGrid::RegisterDataType(const wxString
& typeName
,
9575 wxGridCellRenderer
* renderer
,
9576 wxGridCellEditor
* editor
)
9578 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9582 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9584 wxString typeName
= m_table
->GetTypeName(row
, col
);
9585 return GetDefaultEditorForType(typeName
);
9588 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9590 wxString typeName
= m_table
->GetTypeName(row
, col
);
9591 return GetDefaultRendererForType(typeName
);
9595 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9597 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9598 if ( index
== wxNOT_FOUND
)
9602 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9603 wxFAIL_MSG(errStr
.c_str());
9608 return m_typeRegistry
->GetEditor(index
);
9612 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9614 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9615 if ( index
== wxNOT_FOUND
)
9619 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9620 wxFAIL_MSG(errStr
.c_str());
9625 return m_typeRegistry
->GetRenderer(index
);
9628 // ----------------------------------------------------------------------------
9630 // ----------------------------------------------------------------------------
9632 void wxGrid::EnableDragRowSize( bool enable
)
9634 m_canDragRowSize
= enable
;
9637 void wxGrid::EnableDragColSize( bool enable
)
9639 m_canDragColSize
= enable
;
9642 void wxGrid::EnableDragGridSize( bool enable
)
9644 m_canDragGridSize
= enable
;
9647 void wxGrid::EnableDragCell( bool enable
)
9649 m_canDragCell
= enable
;
9652 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9654 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9656 if ( resizeExistingRows
)
9658 // since we are resizing all rows to the default row size,
9659 // we can simply clear the row heights and row bottoms
9660 // arrays (which also allows us to take advantage of
9661 // some speed optimisations)
9662 m_rowHeights
.Empty();
9663 m_rowBottoms
.Empty();
9664 if ( !GetBatchCount() )
9669 void wxGrid::SetRowSize( int row
, int height
)
9671 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9673 // See comment in SetColSize
9674 if ( height
< GetRowMinimalAcceptableHeight())
9677 if ( m_rowHeights
.IsEmpty() )
9679 // need to really create the array
9683 int h
= wxMax( 0, height
);
9684 int diff
= h
- m_rowHeights
[row
];
9686 m_rowHeights
[row
] = h
;
9688 for ( i
= row
; i
< m_numRows
; i
++ )
9690 m_rowBottoms
[i
] += diff
;
9692 if ( !GetBatchCount() )
9696 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9698 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9700 if ( resizeExistingCols
)
9702 // since we are resizing all columns to the default column size,
9703 // we can simply clear the col widths and col rights
9704 // arrays (which also allows us to take advantage of
9705 // some speed optimisations)
9706 m_colWidths
.Empty();
9707 m_colRights
.Empty();
9708 if ( !GetBatchCount() )
9713 void wxGrid::SetColSize( int col
, int width
)
9715 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9717 // should we check that it's bigger than GetColMinimalWidth(col) here?
9719 // No, because it is reasonable to assume the library user know's
9720 // what he is doing. However whe should test against the weaker
9721 // constariant of minimalAcceptableWidth, as this breaks rendering
9723 // This test then fixes sf.net bug #645734
9725 if ( width
< GetColMinimalAcceptableWidth() )
9728 if ( m_colWidths
.IsEmpty() )
9730 // need to really create the array
9734 // if < 0 calc new width from label
9738 wxArrayString lines
;
9739 wxClientDC
dc(m_colLabelWin
);
9740 dc
.SetFont(GetLabelFont());
9741 StringToLines(GetColLabelValue(col
), lines
);
9742 GetTextBoxSize(dc
, lines
, &w
, &h
);
9746 int w
= wxMax( 0, width
);
9747 int diff
= w
- m_colWidths
[col
];
9748 m_colWidths
[col
] = w
;
9751 for ( i
= col
; i
< m_numCols
; i
++ )
9753 m_colRights
[i
] += diff
;
9756 if ( !GetBatchCount() )
9760 void wxGrid::SetColMinimalWidth( int col
, int width
)
9762 if (width
> GetColMinimalAcceptableWidth())
9764 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9765 m_colMinWidths
[key
] = width
;
9769 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9771 if (width
> GetRowMinimalAcceptableHeight())
9773 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9774 m_rowMinHeights
[key
] = width
;
9778 int wxGrid::GetColMinimalWidth(int col
) const
9780 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9781 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9783 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9786 int wxGrid::GetRowMinimalHeight(int row
) const
9788 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9789 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9791 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9794 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9796 // We do allow a width of 0 since this gives us
9797 // an easy way to temporarily hiding columns.
9799 m_minAcceptableColWidth
= width
;
9802 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9804 // We do allow a height of 0 since this gives us
9805 // an easy way to temporarily hiding rows.
9807 m_minAcceptableRowHeight
= height
;
9810 int wxGrid::GetColMinimalAcceptableWidth() const
9812 return m_minAcceptableColWidth
;
9815 int wxGrid::GetRowMinimalAcceptableHeight() const
9817 return m_minAcceptableRowHeight
;
9820 // ----------------------------------------------------------------------------
9822 // ----------------------------------------------------------------------------
9824 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9826 wxClientDC
dc(m_gridWin
);
9828 // cancel editing of cell
9829 HideCellEditControl();
9830 SaveEditControlValue();
9832 // init both of them to avoid compiler warnings, even if we only need one
9840 wxCoord extent
, extentMax
= 0;
9841 int max
= column
? m_numRows
: m_numCols
;
9842 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9849 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9850 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9853 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9854 extent
= column
? size
.x
: size
.y
;
9855 if ( extent
> extentMax
)
9866 // now also compare with the column label extent
9868 dc
.SetFont( GetLabelFont() );
9872 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9873 if ( GetColLabelTextOrientation() == wxVERTICAL
)
9877 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9879 extent
= column
? w
: h
;
9880 if ( extent
> extentMax
)
9887 // empty column - give default extent (notice that if extentMax is less
9888 // than default extent but != 0, it's ok)
9889 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9894 // leave some space around text
9902 SetColSize( col
, extentMax
);
9903 if ( !GetBatchCount() )
9906 m_gridWin
->GetClientSize( &cw
, &ch
);
9907 wxRect
rect ( CellToRect( 0, col
) );
9909 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9910 rect
.width
= cw
- rect
.x
;
9911 rect
.height
= m_colLabelHeight
;
9912 m_colLabelWin
->Refresh( true, &rect
);
9917 SetRowSize(row
, extentMax
);
9918 if ( !GetBatchCount() )
9921 m_gridWin
->GetClientSize( &cw
, &ch
);
9922 wxRect
rect ( CellToRect( row
, 0 ) );
9924 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9925 rect
.width
= m_rowLabelWidth
;
9926 rect
.height
= ch
- rect
.y
;
9927 m_rowLabelWin
->Refresh( true, &rect
);
9933 SetColMinimalWidth(col
, extentMax
);
9935 SetRowMinimalHeight(row
, extentMax
);
9939 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9941 int width
= m_rowLabelWidth
;
9946 for ( int col
= 0; col
< m_numCols
; col
++ )
9950 AutoSizeColumn(col
, setAsMin
);
9953 width
+= GetColWidth(col
);
9962 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9964 int height
= m_colLabelHeight
;
9969 for ( int row
= 0; row
< m_numRows
; row
++ )
9972 AutoSizeRow(row
, setAsMin
);
9974 height
+= GetRowHeight(row
);
9983 void wxGrid::AutoSize()
9987 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9989 // round up the size to a multiple of scroll step - this ensures that we
9990 // won't get the scrollbars if we're sized exactly to this width
9991 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9993 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
9994 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
9996 // distribute the extra space between the columns/rows to avoid having
9997 // extra white space
9999 // Remove the extra m_extraWidth + 1 added above
10000 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10001 if ( diff
&& m_numCols
)
10003 // try to resize the columns uniformly
10004 wxCoord diffPerCol
= diff
/ m_numCols
;
10007 for ( int col
= 0; col
< m_numCols
; col
++ )
10009 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10013 // add remaining amount to the last columns
10014 diff
-= diffPerCol
* m_numCols
;
10017 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10019 SetColSize(col
, GetColWidth(col
) + 1);
10025 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10026 if ( diff
&& m_numRows
)
10028 // try to resize the columns uniformly
10029 wxCoord diffPerRow
= diff
/ m_numRows
;
10032 for ( int row
= 0; row
< m_numRows
; row
++ )
10034 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10038 // add remaining amount to the last rows
10039 diff
-= diffPerRow
* m_numRows
;
10042 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10044 SetRowSize(row
, GetRowHeight(row
) + 1);
10051 SetClientSize(sizeFit
);
10054 void wxGrid::AutoSizeRowLabelSize( int row
)
10056 wxArrayString lines
;
10059 // Hide the edit control, so it
10060 // won't interfere with drag-shrinking.
10061 if ( IsCellEditControlShown() )
10063 HideCellEditControl();
10064 SaveEditControlValue();
10067 // autosize row height depending on label text
10068 StringToLines( GetRowLabelValue( row
), lines
);
10069 wxClientDC
dc( m_rowLabelWin
);
10070 GetTextBoxSize( dc
, lines
, &w
, &h
);
10071 if ( h
< m_defaultRowHeight
)
10072 h
= m_defaultRowHeight
;
10073 SetRowSize(row
, h
);
10077 void wxGrid::AutoSizeColLabelSize( int col
)
10079 wxArrayString lines
;
10082 // Hide the edit control, so it
10083 // won't interfer with drag-shrinking.
10084 if ( IsCellEditControlShown() )
10086 HideCellEditControl();
10087 SaveEditControlValue();
10090 // autosize column width depending on label text
10091 StringToLines( GetColLabelValue( col
), lines
);
10092 wxClientDC
dc( m_colLabelWin
);
10093 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10094 GetTextBoxSize( dc
, lines
, &w
, &h
);
10096 GetTextBoxSize( dc
, lines
, &h
, &w
);
10097 if ( w
< m_defaultColWidth
)
10098 w
= m_defaultColWidth
;
10099 SetColSize(col
, w
);
10103 wxSize
wxGrid::DoGetBestSize() const
10105 // don't set sizes, only calculate them
10106 wxGrid
*self
= (wxGrid
*)this; // const_cast
10109 width
= self
->SetOrCalcColumnSizes(true);
10110 height
= self
->SetOrCalcRowSizes(true);
10117 // Round up to a multiple the scroll rate
10118 // NOTE: this still doesn't get rid of the scrollbars;
10119 // is there any magic incantation for that?
10121 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10123 width
+= 1 + xpu
- (width
% xpu
);
10125 height
+= 1 + ypu
- (height
% ypu
);
10127 // limit to 1/4 of the screen size
10128 int maxwidth
, maxheight
;
10129 wxDisplaySize( &maxwidth
, &maxheight
);
10132 if ( width
> maxwidth
)
10134 if ( height
> maxheight
)
10135 height
= maxheight
;
10137 wxSize
best(width
, height
);
10139 // NOTE: This size should be cached, but first we need to add calls to
10140 // InvalidateBestSize everywhere that could change the results of this
10142 // CacheBestSize(size);
10152 wxPen
& wxGrid::GetDividerPen() const
10157 // ----------------------------------------------------------------------------
10158 // cell value accessor functions
10159 // ----------------------------------------------------------------------------
10161 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10165 m_table
->SetValue( row
, col
, s
);
10166 if ( !GetBatchCount() )
10169 wxRect
rect( CellToRect( row
, col
) );
10171 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10172 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10173 m_gridWin
->Refresh( false, &rect
);
10176 if ( m_currentCellCoords
.GetRow() == row
&&
10177 m_currentCellCoords
.GetCol() == col
&&
10178 IsCellEditControlShown())
10179 // Note: If we are using IsCellEditControlEnabled,
10180 // this interacts badly with calling SetCellValue from
10181 // an EVT_GRID_CELL_CHANGE handler.
10183 HideCellEditControl();
10184 ShowCellEditControl(); // will reread data from table
10190 // ----------------------------------------------------------------------------
10191 // block, row and col selection
10192 // ----------------------------------------------------------------------------
10194 void wxGrid::SelectRow( int row
, bool addToSelected
)
10196 if ( IsSelection() && !addToSelected
)
10200 m_selection
->SelectRow( row
, false, addToSelected
);
10203 void wxGrid::SelectCol( int col
, bool addToSelected
)
10205 if ( IsSelection() && !addToSelected
)
10209 m_selection
->SelectCol( col
, false, addToSelected
);
10212 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10213 bool addToSelected
)
10215 if ( IsSelection() && !addToSelected
)
10219 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10220 false, addToSelected
);
10223 void wxGrid::SelectAll()
10225 if ( m_numRows
> 0 && m_numCols
> 0 )
10228 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10232 // ----------------------------------------------------------------------------
10233 // cell, row and col deselection
10234 // ----------------------------------------------------------------------------
10236 void wxGrid::DeselectRow( int row
)
10238 if ( !m_selection
)
10241 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10243 if ( m_selection
->IsInSelection(row
, 0 ) )
10244 m_selection
->ToggleCellSelection( row
, 0);
10248 int nCols
= GetNumberCols();
10249 for ( int i
= 0; i
< nCols
; i
++ )
10251 if ( m_selection
->IsInSelection(row
, i
) )
10252 m_selection
->ToggleCellSelection( row
, i
);
10257 void wxGrid::DeselectCol( int col
)
10259 if ( !m_selection
)
10262 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10264 if ( m_selection
->IsInSelection(0, col
) )
10265 m_selection
->ToggleCellSelection( 0, col
);
10269 int nRows
= GetNumberRows();
10270 for ( int i
= 0; i
< nRows
; i
++ )
10272 if ( m_selection
->IsInSelection(i
, col
) )
10273 m_selection
->ToggleCellSelection(i
, col
);
10278 void wxGrid::DeselectCell( int row
, int col
)
10280 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10281 m_selection
->ToggleCellSelection(row
, col
);
10284 bool wxGrid::IsSelection()
10286 return ( m_selection
&& (m_selection
->IsSelection() ||
10287 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10288 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10291 bool wxGrid::IsInSelection( int row
, int col
) const
10293 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10294 ( row
>= m_selectingTopLeft
.GetRow() &&
10295 col
>= m_selectingTopLeft
.GetCol() &&
10296 row
<= m_selectingBottomRight
.GetRow() &&
10297 col
<= m_selectingBottomRight
.GetCol() )) );
10300 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10304 wxGridCellCoordsArray a
;
10308 return m_selection
->m_cellSelection
;
10311 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10315 wxGridCellCoordsArray a
;
10319 return m_selection
->m_blockSelectionTopLeft
;
10322 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10326 wxGridCellCoordsArray a
;
10330 return m_selection
->m_blockSelectionBottomRight
;
10333 wxArrayInt
wxGrid::GetSelectedRows() const
10341 return m_selection
->m_rowSelection
;
10344 wxArrayInt
wxGrid::GetSelectedCols() const
10352 return m_selection
->m_colSelection
;
10355 void wxGrid::ClearSelection()
10357 m_selectingTopLeft
= wxGridNoCellCoords
;
10358 m_selectingBottomRight
= wxGridNoCellCoords
;
10360 m_selection
->ClearSelection();
10364 // This function returns the rectangle that encloses the given block
10365 // in device coords clipped to the client size of the grid window.
10367 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10368 const wxGridCellCoords
&bottomRight
)
10370 wxRect
rect( wxGridNoCellRect
);
10373 cellRect
= CellToRect( topLeft
);
10374 if ( cellRect
!= wxGridNoCellRect
)
10380 rect
= wxRect(0, 0, 0, 0);
10383 cellRect
= CellToRect( bottomRight
);
10384 if ( cellRect
!= wxGridNoCellRect
)
10390 return wxGridNoCellRect
;
10394 int left
= rect
.GetLeft();
10395 int top
= rect
.GetTop();
10396 int right
= rect
.GetRight();
10397 int bottom
= rect
.GetBottom();
10399 int leftCol
= topLeft
.GetCol();
10400 int topRow
= topLeft
.GetRow();
10401 int rightCol
= bottomRight
.GetCol();
10402 int bottomRow
= bottomRight
.GetRow();
10410 leftCol
= rightCol
;
10420 topRow
= bottomRow
;
10424 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10426 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10428 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10430 cellRect
= CellToRect( j
, i
);
10432 if (cellRect
.x
< left
)
10434 if (cellRect
.y
< top
)
10436 if (cellRect
.x
+ cellRect
.width
> right
)
10437 right
= cellRect
.x
+ cellRect
.width
;
10438 if (cellRect
.y
+ cellRect
.height
> bottom
)
10439 bottom
= cellRect
.y
+ cellRect
.height
;
10443 i
= rightCol
; // jump over inner cells.
10448 // convert to scrolled coords
10450 CalcScrolledPosition( left
, top
, &left
, &top
);
10451 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10454 m_gridWin
->GetClientSize( &cw
, &ch
);
10456 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10457 return wxRect(0,0,0,0);
10459 rect
.SetLeft( wxMax(0, left
) );
10460 rect
.SetTop( wxMax(0, top
) );
10461 rect
.SetRight( wxMin(cw
, right
) );
10462 rect
.SetBottom( wxMin(ch
, bottom
) );
10467 // ----------------------------------------------------------------------------
10468 // grid event classes
10469 // ----------------------------------------------------------------------------
10471 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10473 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10474 int row
, int col
, int x
, int y
, bool sel
,
10475 bool control
, bool shift
, bool alt
, bool meta
)
10476 : wxNotifyEvent( type
, id
)
10483 m_control
= control
;
10488 SetEventObject(obj
);
10492 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10494 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10495 int rowOrCol
, int x
, int y
,
10496 bool control
, bool shift
, bool alt
, bool meta
)
10497 : wxNotifyEvent( type
, id
)
10499 m_rowOrCol
= rowOrCol
;
10502 m_control
= control
;
10507 SetEventObject(obj
);
10511 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10513 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10514 const wxGridCellCoords
& topLeft
,
10515 const wxGridCellCoords
& bottomRight
,
10516 bool sel
, bool control
,
10517 bool shift
, bool alt
, bool meta
)
10518 : wxNotifyEvent( type
, id
)
10520 m_topLeft
= topLeft
;
10521 m_bottomRight
= bottomRight
;
10523 m_control
= control
;
10528 SetEventObject(obj
);
10532 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10534 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10535 wxObject
* obj
, int row
,
10536 int col
, wxControl
* ctrl
)
10537 : wxCommandEvent(type
, id
)
10539 SetEventObject(obj
);
10545 #endif // wxUSE_GRID