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 ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6025 int cw
, ch
, left
, dummy
;
6026 m_gridWin
->GetClientSize( &cw
, &ch
);
6027 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6029 wxClientDC
dc( m_gridWin
);
6031 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6032 GetRowMinimalHeight(m_dragRowOrCol
) );
6033 dc
.SetLogicalFunction(wxINVERT
);
6034 if ( m_dragLastPos
>= 0 )
6036 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6038 dc
.DrawLine( left
, y
, left
+cw
, y
);
6041 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6043 int cw
, ch
, dummy
, top
;
6044 m_gridWin
->GetClientSize( &cw
, &ch
);
6045 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6047 wxClientDC
dc( m_gridWin
);
6049 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6050 GetColMinimalWidth(m_dragRowOrCol
) );
6051 dc
.SetLogicalFunction(wxINVERT
);
6052 if ( m_dragLastPos
>= 0 )
6054 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6056 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6063 m_isDragging
= false;
6064 m_startDragPos
= wxDefaultPosition
;
6066 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6067 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6070 if ( event
.Entering() || event
.Leaving() )
6072 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6073 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6078 // ------------ Left button pressed
6080 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6082 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6087 if ( !event
.CmdDown() )
6089 if ( event
.ShiftDown() )
6093 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6094 m_currentCellCoords
.GetCol(),
6097 event
.ControlDown(),
6103 else if ( XToEdgeOfCol(x
) < 0 &&
6104 YToEdgeOfRow(y
) < 0 )
6106 DisableCellEditControl();
6107 MakeCellVisible( coords
);
6109 if ( event
.CmdDown() )
6113 m_selection
->ToggleCellSelection( coords
.GetRow(),
6115 event
.ControlDown(),
6120 m_selectingTopLeft
= wxGridNoCellCoords
;
6121 m_selectingBottomRight
= wxGridNoCellCoords
;
6122 m_selectingKeyboard
= coords
;
6126 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6127 SetCurrentCell( coords
);
6130 if ( m_selection
->GetSelectionMode() !=
6131 wxGrid::wxGridSelectCells
)
6133 HighlightBlock( coords
, coords
);
6141 // ------------ Left double click
6143 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6145 DisableCellEditControl();
6147 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6149 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6154 // we want double click to select a cell and start editing
6155 // (i.e. to behave in same way as sequence of two slow clicks):
6156 m_waitForSlowClick
= true;
6161 // ------------ Left button released
6163 else if ( event
.LeftUp() )
6165 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6169 if (m_winCapture
->HasCapture())
6170 m_winCapture
->ReleaseMouse();
6171 m_winCapture
= NULL
;
6174 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6177 EnableCellEditControl();
6179 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6180 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6181 editor
->StartingClick();
6185 m_waitForSlowClick
= false;
6187 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6188 m_selectingBottomRight
!= wxGridNoCellCoords
)
6192 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6193 m_selectingTopLeft
.GetCol(),
6194 m_selectingBottomRight
.GetRow(),
6195 m_selectingBottomRight
.GetCol(),
6196 event
.ControlDown(),
6202 m_selectingTopLeft
= wxGridNoCellCoords
;
6203 m_selectingBottomRight
= wxGridNoCellCoords
;
6205 // Show the edit control, if it has been hidden for
6207 ShowCellEditControl();
6210 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6212 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6213 DoEndDragResizeRow();
6215 // Note: we are ending the event *after* doing
6216 // default processing in this case
6218 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6220 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6222 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6223 DoEndDragResizeCol();
6225 // Note: we are ending the event *after* doing
6226 // default processing in this case
6228 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6234 // ------------ Right button down
6236 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6238 DisableCellEditControl();
6239 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6244 // no default action at the moment
6248 // ------------ Right double click
6250 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6252 DisableCellEditControl();
6253 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6258 // no default action at the moment
6262 // ------------ Moving and no button action
6264 else if ( event
.Moving() && !event
.IsButton() )
6266 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6268 // out of grid cell area
6269 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6273 int dragRow
= YToEdgeOfRow( y
);
6274 int dragCol
= XToEdgeOfCol( x
);
6276 // Dragging on the corner of a cell to resize in both
6277 // directions is not implemented yet...
6279 if ( dragRow
>= 0 && dragCol
>= 0 )
6281 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6287 m_dragRowOrCol
= dragRow
;
6289 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6291 if ( CanDragRowSize() && CanDragGridSize() )
6292 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
6295 else if ( dragCol
>= 0 )
6297 m_dragRowOrCol
= dragCol
;
6299 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6301 if ( CanDragColSize() && CanDragGridSize() )
6302 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
6305 else // Neither on a row or col edge
6307 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6309 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6315 void wxGrid::DoEndDragResizeRow()
6317 if ( m_dragLastPos
>= 0 )
6319 // erase the last line and resize the row
6321 int cw
, ch
, left
, dummy
;
6322 m_gridWin
->GetClientSize( &cw
, &ch
);
6323 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6325 wxClientDC
dc( m_gridWin
);
6327 dc
.SetLogicalFunction( wxINVERT
);
6328 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6329 HideCellEditControl();
6330 SaveEditControlValue();
6332 int rowTop
= GetRowTop(m_dragRowOrCol
);
6333 SetRowSize( m_dragRowOrCol
,
6334 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6336 if ( !GetBatchCount() )
6338 // Only needed to get the correct rect.y:
6339 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6341 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6342 rect
.width
= m_rowLabelWidth
;
6343 rect
.height
= ch
- rect
.y
;
6344 m_rowLabelWin
->Refresh( true, &rect
);
6347 // if there is a multicell block, paint all of it
6350 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6351 int leftCol
= XToCol(left
);
6352 int rightCol
= internalXToCol(left
+ cw
);
6355 for (i
=leftCol
; i
<rightCol
; i
++)
6357 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6358 if (cell_rows
< subtract_rows
)
6359 subtract_rows
= cell_rows
;
6361 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6362 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6363 rect
.height
= ch
- rect
.y
;
6366 m_gridWin
->Refresh( false, &rect
);
6369 ShowCellEditControl();
6374 void wxGrid::DoEndDragResizeCol()
6376 if ( m_dragLastPos
>= 0 )
6378 // erase the last line and resize the col
6380 int cw
, ch
, dummy
, top
;
6381 m_gridWin
->GetClientSize( &cw
, &ch
);
6382 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6384 wxClientDC
dc( m_gridWin
);
6386 dc
.SetLogicalFunction( wxINVERT
);
6387 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6388 HideCellEditControl();
6389 SaveEditControlValue();
6391 int colLeft
= GetColLeft(m_dragRowOrCol
);
6392 SetColSize( m_dragRowOrCol
,
6393 wxMax( m_dragLastPos
- colLeft
,
6394 GetColMinimalWidth(m_dragRowOrCol
) ) );
6396 if ( !GetBatchCount() )
6398 // Only needed to get the correct rect.x:
6399 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6401 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6402 rect
.width
= cw
- rect
.x
;
6403 rect
.height
= m_colLabelHeight
;
6404 m_colLabelWin
->Refresh( true, &rect
);
6407 // if there is a multicell block, paint all of it
6410 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6411 int topRow
= YToRow(top
);
6412 int bottomRow
= internalYToRow(top
+ cw
);
6415 for (i
=topRow
; i
<bottomRow
; i
++)
6417 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6418 if (cell_cols
< subtract_cols
)
6419 subtract_cols
= cell_cols
;
6422 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6423 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6424 rect
.width
= cw
- rect
.x
;
6428 m_gridWin
->Refresh( false, &rect
);
6431 ShowCellEditControl();
6435 void wxGrid::DoEndDragMoveCol()
6437 //The user clicked on the column but didn't actually drag
6438 if ( m_dragLastPos
< 0 )
6440 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6445 if ( m_moveToCol
== -1 )
6446 newPos
= m_numCols
- 1;
6449 newPos
= GetColPos( m_moveToCol
);
6450 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6454 SetColPos( m_dragRowOrCol
, newPos
);
6457 void wxGrid::SetColPos( int colID
, int newPos
)
6459 if ( m_colAt
.IsEmpty() )
6461 m_colAt
.Alloc( m_numCols
);
6464 for ( i
= 0; i
< m_numCols
; i
++ )
6470 int oldPos
= GetColPos( colID
);
6472 //Reshuffle the m_colAt array
6473 if ( newPos
> oldPos
)
6476 for ( i
= oldPos
; i
< newPos
; i
++ )
6478 m_colAt
[i
] = m_colAt
[i
+1];
6484 for ( i
= oldPos
; i
> newPos
; i
-- )
6486 m_colAt
[i
] = m_colAt
[i
-1];
6490 m_colAt
[newPos
] = colID
;
6492 //Recalculate the column rights
6493 if ( !m_colWidths
.IsEmpty() )
6497 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6499 int colID
= GetColAt( colPos
);
6501 colRight
+= m_colWidths
[colID
];
6502 m_colRights
[colID
] = colRight
;
6506 m_colLabelWin
->Refresh();
6507 m_gridWin
->Refresh();
6512 void wxGrid::EnableDragColMove( bool enable
)
6514 if ( m_canDragColMove
== enable
)
6517 m_canDragColMove
= enable
;
6519 if ( !m_canDragColMove
)
6523 //Recalculate the column rights
6524 if ( !m_colWidths
.IsEmpty() )
6528 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6530 colRight
+= m_colWidths
[colPos
];
6531 m_colRights
[colPos
] = colRight
;
6535 m_colLabelWin
->Refresh();
6536 m_gridWin
->Refresh();
6542 // ------ interaction with data model
6544 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6546 switch ( msg
.GetId() )
6548 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6549 return GetModelValues();
6551 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6552 return SetModelValues();
6554 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6555 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6556 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6557 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6558 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6559 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6560 return Redimension( msg
);
6567 // The behaviour of this function depends on the grid table class
6568 // Clear() function. For the default wxGridStringTable class the
6569 // behavious is to replace all cell contents with wxEmptyString but
6570 // not to change the number of rows or cols.
6572 void wxGrid::ClearGrid()
6576 if (IsCellEditControlEnabled())
6577 DisableCellEditControl();
6580 if (!GetBatchCount())
6581 m_gridWin
->Refresh();
6585 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6587 // TODO: something with updateLabels flag
6591 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6597 if (IsCellEditControlEnabled())
6598 DisableCellEditControl();
6600 bool done
= m_table
->InsertRows( pos
, numRows
);
6603 // the table will have sent the results of the insert row
6604 // operation to this view object as a grid table message
6610 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6612 // TODO: something with updateLabels flag
6616 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6622 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6625 // the table will have sent the results of the append row
6626 // operation to this view object as a grid table message
6632 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6634 // TODO: something with updateLabels flag
6638 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6644 if (IsCellEditControlEnabled())
6645 DisableCellEditControl();
6647 bool done
= m_table
->DeleteRows( pos
, numRows
);
6649 // the table will have sent the results of the delete row
6650 // operation to this view object as a grid table message
6656 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6658 // TODO: something with updateLabels flag
6662 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6668 if (IsCellEditControlEnabled())
6669 DisableCellEditControl();
6671 bool done
= m_table
->InsertCols( pos
, numCols
);
6673 // the table will have sent the results of the insert col
6674 // operation to this view object as a grid table message
6680 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6682 // TODO: something with updateLabels flag
6686 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6692 bool done
= m_table
->AppendCols( numCols
);
6694 // the table will have sent the results of the append col
6695 // operation to this view object as a grid table message
6701 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6703 // TODO: something with updateLabels flag
6707 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6713 if (IsCellEditControlEnabled())
6714 DisableCellEditControl();
6716 bool done
= m_table
->DeleteCols( pos
, numCols
);
6718 // the table will have sent the results of the delete col
6719 // operation to this view object as a grid table message
6726 // ----- event handlers
6729 // Generate a grid event based on a mouse event and
6730 // return the result of ProcessEvent()
6732 int wxGrid::SendEvent( const wxEventType type
,
6734 wxMouseEvent
& mouseEv
)
6736 bool claimed
, vetoed
;
6738 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6740 int rowOrCol
= (row
== -1 ? col
: row
);
6742 wxGridSizeEvent
gridEvt( GetId(),
6746 mouseEv
.GetX() + GetRowLabelSize(),
6747 mouseEv
.GetY() + GetColLabelSize(),
6748 mouseEv
.ControlDown(),
6749 mouseEv
.ShiftDown(),
6751 mouseEv
.MetaDown() );
6753 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6754 vetoed
= !gridEvt
.IsAllowed();
6756 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6758 // Right now, it should _never_ end up here!
6759 wxGridRangeSelectEvent
gridEvt( GetId(),
6763 m_selectingBottomRight
,
6765 mouseEv
.ControlDown(),
6766 mouseEv
.ShiftDown(),
6768 mouseEv
.MetaDown() );
6770 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6771 vetoed
= !gridEvt
.IsAllowed();
6773 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6774 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6775 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6776 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6778 wxPoint pos
= mouseEv
.GetPosition();
6780 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6781 pos
.y
+= GetColLabelSize();
6782 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6783 pos
.x
+= GetRowLabelSize();
6785 wxGridEvent
gridEvt( GetId(),
6792 mouseEv
.ControlDown(),
6793 mouseEv
.ShiftDown(),
6795 mouseEv
.MetaDown() );
6796 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6797 vetoed
= !gridEvt
.IsAllowed();
6801 wxGridEvent
gridEvt( GetId(),
6805 mouseEv
.GetX() + GetRowLabelSize(),
6806 mouseEv
.GetY() + GetColLabelSize(),
6808 mouseEv
.ControlDown(),
6809 mouseEv
.ShiftDown(),
6811 mouseEv
.MetaDown() );
6812 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6813 vetoed
= !gridEvt
.IsAllowed();
6816 // A Veto'd event may not be `claimed' so test this first
6820 return claimed
? 1 : 0;
6823 // Generate a grid event of specified type and return the result
6824 // of ProcessEvent().
6826 int wxGrid::SendEvent( const wxEventType type
,
6829 bool claimed
, vetoed
;
6831 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6833 int rowOrCol
= (row
== -1 ? col
: row
);
6835 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6837 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6838 vetoed
= !gridEvt
.IsAllowed();
6842 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6844 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6845 vetoed
= !gridEvt
.IsAllowed();
6848 // A Veto'd event may not be `claimed' so test this first
6852 return claimed
? 1 : 0;
6855 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6857 // needed to prevent zillions of paint events on MSW
6861 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6863 // Don't do anything if between Begin/EndBatch...
6864 // EndBatch() will do all this on the last nested one anyway.
6865 if ( m_created
&& !GetBatchCount() )
6867 // Refresh to get correct scrolled position:
6868 wxScrolledWindow::Refresh(eraseb
, rect
);
6872 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6873 int width_label
, width_cell
, height_label
, height_cell
;
6876 // Copy rectangle can get scroll offsets..
6877 rect_x
= rect
->GetX();
6878 rect_y
= rect
->GetY();
6879 rectWidth
= rect
->GetWidth();
6880 rectHeight
= rect
->GetHeight();
6882 width_label
= m_rowLabelWidth
- rect_x
;
6883 if (width_label
> rectWidth
)
6884 width_label
= rectWidth
;
6886 height_label
= m_colLabelHeight
- rect_y
;
6887 if (height_label
> rectHeight
)
6888 height_label
= rectHeight
;
6890 if (rect_x
> m_rowLabelWidth
)
6892 x
= rect_x
- m_rowLabelWidth
;
6893 width_cell
= rectWidth
;
6898 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6901 if (rect_y
> m_colLabelHeight
)
6903 y
= rect_y
- m_colLabelHeight
;
6904 height_cell
= rectHeight
;
6909 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6912 // Paint corner label part intersecting rect.
6913 if ( width_label
> 0 && height_label
> 0 )
6915 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6916 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6919 // Paint col labels part intersecting rect.
6920 if ( width_cell
> 0 && height_label
> 0 )
6922 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6923 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6926 // Paint row labels part intersecting rect.
6927 if ( width_label
> 0 && height_cell
> 0 )
6929 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6930 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6933 // Paint cell area part intersecting rect.
6934 if ( width_cell
> 0 && height_cell
> 0 )
6936 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6937 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6942 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6943 m_colLabelWin
->Refresh(eraseb
, NULL
);
6944 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6945 m_gridWin
->Refresh(eraseb
, NULL
);
6950 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6952 if (m_targetWindow
!= this) // check whether initialisation has been done
6954 // update our children window positions and scrollbars
6959 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6961 if ( m_inOnKeyDown
)
6963 // shouldn't be here - we are going round in circles...
6965 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6968 m_inOnKeyDown
= true;
6970 // propagate the event up and see if it gets processed
6971 wxWindow
*parent
= GetParent();
6972 wxKeyEvent
keyEvt( event
);
6973 keyEvt
.SetEventObject( parent
);
6975 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6977 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6979 if (event
.GetKeyCode() == WXK_RIGHT
)
6980 event
.m_keyCode
= WXK_LEFT
;
6981 else if (event
.GetKeyCode() == WXK_LEFT
)
6982 event
.m_keyCode
= WXK_RIGHT
;
6985 // try local handlers
6986 switch ( event
.GetKeyCode() )
6989 if ( event
.ControlDown() )
6990 MoveCursorUpBlock( event
.ShiftDown() );
6992 MoveCursorUp( event
.ShiftDown() );
6996 if ( event
.ControlDown() )
6997 MoveCursorDownBlock( event
.ShiftDown() );
6999 MoveCursorDown( event
.ShiftDown() );
7003 if ( event
.ControlDown() )
7004 MoveCursorLeftBlock( event
.ShiftDown() );
7006 MoveCursorLeft( event
.ShiftDown() );
7010 if ( event
.ControlDown() )
7011 MoveCursorRightBlock( event
.ShiftDown() );
7013 MoveCursorRight( event
.ShiftDown() );
7017 case WXK_NUMPAD_ENTER
:
7018 if ( event
.ControlDown() )
7020 event
.Skip(); // to let the edit control have the return
7024 if ( GetGridCursorRow() < GetNumberRows()-1 )
7026 MoveCursorDown( event
.ShiftDown() );
7030 // at the bottom of a column
7031 DisableCellEditControl();
7041 if (event
.ShiftDown())
7043 if ( GetGridCursorCol() > 0 )
7045 MoveCursorLeft( false );
7050 DisableCellEditControl();
7055 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7057 MoveCursorRight( false );
7062 DisableCellEditControl();
7068 if ( event
.ControlDown() )
7070 MakeCellVisible( 0, 0 );
7071 SetCurrentCell( 0, 0 );
7080 if ( event
.ControlDown() )
7082 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7083 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7100 if ( event
.ControlDown() )
7104 m_selection
->ToggleCellSelection(
7105 m_currentCellCoords
.GetRow(),
7106 m_currentCellCoords
.GetCol(),
7107 event
.ControlDown(),
7115 if ( !IsEditable() )
7116 MoveCursorRight( false );
7127 m_inOnKeyDown
= false;
7130 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7132 // try local handlers
7134 if ( event
.GetKeyCode() == WXK_SHIFT
)
7136 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7137 m_selectingBottomRight
!= wxGridNoCellCoords
)
7141 m_selection
->SelectBlock(
7142 m_selectingTopLeft
.GetRow(),
7143 m_selectingTopLeft
.GetCol(),
7144 m_selectingBottomRight
.GetRow(),
7145 m_selectingBottomRight
.GetCol(),
7146 event
.ControlDown(),
7153 m_selectingTopLeft
= wxGridNoCellCoords
;
7154 m_selectingBottomRight
= wxGridNoCellCoords
;
7155 m_selectingKeyboard
= wxGridNoCellCoords
;
7159 void wxGrid::OnChar( wxKeyEvent
& event
)
7161 // is it possible to edit the current cell at all?
7162 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7164 // yes, now check whether the cells editor accepts the key
7165 int row
= m_currentCellCoords
.GetRow();
7166 int col
= m_currentCellCoords
.GetCol();
7167 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7168 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7170 // <F2> is special and will always start editing, for
7171 // other keys - ask the editor itself
7172 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7173 || editor
->IsAcceptedKey(event
) )
7175 // ensure cell is visble
7176 MakeCellVisible(row
, col
);
7177 EnableCellEditControl();
7179 // a problem can arise if the cell is not completely
7180 // visible (even after calling MakeCellVisible the
7181 // control is not created and calling StartingKey will
7183 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7184 editor
->StartingKey(event
);
7200 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7204 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7206 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7208 // the event has been intercepted - do nothing
7212 #if !defined(__WXMAC__)
7213 wxClientDC
dc( m_gridWin
);
7217 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7219 DisableCellEditControl();
7221 if ( IsVisible( m_currentCellCoords
, false ) )
7224 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7225 if ( !m_gridLinesEnabled
)
7233 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7235 // Otherwise refresh redraws the highlight!
7236 m_currentCellCoords
= coords
;
7238 #if defined(__WXMAC__)
7239 m_gridWin
->Refresh(true /*, & r */);
7241 DrawGridCellArea( dc
, cells
);
7242 DrawAllGridLines( dc
, r
);
7247 m_currentCellCoords
= coords
;
7249 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7250 #if !defined(__WXMAC__)
7251 DrawCellHighlight( dc
, attr
);
7256 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7259 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7263 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7266 rightCol
= GetNumberCols() - 1;
7268 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7271 bottomRow
= GetNumberRows() - 1;
7275 if ( topRow
> bottomRow
)
7282 if ( leftCol
> rightCol
)
7289 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7290 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7292 // First the case that we selected a completely new area
7293 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7294 m_selectingBottomRight
== wxGridNoCellCoords
)
7297 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7298 wxGridCellCoords ( bottomRow
, rightCol
) );
7299 m_gridWin
->Refresh( false, &rect
);
7302 // Now handle changing an existing selection area.
7303 else if ( m_selectingTopLeft
!= updateTopLeft
||
7304 m_selectingBottomRight
!= updateBottomRight
)
7306 // Compute two optimal update rectangles:
7307 // Either one rectangle is a real subset of the
7308 // other, or they are (almost) disjoint!
7310 bool need_refresh
[4];
7314 need_refresh
[3] = false;
7317 // Store intermediate values
7318 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7319 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7320 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7321 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7323 // Determine the outer/inner coordinates.
7324 if (oldLeft
> leftCol
)
7330 if (oldTop
> topRow
)
7336 if (oldRight
< rightCol
)
7339 oldRight
= rightCol
;
7342 if (oldBottom
< bottomRow
)
7345 oldBottom
= bottomRow
;
7349 // Now, either the stuff marked old is the outer
7350 // rectangle or we don't have a situation where one
7351 // is contained in the other.
7353 if ( oldLeft
< leftCol
)
7355 // Refresh the newly selected or deselected
7356 // area to the left of the old or new selection.
7357 need_refresh
[0] = true;
7358 rect
[0] = BlockToDeviceRect(
7359 wxGridCellCoords( oldTop
, oldLeft
),
7360 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7363 if ( oldTop
< topRow
)
7365 // Refresh the newly selected or deselected
7366 // area above the old or new selection.
7367 need_refresh
[1] = true;
7368 rect
[1] = BlockToDeviceRect(
7369 wxGridCellCoords( oldTop
, leftCol
),
7370 wxGridCellCoords( topRow
- 1, rightCol
) );
7373 if ( oldRight
> rightCol
)
7375 // Refresh the newly selected or deselected
7376 // area to the right of the old or new selection.
7377 need_refresh
[2] = true;
7378 rect
[2] = BlockToDeviceRect(
7379 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7380 wxGridCellCoords( oldBottom
, oldRight
) );
7383 if ( oldBottom
> bottomRow
)
7385 // Refresh the newly selected or deselected
7386 // area below the old or new selection.
7387 need_refresh
[3] = true;
7388 rect
[3] = BlockToDeviceRect(
7389 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7390 wxGridCellCoords( oldBottom
, rightCol
) );
7393 // various Refresh() calls
7394 for (i
= 0; i
< 4; i
++ )
7395 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7396 m_gridWin
->Refresh( false, &(rect
[i
]) );
7400 m_selectingTopLeft
= updateTopLeft
;
7401 m_selectingBottomRight
= updateBottomRight
;
7405 // ------ functions to get/send data (see also public functions)
7408 bool wxGrid::GetModelValues()
7410 // Hide the editor, so it won't hide a changed value.
7411 HideCellEditControl();
7415 // all we need to do is repaint the grid
7417 m_gridWin
->Refresh();
7424 bool wxGrid::SetModelValues()
7428 // Disable the editor, so it won't hide a changed value.
7429 // Do we also want to save the current value of the editor first?
7431 DisableCellEditControl();
7435 for ( row
= 0; row
< m_numRows
; row
++ )
7437 for ( col
= 0; col
< m_numCols
; col
++ )
7439 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7449 // Note - this function only draws cells that are in the list of
7450 // exposed cells (usually set from the update region by
7451 // CalcExposedCells)
7453 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7455 if ( !m_numRows
|| !m_numCols
)
7458 int i
, numCells
= cells
.GetCount();
7459 int row
, col
, cell_rows
, cell_cols
;
7460 wxGridCellCoordsArray redrawCells
;
7462 for ( i
= numCells
- 1; i
>= 0; i
-- )
7464 row
= cells
[i
].GetRow();
7465 col
= cells
[i
].GetCol();
7466 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7468 // If this cell is part of a multicell block, find owner for repaint
7469 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7471 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7472 bool marked
= false;
7473 for ( int j
= 0; j
< numCells
; j
++ )
7475 if ( cell
== cells
[j
] )
7484 int count
= redrawCells
.GetCount();
7485 for (int j
= 0; j
< count
; j
++)
7487 if ( cell
== redrawCells
[j
] )
7495 redrawCells
.Add( cell
);
7498 // don't bother drawing this cell
7502 // If this cell is empty, find cell to left that might want to overflow
7503 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7505 for ( int l
= 0; l
< cell_rows
; l
++ )
7507 // find a cell in this row to leave already marked for repaint
7509 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7510 if ((redrawCells
[k
].GetCol() < left
) &&
7511 (redrawCells
[k
].GetRow() == row
))
7513 left
= redrawCells
[k
].GetCol();
7517 left
= 0; // oh well
7519 for (int j
= col
- 1; j
>= left
; j
--)
7521 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7523 if (GetCellOverflow(row
+ l
, j
))
7525 wxGridCellCoords
cell(row
+ l
, j
);
7526 bool marked
= false;
7528 for (int k
= 0; k
< numCells
; k
++)
7530 if ( cell
== cells
[k
] )
7539 int count
= redrawCells
.GetCount();
7540 for (int k
= 0; k
< count
; k
++)
7542 if ( cell
== redrawCells
[k
] )
7549 redrawCells
.Add( cell
);
7558 DrawCell( dc
, cells
[i
] );
7561 numCells
= redrawCells
.GetCount();
7563 for ( i
= numCells
- 1; i
>= 0; i
-- )
7565 DrawCell( dc
, redrawCells
[i
] );
7569 void wxGrid::DrawGridSpace( wxDC
& dc
)
7572 m_gridWin
->GetClientSize( &cw
, &ch
);
7575 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7577 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7578 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7580 if ( right
> rightCol
|| bottom
> bottomRow
)
7583 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7585 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxBRUSHSTYLE_SOLID
) );
7586 dc
.SetPen( *wxTRANSPARENT_PEN
);
7588 if ( right
> rightCol
)
7590 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7593 if ( bottom
> bottomRow
)
7595 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7600 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7602 int row
= coords
.GetRow();
7603 int col
= coords
.GetCol();
7605 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7608 // we draw the cell border ourselves
7609 #if !WXGRID_DRAW_LINES
7610 if ( m_gridLinesEnabled
)
7611 DrawCellBorder( dc
, coords
);
7614 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7616 bool isCurrent
= coords
== m_currentCellCoords
;
7618 wxRect rect
= CellToRect( row
, col
);
7620 // if the editor is shown, we should use it and not the renderer
7621 // Note: However, only if it is really _shown_, i.e. not hidden!
7622 if ( isCurrent
&& IsCellEditControlShown() )
7624 // NB: this "#if..." is temporary and fixes a problem where the
7625 // edit control is erased by this code after being rendered.
7626 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7627 // implicitly, causing this out-of order render.
7628 #if !defined(__WXMAC__)
7629 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7630 editor
->PaintBackground(rect
, attr
);
7636 // but all the rest is drawn by the cell renderer and hence may be customized
7637 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7638 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7645 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7647 // don't show highlight when the grid doesn't have focus
7651 int row
= m_currentCellCoords
.GetRow();
7652 int col
= m_currentCellCoords
.GetCol();
7654 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7657 wxRect rect
= CellToRect(row
, col
);
7659 // hmmm... what could we do here to show that the cell is disabled?
7660 // for now, I just draw a thinner border than for the other ones, but
7661 // it doesn't look really good
7663 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7667 // The center of the drawn line is where the position/width/height of
7668 // the rectangle is actually at (on wxMSW at least), so the
7669 // size of the rectangle is reduced to compensate for the thickness of
7670 // the line. If this is too strange on non-wxMSW platforms then
7671 // please #ifdef this appropriately.
7672 rect
.x
+= penWidth
/ 2;
7673 rect
.y
+= penWidth
/ 2;
7674 rect
.width
-= penWidth
- 1;
7675 rect
.height
-= penWidth
- 1;
7677 // Now draw the rectangle
7678 // use the cellHighlightColour if the cell is inside a selection, this
7679 // will ensure the cell is always visible.
7680 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxPENSTYLE_SOLID
));
7681 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7682 dc
.DrawRectangle(rect
);
7686 // VZ: my experiments with 3D borders...
7688 // how to properly set colours for arbitrary bg?
7689 wxCoord x1
= rect
.x
,
7691 x2
= rect
.x
+ rect
.width
- 1,
7692 y2
= rect
.y
+ rect
.height
- 1;
7694 dc
.SetPen(*wxWHITE_PEN
);
7695 dc
.DrawLine(x1
, y1
, x2
, y1
);
7696 dc
.DrawLine(x1
, y1
, x1
, y2
);
7698 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7699 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7701 dc
.SetPen(*wxBLACK_PEN
);
7702 dc
.DrawLine(x1
, y2
, x2
, y2
);
7703 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7707 wxPen
wxGrid::GetDefaultGridLinePen()
7709 return wxPen(GetGridLineColour(), 1, wxPENSTYLE_SOLID
);
7712 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7714 return GetDefaultGridLinePen();
7717 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7719 return GetDefaultGridLinePen();
7722 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7724 int row
= coords
.GetRow();
7725 int col
= coords
.GetCol();
7726 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7730 wxRect rect
= CellToRect( row
, col
);
7732 // right hand border
7733 dc
.SetPen( GetColGridLinePen(col
) );
7734 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7735 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7738 dc
.SetPen( GetRowGridLinePen(row
) );
7739 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7740 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7743 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7745 // This if block was previously in wxGrid::OnPaint but that doesn't
7746 // seem to get called under wxGTK - MB
7748 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7749 m_numRows
&& m_numCols
)
7751 m_currentCellCoords
.Set(0, 0);
7754 if ( IsCellEditControlShown() )
7756 // don't show highlight when the edit control is shown
7760 // if the active cell was repainted, repaint its highlight too because it
7761 // might have been damaged by the grid lines
7762 size_t count
= cells
.GetCount();
7763 for ( size_t n
= 0; n
< count
; n
++ )
7765 if ( cells
[n
] == m_currentCellCoords
)
7767 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7768 DrawCellHighlight(dc
, attr
);
7776 // TODO: remove this ???
7777 // This is used to redraw all grid lines e.g. when the grid line colour
7780 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7782 #if !WXGRID_DRAW_LINES
7786 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7789 int top
, bottom
, left
, right
;
7791 #if 0 //#ifndef __WXGTK__
7795 m_gridWin
->GetClientSize(&cw
, &ch
);
7797 // virtual coords of visible area
7799 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7800 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7805 reg
.GetBox(x
, y
, w
, h
);
7806 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7807 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7811 m_gridWin
->GetClientSize(&cw
, &ch
);
7812 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7813 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7816 // avoid drawing grid lines past the last row and col
7818 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7819 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7821 // no gridlines inside multicells, clip them out
7822 int leftCol
= GetColPos( internalXToCol(left
) );
7823 int topRow
= internalYToRow(top
);
7824 int rightCol
= GetColPos( internalXToCol(right
) );
7825 int bottomRow
= internalYToRow(bottom
);
7827 wxRegion
clippedcells(0, 0, cw
, ch
);
7829 int i
, j
, cell_rows
, cell_cols
;
7832 for (j
=topRow
; j
<=bottomRow
; j
++)
7835 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7837 i
= GetColAt( colPos
);
7839 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7840 if ((cell_rows
> 1) || (cell_cols
> 1))
7842 rect
= CellToRect(j
,i
);
7843 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7844 clippedcells
.Subtract(rect
);
7846 else if ((cell_rows
< 0) || (cell_cols
< 0))
7848 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7849 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7850 clippedcells
.Subtract(rect
);
7855 dc
.SetDeviceClippingRegion( clippedcells
);
7858 // horizontal grid lines
7860 // already declared above - int i;
7861 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7863 int bot
= GetRowBottom(i
) - 1;
7872 dc
.SetPen( GetRowGridLinePen(i
) );
7873 dc
.DrawLine( left
, bot
, right
, bot
);
7877 // vertical grid lines
7880 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7882 i
= GetColAt( colPos
);
7884 int colRight
= GetColRight(i
);
7886 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7890 if ( colRight
> right
)
7895 if ( colRight
>= left
)
7897 dc
.SetPen( GetColGridLinePen(i
) );
7898 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7902 dc
.DestroyClippingRegion();
7905 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7911 size_t numLabels
= rows
.GetCount();
7913 for ( i
= 0; i
< numLabels
; i
++ )
7915 DrawRowLabel( dc
, rows
[i
] );
7919 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7921 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7926 int rowTop
= GetRowTop(row
),
7927 rowBottom
= GetRowBottom(row
) - 1;
7929 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
7930 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7931 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7932 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7934 dc
.SetPen( *wxWHITE_PEN
);
7935 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7936 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7938 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
7939 dc
.SetTextForeground( GetLabelTextColour() );
7940 dc
.SetFont( GetLabelFont() );
7943 GetRowLabelAlignment( &hAlign
, &vAlign
);
7946 rect
.SetY( GetRowTop(row
) + 2 );
7947 rect
.SetWidth( m_rowLabelWidth
- 4 );
7948 rect
.SetHeight( GetRowHeight(row
) - 4 );
7949 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7952 void wxGrid::SetUseNativeColLabels( bool native
)
7954 m_nativeColumnLabels
= native
;
7957 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
7958 SetColLabelSize( height
);
7961 m_colLabelWin
->Refresh();
7964 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7970 size_t numLabels
= cols
.GetCount();
7972 for ( i
= 0; i
< numLabels
; i
++ )
7974 DrawColLabel( dc
, cols
[i
] );
7978 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7980 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7983 int colLeft
= GetColLeft(col
);
7987 if (m_nativeColumnLabels
)
7989 rect
.SetX( colLeft
);
7991 rect
.SetWidth( GetColWidth(col
));
7992 rect
.SetHeight( m_colLabelHeight
);
7994 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7995 wxRendererNative::Get().DrawHeaderButton( win_dc
->GetWindow(), dc
, rect
, 0 );
7999 int colRight
= GetColRight(col
) - 1;
8001 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxPENSTYLE_SOLID
) );
8002 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
8003 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
8004 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8005 colRight
+ 1, m_colLabelHeight
- 1 );
8007 dc
.SetPen( *wxWHITE_PEN
);
8008 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8009 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8012 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
8013 dc
.SetTextForeground( GetLabelTextColour() );
8014 dc
.SetFont( GetLabelFont() );
8016 int hAlign
, vAlign
, orient
;
8017 GetColLabelAlignment( &hAlign
, &vAlign
);
8018 orient
= GetColLabelTextOrientation();
8020 rect
.SetX( colLeft
+ 2 );
8022 rect
.SetWidth( GetColWidth(col
) - 4 );
8023 rect
.SetHeight( m_colLabelHeight
- 4 );
8024 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
8027 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8028 const wxString
& value
,
8032 int textOrientation
)
8034 wxArrayString lines
;
8036 StringToLines( value
, lines
);
8038 // Forward to new API.
8039 DrawTextRectangle( dc
,
8047 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8048 // add textOrientation support
8049 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8050 const wxArrayString
& lines
,
8054 int textOrientation
)
8056 if ( lines
.empty() )
8059 wxDCClipper
clip(dc
, rect
);
8064 if ( textOrientation
== wxHORIZONTAL
)
8065 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8067 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8071 switch ( vertAlign
)
8073 case wxALIGN_BOTTOM
:
8074 if ( textOrientation
== wxHORIZONTAL
)
8075 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8077 x
= rect
.x
+ rect
.width
- textWidth
;
8080 case wxALIGN_CENTRE
:
8081 if ( textOrientation
== wxHORIZONTAL
)
8082 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8084 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8089 if ( textOrientation
== wxHORIZONTAL
)
8096 // Align each line of a multi-line label
8097 size_t nLines
= lines
.GetCount();
8098 for ( size_t l
= 0; l
< nLines
; l
++ )
8100 const wxString
& line
= lines
[l
];
8104 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8108 wxCoord lineWidth
= 0,
8110 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8112 switch ( horizAlign
)
8115 if ( textOrientation
== wxHORIZONTAL
)
8116 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8118 y
= rect
.y
+ lineWidth
+ 1;
8121 case wxALIGN_CENTRE
:
8122 if ( textOrientation
== wxHORIZONTAL
)
8123 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8125 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8130 if ( textOrientation
== wxHORIZONTAL
)
8133 y
= rect
.y
+ rect
.height
- 1;
8137 if ( textOrientation
== wxHORIZONTAL
)
8139 dc
.DrawText( line
, x
, y
);
8144 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8150 // Split multi-line text up into an array of strings.
8151 // Any existing contents of the string array are preserved.
8153 // TODO: refactor wxTextFile::Read() and reuse the same code from here
8154 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8158 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8159 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8161 while ( startPos
< (int)tVal
.length() )
8163 pos
= tVal
.Mid(startPos
).Find( eol
);
8168 else if ( pos
== 0 )
8170 lines
.Add( wxEmptyString
);
8174 lines
.Add( tVal
.Mid(startPos
, pos
) );
8177 startPos
+= pos
+ 1;
8180 if ( startPos
< (int)tVal
.length() )
8182 lines
.Add( tVal
.Mid( startPos
) );
8186 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8187 const wxArrayString
& lines
,
8188 long *width
, long *height
) const
8192 wxCoord lineW
= 0, lineH
= 0;
8195 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8197 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8198 w
= wxMax( w
, lineW
);
8207 // ------ Batch processing.
8209 void wxGrid::EndBatch()
8211 if ( m_batchCount
> 0 )
8214 if ( !m_batchCount
)
8217 m_rowLabelWin
->Refresh();
8218 m_colLabelWin
->Refresh();
8219 m_cornerLabelWin
->Refresh();
8220 m_gridWin
->Refresh();
8225 // Use this, rather than wxWindow::Refresh(), to force an immediate
8226 // repainting of the grid. Has no effect if you are already inside a
8227 // BeginBatch / EndBatch block.
8229 void wxGrid::ForceRefresh()
8235 bool wxGrid::Enable(bool enable
)
8237 if ( !wxScrolledWindow::Enable(enable
) )
8240 // redraw in the new state
8241 m_gridWin
->Refresh();
8247 // ------ Edit control functions
8250 void wxGrid::EnableEditing( bool edit
)
8252 // TODO: improve this ?
8254 if ( edit
!= m_editable
)
8257 EnableCellEditControl(edit
);
8262 void wxGrid::EnableCellEditControl( bool enable
)
8267 if ( enable
!= m_cellEditCtrlEnabled
)
8271 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8274 // this should be checked by the caller!
8275 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8277 // do it before ShowCellEditControl()
8278 m_cellEditCtrlEnabled
= enable
;
8280 ShowCellEditControl();
8284 //FIXME:add veto support
8285 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8287 HideCellEditControl();
8288 SaveEditControlValue();
8290 // do it after HideCellEditControl()
8291 m_cellEditCtrlEnabled
= enable
;
8296 bool wxGrid::IsCurrentCellReadOnly() const
8299 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8300 bool readonly
= attr
->IsReadOnly();
8306 bool wxGrid::CanEnableCellControl() const
8308 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8309 !IsCurrentCellReadOnly();
8312 bool wxGrid::IsCellEditControlEnabled() const
8314 // the cell edit control might be disable for all cells or just for the
8315 // current one if it's read only
8316 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8319 bool wxGrid::IsCellEditControlShown() const
8321 bool isShown
= false;
8323 if ( m_cellEditCtrlEnabled
)
8325 int row
= m_currentCellCoords
.GetRow();
8326 int col
= m_currentCellCoords
.GetCol();
8327 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8328 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8333 if ( editor
->IsCreated() )
8335 isShown
= editor
->GetControl()->IsShown();
8345 void wxGrid::ShowCellEditControl()
8347 if ( IsCellEditControlEnabled() )
8349 if ( !IsVisible( m_currentCellCoords
, false ) )
8351 m_cellEditCtrlEnabled
= false;
8356 wxRect rect
= CellToRect( m_currentCellCoords
);
8357 int row
= m_currentCellCoords
.GetRow();
8358 int col
= m_currentCellCoords
.GetCol();
8360 // if this is part of a multicell, find owner (topleft)
8361 int cell_rows
, cell_cols
;
8362 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8363 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8367 m_currentCellCoords
.SetRow( row
);
8368 m_currentCellCoords
.SetCol( col
);
8371 // erase the highlight and the cell contents because the editor
8372 // might not cover the entire cell
8373 wxClientDC
dc( m_gridWin
);
8375 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8376 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
8377 dc
.SetPen(*wxTRANSPARENT_PEN
);
8378 dc
.DrawRectangle(rect
);
8380 // convert to scrolled coords
8381 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8387 // cell is shifted by one pixel
8388 // However, don't allow x or y to become negative
8389 // since the SetSize() method interprets that as
8396 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8397 if ( !editor
->IsCreated() )
8399 editor
->Create(m_gridWin
, wxID_ANY
,
8400 new wxGridCellEditorEvtHandler(this, editor
));
8402 wxGridEditorCreatedEvent
evt(GetId(),
8403 wxEVT_GRID_EDITOR_CREATED
,
8407 editor
->GetControl());
8408 GetEventHandler()->ProcessEvent(evt
);
8411 // resize editor to overflow into righthand cells if allowed
8412 int maxWidth
= rect
.width
;
8413 wxString value
= GetCellValue(row
, col
);
8414 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8417 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8418 if (maxWidth
< rect
.width
)
8419 maxWidth
= rect
.width
;
8422 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8423 if (rect
.x
+ maxWidth
> client_right
)
8424 maxWidth
= client_right
- rect
.x
;
8426 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8428 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8429 // may have changed earlier
8430 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8433 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8435 // looks weird going over a multicell
8436 if (m_table
->IsEmptyCell( row
, i
) &&
8437 (rect
.width
< maxWidth
) && (c_rows
== 1))
8439 rect
.width
+= GetColWidth( i
);
8445 if (rect
.GetRight() > client_right
)
8446 rect
.SetRight( client_right
- 1 );
8449 editor
->SetCellAttr( attr
);
8450 editor
->SetSize( rect
);
8452 editor
->GetControl()->Move(
8453 editor
->GetControl()->GetPosition().x
+ nXMove
,
8454 editor
->GetControl()->GetPosition().y
);
8455 editor
->Show( true, attr
);
8457 // recalc dimensions in case we need to
8458 // expand the scrolled window to account for editor
8461 editor
->BeginEdit(row
, col
, this);
8462 editor
->SetCellAttr(NULL
);
8470 void wxGrid::HideCellEditControl()
8472 if ( IsCellEditControlEnabled() )
8474 int row
= m_currentCellCoords
.GetRow();
8475 int col
= m_currentCellCoords
.GetCol();
8477 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8478 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8479 editor
->Show( false );
8483 m_gridWin
->SetFocus();
8485 // refresh whole row to the right
8486 wxRect
rect( CellToRect(row
, col
) );
8487 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8488 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8491 // ensure that the pixels under the focus ring get refreshed as well
8492 rect
.Inflate(10, 10);
8495 m_gridWin
->Refresh( false, &rect
);
8499 void wxGrid::SaveEditControlValue()
8501 if ( IsCellEditControlEnabled() )
8503 int row
= m_currentCellCoords
.GetRow();
8504 int col
= m_currentCellCoords
.GetCol();
8506 wxString oldval
= GetCellValue(row
, col
);
8508 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8509 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8510 bool changed
= editor
->EndEdit(row
, col
, this);
8517 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8518 m_currentCellCoords
.GetRow(),
8519 m_currentCellCoords
.GetCol() ) < 0 )
8521 // Event has been vetoed, set the data back.
8522 SetCellValue(row
, col
, oldval
);
8529 // ------ Grid location functions
8530 // Note that all of these functions work with the logical coordinates of
8531 // grid cells and labels so you will need to convert from device
8532 // coordinates for mouse events etc.
8535 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8537 int row
= YToRow(y
);
8538 int col
= XToCol(x
);
8540 if ( row
== -1 || col
== -1 )
8542 coords
= wxGridNoCellCoords
;
8546 coords
.Set( row
, col
);
8550 // Internal Helper function for computing row or column from some
8551 // (unscrolled) coordinate value, using either
8552 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8553 // of m_rowBottoms/m_ColRights to speed up the search!
8555 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8556 const wxArrayInt
& BorderArray
, int nMax
,
8560 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8565 size_t i_max
= coord
/ defaultDist
,
8568 if (BorderArray
.IsEmpty())
8570 if ((int) i_max
< nMax
)
8572 return clipToMinMax
? nMax
- 1 : -1;
8575 if ( i_max
>= BorderArray
.GetCount())
8577 i_max
= BorderArray
.GetCount() - 1;
8581 if ( coord
>= BorderArray
[i_max
])
8585 i_max
= coord
/ minDist
;
8587 i_max
= BorderArray
.GetCount() - 1;
8590 if ( i_max
>= BorderArray
.GetCount())
8591 i_max
= BorderArray
.GetCount() - 1;
8594 if ( coord
>= BorderArray
[i_max
])
8595 return clipToMinMax
? (int)i_max
: -1;
8596 if ( coord
< BorderArray
[0] )
8599 while ( i_max
- i_min
> 0 )
8601 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8602 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8603 if (coord
>= BorderArray
[ i_max
- 1])
8607 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8608 if (coord
< BorderArray
[median
])
8617 int wxGrid::YToRow( int y
) const
8619 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8620 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8623 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8626 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8628 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8630 int maxPos
= x
/ m_defaultColWidth
;
8633 if (m_colRights
.IsEmpty())
8635 if(maxPos
< m_numCols
)
8636 return GetColAt( maxPos
);
8637 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8640 if ( maxPos
>= m_numCols
)
8641 maxPos
= m_numCols
- 1;
8644 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8647 if (m_minAcceptableColWidth
)
8648 maxPos
= x
/ m_minAcceptableColWidth
;
8650 maxPos
= m_numCols
- 1;
8652 if ( maxPos
>= m_numCols
)
8653 maxPos
= m_numCols
- 1;
8656 //X is beyond the last column
8657 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8658 return clipToMinMax
? GetColAt( maxPos
) : -1;
8660 //X is before the first column
8661 if ( x
< m_colRights
[GetColAt( 0 )] )
8662 return GetColAt( 0 );
8664 //Perform a binary search
8665 while ( maxPos
- minPos
> 0 )
8667 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8668 0, _T("wxGrid: internal error in XToCol"));
8670 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8671 return GetColAt( maxPos
);
8674 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8675 if (x
< m_colRights
[GetColAt( median
)])
8680 return GetColAt( maxPos
);
8683 // return the row number that that the y coord is near
8684 // the edge of, or -1 if not near an edge.
8685 // coords can only possibly be near an edge if
8686 // (a) the row/column is large enough to still allow for an "inner" area
8687 // that is _not_ nead the edge (i.e., if the height/width is smaller
8688 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8691 // (b) resizing rows/columns (the thing for which edge detection is
8692 // relevant at all) is enabled.
8694 int wxGrid::YToEdgeOfRow( int y
) const
8697 i
= internalYToRow(y
);
8699 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8701 // We know that we are in row i, test whether we are
8702 // close enough to lower or upper border, respectively.
8703 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8705 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8712 // return the col number that that the x coord is near the edge of, or
8713 // -1 if not near an edge
8714 // See comment at YToEdgeOfRow for conditions on edge detection.
8716 int wxGrid::XToEdgeOfCol( int x
) const
8719 i
= internalXToCol(x
);
8721 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8723 // We know that we are in column i; test whether we are
8724 // close enough to right or left border, respectively.
8725 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8727 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8734 wxRect
wxGrid::CellToRect( int row
, int col
) const
8736 wxRect
rect( -1, -1, -1, -1 );
8738 if ( row
>= 0 && row
< m_numRows
&&
8739 col
>= 0 && col
< m_numCols
)
8741 int i
, cell_rows
, cell_cols
;
8742 rect
.width
= rect
.height
= 0;
8743 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8744 // if negative then find multicell owner
8749 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8751 rect
.x
= GetColLeft(col
);
8752 rect
.y
= GetRowTop(row
);
8753 for (i
=col
; i
< col
+ cell_cols
; i
++)
8754 rect
.width
+= GetColWidth(i
);
8755 for (i
=row
; i
< row
+ cell_rows
; i
++)
8756 rect
.height
+= GetRowHeight(i
);
8759 // if grid lines are enabled, then the area of the cell is a bit smaller
8760 if (m_gridLinesEnabled
)
8769 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8771 // get the cell rectangle in logical coords
8773 wxRect
r( CellToRect( row
, col
) );
8775 // convert to device coords
8777 int left
, top
, right
, bottom
;
8778 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8779 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8781 // check against the client area of the grid window
8783 m_gridWin
->GetClientSize( &cw
, &ch
);
8785 if ( wholeCellVisible
)
8787 // is the cell wholly visible ?
8788 return ( left
>= 0 && right
<= cw
&&
8789 top
>= 0 && bottom
<= ch
);
8793 // is the cell partly visible ?
8795 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8796 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8800 // make the specified cell location visible by doing a minimal amount
8803 void wxGrid::MakeCellVisible( int row
, int col
)
8806 int xpos
= -1, ypos
= -1;
8808 if ( row
>= 0 && row
< m_numRows
&&
8809 col
>= 0 && col
< m_numCols
)
8811 // get the cell rectangle in logical coords
8812 wxRect
r( CellToRect( row
, col
) );
8814 // convert to device coords
8815 int left
, top
, right
, bottom
;
8816 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8817 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8820 m_gridWin
->GetClientSize( &cw
, &ch
);
8826 else if ( bottom
> ch
)
8828 int h
= r
.GetHeight();
8830 for ( i
= row
- 1; i
>= 0; i
-- )
8832 int rowHeight
= GetRowHeight(i
);
8833 if ( h
+ rowHeight
> ch
)
8840 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8841 // have rounding errors (this is important, because if we do,
8842 // we might not scroll at all and some cells won't be redrawn)
8844 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8845 // so just add a full scroll unit...
8846 ypos
+= m_scrollLineY
;
8849 // special handling for wide cells - show always left part of the cell!
8850 // Otherwise, e.g. when stepping from row to row, it would jump between
8851 // left and right part of the cell on every step!
8853 if ( left
< 0 || (right
- left
) >= cw
)
8857 else if ( right
> cw
)
8859 // position the view so that the cell is on the right
8861 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8862 xpos
= x0
+ (right
- cw
);
8864 // see comment for ypos above
8865 xpos
+= m_scrollLineX
;
8868 if ( xpos
!= -1 || ypos
!= -1 )
8871 xpos
/= m_scrollLineX
;
8873 ypos
/= m_scrollLineY
;
8874 Scroll( xpos
, ypos
);
8881 // ------ Grid cursor movement functions
8884 bool wxGrid::MoveCursorUp( bool expandSelection
)
8886 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8887 m_currentCellCoords
.GetRow() >= 0 )
8889 if ( expandSelection
)
8891 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8892 m_selectingKeyboard
= m_currentCellCoords
;
8893 if ( m_selectingKeyboard
.GetRow() > 0 )
8895 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8896 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8897 m_selectingKeyboard
.GetCol() );
8898 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8901 else if ( m_currentCellCoords
.GetRow() > 0 )
8903 int row
= m_currentCellCoords
.GetRow() - 1;
8904 int col
= m_currentCellCoords
.GetCol();
8906 MakeCellVisible( row
, col
);
8907 SetCurrentCell( row
, col
);
8918 bool wxGrid::MoveCursorDown( bool expandSelection
)
8920 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8921 m_currentCellCoords
.GetRow() < m_numRows
)
8923 if ( expandSelection
)
8925 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8926 m_selectingKeyboard
= m_currentCellCoords
;
8927 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8929 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8930 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8931 m_selectingKeyboard
.GetCol() );
8932 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8935 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8937 int row
= m_currentCellCoords
.GetRow() + 1;
8938 int col
= m_currentCellCoords
.GetCol();
8940 MakeCellVisible( row
, col
);
8941 SetCurrentCell( row
, col
);
8952 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8954 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8955 m_currentCellCoords
.GetCol() >= 0 )
8957 if ( expandSelection
)
8959 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8960 m_selectingKeyboard
= m_currentCellCoords
;
8961 if ( m_selectingKeyboard
.GetCol() > 0 )
8963 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8964 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8965 m_selectingKeyboard
.GetCol() );
8966 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8969 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8971 int row
= m_currentCellCoords
.GetRow();
8972 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8975 MakeCellVisible( row
, col
);
8976 SetCurrentCell( row
, col
);
8987 bool wxGrid::MoveCursorRight( bool expandSelection
)
8989 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8990 m_currentCellCoords
.GetCol() < m_numCols
)
8992 if ( expandSelection
)
8994 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8995 m_selectingKeyboard
= m_currentCellCoords
;
8996 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8998 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8999 MakeCellVisible( m_selectingKeyboard
.GetRow(),
9000 m_selectingKeyboard
.GetCol() );
9001 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9004 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
9006 int row
= m_currentCellCoords
.GetRow();
9007 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
9010 MakeCellVisible( row
, col
);
9011 SetCurrentCell( row
, col
);
9022 bool wxGrid::MovePageUp()
9024 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9027 int row
= m_currentCellCoords
.GetRow();
9031 m_gridWin
->GetClientSize( &cw
, &ch
);
9033 int y
= GetRowTop(row
);
9034 int newRow
= internalYToRow( y
- ch
+ 1 );
9036 if ( newRow
== row
)
9038 // row > 0, so newRow can never be less than 0 here.
9042 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9043 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9051 bool wxGrid::MovePageDown()
9053 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9056 int row
= m_currentCellCoords
.GetRow();
9057 if ( (row
+ 1) < m_numRows
)
9060 m_gridWin
->GetClientSize( &cw
, &ch
);
9062 int y
= GetRowTop(row
);
9063 int newRow
= internalYToRow( y
+ ch
);
9064 if ( newRow
== row
)
9066 // row < m_numRows, so newRow can't overflow here.
9070 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9071 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9079 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9082 m_currentCellCoords
!= wxGridNoCellCoords
&&
9083 m_currentCellCoords
.GetRow() > 0 )
9085 int row
= m_currentCellCoords
.GetRow();
9086 int col
= m_currentCellCoords
.GetCol();
9088 if ( m_table
->IsEmptyCell(row
, col
) )
9090 // starting in an empty cell: find the next block of
9096 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9100 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9102 // starting at the top of a block: find the next block
9108 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9114 // starting within a block: find the top of the block
9119 if ( m_table
->IsEmptyCell(row
, col
) )
9127 MakeCellVisible( row
, col
);
9128 if ( expandSelection
)
9130 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9131 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9136 SetCurrentCell( row
, col
);
9145 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9148 m_currentCellCoords
!= wxGridNoCellCoords
&&
9149 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9151 int row
= m_currentCellCoords
.GetRow();
9152 int col
= m_currentCellCoords
.GetCol();
9154 if ( m_table
->IsEmptyCell(row
, col
) )
9156 // starting in an empty cell: find the next block of
9159 while ( row
< m_numRows
- 1 )
9162 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9166 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9168 // starting at the bottom of a block: find the next block
9171 while ( row
< m_numRows
- 1 )
9174 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9180 // starting within a block: find the bottom of the block
9182 while ( row
< m_numRows
- 1 )
9185 if ( m_table
->IsEmptyCell(row
, col
) )
9193 MakeCellVisible( row
, col
);
9194 if ( expandSelection
)
9196 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9197 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9202 SetCurrentCell( row
, col
);
9211 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9214 m_currentCellCoords
!= wxGridNoCellCoords
&&
9215 m_currentCellCoords
.GetCol() > 0 )
9217 int row
= m_currentCellCoords
.GetRow();
9218 int col
= m_currentCellCoords
.GetCol();
9220 if ( m_table
->IsEmptyCell(row
, col
) )
9222 // starting in an empty cell: find the next block of
9228 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9232 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9234 // starting at the left of a block: find the next block
9240 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9246 // starting within a block: find the left of the block
9251 if ( m_table
->IsEmptyCell(row
, col
) )
9259 MakeCellVisible( row
, col
);
9260 if ( expandSelection
)
9262 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9263 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9268 SetCurrentCell( row
, col
);
9277 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9280 m_currentCellCoords
!= wxGridNoCellCoords
&&
9281 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9283 int row
= m_currentCellCoords
.GetRow();
9284 int col
= m_currentCellCoords
.GetCol();
9286 if ( m_table
->IsEmptyCell(row
, col
) )
9288 // starting in an empty cell: find the next block of
9291 while ( col
< m_numCols
- 1 )
9294 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9298 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9300 // starting at the right of a block: find the next block
9303 while ( col
< m_numCols
- 1 )
9306 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9312 // starting within a block: find the right of the block
9314 while ( col
< m_numCols
- 1 )
9317 if ( m_table
->IsEmptyCell(row
, col
) )
9325 MakeCellVisible( row
, col
);
9326 if ( expandSelection
)
9328 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9329 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9334 SetCurrentCell( row
, col
);
9344 // ------ Label values and formatting
9347 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9350 *horiz
= m_rowLabelHorizAlign
;
9352 *vert
= m_rowLabelVertAlign
;
9355 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9358 *horiz
= m_colLabelHorizAlign
;
9360 *vert
= m_colLabelVertAlign
;
9363 int wxGrid::GetColLabelTextOrientation() const
9365 return m_colLabelTextOrientation
;
9368 wxString
wxGrid::GetRowLabelValue( int row
) const
9372 return m_table
->GetRowLabelValue( row
);
9382 wxString
wxGrid::GetColLabelValue( int col
) const
9386 return m_table
->GetColLabelValue( col
);
9396 void wxGrid::SetRowLabelSize( int width
)
9398 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9400 if ( width
== wxGRID_AUTOSIZE
)
9402 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9405 if ( width
!= m_rowLabelWidth
)
9409 m_rowLabelWin
->Show( false );
9410 m_cornerLabelWin
->Show( false );
9412 else if ( m_rowLabelWidth
== 0 )
9414 m_rowLabelWin
->Show( true );
9415 if ( m_colLabelHeight
> 0 )
9416 m_cornerLabelWin
->Show( true );
9419 m_rowLabelWidth
= width
;
9421 wxScrolledWindow::Refresh( true );
9425 void wxGrid::SetColLabelSize( int height
)
9427 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9429 if ( height
== wxGRID_AUTOSIZE
)
9431 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9434 if ( height
!= m_colLabelHeight
)
9438 m_colLabelWin
->Show( false );
9439 m_cornerLabelWin
->Show( false );
9441 else if ( m_colLabelHeight
== 0 )
9443 m_colLabelWin
->Show( true );
9444 if ( m_rowLabelWidth
> 0 )
9445 m_cornerLabelWin
->Show( true );
9448 m_colLabelHeight
= height
;
9450 wxScrolledWindow::Refresh( true );
9454 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9456 if ( m_labelBackgroundColour
!= colour
)
9458 m_labelBackgroundColour
= colour
;
9459 m_rowLabelWin
->SetBackgroundColour( colour
);
9460 m_colLabelWin
->SetBackgroundColour( colour
);
9461 m_cornerLabelWin
->SetBackgroundColour( colour
);
9463 if ( !GetBatchCount() )
9465 m_rowLabelWin
->Refresh();
9466 m_colLabelWin
->Refresh();
9467 m_cornerLabelWin
->Refresh();
9472 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9474 if ( m_labelTextColour
!= colour
)
9476 m_labelTextColour
= colour
;
9477 if ( !GetBatchCount() )
9479 m_rowLabelWin
->Refresh();
9480 m_colLabelWin
->Refresh();
9485 void wxGrid::SetLabelFont( const wxFont
& font
)
9488 if ( !GetBatchCount() )
9490 m_rowLabelWin
->Refresh();
9491 m_colLabelWin
->Refresh();
9495 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9497 // allow old (incorrect) defs to be used
9500 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9501 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9502 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9507 case wxTOP
: vert
= wxALIGN_TOP
; break;
9508 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9509 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9512 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9514 m_rowLabelHorizAlign
= horiz
;
9517 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9519 m_rowLabelVertAlign
= vert
;
9522 if ( !GetBatchCount() )
9524 m_rowLabelWin
->Refresh();
9528 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9530 // allow old (incorrect) defs to be used
9533 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9534 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9535 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9540 case wxTOP
: vert
= wxALIGN_TOP
; break;
9541 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9542 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9545 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9547 m_colLabelHorizAlign
= horiz
;
9550 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9552 m_colLabelVertAlign
= vert
;
9555 if ( !GetBatchCount() )
9557 m_colLabelWin
->Refresh();
9561 // Note: under MSW, the default column label font must be changed because it
9562 // does not support vertical printing
9564 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9565 // pGrid->SetLabelFont(font);
9566 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9568 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9570 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9571 m_colLabelTextOrientation
= textOrientation
;
9573 if ( !GetBatchCount() )
9574 m_colLabelWin
->Refresh();
9577 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9581 m_table
->SetRowLabelValue( row
, s
);
9582 if ( !GetBatchCount() )
9584 wxRect rect
= CellToRect( row
, 0 );
9585 if ( rect
.height
> 0 )
9587 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9589 rect
.width
= m_rowLabelWidth
;
9590 m_rowLabelWin
->Refresh( true, &rect
);
9596 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9600 m_table
->SetColLabelValue( col
, s
);
9601 if ( !GetBatchCount() )
9603 wxRect rect
= CellToRect( 0, col
);
9604 if ( rect
.width
> 0 )
9606 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9608 rect
.height
= m_colLabelHeight
;
9609 m_colLabelWin
->Refresh( true, &rect
);
9615 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9617 if ( m_gridLineColour
!= colour
)
9619 m_gridLineColour
= colour
;
9621 wxClientDC
dc( m_gridWin
);
9623 DrawAllGridLines( dc
, wxRegion() );
9627 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9629 if ( m_cellHighlightColour
!= colour
)
9631 m_cellHighlightColour
= colour
;
9633 wxClientDC
dc( m_gridWin
);
9635 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9636 DrawCellHighlight(dc
, attr
);
9641 void wxGrid::SetCellHighlightPenWidth(int width
)
9643 if (m_cellHighlightPenWidth
!= width
)
9645 m_cellHighlightPenWidth
= width
;
9647 // Just redrawing the cell highlight is not enough since that won't
9648 // make any visible change if the the thickness is getting smaller.
9649 int row
= m_currentCellCoords
.GetRow();
9650 int col
= m_currentCellCoords
.GetCol();
9651 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9654 wxRect rect
= CellToRect(row
, col
);
9655 m_gridWin
->Refresh(true, &rect
);
9659 void wxGrid::SetCellHighlightROPenWidth(int width
)
9661 if (m_cellHighlightROPenWidth
!= width
)
9663 m_cellHighlightROPenWidth
= width
;
9665 // Just redrawing the cell highlight is not enough since that won't
9666 // make any visible change if the the thickness is getting smaller.
9667 int row
= m_currentCellCoords
.GetRow();
9668 int col
= m_currentCellCoords
.GetCol();
9669 if ( row
== -1 || col
== -1 ||
9670 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9673 wxRect rect
= CellToRect(row
, col
);
9674 m_gridWin
->Refresh(true, &rect
);
9678 void wxGrid::EnableGridLines( bool enable
)
9680 if ( enable
!= m_gridLinesEnabled
)
9682 m_gridLinesEnabled
= enable
;
9684 if ( !GetBatchCount() )
9688 wxClientDC
dc( m_gridWin
);
9690 DrawAllGridLines( dc
, wxRegion() );
9694 m_gridWin
->Refresh();
9700 int wxGrid::GetDefaultRowSize() const
9702 return m_defaultRowHeight
;
9705 int wxGrid::GetRowSize( int row
) const
9707 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9709 return GetRowHeight(row
);
9712 int wxGrid::GetDefaultColSize() const
9714 return m_defaultColWidth
;
9717 int wxGrid::GetColSize( int col
) const
9719 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9721 return GetColWidth(col
);
9724 // ============================================================================
9725 // access to the grid attributes: each of them has a default value in the grid
9726 // itself and may be overidden on a per-cell basis
9727 // ============================================================================
9729 // ----------------------------------------------------------------------------
9730 // setting default attributes
9731 // ----------------------------------------------------------------------------
9733 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9735 m_defaultCellAttr
->SetBackgroundColour(col
);
9737 m_gridWin
->SetBackgroundColour(col
);
9741 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9743 m_defaultCellAttr
->SetTextColour(col
);
9746 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9748 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9751 void wxGrid::SetDefaultCellOverflow( bool allow
)
9753 m_defaultCellAttr
->SetOverflow(allow
);
9756 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9758 m_defaultCellAttr
->SetFont(font
);
9761 // For editors and renderers the type registry takes precedence over the
9762 // default attr, so we need to register the new editor/renderer for the string
9763 // data type in order to make setting a default editor/renderer appear to
9766 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9768 RegisterDataType(wxGRID_VALUE_STRING
,
9770 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9773 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9775 RegisterDataType(wxGRID_VALUE_STRING
,
9776 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9780 // ----------------------------------------------------------------------------
9781 // access to the default attributes
9782 // ----------------------------------------------------------------------------
9784 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9786 return m_defaultCellAttr
->GetBackgroundColour();
9789 wxColour
wxGrid::GetDefaultCellTextColour() const
9791 return m_defaultCellAttr
->GetTextColour();
9794 wxFont
wxGrid::GetDefaultCellFont() const
9796 return m_defaultCellAttr
->GetFont();
9799 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9801 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9804 bool wxGrid::GetDefaultCellOverflow() const
9806 return m_defaultCellAttr
->GetOverflow();
9809 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9811 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9814 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9816 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9819 // ----------------------------------------------------------------------------
9820 // access to cell attributes
9821 // ----------------------------------------------------------------------------
9823 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9825 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9826 wxColour colour
= attr
->GetBackgroundColour();
9832 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9834 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9835 wxColour colour
= attr
->GetTextColour();
9841 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9843 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9844 wxFont font
= attr
->GetFont();
9850 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9852 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9853 attr
->GetAlignment(horiz
, vert
);
9857 bool wxGrid::GetCellOverflow( int row
, int col
) const
9859 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9860 bool allow
= attr
->GetOverflow();
9866 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9868 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9869 attr
->GetSize( num_rows
, num_cols
);
9873 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9875 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9876 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9882 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9884 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9885 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9891 bool wxGrid::IsReadOnly(int row
, int col
) const
9893 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9894 bool isReadOnly
= attr
->IsReadOnly();
9900 // ----------------------------------------------------------------------------
9901 // attribute support: cache, automatic provider creation, ...
9902 // ----------------------------------------------------------------------------
9904 bool wxGrid::CanHaveAttributes() const
9911 return m_table
->CanHaveAttributes();
9914 void wxGrid::ClearAttrCache()
9916 if ( m_attrCache
.row
!= -1 )
9918 wxSafeDecRef(m_attrCache
.attr
);
9919 m_attrCache
.attr
= NULL
;
9920 m_attrCache
.row
= -1;
9924 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9928 wxGrid
*self
= (wxGrid
*)this; // const_cast
9930 self
->ClearAttrCache();
9931 self
->m_attrCache
.row
= row
;
9932 self
->m_attrCache
.col
= col
;
9933 self
->m_attrCache
.attr
= attr
;
9938 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9940 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9942 *attr
= m_attrCache
.attr
;
9943 wxSafeIncRef(m_attrCache
.attr
);
9945 #ifdef DEBUG_ATTR_CACHE
9946 gs_nAttrCacheHits
++;
9953 #ifdef DEBUG_ATTR_CACHE
9954 gs_nAttrCacheMisses
++;
9961 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9963 wxGridCellAttr
*attr
= NULL
;
9964 // Additional test to avoid looking at the cache e.g. for
9965 // wxNoCellCoords, as this will confuse memory management.
9968 if ( !LookupAttr(row
, col
, &attr
) )
9970 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9971 : (wxGridCellAttr
*)NULL
;
9972 CacheAttr(row
, col
, attr
);
9978 attr
->SetDefAttr(m_defaultCellAttr
);
9982 attr
= m_defaultCellAttr
;
9989 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9991 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9992 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9994 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9995 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9997 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
10000 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10002 // artificially inc the ref count to match DecRef() in caller
10004 m_table
->SetAttr(attr
, row
, col
);
10010 // ----------------------------------------------------------------------------
10011 // setting column attributes (wrappers around SetColAttr)
10012 // ----------------------------------------------------------------------------
10014 void wxGrid::SetColFormatBool(int col
)
10016 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10019 void wxGrid::SetColFormatNumber(int col
)
10021 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10024 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10026 wxString typeName
= wxGRID_VALUE_FLOAT
;
10027 if ( (width
!= -1) || (precision
!= -1) )
10029 typeName
<< _T(':') << width
<< _T(',') << precision
;
10032 SetColFormatCustom(col
, typeName
);
10035 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10037 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10039 attr
= new wxGridCellAttr
;
10040 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10041 attr
->SetRenderer(renderer
);
10042 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
10043 attr
->SetEditor(editor
);
10045 SetColAttr(col
, attr
);
10049 // ----------------------------------------------------------------------------
10050 // setting cell attributes: this is forwarded to the table
10051 // ----------------------------------------------------------------------------
10053 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10055 if ( CanHaveAttributes() )
10057 m_table
->SetAttr(attr
, row
, col
);
10062 wxSafeDecRef(attr
);
10066 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10068 if ( CanHaveAttributes() )
10070 m_table
->SetRowAttr(attr
, row
);
10075 wxSafeDecRef(attr
);
10079 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10081 if ( CanHaveAttributes() )
10083 m_table
->SetColAttr(attr
, col
);
10088 wxSafeDecRef(attr
);
10092 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10094 if ( CanHaveAttributes() )
10096 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10097 attr
->SetBackgroundColour(colour
);
10102 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10104 if ( CanHaveAttributes() )
10106 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10107 attr
->SetTextColour(colour
);
10112 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10114 if ( CanHaveAttributes() )
10116 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10117 attr
->SetFont(font
);
10122 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10124 if ( CanHaveAttributes() )
10126 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10127 attr
->SetAlignment(horiz
, vert
);
10132 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10134 if ( CanHaveAttributes() )
10136 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10137 attr
->SetOverflow(allow
);
10142 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10144 if ( CanHaveAttributes() )
10146 int cell_rows
, cell_cols
;
10148 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10149 attr
->GetSize(&cell_rows
, &cell_cols
);
10150 attr
->SetSize(num_rows
, num_cols
);
10153 // Cannot set the size of a cell to 0 or negative values
10154 // While it is perfectly legal to do that, this function cannot
10155 // handle all the possibilies, do it by hand by getting the CellAttr.
10156 // You can only set the size of a cell to 1,1 or greater with this fn
10157 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10158 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10159 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10160 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10162 // if this was already a multicell then "turn off" the other cells first
10163 if ((cell_rows
> 1) || (cell_rows
> 1))
10166 for (j
=row
; j
< row
+ cell_rows
; j
++)
10168 for (i
=col
; i
< col
+ cell_cols
; i
++)
10170 if ((i
!= col
) || (j
!= row
))
10172 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10173 attr_stub
->SetSize( 1, 1 );
10174 attr_stub
->DecRef();
10180 // mark the cells that will be covered by this cell to
10181 // negative or zero values to point back at this cell
10182 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10185 for (j
=row
; j
< row
+ num_rows
; j
++)
10187 for (i
=col
; i
< col
+ num_cols
; i
++)
10189 if ((i
!= col
) || (j
!= row
))
10191 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10192 attr_stub
->SetSize( row
- j
, col
- i
);
10193 attr_stub
->DecRef();
10201 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10203 if ( CanHaveAttributes() )
10205 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10206 attr
->SetRenderer(renderer
);
10211 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10213 if ( CanHaveAttributes() )
10215 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10216 attr
->SetEditor(editor
);
10221 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10223 if ( CanHaveAttributes() )
10225 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10226 attr
->SetReadOnly(isReadOnly
);
10231 // ----------------------------------------------------------------------------
10232 // Data type registration
10233 // ----------------------------------------------------------------------------
10235 void wxGrid::RegisterDataType(const wxString
& typeName
,
10236 wxGridCellRenderer
* renderer
,
10237 wxGridCellEditor
* editor
)
10239 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10243 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10245 wxString typeName
= m_table
->GetTypeName(row
, col
);
10246 return GetDefaultEditorForType(typeName
);
10249 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10251 wxString typeName
= m_table
->GetTypeName(row
, col
);
10252 return GetDefaultRendererForType(typeName
);
10255 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10257 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10258 if ( index
== wxNOT_FOUND
)
10260 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10265 return m_typeRegistry
->GetEditor(index
);
10268 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10270 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10271 if ( index
== wxNOT_FOUND
)
10273 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
10278 return m_typeRegistry
->GetRenderer(index
);
10281 // ----------------------------------------------------------------------------
10283 // ----------------------------------------------------------------------------
10285 void wxGrid::EnableDragRowSize( bool enable
)
10287 m_canDragRowSize
= enable
;
10290 void wxGrid::EnableDragColSize( bool enable
)
10292 m_canDragColSize
= enable
;
10295 void wxGrid::EnableDragGridSize( bool enable
)
10297 m_canDragGridSize
= enable
;
10300 void wxGrid::EnableDragCell( bool enable
)
10302 m_canDragCell
= enable
;
10305 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10307 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10309 if ( resizeExistingRows
)
10311 // since we are resizing all rows to the default row size,
10312 // we can simply clear the row heights and row bottoms
10313 // arrays (which also allows us to take advantage of
10314 // some speed optimisations)
10315 m_rowHeights
.Empty();
10316 m_rowBottoms
.Empty();
10317 if ( !GetBatchCount() )
10322 void wxGrid::SetRowSize( int row
, int height
)
10324 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10326 // See comment in SetColSize
10327 if ( height
< GetRowMinimalAcceptableHeight())
10330 if ( m_rowHeights
.IsEmpty() )
10332 // need to really create the array
10336 int h
= wxMax( 0, height
);
10337 int diff
= h
- m_rowHeights
[row
];
10339 m_rowHeights
[row
] = h
;
10340 for ( int i
= row
; i
< m_numRows
; i
++ )
10342 m_rowBottoms
[i
] += diff
;
10345 if ( !GetBatchCount() )
10349 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10351 // we dont allow zero default column width
10352 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10354 if ( resizeExistingCols
)
10356 // since we are resizing all columns to the default column size,
10357 // we can simply clear the col widths and col rights
10358 // arrays (which also allows us to take advantage of
10359 // some speed optimisations)
10360 m_colWidths
.Empty();
10361 m_colRights
.Empty();
10362 if ( !GetBatchCount() )
10367 void wxGrid::SetColSize( int col
, int width
)
10369 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10371 // should we check that it's bigger than GetColMinimalWidth(col) here?
10373 // No, because it is reasonable to assume the library user know's
10374 // what he is doing. However we should test against the weaker
10375 // constraint of minimalAcceptableWidth, as this breaks rendering
10377 // This test then fixes sf.net bug #645734
10379 if ( width
< GetColMinimalAcceptableWidth() )
10382 if ( m_colWidths
.IsEmpty() )
10384 // need to really create the array
10388 // if < 0 then calculate new width from label
10392 wxArrayString lines
;
10393 wxClientDC
dc(m_colLabelWin
);
10394 dc
.SetFont(GetLabelFont());
10395 StringToLines(GetColLabelValue(col
), lines
);
10396 GetTextBoxSize(dc
, lines
, &w
, &h
);
10400 int w
= wxMax( 0, width
);
10401 int diff
= w
- m_colWidths
[col
];
10402 m_colWidths
[col
] = w
;
10404 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10406 m_colRights
[GetColAt(colPos
)] += diff
;
10409 if ( !GetBatchCount() )
10413 void wxGrid::SetColMinimalWidth( int col
, int width
)
10415 if (width
> GetColMinimalAcceptableWidth())
10417 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10418 m_colMinWidths
[key
] = width
;
10422 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10424 if (width
> GetRowMinimalAcceptableHeight())
10426 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10427 m_rowMinHeights
[key
] = width
;
10431 int wxGrid::GetColMinimalWidth(int col
) const
10433 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10434 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10436 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10439 int wxGrid::GetRowMinimalHeight(int row
) const
10441 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10442 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10444 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10447 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10449 // We do allow a width of 0 since this gives us
10450 // an easy way to temporarily hiding columns.
10452 m_minAcceptableColWidth
= width
;
10455 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10457 // We do allow a height of 0 since this gives us
10458 // an easy way to temporarily hiding rows.
10460 m_minAcceptableRowHeight
= height
;
10463 int wxGrid::GetColMinimalAcceptableWidth() const
10465 return m_minAcceptableColWidth
;
10468 int wxGrid::GetRowMinimalAcceptableHeight() const
10470 return m_minAcceptableRowHeight
;
10473 // ----------------------------------------------------------------------------
10475 // ----------------------------------------------------------------------------
10478 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10480 const bool column
= direction
== wxGRID_COLUMN
;
10482 wxClientDC
dc(m_gridWin
);
10484 // cancel editing of cell
10485 HideCellEditControl();
10486 SaveEditControlValue();
10488 // init both of them to avoid compiler warnings, even if we only need one
10496 wxCoord extent
, extentMax
= 0;
10497 int max
= column
? m_numRows
: m_numCols
;
10498 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10505 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10506 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10509 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10510 extent
= column
? size
.x
: size
.y
;
10511 if ( extent
> extentMax
)
10512 extentMax
= extent
;
10514 renderer
->DecRef();
10520 // now also compare with the column label extent
10522 dc
.SetFont( GetLabelFont() );
10526 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10527 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10531 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10533 extent
= column
? w
: h
;
10534 if ( extent
> extentMax
)
10535 extentMax
= extent
;
10539 // empty column - give default extent (notice that if extentMax is less
10540 // than default extent but != 0, it's OK)
10541 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10546 // leave some space around text
10554 // Ensure automatic width is not less than minimal width. See the
10555 // comment in SetColSize() for explanation of why this isn't done
10556 // in SetColSize().
10558 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
10560 SetColSize( col
, extentMax
);
10561 if ( !GetBatchCount() )
10564 m_gridWin
->GetClientSize( &cw
, &ch
);
10565 wxRect
rect ( CellToRect( 0, col
) );
10567 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10568 rect
.width
= cw
- rect
.x
;
10569 rect
.height
= m_colLabelHeight
;
10570 m_colLabelWin
->Refresh( true, &rect
);
10575 // Ensure automatic width is not less than minimal height. See the
10576 // comment in SetColSize() for explanation of why this isn't done
10577 // in SetRowSize().
10579 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
10581 SetRowSize(row
, extentMax
);
10582 if ( !GetBatchCount() )
10585 m_gridWin
->GetClientSize( &cw
, &ch
);
10586 wxRect
rect( CellToRect( row
, 0 ) );
10588 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10589 rect
.width
= m_rowLabelWidth
;
10590 rect
.height
= ch
- rect
.y
;
10591 m_rowLabelWin
->Refresh( true, &rect
);
10598 SetColMinimalWidth(col
, extentMax
);
10600 SetRowMinimalHeight(row
, extentMax
);
10604 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10606 // calculate size for the rows or columns?
10607 const bool calcRows
= direction
== wxGRID_ROW
;
10609 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10610 : GetGridColLabelWindow());
10611 dc
.SetFont(GetLabelFont());
10613 // which dimension should we take into account for calculations?
10615 // for columns, the text can be only horizontal so it's easy but for rows
10616 // we also have to take into account the text orientation
10618 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10620 wxArrayString lines
;
10621 wxCoord extentMax
= 0;
10623 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10624 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10628 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10629 : GetColLabelValue(rowOrCol
);
10630 StringToLines(label
, lines
);
10633 GetTextBoxSize(dc
, lines
, &w
, &h
);
10635 const wxCoord extent
= useWidth
? w
: h
;
10636 if ( extent
> extentMax
)
10637 extentMax
= extent
;
10642 // empty column - give default extent (notice that if extentMax is less
10643 // than default extent but != 0, it's OK)
10644 extentMax
= calcRows
? GetDefaultRowLabelSize()
10645 : GetDefaultColLabelSize();
10648 // leave some space around text (taken from AutoSizeColOrRow)
10657 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10659 int width
= m_rowLabelWidth
;
10661 wxGridUpdateLocker locker
;
10663 locker
.Create(this);
10665 for ( int col
= 0; col
< m_numCols
; col
++ )
10668 AutoSizeColumn(col
, setAsMin
);
10670 width
+= GetColWidth(col
);
10676 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10678 int height
= m_colLabelHeight
;
10680 wxGridUpdateLocker locker
;
10682 locker
.Create(this);
10684 for ( int row
= 0; row
< m_numRows
; row
++ )
10687 AutoSizeRow(row
, setAsMin
);
10689 height
+= GetRowHeight(row
);
10695 void wxGrid::AutoSize()
10697 wxGridUpdateLocker
locker(this);
10699 // we need to round up the size of the scrollable area to a multiple of
10700 // scroll step to ensure that we don't get the scrollbars when we're sized
10701 // exactly to fit our contents
10702 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10703 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10704 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10705 GetScrollY(size
.y
) * GetScrollLineY());
10707 // distribute the extra space between the columns/rows to avoid having
10708 // extra white space
10709 wxCoord diff
= sizeFit
.x
- size
.x
;
10710 if ( diff
&& m_numCols
)
10712 // try to resize the columns uniformly
10713 wxCoord diffPerCol
= diff
/ m_numCols
;
10716 for ( int col
= 0; col
< m_numCols
; col
++ )
10718 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10722 // add remaining amount to the last columns
10723 diff
-= diffPerCol
* m_numCols
;
10726 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10728 SetColSize(col
, GetColWidth(col
) + 1);
10734 diff
= sizeFit
.y
- size
.y
;
10735 if ( diff
&& m_numRows
)
10737 // try to resize the columns uniformly
10738 wxCoord diffPerRow
= diff
/ m_numRows
;
10741 for ( int row
= 0; row
< m_numRows
; row
++ )
10743 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10747 // add remaining amount to the last rows
10748 diff
-= diffPerRow
* m_numRows
;
10751 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10753 SetRowSize(row
, GetRowHeight(row
) + 1);
10758 // we know that we're not going to have scrollbars so disable them now to
10759 // avoid trouble in SetClientSize() which can otherwise set the correct
10760 // client size but also leave space for (not needed any more) scrollbars
10761 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10762 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10765 void wxGrid::AutoSizeRowLabelSize( int row
)
10767 wxArrayString lines
;
10770 // Hide the edit control, so it
10771 // won't interfere with drag-shrinking.
10772 if ( IsCellEditControlShown() )
10774 HideCellEditControl();
10775 SaveEditControlValue();
10778 // autosize row height depending on label text
10779 StringToLines( GetRowLabelValue( row
), lines
);
10780 wxClientDC
dc( m_rowLabelWin
);
10781 GetTextBoxSize( dc
, lines
, &w
, &h
);
10782 if ( h
< m_defaultRowHeight
)
10783 h
= m_defaultRowHeight
;
10784 SetRowSize(row
, h
);
10788 void wxGrid::AutoSizeColLabelSize( int col
)
10790 wxArrayString lines
;
10793 // Hide the edit control, so it
10794 // won't interfere with drag-shrinking.
10795 if ( IsCellEditControlShown() )
10797 HideCellEditControl();
10798 SaveEditControlValue();
10801 // autosize column width depending on label text
10802 StringToLines( GetColLabelValue( col
), lines
);
10803 wxClientDC
dc( m_colLabelWin
);
10804 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10805 GetTextBoxSize( dc
, lines
, &w
, &h
);
10807 GetTextBoxSize( dc
, lines
, &h
, &w
);
10808 if ( w
< m_defaultColWidth
)
10809 w
= m_defaultColWidth
;
10810 SetColSize(col
, w
);
10814 wxSize
wxGrid::DoGetBestSize() const
10816 wxGrid
*self
= (wxGrid
*)this; // const_cast
10818 // we do the same as in AutoSize() here with the exception that we don't
10819 // change the column/row sizes, only calculate them
10820 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10821 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10822 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10823 GetScrollY(size
.y
) * GetScrollLineY());
10825 // NOTE: This size should be cached, but first we need to add calls to
10826 // InvalidateBestSize everywhere that could change the results of this
10828 // CacheBestSize(size);
10830 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10831 + GetWindowBorderSize();
10839 wxPen
& wxGrid::GetDividerPen() const
10844 // ----------------------------------------------------------------------------
10845 // cell value accessor functions
10846 // ----------------------------------------------------------------------------
10848 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10852 m_table
->SetValue( row
, col
, s
);
10853 if ( !GetBatchCount() )
10856 wxRect
rect( CellToRect( row
, col
) );
10858 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10859 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10860 m_gridWin
->Refresh( false, &rect
);
10863 if ( m_currentCellCoords
.GetRow() == row
&&
10864 m_currentCellCoords
.GetCol() == col
&&
10865 IsCellEditControlShown())
10866 // Note: If we are using IsCellEditControlEnabled,
10867 // this interacts badly with calling SetCellValue from
10868 // an EVT_GRID_CELL_CHANGE handler.
10870 HideCellEditControl();
10871 ShowCellEditControl(); // will reread data from table
10876 // ----------------------------------------------------------------------------
10877 // block, row and column selection
10878 // ----------------------------------------------------------------------------
10880 void wxGrid::SelectRow( int row
, bool addToSelected
)
10882 if ( IsSelection() && !addToSelected
)
10886 m_selection
->SelectRow( row
, false, addToSelected
);
10889 void wxGrid::SelectCol( int col
, bool addToSelected
)
10891 if ( IsSelection() && !addToSelected
)
10895 m_selection
->SelectCol( col
, false, addToSelected
);
10898 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10899 bool addToSelected
)
10901 if ( IsSelection() && !addToSelected
)
10905 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10906 false, addToSelected
);
10909 void wxGrid::SelectAll()
10911 if ( m_numRows
> 0 && m_numCols
> 0 )
10914 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10918 // ----------------------------------------------------------------------------
10919 // cell, row and col deselection
10920 // ----------------------------------------------------------------------------
10922 void wxGrid::DeselectRow( int row
)
10924 if ( !m_selection
)
10927 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10929 if ( m_selection
->IsInSelection(row
, 0 ) )
10930 m_selection
->ToggleCellSelection(row
, 0);
10934 int nCols
= GetNumberCols();
10935 for ( int i
= 0; i
< nCols
; i
++ )
10937 if ( m_selection
->IsInSelection(row
, i
) )
10938 m_selection
->ToggleCellSelection(row
, i
);
10943 void wxGrid::DeselectCol( int col
)
10945 if ( !m_selection
)
10948 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10950 if ( m_selection
->IsInSelection(0, col
) )
10951 m_selection
->ToggleCellSelection(0, col
);
10955 int nRows
= GetNumberRows();
10956 for ( int i
= 0; i
< nRows
; i
++ )
10958 if ( m_selection
->IsInSelection(i
, col
) )
10959 m_selection
->ToggleCellSelection(i
, col
);
10964 void wxGrid::DeselectCell( int row
, int col
)
10966 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10967 m_selection
->ToggleCellSelection(row
, col
);
10970 bool wxGrid::IsSelection() const
10972 return ( m_selection
&& (m_selection
->IsSelection() ||
10973 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10974 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10977 bool wxGrid::IsInSelection( int row
, int col
) const
10979 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10980 ( row
>= m_selectingTopLeft
.GetRow() &&
10981 col
>= m_selectingTopLeft
.GetCol() &&
10982 row
<= m_selectingBottomRight
.GetRow() &&
10983 col
<= m_selectingBottomRight
.GetCol() )) );
10986 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10990 wxGridCellCoordsArray a
;
10994 return m_selection
->m_cellSelection
;
10997 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
11001 wxGridCellCoordsArray a
;
11005 return m_selection
->m_blockSelectionTopLeft
;
11008 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
11012 wxGridCellCoordsArray a
;
11016 return m_selection
->m_blockSelectionBottomRight
;
11019 wxArrayInt
wxGrid::GetSelectedRows() const
11027 return m_selection
->m_rowSelection
;
11030 wxArrayInt
wxGrid::GetSelectedCols() const
11038 return m_selection
->m_colSelection
;
11041 void wxGrid::ClearSelection()
11043 wxRect r1
= BlockToDeviceRect( m_selectingTopLeft
, m_selectingBottomRight
);
11044 wxRect r2
= BlockToDeviceRect( m_currentCellCoords
, m_selectingKeyboard
);
11045 m_selectingTopLeft
=
11046 m_selectingBottomRight
=
11047 m_selectingKeyboard
= wxGridNoCellCoords
;
11048 Refresh( false, &r1
);
11049 Refresh( false, &r2
);
11051 m_selection
->ClearSelection();
11054 // This function returns the rectangle that encloses the given block
11055 // in device coords clipped to the client size of the grid window.
11057 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
11058 const wxGridCellCoords
& bottomRight
) const
11061 wxRect tempCellRect
= CellToRect(topLeft
);
11062 if ( tempCellRect
!= wxGridNoCellRect
)
11064 resultRect
= tempCellRect
;
11068 resultRect
= wxRect(0, 0, 0, 0);
11071 tempCellRect
= CellToRect(bottomRight
);
11072 if ( tempCellRect
!= wxGridNoCellRect
)
11074 resultRect
+= tempCellRect
;
11078 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
11079 return wxGridNoCellRect
;
11082 // Ensure that left/right and top/bottom pairs are in order.
11083 int left
= resultRect
.GetLeft();
11084 int top
= resultRect
.GetTop();
11085 int right
= resultRect
.GetRight();
11086 int bottom
= resultRect
.GetBottom();
11088 int leftCol
= topLeft
.GetCol();
11089 int topRow
= topLeft
.GetRow();
11090 int rightCol
= bottomRight
.GetCol();
11091 int bottomRow
= bottomRight
.GetRow();
11100 leftCol
= rightCol
;
11111 topRow
= bottomRow
;
11115 // The following loop is ONLY necessary to detect and handle merged cells.
11117 m_gridWin
->GetClientSize( &cw
, &ch
);
11119 // Get the origin coordinates: notice that they will be negative if the
11120 // grid is scrolled downwards/to the right.
11121 int gridOriginX
= 0;
11122 int gridOriginY
= 0;
11123 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
11125 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
11126 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
11128 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
11129 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
11131 // Bound our loop so that we only examine the portion of the selected block
11132 // that is shown on screen. Therefore, we compare the Top-Left block values
11133 // to the Top-Left screen values, and the Bottom-Right block values to the
11134 // Bottom-Right screen values, choosing appropriately.
11135 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
11136 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
11137 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
11138 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
11140 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
11142 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
11144 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
11145 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
11147 tempCellRect
= CellToRect( j
, i
);
11149 if (tempCellRect
.x
< left
)
11150 left
= tempCellRect
.x
;
11151 if (tempCellRect
.y
< top
)
11152 top
= tempCellRect
.y
;
11153 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
11154 right
= tempCellRect
.x
+ tempCellRect
.width
;
11155 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
11156 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
11160 i
= visibleRightCol
; // jump over inner cells.
11165 // Convert to scrolled coords
11166 CalcScrolledPosition( left
, top
, &left
, &top
);
11167 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11169 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11170 return wxRect(0,0,0,0);
11172 resultRect
.SetLeft( wxMax(0, left
) );
11173 resultRect
.SetTop( wxMax(0, top
) );
11174 resultRect
.SetRight( wxMin(cw
, right
) );
11175 resultRect
.SetBottom( wxMin(ch
, bottom
) );
11180 // ----------------------------------------------------------------------------
11182 // ----------------------------------------------------------------------------
11184 #if wxUSE_DRAG_AND_DROP
11186 // this allow setting drop target directly on wxGrid
11187 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
11189 GetGridWindow()->SetDropTarget(dropTarget
);
11192 #endif // wxUSE_DRAG_AND_DROP
11194 // ----------------------------------------------------------------------------
11195 // grid event classes
11196 // ----------------------------------------------------------------------------
11198 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11200 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11201 int row
, int col
, int x
, int y
, bool sel
,
11202 bool control
, bool shift
, bool alt
, bool meta
)
11203 : wxNotifyEvent( type
, id
)
11210 m_control
= control
;
11215 SetEventObject(obj
);
11219 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11221 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11222 int rowOrCol
, int x
, int y
,
11223 bool control
, bool shift
, bool alt
, bool meta
)
11224 : wxNotifyEvent( type
, id
)
11226 m_rowOrCol
= rowOrCol
;
11229 m_control
= control
;
11234 SetEventObject(obj
);
11238 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11240 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11241 const wxGridCellCoords
& topLeft
,
11242 const wxGridCellCoords
& bottomRight
,
11243 bool sel
, bool control
,
11244 bool shift
, bool alt
, bool meta
)
11245 : wxNotifyEvent( type
, id
)
11247 m_topLeft
= topLeft
;
11248 m_bottomRight
= bottomRight
;
11250 m_control
= control
;
11255 SetEventObject(obj
);
11259 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11261 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11262 wxObject
* obj
, int row
,
11263 int col
, wxControl
* ctrl
)
11264 : wxCommandEvent(type
, id
)
11266 SetEventObject(obj
);
11272 #endif // wxUSE_GRID