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
);
5842 m_dragRowOrCol
= dragCol
;
5850 m_dragRowOrCol
= dragCol
;
5852 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5854 if ( CanDragColSize() && CanDragGridSize() )
5855 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5861 // Neither on a row or col edge
5863 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5865 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5871 void wxGrid::DoEndDragResizeRow()
5873 if ( m_dragLastPos
>= 0 )
5875 // erase the last line and resize the row
5877 int cw
, ch
, left
, dummy
;
5878 m_gridWin
->GetClientSize( &cw
, &ch
);
5879 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5881 wxClientDC
dc( m_gridWin
);
5883 dc
.SetLogicalFunction( wxINVERT
);
5884 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5885 HideCellEditControl();
5886 SaveEditControlValue();
5888 int rowTop
= GetRowTop(m_dragRowOrCol
);
5889 SetRowSize( m_dragRowOrCol
,
5890 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5892 if ( !GetBatchCount() )
5894 // Only needed to get the correct rect.y:
5895 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5897 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5898 rect
.width
= m_rowLabelWidth
;
5899 rect
.height
= ch
- rect
.y
;
5900 m_rowLabelWin
->Refresh( true, &rect
);
5902 // if there is a multicell block, paint all of it
5905 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5906 int leftCol
= XToCol(left
);
5907 int rightCol
= internalXToCol(left
+cw
);
5910 for (i
=leftCol
; i
<rightCol
; i
++)
5912 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5913 if (cell_rows
< subtract_rows
)
5914 subtract_rows
= cell_rows
;
5916 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5917 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5918 rect
.height
= ch
- rect
.y
;
5921 m_gridWin
->Refresh( false, &rect
);
5924 ShowCellEditControl();
5929 void wxGrid::DoEndDragResizeCol()
5931 if ( m_dragLastPos
>= 0 )
5933 // erase the last line and resize the col
5935 int cw
, ch
, dummy
, top
;
5936 m_gridWin
->GetClientSize( &cw
, &ch
);
5937 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5939 wxClientDC
dc( m_gridWin
);
5941 dc
.SetLogicalFunction( wxINVERT
);
5942 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5943 HideCellEditControl();
5944 SaveEditControlValue();
5946 int colLeft
= GetColLeft(m_dragRowOrCol
);
5947 SetColSize( m_dragRowOrCol
,
5948 wxMax( m_dragLastPos
- colLeft
,
5949 GetColMinimalWidth(m_dragRowOrCol
) ) );
5951 if ( !GetBatchCount() )
5953 // Only needed to get the correct rect.x:
5954 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5956 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5957 rect
.width
= cw
- rect
.x
;
5958 rect
.height
= m_colLabelHeight
;
5959 m_colLabelWin
->Refresh( true, &rect
);
5961 // if there is a multicell block, paint all of it
5964 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5965 int topRow
= YToRow(top
);
5966 int bottomRow
= internalYToRow(top
+cw
);
5969 for (i
=topRow
; i
<bottomRow
; i
++)
5971 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5972 if (cell_cols
< subtract_cols
)
5973 subtract_cols
= cell_cols
;
5975 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5976 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5977 rect
.width
= cw
- rect
.x
;
5980 m_gridWin
->Refresh( false, &rect
);
5983 ShowCellEditControl();
5990 // ------ interaction with data model
5992 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5994 switch ( msg
.GetId() )
5996 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5997 return GetModelValues();
5999 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6000 return SetModelValues();
6002 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6003 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6004 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6005 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6006 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6007 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6008 return Redimension( msg
);
6017 // The behaviour of this function depends on the grid table class
6018 // Clear() function. For the default wxGridStringTable class the
6019 // behavious is to replace all cell contents with wxEmptyString but
6020 // not to change the number of rows or cols.
6022 void wxGrid::ClearGrid()
6026 if (IsCellEditControlEnabled())
6027 DisableCellEditControl();
6030 if ( !GetBatchCount() ) m_gridWin
->Refresh();
6035 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6037 // TODO: something with updateLabels flag
6041 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6047 if (IsCellEditControlEnabled())
6048 DisableCellEditControl();
6050 bool done
= m_table
->InsertRows( pos
, numRows
);
6053 // the table will have sent the results of the insert row
6054 // operation to this view object as a grid table message
6060 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6062 // TODO: something with updateLabels flag
6066 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6072 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6074 // the table will have sent the results of the append row
6075 // operation to this view object as a grid table message
6081 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6083 // TODO: something with updateLabels flag
6087 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6093 if (IsCellEditControlEnabled())
6094 DisableCellEditControl();
6096 bool done
= m_table
->DeleteRows( pos
, numRows
);
6098 // the table will have sent the results of the delete row
6099 // operation to this view object as a grid table message
6105 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6107 // TODO: something with updateLabels flag
6111 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6117 if (IsCellEditControlEnabled())
6118 DisableCellEditControl();
6120 bool done
= m_table
->InsertCols( pos
, numCols
);
6122 // the table will have sent the results of the insert col
6123 // operation to this view object as a grid table message
6129 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6131 // TODO: something with updateLabels flag
6135 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6141 bool done
= m_table
->AppendCols( numCols
);
6143 // the table will have sent the results of the append col
6144 // operation to this view object as a grid table message
6150 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6152 // TODO: something with updateLabels flag
6156 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6162 if (IsCellEditControlEnabled())
6163 DisableCellEditControl();
6165 bool done
= m_table
->DeleteCols( pos
, numCols
);
6167 // the table will have sent the results of the delete col
6168 // operation to this view object as a grid table message
6176 // ----- event handlers
6179 // Generate a grid event based on a mouse event and
6180 // return the result of ProcessEvent()
6182 int wxGrid::SendEvent( const wxEventType type
,
6184 wxMouseEvent
& mouseEv
)
6189 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6191 int rowOrCol
= (row
== -1 ? col
: row
);
6193 wxGridSizeEvent
gridEvt( GetId(),
6197 mouseEv
.GetX() + GetRowLabelSize(),
6198 mouseEv
.GetY() + GetColLabelSize(),
6199 mouseEv
.ControlDown(),
6200 mouseEv
.ShiftDown(),
6202 mouseEv
.MetaDown() );
6204 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6205 vetoed
= !gridEvt
.IsAllowed();
6207 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6209 // Right now, it should _never_ end up here!
6210 wxGridRangeSelectEvent
gridEvt( GetId(),
6214 m_selectingBottomRight
,
6216 mouseEv
.ControlDown(),
6217 mouseEv
.ShiftDown(),
6219 mouseEv
.MetaDown() );
6221 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6222 vetoed
= !gridEvt
.IsAllowed();
6226 wxGridEvent
gridEvt( GetId(),
6230 mouseEv
.GetX() + GetRowLabelSize(),
6231 mouseEv
.GetY() + GetColLabelSize(),
6233 mouseEv
.ControlDown(),
6234 mouseEv
.ShiftDown(),
6236 mouseEv
.MetaDown() );
6237 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6238 vetoed
= !gridEvt
.IsAllowed();
6241 // A Veto'd event may not be `claimed' so test this first
6245 return claimed
? 1 : 0;
6249 // Generate a grid event of specified type and return the result
6250 // of ProcessEvent().
6252 int wxGrid::SendEvent( const wxEventType type
,
6258 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6260 int rowOrCol
= (row
== -1 ? col
: row
);
6262 wxGridSizeEvent
gridEvt( GetId(),
6267 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6268 vetoed
= !gridEvt
.IsAllowed();
6272 wxGridEvent
gridEvt( GetId(),
6277 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6278 vetoed
= !gridEvt
.IsAllowed();
6281 // A Veto'd event may not be `claimed' so test this first
6285 return claimed
? 1 : 0;
6288 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6290 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6293 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6295 // Don't do anything if between Begin/EndBatch...
6296 // EndBatch() will do all this on the last nested one anyway.
6297 if (! GetBatchCount())
6299 // Refresh to get correct scrolled position:
6300 wxScrolledWindow::Refresh(eraseb
, rect
);
6304 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6305 int width_label
, width_cell
, height_label
, height_cell
;
6308 //Copy rectangle can get scroll offsets..
6309 rect_x
= rect
->GetX();
6310 rect_y
= rect
->GetY();
6311 rectWidth
= rect
->GetWidth();
6312 rectHeight
= rect
->GetHeight();
6314 width_label
= m_rowLabelWidth
- rect_x
;
6315 if (width_label
> rectWidth
)
6316 width_label
= rectWidth
;
6318 height_label
= m_colLabelHeight
- rect_y
;
6319 if (height_label
> rectHeight
) height_label
= rectHeight
;
6321 if (rect_x
> m_rowLabelWidth
)
6323 x
= rect_x
- m_rowLabelWidth
;
6324 width_cell
= rectWidth
;
6329 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6332 if (rect_y
> m_colLabelHeight
)
6334 y
= rect_y
- m_colLabelHeight
;
6335 height_cell
= rectHeight
;
6340 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6343 // Paint corner label part intersecting rect.
6344 if ( width_label
> 0 && height_label
> 0 )
6346 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6347 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6350 // Paint col labels part intersecting rect.
6351 if ( width_cell
> 0 && height_label
> 0 )
6353 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6354 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6357 // Paint row labels part intersecting rect.
6358 if ( width_label
> 0 && height_cell
> 0 )
6360 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6361 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6364 // Paint cell area part intersecting rect.
6365 if ( width_cell
> 0 && height_cell
> 0 )
6367 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6368 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6373 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6374 m_colLabelWin
->Refresh(eraseb
, NULL
);
6375 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6376 m_gridWin
->Refresh(eraseb
, NULL
);
6381 void wxGrid::OnSize( wxSizeEvent
& event
)
6383 // position the child windows
6386 // don't call CalcDimensions() from here, the base class handles the size
6392 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6394 if ( m_inOnKeyDown
)
6396 // shouldn't be here - we are going round in circles...
6398 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6401 m_inOnKeyDown
= true;
6403 // propagate the event up and see if it gets processed
6405 wxWindow
*parent
= GetParent();
6406 wxKeyEvent
keyEvt( event
);
6407 keyEvt
.SetEventObject( parent
);
6409 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6412 // try local handlers
6414 switch ( event
.GetKeyCode() )
6417 if ( event
.ControlDown() )
6419 MoveCursorUpBlock( event
.ShiftDown() );
6423 MoveCursorUp( event
.ShiftDown() );
6428 if ( event
.ControlDown() )
6430 MoveCursorDownBlock( event
.ShiftDown() );
6434 MoveCursorDown( event
.ShiftDown() );
6439 if ( event
.ControlDown() )
6441 MoveCursorLeftBlock( event
.ShiftDown() );
6445 MoveCursorLeft( event
.ShiftDown() );
6450 if ( event
.ControlDown() )
6452 MoveCursorRightBlock( event
.ShiftDown() );
6456 MoveCursorRight( event
.ShiftDown() );
6461 case WXK_NUMPAD_ENTER
:
6462 if ( event
.ControlDown() )
6464 event
.Skip(); // to let the edit control have the return
6468 if ( GetGridCursorRow() < GetNumberRows()-1 )
6470 MoveCursorDown( event
.ShiftDown() );
6474 // at the bottom of a column
6475 DisableCellEditControl();
6485 if (event
.ShiftDown())
6487 if ( GetGridCursorCol() > 0 )
6489 MoveCursorLeft( false );
6494 DisableCellEditControl();
6499 if ( GetGridCursorCol() < GetNumberCols()-1 )
6501 MoveCursorRight( false );
6506 DisableCellEditControl();
6512 if ( event
.ControlDown() )
6514 MakeCellVisible( 0, 0 );
6515 SetCurrentCell( 0, 0 );
6524 if ( event
.ControlDown() )
6526 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6527 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6544 if ( event
.ControlDown() )
6548 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6549 m_currentCellCoords
.GetCol(),
6550 event
.ControlDown(),
6557 if ( !IsEditable() )
6559 MoveCursorRight( false );
6562 // Otherwise fall through to default
6570 m_inOnKeyDown
= false;
6573 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6575 // try local handlers
6577 if ( event
.GetKeyCode() == WXK_SHIFT
)
6579 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6580 m_selectingBottomRight
!= wxGridNoCellCoords
)
6584 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6585 m_selectingTopLeft
.GetCol(),
6586 m_selectingBottomRight
.GetRow(),
6587 m_selectingBottomRight
.GetCol(),
6588 event
.ControlDown(),
6595 m_selectingTopLeft
= wxGridNoCellCoords
;
6596 m_selectingBottomRight
= wxGridNoCellCoords
;
6597 m_selectingKeyboard
= wxGridNoCellCoords
;
6601 void wxGrid::OnChar( wxKeyEvent
& event
)
6603 // is it possible to edit the current cell at all?
6604 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6606 // yes, now check whether the cells editor accepts the key
6607 int row
= m_currentCellCoords
.GetRow();
6608 int col
= m_currentCellCoords
.GetCol();
6609 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6610 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6612 // <F2> is special and will always start editing, for
6613 // other keys - ask the editor itself
6614 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6615 || editor
->IsAcceptedKey(event
) )
6617 // ensure cell is visble
6618 MakeCellVisible(row
, col
);
6619 EnableCellEditControl();
6621 // a problem can arise if the cell is not completely
6622 // visible (even after calling MakeCellVisible the
6623 // control is not created and calling StartingKey will
6625 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6626 editor
->StartingKey(event
);
6643 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6647 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6649 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6651 // the event has been intercepted - do nothing
6655 wxClientDC
dc(m_gridWin
);
6658 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6660 DisableCellEditControl();
6662 if ( IsVisible( m_currentCellCoords
, false ) )
6665 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6666 if ( !m_gridLinesEnabled
)
6674 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6676 // Otherwise refresh redraws the highlight!
6677 m_currentCellCoords
= coords
;
6679 DrawGridCellArea(dc
,cells
);
6680 DrawAllGridLines( dc
, r
);
6684 m_currentCellCoords
= coords
;
6686 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6687 DrawCellHighlight(dc
, attr
);
6692 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6695 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6699 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6702 rightCol
= GetNumberCols() - 1;
6704 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6707 bottomRow
= GetNumberRows() - 1;
6711 if ( topRow
> bottomRow
)
6718 if ( leftCol
> rightCol
)
6725 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6726 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6728 // First the case that we selected a completely new area
6729 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6730 m_selectingBottomRight
== wxGridNoCellCoords
)
6733 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6734 wxGridCellCoords ( bottomRow
, rightCol
) );
6735 m_gridWin
->Refresh( false, &rect
);
6737 // Now handle changing an existing selection area.
6738 else if ( m_selectingTopLeft
!= updateTopLeft
||
6739 m_selectingBottomRight
!= updateBottomRight
)
6741 // Compute two optimal update rectangles:
6742 // Either one rectangle is a real subset of the
6743 // other, or they are (almost) disjoint!
6745 bool need_refresh
[4];
6749 need_refresh
[3] = false;
6752 // Store intermediate values
6753 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6754 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6755 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6756 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6758 // Determine the outer/inner coordinates.
6759 if (oldLeft
> leftCol
)
6765 if (oldTop
> topRow
)
6771 if (oldRight
< rightCol
)
6774 oldRight
= rightCol
;
6777 if (oldBottom
< bottomRow
)
6780 oldBottom
= bottomRow
;
6784 // Now, either the stuff marked old is the outer
6785 // rectangle or we don't have a situation where one
6786 // is contained in the other.
6788 if ( oldLeft
< leftCol
)
6790 // Refresh the newly selected or deselected
6791 // area to the left of the old or new selection.
6792 need_refresh
[0] = true;
6793 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6795 wxGridCellCoords ( oldBottom
,
6799 if ( oldTop
< topRow
)
6801 // Refresh the newly selected or deselected
6802 // area above the old or new selection.
6803 need_refresh
[1] = true;
6804 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6806 wxGridCellCoords ( topRow
- 1,
6810 if ( oldRight
> rightCol
)
6812 // Refresh the newly selected or deselected
6813 // area to the right of the old or new selection.
6814 need_refresh
[2] = true;
6815 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6817 wxGridCellCoords ( oldBottom
,
6821 if ( oldBottom
> bottomRow
)
6823 // Refresh the newly selected or deselected
6824 // area below the old or new selection.
6825 need_refresh
[3] = true;
6826 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6828 wxGridCellCoords ( oldBottom
,
6832 // various Refresh() calls
6833 for (i
= 0; i
< 4; i
++ )
6834 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6835 m_gridWin
->Refresh( false, &(rect
[i
]) );
6838 m_selectingTopLeft
= updateTopLeft
;
6839 m_selectingBottomRight
= updateBottomRight
;
6843 // ------ functions to get/send data (see also public functions)
6846 bool wxGrid::GetModelValues()
6848 // Hide the editor, so it won't hide a changed value.
6849 HideCellEditControl();
6853 // all we need to do is repaint the grid
6855 m_gridWin
->Refresh();
6863 bool wxGrid::SetModelValues()
6867 // Disable the editor, so it won't hide a changed value.
6868 // Do we also want to save the current value of the editor first?
6870 DisableCellEditControl();
6874 for ( row
= 0; row
< m_numRows
; row
++ )
6876 for ( col
= 0; col
< m_numCols
; col
++ )
6878 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6890 // Note - this function only draws cells that are in the list of
6891 // exposed cells (usually set from the update region by
6892 // CalcExposedCells)
6894 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6896 if ( !m_numRows
|| !m_numCols
)
6899 int i
, numCells
= cells
.GetCount();
6900 int row
, col
, cell_rows
, cell_cols
;
6901 wxGridCellCoordsArray redrawCells
;
6903 for ( i
= numCells
-1; i
>= 0; i
-- )
6905 row
= cells
[i
].GetRow();
6906 col
= cells
[i
].GetCol();
6907 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6909 // If this cell is part of a multicell block, find owner for repaint
6910 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6912 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6913 bool marked
= false;
6914 for ( int j
= 0; j
< numCells
; j
++ )
6916 if ( cell
== cells
[j
] )
6924 int count
= redrawCells
.GetCount();
6925 for (int j
= 0; j
< count
; j
++)
6927 if ( cell
== redrawCells
[j
] )
6934 redrawCells
.Add( cell
);
6936 continue; // don't bother drawing this cell
6939 // If this cell is empty, find cell to left that might want to overflow
6940 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6942 for ( int l
= 0; l
< cell_rows
; l
++ )
6944 // find a cell in this row to left alreay marked for repaint
6946 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6947 if ((redrawCells
[k
].GetCol() < left
) &&
6948 (redrawCells
[k
].GetRow() == row
))
6949 left
= redrawCells
[k
].GetCol();
6952 left
= 0; // oh well
6954 for (int j
= col
-1; j
>= left
; j
--)
6956 if (!m_table
->IsEmptyCell(row
+l
, j
))
6958 if (GetCellOverflow(row
+l
, j
))
6960 wxGridCellCoords
cell(row
+l
, j
);
6961 bool marked
= false;
6963 for (int k
= 0; k
< numCells
; k
++)
6965 if ( cell
== cells
[k
] )
6974 int count
= redrawCells
.GetCount();
6975 for (int k
= 0; k
< count
; k
++)
6977 if ( cell
== redrawCells
[k
] )
6984 redrawCells
.Add( cell
);
6993 DrawCell( dc
, cells
[i
] );
6996 numCells
= redrawCells
.GetCount();
6998 for ( i
= numCells
- 1; i
>= 0; i
-- )
7000 DrawCell( dc
, redrawCells
[i
] );
7005 void wxGrid::DrawGridSpace( wxDC
& dc
)
7008 m_gridWin
->GetClientSize( &cw
, &ch
);
7011 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7013 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
7014 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
7016 if ( right
> rightCol
|| bottom
> bottomRow
)
7019 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7021 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7022 dc
.SetPen( *wxTRANSPARENT_PEN
);
7024 if ( right
> rightCol
)
7026 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7029 if ( bottom
> bottomRow
)
7031 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7037 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7039 int row
= coords
.GetRow();
7040 int col
= coords
.GetCol();
7042 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7045 // we draw the cell border ourselves
7046 #if !WXGRID_DRAW_LINES
7047 if ( m_gridLinesEnabled
)
7048 DrawCellBorder( dc
, coords
);
7051 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7053 bool isCurrent
= coords
== m_currentCellCoords
;
7055 wxRect rect
= CellToRect( row
, col
);
7057 // if the editor is shown, we should use it and not the renderer
7058 // Note: However, only if it is really _shown_, i.e. not hidden!
7059 if ( isCurrent
&& IsCellEditControlShown() )
7061 // OSAF NB: this "#if..." is temporary and fixes a problem where the
7062 // edit control is erased by this code after being rendered.
7063 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7064 // implicitly, causing this out-of order render.
7065 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7066 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7067 editor
->PaintBackground(rect
, attr
);
7073 // but all the rest is drawn by the cell renderer and hence may be
7075 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7076 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7083 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7085 int row
= m_currentCellCoords
.GetRow();
7086 int col
= m_currentCellCoords
.GetCol();
7088 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7091 wxRect rect
= CellToRect(row
, col
);
7093 // hmmm... what could we do here to show that the cell is disabled?
7094 // for now, I just draw a thinner border than for the other ones, but
7095 // it doesn't look really good
7097 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7101 // The center of th drawn line is where the position/width/height of
7102 // the rectangle is actually at, (on wxMSW atr least,) so we will
7103 // reduce the size of the rectangle to compensate for the thickness of
7104 // the line. If this is too strange on non wxMSW platforms then
7105 // please #ifdef this appropriately.
7106 rect
.x
+= penWidth
/ 2;
7107 rect
.y
+= penWidth
/ 2;
7108 rect
.width
-= penWidth
- 1;
7109 rect
.height
-= penWidth
- 1;
7112 // Now draw the rectangle
7113 // use the cellHighlightColour if the cell is inside a selection, this
7114 // will ensure the cell is always visible.
7115 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7116 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7117 dc
.DrawRectangle(rect
);
7121 // VZ: my experiments with 3d borders...
7123 // how to properly set colours for arbitrary bg?
7124 wxCoord x1
= rect
.x
,
7126 x2
= rect
.x
+ rect
.width
- 1,
7127 y2
= rect
.y
+ rect
.height
- 1;
7129 dc
.SetPen(*wxWHITE_PEN
);
7130 dc
.DrawLine(x1
, y1
, x2
, y1
);
7131 dc
.DrawLine(x1
, y1
, x1
, y2
);
7133 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7134 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7136 dc
.SetPen(*wxBLACK_PEN
);
7137 dc
.DrawLine(x1
, y2
, x2
, y2
);
7138 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7143 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7145 int row
= coords
.GetRow();
7146 int col
= coords
.GetCol();
7147 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7150 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7152 wxRect rect
= CellToRect( row
, col
);
7154 // right hand border
7156 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7157 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7161 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7162 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7165 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7167 // This if block was previously in wxGrid::OnPaint but that doesn't
7168 // seem to get called under wxGTK - MB
7170 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7171 m_numRows
&& m_numCols
)
7173 m_currentCellCoords
.Set(0, 0);
7176 if ( IsCellEditControlShown() )
7178 // don't show highlight when the edit control is shown
7182 // if the active cell was repainted, repaint its highlight too because it
7183 // might have been damaged by the grid lines
7184 size_t count
= cells
.GetCount();
7185 for ( size_t n
= 0; n
< count
; n
++ )
7187 if ( cells
[n
] == m_currentCellCoords
)
7189 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7190 DrawCellHighlight(dc
, attr
);
7198 // TODO: remove this ???
7199 // This is used to redraw all grid lines e.g. when the grid line colour
7202 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7204 #if !WXGRID_DRAW_LINES
7208 if ( !m_gridLinesEnabled
||
7213 int top
, bottom
, left
, right
;
7215 #if 0 //#ifndef __WXGTK__
7219 m_gridWin
->GetClientSize(&cw
, &ch
);
7221 // virtual coords of visible area
7223 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7224 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7229 reg
.GetBox(x
, y
, w
, h
);
7230 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7231 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7235 m_gridWin
->GetClientSize(&cw
, &ch
);
7236 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7237 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7240 // avoid drawing grid lines past the last row and col
7242 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7243 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7245 // no gridlines inside multicells, clip them out
7246 int leftCol
= internalXToCol(left
);
7247 int topRow
= internalYToRow(top
);
7248 int rightCol
= internalXToCol(right
);
7249 int bottomRow
= internalYToRow(bottom
);
7252 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7253 wxRegion
clippedcells(0, 0, cw
, ch
);
7255 int i
, j
, cell_rows
, cell_cols
;
7258 for (j
=topRow
; j
<bottomRow
; j
++)
7260 for (i
=leftCol
; i
<rightCol
; i
++)
7262 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7263 if ((cell_rows
> 1) || (cell_cols
> 1))
7265 rect
= CellToRect(j
,i
);
7266 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7267 clippedcells
.Subtract(rect
);
7269 else if ((cell_rows
< 0) || (cell_cols
< 0))
7271 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7272 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7273 clippedcells
.Subtract(rect
);
7278 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7280 int i
, j
, cell_rows
, cell_cols
;
7283 for (j
=topRow
; j
<bottomRow
; j
++)
7285 for (i
=leftCol
; i
<rightCol
; i
++)
7287 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7288 if ((cell_rows
> 1) || (cell_cols
> 1))
7290 rect
= CellToRect(j
,i
);
7291 clippedcells
.Subtract(rect
);
7293 else if ((cell_rows
< 0) || (cell_cols
< 0))
7295 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7296 clippedcells
.Subtract(rect
);
7301 dc
.SetClippingRegion( clippedcells
);
7303 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7305 // horizontal grid lines
7307 // already declared above - int i;
7308 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7310 int bot
= GetRowBottom(i
) - 1;
7319 dc
.DrawLine( left
, bot
, right
, bot
);
7323 // vertical grid lines
7325 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7327 int colRight
= GetColRight(i
) - 1;
7328 if ( colRight
> right
)
7333 if ( colRight
>= left
)
7335 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7338 dc
.DestroyClippingRegion();
7341 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7347 size_t numLabels
= rows
.GetCount();
7349 for ( i
= 0; i
< numLabels
; i
++ )
7351 DrawRowLabel( dc
, rows
[i
] );
7355 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7357 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7363 rect
.SetY( GetRowTop(row
) + 1 );
7364 rect
.SetWidth( m_rowLabelWidth
- 2 );
7365 rect
.SetHeight( GetRowHeight(row
) - 2 );
7367 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7369 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7371 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7373 int rowTop
= GetRowTop(row
),
7374 rowBottom
= GetRowBottom(row
) - 1;
7376 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7377 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7378 m_rowLabelWidth
-1, rowBottom
);
7380 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7382 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7384 dc
.SetPen( *wxWHITE_PEN
);
7385 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7386 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7388 dc
.SetBackgroundMode( wxTRANSPARENT
);
7389 dc
.SetTextForeground( GetLabelTextColour() );
7390 dc
.SetFont( GetLabelFont() );
7393 GetRowLabelAlignment( &hAlign
, &vAlign
);
7396 rect
.SetY( GetRowTop(row
) + 2 );
7397 rect
.SetWidth( m_rowLabelWidth
- 4 );
7398 rect
.SetHeight( GetRowHeight(row
) - 4 );
7399 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7402 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7408 size_t numLabels
= cols
.GetCount();
7410 for ( i
= 0; i
< numLabels
; i
++ )
7412 DrawColLabel( dc
, cols
[i
] );
7416 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7418 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7421 int colLeft
= GetColLeft(col
);
7425 rect
.SetX( colLeft
+ 1 );
7427 rect
.SetWidth( GetColWidth(col
) - 2 );
7428 rect
.SetHeight( m_colLabelHeight
- 2 );
7430 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7432 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7434 int colRight
= GetColRight(col
) - 1;
7436 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7437 dc
.DrawLine( colRight
, 0,
7438 colRight
, m_colLabelHeight
-1 );
7440 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7442 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7443 colRight
+1, m_colLabelHeight
-1 );
7445 dc
.SetPen( *wxWHITE_PEN
);
7446 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7447 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7449 dc
.SetBackgroundMode( wxTRANSPARENT
);
7450 dc
.SetTextForeground( GetLabelTextColour() );
7451 dc
.SetFont( GetLabelFont() );
7453 int hAlign
, vAlign
, orient
;
7454 GetColLabelAlignment( &hAlign
, &vAlign
);
7455 orient
= GetColLabelTextOrientation();
7457 rect
.SetX( colLeft
+ 2 );
7459 rect
.SetWidth( GetColWidth(col
) - 4 );
7460 rect
.SetHeight( m_colLabelHeight
- 4 );
7461 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7464 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7465 const wxString
& value
,
7469 int textOrientation
)
7471 wxArrayString lines
;
7473 StringToLines( value
, lines
);
7475 //Forward to new API.
7476 DrawTextRectangle( dc
,
7485 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7486 const wxArrayString
& lines
,
7490 int textOrientation
)
7492 long textWidth
= 0, textHeight
= 0;
7493 long lineWidth
= 0, lineHeight
= 0;
7496 dc
.SetClippingRegion( rect
);
7498 nLines
= lines
.GetCount();
7502 float x
= 0.0, y
= 0.0;
7504 if ( textOrientation
== wxHORIZONTAL
)
7505 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7507 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7509 switch ( vertAlign
)
7511 case wxALIGN_BOTTOM
:
7512 if ( textOrientation
== wxHORIZONTAL
)
7513 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7515 x
= rect
.x
+ rect
.width
- textWidth
;
7518 case wxALIGN_CENTRE
:
7519 if ( textOrientation
== wxHORIZONTAL
)
7520 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7522 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7527 if ( textOrientation
== wxHORIZONTAL
)
7534 // Align each line of a multi-line label
7535 for ( l
= 0; l
< nLines
; l
++ )
7537 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7539 switch ( horizAlign
)
7542 if ( textOrientation
== wxHORIZONTAL
)
7543 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7545 y
= rect
.y
+ lineWidth
+ 1;
7548 case wxALIGN_CENTRE
:
7549 if ( textOrientation
== wxHORIZONTAL
)
7550 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7552 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7557 if ( textOrientation
== wxHORIZONTAL
)
7560 y
= rect
.y
+ rect
.height
- 1;
7564 if ( textOrientation
== wxHORIZONTAL
)
7566 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7571 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7577 dc
.DestroyClippingRegion();
7581 // Split multi line text up into an array of strings. Any existing
7582 // contents of the string array are preserved.
7584 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7588 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7589 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7591 while ( startPos
< (int)tVal
.Length() )
7593 pos
= tVal
.Mid(startPos
).Find( eol
);
7598 else if ( pos
== 0 )
7600 lines
.Add( wxEmptyString
);
7604 lines
.Add( value
.Mid(startPos
, pos
) );
7606 startPos
+= pos
+ 1;
7609 if ( startPos
< (int)value
.Length() )
7611 lines
.Add( value
.Mid( startPos
) );
7616 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
7617 const wxArrayString
& lines
,
7618 long *width
, long *height
)
7622 long lineW
= 0, lineH
= 0;
7625 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7627 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7628 w
= wxMax( w
, lineW
);
7637 // ------ Batch processing.
7639 void wxGrid::EndBatch()
7641 if ( m_batchCount
> 0 )
7644 if ( !m_batchCount
)
7647 m_rowLabelWin
->Refresh();
7648 m_colLabelWin
->Refresh();
7649 m_cornerLabelWin
->Refresh();
7650 m_gridWin
->Refresh();
7655 // Use this, rather than wxWindow::Refresh(), to force an immediate
7656 // repainting of the grid. Has no effect if you are already inside a
7657 // BeginBatch / EndBatch block.
7659 void wxGrid::ForceRefresh()
7665 bool wxGrid::Enable(bool enable
)
7667 if ( !wxScrolledWindow::Enable(enable
) )
7670 // redraw in the new state
7671 m_gridWin
->Refresh();
7678 // ------ Edit control functions
7681 void wxGrid::EnableEditing( bool edit
)
7683 // TODO: improve this ?
7685 if ( edit
!= m_editable
)
7688 EnableCellEditControl(edit
);
7693 void wxGrid::EnableCellEditControl( bool enable
)
7698 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7699 SetCurrentCell( 0, 0 );
7701 if ( enable
!= m_cellEditCtrlEnabled
)
7705 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7708 // this should be checked by the caller!
7709 wxASSERT_MSG( CanEnableCellControl(),
7710 _T("can't enable editing for this cell!") );
7712 // do it before ShowCellEditControl()
7713 m_cellEditCtrlEnabled
= enable
;
7715 ShowCellEditControl();
7719 //FIXME:add veto support
7720 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7722 HideCellEditControl();
7723 SaveEditControlValue();
7725 // do it after HideCellEditControl()
7726 m_cellEditCtrlEnabled
= enable
;
7731 bool wxGrid::IsCurrentCellReadOnly() const
7734 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7735 bool readonly
= attr
->IsReadOnly();
7741 bool wxGrid::CanEnableCellControl() const
7743 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7744 !IsCurrentCellReadOnly();
7748 bool wxGrid::IsCellEditControlEnabled() const
7750 // the cell edit control might be disable for all cells or just for the
7751 // current one if it's read only
7752 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7755 bool wxGrid::IsCellEditControlShown() const
7757 bool isShown
= false;
7759 if ( m_cellEditCtrlEnabled
)
7761 int row
= m_currentCellCoords
.GetRow();
7762 int col
= m_currentCellCoords
.GetCol();
7763 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7764 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7769 if ( editor
->IsCreated() )
7771 isShown
= editor
->GetControl()->IsShown();
7781 void wxGrid::ShowCellEditControl()
7783 if ( IsCellEditControlEnabled() )
7785 if ( !IsVisible( m_currentCellCoords
) )
7787 m_cellEditCtrlEnabled
= false;
7792 wxRect rect
= CellToRect( m_currentCellCoords
);
7793 int row
= m_currentCellCoords
.GetRow();
7794 int col
= m_currentCellCoords
.GetCol();
7796 // if this is part of a multicell, find owner (topleft)
7797 int cell_rows
, cell_cols
;
7798 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7799 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7803 m_currentCellCoords
.SetRow( row
);
7804 m_currentCellCoords
.SetCol( col
);
7807 // convert to scrolled coords
7809 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7811 // done in PaintBackground()
7813 // erase the highlight and the cell contents because the editor
7814 // might not cover the entire cell
7815 wxClientDC
dc( m_gridWin
);
7817 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7818 dc
.SetPen(*wxTRANSPARENT_PEN
);
7819 dc
.DrawRectangle(rect
);
7822 // cell is shifted by one pixel
7823 // However, don't allow x or y to become negative
7824 // since the SetSize() method interprets that as
7831 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7832 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7833 if ( !editor
->IsCreated() )
7835 editor
->Create(m_gridWin
, wxID_ANY
,
7836 new wxGridCellEditorEvtHandler(this, editor
));
7838 wxGridEditorCreatedEvent
evt(GetId(),
7839 wxEVT_GRID_EDITOR_CREATED
,
7843 editor
->GetControl());
7844 GetEventHandler()->ProcessEvent(evt
);
7848 // resize editor to overflow into righthand cells if allowed
7849 int maxWidth
= rect
.width
;
7850 wxString value
= GetCellValue(row
, col
);
7851 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7854 GetTextExtent(value
, &maxWidth
, &y
,
7855 NULL
, NULL
, &attr
->GetFont());
7856 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7859 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7860 if (rect
.x
+maxWidth
> client_right
)
7861 maxWidth
= client_right
- rect
.x
;
7863 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7865 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7866 // may have changed earlier
7867 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7870 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7871 // looks weird going over a multicell
7872 if (m_table
->IsEmptyCell(row
,i
) &&
7873 (rect
.width
< maxWidth
) && (c_rows
== 1))
7874 rect
.width
+= GetColWidth(i
);
7879 if (rect
.GetRight() > client_right
)
7880 rect
.SetRight(client_right
- 1);
7883 editor
->SetCellAttr(attr
);
7884 editor
->SetSize( rect
);
7885 editor
->Show( true, attr
);
7887 // recalc dimensions in case we need to
7888 // expand the scrolled window to account for editor
7891 editor
->BeginEdit(row
, col
, this);
7892 editor
->SetCellAttr(NULL
);
7900 void wxGrid::HideCellEditControl()
7902 if ( IsCellEditControlEnabled() )
7904 int row
= m_currentCellCoords
.GetRow();
7905 int col
= m_currentCellCoords
.GetCol();
7907 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7908 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7909 editor
->Show( false );
7913 m_gridWin
->SetFocus();
7915 // refresh whole row to the right
7916 wxRect
rect( CellToRect(row
, col
) );
7917 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7918 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7920 // ensure that the pixels under the focus ring get refreshed as well
7921 rect
.Inflate(10, 10);
7923 m_gridWin
->Refresh( false, &rect
);
7927 void wxGrid::SaveEditControlValue()
7929 if ( IsCellEditControlEnabled() )
7931 int row
= m_currentCellCoords
.GetRow();
7932 int col
= m_currentCellCoords
.GetCol();
7934 wxString oldval
= GetCellValue(row
, col
);
7936 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7937 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7938 bool changed
= editor
->EndEdit(row
, col
, this);
7945 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7946 m_currentCellCoords
.GetRow(),
7947 m_currentCellCoords
.GetCol() ) < 0 )
7949 // Event has been vetoed, set the data back.
7950 SetCellValue(row
, col
, oldval
);
7958 // ------ Grid location functions
7959 // Note that all of these functions work with the logical coordinates of
7960 // grid cells and labels so you will need to convert from device
7961 // coordinates for mouse events etc.
7964 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7966 int row
= YToRow(y
);
7967 int col
= XToCol(x
);
7969 if ( row
== -1 || col
== -1 )
7971 coords
= wxGridNoCellCoords
;
7975 coords
.Set( row
, col
);
7980 // Internal Helper function for computing row or column from some
7981 // (unscrolled) coordinate value, using either
7982 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7983 // of m_rowBottoms/m_ColRights to speed up the search!
7985 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7986 const wxArrayInt
& BorderArray
, int nMax
,
7990 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7995 size_t i_max
= coord
/ defaultDist
,
7998 if (BorderArray
.IsEmpty())
8000 if ((int) i_max
< nMax
)
8002 return clipToMinMax
? nMax
- 1 : -1;
8005 if ( i_max
>= BorderArray
.GetCount())
8006 i_max
= BorderArray
.GetCount() - 1;
8009 if ( coord
>= BorderArray
[i_max
])
8013 i_max
= coord
/ minDist
;
8015 i_max
= BorderArray
.GetCount() - 1;
8018 if ( i_max
>= BorderArray
.GetCount())
8019 i_max
= BorderArray
.GetCount() - 1;
8022 if ( coord
>= BorderArray
[i_max
])
8023 return clipToMinMax
? (int)i_max
: -1;
8024 if ( coord
< BorderArray
[0] )
8027 while ( i_max
- i_min
> 0 )
8029 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8030 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8031 if (coord
>= BorderArray
[ i_max
- 1])
8035 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8036 if (coord
< BorderArray
[median
])
8045 int wxGrid::YToRow( int y
)
8047 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8048 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8051 int wxGrid::XToCol( int x
)
8053 return CoordToRowOrCol(x
, m_defaultColWidth
,
8054 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8057 // return the row number that that the y coord is near the edge of, or
8058 // -1 if not near an edge
8060 int wxGrid::YToEdgeOfRow( int y
)
8063 i
= internalYToRow(y
);
8065 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8067 // We know that we are in row i, test whether we are
8068 // close enough to lower or upper border, respectively.
8069 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8071 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8078 // return the col number that that the x coord is near the edge of, or
8079 // -1 if not near an edge
8081 int wxGrid::XToEdgeOfCol( int x
)
8084 i
= internalXToCol(x
);
8086 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8088 // We know that we are in column i, test whether we are
8089 // close enough to right or left border, respectively.
8090 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8092 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8099 wxRect
wxGrid::CellToRect( int row
, int col
)
8101 wxRect
rect( -1, -1, -1, -1 );
8103 if ( row
>= 0 && row
< m_numRows
&&
8104 col
>= 0 && col
< m_numCols
)
8106 int i
, cell_rows
, cell_cols
;
8107 rect
.width
= rect
.height
= 0;
8108 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8109 // if negative then find multicell owner
8110 if (cell_rows
< 0) row
+= cell_rows
;
8111 if (cell_cols
< 0) col
+= cell_cols
;
8112 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8114 rect
.x
= GetColLeft(col
);
8115 rect
.y
= GetRowTop(row
);
8116 for (i
=col
; i
<col
+cell_cols
; i
++)
8117 rect
.width
+= GetColWidth(i
);
8118 for (i
=row
; i
<row
+cell_rows
; i
++)
8119 rect
.height
+= GetRowHeight(i
);
8122 // if grid lines are enabled, then the area of the cell is a bit smaller
8123 if (m_gridLinesEnabled
)
8132 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8134 // get the cell rectangle in logical coords
8136 wxRect
r( CellToRect( row
, col
) );
8138 // convert to device coords
8140 int left
, top
, right
, bottom
;
8141 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8142 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8144 // check against the client area of the grid window
8147 m_gridWin
->GetClientSize( &cw
, &ch
);
8149 if ( wholeCellVisible
)
8151 // is the cell wholly visible ?
8153 return ( left
>= 0 && right
<= cw
&&
8154 top
>= 0 && bottom
<= ch
);
8158 // is the cell partly visible ?
8160 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8161 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8165 // make the specified cell location visible by doing a minimal amount
8168 void wxGrid::MakeCellVisible( int row
, int col
)
8171 int xpos
= -1, ypos
= -1;
8173 if ( row
>= 0 && row
< m_numRows
&&
8174 col
>= 0 && col
< m_numCols
)
8176 // get the cell rectangle in logical coords
8178 wxRect
r( CellToRect( row
, col
) );
8180 // convert to device coords
8182 int left
, top
, right
, bottom
;
8183 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8184 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8187 m_gridWin
->GetClientSize( &cw
, &ch
);
8193 else if ( bottom
> ch
)
8195 int h
= r
.GetHeight();
8197 for ( i
= row
-1; i
>= 0; i
-- )
8199 int rowHeight
= GetRowHeight(i
);
8200 if ( h
+ rowHeight
> ch
)
8207 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8208 // have rounding errors (this is important, because if we do, we
8209 // might not scroll at all and some cells won't be redrawn)
8211 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8213 ypos
+= m_scrollLineY
;
8220 else if ( right
> cw
)
8222 // position the view so that the cell is on the right
8224 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8225 xpos
= x0
+ (right
- cw
);
8227 // see comment for ypos above
8228 xpos
+= m_scrollLineX
;
8231 if ( xpos
!= -1 || ypos
!= -1 )
8234 xpos
/= m_scrollLineX
;
8236 ypos
/= m_scrollLineY
;
8237 Scroll( xpos
, ypos
);
8245 // ------ Grid cursor movement functions
8248 bool wxGrid::MoveCursorUp( bool expandSelection
)
8250 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8251 m_currentCellCoords
.GetRow() >= 0 )
8253 if ( expandSelection
)
8255 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8256 m_selectingKeyboard
= m_currentCellCoords
;
8257 if ( m_selectingKeyboard
.GetRow() > 0 )
8259 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8260 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8261 m_selectingKeyboard
.GetCol() );
8262 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8265 else if ( m_currentCellCoords
.GetRow() > 0 )
8267 int row
= m_currentCellCoords
.GetRow() - 1;
8268 int col
= m_currentCellCoords
.GetCol();
8270 MakeCellVisible( row
, col
);
8271 SetCurrentCell( row
, col
);
8283 bool wxGrid::MoveCursorDown( bool expandSelection
)
8285 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8286 m_currentCellCoords
.GetRow() < m_numRows
)
8288 if ( expandSelection
)
8290 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8291 m_selectingKeyboard
= m_currentCellCoords
;
8292 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8294 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8295 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8296 m_selectingKeyboard
.GetCol() );
8297 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8300 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8302 int row
= m_currentCellCoords
.GetRow() + 1;
8303 int col
= m_currentCellCoords
.GetCol();
8305 MakeCellVisible( row
, col
);
8306 SetCurrentCell( row
, col
);
8317 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8319 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8320 m_currentCellCoords
.GetCol() >= 0 )
8322 if ( expandSelection
)
8324 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8325 m_selectingKeyboard
= m_currentCellCoords
;
8326 if ( m_selectingKeyboard
.GetCol() > 0 )
8328 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8329 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8330 m_selectingKeyboard
.GetCol() );
8331 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8334 else if ( m_currentCellCoords
.GetCol() > 0 )
8336 int row
= m_currentCellCoords
.GetRow();
8337 int col
= m_currentCellCoords
.GetCol() - 1;
8339 MakeCellVisible( row
, col
);
8340 SetCurrentCell( row
, col
);
8351 bool wxGrid::MoveCursorRight( bool expandSelection
)
8353 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8354 m_currentCellCoords
.GetCol() < m_numCols
)
8356 if ( expandSelection
)
8358 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8359 m_selectingKeyboard
= m_currentCellCoords
;
8360 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8362 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8363 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8364 m_selectingKeyboard
.GetCol() );
8365 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8368 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8370 int row
= m_currentCellCoords
.GetRow();
8371 int col
= m_currentCellCoords
.GetCol() + 1;
8373 MakeCellVisible( row
, col
);
8374 SetCurrentCell( row
, col
);
8385 bool wxGrid::MovePageUp()
8387 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8390 int row
= m_currentCellCoords
.GetRow();
8394 m_gridWin
->GetClientSize( &cw
, &ch
);
8396 int y
= GetRowTop(row
);
8397 int newRow
= internalYToRow( y
- ch
+ 1 );
8399 if ( newRow
== row
)
8401 //row > 0 , so newrow can never be less than 0 here.
8405 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8406 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8414 bool wxGrid::MovePageDown()
8416 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8419 int row
= m_currentCellCoords
.GetRow();
8420 if ( (row
+1) < m_numRows
)
8423 m_gridWin
->GetClientSize( &cw
, &ch
);
8425 int y
= GetRowTop(row
);
8426 int newRow
= internalYToRow( y
+ ch
);
8427 if ( newRow
== row
)
8429 // row < m_numRows , so newrow can't overflow here.
8433 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8434 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8442 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8445 m_currentCellCoords
!= wxGridNoCellCoords
&&
8446 m_currentCellCoords
.GetRow() > 0 )
8448 int row
= m_currentCellCoords
.GetRow();
8449 int col
= m_currentCellCoords
.GetCol();
8451 if ( m_table
->IsEmptyCell(row
, col
) )
8453 // starting in an empty cell: find the next block of
8459 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8463 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8465 // starting at the top of a block: find the next block
8471 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8477 // starting within a block: find the top of the block
8482 if ( m_table
->IsEmptyCell(row
, col
) )
8490 MakeCellVisible( row
, col
);
8491 if ( expandSelection
)
8493 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8494 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8499 SetCurrentCell( row
, col
);
8508 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8511 m_currentCellCoords
!= wxGridNoCellCoords
&&
8512 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8514 int row
= m_currentCellCoords
.GetRow();
8515 int col
= m_currentCellCoords
.GetCol();
8517 if ( m_table
->IsEmptyCell(row
, col
) )
8519 // starting in an empty cell: find the next block of
8522 while ( row
< m_numRows
-1 )
8525 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8529 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8531 // starting at the bottom of a block: find the next block
8534 while ( row
< m_numRows
-1 )
8537 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8543 // starting within a block: find the bottom of the block
8545 while ( row
< m_numRows
-1 )
8548 if ( m_table
->IsEmptyCell(row
, col
) )
8556 MakeCellVisible( row
, col
);
8557 if ( expandSelection
)
8559 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8560 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8565 SetCurrentCell( row
, col
);
8574 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8577 m_currentCellCoords
!= wxGridNoCellCoords
&&
8578 m_currentCellCoords
.GetCol() > 0 )
8580 int row
= m_currentCellCoords
.GetRow();
8581 int col
= m_currentCellCoords
.GetCol();
8583 if ( m_table
->IsEmptyCell(row
, col
) )
8585 // starting in an empty cell: find the next block of
8591 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8595 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8597 // starting at the left of a block: find the next block
8603 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8609 // starting within a block: find the left of the block
8614 if ( m_table
->IsEmptyCell(row
, col
) )
8622 MakeCellVisible( row
, col
);
8623 if ( expandSelection
)
8625 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8626 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8631 SetCurrentCell( row
, col
);
8640 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8643 m_currentCellCoords
!= wxGridNoCellCoords
&&
8644 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8646 int row
= m_currentCellCoords
.GetRow();
8647 int col
= m_currentCellCoords
.GetCol();
8649 if ( m_table
->IsEmptyCell(row
, col
) )
8651 // starting in an empty cell: find the next block of
8654 while ( col
< m_numCols
-1 )
8657 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8661 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8663 // starting at the right of a block: find the next block
8666 while ( col
< m_numCols
-1 )
8669 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8675 // starting within a block: find the right of the block
8677 while ( col
< m_numCols
-1 )
8680 if ( m_table
->IsEmptyCell(row
, col
) )
8688 MakeCellVisible( row
, col
);
8689 if ( expandSelection
)
8691 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8692 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8697 SetCurrentCell( row
, col
);
8709 // ------ Label values and formatting
8712 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8714 *horiz
= m_rowLabelHorizAlign
;
8715 *vert
= m_rowLabelVertAlign
;
8718 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8720 *horiz
= m_colLabelHorizAlign
;
8721 *vert
= m_colLabelVertAlign
;
8724 int wxGrid::GetColLabelTextOrientation()
8726 return m_colLabelTextOrientation
;
8729 wxString
wxGrid::GetRowLabelValue( int row
)
8733 return m_table
->GetRowLabelValue( row
);
8743 wxString
wxGrid::GetColLabelValue( int col
)
8747 return m_table
->GetColLabelValue( col
);
8758 void wxGrid::SetRowLabelSize( int width
)
8760 width
= wxMax( width
, 0 );
8761 if ( width
!= m_rowLabelWidth
)
8765 m_rowLabelWin
->Show( false );
8766 m_cornerLabelWin
->Show( false );
8768 else if ( m_rowLabelWidth
== 0 )
8770 m_rowLabelWin
->Show( true );
8771 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8774 m_rowLabelWidth
= width
;
8776 wxScrolledWindow::Refresh( true );
8781 void wxGrid::SetColLabelSize( int height
)
8783 height
= wxMax( height
, 0 );
8784 if ( height
!= m_colLabelHeight
)
8788 m_colLabelWin
->Show( false );
8789 m_cornerLabelWin
->Show( false );
8791 else if ( m_colLabelHeight
== 0 )
8793 m_colLabelWin
->Show( true );
8794 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8797 m_colLabelHeight
= height
;
8799 wxScrolledWindow::Refresh( true );
8804 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8806 if ( m_labelBackgroundColour
!= colour
)
8808 m_labelBackgroundColour
= colour
;
8809 m_rowLabelWin
->SetBackgroundColour( colour
);
8810 m_colLabelWin
->SetBackgroundColour( colour
);
8811 m_cornerLabelWin
->SetBackgroundColour( colour
);
8813 if ( !GetBatchCount() )
8815 m_rowLabelWin
->Refresh();
8816 m_colLabelWin
->Refresh();
8817 m_cornerLabelWin
->Refresh();
8822 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8824 if ( m_labelTextColour
!= colour
)
8826 m_labelTextColour
= colour
;
8827 if ( !GetBatchCount() )
8829 m_rowLabelWin
->Refresh();
8830 m_colLabelWin
->Refresh();
8835 void wxGrid::SetLabelFont( const wxFont
& font
)
8838 if ( !GetBatchCount() )
8840 m_rowLabelWin
->Refresh();
8841 m_colLabelWin
->Refresh();
8845 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8847 // allow old (incorrect) defs to be used
8850 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8851 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8852 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8857 case wxTOP
: vert
= wxALIGN_TOP
; break;
8858 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8859 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8862 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8864 m_rowLabelHorizAlign
= horiz
;
8867 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8869 m_rowLabelVertAlign
= vert
;
8872 if ( !GetBatchCount() )
8874 m_rowLabelWin
->Refresh();
8878 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8880 // allow old (incorrect) defs to be used
8883 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8884 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8885 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8890 case wxTOP
: vert
= wxALIGN_TOP
; break;
8891 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8892 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8895 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8897 m_colLabelHorizAlign
= horiz
;
8900 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8902 m_colLabelVertAlign
= vert
;
8905 if ( !GetBatchCount() )
8907 m_colLabelWin
->Refresh();
8911 // Note: under MSW, the default column label font must be changed because it
8912 // does not support vertical printing
8914 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8915 // pGrid->SetLabelFont(font);
8916 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8918 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8920 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8922 m_colLabelTextOrientation
= textOrientation
;
8925 if ( !GetBatchCount() )
8927 m_colLabelWin
->Refresh();
8931 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8935 m_table
->SetRowLabelValue( row
, s
);
8936 if ( !GetBatchCount() )
8938 wxRect rect
= CellToRect( row
, 0);
8939 if ( rect
.height
> 0 )
8941 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8943 rect
.width
= m_rowLabelWidth
;
8944 m_rowLabelWin
->Refresh( true, &rect
);
8950 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8954 m_table
->SetColLabelValue( col
, s
);
8955 if ( !GetBatchCount() )
8957 wxRect rect
= CellToRect( 0, col
);
8958 if ( rect
.width
> 0 )
8960 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8962 rect
.height
= m_colLabelHeight
;
8963 m_colLabelWin
->Refresh( true, &rect
);
8969 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8971 if ( m_gridLineColour
!= colour
)
8973 m_gridLineColour
= colour
;
8975 wxClientDC
dc( m_gridWin
);
8977 DrawAllGridLines( dc
, wxRegion() );
8982 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8984 if ( m_cellHighlightColour
!= colour
)
8986 m_cellHighlightColour
= colour
;
8988 wxClientDC
dc( m_gridWin
);
8990 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8991 DrawCellHighlight(dc
, attr
);
8996 void wxGrid::SetCellHighlightPenWidth(int width
)
8998 if (m_cellHighlightPenWidth
!= width
)
9000 m_cellHighlightPenWidth
= width
;
9002 // Just redrawing the cell highlight is not enough since that won't
9003 // make any visible change if the the thickness is getting smaller.
9004 int row
= m_currentCellCoords
.GetRow();
9005 int col
= m_currentCellCoords
.GetCol();
9006 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9008 wxRect rect
= CellToRect(row
, col
);
9009 m_gridWin
->Refresh(true, &rect
);
9013 void wxGrid::SetCellHighlightROPenWidth(int width
)
9015 if (m_cellHighlightROPenWidth
!= width
)
9017 m_cellHighlightROPenWidth
= width
;
9019 // Just redrawing the cell highlight is not enough since that won't
9020 // make any visible change if the the thickness is getting smaller.
9021 int row
= m_currentCellCoords
.GetRow();
9022 int col
= m_currentCellCoords
.GetCol();
9023 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9025 wxRect rect
= CellToRect(row
, col
);
9026 m_gridWin
->Refresh(true, &rect
);
9030 void wxGrid::EnableGridLines( bool enable
)
9032 if ( enable
!= m_gridLinesEnabled
)
9034 m_gridLinesEnabled
= enable
;
9036 if ( !GetBatchCount() )
9040 wxClientDC
dc( m_gridWin
);
9042 DrawAllGridLines( dc
, wxRegion() );
9046 m_gridWin
->Refresh();
9053 int wxGrid::GetDefaultRowSize()
9055 return m_defaultRowHeight
;
9058 int wxGrid::GetRowSize( int row
)
9060 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9062 return GetRowHeight(row
);
9065 int wxGrid::GetDefaultColSize()
9067 return m_defaultColWidth
;
9070 int wxGrid::GetColSize( int col
)
9072 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9074 return GetColWidth(col
);
9077 // ============================================================================
9078 // access to the grid attributes: each of them has a default value in the grid
9079 // itself and may be overidden on a per-cell basis
9080 // ============================================================================
9082 // ----------------------------------------------------------------------------
9083 // setting default attributes
9084 // ----------------------------------------------------------------------------
9086 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9088 m_defaultCellAttr
->SetBackgroundColour(col
);
9090 m_gridWin
->SetBackgroundColour(col
);
9094 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9096 m_defaultCellAttr
->SetTextColour(col
);
9099 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9101 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9104 void wxGrid::SetDefaultCellOverflow( bool allow
)
9106 m_defaultCellAttr
->SetOverflow(allow
);
9109 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9111 m_defaultCellAttr
->SetFont(font
);
9115 // For editors and renderers the type registry takes precedence over the
9116 // default attr, so we need to register the new editor/renderer for the string
9117 // data type in order to make setting a default editor/renderer appear to
9120 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9122 RegisterDataType(wxGRID_VALUE_STRING
,
9124 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9127 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9129 RegisterDataType(wxGRID_VALUE_STRING
,
9130 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9134 // ----------------------------------------------------------------------------
9135 // access to the default attrbiutes
9136 // ----------------------------------------------------------------------------
9138 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9140 return m_defaultCellAttr
->GetBackgroundColour();
9143 wxColour
wxGrid::GetDefaultCellTextColour()
9145 return m_defaultCellAttr
->GetTextColour();
9148 wxFont
wxGrid::GetDefaultCellFont()
9150 return m_defaultCellAttr
->GetFont();
9153 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9155 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9158 bool wxGrid::GetDefaultCellOverflow()
9160 return m_defaultCellAttr
->GetOverflow();
9163 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9165 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9168 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9170 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9173 // ----------------------------------------------------------------------------
9174 // access to cell attributes
9175 // ----------------------------------------------------------------------------
9177 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9179 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9180 wxColour colour
= attr
->GetBackgroundColour();
9185 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9187 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9188 wxColour colour
= attr
->GetTextColour();
9193 wxFont
wxGrid::GetCellFont( int row
, int col
)
9195 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9196 wxFont font
= attr
->GetFont();
9201 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9203 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9204 attr
->GetAlignment(horiz
, vert
);
9208 bool wxGrid::GetCellOverflow( int row
, int col
)
9210 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9211 bool allow
= attr
->GetOverflow();
9217 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9219 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9220 attr
->GetSize( num_rows
, num_cols
);
9224 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9226 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9227 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9233 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9235 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9236 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9242 bool wxGrid::IsReadOnly(int row
, int col
) const
9244 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9245 bool isReadOnly
= attr
->IsReadOnly();
9251 // ----------------------------------------------------------------------------
9252 // attribute support: cache, automatic provider creation, ...
9253 // ----------------------------------------------------------------------------
9255 bool wxGrid::CanHaveAttributes()
9262 return m_table
->CanHaveAttributes();
9265 void wxGrid::ClearAttrCache()
9267 if ( m_attrCache
.row
!= -1 )
9269 wxSafeDecRef(m_attrCache
.attr
);
9270 m_attrCache
.attr
= NULL
;
9271 m_attrCache
.row
= -1;
9275 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9279 wxGrid
*self
= (wxGrid
*)this; // const_cast
9281 self
->ClearAttrCache();
9282 self
->m_attrCache
.row
= row
;
9283 self
->m_attrCache
.col
= col
;
9284 self
->m_attrCache
.attr
= attr
;
9289 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9291 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9293 *attr
= m_attrCache
.attr
;
9294 wxSafeIncRef(m_attrCache
.attr
);
9296 #ifdef DEBUG_ATTR_CACHE
9297 gs_nAttrCacheHits
++;
9304 #ifdef DEBUG_ATTR_CACHE
9305 gs_nAttrCacheMisses
++;
9312 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9314 wxGridCellAttr
*attr
= NULL
;
9315 // Additional test to avoid looking at the cache e.g. for
9316 // wxNoCellCoords, as this will confuse memory management.
9319 if ( !LookupAttr(row
, col
, &attr
) )
9321 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9322 : (wxGridCellAttr
*)NULL
;
9323 CacheAttr(row
, col
, attr
);
9329 attr
->SetDefAttr(m_defaultCellAttr
);
9333 attr
= m_defaultCellAttr
;
9340 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9342 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9343 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9345 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9346 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9348 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9351 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9353 // artificially inc the ref count to match DecRef() in caller
9355 m_table
->SetAttr(attr
, row
, col
);
9361 // ----------------------------------------------------------------------------
9362 // setting column attributes (wrappers around SetColAttr)
9363 // ----------------------------------------------------------------------------
9365 void wxGrid::SetColFormatBool(int col
)
9367 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9370 void wxGrid::SetColFormatNumber(int col
)
9372 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9375 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9377 wxString typeName
= wxGRID_VALUE_FLOAT
;
9378 if ( (width
!= -1) || (precision
!= -1) )
9380 typeName
<< _T(':') << width
<< _T(',') << precision
;
9383 SetColFormatCustom(col
, typeName
);
9386 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9388 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9390 attr
= new wxGridCellAttr
;
9391 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9392 attr
->SetRenderer(renderer
);
9394 SetColAttr(col
, attr
);
9398 // ----------------------------------------------------------------------------
9399 // setting cell attributes: this is forwarded to the table
9400 // ----------------------------------------------------------------------------
9402 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9404 if ( CanHaveAttributes() )
9406 m_table
->SetAttr(attr
, row
, col
);
9415 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9417 if ( CanHaveAttributes() )
9419 m_table
->SetRowAttr(attr
, row
);
9428 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9430 if ( CanHaveAttributes() )
9432 m_table
->SetColAttr(attr
, col
);
9441 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9443 if ( CanHaveAttributes() )
9445 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9446 attr
->SetBackgroundColour(colour
);
9451 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9453 if ( CanHaveAttributes() )
9455 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9456 attr
->SetTextColour(colour
);
9461 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9463 if ( CanHaveAttributes() )
9465 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9466 attr
->SetFont(font
);
9471 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9473 if ( CanHaveAttributes() )
9475 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9476 attr
->SetAlignment(horiz
, vert
);
9481 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9483 if ( CanHaveAttributes() )
9485 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9486 attr
->SetOverflow(allow
);
9491 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9493 if ( CanHaveAttributes() )
9495 int cell_rows
, cell_cols
;
9497 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9498 attr
->GetSize(&cell_rows
, &cell_cols
);
9499 attr
->SetSize(num_rows
, num_cols
);
9502 // Cannot set the size of a cell to 0 or negative values
9503 // While it is perfectly legal to do that, this function cannot
9504 // handle all the possibilies, do it by hand by getting the CellAttr.
9505 // You can only set the size of a cell to 1,1 or greater with this fn
9506 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9507 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9508 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9509 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9511 // if this was already a multicell then "turn off" the other cells first
9512 if ((cell_rows
> 1) || (cell_rows
> 1))
9515 for (j
=row
; j
<row
+cell_rows
; j
++)
9517 for (i
=col
; i
<col
+cell_cols
; i
++)
9519 if ((i
!= col
) || (j
!= row
))
9521 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9522 attr_stub
->SetSize( 1, 1 );
9523 attr_stub
->DecRef();
9529 // mark the cells that will be covered by this cell to
9530 // negative or zero values to point back at this cell
9531 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9534 for (j
=row
; j
<row
+num_rows
; j
++)
9536 for (i
=col
; i
<col
+num_cols
; i
++)
9538 if ((i
!= col
) || (j
!= row
))
9540 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9541 attr_stub
->SetSize( row
-j
, col
-i
);
9542 attr_stub
->DecRef();
9550 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9552 if ( CanHaveAttributes() )
9554 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9555 attr
->SetRenderer(renderer
);
9560 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9562 if ( CanHaveAttributes() )
9564 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9565 attr
->SetEditor(editor
);
9570 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9572 if ( CanHaveAttributes() )
9574 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9575 attr
->SetReadOnly(isReadOnly
);
9580 // ----------------------------------------------------------------------------
9581 // Data type registration
9582 // ----------------------------------------------------------------------------
9584 void wxGrid::RegisterDataType(const wxString
& typeName
,
9585 wxGridCellRenderer
* renderer
,
9586 wxGridCellEditor
* editor
)
9588 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9592 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9594 wxString typeName
= m_table
->GetTypeName(row
, col
);
9595 return GetDefaultEditorForType(typeName
);
9598 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9600 wxString typeName
= m_table
->GetTypeName(row
, col
);
9601 return GetDefaultRendererForType(typeName
);
9605 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9607 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9608 if ( index
== wxNOT_FOUND
)
9612 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9613 wxFAIL_MSG(errStr
.c_str());
9618 return m_typeRegistry
->GetEditor(index
);
9622 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9624 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9625 if ( index
== wxNOT_FOUND
)
9629 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9630 wxFAIL_MSG(errStr
.c_str());
9635 return m_typeRegistry
->GetRenderer(index
);
9638 // ----------------------------------------------------------------------------
9640 // ----------------------------------------------------------------------------
9642 void wxGrid::EnableDragRowSize( bool enable
)
9644 m_canDragRowSize
= enable
;
9647 void wxGrid::EnableDragColSize( bool enable
)
9649 m_canDragColSize
= enable
;
9652 void wxGrid::EnableDragGridSize( bool enable
)
9654 m_canDragGridSize
= enable
;
9657 void wxGrid::EnableDragCell( bool enable
)
9659 m_canDragCell
= enable
;
9662 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9664 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9666 if ( resizeExistingRows
)
9668 // since we are resizing all rows to the default row size,
9669 // we can simply clear the row heights and row bottoms
9670 // arrays (which also allows us to take advantage of
9671 // some speed optimisations)
9672 m_rowHeights
.Empty();
9673 m_rowBottoms
.Empty();
9674 if ( !GetBatchCount() )
9679 void wxGrid::SetRowSize( int row
, int height
)
9681 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9683 // See comment in SetColSize
9684 if ( height
< GetRowMinimalAcceptableHeight())
9687 if ( m_rowHeights
.IsEmpty() )
9689 // need to really create the array
9693 int h
= wxMax( 0, height
);
9694 int diff
= h
- m_rowHeights
[row
];
9696 m_rowHeights
[row
] = h
;
9698 for ( i
= row
; i
< m_numRows
; i
++ )
9700 m_rowBottoms
[i
] += diff
;
9702 if ( !GetBatchCount() )
9706 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9708 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9710 if ( resizeExistingCols
)
9712 // since we are resizing all columns to the default column size,
9713 // we can simply clear the col widths and col rights
9714 // arrays (which also allows us to take advantage of
9715 // some speed optimisations)
9716 m_colWidths
.Empty();
9717 m_colRights
.Empty();
9718 if ( !GetBatchCount() )
9723 void wxGrid::SetColSize( int col
, int width
)
9725 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9727 // should we check that it's bigger than GetColMinimalWidth(col) here?
9729 // No, because it is reasonable to assume the library user know's
9730 // what he is doing. However whe should test against the weaker
9731 // constariant of minimalAcceptableWidth, as this breaks rendering
9733 // This test then fixes sf.net bug #645734
9735 if ( width
< GetColMinimalAcceptableWidth() )
9738 if ( m_colWidths
.IsEmpty() )
9740 // need to really create the array
9744 // if < 0 calc new width from label
9748 wxArrayString lines
;
9749 wxClientDC
dc(m_colLabelWin
);
9750 dc
.SetFont(GetLabelFont());
9751 StringToLines(GetColLabelValue(col
), lines
);
9752 GetTextBoxSize(dc
, lines
, &w
, &h
);
9756 int w
= wxMax( 0, width
);
9757 int diff
= w
- m_colWidths
[col
];
9758 m_colWidths
[col
] = w
;
9761 for ( i
= col
; i
< m_numCols
; i
++ )
9763 m_colRights
[i
] += diff
;
9766 if ( !GetBatchCount() )
9770 void wxGrid::SetColMinimalWidth( int col
, int width
)
9772 if (width
> GetColMinimalAcceptableWidth())
9774 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9775 m_colMinWidths
[key
] = width
;
9779 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9781 if (width
> GetRowMinimalAcceptableHeight())
9783 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9784 m_rowMinHeights
[key
] = width
;
9788 int wxGrid::GetColMinimalWidth(int col
) const
9790 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9791 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9793 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9796 int wxGrid::GetRowMinimalHeight(int row
) const
9798 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9799 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9801 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9804 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9806 // We do allow a width of 0 since this gives us
9807 // an easy way to temporarily hiding columns.
9809 m_minAcceptableColWidth
= width
;
9812 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9814 // We do allow a height of 0 since this gives us
9815 // an easy way to temporarily hiding rows.
9817 m_minAcceptableRowHeight
= height
;
9820 int wxGrid::GetColMinimalAcceptableWidth() const
9822 return m_minAcceptableColWidth
;
9825 int wxGrid::GetRowMinimalAcceptableHeight() const
9827 return m_minAcceptableRowHeight
;
9830 // ----------------------------------------------------------------------------
9832 // ----------------------------------------------------------------------------
9834 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9836 wxClientDC
dc(m_gridWin
);
9838 // cancel editing of cell
9839 HideCellEditControl();
9840 SaveEditControlValue();
9842 // init both of them to avoid compiler warnings, even if we only need one
9850 wxCoord extent
, extentMax
= 0;
9851 int max
= column
? m_numRows
: m_numCols
;
9852 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9859 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9860 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9863 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9864 extent
= column
? size
.x
: size
.y
;
9865 if ( extent
> extentMax
)
9876 // now also compare with the column label extent
9878 dc
.SetFont( GetLabelFont() );
9882 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9883 if ( GetColLabelTextOrientation() == wxVERTICAL
)
9887 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9889 extent
= column
? w
: h
;
9890 if ( extent
> extentMax
)
9897 // empty column - give default extent (notice that if extentMax is less
9898 // than default extent but != 0, it's ok)
9899 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9904 // leave some space around text
9912 SetColSize( col
, extentMax
);
9913 if ( !GetBatchCount() )
9916 m_gridWin
->GetClientSize( &cw
, &ch
);
9917 wxRect
rect ( CellToRect( 0, col
) );
9919 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9920 rect
.width
= cw
- rect
.x
;
9921 rect
.height
= m_colLabelHeight
;
9922 m_colLabelWin
->Refresh( true, &rect
);
9927 SetRowSize(row
, extentMax
);
9928 if ( !GetBatchCount() )
9931 m_gridWin
->GetClientSize( &cw
, &ch
);
9932 wxRect
rect ( CellToRect( row
, 0 ) );
9934 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9935 rect
.width
= m_rowLabelWidth
;
9936 rect
.height
= ch
- rect
.y
;
9937 m_rowLabelWin
->Refresh( true, &rect
);
9943 SetColMinimalWidth(col
, extentMax
);
9945 SetRowMinimalHeight(row
, extentMax
);
9949 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9951 int width
= m_rowLabelWidth
;
9956 for ( int col
= 0; col
< m_numCols
; col
++ )
9960 AutoSizeColumn(col
, setAsMin
);
9963 width
+= GetColWidth(col
);
9972 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9974 int height
= m_colLabelHeight
;
9979 for ( int row
= 0; row
< m_numRows
; row
++ )
9982 AutoSizeRow(row
, setAsMin
);
9984 height
+= GetRowHeight(row
);
9993 void wxGrid::AutoSize()
9997 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9999 // round up the size to a multiple of scroll step - this ensures that we
10000 // won't get the scrollbars if we're sized exactly to this width
10001 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10003 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
10004 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10006 // distribute the extra space between the columns/rows to avoid having
10007 // extra white space
10009 // Remove the extra m_extraWidth + 1 added above
10010 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10011 if ( diff
&& m_numCols
)
10013 // try to resize the columns uniformly
10014 wxCoord diffPerCol
= diff
/ m_numCols
;
10017 for ( int col
= 0; col
< m_numCols
; col
++ )
10019 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10023 // add remaining amount to the last columns
10024 diff
-= diffPerCol
* m_numCols
;
10027 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10029 SetColSize(col
, GetColWidth(col
) + 1);
10035 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10036 if ( diff
&& m_numRows
)
10038 // try to resize the columns uniformly
10039 wxCoord diffPerRow
= diff
/ m_numRows
;
10042 for ( int row
= 0; row
< m_numRows
; row
++ )
10044 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10048 // add remaining amount to the last rows
10049 diff
-= diffPerRow
* m_numRows
;
10052 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10054 SetRowSize(row
, GetRowHeight(row
) + 1);
10061 SetClientSize(sizeFit
);
10064 void wxGrid::AutoSizeRowLabelSize( int row
)
10066 wxArrayString lines
;
10069 // Hide the edit control, so it
10070 // won't interfere with drag-shrinking.
10071 if ( IsCellEditControlShown() )
10073 HideCellEditControl();
10074 SaveEditControlValue();
10077 // autosize row height depending on label text
10078 StringToLines( GetRowLabelValue( row
), lines
);
10079 wxClientDC
dc( m_rowLabelWin
);
10080 GetTextBoxSize( dc
, lines
, &w
, &h
);
10081 if ( h
< m_defaultRowHeight
)
10082 h
= m_defaultRowHeight
;
10083 SetRowSize(row
, h
);
10087 void wxGrid::AutoSizeColLabelSize( int col
)
10089 wxArrayString lines
;
10092 // Hide the edit control, so it
10093 // won't interfer with drag-shrinking.
10094 if ( IsCellEditControlShown() )
10096 HideCellEditControl();
10097 SaveEditControlValue();
10100 // autosize column width depending on label text
10101 StringToLines( GetColLabelValue( col
), lines
);
10102 wxClientDC
dc( m_colLabelWin
);
10103 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10104 GetTextBoxSize( dc
, lines
, &w
, &h
);
10106 GetTextBoxSize( dc
, lines
, &h
, &w
);
10107 if ( w
< m_defaultColWidth
)
10108 w
= m_defaultColWidth
;
10109 SetColSize(col
, w
);
10113 wxSize
wxGrid::DoGetBestSize() const
10115 // don't set sizes, only calculate them
10116 wxGrid
*self
= (wxGrid
*)this; // const_cast
10119 width
= self
->SetOrCalcColumnSizes(true);
10120 height
= self
->SetOrCalcRowSizes(true);
10127 // Round up to a multiple the scroll rate
10128 // NOTE: this still doesn't get rid of the scrollbars;
10129 // is there any magic incantation for that?
10131 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10133 width
+= 1 + xpu
- (width
% xpu
);
10135 height
+= 1 + ypu
- (height
% ypu
);
10137 // limit to 1/4 of the screen size
10138 int maxwidth
, maxheight
;
10139 wxDisplaySize( &maxwidth
, &maxheight
);
10142 if ( width
> maxwidth
)
10144 if ( height
> maxheight
)
10145 height
= maxheight
;
10147 wxSize
best(width
, height
);
10149 // NOTE: This size should be cached, but first we need to add calls to
10150 // InvalidateBestSize everywhere that could change the results of this
10152 // CacheBestSize(size);
10162 wxPen
& wxGrid::GetDividerPen() const
10167 // ----------------------------------------------------------------------------
10168 // cell value accessor functions
10169 // ----------------------------------------------------------------------------
10171 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10175 m_table
->SetValue( row
, col
, s
);
10176 if ( !GetBatchCount() )
10179 wxRect
rect( CellToRect( row
, col
) );
10181 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10182 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10183 m_gridWin
->Refresh( false, &rect
);
10186 if ( m_currentCellCoords
.GetRow() == row
&&
10187 m_currentCellCoords
.GetCol() == col
&&
10188 IsCellEditControlShown())
10189 // Note: If we are using IsCellEditControlEnabled,
10190 // this interacts badly with calling SetCellValue from
10191 // an EVT_GRID_CELL_CHANGE handler.
10193 HideCellEditControl();
10194 ShowCellEditControl(); // will reread data from table
10200 // ----------------------------------------------------------------------------
10201 // block, row and col selection
10202 // ----------------------------------------------------------------------------
10204 void wxGrid::SelectRow( int row
, bool addToSelected
)
10206 if ( IsSelection() && !addToSelected
)
10210 m_selection
->SelectRow( row
, false, addToSelected
);
10213 void wxGrid::SelectCol( int col
, bool addToSelected
)
10215 if ( IsSelection() && !addToSelected
)
10219 m_selection
->SelectCol( col
, false, addToSelected
);
10222 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10223 bool addToSelected
)
10225 if ( IsSelection() && !addToSelected
)
10229 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10230 false, addToSelected
);
10233 void wxGrid::SelectAll()
10235 if ( m_numRows
> 0 && m_numCols
> 0 )
10238 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10242 // ----------------------------------------------------------------------------
10243 // cell, row and col deselection
10244 // ----------------------------------------------------------------------------
10246 void wxGrid::DeselectRow( int row
)
10248 if ( !m_selection
)
10251 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10253 if ( m_selection
->IsInSelection(row
, 0 ) )
10254 m_selection
->ToggleCellSelection( row
, 0);
10258 int nCols
= GetNumberCols();
10259 for ( int i
= 0; i
< nCols
; i
++ )
10261 if ( m_selection
->IsInSelection(row
, i
) )
10262 m_selection
->ToggleCellSelection( row
, i
);
10267 void wxGrid::DeselectCol( int col
)
10269 if ( !m_selection
)
10272 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10274 if ( m_selection
->IsInSelection(0, col
) )
10275 m_selection
->ToggleCellSelection( 0, col
);
10279 int nRows
= GetNumberRows();
10280 for ( int i
= 0; i
< nRows
; i
++ )
10282 if ( m_selection
->IsInSelection(i
, col
) )
10283 m_selection
->ToggleCellSelection(i
, col
);
10288 void wxGrid::DeselectCell( int row
, int col
)
10290 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10291 m_selection
->ToggleCellSelection(row
, col
);
10294 bool wxGrid::IsSelection()
10296 return ( m_selection
&& (m_selection
->IsSelection() ||
10297 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10298 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10301 bool wxGrid::IsInSelection( int row
, int col
) const
10303 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10304 ( row
>= m_selectingTopLeft
.GetRow() &&
10305 col
>= m_selectingTopLeft
.GetCol() &&
10306 row
<= m_selectingBottomRight
.GetRow() &&
10307 col
<= m_selectingBottomRight
.GetCol() )) );
10310 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10314 wxGridCellCoordsArray a
;
10318 return m_selection
->m_cellSelection
;
10321 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10325 wxGridCellCoordsArray a
;
10329 return m_selection
->m_blockSelectionTopLeft
;
10332 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10336 wxGridCellCoordsArray a
;
10340 return m_selection
->m_blockSelectionBottomRight
;
10343 wxArrayInt
wxGrid::GetSelectedRows() const
10351 return m_selection
->m_rowSelection
;
10354 wxArrayInt
wxGrid::GetSelectedCols() const
10362 return m_selection
->m_colSelection
;
10365 void wxGrid::ClearSelection()
10367 m_selectingTopLeft
= wxGridNoCellCoords
;
10368 m_selectingBottomRight
= wxGridNoCellCoords
;
10370 m_selection
->ClearSelection();
10374 // This function returns the rectangle that encloses the given block
10375 // in device coords clipped to the client size of the grid window.
10377 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10378 const wxGridCellCoords
&bottomRight
)
10380 wxRect
rect( wxGridNoCellRect
);
10383 cellRect
= CellToRect( topLeft
);
10384 if ( cellRect
!= wxGridNoCellRect
)
10390 rect
= wxRect(0, 0, 0, 0);
10393 cellRect
= CellToRect( bottomRight
);
10394 if ( cellRect
!= wxGridNoCellRect
)
10400 return wxGridNoCellRect
;
10404 int left
= rect
.GetLeft();
10405 int top
= rect
.GetTop();
10406 int right
= rect
.GetRight();
10407 int bottom
= rect
.GetBottom();
10409 int leftCol
= topLeft
.GetCol();
10410 int topRow
= topLeft
.GetRow();
10411 int rightCol
= bottomRight
.GetCol();
10412 int bottomRow
= bottomRight
.GetRow();
10420 leftCol
= rightCol
;
10430 topRow
= bottomRow
;
10434 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10436 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10438 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10440 cellRect
= CellToRect( j
, i
);
10442 if (cellRect
.x
< left
)
10444 if (cellRect
.y
< top
)
10446 if (cellRect
.x
+ cellRect
.width
> right
)
10447 right
= cellRect
.x
+ cellRect
.width
;
10448 if (cellRect
.y
+ cellRect
.height
> bottom
)
10449 bottom
= cellRect
.y
+ cellRect
.height
;
10453 i
= rightCol
; // jump over inner cells.
10458 // convert to scrolled coords
10460 CalcScrolledPosition( left
, top
, &left
, &top
);
10461 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10464 m_gridWin
->GetClientSize( &cw
, &ch
);
10466 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10467 return wxRect(0,0,0,0);
10469 rect
.SetLeft( wxMax(0, left
) );
10470 rect
.SetTop( wxMax(0, top
) );
10471 rect
.SetRight( wxMin(cw
, right
) );
10472 rect
.SetBottom( wxMin(ch
, bottom
) );
10477 // ----------------------------------------------------------------------------
10478 // grid event classes
10479 // ----------------------------------------------------------------------------
10481 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10483 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10484 int row
, int col
, int x
, int y
, bool sel
,
10485 bool control
, bool shift
, bool alt
, bool meta
)
10486 : wxNotifyEvent( type
, id
)
10493 m_control
= control
;
10498 SetEventObject(obj
);
10502 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10504 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10505 int rowOrCol
, int x
, int y
,
10506 bool control
, bool shift
, bool alt
, bool meta
)
10507 : wxNotifyEvent( type
, id
)
10509 m_rowOrCol
= rowOrCol
;
10512 m_control
= control
;
10517 SetEventObject(obj
);
10521 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10523 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10524 const wxGridCellCoords
& topLeft
,
10525 const wxGridCellCoords
& bottomRight
,
10526 bool sel
, bool control
,
10527 bool shift
, bool alt
, bool meta
)
10528 : wxNotifyEvent( type
, id
)
10530 m_topLeft
= topLeft
;
10531 m_bottomRight
= bottomRight
;
10533 m_control
= control
;
10538 SetEventObject(obj
);
10542 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10544 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10545 wxObject
* obj
, int row
,
10546 int col
, wxControl
* ctrl
)
10547 : wxCommandEvent(type
, id
)
10549 SetEventObject(obj
);
10555 #endif // wxUSE_GRID