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
)
237 class wxGridCellEditorEvtHandler
: public wxEvtHandler
240 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
247 void OnKillFocus(wxFocusEvent
& event
);
248 void OnKeyDown(wxKeyEvent
& event
);
249 void OnChar(wxKeyEvent
& event
);
251 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
255 wxGridCellEditor
* m_editor
;
257 // Work around the fact that a focus kill event can be sent to
258 // a combobox within a set focus event.
261 DECLARE_EVENT_TABLE()
262 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
263 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
267 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
269 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
270 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
271 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
272 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
277 // ----------------------------------------------------------------------------
278 // the internal data representation used by wxGridCellAttrProvider
279 // ----------------------------------------------------------------------------
281 // this class stores attributes set for cells
282 class WXDLLIMPEXP_ADV wxGridCellAttrData
285 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
286 wxGridCellAttr
*GetAttr(int row
, int col
) const;
287 void UpdateAttrRows( size_t pos
, int numRows
);
288 void UpdateAttrCols( size_t pos
, int numCols
);
291 // searches for the attr for given cell, returns wxNOT_FOUND if not found
292 int FindIndex(int row
, int col
) const;
294 wxGridCellWithAttrArray m_attrs
;
297 // this class stores attributes set for rows or columns
298 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
301 // empty ctor to suppress warnings
302 wxGridRowOrColAttrData() { }
303 ~wxGridRowOrColAttrData();
305 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
306 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
307 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
310 wxArrayInt m_rowsOrCols
;
311 wxArrayAttrs m_attrs
;
314 // NB: this is just a wrapper around 3 objects: one which stores cell
315 // attributes, and 2 others for row/col ones
316 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
319 wxGridCellAttrData m_cellAttrs
;
320 wxGridRowOrColAttrData m_rowAttrs
,
325 // ----------------------------------------------------------------------------
326 // data structures used for the data type registry
327 // ----------------------------------------------------------------------------
329 struct wxGridDataTypeInfo
331 wxGridDataTypeInfo(const wxString
& typeName
,
332 wxGridCellRenderer
* renderer
,
333 wxGridCellEditor
* editor
)
334 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
337 ~wxGridDataTypeInfo()
339 wxSafeDecRef(m_renderer
);
340 wxSafeDecRef(m_editor
);
344 wxGridCellRenderer
* m_renderer
;
345 wxGridCellEditor
* m_editor
;
347 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
351 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
352 class WXDLLIMPEXP_ADV
);
355 class WXDLLIMPEXP_ADV wxGridTypeRegistry
358 wxGridTypeRegistry() {}
359 ~wxGridTypeRegistry();
361 void RegisterDataType(const wxString
& typeName
,
362 wxGridCellRenderer
* renderer
,
363 wxGridCellEditor
* editor
);
365 // find one of already registered data types
366 int FindRegisteredDataType(const wxString
& typeName
);
368 // try to FindRegisteredDataType(), if this fails and typeName is one of
369 // standard typenames, register it and return its index
370 int FindDataType(const wxString
& typeName
);
372 // try to FindDataType(), if it fails see if it is not one of already
373 // registered data types with some params in which case clone the
374 // registered data type and set params for it
375 int FindOrCloneDataType(const wxString
& typeName
);
377 wxGridCellRenderer
* GetRenderer(int index
);
378 wxGridCellEditor
* GetEditor(int index
);
381 wxGridDataTypeInfoArray m_typeinfo
;
384 // ----------------------------------------------------------------------------
385 // conditional compilation
386 // ----------------------------------------------------------------------------
388 #ifndef WXGRID_DRAW_LINES
389 #define WXGRID_DRAW_LINES 1
392 // ----------------------------------------------------------------------------
394 // ----------------------------------------------------------------------------
396 //#define DEBUG_ATTR_CACHE
397 #ifdef DEBUG_ATTR_CACHE
398 static size_t gs_nAttrCacheHits
= 0;
399 static size_t gs_nAttrCacheMisses
= 0;
400 #endif // DEBUG_ATTR_CACHE
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
407 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
410 // TODO: this doesn't work at all, grid cells have different sizes and approx
411 // calculations don't work as because of the size mismatch scrollbars
412 // sometimes fail to be shown when they should be or vice versa
414 // The scroll bars may be a little flakey once in a while, but that is
415 // surely much less horrible than having scroll lines of only 1!!!
418 // Well, it's still seriously broken so it might be better but needs
421 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
422 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
424 // the size of hash tables used a bit everywhere (the max number of elements
425 // in these hash tables is the number of rows/columns)
426 static const int GRID_HASH_SIZE
= 100;
429 // ----------------------------------------------------------------------------
431 // ----------------------------------------------------------------------------
433 static inline int GetScrollX(int x
)
435 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
438 static inline int GetScrollY(int y
)
440 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
444 // ============================================================================
446 // ============================================================================
448 // ----------------------------------------------------------------------------
450 // ----------------------------------------------------------------------------
452 wxGridCellEditor::wxGridCellEditor()
459 wxGridCellEditor::~wxGridCellEditor()
464 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
465 wxWindowID
WXUNUSED(id
),
466 wxEvtHandler
* evtHandler
)
469 m_control
->PushEventHandler(evtHandler
);
472 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
473 wxGridCellAttr
*attr
)
475 // erase the background because we might not fill the cell
476 wxClientDC
dc(m_control
->GetParent());
477 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
479 gridWindow
->GetOwner()->PrepareDC(dc
);
481 dc
.SetPen(*wxTRANSPARENT_PEN
);
482 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
483 dc
.DrawRectangle(rectCell
);
485 // redraw the control we just painted over
486 m_control
->Refresh();
489 void wxGridCellEditor::Destroy()
493 m_control
->PopEventHandler(true /* delete it*/);
495 m_control
->Destroy();
500 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
502 wxASSERT_MSG(m_control
,
503 wxT("The wxGridCellEditor must be Created first!"));
504 m_control
->Show(show
);
508 // set the colours/fonts if we have any
511 m_colFgOld
= m_control
->GetForegroundColour();
512 m_control
->SetForegroundColour(attr
->GetTextColour());
514 m_colBgOld
= m_control
->GetBackgroundColour();
515 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
517 // Workaround for GTK+1 font setting problem on some platforms
518 #if !defined(__WXGTK__) || defined(__WXGTK20__)
519 m_fontOld
= m_control
->GetFont();
520 m_control
->SetFont(attr
->GetFont());
522 // can't do anything more in the base class version, the other
523 // attributes may only be used by the derived classes
528 // restore the standard colours fonts
529 if ( m_colFgOld
.Ok() )
531 m_control
->SetForegroundColour(m_colFgOld
);
532 m_colFgOld
= wxNullColour
;
535 if ( m_colBgOld
.Ok() )
537 m_control
->SetBackgroundColour(m_colBgOld
);
538 m_colBgOld
= wxNullColour
;
540 // Workaround for GTK+1 font setting problem on some platforms
541 #if !defined(__WXGTK__) || defined(__WXGTK20__)
542 if ( m_fontOld
.Ok() )
544 m_control
->SetFont(m_fontOld
);
545 m_fontOld
= wxNullFont
;
551 void wxGridCellEditor::SetSize(const wxRect
& rect
)
553 wxASSERT_MSG(m_control
,
554 wxT("The wxGridCellEditor must be Created first!"));
555 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
558 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
563 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
565 bool ctrl
= event
.ControlDown();
566 bool alt
= event
.AltDown();
568 // On the Mac the Alt key is more like shift and is used for entry of
569 // valid characters, so check for Ctrl and Meta instead.
570 alt
= event
.MetaDown();
573 // Assume it's not a valid char if ctrl or alt is down, but if both are
574 // down then it may be because of an AltGr key combination, so let them
575 // through in that case.
576 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
580 int key
= event
.GetUnicodeKey();
583 // if the unicode key code is not really a unicode character (it may
584 // be a function key or etc., the platforms appear to always give us a
585 // small value in this case) then fallback to the ascii key code but
586 // don't do anything for function keys or etc.
589 key
= event
.GetKeyCode();
590 keyOk
= (key
<= 127);
593 #else // !wxUSE_UNICODE
594 int key
= event
.GetKeyCode();
598 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
601 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
606 void wxGridCellEditor::StartingClick()
612 // ----------------------------------------------------------------------------
613 // wxGridCellTextEditor
614 // ----------------------------------------------------------------------------
616 wxGridCellTextEditor::wxGridCellTextEditor()
621 void wxGridCellTextEditor::Create(wxWindow
* parent
,
623 wxEvtHandler
* evtHandler
)
625 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
626 wxDefaultPosition
, wxDefaultSize
627 #if defined(__WXMSW__)
628 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
632 // set max length allowed in the textctrl, if the parameter was set
635 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
638 wxGridCellEditor::Create(parent
, id
, evtHandler
);
641 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
642 wxGridCellAttr
* WXUNUSED(attr
))
644 // as we fill the entire client area, don't do anything here to minimize
648 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
650 wxRect
rect(rectOrig
);
652 // Make the edit control large enough to allow for internal
655 // TODO: remove this if the text ctrl sizing is improved esp. for
658 #if defined(__WXGTK__)
667 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
669 // MB: treat MSW separately here otherwise the caret doesn't show
670 // when the editor is in the first row.
671 #if defined(__WXMSW__)
674 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
677 #if defined(__WXMOTIF__)
681 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
682 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
683 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
684 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
687 wxGridCellEditor::SetSize(rect
);
690 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
692 wxASSERT_MSG(m_control
,
693 wxT("The wxGridCellEditor must be Created first!"));
695 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
697 DoBeginEdit(m_startValue
);
700 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
702 Text()->SetValue(startValue
);
703 Text()->SetInsertionPointEnd();
704 Text()->SetSelection(-1,-1);
708 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
711 wxASSERT_MSG(m_control
,
712 wxT("The wxGridCellEditor must be Created first!"));
714 bool changed
= false;
715 wxString value
= Text()->GetValue();
716 if (value
!= m_startValue
)
720 grid
->GetTable()->SetValue(row
, col
, value
);
722 m_startValue
= wxEmptyString
;
723 // No point in setting the text of the hidden control
724 //Text()->SetValue(m_startValue);
730 void wxGridCellTextEditor::Reset()
732 wxASSERT_MSG(m_control
,
733 wxT("The wxGridCellEditor must be Created first!"));
735 DoReset(m_startValue
);
738 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
740 Text()->SetValue(startValue
);
741 Text()->SetInsertionPointEnd();
744 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
746 return wxGridCellEditor::IsAcceptedKey(event
);
749 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
751 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
752 // longer an appropriate way to get the character into the text control.
753 // Do it ourselves instead. We know that if we get this far that we have
754 // a valid character, so not a whole lot of testing needs to be done.
756 wxTextCtrl
* tc
= Text();
761 ch
= event
.GetUnicodeKey();
763 ch
= (wxChar
)event
.GetKeyCode();
765 ch
= (wxChar
)event
.GetKeyCode();
770 // delete the character at the cursor
771 pos
= tc
->GetInsertionPoint();
772 if (pos
< tc
->GetLastPosition())
773 tc
->Remove(pos
, pos
+1);
777 // delete the character before the cursor
778 pos
= tc
->GetInsertionPoint();
780 tc
->Remove(pos
-1, pos
);
789 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
790 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
792 #if defined(__WXMOTIF__) || defined(__WXGTK__)
793 // wxMotif needs a little extra help...
794 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
795 wxString
s( Text()->GetValue() );
796 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
798 Text()->SetInsertionPoint( pos
);
800 // the other ports can handle a Return key press
806 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
816 if ( !params
.ToLong(&tmp
) )
818 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
822 m_maxChars
= (size_t)tmp
;
827 // return the value in the text control
828 wxString
wxGridCellTextEditor::GetValue() const
830 return Text()->GetValue();
833 // ----------------------------------------------------------------------------
834 // wxGridCellNumberEditor
835 // ----------------------------------------------------------------------------
837 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
843 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
845 wxEvtHandler
* evtHandler
)
850 // create a spin ctrl
851 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
852 wxDefaultPosition
, wxDefaultSize
,
856 wxGridCellEditor::Create(parent
, id
, evtHandler
);
861 // just a text control
862 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
865 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
866 #endif // wxUSE_VALIDATORS
870 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
872 // first get the value
873 wxGridTableBase
*table
= grid
->GetTable();
874 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
876 m_valueOld
= table
->GetValueAsLong(row
, col
);
881 wxString sValue
= table
->GetValue(row
, col
);
882 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
884 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
892 Spin()->SetValue((int)m_valueOld
);
898 DoBeginEdit(GetString());
902 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
912 value
= Spin()->GetValue();
913 changed
= value
!= m_valueOld
;
915 text
= wxString::Format(wxT("%ld"), value
);
920 text
= Text()->GetValue();
921 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
926 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
927 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
929 grid
->GetTable()->SetValue(row
, col
, text
);
935 void wxGridCellNumberEditor::Reset()
940 Spin()->SetValue((int)m_valueOld
);
945 DoReset(GetString());
949 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
951 if ( wxGridCellEditor::IsAcceptedKey(event
) )
953 int keycode
= event
.GetKeyCode();
954 if ( (keycode
< 128) &&
955 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
964 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
966 int keycode
= event
.GetKeyCode();
969 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
971 wxGridCellTextEditor::StartingKey(event
);
980 if ( wxIsdigit(keycode
) )
982 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
983 spin
->SetValue(keycode
- '0');
984 spin
->SetSelection(1,1);
992 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1003 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1007 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1011 // skip the error message below
1016 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1020 // return the value in the spin control if it is there (the text control otherwise)
1021 wxString
wxGridCellNumberEditor::GetValue() const
1028 long value
= Spin()->GetValue();
1029 s
.Printf(wxT("%ld"), value
);
1034 s
= Text()->GetValue();
1040 // ----------------------------------------------------------------------------
1041 // wxGridCellFloatEditor
1042 // ----------------------------------------------------------------------------
1044 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1047 m_precision
= precision
;
1050 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1052 wxEvtHandler
* evtHandler
)
1054 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1056 #if wxUSE_VALIDATORS
1057 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1058 #endif // wxUSE_VALIDATORS
1061 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1063 // first get the value
1064 wxGridTableBase
*table
= grid
->GetTable();
1065 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1067 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1072 wxString sValue
= table
->GetValue(row
, col
);
1073 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1075 wxFAIL_MSG( _T("this cell doesn't have float value") );
1080 DoBeginEdit(GetString());
1083 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1087 wxString
text(Text()->GetValue());
1089 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1090 !wxIsSameDouble(value
, m_valueOld
) )
1092 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1093 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1095 grid
->GetTable()->SetValue(row
, col
, text
);
1102 void wxGridCellFloatEditor::Reset()
1104 DoReset(GetString());
1107 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1109 int keycode
= event
.GetKeyCode();
1111 tmpbuf
[0] = (char) keycode
;
1113 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1115 bool is_decimal_point
= ( strbuf
==
1116 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1118 bool is_decimal_point
= ( strbuf
== _T(".") );
1120 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1121 || is_decimal_point
)
1123 wxGridCellTextEditor::StartingKey(event
);
1125 // skip Skip() below
1132 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1143 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1147 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1149 m_precision
= (int)tmp
;
1151 // skip the error message below
1156 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1160 wxString
wxGridCellFloatEditor::GetString() const
1163 if ( m_width
== -1 )
1165 // default width/precision
1168 else if ( m_precision
== -1 )
1170 // default precision
1171 fmt
.Printf(_T("%%%d.f"), m_width
);
1175 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1178 return wxString::Format(fmt
, m_valueOld
);
1181 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1183 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1185 int keycode
= event
.GetKeyCode();
1186 printf("%d\n", keycode
);
1187 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1189 tmpbuf
[0] = (char) keycode
;
1191 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1193 bool is_decimal_point
=
1194 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1195 wxLOCALE_CAT_NUMBER
) );
1197 bool is_decimal_point
= ( strbuf
== _T(".") );
1199 if ( (keycode
< 128) &&
1200 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1201 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1208 #endif // wxUSE_TEXTCTRL
1212 // ----------------------------------------------------------------------------
1213 // wxGridCellBoolEditor
1214 // ----------------------------------------------------------------------------
1216 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1218 wxEvtHandler
* evtHandler
)
1220 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1221 wxDefaultPosition
, wxDefaultSize
,
1224 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1227 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1229 bool resize
= false;
1230 wxSize size
= m_control
->GetSize();
1231 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1233 // check if the checkbox is not too big/small for this cell
1234 wxSize sizeBest
= m_control
->GetBestSize();
1235 if ( !(size
== sizeBest
) )
1237 // reset to default size if it had been made smaller
1243 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1245 // leave 1 pixel margin
1246 size
.x
= size
.y
= minSize
- 2;
1253 m_control
->SetSize(size
);
1256 // position it in the centre of the rectangle (TODO: support alignment?)
1258 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1259 // the checkbox without label still has some space to the right in wxGTK,
1260 // so shift it to the right
1262 #elif defined(__WXMSW__)
1263 // here too, but in other way
1268 int hAlign
= wxALIGN_CENTRE
;
1269 int vAlign
= wxALIGN_CENTRE
;
1271 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1274 if (hAlign
== wxALIGN_LEFT
)
1280 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1282 else if (hAlign
== wxALIGN_RIGHT
)
1284 x
= r
.x
+ r
.width
- size
.x
- 2;
1285 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1287 else if (hAlign
== wxALIGN_CENTRE
)
1289 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1290 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1293 m_control
->Move(x
, y
);
1296 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1298 m_control
->Show(show
);
1302 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1303 CBox()->SetBackgroundColour(colBg
);
1307 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1309 wxASSERT_MSG(m_control
,
1310 wxT("The wxGridCellEditor must be Created first!"));
1312 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1313 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1316 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1317 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1319 CBox()->SetValue(m_startValue
);
1323 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1326 wxASSERT_MSG(m_control
,
1327 wxT("The wxGridCellEditor must be Created first!"));
1329 bool changed
= false;
1330 bool value
= CBox()->GetValue();
1331 if ( value
!= m_startValue
)
1336 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1337 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1339 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1345 void wxGridCellBoolEditor::Reset()
1347 wxASSERT_MSG(m_control
,
1348 wxT("The wxGridCellEditor must be Created first!"));
1350 CBox()->SetValue(m_startValue
);
1353 void wxGridCellBoolEditor::StartingClick()
1355 CBox()->SetValue(!CBox()->GetValue());
1358 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1360 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1362 int keycode
= event
.GetKeyCode();
1375 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1377 int keycode
= event
.GetKeyCode();
1381 CBox()->SetValue(!CBox()->GetValue());
1385 CBox()->SetValue(true);
1389 CBox()->SetValue(false);
1395 // return the value as "1" for true and the empty string for false
1396 wxString
wxGridCellBoolEditor::GetValue() const
1398 bool bSet
= CBox()->GetValue();
1399 return bSet
? _T("1") : wxEmptyString
;
1402 #endif // wxUSE_CHECKBOX
1406 // ----------------------------------------------------------------------------
1407 // wxGridCellChoiceEditor
1408 // ----------------------------------------------------------------------------
1410 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1412 : m_choices(choices
),
1413 m_allowOthers(allowOthers
) { }
1415 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1416 const wxString choices
[],
1418 : m_allowOthers(allowOthers
)
1422 m_choices
.Alloc(count
);
1423 for ( size_t n
= 0; n
< count
; n
++ )
1425 m_choices
.Add(choices
[n
]);
1430 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1432 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1433 editor
->m_allowOthers
= m_allowOthers
;
1434 editor
->m_choices
= m_choices
;
1439 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1441 wxEvtHandler
* evtHandler
)
1443 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1444 wxDefaultPosition
, wxDefaultSize
,
1446 m_allowOthers
? 0 : wxCB_READONLY
);
1448 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1451 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1452 wxGridCellAttr
* attr
)
1454 // as we fill the entire client area, don't do anything here to minimize
1457 // TODO: It doesn't actually fill the client area since the height of a
1458 // combo always defaults to the standard... Until someone has time to
1459 // figure out the right rectangle to paint, just do it the normal way...
1460 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1463 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1465 wxASSERT_MSG(m_control
,
1466 wxT("The wxGridCellEditor must be Created first!"));
1468 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1470 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1472 // Don't immediately end if we get a kill focus event within BeginEdit
1474 evtHandler
->SetInSetFocus(true);
1476 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1479 Combo()->SetValue(m_startValue
);
1482 // find the right position, or default to the first if not found
1483 int pos
= Combo()->FindString(m_startValue
);
1484 if (pos
== wxNOT_FOUND
)
1486 Combo()->SetSelection(pos
);
1488 Combo()->SetInsertionPointEnd();
1489 Combo()->SetFocus();
1492 evtHandler
->SetInSetFocus(false);
1495 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1498 wxString value
= Combo()->GetValue();
1499 if ( value
== m_startValue
)
1502 grid
->GetTable()->SetValue(row
, col
, value
);
1507 void wxGridCellChoiceEditor::Reset()
1509 Combo()->SetValue(m_startValue
);
1510 Combo()->SetInsertionPointEnd();
1513 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1523 wxStringTokenizer
tk(params
, _T(','));
1524 while ( tk
.HasMoreTokens() )
1526 m_choices
.Add(tk
.GetNextToken());
1530 // return the value in the text control
1531 wxString
wxGridCellChoiceEditor::GetValue() const
1533 return Combo()->GetValue();
1536 #endif // wxUSE_COMBOBOX
1538 // ----------------------------------------------------------------------------
1539 // wxGridCellEditorEvtHandler
1540 // ----------------------------------------------------------------------------
1542 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1544 // Don't disable the cell if we're just starting to edit it
1549 m_grid
->DisableCellEditControl();
1554 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1556 switch ( event
.GetKeyCode() )
1560 m_grid
->DisableCellEditControl();
1564 m_grid
->GetEventHandler()->ProcessEvent( event
);
1568 case WXK_NUMPAD_ENTER
:
1569 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1570 m_editor
->HandleReturn(event
);
1578 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1580 switch ( event
.GetKeyCode() )
1585 case WXK_NUMPAD_ENTER
:
1593 // ----------------------------------------------------------------------------
1594 // wxGridCellWorker is an (almost) empty common base class for
1595 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1596 // ----------------------------------------------------------------------------
1598 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1603 wxGridCellWorker::~wxGridCellWorker()
1607 // ============================================================================
1609 // ============================================================================
1611 // ----------------------------------------------------------------------------
1612 // wxGridCellRenderer
1613 // ----------------------------------------------------------------------------
1615 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1616 wxGridCellAttr
& attr
,
1619 int WXUNUSED(row
), int WXUNUSED(col
),
1622 dc
.SetBackgroundMode( wxSOLID
);
1624 // grey out fields if the grid is disabled
1625 if( grid
.IsEnabled() )
1629 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1633 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1638 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1641 dc
.SetPen( *wxTRANSPARENT_PEN
);
1642 dc
.DrawRectangle(rect
);
1645 // ----------------------------------------------------------------------------
1646 // wxGridCellStringRenderer
1647 // ----------------------------------------------------------------------------
1649 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1650 const wxGridCellAttr
& attr
,
1654 dc
.SetBackgroundMode( wxTRANSPARENT
);
1656 // TODO some special colours for attr.IsReadOnly() case?
1658 // different coloured text when the grid is disabled
1659 if( grid
.IsEnabled() )
1663 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1664 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1668 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1669 dc
.SetTextForeground( attr
.GetTextColour() );
1674 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1675 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1678 dc
.SetFont( attr
.GetFont() );
1681 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1683 const wxString
& text
)
1685 wxCoord x
= 0, y
= 0, max_x
= 0;
1686 dc
.SetFont(attr
.GetFont());
1687 wxStringTokenizer
tk(text
, _T('\n'));
1688 while ( tk
.HasMoreTokens() )
1690 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1691 max_x
= wxMax(max_x
, x
);
1694 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1696 return wxSize(max_x
, y
);
1699 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1700 wxGridCellAttr
& attr
,
1704 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1707 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1708 wxGridCellAttr
& attr
,
1710 const wxRect
& rectCell
,
1714 wxRect rect
= rectCell
;
1717 // erase only this cells background, overflow cells should have been erased
1718 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1721 attr
.GetAlignment(&hAlign
, &vAlign
);
1723 int overflowCols
= 0;
1725 if (attr
.GetOverflow())
1727 int cols
= grid
.GetNumberCols();
1728 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1729 int cell_rows
, cell_cols
;
1730 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1731 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1733 int i
, c_cols
, c_rows
;
1734 for (i
= col
+cell_cols
; i
< cols
; i
++)
1736 bool is_empty
= true;
1737 for (int j
=row
; j
<row
+cell_rows
; j
++)
1739 // check w/ anchor cell for multicell block
1740 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1741 if (c_rows
> 0) c_rows
= 0;
1742 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1749 rect
.width
+= grid
.GetColSize(i
);
1755 if (rect
.width
>= best_width
) break;
1757 overflowCols
= i
- col
- cell_cols
+ 1;
1758 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1761 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1763 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1765 clip
.x
+= rectCell
.width
;
1766 // draw each overflow cell individually
1767 int col_end
= col
+cell_cols
+overflowCols
;
1768 if (col_end
>= grid
.GetNumberCols())
1769 col_end
= grid
.GetNumberCols() - 1;
1770 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1772 clip
.width
= grid
.GetColSize(i
) - 1;
1773 dc
.DestroyClippingRegion();
1774 dc
.SetClippingRegion(clip
);
1776 SetTextColoursAndFont(grid
, attr
, dc
,
1777 grid
.IsInSelection(row
,i
));
1779 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1780 rect
, hAlign
, vAlign
);
1781 clip
.x
+= grid
.GetColSize(i
) - 1;
1787 dc
.DestroyClippingRegion();
1791 // now we only have to draw the text
1792 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1794 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1795 rect
, hAlign
, vAlign
);
1798 // ----------------------------------------------------------------------------
1799 // wxGridCellNumberRenderer
1800 // ----------------------------------------------------------------------------
1802 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1804 wxGridTableBase
*table
= grid
.GetTable();
1806 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1808 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1812 text
= table
->GetValue(row
, col
);
1818 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1819 wxGridCellAttr
& attr
,
1821 const wxRect
& rectCell
,
1825 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1827 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1829 // draw the text right aligned by default
1831 attr
.GetAlignment(&hAlign
, &vAlign
);
1832 hAlign
= wxALIGN_RIGHT
;
1834 wxRect rect
= rectCell
;
1837 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1840 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1841 wxGridCellAttr
& attr
,
1845 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1848 // ----------------------------------------------------------------------------
1849 // wxGridCellFloatRenderer
1850 // ----------------------------------------------------------------------------
1852 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1855 SetPrecision(precision
);
1858 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1860 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1861 renderer
->m_width
= m_width
;
1862 renderer
->m_precision
= m_precision
;
1863 renderer
->m_format
= m_format
;
1868 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1870 wxGridTableBase
*table
= grid
.GetTable();
1875 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1877 val
= table
->GetValueAsDouble(row
, col
);
1882 text
= table
->GetValue(row
, col
);
1883 hasDouble
= text
.ToDouble(&val
);
1890 if ( m_width
== -1 )
1892 if ( m_precision
== -1 )
1894 // default width/precision
1895 m_format
= _T("%f");
1899 m_format
.Printf(_T("%%.%df"), m_precision
);
1902 else if ( m_precision
== -1 )
1904 // default precision
1905 m_format
.Printf(_T("%%%d.f"), m_width
);
1909 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1913 text
.Printf(m_format
, val
);
1916 //else: text already contains the string
1921 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1922 wxGridCellAttr
& attr
,
1924 const wxRect
& rectCell
,
1928 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1930 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1932 // draw the text right aligned by default
1934 attr
.GetAlignment(&hAlign
, &vAlign
);
1935 hAlign
= wxALIGN_RIGHT
;
1937 wxRect rect
= rectCell
;
1940 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1943 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1944 wxGridCellAttr
& attr
,
1948 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1951 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1955 // reset to defaults
1961 wxString tmp
= params
.BeforeFirst(_T(','));
1965 if ( tmp
.ToLong(&width
) )
1967 SetWidth((int)width
);
1971 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1974 tmp
= params
.AfterFirst(_T(','));
1978 if ( tmp
.ToLong(&precision
) )
1980 SetPrecision((int)precision
);
1984 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1991 // ----------------------------------------------------------------------------
1992 // wxGridCellBoolRenderer
1993 // ----------------------------------------------------------------------------
1995 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1997 // FIXME these checkbox size calculations are really ugly...
1999 // between checkmark and box
2000 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2002 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2003 wxGridCellAttr
& WXUNUSED(attr
),
2008 // compute it only once (no locks for MT safeness in GUI thread...)
2009 if ( !ms_sizeCheckMark
.x
)
2011 // get checkbox size
2012 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2013 wxSize size
= checkbox
->GetBestSize();
2014 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
2016 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2017 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2018 checkSize
-= size
.y
/ 2;
2023 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2026 return ms_sizeCheckMark
;
2029 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2030 wxGridCellAttr
& attr
,
2036 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2038 // draw a check mark in the centre (ignoring alignment - TODO)
2039 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2041 // don't draw outside the cell
2042 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2043 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2045 // and even leave (at least) 1 pixel margin
2046 size
.x
= size
.y
= minSize
- 2;
2049 // draw a border around checkmark
2051 attr
.GetAlignment(& hAlign
, &vAlign
);
2054 if (hAlign
== wxALIGN_CENTRE
)
2056 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2057 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2058 rectBorder
.width
= size
.x
;
2059 rectBorder
.height
= size
.y
;
2061 else if (hAlign
== wxALIGN_LEFT
)
2063 rectBorder
.x
= rect
.x
+ 2;
2064 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2065 rectBorder
.width
= size
.x
;
2066 rectBorder
.height
= size
.y
;
2068 else if (hAlign
== wxALIGN_RIGHT
)
2070 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2071 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2072 rectBorder
.width
= size
.x
;
2073 rectBorder
.height
= size
.y
;
2077 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2078 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2081 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2082 value
= !( !cellval
|| (cellval
== wxT("0")) );
2087 wxRect rectMark
= rectBorder
;
2089 // MSW DrawCheckMark() is weird (and should probably be changed...)
2090 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2094 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2097 dc
.SetTextForeground(attr
.GetTextColour());
2098 dc
.DrawCheckMark(rectMark
);
2101 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2102 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2103 dc
.DrawRectangle(rectBorder
);
2106 // ----------------------------------------------------------------------------
2108 // ----------------------------------------------------------------------------
2110 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2114 m_isReadOnly
= Unset
;
2119 m_attrkind
= wxGridCellAttr::Cell
;
2121 m_sizeRows
= m_sizeCols
= 1;
2122 m_overflow
= UnsetOverflow
;
2124 SetDefAttr(attrDefault
);
2127 wxGridCellAttr
*wxGridCellAttr::Clone() const
2129 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2131 if ( HasTextColour() )
2132 attr
->SetTextColour(GetTextColour());
2133 if ( HasBackgroundColour() )
2134 attr
->SetBackgroundColour(GetBackgroundColour());
2136 attr
->SetFont(GetFont());
2137 if ( HasAlignment() )
2138 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2140 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2144 attr
->SetRenderer(m_renderer
);
2145 m_renderer
->IncRef();
2149 attr
->SetEditor(m_editor
);
2154 attr
->SetReadOnly();
2156 attr
->SetKind( m_attrkind
);
2161 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2163 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2164 SetTextColour(mergefrom
->GetTextColour());
2165 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2166 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2167 if ( !HasFont() && mergefrom
->HasFont() )
2168 SetFont(mergefrom
->GetFont());
2169 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2171 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2172 SetAlignment(hAlign
, vAlign
);
2174 if ( !HasSize() && mergefrom
->HasSize() )
2175 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2177 // Directly access member functions as GetRender/Editor don't just return
2178 // m_renderer/m_editor
2180 // Maybe add support for merge of Render and Editor?
2181 if (!HasRenderer() && mergefrom
->HasRenderer() )
2183 m_renderer
= mergefrom
->m_renderer
;
2184 m_renderer
->IncRef();
2186 if ( !HasEditor() && mergefrom
->HasEditor() )
2188 m_editor
= mergefrom
->m_editor
;
2191 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2192 SetReadOnly(mergefrom
->IsReadOnly());
2194 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2195 SetOverflow(mergefrom
->GetOverflow());
2197 SetDefAttr(mergefrom
->m_defGridAttr
);
2200 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2202 // The size of a cell is normally 1,1
2204 // If this cell is larger (2,2) then this is the top left cell
2205 // the other cells that will be covered (lower right cells) must be
2206 // set to negative or zero values such that
2207 // row + num_rows of the covered cell points to the larger cell (this cell)
2208 // same goes for the col + num_cols.
2210 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2212 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2213 !((num_rows
<=0)&&(num_cols
>0)) ||
2214 !((num_rows
==0)&&(num_cols
==0))),
2215 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2217 m_sizeRows
= num_rows
;
2218 m_sizeCols
= num_cols
;
2221 const wxColour
& wxGridCellAttr::GetTextColour() const
2223 if (HasTextColour())
2227 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2229 return m_defGridAttr
->GetTextColour();
2233 wxFAIL_MSG(wxT("Missing default cell attribute"));
2234 return wxNullColour
;
2239 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2241 if (HasBackgroundColour())
2243 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2244 return m_defGridAttr
->GetBackgroundColour();
2247 wxFAIL_MSG(wxT("Missing default cell attribute"));
2248 return wxNullColour
;
2253 const wxFont
& wxGridCellAttr::GetFont() const
2257 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2258 return m_defGridAttr
->GetFont();
2261 wxFAIL_MSG(wxT("Missing default cell attribute"));
2267 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2271 if ( hAlign
) *hAlign
= m_hAlign
;
2272 if ( vAlign
) *vAlign
= m_vAlign
;
2274 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2275 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2278 wxFAIL_MSG(wxT("Missing default cell attribute"));
2282 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2284 if ( num_rows
) *num_rows
= m_sizeRows
;
2285 if ( num_cols
) *num_cols
= m_sizeCols
;
2288 // GetRenderer and GetEditor use a slightly different decision path about
2289 // which attribute to use. If a non-default attr object has one then it is
2290 // used, otherwise the default editor or renderer is fetched from the grid and
2291 // used. It should be the default for the data type of the cell. If it is
2292 // NULL (because the table has a type that the grid does not have in its
2293 // registry,) then the grid's default editor or renderer is used.
2295 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2297 wxGridCellRenderer
*renderer
;
2299 if ( m_renderer
&& this != m_defGridAttr
)
2301 // use the cells renderer if it has one
2302 renderer
= m_renderer
;
2305 else // no non default cell renderer
2307 // get default renderer for the data type
2310 // GetDefaultRendererForCell() will do IncRef() for us
2311 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2320 if (m_defGridAttr
&& this != m_defGridAttr
)
2322 // if we still don't have one then use the grid default
2323 // (no need for IncRef() here neither)
2324 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2326 else // default grid attr
2328 // use m_renderer which we had decided not to use initially
2329 renderer
= m_renderer
;
2336 // we're supposed to always find something
2337 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2342 // same as above, except for s/renderer/editor/g
2343 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2345 wxGridCellEditor
*editor
;
2347 if ( m_editor
&& this != m_defGridAttr
)
2349 // use the cells editor if it has one
2353 else // no non default cell editor
2355 // get default editor for the data type
2358 // GetDefaultEditorForCell() will do IncRef() for us
2359 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2368 if ( m_defGridAttr
&& this != m_defGridAttr
)
2370 // if we still don't have one then use the grid default
2371 // (no need for IncRef() here neither)
2372 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2374 else // default grid attr
2376 // use m_editor which we had decided not to use initially
2384 // we're supposed to always find something
2385 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2390 // ----------------------------------------------------------------------------
2391 // wxGridCellAttrData
2392 // ----------------------------------------------------------------------------
2394 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2396 int n
= FindIndex(row
, col
);
2397 if ( n
== wxNOT_FOUND
)
2399 // add the attribute
2400 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2404 // free the old attribute
2405 m_attrs
[(size_t)n
].attr
->DecRef();
2409 // change the attribute
2410 m_attrs
[(size_t)n
].attr
= attr
;
2414 // remove this attribute
2415 m_attrs
.RemoveAt((size_t)n
);
2420 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2422 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2424 int n
= FindIndex(row
, col
);
2425 if ( n
!= wxNOT_FOUND
)
2427 attr
= m_attrs
[(size_t)n
].attr
;
2434 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2436 size_t count
= m_attrs
.GetCount();
2437 for ( size_t n
= 0; n
< count
; n
++ )
2439 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2440 wxCoord row
= coords
.GetRow();
2441 if ((size_t)row
>= pos
)
2445 // If rows inserted, include row counter where necessary
2446 coords
.SetRow(row
+ numRows
);
2448 else if (numRows
< 0)
2450 // If rows deleted ...
2451 if ((size_t)row
>= pos
- numRows
)
2453 // ...either decrement row counter (if row still exists)...
2454 coords
.SetRow(row
+ numRows
);
2458 // ...or remove the attribute
2459 // No need to DecRef the attribute itself since this is
2460 // done be wxGridCellWithAttr's destructor!
2461 m_attrs
.RemoveAt(n
);
2469 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2471 size_t count
= m_attrs
.GetCount();
2472 for ( size_t n
= 0; n
< count
; n
++ )
2474 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2475 wxCoord col
= coords
.GetCol();
2476 if ( (size_t)col
>= pos
)
2480 // If rows inserted, include row counter where necessary
2481 coords
.SetCol(col
+ numCols
);
2483 else if (numCols
< 0)
2485 // If rows deleted ...
2486 if ((size_t)col
>= pos
- numCols
)
2488 // ...either decrement row counter (if row still exists)...
2489 coords
.SetCol(col
+ numCols
);
2493 // ...or remove the attribute
2494 // No need to DecRef the attribute itself since this is
2495 // done be wxGridCellWithAttr's destructor!
2496 m_attrs
.RemoveAt(n
);
2504 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2506 size_t count
= m_attrs
.GetCount();
2507 for ( size_t n
= 0; n
< count
; n
++ )
2509 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2510 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2519 // ----------------------------------------------------------------------------
2520 // wxGridRowOrColAttrData
2521 // ----------------------------------------------------------------------------
2523 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2525 size_t count
= m_attrs
.Count();
2526 for ( size_t n
= 0; n
< count
; n
++ )
2528 m_attrs
[n
]->DecRef();
2532 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2534 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2536 int n
= m_rowsOrCols
.Index(rowOrCol
);
2537 if ( n
!= wxNOT_FOUND
)
2539 attr
= m_attrs
[(size_t)n
];
2546 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2548 int i
= m_rowsOrCols
.Index(rowOrCol
);
2549 if ( i
== wxNOT_FOUND
)
2551 // add the attribute
2552 m_rowsOrCols
.Add(rowOrCol
);
2557 size_t n
= (size_t)i
;
2560 // change the attribute
2561 m_attrs
[n
]->DecRef();
2566 // remove this attribute
2567 m_attrs
[n
]->DecRef();
2568 m_rowsOrCols
.RemoveAt(n
);
2569 m_attrs
.RemoveAt(n
);
2574 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2576 size_t count
= m_attrs
.GetCount();
2577 for ( size_t n
= 0; n
< count
; n
++ )
2579 int & rowOrCol
= m_rowsOrCols
[n
];
2580 if ( (size_t)rowOrCol
>= pos
)
2582 if ( numRowsOrCols
> 0 )
2584 // If rows inserted, include row counter where necessary
2585 rowOrCol
+= numRowsOrCols
;
2587 else if ( numRowsOrCols
< 0)
2589 // If rows deleted, either decrement row counter (if row still exists)
2590 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2591 rowOrCol
+= numRowsOrCols
;
2594 m_rowsOrCols
.RemoveAt(n
);
2595 m_attrs
[n
]->DecRef();
2596 m_attrs
.RemoveAt(n
);
2604 // ----------------------------------------------------------------------------
2605 // wxGridCellAttrProvider
2606 // ----------------------------------------------------------------------------
2608 wxGridCellAttrProvider::wxGridCellAttrProvider()
2610 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2613 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2618 void wxGridCellAttrProvider::InitData()
2620 m_data
= new wxGridCellAttrProviderData
;
2623 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2624 wxGridCellAttr::wxAttrKind kind
) const
2626 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2631 case (wxGridCellAttr::Any
):
2632 //Get cached merge attributes.
2633 // Currenlty not used as no cache implemented as not mutiable
2634 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2637 //Basicaly implement old version.
2638 //Also check merge cache, so we don't have to re-merge every time..
2639 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2640 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2641 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2643 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2644 // Two or more are non NULL
2645 attr
= new wxGridCellAttr
;
2646 attr
->SetKind(wxGridCellAttr::Merged
);
2650 attr
->MergeWith(attrcell
);
2654 attr
->MergeWith(attrcol
);
2658 attr
->MergeWith(attrrow
);
2661 //store merge attr if cache implemented
2663 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2667 // one or none is non null return it or null.
2668 if(attrrow
) attr
= attrrow
;
2684 case (wxGridCellAttr::Cell
):
2685 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2687 case (wxGridCellAttr::Col
):
2688 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2690 case (wxGridCellAttr::Row
):
2691 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2695 // (wxGridCellAttr::Default):
2696 // (wxGridCellAttr::Merged):
2703 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2709 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2712 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2717 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2720 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2725 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2728 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2732 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2734 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2738 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2742 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2744 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2748 // ----------------------------------------------------------------------------
2749 // wxGridTypeRegistry
2750 // ----------------------------------------------------------------------------
2752 wxGridTypeRegistry::~wxGridTypeRegistry()
2754 size_t count
= m_typeinfo
.Count();
2755 for ( size_t i
= 0; i
< count
; i
++ )
2756 delete m_typeinfo
[i
];
2760 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2761 wxGridCellRenderer
* renderer
,
2762 wxGridCellEditor
* editor
)
2764 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2766 // is it already registered?
2767 int loc
= FindRegisteredDataType(typeName
);
2768 if ( loc
!= wxNOT_FOUND
)
2770 delete m_typeinfo
[loc
];
2771 m_typeinfo
[loc
] = info
;
2775 m_typeinfo
.Add(info
);
2779 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2781 size_t count
= m_typeinfo
.GetCount();
2782 for ( size_t i
= 0; i
< count
; i
++ )
2784 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2793 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2795 int index
= FindRegisteredDataType(typeName
);
2796 if ( index
== wxNOT_FOUND
)
2798 // check whether this is one of the standard ones, in which case
2799 // register it "on the fly"
2801 if ( typeName
== wxGRID_VALUE_STRING
)
2803 RegisterDataType(wxGRID_VALUE_STRING
,
2804 new wxGridCellStringRenderer
,
2805 new wxGridCellTextEditor
);
2807 #endif // wxUSE_TEXTCTRL
2809 if ( typeName
== wxGRID_VALUE_BOOL
)
2811 RegisterDataType(wxGRID_VALUE_BOOL
,
2812 new wxGridCellBoolRenderer
,
2813 new wxGridCellBoolEditor
);
2815 #endif // wxUSE_CHECKBOX
2817 if ( typeName
== wxGRID_VALUE_NUMBER
)
2819 RegisterDataType(wxGRID_VALUE_NUMBER
,
2820 new wxGridCellNumberRenderer
,
2821 new wxGridCellNumberEditor
);
2823 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2825 RegisterDataType(wxGRID_VALUE_FLOAT
,
2826 new wxGridCellFloatRenderer
,
2827 new wxGridCellFloatEditor
);
2829 #endif // wxUSE_TEXTCTRL
2831 if ( typeName
== wxGRID_VALUE_CHOICE
)
2833 RegisterDataType(wxGRID_VALUE_CHOICE
,
2834 new wxGridCellStringRenderer
,
2835 new wxGridCellChoiceEditor
);
2837 #endif // wxUSE_COMBOBOX
2842 // we get here only if just added the entry for this type, so return
2844 index
= m_typeinfo
.GetCount() - 1;
2850 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2852 int index
= FindDataType(typeName
);
2853 if ( index
== wxNOT_FOUND
)
2855 // the first part of the typename is the "real" type, anything after ':'
2856 // are the parameters for the renderer
2857 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2858 if ( index
== wxNOT_FOUND
)
2863 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2864 wxGridCellRenderer
*rendererOld
= renderer
;
2865 renderer
= renderer
->Clone();
2866 rendererOld
->DecRef();
2868 wxGridCellEditor
*editor
= GetEditor(index
);
2869 wxGridCellEditor
*editorOld
= editor
;
2870 editor
= editor
->Clone();
2871 editorOld
->DecRef();
2873 // do it even if there are no parameters to reset them to defaults
2874 wxString params
= typeName
.AfterFirst(_T(':'));
2875 renderer
->SetParameters(params
);
2876 editor
->SetParameters(params
);
2878 // register the new typename
2879 RegisterDataType(typeName
, renderer
, editor
);
2881 // we just registered it, it's the last one
2882 index
= m_typeinfo
.GetCount() - 1;
2888 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2890 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2896 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2898 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2904 // ----------------------------------------------------------------------------
2906 // ----------------------------------------------------------------------------
2908 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2911 wxGridTableBase::wxGridTableBase()
2913 m_view
= (wxGrid
*) NULL
;
2914 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2917 wxGridTableBase::~wxGridTableBase()
2919 delete m_attrProvider
;
2922 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2924 delete m_attrProvider
;
2925 m_attrProvider
= attrProvider
;
2928 bool wxGridTableBase::CanHaveAttributes()
2930 if ( ! GetAttrProvider() )
2932 // use the default attr provider by default
2933 SetAttrProvider(new wxGridCellAttrProvider
);
2938 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2940 if ( m_attrProvider
)
2941 return m_attrProvider
->GetAttr(row
, col
, kind
);
2943 return (wxGridCellAttr
*)NULL
;
2947 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2949 if ( m_attrProvider
)
2951 attr
->SetKind(wxGridCellAttr::Cell
);
2952 m_attrProvider
->SetAttr(attr
, row
, col
);
2956 // as we take ownership of the pointer and don't store it, we must
2962 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2964 if ( m_attrProvider
)
2966 attr
->SetKind(wxGridCellAttr::Row
);
2967 m_attrProvider
->SetRowAttr(attr
, row
);
2971 // as we take ownership of the pointer and don't store it, we must
2977 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2979 if ( m_attrProvider
)
2981 attr
->SetKind(wxGridCellAttr::Col
);
2982 m_attrProvider
->SetColAttr(attr
, col
);
2986 // as we take ownership of the pointer and don't store it, we must
2992 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2993 size_t WXUNUSED(numRows
) )
2995 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3000 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3002 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3007 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3008 size_t WXUNUSED(numRows
) )
3010 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3015 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3016 size_t WXUNUSED(numCols
) )
3018 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3023 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3025 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3030 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3031 size_t WXUNUSED(numCols
) )
3033 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3039 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3042 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3043 // how much it makes sense to us geeks.
3047 wxString
wxGridTableBase::GetColLabelValue( int col
)
3049 // default col labels are:
3050 // cols 0 to 25 : A-Z
3051 // cols 26 to 675 : AA-ZZ
3056 for ( n
= 1; ; n
++ )
3058 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3060 if ( col
< 0 ) break;
3063 // reverse the string...
3065 for ( i
= 0; i
< n
; i
++ )
3074 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3076 return wxGRID_VALUE_STRING
;
3079 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3080 const wxString
& typeName
)
3082 return typeName
== wxGRID_VALUE_STRING
;
3085 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3087 return CanGetValueAs(row
, col
, typeName
);
3090 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3095 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3100 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3105 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3106 long WXUNUSED(value
) )
3110 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3111 double WXUNUSED(value
) )
3115 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3116 bool WXUNUSED(value
) )
3121 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3122 const wxString
& WXUNUSED(typeName
) )
3127 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3128 const wxString
& WXUNUSED(typeName
),
3129 void* WXUNUSED(value
) )
3133 //////////////////////////////////////////////////////////////////////
3135 // Message class for the grid table to send requests and notifications
3139 wxGridTableMessage::wxGridTableMessage()
3141 m_table
= (wxGridTableBase
*) NULL
;
3147 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3148 int commandInt1
, int commandInt2
)
3152 m_comInt1
= commandInt1
;
3153 m_comInt2
= commandInt2
;
3158 //////////////////////////////////////////////////////////////////////
3160 // A basic grid table for string data. An object of this class will
3161 // created by wxGrid if you don't specify an alternative table class.
3164 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3166 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3168 wxGridStringTable::wxGridStringTable()
3173 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3176 m_data
.Alloc( numRows
);
3179 sa
.Alloc( numCols
);
3180 sa
.Add( wxEmptyString
, numCols
);
3182 m_data
.Add( sa
, numRows
);
3185 wxGridStringTable::~wxGridStringTable()
3189 int wxGridStringTable::GetNumberRows()
3191 return m_data
.GetCount();
3194 int wxGridStringTable::GetNumberCols()
3196 if ( m_data
.GetCount() > 0 )
3197 return m_data
[0].GetCount();
3202 wxString
wxGridStringTable::GetValue( int row
, int col
)
3204 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3206 _T("invalid row or column index in wxGridStringTable") );
3208 return m_data
[row
][col
];
3211 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3213 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3214 _T("invalid row or column index in wxGridStringTable") );
3216 m_data
[row
][col
] = value
;
3219 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3221 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3223 _T("invalid row or column index in wxGridStringTable") );
3225 return (m_data
[row
][col
] == wxEmptyString
);
3228 void wxGridStringTable::Clear()
3231 int numRows
, numCols
;
3233 numRows
= m_data
.GetCount();
3236 numCols
= m_data
[0].GetCount();
3238 for ( row
= 0; row
< numRows
; row
++ )
3240 for ( col
= 0; col
< numCols
; col
++ )
3242 m_data
[row
][col
] = wxEmptyString
;
3249 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3251 size_t curNumRows
= m_data
.GetCount();
3252 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3253 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3255 if ( pos
>= curNumRows
)
3257 return AppendRows( numRows
);
3261 sa
.Alloc( curNumCols
);
3262 sa
.Add( wxEmptyString
, curNumCols
);
3263 m_data
.Insert( sa
, pos
, numRows
);
3266 wxGridTableMessage
msg( this,
3267 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3271 GetView()->ProcessTableMessage( msg
);
3277 bool wxGridStringTable::AppendRows( size_t numRows
)
3279 size_t curNumRows
= m_data
.GetCount();
3280 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3281 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3284 if ( curNumCols
> 0 )
3286 sa
.Alloc( curNumCols
);
3287 sa
.Add( wxEmptyString
, curNumCols
);
3290 m_data
.Add( sa
, numRows
);
3294 wxGridTableMessage
msg( this,
3295 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3298 GetView()->ProcessTableMessage( msg
);
3304 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3306 size_t curNumRows
= m_data
.GetCount();
3308 if ( pos
>= curNumRows
)
3310 wxFAIL_MSG( wxString::Format
3312 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3314 (unsigned long)numRows
,
3315 (unsigned long)curNumRows
3321 if ( numRows
> curNumRows
- pos
)
3323 numRows
= curNumRows
- pos
;
3326 if ( numRows
>= curNumRows
)
3332 m_data
.RemoveAt( pos
, numRows
);
3336 wxGridTableMessage
msg( this,
3337 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3341 GetView()->ProcessTableMessage( msg
);
3347 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3351 size_t curNumRows
= m_data
.GetCount();
3352 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3353 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3355 if ( pos
>= curNumCols
)
3357 return AppendCols( numCols
);
3360 for ( row
= 0; row
< curNumRows
; row
++ )
3362 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3364 m_data
[row
].Insert( wxEmptyString
, col
);
3369 wxGridTableMessage
msg( this,
3370 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3374 GetView()->ProcessTableMessage( msg
);
3380 bool wxGridStringTable::AppendCols( size_t numCols
)
3384 size_t curNumRows
= m_data
.GetCount();
3388 // TODO: something better than this ?
3390 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3395 for ( row
= 0; row
< curNumRows
; row
++ )
3397 m_data
[row
].Add( wxEmptyString
, numCols
);
3402 wxGridTableMessage
msg( this,
3403 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3406 GetView()->ProcessTableMessage( msg
);
3412 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3416 size_t curNumRows
= m_data
.GetCount();
3417 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3418 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3420 if ( pos
>= curNumCols
)
3422 wxFAIL_MSG( wxString::Format
3424 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3426 (unsigned long)numCols
,
3427 (unsigned long)curNumCols
3432 if ( numCols
> curNumCols
- pos
)
3434 numCols
= curNumCols
- pos
;
3437 for ( row
= 0; row
< curNumRows
; row
++ )
3439 if ( numCols
>= curNumCols
)
3441 m_data
[row
].Clear();
3445 m_data
[row
].RemoveAt( pos
, numCols
);
3450 wxGridTableMessage
msg( this,
3451 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3455 GetView()->ProcessTableMessage( msg
);
3461 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3463 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3465 // using default label
3467 return wxGridTableBase::GetRowLabelValue( row
);
3471 return m_rowLabels
[ row
];
3475 wxString
wxGridStringTable::GetColLabelValue( int col
)
3477 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3479 // using default label
3481 return wxGridTableBase::GetColLabelValue( col
);
3485 return m_colLabels
[ col
];
3489 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3491 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3493 int n
= m_rowLabels
.GetCount();
3495 for ( i
= n
; i
<= row
; i
++ )
3497 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3501 m_rowLabels
[row
] = value
;
3504 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3506 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3508 int n
= m_colLabels
.GetCount();
3510 for ( i
= n
; i
<= col
; i
++ )
3512 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3516 m_colLabels
[col
] = value
;
3521 //////////////////////////////////////////////////////////////////////
3522 //////////////////////////////////////////////////////////////////////
3524 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3526 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3527 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3528 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3529 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3530 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3531 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3532 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3535 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3537 const wxPoint
&pos
, const wxSize
&size
)
3538 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3543 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3547 // NO - don't do this because it will set both the x and y origin
3548 // coords to match the parent scrolled window and we just want to
3549 // set the y coord - MB
3551 // m_owner->PrepareDC( dc );
3554 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3555 dc
.SetDeviceOrigin( 0, -y
);
3557 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3558 m_owner
->DrawRowLabels( dc
, rows
);
3562 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3564 m_owner
->ProcessRowLabelMouseEvent( event
);
3568 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3570 m_owner
->GetEventHandler()->ProcessEvent(event
);
3574 // This seems to be required for wxMotif otherwise the mouse
3575 // cursor must be in the cell edit control to get key events
3577 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3579 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3582 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3584 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3587 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3589 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3594 //////////////////////////////////////////////////////////////////////
3596 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3598 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3599 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3600 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3601 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3602 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3603 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3604 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3607 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3609 const wxPoint
&pos
, const wxSize
&size
)
3610 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3615 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3619 // NO - don't do this because it will set both the x and y origin
3620 // coords to match the parent scrolled window and we just want to
3621 // set the x coord - MB
3623 // m_owner->PrepareDC( dc );
3626 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3627 dc
.SetDeviceOrigin( -x
, 0 );
3629 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3630 m_owner
->DrawColLabels( dc
, cols
);
3634 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3636 m_owner
->ProcessColLabelMouseEvent( event
);
3639 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3641 m_owner
->GetEventHandler()->ProcessEvent(event
);
3645 // This seems to be required for wxMotif otherwise the mouse
3646 // cursor must be in the cell edit control to get key events
3648 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3650 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3653 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3655 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3658 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3660 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3664 //////////////////////////////////////////////////////////////////////
3666 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3668 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3669 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3670 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3671 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3672 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3673 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3674 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3677 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3679 const wxPoint
&pos
, const wxSize
&size
)
3680 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3685 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3689 int client_height
= 0;
3690 int client_width
= 0;
3691 GetClientSize( &client_width
, &client_height
);
3693 // VZ: any reason for this ifdef? (FIXME)
3698 rect
.SetWidth( client_width
- 2 );
3699 rect
.SetHeight( client_height
- 2 );
3701 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3703 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3704 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3705 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3706 dc
.DrawLine( 0, 0, client_width
, 0 );
3707 dc
.DrawLine( 0, 0, 0, client_height
);
3709 dc
.SetPen( *wxWHITE_PEN
);
3710 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3711 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3712 #endif // __WXGTK__/!__WXGTK__
3716 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3718 m_owner
->ProcessCornerLabelMouseEvent( event
);
3722 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3724 m_owner
->GetEventHandler()->ProcessEvent(event
);
3727 // This seems to be required for wxMotif otherwise the mouse
3728 // cursor must be in the cell edit control to get key events
3730 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3732 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3735 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3737 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3740 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3742 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3746 //////////////////////////////////////////////////////////////////////
3748 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3750 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3751 EVT_PAINT( wxGridWindow::OnPaint
)
3752 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3753 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3754 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3755 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3756 EVT_CHAR ( wxGridWindow::OnChar
)
3757 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3758 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3759 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3762 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3763 wxGridRowLabelWindow
*rowLblWin
,
3764 wxGridColLabelWindow
*colLblWin
,
3767 const wxSize
&size
)
3768 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3769 wxT("grid window") )
3773 m_rowLabelWin
= rowLblWin
;
3774 m_colLabelWin
= colLblWin
;
3778 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3780 wxPaintDC
dc( this );
3781 m_owner
->PrepareDC( dc
);
3782 wxRegion reg
= GetUpdateRegion();
3783 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3784 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3785 #if WXGRID_DRAW_LINES
3786 m_owner
->DrawAllGridLines( dc
, reg
);
3788 m_owner
->DrawGridSpace( dc
);
3789 m_owner
->DrawHighlight( dc
, DirtyCells
);
3793 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3795 wxWindow::ScrollWindow( dx
, dy
, rect
);
3796 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3797 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3801 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3803 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3806 m_owner
->ProcessGridCellMouseEvent( event
);
3809 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3811 m_owner
->GetEventHandler()->ProcessEvent(event
);
3814 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3815 // cursor must be in the cell edit control to get key events
3817 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3819 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3822 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3824 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3827 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3829 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3832 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3836 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3838 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3842 //////////////////////////////////////////////////////////////////////
3844 // Internal Helper function for computing row or column from some
3845 // (unscrolled) coordinate value, using either
3846 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3847 // of m_rowBottoms/m_ColRights to speed up the search!
3849 // Internal helper macros for simpler use of that function
3851 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3852 const wxArrayInt
& BorderArray
, int nMax
,
3855 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3856 m_minAcceptableColWidth, \
3857 m_colRights, m_numCols, true)
3858 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3859 m_minAcceptableRowHeight, \
3860 m_rowBottoms, m_numRows, true)
3861 /////////////////////////////////////////////////////////////////////
3863 #if wxUSE_EXTENDED_RTTI
3864 WX_DEFINE_FLAGS( wxGridStyle
)
3866 wxBEGIN_FLAGS( wxGridStyle
)
3867 // new style border flags, we put them first to
3868 // use them for streaming out
3869 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3870 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3871 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3872 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3873 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3874 wxFLAGS_MEMBER(wxBORDER_NONE
)
3876 // old style border flags
3877 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3878 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3879 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3880 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3881 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3882 wxFLAGS_MEMBER(wxBORDER
)
3884 // standard window styles
3885 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3886 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3887 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3888 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3889 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3890 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3891 wxFLAGS_MEMBER(wxVSCROLL
)
3892 wxFLAGS_MEMBER(wxHSCROLL
)
3894 wxEND_FLAGS( wxGridStyle
)
3896 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3898 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3899 wxHIDE_PROPERTY( Children
)
3900 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3901 wxEND_PROPERTIES_TABLE()
3903 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3904 wxEND_HANDLERS_TABLE()
3906 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3909 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3912 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3915 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3916 EVT_PAINT( wxGrid::OnPaint
)
3917 EVT_SIZE( wxGrid::OnSize
)
3918 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3919 EVT_KEY_UP( wxGrid::OnKeyUp
)
3920 EVT_CHAR ( wxGrid::OnChar
)
3921 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3926 // in order to make sure that a size event is not
3927 // trigerred in a unfinished state
3928 m_cornerLabelWin
= NULL
;
3929 m_rowLabelWin
= NULL
;
3930 m_colLabelWin
= NULL
;
3934 wxGrid::wxGrid( wxWindow
*parent
,
3939 const wxString
& name
)
3940 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3941 m_colMinWidths(GRID_HASH_SIZE
),
3942 m_rowMinHeights(GRID_HASH_SIZE
)
3945 SetBestFittingSize(size
);
3948 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3949 const wxPoint
& pos
, const wxSize
& size
,
3950 long style
, const wxString
& name
)
3952 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3953 style
| wxWANTS_CHARS
, name
))
3956 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3957 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3960 SetBestFittingSize(size
);
3968 // Must do this or ~wxScrollHelper will pop the wrong event handler
3969 SetTargetWindow(this);
3971 wxSafeDecRef(m_defaultCellAttr
);
3973 #ifdef DEBUG_ATTR_CACHE
3974 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3975 wxPrintf(_T("wxGrid attribute cache statistics: "
3976 "total: %u, hits: %u (%u%%)\n"),
3977 total
, gs_nAttrCacheHits
,
3978 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3984 delete m_typeRegistry
;
3990 // ----- internal init and update functions
3993 // NOTE: If using the default visual attributes works everywhere then this can
3994 // be removed as well as the #else cases below.
3995 #define _USE_VISATTR 0
3998 #include "wx/listbox.h"
4001 void wxGrid::Create()
4003 m_created
= false; // set to true by CreateGrid
4005 m_table
= (wxGridTableBase
*) NULL
;
4008 m_cellEditCtrlEnabled
= false;
4010 m_defaultCellAttr
= new wxGridCellAttr();
4012 // Set default cell attributes
4013 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4014 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4015 m_defaultCellAttr
->SetFont(GetFont());
4016 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4017 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4018 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4021 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4022 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4024 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4025 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4028 m_defaultCellAttr
->SetTextColour(
4029 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4030 m_defaultCellAttr
->SetBackgroundColour(
4031 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4036 m_currentCellCoords
= wxGridNoCellCoords
;
4038 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4039 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4041 // create the type registry
4042 m_typeRegistry
= new wxGridTypeRegistry
;
4045 // subwindow components that make up the wxGrid
4046 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4051 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4056 m_colLabelWin
= new wxGridColLabelWindow( this,
4061 m_gridWin
= new wxGridWindow( this,
4068 SetTargetWindow( m_gridWin
);
4071 wxColour gfg
= gva
.colFg
;
4072 wxColour gbg
= gva
.colBg
;
4073 wxColour lfg
= lva
.colFg
;
4074 wxColour lbg
= lva
.colBg
;
4076 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4077 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4078 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4079 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4081 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4082 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4083 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4084 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4085 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4086 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4088 m_gridWin
->SetOwnForegroundColour(gfg
);
4089 m_gridWin
->SetOwnBackgroundColour(gbg
);
4095 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4096 wxGrid::wxGridSelectionModes selmode
)
4098 wxCHECK_MSG( !m_created
,
4100 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4102 m_numRows
= numRows
;
4103 m_numCols
= numCols
;
4105 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4106 m_table
->SetView( this );
4108 m_selection
= new wxGridSelection( this, selmode
);
4117 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4119 wxCHECK_RET( m_created
,
4120 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4122 m_selection
->SetSelectionMode( selmode
);
4125 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4127 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4128 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4130 return m_selection
->GetSelectionMode();
4133 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4134 wxGrid::wxGridSelectionModes selmode
)
4138 // stop all processing
4143 wxGridTableBase
*t
=m_table
;
4156 m_numRows
= table
->GetNumberRows();
4157 m_numCols
= table
->GetNumberCols();
4160 m_table
->SetView( this );
4163 m_selection
= new wxGridSelection( this, selmode
);
4176 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4177 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4179 if ( m_rowLabelWin
)
4181 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4185 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4188 m_labelTextColour
= wxColour( _T("BLACK") );
4191 m_attrCache
.row
= -1;
4192 m_attrCache
.col
= -1;
4193 m_attrCache
.attr
= NULL
;
4195 // TODO: something better than this ?
4197 m_labelFont
= this->GetFont();
4198 m_labelFont
.SetWeight( wxBOLD
);
4200 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4201 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4203 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4204 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4205 m_colLabelTextOrientation
= wxHORIZONTAL
;
4207 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4208 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4210 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4211 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4213 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4214 m_defaultRowHeight
+= 8;
4216 m_defaultRowHeight
+= 4;
4219 m_gridLineColour
= wxColour( 192,192,192 );
4220 m_gridLinesEnabled
= true;
4221 m_cellHighlightColour
= *wxBLACK
;
4222 m_cellHighlightPenWidth
= 2;
4223 m_cellHighlightROPenWidth
= 1;
4225 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4226 m_winCapture
= (wxWindow
*)NULL
;
4227 m_canDragRowSize
= true;
4228 m_canDragColSize
= true;
4229 m_canDragGridSize
= true;
4230 m_canDragCell
= false;
4232 m_dragRowOrCol
= -1;
4233 m_isDragging
= false;
4234 m_startDragPos
= wxDefaultPosition
;
4236 m_waitForSlowClick
= false;
4238 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4239 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4241 m_currentCellCoords
= wxGridNoCellCoords
;
4243 m_selectingTopLeft
= wxGridNoCellCoords
;
4244 m_selectingBottomRight
= wxGridNoCellCoords
;
4245 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4246 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4248 m_editable
= true; // default for whole grid
4250 m_inOnKeyDown
= false;
4256 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4257 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4260 // ----------------------------------------------------------------------------
4261 // the idea is to call these functions only when necessary because they create
4262 // quite big arrays which eat memory mostly unnecessary - in particular, if
4263 // default widths/heights are used for all rows/columns, we may not use these
4266 // with some extra code, it should be possible to only store the
4267 // widths/heights different from default ones but this will be done later...
4268 // ----------------------------------------------------------------------------
4270 void wxGrid::InitRowHeights()
4272 m_rowHeights
.Empty();
4273 m_rowBottoms
.Empty();
4275 m_rowHeights
.Alloc( m_numRows
);
4276 m_rowBottoms
.Alloc( m_numRows
);
4280 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4282 for ( int i
= 0; i
< m_numRows
; i
++ )
4284 rowBottom
+= m_defaultRowHeight
;
4285 m_rowBottoms
.Add( rowBottom
);
4289 void wxGrid::InitColWidths()
4291 m_colWidths
.Empty();
4292 m_colRights
.Empty();
4294 m_colWidths
.Alloc( m_numCols
);
4295 m_colRights
.Alloc( m_numCols
);
4298 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4300 for ( int i
= 0; i
< m_numCols
; i
++ )
4302 colRight
+= m_defaultColWidth
;
4303 m_colRights
.Add( colRight
);
4307 int wxGrid::GetColWidth(int col
) const
4309 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4312 int wxGrid::GetColLeft(int col
) const
4314 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4315 : m_colRights
[col
] - m_colWidths
[col
];
4318 int wxGrid::GetColRight(int col
) const
4320 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4324 int wxGrid::GetRowHeight(int row
) const
4326 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4329 int wxGrid::GetRowTop(int row
) const
4331 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4332 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4335 int wxGrid::GetRowBottom(int row
) const
4337 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4338 : m_rowBottoms
[row
];
4341 void wxGrid::CalcDimensions()
4344 GetClientSize( &cw
, &ch
);
4346 if ( m_rowLabelWin
->IsShown() )
4347 cw
-= m_rowLabelWidth
;
4348 if ( m_colLabelWin
->IsShown() )
4349 ch
-= m_colLabelHeight
;
4352 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4353 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4355 // take into account editor if shown
4356 if( IsCellEditControlShown() )
4359 int r
= m_currentCellCoords
.GetRow();
4360 int c
= m_currentCellCoords
.GetCol();
4361 int x
= GetColLeft(c
);
4362 int y
= GetRowTop(r
);
4364 // how big is the editor
4365 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4366 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4367 editor
->GetControl()->GetSize(&w2
, &h2
);
4370 if( w2
> w
) w
= w2
;
4371 if( h2
> h
) h
= h2
;
4376 // preserve (more or less) the previous position
4378 GetViewStart( &x
, &y
);
4380 // ensure the position is valid for the new scroll ranges
4382 x
= wxMax( w
- 1, 0 );
4384 y
= wxMax( h
- 1, 0 );
4386 // do set scrollbar parameters
4387 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4388 GetScrollX(w
), GetScrollY(h
), x
, y
,
4389 GetBatchCount() != 0);
4391 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4392 // still must reposition the children
4397 void wxGrid::CalcWindowSizes()
4399 // escape if the window is has not been fully created yet
4401 if ( m_cornerLabelWin
== NULL
)
4405 GetClientSize( &cw
, &ch
);
4407 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4408 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4410 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4411 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4413 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4414 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4416 if ( m_gridWin
&& m_gridWin
->IsShown() )
4417 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4421 // this is called when the grid table sends a message to say that it
4422 // has been redimensioned
4424 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4427 bool result
= false;
4429 // Clear the attribute cache as the attribute might refer to a different
4430 // cell than stored in the cache after adding/removing rows/columns.
4432 // By the same reasoning, the editor should be dismissed if columns are
4433 // added or removed. And for consistency, it should IMHO always be
4434 // removed, not only if the cell "underneath" it actually changes.
4435 // For now, I intentionally do not save the editor's content as the
4436 // cell it might want to save that stuff to might no longer exist.
4437 HideCellEditControl();
4439 // if we were using the default widths/heights so far, we must change them
4441 if ( m_colWidths
.IsEmpty() )
4446 if ( m_rowHeights
.IsEmpty() )
4452 switch ( msg
.GetId() )
4454 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4456 size_t pos
= msg
.GetCommandInt();
4457 int numRows
= msg
.GetCommandInt2();
4459 m_numRows
+= numRows
;
4461 if ( !m_rowHeights
.IsEmpty() )
4463 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4464 m_rowBottoms
.Insert( 0, pos
, numRows
);
4467 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4469 for ( i
= pos
; i
< m_numRows
; i
++ )
4471 bottom
+= m_rowHeights
[i
];
4472 m_rowBottoms
[i
] = bottom
;
4475 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4477 // if we have just inserted cols into an empty grid the current
4478 // cell will be undefined...
4480 SetCurrentCell( 0, 0 );
4484 m_selection
->UpdateRows( pos
, numRows
);
4485 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4487 attrProvider
->UpdateAttrRows( pos
, numRows
);
4489 if ( !GetBatchCount() )
4492 m_rowLabelWin
->Refresh();
4498 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4500 int numRows
= msg
.GetCommandInt();
4501 int oldNumRows
= m_numRows
;
4502 m_numRows
+= numRows
;
4504 if ( !m_rowHeights
.IsEmpty() )
4506 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4507 m_rowBottoms
.Add( 0, numRows
);
4510 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4512 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4514 bottom
+= m_rowHeights
[i
];
4515 m_rowBottoms
[i
] = bottom
;
4518 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4520 // if we have just inserted cols into an empty grid the current
4521 // cell will be undefined...
4523 SetCurrentCell( 0, 0 );
4525 if ( !GetBatchCount() )
4528 m_rowLabelWin
->Refresh();
4534 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4536 size_t pos
= msg
.GetCommandInt();
4537 int numRows
= msg
.GetCommandInt2();
4538 m_numRows
-= numRows
;
4540 if ( !m_rowHeights
.IsEmpty() )
4542 m_rowHeights
.RemoveAt( pos
, numRows
);
4543 m_rowBottoms
.RemoveAt( pos
, numRows
);
4546 for ( i
= 0; i
< m_numRows
; i
++ )
4548 h
+= m_rowHeights
[i
];
4549 m_rowBottoms
[i
] = h
;
4554 m_currentCellCoords
= wxGridNoCellCoords
;
4558 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4559 m_currentCellCoords
.Set( 0, 0 );
4563 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4564 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4566 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4567 // ifdef'd out following patch from Paul Gammans
4569 // No need to touch column attributes, unless we
4570 // removed _all_ rows, in this case, we remove
4571 // all column attributes.
4572 // I hate to do this here, but the
4573 // needed data is not available inside UpdateAttrRows.
4574 if ( !GetNumberRows() )
4575 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4578 if ( !GetBatchCount() )
4581 m_rowLabelWin
->Refresh();
4587 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4589 size_t pos
= msg
.GetCommandInt();
4590 int numCols
= msg
.GetCommandInt2();
4591 m_numCols
+= numCols
;
4593 if ( !m_colWidths
.IsEmpty() )
4595 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4596 m_colRights
.Insert( 0, pos
, numCols
);
4599 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4601 for ( i
= pos
; i
< m_numCols
; i
++ )
4603 right
+= m_colWidths
[i
];
4604 m_colRights
[i
] = right
;
4607 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4609 // if we have just inserted cols into an empty grid the current
4610 // cell will be undefined...
4612 SetCurrentCell( 0, 0 );
4616 m_selection
->UpdateCols( pos
, numCols
);
4617 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4619 attrProvider
->UpdateAttrCols( pos
, numCols
);
4620 if ( !GetBatchCount() )
4623 m_colLabelWin
->Refresh();
4630 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4632 int numCols
= msg
.GetCommandInt();
4633 int oldNumCols
= m_numCols
;
4634 m_numCols
+= numCols
;
4635 if ( !m_colWidths
.IsEmpty() )
4637 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4638 m_colRights
.Add( 0, numCols
);
4641 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4643 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4645 right
+= m_colWidths
[i
];
4646 m_colRights
[i
] = right
;
4649 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4651 // if we have just inserted cols into an empty grid the current
4652 // cell will be undefined...
4654 SetCurrentCell( 0, 0 );
4656 if ( !GetBatchCount() )
4659 m_colLabelWin
->Refresh();
4665 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4667 size_t pos
= msg
.GetCommandInt();
4668 int numCols
= msg
.GetCommandInt2();
4669 m_numCols
-= numCols
;
4671 if ( !m_colWidths
.IsEmpty() )
4673 m_colWidths
.RemoveAt( pos
, numCols
);
4674 m_colRights
.RemoveAt( pos
, numCols
);
4677 for ( i
= 0; i
< m_numCols
; i
++ )
4679 w
+= m_colWidths
[i
];
4685 m_currentCellCoords
= wxGridNoCellCoords
;
4689 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4690 m_currentCellCoords
.Set( 0, 0 );
4694 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4695 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4697 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4698 // ifdef'd out following patch from Paul Gammans
4700 // No need to touch row attributes, unless we
4701 // removed _all_ columns, in this case, we remove
4702 // all row attributes.
4703 // I hate to do this here, but the
4704 // needed data is not available inside UpdateAttrCols.
4705 if ( !GetNumberCols() )
4706 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4709 if ( !GetBatchCount() )
4712 m_colLabelWin
->Refresh();
4719 if (result
&& !GetBatchCount() )
4720 m_gridWin
->Refresh();
4725 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4727 wxRegionIterator
iter( reg
);
4730 wxArrayInt rowlabels
;
4737 // TODO: remove this when we can...
4738 // There is a bug in wxMotif that gives garbage update
4739 // rectangles if you jump-scroll a long way by clicking the
4740 // scrollbar with middle button. This is a work-around
4742 #if defined(__WXMOTIF__)
4744 m_gridWin
->GetClientSize( &cw
, &ch
);
4745 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4746 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4749 // logical bounds of update region
4752 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4753 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4755 // find the row labels within these bounds
4758 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4760 if ( GetRowBottom(row
) < top
)
4763 if ( GetRowTop(row
) > bottom
)
4766 rowlabels
.Add( row
);
4776 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4778 wxRegionIterator
iter( reg
);
4781 wxArrayInt colLabels
;
4788 // TODO: remove this when we can...
4789 // There is a bug in wxMotif that gives garbage update
4790 // rectangles if you jump-scroll a long way by clicking the
4791 // scrollbar with middle button. This is a work-around
4793 #if defined(__WXMOTIF__)
4795 m_gridWin
->GetClientSize( &cw
, &ch
);
4796 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4797 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4800 // logical bounds of update region
4803 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4804 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4806 // find the cells within these bounds
4809 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4811 if ( GetColRight(col
) < left
)
4814 if ( GetColLeft(col
) > right
)
4817 colLabels
.Add( col
);
4826 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4828 wxRegionIterator
iter( reg
);
4831 wxGridCellCoordsArray cellsExposed
;
4833 int left
, top
, right
, bottom
;
4838 // TODO: remove this when we can...
4839 // There is a bug in wxMotif that gives garbage update
4840 // rectangles if you jump-scroll a long way by clicking the
4841 // scrollbar with middle button. This is a work-around
4843 #if defined(__WXMOTIF__)
4845 m_gridWin
->GetClientSize( &cw
, &ch
);
4846 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4847 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4848 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4849 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4852 // logical bounds of update region
4854 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4855 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4857 // find the cells within these bounds
4860 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4862 if ( GetRowBottom(row
) <= top
)
4865 if ( GetRowTop(row
) > bottom
)
4868 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4870 if ( GetColRight(col
) <= left
)
4873 if ( GetColLeft(col
) > right
)
4876 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4883 return cellsExposed
;
4887 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4890 wxPoint
pos( event
.GetPosition() );
4891 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4893 if ( event
.Dragging() )
4897 m_isDragging
= true;
4898 m_rowLabelWin
->CaptureMouse();
4901 if ( event
.LeftIsDown() )
4903 switch( m_cursorMode
)
4905 case WXGRID_CURSOR_RESIZE_ROW
:
4907 int cw
, ch
, left
, dummy
;
4908 m_gridWin
->GetClientSize( &cw
, &ch
);
4909 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4911 wxClientDC
dc( m_gridWin
);
4914 GetRowTop(m_dragRowOrCol
) +
4915 GetRowMinimalHeight(m_dragRowOrCol
) );
4916 dc
.SetLogicalFunction(wxINVERT
);
4917 if ( m_dragLastPos
>= 0 )
4919 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4921 dc
.DrawLine( left
, y
, left
+cw
, y
);
4926 case WXGRID_CURSOR_SELECT_ROW
:
4928 if ( (row
= YToRow( y
)) >= 0 )
4932 m_selection
->SelectRow( row
,
4933 event
.ControlDown(),
4942 // default label to suppress warnings about "enumeration value
4943 // 'xxx' not handled in switch
4951 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4956 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4957 m_isDragging
= false;
4960 // ------------ Entering or leaving the window
4962 if ( event
.Entering() || event
.Leaving() )
4964 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4968 // ------------ Left button pressed
4970 else if ( event
.LeftDown() )
4972 // don't send a label click event for a hit on the
4973 // edge of the row label - this is probably the user
4974 // wanting to resize the row
4976 if ( YToEdgeOfRow(y
) < 0 )
4980 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4982 if ( !event
.ShiftDown() && !event
.ControlDown() )
4986 if ( event
.ShiftDown() )
4988 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4991 GetNumberCols() - 1,
4992 event
.ControlDown(),
4999 m_selection
->SelectRow( row
,
5000 event
.ControlDown(),
5007 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5012 // starting to drag-resize a row
5014 if ( CanDragRowSize() )
5015 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5020 // ------------ Left double click
5022 else if (event
.LeftDClick() )
5024 row
= YToEdgeOfRow(y
);
5029 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5031 // no default action at the moment
5036 // adjust row height depending on label text
5037 AutoSizeRowLabelSize( row
);
5039 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5045 // ------------ Left button released
5047 else if ( event
.LeftUp() )
5049 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5051 DoEndDragResizeRow();
5053 // Note: we are ending the event *after* doing
5054 // default processing in this case
5056 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5059 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5064 // ------------ Right button down
5066 else if ( event
.RightDown() )
5070 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5072 // no default action at the moment
5077 // ------------ Right double click
5079 else if ( event
.RightDClick() )
5083 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5085 // no default action at the moment
5090 // ------------ No buttons down and mouse moving
5092 else if ( event
.Moving() )
5094 m_dragRowOrCol
= YToEdgeOfRow( y
);
5095 if ( m_dragRowOrCol
>= 0 )
5097 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5099 // don't capture the mouse yet
5100 if ( CanDragRowSize() )
5101 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5104 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5106 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5112 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5115 wxPoint
pos( event
.GetPosition() );
5116 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5118 if ( event
.Dragging() )
5122 m_isDragging
= true;
5123 m_colLabelWin
->CaptureMouse();
5126 if ( event
.LeftIsDown() )
5128 switch( m_cursorMode
)
5130 case WXGRID_CURSOR_RESIZE_COL
:
5132 int cw
, ch
, dummy
, top
;
5133 m_gridWin
->GetClientSize( &cw
, &ch
);
5134 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5136 wxClientDC
dc( m_gridWin
);
5139 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5140 GetColMinimalWidth(m_dragRowOrCol
));
5141 dc
.SetLogicalFunction(wxINVERT
);
5142 if ( m_dragLastPos
>= 0 )
5144 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5146 dc
.DrawLine( x
, top
, x
, top
+ch
);
5151 case WXGRID_CURSOR_SELECT_COL
:
5153 if ( (col
= XToCol( x
)) >= 0 )
5157 m_selection
->SelectCol( col
,
5158 event
.ControlDown(),
5167 // default label to suppress warnings about "enumeration value
5168 // 'xxx' not handled in switch
5176 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5181 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5182 m_isDragging
= false;
5185 // ------------ Entering or leaving the window
5187 if ( event
.Entering() || event
.Leaving() )
5189 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5193 // ------------ Left button pressed
5195 else if ( event
.LeftDown() )
5197 // don't send a label click event for a hit on the
5198 // edge of the col label - this is probably the user
5199 // wanting to resize the col
5201 if ( XToEdgeOfCol(x
) < 0 )
5205 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5207 if ( !event
.ShiftDown() && !event
.ControlDown() )
5211 if ( event
.ShiftDown() )
5213 m_selection
->SelectBlock( 0,
5214 m_currentCellCoords
.GetCol(),
5215 GetNumberRows() - 1, col
,
5216 event
.ControlDown(),
5223 m_selection
->SelectCol( col
,
5224 event
.ControlDown(),
5231 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5236 // starting to drag-resize a col
5238 if ( CanDragColSize() )
5239 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5244 // ------------ Left double click
5246 if ( event
.LeftDClick() )
5248 col
= XToEdgeOfCol(x
);
5253 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5255 // no default action at the moment
5260 // adjust column width depending on label text
5261 AutoSizeColLabelSize( col
);
5263 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5269 // ------------ Left button released
5271 else if ( event
.LeftUp() )
5273 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5275 DoEndDragResizeCol();
5277 // Note: we are ending the event *after* doing
5278 // default processing in this case
5280 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5283 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5288 // ------------ Right button down
5290 else if ( event
.RightDown() )
5294 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5296 // no default action at the moment
5301 // ------------ Right double click
5303 else if ( event
.RightDClick() )
5307 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5309 // no default action at the moment
5314 // ------------ No buttons down and mouse moving
5316 else if ( event
.Moving() )
5318 m_dragRowOrCol
= XToEdgeOfCol( x
);
5319 if ( m_dragRowOrCol
>= 0 )
5321 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5323 // don't capture the cursor yet
5324 if ( CanDragColSize() )
5325 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5328 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5330 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5336 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5338 if ( event
.LeftDown() )
5340 // indicate corner label by having both row and
5343 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5349 else if ( event
.LeftDClick() )
5351 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5354 else if ( event
.RightDown() )
5356 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5358 // no default action at the moment
5362 else if ( event
.RightDClick() )
5364 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5366 // no default action at the moment
5371 void wxGrid::ChangeCursorMode(CursorMode mode
,
5376 static const wxChar
*cursorModes
[] =
5385 wxLogTrace(_T("grid"),
5386 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5387 win
== m_colLabelWin
? _T("colLabelWin")
5388 : win
? _T("rowLabelWin")
5390 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5391 #endif // __WXDEBUG__
5393 if ( mode
== m_cursorMode
&&
5394 win
== m_winCapture
&&
5395 captureMouse
== (m_winCapture
!= NULL
))
5400 // by default use the grid itself
5406 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5407 m_winCapture
= (wxWindow
*)NULL
;
5410 m_cursorMode
= mode
;
5412 switch ( m_cursorMode
)
5414 case WXGRID_CURSOR_RESIZE_ROW
:
5415 win
->SetCursor( m_rowResizeCursor
);
5418 case WXGRID_CURSOR_RESIZE_COL
:
5419 win
->SetCursor( m_colResizeCursor
);
5423 win
->SetCursor( *wxSTANDARD_CURSOR
);
5426 // we need to capture mouse when resizing
5427 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5428 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5430 if ( captureMouse
&& resize
)
5432 win
->CaptureMouse();
5437 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5440 wxPoint
pos( event
.GetPosition() );
5441 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5443 wxGridCellCoords coords
;
5444 XYToCell( x
, y
, coords
);
5446 int cell_rows
, cell_cols
;
5447 bool isFirstDrag
= !m_isDragging
;
5448 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5449 if ((cell_rows
< 0) || (cell_cols
< 0))
5451 coords
.SetRow(coords
.GetRow() + cell_rows
);
5452 coords
.SetCol(coords
.GetCol() + cell_cols
);
5455 if ( event
.Dragging() )
5457 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5459 // Don't start doing anything until the mouse has been drug at
5460 // least 3 pixels in any direction...
5463 if (m_startDragPos
== wxDefaultPosition
)
5465 m_startDragPos
= pos
;
5468 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5472 m_isDragging
= true;
5473 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5475 // Hide the edit control, so it
5476 // won't interfer with drag-shrinking.
5477 if ( IsCellEditControlShown() )
5479 HideCellEditControl();
5480 SaveEditControlValue();
5483 // Have we captured the mouse yet?
5486 m_winCapture
= m_gridWin
;
5487 m_winCapture
->CaptureMouse();
5490 if ( coords
!= wxGridNoCellCoords
)
5492 if ( event
.ControlDown() )
5494 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5495 m_selectingKeyboard
= coords
;
5496 HighlightBlock ( m_selectingKeyboard
, coords
);
5498 else if ( CanDragCell() )
5502 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5503 m_selectingKeyboard
= coords
;
5505 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5513 if ( !IsSelection() )
5515 HighlightBlock( coords
, coords
);
5519 HighlightBlock( m_currentCellCoords
, coords
);
5523 if (! IsVisible(coords
))
5525 MakeCellVisible(coords
);
5526 // TODO: need to introduce a delay or something here. The
5527 // scrolling is way to fast, at least on MSW - also on GTK.
5531 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5533 int cw
, ch
, left
, dummy
;
5534 m_gridWin
->GetClientSize( &cw
, &ch
);
5535 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5537 wxClientDC
dc( m_gridWin
);
5539 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5540 GetRowMinimalHeight(m_dragRowOrCol
) );
5541 dc
.SetLogicalFunction(wxINVERT
);
5542 if ( m_dragLastPos
>= 0 )
5544 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5546 dc
.DrawLine( left
, y
, left
+cw
, y
);
5549 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5551 int cw
, ch
, dummy
, top
;
5552 m_gridWin
->GetClientSize( &cw
, &ch
);
5553 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5555 wxClientDC
dc( m_gridWin
);
5557 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5558 GetColMinimalWidth(m_dragRowOrCol
) );
5559 dc
.SetLogicalFunction(wxINVERT
);
5560 if ( m_dragLastPos
>= 0 )
5562 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5564 dc
.DrawLine( x
, top
, x
, top
+ch
);
5571 m_isDragging
= false;
5572 m_startDragPos
= wxDefaultPosition
;
5574 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5575 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5578 if ( event
.Entering() || event
.Leaving() )
5580 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5581 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5586 // ------------ Left button pressed
5588 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5590 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5595 if ( !event
.ControlDown() )
5597 if ( event
.ShiftDown() )
5601 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5602 m_currentCellCoords
.GetCol(),
5605 event
.ControlDown(),
5611 else if ( XToEdgeOfCol(x
) < 0 &&
5612 YToEdgeOfRow(y
) < 0 )
5614 DisableCellEditControl();
5615 MakeCellVisible( coords
);
5617 if ( event
.ControlDown() )
5621 m_selection
->ToggleCellSelection( coords
.GetRow(),
5623 event
.ControlDown(),
5628 m_selectingTopLeft
= wxGridNoCellCoords
;
5629 m_selectingBottomRight
= wxGridNoCellCoords
;
5630 m_selectingKeyboard
= coords
;
5634 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5635 SetCurrentCell( coords
);
5638 if ( m_selection
->GetSelectionMode() !=
5639 wxGrid::wxGridSelectCells
)
5641 HighlightBlock( coords
, coords
);
5650 // ------------ Left double click
5652 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5654 DisableCellEditControl();
5656 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5658 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5663 // we want double click to select a cell and start editing
5664 // (i.e. to behave in same way as sequence of two slow clicks):
5665 m_waitForSlowClick
= true;
5672 // ------------ Left button released
5674 else if ( event
.LeftUp() )
5676 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5680 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5681 m_winCapture
= NULL
;
5684 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5687 EnableCellEditControl();
5689 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5690 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5691 editor
->StartingClick();
5695 m_waitForSlowClick
= false;
5697 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5698 m_selectingBottomRight
!= wxGridNoCellCoords
)
5702 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5703 m_selectingTopLeft
.GetCol(),
5704 m_selectingBottomRight
.GetRow(),
5705 m_selectingBottomRight
.GetCol(),
5706 event
.ControlDown(),
5712 m_selectingTopLeft
= wxGridNoCellCoords
;
5713 m_selectingBottomRight
= wxGridNoCellCoords
;
5715 // Show the edit control, if it has been hidden for
5717 ShowCellEditControl();
5720 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5722 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5723 DoEndDragResizeRow();
5725 // Note: we are ending the event *after* doing
5726 // default processing in this case
5728 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5730 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5732 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5733 DoEndDragResizeCol();
5735 // Note: we are ending the event *after* doing
5736 // default processing in this case
5738 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5745 // ------------ Right button down
5747 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5749 DisableCellEditControl();
5750 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5755 // no default action at the moment
5760 // ------------ Right double click
5762 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5764 DisableCellEditControl();
5765 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5770 // no default action at the moment
5774 // ------------ Moving and no button action
5776 else if ( event
.Moving() && !event
.IsButton() )
5778 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5780 // out of grid cell area
5781 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5785 int dragRow
= YToEdgeOfRow( y
);
5786 int dragCol
= XToEdgeOfCol( x
);
5788 // Dragging on the corner of a cell to resize in both
5789 // directions is not implemented yet...
5791 if ( dragRow
>= 0 && dragCol
>= 0 )
5793 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5799 m_dragRowOrCol
= dragRow
;
5801 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5803 if ( CanDragRowSize() && CanDragGridSize() )
5804 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5809 m_dragRowOrCol
= dragCol
;
5817 m_dragRowOrCol
= dragCol
;
5819 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5821 if ( CanDragColSize() && CanDragGridSize() )
5822 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5828 // Neither on a row or col edge
5830 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5832 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5838 void wxGrid::DoEndDragResizeRow()
5840 if ( m_dragLastPos
>= 0 )
5842 // erase the last line and resize the row
5844 int cw
, ch
, left
, dummy
;
5845 m_gridWin
->GetClientSize( &cw
, &ch
);
5846 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5848 wxClientDC
dc( m_gridWin
);
5850 dc
.SetLogicalFunction( wxINVERT
);
5851 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5852 HideCellEditControl();
5853 SaveEditControlValue();
5855 int rowTop
= GetRowTop(m_dragRowOrCol
);
5856 SetRowSize( m_dragRowOrCol
,
5857 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5859 if ( !GetBatchCount() )
5861 // Only needed to get the correct rect.y:
5862 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5864 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5865 rect
.width
= m_rowLabelWidth
;
5866 rect
.height
= ch
- rect
.y
;
5867 m_rowLabelWin
->Refresh( true, &rect
);
5869 // if there is a multicell block, paint all of it
5872 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5873 int leftCol
= XToCol(left
);
5874 int rightCol
= internalXToCol(left
+cw
);
5877 for (i
=leftCol
; i
<rightCol
; i
++)
5879 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5880 if (cell_rows
< subtract_rows
)
5881 subtract_rows
= cell_rows
;
5883 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5884 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5885 rect
.height
= ch
- rect
.y
;
5888 m_gridWin
->Refresh( false, &rect
);
5891 ShowCellEditControl();
5896 void wxGrid::DoEndDragResizeCol()
5898 if ( m_dragLastPos
>= 0 )
5900 // erase the last line and resize the col
5902 int cw
, ch
, dummy
, top
;
5903 m_gridWin
->GetClientSize( &cw
, &ch
);
5904 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5906 wxClientDC
dc( m_gridWin
);
5908 dc
.SetLogicalFunction( wxINVERT
);
5909 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5910 HideCellEditControl();
5911 SaveEditControlValue();
5913 int colLeft
= GetColLeft(m_dragRowOrCol
);
5914 SetColSize( m_dragRowOrCol
,
5915 wxMax( m_dragLastPos
- colLeft
,
5916 GetColMinimalWidth(m_dragRowOrCol
) ) );
5918 if ( !GetBatchCount() )
5920 // Only needed to get the correct rect.x:
5921 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5923 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5924 rect
.width
= cw
- rect
.x
;
5925 rect
.height
= m_colLabelHeight
;
5926 m_colLabelWin
->Refresh( true, &rect
);
5928 // if there is a multicell block, paint all of it
5931 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5932 int topRow
= YToRow(top
);
5933 int bottomRow
= internalYToRow(top
+cw
);
5936 for (i
=topRow
; i
<bottomRow
; i
++)
5938 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5939 if (cell_cols
< subtract_cols
)
5940 subtract_cols
= cell_cols
;
5942 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5943 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5944 rect
.width
= cw
- rect
.x
;
5947 m_gridWin
->Refresh( false, &rect
);
5950 ShowCellEditControl();
5957 // ------ interaction with data model
5959 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5961 switch ( msg
.GetId() )
5963 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5964 return GetModelValues();
5966 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5967 return SetModelValues();
5969 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5970 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5971 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5972 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5973 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5974 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5975 return Redimension( msg
);
5984 // The behaviour of this function depends on the grid table class
5985 // Clear() function. For the default wxGridStringTable class the
5986 // behavious is to replace all cell contents with wxEmptyString but
5987 // not to change the number of rows or cols.
5989 void wxGrid::ClearGrid()
5993 if (IsCellEditControlEnabled())
5994 DisableCellEditControl();
5997 if ( !GetBatchCount() ) m_gridWin
->Refresh();
6002 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6004 // TODO: something with updateLabels flag
6008 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6014 if (IsCellEditControlEnabled())
6015 DisableCellEditControl();
6017 bool done
= m_table
->InsertRows( pos
, numRows
);
6020 // the table will have sent the results of the insert row
6021 // operation to this view object as a grid table message
6027 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6029 // TODO: something with updateLabels flag
6033 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6039 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6041 // the table will have sent the results of the append row
6042 // operation to this view object as a grid table message
6048 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6050 // TODO: something with updateLabels flag
6054 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6060 if (IsCellEditControlEnabled())
6061 DisableCellEditControl();
6063 bool done
= m_table
->DeleteRows( pos
, numRows
);
6065 // the table will have sent the results of the delete row
6066 // operation to this view object as a grid table message
6072 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6074 // TODO: something with updateLabels flag
6078 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6084 if (IsCellEditControlEnabled())
6085 DisableCellEditControl();
6087 bool done
= m_table
->InsertCols( pos
, numCols
);
6089 // the table will have sent the results of the insert col
6090 // operation to this view object as a grid table message
6096 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6098 // TODO: something with updateLabels flag
6102 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6108 bool done
= m_table
->AppendCols( numCols
);
6110 // the table will have sent the results of the append col
6111 // operation to this view object as a grid table message
6117 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6119 // TODO: something with updateLabels flag
6123 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6129 if (IsCellEditControlEnabled())
6130 DisableCellEditControl();
6132 bool done
= m_table
->DeleteCols( pos
, numCols
);
6134 // the table will have sent the results of the delete col
6135 // operation to this view object as a grid table message
6143 // ----- event handlers
6146 // Generate a grid event based on a mouse event and
6147 // return the result of ProcessEvent()
6149 int wxGrid::SendEvent( const wxEventType type
,
6151 wxMouseEvent
& mouseEv
)
6156 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6158 int rowOrCol
= (row
== -1 ? col
: row
);
6160 wxGridSizeEvent
gridEvt( GetId(),
6164 mouseEv
.GetX() + GetRowLabelSize(),
6165 mouseEv
.GetY() + GetColLabelSize(),
6166 mouseEv
.ControlDown(),
6167 mouseEv
.ShiftDown(),
6169 mouseEv
.MetaDown() );
6171 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6172 vetoed
= !gridEvt
.IsAllowed();
6174 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6176 // Right now, it should _never_ end up here!
6177 wxGridRangeSelectEvent
gridEvt( GetId(),
6181 m_selectingBottomRight
,
6183 mouseEv
.ControlDown(),
6184 mouseEv
.ShiftDown(),
6186 mouseEv
.MetaDown() );
6188 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6189 vetoed
= !gridEvt
.IsAllowed();
6193 wxGridEvent
gridEvt( GetId(),
6197 mouseEv
.GetX() + GetRowLabelSize(),
6198 mouseEv
.GetY() + GetColLabelSize(),
6200 mouseEv
.ControlDown(),
6201 mouseEv
.ShiftDown(),
6203 mouseEv
.MetaDown() );
6204 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6205 vetoed
= !gridEvt
.IsAllowed();
6208 // A Veto'd event may not be `claimed' so test this first
6209 if (vetoed
) return -1;
6210 return claimed
? 1 : 0;
6214 // Generate a grid event of specified type and return the result
6215 // of ProcessEvent().
6217 int wxGrid::SendEvent( const wxEventType type
,
6223 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6225 int rowOrCol
= (row
== -1 ? col
: row
);
6227 wxGridSizeEvent
gridEvt( GetId(),
6232 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6233 vetoed
= !gridEvt
.IsAllowed();
6237 wxGridEvent
gridEvt( GetId(),
6242 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6243 vetoed
= !gridEvt
.IsAllowed();
6246 // A Veto'd event may not be `claimed' so test this first
6247 if (vetoed
) return -1;
6248 return claimed
? 1 : 0;
6252 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6254 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6257 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6259 // Don't do anything if between Begin/EndBatch...
6260 // EndBatch() will do all this on the last nested one anyway.
6261 if (! GetBatchCount())
6263 // Refresh to get correct scrolled position:
6264 wxScrolledWindow::Refresh(eraseb
,rect
);
6268 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6269 int width_label
, width_cell
, height_label
, height_cell
;
6272 //Copy rectangle can get scroll offsets..
6273 rect_x
= rect
->GetX();
6274 rect_y
= rect
->GetY();
6275 rectWidth
= rect
->GetWidth();
6276 rectHeight
= rect
->GetHeight();
6278 width_label
= m_rowLabelWidth
- rect_x
;
6279 if (width_label
> rectWidth
) width_label
= rectWidth
;
6281 height_label
= m_colLabelHeight
- rect_y
;
6282 if (height_label
> rectHeight
) height_label
= rectHeight
;
6284 if (rect_x
> m_rowLabelWidth
)
6286 x
= rect_x
- m_rowLabelWidth
;
6287 width_cell
= rectWidth
;
6292 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6295 if (rect_y
> m_colLabelHeight
)
6297 y
= rect_y
- m_colLabelHeight
;
6298 height_cell
= rectHeight
;
6303 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6306 // Paint corner label part intersecting rect.
6307 if ( width_label
> 0 && height_label
> 0 )
6309 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6310 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6313 // Paint col labels part intersecting rect.
6314 if ( width_cell
> 0 && height_label
> 0 )
6316 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6317 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6320 // Paint row labels part intersecting rect.
6321 if ( width_label
> 0 && height_cell
> 0 )
6323 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6324 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6327 // Paint cell area part intersecting rect.
6328 if ( width_cell
> 0 && height_cell
> 0 )
6330 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6331 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6336 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6337 m_colLabelWin
->Refresh(eraseb
, NULL
);
6338 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6339 m_gridWin
->Refresh(eraseb
, NULL
);
6344 void wxGrid::OnSize( wxSizeEvent
& event
)
6346 // position the child windows
6349 // don't call CalcDimensions() from here, the base class handles the size
6355 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6357 if ( m_inOnKeyDown
)
6359 // shouldn't be here - we are going round in circles...
6361 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6364 m_inOnKeyDown
= true;
6366 // propagate the event up and see if it gets processed
6368 wxWindow
*parent
= GetParent();
6369 wxKeyEvent
keyEvt( event
);
6370 keyEvt
.SetEventObject( parent
);
6372 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6375 // try local handlers
6377 switch ( event
.GetKeyCode() )
6380 if ( event
.ControlDown() )
6382 MoveCursorUpBlock( event
.ShiftDown() );
6386 MoveCursorUp( event
.ShiftDown() );
6391 if ( event
.ControlDown() )
6393 MoveCursorDownBlock( event
.ShiftDown() );
6397 MoveCursorDown( event
.ShiftDown() );
6402 if ( event
.ControlDown() )
6404 MoveCursorLeftBlock( event
.ShiftDown() );
6408 MoveCursorLeft( event
.ShiftDown() );
6413 if ( event
.ControlDown() )
6415 MoveCursorRightBlock( event
.ShiftDown() );
6419 MoveCursorRight( event
.ShiftDown() );
6424 case WXK_NUMPAD_ENTER
:
6425 if ( event
.ControlDown() )
6427 event
.Skip(); // to let the edit control have the return
6431 if ( GetGridCursorRow() < GetNumberRows()-1 )
6433 MoveCursorDown( event
.ShiftDown() );
6437 // at the bottom of a column
6438 DisableCellEditControl();
6448 if (event
.ShiftDown())
6450 if ( GetGridCursorCol() > 0 )
6452 MoveCursorLeft( false );
6457 DisableCellEditControl();
6462 if ( GetGridCursorCol() < GetNumberCols()-1 )
6464 MoveCursorRight( false );
6469 DisableCellEditControl();
6475 if ( event
.ControlDown() )
6477 MakeCellVisible( 0, 0 );
6478 SetCurrentCell( 0, 0 );
6487 if ( event
.ControlDown() )
6489 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6490 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6507 if ( event
.ControlDown() )
6511 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6512 m_currentCellCoords
.GetCol(),
6513 event
.ControlDown(),
6520 if ( !IsEditable() )
6522 MoveCursorRight( false );
6525 // Otherwise fall through to default
6533 m_inOnKeyDown
= false;
6536 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6538 // try local handlers
6540 if ( event
.GetKeyCode() == WXK_SHIFT
)
6542 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6543 m_selectingBottomRight
!= wxGridNoCellCoords
)
6547 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6548 m_selectingTopLeft
.GetCol(),
6549 m_selectingBottomRight
.GetRow(),
6550 m_selectingBottomRight
.GetCol(),
6551 event
.ControlDown(),
6558 m_selectingTopLeft
= wxGridNoCellCoords
;
6559 m_selectingBottomRight
= wxGridNoCellCoords
;
6560 m_selectingKeyboard
= wxGridNoCellCoords
;
6564 void wxGrid::OnChar( wxKeyEvent
& event
)
6566 // is it possible to edit the current cell at all?
6567 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6569 // yes, now check whether the cells editor accepts the key
6570 int row
= m_currentCellCoords
.GetRow();
6571 int col
= m_currentCellCoords
.GetCol();
6572 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6573 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6575 // <F2> is special and will always start editing, for
6576 // other keys - ask the editor itself
6577 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6578 || editor
->IsAcceptedKey(event
) )
6580 // ensure cell is visble
6581 MakeCellVisible(row
, col
);
6582 EnableCellEditControl();
6584 // a problem can arise if the cell is not completely
6585 // visible (even after calling MakeCellVisible the
6586 // control is not created and calling StartingKey will
6588 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6589 editor
->StartingKey(event
);
6606 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6610 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6612 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6614 // the event has been intercepted - do nothing
6618 wxClientDC
dc(m_gridWin
);
6621 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6623 DisableCellEditControl();
6625 if ( IsVisible( m_currentCellCoords
, false ) )
6628 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6629 if ( !m_gridLinesEnabled
)
6637 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6639 // Otherwise refresh redraws the highlight!
6640 m_currentCellCoords
= coords
;
6642 DrawGridCellArea(dc
,cells
);
6643 DrawAllGridLines( dc
, r
);
6647 m_currentCellCoords
= coords
;
6649 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6650 DrawCellHighlight(dc
, attr
);
6655 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6658 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6662 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6665 rightCol
= GetNumberCols() - 1;
6667 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6670 bottomRow
= GetNumberRows() - 1;
6674 if ( topRow
> bottomRow
)
6681 if ( leftCol
> rightCol
)
6688 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6689 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6691 // First the case that we selected a completely new area
6692 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6693 m_selectingBottomRight
== wxGridNoCellCoords
)
6696 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6697 wxGridCellCoords ( bottomRow
, rightCol
) );
6698 m_gridWin
->Refresh( false, &rect
);
6700 // Now handle changing an existing selection area.
6701 else if ( m_selectingTopLeft
!= updateTopLeft
||
6702 m_selectingBottomRight
!= updateBottomRight
)
6704 // Compute two optimal update rectangles:
6705 // Either one rectangle is a real subset of the
6706 // other, or they are (almost) disjoint!
6708 bool need_refresh
[4];
6712 need_refresh
[3] = false;
6715 // Store intermediate values
6716 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6717 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6718 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6719 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6721 // Determine the outer/inner coordinates.
6722 if (oldLeft
> leftCol
)
6728 if (oldTop
> topRow
)
6734 if (oldRight
< rightCol
)
6737 oldRight
= rightCol
;
6740 if (oldBottom
< bottomRow
)
6743 oldBottom
= bottomRow
;
6747 // Now, either the stuff marked old is the outer
6748 // rectangle or we don't have a situation where one
6749 // is contained in the other.
6751 if ( oldLeft
< leftCol
)
6753 // Refresh the newly selected or deselected
6754 // area to the left of the old or new selection.
6755 need_refresh
[0] = true;
6756 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6758 wxGridCellCoords ( oldBottom
,
6762 if ( oldTop
< topRow
)
6764 // Refresh the newly selected or deselected
6765 // area above the old or new selection.
6766 need_refresh
[1] = true;
6767 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6769 wxGridCellCoords ( topRow
- 1,
6773 if ( oldRight
> rightCol
)
6775 // Refresh the newly selected or deselected
6776 // area to the right of the old or new selection.
6777 need_refresh
[2] = true;
6778 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6780 wxGridCellCoords ( oldBottom
,
6784 if ( oldBottom
> bottomRow
)
6786 // Refresh the newly selected or deselected
6787 // area below the old or new selection.
6788 need_refresh
[3] = true;
6789 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6791 wxGridCellCoords ( oldBottom
,
6795 // various Refresh() calls
6796 for (i
= 0; i
< 4; i
++ )
6797 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6798 m_gridWin
->Refresh( false, &(rect
[i
]) );
6801 m_selectingTopLeft
= updateTopLeft
;
6802 m_selectingBottomRight
= updateBottomRight
;
6806 // ------ functions to get/send data (see also public functions)
6809 bool wxGrid::GetModelValues()
6811 // Hide the editor, so it won't hide a changed value.
6812 HideCellEditControl();
6816 // all we need to do is repaint the grid
6818 m_gridWin
->Refresh();
6826 bool wxGrid::SetModelValues()
6830 // Disable the editor, so it won't hide a changed value.
6831 // Do we also want to save the current value of the editor first?
6833 DisableCellEditControl();
6837 for ( row
= 0; row
< m_numRows
; row
++ )
6839 for ( col
= 0; col
< m_numCols
; col
++ )
6841 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6853 // Note - this function only draws cells that are in the list of
6854 // exposed cells (usually set from the update region by
6855 // CalcExposedCells)
6857 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6859 if ( !m_numRows
|| !m_numCols
) return;
6861 int i
, numCells
= cells
.GetCount();
6862 int row
, col
, cell_rows
, cell_cols
;
6863 wxGridCellCoordsArray redrawCells
;
6865 for ( i
= numCells
-1; i
>= 0; i
-- )
6867 row
= cells
[i
].GetRow();
6868 col
= cells
[i
].GetCol();
6869 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6871 // If this cell is part of a multicell block, find owner for repaint
6872 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6874 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6875 bool marked
= false;
6876 for ( int j
= 0; j
< numCells
; j
++ )
6878 if ( cell
== cells
[j
] )
6886 int count
= redrawCells
.GetCount();
6887 for (int j
= 0; j
< count
; j
++)
6889 if ( cell
== redrawCells
[j
] )
6895 if (!marked
) redrawCells
.Add( cell
);
6897 continue; // don't bother drawing this cell
6900 // If this cell is empty, find cell to left that might want to overflow
6901 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6903 for ( int l
= 0; l
< cell_rows
; l
++ )
6905 // find a cell in this row to left alreay marked for repaint
6907 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6908 if ((redrawCells
[k
].GetCol() < left
) &&
6909 (redrawCells
[k
].GetRow() == row
))
6910 left
=redrawCells
[k
].GetCol();
6912 if (left
== col
) left
= 0; // oh well
6914 for (int j
= col
-1; j
>= left
; j
--)
6916 if (!m_table
->IsEmptyCell(row
+l
, j
))
6918 if (GetCellOverflow(row
+l
, j
))
6920 wxGridCellCoords
cell(row
+l
, j
);
6921 bool marked
= false;
6923 for (int k
= 0; k
< numCells
; k
++)
6925 if ( cell
== cells
[k
] )
6933 int count
= redrawCells
.GetCount();
6934 for (int k
= 0; k
< count
; k
++)
6936 if ( cell
== redrawCells
[k
] )
6942 if (!marked
) redrawCells
.Add( cell
);
6950 DrawCell( dc
, cells
[i
] );
6953 numCells
= redrawCells
.GetCount();
6955 for ( i
= numCells
- 1; i
>= 0; i
-- )
6957 DrawCell( dc
, redrawCells
[i
] );
6962 void wxGrid::DrawGridSpace( wxDC
& dc
)
6965 m_gridWin
->GetClientSize( &cw
, &ch
);
6968 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6970 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6971 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6973 if ( right
> rightCol
|| bottom
> bottomRow
)
6976 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6978 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6979 dc
.SetPen( *wxTRANSPARENT_PEN
);
6981 if ( right
> rightCol
)
6983 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6986 if ( bottom
> bottomRow
)
6988 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6994 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6996 int row
= coords
.GetRow();
6997 int col
= coords
.GetCol();
6999 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7002 // we draw the cell border ourselves
7003 #if !WXGRID_DRAW_LINES
7004 if ( m_gridLinesEnabled
)
7005 DrawCellBorder( dc
, coords
);
7008 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7010 bool isCurrent
= coords
== m_currentCellCoords
;
7012 wxRect rect
= CellToRect( row
, col
);
7014 // if the editor is shown, we should use it and not the renderer
7015 // Note: However, only if it is really _shown_, i.e. not hidden!
7016 if ( isCurrent
&& IsCellEditControlShown() )
7018 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7019 editor
->PaintBackground(rect
, attr
);
7024 // but all the rest is drawn by the cell renderer and hence may be
7026 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7027 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7034 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7036 int row
= m_currentCellCoords
.GetRow();
7037 int col
= m_currentCellCoords
.GetCol();
7039 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7042 wxRect rect
= CellToRect(row
, col
);
7044 // hmmm... what could we do here to show that the cell is disabled?
7045 // for now, I just draw a thinner border than for the other ones, but
7046 // it doesn't look really good
7048 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7052 // The center of th drawn line is where the position/width/height of
7053 // the rectangle is actually at, (on wxMSW atr least,) so we will
7054 // reduce the size of the rectangle to compensate for the thickness of
7055 // the line. If this is too strange on non wxMSW platforms then
7056 // please #ifdef this appropriately.
7057 rect
.x
+= penWidth
/2;
7058 rect
.y
+= penWidth
/2;
7059 rect
.width
-= penWidth
-1;
7060 rect
.height
-= penWidth
-1;
7063 // Now draw the rectangle
7064 // use the cellHighlightColour if the cell is inside a selection, this
7065 // will ensure the cell is always visible.
7066 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
7067 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7068 dc
.DrawRectangle(rect
);
7072 // VZ: my experiments with 3d borders...
7074 // how to properly set colours for arbitrary bg?
7075 wxCoord x1
= rect
.x
,
7077 x2
= rect
.x
+ rect
.width
-1,
7078 y2
= rect
.y
+ rect
.height
-1;
7080 dc
.SetPen(*wxWHITE_PEN
);
7081 dc
.DrawLine(x1
, y1
, x2
, y1
);
7082 dc
.DrawLine(x1
, y1
, x1
, y2
);
7084 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7085 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7087 dc
.SetPen(*wxBLACK_PEN
);
7088 dc
.DrawLine(x1
, y2
, x2
, y2
);
7089 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7094 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7096 int row
= coords
.GetRow();
7097 int col
= coords
.GetCol();
7098 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7101 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7103 wxRect rect
= CellToRect( row
, col
);
7105 // right hand border
7107 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7108 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7112 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7113 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7116 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7118 // This if block was previously in wxGrid::OnPaint but that doesn't
7119 // seem to get called under wxGTK - MB
7121 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7122 m_numRows
&& m_numCols
)
7124 m_currentCellCoords
.Set(0, 0);
7127 if ( IsCellEditControlShown() )
7129 // don't show highlight when the edit control is shown
7133 // if the active cell was repainted, repaint its highlight too because it
7134 // might have been damaged by the grid lines
7135 size_t count
= cells
.GetCount();
7136 for ( size_t n
= 0; n
< count
; n
++ )
7138 if ( cells
[n
] == m_currentCellCoords
)
7140 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7141 DrawCellHighlight(dc
, attr
);
7149 // TODO: remove this ???
7150 // This is used to redraw all grid lines e.g. when the grid line colour
7153 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7155 #if !WXGRID_DRAW_LINES
7159 if ( !m_gridLinesEnabled
||
7161 !m_numCols
) return;
7163 int top
, bottom
, left
, right
;
7165 #if 0 //#ifndef __WXGTK__
7169 m_gridWin
->GetClientSize(&cw
, &ch
);
7171 // virtual coords of visible area
7173 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7174 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7179 reg
.GetBox(x
, y
, w
, h
);
7180 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7181 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7185 m_gridWin
->GetClientSize(&cw
, &ch
);
7186 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7187 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7190 // avoid drawing grid lines past the last row and col
7192 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7193 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7195 // no gridlines inside multicells, clip them out
7196 int leftCol
= internalXToCol(left
);
7197 int topRow
= internalYToRow(top
);
7198 int rightCol
= internalXToCol(right
);
7199 int bottomRow
= internalYToRow(bottom
);
7202 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7203 wxRegion
clippedcells(0, 0, cw
, ch
);
7205 int i
, j
, cell_rows
, cell_cols
;
7208 for (j
=topRow
; j
<bottomRow
; j
++)
7210 for (i
=leftCol
; i
<rightCol
; i
++)
7212 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7213 if ((cell_rows
> 1) || (cell_cols
> 1))
7215 rect
= CellToRect(j
,i
);
7216 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7217 clippedcells
.Subtract(rect
);
7219 else if ((cell_rows
< 0) || (cell_cols
< 0))
7221 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7222 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7223 clippedcells
.Subtract(rect
);
7228 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7230 int i
, j
, cell_rows
, cell_cols
;
7233 for (j
=topRow
; j
<bottomRow
; j
++)
7235 for (i
=leftCol
; i
<rightCol
; i
++)
7237 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7238 if ((cell_rows
> 1) || (cell_cols
> 1))
7240 rect
= CellToRect(j
,i
);
7241 clippedcells
.Subtract(rect
);
7243 else if ((cell_rows
< 0) || (cell_cols
< 0))
7245 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7246 clippedcells
.Subtract(rect
);
7251 dc
.SetClippingRegion( clippedcells
);
7253 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7255 // horizontal grid lines
7257 // already declared above - int i;
7258 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7260 int bot
= GetRowBottom(i
) - 1;
7269 dc
.DrawLine( left
, bot
, right
, bot
);
7274 // vertical grid lines
7276 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7278 int colRight
= GetColRight(i
) - 1;
7279 if ( colRight
> right
)
7284 if ( colRight
>= left
)
7286 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7289 dc
.DestroyClippingRegion();
7293 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7295 if ( !m_numRows
) return;
7298 size_t numLabels
= rows
.GetCount();
7300 for ( i
= 0; i
< numLabels
; i
++ )
7302 DrawRowLabel( dc
, rows
[i
] );
7307 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7309 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7315 rect
.SetY( GetRowTop(row
) + 1 );
7316 rect
.SetWidth( m_rowLabelWidth
- 2 );
7317 rect
.SetHeight( GetRowHeight(row
) - 2 );
7319 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7321 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7323 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7325 int rowTop
= GetRowTop(row
),
7326 rowBottom
= GetRowBottom(row
) - 1;
7328 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7329 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7330 m_rowLabelWidth
-1, rowBottom
);
7332 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7334 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7336 dc
.SetPen( *wxWHITE_PEN
);
7337 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7338 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7340 dc
.SetBackgroundMode( wxTRANSPARENT
);
7341 dc
.SetTextForeground( GetLabelTextColour() );
7342 dc
.SetFont( GetLabelFont() );
7345 GetRowLabelAlignment( &hAlign
, &vAlign
);
7348 rect
.SetY( GetRowTop(row
) + 2 );
7349 rect
.SetWidth( m_rowLabelWidth
- 4 );
7350 rect
.SetHeight( GetRowHeight(row
) - 4 );
7351 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7355 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7357 if ( !m_numCols
) return;
7360 size_t numLabels
= cols
.GetCount();
7362 for ( i
= 0; i
< numLabels
; i
++ )
7364 DrawColLabel( dc
, cols
[i
] );
7369 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7371 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7374 int colLeft
= GetColLeft(col
);
7378 rect
.SetX( colLeft
+ 1 );
7380 rect
.SetWidth( GetColWidth(col
) - 2 );
7381 rect
.SetHeight( m_colLabelHeight
- 2 );
7383 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7385 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7387 int colRight
= GetColRight(col
) - 1;
7389 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7390 dc
.DrawLine( colRight
, 0,
7391 colRight
, m_colLabelHeight
-1 );
7393 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7395 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7396 colRight
+1, m_colLabelHeight
-1 );
7398 dc
.SetPen( *wxWHITE_PEN
);
7399 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7400 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7402 dc
.SetBackgroundMode( wxTRANSPARENT
);
7403 dc
.SetTextForeground( GetLabelTextColour() );
7404 dc
.SetFont( GetLabelFont() );
7406 int hAlign
, vAlign
, orient
;
7407 GetColLabelAlignment( &hAlign
, &vAlign
);
7408 orient
= GetColLabelTextOrientation();
7410 rect
.SetX( colLeft
+ 2 );
7412 rect
.SetWidth( GetColWidth(col
) - 4 );
7413 rect
.SetHeight( m_colLabelHeight
- 4 );
7414 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7417 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7418 const wxString
& value
,
7422 int textOrientation
)
7424 wxArrayString lines
;
7426 StringToLines( value
, lines
);
7429 //Forward to new API.
7430 DrawTextRectangle( dc
,
7439 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7440 const wxArrayString
& lines
,
7444 int textOrientation
)
7446 long textWidth
, textHeight
;
7447 long lineWidth
, lineHeight
;
7450 dc
.SetClippingRegion( rect
);
7452 nLines
= lines
.GetCount();
7456 float x
= 0.0, y
= 0.0;
7458 if( textOrientation
== wxHORIZONTAL
)
7459 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7461 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7465 case wxALIGN_BOTTOM
:
7466 if( textOrientation
== wxHORIZONTAL
)
7467 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7469 x
= rect
.x
+ rect
.width
- textWidth
;
7472 case wxALIGN_CENTRE
:
7473 if( textOrientation
== wxHORIZONTAL
)
7474 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7476 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7481 if( textOrientation
== wxHORIZONTAL
)
7488 // Align each line of a multi-line label
7489 for( l
= 0; l
< nLines
; l
++ )
7491 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7493 switch( horizAlign
)
7496 if( textOrientation
== wxHORIZONTAL
)
7497 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7499 y
= rect
.y
+ lineWidth
+ 1;
7502 case wxALIGN_CENTRE
:
7503 if( textOrientation
== wxHORIZONTAL
)
7504 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7506 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7511 if( textOrientation
== wxHORIZONTAL
)
7514 y
= rect
.y
+ rect
.height
- 1;
7518 if( textOrientation
== wxHORIZONTAL
)
7520 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7525 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7530 dc
.DestroyClippingRegion();
7534 // Split multi line text up into an array of strings. Any existing
7535 // contents of the string array are preserved.
7537 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7541 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7542 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7544 while ( startPos
< (int)tVal
.Length() )
7546 pos
= tVal
.Mid(startPos
).Find( eol
);
7551 else if ( pos
== 0 )
7553 lines
.Add( wxEmptyString
);
7557 lines
.Add( value
.Mid(startPos
, pos
) );
7561 if ( startPos
< (int)value
.Length() )
7563 lines
.Add( value
.Mid( startPos
) );
7568 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
7569 const wxArrayString
& lines
,
7570 long *width
, long *height
)
7577 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7579 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7580 w
= wxMax( w
, lineW
);
7589 // ------ Batch processing.
7591 void wxGrid::EndBatch()
7593 if ( m_batchCount
> 0 )
7596 if ( !m_batchCount
)
7599 m_rowLabelWin
->Refresh();
7600 m_colLabelWin
->Refresh();
7601 m_cornerLabelWin
->Refresh();
7602 m_gridWin
->Refresh();
7607 // Use this, rather than wxWindow::Refresh(), to force an immediate
7608 // repainting of the grid. Has no effect if you are already inside a
7609 // BeginBatch / EndBatch block.
7611 void wxGrid::ForceRefresh()
7617 bool wxGrid::Enable(bool enable
)
7619 if ( !wxScrolledWindow::Enable(enable
) )
7622 // redraw in the new state
7623 m_gridWin
->Refresh();
7629 // ------ Edit control functions
7633 void wxGrid::EnableEditing( bool edit
)
7635 // TODO: improve this ?
7637 if ( edit
!= m_editable
)
7639 if(!edit
) EnableCellEditControl(edit
);
7645 void wxGrid::EnableCellEditControl( bool enable
)
7650 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7651 SetCurrentCell( 0, 0 );
7653 if ( enable
!= m_cellEditCtrlEnabled
)
7657 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7660 // this should be checked by the caller!
7661 wxASSERT_MSG( CanEnableCellControl(),
7662 _T("can't enable editing for this cell!") );
7664 // do it before ShowCellEditControl()
7665 m_cellEditCtrlEnabled
= enable
;
7667 ShowCellEditControl();
7671 //FIXME:add veto support
7672 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7674 HideCellEditControl();
7675 SaveEditControlValue();
7677 // do it after HideCellEditControl()
7678 m_cellEditCtrlEnabled
= enable
;
7683 bool wxGrid::IsCurrentCellReadOnly() const
7686 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7687 bool readonly
= attr
->IsReadOnly();
7693 bool wxGrid::CanEnableCellControl() const
7695 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7696 !IsCurrentCellReadOnly();
7700 bool wxGrid::IsCellEditControlEnabled() const
7702 // the cell edit control might be disable for all cells or just for the
7703 // current one if it's read only
7704 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7707 bool wxGrid::IsCellEditControlShown() const
7709 bool isShown
= false;
7711 if ( m_cellEditCtrlEnabled
)
7713 int row
= m_currentCellCoords
.GetRow();
7714 int col
= m_currentCellCoords
.GetCol();
7715 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7716 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7721 if ( editor
->IsCreated() )
7723 isShown
= editor
->GetControl()->IsShown();
7733 void wxGrid::ShowCellEditControl()
7735 if ( IsCellEditControlEnabled() )
7737 if ( !IsVisible( m_currentCellCoords
) )
7739 m_cellEditCtrlEnabled
= false;
7744 wxRect rect
= CellToRect( m_currentCellCoords
);
7745 int row
= m_currentCellCoords
.GetRow();
7746 int col
= m_currentCellCoords
.GetCol();
7748 // if this is part of a multicell, find owner (topleft)
7749 int cell_rows
, cell_cols
;
7750 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7751 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7755 m_currentCellCoords
.SetRow( row
);
7756 m_currentCellCoords
.SetCol( col
);
7759 // convert to scrolled coords
7761 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7763 // done in PaintBackground()
7765 // erase the highlight and the cell contents because the editor
7766 // might not cover the entire cell
7767 wxClientDC
dc( m_gridWin
);
7769 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7770 dc
.SetPen(*wxTRANSPARENT_PEN
);
7771 dc
.DrawRectangle(rect
);
7774 // cell is shifted by one pixel
7775 // However, don't allow x or y to become negative
7776 // since the SetSize() method interprets that as
7783 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7784 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7785 if ( !editor
->IsCreated() )
7787 editor
->Create(m_gridWin
, wxID_ANY
,
7788 new wxGridCellEditorEvtHandler(this, editor
));
7790 wxGridEditorCreatedEvent
evt(GetId(),
7791 wxEVT_GRID_EDITOR_CREATED
,
7795 editor
->GetControl());
7796 GetEventHandler()->ProcessEvent(evt
);
7800 // resize editor to overflow into righthand cells if allowed
7801 int maxWidth
= rect
.width
;
7802 wxString value
= GetCellValue(row
, col
);
7803 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7806 GetTextExtent(value
, &maxWidth
, &y
,
7807 NULL
, NULL
, &attr
->GetFont());
7808 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7810 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7811 if (rect
.x
+maxWidth
> client_right
)
7812 maxWidth
= client_right
- rect
.x
;
7814 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7816 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7817 // may have changed earlier
7818 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7821 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7822 // looks weird going over a multicell
7823 if (m_table
->IsEmptyCell(row
,i
) &&
7824 (rect
.width
< maxWidth
) && (c_rows
== 1))
7825 rect
.width
+= GetColWidth(i
);
7829 if (rect
.GetRight() > client_right
)
7830 rect
.SetRight(client_right
-1);
7833 editor
->SetCellAttr(attr
);
7834 editor
->SetSize( rect
);
7835 editor
->Show( true, attr
);
7837 // recalc dimensions in case we need to
7838 // expand the scrolled window to account for editor
7841 editor
->BeginEdit(row
, col
, this);
7842 editor
->SetCellAttr(NULL
);
7851 void wxGrid::HideCellEditControl()
7853 if ( IsCellEditControlEnabled() )
7855 int row
= m_currentCellCoords
.GetRow();
7856 int col
= m_currentCellCoords
.GetCol();
7858 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7859 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7860 editor
->Show( false );
7864 m_gridWin
->SetFocus();
7866 // refresh whole row to the right
7867 wxRect
rect( CellToRect(row
, col
) );
7868 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7869 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7871 // ensure that the pixels under the focus ring get refreshed as well
7872 rect
.Inflate(10,10);
7874 m_gridWin
->Refresh( false, &rect
);
7879 void wxGrid::SaveEditControlValue()
7881 if ( IsCellEditControlEnabled() )
7883 int row
= m_currentCellCoords
.GetRow();
7884 int col
= m_currentCellCoords
.GetCol();
7886 wxString oldval
= GetCellValue(row
,col
);
7888 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7889 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7890 bool changed
= editor
->EndEdit(row
, col
, this);
7897 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7898 m_currentCellCoords
.GetRow(),
7899 m_currentCellCoords
.GetCol() ) < 0 ) {
7901 // Event has been vetoed, set the data back.
7902 SetCellValue(row
,col
,oldval
);
7910 // ------ Grid location functions
7911 // Note that all of these functions work with the logical coordinates of
7912 // grid cells and labels so you will need to convert from device
7913 // coordinates for mouse events etc.
7916 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7918 int row
= YToRow(y
);
7919 int col
= XToCol(x
);
7921 if ( row
== -1 || col
== -1 )
7923 coords
= wxGridNoCellCoords
;
7927 coords
.Set( row
, col
);
7932 // Internal Helper function for computing row or column from some
7933 // (unscrolled) coordinate value, using either
7934 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7935 // of m_rowBottoms/m_ColRights to speed up the search!
7937 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7938 const wxArrayInt
& BorderArray
, int nMax
,
7943 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7949 size_t i_max
= coord
/ defaultDist
,
7952 if (BorderArray
.IsEmpty())
7954 if((int) i_max
< nMax
)
7956 return clipToMinMax
? nMax
- 1 : -1;
7959 if ( i_max
>= BorderArray
.GetCount())
7960 i_max
= BorderArray
.GetCount() - 1;
7963 if ( coord
>= BorderArray
[i_max
])
7967 i_max
= coord
/ minDist
;
7969 i_max
= BorderArray
.GetCount() - 1;
7971 if ( i_max
>= BorderArray
.GetCount())
7972 i_max
= BorderArray
.GetCount() - 1;
7974 if ( coord
>= BorderArray
[i_max
])
7975 return clipToMinMax
? (int)i_max
: -1;
7976 if ( coord
< BorderArray
[0] )
7979 while ( i_max
- i_min
> 0 )
7981 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7982 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7983 if (coord
>= BorderArray
[ i_max
- 1])
7987 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7988 if (coord
< BorderArray
[median
])
7996 int wxGrid::YToRow( int y
)
7998 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7999 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8003 int wxGrid::XToCol( int x
)
8005 return CoordToRowOrCol(x
, m_defaultColWidth
,
8006 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8010 // return the row number that that the y coord is near the edge of, or
8011 // -1 if not near an edge
8013 int wxGrid::YToEdgeOfRow( int y
)
8016 i
= internalYToRow(y
);
8018 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8020 // We know that we are in row i, test whether we are
8021 // close enough to lower or upper border, respectively.
8022 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8024 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8032 // return the col number that that the x coord is near the edge of, or
8033 // -1 if not near an edge
8035 int wxGrid::XToEdgeOfCol( int x
)
8038 i
= internalXToCol(x
);
8040 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8042 // We know that we are in column i, test whether we are
8043 // close enough to right or left border, respectively.
8044 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8046 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8054 wxRect
wxGrid::CellToRect( int row
, int col
)
8056 wxRect
rect( -1, -1, -1, -1 );
8058 if ( row
>= 0 && row
< m_numRows
&&
8059 col
>= 0 && col
< m_numCols
)
8061 int i
, cell_rows
, cell_cols
;
8062 rect
.width
= rect
.height
= 0;
8063 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8064 // if negative then find multicell owner
8065 if (cell_rows
< 0) row
+= cell_rows
;
8066 if (cell_cols
< 0) col
+= cell_cols
;
8067 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8069 rect
.x
= GetColLeft(col
);
8070 rect
.y
= GetRowTop(row
);
8071 for (i
=col
; i
<col
+cell_cols
; i
++)
8072 rect
.width
+= GetColWidth(i
);
8073 for (i
=row
; i
<row
+cell_rows
; i
++)
8074 rect
.height
+= GetRowHeight(i
);
8077 // if grid lines are enabled, then the area of the cell is a bit smaller
8078 if (m_gridLinesEnabled
) {
8086 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8088 // get the cell rectangle in logical coords
8090 wxRect
r( CellToRect( row
, col
) );
8092 // convert to device coords
8094 int left
, top
, right
, bottom
;
8095 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8096 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8098 // check against the client area of the grid window
8101 m_gridWin
->GetClientSize( &cw
, &ch
);
8103 if ( wholeCellVisible
)
8105 // is the cell wholly visible ?
8107 return ( left
>= 0 && right
<= cw
&&
8108 top
>= 0 && bottom
<= ch
);
8112 // is the cell partly visible ?
8114 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8115 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8120 // make the specified cell location visible by doing a minimal amount
8123 void wxGrid::MakeCellVisible( int row
, int col
)
8127 int xpos
= -1, ypos
= -1;
8129 if ( row
>= 0 && row
< m_numRows
&&
8130 col
>= 0 && col
< m_numCols
)
8132 // get the cell rectangle in logical coords
8134 wxRect
r( CellToRect( row
, col
) );
8136 // convert to device coords
8138 int left
, top
, right
, bottom
;
8139 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8140 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8143 m_gridWin
->GetClientSize( &cw
, &ch
);
8149 else if ( bottom
> ch
)
8151 int h
= r
.GetHeight();
8153 for ( i
= row
-1; i
>= 0; i
-- )
8155 int rowHeight
= GetRowHeight(i
);
8156 if ( h
+ rowHeight
> ch
)
8163 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8164 // have rounding errors (this is important, because if we do, we
8165 // might not scroll at all and some cells won't be redrawn)
8167 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8169 ypos
+= m_scrollLineY
;
8176 else if ( right
> cw
)
8178 // position the view so that the cell is on the right
8180 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8181 xpos
= x0
+ (right
- cw
);
8183 // see comment for ypos above
8184 xpos
+= m_scrollLineX
;
8187 if ( xpos
!= -1 || ypos
!= -1 )
8190 xpos
/= m_scrollLineX
;
8192 ypos
/= m_scrollLineY
;
8193 Scroll( xpos
, ypos
);
8201 // ------ Grid cursor movement functions
8204 bool wxGrid::MoveCursorUp( bool expandSelection
)
8206 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8207 m_currentCellCoords
.GetRow() >= 0 )
8209 if ( expandSelection
)
8211 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8212 m_selectingKeyboard
= m_currentCellCoords
;
8213 if ( m_selectingKeyboard
.GetRow() > 0 )
8215 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8216 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8217 m_selectingKeyboard
.GetCol() );
8218 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8221 else if ( m_currentCellCoords
.GetRow() > 0 )
8224 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8225 m_currentCellCoords
.GetCol() );
8226 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8227 m_currentCellCoords
.GetCol() );
8238 bool wxGrid::MoveCursorDown( bool expandSelection
)
8240 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8241 m_currentCellCoords
.GetRow() < m_numRows
)
8243 if ( expandSelection
)
8245 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8246 m_selectingKeyboard
= m_currentCellCoords
;
8247 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8249 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8250 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8251 m_selectingKeyboard
.GetCol() );
8252 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8255 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8258 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8259 m_currentCellCoords
.GetCol() );
8260 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8261 m_currentCellCoords
.GetCol() );
8272 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8274 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8275 m_currentCellCoords
.GetCol() >= 0 )
8277 if ( expandSelection
)
8279 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8280 m_selectingKeyboard
= m_currentCellCoords
;
8281 if ( m_selectingKeyboard
.GetCol() > 0 )
8283 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8284 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8285 m_selectingKeyboard
.GetCol() );
8286 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8289 else if ( m_currentCellCoords
.GetCol() > 0 )
8292 MakeCellVisible( m_currentCellCoords
.GetRow(),
8293 m_currentCellCoords
.GetCol() - 1 );
8294 SetCurrentCell( m_currentCellCoords
.GetRow(),
8295 m_currentCellCoords
.GetCol() - 1 );
8306 bool wxGrid::MoveCursorRight( bool expandSelection
)
8308 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8309 m_currentCellCoords
.GetCol() < m_numCols
)
8311 if ( expandSelection
)
8313 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8314 m_selectingKeyboard
= m_currentCellCoords
;
8315 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8317 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8318 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8319 m_selectingKeyboard
.GetCol() );
8320 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8323 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8326 MakeCellVisible( m_currentCellCoords
.GetRow(),
8327 m_currentCellCoords
.GetCol() + 1 );
8328 SetCurrentCell( m_currentCellCoords
.GetRow(),
8329 m_currentCellCoords
.GetCol() + 1 );
8340 bool wxGrid::MovePageUp()
8342 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8344 int row
= m_currentCellCoords
.GetRow();
8348 m_gridWin
->GetClientSize( &cw
, &ch
);
8350 int y
= GetRowTop(row
);
8351 int newRow
= internalYToRow( y
- ch
+ 1 );
8353 if ( newRow
== row
)
8355 //row > 0 , so newrow can never be less than 0 here.
8359 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8360 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8368 bool wxGrid::MovePageDown()
8370 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8372 int row
= m_currentCellCoords
.GetRow();
8373 if ( (row
+1) < m_numRows
)
8376 m_gridWin
->GetClientSize( &cw
, &ch
);
8378 int y
= GetRowTop(row
);
8379 int newRow
= internalYToRow( y
+ ch
);
8380 if ( newRow
== row
)
8382 // row < m_numRows , so newrow can't overflow here.
8386 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8387 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8395 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8398 m_currentCellCoords
!= wxGridNoCellCoords
&&
8399 m_currentCellCoords
.GetRow() > 0 )
8401 int row
= m_currentCellCoords
.GetRow();
8402 int col
= m_currentCellCoords
.GetCol();
8404 if ( m_table
->IsEmptyCell(row
, col
) )
8406 // starting in an empty cell: find the next block of
8412 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8415 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8417 // starting at the top of a block: find the next block
8423 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8428 // starting within a block: find the top of the block
8433 if ( m_table
->IsEmptyCell(row
, col
) )
8441 MakeCellVisible( row
, col
);
8442 if ( expandSelection
)
8444 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8445 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8450 SetCurrentCell( row
, col
);
8458 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8461 m_currentCellCoords
!= wxGridNoCellCoords
&&
8462 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8464 int row
= m_currentCellCoords
.GetRow();
8465 int col
= m_currentCellCoords
.GetCol();
8467 if ( m_table
->IsEmptyCell(row
, col
) )
8469 // starting in an empty cell: find the next block of
8472 while ( row
< m_numRows
-1 )
8475 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8478 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8480 // starting at the bottom of a block: find the next block
8483 while ( row
< m_numRows
-1 )
8486 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8491 // starting within a block: find the bottom of the block
8493 while ( row
< m_numRows
-1 )
8496 if ( m_table
->IsEmptyCell(row
, col
) )
8504 MakeCellVisible( row
, col
);
8505 if ( expandSelection
)
8507 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8508 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8513 SetCurrentCell( row
, col
);
8522 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8525 m_currentCellCoords
!= wxGridNoCellCoords
&&
8526 m_currentCellCoords
.GetCol() > 0 )
8528 int row
= m_currentCellCoords
.GetRow();
8529 int col
= m_currentCellCoords
.GetCol();
8531 if ( m_table
->IsEmptyCell(row
, col
) )
8533 // starting in an empty cell: find the next block of
8539 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8542 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8544 // starting at the left of a block: find the next block
8550 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8555 // starting within a block: find the left of the block
8560 if ( m_table
->IsEmptyCell(row
, col
) )
8568 MakeCellVisible( row
, col
);
8569 if ( expandSelection
)
8571 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8572 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8577 SetCurrentCell( row
, col
);
8586 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8589 m_currentCellCoords
!= wxGridNoCellCoords
&&
8590 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8592 int row
= m_currentCellCoords
.GetRow();
8593 int col
= m_currentCellCoords
.GetCol();
8595 if ( m_table
->IsEmptyCell(row
, col
) )
8597 // starting in an empty cell: find the next block of
8600 while ( col
< m_numCols
-1 )
8603 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8606 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8608 // starting at the right of a block: find the next block
8611 while ( col
< m_numCols
-1 )
8614 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8619 // starting within a block: find the right of the block
8621 while ( col
< m_numCols
-1 )
8624 if ( m_table
->IsEmptyCell(row
, col
) )
8632 MakeCellVisible( row
, col
);
8633 if ( expandSelection
)
8635 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8636 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8641 SetCurrentCell( row
, col
);
8653 // ------ Label values and formatting
8656 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8658 *horiz
= m_rowLabelHorizAlign
;
8659 *vert
= m_rowLabelVertAlign
;
8662 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8664 *horiz
= m_colLabelHorizAlign
;
8665 *vert
= m_colLabelVertAlign
;
8668 int wxGrid::GetColLabelTextOrientation()
8670 return m_colLabelTextOrientation
;
8673 wxString
wxGrid::GetRowLabelValue( int row
)
8677 return m_table
->GetRowLabelValue( row
);
8687 wxString
wxGrid::GetColLabelValue( int col
)
8691 return m_table
->GetColLabelValue( col
);
8702 void wxGrid::SetRowLabelSize( int width
)
8704 width
= wxMax( width
, 0 );
8705 if ( width
!= m_rowLabelWidth
)
8709 m_rowLabelWin
->Show( false );
8710 m_cornerLabelWin
->Show( false );
8712 else if ( m_rowLabelWidth
== 0 )
8714 m_rowLabelWin
->Show( true );
8715 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8718 m_rowLabelWidth
= width
;
8720 wxScrolledWindow::Refresh( true );
8725 void wxGrid::SetColLabelSize( int height
)
8727 height
= wxMax( height
, 0 );
8728 if ( height
!= m_colLabelHeight
)
8732 m_colLabelWin
->Show( false );
8733 m_cornerLabelWin
->Show( false );
8735 else if ( m_colLabelHeight
== 0 )
8737 m_colLabelWin
->Show( true );
8738 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8741 m_colLabelHeight
= height
;
8743 wxScrolledWindow::Refresh( true );
8748 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8750 if ( m_labelBackgroundColour
!= colour
)
8752 m_labelBackgroundColour
= colour
;
8753 m_rowLabelWin
->SetBackgroundColour( colour
);
8754 m_colLabelWin
->SetBackgroundColour( colour
);
8755 m_cornerLabelWin
->SetBackgroundColour( colour
);
8757 if ( !GetBatchCount() )
8759 m_rowLabelWin
->Refresh();
8760 m_colLabelWin
->Refresh();
8761 m_cornerLabelWin
->Refresh();
8766 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8768 if ( m_labelTextColour
!= colour
)
8770 m_labelTextColour
= colour
;
8771 if ( !GetBatchCount() )
8773 m_rowLabelWin
->Refresh();
8774 m_colLabelWin
->Refresh();
8779 void wxGrid::SetLabelFont( const wxFont
& font
)
8782 if ( !GetBatchCount() )
8784 m_rowLabelWin
->Refresh();
8785 m_colLabelWin
->Refresh();
8789 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8791 // allow old (incorrect) defs to be used
8794 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8795 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8796 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8801 case wxTOP
: vert
= wxALIGN_TOP
; break;
8802 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8803 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8806 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8808 m_rowLabelHorizAlign
= horiz
;
8811 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8813 m_rowLabelVertAlign
= vert
;
8816 if ( !GetBatchCount() )
8818 m_rowLabelWin
->Refresh();
8822 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8824 // allow old (incorrect) defs to be used
8827 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8828 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8829 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8834 case wxTOP
: vert
= wxALIGN_TOP
; break;
8835 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8836 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8839 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8841 m_colLabelHorizAlign
= horiz
;
8844 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8846 m_colLabelVertAlign
= vert
;
8849 if ( !GetBatchCount() )
8851 m_colLabelWin
->Refresh();
8855 // Note: under MSW, the default column label font must be changed because it
8856 // does not support vertical printing
8858 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8859 // pGrid->SetLabelFont(font);
8860 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8862 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8864 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8866 m_colLabelTextOrientation
= textOrientation
;
8869 if ( !GetBatchCount() )
8871 m_colLabelWin
->Refresh();
8875 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8879 m_table
->SetRowLabelValue( row
, s
);
8880 if ( !GetBatchCount() )
8882 wxRect rect
= CellToRect( row
, 0);
8883 if ( rect
.height
> 0 )
8885 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8887 rect
.width
= m_rowLabelWidth
;
8888 m_rowLabelWin
->Refresh( true, &rect
);
8894 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8898 m_table
->SetColLabelValue( col
, s
);
8899 if ( !GetBatchCount() )
8901 wxRect rect
= CellToRect( 0, col
);
8902 if ( rect
.width
> 0 )
8904 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8906 rect
.height
= m_colLabelHeight
;
8907 m_colLabelWin
->Refresh( true, &rect
);
8913 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8915 if ( m_gridLineColour
!= colour
)
8917 m_gridLineColour
= colour
;
8919 wxClientDC
dc( m_gridWin
);
8921 DrawAllGridLines( dc
, wxRegion() );
8926 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8928 if ( m_cellHighlightColour
!= colour
)
8930 m_cellHighlightColour
= colour
;
8932 wxClientDC
dc( m_gridWin
);
8934 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8935 DrawCellHighlight(dc
, attr
);
8940 void wxGrid::SetCellHighlightPenWidth(int width
)
8942 if (m_cellHighlightPenWidth
!= width
) {
8943 m_cellHighlightPenWidth
= width
;
8945 // Just redrawing the cell highlight is not enough since that won't
8946 // make any visible change if the the thickness is getting smaller.
8947 int row
= m_currentCellCoords
.GetRow();
8948 int col
= m_currentCellCoords
.GetCol();
8949 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8951 wxRect rect
= CellToRect(row
, col
);
8952 m_gridWin
->Refresh(true, &rect
);
8956 void wxGrid::SetCellHighlightROPenWidth(int width
)
8958 if (m_cellHighlightROPenWidth
!= width
) {
8959 m_cellHighlightROPenWidth
= width
;
8961 // Just redrawing the cell highlight is not enough since that won't
8962 // make any visible change if the the thickness is getting smaller.
8963 int row
= m_currentCellCoords
.GetRow();
8964 int col
= m_currentCellCoords
.GetCol();
8965 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8967 wxRect rect
= CellToRect(row
, col
);
8968 m_gridWin
->Refresh(true, &rect
);
8972 void wxGrid::EnableGridLines( bool enable
)
8974 if ( enable
!= m_gridLinesEnabled
)
8976 m_gridLinesEnabled
= enable
;
8978 if ( !GetBatchCount() )
8982 wxClientDC
dc( m_gridWin
);
8984 DrawAllGridLines( dc
, wxRegion() );
8988 m_gridWin
->Refresh();
8995 int wxGrid::GetDefaultRowSize()
8997 return m_defaultRowHeight
;
9000 int wxGrid::GetRowSize( int row
)
9002 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9004 return GetRowHeight(row
);
9007 int wxGrid::GetDefaultColSize()
9009 return m_defaultColWidth
;
9012 int wxGrid::GetColSize( int col
)
9014 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9016 return GetColWidth(col
);
9019 // ============================================================================
9020 // access to the grid attributes: each of them has a default value in the grid
9021 // itself and may be overidden on a per-cell basis
9022 // ============================================================================
9024 // ----------------------------------------------------------------------------
9025 // setting default attributes
9026 // ----------------------------------------------------------------------------
9028 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9030 m_defaultCellAttr
->SetBackgroundColour(col
);
9032 m_gridWin
->SetBackgroundColour(col
);
9036 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9038 m_defaultCellAttr
->SetTextColour(col
);
9041 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9043 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9046 void wxGrid::SetDefaultCellOverflow( bool allow
)
9048 m_defaultCellAttr
->SetOverflow(allow
);
9051 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9053 m_defaultCellAttr
->SetFont(font
);
9057 // For editors and renderers the type registry takes precedence over the
9058 // default attr, so we need to register the new editor/renderer for the string
9059 // data type in order to make setting a default editor/renderer appear to
9062 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9064 RegisterDataType(wxGRID_VALUE_STRING
,
9066 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9069 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9071 RegisterDataType(wxGRID_VALUE_STRING
,
9072 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9076 // ----------------------------------------------------------------------------
9077 // access to the default attrbiutes
9078 // ----------------------------------------------------------------------------
9080 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9082 return m_defaultCellAttr
->GetBackgroundColour();
9085 wxColour
wxGrid::GetDefaultCellTextColour()
9087 return m_defaultCellAttr
->GetTextColour();
9090 wxFont
wxGrid::GetDefaultCellFont()
9092 return m_defaultCellAttr
->GetFont();
9095 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9097 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9100 bool wxGrid::GetDefaultCellOverflow()
9102 return m_defaultCellAttr
->GetOverflow();
9105 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9107 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9110 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9112 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
9115 // ----------------------------------------------------------------------------
9116 // access to cell attributes
9117 // ----------------------------------------------------------------------------
9119 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9121 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9122 wxColour colour
= attr
->GetBackgroundColour();
9127 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9129 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9130 wxColour colour
= attr
->GetTextColour();
9135 wxFont
wxGrid::GetCellFont( int row
, int col
)
9137 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9138 wxFont font
= attr
->GetFont();
9143 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9145 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9146 attr
->GetAlignment(horiz
, vert
);
9150 bool wxGrid::GetCellOverflow( int row
, int col
)
9152 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9153 bool allow
= attr
->GetOverflow();
9158 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9160 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9161 attr
->GetSize( num_rows
, num_cols
);
9165 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9167 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9168 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9174 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9176 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9177 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9183 bool wxGrid::IsReadOnly(int row
, int col
) const
9185 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9186 bool isReadOnly
= attr
->IsReadOnly();
9191 // ----------------------------------------------------------------------------
9192 // attribute support: cache, automatic provider creation, ...
9193 // ----------------------------------------------------------------------------
9195 bool wxGrid::CanHaveAttributes()
9202 return m_table
->CanHaveAttributes();
9205 void wxGrid::ClearAttrCache()
9207 if ( m_attrCache
.row
!= -1 )
9209 wxSafeDecRef(m_attrCache
.attr
);
9210 m_attrCache
.attr
= NULL
;
9211 m_attrCache
.row
= -1;
9215 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9219 wxGrid
*self
= (wxGrid
*)this; // const_cast
9221 self
->ClearAttrCache();
9222 self
->m_attrCache
.row
= row
;
9223 self
->m_attrCache
.col
= col
;
9224 self
->m_attrCache
.attr
= attr
;
9229 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9231 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9233 *attr
= m_attrCache
.attr
;
9234 wxSafeIncRef(m_attrCache
.attr
);
9236 #ifdef DEBUG_ATTR_CACHE
9237 gs_nAttrCacheHits
++;
9244 #ifdef DEBUG_ATTR_CACHE
9245 gs_nAttrCacheMisses
++;
9251 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9253 wxGridCellAttr
*attr
= NULL
;
9254 // Additional test to avoid looking at the cache e.g. for
9255 // wxNoCellCoords, as this will confuse memory management.
9258 if ( !LookupAttr(row
, col
, &attr
) )
9260 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9261 : (wxGridCellAttr
*)NULL
;
9262 CacheAttr(row
, col
, attr
);
9267 attr
->SetDefAttr(m_defaultCellAttr
);
9271 attr
= m_defaultCellAttr
;
9278 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9280 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9281 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9283 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9284 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9286 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9289 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9291 // artificially inc the ref count to match DecRef() in caller
9293 m_table
->SetAttr(attr
, row
, col
);
9299 // ----------------------------------------------------------------------------
9300 // setting column attributes (wrappers around SetColAttr)
9301 // ----------------------------------------------------------------------------
9303 void wxGrid::SetColFormatBool(int col
)
9305 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9308 void wxGrid::SetColFormatNumber(int col
)
9310 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9313 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9315 wxString typeName
= wxGRID_VALUE_FLOAT
;
9316 if ( (width
!= -1) || (precision
!= -1) )
9318 typeName
<< _T(':') << width
<< _T(',') << precision
;
9321 SetColFormatCustom(col
, typeName
);
9324 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9326 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9328 attr
= new wxGridCellAttr
;
9329 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9330 attr
->SetRenderer(renderer
);
9332 SetColAttr(col
, attr
);
9336 // ----------------------------------------------------------------------------
9337 // setting cell attributes: this is forwarded to the table
9338 // ----------------------------------------------------------------------------
9340 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9342 if ( CanHaveAttributes() )
9344 m_table
->SetAttr(attr
, row
, col
);
9353 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9355 if ( CanHaveAttributes() )
9357 m_table
->SetRowAttr(attr
, row
);
9366 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9368 if ( CanHaveAttributes() )
9370 m_table
->SetColAttr(attr
, col
);
9379 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9381 if ( CanHaveAttributes() )
9383 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9384 attr
->SetBackgroundColour(colour
);
9389 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9391 if ( CanHaveAttributes() )
9393 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9394 attr
->SetTextColour(colour
);
9399 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9401 if ( CanHaveAttributes() )
9403 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9404 attr
->SetFont(font
);
9409 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9411 if ( CanHaveAttributes() )
9413 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9414 attr
->SetAlignment(horiz
, vert
);
9419 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9421 if ( CanHaveAttributes() )
9423 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9424 attr
->SetOverflow(allow
);
9429 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9431 if ( CanHaveAttributes() )
9433 int cell_rows
, cell_cols
;
9435 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9436 attr
->GetSize(&cell_rows
, &cell_cols
);
9437 attr
->SetSize(num_rows
, num_cols
);
9440 // Cannot set the size of a cell to 0 or negative values
9441 // While it is perfectly legal to do that, this function cannot
9442 // handle all the possibilies, do it by hand by getting the CellAttr.
9443 // You can only set the size of a cell to 1,1 or greater with this fn
9444 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9445 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9446 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9447 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9449 // if this was already a multicell then "turn off" the other cells first
9450 if ((cell_rows
> 1) || (cell_rows
> 1))
9453 for (j
=row
; j
<row
+cell_rows
; j
++)
9455 for (i
=col
; i
<col
+cell_cols
; i
++)
9457 if ((i
!= col
) || (j
!= row
))
9459 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9460 attr_stub
->SetSize( 1, 1 );
9461 attr_stub
->DecRef();
9467 // mark the cells that will be covered by this cell to
9468 // negative or zero values to point back at this cell
9469 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9472 for (j
=row
; j
<row
+num_rows
; j
++)
9474 for (i
=col
; i
<col
+num_cols
; i
++)
9476 if ((i
!= col
) || (j
!= row
))
9478 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9479 attr_stub
->SetSize( row
-j
, col
-i
);
9480 attr_stub
->DecRef();
9488 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9490 if ( CanHaveAttributes() )
9492 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9493 attr
->SetRenderer(renderer
);
9498 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9500 if ( CanHaveAttributes() )
9502 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9503 attr
->SetEditor(editor
);
9508 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9510 if ( CanHaveAttributes() )
9512 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9513 attr
->SetReadOnly(isReadOnly
);
9518 // ----------------------------------------------------------------------------
9519 // Data type registration
9520 // ----------------------------------------------------------------------------
9522 void wxGrid::RegisterDataType(const wxString
& typeName
,
9523 wxGridCellRenderer
* renderer
,
9524 wxGridCellEditor
* editor
)
9526 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9530 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9532 wxString typeName
= m_table
->GetTypeName(row
, col
);
9533 return GetDefaultEditorForType(typeName
);
9536 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9538 wxString typeName
= m_table
->GetTypeName(row
, col
);
9539 return GetDefaultRendererForType(typeName
);
9543 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9545 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9546 if ( index
== wxNOT_FOUND
)
9548 wxFAIL_MSG(wxT("Unknown data type name"));
9553 return m_typeRegistry
->GetEditor(index
);
9557 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9559 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9560 if ( index
== wxNOT_FOUND
)
9562 wxFAIL_MSG(wxT("Unknown data type name"));
9567 return m_typeRegistry
->GetRenderer(index
);
9571 // ----------------------------------------------------------------------------
9573 // ----------------------------------------------------------------------------
9575 void wxGrid::EnableDragRowSize( bool enable
)
9577 m_canDragRowSize
= enable
;
9581 void wxGrid::EnableDragColSize( bool enable
)
9583 m_canDragColSize
= enable
;
9586 void wxGrid::EnableDragGridSize( bool enable
)
9588 m_canDragGridSize
= enable
;
9591 void wxGrid::EnableDragCell( bool enable
)
9593 m_canDragCell
= enable
;
9596 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9598 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9600 if ( resizeExistingRows
)
9602 // since we are resizing all rows to the default row size,
9603 // we can simply clear the row heights and row bottoms
9604 // arrays (which also allows us to take advantage of
9605 // some speed optimisations)
9606 m_rowHeights
.Empty();
9607 m_rowBottoms
.Empty();
9608 if ( !GetBatchCount() )
9613 void wxGrid::SetRowSize( int row
, int height
)
9615 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9617 // See comment in SetColSize
9618 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9620 if ( m_rowHeights
.IsEmpty() )
9622 // need to really create the array
9626 int h
= wxMax( 0, height
);
9627 int diff
= h
- m_rowHeights
[row
];
9629 m_rowHeights
[row
] = h
;
9631 for ( i
= row
; i
< m_numRows
; i
++ )
9633 m_rowBottoms
[i
] += diff
;
9635 if ( !GetBatchCount() )
9639 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9641 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9643 if ( resizeExistingCols
)
9645 // since we are resizing all columns to the default column size,
9646 // we can simply clear the col widths and col rights
9647 // arrays (which also allows us to take advantage of
9648 // some speed optimisations)
9649 m_colWidths
.Empty();
9650 m_colRights
.Empty();
9651 if ( !GetBatchCount() )
9656 void wxGrid::SetColSize( int col
, int width
)
9658 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9660 // should we check that it's bigger than GetColMinimalWidth(col) here?
9662 // No, because it is reasonable to assume the library user know's
9663 // what he is doing. However whe should test against the weaker
9664 // constariant of minimalAcceptableWidth, as this breaks rendering
9666 // This test then fixes sf.net bug #645734
9668 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9670 if ( m_colWidths
.IsEmpty() )
9672 // need to really create the array
9676 // if < 0 calc new width from label
9680 wxArrayString lines
;
9681 wxClientDC
dc(m_colLabelWin
);
9682 dc
.SetFont(GetLabelFont());
9683 StringToLines(GetColLabelValue(col
), lines
);
9684 GetTextBoxSize(dc
, lines
, &w
, &h
);
9687 int w
= wxMax( 0, width
);
9688 int diff
= w
- m_colWidths
[col
];
9689 m_colWidths
[col
] = w
;
9692 for ( i
= col
; i
< m_numCols
; i
++ )
9694 m_colRights
[i
] += diff
;
9696 if ( !GetBatchCount() )
9701 void wxGrid::SetColMinimalWidth( int col
, int width
)
9703 if (width
> GetColMinimalAcceptableWidth()) {
9704 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9705 m_colMinWidths
[key
] = width
;
9709 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9711 if (width
> GetRowMinimalAcceptableHeight()) {
9712 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9713 m_rowMinHeights
[key
] = width
;
9717 int wxGrid::GetColMinimalWidth(int col
) const
9719 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9720 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9721 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9724 int wxGrid::GetRowMinimalHeight(int row
) const
9726 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9727 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9728 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9731 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9733 // We do allow a width of 0 since this gives us
9734 // an easy way to temporarily hidding columns.
9737 m_minAcceptableColWidth
= width
;
9740 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9742 // We do allow a height of 0 since this gives us
9743 // an easy way to temporarily hidding rows.
9746 m_minAcceptableRowHeight
= height
;
9749 int wxGrid::GetColMinimalAcceptableWidth() const
9751 return m_minAcceptableColWidth
;
9754 int wxGrid::GetRowMinimalAcceptableHeight() const
9756 return m_minAcceptableRowHeight
;
9759 // ----------------------------------------------------------------------------
9761 // ----------------------------------------------------------------------------
9763 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9765 wxClientDC
dc(m_gridWin
);
9767 //Cancel editting of cell
9768 HideCellEditControl();
9769 SaveEditControlValue();
9771 // init both of them to avoid compiler warnings, even if weo nly need one
9779 wxCoord extent
, extentMax
= 0;
9780 int max
= column
? m_numRows
: m_numCols
;
9781 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9788 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9789 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9792 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9793 extent
= column
? size
.x
: size
.y
;
9794 if ( extent
> extentMax
)
9805 // now also compare with the column label extent
9807 dc
.SetFont( GetLabelFont() );
9811 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9812 if( GetColLabelTextOrientation() == wxVERTICAL
)
9816 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9818 extent
= column
? w
: h
;
9819 if ( extent
> extentMax
)
9826 // empty column - give default extent (notice that if extentMax is less
9827 // than default extent but != 0, it's ok)
9828 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9834 // leave some space around text
9845 SetColSize(col
, extentMax
);
9846 if ( !GetBatchCount() )
9849 m_gridWin
->GetClientSize( &cw
, &ch
);
9850 wxRect
rect ( CellToRect( 0, col
) );
9852 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9853 rect
.width
= cw
- rect
.x
;
9854 rect
.height
= m_colLabelHeight
;
9855 m_colLabelWin
->Refresh( true, &rect
);
9860 SetRowSize(row
, extentMax
);
9861 if ( !GetBatchCount() )
9864 m_gridWin
->GetClientSize( &cw
, &ch
);
9865 wxRect
rect ( CellToRect( row
, 0 ) );
9867 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9868 rect
.width
= m_rowLabelWidth
;
9869 rect
.height
= ch
- rect
.y
;
9870 m_rowLabelWin
->Refresh( true, &rect
);
9876 SetColMinimalWidth(col
, extentMax
);
9878 SetRowMinimalHeight(row
, extentMax
);
9882 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9884 int width
= m_rowLabelWidth
;
9889 for ( int col
= 0; col
< m_numCols
; col
++ )
9893 AutoSizeColumn(col
, setAsMin
);
9896 width
+= GetColWidth(col
);
9905 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9907 int height
= m_colLabelHeight
;
9912 for ( int row
= 0; row
< m_numRows
; row
++ )
9916 AutoSizeRow(row
, setAsMin
);
9919 height
+= GetRowHeight(row
);
9928 void wxGrid::AutoSize()
9932 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9934 // round up the size to a multiple of scroll step - this ensures that we
9935 // won't get the scrollbars if we're sized exactly to this width
9936 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9938 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
9939 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
9941 // distribute the extra space between the columns/rows to avoid having
9942 // extra white space
9944 // Remove the extra m_extraWidth + 1 added above
9945 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9946 if ( diff
&& m_numCols
)
9948 // try to resize the columns uniformly
9949 wxCoord diffPerCol
= diff
/ m_numCols
;
9952 for ( int col
= 0; col
< m_numCols
; col
++ )
9954 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9958 // add remaining amount to the last columns
9959 diff
-= diffPerCol
* m_numCols
;
9962 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9964 SetColSize(col
, GetColWidth(col
) + 1);
9970 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9971 if ( diff
&& m_numRows
)
9973 // try to resize the columns uniformly
9974 wxCoord diffPerRow
= diff
/ m_numRows
;
9977 for ( int row
= 0; row
< m_numRows
; row
++ )
9979 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9983 // add remaining amount to the last rows
9984 diff
-= diffPerRow
* m_numRows
;
9987 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9989 SetRowSize(row
, GetRowHeight(row
) + 1);
9996 SetClientSize(sizeFit
);
9999 void wxGrid::AutoSizeRowLabelSize( int row
)
10001 wxArrayString lines
;
10004 // Hide the edit control, so it
10005 // won't interfer with drag-shrinking.
10006 if( IsCellEditControlShown() )
10008 HideCellEditControl();
10009 SaveEditControlValue();
10012 // autosize row height depending on label text
10013 StringToLines( GetRowLabelValue( row
), lines
);
10014 wxClientDC
dc( m_rowLabelWin
);
10015 GetTextBoxSize( dc
, lines
, &w
, &h
);
10016 if( h
< m_defaultRowHeight
)
10017 h
= m_defaultRowHeight
;
10018 SetRowSize(row
, h
);
10022 void wxGrid::AutoSizeColLabelSize( int col
)
10024 wxArrayString lines
;
10027 // Hide the edit control, so it
10028 // won't interfer with drag-shrinking.
10029 if( IsCellEditControlShown() )
10031 HideCellEditControl();
10032 SaveEditControlValue();
10035 // autosize column width depending on label text
10036 StringToLines( GetColLabelValue( col
), lines
);
10037 wxClientDC
dc( m_colLabelWin
);
10038 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
10039 GetTextBoxSize( dc
, lines
, &w
, &h
);
10041 GetTextBoxSize( dc
, lines
, &h
, &w
);
10042 if( w
< m_defaultColWidth
)
10043 w
= m_defaultColWidth
;
10044 SetColSize(col
, w
);
10048 wxSize
wxGrid::DoGetBestSize() const
10050 // don't set sizes, only calculate them
10051 wxGrid
*self
= (wxGrid
*)this; // const_cast
10054 width
= self
->SetOrCalcColumnSizes(true);
10055 height
= self
->SetOrCalcRowSizes(true);
10057 if (!width
) width
=100;
10058 if (!height
) height
=80;
10060 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
10061 // of the scrollbars, is there any magic incantaion for that?
10063 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10065 width
+= 1 + xpu
- (width
% xpu
);
10067 height
+= 1 + ypu
- (height
% ypu
);
10069 // limit to 1/4 of the screen size
10070 int maxwidth
, maxheight
;
10071 wxDisplaySize( & maxwidth
, & maxheight
);
10074 if ( width
> maxwidth
) width
= maxwidth
;
10075 if ( height
> maxheight
) height
= maxheight
;
10078 wxSize
best(width
, height
);
10079 // NOTE: This size should be cached, but first we need to add calls to
10080 // InvalidateBestSize everywhere that could change the results of this
10082 // CacheBestSize(size);
10092 wxPen
& wxGrid::GetDividerPen() const
10097 // ----------------------------------------------------------------------------
10098 // cell value accessor functions
10099 // ----------------------------------------------------------------------------
10101 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10105 m_table
->SetValue( row
, col
, s
);
10106 if ( !GetBatchCount() )
10109 wxRect
rect( CellToRect( row
, col
) );
10111 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10112 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10113 m_gridWin
->Refresh( false, &rect
);
10116 if ( m_currentCellCoords
.GetRow() == row
&&
10117 m_currentCellCoords
.GetCol() == col
&&
10118 IsCellEditControlShown())
10119 // Note: If we are using IsCellEditControlEnabled,
10120 // this interacts badly with calling SetCellValue from
10121 // an EVT_GRID_CELL_CHANGE handler.
10123 HideCellEditControl();
10124 ShowCellEditControl(); // will reread data from table
10131 // ------ Block, row and col selection
10134 void wxGrid::SelectRow( int row
, bool addToSelected
)
10136 if ( IsSelection() && !addToSelected
)
10140 m_selection
->SelectRow( row
, false, addToSelected
);
10144 void wxGrid::SelectCol( int col
, bool addToSelected
)
10146 if ( IsSelection() && !addToSelected
)
10150 m_selection
->SelectCol( col
, false, addToSelected
);
10154 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10155 bool addToSelected
)
10157 if ( IsSelection() && !addToSelected
)
10161 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10162 false, addToSelected
);
10166 void wxGrid::SelectAll()
10168 if ( m_numRows
> 0 && m_numCols
> 0 )
10171 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10176 // ------ Cell, row and col deselection
10179 void wxGrid::DeselectRow( int row
)
10181 if ( !m_selection
)
10184 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10186 if ( m_selection
->IsInSelection(row
, 0 ) )
10187 m_selection
->ToggleCellSelection( row
, 0);
10191 int nCols
= GetNumberCols();
10192 for ( int i
= 0; i
< nCols
; i
++ )
10194 if ( m_selection
->IsInSelection(row
, i
) )
10195 m_selection
->ToggleCellSelection( row
, i
);
10200 void wxGrid::DeselectCol( int col
)
10202 if ( !m_selection
)
10205 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10207 if ( m_selection
->IsInSelection(0, col
) )
10208 m_selection
->ToggleCellSelection( 0, col
);
10212 int nRows
= GetNumberRows();
10213 for ( int i
= 0; i
< nRows
; i
++ )
10215 if ( m_selection
->IsInSelection(i
, col
) )
10216 m_selection
->ToggleCellSelection(i
, col
);
10221 void wxGrid::DeselectCell( int row
, int col
)
10223 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10224 m_selection
->ToggleCellSelection(row
, col
);
10227 bool wxGrid::IsSelection()
10229 return ( m_selection
&& (m_selection
->IsSelection() ||
10230 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10231 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10234 bool wxGrid::IsInSelection( int row
, int col
) const
10236 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10237 ( row
>= m_selectingTopLeft
.GetRow() &&
10238 col
>= m_selectingTopLeft
.GetCol() &&
10239 row
<= m_selectingBottomRight
.GetRow() &&
10240 col
<= m_selectingBottomRight
.GetCol() )) );
10243 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10245 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10246 return m_selection
->m_cellSelection
;
10248 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10250 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10251 return m_selection
->m_blockSelectionTopLeft
;
10253 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10255 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10256 return m_selection
->m_blockSelectionBottomRight
;
10258 wxArrayInt
wxGrid::GetSelectedRows() const
10260 if (!m_selection
) { wxArrayInt a
; return a
; }
10261 return m_selection
->m_rowSelection
;
10263 wxArrayInt
wxGrid::GetSelectedCols() const
10265 if (!m_selection
) { wxArrayInt a
; return a
; }
10266 return m_selection
->m_colSelection
;
10270 void wxGrid::ClearSelection()
10272 m_selectingTopLeft
= wxGridNoCellCoords
;
10273 m_selectingBottomRight
= wxGridNoCellCoords
;
10275 m_selection
->ClearSelection();
10279 // This function returns the rectangle that encloses the given block
10280 // in device coords clipped to the client size of the grid window.
10282 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10283 const wxGridCellCoords
&bottomRight
)
10285 wxRect
rect( wxGridNoCellRect
);
10288 cellRect
= CellToRect( topLeft
);
10289 if ( cellRect
!= wxGridNoCellRect
)
10295 rect
= wxRect(0,0,0,0);
10298 cellRect
= CellToRect( bottomRight
);
10299 if ( cellRect
!= wxGridNoCellRect
)
10305 return wxGridNoCellRect
;
10309 int left
= rect
.GetLeft();
10310 int top
= rect
.GetTop();
10311 int right
= rect
.GetRight();
10312 int bottom
= rect
.GetBottom();
10314 int leftCol
= topLeft
.GetCol();
10315 int topRow
= topLeft
.GetRow();
10316 int rightCol
= bottomRight
.GetCol();
10317 int bottomRow
= bottomRight
.GetRow();
10335 topRow
= bottomRow
;
10340 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10342 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10344 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10346 cellRect
= CellToRect( j
, i
);
10348 if (cellRect
.x
< left
)
10350 if (cellRect
.y
< top
)
10352 if (cellRect
.x
+ cellRect
.width
> right
)
10353 right
= cellRect
.x
+ cellRect
.width
;
10354 if (cellRect
.y
+ cellRect
.height
> bottom
)
10355 bottom
= cellRect
.y
+ cellRect
.height
;
10357 else i
= rightCol
; // jump over inner cells.
10361 // convert to scrolled coords
10363 CalcScrolledPosition( left
, top
, &left
, &top
);
10364 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10367 m_gridWin
->GetClientSize( &cw
, &ch
);
10369 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10370 return wxRect(0,0,0,0);
10372 rect
.SetLeft( wxMax(0, left
) );
10373 rect
.SetTop( wxMax(0, top
) );
10374 rect
.SetRight( wxMin(cw
, right
) );
10375 rect
.SetBottom( wxMin(ch
, bottom
) );
10381 // ------ Grid event classes
10384 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10386 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10387 int row
, int col
, int x
, int y
, bool sel
,
10388 bool control
, bool shift
, bool alt
, bool meta
)
10389 : wxNotifyEvent( type
, id
)
10396 m_control
= control
;
10401 SetEventObject(obj
);
10405 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10407 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10408 int rowOrCol
, int x
, int y
,
10409 bool control
, bool shift
, bool alt
, bool meta
)
10410 : wxNotifyEvent( type
, id
)
10412 m_rowOrCol
= rowOrCol
;
10415 m_control
= control
;
10420 SetEventObject(obj
);
10424 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10426 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10427 const wxGridCellCoords
& topLeft
,
10428 const wxGridCellCoords
& bottomRight
,
10429 bool sel
, bool control
,
10430 bool shift
, bool alt
, bool meta
)
10431 : wxNotifyEvent( type
, id
)
10433 m_topLeft
= topLeft
;
10434 m_bottomRight
= bottomRight
;
10436 m_control
= control
;
10441 SetEventObject(obj
);
10445 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10447 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10448 wxObject
* obj
, int row
,
10449 int col
, wxControl
* ctrl
)
10450 : wxCommandEvent(type
, id
)
10452 SetEventObject(obj
);
10458 #endif // wxUSE_GRID