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
;
418 // ----------------------------------------------------------------------------
419 // conditional compilation
420 // ----------------------------------------------------------------------------
422 #ifndef WXGRID_DRAW_LINES
423 #define WXGRID_DRAW_LINES 1
426 // ----------------------------------------------------------------------------
428 // ----------------------------------------------------------------------------
430 //#define DEBUG_ATTR_CACHE
431 #ifdef DEBUG_ATTR_CACHE
432 static size_t gs_nAttrCacheHits
= 0;
433 static size_t gs_nAttrCacheMisses
= 0;
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
441 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
444 // TODO: this doesn't work at all, grid cells have different sizes and approx
445 // calculations don't work as because of the size mismatch scrollbars
446 // sometimes fail to be shown when they should be or vice versa
448 // The scroll bars may be a little flakey once in a while, but that is
449 // surely much less horrible than having scroll lines of only 1!!!
452 // Well, it's still seriously broken so it might be better but needs
455 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
456 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
458 // the size of hash tables used a bit everywhere (the max number of elements
459 // in these hash tables is the number of rows/columns)
460 static const int GRID_HASH_SIZE
= 100;
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 static inline int GetScrollX(int x
)
469 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
472 static inline int GetScrollY(int y
)
474 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
478 // ============================================================================
480 // ============================================================================
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 wxGridCellEditor::wxGridCellEditor()
492 wxGridCellEditor::~wxGridCellEditor()
497 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
498 wxWindowID
WXUNUSED(id
),
499 wxEvtHandler
* evtHandler
)
502 m_control
->PushEventHandler(evtHandler
);
505 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
506 wxGridCellAttr
*attr
)
508 // erase the background because we might not fill the cell
509 wxClientDC
dc(m_control
->GetParent());
510 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
512 gridWindow
->GetOwner()->PrepareDC(dc
);
514 dc
.SetPen(*wxTRANSPARENT_PEN
);
515 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
516 dc
.DrawRectangle(rectCell
);
518 // redraw the control we just painted over
519 m_control
->Refresh();
522 void wxGridCellEditor::Destroy()
526 m_control
->PopEventHandler( true /* delete it*/ );
528 m_control
->Destroy();
533 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
535 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
537 m_control
->Show(show
);
541 // set the colours/fonts if we have any
544 m_colFgOld
= m_control
->GetForegroundColour();
545 m_control
->SetForegroundColour(attr
->GetTextColour());
547 m_colBgOld
= m_control
->GetBackgroundColour();
548 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
550 // Workaround for GTK+1 font setting problem on some platforms
551 #if !defined(__WXGTK__) || defined(__WXGTK20__)
552 m_fontOld
= m_control
->GetFont();
553 m_control
->SetFont(attr
->GetFont());
556 // can't do anything more in the base class version, the other
557 // attributes may only be used by the derived classes
562 // restore the standard colours fonts
563 if ( m_colFgOld
.Ok() )
565 m_control
->SetForegroundColour(m_colFgOld
);
566 m_colFgOld
= wxNullColour
;
569 if ( m_colBgOld
.Ok() )
571 m_control
->SetBackgroundColour(m_colBgOld
);
572 m_colBgOld
= wxNullColour
;
575 // Workaround for GTK+1 font setting problem on some platforms
576 #if !defined(__WXGTK__) || defined(__WXGTK20__)
577 if ( m_fontOld
.Ok() )
579 m_control
->SetFont(m_fontOld
);
580 m_fontOld
= wxNullFont
;
586 void wxGridCellEditor::SetSize(const wxRect
& rect
)
588 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
590 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
593 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
598 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
600 bool ctrl
= event
.ControlDown();
601 bool alt
= event
.AltDown();
604 // On the Mac the Alt key is more like shift and is used for entry of
605 // valid characters, so check for Ctrl and Meta instead.
606 alt
= event
.MetaDown();
609 // Assume it's not a valid char if ctrl or alt is down, but if both are
610 // down then it may be because of an AltGr key combination, so let them
611 // through in that case.
612 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
619 // If it's a F-Key or other special key then it shouldn't start the
621 if (event
.GetKeyCode() >= WXK_START
)
625 // if the unicode key code is not really a unicode character (it may
626 // be a function key or etc., the platforms appear to always give us a
627 // small value in this case) then fallback to the ASCII key code but
628 // don't do anything for function keys or etc.
629 key
= event
.GetUnicodeKey();
632 key
= event
.GetKeyCode();
633 keyOk
= (key
<= 127);
636 key
= event
.GetKeyCode();
637 keyOk
= (key
<= 255);
643 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
648 void wxGridCellEditor::StartingClick()
654 // ----------------------------------------------------------------------------
655 // wxGridCellTextEditor
656 // ----------------------------------------------------------------------------
658 wxGridCellTextEditor::wxGridCellTextEditor()
663 void wxGridCellTextEditor::Create(wxWindow
* parent
,
665 wxEvtHandler
* evtHandler
)
667 DoCreate(parent
, id
, evtHandler
);
670 void wxGridCellTextEditor::DoCreate(wxWindow
* parent
,
672 wxEvtHandler
* evtHandler
,
675 style
|= wxTE_PROCESS_ENTER
|
680 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
681 wxDefaultPosition
, wxDefaultSize
,
684 // set max length allowed in the textctrl, if the parameter was set
685 if ( m_maxChars
!= 0 )
687 Text()->SetMaxLength(m_maxChars
);
690 wxGridCellEditor::Create(parent
, id
, evtHandler
);
693 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
694 wxGridCellAttr
* WXUNUSED(attr
))
696 // as we fill the entire client area,
697 // don't do anything here to minimize flicker
700 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
702 wxRect
rect(rectOrig
);
704 // Make the edit control large enough to allow for internal margins
706 // TODO: remove this if the text ctrl sizing is improved esp. for unix
708 #if defined(__WXGTK__)
716 #elif defined(__WXMSW__)
730 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
731 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
733 #if defined(__WXMOTIF__)
738 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
739 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
740 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
741 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
744 wxGridCellEditor::SetSize(rect
);
747 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
749 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
751 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
753 DoBeginEdit(m_startValue
);
756 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
758 Text()->SetValue(startValue
);
759 Text()->SetInsertionPointEnd();
760 Text()->SetSelection(-1, -1);
764 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
766 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
768 bool changed
= false;
769 wxString value
= Text()->GetValue();
770 if (value
!= m_startValue
)
774 grid
->GetTable()->SetValue(row
, col
, value
);
776 m_startValue
= wxEmptyString
;
778 // No point in setting the text of the hidden control
779 //Text()->SetValue(m_startValue);
784 void wxGridCellTextEditor::Reset()
786 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
788 DoReset(m_startValue
);
791 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
793 Text()->SetValue(startValue
);
794 Text()->SetInsertionPointEnd();
797 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
799 return wxGridCellEditor::IsAcceptedKey(event
);
802 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
804 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
805 // longer an appropriate way to get the character into the text control.
806 // Do it ourselves instead. We know that if we get this far that we have
807 // a valid character, so not a whole lot of testing needs to be done.
809 wxTextCtrl
* tc
= Text();
814 ch
= event
.GetUnicodeKey();
816 ch
= (wxChar
)event
.GetKeyCode();
818 ch
= (wxChar
)event
.GetKeyCode();
824 // delete the character at the cursor
825 pos
= tc
->GetInsertionPoint();
826 if (pos
< tc
->GetLastPosition())
827 tc
->Remove(pos
, pos
+ 1);
831 // delete the character before the cursor
832 pos
= tc
->GetInsertionPoint();
834 tc
->Remove(pos
- 1, pos
);
843 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
844 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
846 #if defined(__WXMOTIF__) || defined(__WXGTK__)
847 // wxMotif needs a little extra help...
848 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
849 wxString
s( Text()->GetValue() );
850 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
852 Text()->SetInsertionPoint( pos
);
854 // the other ports can handle a Return key press
860 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
870 if ( params
.ToLong(&tmp
) )
872 m_maxChars
= (size_t)tmp
;
876 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
881 // return the value in the text control
882 wxString
wxGridCellTextEditor::GetValue() const
884 return Text()->GetValue();
887 // ----------------------------------------------------------------------------
888 // wxGridCellNumberEditor
889 // ----------------------------------------------------------------------------
891 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
897 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
899 wxEvtHandler
* evtHandler
)
904 // create a spin ctrl
905 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
906 wxDefaultPosition
, wxDefaultSize
,
910 wxGridCellEditor::Create(parent
, id
, evtHandler
);
915 // just a text control
916 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
919 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
924 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
926 // first get the value
927 wxGridTableBase
*table
= grid
->GetTable();
928 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
930 m_valueOld
= table
->GetValueAsLong(row
, col
);
935 wxString sValue
= table
->GetValue(row
, col
);
936 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
938 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
946 Spin()->SetValue((int)m_valueOld
);
952 DoBeginEdit(GetString());
956 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
966 value
= Spin()->GetValue();
967 changed
= value
!= m_valueOld
;
969 text
= wxString::Format(wxT("%ld"), value
);
974 text
= Text()->GetValue();
975 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
980 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
981 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
983 grid
->GetTable()->SetValue(row
, col
, text
);
989 void wxGridCellNumberEditor::Reset()
994 Spin()->SetValue((int)m_valueOld
);
999 DoReset(GetString());
1003 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
1005 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1007 int keycode
= event
.GetKeyCode();
1008 if ( (keycode
< 128) &&
1009 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
1018 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
1020 int keycode
= event
.GetKeyCode();
1023 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
1025 wxGridCellTextEditor::StartingKey(event
);
1027 // skip Skip() below
1034 if ( wxIsdigit(keycode
) )
1036 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1037 spin
->SetValue(keycode
- '0');
1038 spin
->SetSelection(1,1);
1047 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1058 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1062 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1066 // skip the error message below
1071 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1075 // return the value in the spin control if it is there (the text control otherwise)
1076 wxString
wxGridCellNumberEditor::GetValue() const
1083 long value
= Spin()->GetValue();
1084 s
.Printf(wxT("%ld"), value
);
1089 s
= Text()->GetValue();
1095 // ----------------------------------------------------------------------------
1096 // wxGridCellFloatEditor
1097 // ----------------------------------------------------------------------------
1099 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1102 m_precision
= precision
;
1105 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1107 wxEvtHandler
* evtHandler
)
1109 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1111 #if wxUSE_VALIDATORS
1112 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1116 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1118 // first get the value
1119 wxGridTableBase
*table
= grid
->GetTable();
1120 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1122 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1127 wxString sValue
= table
->GetValue(row
, col
);
1128 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1130 wxFAIL_MSG( _T("this cell doesn't have float value") );
1135 DoBeginEdit(GetString());
1138 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1142 wxString
text(Text()->GetValue());
1144 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1145 !wxIsSameDouble(value
, m_valueOld
) )
1147 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1148 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1150 grid
->GetTable()->SetValue(row
, col
, text
);
1158 void wxGridCellFloatEditor::Reset()
1160 DoReset(GetString());
1163 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1165 int keycode
= event
.GetKeyCode();
1167 tmpbuf
[0] = (char) keycode
;
1169 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1172 bool is_decimal_point
= ( strbuf
==
1173 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1175 bool is_decimal_point
= ( strbuf
== _T(".") );
1178 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1179 || is_decimal_point
)
1181 wxGridCellTextEditor::StartingKey(event
);
1183 // skip Skip() below
1190 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1201 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1205 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1207 m_precision
= (int)tmp
;
1209 // skip the error message below
1214 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1218 wxString
wxGridCellFloatEditor::GetString() const
1221 if ( m_precision
== -1 && m_width
!= -1)
1223 // default precision
1224 fmt
.Printf(_T("%%%d.f"), m_width
);
1226 else if ( m_precision
!= -1 && m_width
== -1)
1229 fmt
.Printf(_T("%%.%df"), m_precision
);
1231 else if ( m_precision
!= -1 && m_width
!= -1 )
1233 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1237 // default width/precision
1241 return wxString::Format(fmt
, m_valueOld
);
1244 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1246 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1248 const int keycode
= event
.GetKeyCode();
1249 if ( isascii(keycode
) )
1252 tmpbuf
[0] = (char) keycode
;
1254 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1257 const wxString decimalPoint
=
1258 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1260 const wxString
decimalPoint(_T('.'));
1263 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1264 if ( wxIsdigit(keycode
) ||
1265 tolower(keycode
) == 'e' ||
1266 keycode
== decimalPoint
||
1278 #endif // wxUSE_TEXTCTRL
1282 // ----------------------------------------------------------------------------
1283 // wxGridCellBoolEditor
1284 // ----------------------------------------------------------------------------
1286 // the default values for GetValue()
1287 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1289 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1291 wxEvtHandler
* evtHandler
)
1293 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1294 wxDefaultPosition
, wxDefaultSize
,
1297 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1300 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1302 bool resize
= false;
1303 wxSize size
= m_control
->GetSize();
1304 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1306 // check if the checkbox is not too big/small for this cell
1307 wxSize sizeBest
= m_control
->GetBestSize();
1308 if ( !(size
== sizeBest
) )
1310 // reset to default size if it had been made smaller
1316 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1318 // leave 1 pixel margin
1319 size
.x
= size
.y
= minSize
- 2;
1326 m_control
->SetSize(size
);
1329 // position it in the centre of the rectangle (TODO: support alignment?)
1331 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1332 // the checkbox without label still has some space to the right in wxGTK,
1333 // so shift it to the right
1335 #elif defined(__WXMSW__)
1336 // here too, but in other way
1341 int hAlign
= wxALIGN_CENTRE
;
1342 int vAlign
= wxALIGN_CENTRE
;
1344 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1347 if (hAlign
== wxALIGN_LEFT
)
1355 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1357 else if (hAlign
== wxALIGN_RIGHT
)
1359 x
= r
.x
+ r
.width
- size
.x
- 2;
1360 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1362 else if (hAlign
== wxALIGN_CENTRE
)
1364 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1365 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1368 m_control
->Move(x
, y
);
1371 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1373 m_control
->Show(show
);
1377 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1378 CBox()->SetBackgroundColour(colBg
);
1382 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1384 wxASSERT_MSG(m_control
,
1385 wxT("The wxGridCellEditor must be created first!"));
1387 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1389 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1393 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1395 if ( cellval
== ms_stringValues
[false] )
1396 m_startValue
= false;
1397 else if ( cellval
== ms_stringValues
[true] )
1398 m_startValue
= true;
1401 // do not try to be smart here and convert it to true or false
1402 // because we'll still overwrite it with something different and
1403 // this risks to be very surprising for the user code, let them
1405 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1409 CBox()->SetValue(m_startValue
);
1413 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1416 wxASSERT_MSG(m_control
,
1417 wxT("The wxGridCellEditor must be created first!"));
1419 bool changed
= false;
1420 bool value
= CBox()->GetValue();
1421 if ( value
!= m_startValue
)
1426 wxGridTableBase
* const table
= grid
->GetTable();
1427 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1428 table
->SetValueAsBool(row
, col
, value
);
1430 table
->SetValue(row
, col
, GetValue());
1436 void wxGridCellBoolEditor::Reset()
1438 wxASSERT_MSG(m_control
,
1439 wxT("The wxGridCellEditor must be created first!"));
1441 CBox()->SetValue(m_startValue
);
1444 void wxGridCellBoolEditor::StartingClick()
1446 CBox()->SetValue(!CBox()->GetValue());
1449 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1451 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1453 int keycode
= event
.GetKeyCode();
1466 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1468 int keycode
= event
.GetKeyCode();
1472 CBox()->SetValue(!CBox()->GetValue());
1476 CBox()->SetValue(true);
1480 CBox()->SetValue(false);
1485 wxString
wxGridCellBoolEditor::GetValue() const
1487 return ms_stringValues
[CBox()->GetValue()];
1491 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1492 const wxString
& valueFalse
)
1494 ms_stringValues
[false] = valueFalse
;
1495 ms_stringValues
[true] = valueTrue
;
1499 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1501 return value
== ms_stringValues
[true];
1504 #endif // wxUSE_CHECKBOX
1508 // ----------------------------------------------------------------------------
1509 // wxGridCellChoiceEditor
1510 // ----------------------------------------------------------------------------
1512 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1514 : m_choices(choices
),
1515 m_allowOthers(allowOthers
) { }
1517 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1518 const wxString choices
[],
1520 : m_allowOthers(allowOthers
)
1524 m_choices
.Alloc(count
);
1525 for ( size_t n
= 0; n
< count
; n
++ )
1527 m_choices
.Add(choices
[n
]);
1532 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1534 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1535 editor
->m_allowOthers
= m_allowOthers
;
1536 editor
->m_choices
= m_choices
;
1541 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1543 wxEvtHandler
* evtHandler
)
1545 int style
= wxTE_PROCESS_ENTER
|
1549 if ( !m_allowOthers
)
1550 style
|= wxCB_READONLY
;
1551 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1552 wxDefaultPosition
, wxDefaultSize
,
1556 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1559 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1560 wxGridCellAttr
* attr
)
1562 // as we fill the entire client area, don't do anything here to minimize
1565 // TODO: It doesn't actually fill the client area since the height of a
1566 // combo always defaults to the standard. Until someone has time to
1567 // figure out the right rectangle to paint, just do it the normal way.
1568 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1571 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1573 wxASSERT_MSG(m_control
,
1574 wxT("The wxGridCellEditor must be created first!"));
1576 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1578 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1580 // Don't immediately end if we get a kill focus event within BeginEdit
1582 evtHandler
->SetInSetFocus(true);
1584 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1588 Combo()->SetValue(m_startValue
);
1589 Combo()->SetInsertionPointEnd();
1591 else // the combobox is read-only
1593 // find the right position, or default to the first if not found
1594 int pos
= Combo()->FindString(m_startValue
);
1595 if (pos
== wxNOT_FOUND
)
1597 Combo()->SetSelection(pos
);
1600 Combo()->SetFocus();
1604 // When dropping down the menu, a kill focus event
1605 // happens after this point, so we can't reset the flag yet.
1606 #if !defined(__WXGTK20__)
1607 evtHandler
->SetInSetFocus(false);
1612 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1615 wxString value
= Combo()->GetValue();
1616 if ( value
== m_startValue
)
1619 grid
->GetTable()->SetValue(row
, col
, value
);
1624 void wxGridCellChoiceEditor::Reset()
1626 Combo()->SetValue(m_startValue
);
1627 Combo()->SetInsertionPointEnd();
1630 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1640 wxStringTokenizer
tk(params
, _T(','));
1641 while ( tk
.HasMoreTokens() )
1643 m_choices
.Add(tk
.GetNextToken());
1647 // return the value in the text control
1648 wxString
wxGridCellChoiceEditor::GetValue() const
1650 return Combo()->GetValue();
1653 #endif // wxUSE_COMBOBOX
1655 // ----------------------------------------------------------------------------
1656 // wxGridCellEditorEvtHandler
1657 // ----------------------------------------------------------------------------
1659 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1661 // Don't disable the cell if we're just starting to edit it
1666 m_grid
->DisableCellEditControl();
1671 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1673 switch ( event
.GetKeyCode() )
1677 m_grid
->DisableCellEditControl();
1681 m_grid
->GetEventHandler()->ProcessEvent( event
);
1685 case WXK_NUMPAD_ENTER
:
1686 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1687 m_editor
->HandleReturn(event
);
1696 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1698 int row
= m_grid
->GetGridCursorRow();
1699 int col
= m_grid
->GetGridCursorCol();
1700 wxRect rect
= m_grid
->CellToRect( row
, col
);
1702 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1704 // if cell width is smaller than grid client area, cell is wholly visible
1705 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1707 switch ( event
.GetKeyCode() )
1712 case WXK_NUMPAD_ENTER
:
1717 if ( wholeCellVisible
)
1719 // no special processing needed...
1724 // do special processing for partly visible cell...
1726 // get the widths of all cells previous to this one
1728 for ( int i
= 0; i
< col
; i
++ )
1730 colXPos
+= m_grid
->GetColSize(i
);
1733 int xUnit
= 1, yUnit
= 1;
1734 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1737 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1741 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1749 if ( wholeCellVisible
)
1751 // no special processing needed...
1756 // do special processing for partly visible cell...
1759 wxString value
= m_grid
->GetCellValue(row
, col
);
1760 if ( wxEmptyString
!= value
)
1762 // get width of cell CONTENTS (text)
1764 wxFont font
= m_grid
->GetCellFont(row
, col
);
1765 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1767 // try to RIGHT align the text by scrolling
1768 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1770 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1771 // otherwise the last part of the cell content might be hidden below the scroll bar
1772 // FIXME: maybe there is a more suitable correction?
1773 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1774 if ( textWidth
< 0 )
1780 // get the widths of all cells previous to this one
1782 for ( int i
= 0; i
< col
; i
++ )
1784 colXPos
+= m_grid
->GetColSize(i
);
1787 // and add the (modified) text width of the cell contents
1788 // as we'd like to see the last part of the cell contents
1789 colXPos
+= textWidth
;
1791 int xUnit
= 1, yUnit
= 1;
1792 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1793 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1804 // ----------------------------------------------------------------------------
1805 // wxGridCellWorker is an (almost) empty common base class for
1806 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1807 // ----------------------------------------------------------------------------
1809 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1814 wxGridCellWorker::~wxGridCellWorker()
1818 // ============================================================================
1820 // ============================================================================
1822 // ----------------------------------------------------------------------------
1823 // wxGridCellRenderer
1824 // ----------------------------------------------------------------------------
1826 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1827 wxGridCellAttr
& attr
,
1830 int WXUNUSED(row
), int WXUNUSED(col
),
1833 dc
.SetBackgroundMode( wxBRUSHSTYLE_SOLID
);
1835 // grey out fields if the grid is disabled
1836 if ( grid
.IsEnabled() )
1841 if ( grid
.HasFocus() )
1842 clr
= grid
.GetSelectionBackground();
1844 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1845 dc
.SetBrush( wxBrush(clr
, wxBRUSHSTYLE_SOLID
) );
1849 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
1854 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxBRUSHSTYLE_SOLID
));
1857 dc
.SetPen( *wxTRANSPARENT_PEN
);
1858 dc
.DrawRectangle(rect
);
1861 // ----------------------------------------------------------------------------
1862 // wxGridCellStringRenderer
1863 // ----------------------------------------------------------------------------
1865 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1866 const wxGridCellAttr
& attr
,
1870 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
1872 // TODO some special colours for attr.IsReadOnly() case?
1874 // different coloured text when the grid is disabled
1875 if ( grid
.IsEnabled() )
1880 if ( grid
.HasFocus() )
1881 clr
= grid
.GetSelectionBackground();
1883 clr
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW
);
1884 dc
.SetTextBackground( clr
);
1885 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1889 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1890 dc
.SetTextForeground( attr
.GetTextColour() );
1895 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1896 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1899 dc
.SetFont( attr
.GetFont() );
1902 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1904 const wxString
& text
)
1906 wxCoord x
= 0, y
= 0, max_x
= 0;
1907 dc
.SetFont(attr
.GetFont());
1908 wxStringTokenizer
tk(text
, _T('\n'));
1909 while ( tk
.HasMoreTokens() )
1911 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1912 max_x
= wxMax(max_x
, x
);
1915 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1917 return wxSize(max_x
, y
);
1920 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1921 wxGridCellAttr
& attr
,
1925 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1928 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1929 wxGridCellAttr
& attr
,
1931 const wxRect
& rectCell
,
1935 wxRect rect
= rectCell
;
1938 // erase only this cells background, overflow cells should have been erased
1939 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1942 attr
.GetAlignment(&hAlign
, &vAlign
);
1944 int overflowCols
= 0;
1946 if (attr
.GetOverflow())
1948 int cols
= grid
.GetNumberCols();
1949 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1950 int cell_rows
, cell_cols
;
1951 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1952 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1954 int i
, c_cols
, c_rows
;
1955 for (i
= col
+cell_cols
; i
< cols
; i
++)
1957 bool is_empty
= true;
1958 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1960 // check w/ anchor cell for multicell block
1961 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1964 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1973 rect
.width
+= grid
.GetColSize(i
);
1981 if (rect
.width
>= best_width
)
1985 overflowCols
= i
- col
- cell_cols
+ 1;
1986 if (overflowCols
>= cols
)
1987 overflowCols
= cols
- 1;
1990 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1992 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1994 clip
.x
+= rectCell
.width
;
1995 // draw each overflow cell individually
1996 int col_end
= col
+ cell_cols
+ overflowCols
;
1997 if (col_end
>= grid
.GetNumberCols())
1998 col_end
= grid
.GetNumberCols() - 1;
1999 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
2001 clip
.width
= grid
.GetColSize(i
) - 1;
2002 dc
.DestroyClippingRegion();
2003 dc
.SetClippingRegion(clip
);
2005 SetTextColoursAndFont(grid
, attr
, dc
,
2006 grid
.IsInSelection(row
,i
));
2008 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2009 rect
, hAlign
, vAlign
);
2010 clip
.x
+= grid
.GetColSize(i
) - 1;
2016 dc
.DestroyClippingRegion();
2020 // now we only have to draw the text
2021 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2023 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
2024 rect
, hAlign
, vAlign
);
2027 // ----------------------------------------------------------------------------
2028 // wxGridCellNumberRenderer
2029 // ----------------------------------------------------------------------------
2031 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2033 wxGridTableBase
*table
= grid
.GetTable();
2035 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
2037 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
2041 text
= table
->GetValue(row
, col
);
2047 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2048 wxGridCellAttr
& attr
,
2050 const wxRect
& rectCell
,
2054 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2056 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2058 // draw the text right aligned by default
2060 attr
.GetAlignment(&hAlign
, &vAlign
);
2061 hAlign
= wxALIGN_RIGHT
;
2063 wxRect rect
= rectCell
;
2066 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2069 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2070 wxGridCellAttr
& attr
,
2074 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2077 // ----------------------------------------------------------------------------
2078 // wxGridCellFloatRenderer
2079 // ----------------------------------------------------------------------------
2081 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2084 SetPrecision(precision
);
2087 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2089 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2090 renderer
->m_width
= m_width
;
2091 renderer
->m_precision
= m_precision
;
2092 renderer
->m_format
= m_format
;
2097 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2099 wxGridTableBase
*table
= grid
.GetTable();
2104 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2106 val
= table
->GetValueAsDouble(row
, col
);
2111 text
= table
->GetValue(row
, col
);
2112 hasDouble
= text
.ToDouble(&val
);
2119 if ( m_width
== -1 )
2121 if ( m_precision
== -1 )
2123 // default width/precision
2124 m_format
= _T("%f");
2128 m_format
.Printf(_T("%%.%df"), m_precision
);
2131 else if ( m_precision
== -1 )
2133 // default precision
2134 m_format
.Printf(_T("%%%d.f"), m_width
);
2138 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2142 text
.Printf(m_format
, val
);
2145 //else: text already contains the string
2150 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2151 wxGridCellAttr
& attr
,
2153 const wxRect
& rectCell
,
2157 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2159 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2161 // draw the text right aligned by default
2163 attr
.GetAlignment(&hAlign
, &vAlign
);
2164 hAlign
= wxALIGN_RIGHT
;
2166 wxRect rect
= rectCell
;
2169 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2172 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2173 wxGridCellAttr
& attr
,
2177 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2180 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2184 // reset to defaults
2190 wxString tmp
= params
.BeforeFirst(_T(','));
2194 if ( tmp
.ToLong(&width
) )
2196 SetWidth((int)width
);
2200 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2204 tmp
= params
.AfterFirst(_T(','));
2208 if ( tmp
.ToLong(&precision
) )
2210 SetPrecision((int)precision
);
2214 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2220 // ----------------------------------------------------------------------------
2221 // wxGridCellBoolRenderer
2222 // ----------------------------------------------------------------------------
2224 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2226 // FIXME these checkbox size calculations are really ugly...
2228 // between checkmark and box
2229 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2231 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2232 wxGridCellAttr
& WXUNUSED(attr
),
2237 // compute it only once (no locks for MT safeness in GUI thread...)
2238 if ( !ms_sizeCheckMark
.x
)
2240 // get checkbox size
2241 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2242 wxSize size
= checkbox
->GetBestSize();
2243 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2245 #if defined(__WXMOTIF__)
2246 checkSize
-= size
.y
/ 2;
2251 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2254 return ms_sizeCheckMark
;
2257 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2258 wxGridCellAttr
& attr
,
2264 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2266 // draw a check mark in the centre (ignoring alignment - TODO)
2267 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2269 // don't draw outside the cell
2270 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2271 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2273 // and even leave (at least) 1 pixel margin
2274 size
.x
= size
.y
= minSize
;
2277 // draw a border around checkmark
2279 attr
.GetAlignment(&hAlign
, &vAlign
);
2282 if (hAlign
== wxALIGN_CENTRE
)
2284 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2285 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2286 rectBorder
.width
= size
.x
;
2287 rectBorder
.height
= size
.y
;
2289 else if (hAlign
== wxALIGN_LEFT
)
2291 rectBorder
.x
= rect
.x
+ 2;
2292 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2293 rectBorder
.width
= size
.x
;
2294 rectBorder
.height
= size
.y
;
2296 else if (hAlign
== wxALIGN_RIGHT
)
2298 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2299 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2300 rectBorder
.width
= size
.x
;
2301 rectBorder
.height
= size
.y
;
2305 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2307 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2311 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2312 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2317 flags
|= wxCONTROL_CHECKED
;
2319 wxRendererNative::Get().DrawCheckBox( &grid
, dc
, rectBorder
, flags
);
2322 // ----------------------------------------------------------------------------
2324 // ----------------------------------------------------------------------------
2326 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2330 m_isReadOnly
= Unset
;
2335 m_attrkind
= wxGridCellAttr::Cell
;
2337 m_sizeRows
= m_sizeCols
= 1;
2338 m_overflow
= UnsetOverflow
;
2340 SetDefAttr(attrDefault
);
2343 wxGridCellAttr
*wxGridCellAttr::Clone() const
2345 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2347 if ( HasTextColour() )
2348 attr
->SetTextColour(GetTextColour());
2349 if ( HasBackgroundColour() )
2350 attr
->SetBackgroundColour(GetBackgroundColour());
2352 attr
->SetFont(GetFont());
2353 if ( HasAlignment() )
2354 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2356 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2360 attr
->SetRenderer(m_renderer
);
2361 m_renderer
->IncRef();
2365 attr
->SetEditor(m_editor
);
2370 attr
->SetReadOnly();
2372 attr
->SetOverflow( m_overflow
== Overflow
);
2373 attr
->SetKind( m_attrkind
);
2378 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2380 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2381 SetTextColour(mergefrom
->GetTextColour());
2382 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2383 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2384 if ( !HasFont() && mergefrom
->HasFont() )
2385 SetFont(mergefrom
->GetFont());
2386 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2389 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2390 SetAlignment(hAlign
, vAlign
);
2392 if ( !HasSize() && mergefrom
->HasSize() )
2393 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2395 // Directly access member functions as GetRender/Editor don't just return
2396 // m_renderer/m_editor
2398 // Maybe add support for merge of Render and Editor?
2399 if (!HasRenderer() && mergefrom
->HasRenderer() )
2401 m_renderer
= mergefrom
->m_renderer
;
2402 m_renderer
->IncRef();
2404 if ( !HasEditor() && mergefrom
->HasEditor() )
2406 m_editor
= mergefrom
->m_editor
;
2409 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2410 SetReadOnly(mergefrom
->IsReadOnly());
2412 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2413 SetOverflow(mergefrom
->GetOverflow());
2415 SetDefAttr(mergefrom
->m_defGridAttr
);
2418 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2420 // The size of a cell is normally 1,1
2422 // If this cell is larger (2,2) then this is the top left cell
2423 // the other cells that will be covered (lower right cells) must be
2424 // set to negative or zero values such that
2425 // row + num_rows of the covered cell points to the larger cell (this cell)
2426 // same goes for the col + num_cols.
2428 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2430 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2431 !((num_rows
<= 0) && (num_cols
> 0)) ||
2432 !((num_rows
== 0) && (num_cols
== 0))),
2433 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2435 m_sizeRows
= num_rows
;
2436 m_sizeCols
= num_cols
;
2439 const wxColour
& wxGridCellAttr::GetTextColour() const
2441 if (HasTextColour())
2445 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2447 return m_defGridAttr
->GetTextColour();
2451 wxFAIL_MSG(wxT("Missing default cell attribute"));
2452 return wxNullColour
;
2456 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2458 if (HasBackgroundColour())
2462 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2464 return m_defGridAttr
->GetBackgroundColour();
2468 wxFAIL_MSG(wxT("Missing default cell attribute"));
2469 return wxNullColour
;
2473 const wxFont
& wxGridCellAttr::GetFont() const
2479 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2481 return m_defGridAttr
->GetFont();
2485 wxFAIL_MSG(wxT("Missing default cell attribute"));
2490 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2499 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2501 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2505 wxFAIL_MSG(wxT("Missing default cell attribute"));
2509 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2512 *num_rows
= m_sizeRows
;
2514 *num_cols
= m_sizeCols
;
2517 // GetRenderer and GetEditor use a slightly different decision path about
2518 // which attribute to use. If a non-default attr object has one then it is
2519 // used, otherwise the default editor or renderer is fetched from the grid and
2520 // used. It should be the default for the data type of the cell. If it is
2521 // NULL (because the table has a type that the grid does not have in its
2522 // registry), then the grid's default editor or renderer is used.
2524 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2526 wxGridCellRenderer
*renderer
= NULL
;
2528 if ( m_renderer
&& this != m_defGridAttr
)
2530 // use the cells renderer if it has one
2531 renderer
= m_renderer
;
2534 else // no non-default cell renderer
2536 // get default renderer for the data type
2539 // GetDefaultRendererForCell() will do IncRef() for us
2540 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2543 if ( renderer
== NULL
)
2545 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2547 // if we still don't have one then use the grid default
2548 // (no need for IncRef() here neither)
2549 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2551 else // default grid attr
2553 // use m_renderer which we had decided not to use initially
2554 renderer
= m_renderer
;
2561 // we're supposed to always find something
2562 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2567 // same as above, except for s/renderer/editor/g
2568 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2570 wxGridCellEditor
*editor
= NULL
;
2572 if ( m_editor
&& this != m_defGridAttr
)
2574 // use the cells editor if it has one
2578 else // no non default cell editor
2580 // get default editor for the data type
2583 // GetDefaultEditorForCell() will do IncRef() for us
2584 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2587 if ( editor
== NULL
)
2589 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2591 // if we still don't have one then use the grid default
2592 // (no need for IncRef() here neither)
2593 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2595 else // default grid attr
2597 // use m_editor which we had decided not to use initially
2605 // we're supposed to always find something
2606 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2611 // ----------------------------------------------------------------------------
2612 // wxGridCellAttrData
2613 // ----------------------------------------------------------------------------
2615 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2617 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
2618 // touch attribute's reference counting explicitly, since this
2619 // is managed by class wxGridCellWithAttr
2620 int n
= FindIndex(row
, col
);
2621 if ( n
== wxNOT_FOUND
)
2625 // add the attribute
2626 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2628 //else: nothing to do
2630 else // we already have an attribute for this cell
2634 // change the attribute
2635 m_attrs
[(size_t)n
].ChangeAttr(attr
);
2639 // remove this attribute
2640 m_attrs
.RemoveAt((size_t)n
);
2645 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2647 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2649 int n
= FindIndex(row
, col
);
2650 if ( n
!= wxNOT_FOUND
)
2652 attr
= m_attrs
[(size_t)n
].attr
;
2659 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2661 size_t count
= m_attrs
.GetCount();
2662 for ( size_t n
= 0; n
< count
; n
++ )
2664 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2665 wxCoord row
= coords
.GetRow();
2666 if ((size_t)row
>= pos
)
2670 // If rows inserted, include row counter where necessary
2671 coords
.SetRow(row
+ numRows
);
2673 else if (numRows
< 0)
2675 // If rows deleted ...
2676 if ((size_t)row
>= pos
- numRows
)
2678 // ...either decrement row counter (if row still exists)...
2679 coords
.SetRow(row
+ numRows
);
2683 // ...or remove the attribute
2684 m_attrs
.RemoveAt(n
);
2693 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2695 size_t count
= m_attrs
.GetCount();
2696 for ( size_t n
= 0; n
< count
; n
++ )
2698 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2699 wxCoord col
= coords
.GetCol();
2700 if ( (size_t)col
>= pos
)
2704 // If rows inserted, include row counter where necessary
2705 coords
.SetCol(col
+ numCols
);
2707 else if (numCols
< 0)
2709 // If rows deleted ...
2710 if ((size_t)col
>= pos
- numCols
)
2712 // ...either decrement row counter (if row still exists)...
2713 coords
.SetCol(col
+ numCols
);
2717 // ...or remove the attribute
2718 m_attrs
.RemoveAt(n
);
2727 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2729 size_t count
= m_attrs
.GetCount();
2730 for ( size_t n
= 0; n
< count
; n
++ )
2732 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2733 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2742 // ----------------------------------------------------------------------------
2743 // wxGridRowOrColAttrData
2744 // ----------------------------------------------------------------------------
2746 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2748 size_t count
= m_attrs
.GetCount();
2749 for ( size_t n
= 0; n
< count
; n
++ )
2751 m_attrs
[n
]->DecRef();
2755 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2757 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2759 int n
= m_rowsOrCols
.Index(rowOrCol
);
2760 if ( n
!= wxNOT_FOUND
)
2762 attr
= m_attrs
[(size_t)n
];
2769 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2771 int i
= m_rowsOrCols
.Index(rowOrCol
);
2772 if ( i
== wxNOT_FOUND
)
2776 // add the attribute - no need to do anything to reference count
2777 // since we take ownership of the attribute.
2778 m_rowsOrCols
.Add(rowOrCol
);
2781 // nothing to remove
2785 size_t n
= (size_t)i
;
2786 if ( m_attrs
[n
] == attr
)
2791 // change the attribute, handling reference count manually,
2792 // taking ownership of the new attribute.
2793 m_attrs
[n
]->DecRef();
2798 // remove this attribute, handling reference count manually
2799 m_attrs
[n
]->DecRef();
2800 m_rowsOrCols
.RemoveAt(n
);
2801 m_attrs
.RemoveAt(n
);
2806 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2808 size_t count
= m_attrs
.GetCount();
2809 for ( size_t n
= 0; n
< count
; n
++ )
2811 int & rowOrCol
= m_rowsOrCols
[n
];
2812 if ( (size_t)rowOrCol
>= pos
)
2814 if ( numRowsOrCols
> 0 )
2816 // If rows inserted, include row counter where necessary
2817 rowOrCol
+= numRowsOrCols
;
2819 else if ( numRowsOrCols
< 0)
2821 // If rows deleted, either decrement row counter (if row still exists)
2822 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2823 rowOrCol
+= numRowsOrCols
;
2826 m_rowsOrCols
.RemoveAt(n
);
2827 m_attrs
[n
]->DecRef();
2828 m_attrs
.RemoveAt(n
);
2837 // ----------------------------------------------------------------------------
2838 // wxGridCellAttrProvider
2839 // ----------------------------------------------------------------------------
2841 wxGridCellAttrProvider::wxGridCellAttrProvider()
2843 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2846 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2851 void wxGridCellAttrProvider::InitData()
2853 m_data
= new wxGridCellAttrProviderData
;
2856 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2857 wxGridCellAttr::wxAttrKind kind
) const
2859 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2864 case (wxGridCellAttr::Any
):
2865 // Get cached merge attributes.
2866 // Currently not used as no cache implemented as not mutable
2867 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2870 // Basically implement old version.
2871 // Also check merge cache, so we don't have to re-merge every time..
2872 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2873 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2874 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2876 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2878 // Two or more are non NULL
2879 attr
= new wxGridCellAttr
;
2880 attr
->SetKind(wxGridCellAttr::Merged
);
2882 // Order is important..
2885 attr
->MergeWith(attrcell
);
2890 attr
->MergeWith(attrcol
);
2895 attr
->MergeWith(attrrow
);
2899 // store merge attr if cache implemented
2901 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2905 // one or none is non null return it or null.
2924 case (wxGridCellAttr::Cell
):
2925 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2928 case (wxGridCellAttr::Col
):
2929 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2932 case (wxGridCellAttr::Row
):
2933 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2938 // (wxGridCellAttr::Default):
2939 // (wxGridCellAttr::Merged):
2947 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2953 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2956 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2961 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2964 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2969 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2972 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2976 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2978 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2982 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2986 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2988 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2992 // ----------------------------------------------------------------------------
2993 // wxGridTypeRegistry
2994 // ----------------------------------------------------------------------------
2996 wxGridTypeRegistry::~wxGridTypeRegistry()
2998 size_t count
= m_typeinfo
.GetCount();
2999 for ( size_t i
= 0; i
< count
; i
++ )
3000 delete m_typeinfo
[i
];
3003 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
3004 wxGridCellRenderer
* renderer
,
3005 wxGridCellEditor
* editor
)
3007 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
3009 // is it already registered?
3010 int loc
= FindRegisteredDataType(typeName
);
3011 if ( loc
!= wxNOT_FOUND
)
3013 delete m_typeinfo
[loc
];
3014 m_typeinfo
[loc
] = info
;
3018 m_typeinfo
.Add(info
);
3022 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
3024 size_t count
= m_typeinfo
.GetCount();
3025 for ( size_t i
= 0; i
< count
; i
++ )
3027 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
3036 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3038 int index
= FindRegisteredDataType(typeName
);
3039 if ( index
== wxNOT_FOUND
)
3041 // check whether this is one of the standard ones, in which case
3042 // register it "on the fly"
3044 if ( typeName
== wxGRID_VALUE_STRING
)
3046 RegisterDataType(wxGRID_VALUE_STRING
,
3047 new wxGridCellStringRenderer
,
3048 new wxGridCellTextEditor
);
3051 #endif // wxUSE_TEXTCTRL
3053 if ( typeName
== wxGRID_VALUE_BOOL
)
3055 RegisterDataType(wxGRID_VALUE_BOOL
,
3056 new wxGridCellBoolRenderer
,
3057 new wxGridCellBoolEditor
);
3060 #endif // wxUSE_CHECKBOX
3062 if ( typeName
== wxGRID_VALUE_NUMBER
)
3064 RegisterDataType(wxGRID_VALUE_NUMBER
,
3065 new wxGridCellNumberRenderer
,
3066 new wxGridCellNumberEditor
);
3068 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3070 RegisterDataType(wxGRID_VALUE_FLOAT
,
3071 new wxGridCellFloatRenderer
,
3072 new wxGridCellFloatEditor
);
3075 #endif // wxUSE_TEXTCTRL
3077 if ( typeName
== wxGRID_VALUE_CHOICE
)
3079 RegisterDataType(wxGRID_VALUE_CHOICE
,
3080 new wxGridCellStringRenderer
,
3081 new wxGridCellChoiceEditor
);
3084 #endif // wxUSE_COMBOBOX
3089 // we get here only if just added the entry for this type, so return
3091 index
= m_typeinfo
.GetCount() - 1;
3097 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3099 int index
= FindDataType(typeName
);
3100 if ( index
== wxNOT_FOUND
)
3102 // the first part of the typename is the "real" type, anything after ':'
3103 // are the parameters for the renderer
3104 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3105 if ( index
== wxNOT_FOUND
)
3110 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3111 wxGridCellRenderer
*rendererOld
= renderer
;
3112 renderer
= renderer
->Clone();
3113 rendererOld
->DecRef();
3115 wxGridCellEditor
*editor
= GetEditor(index
);
3116 wxGridCellEditor
*editorOld
= editor
;
3117 editor
= editor
->Clone();
3118 editorOld
->DecRef();
3120 // do it even if there are no parameters to reset them to defaults
3121 wxString params
= typeName
.AfterFirst(_T(':'));
3122 renderer
->SetParameters(params
);
3123 editor
->SetParameters(params
);
3125 // register the new typename
3126 RegisterDataType(typeName
, renderer
, editor
);
3128 // we just registered it, it's the last one
3129 index
= m_typeinfo
.GetCount() - 1;
3135 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3137 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3144 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3146 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3153 // ----------------------------------------------------------------------------
3155 // ----------------------------------------------------------------------------
3157 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3159 wxGridTableBase::wxGridTableBase()
3161 m_view
= (wxGrid
*) NULL
;
3162 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3165 wxGridTableBase::~wxGridTableBase()
3167 delete m_attrProvider
;
3170 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3172 delete m_attrProvider
;
3173 m_attrProvider
= attrProvider
;
3176 bool wxGridTableBase::CanHaveAttributes()
3178 if ( ! GetAttrProvider() )
3180 // use the default attr provider by default
3181 SetAttrProvider(new wxGridCellAttrProvider
);
3187 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3189 if ( m_attrProvider
)
3190 return m_attrProvider
->GetAttr(row
, col
, kind
);
3192 return (wxGridCellAttr
*)NULL
;
3195 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3197 if ( m_attrProvider
)
3200 attr
->SetKind(wxGridCellAttr::Cell
);
3201 m_attrProvider
->SetAttr(attr
, row
, col
);
3205 // as we take ownership of the pointer and don't store it, we must
3211 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3213 if ( m_attrProvider
)
3215 attr
->SetKind(wxGridCellAttr::Row
);
3216 m_attrProvider
->SetRowAttr(attr
, row
);
3220 // as we take ownership of the pointer and don't store it, we must
3226 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3228 if ( m_attrProvider
)
3230 attr
->SetKind(wxGridCellAttr::Col
);
3231 m_attrProvider
->SetColAttr(attr
, col
);
3235 // as we take ownership of the pointer and don't store it, we must
3241 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3242 size_t WXUNUSED(numRows
) )
3244 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3249 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3251 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3256 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3257 size_t WXUNUSED(numRows
) )
3259 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3264 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3265 size_t WXUNUSED(numCols
) )
3267 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3272 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3274 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3279 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3280 size_t WXUNUSED(numCols
) )
3282 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3287 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3291 // RD: Starting the rows at zero confuses users,
3292 // no matter how much it makes sense to us geeks.
3298 wxString
wxGridTableBase::GetColLabelValue( int col
)
3300 // default col labels are:
3301 // cols 0 to 25 : A-Z
3302 // cols 26 to 675 : AA-ZZ
3307 for ( n
= 1; ; n
++ )
3309 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3315 // reverse the string...
3317 for ( i
= 0; i
< n
; i
++ )
3325 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3327 return wxGRID_VALUE_STRING
;
3330 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3331 const wxString
& typeName
)
3333 return typeName
== wxGRID_VALUE_STRING
;
3336 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3338 return CanGetValueAs(row
, col
, typeName
);
3341 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3346 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3351 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3356 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3357 long WXUNUSED(value
) )
3361 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3362 double WXUNUSED(value
) )
3366 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3367 bool WXUNUSED(value
) )
3371 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3372 const wxString
& WXUNUSED(typeName
) )
3377 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3378 const wxString
& WXUNUSED(typeName
),
3379 void* WXUNUSED(value
) )
3383 //////////////////////////////////////////////////////////////////////
3385 // Message class for the grid table to send requests and notifications
3389 wxGridTableMessage::wxGridTableMessage()
3391 m_table
= (wxGridTableBase
*) NULL
;
3397 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3398 int commandInt1
, int commandInt2
)
3402 m_comInt1
= commandInt1
;
3403 m_comInt2
= commandInt2
;
3406 //////////////////////////////////////////////////////////////////////
3408 // A basic grid table for string data. An object of this class will
3409 // created by wxGrid if you don't specify an alternative table class.
3412 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3414 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3416 wxGridStringTable::wxGridStringTable()
3421 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3424 m_data
.Alloc( numRows
);
3427 sa
.Alloc( numCols
);
3428 sa
.Add( wxEmptyString
, numCols
);
3430 m_data
.Add( sa
, numRows
);
3433 wxGridStringTable::~wxGridStringTable()
3437 int wxGridStringTable::GetNumberRows()
3439 return m_data
.GetCount();
3442 int wxGridStringTable::GetNumberCols()
3444 if ( m_data
.GetCount() > 0 )
3445 return m_data
[0].GetCount();
3450 wxString
wxGridStringTable::GetValue( int row
, int col
)
3452 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3454 _T("invalid row or column index in wxGridStringTable") );
3456 return m_data
[row
][col
];
3459 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3461 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3462 _T("invalid row or column index in wxGridStringTable") );
3464 m_data
[row
][col
] = value
;
3467 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3469 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3471 _T("invalid row or column index in wxGridStringTable") );
3473 return (m_data
[row
][col
] == wxEmptyString
);
3476 void wxGridStringTable::Clear()
3479 int numRows
, numCols
;
3481 numRows
= m_data
.GetCount();
3484 numCols
= m_data
[0].GetCount();
3486 for ( row
= 0; row
< numRows
; row
++ )
3488 for ( col
= 0; col
< numCols
; col
++ )
3490 m_data
[row
][col
] = wxEmptyString
;
3496 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3498 size_t curNumRows
= m_data
.GetCount();
3499 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3500 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3502 if ( pos
>= curNumRows
)
3504 return AppendRows( numRows
);
3508 sa
.Alloc( curNumCols
);
3509 sa
.Add( wxEmptyString
, curNumCols
);
3510 m_data
.Insert( sa
, pos
, numRows
);
3514 wxGridTableMessage
msg( this,
3515 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3519 GetView()->ProcessTableMessage( msg
);
3525 bool wxGridStringTable::AppendRows( size_t numRows
)
3527 size_t curNumRows
= m_data
.GetCount();
3528 size_t curNumCols
= ( curNumRows
> 0
3529 ? m_data
[0].GetCount()
3530 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3533 if ( curNumCols
> 0 )
3535 sa
.Alloc( curNumCols
);
3536 sa
.Add( wxEmptyString
, curNumCols
);
3539 m_data
.Add( sa
, numRows
);
3543 wxGridTableMessage
msg( this,
3544 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3547 GetView()->ProcessTableMessage( msg
);
3553 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3555 size_t curNumRows
= m_data
.GetCount();
3557 if ( pos
>= curNumRows
)
3559 wxFAIL_MSG( wxString::Format
3561 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3563 (unsigned long)numRows
,
3564 (unsigned long)curNumRows
3570 if ( numRows
> curNumRows
- pos
)
3572 numRows
= curNumRows
- pos
;
3575 if ( numRows
>= curNumRows
)
3581 m_data
.RemoveAt( pos
, numRows
);
3586 wxGridTableMessage
msg( this,
3587 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3591 GetView()->ProcessTableMessage( msg
);
3597 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3601 size_t curNumRows
= m_data
.GetCount();
3602 size_t curNumCols
= ( curNumRows
> 0
3603 ? m_data
[0].GetCount()
3604 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3606 if ( pos
>= curNumCols
)
3608 return AppendCols( numCols
);
3611 if ( !m_colLabels
.IsEmpty() )
3613 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3616 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3617 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3620 for ( row
= 0; row
< curNumRows
; row
++ )
3622 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3624 m_data
[row
].Insert( wxEmptyString
, col
);
3630 wxGridTableMessage
msg( this,
3631 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3635 GetView()->ProcessTableMessage( msg
);
3641 bool wxGridStringTable::AppendCols( size_t numCols
)
3645 size_t curNumRows
= m_data
.GetCount();
3650 // TODO: something better than this ?
3652 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3657 for ( row
= 0; row
< curNumRows
; row
++ )
3659 m_data
[row
].Add( wxEmptyString
, numCols
);
3664 wxGridTableMessage
msg( this,
3665 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3668 GetView()->ProcessTableMessage( msg
);
3674 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3678 size_t curNumRows
= m_data
.GetCount();
3679 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3680 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3682 if ( pos
>= curNumCols
)
3684 wxFAIL_MSG( wxString::Format
3686 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3688 (unsigned long)numCols
,
3689 (unsigned long)curNumCols
3696 colID
= GetView()->GetColAt( pos
);
3700 if ( numCols
> curNumCols
- colID
)
3702 numCols
= curNumCols
- colID
;
3705 if ( !m_colLabels
.IsEmpty() )
3707 // m_colLabels stores just as many elements as it needs, e.g. if only
3708 // the label of the first column had been set it would have only one
3709 // element and not numCols, so account for it
3710 int nToRm
= m_colLabels
.size() - colID
;
3712 m_colLabels
.RemoveAt( colID
, nToRm
);
3715 for ( row
= 0; row
< curNumRows
; row
++ )
3717 if ( numCols
>= curNumCols
)
3719 m_data
[row
].Clear();
3723 m_data
[row
].RemoveAt( colID
, numCols
);
3729 wxGridTableMessage
msg( this,
3730 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3734 GetView()->ProcessTableMessage( msg
);
3740 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3742 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3744 // using default label
3746 return wxGridTableBase::GetRowLabelValue( row
);
3750 return m_rowLabels
[row
];
3754 wxString
wxGridStringTable::GetColLabelValue( int col
)
3756 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3758 // using default label
3760 return wxGridTableBase::GetColLabelValue( col
);
3764 return m_colLabels
[col
];
3768 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3770 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3772 int n
= m_rowLabels
.GetCount();
3775 for ( i
= n
; i
<= row
; i
++ )
3777 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3781 m_rowLabels
[row
] = value
;
3784 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3786 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3788 int n
= m_colLabels
.GetCount();
3791 for ( i
= n
; i
<= col
; i
++ )
3793 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3797 m_colLabels
[col
] = value
;
3801 //////////////////////////////////////////////////////////////////////
3802 //////////////////////////////////////////////////////////////////////
3804 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3805 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3808 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3810 m_owner
->CancelMouseCapture();
3813 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3815 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3816 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3817 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3818 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3821 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3823 const wxPoint
&pos
, const wxSize
&size
)
3824 : wxGridSubwindow(parent
, id
, pos
, size
)
3829 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3833 // NO - don't do this because it will set both the x and y origin
3834 // coords to match the parent scrolled window and we just want to
3835 // set the y coord - MB
3837 // m_owner->PrepareDC( dc );
3840 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3841 wxPoint pt
= dc
.GetDeviceOrigin();
3842 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3844 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3845 m_owner
->DrawRowLabels( dc
, rows
);
3848 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3850 m_owner
->ProcessRowLabelMouseEvent( event
);
3853 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3855 m_owner
->GetEventHandler()->ProcessEvent( event
);
3858 //////////////////////////////////////////////////////////////////////
3860 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3862 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3863 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3864 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3865 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3868 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3870 const wxPoint
&pos
, const wxSize
&size
)
3871 : wxGridSubwindow(parent
, id
, pos
, size
)
3876 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3880 // NO - don't do this because it will set both the x and y origin
3881 // coords to match the parent scrolled window and we just want to
3882 // set the x coord - MB
3884 // m_owner->PrepareDC( dc );
3887 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3888 wxPoint pt
= dc
.GetDeviceOrigin();
3889 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3890 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3892 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3894 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3895 m_owner
->DrawColLabels( dc
, cols
);
3898 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3900 m_owner
->ProcessColLabelMouseEvent( event
);
3903 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3905 m_owner
->GetEventHandler()->ProcessEvent( event
);
3908 //////////////////////////////////////////////////////////////////////
3910 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3912 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3913 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3914 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3915 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3918 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3920 const wxPoint
&pos
, const wxSize
&size
)
3921 : wxGridSubwindow(parent
, id
, pos
, size
)
3926 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3930 int client_height
= 0;
3931 int client_width
= 0;
3932 GetClientSize( &client_width
, &client_height
);
3934 // VZ: any reason for this ifdef? (FIXME)
3940 rect
.SetWidth( client_width
- 2 );
3941 rect
.SetHeight( client_height
- 2 );
3943 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3945 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
3946 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3947 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3948 dc
.DrawLine( 0, 0, client_width
, 0 );
3949 dc
.DrawLine( 0, 0, 0, client_height
);
3951 dc
.SetPen( *wxWHITE_PEN
);
3952 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3953 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3957 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3959 m_owner
->ProcessCornerLabelMouseEvent( event
);
3962 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3964 m_owner
->GetEventHandler()->ProcessEvent(event
);
3967 //////////////////////////////////////////////////////////////////////
3969 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3971 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
3972 EVT_PAINT( wxGridWindow::OnPaint
)
3973 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3974 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3975 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3976 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3977 EVT_CHAR( wxGridWindow::OnChar
)
3978 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3979 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3980 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3983 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3984 wxGridRowLabelWindow
*rowLblWin
,
3985 wxGridColLabelWindow
*colLblWin
,
3988 const wxSize
&size
)
3989 : wxGridSubwindow(parent
, id
, pos
, size
,
3990 wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3991 wxT("grid window") )
3994 m_rowLabelWin
= rowLblWin
;
3995 m_colLabelWin
= colLblWin
;
3998 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4000 wxPaintDC
dc( this );
4001 m_owner
->PrepareDC( dc
);
4002 wxRegion reg
= GetUpdateRegion();
4003 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4004 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4006 #if WXGRID_DRAW_LINES
4007 m_owner
->DrawAllGridLines( dc
, reg
);
4010 m_owner
->DrawGridSpace( dc
);
4011 m_owner
->DrawHighlight( dc
, dirtyCells
);
4014 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4016 wxWindow::ScrollWindow( dx
, dy
, rect
);
4017 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4018 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4021 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4023 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4026 m_owner
->ProcessGridCellMouseEvent( event
);
4029 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4031 m_owner
->GetEventHandler()->ProcessEvent( event
);
4034 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4035 // cursor must be in the cell edit control to get key events
4037 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4039 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4043 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4045 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4049 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4051 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4055 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4059 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4061 // and if we have any selection, it has to be repainted, because it
4062 // uses different colour when the grid is not focused:
4063 if ( m_owner
->IsSelection() )
4069 // NB: Note that this code is in "else" branch only because the other
4070 // branch refreshes everything and so there's no point in calling
4071 // Refresh() again, *not* because it should only be done if
4072 // !IsSelection(). If the above code is ever optimized to refresh
4073 // only selected area, this needs to be moved out of the "else"
4074 // branch so that it's always executed.
4076 // current cell cursor {dis,re}appears on focus change:
4077 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
4078 m_owner
->GetGridCursorCol());
4079 const wxRect cursor
=
4080 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
4081 Refresh(true, &cursor
);
4084 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4088 //////////////////////////////////////////////////////////////////////
4090 // Internal Helper function for computing row or column from some
4091 // (unscrolled) coordinate value, using either
4092 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4093 // of m_rowBottoms/m_ColRights to speed up the search!
4095 // Internal helper macros for simpler use of that function
4097 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4098 const wxArrayInt
& BorderArray
, int nMax
,
4101 #define internalXToCol(x) XToCol(x, true)
4102 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4103 m_minAcceptableRowHeight, \
4104 m_rowBottoms, m_numRows, true)
4106 /////////////////////////////////////////////////////////////////////
4108 #if wxUSE_EXTENDED_RTTI
4109 WX_DEFINE_FLAGS( wxGridStyle
)
4111 wxBEGIN_FLAGS( wxGridStyle
)
4112 // new style border flags, we put them first to
4113 // use them for streaming out
4114 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4115 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4116 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4117 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4118 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4119 wxFLAGS_MEMBER(wxBORDER_NONE
)
4121 // old style border flags
4122 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4123 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4124 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4125 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4126 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4127 wxFLAGS_MEMBER(wxBORDER
)
4129 // standard window styles
4130 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4131 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4132 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4133 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4134 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4135 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4136 wxFLAGS_MEMBER(wxVSCROLL
)
4137 wxFLAGS_MEMBER(wxHSCROLL
)
4139 wxEND_FLAGS( wxGridStyle
)
4141 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4143 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4144 wxHIDE_PROPERTY( Children
)
4145 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4146 wxEND_PROPERTIES_TABLE()
4148 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4149 wxEND_HANDLERS_TABLE()
4151 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4154 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4157 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4160 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4161 EVT_PAINT( wxGrid::OnPaint
)
4162 EVT_SIZE( wxGrid::OnSize
)
4163 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4164 EVT_KEY_UP( wxGrid::OnKeyUp
)
4165 EVT_CHAR ( wxGrid::OnChar
)
4166 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4174 wxGrid::wxGrid( wxWindow
*parent
,
4179 const wxString
& name
)
4182 Create(parent
, id
, pos
, size
, style
, name
);
4185 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4186 const wxPoint
& pos
, const wxSize
& size
,
4187 long style
, const wxString
& name
)
4189 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4190 style
| wxWANTS_CHARS
, name
))
4193 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4194 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4197 SetInitialSize(size
);
4205 // Must do this or ~wxScrollHelper will pop the wrong event handler
4206 SetTargetWindow(this);
4208 wxSafeDecRef(m_defaultCellAttr
);
4210 #ifdef DEBUG_ATTR_CACHE
4211 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4212 wxPrintf(_T("wxGrid attribute cache statistics: "
4213 "total: %u, hits: %u (%u%%)\n"),
4214 total
, gs_nAttrCacheHits
,
4215 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4218 // if we own the table, just delete it, otherwise at least don't leave it
4219 // with dangling view pointer
4222 else if ( m_table
&& m_table
->GetView() == this )
4223 m_table
->SetView(NULL
);
4225 delete m_typeRegistry
;
4230 // ----- internal init and update functions
4233 // NOTE: If using the default visual attributes works everywhere then this can
4234 // be removed as well as the #else cases below.
4235 #define _USE_VISATTR 0
4237 void wxGrid::Create()
4239 // create the type registry
4240 m_typeRegistry
= new wxGridTypeRegistry
;
4242 m_cellEditCtrlEnabled
= false;
4244 m_defaultCellAttr
= new wxGridCellAttr();
4246 // Set default cell attributes
4247 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4248 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4249 m_defaultCellAttr
->SetFont(GetFont());
4250 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4251 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4252 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4255 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4256 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4258 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4259 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4262 m_defaultCellAttr
->SetTextColour(
4263 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4264 m_defaultCellAttr
->SetBackgroundColour(
4265 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4270 m_currentCellCoords
= wxGridNoCellCoords
;
4272 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4273 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4275 // subwindow components that make up the wxGrid
4276 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4281 m_colLabelWin
= new wxGridColLabelWindow( this,
4286 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4291 m_gridWin
= new wxGridWindow( this,
4298 SetTargetWindow( m_gridWin
);
4301 wxColour gfg
= gva
.colFg
;
4302 wxColour gbg
= gva
.colBg
;
4303 wxColour lfg
= lva
.colFg
;
4304 wxColour lbg
= lva
.colBg
;
4306 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4307 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4308 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4309 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4312 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4313 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4314 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4315 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4316 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4317 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4319 m_gridWin
->SetOwnForegroundColour(gfg
);
4320 m_gridWin
->SetOwnBackgroundColour(gbg
);
4325 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4326 wxGrid::wxGridSelectionModes selmode
)
4328 wxCHECK_MSG( !m_created
,
4330 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4332 m_numRows
= numRows
;
4333 m_numCols
= numCols
;
4335 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4336 m_table
->SetView( this );
4338 m_selection
= new wxGridSelection( this, selmode
);
4347 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4349 wxCHECK_RET( m_created
,
4350 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4352 m_selection
->SetSelectionMode( selmode
);
4355 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4357 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4358 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4360 return m_selection
->GetSelectionMode();
4363 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4364 wxGrid::wxGridSelectionModes selmode
)
4366 bool checkSelection
= false;
4369 // stop all processing
4374 m_table
->SetView(0);
4386 checkSelection
= true;
4388 // kill row and column size arrays
4389 m_colWidths
.Empty();
4390 m_colRights
.Empty();
4391 m_rowHeights
.Empty();
4392 m_rowBottoms
.Empty();
4397 m_numRows
= table
->GetNumberRows();
4398 m_numCols
= table
->GetNumberCols();
4401 m_table
->SetView( this );
4402 m_ownTable
= takeOwnership
;
4403 m_selection
= new wxGridSelection( this, selmode
);
4406 // If the newly set table is smaller than the
4407 // original one current cell and selection regions
4408 // might be invalid,
4409 m_selectingKeyboard
= wxGridNoCellCoords
;
4410 m_currentCellCoords
=
4411 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4412 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4413 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4414 m_selectingTopLeft
.GetCol() >= m_numCols
)
4416 m_selectingTopLeft
= wxGridNoCellCoords
;
4417 m_selectingBottomRight
= wxGridNoCellCoords
;
4420 m_selectingBottomRight
=
4421 wxGridCellCoords(wxMin(m_numRows
,
4422 m_selectingBottomRight
.GetRow()),
4424 m_selectingBottomRight
.GetCol()));
4434 void wxGrid::InitVars()
4438 m_cornerLabelWin
= NULL
;
4439 m_rowLabelWin
= NULL
;
4440 m_colLabelWin
= NULL
;
4447 m_defaultCellAttr
= NULL
;
4448 m_typeRegistry
= NULL
;
4449 m_winCapture
= NULL
;
4454 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4455 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4457 if ( m_rowLabelWin
)
4459 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4463 m_labelBackgroundColour
= *wxWHITE
;
4466 m_labelTextColour
= *wxBLACK
;
4469 m_attrCache
.row
= -1;
4470 m_attrCache
.col
= -1;
4471 m_attrCache
.attr
= NULL
;
4473 // TODO: something better than this ?
4475 m_labelFont
= this->GetFont();
4476 m_labelFont
.SetWeight( wxBOLD
);
4478 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4479 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4481 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4482 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4483 m_colLabelTextOrientation
= wxHORIZONTAL
;
4485 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4486 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4488 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4489 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4491 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4492 m_defaultRowHeight
+= 8;
4494 m_defaultRowHeight
+= 4;
4497 m_gridLineColour
= wxColour( 192,192,192 );
4498 m_gridLinesEnabled
= true;
4499 m_cellHighlightColour
= *wxBLACK
;
4500 m_cellHighlightPenWidth
= 2;
4501 m_cellHighlightROPenWidth
= 1;
4503 m_canDragColMove
= false;
4505 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4506 m_winCapture
= (wxWindow
*)NULL
;
4507 m_canDragRowSize
= true;
4508 m_canDragColSize
= true;
4509 m_canDragGridSize
= true;
4510 m_canDragCell
= false;
4512 m_dragRowOrCol
= -1;
4513 m_isDragging
= false;
4514 m_startDragPos
= wxDefaultPosition
;
4515 m_nativeColumnLabels
= false;
4517 m_waitForSlowClick
= false;
4519 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4520 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4522 m_currentCellCoords
= wxGridNoCellCoords
;
4526 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4527 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4529 m_editable
= true; // default for whole grid
4531 m_inOnKeyDown
= false;
4537 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4538 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4541 // ----------------------------------------------------------------------------
4542 // the idea is to call these functions only when necessary because they create
4543 // quite big arrays which eat memory mostly unnecessary - in particular, if
4544 // default widths/heights are used for all rows/columns, we may not use these
4547 // with some extra code, it should be possible to only store the widths/heights
4548 // different from default ones (resulting in space savings for huge grids) but
4549 // this is not done currently
4550 // ----------------------------------------------------------------------------
4552 void wxGrid::InitRowHeights()
4554 m_rowHeights
.Empty();
4555 m_rowBottoms
.Empty();
4557 m_rowHeights
.Alloc( m_numRows
);
4558 m_rowBottoms
.Alloc( m_numRows
);
4560 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4563 for ( int i
= 0; i
< m_numRows
; i
++ )
4565 rowBottom
+= m_defaultRowHeight
;
4566 m_rowBottoms
.Add( rowBottom
);
4570 void wxGrid::InitColWidths()
4572 m_colWidths
.Empty();
4573 m_colRights
.Empty();
4575 m_colWidths
.Alloc( m_numCols
);
4576 m_colRights
.Alloc( m_numCols
);
4578 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4581 for ( int i
= 0; i
< m_numCols
; i
++ )
4583 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4584 m_colRights
.Add( colRight
);
4588 int wxGrid::GetColWidth(int col
) const
4590 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4593 int wxGrid::GetColLeft(int col
) const
4595 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4596 : m_colRights
[col
] - m_colWidths
[col
];
4599 int wxGrid::GetColRight(int col
) const
4601 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4605 int wxGrid::GetRowHeight(int row
) const
4607 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4610 int wxGrid::GetRowTop(int row
) const
4612 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4613 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4616 int wxGrid::GetRowBottom(int row
) const
4618 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4619 : m_rowBottoms
[row
];
4622 void wxGrid::CalcDimensions()
4624 // compute the size of the scrollable area
4625 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4626 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4631 // take into account editor if shown
4632 if ( IsCellEditControlShown() )
4635 int r
= m_currentCellCoords
.GetRow();
4636 int c
= m_currentCellCoords
.GetCol();
4637 int x
= GetColLeft(c
);
4638 int y
= GetRowTop(r
);
4640 // how big is the editor
4641 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4642 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4643 editor
->GetControl()->GetSize(&w2
, &h2
);
4654 // preserve (more or less) the previous position
4656 GetViewStart( &x
, &y
);
4658 // ensure the position is valid for the new scroll ranges
4660 x
= wxMax( w
- 1, 0 );
4662 y
= wxMax( h
- 1, 0 );
4664 // do set scrollbar parameters
4665 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4666 GetScrollX(w
), GetScrollY(h
),
4668 GetBatchCount() != 0);
4670 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4671 // still must reposition the children
4675 void wxGrid::CalcWindowSizes()
4677 // escape if the window is has not been fully created yet
4679 if ( m_cornerLabelWin
== NULL
)
4683 GetClientSize( &cw
, &ch
);
4685 // this block of code tries to work around the following problem: the grid
4686 // could have been just resized to have enough space to show the full grid
4687 // window contents without the scrollbars, but its client size could be
4688 // not big enough because the grid has the scrollbars right now and so the
4689 // scrollbars would remain even though we don't need them any more
4691 // to prevent this from happening, check if we have enough space for
4692 // everything without the scrollbars and explicitly disable them then
4693 wxSize size
= GetSize() - GetWindowBorderSize();
4694 if ( size
!= wxSize(cw
, ch
) )
4696 // check if we have enough space for grid window after accounting for
4697 // the fixed size elements
4698 size
.x
-= m_rowLabelWidth
;
4699 size
.y
-= m_colLabelHeight
;
4701 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4703 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4705 // yes, we do, so remove the scrollbars and use the new client size
4706 // (which should be the same as full window size - borders now)
4707 SetScrollbars(0, 0, 0, 0);
4708 GetClientSize(&cw
, &ch
);
4712 // the grid may be too small to have enough space for the labels yet, don't
4713 // size the windows to negative sizes in this case
4714 int gw
= cw
- m_rowLabelWidth
;
4715 int gh
= ch
- m_colLabelHeight
;
4721 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4722 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4724 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4725 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
4727 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4728 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
4730 if ( m_gridWin
&& m_gridWin
->IsShown() )
4731 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
4734 // this is called when the grid table sends a message
4735 // to indicate that it has been redimensioned
4737 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4740 bool result
= false;
4742 // Clear the attribute cache as the attribute might refer to a different
4743 // cell than stored in the cache after adding/removing rows/columns.
4746 // By the same reasoning, the editor should be dismissed if columns are
4747 // added or removed. And for consistency, it should IMHO always be
4748 // removed, not only if the cell "underneath" it actually changes.
4749 // For now, I intentionally do not save the editor's content as the
4750 // cell it might want to save that stuff to might no longer exist.
4751 HideCellEditControl();
4754 // if we were using the default widths/heights so far, we must change them
4756 if ( m_colWidths
.IsEmpty() )
4761 if ( m_rowHeights
.IsEmpty() )
4767 switch ( msg
.GetId() )
4769 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4771 size_t pos
= msg
.GetCommandInt();
4772 int numRows
= msg
.GetCommandInt2();
4774 m_numRows
+= numRows
;
4776 if ( !m_rowHeights
.IsEmpty() )
4778 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4779 m_rowBottoms
.Insert( 0, pos
, numRows
);
4783 bottom
= m_rowBottoms
[pos
- 1];
4785 for ( i
= pos
; i
< m_numRows
; i
++ )
4787 bottom
+= m_rowHeights
[i
];
4788 m_rowBottoms
[i
] = bottom
;
4792 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4794 // if we have just inserted cols into an empty grid the current
4795 // cell will be undefined...
4797 SetCurrentCell( 0, 0 );
4801 m_selection
->UpdateRows( pos
, numRows
);
4802 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4804 attrProvider
->UpdateAttrRows( pos
, numRows
);
4806 if ( !GetBatchCount() )
4809 m_rowLabelWin
->Refresh();
4815 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4817 int numRows
= msg
.GetCommandInt();
4818 int oldNumRows
= m_numRows
;
4819 m_numRows
+= numRows
;
4821 if ( !m_rowHeights
.IsEmpty() )
4823 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4824 m_rowBottoms
.Add( 0, numRows
);
4827 if ( oldNumRows
> 0 )
4828 bottom
= m_rowBottoms
[oldNumRows
- 1];
4830 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4832 bottom
+= m_rowHeights
[i
];
4833 m_rowBottoms
[i
] = bottom
;
4837 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4839 // if we have just inserted cols into an empty grid the current
4840 // cell will be undefined...
4842 SetCurrentCell( 0, 0 );
4845 if ( !GetBatchCount() )
4848 m_rowLabelWin
->Refresh();
4854 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4856 size_t pos
= msg
.GetCommandInt();
4857 int numRows
= msg
.GetCommandInt2();
4858 m_numRows
-= numRows
;
4860 if ( !m_rowHeights
.IsEmpty() )
4862 m_rowHeights
.RemoveAt( pos
, numRows
);
4863 m_rowBottoms
.RemoveAt( pos
, numRows
);
4866 for ( i
= 0; i
< m_numRows
; i
++ )
4868 h
+= m_rowHeights
[i
];
4869 m_rowBottoms
[i
] = h
;
4875 m_currentCellCoords
= wxGridNoCellCoords
;
4879 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4880 m_currentCellCoords
.Set( 0, 0 );
4884 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4885 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4888 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4890 // ifdef'd out following patch from Paul Gammans
4892 // No need to touch column attributes, unless we
4893 // removed _all_ rows, in this case, we remove
4894 // all column attributes.
4895 // I hate to do this here, but the
4896 // needed data is not available inside UpdateAttrRows.
4897 if ( !GetNumberRows() )
4898 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4902 if ( !GetBatchCount() )
4905 m_rowLabelWin
->Refresh();
4911 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4913 size_t pos
= msg
.GetCommandInt();
4914 int numCols
= msg
.GetCommandInt2();
4915 m_numCols
+= numCols
;
4917 if ( !m_colAt
.IsEmpty() )
4919 //Shift the column IDs
4921 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4923 if ( m_colAt
[i
] >= (int)pos
)
4924 m_colAt
[i
] += numCols
;
4927 m_colAt
.Insert( pos
, pos
, numCols
);
4929 //Set the new columns' positions
4930 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4936 if ( !m_colWidths
.IsEmpty() )
4938 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4939 m_colRights
.Insert( 0, pos
, numCols
);
4943 right
= m_colRights
[GetColAt( pos
- 1 )];
4946 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4948 i
= GetColAt( colPos
);
4950 right
+= m_colWidths
[i
];
4951 m_colRights
[i
] = right
;
4955 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4957 // if we have just inserted cols into an empty grid the current
4958 // cell will be undefined...
4960 SetCurrentCell( 0, 0 );
4964 m_selection
->UpdateCols( pos
, numCols
);
4965 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4967 attrProvider
->UpdateAttrCols( pos
, numCols
);
4968 if ( !GetBatchCount() )
4971 m_colLabelWin
->Refresh();
4977 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4979 int numCols
= msg
.GetCommandInt();
4980 int oldNumCols
= m_numCols
;
4981 m_numCols
+= numCols
;
4983 if ( !m_colAt
.IsEmpty() )
4985 m_colAt
.Add( 0, numCols
);
4987 //Set the new columns' positions
4989 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4995 if ( !m_colWidths
.IsEmpty() )
4997 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4998 m_colRights
.Add( 0, numCols
);
5001 if ( oldNumCols
> 0 )
5002 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5005 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5007 i
= GetColAt( colPos
);
5009 right
+= m_colWidths
[i
];
5010 m_colRights
[i
] = right
;
5014 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5016 // if we have just inserted cols into an empty grid the current
5017 // cell will be undefined...
5019 SetCurrentCell( 0, 0 );
5021 if ( !GetBatchCount() )
5024 m_colLabelWin
->Refresh();
5030 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5032 size_t pos
= msg
.GetCommandInt();
5033 int numCols
= msg
.GetCommandInt2();
5034 m_numCols
-= numCols
;
5036 if ( !m_colAt
.IsEmpty() )
5038 int colID
= GetColAt( pos
);
5040 m_colAt
.RemoveAt( pos
, numCols
);
5042 //Shift the column IDs
5044 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5046 if ( m_colAt
[colPos
] > colID
)
5047 m_colAt
[colPos
] -= numCols
;
5051 if ( !m_colWidths
.IsEmpty() )
5053 m_colWidths
.RemoveAt( pos
, numCols
);
5054 m_colRights
.RemoveAt( pos
, numCols
);
5058 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5060 i
= GetColAt( colPos
);
5062 w
+= m_colWidths
[i
];
5069 m_currentCellCoords
= wxGridNoCellCoords
;
5073 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5074 m_currentCellCoords
.Set( 0, 0 );
5078 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5079 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5082 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5084 // ifdef'd out following patch from Paul Gammans
5086 // No need to touch row attributes, unless we
5087 // removed _all_ columns, in this case, we remove
5088 // all row attributes.
5089 // I hate to do this here, but the
5090 // needed data is not available inside UpdateAttrCols.
5091 if ( !GetNumberCols() )
5092 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5096 if ( !GetBatchCount() )
5099 m_colLabelWin
->Refresh();
5106 if (result
&& !GetBatchCount() )
5107 m_gridWin
->Refresh();
5112 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5114 wxRegionIterator
iter( reg
);
5117 wxArrayInt rowlabels
;
5124 // TODO: remove this when we can...
5125 // There is a bug in wxMotif that gives garbage update
5126 // rectangles if you jump-scroll a long way by clicking the
5127 // scrollbar with middle button. This is a work-around
5129 #if defined(__WXMOTIF__)
5131 m_gridWin
->GetClientSize( &cw
, &ch
);
5132 if ( r
.GetTop() > ch
)
5134 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5137 // logical bounds of update region
5140 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5141 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5143 // find the row labels within these bounds
5146 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5148 if ( GetRowBottom(row
) < top
)
5151 if ( GetRowTop(row
) > bottom
)
5154 rowlabels
.Add( row
);
5163 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5165 wxRegionIterator
iter( reg
);
5168 wxArrayInt colLabels
;
5175 // TODO: remove this when we can...
5176 // There is a bug in wxMotif that gives garbage update
5177 // rectangles if you jump-scroll a long way by clicking the
5178 // scrollbar with middle button. This is a work-around
5180 #if defined(__WXMOTIF__)
5182 m_gridWin
->GetClientSize( &cw
, &ch
);
5183 if ( r
.GetLeft() > cw
)
5185 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5188 // logical bounds of update region
5191 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5192 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5194 // find the cells within these bounds
5198 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5200 col
= GetColAt( colPos
);
5202 if ( GetColRight(col
) < left
)
5205 if ( GetColLeft(col
) > right
)
5208 colLabels
.Add( col
);
5217 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5219 wxRegionIterator
iter( reg
);
5222 wxGridCellCoordsArray cellsExposed
;
5224 int left
, top
, right
, bottom
;
5229 // TODO: remove this when we can...
5230 // There is a bug in wxMotif that gives garbage update
5231 // rectangles if you jump-scroll a long way by clicking the
5232 // scrollbar with middle button. This is a work-around
5234 #if defined(__WXMOTIF__)
5236 m_gridWin
->GetClientSize( &cw
, &ch
);
5237 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5238 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5239 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5240 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5243 // logical bounds of update region
5245 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5246 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5248 // find the cells within these bounds
5251 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5253 if ( GetRowBottom(row
) <= top
)
5256 if ( GetRowTop(row
) > bottom
)
5260 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5262 col
= GetColAt( colPos
);
5264 if ( GetColRight(col
) <= left
)
5267 if ( GetColLeft(col
) > right
)
5270 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5277 return cellsExposed
;
5281 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5284 wxPoint
pos( event
.GetPosition() );
5285 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5287 if ( event
.Dragging() )
5291 m_isDragging
= true;
5292 m_rowLabelWin
->CaptureMouse();
5295 if ( event
.LeftIsDown() )
5297 switch ( m_cursorMode
)
5299 case WXGRID_CURSOR_RESIZE_ROW
:
5301 int cw
, ch
, left
, dummy
;
5302 m_gridWin
->GetClientSize( &cw
, &ch
);
5303 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5305 wxClientDC
dc( m_gridWin
);
5308 GetRowTop(m_dragRowOrCol
) +
5309 GetRowMinimalHeight(m_dragRowOrCol
) );
5310 dc
.SetLogicalFunction(wxINVERT
);
5311 if ( m_dragLastPos
>= 0 )
5313 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5315 dc
.DrawLine( left
, y
, left
+cw
, y
);
5320 case WXGRID_CURSOR_SELECT_ROW
:
5322 if ( (row
= YToRow( y
)) >= 0 )
5326 m_selection
->SelectRow( row
,
5327 event
.ControlDown(),
5336 // default label to suppress warnings about "enumeration value
5337 // 'xxx' not handled in switch
5345 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5350 if (m_rowLabelWin
->HasCapture())
5351 m_rowLabelWin
->ReleaseMouse();
5352 m_isDragging
= false;
5355 // ------------ Entering or leaving the window
5357 if ( event
.Entering() || event
.Leaving() )
5359 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5362 // ------------ Left button pressed
5364 else if ( event
.LeftDown() )
5366 // don't send a label click event for a hit on the
5367 // edge of the row label - this is probably the user
5368 // wanting to resize the row
5370 if ( YToEdgeOfRow(y
) < 0 )
5374 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5376 if ( !event
.ShiftDown() && !event
.CmdDown() )
5380 if ( event
.ShiftDown() )
5382 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5385 GetNumberCols() - 1,
5386 event
.ControlDown(),
5393 m_selection
->SelectRow( row
,
5394 event
.ControlDown(),
5401 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5406 // starting to drag-resize a row
5407 if ( CanDragRowSize() )
5408 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5412 // ------------ Left double click
5414 else if (event
.LeftDClick() )
5416 row
= YToEdgeOfRow(y
);
5421 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5423 // no default action at the moment
5428 // adjust row height depending on label text
5429 AutoSizeRowLabelSize( row
);
5431 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5436 // ------------ Left button released
5438 else if ( event
.LeftUp() )
5440 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5442 DoEndDragResizeRow();
5444 // Note: we are ending the event *after* doing
5445 // default processing in this case
5447 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5450 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5454 // ------------ Right button down
5456 else if ( event
.RightDown() )
5460 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5462 // no default action at the moment
5466 // ------------ Right double click
5468 else if ( event
.RightDClick() )
5472 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5474 // no default action at the moment
5478 // ------------ No buttons down and mouse moving
5480 else if ( event
.Moving() )
5482 m_dragRowOrCol
= YToEdgeOfRow( y
);
5483 if ( m_dragRowOrCol
>= 0 )
5485 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5487 // don't capture the mouse yet
5488 if ( CanDragRowSize() )
5489 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5492 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5494 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5499 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5502 wxPoint
pos( event
.GetPosition() );
5503 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5505 if ( event
.Dragging() )
5509 m_isDragging
= true;
5510 m_colLabelWin
->CaptureMouse();
5512 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5513 m_dragRowOrCol
= XToCol( x
);
5516 if ( event
.LeftIsDown() )
5518 switch ( m_cursorMode
)
5520 case WXGRID_CURSOR_RESIZE_COL
:
5522 int cw
, ch
, dummy
, top
;
5523 m_gridWin
->GetClientSize( &cw
, &ch
);
5524 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5526 wxClientDC
dc( m_gridWin
);
5529 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5530 GetColMinimalWidth(m_dragRowOrCol
));
5531 dc
.SetLogicalFunction(wxINVERT
);
5532 if ( m_dragLastPos
>= 0 )
5534 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5536 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5541 case WXGRID_CURSOR_SELECT_COL
:
5543 if ( (col
= XToCol( x
)) >= 0 )
5547 m_selection
->SelectCol( col
,
5548 event
.ControlDown(),
5557 case WXGRID_CURSOR_MOVE_COL
:
5560 m_moveToCol
= GetColAt( 0 );
5562 m_moveToCol
= XToCol( x
);
5566 if ( m_moveToCol
< 0 )
5567 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5569 markerX
= GetColLeft( m_moveToCol
);
5571 if ( markerX
!= m_dragLastPos
)
5573 wxClientDC
dc( m_colLabelWin
);
5576 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5580 //Clean up the last indicator
5581 if ( m_dragLastPos
>= 0 )
5583 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5585 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5586 dc
.SetPen(wxNullPen
);
5588 if ( XToCol( m_dragLastPos
) != -1 )
5589 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5592 //Moving to the same place? Don't draw a marker
5593 if ( (m_moveToCol
== m_dragRowOrCol
)
5594 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5595 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5602 wxPen
pen( *wxBLUE
, 2 );
5605 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5607 dc
.SetPen(wxNullPen
);
5609 m_dragLastPos
= markerX
- 1;
5614 // default label to suppress warnings about "enumeration value
5615 // 'xxx' not handled in switch
5623 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5628 if (m_colLabelWin
->HasCapture())
5629 m_colLabelWin
->ReleaseMouse();
5630 m_isDragging
= false;
5633 // ------------ Entering or leaving the window
5635 if ( event
.Entering() || event
.Leaving() )
5637 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5640 // ------------ Left button pressed
5642 else if ( event
.LeftDown() )
5644 // don't send a label click event for a hit on the
5645 // edge of the col label - this is probably the user
5646 // wanting to resize the col
5648 if ( XToEdgeOfCol(x
) < 0 )
5652 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5654 if ( m_canDragColMove
)
5656 //Show button as pressed
5657 wxClientDC
dc( m_colLabelWin
);
5658 int colLeft
= GetColLeft( col
);
5659 int colRight
= GetColRight( col
) - 1;
5660 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5661 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5662 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5664 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5668 if ( !event
.ShiftDown() && !event
.CmdDown() )
5672 if ( event
.ShiftDown() )
5674 m_selection
->SelectBlock( 0,
5675 m_currentCellCoords
.GetCol(),
5676 GetNumberRows() - 1, col
,
5677 event
.ControlDown(),
5684 m_selection
->SelectCol( col
,
5685 event
.ControlDown(),
5692 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5698 // starting to drag-resize a col
5700 if ( CanDragColSize() )
5701 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5705 // ------------ Left double click
5707 if ( event
.LeftDClick() )
5709 col
= XToEdgeOfCol(x
);
5714 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5716 // no default action at the moment
5721 // adjust column width depending on label text
5722 AutoSizeColLabelSize( col
);
5724 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5729 // ------------ Left button released
5731 else if ( event
.LeftUp() )
5733 switch ( m_cursorMode
)
5735 case WXGRID_CURSOR_RESIZE_COL
:
5736 DoEndDragResizeCol();
5738 // Note: we are ending the event *after* doing
5739 // default processing in this case
5741 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5744 case WXGRID_CURSOR_MOVE_COL
:
5747 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5750 case WXGRID_CURSOR_SELECT_COL
:
5751 case WXGRID_CURSOR_SELECT_CELL
:
5752 case WXGRID_CURSOR_RESIZE_ROW
:
5753 case WXGRID_CURSOR_SELECT_ROW
:
5754 // nothing to do (?)
5758 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5762 // ------------ Right button down
5764 else if ( event
.RightDown() )
5768 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5770 // no default action at the moment
5774 // ------------ Right double click
5776 else if ( event
.RightDClick() )
5780 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5782 // no default action at the moment
5786 // ------------ No buttons down and mouse moving
5788 else if ( event
.Moving() )
5790 m_dragRowOrCol
= XToEdgeOfCol( x
);
5791 if ( m_dragRowOrCol
>= 0 )
5793 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5795 // don't capture the cursor yet
5796 if ( CanDragColSize() )
5797 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5800 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5802 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5807 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5809 if ( event
.LeftDown() )
5811 // indicate corner label by having both row and
5814 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5819 else if ( event
.LeftDClick() )
5821 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5823 else if ( event
.RightDown() )
5825 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5827 // no default action at the moment
5830 else if ( event
.RightDClick() )
5832 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5834 // no default action at the moment
5839 void wxGrid::CancelMouseCapture()
5841 // cancel operation currently in progress, whatever it is
5844 m_isDragging
= false;
5845 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5846 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5847 m_winCapture
= NULL
;
5849 // remove traces of whatever we drew on screen
5854 void wxGrid::ChangeCursorMode(CursorMode mode
,
5859 static const wxChar
*cursorModes
[] =
5869 wxLogTrace(_T("grid"),
5870 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5871 win
== m_colLabelWin
? _T("colLabelWin")
5872 : win
? _T("rowLabelWin")
5874 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5877 if ( mode
== m_cursorMode
&&
5878 win
== m_winCapture
&&
5879 captureMouse
== (m_winCapture
!= NULL
))
5884 // by default use the grid itself
5890 if (m_winCapture
->HasCapture())
5891 m_winCapture
->ReleaseMouse();
5892 m_winCapture
= (wxWindow
*)NULL
;
5895 m_cursorMode
= mode
;
5897 switch ( m_cursorMode
)
5899 case WXGRID_CURSOR_RESIZE_ROW
:
5900 win
->SetCursor( m_rowResizeCursor
);
5903 case WXGRID_CURSOR_RESIZE_COL
:
5904 win
->SetCursor( m_colResizeCursor
);
5907 case WXGRID_CURSOR_MOVE_COL
:
5908 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5912 win
->SetCursor( *wxSTANDARD_CURSOR
);
5916 // we need to capture mouse when resizing
5917 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5918 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5920 if ( captureMouse
&& resize
)
5922 win
->CaptureMouse();
5927 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5930 wxPoint
pos( event
.GetPosition() );
5931 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5933 wxGridCellCoords coords
;
5934 XYToCell( x
, y
, coords
);
5936 int cell_rows
, cell_cols
;
5937 bool isFirstDrag
= !m_isDragging
;
5938 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5939 if ((cell_rows
< 0) || (cell_cols
< 0))
5941 coords
.SetRow(coords
.GetRow() + cell_rows
);
5942 coords
.SetCol(coords
.GetCol() + cell_cols
);
5945 if ( event
.Dragging() )
5947 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5949 // Don't start doing anything until the mouse has been dragged at
5950 // least 3 pixels in any direction...
5953 if (m_startDragPos
== wxDefaultPosition
)
5955 m_startDragPos
= pos
;
5958 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5962 m_isDragging
= true;
5963 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5965 // Hide the edit control, so it
5966 // won't interfere with drag-shrinking.
5967 if ( IsCellEditControlShown() )
5969 HideCellEditControl();
5970 SaveEditControlValue();
5973 if ( coords
!= wxGridNoCellCoords
)
5975 if ( event
.CmdDown() )
5977 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5978 m_selectingKeyboard
= coords
;
5979 HighlightBlock( m_selectingKeyboard
, coords
);
5981 else if ( CanDragCell() )
5985 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5986 m_selectingKeyboard
= coords
;
5988 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5997 if ( !IsSelection() )
5999 HighlightBlock( coords
, coords
);
6003 HighlightBlock( m_currentCellCoords
, coords
);
6007 if (! IsVisible(coords
))
6009 MakeCellVisible(coords
);
6010 // TODO: need to introduce a delay or something here. The
6011 // scrolling is way to fast, at least on MSW - also on GTK.
6014 // Have we captured the mouse yet?
6017 m_winCapture
= m_gridWin
;
6018 m_winCapture
->CaptureMouse();
6023 else if ( event
.LeftIsDown() &&
6024 m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6026 int cw
, ch
, left
, dummy
;
6027 m_gridWin
->GetClientSize( &cw
, &ch
);
6028 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6030 wxClientDC
dc( m_gridWin
);
6032 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6033 GetRowMinimalHeight(m_dragRowOrCol
) );
6034 dc
.SetLogicalFunction(wxINVERT
);
6035 if ( m_dragLastPos
>= 0 )
6037 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6039 dc
.DrawLine( left
, y
, left
+cw
, y
);
6042 else if ( event
.LeftIsDown() &&
6043 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6045 int cw
, ch
, dummy
, top
;
6046 m_gridWin
->GetClientSize( &cw
, &ch
);
6047 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6049 wxClientDC
dc( m_gridWin
);
6051 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6052 GetColMinimalWidth(m_dragRowOrCol
) );
6053 dc
.SetLogicalFunction(wxINVERT
);
6054 if ( m_dragLastPos
>= 0 )
6056 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6058 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6065 m_isDragging
= false;
6066 m_startDragPos
= wxDefaultPosition
;
6068 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6069 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6072 if ( event
.Entering() || event
.Leaving() )
6074 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6075 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6080 // ------------ Left button pressed
6082 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6084 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6089 if ( !event
.CmdDown() )
6091 if ( event
.ShiftDown() )
6095 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6096 m_currentCellCoords
.GetCol(),
6099 event
.ControlDown(),
6105 else if ( XToEdgeOfCol(x
) < 0 &&
6106 YToEdgeOfRow(y
) < 0 )
6108 DisableCellEditControl();
6109 MakeCellVisible( coords
);
6111 if ( event
.CmdDown() )
6115 m_selection
->ToggleCellSelection( coords
.GetRow(),
6117 event
.ControlDown(),
6122 m_selectingTopLeft
= wxGridNoCellCoords
;
6123 m_selectingBottomRight
= wxGridNoCellCoords
;
6124 m_selectingKeyboard
= coords
;
6128 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6129 SetCurrentCell( coords
);
6132 if ( m_selection
->GetSelectionMode() !=
6133 wxGrid::wxGridSelectCells
)
6135 HighlightBlock( coords
, coords
);
6143 // ------------ Left double click
6145 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6147 DisableCellEditControl();
6149 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6151 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6156 // we want double click to select a cell and start editing
6157 // (i.e. to behave in same way as sequence of two slow clicks):
6158 m_waitForSlowClick
= true;
6163 // ------------ Left button released
6165 else if ( event
.LeftUp() )
6167 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6171 if (m_winCapture
->HasCapture())
6172 m_winCapture
->ReleaseMouse();
6173 m_winCapture
= NULL
;
6176 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6179 EnableCellEditControl();
6181 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6182 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6183 editor
->StartingClick();
6187 m_waitForSlowClick
= false;
6189 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6190 m_selectingBottomRight
!= wxGridNoCellCoords
)
6194 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6195 m_selectingTopLeft
.GetCol(),
6196 m_selectingBottomRight
.GetRow(),
6197 m_selectingBottomRight
.GetCol(),
6198 event
.ControlDown(),
6204 m_selectingTopLeft
= wxGridNoCellCoords
;
6205 m_selectingBottomRight
= wxGridNoCellCoords
;
6207 // Show the edit control, if it has been hidden for
6209 ShowCellEditControl();
6212 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6214 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6215 DoEndDragResizeRow();
6217 // Note: we are ending the event *after* doing
6218 // default processing in this case
6220 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6222 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6224 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6225 DoEndDragResizeCol();
6227 // Note: we are ending the event *after* doing
6228 // default processing in this case
6230 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6236 // ------------ Right button down
6238 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6240 DisableCellEditControl();
6241 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6246 // no default action at the moment
6250 // ------------ Right double click
6252 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6254 DisableCellEditControl();
6255 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6260 // no default action at the moment
6264 // ------------ Moving and no button action
6266 else if ( event
.Moving() && !event
.IsButton() )
6268 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6270 // out of grid cell area
6271 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6275 int dragRow
= YToEdgeOfRow( y
);
6276 int dragCol
= XToEdgeOfCol( x
);
6278 // Dragging on the corner of a cell to resize in both
6279 // directions is not implemented yet...
6281 if ( dragRow
>= 0 && dragCol
>= 0 )
6283 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6289 m_dragRowOrCol
= dragRow
;
6291 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6293 if ( CanDragRowSize() && CanDragGridSize() )
6294 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6297 else if ( dragCol
>= 0 )
6299 m_dragRowOrCol
= dragCol
;
6301 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6303 if ( CanDragColSize() && CanDragGridSize() )
6304 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6307 else // Neither on a row or col edge
6309 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6311 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6317 void wxGrid::DoEndDragResizeRow()
6319 if ( m_dragLastPos
>= 0 )
6321 // erase the last line and resize the row
6323 int cw
, ch
, left
, dummy
;
6324 m_gridWin
->GetClientSize( &cw
, &ch
);
6325 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6327 wxClientDC
dc( m_gridWin
);
6329 dc
.SetLogicalFunction( wxINVERT
);
6330 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6331 HideCellEditControl();
6332 SaveEditControlValue();
6334 int rowTop
= GetRowTop(m_dragRowOrCol
);
6335 SetRowSize( m_dragRowOrCol
,
6336 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6338 if ( !GetBatchCount() )
6340 // Only needed to get the correct rect.y:
6341 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6343 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6344 rect
.width
= m_rowLabelWidth
;
6345 rect
.height
= ch
- rect
.y
;
6346 m_rowLabelWin
->Refresh( true, &rect
);
6349 // if there is a multicell block, paint all of it
6352 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6353 int leftCol
= XToCol(left
);
6354 int rightCol
= internalXToCol(left
+ cw
);
6357 for (i
=leftCol
; i
<rightCol
; i
++)
6359 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6360 if (cell_rows
< subtract_rows
)
6361 subtract_rows
= cell_rows
;
6363 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6364 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6365 rect
.height
= ch
- rect
.y
;
6368 m_gridWin
->Refresh( false, &rect
);
6371 ShowCellEditControl();
6376 void wxGrid::DoEndDragResizeCol()
6378 if ( m_dragLastPos
>= 0 )
6380 // erase the last line and resize the col
6382 int cw
, ch
, dummy
, top
;
6383 m_gridWin
->GetClientSize( &cw
, &ch
);
6384 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6386 wxClientDC
dc( m_gridWin
);
6388 dc
.SetLogicalFunction( wxINVERT
);
6389 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6390 HideCellEditControl();
6391 SaveEditControlValue();
6393 int colLeft
= GetColLeft(m_dragRowOrCol
);
6394 SetColSize( m_dragRowOrCol
,
6395 wxMax( m_dragLastPos
- colLeft
,
6396 GetColMinimalWidth(m_dragRowOrCol
) ) );
6398 if ( !GetBatchCount() )
6400 // Only needed to get the correct rect.x:
6401 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6403 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6404 rect
.width
= cw
- rect
.x
;
6405 rect
.height
= m_colLabelHeight
;
6406 m_colLabelWin
->Refresh( true, &rect
);
6409 // if there is a multicell block, paint all of it
6412 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6413 int topRow
= YToRow(top
);
6414 int bottomRow
= internalYToRow(top
+ cw
);
6417 for (i
=topRow
; i
<bottomRow
; i
++)
6419 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6420 if (cell_cols
< subtract_cols
)
6421 subtract_cols
= cell_cols
;
6424 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6425 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6426 rect
.width
= cw
- rect
.x
;
6430 m_gridWin
->Refresh( false, &rect
);
6433 ShowCellEditControl();
6437 void wxGrid::DoEndDragMoveCol()
6439 //The user clicked on the column but didn't actually drag
6440 if ( m_dragLastPos
< 0 )
6442 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6447 if ( m_moveToCol
== -1 )
6448 newPos
= m_numCols
- 1;
6451 newPos
= GetColPos( m_moveToCol
);
6452 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6456 SetColPos( m_dragRowOrCol
, newPos
);
6459 void wxGrid::SetColPos( int colID
, int newPos
)
6461 if ( m_colAt
.IsEmpty() )
6463 m_colAt
.Alloc( m_numCols
);
6466 for ( i
= 0; i
< m_numCols
; i
++ )
6472 int oldPos
= GetColPos( colID
);
6474 //Reshuffle the m_colAt array
6475 if ( newPos
> oldPos
)
6478 for ( i
= oldPos
; i
< newPos
; i
++ )
6480 m_colAt
[i
] = m_colAt
[i
+1];
6486 for ( i
= oldPos
; i
> newPos
; i
-- )
6488 m_colAt
[i
] = m_colAt
[i
-1];
6492 m_colAt
[newPos
] = colID
;
6494 //Recalculate the column rights
6495 if ( !m_colWidths
.IsEmpty() )
6499 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6501 int colID
= GetColAt( colPos
);
6503 colRight
+= m_colWidths
[colID
];
6504 m_colRights
[colID
] = colRight
;
6508 m_colLabelWin
->Refresh();
6509 m_gridWin
->Refresh();
6514 void wxGrid::EnableDragColMove( bool enable
)
6516 if ( m_canDragColMove
== enable
)
6519 m_canDragColMove
= enable
;
6521 if ( !m_canDragColMove
)
6525 //Recalculate the column rights
6526 if ( !m_colWidths
.IsEmpty() )
6530 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6532 colRight
+= m_colWidths
[colPos
];
6533 m_colRights
[colPos
] = colRight
;
6537 m_colLabelWin
->Refresh();
6538 m_gridWin
->Refresh();
6544 // ------ interaction with data model
6546 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6548 switch ( msg
.GetId() )
6550 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6551 return GetModelValues();
6553 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6554 return SetModelValues();
6556 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6557 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6558 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6559 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6560 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6561 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6562 return Redimension( msg
);
6569 // The behaviour of this function depends on the grid table class
6570 // Clear() function. For the default wxGridStringTable class the
6571 // behavious is to replace all cell contents with wxEmptyString but
6572 // not to change the number of rows or cols.
6574 void wxGrid::ClearGrid()
6578 if (IsCellEditControlEnabled())
6579 DisableCellEditControl();
6582 if (!GetBatchCount())
6583 m_gridWin
->Refresh();
6587 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6589 // TODO: something with updateLabels flag
6593 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6599 if (IsCellEditControlEnabled())
6600 DisableCellEditControl();
6602 bool done
= m_table
->InsertRows( pos
, numRows
);
6605 // the table will have sent the results of the insert row
6606 // operation to this view object as a grid table message
6612 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6614 // TODO: something with updateLabels flag
6618 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6624 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6627 // the table will have sent the results of the append row
6628 // operation to this view object as a grid table message
6634 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6636 // TODO: something with updateLabels flag
6640 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6646 if (IsCellEditControlEnabled())
6647 DisableCellEditControl();
6649 bool done
= m_table
->DeleteRows( pos
, numRows
);
6651 // the table will have sent the results of the delete row
6652 // operation to this view object as a grid table message
6658 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6660 // TODO: something with updateLabels flag
6664 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6670 if (IsCellEditControlEnabled())
6671 DisableCellEditControl();
6673 bool done
= m_table
->InsertCols( pos
, numCols
);
6675 // the table will have sent the results of the insert col
6676 // operation to this view object as a grid table message
6682 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6684 // TODO: something with updateLabels flag
6688 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6694 bool done
= m_table
->AppendCols( numCols
);
6696 // the table will have sent the results of the append col
6697 // operation to this view object as a grid table message
6703 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6705 // TODO: something with updateLabels flag
6709 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6715 if (IsCellEditControlEnabled())
6716 DisableCellEditControl();
6718 bool done
= m_table
->DeleteCols( pos
, numCols
);
6720 // the table will have sent the results of the delete col
6721 // operation to this view object as a grid table message
6728 // ----- event handlers
6731 // Generate a grid event based on a mouse event and
6732 // return the result of ProcessEvent()
6734 int wxGrid::SendEvent( const wxEventType type
,
6736 wxMouseEvent
& mouseEv
)
6738 bool claimed
, vetoed
;
6740 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6742 int rowOrCol
= (row
== -1 ? col
: row
);
6744 wxGridSizeEvent
gridEvt( GetId(),
6748 mouseEv
.GetX() + GetRowLabelSize(),
6749 mouseEv
.GetY() + GetColLabelSize(),
6750 mouseEv
.ControlDown(),
6751 mouseEv
.ShiftDown(),
6753 mouseEv
.MetaDown() );
6755 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6756 vetoed
= !gridEvt
.IsAllowed();
6758 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6760 // Right now, it should _never_ end up here!
6761 wxGridRangeSelectEvent
gridEvt( GetId(),
6765 m_selectingBottomRight
,
6767 mouseEv
.ControlDown(),
6768 mouseEv
.ShiftDown(),
6770 mouseEv
.MetaDown() );
6772 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6773 vetoed
= !gridEvt
.IsAllowed();
6775 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6776 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6777 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6778 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6780 wxPoint pos
= mouseEv
.GetPosition();
6782 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6783 pos
.y
+= GetColLabelSize();
6784 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6785 pos
.x
+= GetRowLabelSize();
6787 wxGridEvent
gridEvt( GetId(),
6794 mouseEv
.ControlDown(),
6795 mouseEv
.ShiftDown(),
6797 mouseEv
.MetaDown() );
6798 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6799 vetoed
= !gridEvt
.IsAllowed();
6803 wxGridEvent
gridEvt( GetId(),
6807 mouseEv
.GetX() + GetRowLabelSize(),
6808 mouseEv
.GetY() + GetColLabelSize(),
6810 mouseEv
.ControlDown(),
6811 mouseEv
.ShiftDown(),
6813 mouseEv
.MetaDown() );
6814 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6815 vetoed
= !gridEvt
.IsAllowed();
6818 // A Veto'd event may not be `claimed' so test this first
6822 return claimed
? 1 : 0;
6825 // Generate a grid event of specified type and return the result
6826 // of ProcessEvent().
6828 int wxGrid::SendEvent( const wxEventType type
,
6831 bool claimed
, vetoed
;
6833 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6835 int rowOrCol
= (row
== -1 ? col
: row
);
6837 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6839 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6840 vetoed
= !gridEvt
.IsAllowed();
6844 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6846 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6847 vetoed
= !gridEvt
.IsAllowed();
6850 // A Veto'd event may not be `claimed' so test this first
6854 return claimed
? 1 : 0;
6857 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6859 // needed to prevent zillions of paint events on MSW
6863 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6865 // Don't do anything if between Begin/EndBatch...
6866 // EndBatch() will do all this on the last nested one anyway.
6867 if ( m_created
&& !GetBatchCount() )
6869 // Refresh to get correct scrolled position:
6870 wxScrolledWindow::Refresh(eraseb
, rect
);
6874 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6875 int width_label
, width_cell
, height_label
, height_cell
;
6878 // Copy rectangle can get scroll offsets..
6879 rect_x
= rect
->GetX();
6880 rect_y
= rect
->GetY();
6881 rectWidth
= rect
->GetWidth();
6882 rectHeight
= rect
->GetHeight();
6884 width_label
= m_rowLabelWidth
- rect_x
;
6885 if (width_label
> rectWidth
)
6886 width_label
= rectWidth
;
6888 height_label
= m_colLabelHeight
- rect_y
;
6889 if (height_label
> rectHeight
)
6890 height_label
= rectHeight
;
6892 if (rect_x
> m_rowLabelWidth
)
6894 x
= rect_x
- m_rowLabelWidth
;
6895 width_cell
= rectWidth
;
6900 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6903 if (rect_y
> m_colLabelHeight
)
6905 y
= rect_y
- m_colLabelHeight
;
6906 height_cell
= rectHeight
;
6911 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6914 // Paint corner label part intersecting rect.
6915 if ( width_label
> 0 && height_label
> 0 )
6917 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6918 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6921 // Paint col labels part intersecting rect.
6922 if ( width_cell
> 0 && height_label
> 0 )
6924 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6925 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6928 // Paint row labels part intersecting rect.
6929 if ( width_label
> 0 && height_cell
> 0 )
6931 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6932 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6935 // Paint cell area part intersecting rect.
6936 if ( width_cell
> 0 && height_cell
> 0 )
6938 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6939 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6944 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6945 m_colLabelWin
->Refresh(eraseb
, NULL
);
6946 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6947 m_gridWin
->Refresh(eraseb
, NULL
);
6952 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6954 if (m_targetWindow
!= this) // check whether initialisation has been done
6956 // update our children window positions and scrollbars
6961 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6963 if ( m_inOnKeyDown
)
6965 // shouldn't be here - we are going round in circles...
6967 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6970 m_inOnKeyDown
= true;
6972 // propagate the event up and see if it gets processed
6973 wxWindow
*parent
= GetParent();
6974 wxKeyEvent
keyEvt( event
);
6975 keyEvt
.SetEventObject( parent
);
6977 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6979 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6981 if (event
.GetKeyCode() == WXK_RIGHT
)
6982 event
.m_keyCode
= WXK_LEFT
;
6983 else if (event
.GetKeyCode() == WXK_LEFT
)
6984 event
.m_keyCode
= WXK_RIGHT
;
6987 // try local handlers
6988 switch ( event
.GetKeyCode() )
6991 if ( event
.ControlDown() )
6992 MoveCursorUpBlock( event
.ShiftDown() );
6994 MoveCursorUp( event
.ShiftDown() );
6998 if ( event
.ControlDown() )
6999 MoveCursorDownBlock( event
.ShiftDown() );
7001 MoveCursorDown( event
.ShiftDown() );
7005 if ( event
.ControlDown() )
7006 MoveCursorLeftBlock( event
.ShiftDown() );
7008 MoveCursorLeft( event
.ShiftDown() );
7012 if ( event
.ControlDown() )
7013 MoveCursorRightBlock( event
.ShiftDown() );
7015 MoveCursorRight( event
.ShiftDown() );
7019 case WXK_NUMPAD_ENTER
:
7020 if ( event
.ControlDown() )
7022 event
.Skip(); // to let the edit control have the return
7026 if ( GetGridCursorRow() < GetNumberRows()-1 )
7028 MoveCursorDown( event
.ShiftDown() );
7032 // at the bottom of a column
7033 DisableCellEditControl();
7043 if (event
.ShiftDown())
7045 if ( GetGridCursorCol() > 0 )
7047 MoveCursorLeft( false );
7052 DisableCellEditControl();
7057 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7059 MoveCursorRight( false );
7064 DisableCellEditControl();
7070 if ( event
.ControlDown() )
7072 MakeCellVisible( 0, 0 );
7073 SetCurrentCell( 0, 0 );
7082 if ( event
.ControlDown() )
7084 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7085 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7102 if ( event
.ControlDown() )
7106 m_selection
->ToggleCellSelection(
7107 m_currentCellCoords
.GetRow(),
7108 m_currentCellCoords
.GetCol(),
7109 event
.ControlDown(),
7117 if ( !IsEditable() )
7118 MoveCursorRight( false );
7129 m_inOnKeyDown
= false;
7132 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7134 // try local handlers
7136 if ( event
.GetKeyCode() == WXK_SHIFT
)
7138 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7139 m_selectingBottomRight
!= wxGridNoCellCoords
)
7143 m_selection
->SelectBlock(
7144 m_selectingTopLeft
.GetRow(),
7145 m_selectingTopLeft
.GetCol(),
7146 m_selectingBottomRight
.GetRow(),
7147 m_selectingBottomRight
.GetCol(),
7148 event
.ControlDown(),
7155 m_selectingTopLeft
= wxGridNoCellCoords
;
7156 m_selectingBottomRight
= wxGridNoCellCoords
;
7157 m_selectingKeyboard
= wxGridNoCellCoords
;
7161 void wxGrid::OnChar( wxKeyEvent
& event
)
7163 // is it possible to edit the current cell at all?
7164 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7166 // yes, now check whether the cells editor accepts the key
7167 int row
= m_currentCellCoords
.GetRow();
7168 int col
= m_currentCellCoords
.GetCol();
7169 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7170 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7172 // <F2> is special and will always start editing, for
7173 // other keys - ask the editor itself
7174 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7175 || editor
->IsAcceptedKey(event
) )
7177 // ensure cell is visble
7178 MakeCellVisible(row
, col
);
7179 EnableCellEditControl();
7181 // a problem can arise if the cell is not completely
7182 // visible (even after calling MakeCellVisible the
7183 // control is not created and calling StartingKey will
7185 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7186 editor
->StartingKey(event
);
7202 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7206 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7208 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7210 // the event has been intercepted - do nothing
7214 #if !defined(__WXMAC__)
7215 wxClientDC
dc( m_gridWin
);
7219 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7221 DisableCellEditControl();
7223 if ( IsVisible( m_currentCellCoords
, false ) )
7226 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7227 if ( !m_gridLinesEnabled
)
7235 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7237 // Otherwise refresh redraws the highlight!
7238 m_currentCellCoords
= coords
;
7240 #if defined(__WXMAC__)
7241 m_gridWin
->Refresh(true /*, & r */);
7243 DrawGridCellArea( dc
, cells
);
7244 DrawAllGridLines( dc
, r
);
7249 m_currentCellCoords
= coords
;
7251 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7252 #if !defined(__WXMAC__)
7253 DrawCellHighlight( dc
, attr
);
7258 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7261 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7265 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7268 rightCol
= GetNumberCols() - 1;
7270 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7273 bottomRow
= GetNumberRows() - 1;
7277 if ( topRow
> bottomRow
)
7284 if ( leftCol
> rightCol
)
7291 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7292 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7294 // First the case that we selected a completely new area
7295 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7296 m_selectingBottomRight
== wxGridNoCellCoords
)
7299 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7300 wxGridCellCoords ( bottomRow
, rightCol
) );
7301 m_gridWin
->Refresh( false, &rect
);
7304 // Now handle changing an existing selection area.
7305 else if ( m_selectingTopLeft
!= updateTopLeft
||
7306 m_selectingBottomRight
!= updateBottomRight
)
7308 // Compute two optimal update rectangles:
7309 // Either one rectangle is a real subset of the
7310 // other, or they are (almost) disjoint!
7312 bool need_refresh
[4];
7316 need_refresh
[3] = false;
7319 // Store intermediate values
7320 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7321 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7322 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7323 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7325 // Determine the outer/inner coordinates.
7326 if (oldLeft
> leftCol
)
7332 if (oldTop
> topRow
)
7338 if (oldRight
< rightCol
)
7341 oldRight
= rightCol
;
7344 if (oldBottom
< bottomRow
)
7347 oldBottom
= bottomRow
;
7351 // Now, either the stuff marked old is the outer
7352 // rectangle or we don't have a situation where one
7353 // is contained in the other.
7355 if ( oldLeft
< leftCol
)
7357 // Refresh the newly selected or deselected
7358 // area to the left of the old or new selection.
7359 need_refresh
[0] = true;
7360 rect
[0] = BlockToDeviceRect(
7361 wxGridCellCoords( oldTop
, oldLeft
),
7362 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7365 if ( oldTop
< topRow
)
7367 // Refresh the newly selected or deselected
7368 // area above the old or new selection.
7369 need_refresh
[1] = true;
7370 rect
[1] = BlockToDeviceRect(
7371 wxGridCellCoords( oldTop
, leftCol
),
7372 wxGridCellCoords( topRow
- 1, rightCol
) );
7375 if ( oldRight
> rightCol
)
7377 // Refresh the newly selected or deselected
7378 // area to the right of the old or new selection.
7379 need_refresh
[2] = true;
7380 rect
[2] = BlockToDeviceRect(
7381 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7382 wxGridCellCoords( oldBottom
, oldRight
) );
7385 if ( oldBottom
> bottomRow
)
7387 // Refresh the newly selected or deselected
7388 // area below the old or new selection.
7389 need_refresh
[3] = true;
7390 rect
[3] = BlockToDeviceRect(
7391 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7392 wxGridCellCoords( oldBottom
, rightCol
) );
7395 // various Refresh() calls
7396 for (i
= 0; i
< 4; i
++ )
7397 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7398 m_gridWin
->Refresh( false, &(rect
[i
]) );
7402 m_selectingTopLeft
= updateTopLeft
;
7403 m_selectingBottomRight
= updateBottomRight
;
7407 // ------ functions to get/send data (see also public functions)
7410 bool wxGrid::GetModelValues()
7412 // Hide the editor, so it won't hide a changed value.
7413 HideCellEditControl();
7417 // all we need to do is repaint the grid
7419 m_gridWin
->Refresh();
7426 bool wxGrid::SetModelValues()
7430 // Disable the editor, so it won't hide a changed value.
7431 // Do we also want to save the current value of the editor first?
7433 DisableCellEditControl();
7437 for ( row
= 0; row
< m_numRows
; row
++ )
7439 for ( col
= 0; col
< m_numCols
; col
++ )
7441 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7451 // Note - this function only draws cells that are in the list of
7452 // exposed cells (usually set from the update region by
7453 // CalcExposedCells)
7455 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7457 if ( !m_numRows
|| !m_numCols
)
7460 int i
, numCells
= cells
.GetCount();
7461 int row
, col
, cell_rows
, cell_cols
;
7462 wxGridCellCoordsArray redrawCells
;
7464 for ( i
= numCells
- 1; i
>= 0; i
-- )
7466 row
= cells
[i
].GetRow();
7467 col
= cells
[i
].GetCol();
7468 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7470 // If this cell is part of a multicell block, find owner for repaint
7471 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7473 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7474 bool marked
= false;
7475 for ( int j
= 0; j
< numCells
; j
++ )
7477 if ( cell
== cells
[j
] )
7486 int count
= redrawCells
.GetCount();
7487 for (int j
= 0; j
< count
; j
++)
7489 if ( cell
== redrawCells
[j
] )
7497 redrawCells
.Add( cell
);
7500 // don't bother drawing this cell
7504 // If this cell is empty, find cell to left that might want to overflow
7505 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7507 for ( int l
= 0; l
< cell_rows
; l
++ )
7509 // find a cell in this row to leave already marked for repaint
7511 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7512 if ((redrawCells
[k
].GetCol() < left
) &&
7513 (redrawCells
[k
].GetRow() == row
))
7515 left
= redrawCells
[k
].GetCol();
7519 left
= 0; // oh well
7521 for (int j
= col
- 1; j
>= left
; j
--)
7523 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7525 if (GetCellOverflow(row
+ l
, j
))
7527 wxGridCellCoords
cell(row
+ l
, j
);
7528 bool marked
= false;
7530 for (int k
= 0; k
< numCells
; k
++)
7532 if ( cell
== cells
[k
] )
7541 int count
= redrawCells
.GetCount();
7542 for (int k
= 0; k
< count
; k
++)
7544 if ( cell
== redrawCells
[k
] )
7551 redrawCells
.Add( cell
);
7560 DrawCell( dc
, cells
[i
] );
7563 numCells
= redrawCells
.GetCount();
7565 for ( i
= numCells
- 1; i
>= 0; i
-- )
7567 DrawCell( dc
, redrawCells
[i
] );
7571 void wxGrid::DrawGridSpace( wxDC
& dc
)
7574 m_gridWin
->GetClientSize( &cw
, &ch
);
7577 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7579 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7580 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7582 if ( right
> rightCol
|| bottom
> bottomRow
)
7585 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7587 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
7588 dc
.SetPen( *wxTRANSPARENT_PEN
);
7590 if ( right
> rightCol
)
7592 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7595 if ( bottom
> bottomRow
)
7597 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7602 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7604 int row
= coords
.GetRow();
7605 int col
= coords
.GetCol();
7607 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7610 // we draw the cell border ourselves
7611 #if !WXGRID_DRAW_LINES
7612 if ( m_gridLinesEnabled
)
7613 DrawCellBorder( dc
, coords
);
7616 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7618 bool isCurrent
= coords
== m_currentCellCoords
;
7620 wxRect rect
= CellToRect( row
, col
);
7622 // if the editor is shown, we should use it and not the renderer
7623 // Note: However, only if it is really _shown_, i.e. not hidden!
7624 if ( isCurrent
&& IsCellEditControlShown() )
7626 // NB: this "#if..." is temporary and fixes a problem where the
7627 // edit control is erased by this code after being rendered.
7628 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7629 // implicitly, causing this out-of order render.
7630 #if !defined(__WXMAC__)
7631 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7632 editor
->PaintBackground(rect
, attr
);
7638 // but all the rest is drawn by the cell renderer and hence may be customized
7639 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7640 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7647 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7649 // don't show highlight when the grid doesn't have focus
7653 int row
= m_currentCellCoords
.GetRow();
7654 int col
= m_currentCellCoords
.GetCol();
7656 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7659 wxRect rect
= CellToRect(row
, col
);
7661 // hmmm... what could we do here to show that the cell is disabled?
7662 // for now, I just draw a thinner border than for the other ones, but
7663 // it doesn't look really good
7665 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7669 // The center of the drawn line is where the position/width/height of
7670 // the rectangle is actually at (on wxMSW at least), so the
7671 // size of the rectangle is reduced to compensate for the thickness of
7672 // the line. If this is too strange on non-wxMSW platforms then
7673 // please #ifdef this appropriately.
7674 rect
.x
+= penWidth
/ 2;
7675 rect
.y
+= penWidth
/ 2;
7676 rect
.width
-= penWidth
- 1;
7677 rect
.height
-= penWidth
- 1;
7679 // Now draw the rectangle
7680 // use the cellHighlightColour if the cell is inside a selection, this
7681 // will ensure the cell is always visible.
7682 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxPENSTYLE_SOLID
));
7683 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7684 dc
.DrawRectangle(rect
);
7688 // VZ: my experiments with 3D borders...
7690 // how to properly set colours for arbitrary bg?
7691 wxCoord x1
= rect
.x
,
7693 x2
= rect
.x
+ rect
.width
- 1,
7694 y2
= rect
.y
+ rect
.height
- 1;
7696 dc
.SetPen(*wxWHITE_PEN
);
7697 dc
.DrawLine(x1
, y1
, x2
, y1
);
7698 dc
.DrawLine(x1
, y1
, x1
, y2
);
7700 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7701 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7703 dc
.SetPen(*wxBLACK_PEN
);
7704 dc
.DrawLine(x1
, y2
, x2
, y2
);
7705 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7709 wxPen
wxGrid::GetDefaultGridLinePen()
7711 return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID
);
7714 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7716 return GetDefaultGridLinePen();
7719 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7721 return GetDefaultGridLinePen();
7724 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7726 int row
= coords
.GetRow();
7727 int col
= coords
.GetCol();
7728 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7732 wxRect rect
= CellToRect( row
, col
);
7734 // right hand border
7735 dc
.SetPen( GetColGridLinePen(col
) );
7736 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7737 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7740 dc
.SetPen( GetRowGridLinePen(row
) );
7741 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7742 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7745 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7747 // This if block was previously in wxGrid::OnPaint but that doesn't
7748 // seem to get called under wxGTK - MB
7750 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7751 m_numRows
&& m_numCols
)
7753 m_currentCellCoords
.Set(0, 0);
7756 if ( IsCellEditControlShown() )
7758 // don't show highlight when the edit control is shown
7762 // if the active cell was repainted, repaint its highlight too because it
7763 // might have been damaged by the grid lines
7764 size_t count
= cells
.GetCount();
7765 for ( size_t n
= 0; n
< count
; n
++ )
7767 if ( cells
[n
] == m_currentCellCoords
)
7769 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7770 DrawCellHighlight(dc
, attr
);
7778 // TODO: remove this ???
7779 // This is used to redraw all grid lines e.g. when the grid line colour
7782 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7784 #if !WXGRID_DRAW_LINES
7788 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7791 int top
, bottom
, left
, right
;
7793 #if 0 //#ifndef __WXGTK__
7797 m_gridWin
->GetClientSize(&cw
, &ch
);
7799 // virtual coords of visible area
7801 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7802 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7807 reg
.GetBox(x
, y
, w
, h
);
7808 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7809 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7813 m_gridWin
->GetClientSize(&cw
, &ch
);
7814 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7815 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7818 // avoid drawing grid lines past the last row and col
7820 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7821 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7823 // no gridlines inside multicells, clip them out
7824 int leftCol
= GetColPos( internalXToCol(left
) );
7825 int topRow
= internalYToRow(top
);
7826 int rightCol
= GetColPos( internalXToCol(right
) );
7827 int bottomRow
= internalYToRow(bottom
);
7829 wxRegion
clippedcells(0, 0, cw
, ch
);
7831 int i
, j
, cell_rows
, cell_cols
;
7834 for (j
=topRow
; j
<=bottomRow
; j
++)
7837 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7839 i
= GetColAt( colPos
);
7841 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7842 if ((cell_rows
> 1) || (cell_cols
> 1))
7844 rect
= CellToRect(j
,i
);
7845 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7846 clippedcells
.Subtract(rect
);
7848 else if ((cell_rows
< 0) || (cell_cols
< 0))
7850 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7851 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7852 clippedcells
.Subtract(rect
);
7857 dc
.SetDeviceClippingRegion( clippedcells
);
7860 // horizontal grid lines
7862 // already declared above - int i;
7863 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7865 int bot
= GetRowBottom(i
) - 1;
7874 dc
.SetPen( GetRowGridLinePen(i
) );
7875 dc
.DrawLine( left
, bot
, right
, bot
);
7879 // vertical grid lines
7882 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7884 i
= GetColAt( colPos
);
7886 int colRight
= GetColRight(i
);
7888 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7892 if ( colRight
> right
)
7897 if ( colRight
>= left
)
7899 dc
.SetPen( GetColGridLinePen(i
) );
7900 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7904 dc
.DestroyClippingRegion();
7907 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7913 size_t numLabels
= rows
.GetCount();
7915 for ( i
= 0; i
< numLabels
; i
++ )
7917 DrawRowLabel( dc
, rows
[i
] );
7921 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7923 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7928 int rowTop
= GetRowTop(row
),
7929 rowBottom
= GetRowBottom(row
) - 1;
7931 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
7932 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7933 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7934 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7936 dc
.SetPen( *wxWHITE_PEN
);
7937 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7938 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7940 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7941 dc
.SetTextForeground( GetLabelTextColour() );
7942 dc
.SetFont( GetLabelFont() );
7945 GetRowLabelAlignment( &hAlign
, &vAlign
);
7948 rect
.SetY( GetRowTop(row
) + 2 );
7949 rect
.SetWidth( m_rowLabelWidth
- 4 );
7950 rect
.SetHeight( GetRowHeight(row
) - 4 );
7951 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7954 void wxGrid::SetUseNativeColLabels( bool native
)
7956 m_nativeColumnLabels
= native
;
7959 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7960 SetColLabelSize( height
);
7963 m_colLabelWin
->Refresh();
7966 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7972 size_t numLabels
= cols
.GetCount();
7974 for ( i
= 0; i
< numLabels
; i
++ )
7976 DrawColLabel( dc
, cols
[i
] );
7980 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7982 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7985 int colLeft
= GetColLeft(col
);
7989 if (m_nativeColumnLabels
)
7991 rect
.SetX( colLeft
);
7993 rect
.SetWidth( GetColWidth(col
));
7994 rect
.SetHeight( m_colLabelHeight
);
7996 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7997 wxRendererNative::Get().DrawHeaderButton( win_dc
->GetWindow(), dc
, rect
, 0 );
8001 int colRight
= GetColRight(col
) - 1;
8003 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
8004 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
8005 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
8006 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8007 colRight
+ 1, m_colLabelHeight
- 1 );
8009 dc
.SetPen( *wxWHITE_PEN
);
8010 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8011 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8014 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8015 dc
.SetTextForeground( GetLabelTextColour() );
8016 dc
.SetFont( GetLabelFont() );
8018 int hAlign
, vAlign
, orient
;
8019 GetColLabelAlignment( &hAlign
, &vAlign
);
8020 orient
= GetColLabelTextOrientation();
8022 rect
.SetX( colLeft
+ 2 );
8024 rect
.SetWidth( GetColWidth(col
) - 4 );
8025 rect
.SetHeight( m_colLabelHeight
- 4 );
8026 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
8029 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8030 const wxString
& value
,
8034 int textOrientation
)
8036 wxArrayString lines
;
8038 StringToLines( value
, lines
);
8040 // Forward to new API.
8041 DrawTextRectangle( dc
,
8049 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8050 // add textOrientation support
8051 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8052 const wxArrayString
& lines
,
8056 int textOrientation
)
8058 if ( lines
.empty() )
8061 wxDCClipper
clip(dc
, rect
);
8066 if ( textOrientation
== wxHORIZONTAL
)
8067 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8069 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8073 switch ( vertAlign
)
8075 case wxALIGN_BOTTOM
:
8076 if ( textOrientation
== wxHORIZONTAL
)
8077 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8079 x
= rect
.x
+ rect
.width
- textWidth
;
8082 case wxALIGN_CENTRE
:
8083 if ( textOrientation
== wxHORIZONTAL
)
8084 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8086 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8091 if ( textOrientation
== wxHORIZONTAL
)
8098 // Align each line of a multi-line label
8099 size_t nLines
= lines
.GetCount();
8100 for ( size_t l
= 0; l
< nLines
; l
++ )
8102 const wxString
& line
= lines
[l
];
8106 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8110 wxCoord lineWidth
= 0,
8112 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8114 switch ( horizAlign
)
8117 if ( textOrientation
== wxHORIZONTAL
)
8118 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8120 y
= rect
.y
+ lineWidth
+ 1;
8123 case wxALIGN_CENTRE
:
8124 if ( textOrientation
== wxHORIZONTAL
)
8125 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8127 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8132 if ( textOrientation
== wxHORIZONTAL
)
8135 y
= rect
.y
+ rect
.height
- 1;
8139 if ( textOrientation
== wxHORIZONTAL
)
8141 dc
.DrawText( line
, x
, y
);
8146 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8152 // Split multi-line text up into an array of strings.
8153 // Any existing contents of the string array are preserved.
8155 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8156 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8160 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8161 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8163 while ( startPos
< (int)tVal
.length() )
8165 pos
= tVal
.Mid(startPos
).Find( eol
);
8170 else if ( pos
== 0 )
8172 lines
.Add( wxEmptyString
);
8176 lines
.Add( tVal
.Mid(startPos
, pos
) );
8179 startPos
+= pos
+ 1;
8182 if ( startPos
< (int)tVal
.length() )
8184 lines
.Add( tVal
.Mid( startPos
) );
8188 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8189 const wxArrayString
& lines
,
8190 long *width
, long *height
) const
8194 wxCoord lineW
= 0, lineH
= 0;
8197 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8199 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8200 w
= wxMax( w
, lineW
);
8209 // ------ Batch processing.
8211 void wxGrid::EndBatch()
8213 if ( m_batchCount
> 0 )
8216 if ( !m_batchCount
)
8219 m_rowLabelWin
->Refresh();
8220 m_colLabelWin
->Refresh();
8221 m_cornerLabelWin
->Refresh();
8222 m_gridWin
->Refresh();
8227 // Use this, rather than wxWindow::Refresh(), to force an immediate
8228 // repainting of the grid. Has no effect if you are already inside a
8229 // BeginBatch / EndBatch block.
8231 void wxGrid::ForceRefresh()
8237 bool wxGrid::Enable(bool enable
)
8239 if ( !wxScrolledWindow::Enable(enable
) )
8242 // redraw in the new state
8243 m_gridWin
->Refresh();
8249 // ------ Edit control functions
8252 void wxGrid::EnableEditing( bool edit
)
8254 // TODO: improve this ?
8256 if ( edit
!= m_editable
)
8259 EnableCellEditControl(edit
);
8264 void wxGrid::EnableCellEditControl( bool enable
)
8269 if ( enable
!= m_cellEditCtrlEnabled
)
8273 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8276 // this should be checked by the caller!
8277 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8279 // do it before ShowCellEditControl()
8280 m_cellEditCtrlEnabled
= enable
;
8282 ShowCellEditControl();
8286 //FIXME:add veto support
8287 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8289 HideCellEditControl();
8290 SaveEditControlValue();
8292 // do it after HideCellEditControl()
8293 m_cellEditCtrlEnabled
= enable
;
8298 bool wxGrid::IsCurrentCellReadOnly() const
8301 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8302 bool readonly
= attr
->IsReadOnly();
8308 bool wxGrid::CanEnableCellControl() const
8310 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8311 !IsCurrentCellReadOnly();
8314 bool wxGrid::IsCellEditControlEnabled() const
8316 // the cell edit control might be disable for all cells or just for the
8317 // current one if it's read only
8318 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8321 bool wxGrid::IsCellEditControlShown() const
8323 bool isShown
= false;
8325 if ( m_cellEditCtrlEnabled
)
8327 int row
= m_currentCellCoords
.GetRow();
8328 int col
= m_currentCellCoords
.GetCol();
8329 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8330 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8335 if ( editor
->IsCreated() )
8337 isShown
= editor
->GetControl()->IsShown();
8347 void wxGrid::ShowCellEditControl()
8349 if ( IsCellEditControlEnabled() )
8351 if ( !IsVisible( m_currentCellCoords
, false ) )
8353 m_cellEditCtrlEnabled
= false;
8358 wxRect rect
= CellToRect( m_currentCellCoords
);
8359 int row
= m_currentCellCoords
.GetRow();
8360 int col
= m_currentCellCoords
.GetCol();
8362 // if this is part of a multicell, find owner (topleft)
8363 int cell_rows
, cell_cols
;
8364 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8365 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8369 m_currentCellCoords
.SetRow( row
);
8370 m_currentCellCoords
.SetCol( col
);
8373 // erase the highlight and the cell contents because the editor
8374 // might not cover the entire cell
8375 wxClientDC
dc( m_gridWin
);
8377 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8378 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
8379 dc
.SetPen(*wxTRANSPARENT_PEN
);
8380 dc
.DrawRectangle(rect
);
8382 // convert to scrolled coords
8383 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8389 // cell is shifted by one pixel
8390 // However, don't allow x or y to become negative
8391 // since the SetSize() method interprets that as
8398 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8399 if ( !editor
->IsCreated() )
8401 editor
->Create(m_gridWin
, wxID_ANY
,
8402 new wxGridCellEditorEvtHandler(this, editor
));
8404 wxGridEditorCreatedEvent
evt(GetId(),
8405 wxEVT_GRID_EDITOR_CREATED
,
8409 editor
->GetControl());
8410 GetEventHandler()->ProcessEvent(evt
);
8413 // resize editor to overflow into righthand cells if allowed
8414 int maxWidth
= rect
.width
;
8415 wxString value
= GetCellValue(row
, col
);
8416 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8419 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8420 if (maxWidth
< rect
.width
)
8421 maxWidth
= rect
.width
;
8424 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8425 if (rect
.x
+ maxWidth
> client_right
)
8426 maxWidth
= client_right
- rect
.x
;
8428 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8430 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8431 // may have changed earlier
8432 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8435 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8437 // looks weird going over a multicell
8438 if (m_table
->IsEmptyCell( row
, i
) &&
8439 (rect
.width
< maxWidth
) && (c_rows
== 1))
8441 rect
.width
+= GetColWidth( i
);
8447 if (rect
.GetRight() > client_right
)
8448 rect
.SetRight( client_right
- 1 );
8451 editor
->SetCellAttr( attr
);
8452 editor
->SetSize( rect
);
8454 editor
->GetControl()->Move(
8455 editor
->GetControl()->GetPosition().x
+ nXMove
,
8456 editor
->GetControl()->GetPosition().y
);
8457 editor
->Show( true, attr
);
8459 // recalc dimensions in case we need to
8460 // expand the scrolled window to account for editor
8463 editor
->BeginEdit(row
, col
, this);
8464 editor
->SetCellAttr(NULL
);
8472 void wxGrid::HideCellEditControl()
8474 if ( IsCellEditControlEnabled() )
8476 int row
= m_currentCellCoords
.GetRow();
8477 int col
= m_currentCellCoords
.GetCol();
8479 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8480 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8481 editor
->Show( false );
8485 m_gridWin
->SetFocus();
8487 // refresh whole row to the right
8488 wxRect
rect( CellToRect(row
, col
) );
8489 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8490 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8493 // ensure that the pixels under the focus ring get refreshed as well
8494 rect
.Inflate(10, 10);
8497 m_gridWin
->Refresh( false, &rect
);
8501 void wxGrid::SaveEditControlValue()
8503 if ( IsCellEditControlEnabled() )
8505 int row
= m_currentCellCoords
.GetRow();
8506 int col
= m_currentCellCoords
.GetCol();
8508 wxString oldval
= GetCellValue(row
, col
);
8510 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8511 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8512 bool changed
= editor
->EndEdit(row
, col
, this);
8519 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8520 m_currentCellCoords
.GetRow(),
8521 m_currentCellCoords
.GetCol() ) < 0 )
8523 // Event has been vetoed, set the data back.
8524 SetCellValue(row
, col
, oldval
);
8531 // ------ Grid location functions
8532 // Note that all of these functions work with the logical coordinates of
8533 // grid cells and labels so you will need to convert from device
8534 // coordinates for mouse events etc.
8537 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8539 int row
= YToRow(y
);
8540 int col
= XToCol(x
);
8542 if ( row
== -1 || col
== -1 )
8544 coords
= wxGridNoCellCoords
;
8548 coords
.Set( row
, col
);
8552 // Internal Helper function for computing row or column from some
8553 // (unscrolled) coordinate value, using either
8554 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8555 // of m_rowBottoms/m_ColRights to speed up the search!
8557 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8558 const wxArrayInt
& BorderArray
, int nMax
,
8562 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8567 size_t i_max
= coord
/ defaultDist
,
8570 if (BorderArray
.IsEmpty())
8572 if ((int) i_max
< nMax
)
8574 return clipToMinMax
? nMax
- 1 : -1;
8577 if ( i_max
>= BorderArray
.GetCount())
8579 i_max
= BorderArray
.GetCount() - 1;
8583 if ( coord
>= BorderArray
[i_max
])
8587 i_max
= coord
/ minDist
;
8589 i_max
= BorderArray
.GetCount() - 1;
8592 if ( i_max
>= BorderArray
.GetCount())
8593 i_max
= BorderArray
.GetCount() - 1;
8596 if ( coord
>= BorderArray
[i_max
])
8597 return clipToMinMax
? (int)i_max
: -1;
8598 if ( coord
< BorderArray
[0] )
8601 while ( i_max
- i_min
> 0 )
8603 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8604 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8605 if (coord
>= BorderArray
[ i_max
- 1])
8609 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8610 if (coord
< BorderArray
[median
])
8619 int wxGrid::YToRow( int y
) const
8621 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8622 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8625 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8628 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8630 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8632 int maxPos
= x
/ m_defaultColWidth
;
8635 if (m_colRights
.IsEmpty())
8637 if(maxPos
< m_numCols
)
8638 return GetColAt( maxPos
);
8639 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8642 if ( maxPos
>= m_numCols
)
8643 maxPos
= m_numCols
- 1;
8646 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8649 if (m_minAcceptableColWidth
)
8650 maxPos
= x
/ m_minAcceptableColWidth
;
8652 maxPos
= m_numCols
- 1;
8654 if ( maxPos
>= m_numCols
)
8655 maxPos
= m_numCols
- 1;
8658 //X is beyond the last column
8659 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8660 return clipToMinMax
? GetColAt( maxPos
) : -1;
8662 //X is before the first column
8663 if ( x
< m_colRights
[GetColAt( 0 )] )
8664 return GetColAt( 0 );
8666 //Perform a binary search
8667 while ( maxPos
- minPos
> 0 )
8669 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8670 0, _T("wxGrid: internal error in XToCol"));
8672 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8673 return GetColAt( maxPos
);
8676 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8677 if (x
< m_colRights
[GetColAt( median
)])
8682 return GetColAt( maxPos
);
8685 // return the row number that that the y coord is near
8686 // the edge of, or -1 if not near an edge.
8687 // coords can only possibly be near an edge if
8688 // (a) the row/column is large enough to still allow for an "inner" area
8689 // that is _not_ nead the edge (i.e., if the height/width is smaller
8690 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8693 // (b) resizing rows/columns (the thing for which edge detection is
8694 // relevant at all) is enabled.
8696 int wxGrid::YToEdgeOfRow( int y
) const
8699 i
= internalYToRow(y
);
8701 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8703 // We know that we are in row i, test whether we are
8704 // close enough to lower or upper border, respectively.
8705 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8707 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8714 // return the col number that that the x coord is near the edge of, or
8715 // -1 if not near an edge
8716 // See comment at YToEdgeOfRow for conditions on edge detection.
8718 int wxGrid::XToEdgeOfCol( int x
) const
8721 i
= internalXToCol(x
);
8723 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8725 // We know that we are in column i; test whether we are
8726 // close enough to right or left border, respectively.
8727 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8729 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8736 wxRect
wxGrid::CellToRect( int row
, int col
) const
8738 wxRect
rect( -1, -1, -1, -1 );
8740 if ( row
>= 0 && row
< m_numRows
&&
8741 col
>= 0 && col
< m_numCols
)
8743 int i
, cell_rows
, cell_cols
;
8744 rect
.width
= rect
.height
= 0;
8745 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8746 // if negative then find multicell owner
8751 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8753 rect
.x
= GetColLeft(col
);
8754 rect
.y
= GetRowTop(row
);
8755 for (i
=col
; i
< col
+ cell_cols
; i
++)
8756 rect
.width
+= GetColWidth(i
);
8757 for (i
=row
; i
< row
+ cell_rows
; i
++)
8758 rect
.height
+= GetRowHeight(i
);
8761 // if grid lines are enabled, then the area of the cell is a bit smaller
8762 if (m_gridLinesEnabled
)
8771 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8773 // get the cell rectangle in logical coords
8775 wxRect
r( CellToRect( row
, col
) );
8777 // convert to device coords
8779 int left
, top
, right
, bottom
;
8780 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8781 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8783 // check against the client area of the grid window
8785 m_gridWin
->GetClientSize( &cw
, &ch
);
8787 if ( wholeCellVisible
)
8789 // is the cell wholly visible ?
8790 return ( left
>= 0 && right
<= cw
&&
8791 top
>= 0 && bottom
<= ch
);
8795 // is the cell partly visible ?
8797 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8798 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8802 // make the specified cell location visible by doing a minimal amount
8805 void wxGrid::MakeCellVisible( int row
, int col
)
8808 int xpos
= -1, ypos
= -1;
8810 if ( row
>= 0 && row
< m_numRows
&&
8811 col
>= 0 && col
< m_numCols
)
8813 // get the cell rectangle in logical coords
8814 wxRect
r( CellToRect( row
, col
) );
8816 // convert to device coords
8817 int left
, top
, right
, bottom
;
8818 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8819 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8822 m_gridWin
->GetClientSize( &cw
, &ch
);
8828 else if ( bottom
> ch
)
8830 int h
= r
.GetHeight();
8832 for ( i
= row
- 1; i
>= 0; i
-- )
8834 int rowHeight
= GetRowHeight(i
);
8835 if ( h
+ rowHeight
> ch
)
8842 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8843 // have rounding errors (this is important, because if we do,
8844 // we might not scroll at all and some cells won't be redrawn)
8846 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8847 // so just add a full scroll unit...
8848 ypos
+= m_scrollLineY
;
8851 // special handling for wide cells - show always left part of the cell!
8852 // Otherwise, e.g. when stepping from row to row, it would jump between
8853 // left and right part of the cell on every step!
8855 if ( left
< 0 || (right
- left
) >= cw
)
8859 else if ( right
> cw
)
8861 // position the view so that the cell is on the right
8863 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8864 xpos
= x0
+ (right
- cw
);
8866 // see comment for ypos above
8867 xpos
+= m_scrollLineX
;
8870 if ( xpos
!= -1 || ypos
!= -1 )
8873 xpos
/= m_scrollLineX
;
8875 ypos
/= m_scrollLineY
;
8876 Scroll( xpos
, ypos
);
8883 // ------ Grid cursor movement functions
8886 bool wxGrid::MoveCursorUp( bool expandSelection
)
8888 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8889 m_currentCellCoords
.GetRow() >= 0 )
8891 if ( expandSelection
)
8893 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8894 m_selectingKeyboard
= m_currentCellCoords
;
8895 if ( m_selectingKeyboard
.GetRow() > 0 )
8897 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8898 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8899 m_selectingKeyboard
.GetCol() );
8900 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8903 else if ( m_currentCellCoords
.GetRow() > 0 )
8905 int row
= m_currentCellCoords
.GetRow() - 1;
8906 int col
= m_currentCellCoords
.GetCol();
8908 MakeCellVisible( row
, col
);
8909 SetCurrentCell( row
, col
);
8920 bool wxGrid::MoveCursorDown( bool expandSelection
)
8922 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8923 m_currentCellCoords
.GetRow() < m_numRows
)
8925 if ( expandSelection
)
8927 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8928 m_selectingKeyboard
= m_currentCellCoords
;
8929 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8931 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8932 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8933 m_selectingKeyboard
.GetCol() );
8934 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8937 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8939 int row
= m_currentCellCoords
.GetRow() + 1;
8940 int col
= m_currentCellCoords
.GetCol();
8942 MakeCellVisible( row
, col
);
8943 SetCurrentCell( row
, col
);
8954 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8956 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8957 m_currentCellCoords
.GetCol() >= 0 )
8959 if ( expandSelection
)
8961 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8962 m_selectingKeyboard
= m_currentCellCoords
;
8963 if ( m_selectingKeyboard
.GetCol() > 0 )
8965 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8966 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8967 m_selectingKeyboard
.GetCol() );
8968 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8971 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8973 int row
= m_currentCellCoords
.GetRow();
8974 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8977 MakeCellVisible( row
, col
);
8978 SetCurrentCell( row
, col
);
8989 bool wxGrid::MoveCursorRight( bool expandSelection
)
8991 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8992 m_currentCellCoords
.GetCol() < m_numCols
)
8994 if ( expandSelection
)
8996 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8997 m_selectingKeyboard
= m_currentCellCoords
;
8998 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
9000 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
9001 MakeCellVisible( m_selectingKeyboard
.GetRow(),
9002 m_selectingKeyboard
.GetCol() );
9003 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9006 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
9008 int row
= m_currentCellCoords
.GetRow();
9009 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
9012 MakeCellVisible( row
, col
);
9013 SetCurrentCell( row
, col
);
9024 bool wxGrid::MovePageUp()
9026 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9029 int row
= m_currentCellCoords
.GetRow();
9033 m_gridWin
->GetClientSize( &cw
, &ch
);
9035 int y
= GetRowTop(row
);
9036 int newRow
= internalYToRow( y
- ch
+ 1 );
9038 if ( newRow
== row
)
9040 // row > 0, so newRow can never be less than 0 here.
9044 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9045 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9053 bool wxGrid::MovePageDown()
9055 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9058 int row
= m_currentCellCoords
.GetRow();
9059 if ( (row
+ 1) < m_numRows
)
9062 m_gridWin
->GetClientSize( &cw
, &ch
);
9064 int y
= GetRowTop(row
);
9065 int newRow
= internalYToRow( y
+ ch
);
9066 if ( newRow
== row
)
9068 // row < m_numRows, so newRow can't overflow here.
9072 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9073 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9081 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9084 m_currentCellCoords
!= wxGridNoCellCoords
&&
9085 m_currentCellCoords
.GetRow() > 0 )
9087 int row
= m_currentCellCoords
.GetRow();
9088 int col
= m_currentCellCoords
.GetCol();
9090 if ( m_table
->IsEmptyCell(row
, col
) )
9092 // starting in an empty cell: find the next block of
9098 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9102 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9104 // starting at the top of a block: find the next block
9110 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9116 // starting within a block: find the top of the block
9121 if ( m_table
->IsEmptyCell(row
, col
) )
9129 MakeCellVisible( row
, col
);
9130 if ( expandSelection
)
9132 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9133 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9138 SetCurrentCell( row
, col
);
9147 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9150 m_currentCellCoords
!= wxGridNoCellCoords
&&
9151 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9153 int row
= m_currentCellCoords
.GetRow();
9154 int col
= m_currentCellCoords
.GetCol();
9156 if ( m_table
->IsEmptyCell(row
, col
) )
9158 // starting in an empty cell: find the next block of
9161 while ( row
< m_numRows
- 1 )
9164 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9168 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9170 // starting at the bottom of a block: find the next block
9173 while ( row
< m_numRows
- 1 )
9176 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9182 // starting within a block: find the bottom of the block
9184 while ( row
< m_numRows
- 1 )
9187 if ( m_table
->IsEmptyCell(row
, col
) )
9195 MakeCellVisible( row
, col
);
9196 if ( expandSelection
)
9198 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9199 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9204 SetCurrentCell( row
, col
);
9213 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9216 m_currentCellCoords
!= wxGridNoCellCoords
&&
9217 m_currentCellCoords
.GetCol() > 0 )
9219 int row
= m_currentCellCoords
.GetRow();
9220 int col
= m_currentCellCoords
.GetCol();
9222 if ( m_table
->IsEmptyCell(row
, col
) )
9224 // starting in an empty cell: find the next block of
9230 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9234 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9236 // starting at the left of a block: find the next block
9242 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9248 // starting within a block: find the left of the block
9253 if ( m_table
->IsEmptyCell(row
, col
) )
9261 MakeCellVisible( row
, col
);
9262 if ( expandSelection
)
9264 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9265 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9270 SetCurrentCell( row
, col
);
9279 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9282 m_currentCellCoords
!= wxGridNoCellCoords
&&
9283 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9285 int row
= m_currentCellCoords
.GetRow();
9286 int col
= m_currentCellCoords
.GetCol();
9288 if ( m_table
->IsEmptyCell(row
, col
) )
9290 // starting in an empty cell: find the next block of
9293 while ( col
< m_numCols
- 1 )
9296 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9300 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9302 // starting at the right of a block: find the next block
9305 while ( col
< m_numCols
- 1 )
9308 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9314 // starting within a block: find the right of the block
9316 while ( col
< m_numCols
- 1 )
9319 if ( m_table
->IsEmptyCell(row
, col
) )
9327 MakeCellVisible( row
, col
);
9328 if ( expandSelection
)
9330 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9331 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9336 SetCurrentCell( row
, col
);
9346 // ------ Label values and formatting
9349 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9352 *horiz
= m_rowLabelHorizAlign
;
9354 *vert
= m_rowLabelVertAlign
;
9357 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9360 *horiz
= m_colLabelHorizAlign
;
9362 *vert
= m_colLabelVertAlign
;
9365 int wxGrid::GetColLabelTextOrientation() const
9367 return m_colLabelTextOrientation
;
9370 wxString
wxGrid::GetRowLabelValue( int row
) const
9374 return m_table
->GetRowLabelValue( row
);
9384 wxString
wxGrid::GetColLabelValue( int col
) const
9388 return m_table
->GetColLabelValue( col
);
9398 void wxGrid::SetRowLabelSize( int width
)
9400 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9402 if ( width
== wxGRID_AUTOSIZE
)
9404 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9407 if ( width
!= m_rowLabelWidth
)
9411 m_rowLabelWin
->Show( false );
9412 m_cornerLabelWin
->Show( false );
9414 else if ( m_rowLabelWidth
== 0 )
9416 m_rowLabelWin
->Show( true );
9417 if ( m_colLabelHeight
> 0 )
9418 m_cornerLabelWin
->Show( true );
9421 m_rowLabelWidth
= width
;
9423 wxScrolledWindow::Refresh( true );
9427 void wxGrid::SetColLabelSize( int height
)
9429 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9431 if ( height
== wxGRID_AUTOSIZE
)
9433 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9436 if ( height
!= m_colLabelHeight
)
9440 m_colLabelWin
->Show( false );
9441 m_cornerLabelWin
->Show( false );
9443 else if ( m_colLabelHeight
== 0 )
9445 m_colLabelWin
->Show( true );
9446 if ( m_rowLabelWidth
> 0 )
9447 m_cornerLabelWin
->Show( true );
9450 m_colLabelHeight
= height
;
9452 wxScrolledWindow::Refresh( true );
9456 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9458 if ( m_labelBackgroundColour
!= colour
)
9460 m_labelBackgroundColour
= colour
;
9461 m_rowLabelWin
->SetBackgroundColour( colour
);
9462 m_colLabelWin
->SetBackgroundColour( colour
);
9463 m_cornerLabelWin
->SetBackgroundColour( colour
);
9465 if ( !GetBatchCount() )
9467 m_rowLabelWin
->Refresh();
9468 m_colLabelWin
->Refresh();
9469 m_cornerLabelWin
->Refresh();
9474 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9476 if ( m_labelTextColour
!= colour
)
9478 m_labelTextColour
= colour
;
9479 if ( !GetBatchCount() )
9481 m_rowLabelWin
->Refresh();
9482 m_colLabelWin
->Refresh();
9487 void wxGrid::SetLabelFont( const wxFont
& font
)
9490 if ( !GetBatchCount() )
9492 m_rowLabelWin
->Refresh();
9493 m_colLabelWin
->Refresh();
9497 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9499 // allow old (incorrect) defs to be used
9502 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9503 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9504 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9509 case wxTOP
: vert
= wxALIGN_TOP
; break;
9510 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9511 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9514 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9516 m_rowLabelHorizAlign
= horiz
;
9519 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9521 m_rowLabelVertAlign
= vert
;
9524 if ( !GetBatchCount() )
9526 m_rowLabelWin
->Refresh();
9530 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9532 // allow old (incorrect) defs to be used
9535 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9536 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9537 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9542 case wxTOP
: vert
= wxALIGN_TOP
; break;
9543 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9544 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9547 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9549 m_colLabelHorizAlign
= horiz
;
9552 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9554 m_colLabelVertAlign
= vert
;
9557 if ( !GetBatchCount() )
9559 m_colLabelWin
->Refresh();
9563 // Note: under MSW, the default column label font must be changed because it
9564 // does not support vertical printing
9566 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9567 // pGrid->SetLabelFont(font);
9568 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9570 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9572 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9573 m_colLabelTextOrientation
= textOrientation
;
9575 if ( !GetBatchCount() )
9576 m_colLabelWin
->Refresh();
9579 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9583 m_table
->SetRowLabelValue( row
, s
);
9584 if ( !GetBatchCount() )
9586 wxRect rect
= CellToRect( row
, 0 );
9587 if ( rect
.height
> 0 )
9589 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9591 rect
.width
= m_rowLabelWidth
;
9592 m_rowLabelWin
->Refresh( true, &rect
);
9598 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9602 m_table
->SetColLabelValue( col
, s
);
9603 if ( !GetBatchCount() )
9605 wxRect rect
= CellToRect( 0, col
);
9606 if ( rect
.width
> 0 )
9608 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9610 rect
.height
= m_colLabelHeight
;
9611 m_colLabelWin
->Refresh( true, &rect
);
9617 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9619 if ( m_gridLineColour
!= colour
)
9621 m_gridLineColour
= colour
;
9623 wxClientDC
dc( m_gridWin
);
9625 DrawAllGridLines( dc
, wxRegion() );
9629 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9631 if ( m_cellHighlightColour
!= colour
)
9633 m_cellHighlightColour
= colour
;
9635 wxClientDC
dc( m_gridWin
);
9637 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9638 DrawCellHighlight(dc
, attr
);
9643 void wxGrid::SetCellHighlightPenWidth(int width
)
9645 if (m_cellHighlightPenWidth
!= width
)
9647 m_cellHighlightPenWidth
= width
;
9649 // Just redrawing the cell highlight is not enough since that won't
9650 // make any visible change if the the thickness is getting smaller.
9651 int row
= m_currentCellCoords
.GetRow();
9652 int col
= m_currentCellCoords
.GetCol();
9653 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9656 wxRect rect
= CellToRect(row
, col
);
9657 m_gridWin
->Refresh(true, &rect
);
9661 void wxGrid::SetCellHighlightROPenWidth(int width
)
9663 if (m_cellHighlightROPenWidth
!= width
)
9665 m_cellHighlightROPenWidth
= width
;
9667 // Just redrawing the cell highlight is not enough since that won't
9668 // make any visible change if the the thickness is getting smaller.
9669 int row
= m_currentCellCoords
.GetRow();
9670 int col
= m_currentCellCoords
.GetCol();
9671 if ( row
== -1 || col
== -1 ||
9672 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9675 wxRect rect
= CellToRect(row
, col
);
9676 m_gridWin
->Refresh(true, &rect
);
9680 void wxGrid::EnableGridLines( bool enable
)
9682 if ( enable
!= m_gridLinesEnabled
)
9684 m_gridLinesEnabled
= enable
;
9686 if ( !GetBatchCount() )
9690 wxClientDC
dc( m_gridWin
);
9692 DrawAllGridLines( dc
, wxRegion() );
9696 m_gridWin
->Refresh();
9702 int wxGrid::GetDefaultRowSize() const
9704 return m_defaultRowHeight
;
9707 int wxGrid::GetRowSize( int row
) const
9709 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9711 return GetRowHeight(row
);
9714 int wxGrid::GetDefaultColSize() const
9716 return m_defaultColWidth
;
9719 int wxGrid::GetColSize( int col
) const
9721 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9723 return GetColWidth(col
);
9726 // ============================================================================
9727 // access to the grid attributes: each of them has a default value in the grid
9728 // itself and may be overidden on a per-cell basis
9729 // ============================================================================
9731 // ----------------------------------------------------------------------------
9732 // setting default attributes
9733 // ----------------------------------------------------------------------------
9735 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9737 m_defaultCellAttr
->SetBackgroundColour(col
);
9739 m_gridWin
->SetBackgroundColour(col
);
9743 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9745 m_defaultCellAttr
->SetTextColour(col
);
9748 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9750 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9753 void wxGrid::SetDefaultCellOverflow( bool allow
)
9755 m_defaultCellAttr
->SetOverflow(allow
);
9758 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9760 m_defaultCellAttr
->SetFont(font
);
9763 // For editors and renderers the type registry takes precedence over the
9764 // default attr, so we need to register the new editor/renderer for the string
9765 // data type in order to make setting a default editor/renderer appear to
9768 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9770 RegisterDataType(wxGRID_VALUE_STRING
,
9772 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9775 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9777 RegisterDataType(wxGRID_VALUE_STRING
,
9778 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9782 // ----------------------------------------------------------------------------
9783 // access to the default attributes
9784 // ----------------------------------------------------------------------------
9786 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9788 return m_defaultCellAttr
->GetBackgroundColour();
9791 wxColour
wxGrid::GetDefaultCellTextColour() const
9793 return m_defaultCellAttr
->GetTextColour();
9796 wxFont
wxGrid::GetDefaultCellFont() const
9798 return m_defaultCellAttr
->GetFont();
9801 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9803 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9806 bool wxGrid::GetDefaultCellOverflow() const
9808 return m_defaultCellAttr
->GetOverflow();
9811 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9813 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9816 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9818 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9821 // ----------------------------------------------------------------------------
9822 // access to cell attributes
9823 // ----------------------------------------------------------------------------
9825 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9827 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9828 wxColour colour
= attr
->GetBackgroundColour();
9834 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9836 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9837 wxColour colour
= attr
->GetTextColour();
9843 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9845 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9846 wxFont font
= attr
->GetFont();
9852 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9854 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9855 attr
->GetAlignment(horiz
, vert
);
9859 bool wxGrid::GetCellOverflow( int row
, int col
) const
9861 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9862 bool allow
= attr
->GetOverflow();
9868 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9870 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9871 attr
->GetSize( num_rows
, num_cols
);
9875 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9877 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9878 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9884 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9886 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9887 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9893 bool wxGrid::IsReadOnly(int row
, int col
) const
9895 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9896 bool isReadOnly
= attr
->IsReadOnly();
9902 // ----------------------------------------------------------------------------
9903 // attribute support: cache, automatic provider creation, ...
9904 // ----------------------------------------------------------------------------
9906 bool wxGrid::CanHaveAttributes() const
9913 return m_table
->CanHaveAttributes();
9916 void wxGrid::ClearAttrCache()
9918 if ( m_attrCache
.row
!= -1 )
9920 wxSafeDecRef(m_attrCache
.attr
);
9921 m_attrCache
.attr
= NULL
;
9922 m_attrCache
.row
= -1;
9926 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9930 wxGrid
*self
= (wxGrid
*)this; // const_cast
9932 self
->ClearAttrCache();
9933 self
->m_attrCache
.row
= row
;
9934 self
->m_attrCache
.col
= col
;
9935 self
->m_attrCache
.attr
= attr
;
9940 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9942 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9944 *attr
= m_attrCache
.attr
;
9945 wxSafeIncRef(m_attrCache
.attr
);
9947 #ifdef DEBUG_ATTR_CACHE
9948 gs_nAttrCacheHits
++;
9955 #ifdef DEBUG_ATTR_CACHE
9956 gs_nAttrCacheMisses
++;
9963 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9965 wxGridCellAttr
*attr
= NULL
;
9966 // Additional test to avoid looking at the cache e.g. for
9967 // wxNoCellCoords, as this will confuse memory management.
9970 if ( !LookupAttr(row
, col
, &attr
) )
9972 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9973 : (wxGridCellAttr
*)NULL
;
9974 CacheAttr(row
, col
, attr
);
9980 attr
->SetDefAttr(m_defaultCellAttr
);
9984 attr
= m_defaultCellAttr
;
9991 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9993 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9994 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9996 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9997 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9999 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
10002 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10004 // artificially inc the ref count to match DecRef() in caller
10006 m_table
->SetAttr(attr
, row
, col
);
10012 // ----------------------------------------------------------------------------
10013 // setting column attributes (wrappers around SetColAttr)
10014 // ----------------------------------------------------------------------------
10016 void wxGrid::SetColFormatBool(int col
)
10018 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10021 void wxGrid::SetColFormatNumber(int col
)
10023 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10026 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10028 wxString typeName
= wxGRID_VALUE_FLOAT
;
10029 if ( (width
!= -1) || (precision
!= -1) )
10031 typeName
<< _T(':') << width
<< _T(',') << precision
;
10034 SetColFormatCustom(col
, typeName
);
10037 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10039 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10041 attr
= new wxGridCellAttr
;
10042 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10043 attr
->SetRenderer(renderer
);
10044 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
10045 attr
->SetEditor(editor
);
10047 SetColAttr(col
, attr
);
10051 // ----------------------------------------------------------------------------
10052 // setting cell attributes: this is forwarded to the table
10053 // ----------------------------------------------------------------------------
10055 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10057 if ( CanHaveAttributes() )
10059 m_table
->SetAttr(attr
, row
, col
);
10064 wxSafeDecRef(attr
);
10068 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10070 if ( CanHaveAttributes() )
10072 m_table
->SetRowAttr(attr
, row
);
10077 wxSafeDecRef(attr
);
10081 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10083 if ( CanHaveAttributes() )
10085 m_table
->SetColAttr(attr
, col
);
10090 wxSafeDecRef(attr
);
10094 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10096 if ( CanHaveAttributes() )
10098 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10099 attr
->SetBackgroundColour(colour
);
10104 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10106 if ( CanHaveAttributes() )
10108 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10109 attr
->SetTextColour(colour
);
10114 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10116 if ( CanHaveAttributes() )
10118 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10119 attr
->SetFont(font
);
10124 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10126 if ( CanHaveAttributes() )
10128 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10129 attr
->SetAlignment(horiz
, vert
);
10134 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10136 if ( CanHaveAttributes() )
10138 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10139 attr
->SetOverflow(allow
);
10144 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10146 if ( CanHaveAttributes() )
10148 int cell_rows
, cell_cols
;
10150 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10151 attr
->GetSize(&cell_rows
, &cell_cols
);
10152 attr
->SetSize(num_rows
, num_cols
);
10155 // Cannot set the size of a cell to 0 or negative values
10156 // While it is perfectly legal to do that, this function cannot
10157 // handle all the possibilies, do it by hand by getting the CellAttr.
10158 // You can only set the size of a cell to 1,1 or greater with this fn
10159 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10160 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10161 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10162 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10164 // if this was already a multicell then "turn off" the other cells first
10165 if ((cell_rows
> 1) || (cell_rows
> 1))
10168 for (j
=row
; j
< row
+ cell_rows
; j
++)
10170 for (i
=col
; i
< col
+ cell_cols
; i
++)
10172 if ((i
!= col
) || (j
!= row
))
10174 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10175 attr_stub
->SetSize( 1, 1 );
10176 attr_stub
->DecRef();
10182 // mark the cells that will be covered by this cell to
10183 // negative or zero values to point back at this cell
10184 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10187 for (j
=row
; j
< row
+ num_rows
; j
++)
10189 for (i
=col
; i
< col
+ num_cols
; i
++)
10191 if ((i
!= col
) || (j
!= row
))
10193 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10194 attr_stub
->SetSize( row
- j
, col
- i
);
10195 attr_stub
->DecRef();
10203 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10205 if ( CanHaveAttributes() )
10207 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10208 attr
->SetRenderer(renderer
);
10213 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10215 if ( CanHaveAttributes() )
10217 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10218 attr
->SetEditor(editor
);
10223 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10225 if ( CanHaveAttributes() )
10227 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10228 attr
->SetReadOnly(isReadOnly
);
10233 // ----------------------------------------------------------------------------
10234 // Data type registration
10235 // ----------------------------------------------------------------------------
10237 void wxGrid::RegisterDataType(const wxString
& typeName
,
10238 wxGridCellRenderer
* renderer
,
10239 wxGridCellEditor
* editor
)
10241 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10245 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10247 wxString typeName
= m_table
->GetTypeName(row
, col
);
10248 return GetDefaultEditorForType(typeName
);
10251 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10253 wxString typeName
= m_table
->GetTypeName(row
, col
);
10254 return GetDefaultRendererForType(typeName
);
10257 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10259 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10260 if ( index
== wxNOT_FOUND
)
10262 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10267 return m_typeRegistry
->GetEditor(index
);
10270 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10272 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10273 if ( index
== wxNOT_FOUND
)
10275 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10280 return m_typeRegistry
->GetRenderer(index
);
10283 // ----------------------------------------------------------------------------
10285 // ----------------------------------------------------------------------------
10287 void wxGrid::EnableDragRowSize( bool enable
)
10289 m_canDragRowSize
= enable
;
10292 void wxGrid::EnableDragColSize( bool enable
)
10294 m_canDragColSize
= enable
;
10297 void wxGrid::EnableDragGridSize( bool enable
)
10299 m_canDragGridSize
= enable
;
10302 void wxGrid::EnableDragCell( bool enable
)
10304 m_canDragCell
= enable
;
10307 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10309 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10311 if ( resizeExistingRows
)
10313 // since we are resizing all rows to the default row size,
10314 // we can simply clear the row heights and row bottoms
10315 // arrays (which also allows us to take advantage of
10316 // some speed optimisations)
10317 m_rowHeights
.Empty();
10318 m_rowBottoms
.Empty();
10319 if ( !GetBatchCount() )
10324 void wxGrid::SetRowSize( int row
, int height
)
10326 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10328 // See comment in SetColSize
10329 if ( height
< GetRowMinimalAcceptableHeight())
10332 if ( m_rowHeights
.IsEmpty() )
10334 // need to really create the array
10338 int h
= wxMax( 0, height
);
10339 int diff
= h
- m_rowHeights
[row
];
10341 m_rowHeights
[row
] = h
;
10342 for ( int i
= row
; i
< m_numRows
; i
++ )
10344 m_rowBottoms
[i
] += diff
;
10347 if ( !GetBatchCount() )
10351 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10353 // we dont allow zero default column width
10354 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10356 if ( resizeExistingCols
)
10358 // since we are resizing all columns to the default column size,
10359 // we can simply clear the col widths and col rights
10360 // arrays (which also allows us to take advantage of
10361 // some speed optimisations)
10362 m_colWidths
.Empty();
10363 m_colRights
.Empty();
10364 if ( !GetBatchCount() )
10369 void wxGrid::SetColSize( int col
, int width
)
10371 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10373 // should we check that it's bigger than GetColMinimalWidth(col) here?
10375 // No, because it is reasonable to assume the library user know's
10376 // what he is doing. However we should test against the weaker
10377 // constraint of minimalAcceptableWidth, as this breaks rendering
10379 // This test then fixes sf.net bug #645734
10381 if ( width
< GetColMinimalAcceptableWidth() )
10384 if ( m_colWidths
.IsEmpty() )
10386 // need to really create the array
10390 // if < 0 then calculate new width from label
10394 wxArrayString lines
;
10395 wxClientDC
dc(m_colLabelWin
);
10396 dc
.SetFont(GetLabelFont());
10397 StringToLines(GetColLabelValue(col
), lines
);
10398 GetTextBoxSize(dc
, lines
, &w
, &h
);
10402 int w
= wxMax( 0, width
);
10403 int diff
= w
- m_colWidths
[col
];
10404 m_colWidths
[col
] = w
;
10406 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10408 m_colRights
[GetColAt(colPos
)] += diff
;
10411 if ( !GetBatchCount() )
10415 void wxGrid::SetColMinimalWidth( int col
, int width
)
10417 if (width
> GetColMinimalAcceptableWidth())
10419 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10420 m_colMinWidths
[key
] = width
;
10424 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10426 if (width
> GetRowMinimalAcceptableHeight())
10428 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10429 m_rowMinHeights
[key
] = width
;
10433 int wxGrid::GetColMinimalWidth(int col
) const
10435 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10436 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10438 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10441 int wxGrid::GetRowMinimalHeight(int row
) const
10443 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10444 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10446 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10449 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10451 // We do allow a width of 0 since this gives us
10452 // an easy way to temporarily hiding columns.
10454 m_minAcceptableColWidth
= width
;
10457 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10459 // We do allow a height of 0 since this gives us
10460 // an easy way to temporarily hiding rows.
10462 m_minAcceptableRowHeight
= height
;
10465 int wxGrid::GetColMinimalAcceptableWidth() const
10467 return m_minAcceptableColWidth
;
10470 int wxGrid::GetRowMinimalAcceptableHeight() const
10472 return m_minAcceptableRowHeight
;
10475 // ----------------------------------------------------------------------------
10477 // ----------------------------------------------------------------------------
10480 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10482 const bool column
= direction
== wxGRID_COLUMN
;
10484 wxClientDC
dc(m_gridWin
);
10486 // cancel editing of cell
10487 HideCellEditControl();
10488 SaveEditControlValue();
10490 // init both of them to avoid compiler warnings, even if we only need one
10498 wxCoord extent
, extentMax
= 0;
10499 int max
= column
? m_numRows
: m_numCols
;
10500 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10507 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10508 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10511 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10512 extent
= column
? size
.x
: size
.y
;
10513 if ( extent
> extentMax
)
10514 extentMax
= extent
;
10516 renderer
->DecRef();
10522 // now also compare with the column label extent
10524 dc
.SetFont( GetLabelFont() );
10528 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10529 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10533 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10535 extent
= column
? w
: h
;
10536 if ( extent
> extentMax
)
10537 extentMax
= extent
;
10541 // empty column - give default extent (notice that if extentMax is less
10542 // than default extent but != 0, it's OK)
10543 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10548 // leave some space around text
10556 // Ensure automatic width is not less than minimal width. See the
10557 // comment in SetColSize() for explanation of why this isn't done
10558 // in SetColSize().
10560 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10562 SetColSize( col
, extentMax
);
10563 if ( !GetBatchCount() )
10566 m_gridWin
->GetClientSize( &cw
, &ch
);
10567 wxRect
rect ( CellToRect( 0, col
) );
10569 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10570 rect
.width
= cw
- rect
.x
;
10571 rect
.height
= m_colLabelHeight
;
10572 m_colLabelWin
->Refresh( true, &rect
);
10577 // Ensure automatic width is not less than minimal height. See the
10578 // comment in SetColSize() for explanation of why this isn't done
10579 // in SetRowSize().
10581 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10583 SetRowSize(row
, extentMax
);
10584 if ( !GetBatchCount() )
10587 m_gridWin
->GetClientSize( &cw
, &ch
);
10588 wxRect
rect( CellToRect( row
, 0 ) );
10590 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10591 rect
.width
= m_rowLabelWidth
;
10592 rect
.height
= ch
- rect
.y
;
10593 m_rowLabelWin
->Refresh( true, &rect
);
10600 SetColMinimalWidth(col
, extentMax
);
10602 SetRowMinimalHeight(row
, extentMax
);
10606 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10608 // calculate size for the rows or columns?
10609 const bool calcRows
= direction
== wxGRID_ROW
;
10611 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10612 : GetGridColLabelWindow());
10613 dc
.SetFont(GetLabelFont());
10615 // which dimension should we take into account for calculations?
10617 // for columns, the text can be only horizontal so it's easy but for rows
10618 // we also have to take into account the text orientation
10620 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10622 wxArrayString lines
;
10623 wxCoord extentMax
= 0;
10625 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10626 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10630 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10631 : GetColLabelValue(rowOrCol
);
10632 StringToLines(label
, lines
);
10635 GetTextBoxSize(dc
, lines
, &w
, &h
);
10637 const wxCoord extent
= useWidth
? w
: h
;
10638 if ( extent
> extentMax
)
10639 extentMax
= extent
;
10644 // empty column - give default extent (notice that if extentMax is less
10645 // than default extent but != 0, it's OK)
10646 extentMax
= calcRows
? GetDefaultRowLabelSize()
10647 : GetDefaultColLabelSize();
10650 // leave some space around text (taken from AutoSizeColOrRow)
10659 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10661 int width
= m_rowLabelWidth
;
10663 wxGridUpdateLocker locker
;
10665 locker
.Create(this);
10667 for ( int col
= 0; col
< m_numCols
; col
++ )
10670 AutoSizeColumn(col
, setAsMin
);
10672 width
+= GetColWidth(col
);
10678 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10680 int height
= m_colLabelHeight
;
10682 wxGridUpdateLocker locker
;
10684 locker
.Create(this);
10686 for ( int row
= 0; row
< m_numRows
; row
++ )
10689 AutoSizeRow(row
, setAsMin
);
10691 height
+= GetRowHeight(row
);
10697 void wxGrid::AutoSize()
10699 wxGridUpdateLocker
locker(this);
10701 // we need to round up the size of the scrollable area to a multiple of
10702 // scroll step to ensure that we don't get the scrollbars when we're sized
10703 // exactly to fit our contents
10704 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10705 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10706 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10707 GetScrollY(size
.y
) * GetScrollLineY());
10709 // distribute the extra space between the columns/rows to avoid having
10710 // extra white space
10711 wxCoord diff
= sizeFit
.x
- size
.x
;
10712 if ( diff
&& m_numCols
)
10714 // try to resize the columns uniformly
10715 wxCoord diffPerCol
= diff
/ m_numCols
;
10718 for ( int col
= 0; col
< m_numCols
; col
++ )
10720 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10724 // add remaining amount to the last columns
10725 diff
-= diffPerCol
* m_numCols
;
10728 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10730 SetColSize(col
, GetColWidth(col
) + 1);
10736 diff
= sizeFit
.y
- size
.y
;
10737 if ( diff
&& m_numRows
)
10739 // try to resize the columns uniformly
10740 wxCoord diffPerRow
= diff
/ m_numRows
;
10743 for ( int row
= 0; row
< m_numRows
; row
++ )
10745 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10749 // add remaining amount to the last rows
10750 diff
-= diffPerRow
* m_numRows
;
10753 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10755 SetRowSize(row
, GetRowHeight(row
) + 1);
10760 // we know that we're not going to have scrollbars so disable them now to
10761 // avoid trouble in SetClientSize() which can otherwise set the correct
10762 // client size but also leave space for (not needed any more) scrollbars
10763 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10764 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10767 void wxGrid::AutoSizeRowLabelSize( int row
)
10769 wxArrayString lines
;
10772 // Hide the edit control, so it
10773 // won't interfere with drag-shrinking.
10774 if ( IsCellEditControlShown() )
10776 HideCellEditControl();
10777 SaveEditControlValue();
10780 // autosize row height depending on label text
10781 StringToLines( GetRowLabelValue( row
), lines
);
10782 wxClientDC
dc( m_rowLabelWin
);
10783 GetTextBoxSize( dc
, lines
, &w
, &h
);
10784 if ( h
< m_defaultRowHeight
)
10785 h
= m_defaultRowHeight
;
10786 SetRowSize(row
, h
);
10790 void wxGrid::AutoSizeColLabelSize( int col
)
10792 wxArrayString lines
;
10795 // Hide the edit control, so it
10796 // won't interfere with drag-shrinking.
10797 if ( IsCellEditControlShown() )
10799 HideCellEditControl();
10800 SaveEditControlValue();
10803 // autosize column width depending on label text
10804 StringToLines( GetColLabelValue( col
), lines
);
10805 wxClientDC
dc( m_colLabelWin
);
10806 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10807 GetTextBoxSize( dc
, lines
, &w
, &h
);
10809 GetTextBoxSize( dc
, lines
, &h
, &w
);
10810 if ( w
< m_defaultColWidth
)
10811 w
= m_defaultColWidth
;
10812 SetColSize(col
, w
);
10816 wxSize
wxGrid::DoGetBestSize() const
10818 wxGrid
*self
= (wxGrid
*)this; // const_cast
10820 // we do the same as in AutoSize() here with the exception that we don't
10821 // change the column/row sizes, only calculate them
10822 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10823 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10824 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10825 GetScrollY(size
.y
) * GetScrollLineY());
10827 // NOTE: This size should be cached, but first we need to add calls to
10828 // InvalidateBestSize everywhere that could change the results of this
10830 // CacheBestSize(size);
10832 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10833 + GetWindowBorderSize();
10841 wxPen
& wxGrid::GetDividerPen() const
10846 // ----------------------------------------------------------------------------
10847 // cell value accessor functions
10848 // ----------------------------------------------------------------------------
10850 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10854 m_table
->SetValue( row
, col
, s
);
10855 if ( !GetBatchCount() )
10858 wxRect
rect( CellToRect( row
, col
) );
10860 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10861 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10862 m_gridWin
->Refresh( false, &rect
);
10865 if ( m_currentCellCoords
.GetRow() == row
&&
10866 m_currentCellCoords
.GetCol() == col
&&
10867 IsCellEditControlShown())
10868 // Note: If we are using IsCellEditControlEnabled,
10869 // this interacts badly with calling SetCellValue from
10870 // an EVT_GRID_CELL_CHANGE handler.
10872 HideCellEditControl();
10873 ShowCellEditControl(); // will reread data from table
10878 // ----------------------------------------------------------------------------
10879 // block, row and column selection
10880 // ----------------------------------------------------------------------------
10882 void wxGrid::SelectRow( int row
, bool addToSelected
)
10884 if ( IsSelection() && !addToSelected
)
10888 m_selection
->SelectRow( row
, false, addToSelected
);
10891 void wxGrid::SelectCol( int col
, bool addToSelected
)
10893 if ( IsSelection() && !addToSelected
)
10897 m_selection
->SelectCol( col
, false, addToSelected
);
10900 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10901 bool addToSelected
)
10903 if ( IsSelection() && !addToSelected
)
10907 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10908 false, addToSelected
);
10911 void wxGrid::SelectAll()
10913 if ( m_numRows
> 0 && m_numCols
> 0 )
10916 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10920 // ----------------------------------------------------------------------------
10921 // cell, row and col deselection
10922 // ----------------------------------------------------------------------------
10924 void wxGrid::DeselectRow( int row
)
10926 if ( !m_selection
)
10929 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10931 if ( m_selection
->IsInSelection(row
, 0 ) )
10932 m_selection
->ToggleCellSelection(row
, 0);
10936 int nCols
= GetNumberCols();
10937 for ( int i
= 0; i
< nCols
; i
++ )
10939 if ( m_selection
->IsInSelection(row
, i
) )
10940 m_selection
->ToggleCellSelection(row
, i
);
10945 void wxGrid::DeselectCol( int col
)
10947 if ( !m_selection
)
10950 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10952 if ( m_selection
->IsInSelection(0, col
) )
10953 m_selection
->ToggleCellSelection(0, col
);
10957 int nRows
= GetNumberRows();
10958 for ( int i
= 0; i
< nRows
; i
++ )
10960 if ( m_selection
->IsInSelection(i
, col
) )
10961 m_selection
->ToggleCellSelection(i
, col
);
10966 void wxGrid::DeselectCell( int row
, int col
)
10968 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10969 m_selection
->ToggleCellSelection(row
, col
);
10972 bool wxGrid::IsSelection() const
10974 return ( m_selection
&& (m_selection
->IsSelection() ||
10975 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10976 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10979 bool wxGrid::IsInSelection( int row
, int col
) const
10981 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10982 ( row
>= m_selectingTopLeft
.GetRow() &&
10983 col
>= m_selectingTopLeft
.GetCol() &&
10984 row
<= m_selectingBottomRight
.GetRow() &&
10985 col
<= m_selectingBottomRight
.GetCol() )) );
10988 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10992 wxGridCellCoordsArray a
;
10996 return m_selection
->m_cellSelection
;
10999 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
11003 wxGridCellCoordsArray a
;
11007 return m_selection
->m_blockSelectionTopLeft
;
11010 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
11014 wxGridCellCoordsArray a
;
11018 return m_selection
->m_blockSelectionBottomRight
;
11021 wxArrayInt
wxGrid::GetSelectedRows() const
11029 return m_selection
->m_rowSelection
;
11032 wxArrayInt
wxGrid::GetSelectedCols() const
11040 return m_selection
->m_colSelection
;
11043 void wxGrid::ClearSelection()
11045 wxRect r1
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
);
11046 wxRect r2
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard
);
11047 m_selectingTopLeft
=
11048 m_selectingBottomRight
=
11049 m_selectingKeyboard
= wxGridNoCellCoords
;
11050 Refresh( false, &r1
);
11051 Refresh( false, &r2
);
11053 m_selection
->ClearSelection();
11056 // This function returns the rectangle that encloses the given block
11057 // in device coords clipped to the client size of the grid window.
11059 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11060 const wxGridCellCoords
& bottomRight
) const
11063 wxRect tempCellRect
= CellToRect(topLeft
);
11064 if ( tempCellRect
!= wxGridNoCellRect
)
11066 resultRect
= tempCellRect
;
11070 resultRect
= wxRect(0, 0, 0, 0);
11073 tempCellRect
= CellToRect(bottomRight
);
11074 if ( tempCellRect
!= wxGridNoCellRect
)
11076 resultRect
+= tempCellRect
;
11080 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11081 return wxGridNoCellRect
;
11084 // Ensure that left/right and top/bottom pairs are in order.
11085 int left
= resultRect
.GetLeft();
11086 int top
= resultRect
.GetTop();
11087 int right
= resultRect
.GetRight();
11088 int bottom
= resultRect
.GetBottom();
11090 int leftCol
= topLeft
.GetCol();
11091 int topRow
= topLeft
.GetRow();
11092 int rightCol
= bottomRight
.GetCol();
11093 int bottomRow
= bottomRight
.GetRow();
11102 leftCol
= rightCol
;
11113 topRow
= bottomRow
;
11117 // The following loop is ONLY necessary to detect and handle merged cells.
11119 m_gridWin
->GetClientSize( &cw
, &ch
);
11121 // Get the origin coordinates: notice that they will be negative if the
11122 // grid is scrolled downwards/to the right.
11123 int gridOriginX
= 0;
11124 int gridOriginY
= 0;
11125 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11127 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11128 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11130 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11131 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11133 // Bound our loop so that we only examine the portion of the selected block
11134 // that is shown on screen. Therefore, we compare the Top-Left block values
11135 // to the Top-Left screen values, and the Bottom-Right block values to the
11136 // Bottom-Right screen values, choosing appropriately.
11137 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11138 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11139 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11140 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11142 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11144 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11146 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11147 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11149 tempCellRect
= CellToRect( j
, i
);
11151 if (tempCellRect
.x
< left
)
11152 left
= tempCellRect
.x
;
11153 if (tempCellRect
.y
< top
)
11154 top
= tempCellRect
.y
;
11155 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11156 right
= tempCellRect
.x
+ tempCellRect
.width
;
11157 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11158 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11162 i
= visibleRightCol
; // jump over inner cells.
11167 // Convert to scrolled coords
11168 CalcScrolledPosition( left
, top
, &left
, &top
);
11169 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11171 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11172 return wxRect(0,0,0,0);
11174 resultRect
.SetLeft( wxMax(0, left
) );
11175 resultRect
.SetTop( wxMax(0, top
) );
11176 resultRect
.SetRight( wxMin(cw
, right
) );
11177 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11182 // ----------------------------------------------------------------------------
11184 // ----------------------------------------------------------------------------
11186 #if wxUSE_DRAG_AND_DROP
11188 // this allow setting drop target directly on wxGrid
11189 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11191 GetGridWindow()->SetDropTarget(dropTarget
);
11194 #endif // wxUSE_DRAG_AND_DROP
11196 // ----------------------------------------------------------------------------
11197 // grid event classes
11198 // ----------------------------------------------------------------------------
11200 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11202 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11203 int row
, int col
, int x
, int y
, bool sel
,
11204 bool control
, bool shift
, bool alt
, bool meta
)
11205 : wxNotifyEvent( type
, id
)
11212 m_control
= control
;
11217 SetEventObject(obj
);
11221 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11223 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11224 int rowOrCol
, int x
, int y
,
11225 bool control
, bool shift
, bool alt
, bool meta
)
11226 : wxNotifyEvent( type
, id
)
11228 m_rowOrCol
= rowOrCol
;
11231 m_control
= control
;
11236 SetEventObject(obj
);
11240 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11242 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11243 const wxGridCellCoords
& topLeft
,
11244 const wxGridCellCoords
& bottomRight
,
11245 bool sel
, bool control
,
11246 bool shift
, bool alt
, bool meta
)
11247 : wxNotifyEvent( type
, id
)
11249 m_topLeft
= topLeft
;
11250 m_bottomRight
= bottomRight
;
11252 m_control
= control
;
11257 SetEventObject(obj
);
11261 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11263 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11264 wxObject
* obj
, int row
,
11265 int col
, wxControl
* ctrl
)
11266 : wxCommandEvent(type
, id
)
11268 SetEventObject(obj
);
11274 #endif // wxUSE_GRID