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 wxGridSelectionModes selmode
)
4284 wxCHECK_MSG( !m_created
,
4286 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4288 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
4291 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
4293 wxCHECK_RET( m_created
,
4294 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4296 m_selection
->SetSelectionMode( selmode
);
4299 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4301 wxCHECK_MSG( m_created
, wxGridSelectCells
,
4302 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4304 return m_selection
->GetSelectionMode();
4308 wxGrid::SetTable(wxGridTableBase
*table
,
4310 wxGrid::wxGridSelectionModes selmode
)
4312 bool checkSelection
= false;
4315 // stop all processing
4320 m_table
->SetView(0);
4332 checkSelection
= true;
4334 // kill row and column size arrays
4335 m_colWidths
.Empty();
4336 m_colRights
.Empty();
4337 m_rowHeights
.Empty();
4338 m_rowBottoms
.Empty();
4343 m_numRows
= table
->GetNumberRows();
4344 m_numCols
= table
->GetNumberCols();
4347 m_table
->SetView( this );
4348 m_ownTable
= takeOwnership
;
4349 m_selection
= new wxGridSelection( this, selmode
);
4352 // If the newly set table is smaller than the
4353 // original one current cell and selection regions
4354 // might be invalid,
4355 m_selectingKeyboard
= wxGridNoCellCoords
;
4356 m_currentCellCoords
=
4357 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4358 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4359 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4360 m_selectingTopLeft
.GetCol() >= m_numCols
)
4362 m_selectingTopLeft
= wxGridNoCellCoords
;
4363 m_selectingBottomRight
= wxGridNoCellCoords
;
4366 m_selectingBottomRight
=
4367 wxGridCellCoords(wxMin(m_numRows
,
4368 m_selectingBottomRight
.GetRow()),
4370 m_selectingBottomRight
.GetCol()));
4380 void wxGrid::InitVars()
4384 m_cornerLabelWin
= NULL
;
4385 m_rowLabelWin
= NULL
;
4386 m_colLabelWin
= NULL
;
4393 m_defaultCellAttr
= NULL
;
4394 m_typeRegistry
= NULL
;
4395 m_winCapture
= NULL
;
4400 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4401 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4403 if ( m_rowLabelWin
)
4405 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4409 m_labelBackgroundColour
= *wxWHITE
;
4412 m_labelTextColour
= *wxBLACK
;
4415 m_attrCache
.row
= -1;
4416 m_attrCache
.col
= -1;
4417 m_attrCache
.attr
= NULL
;
4419 m_labelFont
= GetFont();
4420 m_labelFont
.SetWeight( wxBOLD
);
4422 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4423 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4425 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4426 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4427 m_colLabelTextOrientation
= wxHORIZONTAL
;
4429 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4430 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4432 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4433 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4435 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4436 m_defaultRowHeight
+= 8;
4438 m_defaultRowHeight
+= 4;
4441 m_gridLineColour
= wxColour( 192,192,192 );
4442 m_gridLinesEnabled
= true;
4443 m_cellHighlightColour
= *wxBLACK
;
4444 m_cellHighlightPenWidth
= 2;
4445 m_cellHighlightROPenWidth
= 1;
4447 m_canDragColMove
= false;
4449 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4450 m_winCapture
= (wxWindow
*)NULL
;
4451 m_canDragRowSize
= true;
4452 m_canDragColSize
= true;
4453 m_canDragGridSize
= true;
4454 m_canDragCell
= false;
4456 m_dragRowOrCol
= -1;
4457 m_isDragging
= false;
4458 m_startDragPos
= wxDefaultPosition
;
4459 m_nativeColumnLabels
= false;
4461 m_waitForSlowClick
= false;
4463 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4464 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4466 m_currentCellCoords
= wxGridNoCellCoords
;
4470 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4471 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4473 m_editable
= true; // default for whole grid
4475 m_inOnKeyDown
= false;
4481 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4482 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4485 // ----------------------------------------------------------------------------
4486 // the idea is to call these functions only when necessary because they create
4487 // quite big arrays which eat memory mostly unnecessary - in particular, if
4488 // default widths/heights are used for all rows/columns, we may not use these
4491 // with some extra code, it should be possible to only store the widths/heights
4492 // different from default ones (resulting in space savings for huge grids) but
4493 // this is not done currently
4494 // ----------------------------------------------------------------------------
4496 void wxGrid::InitRowHeights()
4498 m_rowHeights
.Empty();
4499 m_rowBottoms
.Empty();
4501 m_rowHeights
.Alloc( m_numRows
);
4502 m_rowBottoms
.Alloc( m_numRows
);
4504 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4507 for ( int i
= 0; i
< m_numRows
; i
++ )
4509 rowBottom
+= m_defaultRowHeight
;
4510 m_rowBottoms
.Add( rowBottom
);
4514 void wxGrid::InitColWidths()
4516 m_colWidths
.Empty();
4517 m_colRights
.Empty();
4519 m_colWidths
.Alloc( m_numCols
);
4520 m_colRights
.Alloc( m_numCols
);
4522 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4525 for ( int i
= 0; i
< m_numCols
; i
++ )
4527 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4528 m_colRights
.Add( colRight
);
4532 int wxGrid::GetColWidth(int col
) const
4534 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4537 int wxGrid::GetColLeft(int col
) const
4539 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4540 : m_colRights
[col
] - m_colWidths
[col
];
4543 int wxGrid::GetColRight(int col
) const
4545 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4549 int wxGrid::GetRowHeight(int row
) const
4551 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4554 int wxGrid::GetRowTop(int row
) const
4556 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4557 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4560 int wxGrid::GetRowBottom(int row
) const
4562 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4563 : m_rowBottoms
[row
];
4566 void wxGrid::CalcDimensions()
4568 // compute the size of the scrollable area
4569 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4570 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4575 // take into account editor if shown
4576 if ( IsCellEditControlShown() )
4579 int r
= m_currentCellCoords
.GetRow();
4580 int c
= m_currentCellCoords
.GetCol();
4581 int x
= GetColLeft(c
);
4582 int y
= GetRowTop(r
);
4584 // how big is the editor
4585 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4586 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4587 editor
->GetControl()->GetSize(&w2
, &h2
);
4598 // preserve (more or less) the previous position
4600 GetViewStart( &x
, &y
);
4602 // ensure the position is valid for the new scroll ranges
4604 x
= wxMax( w
- 1, 0 );
4606 y
= wxMax( h
- 1, 0 );
4608 // update the virtual size and refresh the scrollbars to reflect it
4609 m_gridWin
->SetVirtualSize(w
, h
);
4613 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4614 // still must reposition the children
4618 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
4620 wxSize
sizeGridWin(size
);
4621 sizeGridWin
.x
-= m_rowLabelWidth
;
4622 sizeGridWin
.y
-= m_colLabelHeight
;
4627 void wxGrid::CalcWindowSizes()
4629 // escape if the window is has not been fully created yet
4631 if ( m_cornerLabelWin
== NULL
)
4635 GetClientSize( &cw
, &ch
);
4637 // the grid may be too small to have enough space for the labels yet, don't
4638 // size the windows to negative sizes in this case
4639 int gw
= cw
- m_rowLabelWidth
;
4640 int gh
= ch
- m_colLabelHeight
;
4646 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4647 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4649 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4650 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4652 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4653 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4655 if ( m_gridWin
&& m_gridWin
->IsShown() )
4656 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4659 // this is called when the grid table sends a message
4660 // to indicate that it has been redimensioned
4662 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4665 bool result
= false;
4667 // Clear the attribute cache as the attribute might refer to a different
4668 // cell than stored in the cache after adding/removing rows/columns.
4671 // By the same reasoning, the editor should be dismissed if columns are
4672 // added or removed. And for consistency, it should IMHO always be
4673 // removed, not only if the cell "underneath" it actually changes.
4674 // For now, I intentionally do not save the editor's content as the
4675 // cell it might want to save that stuff to might no longer exist.
4676 HideCellEditControl();
4679 // if we were using the default widths/heights so far, we must change them
4681 if ( m_colWidths
.IsEmpty() )
4686 if ( m_rowHeights
.IsEmpty() )
4692 switch ( msg
.GetId() )
4694 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4696 size_t pos
= msg
.GetCommandInt();
4697 int numRows
= msg
.GetCommandInt2();
4699 m_numRows
+= numRows
;
4701 if ( !m_rowHeights
.IsEmpty() )
4703 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4704 m_rowBottoms
.Insert( 0, pos
, numRows
);
4708 bottom
= m_rowBottoms
[pos
- 1];
4710 for ( i
= pos
; i
< m_numRows
; i
++ )
4712 bottom
+= m_rowHeights
[i
];
4713 m_rowBottoms
[i
] = bottom
;
4717 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4719 // if we have just inserted cols into an empty grid the current
4720 // cell will be undefined...
4722 SetCurrentCell( 0, 0 );
4726 m_selection
->UpdateRows( pos
, numRows
);
4727 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4729 attrProvider
->UpdateAttrRows( pos
, numRows
);
4731 if ( !GetBatchCount() )
4734 m_rowLabelWin
->Refresh();
4740 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4742 int numRows
= msg
.GetCommandInt();
4743 int oldNumRows
= m_numRows
;
4744 m_numRows
+= numRows
;
4746 if ( !m_rowHeights
.IsEmpty() )
4748 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4749 m_rowBottoms
.Add( 0, numRows
);
4752 if ( oldNumRows
> 0 )
4753 bottom
= m_rowBottoms
[oldNumRows
- 1];
4755 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4757 bottom
+= m_rowHeights
[i
];
4758 m_rowBottoms
[i
] = bottom
;
4762 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4764 // if we have just inserted cols into an empty grid the current
4765 // cell will be undefined...
4767 SetCurrentCell( 0, 0 );
4770 if ( !GetBatchCount() )
4773 m_rowLabelWin
->Refresh();
4779 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4781 size_t pos
= msg
.GetCommandInt();
4782 int numRows
= msg
.GetCommandInt2();
4783 m_numRows
-= numRows
;
4785 if ( !m_rowHeights
.IsEmpty() )
4787 m_rowHeights
.RemoveAt( pos
, numRows
);
4788 m_rowBottoms
.RemoveAt( pos
, numRows
);
4791 for ( i
= 0; i
< m_numRows
; i
++ )
4793 h
+= m_rowHeights
[i
];
4794 m_rowBottoms
[i
] = h
;
4800 m_currentCellCoords
= wxGridNoCellCoords
;
4804 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4805 m_currentCellCoords
.Set( 0, 0 );
4809 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4810 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4813 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4815 // ifdef'd out following patch from Paul Gammans
4817 // No need to touch column attributes, unless we
4818 // removed _all_ rows, in this case, we remove
4819 // all column attributes.
4820 // I hate to do this here, but the
4821 // needed data is not available inside UpdateAttrRows.
4822 if ( !GetNumberRows() )
4823 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4827 if ( !GetBatchCount() )
4830 m_rowLabelWin
->Refresh();
4836 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4838 size_t pos
= msg
.GetCommandInt();
4839 int numCols
= msg
.GetCommandInt2();
4840 m_numCols
+= numCols
;
4842 if ( !m_colAt
.IsEmpty() )
4844 //Shift the column IDs
4846 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4848 if ( m_colAt
[i
] >= (int)pos
)
4849 m_colAt
[i
] += numCols
;
4852 m_colAt
.Insert( pos
, pos
, numCols
);
4854 //Set the new columns' positions
4855 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4861 if ( !m_colWidths
.IsEmpty() )
4863 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4864 m_colRights
.Insert( 0, pos
, numCols
);
4868 right
= m_colRights
[GetColAt( pos
- 1 )];
4871 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4873 i
= GetColAt( colPos
);
4875 right
+= m_colWidths
[i
];
4876 m_colRights
[i
] = right
;
4880 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4882 // if we have just inserted cols into an empty grid the current
4883 // cell will be undefined...
4885 SetCurrentCell( 0, 0 );
4889 m_selection
->UpdateCols( pos
, numCols
);
4890 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4892 attrProvider
->UpdateAttrCols( pos
, numCols
);
4893 if ( !GetBatchCount() )
4896 m_colLabelWin
->Refresh();
4902 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4904 int numCols
= msg
.GetCommandInt();
4905 int oldNumCols
= m_numCols
;
4906 m_numCols
+= numCols
;
4908 if ( !m_colAt
.IsEmpty() )
4910 m_colAt
.Add( 0, numCols
);
4912 //Set the new columns' positions
4914 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4920 if ( !m_colWidths
.IsEmpty() )
4922 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4923 m_colRights
.Add( 0, numCols
);
4926 if ( oldNumCols
> 0 )
4927 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4930 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4932 i
= GetColAt( colPos
);
4934 right
+= m_colWidths
[i
];
4935 m_colRights
[i
] = right
;
4939 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4941 // if we have just inserted cols into an empty grid the current
4942 // cell will be undefined...
4944 SetCurrentCell( 0, 0 );
4946 if ( !GetBatchCount() )
4949 m_colLabelWin
->Refresh();
4955 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4957 size_t pos
= msg
.GetCommandInt();
4958 int numCols
= msg
.GetCommandInt2();
4959 m_numCols
-= numCols
;
4961 if ( !m_colAt
.IsEmpty() )
4963 int colID
= GetColAt( pos
);
4965 m_colAt
.RemoveAt( pos
, numCols
);
4967 //Shift the column IDs
4969 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4971 if ( m_colAt
[colPos
] > colID
)
4972 m_colAt
[colPos
] -= numCols
;
4976 if ( !m_colWidths
.IsEmpty() )
4978 m_colWidths
.RemoveAt( pos
, numCols
);
4979 m_colRights
.RemoveAt( pos
, numCols
);
4983 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4985 i
= GetColAt( colPos
);
4987 w
+= m_colWidths
[i
];
4994 m_currentCellCoords
= wxGridNoCellCoords
;
4998 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4999 m_currentCellCoords
.Set( 0, 0 );
5003 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5004 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5007 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5009 // ifdef'd out following patch from Paul Gammans
5011 // No need to touch row attributes, unless we
5012 // removed _all_ columns, in this case, we remove
5013 // all row attributes.
5014 // I hate to do this here, but the
5015 // needed data is not available inside UpdateAttrCols.
5016 if ( !GetNumberCols() )
5017 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5021 if ( !GetBatchCount() )
5024 m_colLabelWin
->Refresh();
5031 if (result
&& !GetBatchCount() )
5032 m_gridWin
->Refresh();
5037 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5039 wxRegionIterator
iter( reg
);
5042 wxArrayInt rowlabels
;
5049 // TODO: remove this when we can...
5050 // There is a bug in wxMotif that gives garbage update
5051 // rectangles if you jump-scroll a long way by clicking the
5052 // scrollbar with middle button. This is a work-around
5054 #if defined(__WXMOTIF__)
5056 m_gridWin
->GetClientSize( &cw
, &ch
);
5057 if ( r
.GetTop() > ch
)
5059 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5062 // logical bounds of update region
5065 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5066 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5068 // find the row labels within these bounds
5071 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5073 if ( GetRowBottom(row
) < top
)
5076 if ( GetRowTop(row
) > bottom
)
5079 rowlabels
.Add( row
);
5088 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5090 wxRegionIterator
iter( reg
);
5093 wxArrayInt colLabels
;
5100 // TODO: remove this when we can...
5101 // There is a bug in wxMotif that gives garbage update
5102 // rectangles if you jump-scroll a long way by clicking the
5103 // scrollbar with middle button. This is a work-around
5105 #if defined(__WXMOTIF__)
5107 m_gridWin
->GetClientSize( &cw
, &ch
);
5108 if ( r
.GetLeft() > cw
)
5110 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5113 // logical bounds of update region
5116 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5117 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5119 // find the cells within these bounds
5123 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5125 col
= GetColAt( colPos
);
5127 if ( GetColRight(col
) < left
)
5130 if ( GetColLeft(col
) > right
)
5133 colLabels
.Add( col
);
5142 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5144 wxRegionIterator
iter( reg
);
5147 wxGridCellCoordsArray cellsExposed
;
5149 int left
, top
, right
, bottom
;
5154 // TODO: remove this when we can...
5155 // There is a bug in wxMotif that gives garbage update
5156 // rectangles if you jump-scroll a long way by clicking the
5157 // scrollbar with middle button. This is a work-around
5159 #if defined(__WXMOTIF__)
5161 m_gridWin
->GetClientSize( &cw
, &ch
);
5162 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5163 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5164 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5165 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5168 // logical bounds of update region
5170 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5171 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5173 // find the cells within these bounds
5176 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5178 if ( GetRowBottom(row
) <= top
)
5181 if ( GetRowTop(row
) > bottom
)
5185 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5187 col
= GetColAt( colPos
);
5189 if ( GetColRight(col
) <= left
)
5192 if ( GetColLeft(col
) > right
)
5195 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5202 return cellsExposed
;
5206 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5209 wxPoint
pos( event
.GetPosition() );
5210 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5212 if ( event
.Dragging() )
5216 m_isDragging
= true;
5217 m_rowLabelWin
->CaptureMouse();
5220 if ( event
.LeftIsDown() )
5222 switch ( m_cursorMode
)
5224 case WXGRID_CURSOR_RESIZE_ROW
:
5226 int cw
, ch
, left
, dummy
;
5227 m_gridWin
->GetClientSize( &cw
, &ch
);
5228 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5230 wxClientDC
dc( m_gridWin
);
5233 GetRowTop(m_dragRowOrCol
) +
5234 GetRowMinimalHeight(m_dragRowOrCol
) );
5235 dc
.SetLogicalFunction(wxINVERT
);
5236 if ( m_dragLastPos
>= 0 )
5238 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5240 dc
.DrawLine( left
, y
, left
+cw
, y
);
5245 case WXGRID_CURSOR_SELECT_ROW
:
5247 if ( (row
= YToRow( y
)) >= 0 )
5251 m_selection
->SelectRow( row
,
5252 event
.ControlDown(),
5261 // default label to suppress warnings about "enumeration value
5262 // 'xxx' not handled in switch
5270 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5275 if (m_rowLabelWin
->HasCapture())
5276 m_rowLabelWin
->ReleaseMouse();
5277 m_isDragging
= false;
5280 // ------------ Entering or leaving the window
5282 if ( event
.Entering() || event
.Leaving() )
5284 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5287 // ------------ Left button pressed
5289 else if ( event
.LeftDown() )
5291 // don't send a label click event for a hit on the
5292 // edge of the row label - this is probably the user
5293 // wanting to resize the row
5295 if ( YToEdgeOfRow(y
) < 0 )
5299 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5301 if ( !event
.ShiftDown() && !event
.CmdDown() )
5305 if ( event
.ShiftDown() )
5307 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5310 GetNumberCols() - 1,
5311 event
.ControlDown(),
5318 m_selection
->SelectRow( row
,
5319 event
.ControlDown(),
5326 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5331 // starting to drag-resize a row
5332 if ( CanDragRowSize() )
5333 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5337 // ------------ Left double click
5339 else if (event
.LeftDClick() )
5341 row
= YToEdgeOfRow(y
);
5346 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5348 // no default action at the moment
5353 // adjust row height depending on label text
5354 AutoSizeRowLabelSize( row
);
5356 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5361 // ------------ Left button released
5363 else if ( event
.LeftUp() )
5365 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5367 DoEndDragResizeRow();
5369 // Note: we are ending the event *after* doing
5370 // default processing in this case
5372 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5375 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5379 // ------------ Right button down
5381 else if ( event
.RightDown() )
5385 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5387 // no default action at the moment
5391 // ------------ Right double click
5393 else if ( event
.RightDClick() )
5397 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5399 // no default action at the moment
5403 // ------------ No buttons down and mouse moving
5405 else if ( event
.Moving() )
5407 m_dragRowOrCol
= YToEdgeOfRow( y
);
5408 if ( m_dragRowOrCol
>= 0 )
5410 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5412 // don't capture the mouse yet
5413 if ( CanDragRowSize() )
5414 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5417 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5419 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5424 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5427 wxPoint
pos( event
.GetPosition() );
5428 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5430 if ( event
.Dragging() )
5434 m_isDragging
= true;
5435 m_colLabelWin
->CaptureMouse();
5437 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5438 m_dragRowOrCol
= XToCol( x
);
5441 if ( event
.LeftIsDown() )
5443 switch ( m_cursorMode
)
5445 case WXGRID_CURSOR_RESIZE_COL
:
5447 int cw
, ch
, dummy
, top
;
5448 m_gridWin
->GetClientSize( &cw
, &ch
);
5449 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5451 wxClientDC
dc( m_gridWin
);
5454 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5455 GetColMinimalWidth(m_dragRowOrCol
));
5456 dc
.SetLogicalFunction(wxINVERT
);
5457 if ( m_dragLastPos
>= 0 )
5459 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5461 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5466 case WXGRID_CURSOR_SELECT_COL
:
5468 if ( (col
= XToCol( x
)) >= 0 )
5472 m_selection
->SelectCol( col
,
5473 event
.ControlDown(),
5482 case WXGRID_CURSOR_MOVE_COL
:
5485 m_moveToCol
= GetColAt( 0 );
5487 m_moveToCol
= XToCol( x
);
5491 if ( m_moveToCol
< 0 )
5492 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5493 else if ( x
>= (GetColLeft( m_moveToCol
) + (GetColWidth(m_moveToCol
) / 2)) )
5495 m_moveToCol
= GetColAt( GetColPos( m_moveToCol
) + 1 );
5496 if ( m_moveToCol
< 0 )
5497 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5499 markerX
= GetColLeft( m_moveToCol
);
5502 markerX
= GetColLeft( m_moveToCol
);
5504 if ( markerX
!= m_dragLastPos
)
5506 wxClientDC
dc( m_colLabelWin
);
5510 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5514 //Clean up the last indicator
5515 if ( m_dragLastPos
>= 0 )
5517 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5519 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5520 dc
.SetPen(wxNullPen
);
5522 if ( XToCol( m_dragLastPos
) != -1 )
5523 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5526 const wxColour
*color
;
5527 //Moving to the same place? Don't draw a marker
5528 if ( (m_moveToCol
== m_dragRowOrCol
)
5529 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5530 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5531 color
= wxLIGHT_GREY
;
5536 wxPen
pen( *color
, 2 );
5539 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5541 dc
.SetPen(wxNullPen
);
5543 m_dragLastPos
= markerX
- 1;
5548 // default label to suppress warnings about "enumeration value
5549 // 'xxx' not handled in switch
5557 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5562 if (m_colLabelWin
->HasCapture())
5563 m_colLabelWin
->ReleaseMouse();
5564 m_isDragging
= false;
5567 // ------------ Entering or leaving the window
5569 if ( event
.Entering() || event
.Leaving() )
5571 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5574 // ------------ Left button pressed
5576 else if ( event
.LeftDown() )
5578 // don't send a label click event for a hit on the
5579 // edge of the col label - this is probably the user
5580 // wanting to resize the col
5582 if ( XToEdgeOfCol(x
) < 0 )
5586 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5588 if ( m_canDragColMove
)
5590 //Show button as pressed
5591 wxClientDC
dc( m_colLabelWin
);
5592 int colLeft
= GetColLeft( col
);
5593 int colRight
= GetColRight( col
) - 1;
5594 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5595 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5596 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5598 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5602 if ( !event
.ShiftDown() && !event
.CmdDown() )
5606 if ( event
.ShiftDown() )
5608 m_selection
->SelectBlock( 0,
5609 m_currentCellCoords
.GetCol(),
5610 GetNumberRows() - 1, col
,
5611 event
.ControlDown(),
5618 m_selection
->SelectCol( col
,
5619 event
.ControlDown(),
5626 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5632 // starting to drag-resize a col
5634 if ( CanDragColSize() )
5635 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5639 // ------------ Left double click
5641 if ( event
.LeftDClick() )
5643 col
= XToEdgeOfCol(x
);
5648 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5650 // no default action at the moment
5655 // adjust column width depending on label text
5656 AutoSizeColLabelSize( col
);
5658 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5663 // ------------ Left button released
5665 else if ( event
.LeftUp() )
5667 switch ( m_cursorMode
)
5669 case WXGRID_CURSOR_RESIZE_COL
:
5670 DoEndDragResizeCol();
5672 // Note: we are ending the event *after* doing
5673 // default processing in this case
5675 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5678 case WXGRID_CURSOR_MOVE_COL
:
5681 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5684 case WXGRID_CURSOR_SELECT_COL
:
5685 case WXGRID_CURSOR_SELECT_CELL
:
5686 case WXGRID_CURSOR_RESIZE_ROW
:
5687 case WXGRID_CURSOR_SELECT_ROW
:
5688 // nothing to do (?)
5692 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5696 // ------------ Right button down
5698 else if ( event
.RightDown() )
5702 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5704 // no default action at the moment
5708 // ------------ Right double click
5710 else if ( event
.RightDClick() )
5714 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5716 // no default action at the moment
5720 // ------------ No buttons down and mouse moving
5722 else if ( event
.Moving() )
5724 m_dragRowOrCol
= XToEdgeOfCol( x
);
5725 if ( m_dragRowOrCol
>= 0 )
5727 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5729 // don't capture the cursor yet
5730 if ( CanDragColSize() )
5731 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5734 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5736 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5741 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5743 if ( event
.LeftDown() )
5745 // indicate corner label by having both row and
5748 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5753 else if ( event
.LeftDClick() )
5755 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5757 else if ( event
.RightDown() )
5759 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5761 // no default action at the moment
5764 else if ( event
.RightDClick() )
5766 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5768 // no default action at the moment
5773 void wxGrid::CancelMouseCapture()
5775 // cancel operation currently in progress, whatever it is
5778 m_isDragging
= false;
5779 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5780 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5781 m_winCapture
= NULL
;
5783 // remove traces of whatever we drew on screen
5788 void wxGrid::ChangeCursorMode(CursorMode mode
,
5793 static const wxChar
*cursorModes
[] =
5803 wxLogTrace(_T("grid"),
5804 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5805 win
== m_colLabelWin
? _T("colLabelWin")
5806 : win
? _T("rowLabelWin")
5808 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5811 if ( mode
== m_cursorMode
&&
5812 win
== m_winCapture
&&
5813 captureMouse
== (m_winCapture
!= NULL
))
5818 // by default use the grid itself
5824 if (m_winCapture
->HasCapture())
5825 m_winCapture
->ReleaseMouse();
5826 m_winCapture
= (wxWindow
*)NULL
;
5829 m_cursorMode
= mode
;
5831 switch ( m_cursorMode
)
5833 case WXGRID_CURSOR_RESIZE_ROW
:
5834 win
->SetCursor( m_rowResizeCursor
);
5837 case WXGRID_CURSOR_RESIZE_COL
:
5838 win
->SetCursor( m_colResizeCursor
);
5841 case WXGRID_CURSOR_MOVE_COL
:
5842 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5846 win
->SetCursor( *wxSTANDARD_CURSOR
);
5850 // we need to capture mouse when resizing
5851 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5852 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5854 if ( captureMouse
&& resize
)
5856 win
->CaptureMouse();
5861 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5864 wxPoint
pos( event
.GetPosition() );
5865 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5867 wxGridCellCoords coords
;
5868 XYToCell( x
, y
, coords
);
5870 int cell_rows
, cell_cols
;
5871 bool isFirstDrag
= !m_isDragging
;
5872 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5873 if ((cell_rows
< 0) || (cell_cols
< 0))
5875 coords
.SetRow(coords
.GetRow() + cell_rows
);
5876 coords
.SetCol(coords
.GetCol() + cell_cols
);
5879 if ( event
.Dragging() )
5881 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5883 // Don't start doing anything until the mouse has been dragged at
5884 // least 3 pixels in any direction...
5887 if (m_startDragPos
== wxDefaultPosition
)
5889 m_startDragPos
= pos
;
5892 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5896 m_isDragging
= true;
5897 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5899 // Hide the edit control, so it
5900 // won't interfere with drag-shrinking.
5901 if ( IsCellEditControlShown() )
5903 HideCellEditControl();
5904 SaveEditControlValue();
5907 if ( coords
!= wxGridNoCellCoords
)
5909 if ( event
.CmdDown() )
5911 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5912 m_selectingKeyboard
= coords
;
5913 HighlightBlock( m_selectingKeyboard
, coords
);
5915 else if ( CanDragCell() )
5919 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5920 m_selectingKeyboard
= coords
;
5922 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5931 if ( !IsSelection() )
5933 HighlightBlock( coords
, coords
);
5937 HighlightBlock( m_currentCellCoords
, coords
);
5941 if (! IsVisible(coords
))
5943 MakeCellVisible(coords
);
5944 // TODO: need to introduce a delay or something here. The
5945 // scrolling is way too fast, at least under MSW and GTK.
5948 // Have we captured the mouse yet?
5951 m_winCapture
= m_gridWin
;
5952 m_winCapture
->CaptureMouse();
5957 else if ( event
.LeftIsDown() &&
5958 m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5960 int cw
, ch
, left
, dummy
;
5961 m_gridWin
->GetClientSize( &cw
, &ch
);
5962 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5964 wxClientDC
dc( m_gridWin
);
5966 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5967 GetRowMinimalHeight(m_dragRowOrCol
) );
5968 dc
.SetLogicalFunction(wxINVERT
);
5969 if ( m_dragLastPos
>= 0 )
5971 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5973 dc
.DrawLine( left
, y
, left
+cw
, y
);
5976 else if ( event
.LeftIsDown() &&
5977 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5979 int cw
, ch
, dummy
, top
;
5980 m_gridWin
->GetClientSize( &cw
, &ch
);
5981 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5983 wxClientDC
dc( m_gridWin
);
5985 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5986 GetColMinimalWidth(m_dragRowOrCol
) );
5987 dc
.SetLogicalFunction(wxINVERT
);
5988 if ( m_dragLastPos
>= 0 )
5990 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5992 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5999 m_isDragging
= false;
6000 m_startDragPos
= wxDefaultPosition
;
6002 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6003 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6006 if ( event
.Entering() || event
.Leaving() )
6008 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6009 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6014 // ------------ Left button pressed
6016 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6018 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6023 if ( !event
.CmdDown() )
6025 if ( event
.ShiftDown() )
6029 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6030 m_currentCellCoords
.GetCol(),
6033 event
.ControlDown(),
6039 else if ( XToEdgeOfCol(x
) < 0 &&
6040 YToEdgeOfRow(y
) < 0 )
6042 DisableCellEditControl();
6043 MakeCellVisible( coords
);
6045 if ( event
.CmdDown() )
6049 m_selection
->ToggleCellSelection( coords
.GetRow(),
6051 event
.ControlDown(),
6056 m_selectingTopLeft
= wxGridNoCellCoords
;
6057 m_selectingBottomRight
= wxGridNoCellCoords
;
6058 m_selectingKeyboard
= coords
;
6062 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
6063 coords
!= wxGridNoCellCoords
;
6064 SetCurrentCell( coords
);
6070 // ------------ Left double click
6072 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6074 DisableCellEditControl();
6076 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6078 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6083 // we want double click to select a cell and start editing
6084 // (i.e. to behave in same way as sequence of two slow clicks):
6085 m_waitForSlowClick
= true;
6090 // ------------ Left button released
6092 else if ( event
.LeftUp() )
6094 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6098 if (m_winCapture
->HasCapture())
6099 m_winCapture
->ReleaseMouse();
6100 m_winCapture
= NULL
;
6103 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6106 EnableCellEditControl();
6108 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6109 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6110 editor
->StartingClick();
6114 m_waitForSlowClick
= false;
6116 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6117 m_selectingBottomRight
!= wxGridNoCellCoords
)
6121 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6122 m_selectingTopLeft
.GetCol(),
6123 m_selectingBottomRight
.GetRow(),
6124 m_selectingBottomRight
.GetCol(),
6125 event
.ControlDown(),
6131 m_selectingTopLeft
= wxGridNoCellCoords
;
6132 m_selectingBottomRight
= wxGridNoCellCoords
;
6134 // Show the edit control, if it has been hidden for
6136 ShowCellEditControl();
6139 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6141 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6142 DoEndDragResizeRow();
6144 // Note: we are ending the event *after* doing
6145 // default processing in this case
6147 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6149 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6151 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6152 DoEndDragResizeCol();
6154 // Note: we are ending the event *after* doing
6155 // default processing in this case
6157 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6163 // ------------ Right button down
6165 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6167 DisableCellEditControl();
6168 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6173 // no default action at the moment
6177 // ------------ Right double click
6179 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6181 DisableCellEditControl();
6182 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6187 // no default action at the moment
6191 // ------------ Moving and no button action
6193 else if ( event
.Moving() && !event
.IsButton() )
6195 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6197 // out of grid cell area
6198 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6202 int dragRow
= YToEdgeOfRow( y
);
6203 int dragCol
= XToEdgeOfCol( x
);
6205 // Dragging on the corner of a cell to resize in both
6206 // directions is not implemented yet...
6208 if ( dragRow
>= 0 && dragCol
>= 0 )
6210 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6216 m_dragRowOrCol
= dragRow
;
6218 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6220 if ( CanDragRowSize() && CanDragGridSize() )
6221 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6224 else if ( dragCol
>= 0 )
6226 m_dragRowOrCol
= dragCol
;
6228 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6230 if ( CanDragColSize() && CanDragGridSize() )
6231 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6234 else // Neither on a row or col edge
6236 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6238 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6244 void wxGrid::DoEndDragResizeRow()
6246 if ( m_dragLastPos
>= 0 )
6248 // erase the last line and resize the row
6250 int cw
, ch
, left
, dummy
;
6251 m_gridWin
->GetClientSize( &cw
, &ch
);
6252 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6254 wxClientDC
dc( m_gridWin
);
6256 dc
.SetLogicalFunction( wxINVERT
);
6257 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6258 HideCellEditControl();
6259 SaveEditControlValue();
6261 int rowTop
= GetRowTop(m_dragRowOrCol
);
6262 SetRowSize( m_dragRowOrCol
,
6263 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6265 if ( !GetBatchCount() )
6267 // Only needed to get the correct rect.y:
6268 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6270 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6271 rect
.width
= m_rowLabelWidth
;
6272 rect
.height
= ch
- rect
.y
;
6273 m_rowLabelWin
->Refresh( true, &rect
);
6276 // if there is a multicell block, paint all of it
6279 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6280 int leftCol
= XToCol(left
);
6281 int rightCol
= internalXToCol(left
+ cw
);
6284 for (i
=leftCol
; i
<rightCol
; i
++)
6286 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6287 if (cell_rows
< subtract_rows
)
6288 subtract_rows
= cell_rows
;
6290 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6291 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6292 rect
.height
= ch
- rect
.y
;
6295 m_gridWin
->Refresh( false, &rect
);
6298 ShowCellEditControl();
6303 void wxGrid::DoEndDragResizeCol()
6305 if ( m_dragLastPos
>= 0 )
6307 // erase the last line and resize the col
6309 int cw
, ch
, dummy
, top
;
6310 m_gridWin
->GetClientSize( &cw
, &ch
);
6311 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6313 wxClientDC
dc( m_gridWin
);
6315 dc
.SetLogicalFunction( wxINVERT
);
6316 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6317 HideCellEditControl();
6318 SaveEditControlValue();
6320 int colLeft
= GetColLeft(m_dragRowOrCol
);
6321 SetColSize( m_dragRowOrCol
,
6322 wxMax( m_dragLastPos
- colLeft
,
6323 GetColMinimalWidth(m_dragRowOrCol
) ) );
6325 if ( !GetBatchCount() )
6327 // Only needed to get the correct rect.x:
6328 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6330 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6331 rect
.width
= cw
- rect
.x
;
6332 rect
.height
= m_colLabelHeight
;
6333 m_colLabelWin
->Refresh( true, &rect
);
6336 // if there is a multicell block, paint all of it
6339 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6340 int topRow
= YToRow(top
);
6341 int bottomRow
= internalYToRow(top
+ cw
);
6344 for (i
=topRow
; i
<bottomRow
; i
++)
6346 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6347 if (cell_cols
< subtract_cols
)
6348 subtract_cols
= cell_cols
;
6351 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6352 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6353 rect
.width
= cw
- rect
.x
;
6357 m_gridWin
->Refresh( false, &rect
);
6360 ShowCellEditControl();
6364 void wxGrid::DoEndDragMoveCol()
6366 //The user clicked on the column but didn't actually drag
6367 if ( m_dragLastPos
< 0 )
6369 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6374 if ( m_moveToCol
== -1 )
6375 newPos
= m_numCols
- 1;
6378 newPos
= GetColPos( m_moveToCol
);
6379 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6383 SetColPos( m_dragRowOrCol
, newPos
);
6386 void wxGrid::SetColPos( int colID
, int newPos
)
6388 if ( m_colAt
.IsEmpty() )
6390 m_colAt
.Alloc( m_numCols
);
6393 for ( i
= 0; i
< m_numCols
; i
++ )
6399 int oldPos
= GetColPos( colID
);
6401 //Reshuffle the m_colAt array
6402 if ( newPos
> oldPos
)
6405 for ( i
= oldPos
; i
< newPos
; i
++ )
6407 m_colAt
[i
] = m_colAt
[i
+1];
6413 for ( i
= oldPos
; i
> newPos
; i
-- )
6415 m_colAt
[i
] = m_colAt
[i
-1];
6419 m_colAt
[newPos
] = colID
;
6421 //Recalculate the column rights
6422 if ( !m_colWidths
.IsEmpty() )
6426 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6428 int colID
= GetColAt( colPos
);
6430 colRight
+= m_colWidths
[colID
];
6431 m_colRights
[colID
] = colRight
;
6435 m_colLabelWin
->Refresh();
6436 m_gridWin
->Refresh();
6441 void wxGrid::EnableDragColMove( bool enable
)
6443 if ( m_canDragColMove
== enable
)
6446 m_canDragColMove
= enable
;
6448 if ( !m_canDragColMove
)
6452 //Recalculate the column rights
6453 if ( !m_colWidths
.IsEmpty() )
6457 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6459 colRight
+= m_colWidths
[colPos
];
6460 m_colRights
[colPos
] = colRight
;
6464 m_colLabelWin
->Refresh();
6465 m_gridWin
->Refresh();
6471 // ------ interaction with data model
6473 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6475 switch ( msg
.GetId() )
6477 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6478 return GetModelValues();
6480 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6481 return SetModelValues();
6483 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6484 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6485 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6486 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6487 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6488 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6489 return Redimension( msg
);
6496 // The behaviour of this function depends on the grid table class
6497 // Clear() function. For the default wxGridStringTable class the
6498 // behavious is to replace all cell contents with wxEmptyString but
6499 // not to change the number of rows or cols.
6501 void wxGrid::ClearGrid()
6505 if (IsCellEditControlEnabled())
6506 DisableCellEditControl();
6509 if (!GetBatchCount())
6510 m_gridWin
->Refresh();
6514 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6518 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6524 if (IsCellEditControlEnabled())
6525 DisableCellEditControl();
6527 bool done
= m_table
->InsertRows( pos
, numRows
);
6530 // the table will have sent the results of the insert row
6531 // operation to this view object as a grid table message
6537 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6541 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6547 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6550 // the table will have sent the results of the append row
6551 // operation to this view object as a grid table message
6557 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6561 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6567 if (IsCellEditControlEnabled())
6568 DisableCellEditControl();
6570 bool done
= m_table
->DeleteRows( pos
, numRows
);
6572 // the table will have sent the results of the delete row
6573 // operation to this view object as a grid table message
6579 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6583 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6589 if (IsCellEditControlEnabled())
6590 DisableCellEditControl();
6592 bool done
= m_table
->InsertCols( pos
, numCols
);
6594 // the table will have sent the results of the insert col
6595 // operation to this view object as a grid table message
6601 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6605 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6611 bool done
= m_table
->AppendCols( numCols
);
6613 // the table will have sent the results of the append col
6614 // operation to this view object as a grid table message
6620 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6624 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6630 if (IsCellEditControlEnabled())
6631 DisableCellEditControl();
6633 bool done
= m_table
->DeleteCols( pos
, numCols
);
6635 // the table will have sent the results of the delete col
6636 // operation to this view object as a grid table message
6643 // ----- event handlers
6646 // Generate a grid event based on a mouse event and
6647 // return the result of ProcessEvent()
6649 int wxGrid::SendEvent( const wxEventType type
,
6651 wxMouseEvent
& mouseEv
)
6653 bool claimed
, vetoed
;
6655 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6657 int rowOrCol
= (row
== -1 ? col
: row
);
6659 wxGridSizeEvent
gridEvt( GetId(),
6663 mouseEv
.GetX() + GetRowLabelSize(),
6664 mouseEv
.GetY() + GetColLabelSize(),
6665 mouseEv
.ControlDown(),
6666 mouseEv
.ShiftDown(),
6668 mouseEv
.MetaDown() );
6670 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6671 vetoed
= !gridEvt
.IsAllowed();
6673 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6675 // Right now, it should _never_ end up here!
6676 wxGridRangeSelectEvent
gridEvt( GetId(),
6680 m_selectingBottomRight
,
6682 mouseEv
.ControlDown(),
6683 mouseEv
.ShiftDown(),
6685 mouseEv
.MetaDown() );
6687 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6688 vetoed
= !gridEvt
.IsAllowed();
6690 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6691 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6692 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6693 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6695 wxPoint pos
= mouseEv
.GetPosition();
6697 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6698 pos
.y
+= GetColLabelSize();
6699 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6700 pos
.x
+= GetRowLabelSize();
6702 wxGridEvent
gridEvt( GetId(),
6709 mouseEv
.ControlDown(),
6710 mouseEv
.ShiftDown(),
6712 mouseEv
.MetaDown() );
6713 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6714 vetoed
= !gridEvt
.IsAllowed();
6718 wxGridEvent
gridEvt( GetId(),
6722 mouseEv
.GetX() + GetRowLabelSize(),
6723 mouseEv
.GetY() + GetColLabelSize(),
6725 mouseEv
.ControlDown(),
6726 mouseEv
.ShiftDown(),
6728 mouseEv
.MetaDown() );
6729 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6730 vetoed
= !gridEvt
.IsAllowed();
6733 // A Veto'd event may not be `claimed' so test this first
6737 return claimed
? 1 : 0;
6740 // Generate a grid event of specified type and return the result
6741 // of ProcessEvent().
6743 int wxGrid::SendEvent( const wxEventType type
,
6746 bool claimed
, vetoed
;
6748 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6750 int rowOrCol
= (row
== -1 ? col
: row
);
6752 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6754 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6755 vetoed
= !gridEvt
.IsAllowed();
6759 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6761 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6762 vetoed
= !gridEvt
.IsAllowed();
6765 // A Veto'd event may not be `claimed' so test this first
6769 return claimed
? 1 : 0;
6772 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6774 // needed to prevent zillions of paint events on MSW
6778 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6780 // Don't do anything if between Begin/EndBatch...
6781 // EndBatch() will do all this on the last nested one anyway.
6782 if ( m_created
&& !GetBatchCount() )
6784 // Refresh to get correct scrolled position:
6785 wxScrolledWindow::Refresh(eraseb
, rect
);
6789 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6790 int width_label
, width_cell
, height_label
, height_cell
;
6793 // Copy rectangle can get scroll offsets..
6794 rect_x
= rect
->GetX();
6795 rect_y
= rect
->GetY();
6796 rectWidth
= rect
->GetWidth();
6797 rectHeight
= rect
->GetHeight();
6799 width_label
= m_rowLabelWidth
- rect_x
;
6800 if (width_label
> rectWidth
)
6801 width_label
= rectWidth
;
6803 height_label
= m_colLabelHeight
- rect_y
;
6804 if (height_label
> rectHeight
)
6805 height_label
= rectHeight
;
6807 if (rect_x
> m_rowLabelWidth
)
6809 x
= rect_x
- m_rowLabelWidth
;
6810 width_cell
= rectWidth
;
6815 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6818 if (rect_y
> m_colLabelHeight
)
6820 y
= rect_y
- m_colLabelHeight
;
6821 height_cell
= rectHeight
;
6826 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6829 // Paint corner label part intersecting rect.
6830 if ( width_label
> 0 && height_label
> 0 )
6832 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6833 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6836 // Paint col labels part intersecting rect.
6837 if ( width_cell
> 0 && height_label
> 0 )
6839 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6840 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6843 // Paint row labels part intersecting rect.
6844 if ( width_label
> 0 && height_cell
> 0 )
6846 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6847 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6850 // Paint cell area part intersecting rect.
6851 if ( width_cell
> 0 && height_cell
> 0 )
6853 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6854 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6859 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6860 m_colLabelWin
->Refresh(eraseb
, NULL
);
6861 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6862 m_gridWin
->Refresh(eraseb
, NULL
);
6867 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6869 if (m_targetWindow
!= this) // check whether initialisation has been done
6871 // reposition our children windows
6876 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6878 if ( m_inOnKeyDown
)
6880 // shouldn't be here - we are going round in circles...
6882 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6885 m_inOnKeyDown
= true;
6887 // propagate the event up and see if it gets processed
6888 wxWindow
*parent
= GetParent();
6889 wxKeyEvent
keyEvt( event
);
6890 keyEvt
.SetEventObject( parent
);
6892 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6894 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6896 if (event
.GetKeyCode() == WXK_RIGHT
)
6897 event
.m_keyCode
= WXK_LEFT
;
6898 else if (event
.GetKeyCode() == WXK_LEFT
)
6899 event
.m_keyCode
= WXK_RIGHT
;
6902 // try local handlers
6903 switch ( event
.GetKeyCode() )
6906 if ( event
.ControlDown() )
6907 MoveCursorUpBlock( event
.ShiftDown() );
6909 MoveCursorUp( event
.ShiftDown() );
6913 if ( event
.ControlDown() )
6914 MoveCursorDownBlock( event
.ShiftDown() );
6916 MoveCursorDown( event
.ShiftDown() );
6920 if ( event
.ControlDown() )
6921 MoveCursorLeftBlock( event
.ShiftDown() );
6923 MoveCursorLeft( event
.ShiftDown() );
6927 if ( event
.ControlDown() )
6928 MoveCursorRightBlock( event
.ShiftDown() );
6930 MoveCursorRight( event
.ShiftDown() );
6934 case WXK_NUMPAD_ENTER
:
6935 if ( event
.ControlDown() )
6937 event
.Skip(); // to let the edit control have the return
6941 if ( GetGridCursorRow() < GetNumberRows()-1 )
6943 MoveCursorDown( event
.ShiftDown() );
6947 // at the bottom of a column
6948 DisableCellEditControl();
6958 if (event
.ShiftDown())
6960 if ( GetGridCursorCol() > 0 )
6962 MoveCursorLeft( false );
6967 DisableCellEditControl();
6972 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6974 MoveCursorRight( false );
6979 DisableCellEditControl();
6985 if ( event
.ControlDown() )
6987 MakeCellVisible( 0, 0 );
6988 SetCurrentCell( 0, 0 );
6997 if ( event
.ControlDown() )
6999 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7000 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7017 // Ctrl-Space selects the current column, Shift-Space -- the
7018 // current row and Ctrl-Shift-Space -- everything
7019 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
7022 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
7026 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
7029 case wxMOD_CONTROL
| wxMOD_SHIFT
:
7030 m_selection
->SelectBlock(0, 0,
7031 m_numRows
- 1, m_numCols
- 1);
7035 if ( !IsEditable() )
7037 MoveCursorRight(false);
7040 //else: fall through
7053 m_inOnKeyDown
= false;
7056 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7058 // try local handlers
7060 if ( event
.GetKeyCode() == WXK_SHIFT
)
7062 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7063 m_selectingBottomRight
!= wxGridNoCellCoords
)
7067 m_selection
->SelectBlock(
7068 m_selectingTopLeft
.GetRow(),
7069 m_selectingTopLeft
.GetCol(),
7070 m_selectingBottomRight
.GetRow(),
7071 m_selectingBottomRight
.GetCol(),
7072 event
.ControlDown(),
7079 m_selectingTopLeft
= wxGridNoCellCoords
;
7080 m_selectingBottomRight
= wxGridNoCellCoords
;
7081 m_selectingKeyboard
= wxGridNoCellCoords
;
7085 void wxGrid::OnChar( wxKeyEvent
& event
)
7087 // is it possible to edit the current cell at all?
7088 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7090 // yes, now check whether the cells editor accepts the key
7091 int row
= m_currentCellCoords
.GetRow();
7092 int col
= m_currentCellCoords
.GetCol();
7093 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7094 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7096 // <F2> is special and will always start editing, for
7097 // other keys - ask the editor itself
7098 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7099 || editor
->IsAcceptedKey(event
) )
7101 // ensure cell is visble
7102 MakeCellVisible(row
, col
);
7103 EnableCellEditControl();
7105 // a problem can arise if the cell is not completely
7106 // visible (even after calling MakeCellVisible the
7107 // control is not created and calling StartingKey will
7109 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7110 editor
->StartingKey(event
);
7126 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7130 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7132 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7134 // the event has been intercepted - do nothing
7138 #if !defined(__WXMAC__)
7139 wxClientDC
dc( m_gridWin
);
7143 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7145 DisableCellEditControl();
7147 if ( IsVisible( m_currentCellCoords
, false ) )
7150 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7151 if ( !m_gridLinesEnabled
)
7159 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7161 // Otherwise refresh redraws the highlight!
7162 m_currentCellCoords
= coords
;
7164 #if defined(__WXMAC__)
7165 m_gridWin
->Refresh(true /*, & r */);
7167 DrawGridCellArea( dc
, cells
);
7168 DrawAllGridLines( dc
, r
);
7173 m_currentCellCoords
= coords
;
7175 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7176 #if !defined(__WXMAC__)
7177 DrawCellHighlight( dc
, attr
);
7182 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7185 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7189 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7192 rightCol
= GetNumberCols() - 1;
7194 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7197 bottomRow
= GetNumberRows() - 1;
7201 if ( topRow
> bottomRow
)
7208 if ( leftCol
> rightCol
)
7215 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7216 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7218 // First the case that we selected a completely new area
7219 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7220 m_selectingBottomRight
== wxGridNoCellCoords
)
7223 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7224 wxGridCellCoords ( bottomRow
, rightCol
) );
7225 m_gridWin
->Refresh( false, &rect
);
7228 // Now handle changing an existing selection area.
7229 else if ( m_selectingTopLeft
!= updateTopLeft
||
7230 m_selectingBottomRight
!= updateBottomRight
)
7232 // Compute two optimal update rectangles:
7233 // Either one rectangle is a real subset of the
7234 // other, or they are (almost) disjoint!
7236 bool need_refresh
[4];
7240 need_refresh
[3] = false;
7243 // Store intermediate values
7244 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7245 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7246 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7247 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7249 // Determine the outer/inner coordinates.
7250 if (oldLeft
> leftCol
)
7256 if (oldTop
> topRow
)
7262 if (oldRight
< rightCol
)
7265 oldRight
= rightCol
;
7268 if (oldBottom
< bottomRow
)
7271 oldBottom
= bottomRow
;
7275 // Now, either the stuff marked old is the outer
7276 // rectangle or we don't have a situation where one
7277 // is contained in the other.
7279 if ( oldLeft
< leftCol
)
7281 // Refresh the newly selected or deselected
7282 // area to the left of the old or new selection.
7283 need_refresh
[0] = true;
7284 rect
[0] = BlockToDeviceRect(
7285 wxGridCellCoords( oldTop
, oldLeft
),
7286 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7289 if ( oldTop
< topRow
)
7291 // Refresh the newly selected or deselected
7292 // area above the old or new selection.
7293 need_refresh
[1] = true;
7294 rect
[1] = BlockToDeviceRect(
7295 wxGridCellCoords( oldTop
, leftCol
),
7296 wxGridCellCoords( topRow
- 1, rightCol
) );
7299 if ( oldRight
> rightCol
)
7301 // Refresh the newly selected or deselected
7302 // area to the right of the old or new selection.
7303 need_refresh
[2] = true;
7304 rect
[2] = BlockToDeviceRect(
7305 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7306 wxGridCellCoords( oldBottom
, oldRight
) );
7309 if ( oldBottom
> bottomRow
)
7311 // Refresh the newly selected or deselected
7312 // area below the old or new selection.
7313 need_refresh
[3] = true;
7314 rect
[3] = BlockToDeviceRect(
7315 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7316 wxGridCellCoords( oldBottom
, rightCol
) );
7319 // various Refresh() calls
7320 for (i
= 0; i
< 4; i
++ )
7321 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7322 m_gridWin
->Refresh( false, &(rect
[i
]) );
7326 m_selectingTopLeft
= updateTopLeft
;
7327 m_selectingBottomRight
= updateBottomRight
;
7331 // ------ functions to get/send data (see also public functions)
7334 bool wxGrid::GetModelValues()
7336 // Hide the editor, so it won't hide a changed value.
7337 HideCellEditControl();
7341 // all we need to do is repaint the grid
7343 m_gridWin
->Refresh();
7350 bool wxGrid::SetModelValues()
7354 // Disable the editor, so it won't hide a changed value.
7355 // Do we also want to save the current value of the editor first?
7357 DisableCellEditControl();
7361 for ( row
= 0; row
< m_numRows
; row
++ )
7363 for ( col
= 0; col
< m_numCols
; col
++ )
7365 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7375 // Note - this function only draws cells that are in the list of
7376 // exposed cells (usually set from the update region by
7377 // CalcExposedCells)
7379 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7381 if ( !m_numRows
|| !m_numCols
)
7384 int i
, numCells
= cells
.GetCount();
7385 int row
, col
, cell_rows
, cell_cols
;
7386 wxGridCellCoordsArray redrawCells
;
7388 for ( i
= numCells
- 1; i
>= 0; i
-- )
7390 row
= cells
[i
].GetRow();
7391 col
= cells
[i
].GetCol();
7392 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7394 // If this cell is part of a multicell block, find owner for repaint
7395 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7397 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7398 bool marked
= false;
7399 for ( int j
= 0; j
< numCells
; j
++ )
7401 if ( cell
== cells
[j
] )
7410 int count
= redrawCells
.GetCount();
7411 for (int j
= 0; j
< count
; j
++)
7413 if ( cell
== redrawCells
[j
] )
7421 redrawCells
.Add( cell
);
7424 // don't bother drawing this cell
7428 // If this cell is empty, find cell to left that might want to overflow
7429 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7431 for ( int l
= 0; l
< cell_rows
; l
++ )
7433 // find a cell in this row to leave already marked for repaint
7435 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7436 if ((redrawCells
[k
].GetCol() < left
) &&
7437 (redrawCells
[k
].GetRow() == row
))
7439 left
= redrawCells
[k
].GetCol();
7443 left
= 0; // oh well
7445 for (int j
= col
- 1; j
>= left
; j
--)
7447 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7449 if (GetCellOverflow(row
+ l
, j
))
7451 wxGridCellCoords
cell(row
+ l
, j
);
7452 bool marked
= false;
7454 for (int k
= 0; k
< numCells
; k
++)
7456 if ( cell
== cells
[k
] )
7465 int count
= redrawCells
.GetCount();
7466 for (int k
= 0; k
< count
; k
++)
7468 if ( cell
== redrawCells
[k
] )
7475 redrawCells
.Add( cell
);
7484 DrawCell( dc
, cells
[i
] );
7487 numCells
= redrawCells
.GetCount();
7489 for ( i
= numCells
- 1; i
>= 0; i
-- )
7491 DrawCell( dc
, redrawCells
[i
] );
7495 void wxGrid::DrawGridSpace( wxDC
& dc
)
7498 m_gridWin
->GetClientSize( &cw
, &ch
);
7501 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7503 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7504 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7506 if ( right
> rightCol
|| bottom
> bottomRow
)
7509 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7511 dc
.SetBrush(GetDefaultCellBackgroundColour());
7512 dc
.SetPen( *wxTRANSPARENT_PEN
);
7514 if ( right
> rightCol
)
7516 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7519 if ( bottom
> bottomRow
)
7521 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7526 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7528 int row
= coords
.GetRow();
7529 int col
= coords
.GetCol();
7531 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7534 // we draw the cell border ourselves
7535 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7537 bool isCurrent
= coords
== m_currentCellCoords
;
7539 wxRect rect
= CellToRect( row
, col
);
7541 // if the editor is shown, we should use it and not the renderer
7542 // Note: However, only if it is really _shown_, i.e. not hidden!
7543 if ( isCurrent
&& IsCellEditControlShown() )
7545 // NB: this "#if..." is temporary and fixes a problem where the
7546 // edit control is erased by this code after being rendered.
7547 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7548 // implicitly, causing this out-of order render.
7549 #if !defined(__WXMAC__)
7550 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7551 editor
->PaintBackground(rect
, attr
);
7557 // but all the rest is drawn by the cell renderer and hence may be customized
7558 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7559 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7566 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7568 // don't show highlight when the grid doesn't have focus
7572 int row
= m_currentCellCoords
.GetRow();
7573 int col
= m_currentCellCoords
.GetCol();
7575 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7578 wxRect rect
= CellToRect(row
, col
);
7580 // hmmm... what could we do here to show that the cell is disabled?
7581 // for now, I just draw a thinner border than for the other ones, but
7582 // it doesn't look really good
7584 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7588 // The center of the drawn line is where the position/width/height of
7589 // the rectangle is actually at (on wxMSW at least), so the
7590 // size of the rectangle is reduced to compensate for the thickness of
7591 // the line. If this is too strange on non-wxMSW platforms then
7592 // please #ifdef this appropriately.
7593 rect
.x
+= penWidth
/ 2;
7594 rect
.y
+= penWidth
/ 2;
7595 rect
.width
-= penWidth
- 1;
7596 rect
.height
-= penWidth
- 1;
7598 // Now draw the rectangle
7599 // use the cellHighlightColour if the cell is inside a selection, this
7600 // will ensure the cell is always visible.
7601 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
7602 : m_cellHighlightColour
,
7604 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7605 dc
.DrawRectangle(rect
);
7609 // VZ: my experiments with 3D borders...
7611 // how to properly set colours for arbitrary bg?
7612 wxCoord x1
= rect
.x
,
7614 x2
= rect
.x
+ rect
.width
- 1,
7615 y2
= rect
.y
+ rect
.height
- 1;
7617 dc
.SetPen(*wxWHITE_PEN
);
7618 dc
.DrawLine(x1
, y1
, x2
, y1
);
7619 dc
.DrawLine(x1
, y1
, x1
, y2
);
7621 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7622 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7624 dc
.SetPen(*wxBLACK_PEN
);
7625 dc
.DrawLine(x1
, y2
, x2
, y2
);
7626 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7630 wxPen
wxGrid::GetDefaultGridLinePen()
7632 return wxPen(GetGridLineColour());
7635 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7637 return GetDefaultGridLinePen();
7640 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7642 return GetDefaultGridLinePen();
7645 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7647 int row
= coords
.GetRow();
7648 int col
= coords
.GetCol();
7649 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7653 wxRect rect
= CellToRect( row
, col
);
7655 // right hand border
7656 dc
.SetPen( GetColGridLinePen(col
) );
7657 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7658 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7661 dc
.SetPen( GetRowGridLinePen(row
) );
7662 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7663 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7666 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7668 // This if block was previously in wxGrid::OnPaint but that doesn't
7669 // seem to get called under wxGTK - MB
7671 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7672 m_numRows
&& m_numCols
)
7674 m_currentCellCoords
.Set(0, 0);
7677 if ( IsCellEditControlShown() )
7679 // don't show highlight when the edit control is shown
7683 // if the active cell was repainted, repaint its highlight too because it
7684 // might have been damaged by the grid lines
7685 size_t count
= cells
.GetCount();
7686 for ( size_t n
= 0; n
< count
; n
++ )
7688 wxGridCellCoords cell
= cells
[n
];
7690 // If we are using attributes, then we may have just exposed another
7691 // cell in a partially-visible merged cluster of cells. If the "anchor"
7692 // (upper left) cell of this merged cluster is the cell indicated by
7693 // m_currentCellCoords, then we need to refresh the cell highlight even
7694 // though the "anchor" itself is not part of our update segment.
7695 if ( CanHaveAttributes() )
7699 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
7702 cell
.SetRow(cell
.GetRow() + rows
);
7705 cell
.SetCol(cell
.GetCol() + cols
);
7708 if ( cell
== m_currentCellCoords
)
7710 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7711 DrawCellHighlight(dc
, attr
);
7719 // This is used to redraw all grid lines e.g. when the grid line colour
7722 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7724 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7727 int top
, bottom
, left
, right
;
7730 m_gridWin
->GetClientSize(&cw
, &ch
);
7731 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7732 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7734 // avoid drawing grid lines past the last row and col
7736 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7737 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7739 // no gridlines inside multicells, clip them out
7740 int leftCol
= GetColPos( internalXToCol(left
) );
7741 int topRow
= internalYToRow(top
);
7742 int rightCol
= GetColPos( internalXToCol(right
) );
7743 int bottomRow
= internalYToRow(bottom
);
7745 wxRegion
clippedcells(0, 0, cw
, ch
);
7747 int cell_rows
, cell_cols
;
7750 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
7752 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
7754 int i
= GetColAt( colPos
);
7756 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7757 if ((cell_rows
> 1) || (cell_cols
> 1))
7759 rect
= CellToRect(j
,i
);
7760 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7761 clippedcells
.Subtract(rect
);
7763 else if ((cell_rows
< 0) || (cell_cols
< 0))
7765 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7766 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7767 clippedcells
.Subtract(rect
);
7772 dc
.SetDeviceClippingRegion( clippedcells
);
7775 // horizontal grid lines
7776 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7778 int bot
= GetRowBottom(i
) - 1;
7785 dc
.SetPen( GetRowGridLinePen(i
) );
7786 dc
.DrawLine( left
, bot
, right
, bot
);
7790 // vertical grid lines
7791 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7793 int i
= GetColAt( colPos
);
7795 int colRight
= GetColRight(i
);
7797 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7801 if ( colRight
> right
)
7804 if ( colRight
>= left
)
7806 dc
.SetPen( GetColGridLinePen(i
) );
7807 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7811 dc
.DestroyClippingRegion();
7814 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7819 const size_t numLabels
= rows
.GetCount();
7820 for ( size_t i
= 0; i
< numLabels
; i
++ )
7822 DrawRowLabel( dc
, rows
[i
] );
7826 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7828 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7833 int rowTop
= GetRowTop(row
),
7834 rowBottom
= GetRowBottom(row
) - 1;
7836 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
7837 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7838 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7839 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7841 dc
.SetPen( *wxWHITE_PEN
);
7842 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7843 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7845 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7846 dc
.SetTextForeground( GetLabelTextColour() );
7847 dc
.SetFont( GetLabelFont() );
7850 GetRowLabelAlignment( &hAlign
, &vAlign
);
7853 rect
.SetY( GetRowTop(row
) + 2 );
7854 rect
.SetWidth( m_rowLabelWidth
- 4 );
7855 rect
.SetHeight( GetRowHeight(row
) - 4 );
7856 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7859 void wxGrid::SetUseNativeColLabels( bool native
)
7861 m_nativeColumnLabels
= native
;
7864 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7865 SetColLabelSize( height
);
7868 m_colLabelWin
->Refresh();
7869 m_cornerLabelWin
->Refresh();
7872 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7877 const size_t numLabels
= cols
.GetCount();
7878 for ( size_t i
= 0; i
< numLabels
; i
++ )
7880 DrawColLabel( dc
, cols
[i
] );
7884 void wxGrid::DrawCornerLabel(wxDC
& dc
)
7886 if ( m_nativeColumnLabels
)
7888 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
7891 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
7895 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
7896 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
7897 m_rowLabelWidth
- 1, 0 );
7898 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
7899 0, m_colLabelHeight
- 1 );
7900 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
7901 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
7903 dc
.SetPen( *wxWHITE_PEN
);
7904 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
7905 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
7909 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
7911 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7914 int colLeft
= GetColLeft(col
);
7916 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
7918 if ( m_nativeColumnLabels
)
7920 wxRendererNative::Get().DrawHeaderButton(m_colLabelWin
, dc
, rect
, 0);
7924 int colRight
= GetColRight(col
) - 1;
7926 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
7927 dc
.DrawLine( colRight
, 0,
7928 colRight
, m_colLabelHeight
- 1 );
7929 dc
.DrawLine( colLeft
, 0,
7931 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7932 colRight
+ 1, m_colLabelHeight
- 1 );
7934 dc
.SetPen( *wxWHITE_PEN
);
7935 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7936 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7939 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7940 dc
.SetTextForeground( GetLabelTextColour() );
7941 dc
.SetFont( GetLabelFont() );
7944 GetColLabelAlignment( &hAlign
, &vAlign
);
7945 const int orient
= GetColLabelTextOrientation();
7948 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
7951 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
7952 // we just have to add textOrientation support
7953 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7954 const wxString
& value
,
7958 int textOrientation
)
7960 wxArrayString lines
;
7962 StringToLines( value
, lines
);
7964 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
7967 void wxGrid::DrawTextRectangle(wxDC
& dc
,
7968 const wxArrayString
& lines
,
7972 int textOrientation
)
7974 if ( lines
.empty() )
7977 wxDCClipper
clip(dc
, rect
);
7982 if ( textOrientation
== wxHORIZONTAL
)
7983 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
7985 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7989 switch ( vertAlign
)
7991 case wxALIGN_BOTTOM
:
7992 if ( textOrientation
== wxHORIZONTAL
)
7993 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7995 x
= rect
.x
+ rect
.width
- textWidth
;
7998 case wxALIGN_CENTRE
:
7999 if ( textOrientation
== wxHORIZONTAL
)
8000 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8002 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8007 if ( textOrientation
== wxHORIZONTAL
)
8014 // Align each line of a multi-line label
8015 size_t nLines
= lines
.GetCount();
8016 for ( size_t l
= 0; l
< nLines
; l
++ )
8018 const wxString
& line
= lines
[l
];
8022 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8026 wxCoord lineWidth
= 0,
8028 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8030 switch ( horizAlign
)
8033 if ( textOrientation
== wxHORIZONTAL
)
8034 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8036 y
= rect
.y
+ lineWidth
+ 1;
8039 case wxALIGN_CENTRE
:
8040 if ( textOrientation
== wxHORIZONTAL
)
8041 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8043 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8048 if ( textOrientation
== wxHORIZONTAL
)
8051 y
= rect
.y
+ rect
.height
- 1;
8055 if ( textOrientation
== wxHORIZONTAL
)
8057 dc
.DrawText( line
, x
, y
);
8062 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8068 // Split multi-line text up into an array of strings.
8069 // Any existing contents of the string array are preserved.
8071 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8072 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8076 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8077 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8079 while ( startPos
< (int)tVal
.length() )
8081 pos
= tVal
.Mid(startPos
).Find( eol
);
8086 else if ( pos
== 0 )
8088 lines
.Add( wxEmptyString
);
8092 lines
.Add( tVal
.Mid(startPos
, pos
) );
8095 startPos
+= pos
+ 1;
8098 if ( startPos
< (int)tVal
.length() )
8100 lines
.Add( tVal
.Mid( startPos
) );
8104 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8105 const wxArrayString
& lines
,
8106 long *width
, long *height
) const
8110 wxCoord lineW
= 0, lineH
= 0;
8113 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8115 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8116 w
= wxMax( w
, lineW
);
8125 // ------ Batch processing.
8127 void wxGrid::EndBatch()
8129 if ( m_batchCount
> 0 )
8132 if ( !m_batchCount
)
8135 m_rowLabelWin
->Refresh();
8136 m_colLabelWin
->Refresh();
8137 m_cornerLabelWin
->Refresh();
8138 m_gridWin
->Refresh();
8143 // Use this, rather than wxWindow::Refresh(), to force an immediate
8144 // repainting of the grid. Has no effect if you are already inside a
8145 // BeginBatch / EndBatch block.
8147 void wxGrid::ForceRefresh()
8153 bool wxGrid::Enable(bool enable
)
8155 if ( !wxScrolledWindow::Enable(enable
) )
8158 // redraw in the new state
8159 m_gridWin
->Refresh();
8165 // ------ Edit control functions
8168 void wxGrid::EnableEditing( bool edit
)
8170 // TODO: improve this ?
8172 if ( edit
!= m_editable
)
8175 EnableCellEditControl(edit
);
8180 void wxGrid::EnableCellEditControl( bool enable
)
8185 if ( enable
!= m_cellEditCtrlEnabled
)
8189 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8192 // this should be checked by the caller!
8193 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8195 // do it before ShowCellEditControl()
8196 m_cellEditCtrlEnabled
= enable
;
8198 ShowCellEditControl();
8202 //FIXME:add veto support
8203 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8205 HideCellEditControl();
8206 SaveEditControlValue();
8208 // do it after HideCellEditControl()
8209 m_cellEditCtrlEnabled
= enable
;
8214 bool wxGrid::IsCurrentCellReadOnly() const
8217 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8218 bool readonly
= attr
->IsReadOnly();
8224 bool wxGrid::CanEnableCellControl() const
8226 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8227 !IsCurrentCellReadOnly();
8230 bool wxGrid::IsCellEditControlEnabled() const
8232 // the cell edit control might be disable for all cells or just for the
8233 // current one if it's read only
8234 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8237 bool wxGrid::IsCellEditControlShown() const
8239 bool isShown
= false;
8241 if ( m_cellEditCtrlEnabled
)
8243 int row
= m_currentCellCoords
.GetRow();
8244 int col
= m_currentCellCoords
.GetCol();
8245 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8246 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8251 if ( editor
->IsCreated() )
8253 isShown
= editor
->GetControl()->IsShown();
8263 void wxGrid::ShowCellEditControl()
8265 if ( IsCellEditControlEnabled() )
8267 if ( !IsVisible( m_currentCellCoords
, false ) )
8269 m_cellEditCtrlEnabled
= false;
8274 wxRect rect
= CellToRect( m_currentCellCoords
);
8275 int row
= m_currentCellCoords
.GetRow();
8276 int col
= m_currentCellCoords
.GetCol();
8278 // if this is part of a multicell, find owner (topleft)
8279 int cell_rows
, cell_cols
;
8280 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8281 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8285 m_currentCellCoords
.SetRow( row
);
8286 m_currentCellCoords
.SetCol( col
);
8289 // erase the highlight and the cell contents because the editor
8290 // might not cover the entire cell
8291 wxClientDC
dc( m_gridWin
);
8293 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8294 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
8295 dc
.SetPen(*wxTRANSPARENT_PEN
);
8296 dc
.DrawRectangle(rect
);
8298 // convert to scrolled coords
8299 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8305 // cell is shifted by one pixel
8306 // However, don't allow x or y to become negative
8307 // since the SetSize() method interprets that as
8314 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8315 if ( !editor
->IsCreated() )
8317 editor
->Create(m_gridWin
, wxID_ANY
,
8318 new wxGridCellEditorEvtHandler(this, editor
));
8320 wxGridEditorCreatedEvent
evt(GetId(),
8321 wxEVT_GRID_EDITOR_CREATED
,
8325 editor
->GetControl());
8326 GetEventHandler()->ProcessEvent(evt
);
8329 // resize editor to overflow into righthand cells if allowed
8330 int maxWidth
= rect
.width
;
8331 wxString value
= GetCellValue(row
, col
);
8332 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8335 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8336 if (maxWidth
< rect
.width
)
8337 maxWidth
= rect
.width
;
8340 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8341 if (rect
.x
+ maxWidth
> client_right
)
8342 maxWidth
= client_right
- rect
.x
;
8344 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8346 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8347 // may have changed earlier
8348 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8351 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8353 // looks weird going over a multicell
8354 if (m_table
->IsEmptyCell( row
, i
) &&
8355 (rect
.width
< maxWidth
) && (c_rows
== 1))
8357 rect
.width
+= GetColWidth( i
);
8363 if (rect
.GetRight() > client_right
)
8364 rect
.SetRight( client_right
- 1 );
8367 editor
->SetCellAttr( attr
);
8368 editor
->SetSize( rect
);
8370 editor
->GetControl()->Move(
8371 editor
->GetControl()->GetPosition().x
+ nXMove
,
8372 editor
->GetControl()->GetPosition().y
);
8373 editor
->Show( true, attr
);
8375 // recalc dimensions in case we need to
8376 // expand the scrolled window to account for editor
8379 editor
->BeginEdit(row
, col
, this);
8380 editor
->SetCellAttr(NULL
);
8388 void wxGrid::HideCellEditControl()
8390 if ( IsCellEditControlEnabled() )
8392 int row
= m_currentCellCoords
.GetRow();
8393 int col
= m_currentCellCoords
.GetCol();
8395 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8396 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8397 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
8398 editor
->Show( false );
8402 // return the focus to the grid itself if the editor had it
8404 // note that we must not do this unconditionally to avoid stealing
8405 // focus from the window which just received it if we are hiding the
8406 // editor precisely because we lost focus
8407 if ( editorHadFocus
)
8408 m_gridWin
->SetFocus();
8410 // refresh whole row to the right
8411 wxRect
rect( CellToRect(row
, col
) );
8412 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8413 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8416 // ensure that the pixels under the focus ring get refreshed as well
8417 rect
.Inflate(10, 10);
8420 m_gridWin
->Refresh( false, &rect
);
8424 void wxGrid::SaveEditControlValue()
8426 if ( IsCellEditControlEnabled() )
8428 int row
= m_currentCellCoords
.GetRow();
8429 int col
= m_currentCellCoords
.GetCol();
8431 wxString oldval
= GetCellValue(row
, col
);
8433 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8434 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8435 bool changed
= editor
->EndEdit(row
, col
, this);
8442 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8443 m_currentCellCoords
.GetRow(),
8444 m_currentCellCoords
.GetCol() ) < 0 )
8446 // Event has been vetoed, set the data back.
8447 SetCellValue(row
, col
, oldval
);
8454 // ------ Grid location functions
8455 // Note that all of these functions work with the logical coordinates of
8456 // grid cells and labels so you will need to convert from device
8457 // coordinates for mouse events etc.
8460 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8462 int row
= YToRow(y
);
8463 int col
= XToCol(x
);
8465 if ( row
== -1 || col
== -1 )
8467 coords
= wxGridNoCellCoords
;
8471 coords
.Set( row
, col
);
8475 // Internal Helper function for computing row or column from some
8476 // (unscrolled) coordinate value, using either
8477 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8478 // of m_rowBottoms/m_ColRights to speed up the search!
8480 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8481 const wxArrayInt
& BorderArray
, int nMax
,
8485 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8490 size_t i_max
= coord
/ defaultDist
,
8493 if (BorderArray
.IsEmpty())
8495 if ((int) i_max
< nMax
)
8497 return clipToMinMax
? nMax
- 1 : -1;
8500 if ( i_max
>= BorderArray
.GetCount())
8502 i_max
= BorderArray
.GetCount() - 1;
8506 if ( coord
>= BorderArray
[i_max
])
8510 i_max
= coord
/ minDist
;
8512 i_max
= BorderArray
.GetCount() - 1;
8515 if ( i_max
>= BorderArray
.GetCount())
8516 i_max
= BorderArray
.GetCount() - 1;
8519 if ( coord
>= BorderArray
[i_max
])
8520 return clipToMinMax
? (int)i_max
: -1;
8521 if ( coord
< BorderArray
[0] )
8524 while ( i_max
- i_min
> 0 )
8526 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8527 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8528 if (coord
>= BorderArray
[ i_max
- 1])
8532 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8533 if (coord
< BorderArray
[median
])
8542 int wxGrid::YToRow( int y
) const
8544 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8545 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8548 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8551 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8553 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8555 int maxPos
= x
/ m_defaultColWidth
;
8558 if (m_colRights
.IsEmpty())
8560 if(maxPos
< m_numCols
)
8561 return GetColAt( maxPos
);
8562 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8565 if ( maxPos
>= m_numCols
)
8566 maxPos
= m_numCols
- 1;
8569 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8572 if (m_minAcceptableColWidth
)
8573 maxPos
= x
/ m_minAcceptableColWidth
;
8575 maxPos
= m_numCols
- 1;
8577 if ( maxPos
>= m_numCols
)
8578 maxPos
= m_numCols
- 1;
8581 //X is beyond the last column
8582 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8583 return clipToMinMax
? GetColAt( maxPos
) : -1;
8585 //X is before the first column
8586 if ( x
< m_colRights
[GetColAt( 0 )] )
8587 return GetColAt( 0 );
8589 //Perform a binary search
8590 while ( maxPos
- minPos
> 0 )
8592 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8593 0, _T("wxGrid: internal error in XToCol"));
8595 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8596 return GetColAt( maxPos
);
8599 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8600 if (x
< m_colRights
[GetColAt( median
)])
8605 return GetColAt( maxPos
);
8608 // return the row number that that the y coord is near
8609 // the edge of, or -1 if not near an edge.
8610 // coords can only possibly be near an edge if
8611 // (a) the row/column is large enough to still allow for an "inner" area
8612 // that is _not_ nead the edge (i.e., if the height/width is smaller
8613 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8616 // (b) resizing rows/columns (the thing for which edge detection is
8617 // relevant at all) is enabled.
8619 int wxGrid::YToEdgeOfRow( int y
) const
8622 i
= internalYToRow(y
);
8624 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8626 // We know that we are in row i, test whether we are
8627 // close enough to lower or upper border, respectively.
8628 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8630 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8637 // return the col number that that the x coord is near the edge of, or
8638 // -1 if not near an edge
8639 // See comment at YToEdgeOfRow for conditions on edge detection.
8641 int wxGrid::XToEdgeOfCol( int x
) const
8644 i
= internalXToCol(x
);
8646 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8648 // We know that we are in column i; test whether we are
8649 // close enough to right or left border, respectively.
8650 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8652 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8659 wxRect
wxGrid::CellToRect( int row
, int col
) const
8661 wxRect
rect( -1, -1, -1, -1 );
8663 if ( row
>= 0 && row
< m_numRows
&&
8664 col
>= 0 && col
< m_numCols
)
8666 int i
, cell_rows
, cell_cols
;
8667 rect
.width
= rect
.height
= 0;
8668 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8669 // if negative then find multicell owner
8674 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8676 rect
.x
= GetColLeft(col
);
8677 rect
.y
= GetRowTop(row
);
8678 for (i
=col
; i
< col
+ cell_cols
; i
++)
8679 rect
.width
+= GetColWidth(i
);
8680 for (i
=row
; i
< row
+ cell_rows
; i
++)
8681 rect
.height
+= GetRowHeight(i
);
8684 // if grid lines are enabled, then the area of the cell is a bit smaller
8685 if (m_gridLinesEnabled
)
8694 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8696 // get the cell rectangle in logical coords
8698 wxRect
r( CellToRect( row
, col
) );
8700 // convert to device coords
8702 int left
, top
, right
, bottom
;
8703 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8704 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8706 // check against the client area of the grid window
8708 m_gridWin
->GetClientSize( &cw
, &ch
);
8710 if ( wholeCellVisible
)
8712 // is the cell wholly visible ?
8713 return ( left
>= 0 && right
<= cw
&&
8714 top
>= 0 && bottom
<= ch
);
8718 // is the cell partly visible ?
8720 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8721 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8725 // make the specified cell location visible by doing a minimal amount
8728 void wxGrid::MakeCellVisible( int row
, int col
)
8731 int xpos
= -1, ypos
= -1;
8733 if ( row
>= 0 && row
< m_numRows
&&
8734 col
>= 0 && col
< m_numCols
)
8736 // get the cell rectangle in logical coords
8737 wxRect
r( CellToRect( row
, col
) );
8739 // convert to device coords
8740 int left
, top
, right
, bottom
;
8741 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8742 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8745 m_gridWin
->GetClientSize( &cw
, &ch
);
8751 else if ( bottom
> ch
)
8753 int h
= r
.GetHeight();
8755 for ( i
= row
- 1; i
>= 0; i
-- )
8757 int rowHeight
= GetRowHeight(i
);
8758 if ( h
+ rowHeight
> ch
)
8765 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8766 // have rounding errors (this is important, because if we do,
8767 // we might not scroll at all and some cells won't be redrawn)
8769 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8770 // so just add a full scroll unit...
8771 ypos
+= m_scrollLineY
;
8774 // special handling for wide cells - show always left part of the cell!
8775 // Otherwise, e.g. when stepping from row to row, it would jump between
8776 // left and right part of the cell on every step!
8778 if ( left
< 0 || (right
- left
) >= cw
)
8782 else if ( right
> cw
)
8784 // position the view so that the cell is on the right
8786 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8787 xpos
= x0
+ (right
- cw
);
8789 // see comment for ypos above
8790 xpos
+= m_scrollLineX
;
8793 if ( xpos
!= -1 || ypos
!= -1 )
8796 xpos
/= m_scrollLineX
;
8798 ypos
/= m_scrollLineY
;
8799 Scroll( xpos
, ypos
);
8806 // ------ Grid cursor movement functions
8809 bool wxGrid::MoveCursorUp( bool expandSelection
)
8811 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8812 m_currentCellCoords
.GetRow() >= 0 )
8814 if ( expandSelection
)
8816 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8817 m_selectingKeyboard
= m_currentCellCoords
;
8818 if ( m_selectingKeyboard
.GetRow() > 0 )
8820 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8821 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8822 m_selectingKeyboard
.GetCol() );
8823 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8826 else if ( m_currentCellCoords
.GetRow() > 0 )
8828 int row
= m_currentCellCoords
.GetRow() - 1;
8829 int col
= m_currentCellCoords
.GetCol();
8831 MakeCellVisible( row
, col
);
8832 SetCurrentCell( row
, col
);
8843 bool wxGrid::MoveCursorDown( bool expandSelection
)
8845 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8846 m_currentCellCoords
.GetRow() < m_numRows
)
8848 if ( expandSelection
)
8850 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8851 m_selectingKeyboard
= m_currentCellCoords
;
8852 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8854 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8855 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8856 m_selectingKeyboard
.GetCol() );
8857 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8860 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8862 int row
= m_currentCellCoords
.GetRow() + 1;
8863 int col
= m_currentCellCoords
.GetCol();
8865 MakeCellVisible( row
, col
);
8866 SetCurrentCell( row
, col
);
8877 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8879 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8880 m_currentCellCoords
.GetCol() >= 0 )
8882 if ( expandSelection
)
8884 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8885 m_selectingKeyboard
= m_currentCellCoords
;
8886 if ( m_selectingKeyboard
.GetCol() > 0 )
8888 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8889 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8890 m_selectingKeyboard
.GetCol() );
8891 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8894 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8896 int row
= m_currentCellCoords
.GetRow();
8897 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8900 MakeCellVisible( row
, col
);
8901 SetCurrentCell( row
, col
);
8912 bool wxGrid::MoveCursorRight( bool expandSelection
)
8914 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8915 m_currentCellCoords
.GetCol() < m_numCols
)
8917 if ( expandSelection
)
8919 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8920 m_selectingKeyboard
= m_currentCellCoords
;
8921 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8923 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8924 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8925 m_selectingKeyboard
.GetCol() );
8926 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8929 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8931 int row
= m_currentCellCoords
.GetRow();
8932 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8935 MakeCellVisible( row
, col
);
8936 SetCurrentCell( row
, col
);
8947 bool wxGrid::MovePageUp()
8949 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8952 int row
= m_currentCellCoords
.GetRow();
8956 m_gridWin
->GetClientSize( &cw
, &ch
);
8958 int y
= GetRowTop(row
);
8959 int newRow
= internalYToRow( y
- ch
+ 1 );
8961 if ( newRow
== row
)
8963 // row > 0, so newRow can never be less than 0 here.
8967 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8968 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8976 bool wxGrid::MovePageDown()
8978 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8981 int row
= m_currentCellCoords
.GetRow();
8982 if ( (row
+ 1) < m_numRows
)
8985 m_gridWin
->GetClientSize( &cw
, &ch
);
8987 int y
= GetRowTop(row
);
8988 int newRow
= internalYToRow( y
+ ch
);
8989 if ( newRow
== row
)
8991 // row < m_numRows, so newRow can't overflow here.
8995 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8996 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9004 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9007 m_currentCellCoords
!= wxGridNoCellCoords
&&
9008 m_currentCellCoords
.GetRow() > 0 )
9010 int row
= m_currentCellCoords
.GetRow();
9011 int col
= m_currentCellCoords
.GetCol();
9013 if ( m_table
->IsEmptyCell(row
, col
) )
9015 // starting in an empty cell: find the next block of
9021 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9025 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9027 // starting at the top of a block: find the next block
9033 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9039 // starting within a block: find the top of the block
9044 if ( m_table
->IsEmptyCell(row
, col
) )
9052 MakeCellVisible( row
, col
);
9053 if ( expandSelection
)
9055 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9056 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9061 SetCurrentCell( row
, col
);
9070 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9073 m_currentCellCoords
!= wxGridNoCellCoords
&&
9074 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9076 int row
= m_currentCellCoords
.GetRow();
9077 int col
= m_currentCellCoords
.GetCol();
9079 if ( m_table
->IsEmptyCell(row
, col
) )
9081 // starting in an empty cell: find the next block of
9084 while ( row
< m_numRows
- 1 )
9087 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9091 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9093 // starting at the bottom of a block: find the next block
9096 while ( row
< m_numRows
- 1 )
9099 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9105 // starting within a block: find the bottom of the block
9107 while ( row
< m_numRows
- 1 )
9110 if ( m_table
->IsEmptyCell(row
, col
) )
9118 MakeCellVisible( row
, col
);
9119 if ( expandSelection
)
9121 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9122 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9127 SetCurrentCell( row
, col
);
9136 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9139 m_currentCellCoords
!= wxGridNoCellCoords
&&
9140 m_currentCellCoords
.GetCol() > 0 )
9142 int row
= m_currentCellCoords
.GetRow();
9143 int col
= m_currentCellCoords
.GetCol();
9145 if ( m_table
->IsEmptyCell(row
, col
) )
9147 // starting in an empty cell: find the next block of
9153 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9157 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9159 // starting at the left of a block: find the next block
9165 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9171 // starting within a block: find the left of the block
9176 if ( m_table
->IsEmptyCell(row
, col
) )
9184 MakeCellVisible( row
, col
);
9185 if ( expandSelection
)
9187 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9188 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9193 SetCurrentCell( row
, col
);
9202 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9205 m_currentCellCoords
!= wxGridNoCellCoords
&&
9206 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9208 int row
= m_currentCellCoords
.GetRow();
9209 int col
= m_currentCellCoords
.GetCol();
9211 if ( m_table
->IsEmptyCell(row
, col
) )
9213 // starting in an empty cell: find the next block of
9216 while ( col
< m_numCols
- 1 )
9219 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9223 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9225 // starting at the right of a block: find the next block
9228 while ( col
< m_numCols
- 1 )
9231 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9237 // starting within a block: find the right of the block
9239 while ( col
< m_numCols
- 1 )
9242 if ( m_table
->IsEmptyCell(row
, col
) )
9250 MakeCellVisible( row
, col
);
9251 if ( expandSelection
)
9253 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9254 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9259 SetCurrentCell( row
, col
);
9269 // ------ Label values and formatting
9272 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9275 *horiz
= m_rowLabelHorizAlign
;
9277 *vert
= m_rowLabelVertAlign
;
9280 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9283 *horiz
= m_colLabelHorizAlign
;
9285 *vert
= m_colLabelVertAlign
;
9288 int wxGrid::GetColLabelTextOrientation() const
9290 return m_colLabelTextOrientation
;
9293 wxString
wxGrid::GetRowLabelValue( int row
) const
9297 return m_table
->GetRowLabelValue( row
);
9307 wxString
wxGrid::GetColLabelValue( int col
) const
9311 return m_table
->GetColLabelValue( col
);
9321 void wxGrid::SetRowLabelSize( int width
)
9323 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9325 if ( width
== wxGRID_AUTOSIZE
)
9327 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9330 if ( width
!= m_rowLabelWidth
)
9334 m_rowLabelWin
->Show( false );
9335 m_cornerLabelWin
->Show( false );
9337 else if ( m_rowLabelWidth
== 0 )
9339 m_rowLabelWin
->Show( true );
9340 if ( m_colLabelHeight
> 0 )
9341 m_cornerLabelWin
->Show( true );
9344 m_rowLabelWidth
= width
;
9346 wxScrolledWindow::Refresh( true );
9350 void wxGrid::SetColLabelSize( int height
)
9352 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9354 if ( height
== wxGRID_AUTOSIZE
)
9356 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9359 if ( height
!= m_colLabelHeight
)
9363 m_colLabelWin
->Show( false );
9364 m_cornerLabelWin
->Show( false );
9366 else if ( m_colLabelHeight
== 0 )
9368 m_colLabelWin
->Show( true );
9369 if ( m_rowLabelWidth
> 0 )
9370 m_cornerLabelWin
->Show( true );
9373 m_colLabelHeight
= height
;
9375 wxScrolledWindow::Refresh( true );
9379 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9381 if ( m_labelBackgroundColour
!= colour
)
9383 m_labelBackgroundColour
= colour
;
9384 m_rowLabelWin
->SetBackgroundColour( colour
);
9385 m_colLabelWin
->SetBackgroundColour( colour
);
9386 m_cornerLabelWin
->SetBackgroundColour( colour
);
9388 if ( !GetBatchCount() )
9390 m_rowLabelWin
->Refresh();
9391 m_colLabelWin
->Refresh();
9392 m_cornerLabelWin
->Refresh();
9397 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9399 if ( m_labelTextColour
!= colour
)
9401 m_labelTextColour
= colour
;
9402 if ( !GetBatchCount() )
9404 m_rowLabelWin
->Refresh();
9405 m_colLabelWin
->Refresh();
9410 void wxGrid::SetLabelFont( const wxFont
& font
)
9413 if ( !GetBatchCount() )
9415 m_rowLabelWin
->Refresh();
9416 m_colLabelWin
->Refresh();
9420 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9422 // allow old (incorrect) defs to be used
9425 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9426 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9427 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9432 case wxTOP
: vert
= wxALIGN_TOP
; break;
9433 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9434 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9437 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9439 m_rowLabelHorizAlign
= horiz
;
9442 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9444 m_rowLabelVertAlign
= vert
;
9447 if ( !GetBatchCount() )
9449 m_rowLabelWin
->Refresh();
9453 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9455 // allow old (incorrect) defs to be used
9458 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9459 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9460 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9465 case wxTOP
: vert
= wxALIGN_TOP
; break;
9466 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9467 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9470 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9472 m_colLabelHorizAlign
= horiz
;
9475 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9477 m_colLabelVertAlign
= vert
;
9480 if ( !GetBatchCount() )
9482 m_colLabelWin
->Refresh();
9486 // Note: under MSW, the default column label font must be changed because it
9487 // does not support vertical printing
9489 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9490 // pGrid->SetLabelFont(font);
9491 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9493 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9495 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9496 m_colLabelTextOrientation
= textOrientation
;
9498 if ( !GetBatchCount() )
9499 m_colLabelWin
->Refresh();
9502 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9506 m_table
->SetRowLabelValue( row
, s
);
9507 if ( !GetBatchCount() )
9509 wxRect rect
= CellToRect( row
, 0 );
9510 if ( rect
.height
> 0 )
9512 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9514 rect
.width
= m_rowLabelWidth
;
9515 m_rowLabelWin
->Refresh( true, &rect
);
9521 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9525 m_table
->SetColLabelValue( col
, s
);
9526 if ( !GetBatchCount() )
9528 wxRect rect
= CellToRect( 0, col
);
9529 if ( rect
.width
> 0 )
9531 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9533 rect
.height
= m_colLabelHeight
;
9534 m_colLabelWin
->Refresh( true, &rect
);
9540 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9542 if ( m_gridLineColour
!= colour
)
9544 m_gridLineColour
= colour
;
9546 wxClientDC
dc( m_gridWin
);
9548 DrawAllGridLines( dc
, wxRegion() );
9552 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9554 if ( m_cellHighlightColour
!= colour
)
9556 m_cellHighlightColour
= colour
;
9558 wxClientDC
dc( m_gridWin
);
9560 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9561 DrawCellHighlight(dc
, attr
);
9566 void wxGrid::SetCellHighlightPenWidth(int width
)
9568 if (m_cellHighlightPenWidth
!= width
)
9570 m_cellHighlightPenWidth
= width
;
9572 // Just redrawing the cell highlight is not enough since that won't
9573 // make any visible change if the the thickness is getting smaller.
9574 int row
= m_currentCellCoords
.GetRow();
9575 int col
= m_currentCellCoords
.GetCol();
9576 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9579 wxRect rect
= CellToRect(row
, col
);
9580 m_gridWin
->Refresh(true, &rect
);
9584 void wxGrid::SetCellHighlightROPenWidth(int width
)
9586 if (m_cellHighlightROPenWidth
!= width
)
9588 m_cellHighlightROPenWidth
= width
;
9590 // Just redrawing the cell highlight is not enough since that won't
9591 // make any visible change if the the thickness is getting smaller.
9592 int row
= m_currentCellCoords
.GetRow();
9593 int col
= m_currentCellCoords
.GetCol();
9594 if ( row
== -1 || col
== -1 ||
9595 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9598 wxRect rect
= CellToRect(row
, col
);
9599 m_gridWin
->Refresh(true, &rect
);
9603 void wxGrid::EnableGridLines( bool enable
)
9605 if ( enable
!= m_gridLinesEnabled
)
9607 m_gridLinesEnabled
= enable
;
9609 if ( !GetBatchCount() )
9613 wxClientDC
dc( m_gridWin
);
9615 DrawAllGridLines( dc
, wxRegion() );
9619 m_gridWin
->Refresh();
9625 int wxGrid::GetDefaultRowSize() const
9627 return m_defaultRowHeight
;
9630 int wxGrid::GetRowSize( int row
) const
9632 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9634 return GetRowHeight(row
);
9637 int wxGrid::GetDefaultColSize() const
9639 return m_defaultColWidth
;
9642 int wxGrid::GetColSize( int col
) const
9644 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9646 return GetColWidth(col
);
9649 // ============================================================================
9650 // access to the grid attributes: each of them has a default value in the grid
9651 // itself and may be overidden on a per-cell basis
9652 // ============================================================================
9654 // ----------------------------------------------------------------------------
9655 // setting default attributes
9656 // ----------------------------------------------------------------------------
9658 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9660 m_defaultCellAttr
->SetBackgroundColour(col
);
9662 m_gridWin
->SetBackgroundColour(col
);
9666 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9668 m_defaultCellAttr
->SetTextColour(col
);
9671 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9673 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9676 void wxGrid::SetDefaultCellOverflow( bool allow
)
9678 m_defaultCellAttr
->SetOverflow(allow
);
9681 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9683 m_defaultCellAttr
->SetFont(font
);
9686 // For editors and renderers the type registry takes precedence over the
9687 // default attr, so we need to register the new editor/renderer for the string
9688 // data type in order to make setting a default editor/renderer appear to
9691 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9693 RegisterDataType(wxGRID_VALUE_STRING
,
9695 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9698 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9700 RegisterDataType(wxGRID_VALUE_STRING
,
9701 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9705 // ----------------------------------------------------------------------------
9706 // access to the default attributes
9707 // ----------------------------------------------------------------------------
9709 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9711 return m_defaultCellAttr
->GetBackgroundColour();
9714 wxColour
wxGrid::GetDefaultCellTextColour() const
9716 return m_defaultCellAttr
->GetTextColour();
9719 wxFont
wxGrid::GetDefaultCellFont() const
9721 return m_defaultCellAttr
->GetFont();
9724 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9726 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9729 bool wxGrid::GetDefaultCellOverflow() const
9731 return m_defaultCellAttr
->GetOverflow();
9734 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9736 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9739 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9741 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9744 // ----------------------------------------------------------------------------
9745 // access to cell attributes
9746 // ----------------------------------------------------------------------------
9748 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9750 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9751 wxColour colour
= attr
->GetBackgroundColour();
9757 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9759 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9760 wxColour colour
= attr
->GetTextColour();
9766 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9768 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9769 wxFont font
= attr
->GetFont();
9775 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9777 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9778 attr
->GetAlignment(horiz
, vert
);
9782 bool wxGrid::GetCellOverflow( int row
, int col
) const
9784 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9785 bool allow
= attr
->GetOverflow();
9791 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9793 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9794 attr
->GetSize( num_rows
, num_cols
);
9798 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9800 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9801 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9807 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9809 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9810 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9816 bool wxGrid::IsReadOnly(int row
, int col
) const
9818 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9819 bool isReadOnly
= attr
->IsReadOnly();
9825 // ----------------------------------------------------------------------------
9826 // attribute support: cache, automatic provider creation, ...
9827 // ----------------------------------------------------------------------------
9829 bool wxGrid::CanHaveAttributes() const
9836 return m_table
->CanHaveAttributes();
9839 void wxGrid::ClearAttrCache()
9841 if ( m_attrCache
.row
!= -1 )
9843 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
9844 m_attrCache
.attr
= NULL
;
9845 m_attrCache
.row
= -1;
9846 // wxSafeDecRec(...) might cause event processing that accesses
9847 // the cached attribute, if one exists (e.g. by deleting the
9848 // editor stored within the attribute). Therefore it is important
9849 // to invalidate the cache before calling wxSafeDecRef!
9850 wxSafeDecRef(oldAttr
);
9854 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9858 wxGrid
*self
= (wxGrid
*)this; // const_cast
9860 self
->ClearAttrCache();
9861 self
->m_attrCache
.row
= row
;
9862 self
->m_attrCache
.col
= col
;
9863 self
->m_attrCache
.attr
= attr
;
9868 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9870 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9872 *attr
= m_attrCache
.attr
;
9873 wxSafeIncRef(m_attrCache
.attr
);
9875 #ifdef DEBUG_ATTR_CACHE
9876 gs_nAttrCacheHits
++;
9883 #ifdef DEBUG_ATTR_CACHE
9884 gs_nAttrCacheMisses
++;
9891 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9893 wxGridCellAttr
*attr
= NULL
;
9894 // Additional test to avoid looking at the cache e.g. for
9895 // wxNoCellCoords, as this will confuse memory management.
9898 if ( !LookupAttr(row
, col
, &attr
) )
9900 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9901 : (wxGridCellAttr
*)NULL
;
9902 CacheAttr(row
, col
, attr
);
9908 attr
->SetDefAttr(m_defaultCellAttr
);
9912 attr
= m_defaultCellAttr
;
9919 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9921 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9922 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9924 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9925 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9927 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9930 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9932 // artificially inc the ref count to match DecRef() in caller
9934 m_table
->SetAttr(attr
, row
, col
);
9940 // ----------------------------------------------------------------------------
9941 // setting column attributes (wrappers around SetColAttr)
9942 // ----------------------------------------------------------------------------
9944 void wxGrid::SetColFormatBool(int col
)
9946 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9949 void wxGrid::SetColFormatNumber(int col
)
9951 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9954 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9956 wxString typeName
= wxGRID_VALUE_FLOAT
;
9957 if ( (width
!= -1) || (precision
!= -1) )
9959 typeName
<< _T(':') << width
<< _T(',') << precision
;
9962 SetColFormatCustom(col
, typeName
);
9965 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9967 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9969 attr
= new wxGridCellAttr
;
9970 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9971 attr
->SetRenderer(renderer
);
9972 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
9973 attr
->SetEditor(editor
);
9975 SetColAttr(col
, attr
);
9979 // ----------------------------------------------------------------------------
9980 // setting cell attributes: this is forwarded to the table
9981 // ----------------------------------------------------------------------------
9983 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9985 if ( CanHaveAttributes() )
9987 m_table
->SetAttr(attr
, row
, col
);
9996 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9998 if ( CanHaveAttributes() )
10000 m_table
->SetRowAttr(attr
, row
);
10005 wxSafeDecRef(attr
);
10009 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10011 if ( CanHaveAttributes() )
10013 m_table
->SetColAttr(attr
, col
);
10018 wxSafeDecRef(attr
);
10022 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10024 if ( CanHaveAttributes() )
10026 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10027 attr
->SetBackgroundColour(colour
);
10032 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10034 if ( CanHaveAttributes() )
10036 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10037 attr
->SetTextColour(colour
);
10042 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10044 if ( CanHaveAttributes() )
10046 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10047 attr
->SetFont(font
);
10052 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10054 if ( CanHaveAttributes() )
10056 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10057 attr
->SetAlignment(horiz
, vert
);
10062 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10064 if ( CanHaveAttributes() )
10066 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10067 attr
->SetOverflow(allow
);
10072 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10074 if ( CanHaveAttributes() )
10076 int cell_rows
, cell_cols
;
10078 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10079 attr
->GetSize(&cell_rows
, &cell_cols
);
10080 attr
->SetSize(num_rows
, num_cols
);
10083 // Cannot set the size of a cell to 0 or negative values
10084 // While it is perfectly legal to do that, this function cannot
10085 // handle all the possibilies, do it by hand by getting the CellAttr.
10086 // You can only set the size of a cell to 1,1 or greater with this fn
10087 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10088 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10089 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10090 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10092 // if this was already a multicell then "turn off" the other cells first
10093 if ((cell_rows
> 1) || (cell_cols
> 1))
10096 for (j
=row
; j
< row
+ cell_rows
; j
++)
10098 for (i
=col
; i
< col
+ cell_cols
; i
++)
10100 if ((i
!= col
) || (j
!= row
))
10102 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10103 attr_stub
->SetSize( 1, 1 );
10104 attr_stub
->DecRef();
10110 // mark the cells that will be covered by this cell to
10111 // negative or zero values to point back at this cell
10112 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10115 for (j
=row
; j
< row
+ num_rows
; j
++)
10117 for (i
=col
; i
< col
+ num_cols
; i
++)
10119 if ((i
!= col
) || (j
!= row
))
10121 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10122 attr_stub
->SetSize( row
- j
, col
- i
);
10123 attr_stub
->DecRef();
10131 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10133 if ( CanHaveAttributes() )
10135 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10136 attr
->SetRenderer(renderer
);
10141 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10143 if ( CanHaveAttributes() )
10145 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10146 attr
->SetEditor(editor
);
10151 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10153 if ( CanHaveAttributes() )
10155 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10156 attr
->SetReadOnly(isReadOnly
);
10161 // ----------------------------------------------------------------------------
10162 // Data type registration
10163 // ----------------------------------------------------------------------------
10165 void wxGrid::RegisterDataType(const wxString
& typeName
,
10166 wxGridCellRenderer
* renderer
,
10167 wxGridCellEditor
* editor
)
10169 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10173 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10175 wxString typeName
= m_table
->GetTypeName(row
, col
);
10176 return GetDefaultEditorForType(typeName
);
10179 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10181 wxString typeName
= m_table
->GetTypeName(row
, col
);
10182 return GetDefaultRendererForType(typeName
);
10185 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10187 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10188 if ( index
== wxNOT_FOUND
)
10190 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10195 return m_typeRegistry
->GetEditor(index
);
10198 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10200 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10201 if ( index
== wxNOT_FOUND
)
10203 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10208 return m_typeRegistry
->GetRenderer(index
);
10211 // ----------------------------------------------------------------------------
10213 // ----------------------------------------------------------------------------
10215 void wxGrid::EnableDragRowSize( bool enable
)
10217 m_canDragRowSize
= enable
;
10220 void wxGrid::EnableDragColSize( bool enable
)
10222 m_canDragColSize
= enable
;
10225 void wxGrid::EnableDragGridSize( bool enable
)
10227 m_canDragGridSize
= enable
;
10230 void wxGrid::EnableDragCell( bool enable
)
10232 m_canDragCell
= enable
;
10235 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10237 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10239 if ( resizeExistingRows
)
10241 // since we are resizing all rows to the default row size,
10242 // we can simply clear the row heights and row bottoms
10243 // arrays (which also allows us to take advantage of
10244 // some speed optimisations)
10245 m_rowHeights
.Empty();
10246 m_rowBottoms
.Empty();
10247 if ( !GetBatchCount() )
10252 void wxGrid::SetRowSize( int row
, int height
)
10254 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10256 // if < 0 then calculate new height from label
10260 wxArrayString lines
;
10261 wxClientDC
dc(m_rowLabelWin
);
10262 dc
.SetFont(GetLabelFont());
10263 StringToLines(GetRowLabelValue( row
), lines
);
10264 GetTextBoxSize( dc
, lines
, &w
, &h
);
10265 //check that it is not less than the minimal height
10266 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
10269 // See comment in SetColSize
10270 if ( height
< GetRowMinimalAcceptableHeight())
10273 if ( m_rowHeights
.IsEmpty() )
10275 // need to really create the array
10279 int h
= wxMax( 0, height
);
10280 int diff
= h
- m_rowHeights
[row
];
10282 m_rowHeights
[row
] = h
;
10283 for ( int i
= row
; i
< m_numRows
; i
++ )
10285 m_rowBottoms
[i
] += diff
;
10288 if ( !GetBatchCount() )
10292 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10294 // we dont allow zero default column width
10295 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10297 if ( resizeExistingCols
)
10299 // since we are resizing all columns to the default column size,
10300 // we can simply clear the col widths and col rights
10301 // arrays (which also allows us to take advantage of
10302 // some speed optimisations)
10303 m_colWidths
.Empty();
10304 m_colRights
.Empty();
10305 if ( !GetBatchCount() )
10310 void wxGrid::SetColSize( int col
, int width
)
10312 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10314 // if < 0 then calculate new width from label
10318 wxArrayString lines
;
10319 wxClientDC
dc(m_colLabelWin
);
10320 dc
.SetFont(GetLabelFont());
10321 StringToLines(GetColLabelValue(col
), lines
);
10322 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10323 GetTextBoxSize( dc
, lines
, &w
, &h
);
10325 GetTextBoxSize( dc
, lines
, &h
, &w
);
10327 //check that it is not less than the minimal width
10328 width
= wxMax(width
, GetColMinimalAcceptableWidth());
10331 // should we check that it's bigger than GetColMinimalWidth(col) here?
10333 // No, because it is reasonable to assume the library user know's
10334 // what he is doing. However we should test against the weaker
10335 // constraint of minimalAcceptableWidth, as this breaks rendering
10337 // This test then fixes sf.net bug #645734
10339 if ( width
< GetColMinimalAcceptableWidth() )
10342 if ( m_colWidths
.IsEmpty() )
10344 // need to really create the array
10348 int w
= wxMax( 0, width
);
10349 int diff
= w
- m_colWidths
[col
];
10350 m_colWidths
[col
] = w
;
10352 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10354 m_colRights
[GetColAt(colPos
)] += diff
;
10357 if ( !GetBatchCount() )
10361 void wxGrid::SetColMinimalWidth( int col
, int width
)
10363 if (width
> GetColMinimalAcceptableWidth())
10365 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10366 m_colMinWidths
[key
] = width
;
10370 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10372 if (width
> GetRowMinimalAcceptableHeight())
10374 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10375 m_rowMinHeights
[key
] = width
;
10379 int wxGrid::GetColMinimalWidth(int col
) const
10381 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10382 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10384 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10387 int wxGrid::GetRowMinimalHeight(int row
) const
10389 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10390 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10392 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10395 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10397 // We do allow a width of 0 since this gives us
10398 // an easy way to temporarily hiding columns.
10400 m_minAcceptableColWidth
= width
;
10403 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10405 // We do allow a height of 0 since this gives us
10406 // an easy way to temporarily hiding rows.
10408 m_minAcceptableRowHeight
= height
;
10411 int wxGrid::GetColMinimalAcceptableWidth() const
10413 return m_minAcceptableColWidth
;
10416 int wxGrid::GetRowMinimalAcceptableHeight() const
10418 return m_minAcceptableRowHeight
;
10421 // ----------------------------------------------------------------------------
10423 // ----------------------------------------------------------------------------
10426 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10428 const bool column
= direction
== wxGRID_COLUMN
;
10430 wxClientDC
dc(m_gridWin
);
10432 // cancel editing of cell
10433 HideCellEditControl();
10434 SaveEditControlValue();
10436 // init both of them to avoid compiler warnings, even if we only need one
10444 wxCoord extent
, extentMax
= 0;
10445 int max
= column
? m_numRows
: m_numCols
;
10446 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10453 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10454 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10457 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10458 extent
= column
? size
.x
: size
.y
;
10459 if ( extent
> extentMax
)
10460 extentMax
= extent
;
10462 renderer
->DecRef();
10468 // now also compare with the column label extent
10470 dc
.SetFont( GetLabelFont() );
10474 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10475 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10479 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10481 extent
= column
? w
: h
;
10482 if ( extent
> extentMax
)
10483 extentMax
= extent
;
10487 // empty column - give default extent (notice that if extentMax is less
10488 // than default extent but != 0, it's OK)
10489 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10494 // leave some space around text
10502 // Ensure automatic width is not less than minimal width. See the
10503 // comment in SetColSize() for explanation of why this isn't done
10504 // in SetColSize().
10506 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10508 SetColSize( col
, extentMax
);
10509 if ( !GetBatchCount() )
10512 m_gridWin
->GetClientSize( &cw
, &ch
);
10513 wxRect
rect ( CellToRect( 0, col
) );
10515 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10516 rect
.width
= cw
- rect
.x
;
10517 rect
.height
= m_colLabelHeight
;
10518 m_colLabelWin
->Refresh( true, &rect
);
10523 // Ensure automatic width is not less than minimal height. See the
10524 // comment in SetColSize() for explanation of why this isn't done
10525 // in SetRowSize().
10527 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10529 SetRowSize(row
, extentMax
);
10530 if ( !GetBatchCount() )
10533 m_gridWin
->GetClientSize( &cw
, &ch
);
10534 wxRect
rect( CellToRect( row
, 0 ) );
10536 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10537 rect
.width
= m_rowLabelWidth
;
10538 rect
.height
= ch
- rect
.y
;
10539 m_rowLabelWin
->Refresh( true, &rect
);
10546 SetColMinimalWidth(col
, extentMax
);
10548 SetRowMinimalHeight(row
, extentMax
);
10552 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10554 // calculate size for the rows or columns?
10555 const bool calcRows
= direction
== wxGRID_ROW
;
10557 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10558 : GetGridColLabelWindow());
10559 dc
.SetFont(GetLabelFont());
10561 // which dimension should we take into account for calculations?
10563 // for columns, the text can be only horizontal so it's easy but for rows
10564 // we also have to take into account the text orientation
10566 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10568 wxArrayString lines
;
10569 wxCoord extentMax
= 0;
10571 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10572 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10576 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10577 : GetColLabelValue(rowOrCol
);
10578 StringToLines(label
, lines
);
10581 GetTextBoxSize(dc
, lines
, &w
, &h
);
10583 const wxCoord extent
= useWidth
? w
: h
;
10584 if ( extent
> extentMax
)
10585 extentMax
= extent
;
10590 // empty column - give default extent (notice that if extentMax is less
10591 // than default extent but != 0, it's OK)
10592 extentMax
= calcRows
? GetDefaultRowLabelSize()
10593 : GetDefaultColLabelSize();
10596 // leave some space around text (taken from AutoSizeColOrRow)
10605 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10607 int width
= m_rowLabelWidth
;
10609 wxGridUpdateLocker locker
;
10611 locker
.Create(this);
10613 for ( int col
= 0; col
< m_numCols
; col
++ )
10616 AutoSizeColumn(col
, setAsMin
);
10618 width
+= GetColWidth(col
);
10624 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10626 int height
= m_colLabelHeight
;
10628 wxGridUpdateLocker locker
;
10630 locker
.Create(this);
10632 for ( int row
= 0; row
< m_numRows
; row
++ )
10635 AutoSizeRow(row
, setAsMin
);
10637 height
+= GetRowHeight(row
);
10643 void wxGrid::AutoSize()
10645 wxGridUpdateLocker
locker(this);
10647 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10648 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10650 // we know that we're not going to have scrollbars so disable them now to
10651 // avoid trouble in SetClientSize() which can otherwise set the correct
10652 // client size but also leave space for (not needed any more) scrollbars
10653 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10655 // restore the scroll rate parameters overwritten by SetScrollbars()
10656 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
10658 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
10661 void wxGrid::AutoSizeRowLabelSize( int row
)
10663 // Hide the edit control, so it
10664 // won't interfere with drag-shrinking.
10665 if ( IsCellEditControlShown() )
10667 HideCellEditControl();
10668 SaveEditControlValue();
10671 // autosize row height depending on label text
10672 SetRowSize(row
, -1);
10676 void wxGrid::AutoSizeColLabelSize( int col
)
10678 // Hide the edit control, so it
10679 // won't interfere with drag-shrinking.
10680 if ( IsCellEditControlShown() )
10682 HideCellEditControl();
10683 SaveEditControlValue();
10686 // autosize column width depending on label text
10687 SetColSize(col
, -1);
10691 wxSize
wxGrid::DoGetBestSize() const
10693 wxGrid
*self
= (wxGrid
*)this; // const_cast
10695 // we do the same as in AutoSize() here with the exception that we don't
10696 // change the column/row sizes, only calculate them
10697 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10698 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10700 // NOTE: This size should be cached, but first we need to add calls to
10701 // InvalidateBestSize everywhere that could change the results of this
10703 // CacheBestSize(size);
10705 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
10706 + GetWindowBorderSize();
10714 wxPen
& wxGrid::GetDividerPen() const
10719 // ----------------------------------------------------------------------------
10720 // cell value accessor functions
10721 // ----------------------------------------------------------------------------
10723 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10727 m_table
->SetValue( row
, col
, s
);
10728 if ( !GetBatchCount() )
10731 wxRect
rect( CellToRect( row
, col
) );
10733 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10734 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10735 m_gridWin
->Refresh( false, &rect
);
10738 if ( m_currentCellCoords
.GetRow() == row
&&
10739 m_currentCellCoords
.GetCol() == col
&&
10740 IsCellEditControlShown())
10741 // Note: If we are using IsCellEditControlEnabled,
10742 // this interacts badly with calling SetCellValue from
10743 // an EVT_GRID_CELL_CHANGE handler.
10745 HideCellEditControl();
10746 ShowCellEditControl(); // will reread data from table
10751 // ----------------------------------------------------------------------------
10752 // block, row and column selection
10753 // ----------------------------------------------------------------------------
10755 void wxGrid::SelectRow( int row
, bool addToSelected
)
10757 if ( IsSelection() && !addToSelected
)
10761 m_selection
->SelectRow( row
, false, addToSelected
);
10764 void wxGrid::SelectCol( int col
, bool addToSelected
)
10766 if ( IsSelection() && !addToSelected
)
10770 m_selection
->SelectCol( col
, false, addToSelected
);
10773 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10774 bool addToSelected
)
10776 if ( IsSelection() && !addToSelected
)
10780 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10781 false, addToSelected
);
10784 void wxGrid::SelectAll()
10786 if ( m_numRows
> 0 && m_numCols
> 0 )
10789 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10793 // ----------------------------------------------------------------------------
10794 // cell, row and col deselection
10795 // ----------------------------------------------------------------------------
10797 void wxGrid::DeselectRow( int row
)
10799 if ( !m_selection
)
10802 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10804 if ( m_selection
->IsInSelection(row
, 0 ) )
10805 m_selection
->ToggleCellSelection(row
, 0);
10809 int nCols
= GetNumberCols();
10810 for ( int i
= 0; i
< nCols
; i
++ )
10812 if ( m_selection
->IsInSelection(row
, i
) )
10813 m_selection
->ToggleCellSelection(row
, i
);
10818 void wxGrid::DeselectCol( int col
)
10820 if ( !m_selection
)
10823 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10825 if ( m_selection
->IsInSelection(0, col
) )
10826 m_selection
->ToggleCellSelection(0, col
);
10830 int nRows
= GetNumberRows();
10831 for ( int i
= 0; i
< nRows
; i
++ )
10833 if ( m_selection
->IsInSelection(i
, col
) )
10834 m_selection
->ToggleCellSelection(i
, col
);
10839 void wxGrid::DeselectCell( int row
, int col
)
10841 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10842 m_selection
->ToggleCellSelection(row
, col
);
10845 bool wxGrid::IsSelection() const
10847 return ( m_selection
&& (m_selection
->IsSelection() ||
10848 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10849 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10852 bool wxGrid::IsInSelection( int row
, int col
) const
10854 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10855 ( row
>= m_selectingTopLeft
.GetRow() &&
10856 col
>= m_selectingTopLeft
.GetCol() &&
10857 row
<= m_selectingBottomRight
.GetRow() &&
10858 col
<= m_selectingBottomRight
.GetCol() )) );
10861 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10865 wxGridCellCoordsArray a
;
10869 return m_selection
->m_cellSelection
;
10872 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10876 wxGridCellCoordsArray a
;
10880 return m_selection
->m_blockSelectionTopLeft
;
10883 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10887 wxGridCellCoordsArray a
;
10891 return m_selection
->m_blockSelectionBottomRight
;
10894 wxArrayInt
wxGrid::GetSelectedRows() const
10902 return m_selection
->m_rowSelection
;
10905 wxArrayInt
wxGrid::GetSelectedCols() const
10913 return m_selection
->m_colSelection
;
10916 void wxGrid::ClearSelection()
10918 wxRect r1
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
);
10919 wxRect r2
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard
);
10920 m_selectingTopLeft
=
10921 m_selectingBottomRight
=
10922 m_selectingKeyboard
= wxGridNoCellCoords
;
10923 Refresh( false, &r1
);
10924 Refresh( false, &r2
);
10926 m_selection
->ClearSelection();
10929 // This function returns the rectangle that encloses the given block
10930 // in device coords clipped to the client size of the grid window.
10932 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
10933 const wxGridCellCoords
& bottomRight
) const
10936 wxRect tempCellRect
= CellToRect(topLeft
);
10937 if ( tempCellRect
!= wxGridNoCellRect
)
10939 resultRect
= tempCellRect
;
10943 resultRect
= wxRect(0, 0, 0, 0);
10946 tempCellRect
= CellToRect(bottomRight
);
10947 if ( tempCellRect
!= wxGridNoCellRect
)
10949 resultRect
+= tempCellRect
;
10953 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
10954 return wxGridNoCellRect
;
10957 // Ensure that left/right and top/bottom pairs are in order.
10958 int left
= resultRect
.GetLeft();
10959 int top
= resultRect
.GetTop();
10960 int right
= resultRect
.GetRight();
10961 int bottom
= resultRect
.GetBottom();
10963 int leftCol
= topLeft
.GetCol();
10964 int topRow
= topLeft
.GetRow();
10965 int rightCol
= bottomRight
.GetCol();
10966 int bottomRow
= bottomRight
.GetRow();
10975 leftCol
= rightCol
;
10986 topRow
= bottomRow
;
10990 // The following loop is ONLY necessary to detect and handle merged cells.
10992 m_gridWin
->GetClientSize( &cw
, &ch
);
10994 // Get the origin coordinates: notice that they will be negative if the
10995 // grid is scrolled downwards/to the right.
10996 int gridOriginX
= 0;
10997 int gridOriginY
= 0;
10998 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11000 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11001 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11003 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11004 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11006 // Bound our loop so that we only examine the portion of the selected block
11007 // that is shown on screen. Therefore, we compare the Top-Left block values
11008 // to the Top-Left screen values, and the Bottom-Right block values to the
11009 // Bottom-Right screen values, choosing appropriately.
11010 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11011 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11012 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11013 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11015 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11017 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11019 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11020 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11022 tempCellRect
= CellToRect( j
, i
);
11024 if (tempCellRect
.x
< left
)
11025 left
= tempCellRect
.x
;
11026 if (tempCellRect
.y
< top
)
11027 top
= tempCellRect
.y
;
11028 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11029 right
= tempCellRect
.x
+ tempCellRect
.width
;
11030 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11031 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11035 i
= visibleRightCol
; // jump over inner cells.
11040 // Convert to scrolled coords
11041 CalcScrolledPosition( left
, top
, &left
, &top
);
11042 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11044 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11045 return wxRect(0,0,0,0);
11047 resultRect
.SetLeft( wxMax(0, left
) );
11048 resultRect
.SetTop( wxMax(0, top
) );
11049 resultRect
.SetRight( wxMin(cw
, right
) );
11050 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11055 // ----------------------------------------------------------------------------
11057 // ----------------------------------------------------------------------------
11059 #if wxUSE_DRAG_AND_DROP
11061 // this allow setting drop target directly on wxGrid
11062 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11064 GetGridWindow()->SetDropTarget(dropTarget
);
11067 #endif // wxUSE_DRAG_AND_DROP
11069 // ----------------------------------------------------------------------------
11070 // grid event classes
11071 // ----------------------------------------------------------------------------
11073 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11075 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11076 int row
, int col
, int x
, int y
, bool sel
,
11077 bool control
, bool shift
, bool alt
, bool meta
)
11078 : wxNotifyEvent( type
, id
)
11085 m_control
= control
;
11090 SetEventObject(obj
);
11094 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11096 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11097 int rowOrCol
, int x
, int y
,
11098 bool control
, bool shift
, bool alt
, bool meta
)
11099 : wxNotifyEvent( type
, id
)
11101 m_rowOrCol
= rowOrCol
;
11104 m_control
= control
;
11109 SetEventObject(obj
);
11113 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11115 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11116 const wxGridCellCoords
& topLeft
,
11117 const wxGridCellCoords
& bottomRight
,
11118 bool sel
, bool control
,
11119 bool shift
, bool alt
, bool meta
)
11120 : wxNotifyEvent( type
, id
)
11122 m_topLeft
= topLeft
;
11123 m_bottomRight
= bottomRight
;
11125 m_control
= control
;
11130 SetEventObject(obj
);
11134 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11136 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11137 wxObject
* obj
, int row
,
11138 int col
, wxControl
* ctrl
)
11139 : wxCommandEvent(type
, id
)
11141 SetEventObject(obj
);
11147 #endif // wxUSE_GRID