1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/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, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
26 #include "wx/settings.h"
28 #include "wx/textctrl.h"
29 #include "wx/checkbox.h"
30 #include "wx/combobox.h"
31 #include "wx/valtext.h"
34 #include "wx/listbox.h"
37 #include "wx/textfile.h"
38 #include "wx/spinctrl.h"
39 #include "wx/tokenzr.h"
40 #include "wx/renderer.h"
42 #include "wx/generic/gridsel.h"
44 const wxChar wxGridNameStr
[] = wxT("grid");
46 #if defined(__WXMOTIF__)
47 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
49 #define WXUNUSED_MOTIF(identifier) identifier
52 #if defined(__WXGTK__)
53 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
55 #define WXUNUSED_GTK(identifier) identifier
58 // Required for wxIs... functions
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
66 class WXDLLIMPEXP_ADV
);
68 struct wxGridCellWithAttr
70 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
71 : coords(row
, col
), attr(attr_
)
76 wxGridCellWithAttr(const wxGridCellWithAttr
& other
)
77 : coords(other
.coords
),
83 wxGridCellWithAttr
& operator=(const wxGridCellWithAttr
& other
)
85 coords
= other
.coords
;
86 if (attr
!= other
.attr
)
95 void ChangeAttr(wxGridCellAttr
* new_attr
)
99 // "Delete" (i.e. DecRef) the old attribute.
102 // Take ownership of the new attribute, i.e. no IncRef.
106 ~wxGridCellWithAttr()
111 wxGridCellCoords coords
;
112 wxGridCellAttr
*attr
;
115 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
116 class WXDLLIMPEXP_ADV
);
118 #include "wx/arrimpl.cpp"
120 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
121 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
128 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
129 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
130 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
131 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
132 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
133 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
134 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
135 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
136 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
137 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
138 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
139 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
140 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
141 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
142 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
143 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
144 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 // common base class for various grid subwindows
151 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
154 wxGridSubwindow() { m_owner
= NULL
; }
155 wxGridSubwindow(wxGrid
*owner
,
159 int additionalStyle
= 0,
160 const wxString
& name
= wxPanelNameStr
)
161 : wxWindow(owner
, id
, pos
, size
,
162 wxBORDER_NONE
| additionalStyle
,
168 virtual bool AcceptsFocus() const { return false; }
170 wxGrid
*GetOwner() { return m_owner
; }
173 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
177 DECLARE_EVENT_TABLE()
178 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
181 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
184 wxGridRowLabelWindow() { }
185 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
186 const wxPoint
&pos
, const wxSize
&size
);
189 void OnPaint( wxPaintEvent
& event
);
190 void OnMouseEvent( wxMouseEvent
& event
);
191 void OnMouseWheel( wxMouseEvent
& event
);
193 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
194 DECLARE_EVENT_TABLE()
195 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
199 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
202 wxGridColLabelWindow() { }
203 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
204 const wxPoint
&pos
, const wxSize
&size
);
207 void OnPaint( wxPaintEvent
& event
);
208 void OnMouseEvent( wxMouseEvent
& event
);
209 void OnMouseWheel( wxMouseEvent
& event
);
211 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
212 DECLARE_EVENT_TABLE()
213 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
217 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
220 wxGridCornerLabelWindow() { }
221 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
222 const wxPoint
&pos
, const wxSize
&size
);
225 void OnMouseEvent( wxMouseEvent
& event
);
226 void OnMouseWheel( wxMouseEvent
& event
);
227 void OnPaint( wxPaintEvent
& event
);
229 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
230 DECLARE_EVENT_TABLE()
231 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
234 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
239 m_rowLabelWin
= NULL
;
240 m_colLabelWin
= NULL
;
243 wxGridWindow( wxGrid
*parent
,
244 wxGridRowLabelWindow
*rowLblWin
,
245 wxGridColLabelWindow
*colLblWin
,
246 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
248 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
250 virtual bool AcceptsFocus() const { return true; }
253 wxGridRowLabelWindow
*m_rowLabelWin
;
254 wxGridColLabelWindow
*m_colLabelWin
;
256 void OnPaint( wxPaintEvent
&event
);
257 void OnMouseWheel( wxMouseEvent
& event
);
258 void OnMouseEvent( wxMouseEvent
& event
);
259 void OnKeyDown( wxKeyEvent
& );
260 void OnKeyUp( wxKeyEvent
& );
261 void OnChar( wxKeyEvent
& );
262 void OnEraseBackground( wxEraseEvent
& );
263 void OnFocus( wxFocusEvent
& );
265 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
266 DECLARE_EVENT_TABLE()
267 DECLARE_NO_COPY_CLASS(wxGridWindow
)
271 class wxGridCellEditorEvtHandler
: public wxEvtHandler
274 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
281 void OnKillFocus(wxFocusEvent
& event
);
282 void OnKeyDown(wxKeyEvent
& event
);
283 void OnChar(wxKeyEvent
& event
);
285 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
289 wxGridCellEditor
*m_editor
;
291 // Work around the fact that a focus kill event can be sent to
292 // a combobox within a set focus event.
295 DECLARE_EVENT_TABLE()
296 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
297 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
301 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
303 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
304 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
305 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
306 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
310 // ----------------------------------------------------------------------------
311 // the internal data representation used by wxGridCellAttrProvider
312 // ----------------------------------------------------------------------------
314 // this class stores attributes set for cells
315 class WXDLLIMPEXP_ADV wxGridCellAttrData
318 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
319 wxGridCellAttr
*GetAttr(int row
, int col
) const;
320 void UpdateAttrRows( size_t pos
, int numRows
);
321 void UpdateAttrCols( size_t pos
, int numCols
);
324 // searches for the attr for given cell, returns wxNOT_FOUND if not found
325 int FindIndex(int row
, int col
) const;
327 wxGridCellWithAttrArray m_attrs
;
330 // this class stores attributes set for rows or columns
331 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
334 // empty ctor to suppress warnings
335 wxGridRowOrColAttrData() {}
336 ~wxGridRowOrColAttrData();
338 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
339 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
340 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
343 wxArrayInt m_rowsOrCols
;
344 wxArrayAttrs m_attrs
;
347 // NB: this is just a wrapper around 3 objects: one which stores cell
348 // attributes, and 2 others for row/col ones
349 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
352 wxGridCellAttrData m_cellAttrs
;
353 wxGridRowOrColAttrData m_rowAttrs
,
358 // ----------------------------------------------------------------------------
359 // data structures used for the data type registry
360 // ----------------------------------------------------------------------------
362 struct wxGridDataTypeInfo
364 wxGridDataTypeInfo(const wxString
& typeName
,
365 wxGridCellRenderer
* renderer
,
366 wxGridCellEditor
* editor
)
367 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
370 ~wxGridDataTypeInfo()
372 wxSafeDecRef(m_renderer
);
373 wxSafeDecRef(m_editor
);
377 wxGridCellRenderer
* m_renderer
;
378 wxGridCellEditor
* m_editor
;
380 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
384 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
385 class WXDLLIMPEXP_ADV
);
388 class WXDLLIMPEXP_ADV wxGridTypeRegistry
391 wxGridTypeRegistry() {}
392 ~wxGridTypeRegistry();
394 void RegisterDataType(const wxString
& typeName
,
395 wxGridCellRenderer
* renderer
,
396 wxGridCellEditor
* editor
);
398 // find one of already registered data types
399 int FindRegisteredDataType(const wxString
& typeName
);
401 // try to FindRegisteredDataType(), if this fails and typeName is one of
402 // standard typenames, register it and return its index
403 int FindDataType(const wxString
& typeName
);
405 // try to FindDataType(), if it fails see if it is not one of already
406 // registered data types with some params in which case clone the
407 // registered data type and set params for it
408 int FindOrCloneDataType(const wxString
& typeName
);
410 wxGridCellRenderer
* GetRenderer(int index
);
411 wxGridCellEditor
* GetEditor(int index
);
414 wxGridDataTypeInfoArray m_typeinfo
;
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 //#define DEBUG_ATTR_CACHE
422 #ifdef DEBUG_ATTR_CACHE
423 static size_t gs_nAttrCacheHits
= 0;
424 static size_t gs_nAttrCacheMisses
= 0;
427 // ----------------------------------------------------------------------------
429 // ----------------------------------------------------------------------------
431 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
432 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
435 static const size_t GRID_SCROLL_LINE_X
= 15;
436 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
438 // the size of hash tables used a bit everywhere (the max number of elements
439 // in these hash tables is the number of rows/columns)
440 static const int GRID_HASH_SIZE
= 100;
442 // ============================================================================
444 // ============================================================================
446 // ----------------------------------------------------------------------------
448 // ----------------------------------------------------------------------------
450 wxGridCellEditor::wxGridCellEditor()
456 wxGridCellEditor::~wxGridCellEditor()
461 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
462 wxWindowID
WXUNUSED(id
),
463 wxEvtHandler
* evtHandler
)
466 m_control
->PushEventHandler(evtHandler
);
469 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
470 wxGridCellAttr
*attr
)
472 // erase the background because we might not fill the cell
473 wxClientDC
dc(m_control
->GetParent());
474 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
476 gridWindow
->GetOwner()->PrepareDC(dc
);
478 dc
.SetPen(*wxTRANSPARENT_PEN
);
479 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
480 dc
.DrawRectangle(rectCell
);
482 // redraw the control we just painted over
483 m_control
->Refresh();
486 void wxGridCellEditor::Destroy()
490 m_control
->PopEventHandler( true /* delete it*/ );
492 m_control
->Destroy();
497 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
499 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
501 m_control
->Show(show
);
505 // set the colours/fonts if we have any
508 m_colFgOld
= m_control
->GetForegroundColour();
509 m_control
->SetForegroundColour(attr
->GetTextColour());
511 m_colBgOld
= m_control
->GetBackgroundColour();
512 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
514 // Workaround for GTK+1 font setting problem on some platforms
515 #if !defined(__WXGTK__) || defined(__WXGTK20__)
516 m_fontOld
= m_control
->GetFont();
517 m_control
->SetFont(attr
->GetFont());
520 // can't do anything more in the base class version, the other
521 // attributes may only be used by the derived classes
526 // restore the standard colours fonts
527 if ( m_colFgOld
.Ok() )
529 m_control
->SetForegroundColour(m_colFgOld
);
530 m_colFgOld
= wxNullColour
;
533 if ( m_colBgOld
.Ok() )
535 m_control
->SetBackgroundColour(m_colBgOld
);
536 m_colBgOld
= wxNullColour
;
539 // Workaround for GTK+1 font setting problem on some platforms
540 #if !defined(__WXGTK__) || defined(__WXGTK20__)
541 if ( m_fontOld
.Ok() )
543 m_control
->SetFont(m_fontOld
);
544 m_fontOld
= wxNullFont
;
550 void wxGridCellEditor::SetSize(const wxRect
& rect
)
552 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
554 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
557 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
562 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
564 bool ctrl
= event
.ControlDown();
565 bool alt
= event
.AltDown();
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
))
583 // If it's a F-Key or other special key then it shouldn't start the
585 if (event
.GetKeyCode() >= WXK_START
)
589 // if the unicode key code is not really a unicode character (it may
590 // be a function key or etc., the platforms appear to always give us a
591 // small value in this case) then fallback to the ASCII key code but
592 // don't do anything for function keys or etc.
593 key
= event
.GetUnicodeKey();
596 key
= event
.GetKeyCode();
597 keyOk
= (key
<= 127);
600 key
= event
.GetKeyCode();
601 keyOk
= (key
<= 255);
607 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
612 void wxGridCellEditor::StartingClick()
618 // ----------------------------------------------------------------------------
619 // wxGridCellTextEditor
620 // ----------------------------------------------------------------------------
622 wxGridCellTextEditor::wxGridCellTextEditor()
627 void wxGridCellTextEditor::Create(wxWindow
* parent
,
629 wxEvtHandler
* evtHandler
)
631 DoCreate(parent
, id
, evtHandler
);
634 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
636 wxEvtHandler
* evtHandler
,
639 style
|= wxTE_PROCESS_ENTER
| wxTE_PROCESS_TAB
| wxNO_BORDER
;
641 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
642 wxDefaultPosition
, wxDefaultSize
,
645 // set max length allowed in the textctrl, if the parameter was set
646 if ( m_maxChars
!= 0 )
648 Text()->SetMaxLength(m_maxChars
);
651 wxGridCellEditor::Create(parent
, id
, evtHandler
);
654 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
655 wxGridCellAttr
* WXUNUSED(attr
))
657 // as we fill the entire client area,
658 // don't do anything here to minimize flicker
661 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
663 wxRect
rect(rectOrig
);
665 // Make the edit control large enough to allow for internal margins
667 // TODO: remove this if the text ctrl sizing is improved esp. for unix
669 #if defined(__WXGTK__)
677 #elif defined(__WXMSW__)
691 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
692 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
694 #if defined(__WXMOTIF__)
699 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
700 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
701 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
702 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
705 wxGridCellEditor::SetSize(rect
);
708 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
710 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
712 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
714 DoBeginEdit(m_startValue
);
717 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
719 Text()->SetValue(startValue
);
720 Text()->SetInsertionPointEnd();
721 Text()->SetSelection(-1, -1);
725 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
727 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
729 bool changed
= false;
730 wxString value
= Text()->GetValue();
731 if (value
!= m_startValue
)
735 grid
->GetTable()->SetValue(row
, col
, value
);
737 m_startValue
= wxEmptyString
;
739 // No point in setting the text of the hidden control
740 //Text()->SetValue(m_startValue);
745 void wxGridCellTextEditor::Reset()
747 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
749 DoReset(m_startValue
);
752 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
754 Text()->SetValue(startValue
);
755 Text()->SetInsertionPointEnd();
758 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
760 return wxGridCellEditor::IsAcceptedKey(event
);
763 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
765 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
766 // longer an appropriate way to get the character into the text control.
767 // Do it ourselves instead. We know that if we get this far that we have
768 // a valid character, so not a whole lot of testing needs to be done.
770 wxTextCtrl
* tc
= Text();
775 ch
= event
.GetUnicodeKey();
777 ch
= (wxChar
)event
.GetKeyCode();
779 ch
= (wxChar
)event
.GetKeyCode();
785 // delete the character at the cursor
786 pos
= tc
->GetInsertionPoint();
787 if (pos
< tc
->GetLastPosition())
788 tc
->Remove(pos
, pos
+ 1);
792 // delete the character before the cursor
793 pos
= tc
->GetInsertionPoint();
795 tc
->Remove(pos
- 1, pos
);
804 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
805 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
807 #if defined(__WXMOTIF__) || defined(__WXGTK__)
808 // wxMotif needs a little extra help...
809 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
810 wxString
s( Text()->GetValue() );
811 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
813 Text()->SetInsertionPoint( pos
);
815 // the other ports can handle a Return key press
821 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
831 if ( params
.ToLong(&tmp
) )
833 m_maxChars
= (size_t)tmp
;
837 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
842 // return the value in the text control
843 wxString
wxGridCellTextEditor::GetValue() const
845 return Text()->GetValue();
848 // ----------------------------------------------------------------------------
849 // wxGridCellNumberEditor
850 // ----------------------------------------------------------------------------
852 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
858 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
860 wxEvtHandler
* evtHandler
)
865 // create a spin ctrl
866 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
867 wxDefaultPosition
, wxDefaultSize
,
871 wxGridCellEditor::Create(parent
, id
, evtHandler
);
876 // just a text control
877 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
880 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
885 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
887 // first get the value
888 wxGridTableBase
*table
= grid
->GetTable();
889 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
891 m_valueOld
= table
->GetValueAsLong(row
, col
);
896 wxString sValue
= table
->GetValue(row
, col
);
897 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
899 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
907 Spin()->SetValue((int)m_valueOld
);
913 DoBeginEdit(GetString());
917 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
926 value
= Spin()->GetValue();
927 if ( value
== m_valueOld
)
930 text
.Printf(wxT("%ld"), value
);
932 else // using unconstrained input
933 #endif // wxUSE_SPINCTRL
935 const wxString
textOld(grid
->GetCellValue(row
, col
));
936 text
= Text()->GetValue();
939 if ( textOld
.empty() )
942 else // non-empty text now (maybe 0)
944 if ( !text
.ToLong(&value
) )
947 // if value == m_valueOld == 0 but old text was "" and new one is
948 // "0" something still did change
949 if ( value
== m_valueOld
&& (value
|| !textOld
.empty()) )
954 wxGridTableBase
* const table
= grid
->GetTable();
955 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
956 table
->SetValueAsLong(row
, col
, value
);
958 table
->SetValue(row
, col
, text
);
963 void wxGridCellNumberEditor::Reset()
968 Spin()->SetValue((int)m_valueOld
);
973 DoReset(GetString());
977 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
979 if ( wxGridCellEditor::IsAcceptedKey(event
) )
981 int keycode
= event
.GetKeyCode();
982 if ( (keycode
< 128) &&
983 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
992 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
994 int keycode
= event
.GetKeyCode();
997 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
999 wxGridCellTextEditor::StartingKey(event
);
1001 // skip Skip() below
1008 if ( wxIsdigit(keycode
) )
1010 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1011 spin
->SetValue(keycode
- '0');
1012 spin
->SetSelection(1,1);
1021 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1032 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1036 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1040 // skip the error message below
1045 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1049 // return the value in the spin control if it is there (the text control otherwise)
1050 wxString
wxGridCellNumberEditor::GetValue() const
1057 long value
= Spin()->GetValue();
1058 s
.Printf(wxT("%ld"), value
);
1063 s
= Text()->GetValue();
1069 // ----------------------------------------------------------------------------
1070 // wxGridCellFloatEditor
1071 // ----------------------------------------------------------------------------
1073 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1076 m_precision
= precision
;
1079 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1081 wxEvtHandler
* evtHandler
)
1083 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1085 #if wxUSE_VALIDATORS
1086 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1090 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1092 // first get the value
1093 wxGridTableBase
* const table
= grid
->GetTable();
1094 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1096 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1102 const wxString value
= table
->GetValue(row
, col
);
1103 if ( !value
.empty() )
1105 if ( !value
.ToDouble(&m_valueOld
) )
1107 wxFAIL_MSG( _T("this cell doesn't have float value") );
1113 DoBeginEdit(GetString());
1116 bool wxGridCellFloatEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
1118 const wxString
text(Text()->GetValue()),
1119 textOld(grid
->GetCellValue(row
, col
));
1122 if ( !text
.empty() )
1124 if ( !text
.ToDouble(&value
) )
1127 else // new value is empty string
1129 if ( textOld
.empty() )
1130 return false; // nothing changed
1135 // the test for empty strings ensures that we don't skip the value setting
1136 // when "" is replaced by "0" or vice versa as "" numeric value is also 0.
1137 if ( wxIsSameDouble(value
, m_valueOld
) && !text
.empty() && !textOld
.empty() )
1138 return false; // nothing changed
1140 wxGridTableBase
* const table
= grid
->GetTable();
1142 if ( table
->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1143 table
->SetValueAsDouble(row
, col
, value
);
1145 table
->SetValue(row
, col
, text
);
1150 void wxGridCellFloatEditor::Reset()
1152 DoReset(GetString());
1155 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1157 int keycode
= event
.GetKeyCode();
1159 tmpbuf
[0] = (char) keycode
;
1161 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1164 bool is_decimal_point
= ( strbuf
==
1165 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1167 bool is_decimal_point
= ( strbuf
== _T(".") );
1170 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1171 || is_decimal_point
)
1173 wxGridCellTextEditor::StartingKey(event
);
1175 // skip Skip() below
1182 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1193 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1197 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1199 m_precision
= (int)tmp
;
1201 // skip the error message below
1206 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1210 wxString
wxGridCellFloatEditor::GetString() const
1213 if ( m_precision
== -1 && m_width
!= -1)
1215 // default precision
1216 fmt
.Printf(_T("%%%d.f"), m_width
);
1218 else if ( m_precision
!= -1 && m_width
== -1)
1221 fmt
.Printf(_T("%%.%df"), m_precision
);
1223 else if ( m_precision
!= -1 && m_width
!= -1 )
1225 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1229 // default width/precision
1233 return wxString::Format(fmt
, m_valueOld
);
1236 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1238 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1240 const int keycode
= event
.GetKeyCode();
1241 if ( isascii(keycode
) )
1244 tmpbuf
[0] = (char) keycode
;
1246 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1249 const wxString decimalPoint
=
1250 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1252 const wxString
decimalPoint(_T('.'));
1255 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1256 if ( wxIsdigit(keycode
) ||
1257 tolower(keycode
) == 'e' ||
1258 keycode
== decimalPoint
||
1270 #endif // wxUSE_TEXTCTRL
1274 // ----------------------------------------------------------------------------
1275 // wxGridCellBoolEditor
1276 // ----------------------------------------------------------------------------
1278 // the default values for GetValue()
1279 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1281 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1283 wxEvtHandler
* evtHandler
)
1285 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1286 wxDefaultPosition
, wxDefaultSize
,
1289 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1292 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1294 bool resize
= false;
1295 wxSize size
= m_control
->GetSize();
1296 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1298 // check if the checkbox is not too big/small for this cell
1299 wxSize sizeBest
= m_control
->GetBestSize();
1300 if ( !(size
== sizeBest
) )
1302 // reset to default size if it had been made smaller
1308 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1310 // leave 1 pixel margin
1311 size
.x
= size
.y
= minSize
- 2;
1318 m_control
->SetSize(size
);
1321 // position it in the centre of the rectangle (TODO: support alignment?)
1323 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1324 // the checkbox without label still has some space to the right in wxGTK,
1325 // so shift it to the right
1327 #elif defined(__WXMSW__)
1328 // here too, but in other way
1333 int hAlign
= wxALIGN_CENTRE
;
1334 int vAlign
= wxALIGN_CENTRE
;
1336 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1339 if (hAlign
== wxALIGN_LEFT
)
1347 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1349 else if (hAlign
== wxALIGN_RIGHT
)
1351 x
= r
.x
+ r
.width
- size
.x
- 2;
1352 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1354 else if (hAlign
== wxALIGN_CENTRE
)
1356 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1357 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1360 m_control
->Move(x
, y
);
1363 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1365 m_control
->Show(show
);
1369 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1370 CBox()->SetBackgroundColour(colBg
);
1374 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1376 wxASSERT_MSG(m_control
,
1377 wxT("The wxGridCellEditor must be created first!"));
1379 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1381 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1385 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1387 if ( cellval
== ms_stringValues
[false] )
1388 m_startValue
= false;
1389 else if ( cellval
== ms_stringValues
[true] )
1390 m_startValue
= true;
1393 // do not try to be smart here and convert it to true or false
1394 // because we'll still overwrite it with something different and
1395 // this risks to be very surprising for the user code, let them
1397 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1401 CBox()->SetValue(m_startValue
);
1405 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1408 wxASSERT_MSG(m_control
,
1409 wxT("The wxGridCellEditor must be created first!"));
1411 bool changed
= false;
1412 bool value
= CBox()->GetValue();
1413 if ( value
!= m_startValue
)
1418 wxGridTableBase
* const table
= grid
->GetTable();
1419 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1420 table
->SetValueAsBool(row
, col
, value
);
1422 table
->SetValue(row
, col
, GetValue());
1428 void wxGridCellBoolEditor::Reset()
1430 wxASSERT_MSG(m_control
,
1431 wxT("The wxGridCellEditor must be created first!"));
1433 CBox()->SetValue(m_startValue
);
1436 void wxGridCellBoolEditor::StartingClick()
1438 CBox()->SetValue(!CBox()->GetValue());
1441 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1443 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1445 int keycode
= event
.GetKeyCode();
1458 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1460 int keycode
= event
.GetKeyCode();
1464 CBox()->SetValue(!CBox()->GetValue());
1468 CBox()->SetValue(true);
1472 CBox()->SetValue(false);
1477 wxString
wxGridCellBoolEditor::GetValue() const
1479 return ms_stringValues
[CBox()->GetValue()];
1483 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1484 const wxString
& valueFalse
)
1486 ms_stringValues
[false] = valueFalse
;
1487 ms_stringValues
[true] = valueTrue
;
1491 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1493 return value
== ms_stringValues
[true];
1496 #endif // wxUSE_CHECKBOX
1500 // ----------------------------------------------------------------------------
1501 // wxGridCellChoiceEditor
1502 // ----------------------------------------------------------------------------
1504 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1506 : m_choices(choices
),
1507 m_allowOthers(allowOthers
) { }
1509 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1510 const wxString choices
[],
1512 : m_allowOthers(allowOthers
)
1516 m_choices
.Alloc(count
);
1517 for ( size_t n
= 0; n
< count
; n
++ )
1519 m_choices
.Add(choices
[n
]);
1524 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1526 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1527 editor
->m_allowOthers
= m_allowOthers
;
1528 editor
->m_choices
= m_choices
;
1533 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1535 wxEvtHandler
* evtHandler
)
1537 int style
= wxTE_PROCESS_ENTER
|
1541 if ( !m_allowOthers
)
1542 style
|= wxCB_READONLY
;
1543 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1544 wxDefaultPosition
, wxDefaultSize
,
1548 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1551 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1552 wxGridCellAttr
* attr
)
1554 // as we fill the entire client area, don't do anything here to minimize
1557 // TODO: It doesn't actually fill the client area since the height of a
1558 // combo always defaults to the standard. Until someone has time to
1559 // figure out the right rectangle to paint, just do it the normal way.
1560 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1563 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1565 wxASSERT_MSG(m_control
,
1566 wxT("The wxGridCellEditor must be created first!"));
1568 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1570 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1572 // Don't immediately end if we get a kill focus event within BeginEdit
1574 evtHandler
->SetInSetFocus(true);
1576 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1578 Reset(); // this updates combo box to correspond to m_startValue
1580 Combo()->SetFocus();
1584 // When dropping down the menu, a kill focus event
1585 // happens after this point, so we can't reset the flag yet.
1586 #if !defined(__WXGTK20__)
1587 evtHandler
->SetInSetFocus(false);
1592 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1595 wxString value
= Combo()->GetValue();
1596 if ( value
== m_startValue
)
1599 grid
->GetTable()->SetValue(row
, col
, value
);
1604 void wxGridCellChoiceEditor::Reset()
1608 Combo()->SetValue(m_startValue
);
1609 Combo()->SetInsertionPointEnd();
1611 else // the combobox is read-only
1613 // find the right position, or default to the first if not found
1614 int pos
= Combo()->FindString(m_startValue
);
1615 if (pos
== wxNOT_FOUND
)
1617 Combo()->SetSelection(pos
);
1621 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1631 wxStringTokenizer
tk(params
, _T(','));
1632 while ( tk
.HasMoreTokens() )
1634 m_choices
.Add(tk
.GetNextToken());
1638 // return the value in the text control
1639 wxString
wxGridCellChoiceEditor::GetValue() const
1641 return Combo()->GetValue();
1644 #endif // wxUSE_COMBOBOX
1646 // ----------------------------------------------------------------------------
1647 // wxGridCellEditorEvtHandler
1648 // ----------------------------------------------------------------------------
1650 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1652 // Don't disable the cell if we're just starting to edit it
1657 m_grid
->DisableCellEditControl();
1662 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1664 switch ( event
.GetKeyCode() )
1668 m_grid
->DisableCellEditControl();
1672 m_grid
->GetEventHandler()->ProcessEvent( event
);
1676 case WXK_NUMPAD_ENTER
:
1677 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1678 m_editor
->HandleReturn(event
);
1687 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1689 int row
= m_grid
->GetGridCursorRow();
1690 int col
= m_grid
->GetGridCursorCol();
1691 wxRect rect
= m_grid
->CellToRect( row
, col
);
1693 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1695 // if cell width is smaller than grid client area, cell is wholly visible
1696 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1698 switch ( event
.GetKeyCode() )
1703 case WXK_NUMPAD_ENTER
:
1708 if ( wholeCellVisible
)
1710 // no special processing needed...
1715 // do special processing for partly visible cell...
1717 // get the widths of all cells previous to this one
1719 for ( int i
= 0; i
< col
; i
++ )
1721 colXPos
+= m_grid
->GetColSize(i
);
1724 int xUnit
= 1, yUnit
= 1;
1725 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1728 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1732 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1740 if ( wholeCellVisible
)
1742 // no special processing needed...
1747 // do special processing for partly visible cell...
1750 wxString value
= m_grid
->GetCellValue(row
, col
);
1751 if ( wxEmptyString
!= value
)
1753 // get width of cell CONTENTS (text)
1755 wxFont font
= m_grid
->GetCellFont(row
, col
);
1756 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1758 // try to RIGHT align the text by scrolling
1759 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1761 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1762 // otherwise the last part of the cell content might be hidden below the scroll bar
1763 // FIXME: maybe there is a more suitable correction?
1764 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1765 if ( textWidth
< 0 )
1771 // get the widths of all cells previous to this one
1773 for ( int i
= 0; i
< col
; i
++ )
1775 colXPos
+= m_grid
->GetColSize(i
);
1778 // and add the (modified) text width of the cell contents
1779 // as we'd like to see the last part of the cell contents
1780 colXPos
+= textWidth
;
1782 int xUnit
= 1, yUnit
= 1;
1783 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1784 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1795 // ----------------------------------------------------------------------------
1796 // wxGridCellWorker is an (almost) empty common base class for
1797 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1798 // ----------------------------------------------------------------------------
1800 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1805 wxGridCellWorker::~wxGridCellWorker()
1809 // ============================================================================
1811 // ============================================================================
1813 // ----------------------------------------------------------------------------
1814 // wxGridCellRenderer
1815 // ----------------------------------------------------------------------------
1817 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1818 wxGridCellAttr
& attr
,
1821 int WXUNUSED(row
), int WXUNUSED(col
),
1824 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
1827 if ( grid
.IsEnabled() )
1831 if ( grid
.HasFocus() )
1832 clr
= grid
.GetSelectionBackground();
1834 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1838 clr
= attr
.GetBackgroundColour();
1841 else // grey out fields if the grid is disabled
1843 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
1847 dc
.SetPen( *wxTRANSPARENT_PEN
);
1848 dc
.DrawRectangle(rect
);
1851 // ----------------------------------------------------------------------------
1852 // wxGridCellStringRenderer
1853 // ----------------------------------------------------------------------------
1855 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1856 const wxGridCellAttr
& attr
,
1860 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
1862 // TODO some special colours for attr.IsReadOnly() case?
1864 // different coloured text when the grid is disabled
1865 if ( grid
.IsEnabled() )
1870 if ( grid
.HasFocus() )
1871 clr
= grid
.GetSelectionBackground();
1873 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1874 dc
.SetTextBackground( clr
);
1875 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1879 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1880 dc
.SetTextForeground( attr
.GetTextColour() );
1885 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1886 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1889 dc
.SetFont( attr
.GetFont() );
1892 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1894 const wxString
& text
)
1896 wxCoord x
= 0, y
= 0, max_x
= 0;
1897 dc
.SetFont(attr
.GetFont());
1898 wxStringTokenizer
tk(text
, _T('\n'));
1899 while ( tk
.HasMoreTokens() )
1901 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1902 max_x
= wxMax(max_x
, x
);
1905 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1907 return wxSize(max_x
, y
);
1910 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1911 wxGridCellAttr
& attr
,
1915 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1918 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1919 wxGridCellAttr
& attr
,
1921 const wxRect
& rectCell
,
1925 wxRect rect
= rectCell
;
1928 // erase only this cells background, overflow cells should have been erased
1929 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1932 attr
.GetAlignment(&hAlign
, &vAlign
);
1934 int overflowCols
= 0;
1936 if (attr
.GetOverflow())
1938 int cols
= grid
.GetNumberCols();
1939 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1940 int cell_rows
, cell_cols
;
1941 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1942 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1944 int i
, c_cols
, c_rows
;
1945 for (i
= col
+cell_cols
; i
< cols
; i
++)
1947 bool is_empty
= true;
1948 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1950 // check w/ anchor cell for multicell block
1951 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1954 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1963 rect
.width
+= grid
.GetColSize(i
);
1971 if (rect
.width
>= best_width
)
1975 overflowCols
= i
- col
- cell_cols
+ 1;
1976 if (overflowCols
>= cols
)
1977 overflowCols
= cols
- 1;
1980 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1982 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1984 clip
.x
+= rectCell
.width
;
1985 // draw each overflow cell individually
1986 int col_end
= col
+ cell_cols
+ overflowCols
;
1987 if (col_end
>= grid
.GetNumberCols())
1988 col_end
= grid
.GetNumberCols() - 1;
1989 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1991 clip
.width
= grid
.GetColSize(i
) - 1;
1992 dc
.DestroyClippingRegion();
1993 dc
.SetClippingRegion(clip
);
1995 SetTextColoursAndFont(grid
, attr
, dc
,
1996 grid
.IsInSelection(row
,i
));
1998 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1999 rect
, hAlign
, vAlign
);
2000 clip
.x
+= grid
.GetColSize(i
) - 1;
2006 dc
.DestroyClippingRegion();
2010 // now we only have to draw the text
2011 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2013 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2014 rect
, hAlign
, vAlign
);
2017 // ----------------------------------------------------------------------------
2018 // wxGridCellNumberRenderer
2019 // ----------------------------------------------------------------------------
2021 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2023 wxGridTableBase
*table
= grid
.GetTable();
2025 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2027 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2031 text
= table
->GetValue(row
, col
);
2037 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2038 wxGridCellAttr
& attr
,
2040 const wxRect
& rectCell
,
2044 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2046 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2048 // draw the text right aligned by default
2050 attr
.GetAlignment(&hAlign
, &vAlign
);
2051 hAlign
= wxALIGN_RIGHT
;
2053 wxRect rect
= rectCell
;
2056 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2059 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2060 wxGridCellAttr
& attr
,
2064 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2067 // ----------------------------------------------------------------------------
2068 // wxGridCellFloatRenderer
2069 // ----------------------------------------------------------------------------
2071 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2074 SetPrecision(precision
);
2077 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2079 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2080 renderer
->m_width
= m_width
;
2081 renderer
->m_precision
= m_precision
;
2082 renderer
->m_format
= m_format
;
2087 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2089 wxGridTableBase
*table
= grid
.GetTable();
2094 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2096 val
= table
->GetValueAsDouble(row
, col
);
2101 text
= table
->GetValue(row
, col
);
2102 hasDouble
= text
.ToDouble(&val
);
2109 if ( m_width
== -1 )
2111 if ( m_precision
== -1 )
2113 // default width/precision
2114 m_format
= _T("%f");
2118 m_format
.Printf(_T("%%.%df"), m_precision
);
2121 else if ( m_precision
== -1 )
2123 // default precision
2124 m_format
.Printf(_T("%%%d.f"), m_width
);
2128 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2132 text
.Printf(m_format
, val
);
2135 //else: text already contains the string
2140 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2141 wxGridCellAttr
& attr
,
2143 const wxRect
& rectCell
,
2147 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2149 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2151 // draw the text right aligned by default
2153 attr
.GetAlignment(&hAlign
, &vAlign
);
2154 hAlign
= wxALIGN_RIGHT
;
2156 wxRect rect
= rectCell
;
2159 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2162 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2163 wxGridCellAttr
& attr
,
2167 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2170 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2174 // reset to defaults
2180 wxString tmp
= params
.BeforeFirst(_T(','));
2184 if ( tmp
.ToLong(&width
) )
2186 SetWidth((int)width
);
2190 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2194 tmp
= params
.AfterFirst(_T(','));
2198 if ( tmp
.ToLong(&precision
) )
2200 SetPrecision((int)precision
);
2204 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2210 // ----------------------------------------------------------------------------
2211 // wxGridCellBoolRenderer
2212 // ----------------------------------------------------------------------------
2214 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2216 // FIXME these checkbox size calculations are really ugly...
2218 // between checkmark and box
2219 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2221 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2222 wxGridCellAttr
& WXUNUSED(attr
),
2227 // compute it only once (no locks for MT safeness in GUI thread...)
2228 if ( !ms_sizeCheckMark
.x
)
2230 // get checkbox size
2231 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2232 wxSize size
= checkbox
->GetBestSize();
2233 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2235 #if defined(__WXMOTIF__)
2236 checkSize
-= size
.y
/ 2;
2241 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2244 return ms_sizeCheckMark
;
2247 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2248 wxGridCellAttr
& attr
,
2254 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2256 // draw a check mark in the centre (ignoring alignment - TODO)
2257 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2259 // don't draw outside the cell
2260 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2261 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2263 // and even leave (at least) 1 pixel margin
2264 size
.x
= size
.y
= minSize
;
2267 // draw a border around checkmark
2269 attr
.GetAlignment(&hAlign
, &vAlign
);
2272 if (hAlign
== wxALIGN_CENTRE
)
2274 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2275 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2276 rectBorder
.width
= size
.x
;
2277 rectBorder
.height
= size
.y
;
2279 else if (hAlign
== wxALIGN_LEFT
)
2281 rectBorder
.x
= rect
.x
+ 2;
2282 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2283 rectBorder
.width
= size
.x
;
2284 rectBorder
.height
= size
.y
;
2286 else if (hAlign
== wxALIGN_RIGHT
)
2288 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2289 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2290 rectBorder
.width
= size
.x
;
2291 rectBorder
.height
= size
.y
;
2295 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2297 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2301 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2302 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2307 flags
|= wxCONTROL_CHECKED
;
2309 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2312 // ----------------------------------------------------------------------------
2314 // ----------------------------------------------------------------------------
2316 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2320 m_isReadOnly
= Unset
;
2325 m_attrkind
= wxGridCellAttr::Cell
;
2327 m_sizeRows
= m_sizeCols
= 1;
2328 m_overflow
= UnsetOverflow
;
2330 SetDefAttr(attrDefault
);
2333 wxGridCellAttr
*wxGridCellAttr::Clone() const
2335 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2337 if ( HasTextColour() )
2338 attr
->SetTextColour(GetTextColour());
2339 if ( HasBackgroundColour() )
2340 attr
->SetBackgroundColour(GetBackgroundColour());
2342 attr
->SetFont(GetFont());
2343 if ( HasAlignment() )
2344 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2346 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2350 attr
->SetRenderer(m_renderer
);
2351 m_renderer
->IncRef();
2355 attr
->SetEditor(m_editor
);
2360 attr
->SetReadOnly();
2362 attr
->SetOverflow( m_overflow
== Overflow
);
2363 attr
->SetKind( m_attrkind
);
2368 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2370 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2371 SetTextColour(mergefrom
->GetTextColour());
2372 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2373 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2374 if ( !HasFont() && mergefrom
->HasFont() )
2375 SetFont(mergefrom
->GetFont());
2376 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2379 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2380 SetAlignment(hAlign
, vAlign
);
2382 if ( !HasSize() && mergefrom
->HasSize() )
2383 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2385 // Directly access member functions as GetRender/Editor don't just return
2386 // m_renderer/m_editor
2388 // Maybe add support for merge of Render and Editor?
2389 if (!HasRenderer() && mergefrom
->HasRenderer() )
2391 m_renderer
= mergefrom
->m_renderer
;
2392 m_renderer
->IncRef();
2394 if ( !HasEditor() && mergefrom
->HasEditor() )
2396 m_editor
= mergefrom
->m_editor
;
2399 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2400 SetReadOnly(mergefrom
->IsReadOnly());
2402 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2403 SetOverflow(mergefrom
->GetOverflow());
2405 SetDefAttr(mergefrom
->m_defGridAttr
);
2408 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2410 // The size of a cell is normally 1,1
2412 // If this cell is larger (2,2) then this is the top left cell
2413 // the other cells that will be covered (lower right cells) must be
2414 // set to negative or zero values such that
2415 // row + num_rows of the covered cell points to the larger cell (this cell)
2416 // same goes for the col + num_cols.
2418 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2420 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2421 !((num_rows
<= 0) && (num_cols
> 0)) ||
2422 !((num_rows
== 0) && (num_cols
== 0))),
2423 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2425 m_sizeRows
= num_rows
;
2426 m_sizeCols
= num_cols
;
2429 const wxColour
& wxGridCellAttr::GetTextColour() const
2431 if (HasTextColour())
2435 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2437 return m_defGridAttr
->GetTextColour();
2441 wxFAIL_MSG(wxT("Missing default cell attribute"));
2442 return wxNullColour
;
2446 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2448 if (HasBackgroundColour())
2452 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2454 return m_defGridAttr
->GetBackgroundColour();
2458 wxFAIL_MSG(wxT("Missing default cell attribute"));
2459 return wxNullColour
;
2463 const wxFont
& wxGridCellAttr::GetFont() const
2469 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2471 return m_defGridAttr
->GetFont();
2475 wxFAIL_MSG(wxT("Missing default cell attribute"));
2480 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2489 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2491 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2495 wxFAIL_MSG(wxT("Missing default cell attribute"));
2499 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2502 *num_rows
= m_sizeRows
;
2504 *num_cols
= m_sizeCols
;
2507 // GetRenderer and GetEditor use a slightly different decision path about
2508 // which attribute to use. If a non-default attr object has one then it is
2509 // used, otherwise the default editor or renderer is fetched from the grid and
2510 // used. It should be the default for the data type of the cell. If it is
2511 // NULL (because the table has a type that the grid does not have in its
2512 // registry), then the grid's default editor or renderer is used.
2514 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2516 wxGridCellRenderer
*renderer
= NULL
;
2518 if ( m_renderer
&& this != m_defGridAttr
)
2520 // use the cells renderer if it has one
2521 renderer
= m_renderer
;
2524 else // no non-default cell renderer
2526 // get default renderer for the data type
2529 // GetDefaultRendererForCell() will do IncRef() for us
2530 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2533 if ( renderer
== NULL
)
2535 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2537 // if we still don't have one then use the grid default
2538 // (no need for IncRef() here neither)
2539 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2541 else // default grid attr
2543 // use m_renderer which we had decided not to use initially
2544 renderer
= m_renderer
;
2551 // we're supposed to always find something
2552 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2557 // same as above, except for s/renderer/editor/g
2558 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2560 wxGridCellEditor
*editor
= NULL
;
2562 if ( m_editor
&& this != m_defGridAttr
)
2564 // use the cells editor if it has one
2568 else // no non default cell editor
2570 // get default editor for the data type
2573 // GetDefaultEditorForCell() will do IncRef() for us
2574 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2577 if ( editor
== NULL
)
2579 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2581 // if we still don't have one then use the grid default
2582 // (no need for IncRef() here neither)
2583 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2585 else // default grid attr
2587 // use m_editor which we had decided not to use initially
2595 // we're supposed to always find something
2596 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2601 // ----------------------------------------------------------------------------
2602 // wxGridCellAttrData
2603 // ----------------------------------------------------------------------------
2605 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2607 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
2608 // touch attribute's reference counting explicitly, since this
2609 // is managed by class wxGridCellWithAttr
2610 int n
= FindIndex(row
, col
);
2611 if ( n
== wxNOT_FOUND
)
2615 // add the attribute
2616 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2618 //else: nothing to do
2620 else // we already have an attribute for this cell
2624 // change the attribute
2625 m_attrs
[(size_t)n
].ChangeAttr(attr
);
2629 // remove this attribute
2630 m_attrs
.RemoveAt((size_t)n
);
2635 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2637 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2639 int n
= FindIndex(row
, col
);
2640 if ( n
!= wxNOT_FOUND
)
2642 attr
= m_attrs
[(size_t)n
].attr
;
2649 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2651 size_t count
= m_attrs
.GetCount();
2652 for ( size_t n
= 0; n
< count
; n
++ )
2654 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2655 wxCoord row
= coords
.GetRow();
2656 if ((size_t)row
>= pos
)
2660 // If rows inserted, include row counter where necessary
2661 coords
.SetRow(row
+ numRows
);
2663 else if (numRows
< 0)
2665 // If rows deleted ...
2666 if ((size_t)row
>= pos
- numRows
)
2668 // ...either decrement row counter (if row still exists)...
2669 coords
.SetRow(row
+ numRows
);
2673 // ...or remove the attribute
2674 m_attrs
.RemoveAt(n
);
2683 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2685 size_t count
= m_attrs
.GetCount();
2686 for ( size_t n
= 0; n
< count
; n
++ )
2688 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2689 wxCoord col
= coords
.GetCol();
2690 if ( (size_t)col
>= pos
)
2694 // If rows inserted, include row counter where necessary
2695 coords
.SetCol(col
+ numCols
);
2697 else if (numCols
< 0)
2699 // If rows deleted ...
2700 if ((size_t)col
>= pos
- numCols
)
2702 // ...either decrement row counter (if row still exists)...
2703 coords
.SetCol(col
+ numCols
);
2707 // ...or remove the attribute
2708 m_attrs
.RemoveAt(n
);
2717 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2719 size_t count
= m_attrs
.GetCount();
2720 for ( size_t n
= 0; n
< count
; n
++ )
2722 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2723 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2732 // ----------------------------------------------------------------------------
2733 // wxGridRowOrColAttrData
2734 // ----------------------------------------------------------------------------
2736 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2738 size_t count
= m_attrs
.GetCount();
2739 for ( size_t n
= 0; n
< count
; n
++ )
2741 m_attrs
[n
]->DecRef();
2745 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2747 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2749 int n
= m_rowsOrCols
.Index(rowOrCol
);
2750 if ( n
!= wxNOT_FOUND
)
2752 attr
= m_attrs
[(size_t)n
];
2759 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2761 int i
= m_rowsOrCols
.Index(rowOrCol
);
2762 if ( i
== wxNOT_FOUND
)
2766 // add the attribute - no need to do anything to reference count
2767 // since we take ownership of the attribute.
2768 m_rowsOrCols
.Add(rowOrCol
);
2771 // nothing to remove
2775 size_t n
= (size_t)i
;
2776 if ( m_attrs
[n
] == attr
)
2781 // change the attribute, handling reference count manually,
2782 // taking ownership of the new attribute.
2783 m_attrs
[n
]->DecRef();
2788 // remove this attribute, handling reference count manually
2789 m_attrs
[n
]->DecRef();
2790 m_rowsOrCols
.RemoveAt(n
);
2791 m_attrs
.RemoveAt(n
);
2796 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2798 size_t count
= m_attrs
.GetCount();
2799 for ( size_t n
= 0; n
< count
; n
++ )
2801 int & rowOrCol
= m_rowsOrCols
[n
];
2802 if ( (size_t)rowOrCol
>= pos
)
2804 if ( numRowsOrCols
> 0 )
2806 // If rows inserted, include row counter where necessary
2807 rowOrCol
+= numRowsOrCols
;
2809 else if ( numRowsOrCols
< 0)
2811 // If rows deleted, either decrement row counter (if row still exists)
2812 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2813 rowOrCol
+= numRowsOrCols
;
2816 m_rowsOrCols
.RemoveAt(n
);
2817 m_attrs
[n
]->DecRef();
2818 m_attrs
.RemoveAt(n
);
2827 // ----------------------------------------------------------------------------
2828 // wxGridCellAttrProvider
2829 // ----------------------------------------------------------------------------
2831 wxGridCellAttrProvider::wxGridCellAttrProvider()
2833 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2836 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2841 void wxGridCellAttrProvider::InitData()
2843 m_data
= new wxGridCellAttrProviderData
;
2846 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2847 wxGridCellAttr::wxAttrKind kind
) const
2849 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2854 case (wxGridCellAttr::Any
):
2855 // Get cached merge attributes.
2856 // Currently not used as no cache implemented as not mutable
2857 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2860 // Basically implement old version.
2861 // Also check merge cache, so we don't have to re-merge every time..
2862 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2863 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2864 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2866 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2868 // Two or more are non NULL
2869 attr
= new wxGridCellAttr
;
2870 attr
->SetKind(wxGridCellAttr::Merged
);
2872 // Order is important..
2875 attr
->MergeWith(attrcell
);
2880 attr
->MergeWith(attrcol
);
2885 attr
->MergeWith(attrrow
);
2889 // store merge attr if cache implemented
2891 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2895 // one or none is non null return it or null.
2914 case (wxGridCellAttr::Cell
):
2915 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2918 case (wxGridCellAttr::Col
):
2919 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2922 case (wxGridCellAttr::Row
):
2923 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2928 // (wxGridCellAttr::Default):
2929 // (wxGridCellAttr::Merged):
2937 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2943 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2946 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2951 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2954 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2959 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2962 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2966 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2968 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2972 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2976 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2978 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2982 // ----------------------------------------------------------------------------
2983 // wxGridTypeRegistry
2984 // ----------------------------------------------------------------------------
2986 wxGridTypeRegistry::~wxGridTypeRegistry()
2988 size_t count
= m_typeinfo
.GetCount();
2989 for ( size_t i
= 0; i
< count
; i
++ )
2990 delete m_typeinfo
[i
];
2993 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2994 wxGridCellRenderer
* renderer
,
2995 wxGridCellEditor
* editor
)
2997 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2999 // is it already registered?
3000 int loc
= FindRegisteredDataType(typeName
);
3001 if ( loc
!= wxNOT_FOUND
)
3003 delete m_typeinfo
[loc
];
3004 m_typeinfo
[loc
] = info
;
3008 m_typeinfo
.Add(info
);
3012 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3014 size_t count
= m_typeinfo
.GetCount();
3015 for ( size_t i
= 0; i
< count
; i
++ )
3017 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3026 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3028 int index
= FindRegisteredDataType(typeName
);
3029 if ( index
== wxNOT_FOUND
)
3031 // check whether this is one of the standard ones, in which case
3032 // register it "on the fly"
3034 if ( typeName
== wxGRID_VALUE_STRING
)
3036 RegisterDataType(wxGRID_VALUE_STRING
,
3037 new wxGridCellStringRenderer
,
3038 new wxGridCellTextEditor
);
3041 #endif // wxUSE_TEXTCTRL
3043 if ( typeName
== wxGRID_VALUE_BOOL
)
3045 RegisterDataType(wxGRID_VALUE_BOOL
,
3046 new wxGridCellBoolRenderer
,
3047 new wxGridCellBoolEditor
);
3050 #endif // wxUSE_CHECKBOX
3052 if ( typeName
== wxGRID_VALUE_NUMBER
)
3054 RegisterDataType(wxGRID_VALUE_NUMBER
,
3055 new wxGridCellNumberRenderer
,
3056 new wxGridCellNumberEditor
);
3058 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3060 RegisterDataType(wxGRID_VALUE_FLOAT
,
3061 new wxGridCellFloatRenderer
,
3062 new wxGridCellFloatEditor
);
3065 #endif // wxUSE_TEXTCTRL
3067 if ( typeName
== wxGRID_VALUE_CHOICE
)
3069 RegisterDataType(wxGRID_VALUE_CHOICE
,
3070 new wxGridCellStringRenderer
,
3071 new wxGridCellChoiceEditor
);
3074 #endif // wxUSE_COMBOBOX
3079 // we get here only if just added the entry for this type, so return
3081 index
= m_typeinfo
.GetCount() - 1;
3087 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3089 int index
= FindDataType(typeName
);
3090 if ( index
== wxNOT_FOUND
)
3092 // the first part of the typename is the "real" type, anything after ':'
3093 // are the parameters for the renderer
3094 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3095 if ( index
== wxNOT_FOUND
)
3100 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3101 wxGridCellRenderer
*rendererOld
= renderer
;
3102 renderer
= renderer
->Clone();
3103 rendererOld
->DecRef();
3105 wxGridCellEditor
*editor
= GetEditor(index
);
3106 wxGridCellEditor
*editorOld
= editor
;
3107 editor
= editor
->Clone();
3108 editorOld
->DecRef();
3110 // do it even if there are no parameters to reset them to defaults
3111 wxString params
= typeName
.AfterFirst(_T(':'));
3112 renderer
->SetParameters(params
);
3113 editor
->SetParameters(params
);
3115 // register the new typename
3116 RegisterDataType(typeName
, renderer
, editor
);
3118 // we just registered it, it's the last one
3119 index
= m_typeinfo
.GetCount() - 1;
3125 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3127 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3134 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3136 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3143 // ----------------------------------------------------------------------------
3145 // ----------------------------------------------------------------------------
3147 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3149 wxGridTableBase::wxGridTableBase()
3151 m_view
= (wxGrid
*) NULL
;
3152 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3155 wxGridTableBase::~wxGridTableBase()
3157 delete m_attrProvider
;
3160 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3162 delete m_attrProvider
;
3163 m_attrProvider
= attrProvider
;
3166 bool wxGridTableBase::CanHaveAttributes()
3168 if ( ! GetAttrProvider() )
3170 // use the default attr provider by default
3171 SetAttrProvider(new wxGridCellAttrProvider
);
3177 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3179 if ( m_attrProvider
)
3180 return m_attrProvider
->GetAttr(row
, col
, kind
);
3182 return (wxGridCellAttr
*)NULL
;
3185 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3187 if ( m_attrProvider
)
3190 attr
->SetKind(wxGridCellAttr::Cell
);
3191 m_attrProvider
->SetAttr(attr
, row
, col
);
3195 // as we take ownership of the pointer and don't store it, we must
3201 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3203 if ( m_attrProvider
)
3205 attr
->SetKind(wxGridCellAttr::Row
);
3206 m_attrProvider
->SetRowAttr(attr
, row
);
3210 // as we take ownership of the pointer and don't store it, we must
3216 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3218 if ( m_attrProvider
)
3220 attr
->SetKind(wxGridCellAttr::Col
);
3221 m_attrProvider
->SetColAttr(attr
, col
);
3225 // as we take ownership of the pointer and don't store it, we must
3231 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3232 size_t WXUNUSED(numRows
) )
3234 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3239 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3241 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3246 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3247 size_t WXUNUSED(numRows
) )
3249 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3254 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3255 size_t WXUNUSED(numCols
) )
3257 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3262 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3264 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3269 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3270 size_t WXUNUSED(numCols
) )
3272 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3277 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3281 // RD: Starting the rows at zero confuses users,
3282 // no matter how much it makes sense to us geeks.
3288 wxString
wxGridTableBase::GetColLabelValue( int col
)
3290 // default col labels are:
3291 // cols 0 to 25 : A-Z
3292 // cols 26 to 675 : AA-ZZ
3297 for ( n
= 1; ; n
++ )
3299 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3305 // reverse the string...
3307 for ( i
= 0; i
< n
; i
++ )
3315 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3317 return wxGRID_VALUE_STRING
;
3320 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3321 const wxString
& typeName
)
3323 return typeName
== wxGRID_VALUE_STRING
;
3326 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3328 return CanGetValueAs(row
, col
, typeName
);
3331 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3336 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3341 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3346 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3347 long WXUNUSED(value
) )
3351 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3352 double WXUNUSED(value
) )
3356 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3357 bool WXUNUSED(value
) )
3361 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3362 const wxString
& WXUNUSED(typeName
) )
3367 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3368 const wxString
& WXUNUSED(typeName
),
3369 void* WXUNUSED(value
) )
3373 //////////////////////////////////////////////////////////////////////
3375 // Message class for the grid table to send requests and notifications
3379 wxGridTableMessage::wxGridTableMessage()
3381 m_table
= (wxGridTableBase
*) NULL
;
3387 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3388 int commandInt1
, int commandInt2
)
3392 m_comInt1
= commandInt1
;
3393 m_comInt2
= commandInt2
;
3396 //////////////////////////////////////////////////////////////////////
3398 // A basic grid table for string data. An object of this class will
3399 // created by wxGrid if you don't specify an alternative table class.
3402 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3404 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3406 wxGridStringTable::wxGridStringTable()
3411 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3414 m_data
.Alloc( numRows
);
3417 sa
.Alloc( numCols
);
3418 sa
.Add( wxEmptyString
, numCols
);
3420 m_data
.Add( sa
, numRows
);
3423 wxGridStringTable::~wxGridStringTable()
3427 int wxGridStringTable::GetNumberRows()
3429 return m_data
.GetCount();
3432 int wxGridStringTable::GetNumberCols()
3434 if ( m_data
.GetCount() > 0 )
3435 return m_data
[0].GetCount();
3440 wxString
wxGridStringTable::GetValue( int row
, int col
)
3442 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3444 _T("invalid row or column index in wxGridStringTable") );
3446 return m_data
[row
][col
];
3449 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3451 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3452 _T("invalid row or column index in wxGridStringTable") );
3454 m_data
[row
][col
] = value
;
3457 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3459 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3461 _T("invalid row or column index in wxGridStringTable") );
3463 return (m_data
[row
][col
] == wxEmptyString
);
3466 void wxGridStringTable::Clear()
3469 int numRows
, numCols
;
3471 numRows
= m_data
.GetCount();
3474 numCols
= m_data
[0].GetCount();
3476 for ( row
= 0; row
< numRows
; row
++ )
3478 for ( col
= 0; col
< numCols
; col
++ )
3480 m_data
[row
][col
] = wxEmptyString
;
3486 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3488 size_t curNumRows
= m_data
.GetCount();
3489 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3490 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3492 if ( pos
>= curNumRows
)
3494 return AppendRows( numRows
);
3498 sa
.Alloc( curNumCols
);
3499 sa
.Add( wxEmptyString
, curNumCols
);
3500 m_data
.Insert( sa
, pos
, numRows
);
3504 wxGridTableMessage
msg( this,
3505 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3509 GetView()->ProcessTableMessage( msg
);
3515 bool wxGridStringTable::AppendRows( size_t numRows
)
3517 size_t curNumRows
= m_data
.GetCount();
3518 size_t curNumCols
= ( curNumRows
> 0
3519 ? m_data
[0].GetCount()
3520 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3523 if ( curNumCols
> 0 )
3525 sa
.Alloc( curNumCols
);
3526 sa
.Add( wxEmptyString
, curNumCols
);
3529 m_data
.Add( sa
, numRows
);
3533 wxGridTableMessage
msg( this,
3534 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3537 GetView()->ProcessTableMessage( msg
);
3543 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3545 size_t curNumRows
= m_data
.GetCount();
3547 if ( pos
>= curNumRows
)
3549 wxFAIL_MSG( wxString::Format
3551 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3553 (unsigned long)numRows
,
3554 (unsigned long)curNumRows
3560 if ( numRows
> curNumRows
- pos
)
3562 numRows
= curNumRows
- pos
;
3565 if ( numRows
>= curNumRows
)
3571 m_data
.RemoveAt( pos
, numRows
);
3576 wxGridTableMessage
msg( this,
3577 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3581 GetView()->ProcessTableMessage( msg
);
3587 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3591 size_t curNumRows
= m_data
.GetCount();
3592 size_t curNumCols
= ( curNumRows
> 0
3593 ? m_data
[0].GetCount()
3594 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3596 if ( pos
>= curNumCols
)
3598 return AppendCols( numCols
);
3601 if ( !m_colLabels
.IsEmpty() )
3603 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3606 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3607 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3610 for ( row
= 0; row
< curNumRows
; row
++ )
3612 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3614 m_data
[row
].Insert( wxEmptyString
, col
);
3620 wxGridTableMessage
msg( this,
3621 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3625 GetView()->ProcessTableMessage( msg
);
3631 bool wxGridStringTable::AppendCols( size_t numCols
)
3635 size_t curNumRows
= m_data
.GetCount();
3637 for ( row
= 0; row
< curNumRows
; row
++ )
3639 m_data
[row
].Add( wxEmptyString
, numCols
);
3644 wxGridTableMessage
msg( this,
3645 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3648 GetView()->ProcessTableMessage( msg
);
3654 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3658 size_t curNumRows
= m_data
.GetCount();
3659 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3660 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3662 if ( pos
>= curNumCols
)
3664 wxFAIL_MSG( wxString::Format
3666 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3668 (unsigned long)numCols
,
3669 (unsigned long)curNumCols
3676 colID
= GetView()->GetColAt( pos
);
3680 if ( numCols
> curNumCols
- colID
)
3682 numCols
= curNumCols
- colID
;
3685 if ( !m_colLabels
.IsEmpty() )
3687 // m_colLabels stores just as many elements as it needs, e.g. if only
3688 // the label of the first column had been set it would have only one
3689 // element and not numCols, so account for it
3690 int nToRm
= m_colLabels
.size() - colID
;
3692 m_colLabels
.RemoveAt( colID
, nToRm
);
3695 for ( row
= 0; row
< curNumRows
; row
++ )
3697 if ( numCols
>= curNumCols
)
3699 m_data
[row
].Clear();
3703 m_data
[row
].RemoveAt( colID
, numCols
);
3709 wxGridTableMessage
msg( this,
3710 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3714 GetView()->ProcessTableMessage( msg
);
3720 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3722 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3724 // using default label
3726 return wxGridTableBase::GetRowLabelValue( row
);
3730 return m_rowLabels
[row
];
3734 wxString
wxGridStringTable::GetColLabelValue( int col
)
3736 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3738 // using default label
3740 return wxGridTableBase::GetColLabelValue( col
);
3744 return m_colLabels
[col
];
3748 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3750 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3752 int n
= m_rowLabels
.GetCount();
3755 for ( i
= n
; i
<= row
; i
++ )
3757 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3761 m_rowLabels
[row
] = value
;
3764 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3766 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3768 int n
= m_colLabels
.GetCount();
3771 for ( i
= n
; i
<= col
; i
++ )
3773 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3777 m_colLabels
[col
] = value
;
3781 //////////////////////////////////////////////////////////////////////
3782 //////////////////////////////////////////////////////////////////////
3784 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3785 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3788 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3790 m_owner
->CancelMouseCapture();
3793 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3795 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3796 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3797 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3798 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3801 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3803 const wxPoint
&pos
, const wxSize
&size
)
3804 : wxGridSubwindow(parent
, id
, pos
, size
)
3809 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3813 // NO - don't do this because it will set both the x and y origin
3814 // coords to match the parent scrolled window and we just want to
3815 // set the y coord - MB
3817 // m_owner->PrepareDC( dc );
3820 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3821 wxPoint pt
= dc
.GetDeviceOrigin();
3822 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3824 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3825 m_owner
->DrawRowLabels( dc
, rows
);
3828 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3830 m_owner
->ProcessRowLabelMouseEvent( event
);
3833 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3835 m_owner
->GetEventHandler()->ProcessEvent( event
);
3838 //////////////////////////////////////////////////////////////////////
3840 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3842 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3843 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3844 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3845 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3848 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3850 const wxPoint
&pos
, const wxSize
&size
)
3851 : wxGridSubwindow(parent
, id
, pos
, size
)
3856 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3860 // NO - don't do this because it will set both the x and y origin
3861 // coords to match the parent scrolled window and we just want to
3862 // set the x coord - MB
3864 // m_owner->PrepareDC( dc );
3867 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3868 wxPoint pt
= dc
.GetDeviceOrigin();
3869 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3870 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3872 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3874 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3875 m_owner
->DrawColLabels( dc
, cols
);
3878 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3880 m_owner
->ProcessColLabelMouseEvent( event
);
3883 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3885 m_owner
->GetEventHandler()->ProcessEvent( event
);
3888 //////////////////////////////////////////////////////////////////////
3890 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3892 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3893 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3894 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3895 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3898 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3901 const wxSize
& size
)
3902 : wxGridSubwindow(parent
, id
, pos
, size
)
3907 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3911 m_owner
->DrawCornerLabel(dc
);
3914 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3916 m_owner
->ProcessCornerLabelMouseEvent( event
);
3919 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3921 m_owner
->GetEventHandler()->ProcessEvent(event
);
3924 //////////////////////////////////////////////////////////////////////
3926 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3928 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
3929 EVT_PAINT( wxGridWindow::OnPaint
)
3930 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3931 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3932 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3933 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3934 EVT_CHAR( wxGridWindow::OnChar
)
3935 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3936 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3937 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3940 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3941 wxGridRowLabelWindow
*rowLblWin
,
3942 wxGridColLabelWindow
*colLblWin
,
3945 const wxSize
&size
)
3946 : wxGridSubwindow(parent
, id
, pos
, size
,
3947 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3948 wxT("grid window") )
3951 m_rowLabelWin
= rowLblWin
;
3952 m_colLabelWin
= colLblWin
;
3955 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3957 wxPaintDC
dc( this );
3958 m_owner
->PrepareDC( dc
);
3959 wxRegion reg
= GetUpdateRegion();
3960 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3961 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3963 m_owner
->DrawAllGridLines( dc
, reg
);
3965 m_owner
->DrawGridSpace( dc
);
3966 m_owner
->DrawHighlight( dc
, dirtyCells
);
3969 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3971 wxWindow::ScrollWindow( dx
, dy
, rect
);
3972 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3973 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3976 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3978 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3981 m_owner
->ProcessGridCellMouseEvent( event
);
3984 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3986 m_owner
->GetEventHandler()->ProcessEvent( event
);
3989 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3990 // cursor must be in the cell edit control to get key events
3992 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3994 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3998 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4000 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4004 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4006 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4010 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4014 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4016 // and if we have any selection, it has to be repainted, because it
4017 // uses different colour when the grid is not focused:
4018 if ( m_owner
->IsSelection() )
4024 // NB: Note that this code is in "else" branch only because the other
4025 // branch refreshes everything and so there's no point in calling
4026 // Refresh() again, *not* because it should only be done if
4027 // !IsSelection(). If the above code is ever optimized to refresh
4028 // only selected area, this needs to be moved out of the "else"
4029 // branch so that it's always executed.
4031 // current cell cursor {dis,re}appears on focus change:
4032 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4033 m_owner
->GetGridCursorCol());
4034 const wxRect cursor
=
4035 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4036 Refresh(true, &cursor
);
4039 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4043 //////////////////////////////////////////////////////////////////////
4045 // Internal Helper function for computing row or column from some
4046 // (unscrolled) coordinate value, using either
4047 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4048 // of m_rowBottoms/m_ColRights to speed up the search!
4050 // Internal helper macros for simpler use of that function
4052 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4053 const wxArrayInt
& BorderArray
, int nMax
,
4056 #define internalXToCol(x) XToCol(x, true)
4057 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4058 m_minAcceptableRowHeight, \
4059 m_rowBottoms, m_numRows, true)
4061 /////////////////////////////////////////////////////////////////////
4063 #if wxUSE_EXTENDED_RTTI
4064 WX_DEFINE_FLAGS( wxGridStyle
)
4066 wxBEGIN_FLAGS( wxGridStyle
)
4067 // new style border flags, we put them first to
4068 // use them for streaming out
4069 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4070 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4071 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4072 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4073 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4074 wxFLAGS_MEMBER(wxBORDER_NONE
)
4076 // old style border flags
4077 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4078 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4079 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4080 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4081 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4082 wxFLAGS_MEMBER(wxBORDER
)
4084 // standard window styles
4085 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4086 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4087 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4088 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4089 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4090 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4091 wxFLAGS_MEMBER(wxVSCROLL
)
4092 wxFLAGS_MEMBER(wxHSCROLL
)
4094 wxEND_FLAGS( wxGridStyle
)
4096 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4098 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4099 wxHIDE_PROPERTY( Children
)
4100 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4101 wxEND_PROPERTIES_TABLE()
4103 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4104 wxEND_HANDLERS_TABLE()
4106 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4109 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4112 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4115 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4116 EVT_PAINT( wxGrid::OnPaint
)
4117 EVT_SIZE( wxGrid::OnSize
)
4118 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4119 EVT_KEY_UP( wxGrid::OnKeyUp
)
4120 EVT_CHAR ( wxGrid::OnChar
)
4121 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4129 wxGrid::wxGrid( wxWindow
*parent
,
4134 const wxString
& name
)
4137 Create(parent
, id
, pos
, size
, style
, name
);
4140 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4141 const wxPoint
& pos
, const wxSize
& size
,
4142 long style
, const wxString
& name
)
4144 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4145 style
| wxWANTS_CHARS
, name
))
4148 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4149 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4152 SetInitialSize(size
);
4153 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
4161 // Must do this or ~wxScrollHelper will pop the wrong event handler
4162 SetTargetWindow(this);
4164 wxSafeDecRef(m_defaultCellAttr
);
4166 #ifdef DEBUG_ATTR_CACHE
4167 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4168 wxPrintf(_T("wxGrid attribute cache statistics: "
4169 "total: %u, hits: %u (%u%%)\n"),
4170 total
, gs_nAttrCacheHits
,
4171 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4174 // if we own the table, just delete it, otherwise at least don't leave it
4175 // with dangling view pointer
4178 else if ( m_table
&& m_table
->GetView() == this )
4179 m_table
->SetView(NULL
);
4181 delete m_typeRegistry
;
4186 // ----- internal init and update functions
4189 // NOTE: If using the default visual attributes works everywhere then this can
4190 // be removed as well as the #else cases below.
4191 #define _USE_VISATTR 0
4193 void wxGrid::Create()
4195 // create the type registry
4196 m_typeRegistry
= new wxGridTypeRegistry
;
4198 m_cellEditCtrlEnabled
= false;
4200 m_defaultCellAttr
= new wxGridCellAttr();
4202 // Set default cell attributes
4203 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4204 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4205 m_defaultCellAttr
->SetFont(GetFont());
4206 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4207 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4208 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4211 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4212 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4214 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4215 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4218 m_defaultCellAttr
->SetTextColour(
4219 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4220 m_defaultCellAttr
->SetBackgroundColour(
4221 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4226 m_currentCellCoords
= wxGridNoCellCoords
;
4228 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4229 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4231 // subwindow components that make up the wxGrid
4232 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4237 m_colLabelWin
= new wxGridColLabelWindow( this,
4242 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4247 m_gridWin
= new wxGridWindow( this,
4254 SetTargetWindow( m_gridWin
);
4257 wxColour gfg
= gva
.colFg
;
4258 wxColour gbg
= gva
.colBg
;
4259 wxColour lfg
= lva
.colFg
;
4260 wxColour lbg
= lva
.colBg
;
4262 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4263 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4264 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4265 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4268 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4269 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4270 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4271 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4272 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4273 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4275 m_gridWin
->SetOwnForegroundColour(gfg
);
4276 m_gridWin
->SetOwnBackgroundColour(gbg
);
4281 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4282 wxGrid::wxGridSelectionModes selmode
)
4284 wxCHECK_MSG( !m_created
,
4286 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4288 m_numRows
= numRows
;
4289 m_numCols
= numCols
;
4291 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4292 m_table
->SetView( this );
4294 m_selection
= new wxGridSelection( this, selmode
);
4303 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4305 wxCHECK_RET( m_created
,
4306 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4308 m_selection
->SetSelectionMode( selmode
);
4311 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4313 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4314 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4316 return m_selection
->GetSelectionMode();
4319 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4320 wxGrid::wxGridSelectionModes selmode
)
4322 bool checkSelection
= false;
4325 // stop all processing
4330 m_table
->SetView(0);
4342 checkSelection
= true;
4344 // kill row and column size arrays
4345 m_colWidths
.Empty();
4346 m_colRights
.Empty();
4347 m_rowHeights
.Empty();
4348 m_rowBottoms
.Empty();
4353 m_numRows
= table
->GetNumberRows();
4354 m_numCols
= table
->GetNumberCols();
4357 m_table
->SetView( this );
4358 m_ownTable
= takeOwnership
;
4359 m_selection
= new wxGridSelection( this, selmode
);
4362 // If the newly set table is smaller than the
4363 // original one current cell and selection regions
4364 // might be invalid,
4365 m_selectingKeyboard
= wxGridNoCellCoords
;
4366 m_currentCellCoords
=
4367 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4368 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4369 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4370 m_selectingTopLeft
.GetCol() >= m_numCols
)
4372 m_selectingTopLeft
= wxGridNoCellCoords
;
4373 m_selectingBottomRight
= wxGridNoCellCoords
;
4376 m_selectingBottomRight
=
4377 wxGridCellCoords(wxMin(m_numRows
,
4378 m_selectingBottomRight
.GetRow()),
4380 m_selectingBottomRight
.GetCol()));
4390 void wxGrid::InitVars()
4394 m_cornerLabelWin
= NULL
;
4395 m_rowLabelWin
= NULL
;
4396 m_colLabelWin
= NULL
;
4403 m_defaultCellAttr
= NULL
;
4404 m_typeRegistry
= NULL
;
4405 m_winCapture
= NULL
;
4410 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4411 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4413 if ( m_rowLabelWin
)
4415 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4419 m_labelBackgroundColour
= *wxWHITE
;
4422 m_labelTextColour
= *wxBLACK
;
4425 m_attrCache
.row
= -1;
4426 m_attrCache
.col
= -1;
4427 m_attrCache
.attr
= NULL
;
4429 m_labelFont
= GetFont();
4430 m_labelFont
.SetWeight( wxBOLD
);
4432 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4433 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4435 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4436 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4437 m_colLabelTextOrientation
= wxHORIZONTAL
;
4439 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4440 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4442 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4443 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4445 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4446 m_defaultRowHeight
+= 8;
4448 m_defaultRowHeight
+= 4;
4451 m_gridLineColour
= wxColour( 192,192,192 );
4452 m_gridLinesEnabled
= true;
4453 m_cellHighlightColour
= *wxBLACK
;
4454 m_cellHighlightPenWidth
= 2;
4455 m_cellHighlightROPenWidth
= 1;
4457 m_canDragColMove
= false;
4459 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4460 m_winCapture
= (wxWindow
*)NULL
;
4461 m_canDragRowSize
= true;
4462 m_canDragColSize
= true;
4463 m_canDragGridSize
= true;
4464 m_canDragCell
= false;
4466 m_dragRowOrCol
= -1;
4467 m_isDragging
= false;
4468 m_startDragPos
= wxDefaultPosition
;
4469 m_nativeColumnLabels
= false;
4471 m_waitForSlowClick
= false;
4473 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4474 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4476 m_currentCellCoords
= wxGridNoCellCoords
;
4480 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4481 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4483 m_editable
= true; // default for whole grid
4485 m_inOnKeyDown
= false;
4491 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4492 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4495 // ----------------------------------------------------------------------------
4496 // the idea is to call these functions only when necessary because they create
4497 // quite big arrays which eat memory mostly unnecessary - in particular, if
4498 // default widths/heights are used for all rows/columns, we may not use these
4501 // with some extra code, it should be possible to only store the widths/heights
4502 // different from default ones (resulting in space savings for huge grids) but
4503 // this is not done currently
4504 // ----------------------------------------------------------------------------
4506 void wxGrid::InitRowHeights()
4508 m_rowHeights
.Empty();
4509 m_rowBottoms
.Empty();
4511 m_rowHeights
.Alloc( m_numRows
);
4512 m_rowBottoms
.Alloc( m_numRows
);
4514 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4517 for ( int i
= 0; i
< m_numRows
; i
++ )
4519 rowBottom
+= m_defaultRowHeight
;
4520 m_rowBottoms
.Add( rowBottom
);
4524 void wxGrid::InitColWidths()
4526 m_colWidths
.Empty();
4527 m_colRights
.Empty();
4529 m_colWidths
.Alloc( m_numCols
);
4530 m_colRights
.Alloc( m_numCols
);
4532 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4535 for ( int i
= 0; i
< m_numCols
; i
++ )
4537 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4538 m_colRights
.Add( colRight
);
4542 int wxGrid::GetColWidth(int col
) const
4544 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4547 int wxGrid::GetColLeft(int col
) const
4549 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4550 : m_colRights
[col
] - m_colWidths
[col
];
4553 int wxGrid::GetColRight(int col
) const
4555 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4559 int wxGrid::GetRowHeight(int row
) const
4561 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4564 int wxGrid::GetRowTop(int row
) const
4566 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4567 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4570 int wxGrid::GetRowBottom(int row
) const
4572 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4573 : m_rowBottoms
[row
];
4576 void wxGrid::CalcDimensions()
4578 // compute the size of the scrollable area
4579 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4580 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4585 // take into account editor if shown
4586 if ( IsCellEditControlShown() )
4589 int r
= m_currentCellCoords
.GetRow();
4590 int c
= m_currentCellCoords
.GetCol();
4591 int x
= GetColLeft(c
);
4592 int y
= GetRowTop(r
);
4594 // how big is the editor
4595 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4596 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4597 editor
->GetControl()->GetSize(&w2
, &h2
);
4608 // preserve (more or less) the previous position
4610 GetViewStart( &x
, &y
);
4612 // ensure the position is valid for the new scroll ranges
4614 x
= wxMax( w
- 1, 0 );
4616 y
= wxMax( h
- 1, 0 );
4618 // update the virtual size and refresh the scrollbars to reflect it
4619 m_gridWin
->SetVirtualSize(w
, h
);
4623 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4624 // still must reposition the children
4628 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4630 wxSize
sizeGridWin(size
);
4631 sizeGridWin
.x
-= m_rowLabelWidth
;
4632 sizeGridWin
.y
-= m_colLabelHeight
;
4637 void wxGrid::CalcWindowSizes()
4639 // escape if the window is has not been fully created yet
4641 if ( m_cornerLabelWin
== NULL
)
4645 GetClientSize( &cw
, &ch
);
4647 // the grid may be too small to have enough space for the labels yet, don't
4648 // size the windows to negative sizes in this case
4649 int gw
= cw
- m_rowLabelWidth
;
4650 int gh
= ch
- m_colLabelHeight
;
4656 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4657 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4659 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4660 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4662 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4663 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4665 if ( m_gridWin
&& m_gridWin
->IsShown() )
4666 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4669 // this is called when the grid table sends a message
4670 // to indicate that it has been redimensioned
4672 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4675 bool result
= false;
4677 // Clear the attribute cache as the attribute might refer to a different
4678 // cell than stored in the cache after adding/removing rows/columns.
4681 // By the same reasoning, the editor should be dismissed if columns are
4682 // added or removed. And for consistency, it should IMHO always be
4683 // removed, not only if the cell "underneath" it actually changes.
4684 // For now, I intentionally do not save the editor's content as the
4685 // cell it might want to save that stuff to might no longer exist.
4686 HideCellEditControl();
4689 // if we were using the default widths/heights so far, we must change them
4691 if ( m_colWidths
.IsEmpty() )
4696 if ( m_rowHeights
.IsEmpty() )
4702 switch ( msg
.GetId() )
4704 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4706 size_t pos
= msg
.GetCommandInt();
4707 int numRows
= msg
.GetCommandInt2();
4709 m_numRows
+= numRows
;
4711 if ( !m_rowHeights
.IsEmpty() )
4713 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4714 m_rowBottoms
.Insert( 0, pos
, numRows
);
4718 bottom
= m_rowBottoms
[pos
- 1];
4720 for ( i
= pos
; i
< m_numRows
; i
++ )
4722 bottom
+= m_rowHeights
[i
];
4723 m_rowBottoms
[i
] = bottom
;
4727 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4729 // if we have just inserted cols into an empty grid the current
4730 // cell will be undefined...
4732 SetCurrentCell( 0, 0 );
4736 m_selection
->UpdateRows( pos
, numRows
);
4737 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4739 attrProvider
->UpdateAttrRows( pos
, numRows
);
4741 if ( !GetBatchCount() )
4744 m_rowLabelWin
->Refresh();
4750 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4752 int numRows
= msg
.GetCommandInt();
4753 int oldNumRows
= m_numRows
;
4754 m_numRows
+= numRows
;
4756 if ( !m_rowHeights
.IsEmpty() )
4758 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4759 m_rowBottoms
.Add( 0, numRows
);
4762 if ( oldNumRows
> 0 )
4763 bottom
= m_rowBottoms
[oldNumRows
- 1];
4765 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4767 bottom
+= m_rowHeights
[i
];
4768 m_rowBottoms
[i
] = bottom
;
4772 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4774 // if we have just inserted cols into an empty grid the current
4775 // cell will be undefined...
4777 SetCurrentCell( 0, 0 );
4780 if ( !GetBatchCount() )
4783 m_rowLabelWin
->Refresh();
4789 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4791 size_t pos
= msg
.GetCommandInt();
4792 int numRows
= msg
.GetCommandInt2();
4793 m_numRows
-= numRows
;
4795 if ( !m_rowHeights
.IsEmpty() )
4797 m_rowHeights
.RemoveAt( pos
, numRows
);
4798 m_rowBottoms
.RemoveAt( pos
, numRows
);
4801 for ( i
= 0; i
< m_numRows
; i
++ )
4803 h
+= m_rowHeights
[i
];
4804 m_rowBottoms
[i
] = h
;
4810 m_currentCellCoords
= wxGridNoCellCoords
;
4814 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4815 m_currentCellCoords
.Set( 0, 0 );
4819 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4820 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4823 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4825 // ifdef'd out following patch from Paul Gammans
4827 // No need to touch column attributes, unless we
4828 // removed _all_ rows, in this case, we remove
4829 // all column attributes.
4830 // I hate to do this here, but the
4831 // needed data is not available inside UpdateAttrRows.
4832 if ( !GetNumberRows() )
4833 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4837 if ( !GetBatchCount() )
4840 m_rowLabelWin
->Refresh();
4846 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4848 size_t pos
= msg
.GetCommandInt();
4849 int numCols
= msg
.GetCommandInt2();
4850 m_numCols
+= numCols
;
4852 if ( !m_colAt
.IsEmpty() )
4854 //Shift the column IDs
4856 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4858 if ( m_colAt
[i
] >= (int)pos
)
4859 m_colAt
[i
] += numCols
;
4862 m_colAt
.Insert( pos
, pos
, numCols
);
4864 //Set the new columns' positions
4865 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4871 if ( !m_colWidths
.IsEmpty() )
4873 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4874 m_colRights
.Insert( 0, pos
, numCols
);
4878 right
= m_colRights
[GetColAt( pos
- 1 )];
4881 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4883 i
= GetColAt( colPos
);
4885 right
+= m_colWidths
[i
];
4886 m_colRights
[i
] = right
;
4890 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4892 // if we have just inserted cols into an empty grid the current
4893 // cell will be undefined...
4895 SetCurrentCell( 0, 0 );
4899 m_selection
->UpdateCols( pos
, numCols
);
4900 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4902 attrProvider
->UpdateAttrCols( pos
, numCols
);
4903 if ( !GetBatchCount() )
4906 m_colLabelWin
->Refresh();
4912 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4914 int numCols
= msg
.GetCommandInt();
4915 int oldNumCols
= m_numCols
;
4916 m_numCols
+= numCols
;
4918 if ( !m_colAt
.IsEmpty() )
4920 m_colAt
.Add( 0, numCols
);
4922 //Set the new columns' positions
4924 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4930 if ( !m_colWidths
.IsEmpty() )
4932 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4933 m_colRights
.Add( 0, numCols
);
4936 if ( oldNumCols
> 0 )
4937 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4940 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4942 i
= GetColAt( colPos
);
4944 right
+= m_colWidths
[i
];
4945 m_colRights
[i
] = right
;
4949 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4951 // if we have just inserted cols into an empty grid the current
4952 // cell will be undefined...
4954 SetCurrentCell( 0, 0 );
4956 if ( !GetBatchCount() )
4959 m_colLabelWin
->Refresh();
4965 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4967 size_t pos
= msg
.GetCommandInt();
4968 int numCols
= msg
.GetCommandInt2();
4969 m_numCols
-= numCols
;
4971 if ( !m_colAt
.IsEmpty() )
4973 int colID
= GetColAt( pos
);
4975 m_colAt
.RemoveAt( pos
, numCols
);
4977 //Shift the column IDs
4979 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4981 if ( m_colAt
[colPos
] > colID
)
4982 m_colAt
[colPos
] -= numCols
;
4986 if ( !m_colWidths
.IsEmpty() )
4988 m_colWidths
.RemoveAt( pos
, numCols
);
4989 m_colRights
.RemoveAt( pos
, numCols
);
4993 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4995 i
= GetColAt( colPos
);
4997 w
+= m_colWidths
[i
];
5004 m_currentCellCoords
= wxGridNoCellCoords
;
5008 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5009 m_currentCellCoords
.Set( 0, 0 );
5013 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5014 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5017 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5019 // ifdef'd out following patch from Paul Gammans
5021 // No need to touch row attributes, unless we
5022 // removed _all_ columns, in this case, we remove
5023 // all row attributes.
5024 // I hate to do this here, but the
5025 // needed data is not available inside UpdateAttrCols.
5026 if ( !GetNumberCols() )
5027 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5031 if ( !GetBatchCount() )
5034 m_colLabelWin
->Refresh();
5041 if (result
&& !GetBatchCount() )
5042 m_gridWin
->Refresh();
5047 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5049 wxRegionIterator
iter( reg
);
5052 wxArrayInt rowlabels
;
5059 // TODO: remove this when we can...
5060 // There is a bug in wxMotif that gives garbage update
5061 // rectangles if you jump-scroll a long way by clicking the
5062 // scrollbar with middle button. This is a work-around
5064 #if defined(__WXMOTIF__)
5066 m_gridWin
->GetClientSize( &cw
, &ch
);
5067 if ( r
.GetTop() > ch
)
5069 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5072 // logical bounds of update region
5075 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5076 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5078 // find the row labels within these bounds
5081 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5083 if ( GetRowBottom(row
) < top
)
5086 if ( GetRowTop(row
) > bottom
)
5089 rowlabels
.Add( row
);
5098 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5100 wxRegionIterator
iter( reg
);
5103 wxArrayInt colLabels
;
5110 // TODO: remove this when we can...
5111 // There is a bug in wxMotif that gives garbage update
5112 // rectangles if you jump-scroll a long way by clicking the
5113 // scrollbar with middle button. This is a work-around
5115 #if defined(__WXMOTIF__)
5117 m_gridWin
->GetClientSize( &cw
, &ch
);
5118 if ( r
.GetLeft() > cw
)
5120 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5123 // logical bounds of update region
5126 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5127 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5129 // find the cells within these bounds
5133 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5135 col
= GetColAt( colPos
);
5137 if ( GetColRight(col
) < left
)
5140 if ( GetColLeft(col
) > right
)
5143 colLabels
.Add( col
);
5152 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5154 wxRegionIterator
iter( reg
);
5157 wxGridCellCoordsArray cellsExposed
;
5159 int left
, top
, right
, bottom
;
5164 // TODO: remove this when we can...
5165 // There is a bug in wxMotif that gives garbage update
5166 // rectangles if you jump-scroll a long way by clicking the
5167 // scrollbar with middle button. This is a work-around
5169 #if defined(__WXMOTIF__)
5171 m_gridWin
->GetClientSize( &cw
, &ch
);
5172 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5173 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5174 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5175 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5178 // logical bounds of update region
5180 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5181 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5183 // find the cells within these bounds
5186 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5188 if ( GetRowBottom(row
) <= top
)
5191 if ( GetRowTop(row
) > bottom
)
5195 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5197 col
= GetColAt( colPos
);
5199 if ( GetColRight(col
) <= left
)
5202 if ( GetColLeft(col
) > right
)
5205 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5212 return cellsExposed
;
5216 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5219 wxPoint
pos( event
.GetPosition() );
5220 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5222 if ( event
.Dragging() )
5226 m_isDragging
= true;
5227 m_rowLabelWin
->CaptureMouse();
5230 if ( event
.LeftIsDown() )
5232 switch ( m_cursorMode
)
5234 case WXGRID_CURSOR_RESIZE_ROW
:
5236 int cw
, ch
, left
, dummy
;
5237 m_gridWin
->GetClientSize( &cw
, &ch
);
5238 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5240 wxClientDC
dc( m_gridWin
);
5243 GetRowTop(m_dragRowOrCol
) +
5244 GetRowMinimalHeight(m_dragRowOrCol
) );
5245 dc
.SetLogicalFunction(wxINVERT
);
5246 if ( m_dragLastPos
>= 0 )
5248 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5250 dc
.DrawLine( left
, y
, left
+cw
, y
);
5255 case WXGRID_CURSOR_SELECT_ROW
:
5257 if ( (row
= YToRow( y
)) >= 0 )
5261 m_selection
->SelectRow( row
,
5262 event
.ControlDown(),
5271 // default label to suppress warnings about "enumeration value
5272 // 'xxx' not handled in switch
5280 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5285 if (m_rowLabelWin
->HasCapture())
5286 m_rowLabelWin
->ReleaseMouse();
5287 m_isDragging
= false;
5290 // ------------ Entering or leaving the window
5292 if ( event
.Entering() || event
.Leaving() )
5294 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5297 // ------------ Left button pressed
5299 else if ( event
.LeftDown() )
5301 // don't send a label click event for a hit on the
5302 // edge of the row label - this is probably the user
5303 // wanting to resize the row
5305 if ( YToEdgeOfRow(y
) < 0 )
5309 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5311 if ( !event
.ShiftDown() && !event
.CmdDown() )
5315 if ( event
.ShiftDown() )
5317 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5320 GetNumberCols() - 1,
5321 event
.ControlDown(),
5328 m_selection
->SelectRow( row
,
5329 event
.ControlDown(),
5336 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5341 // starting to drag-resize a row
5342 if ( CanDragRowSize() )
5343 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5347 // ------------ Left double click
5349 else if (event
.LeftDClick() )
5351 row
= YToEdgeOfRow(y
);
5356 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5358 // no default action at the moment
5363 // adjust row height depending on label text
5364 AutoSizeRowLabelSize( row
);
5366 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5371 // ------------ Left button released
5373 else if ( event
.LeftUp() )
5375 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5377 DoEndDragResizeRow();
5379 // Note: we are ending the event *after* doing
5380 // default processing in this case
5382 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5385 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5389 // ------------ Right button down
5391 else if ( event
.RightDown() )
5395 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5397 // no default action at the moment
5401 // ------------ Right double click
5403 else if ( event
.RightDClick() )
5407 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5409 // no default action at the moment
5413 // ------------ No buttons down and mouse moving
5415 else if ( event
.Moving() )
5417 m_dragRowOrCol
= YToEdgeOfRow( y
);
5418 if ( m_dragRowOrCol
>= 0 )
5420 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5422 // don't capture the mouse yet
5423 if ( CanDragRowSize() )
5424 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5427 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5429 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5434 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5437 wxPoint
pos( event
.GetPosition() );
5438 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5440 if ( event
.Dragging() )
5444 m_isDragging
= true;
5445 m_colLabelWin
->CaptureMouse();
5447 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5448 m_dragRowOrCol
= XToCol( x
);
5451 if ( event
.LeftIsDown() )
5453 switch ( m_cursorMode
)
5455 case WXGRID_CURSOR_RESIZE_COL
:
5457 int cw
, ch
, dummy
, top
;
5458 m_gridWin
->GetClientSize( &cw
, &ch
);
5459 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5461 wxClientDC
dc( m_gridWin
);
5464 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5465 GetColMinimalWidth(m_dragRowOrCol
));
5466 dc
.SetLogicalFunction(wxINVERT
);
5467 if ( m_dragLastPos
>= 0 )
5469 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5471 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5476 case WXGRID_CURSOR_SELECT_COL
:
5478 if ( (col
= XToCol( x
)) >= 0 )
5482 m_selection
->SelectCol( col
,
5483 event
.ControlDown(),
5492 case WXGRID_CURSOR_MOVE_COL
:
5495 m_moveToCol
= GetColAt( 0 );
5497 m_moveToCol
= XToCol( x
);
5501 if ( m_moveToCol
< 0 )
5502 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5503 else if ( x
>= (GetColLeft( m_moveToCol
) + (GetColWidth(m_moveToCol
) / 2)) )
5505 m_moveToCol
= GetColAt( GetColPos( m_moveToCol
) + 1 );
5506 if ( m_moveToCol
< 0 )
5507 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5509 markerX
= GetColLeft( m_moveToCol
);
5512 markerX
= GetColLeft( m_moveToCol
);
5514 if ( markerX
!= m_dragLastPos
)
5516 wxClientDC
dc( m_colLabelWin
);
5520 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5524 //Clean up the last indicator
5525 if ( m_dragLastPos
>= 0 )
5527 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5529 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5530 dc
.SetPen(wxNullPen
);
5532 if ( XToCol( m_dragLastPos
) != -1 )
5533 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5536 const wxColour
*color
;
5537 //Moving to the same place? Don't draw a marker
5538 if ( (m_moveToCol
== m_dragRowOrCol
)
5539 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5540 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5541 color
= wxLIGHT_GREY
;
5546 wxPen
pen( *color
, 2 );
5549 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5551 dc
.SetPen(wxNullPen
);
5553 m_dragLastPos
= markerX
- 1;
5558 // default label to suppress warnings about "enumeration value
5559 // 'xxx' not handled in switch
5567 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5572 if (m_colLabelWin
->HasCapture())
5573 m_colLabelWin
->ReleaseMouse();
5574 m_isDragging
= false;
5577 // ------------ Entering or leaving the window
5579 if ( event
.Entering() || event
.Leaving() )
5581 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5584 // ------------ Left button pressed
5586 else if ( event
.LeftDown() )
5588 // don't send a label click event for a hit on the
5589 // edge of the col label - this is probably the user
5590 // wanting to resize the col
5592 if ( XToEdgeOfCol(x
) < 0 )
5596 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5598 if ( m_canDragColMove
)
5600 //Show button as pressed
5601 wxClientDC
dc( m_colLabelWin
);
5602 int colLeft
= GetColLeft( col
);
5603 int colRight
= GetColRight( col
) - 1;
5604 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5605 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5606 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5608 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5612 if ( !event
.ShiftDown() && !event
.CmdDown() )
5616 if ( event
.ShiftDown() )
5618 m_selection
->SelectBlock( 0,
5619 m_currentCellCoords
.GetCol(),
5620 GetNumberRows() - 1, col
,
5621 event
.ControlDown(),
5628 m_selection
->SelectCol( col
,
5629 event
.ControlDown(),
5636 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5642 // starting to drag-resize a col
5644 if ( CanDragColSize() )
5645 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5649 // ------------ Left double click
5651 if ( event
.LeftDClick() )
5653 col
= XToEdgeOfCol(x
);
5658 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5660 // no default action at the moment
5665 // adjust column width depending on label text
5666 AutoSizeColLabelSize( col
);
5668 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5673 // ------------ Left button released
5675 else if ( event
.LeftUp() )
5677 switch ( m_cursorMode
)
5679 case WXGRID_CURSOR_RESIZE_COL
:
5680 DoEndDragResizeCol();
5682 // Note: we are ending the event *after* doing
5683 // default processing in this case
5685 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5688 case WXGRID_CURSOR_MOVE_COL
:
5691 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5694 case WXGRID_CURSOR_SELECT_COL
:
5695 case WXGRID_CURSOR_SELECT_CELL
:
5696 case WXGRID_CURSOR_RESIZE_ROW
:
5697 case WXGRID_CURSOR_SELECT_ROW
:
5698 // nothing to do (?)
5702 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5706 // ------------ Right button down
5708 else if ( event
.RightDown() )
5712 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5714 // no default action at the moment
5718 // ------------ Right double click
5720 else if ( event
.RightDClick() )
5724 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5726 // no default action at the moment
5730 // ------------ No buttons down and mouse moving
5732 else if ( event
.Moving() )
5734 m_dragRowOrCol
= XToEdgeOfCol( x
);
5735 if ( m_dragRowOrCol
>= 0 )
5737 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5739 // don't capture the cursor yet
5740 if ( CanDragColSize() )
5741 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5744 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5746 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5751 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5753 if ( event
.LeftDown() )
5755 // indicate corner label by having both row and
5758 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5763 else if ( event
.LeftDClick() )
5765 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5767 else if ( event
.RightDown() )
5769 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5771 // no default action at the moment
5774 else if ( event
.RightDClick() )
5776 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5778 // no default action at the moment
5783 void wxGrid::CancelMouseCapture()
5785 // cancel operation currently in progress, whatever it is
5788 m_isDragging
= false;
5789 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5790 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5791 m_winCapture
= NULL
;
5793 // remove traces of whatever we drew on screen
5798 void wxGrid::ChangeCursorMode(CursorMode mode
,
5803 static const wxChar
*cursorModes
[] =
5813 wxLogTrace(_T("grid"),
5814 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5815 win
== m_colLabelWin
? _T("colLabelWin")
5816 : win
? _T("rowLabelWin")
5818 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5821 if ( mode
== m_cursorMode
&&
5822 win
== m_winCapture
&&
5823 captureMouse
== (m_winCapture
!= NULL
))
5828 // by default use the grid itself
5834 if (m_winCapture
->HasCapture())
5835 m_winCapture
->ReleaseMouse();
5836 m_winCapture
= (wxWindow
*)NULL
;
5839 m_cursorMode
= mode
;
5841 switch ( m_cursorMode
)
5843 case WXGRID_CURSOR_RESIZE_ROW
:
5844 win
->SetCursor( m_rowResizeCursor
);
5847 case WXGRID_CURSOR_RESIZE_COL
:
5848 win
->SetCursor( m_colResizeCursor
);
5851 case WXGRID_CURSOR_MOVE_COL
:
5852 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5856 win
->SetCursor( *wxSTANDARD_CURSOR
);
5860 // we need to capture mouse when resizing
5861 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5862 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5864 if ( captureMouse
&& resize
)
5866 win
->CaptureMouse();
5871 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5874 wxPoint
pos( event
.GetPosition() );
5875 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5877 wxGridCellCoords coords
;
5878 XYToCell( x
, y
, coords
);
5880 int cell_rows
, cell_cols
;
5881 bool isFirstDrag
= !m_isDragging
;
5882 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5883 if ((cell_rows
< 0) || (cell_cols
< 0))
5885 coords
.SetRow(coords
.GetRow() + cell_rows
);
5886 coords
.SetCol(coords
.GetCol() + cell_cols
);
5889 if ( event
.Dragging() )
5891 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5893 // Don't start doing anything until the mouse has been dragged at
5894 // least 3 pixels in any direction...
5897 if (m_startDragPos
== wxDefaultPosition
)
5899 m_startDragPos
= pos
;
5902 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5906 m_isDragging
= true;
5907 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5909 // Hide the edit control, so it
5910 // won't interfere with drag-shrinking.
5911 if ( IsCellEditControlShown() )
5913 HideCellEditControl();
5914 SaveEditControlValue();
5917 if ( coords
!= wxGridNoCellCoords
)
5919 if ( event
.CmdDown() )
5921 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5922 m_selectingKeyboard
= coords
;
5923 HighlightBlock( m_selectingKeyboard
, coords
);
5925 else if ( CanDragCell() )
5929 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5930 m_selectingKeyboard
= coords
;
5932 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5941 if ( !IsSelection() )
5943 HighlightBlock( coords
, coords
);
5947 HighlightBlock( m_currentCellCoords
, coords
);
5951 if (! IsVisible(coords
))
5953 MakeCellVisible(coords
);
5954 // TODO: need to introduce a delay or something here. The
5955 // scrolling is way too fast, at least under MSW and GTK.
5958 // Have we captured the mouse yet?
5961 m_winCapture
= m_gridWin
;
5962 m_winCapture
->CaptureMouse();
5967 else if ( event
.LeftIsDown() &&
5968 m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5970 int cw
, ch
, left
, dummy
;
5971 m_gridWin
->GetClientSize( &cw
, &ch
);
5972 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5974 wxClientDC
dc( m_gridWin
);
5976 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5977 GetRowMinimalHeight(m_dragRowOrCol
) );
5978 dc
.SetLogicalFunction(wxINVERT
);
5979 if ( m_dragLastPos
>= 0 )
5981 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5983 dc
.DrawLine( left
, y
, left
+cw
, y
);
5986 else if ( event
.LeftIsDown() &&
5987 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5989 int cw
, ch
, dummy
, top
;
5990 m_gridWin
->GetClientSize( &cw
, &ch
);
5991 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5993 wxClientDC
dc( m_gridWin
);
5995 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5996 GetColMinimalWidth(m_dragRowOrCol
) );
5997 dc
.SetLogicalFunction(wxINVERT
);
5998 if ( m_dragLastPos
>= 0 )
6000 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6002 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6009 m_isDragging
= false;
6010 m_startDragPos
= wxDefaultPosition
;
6012 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6013 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6016 if ( event
.Entering() || event
.Leaving() )
6018 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6019 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6024 // ------------ Left button pressed
6026 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6028 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6033 if ( !event
.CmdDown() )
6035 if ( event
.ShiftDown() )
6039 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6040 m_currentCellCoords
.GetCol(),
6043 event
.ControlDown(),
6049 else if ( XToEdgeOfCol(x
) < 0 &&
6050 YToEdgeOfRow(y
) < 0 )
6052 DisableCellEditControl();
6053 MakeCellVisible( coords
);
6055 if ( event
.CmdDown() )
6059 m_selection
->ToggleCellSelection( coords
.GetRow(),
6061 event
.ControlDown(),
6066 m_selectingTopLeft
= wxGridNoCellCoords
;
6067 m_selectingBottomRight
= wxGridNoCellCoords
;
6068 m_selectingKeyboard
= coords
;
6072 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
6073 coords
!= wxGridNoCellCoords
;
6074 SetCurrentCell( coords
);
6080 // ------------ Left double click
6082 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6084 DisableCellEditControl();
6086 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6088 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6093 // we want double click to select a cell and start editing
6094 // (i.e. to behave in same way as sequence of two slow clicks):
6095 m_waitForSlowClick
= true;
6100 // ------------ Left button released
6102 else if ( event
.LeftUp() )
6104 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6108 if (m_winCapture
->HasCapture())
6109 m_winCapture
->ReleaseMouse();
6110 m_winCapture
= NULL
;
6113 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6116 EnableCellEditControl();
6118 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6119 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6120 editor
->StartingClick();
6124 m_waitForSlowClick
= false;
6126 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6127 m_selectingBottomRight
!= wxGridNoCellCoords
)
6131 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6132 m_selectingTopLeft
.GetCol(),
6133 m_selectingBottomRight
.GetRow(),
6134 m_selectingBottomRight
.GetCol(),
6135 event
.ControlDown(),
6141 m_selectingTopLeft
= wxGridNoCellCoords
;
6142 m_selectingBottomRight
= wxGridNoCellCoords
;
6144 // Show the edit control, if it has been hidden for
6146 ShowCellEditControl();
6149 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6151 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6152 DoEndDragResizeRow();
6154 // Note: we are ending the event *after* doing
6155 // default processing in this case
6157 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6159 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6161 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6162 DoEndDragResizeCol();
6164 // Note: we are ending the event *after* doing
6165 // default processing in this case
6167 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6173 // ------------ Right button down
6175 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6177 DisableCellEditControl();
6178 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6183 // no default action at the moment
6187 // ------------ Right double click
6189 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6191 DisableCellEditControl();
6192 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6197 // no default action at the moment
6201 // ------------ Moving and no button action
6203 else if ( event
.Moving() && !event
.IsButton() )
6205 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6207 // out of grid cell area
6208 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6212 int dragRow
= YToEdgeOfRow( y
);
6213 int dragCol
= XToEdgeOfCol( x
);
6215 // Dragging on the corner of a cell to resize in both
6216 // directions is not implemented yet...
6218 if ( dragRow
>= 0 && dragCol
>= 0 )
6220 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6226 m_dragRowOrCol
= dragRow
;
6228 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6230 if ( CanDragRowSize() && CanDragGridSize() )
6231 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6234 else if ( dragCol
>= 0 )
6236 m_dragRowOrCol
= dragCol
;
6238 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6240 if ( CanDragColSize() && CanDragGridSize() )
6241 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6244 else // Neither on a row or col edge
6246 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6248 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6254 void wxGrid::DoEndDragResizeRow()
6256 if ( m_dragLastPos
>= 0 )
6258 // erase the last line and resize the row
6260 int cw
, ch
, left
, dummy
;
6261 m_gridWin
->GetClientSize( &cw
, &ch
);
6262 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6264 wxClientDC
dc( m_gridWin
);
6266 dc
.SetLogicalFunction( wxINVERT
);
6267 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6268 HideCellEditControl();
6269 SaveEditControlValue();
6271 int rowTop
= GetRowTop(m_dragRowOrCol
);
6272 SetRowSize( m_dragRowOrCol
,
6273 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6275 if ( !GetBatchCount() )
6277 // Only needed to get the correct rect.y:
6278 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6280 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6281 rect
.width
= m_rowLabelWidth
;
6282 rect
.height
= ch
- rect
.y
;
6283 m_rowLabelWin
->Refresh( true, &rect
);
6286 // if there is a multicell block, paint all of it
6289 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6290 int leftCol
= XToCol(left
);
6291 int rightCol
= internalXToCol(left
+ cw
);
6294 for (i
=leftCol
; i
<rightCol
; i
++)
6296 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6297 if (cell_rows
< subtract_rows
)
6298 subtract_rows
= cell_rows
;
6300 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6301 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6302 rect
.height
= ch
- rect
.y
;
6305 m_gridWin
->Refresh( false, &rect
);
6308 ShowCellEditControl();
6313 void wxGrid::DoEndDragResizeCol()
6315 if ( m_dragLastPos
>= 0 )
6317 // erase the last line and resize the col
6319 int cw
, ch
, dummy
, top
;
6320 m_gridWin
->GetClientSize( &cw
, &ch
);
6321 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6323 wxClientDC
dc( m_gridWin
);
6325 dc
.SetLogicalFunction( wxINVERT
);
6326 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6327 HideCellEditControl();
6328 SaveEditControlValue();
6330 int colLeft
= GetColLeft(m_dragRowOrCol
);
6331 SetColSize( m_dragRowOrCol
,
6332 wxMax( m_dragLastPos
- colLeft
,
6333 GetColMinimalWidth(m_dragRowOrCol
) ) );
6335 if ( !GetBatchCount() )
6337 // Only needed to get the correct rect.x:
6338 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6340 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6341 rect
.width
= cw
- rect
.x
;
6342 rect
.height
= m_colLabelHeight
;
6343 m_colLabelWin
->Refresh( true, &rect
);
6346 // if there is a multicell block, paint all of it
6349 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6350 int topRow
= YToRow(top
);
6351 int bottomRow
= internalYToRow(top
+ cw
);
6354 for (i
=topRow
; i
<bottomRow
; i
++)
6356 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6357 if (cell_cols
< subtract_cols
)
6358 subtract_cols
= cell_cols
;
6361 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6362 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6363 rect
.width
= cw
- rect
.x
;
6367 m_gridWin
->Refresh( false, &rect
);
6370 ShowCellEditControl();
6374 void wxGrid::DoEndDragMoveCol()
6376 //The user clicked on the column but didn't actually drag
6377 if ( m_dragLastPos
< 0 )
6379 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6384 if ( m_moveToCol
== -1 )
6385 newPos
= m_numCols
- 1;
6388 newPos
= GetColPos( m_moveToCol
);
6389 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6393 SetColPos( m_dragRowOrCol
, newPos
);
6396 void wxGrid::SetColPos( int colID
, int newPos
)
6398 if ( m_colAt
.IsEmpty() )
6400 m_colAt
.Alloc( m_numCols
);
6403 for ( i
= 0; i
< m_numCols
; i
++ )
6409 int oldPos
= GetColPos( colID
);
6411 //Reshuffle the m_colAt array
6412 if ( newPos
> oldPos
)
6415 for ( i
= oldPos
; i
< newPos
; i
++ )
6417 m_colAt
[i
] = m_colAt
[i
+1];
6423 for ( i
= oldPos
; i
> newPos
; i
-- )
6425 m_colAt
[i
] = m_colAt
[i
-1];
6429 m_colAt
[newPos
] = colID
;
6431 //Recalculate the column rights
6432 if ( !m_colWidths
.IsEmpty() )
6436 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6438 int colID
= GetColAt( colPos
);
6440 colRight
+= m_colWidths
[colID
];
6441 m_colRights
[colID
] = colRight
;
6445 m_colLabelWin
->Refresh();
6446 m_gridWin
->Refresh();
6451 void wxGrid::EnableDragColMove( bool enable
)
6453 if ( m_canDragColMove
== enable
)
6456 m_canDragColMove
= enable
;
6458 if ( !m_canDragColMove
)
6462 //Recalculate the column rights
6463 if ( !m_colWidths
.IsEmpty() )
6467 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6469 colRight
+= m_colWidths
[colPos
];
6470 m_colRights
[colPos
] = colRight
;
6474 m_colLabelWin
->Refresh();
6475 m_gridWin
->Refresh();
6481 // ------ interaction with data model
6483 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6485 switch ( msg
.GetId() )
6487 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6488 return GetModelValues();
6490 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6491 return SetModelValues();
6493 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6494 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6495 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6496 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6497 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6498 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6499 return Redimension( msg
);
6506 // The behaviour of this function depends on the grid table class
6507 // Clear() function. For the default wxGridStringTable class the
6508 // behavious is to replace all cell contents with wxEmptyString but
6509 // not to change the number of rows or cols.
6511 void wxGrid::ClearGrid()
6515 if (IsCellEditControlEnabled())
6516 DisableCellEditControl();
6519 if (!GetBatchCount())
6520 m_gridWin
->Refresh();
6524 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6528 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6534 if (IsCellEditControlEnabled())
6535 DisableCellEditControl();
6537 bool done
= m_table
->InsertRows( pos
, numRows
);
6540 // the table will have sent the results of the insert row
6541 // operation to this view object as a grid table message
6547 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6551 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6557 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6560 // the table will have sent the results of the append row
6561 // operation to this view object as a grid table message
6567 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6571 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6577 if (IsCellEditControlEnabled())
6578 DisableCellEditControl();
6580 bool done
= m_table
->DeleteRows( pos
, numRows
);
6582 // the table will have sent the results of the delete row
6583 // operation to this view object as a grid table message
6589 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6593 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6599 if (IsCellEditControlEnabled())
6600 DisableCellEditControl();
6602 bool done
= m_table
->InsertCols( pos
, numCols
);
6604 // the table will have sent the results of the insert col
6605 // operation to this view object as a grid table message
6611 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6615 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6621 bool done
= m_table
->AppendCols( numCols
);
6623 // the table will have sent the results of the append col
6624 // operation to this view object as a grid table message
6630 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6634 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6640 if (IsCellEditControlEnabled())
6641 DisableCellEditControl();
6643 bool done
= m_table
->DeleteCols( pos
, numCols
);
6645 // the table will have sent the results of the delete col
6646 // operation to this view object as a grid table message
6653 // ----- event handlers
6656 // Generate a grid event based on a mouse event and
6657 // return the result of ProcessEvent()
6659 int wxGrid::SendEvent( const wxEventType type
,
6661 wxMouseEvent
& mouseEv
)
6663 bool claimed
, vetoed
;
6665 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6667 int rowOrCol
= (row
== -1 ? col
: row
);
6669 wxGridSizeEvent
gridEvt( GetId(),
6673 mouseEv
.GetX() + GetRowLabelSize(),
6674 mouseEv
.GetY() + GetColLabelSize(),
6675 mouseEv
.ControlDown(),
6676 mouseEv
.ShiftDown(),
6678 mouseEv
.MetaDown() );
6680 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6681 vetoed
= !gridEvt
.IsAllowed();
6683 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6685 // Right now, it should _never_ end up here!
6686 wxGridRangeSelectEvent
gridEvt( GetId(),
6690 m_selectingBottomRight
,
6692 mouseEv
.ControlDown(),
6693 mouseEv
.ShiftDown(),
6695 mouseEv
.MetaDown() );
6697 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6698 vetoed
= !gridEvt
.IsAllowed();
6700 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6701 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6702 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6703 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6705 wxPoint pos
= mouseEv
.GetPosition();
6707 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6708 pos
.y
+= GetColLabelSize();
6709 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6710 pos
.x
+= GetRowLabelSize();
6712 wxGridEvent
gridEvt( GetId(),
6719 mouseEv
.ControlDown(),
6720 mouseEv
.ShiftDown(),
6722 mouseEv
.MetaDown() );
6723 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6724 vetoed
= !gridEvt
.IsAllowed();
6728 wxGridEvent
gridEvt( GetId(),
6732 mouseEv
.GetX() + GetRowLabelSize(),
6733 mouseEv
.GetY() + GetColLabelSize(),
6735 mouseEv
.ControlDown(),
6736 mouseEv
.ShiftDown(),
6738 mouseEv
.MetaDown() );
6739 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6740 vetoed
= !gridEvt
.IsAllowed();
6743 // A Veto'd event may not be `claimed' so test this first
6747 return claimed
? 1 : 0;
6750 // Generate a grid event of specified type and return the result
6751 // of ProcessEvent().
6753 int wxGrid::SendEvent( const wxEventType type
,
6756 bool claimed
, vetoed
;
6758 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6760 int rowOrCol
= (row
== -1 ? col
: row
);
6762 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6764 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6765 vetoed
= !gridEvt
.IsAllowed();
6769 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6771 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6772 vetoed
= !gridEvt
.IsAllowed();
6775 // A Veto'd event may not be `claimed' so test this first
6779 return claimed
? 1 : 0;
6782 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6784 // needed to prevent zillions of paint events on MSW
6788 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6790 // Don't do anything if between Begin/EndBatch...
6791 // EndBatch() will do all this on the last nested one anyway.
6792 if ( m_created
&& !GetBatchCount() )
6794 // Refresh to get correct scrolled position:
6795 wxScrolledWindow::Refresh(eraseb
, rect
);
6799 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6800 int width_label
, width_cell
, height_label
, height_cell
;
6803 // Copy rectangle can get scroll offsets..
6804 rect_x
= rect
->GetX();
6805 rect_y
= rect
->GetY();
6806 rectWidth
= rect
->GetWidth();
6807 rectHeight
= rect
->GetHeight();
6809 width_label
= m_rowLabelWidth
- rect_x
;
6810 if (width_label
> rectWidth
)
6811 width_label
= rectWidth
;
6813 height_label
= m_colLabelHeight
- rect_y
;
6814 if (height_label
> rectHeight
)
6815 height_label
= rectHeight
;
6817 if (rect_x
> m_rowLabelWidth
)
6819 x
= rect_x
- m_rowLabelWidth
;
6820 width_cell
= rectWidth
;
6825 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6828 if (rect_y
> m_colLabelHeight
)
6830 y
= rect_y
- m_colLabelHeight
;
6831 height_cell
= rectHeight
;
6836 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6839 // Paint corner label part intersecting rect.
6840 if ( width_label
> 0 && height_label
> 0 )
6842 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6843 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6846 // Paint col labels part intersecting rect.
6847 if ( width_cell
> 0 && height_label
> 0 )
6849 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6850 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6853 // Paint row labels part intersecting rect.
6854 if ( width_label
> 0 && height_cell
> 0 )
6856 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6857 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6860 // Paint cell area part intersecting rect.
6861 if ( width_cell
> 0 && height_cell
> 0 )
6863 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6864 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6869 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6870 m_colLabelWin
->Refresh(eraseb
, NULL
);
6871 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6872 m_gridWin
->Refresh(eraseb
, NULL
);
6877 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6879 if (m_targetWindow
!= this) // check whether initialisation has been done
6881 // reposition our children windows
6886 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6888 if ( m_inOnKeyDown
)
6890 // shouldn't be here - we are going round in circles...
6892 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6895 m_inOnKeyDown
= true;
6897 // propagate the event up and see if it gets processed
6898 wxWindow
*parent
= GetParent();
6899 wxKeyEvent
keyEvt( event
);
6900 keyEvt
.SetEventObject( parent
);
6902 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6904 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6906 if (event
.GetKeyCode() == WXK_RIGHT
)
6907 event
.m_keyCode
= WXK_LEFT
;
6908 else if (event
.GetKeyCode() == WXK_LEFT
)
6909 event
.m_keyCode
= WXK_RIGHT
;
6912 // try local handlers
6913 switch ( event
.GetKeyCode() )
6916 if ( event
.ControlDown() )
6917 MoveCursorUpBlock( event
.ShiftDown() );
6919 MoveCursorUp( event
.ShiftDown() );
6923 if ( event
.ControlDown() )
6924 MoveCursorDownBlock( event
.ShiftDown() );
6926 MoveCursorDown( event
.ShiftDown() );
6930 if ( event
.ControlDown() )
6931 MoveCursorLeftBlock( event
.ShiftDown() );
6933 MoveCursorLeft( event
.ShiftDown() );
6937 if ( event
.ControlDown() )
6938 MoveCursorRightBlock( event
.ShiftDown() );
6940 MoveCursorRight( event
.ShiftDown() );
6944 case WXK_NUMPAD_ENTER
:
6945 if ( event
.ControlDown() )
6947 event
.Skip(); // to let the edit control have the return
6951 if ( GetGridCursorRow() < GetNumberRows()-1 )
6953 MoveCursorDown( event
.ShiftDown() );
6957 // at the bottom of a column
6958 DisableCellEditControl();
6968 if (event
.ShiftDown())
6970 if ( GetGridCursorCol() > 0 )
6972 MoveCursorLeft( false );
6977 DisableCellEditControl();
6982 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6984 MoveCursorRight( false );
6989 DisableCellEditControl();
6995 if ( event
.ControlDown() )
6997 MakeCellVisible( 0, 0 );
6998 SetCurrentCell( 0, 0 );
7007 if ( event
.ControlDown() )
7009 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7010 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7027 // Ctrl-Space selects the current column, Shift-Space -- the
7028 // current row and Ctrl-Shift-Space -- everything
7029 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
7032 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
7036 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
7039 case wxMOD_CONTROL
| wxMOD_SHIFT
:
7040 m_selection
->SelectBlock(0, 0,
7041 m_numRows
- 1, m_numCols
- 1);
7045 if ( !IsEditable() )
7047 MoveCursorRight(false);
7050 //else: fall through
7063 m_inOnKeyDown
= false;
7066 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7068 // try local handlers
7070 if ( event
.GetKeyCode() == WXK_SHIFT
)
7072 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7073 m_selectingBottomRight
!= wxGridNoCellCoords
)
7077 m_selection
->SelectBlock(
7078 m_selectingTopLeft
.GetRow(),
7079 m_selectingTopLeft
.GetCol(),
7080 m_selectingBottomRight
.GetRow(),
7081 m_selectingBottomRight
.GetCol(),
7082 event
.ControlDown(),
7089 m_selectingTopLeft
= wxGridNoCellCoords
;
7090 m_selectingBottomRight
= wxGridNoCellCoords
;
7091 m_selectingKeyboard
= wxGridNoCellCoords
;
7095 void wxGrid::OnChar( wxKeyEvent
& event
)
7097 // is it possible to edit the current cell at all?
7098 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7100 // yes, now check whether the cells editor accepts the key
7101 int row
= m_currentCellCoords
.GetRow();
7102 int col
= m_currentCellCoords
.GetCol();
7103 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7104 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7106 // <F2> is special and will always start editing, for
7107 // other keys - ask the editor itself
7108 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7109 || editor
->IsAcceptedKey(event
) )
7111 // ensure cell is visble
7112 MakeCellVisible(row
, col
);
7113 EnableCellEditControl();
7115 // a problem can arise if the cell is not completely
7116 // visible (even after calling MakeCellVisible the
7117 // control is not created and calling StartingKey will
7119 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7120 editor
->StartingKey(event
);
7136 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7140 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7142 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7144 // the event has been intercepted - do nothing
7148 #if !defined(__WXMAC__)
7149 wxClientDC
dc( m_gridWin
);
7153 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7155 DisableCellEditControl();
7157 if ( IsVisible( m_currentCellCoords
, false ) )
7160 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7161 if ( !m_gridLinesEnabled
)
7169 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7171 // Otherwise refresh redraws the highlight!
7172 m_currentCellCoords
= coords
;
7174 #if defined(__WXMAC__)
7175 m_gridWin
->Refresh(true /*, & r */);
7177 DrawGridCellArea( dc
, cells
);
7178 DrawAllGridLines( dc
, r
);
7183 m_currentCellCoords
= coords
;
7185 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7186 #if !defined(__WXMAC__)
7187 DrawCellHighlight( dc
, attr
);
7192 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7195 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7199 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7202 rightCol
= GetNumberCols() - 1;
7204 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7207 bottomRow
= GetNumberRows() - 1;
7211 if ( topRow
> bottomRow
)
7218 if ( leftCol
> rightCol
)
7225 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7226 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7228 // First the case that we selected a completely new area
7229 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7230 m_selectingBottomRight
== wxGridNoCellCoords
)
7233 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7234 wxGridCellCoords ( bottomRow
, rightCol
) );
7235 m_gridWin
->Refresh( false, &rect
);
7238 // Now handle changing an existing selection area.
7239 else if ( m_selectingTopLeft
!= updateTopLeft
||
7240 m_selectingBottomRight
!= updateBottomRight
)
7242 // Compute two optimal update rectangles:
7243 // Either one rectangle is a real subset of the
7244 // other, or they are (almost) disjoint!
7246 bool need_refresh
[4];
7250 need_refresh
[3] = false;
7253 // Store intermediate values
7254 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7255 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7256 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7257 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7259 // Determine the outer/inner coordinates.
7260 if (oldLeft
> leftCol
)
7266 if (oldTop
> topRow
)
7272 if (oldRight
< rightCol
)
7275 oldRight
= rightCol
;
7278 if (oldBottom
< bottomRow
)
7281 oldBottom
= bottomRow
;
7285 // Now, either the stuff marked old is the outer
7286 // rectangle or we don't have a situation where one
7287 // is contained in the other.
7289 if ( oldLeft
< leftCol
)
7291 // Refresh the newly selected or deselected
7292 // area to the left of the old or new selection.
7293 need_refresh
[0] = true;
7294 rect
[0] = BlockToDeviceRect(
7295 wxGridCellCoords( oldTop
, oldLeft
),
7296 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7299 if ( oldTop
< topRow
)
7301 // Refresh the newly selected or deselected
7302 // area above the old or new selection.
7303 need_refresh
[1] = true;
7304 rect
[1] = BlockToDeviceRect(
7305 wxGridCellCoords( oldTop
, leftCol
),
7306 wxGridCellCoords( topRow
- 1, rightCol
) );
7309 if ( oldRight
> rightCol
)
7311 // Refresh the newly selected or deselected
7312 // area to the right of the old or new selection.
7313 need_refresh
[2] = true;
7314 rect
[2] = BlockToDeviceRect(
7315 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7316 wxGridCellCoords( oldBottom
, oldRight
) );
7319 if ( oldBottom
> bottomRow
)
7321 // Refresh the newly selected or deselected
7322 // area below the old or new selection.
7323 need_refresh
[3] = true;
7324 rect
[3] = BlockToDeviceRect(
7325 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7326 wxGridCellCoords( oldBottom
, rightCol
) );
7329 // various Refresh() calls
7330 for (i
= 0; i
< 4; i
++ )
7331 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7332 m_gridWin
->Refresh( false, &(rect
[i
]) );
7336 m_selectingTopLeft
= updateTopLeft
;
7337 m_selectingBottomRight
= updateBottomRight
;
7341 // ------ functions to get/send data (see also public functions)
7344 bool wxGrid::GetModelValues()
7346 // Hide the editor, so it won't hide a changed value.
7347 HideCellEditControl();
7351 // all we need to do is repaint the grid
7353 m_gridWin
->Refresh();
7360 bool wxGrid::SetModelValues()
7364 // Disable the editor, so it won't hide a changed value.
7365 // Do we also want to save the current value of the editor first?
7367 DisableCellEditControl();
7371 for ( row
= 0; row
< m_numRows
; row
++ )
7373 for ( col
= 0; col
< m_numCols
; col
++ )
7375 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7385 // Note - this function only draws cells that are in the list of
7386 // exposed cells (usually set from the update region by
7387 // CalcExposedCells)
7389 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7391 if ( !m_numRows
|| !m_numCols
)
7394 int i
, numCells
= cells
.GetCount();
7395 int row
, col
, cell_rows
, cell_cols
;
7396 wxGridCellCoordsArray redrawCells
;
7398 for ( i
= numCells
- 1; i
>= 0; i
-- )
7400 row
= cells
[i
].GetRow();
7401 col
= cells
[i
].GetCol();
7402 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7404 // If this cell is part of a multicell block, find owner for repaint
7405 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7407 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7408 bool marked
= false;
7409 for ( int j
= 0; j
< numCells
; j
++ )
7411 if ( cell
== cells
[j
] )
7420 int count
= redrawCells
.GetCount();
7421 for (int j
= 0; j
< count
; j
++)
7423 if ( cell
== redrawCells
[j
] )
7431 redrawCells
.Add( cell
);
7434 // don't bother drawing this cell
7438 // If this cell is empty, find cell to left that might want to overflow
7439 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7441 for ( int l
= 0; l
< cell_rows
; l
++ )
7443 // find a cell in this row to leave already marked for repaint
7445 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7446 if ((redrawCells
[k
].GetCol() < left
) &&
7447 (redrawCells
[k
].GetRow() == row
))
7449 left
= redrawCells
[k
].GetCol();
7453 left
= 0; // oh well
7455 for (int j
= col
- 1; j
>= left
; j
--)
7457 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7459 if (GetCellOverflow(row
+ l
, j
))
7461 wxGridCellCoords
cell(row
+ l
, j
);
7462 bool marked
= false;
7464 for (int k
= 0; k
< numCells
; k
++)
7466 if ( cell
== cells
[k
] )
7475 int count
= redrawCells
.GetCount();
7476 for (int k
= 0; k
< count
; k
++)
7478 if ( cell
== redrawCells
[k
] )
7485 redrawCells
.Add( cell
);
7494 DrawCell( dc
, cells
[i
] );
7497 numCells
= redrawCells
.GetCount();
7499 for ( i
= numCells
- 1; i
>= 0; i
-- )
7501 DrawCell( dc
, redrawCells
[i
] );
7505 void wxGrid::DrawGridSpace( wxDC
& dc
)
7508 m_gridWin
->GetClientSize( &cw
, &ch
);
7511 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7513 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7514 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7516 if ( right
> rightCol
|| bottom
> bottomRow
)
7519 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7521 dc
.SetBrush(GetDefaultCellBackgroundColour());
7522 dc
.SetPen( *wxTRANSPARENT_PEN
);
7524 if ( right
> rightCol
)
7526 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7529 if ( bottom
> bottomRow
)
7531 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7536 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7538 int row
= coords
.GetRow();
7539 int col
= coords
.GetCol();
7541 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7544 // we draw the cell border ourselves
7545 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7547 bool isCurrent
= coords
== m_currentCellCoords
;
7549 wxRect rect
= CellToRect( row
, col
);
7551 // if the editor is shown, we should use it and not the renderer
7552 // Note: However, only if it is really _shown_, i.e. not hidden!
7553 if ( isCurrent
&& IsCellEditControlShown() )
7555 // NB: this "#if..." is temporary and fixes a problem where the
7556 // edit control is erased by this code after being rendered.
7557 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7558 // implicitly, causing this out-of order render.
7559 #if !defined(__WXMAC__)
7560 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7561 editor
->PaintBackground(rect
, attr
);
7567 // but all the rest is drawn by the cell renderer and hence may be customized
7568 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7569 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7576 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7578 // don't show highlight when the grid doesn't have focus
7582 int row
= m_currentCellCoords
.GetRow();
7583 int col
= m_currentCellCoords
.GetCol();
7585 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7588 wxRect rect
= CellToRect(row
, col
);
7590 // hmmm... what could we do here to show that the cell is disabled?
7591 // for now, I just draw a thinner border than for the other ones, but
7592 // it doesn't look really good
7594 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7598 // The center of the drawn line is where the position/width/height of
7599 // the rectangle is actually at (on wxMSW at least), so the
7600 // size of the rectangle is reduced to compensate for the thickness of
7601 // the line. If this is too strange on non-wxMSW platforms then
7602 // please #ifdef this appropriately.
7603 rect
.x
+= penWidth
/ 2;
7604 rect
.y
+= penWidth
/ 2;
7605 rect
.width
-= penWidth
- 1;
7606 rect
.height
-= penWidth
- 1;
7608 // Now draw the rectangle
7609 // use the cellHighlightColour if the cell is inside a selection, this
7610 // will ensure the cell is always visible.
7611 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
7612 : m_cellHighlightColour
,
7614 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7615 dc
.DrawRectangle(rect
);
7619 // VZ: my experiments with 3D borders...
7621 // how to properly set colours for arbitrary bg?
7622 wxCoord x1
= rect
.x
,
7624 x2
= rect
.x
+ rect
.width
- 1,
7625 y2
= rect
.y
+ rect
.height
- 1;
7627 dc
.SetPen(*wxWHITE_PEN
);
7628 dc
.DrawLine(x1
, y1
, x2
, y1
);
7629 dc
.DrawLine(x1
, y1
, x1
, y2
);
7631 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7632 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7634 dc
.SetPen(*wxBLACK_PEN
);
7635 dc
.DrawLine(x1
, y2
, x2
, y2
);
7636 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7640 wxPen
wxGrid::GetDefaultGridLinePen()
7642 return wxPen(GetGridLineColour());
7645 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7647 return GetDefaultGridLinePen();
7650 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7652 return GetDefaultGridLinePen();
7655 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7657 int row
= coords
.GetRow();
7658 int col
= coords
.GetCol();
7659 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7663 wxRect rect
= CellToRect( row
, col
);
7665 // right hand border
7666 dc
.SetPen( GetColGridLinePen(col
) );
7667 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7668 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7671 dc
.SetPen( GetRowGridLinePen(row
) );
7672 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7673 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7676 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7678 // This if block was previously in wxGrid::OnPaint but that doesn't
7679 // seem to get called under wxGTK - MB
7681 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7682 m_numRows
&& m_numCols
)
7684 m_currentCellCoords
.Set(0, 0);
7687 if ( IsCellEditControlShown() )
7689 // don't show highlight when the edit control is shown
7693 // if the active cell was repainted, repaint its highlight too because it
7694 // might have been damaged by the grid lines
7695 size_t count
= cells
.GetCount();
7696 for ( size_t n
= 0; n
< count
; n
++ )
7698 wxGridCellCoords cell
= cells
[n
];
7700 // If we are using attributes, then we may have just exposed another
7701 // cell in a partially-visible merged cluster of cells. If the "anchor"
7702 // (upper left) cell of this merged cluster is the cell indicated by
7703 // m_currentCellCoords, then we need to refresh the cell highlight even
7704 // though the "anchor" itself is not part of our update segment.
7705 if ( CanHaveAttributes() )
7709 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
7712 cell
.SetRow(cell
.GetRow() + rows
);
7715 cell
.SetCol(cell
.GetCol() + cols
);
7718 if ( cell
== m_currentCellCoords
)
7720 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7721 DrawCellHighlight(dc
, attr
);
7729 // This is used to redraw all grid lines e.g. when the grid line colour
7732 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7734 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7737 int top
, bottom
, left
, right
;
7740 m_gridWin
->GetClientSize(&cw
, &ch
);
7741 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7742 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7744 // avoid drawing grid lines past the last row and col
7746 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7747 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7749 // no gridlines inside multicells, clip them out
7750 int leftCol
= GetColPos( internalXToCol(left
) );
7751 int topRow
= internalYToRow(top
);
7752 int rightCol
= GetColPos( internalXToCol(right
) );
7753 int bottomRow
= internalYToRow(bottom
);
7755 wxRegion
clippedcells(0, 0, cw
, ch
);
7757 int cell_rows
, cell_cols
;
7760 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
7762 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
7764 int i
= GetColAt( colPos
);
7766 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7767 if ((cell_rows
> 1) || (cell_cols
> 1))
7769 rect
= CellToRect(j
,i
);
7770 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7771 clippedcells
.Subtract(rect
);
7773 else if ((cell_rows
< 0) || (cell_cols
< 0))
7775 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7776 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7777 clippedcells
.Subtract(rect
);
7782 dc
.SetDeviceClippingRegion( clippedcells
);
7785 // horizontal grid lines
7786 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7788 int bot
= GetRowBottom(i
) - 1;
7795 dc
.SetPen( GetRowGridLinePen(i
) );
7796 dc
.DrawLine( left
, bot
, right
, bot
);
7800 // vertical grid lines
7801 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7803 int i
= GetColAt( colPos
);
7805 int colRight
= GetColRight(i
);
7807 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7811 if ( colRight
> right
)
7814 if ( colRight
>= left
)
7816 dc
.SetPen( GetColGridLinePen(i
) );
7817 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7821 dc
.DestroyClippingRegion();
7824 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7829 const size_t numLabels
= rows
.GetCount();
7830 for ( size_t i
= 0; i
< numLabels
; i
++ )
7832 DrawRowLabel( dc
, rows
[i
] );
7836 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7838 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7843 int rowTop
= GetRowTop(row
),
7844 rowBottom
= GetRowBottom(row
) - 1;
7846 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
7847 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7848 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7849 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7851 dc
.SetPen( *wxWHITE_PEN
);
7852 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7853 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7855 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7856 dc
.SetTextForeground( GetLabelTextColour() );
7857 dc
.SetFont( GetLabelFont() );
7860 GetRowLabelAlignment( &hAlign
, &vAlign
);
7863 rect
.SetY( GetRowTop(row
) + 2 );
7864 rect
.SetWidth( m_rowLabelWidth
- 4 );
7865 rect
.SetHeight( GetRowHeight(row
) - 4 );
7866 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7869 void wxGrid::SetUseNativeColLabels( bool native
)
7871 m_nativeColumnLabels
= native
;
7874 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7875 SetColLabelSize( height
);
7878 m_colLabelWin
->Refresh();
7879 m_cornerLabelWin
->Refresh();
7882 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7887 const size_t numLabels
= cols
.GetCount();
7888 for ( size_t i
= 0; i
< numLabels
; i
++ )
7890 DrawColLabel( dc
, cols
[i
] );
7894 void wxGrid::DrawCornerLabel(wxDC
& dc
)
7896 if ( m_nativeColumnLabels
)
7898 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
7901 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
7905 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
7906 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
7907 m_rowLabelWidth
- 1, 0 );
7908 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
7909 0, m_colLabelHeight
- 1 );
7910 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
7911 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
7913 dc
.SetPen( *wxWHITE_PEN
);
7914 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
7915 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
7919 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
7921 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7924 int colLeft
= GetColLeft(col
);
7926 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
7928 if ( m_nativeColumnLabels
)
7930 wxRendererNative::Get().DrawHeaderButton(m_colLabelWin
, dc
, rect
, 0);
7934 int colRight
= GetColRight(col
) - 1;
7936 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
7937 dc
.DrawLine( colRight
, 0,
7938 colRight
, m_colLabelHeight
- 1 );
7939 dc
.DrawLine( colLeft
, 0,
7941 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7942 colRight
+ 1, m_colLabelHeight
- 1 );
7944 dc
.SetPen( *wxWHITE_PEN
);
7945 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7946 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7949 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7950 dc
.SetTextForeground( GetLabelTextColour() );
7951 dc
.SetFont( GetLabelFont() );
7954 GetColLabelAlignment( &hAlign
, &vAlign
);
7955 const int orient
= GetColLabelTextOrientation();
7958 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
7961 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
7962 // we just have to add textOrientation support
7963 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7964 const wxString
& value
,
7968 int textOrientation
)
7970 wxArrayString lines
;
7972 StringToLines( value
, lines
);
7974 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
7977 void wxGrid::DrawTextRectangle(wxDC
& dc
,
7978 const wxArrayString
& lines
,
7982 int textOrientation
)
7984 if ( lines
.empty() )
7987 wxDCClipper
clip(dc
, rect
);
7992 if ( textOrientation
== wxHORIZONTAL
)
7993 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
7995 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7999 switch ( vertAlign
)
8001 case wxALIGN_BOTTOM
:
8002 if ( textOrientation
== wxHORIZONTAL
)
8003 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8005 x
= rect
.x
+ rect
.width
- textWidth
;
8008 case wxALIGN_CENTRE
:
8009 if ( textOrientation
== wxHORIZONTAL
)
8010 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8012 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8017 if ( textOrientation
== wxHORIZONTAL
)
8024 // Align each line of a multi-line label
8025 size_t nLines
= lines
.GetCount();
8026 for ( size_t l
= 0; l
< nLines
; l
++ )
8028 const wxString
& line
= lines
[l
];
8032 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8036 wxCoord lineWidth
= 0,
8038 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8040 switch ( horizAlign
)
8043 if ( textOrientation
== wxHORIZONTAL
)
8044 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8046 y
= rect
.y
+ lineWidth
+ 1;
8049 case wxALIGN_CENTRE
:
8050 if ( textOrientation
== wxHORIZONTAL
)
8051 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8053 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8058 if ( textOrientation
== wxHORIZONTAL
)
8061 y
= rect
.y
+ rect
.height
- 1;
8065 if ( textOrientation
== wxHORIZONTAL
)
8067 dc
.DrawText( line
, x
, y
);
8072 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8078 // Split multi-line text up into an array of strings.
8079 // Any existing contents of the string array are preserved.
8081 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8082 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8086 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8087 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8089 while ( startPos
< (int)tVal
.length() )
8091 pos
= tVal
.Mid(startPos
).Find( eol
);
8096 else if ( pos
== 0 )
8098 lines
.Add( wxEmptyString
);
8102 lines
.Add( tVal
.Mid(startPos
, pos
) );
8105 startPos
+= pos
+ 1;
8108 if ( startPos
< (int)tVal
.length() )
8110 lines
.Add( tVal
.Mid( startPos
) );
8114 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8115 const wxArrayString
& lines
,
8116 long *width
, long *height
) const
8120 wxCoord lineW
= 0, lineH
= 0;
8123 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8125 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8126 w
= wxMax( w
, lineW
);
8135 // ------ Batch processing.
8137 void wxGrid::EndBatch()
8139 if ( m_batchCount
> 0 )
8142 if ( !m_batchCount
)
8145 m_rowLabelWin
->Refresh();
8146 m_colLabelWin
->Refresh();
8147 m_cornerLabelWin
->Refresh();
8148 m_gridWin
->Refresh();
8153 // Use this, rather than wxWindow::Refresh(), to force an immediate
8154 // repainting of the grid. Has no effect if you are already inside a
8155 // BeginBatch / EndBatch block.
8157 void wxGrid::ForceRefresh()
8163 bool wxGrid::Enable(bool enable
)
8165 if ( !wxScrolledWindow::Enable(enable
) )
8168 // redraw in the new state
8169 m_gridWin
->Refresh();
8175 // ------ Edit control functions
8178 void wxGrid::EnableEditing( bool edit
)
8180 // TODO: improve this ?
8182 if ( edit
!= m_editable
)
8185 EnableCellEditControl(edit
);
8190 void wxGrid::EnableCellEditControl( bool enable
)
8195 if ( enable
!= m_cellEditCtrlEnabled
)
8199 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8202 // this should be checked by the caller!
8203 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8205 // do it before ShowCellEditControl()
8206 m_cellEditCtrlEnabled
= enable
;
8208 ShowCellEditControl();
8212 //FIXME:add veto support
8213 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8215 HideCellEditControl();
8216 SaveEditControlValue();
8218 // do it after HideCellEditControl()
8219 m_cellEditCtrlEnabled
= enable
;
8224 bool wxGrid::IsCurrentCellReadOnly() const
8227 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8228 bool readonly
= attr
->IsReadOnly();
8234 bool wxGrid::CanEnableCellControl() const
8236 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8237 !IsCurrentCellReadOnly();
8240 bool wxGrid::IsCellEditControlEnabled() const
8242 // the cell edit control might be disable for all cells or just for the
8243 // current one if it's read only
8244 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8247 bool wxGrid::IsCellEditControlShown() const
8249 bool isShown
= false;
8251 if ( m_cellEditCtrlEnabled
)
8253 int row
= m_currentCellCoords
.GetRow();
8254 int col
= m_currentCellCoords
.GetCol();
8255 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8256 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8261 if ( editor
->IsCreated() )
8263 isShown
= editor
->GetControl()->IsShown();
8273 void wxGrid::ShowCellEditControl()
8275 if ( IsCellEditControlEnabled() )
8277 if ( !IsVisible( m_currentCellCoords
, false ) )
8279 m_cellEditCtrlEnabled
= false;
8284 wxRect rect
= CellToRect( m_currentCellCoords
);
8285 int row
= m_currentCellCoords
.GetRow();
8286 int col
= m_currentCellCoords
.GetCol();
8288 // if this is part of a multicell, find owner (topleft)
8289 int cell_rows
, cell_cols
;
8290 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8291 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8295 m_currentCellCoords
.SetRow( row
);
8296 m_currentCellCoords
.SetCol( col
);
8299 // erase the highlight and the cell contents because the editor
8300 // might not cover the entire cell
8301 wxClientDC
dc( m_gridWin
);
8303 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8304 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
8305 dc
.SetPen(*wxTRANSPARENT_PEN
);
8306 dc
.DrawRectangle(rect
);
8308 // convert to scrolled coords
8309 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8315 // cell is shifted by one pixel
8316 // However, don't allow x or y to become negative
8317 // since the SetSize() method interprets that as
8324 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8325 if ( !editor
->IsCreated() )
8327 editor
->Create(m_gridWin
, wxID_ANY
,
8328 new wxGridCellEditorEvtHandler(this, editor
));
8330 wxGridEditorCreatedEvent
evt(GetId(),
8331 wxEVT_GRID_EDITOR_CREATED
,
8335 editor
->GetControl());
8336 GetEventHandler()->ProcessEvent(evt
);
8339 // resize editor to overflow into righthand cells if allowed
8340 int maxWidth
= rect
.width
;
8341 wxString value
= GetCellValue(row
, col
);
8342 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8345 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8346 if (maxWidth
< rect
.width
)
8347 maxWidth
= rect
.width
;
8350 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8351 if (rect
.x
+ maxWidth
> client_right
)
8352 maxWidth
= client_right
- rect
.x
;
8354 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8356 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8357 // may have changed earlier
8358 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8361 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8363 // looks weird going over a multicell
8364 if (m_table
->IsEmptyCell( row
, i
) &&
8365 (rect
.width
< maxWidth
) && (c_rows
== 1))
8367 rect
.width
+= GetColWidth( i
);
8373 if (rect
.GetRight() > client_right
)
8374 rect
.SetRight( client_right
- 1 );
8377 editor
->SetCellAttr( attr
);
8378 editor
->SetSize( rect
);
8380 editor
->GetControl()->Move(
8381 editor
->GetControl()->GetPosition().x
+ nXMove
,
8382 editor
->GetControl()->GetPosition().y
);
8383 editor
->Show( true, attr
);
8385 // recalc dimensions in case we need to
8386 // expand the scrolled window to account for editor
8389 editor
->BeginEdit(row
, col
, this);
8390 editor
->SetCellAttr(NULL
);
8398 void wxGrid::HideCellEditControl()
8400 if ( IsCellEditControlEnabled() )
8402 int row
= m_currentCellCoords
.GetRow();
8403 int col
= m_currentCellCoords
.GetCol();
8405 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8406 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8407 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
8408 editor
->Show( false );
8412 // return the focus to the grid itself if the editor had it
8414 // note that we must not do this unconditionally to avoid stealing
8415 // focus from the window which just received it if we are hiding the
8416 // editor precisely because we lost focus
8417 if ( editorHadFocus
)
8418 m_gridWin
->SetFocus();
8420 // refresh whole row to the right
8421 wxRect
rect( CellToRect(row
, col
) );
8422 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8423 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8426 // ensure that the pixels under the focus ring get refreshed as well
8427 rect
.Inflate(10, 10);
8430 m_gridWin
->Refresh( false, &rect
);
8434 void wxGrid::SaveEditControlValue()
8436 if ( IsCellEditControlEnabled() )
8438 int row
= m_currentCellCoords
.GetRow();
8439 int col
= m_currentCellCoords
.GetCol();
8441 wxString oldval
= GetCellValue(row
, col
);
8443 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8444 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8445 bool changed
= editor
->EndEdit(row
, col
, this);
8452 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8453 m_currentCellCoords
.GetRow(),
8454 m_currentCellCoords
.GetCol() ) < 0 )
8456 // Event has been vetoed, set the data back.
8457 SetCellValue(row
, col
, oldval
);
8464 // ------ Grid location functions
8465 // Note that all of these functions work with the logical coordinates of
8466 // grid cells and labels so you will need to convert from device
8467 // coordinates for mouse events etc.
8470 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8472 int row
= YToRow(y
);
8473 int col
= XToCol(x
);
8475 if ( row
== -1 || col
== -1 )
8477 coords
= wxGridNoCellCoords
;
8481 coords
.Set( row
, col
);
8485 // Internal Helper function for computing row or column from some
8486 // (unscrolled) coordinate value, using either
8487 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8488 // of m_rowBottoms/m_ColRights to speed up the search!
8490 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8491 const wxArrayInt
& BorderArray
, int nMax
,
8495 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8500 size_t i_max
= coord
/ defaultDist
,
8503 if (BorderArray
.IsEmpty())
8505 if ((int) i_max
< nMax
)
8507 return clipToMinMax
? nMax
- 1 : -1;
8510 if ( i_max
>= BorderArray
.GetCount())
8512 i_max
= BorderArray
.GetCount() - 1;
8516 if ( coord
>= BorderArray
[i_max
])
8520 i_max
= coord
/ minDist
;
8522 i_max
= BorderArray
.GetCount() - 1;
8525 if ( i_max
>= BorderArray
.GetCount())
8526 i_max
= BorderArray
.GetCount() - 1;
8529 if ( coord
>= BorderArray
[i_max
])
8530 return clipToMinMax
? (int)i_max
: -1;
8531 if ( coord
< BorderArray
[0] )
8534 while ( i_max
- i_min
> 0 )
8536 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8537 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8538 if (coord
>= BorderArray
[ i_max
- 1])
8542 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8543 if (coord
< BorderArray
[median
])
8552 int wxGrid::YToRow( int y
) const
8554 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8555 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8558 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8561 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8563 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8565 int maxPos
= x
/ m_defaultColWidth
;
8568 if (m_colRights
.IsEmpty())
8570 if(maxPos
< m_numCols
)
8571 return GetColAt( maxPos
);
8572 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8575 if ( maxPos
>= m_numCols
)
8576 maxPos
= m_numCols
- 1;
8579 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8582 if (m_minAcceptableColWidth
)
8583 maxPos
= x
/ m_minAcceptableColWidth
;
8585 maxPos
= m_numCols
- 1;
8587 if ( maxPos
>= m_numCols
)
8588 maxPos
= m_numCols
- 1;
8591 //X is beyond the last column
8592 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8593 return clipToMinMax
? GetColAt( maxPos
) : -1;
8595 //X is before the first column
8596 if ( x
< m_colRights
[GetColAt( 0 )] )
8597 return GetColAt( 0 );
8599 //Perform a binary search
8600 while ( maxPos
- minPos
> 0 )
8602 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8603 0, _T("wxGrid: internal error in XToCol"));
8605 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8606 return GetColAt( maxPos
);
8609 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8610 if (x
< m_colRights
[GetColAt( median
)])
8615 return GetColAt( maxPos
);
8618 // return the row number that that the y coord is near
8619 // the edge of, or -1 if not near an edge.
8620 // coords can only possibly be near an edge if
8621 // (a) the row/column is large enough to still allow for an "inner" area
8622 // that is _not_ nead the edge (i.e., if the height/width is smaller
8623 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8626 // (b) resizing rows/columns (the thing for which edge detection is
8627 // relevant at all) is enabled.
8629 int wxGrid::YToEdgeOfRow( int y
) const
8632 i
= internalYToRow(y
);
8634 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8636 // We know that we are in row i, test whether we are
8637 // close enough to lower or upper border, respectively.
8638 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8640 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8647 // return the col number that that the x coord is near the edge of, or
8648 // -1 if not near an edge
8649 // See comment at YToEdgeOfRow for conditions on edge detection.
8651 int wxGrid::XToEdgeOfCol( int x
) const
8654 i
= internalXToCol(x
);
8656 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8658 // We know that we are in column i; test whether we are
8659 // close enough to right or left border, respectively.
8660 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8662 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8669 wxRect
wxGrid::CellToRect( int row
, int col
) const
8671 wxRect
rect( -1, -1, -1, -1 );
8673 if ( row
>= 0 && row
< m_numRows
&&
8674 col
>= 0 && col
< m_numCols
)
8676 int i
, cell_rows
, cell_cols
;
8677 rect
.width
= rect
.height
= 0;
8678 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8679 // if negative then find multicell owner
8684 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8686 rect
.x
= GetColLeft(col
);
8687 rect
.y
= GetRowTop(row
);
8688 for (i
=col
; i
< col
+ cell_cols
; i
++)
8689 rect
.width
+= GetColWidth(i
);
8690 for (i
=row
; i
< row
+ cell_rows
; i
++)
8691 rect
.height
+= GetRowHeight(i
);
8694 // if grid lines are enabled, then the area of the cell is a bit smaller
8695 if (m_gridLinesEnabled
)
8704 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8706 // get the cell rectangle in logical coords
8708 wxRect
r( CellToRect( row
, col
) );
8710 // convert to device coords
8712 int left
, top
, right
, bottom
;
8713 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8714 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8716 // check against the client area of the grid window
8718 m_gridWin
->GetClientSize( &cw
, &ch
);
8720 if ( wholeCellVisible
)
8722 // is the cell wholly visible ?
8723 return ( left
>= 0 && right
<= cw
&&
8724 top
>= 0 && bottom
<= ch
);
8728 // is the cell partly visible ?
8730 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8731 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8735 // make the specified cell location visible by doing a minimal amount
8738 void wxGrid::MakeCellVisible( int row
, int col
)
8741 int xpos
= -1, ypos
= -1;
8743 if ( row
>= 0 && row
< m_numRows
&&
8744 col
>= 0 && col
< m_numCols
)
8746 // get the cell rectangle in logical coords
8747 wxRect
r( CellToRect( row
, col
) );
8749 // convert to device coords
8750 int left
, top
, right
, bottom
;
8751 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8752 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8755 m_gridWin
->GetClientSize( &cw
, &ch
);
8761 else if ( bottom
> ch
)
8763 int h
= r
.GetHeight();
8765 for ( i
= row
- 1; i
>= 0; i
-- )
8767 int rowHeight
= GetRowHeight(i
);
8768 if ( h
+ rowHeight
> ch
)
8775 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8776 // have rounding errors (this is important, because if we do,
8777 // we might not scroll at all and some cells won't be redrawn)
8779 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8780 // so just add a full scroll unit...
8781 ypos
+= m_scrollLineY
;
8784 // special handling for wide cells - show always left part of the cell!
8785 // Otherwise, e.g. when stepping from row to row, it would jump between
8786 // left and right part of the cell on every step!
8788 if ( left
< 0 || (right
- left
) >= cw
)
8792 else if ( right
> cw
)
8794 // position the view so that the cell is on the right
8796 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8797 xpos
= x0
+ (right
- cw
);
8799 // see comment for ypos above
8800 xpos
+= m_scrollLineX
;
8803 if ( xpos
!= -1 || ypos
!= -1 )
8806 xpos
/= m_scrollLineX
;
8808 ypos
/= m_scrollLineY
;
8809 Scroll( xpos
, ypos
);
8816 // ------ Grid cursor movement functions
8819 bool wxGrid::MoveCursorUp( bool expandSelection
)
8821 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8822 m_currentCellCoords
.GetRow() >= 0 )
8824 if ( expandSelection
)
8826 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8827 m_selectingKeyboard
= m_currentCellCoords
;
8828 if ( m_selectingKeyboard
.GetRow() > 0 )
8830 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8831 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8832 m_selectingKeyboard
.GetCol() );
8833 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8836 else if ( m_currentCellCoords
.GetRow() > 0 )
8838 int row
= m_currentCellCoords
.GetRow() - 1;
8839 int col
= m_currentCellCoords
.GetCol();
8841 MakeCellVisible( row
, col
);
8842 SetCurrentCell( row
, col
);
8853 bool wxGrid::MoveCursorDown( bool expandSelection
)
8855 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8856 m_currentCellCoords
.GetRow() < m_numRows
)
8858 if ( expandSelection
)
8860 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8861 m_selectingKeyboard
= m_currentCellCoords
;
8862 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8864 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8865 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8866 m_selectingKeyboard
.GetCol() );
8867 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8870 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8872 int row
= m_currentCellCoords
.GetRow() + 1;
8873 int col
= m_currentCellCoords
.GetCol();
8875 MakeCellVisible( row
, col
);
8876 SetCurrentCell( row
, col
);
8887 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8889 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8890 m_currentCellCoords
.GetCol() >= 0 )
8892 if ( expandSelection
)
8894 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8895 m_selectingKeyboard
= m_currentCellCoords
;
8896 if ( m_selectingKeyboard
.GetCol() > 0 )
8898 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8899 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8900 m_selectingKeyboard
.GetCol() );
8901 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8904 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8906 int row
= m_currentCellCoords
.GetRow();
8907 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8910 MakeCellVisible( row
, col
);
8911 SetCurrentCell( row
, col
);
8922 bool wxGrid::MoveCursorRight( bool expandSelection
)
8924 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8925 m_currentCellCoords
.GetCol() < m_numCols
)
8927 if ( expandSelection
)
8929 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8930 m_selectingKeyboard
= m_currentCellCoords
;
8931 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8933 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8934 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8935 m_selectingKeyboard
.GetCol() );
8936 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8939 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8941 int row
= m_currentCellCoords
.GetRow();
8942 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8945 MakeCellVisible( row
, col
);
8946 SetCurrentCell( row
, col
);
8957 bool wxGrid::MovePageUp()
8959 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8962 int row
= m_currentCellCoords
.GetRow();
8966 m_gridWin
->GetClientSize( &cw
, &ch
);
8968 int y
= GetRowTop(row
);
8969 int newRow
= internalYToRow( y
- ch
+ 1 );
8971 if ( newRow
== row
)
8973 // row > 0, so newRow can never be less than 0 here.
8977 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8978 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8986 bool wxGrid::MovePageDown()
8988 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8991 int row
= m_currentCellCoords
.GetRow();
8992 if ( (row
+ 1) < m_numRows
)
8995 m_gridWin
->GetClientSize( &cw
, &ch
);
8997 int y
= GetRowTop(row
);
8998 int newRow
= internalYToRow( y
+ ch
);
8999 if ( newRow
== row
)
9001 // row < m_numRows, so newRow can't overflow here.
9005 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9006 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9014 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9017 m_currentCellCoords
!= wxGridNoCellCoords
&&
9018 m_currentCellCoords
.GetRow() > 0 )
9020 int row
= m_currentCellCoords
.GetRow();
9021 int col
= m_currentCellCoords
.GetCol();
9023 if ( m_table
->IsEmptyCell(row
, col
) )
9025 // starting in an empty cell: find the next block of
9031 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9035 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9037 // starting at the top of a block: find the next block
9043 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9049 // starting within a block: find the top of the block
9054 if ( m_table
->IsEmptyCell(row
, col
) )
9062 MakeCellVisible( row
, col
);
9063 if ( expandSelection
)
9065 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9066 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9071 SetCurrentCell( row
, col
);
9080 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9083 m_currentCellCoords
!= wxGridNoCellCoords
&&
9084 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9086 int row
= m_currentCellCoords
.GetRow();
9087 int col
= m_currentCellCoords
.GetCol();
9089 if ( m_table
->IsEmptyCell(row
, col
) )
9091 // starting in an empty cell: find the next block of
9094 while ( row
< m_numRows
- 1 )
9097 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9101 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9103 // starting at the bottom of a block: find the next block
9106 while ( row
< m_numRows
- 1 )
9109 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9115 // starting within a block: find the bottom of the block
9117 while ( row
< m_numRows
- 1 )
9120 if ( m_table
->IsEmptyCell(row
, col
) )
9128 MakeCellVisible( row
, col
);
9129 if ( expandSelection
)
9131 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9132 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9137 SetCurrentCell( row
, col
);
9146 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9149 m_currentCellCoords
!= wxGridNoCellCoords
&&
9150 m_currentCellCoords
.GetCol() > 0 )
9152 int row
= m_currentCellCoords
.GetRow();
9153 int col
= m_currentCellCoords
.GetCol();
9155 if ( m_table
->IsEmptyCell(row
, col
) )
9157 // starting in an empty cell: find the next block of
9163 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9167 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9169 // starting at the left of a block: find the next block
9175 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9181 // starting within a block: find the left of the block
9186 if ( m_table
->IsEmptyCell(row
, col
) )
9194 MakeCellVisible( row
, col
);
9195 if ( expandSelection
)
9197 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9198 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9203 SetCurrentCell( row
, col
);
9212 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9215 m_currentCellCoords
!= wxGridNoCellCoords
&&
9216 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9218 int row
= m_currentCellCoords
.GetRow();
9219 int col
= m_currentCellCoords
.GetCol();
9221 if ( m_table
->IsEmptyCell(row
, col
) )
9223 // starting in an empty cell: find the next block of
9226 while ( col
< m_numCols
- 1 )
9229 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9233 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9235 // starting at the right of a block: find the next block
9238 while ( col
< m_numCols
- 1 )
9241 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9247 // starting within a block: find the right of the block
9249 while ( col
< m_numCols
- 1 )
9252 if ( m_table
->IsEmptyCell(row
, col
) )
9260 MakeCellVisible( row
, col
);
9261 if ( expandSelection
)
9263 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9264 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9269 SetCurrentCell( row
, col
);
9279 // ------ Label values and formatting
9282 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9285 *horiz
= m_rowLabelHorizAlign
;
9287 *vert
= m_rowLabelVertAlign
;
9290 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9293 *horiz
= m_colLabelHorizAlign
;
9295 *vert
= m_colLabelVertAlign
;
9298 int wxGrid::GetColLabelTextOrientation() const
9300 return m_colLabelTextOrientation
;
9303 wxString
wxGrid::GetRowLabelValue( int row
) const
9307 return m_table
->GetRowLabelValue( row
);
9317 wxString
wxGrid::GetColLabelValue( int col
) const
9321 return m_table
->GetColLabelValue( col
);
9331 void wxGrid::SetRowLabelSize( int width
)
9333 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9335 if ( width
== wxGRID_AUTOSIZE
)
9337 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9340 if ( width
!= m_rowLabelWidth
)
9344 m_rowLabelWin
->Show( false );
9345 m_cornerLabelWin
->Show( false );
9347 else if ( m_rowLabelWidth
== 0 )
9349 m_rowLabelWin
->Show( true );
9350 if ( m_colLabelHeight
> 0 )
9351 m_cornerLabelWin
->Show( true );
9354 m_rowLabelWidth
= width
;
9356 wxScrolledWindow::Refresh( true );
9360 void wxGrid::SetColLabelSize( int height
)
9362 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9364 if ( height
== wxGRID_AUTOSIZE
)
9366 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9369 if ( height
!= m_colLabelHeight
)
9373 m_colLabelWin
->Show( false );
9374 m_cornerLabelWin
->Show( false );
9376 else if ( m_colLabelHeight
== 0 )
9378 m_colLabelWin
->Show( true );
9379 if ( m_rowLabelWidth
> 0 )
9380 m_cornerLabelWin
->Show( true );
9383 m_colLabelHeight
= height
;
9385 wxScrolledWindow::Refresh( true );
9389 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9391 if ( m_labelBackgroundColour
!= colour
)
9393 m_labelBackgroundColour
= colour
;
9394 m_rowLabelWin
->SetBackgroundColour( colour
);
9395 m_colLabelWin
->SetBackgroundColour( colour
);
9396 m_cornerLabelWin
->SetBackgroundColour( colour
);
9398 if ( !GetBatchCount() )
9400 m_rowLabelWin
->Refresh();
9401 m_colLabelWin
->Refresh();
9402 m_cornerLabelWin
->Refresh();
9407 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9409 if ( m_labelTextColour
!= colour
)
9411 m_labelTextColour
= colour
;
9412 if ( !GetBatchCount() )
9414 m_rowLabelWin
->Refresh();
9415 m_colLabelWin
->Refresh();
9420 void wxGrid::SetLabelFont( const wxFont
& font
)
9423 if ( !GetBatchCount() )
9425 m_rowLabelWin
->Refresh();
9426 m_colLabelWin
->Refresh();
9430 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9432 // allow old (incorrect) defs to be used
9435 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9436 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9437 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9442 case wxTOP
: vert
= wxALIGN_TOP
; break;
9443 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9444 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9447 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9449 m_rowLabelHorizAlign
= horiz
;
9452 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9454 m_rowLabelVertAlign
= vert
;
9457 if ( !GetBatchCount() )
9459 m_rowLabelWin
->Refresh();
9463 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9465 // allow old (incorrect) defs to be used
9468 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9469 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9470 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9475 case wxTOP
: vert
= wxALIGN_TOP
; break;
9476 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9477 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9480 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9482 m_colLabelHorizAlign
= horiz
;
9485 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9487 m_colLabelVertAlign
= vert
;
9490 if ( !GetBatchCount() )
9492 m_colLabelWin
->Refresh();
9496 // Note: under MSW, the default column label font must be changed because it
9497 // does not support vertical printing
9499 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9500 // pGrid->SetLabelFont(font);
9501 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9503 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9505 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9506 m_colLabelTextOrientation
= textOrientation
;
9508 if ( !GetBatchCount() )
9509 m_colLabelWin
->Refresh();
9512 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9516 m_table
->SetRowLabelValue( row
, s
);
9517 if ( !GetBatchCount() )
9519 wxRect rect
= CellToRect( row
, 0 );
9520 if ( rect
.height
> 0 )
9522 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9524 rect
.width
= m_rowLabelWidth
;
9525 m_rowLabelWin
->Refresh( true, &rect
);
9531 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9535 m_table
->SetColLabelValue( col
, s
);
9536 if ( !GetBatchCount() )
9538 wxRect rect
= CellToRect( 0, col
);
9539 if ( rect
.width
> 0 )
9541 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9543 rect
.height
= m_colLabelHeight
;
9544 m_colLabelWin
->Refresh( true, &rect
);
9550 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9552 if ( m_gridLineColour
!= colour
)
9554 m_gridLineColour
= colour
;
9556 wxClientDC
dc( m_gridWin
);
9558 DrawAllGridLines( dc
, wxRegion() );
9562 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9564 if ( m_cellHighlightColour
!= colour
)
9566 m_cellHighlightColour
= colour
;
9568 wxClientDC
dc( m_gridWin
);
9570 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9571 DrawCellHighlight(dc
, attr
);
9576 void wxGrid::SetCellHighlightPenWidth(int width
)
9578 if (m_cellHighlightPenWidth
!= width
)
9580 m_cellHighlightPenWidth
= width
;
9582 // Just redrawing the cell highlight is not enough since that won't
9583 // make any visible change if the the thickness is getting smaller.
9584 int row
= m_currentCellCoords
.GetRow();
9585 int col
= m_currentCellCoords
.GetCol();
9586 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9589 wxRect rect
= CellToRect(row
, col
);
9590 m_gridWin
->Refresh(true, &rect
);
9594 void wxGrid::SetCellHighlightROPenWidth(int width
)
9596 if (m_cellHighlightROPenWidth
!= width
)
9598 m_cellHighlightROPenWidth
= width
;
9600 // Just redrawing the cell highlight is not enough since that won't
9601 // make any visible change if the the thickness is getting smaller.
9602 int row
= m_currentCellCoords
.GetRow();
9603 int col
= m_currentCellCoords
.GetCol();
9604 if ( row
== -1 || col
== -1 ||
9605 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9608 wxRect rect
= CellToRect(row
, col
);
9609 m_gridWin
->Refresh(true, &rect
);
9613 void wxGrid::EnableGridLines( bool enable
)
9615 if ( enable
!= m_gridLinesEnabled
)
9617 m_gridLinesEnabled
= enable
;
9619 if ( !GetBatchCount() )
9623 wxClientDC
dc( m_gridWin
);
9625 DrawAllGridLines( dc
, wxRegion() );
9629 m_gridWin
->Refresh();
9635 int wxGrid::GetDefaultRowSize() const
9637 return m_defaultRowHeight
;
9640 int wxGrid::GetRowSize( int row
) const
9642 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9644 return GetRowHeight(row
);
9647 int wxGrid::GetDefaultColSize() const
9649 return m_defaultColWidth
;
9652 int wxGrid::GetColSize( int col
) const
9654 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9656 return GetColWidth(col
);
9659 // ============================================================================
9660 // access to the grid attributes: each of them has a default value in the grid
9661 // itself and may be overidden on a per-cell basis
9662 // ============================================================================
9664 // ----------------------------------------------------------------------------
9665 // setting default attributes
9666 // ----------------------------------------------------------------------------
9668 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9670 m_defaultCellAttr
->SetBackgroundColour(col
);
9672 m_gridWin
->SetBackgroundColour(col
);
9676 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9678 m_defaultCellAttr
->SetTextColour(col
);
9681 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9683 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9686 void wxGrid::SetDefaultCellOverflow( bool allow
)
9688 m_defaultCellAttr
->SetOverflow(allow
);
9691 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9693 m_defaultCellAttr
->SetFont(font
);
9696 // For editors and renderers the type registry takes precedence over the
9697 // default attr, so we need to register the new editor/renderer for the string
9698 // data type in order to make setting a default editor/renderer appear to
9701 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9703 RegisterDataType(wxGRID_VALUE_STRING
,
9705 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9708 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9710 RegisterDataType(wxGRID_VALUE_STRING
,
9711 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9715 // ----------------------------------------------------------------------------
9716 // access to the default attributes
9717 // ----------------------------------------------------------------------------
9719 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9721 return m_defaultCellAttr
->GetBackgroundColour();
9724 wxColour
wxGrid::GetDefaultCellTextColour() const
9726 return m_defaultCellAttr
->GetTextColour();
9729 wxFont
wxGrid::GetDefaultCellFont() const
9731 return m_defaultCellAttr
->GetFont();
9734 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9736 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9739 bool wxGrid::GetDefaultCellOverflow() const
9741 return m_defaultCellAttr
->GetOverflow();
9744 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9746 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9749 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9751 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9754 // ----------------------------------------------------------------------------
9755 // access to cell attributes
9756 // ----------------------------------------------------------------------------
9758 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9760 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9761 wxColour colour
= attr
->GetBackgroundColour();
9767 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9769 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9770 wxColour colour
= attr
->GetTextColour();
9776 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9778 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9779 wxFont font
= attr
->GetFont();
9785 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9787 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9788 attr
->GetAlignment(horiz
, vert
);
9792 bool wxGrid::GetCellOverflow( int row
, int col
) const
9794 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9795 bool allow
= attr
->GetOverflow();
9801 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9803 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9804 attr
->GetSize( num_rows
, num_cols
);
9808 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9810 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9811 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9817 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9819 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9820 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9826 bool wxGrid::IsReadOnly(int row
, int col
) const
9828 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9829 bool isReadOnly
= attr
->IsReadOnly();
9835 // ----------------------------------------------------------------------------
9836 // attribute support: cache, automatic provider creation, ...
9837 // ----------------------------------------------------------------------------
9839 bool wxGrid::CanHaveAttributes() const
9846 return m_table
->CanHaveAttributes();
9849 void wxGrid::ClearAttrCache()
9851 if ( m_attrCache
.row
!= -1 )
9853 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
9854 m_attrCache
.attr
= NULL
;
9855 m_attrCache
.row
= -1;
9856 // wxSafeDecRec(...) might cause event processing that accesses
9857 // the cached attribute, if one exists (e.g. by deleting the
9858 // editor stored within the attribute). Therefore it is important
9859 // to invalidate the cache before calling wxSafeDecRef!
9860 wxSafeDecRef(oldAttr
);
9864 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9868 wxGrid
*self
= (wxGrid
*)this; // const_cast
9870 self
->ClearAttrCache();
9871 self
->m_attrCache
.row
= row
;
9872 self
->m_attrCache
.col
= col
;
9873 self
->m_attrCache
.attr
= attr
;
9878 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9880 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9882 *attr
= m_attrCache
.attr
;
9883 wxSafeIncRef(m_attrCache
.attr
);
9885 #ifdef DEBUG_ATTR_CACHE
9886 gs_nAttrCacheHits
++;
9893 #ifdef DEBUG_ATTR_CACHE
9894 gs_nAttrCacheMisses
++;
9901 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9903 wxGridCellAttr
*attr
= NULL
;
9904 // Additional test to avoid looking at the cache e.g. for
9905 // wxNoCellCoords, as this will confuse memory management.
9908 if ( !LookupAttr(row
, col
, &attr
) )
9910 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9911 : (wxGridCellAttr
*)NULL
;
9912 CacheAttr(row
, col
, attr
);
9918 attr
->SetDefAttr(m_defaultCellAttr
);
9922 attr
= m_defaultCellAttr
;
9929 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9931 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9932 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9934 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9935 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9937 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9940 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9942 // artificially inc the ref count to match DecRef() in caller
9944 m_table
->SetAttr(attr
, row
, col
);
9950 // ----------------------------------------------------------------------------
9951 // setting column attributes (wrappers around SetColAttr)
9952 // ----------------------------------------------------------------------------
9954 void wxGrid::SetColFormatBool(int col
)
9956 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9959 void wxGrid::SetColFormatNumber(int col
)
9961 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9964 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9966 wxString typeName
= wxGRID_VALUE_FLOAT
;
9967 if ( (width
!= -1) || (precision
!= -1) )
9969 typeName
<< _T(':') << width
<< _T(',') << precision
;
9972 SetColFormatCustom(col
, typeName
);
9975 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9977 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9979 attr
= new wxGridCellAttr
;
9980 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9981 attr
->SetRenderer(renderer
);
9982 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
9983 attr
->SetEditor(editor
);
9985 SetColAttr(col
, attr
);
9989 // ----------------------------------------------------------------------------
9990 // setting cell attributes: this is forwarded to the table
9991 // ----------------------------------------------------------------------------
9993 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9995 if ( CanHaveAttributes() )
9997 m_table
->SetAttr(attr
, row
, col
);
10002 wxSafeDecRef(attr
);
10006 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10008 if ( CanHaveAttributes() )
10010 m_table
->SetRowAttr(attr
, row
);
10015 wxSafeDecRef(attr
);
10019 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10021 if ( CanHaveAttributes() )
10023 m_table
->SetColAttr(attr
, col
);
10028 wxSafeDecRef(attr
);
10032 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10034 if ( CanHaveAttributes() )
10036 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10037 attr
->SetBackgroundColour(colour
);
10042 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10044 if ( CanHaveAttributes() )
10046 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10047 attr
->SetTextColour(colour
);
10052 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10054 if ( CanHaveAttributes() )
10056 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10057 attr
->SetFont(font
);
10062 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10064 if ( CanHaveAttributes() )
10066 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10067 attr
->SetAlignment(horiz
, vert
);
10072 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10074 if ( CanHaveAttributes() )
10076 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10077 attr
->SetOverflow(allow
);
10082 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10084 if ( CanHaveAttributes() )
10086 int cell_rows
, cell_cols
;
10088 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10089 attr
->GetSize(&cell_rows
, &cell_cols
);
10090 attr
->SetSize(num_rows
, num_cols
);
10093 // Cannot set the size of a cell to 0 or negative values
10094 // While it is perfectly legal to do that, this function cannot
10095 // handle all the possibilies, do it by hand by getting the CellAttr.
10096 // You can only set the size of a cell to 1,1 or greater with this fn
10097 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10098 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10099 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10100 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10102 // if this was already a multicell then "turn off" the other cells first
10103 if ((cell_rows
> 1) || (cell_cols
> 1))
10106 for (j
=row
; j
< row
+ cell_rows
; j
++)
10108 for (i
=col
; i
< col
+ cell_cols
; i
++)
10110 if ((i
!= col
) || (j
!= row
))
10112 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10113 attr_stub
->SetSize( 1, 1 );
10114 attr_stub
->DecRef();
10120 // mark the cells that will be covered by this cell to
10121 // negative or zero values to point back at this cell
10122 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10125 for (j
=row
; j
< row
+ num_rows
; j
++)
10127 for (i
=col
; i
< col
+ num_cols
; i
++)
10129 if ((i
!= col
) || (j
!= row
))
10131 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10132 attr_stub
->SetSize( row
- j
, col
- i
);
10133 attr_stub
->DecRef();
10141 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10143 if ( CanHaveAttributes() )
10145 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10146 attr
->SetRenderer(renderer
);
10151 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10153 if ( CanHaveAttributes() )
10155 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10156 attr
->SetEditor(editor
);
10161 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10163 if ( CanHaveAttributes() )
10165 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10166 attr
->SetReadOnly(isReadOnly
);
10171 // ----------------------------------------------------------------------------
10172 // Data type registration
10173 // ----------------------------------------------------------------------------
10175 void wxGrid::RegisterDataType(const wxString
& typeName
,
10176 wxGridCellRenderer
* renderer
,
10177 wxGridCellEditor
* editor
)
10179 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10183 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10185 wxString typeName
= m_table
->GetTypeName(row
, col
);
10186 return GetDefaultEditorForType(typeName
);
10189 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10191 wxString typeName
= m_table
->GetTypeName(row
, col
);
10192 return GetDefaultRendererForType(typeName
);
10195 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10197 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10198 if ( index
== wxNOT_FOUND
)
10200 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10205 return m_typeRegistry
->GetEditor(index
);
10208 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10210 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10211 if ( index
== wxNOT_FOUND
)
10213 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10218 return m_typeRegistry
->GetRenderer(index
);
10221 // ----------------------------------------------------------------------------
10223 // ----------------------------------------------------------------------------
10225 void wxGrid::EnableDragRowSize( bool enable
)
10227 m_canDragRowSize
= enable
;
10230 void wxGrid::EnableDragColSize( bool enable
)
10232 m_canDragColSize
= enable
;
10235 void wxGrid::EnableDragGridSize( bool enable
)
10237 m_canDragGridSize
= enable
;
10240 void wxGrid::EnableDragCell( bool enable
)
10242 m_canDragCell
= enable
;
10245 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10247 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10249 if ( resizeExistingRows
)
10251 // since we are resizing all rows to the default row size,
10252 // we can simply clear the row heights and row bottoms
10253 // arrays (which also allows us to take advantage of
10254 // some speed optimisations)
10255 m_rowHeights
.Empty();
10256 m_rowBottoms
.Empty();
10257 if ( !GetBatchCount() )
10262 void wxGrid::SetRowSize( int row
, int height
)
10264 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10266 // if < 0 then calculate new height from label
10270 wxArrayString lines
;
10271 wxClientDC
dc(m_rowLabelWin
);
10272 dc
.SetFont(GetLabelFont());
10273 StringToLines(GetRowLabelValue( row
), lines
);
10274 GetTextBoxSize( dc
, lines
, &w
, &h
);
10275 //check that it is not less than the minimal height
10276 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
10279 // See comment in SetColSize
10280 if ( height
< GetRowMinimalAcceptableHeight())
10283 if ( m_rowHeights
.IsEmpty() )
10285 // need to really create the array
10289 int h
= wxMax( 0, height
);
10290 int diff
= h
- m_rowHeights
[row
];
10292 m_rowHeights
[row
] = h
;
10293 for ( int i
= row
; i
< m_numRows
; i
++ )
10295 m_rowBottoms
[i
] += diff
;
10298 if ( !GetBatchCount() )
10302 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10304 // we dont allow zero default column width
10305 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10307 if ( resizeExistingCols
)
10309 // since we are resizing all columns to the default column size,
10310 // we can simply clear the col widths and col rights
10311 // arrays (which also allows us to take advantage of
10312 // some speed optimisations)
10313 m_colWidths
.Empty();
10314 m_colRights
.Empty();
10315 if ( !GetBatchCount() )
10320 void wxGrid::SetColSize( int col
, int width
)
10322 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10324 // if < 0 then calculate new width from label
10328 wxArrayString lines
;
10329 wxClientDC
dc(m_colLabelWin
);
10330 dc
.SetFont(GetLabelFont());
10331 StringToLines(GetColLabelValue(col
), lines
);
10332 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10333 GetTextBoxSize( dc
, lines
, &w
, &h
);
10335 GetTextBoxSize( dc
, lines
, &h
, &w
);
10337 //check that it is not less than the minimal width
10338 width
= wxMax(width
, GetColMinimalAcceptableWidth());
10341 // should we check that it's bigger than GetColMinimalWidth(col) here?
10343 // No, because it is reasonable to assume the library user know's
10344 // what he is doing. However we should test against the weaker
10345 // constraint of minimalAcceptableWidth, as this breaks rendering
10347 // This test then fixes sf.net bug #645734
10349 if ( width
< GetColMinimalAcceptableWidth() )
10352 if ( m_colWidths
.IsEmpty() )
10354 // need to really create the array
10358 int w
= wxMax( 0, width
);
10359 int diff
= w
- m_colWidths
[col
];
10360 m_colWidths
[col
] = w
;
10362 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10364 m_colRights
[GetColAt(colPos
)] += diff
;
10367 if ( !GetBatchCount() )
10371 void wxGrid::SetColMinimalWidth( int col
, int width
)
10373 if (width
> GetColMinimalAcceptableWidth())
10375 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10376 m_colMinWidths
[key
] = width
;
10380 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10382 if (width
> GetRowMinimalAcceptableHeight())
10384 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10385 m_rowMinHeights
[key
] = width
;
10389 int wxGrid::GetColMinimalWidth(int col
) const
10391 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10392 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10394 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10397 int wxGrid::GetRowMinimalHeight(int row
) const
10399 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10400 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10402 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10405 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10407 // We do allow a width of 0 since this gives us
10408 // an easy way to temporarily hiding columns.
10410 m_minAcceptableColWidth
= width
;
10413 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10415 // We do allow a height of 0 since this gives us
10416 // an easy way to temporarily hiding rows.
10418 m_minAcceptableRowHeight
= height
;
10421 int wxGrid::GetColMinimalAcceptableWidth() const
10423 return m_minAcceptableColWidth
;
10426 int wxGrid::GetRowMinimalAcceptableHeight() const
10428 return m_minAcceptableRowHeight
;
10431 // ----------------------------------------------------------------------------
10433 // ----------------------------------------------------------------------------
10436 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10438 const bool column
= direction
== wxGRID_COLUMN
;
10440 wxClientDC
dc(m_gridWin
);
10442 // cancel editing of cell
10443 HideCellEditControl();
10444 SaveEditControlValue();
10446 // init both of them to avoid compiler warnings, even if we only need one
10454 wxCoord extent
, extentMax
= 0;
10455 int max
= column
? m_numRows
: m_numCols
;
10456 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10463 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10464 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10467 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10468 extent
= column
? size
.x
: size
.y
;
10469 if ( extent
> extentMax
)
10470 extentMax
= extent
;
10472 renderer
->DecRef();
10478 // now also compare with the column label extent
10480 dc
.SetFont( GetLabelFont() );
10484 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10485 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10489 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10491 extent
= column
? w
: h
;
10492 if ( extent
> extentMax
)
10493 extentMax
= extent
;
10497 // empty column - give default extent (notice that if extentMax is less
10498 // than default extent but != 0, it's OK)
10499 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10504 // leave some space around text
10512 // Ensure automatic width is not less than minimal width. See the
10513 // comment in SetColSize() for explanation of why this isn't done
10514 // in SetColSize().
10516 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10518 SetColSize( col
, extentMax
);
10519 if ( !GetBatchCount() )
10522 m_gridWin
->GetClientSize( &cw
, &ch
);
10523 wxRect
rect ( CellToRect( 0, col
) );
10525 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10526 rect
.width
= cw
- rect
.x
;
10527 rect
.height
= m_colLabelHeight
;
10528 m_colLabelWin
->Refresh( true, &rect
);
10533 // Ensure automatic width is not less than minimal height. See the
10534 // comment in SetColSize() for explanation of why this isn't done
10535 // in SetRowSize().
10537 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10539 SetRowSize(row
, extentMax
);
10540 if ( !GetBatchCount() )
10543 m_gridWin
->GetClientSize( &cw
, &ch
);
10544 wxRect
rect( CellToRect( row
, 0 ) );
10546 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10547 rect
.width
= m_rowLabelWidth
;
10548 rect
.height
= ch
- rect
.y
;
10549 m_rowLabelWin
->Refresh( true, &rect
);
10556 SetColMinimalWidth(col
, extentMax
);
10558 SetRowMinimalHeight(row
, extentMax
);
10562 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10564 // calculate size for the rows or columns?
10565 const bool calcRows
= direction
== wxGRID_ROW
;
10567 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10568 : GetGridColLabelWindow());
10569 dc
.SetFont(GetLabelFont());
10571 // which dimension should we take into account for calculations?
10573 // for columns, the text can be only horizontal so it's easy but for rows
10574 // we also have to take into account the text orientation
10576 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10578 wxArrayString lines
;
10579 wxCoord extentMax
= 0;
10581 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10582 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10586 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10587 : GetColLabelValue(rowOrCol
);
10588 StringToLines(label
, lines
);
10591 GetTextBoxSize(dc
, lines
, &w
, &h
);
10593 const wxCoord extent
= useWidth
? w
: h
;
10594 if ( extent
> extentMax
)
10595 extentMax
= extent
;
10600 // empty column - give default extent (notice that if extentMax is less
10601 // than default extent but != 0, it's OK)
10602 extentMax
= calcRows
? GetDefaultRowLabelSize()
10603 : GetDefaultColLabelSize();
10606 // leave some space around text (taken from AutoSizeColOrRow)
10615 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10617 int width
= m_rowLabelWidth
;
10619 wxGridUpdateLocker locker
;
10621 locker
.Create(this);
10623 for ( int col
= 0; col
< m_numCols
; col
++ )
10626 AutoSizeColumn(col
, setAsMin
);
10628 width
+= GetColWidth(col
);
10634 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10636 int height
= m_colLabelHeight
;
10638 wxGridUpdateLocker locker
;
10640 locker
.Create(this);
10642 for ( int row
= 0; row
< m_numRows
; row
++ )
10645 AutoSizeRow(row
, setAsMin
);
10647 height
+= GetRowHeight(row
);
10653 void wxGrid::AutoSize()
10655 wxGridUpdateLocker
locker(this);
10657 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10658 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10660 // we know that we're not going to have scrollbars so disable them now to
10661 // avoid trouble in SetClientSize() which can otherwise set the correct
10662 // client size but also leave space for (not needed any more) scrollbars
10663 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10665 // restore the scroll rate parameters overwritten by SetScrollbars()
10666 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
10668 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
10671 void wxGrid::AutoSizeRowLabelSize( int row
)
10673 // Hide the edit control, so it
10674 // won't interfere with drag-shrinking.
10675 if ( IsCellEditControlShown() )
10677 HideCellEditControl();
10678 SaveEditControlValue();
10681 // autosize row height depending on label text
10682 SetRowSize(row
, -1);
10686 void wxGrid::AutoSizeColLabelSize( int col
)
10688 // Hide the edit control, so it
10689 // won't interfere with drag-shrinking.
10690 if ( IsCellEditControlShown() )
10692 HideCellEditControl();
10693 SaveEditControlValue();
10696 // autosize column width depending on label text
10697 SetColSize(col
, -1);
10701 wxSize
wxGrid::DoGetBestSize() const
10703 wxGrid
*self
= (wxGrid
*)this; // const_cast
10705 // we do the same as in AutoSize() here with the exception that we don't
10706 // change the column/row sizes, only calculate them
10707 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10708 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10710 // NOTE: This size should be cached, but first we need to add calls to
10711 // InvalidateBestSize everywhere that could change the results of this
10713 // CacheBestSize(size);
10715 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
10716 + GetWindowBorderSize();
10724 wxPen
& wxGrid::GetDividerPen() const
10729 // ----------------------------------------------------------------------------
10730 // cell value accessor functions
10731 // ----------------------------------------------------------------------------
10733 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10737 m_table
->SetValue( row
, col
, s
);
10738 if ( !GetBatchCount() )
10741 wxRect
rect( CellToRect( row
, col
) );
10743 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10744 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10745 m_gridWin
->Refresh( false, &rect
);
10748 if ( m_currentCellCoords
.GetRow() == row
&&
10749 m_currentCellCoords
.GetCol() == col
&&
10750 IsCellEditControlShown())
10751 // Note: If we are using IsCellEditControlEnabled,
10752 // this interacts badly with calling SetCellValue from
10753 // an EVT_GRID_CELL_CHANGE handler.
10755 HideCellEditControl();
10756 ShowCellEditControl(); // will reread data from table
10761 // ----------------------------------------------------------------------------
10762 // block, row and column selection
10763 // ----------------------------------------------------------------------------
10765 void wxGrid::SelectRow( int row
, bool addToSelected
)
10767 if ( IsSelection() && !addToSelected
)
10771 m_selection
->SelectRow( row
, false, addToSelected
);
10774 void wxGrid::SelectCol( int col
, bool addToSelected
)
10776 if ( IsSelection() && !addToSelected
)
10780 m_selection
->SelectCol( col
, false, addToSelected
);
10783 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10784 bool addToSelected
)
10786 if ( IsSelection() && !addToSelected
)
10790 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10791 false, addToSelected
);
10794 void wxGrid::SelectAll()
10796 if ( m_numRows
> 0 && m_numCols
> 0 )
10799 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10803 // ----------------------------------------------------------------------------
10804 // cell, row and col deselection
10805 // ----------------------------------------------------------------------------
10807 void wxGrid::DeselectRow( int row
)
10809 if ( !m_selection
)
10812 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10814 if ( m_selection
->IsInSelection(row
, 0 ) )
10815 m_selection
->ToggleCellSelection(row
, 0);
10819 int nCols
= GetNumberCols();
10820 for ( int i
= 0; i
< nCols
; i
++ )
10822 if ( m_selection
->IsInSelection(row
, i
) )
10823 m_selection
->ToggleCellSelection(row
, i
);
10828 void wxGrid::DeselectCol( int col
)
10830 if ( !m_selection
)
10833 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10835 if ( m_selection
->IsInSelection(0, col
) )
10836 m_selection
->ToggleCellSelection(0, col
);
10840 int nRows
= GetNumberRows();
10841 for ( int i
= 0; i
< nRows
; i
++ )
10843 if ( m_selection
->IsInSelection(i
, col
) )
10844 m_selection
->ToggleCellSelection(i
, col
);
10849 void wxGrid::DeselectCell( int row
, int col
)
10851 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10852 m_selection
->ToggleCellSelection(row
, col
);
10855 bool wxGrid::IsSelection() const
10857 return ( m_selection
&& (m_selection
->IsSelection() ||
10858 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10859 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10862 bool wxGrid::IsInSelection( int row
, int col
) const
10864 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10865 ( row
>= m_selectingTopLeft
.GetRow() &&
10866 col
>= m_selectingTopLeft
.GetCol() &&
10867 row
<= m_selectingBottomRight
.GetRow() &&
10868 col
<= m_selectingBottomRight
.GetCol() )) );
10871 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10875 wxGridCellCoordsArray a
;
10879 return m_selection
->m_cellSelection
;
10882 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10886 wxGridCellCoordsArray a
;
10890 return m_selection
->m_blockSelectionTopLeft
;
10893 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10897 wxGridCellCoordsArray a
;
10901 return m_selection
->m_blockSelectionBottomRight
;
10904 wxArrayInt
wxGrid::GetSelectedRows() const
10912 return m_selection
->m_rowSelection
;
10915 wxArrayInt
wxGrid::GetSelectedCols() const
10923 return m_selection
->m_colSelection
;
10926 void wxGrid::ClearSelection()
10928 wxRect r1
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
);
10929 wxRect r2
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard
);
10930 m_selectingTopLeft
=
10931 m_selectingBottomRight
=
10932 m_selectingKeyboard
= wxGridNoCellCoords
;
10933 Refresh( false, &r1
);
10934 Refresh( false, &r2
);
10936 m_selection
->ClearSelection();
10939 // This function returns the rectangle that encloses the given block
10940 // in device coords clipped to the client size of the grid window.
10942 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
10943 const wxGridCellCoords
& bottomRight
) const
10946 wxRect tempCellRect
= CellToRect(topLeft
);
10947 if ( tempCellRect
!= wxGridNoCellRect
)
10949 resultRect
= tempCellRect
;
10953 resultRect
= wxRect(0, 0, 0, 0);
10956 tempCellRect
= CellToRect(bottomRight
);
10957 if ( tempCellRect
!= wxGridNoCellRect
)
10959 resultRect
+= tempCellRect
;
10963 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
10964 return wxGridNoCellRect
;
10967 // Ensure that left/right and top/bottom pairs are in order.
10968 int left
= resultRect
.GetLeft();
10969 int top
= resultRect
.GetTop();
10970 int right
= resultRect
.GetRight();
10971 int bottom
= resultRect
.GetBottom();
10973 int leftCol
= topLeft
.GetCol();
10974 int topRow
= topLeft
.GetRow();
10975 int rightCol
= bottomRight
.GetCol();
10976 int bottomRow
= bottomRight
.GetRow();
10985 leftCol
= rightCol
;
10996 topRow
= bottomRow
;
11000 // The following loop is ONLY necessary to detect and handle merged cells.
11002 m_gridWin
->GetClientSize( &cw
, &ch
);
11004 // Get the origin coordinates: notice that they will be negative if the
11005 // grid is scrolled downwards/to the right.
11006 int gridOriginX
= 0;
11007 int gridOriginY
= 0;
11008 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11010 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11011 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11013 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11014 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11016 // Bound our loop so that we only examine the portion of the selected block
11017 // that is shown on screen. Therefore, we compare the Top-Left block values
11018 // to the Top-Left screen values, and the Bottom-Right block values to the
11019 // Bottom-Right screen values, choosing appropriately.
11020 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11021 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11022 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11023 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11025 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11027 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11029 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11030 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11032 tempCellRect
= CellToRect( j
, i
);
11034 if (tempCellRect
.x
< left
)
11035 left
= tempCellRect
.x
;
11036 if (tempCellRect
.y
< top
)
11037 top
= tempCellRect
.y
;
11038 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11039 right
= tempCellRect
.x
+ tempCellRect
.width
;
11040 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11041 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11045 i
= visibleRightCol
; // jump over inner cells.
11050 // Convert to scrolled coords
11051 CalcScrolledPosition( left
, top
, &left
, &top
);
11052 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11054 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11055 return wxRect(0,0,0,0);
11057 resultRect
.SetLeft( wxMax(0, left
) );
11058 resultRect
.SetTop( wxMax(0, top
) );
11059 resultRect
.SetRight( wxMin(cw
, right
) );
11060 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11065 // ----------------------------------------------------------------------------
11067 // ----------------------------------------------------------------------------
11069 #if wxUSE_DRAG_AND_DROP
11071 // this allow setting drop target directly on wxGrid
11072 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11074 GetGridWindow()->SetDropTarget(dropTarget
);
11077 #endif // wxUSE_DRAG_AND_DROP
11079 // ----------------------------------------------------------------------------
11080 // grid event classes
11081 // ----------------------------------------------------------------------------
11083 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11085 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11086 int row
, int col
, int x
, int y
, bool sel
,
11087 bool control
, bool shift
, bool alt
, bool meta
)
11088 : wxNotifyEvent( type
, id
)
11095 m_control
= control
;
11100 SetEventObject(obj
);
11104 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11106 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11107 int rowOrCol
, int x
, int y
,
11108 bool control
, bool shift
, bool alt
, bool meta
)
11109 : wxNotifyEvent( type
, id
)
11111 m_rowOrCol
= rowOrCol
;
11114 m_control
= control
;
11119 SetEventObject(obj
);
11123 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11125 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11126 const wxGridCellCoords
& topLeft
,
11127 const wxGridCellCoords
& bottomRight
,
11128 bool sel
, bool control
,
11129 bool shift
, bool alt
, bool meta
)
11130 : wxNotifyEvent( type
, id
)
11132 m_topLeft
= topLeft
;
11133 m_bottomRight
= bottomRight
;
11135 m_control
= control
;
11140 SetEventObject(obj
);
11144 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11146 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11147 wxObject
* obj
, int row
,
11148 int col
, wxControl
* ctrl
)
11149 : wxCommandEvent(type
, id
)
11151 SetEventObject(obj
);
11157 #endif // wxUSE_GRID