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"
23 #include "wx/dcclient.h"
24 #include "wx/settings.h"
26 #include "wx/textctrl.h"
27 #include "wx/checkbox.h"
28 #include "wx/combobox.h"
29 #include "wx/valtext.h"
32 #include "wx/listbox.h"
35 #include "wx/textfile.h"
36 #include "wx/spinctrl.h"
37 #include "wx/tokenzr.h"
38 #include "wx/renderer.h"
41 #include "wx/generic/gridsel.h"
43 #if defined(__WXMOTIF__)
44 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
46 #define WXUNUSED_MOTIF(identifier) identifier
49 #if defined(__WXGTK__)
50 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
52 #define WXUNUSED_GTK(identifier) identifier
55 // Required for wxIs... functions
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
63 class WXDLLIMPEXP_ADV
);
65 struct wxGridCellWithAttr
67 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
68 : coords(row
, col
), attr(attr_
)
77 wxGridCellCoords coords
;
81 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
82 // without rewriting the macros, which require a public copy constructor.
85 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
86 class WXDLLIMPEXP_ADV
);
88 #include "wx/arrimpl.cpp"
90 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
91 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
98 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
99 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
100 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
101 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
102 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
103 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
123 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
124 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
125 const wxPoint
&pos
, const wxSize
&size
);
130 void OnPaint( wxPaintEvent
& event
);
131 void OnMouseEvent( wxMouseEvent
& event
);
132 void OnMouseWheel( wxMouseEvent
& event
);
133 void OnKeyDown( wxKeyEvent
& event
);
134 void OnKeyUp( wxKeyEvent
& );
135 void OnChar( wxKeyEvent
& );
137 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
138 DECLARE_EVENT_TABLE()
139 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
143 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
146 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
147 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
148 const wxPoint
&pos
, const wxSize
&size
);
153 void OnPaint( wxPaintEvent
& event
);
154 void OnMouseEvent( wxMouseEvent
& event
);
155 void OnMouseWheel( wxMouseEvent
& event
);
156 void OnKeyDown( wxKeyEvent
& event
);
157 void OnKeyUp( wxKeyEvent
& );
158 void OnChar( wxKeyEvent
& );
160 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
161 DECLARE_EVENT_TABLE()
162 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
166 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
169 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
170 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
171 const wxPoint
&pos
, const wxSize
&size
);
176 void OnMouseEvent( wxMouseEvent
& event
);
177 void OnMouseWheel( wxMouseEvent
& event
);
178 void OnKeyDown( wxKeyEvent
& event
);
179 void OnKeyUp( wxKeyEvent
& );
180 void OnChar( wxKeyEvent
& );
181 void OnPaint( wxPaintEvent
& event
);
183 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
184 DECLARE_EVENT_TABLE()
185 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
188 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
194 m_rowLabelWin
= NULL
;
195 m_colLabelWin
= NULL
;
198 wxGridWindow( wxGrid
*parent
,
199 wxGridRowLabelWindow
*rowLblWin
,
200 wxGridColLabelWindow
*colLblWin
,
201 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
202 virtual ~wxGridWindow() {}
204 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
206 wxGrid
* GetOwner() { return m_owner
; }
210 wxGridRowLabelWindow
*m_rowLabelWin
;
211 wxGridColLabelWindow
*m_colLabelWin
;
213 void OnPaint( wxPaintEvent
&event
);
214 void OnMouseWheel( wxMouseEvent
& event
);
215 void OnMouseEvent( wxMouseEvent
& event
);
216 void OnKeyDown( wxKeyEvent
& );
217 void OnKeyUp( wxKeyEvent
& );
218 void OnChar( wxKeyEvent
& );
219 void OnEraseBackground( wxEraseEvent
& );
220 void OnFocus( wxFocusEvent
& );
222 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
223 DECLARE_EVENT_TABLE()
224 DECLARE_NO_COPY_CLASS(wxGridWindow
)
228 class wxGridCellEditorEvtHandler
: public wxEvtHandler
231 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
238 void OnKillFocus(wxFocusEvent
& event
);
239 void OnKeyDown(wxKeyEvent
& event
);
240 void OnChar(wxKeyEvent
& event
);
242 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
246 wxGridCellEditor
*m_editor
;
248 // Work around the fact that a focus kill event can be sent to
249 // a combobox within a set focus event.
252 DECLARE_EVENT_TABLE()
253 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
254 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
258 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
260 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
261 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
262 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
263 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
267 // ----------------------------------------------------------------------------
268 // the internal data representation used by wxGridCellAttrProvider
269 // ----------------------------------------------------------------------------
271 // this class stores attributes set for cells
272 class WXDLLIMPEXP_ADV wxGridCellAttrData
275 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
276 wxGridCellAttr
*GetAttr(int row
, int col
) const;
277 void UpdateAttrRows( size_t pos
, int numRows
);
278 void UpdateAttrCols( size_t pos
, int numCols
);
281 // searches for the attr for given cell, returns wxNOT_FOUND if not found
282 int FindIndex(int row
, int col
) const;
284 wxGridCellWithAttrArray m_attrs
;
287 // this class stores attributes set for rows or columns
288 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
291 // empty ctor to suppress warnings
292 wxGridRowOrColAttrData() {}
293 ~wxGridRowOrColAttrData();
295 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
296 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
297 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
300 wxArrayInt m_rowsOrCols
;
301 wxArrayAttrs m_attrs
;
304 // NB: this is just a wrapper around 3 objects: one which stores cell
305 // attributes, and 2 others for row/col ones
306 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
309 wxGridCellAttrData m_cellAttrs
;
310 wxGridRowOrColAttrData m_rowAttrs
,
315 // ----------------------------------------------------------------------------
316 // data structures used for the data type registry
317 // ----------------------------------------------------------------------------
319 struct wxGridDataTypeInfo
321 wxGridDataTypeInfo(const wxString
& typeName
,
322 wxGridCellRenderer
* renderer
,
323 wxGridCellEditor
* editor
)
324 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
327 ~wxGridDataTypeInfo()
329 wxSafeDecRef(m_renderer
);
330 wxSafeDecRef(m_editor
);
334 wxGridCellRenderer
* m_renderer
;
335 wxGridCellEditor
* m_editor
;
337 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
341 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
342 class WXDLLIMPEXP_ADV
);
345 class WXDLLIMPEXP_ADV wxGridTypeRegistry
348 wxGridTypeRegistry() {}
349 ~wxGridTypeRegistry();
351 void RegisterDataType(const wxString
& typeName
,
352 wxGridCellRenderer
* renderer
,
353 wxGridCellEditor
* editor
);
355 // find one of already registered data types
356 int FindRegisteredDataType(const wxString
& typeName
);
358 // try to FindRegisteredDataType(), if this fails and typeName is one of
359 // standard typenames, register it and return its index
360 int FindDataType(const wxString
& typeName
);
362 // try to FindDataType(), if it fails see if it is not one of already
363 // registered data types with some params in which case clone the
364 // registered data type and set params for it
365 int FindOrCloneDataType(const wxString
& typeName
);
367 wxGridCellRenderer
* GetRenderer(int index
);
368 wxGridCellEditor
* GetEditor(int index
);
371 wxGridDataTypeInfoArray m_typeinfo
;
375 // ----------------------------------------------------------------------------
376 // conditional compilation
377 // ----------------------------------------------------------------------------
379 #ifndef WXGRID_DRAW_LINES
380 #define WXGRID_DRAW_LINES 1
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 //#define DEBUG_ATTR_CACHE
388 #ifdef DEBUG_ATTR_CACHE
389 static size_t gs_nAttrCacheHits
= 0;
390 static size_t gs_nAttrCacheMisses
= 0;
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
397 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
398 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
401 // TODO: this doesn't work at all, grid cells have different sizes and approx
402 // calculations don't work as because of the size mismatch scrollbars
403 // sometimes fail to be shown when they should be or vice versa
405 // The scroll bars may be a little flakey once in a while, but that is
406 // surely much less horrible than having scroll lines of only 1!!!
409 // Well, it's still seriously broken so it might be better but needs
412 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
413 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
415 // the size of hash tables used a bit everywhere (the max number of elements
416 // in these hash tables is the number of rows/columns)
417 static const int GRID_HASH_SIZE
= 100;
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 static inline int GetScrollX(int x
)
426 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
429 static inline int GetScrollY(int y
)
431 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
435 // ============================================================================
437 // ============================================================================
439 // ----------------------------------------------------------------------------
441 // ----------------------------------------------------------------------------
443 wxGridCellEditor::wxGridCellEditor()
449 wxGridCellEditor::~wxGridCellEditor()
454 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
455 wxWindowID
WXUNUSED(id
),
456 wxEvtHandler
* evtHandler
)
459 m_control
->PushEventHandler(evtHandler
);
462 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
463 wxGridCellAttr
*attr
)
465 // erase the background because we might not fill the cell
466 wxClientDC
dc(m_control
->GetParent());
467 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
469 gridWindow
->GetOwner()->PrepareDC(dc
);
471 dc
.SetPen(*wxTRANSPARENT_PEN
);
472 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
473 dc
.DrawRectangle(rectCell
);
475 // redraw the control we just painted over
476 m_control
->Refresh();
479 void wxGridCellEditor::Destroy()
483 m_control
->PopEventHandler( true /* delete it*/ );
485 m_control
->Destroy();
490 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
492 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
494 m_control
->Show(show
);
498 // set the colours/fonts if we have any
501 m_colFgOld
= m_control
->GetForegroundColour();
502 m_control
->SetForegroundColour(attr
->GetTextColour());
504 m_colBgOld
= m_control
->GetBackgroundColour();
505 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
507 // Workaround for GTK+1 font setting problem on some platforms
508 #if !defined(__WXGTK__) || defined(__WXGTK20__)
509 m_fontOld
= m_control
->GetFont();
510 m_control
->SetFont(attr
->GetFont());
513 // can't do anything more in the base class version, the other
514 // attributes may only be used by the derived classes
519 // restore the standard colours fonts
520 if ( m_colFgOld
.Ok() )
522 m_control
->SetForegroundColour(m_colFgOld
);
523 m_colFgOld
= wxNullColour
;
526 if ( m_colBgOld
.Ok() )
528 m_control
->SetBackgroundColour(m_colBgOld
);
529 m_colBgOld
= wxNullColour
;
532 // Workaround for GTK+1 font setting problem on some platforms
533 #if !defined(__WXGTK__) || defined(__WXGTK20__)
534 if ( m_fontOld
.Ok() )
536 m_control
->SetFont(m_fontOld
);
537 m_fontOld
= wxNullFont
;
543 void wxGridCellEditor::SetSize(const wxRect
& rect
)
545 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
547 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
550 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
555 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
557 bool ctrl
= event
.ControlDown();
558 bool alt
= event
.AltDown();
561 // On the Mac the Alt key is more like shift and is used for entry of
562 // valid characters, so check for Ctrl and Meta instead.
563 alt
= event
.MetaDown();
566 // Assume it's not a valid char if ctrl or alt is down, but if both are
567 // down then it may be because of an AltGr key combination, so let them
568 // through in that case.
569 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
576 // If it's a F-Key or other special key then it shouldn't start the
578 if (event
.GetKeyCode() >= WXK_START
)
582 // if the unicode key code is not really a unicode character (it may
583 // be a function key or etc., the platforms appear to always give us a
584 // small value in this case) then fallback to the ASCII key code but
585 // don't do anything for function keys or etc.
586 key
= event
.GetUnicodeKey();
589 key
= event
.GetKeyCode();
590 keyOk
= (key
<= 127);
593 key
= event
.GetKeyCode();
594 keyOk
= (key
<= 255);
600 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
605 void wxGridCellEditor::StartingClick()
611 // ----------------------------------------------------------------------------
612 // wxGridCellTextEditor
613 // ----------------------------------------------------------------------------
615 wxGridCellTextEditor::wxGridCellTextEditor()
620 void wxGridCellTextEditor::Create(wxWindow
* parent
,
622 wxEvtHandler
* evtHandler
)
624 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
625 wxDefaultPosition
, wxDefaultSize
626 #if defined(__WXMSW__)
627 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
| wxNO_BORDER
631 // set max length allowed in the textctrl, if the parameter was set
634 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
637 wxGridCellEditor::Create(parent
, id
, evtHandler
);
640 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
641 wxGridCellAttr
* WXUNUSED(attr
))
643 // as we fill the entire client area,
644 // don't do anything here to minimize flicker
647 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
649 wxRect
rect(rectOrig
);
651 // Make the edit control large enough to allow for internal margins
653 // TODO: remove this if the text ctrl sizing is improved esp. for unix
655 #if defined(__WXGTK__)
663 #elif defined(__WXMSW__)
677 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
678 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
680 #if defined(__WXMOTIF__)
685 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
686 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
687 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
688 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
691 wxGridCellEditor::SetSize(rect
);
694 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
696 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
698 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
700 DoBeginEdit(m_startValue
);
703 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
705 Text()->SetValue(startValue
);
706 Text()->SetInsertionPointEnd();
707 Text()->SetSelection(-1, -1);
711 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
713 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
715 bool changed
= false;
716 wxString value
= Text()->GetValue();
717 if (value
!= m_startValue
)
721 grid
->GetTable()->SetValue(row
, col
, value
);
723 m_startValue
= wxEmptyString
;
725 // No point in setting the text of the hidden control
726 //Text()->SetValue(m_startValue);
731 void wxGridCellTextEditor::Reset()
733 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
735 DoReset(m_startValue
);
738 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
740 Text()->SetValue(startValue
);
741 Text()->SetInsertionPointEnd();
744 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
746 return wxGridCellEditor::IsAcceptedKey(event
);
749 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
751 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
752 // longer an appropriate way to get the character into the text control.
753 // Do it ourselves instead. We know that if we get this far that we have
754 // a valid character, so not a whole lot of testing needs to be done.
756 wxTextCtrl
* tc
= Text();
761 ch
= event
.GetUnicodeKey();
763 ch
= (wxChar
)event
.GetKeyCode();
765 ch
= (wxChar
)event
.GetKeyCode();
771 // delete the character at the cursor
772 pos
= tc
->GetInsertionPoint();
773 if (pos
< tc
->GetLastPosition())
774 tc
->Remove(pos
, pos
+ 1);
778 // delete the character before the cursor
779 pos
= tc
->GetInsertionPoint();
781 tc
->Remove(pos
- 1, pos
);
790 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
791 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
793 #if defined(__WXMOTIF__) || defined(__WXGTK__)
794 // wxMotif needs a little extra help...
795 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
796 wxString
s( Text()->GetValue() );
797 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
799 Text()->SetInsertionPoint( pos
);
801 // the other ports can handle a Return key press
807 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
817 if ( params
.ToLong(&tmp
) )
819 m_maxChars
= (size_t)tmp
;
823 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
828 // return the value in the text control
829 wxString
wxGridCellTextEditor::GetValue() const
831 return Text()->GetValue();
834 // ----------------------------------------------------------------------------
835 // wxGridCellNumberEditor
836 // ----------------------------------------------------------------------------
838 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
844 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
846 wxEvtHandler
* evtHandler
)
851 // create a spin ctrl
852 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
853 wxDefaultPosition
, wxDefaultSize
,
857 wxGridCellEditor::Create(parent
, id
, evtHandler
);
862 // just a text control
863 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
866 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
871 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
873 // first get the value
874 wxGridTableBase
*table
= grid
->GetTable();
875 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
877 m_valueOld
= table
->GetValueAsLong(row
, col
);
882 wxString sValue
= table
->GetValue(row
, col
);
883 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
885 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
893 Spin()->SetValue((int)m_valueOld
);
899 DoBeginEdit(GetString());
903 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
913 value
= Spin()->GetValue();
914 changed
= value
!= m_valueOld
;
916 text
= wxString::Format(wxT("%ld"), value
);
921 text
= Text()->GetValue();
922 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
927 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
928 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
930 grid
->GetTable()->SetValue(row
, col
, text
);
936 void wxGridCellNumberEditor::Reset()
941 Spin()->SetValue((int)m_valueOld
);
946 DoReset(GetString());
950 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
952 if ( wxGridCellEditor::IsAcceptedKey(event
) )
954 int keycode
= event
.GetKeyCode();
955 if ( (keycode
< 128) &&
956 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
965 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
967 int keycode
= event
.GetKeyCode();
970 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
972 wxGridCellTextEditor::StartingKey(event
);
981 if ( wxIsdigit(keycode
) )
983 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
984 spin
->SetValue(keycode
- '0');
985 spin
->SetSelection(1,1);
994 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1005 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1009 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1013 // skip the error message below
1018 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1022 // return the value in the spin control if it is there (the text control otherwise)
1023 wxString
wxGridCellNumberEditor::GetValue() const
1030 long value
= Spin()->GetValue();
1031 s
.Printf(wxT("%ld"), value
);
1036 s
= Text()->GetValue();
1042 // ----------------------------------------------------------------------------
1043 // wxGridCellFloatEditor
1044 // ----------------------------------------------------------------------------
1046 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1049 m_precision
= precision
;
1052 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1054 wxEvtHandler
* evtHandler
)
1056 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1058 #if wxUSE_VALIDATORS
1059 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1063 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1065 // first get the value
1066 wxGridTableBase
*table
= grid
->GetTable();
1067 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1069 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1074 wxString sValue
= table
->GetValue(row
, col
);
1075 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1077 wxFAIL_MSG( _T("this cell doesn't have float value") );
1082 DoBeginEdit(GetString());
1085 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1089 wxString
text(Text()->GetValue());
1091 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1092 !wxIsSameDouble(value
, m_valueOld
) )
1094 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1095 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1097 grid
->GetTable()->SetValue(row
, col
, text
);
1105 void wxGridCellFloatEditor::Reset()
1107 DoReset(GetString());
1110 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1112 int keycode
= event
.GetKeyCode();
1114 tmpbuf
[0] = (char) keycode
;
1116 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1119 bool is_decimal_point
= ( strbuf
==
1120 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1122 bool is_decimal_point
= ( strbuf
== _T(".") );
1125 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1126 || is_decimal_point
)
1128 wxGridCellTextEditor::StartingKey(event
);
1130 // skip Skip() below
1137 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1148 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1152 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1154 m_precision
= (int)tmp
;
1156 // skip the error message below
1161 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1165 wxString
wxGridCellFloatEditor::GetString() const
1168 if ( m_precision
== -1 && m_width
!= -1)
1170 // default precision
1171 fmt
.Printf(_T("%%%d.f"), m_width
);
1173 else if ( m_precision
!= -1 && m_width
== -1)
1176 fmt
.Printf(_T("%%.%df"), m_precision
);
1178 else if ( m_precision
!= -1 && m_width
!= -1 )
1180 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1184 // default width/precision
1188 return wxString::Format(fmt
, m_valueOld
);
1191 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1193 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1195 const int keycode
= event
.GetKeyCode();
1196 if ( isascii(keycode
) )
1199 tmpbuf
[0] = (char) keycode
;
1201 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1204 const wxString decimalPoint
=
1205 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1207 const wxString
decimalPoint(_T('.'));
1210 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1211 if ( wxIsdigit(keycode
) ||
1212 tolower(keycode
) == 'e' ||
1213 keycode
== decimalPoint
||
1225 #endif // wxUSE_TEXTCTRL
1229 // ----------------------------------------------------------------------------
1230 // wxGridCellBoolEditor
1231 // ----------------------------------------------------------------------------
1233 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1235 wxEvtHandler
* evtHandler
)
1237 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1238 wxDefaultPosition
, wxDefaultSize
,
1241 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1244 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1246 bool resize
= false;
1247 wxSize size
= m_control
->GetSize();
1248 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1250 // check if the checkbox is not too big/small for this cell
1251 wxSize sizeBest
= m_control
->GetBestSize();
1252 if ( !(size
== sizeBest
) )
1254 // reset to default size if it had been made smaller
1260 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1262 // leave 1 pixel margin
1263 size
.x
= size
.y
= minSize
- 2;
1270 m_control
->SetSize(size
);
1273 // position it in the centre of the rectangle (TODO: support alignment?)
1275 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1276 // the checkbox without label still has some space to the right in wxGTK,
1277 // so shift it to the right
1279 #elif defined(__WXMSW__)
1280 // here too, but in other way
1285 int hAlign
= wxALIGN_CENTRE
;
1286 int vAlign
= wxALIGN_CENTRE
;
1288 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1291 if (hAlign
== wxALIGN_LEFT
)
1299 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1301 else if (hAlign
== wxALIGN_RIGHT
)
1303 x
= r
.x
+ r
.width
- size
.x
- 2;
1304 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1306 else if (hAlign
== wxALIGN_CENTRE
)
1308 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1309 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1312 m_control
->Move(x
, y
);
1315 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1317 m_control
->Show(show
);
1321 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1322 CBox()->SetBackgroundColour(colBg
);
1326 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1328 wxASSERT_MSG(m_control
,
1329 wxT("The wxGridCellEditor must be created first!"));
1331 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1333 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1337 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1338 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1341 CBox()->SetValue(m_startValue
);
1345 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1348 wxASSERT_MSG(m_control
,
1349 wxT("The wxGridCellEditor must be created first!"));
1351 bool changed
= false;
1352 bool value
= CBox()->GetValue();
1353 if ( value
!= m_startValue
)
1358 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1359 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1361 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1367 void wxGridCellBoolEditor::Reset()
1369 wxASSERT_MSG(m_control
,
1370 wxT("The wxGridCellEditor must be created first!"));
1372 CBox()->SetValue(m_startValue
);
1375 void wxGridCellBoolEditor::StartingClick()
1377 CBox()->SetValue(!CBox()->GetValue());
1380 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1382 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1384 int keycode
= event
.GetKeyCode();
1397 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1399 int keycode
= event
.GetKeyCode();
1403 CBox()->SetValue(!CBox()->GetValue());
1407 CBox()->SetValue(true);
1411 CBox()->SetValue(false);
1417 // return the value as "1" for true and the empty string for false
1418 wxString
wxGridCellBoolEditor::GetValue() const
1420 bool bSet
= CBox()->GetValue();
1421 return bSet
? _T("1") : wxEmptyString
;
1424 #endif // wxUSE_CHECKBOX
1428 // ----------------------------------------------------------------------------
1429 // wxGridCellChoiceEditor
1430 // ----------------------------------------------------------------------------
1432 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1434 : m_choices(choices
),
1435 m_allowOthers(allowOthers
) { }
1437 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1438 const wxString choices
[],
1440 : m_allowOthers(allowOthers
)
1444 m_choices
.Alloc(count
);
1445 for ( size_t n
= 0; n
< count
; n
++ )
1447 m_choices
.Add(choices
[n
]);
1452 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1454 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1455 editor
->m_allowOthers
= m_allowOthers
;
1456 editor
->m_choices
= m_choices
;
1461 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1463 wxEvtHandler
* evtHandler
)
1465 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1466 wxDefaultPosition
, wxDefaultSize
,
1468 m_allowOthers
? 0 : wxCB_READONLY
);
1470 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1473 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1474 wxGridCellAttr
* attr
)
1476 // as we fill the entire client area, don't do anything here to minimize
1479 // TODO: It doesn't actually fill the client area since the height of a
1480 // combo always defaults to the standard. Until someone has time to
1481 // figure out the right rectangle to paint, just do it the normal way.
1482 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1485 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1487 wxASSERT_MSG(m_control
,
1488 wxT("The wxGridCellEditor must be created first!"));
1490 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1492 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1494 // Don't immediately end if we get a kill focus event within BeginEdit
1496 evtHandler
->SetInSetFocus(true);
1498 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1502 Combo()->SetValue(m_startValue
);
1506 // find the right position, or default to the first if not found
1507 int pos
= Combo()->FindString(m_startValue
);
1508 if (pos
== wxNOT_FOUND
)
1510 Combo()->SetSelection(pos
);
1513 Combo()->SetInsertionPointEnd();
1514 Combo()->SetFocus();
1518 // When dropping down the menu, a kill focus event
1519 // happens after this point, so we can't reset the flag yet.
1520 #if !defined(__WXGTK20__)
1521 evtHandler
->SetInSetFocus(false);
1526 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1529 wxString value
= Combo()->GetValue();
1530 if ( value
== m_startValue
)
1533 grid
->GetTable()->SetValue(row
, col
, value
);
1538 void wxGridCellChoiceEditor::Reset()
1540 Combo()->SetValue(m_startValue
);
1541 Combo()->SetInsertionPointEnd();
1544 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1554 wxStringTokenizer
tk(params
, _T(','));
1555 while ( tk
.HasMoreTokens() )
1557 m_choices
.Add(tk
.GetNextToken());
1561 // return the value in the text control
1562 wxString
wxGridCellChoiceEditor::GetValue() const
1564 return Combo()->GetValue();
1567 #endif // wxUSE_COMBOBOX
1569 // ----------------------------------------------------------------------------
1570 // wxGridCellEditorEvtHandler
1571 // ----------------------------------------------------------------------------
1573 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1575 // Don't disable the cell if we're just starting to edit it
1580 m_grid
->DisableCellEditControl();
1585 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1587 switch ( event
.GetKeyCode() )
1591 m_grid
->DisableCellEditControl();
1595 m_grid
->GetEventHandler()->ProcessEvent( event
);
1599 case WXK_NUMPAD_ENTER
:
1600 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1601 m_editor
->HandleReturn(event
);
1610 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1612 int row
= m_grid
->GetGridCursorRow();
1613 int col
= m_grid
->GetGridCursorCol();
1614 wxRect rect
= m_grid
->CellToRect( row
, col
);
1616 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1618 // if cell width is smaller than grid client area, cell is wholly visible
1619 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1621 switch ( event
.GetKeyCode() )
1626 case WXK_NUMPAD_ENTER
:
1631 if ( wholeCellVisible
)
1633 // no special processing needed...
1638 // do special processing for partly visible cell...
1640 // get the widths of all cells previous to this one
1642 for ( int i
= 0; i
< col
; i
++ )
1644 colXPos
+= m_grid
->GetColSize(i
);
1647 int xUnit
= 1, yUnit
= 1;
1648 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1651 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1655 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1663 if ( wholeCellVisible
)
1665 // no special processing needed...
1670 // do special processing for partly visible cell...
1673 wxString value
= m_grid
->GetCellValue(row
, col
);
1674 if ( wxEmptyString
!= value
)
1676 // get width of cell CONTENTS (text)
1678 wxFont font
= m_grid
->GetCellFont(row
, col
);
1679 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1681 // try to RIGHT align the text by scrolling
1682 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1684 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1685 // otherwise the last part of the cell content might be hidden below the scroll bar
1686 // FIXME: maybe there is a more suitable correction?
1687 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1688 if ( textWidth
< 0 )
1694 // get the widths of all cells previous to this one
1696 for ( int i
= 0; i
< col
; i
++ )
1698 colXPos
+= m_grid
->GetColSize(i
);
1701 // and add the (modified) text width of the cell contents
1702 // as we'd like to see the last part of the cell contents
1703 colXPos
+= textWidth
;
1705 int xUnit
= 1, yUnit
= 1;
1706 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1707 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1718 // ----------------------------------------------------------------------------
1719 // wxGridCellWorker is an (almost) empty common base class for
1720 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1721 // ----------------------------------------------------------------------------
1723 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1728 wxGridCellWorker::~wxGridCellWorker()
1732 // ============================================================================
1734 // ============================================================================
1736 // ----------------------------------------------------------------------------
1737 // wxGridCellRenderer
1738 // ----------------------------------------------------------------------------
1740 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1741 wxGridCellAttr
& attr
,
1744 int WXUNUSED(row
), int WXUNUSED(col
),
1747 dc
.SetBackgroundMode( wxSOLID
);
1749 // grey out fields if the grid is disabled
1750 if ( grid
.IsEnabled() )
1754 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1758 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1763 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1766 dc
.SetPen( *wxTRANSPARENT_PEN
);
1767 dc
.DrawRectangle(rect
);
1770 // ----------------------------------------------------------------------------
1771 // wxGridCellStringRenderer
1772 // ----------------------------------------------------------------------------
1774 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1775 const wxGridCellAttr
& attr
,
1779 dc
.SetBackgroundMode( wxTRANSPARENT
);
1781 // TODO some special colours for attr.IsReadOnly() case?
1783 // different coloured text when the grid is disabled
1784 if ( grid
.IsEnabled() )
1788 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1789 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1793 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1794 dc
.SetTextForeground( attr
.GetTextColour() );
1799 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1800 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1803 dc
.SetFont( attr
.GetFont() );
1806 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1808 const wxString
& text
)
1810 wxCoord x
= 0, y
= 0, max_x
= 0;
1811 dc
.SetFont(attr
.GetFont());
1812 wxStringTokenizer
tk(text
, _T('\n'));
1813 while ( tk
.HasMoreTokens() )
1815 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1816 max_x
= wxMax(max_x
, x
);
1819 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1821 return wxSize(max_x
, y
);
1824 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1825 wxGridCellAttr
& attr
,
1829 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1832 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1833 wxGridCellAttr
& attr
,
1835 const wxRect
& rectCell
,
1839 wxRect rect
= rectCell
;
1842 // erase only this cells background, overflow cells should have been erased
1843 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1846 attr
.GetAlignment(&hAlign
, &vAlign
);
1848 int overflowCols
= 0;
1850 if (attr
.GetOverflow())
1852 int cols
= grid
.GetNumberCols();
1853 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1854 int cell_rows
, cell_cols
;
1855 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1856 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1858 int i
, c_cols
, c_rows
;
1859 for (i
= col
+cell_cols
; i
< cols
; i
++)
1861 bool is_empty
= true;
1862 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1864 // check w/ anchor cell for multicell block
1865 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1868 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1877 rect
.width
+= grid
.GetColSize(i
);
1885 if (rect
.width
>= best_width
)
1889 overflowCols
= i
- col
- cell_cols
+ 1;
1890 if (overflowCols
>= cols
)
1891 overflowCols
= cols
- 1;
1894 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1896 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1898 clip
.x
+= rectCell
.width
;
1899 // draw each overflow cell individually
1900 int col_end
= col
+ cell_cols
+ overflowCols
;
1901 if (col_end
>= grid
.GetNumberCols())
1902 col_end
= grid
.GetNumberCols() - 1;
1903 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1905 clip
.width
= grid
.GetColSize(i
) - 1;
1906 dc
.DestroyClippingRegion();
1907 dc
.SetClippingRegion(clip
);
1909 SetTextColoursAndFont(grid
, attr
, dc
,
1910 grid
.IsInSelection(row
,i
));
1912 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1913 rect
, hAlign
, vAlign
);
1914 clip
.x
+= grid
.GetColSize(i
) - 1;
1920 dc
.DestroyClippingRegion();
1924 // now we only have to draw the text
1925 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1927 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1928 rect
, hAlign
, vAlign
);
1931 // ----------------------------------------------------------------------------
1932 // wxGridCellNumberRenderer
1933 // ----------------------------------------------------------------------------
1935 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1937 wxGridTableBase
*table
= grid
.GetTable();
1939 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1941 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1945 text
= table
->GetValue(row
, col
);
1951 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1952 wxGridCellAttr
& attr
,
1954 const wxRect
& rectCell
,
1958 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1960 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1962 // draw the text right aligned by default
1964 attr
.GetAlignment(&hAlign
, &vAlign
);
1965 hAlign
= wxALIGN_RIGHT
;
1967 wxRect rect
= rectCell
;
1970 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1973 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1974 wxGridCellAttr
& attr
,
1978 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1981 // ----------------------------------------------------------------------------
1982 // wxGridCellFloatRenderer
1983 // ----------------------------------------------------------------------------
1985 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1988 SetPrecision(precision
);
1991 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1993 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1994 renderer
->m_width
= m_width
;
1995 renderer
->m_precision
= m_precision
;
1996 renderer
->m_format
= m_format
;
2001 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2003 wxGridTableBase
*table
= grid
.GetTable();
2008 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2010 val
= table
->GetValueAsDouble(row
, col
);
2015 text
= table
->GetValue(row
, col
);
2016 hasDouble
= text
.ToDouble(&val
);
2023 if ( m_width
== -1 )
2025 if ( m_precision
== -1 )
2027 // default width/precision
2028 m_format
= _T("%f");
2032 m_format
.Printf(_T("%%.%df"), m_precision
);
2035 else if ( m_precision
== -1 )
2037 // default precision
2038 m_format
.Printf(_T("%%%d.f"), m_width
);
2042 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2046 text
.Printf(m_format
, val
);
2049 //else: text already contains the string
2054 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2055 wxGridCellAttr
& attr
,
2057 const wxRect
& rectCell
,
2061 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2063 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2065 // draw the text right aligned by default
2067 attr
.GetAlignment(&hAlign
, &vAlign
);
2068 hAlign
= wxALIGN_RIGHT
;
2070 wxRect rect
= rectCell
;
2073 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2076 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2077 wxGridCellAttr
& attr
,
2081 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2084 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2088 // reset to defaults
2094 wxString tmp
= params
.BeforeFirst(_T(','));
2098 if ( tmp
.ToLong(&width
) )
2100 SetWidth((int)width
);
2104 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2108 tmp
= params
.AfterFirst(_T(','));
2112 if ( tmp
.ToLong(&precision
) )
2114 SetPrecision((int)precision
);
2118 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2124 // ----------------------------------------------------------------------------
2125 // wxGridCellBoolRenderer
2126 // ----------------------------------------------------------------------------
2128 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2130 // FIXME these checkbox size calculations are really ugly...
2132 // between checkmark and box
2133 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2135 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2136 wxGridCellAttr
& WXUNUSED(attr
),
2141 // compute it only once (no locks for MT safeness in GUI thread...)
2142 if ( !ms_sizeCheckMark
.x
)
2144 // get checkbox size
2145 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2146 wxSize size
= checkbox
->GetBestSize();
2147 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2149 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2150 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2151 checkSize
-= size
.y
/ 2;
2156 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2159 return ms_sizeCheckMark
;
2162 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2163 wxGridCellAttr
& attr
,
2169 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2171 // draw a check mark in the centre (ignoring alignment - TODO)
2172 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2174 // don't draw outside the cell
2175 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2176 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2178 // and even leave (at least) 1 pixel margin
2179 size
.x
= size
.y
= minSize
- 2;
2182 // draw a border around checkmark
2184 attr
.GetAlignment(&hAlign
, &vAlign
);
2187 if (hAlign
== wxALIGN_CENTRE
)
2189 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2190 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2191 rectBorder
.width
= size
.x
;
2192 rectBorder
.height
= size
.y
;
2194 else if (hAlign
== wxALIGN_LEFT
)
2196 rectBorder
.x
= rect
.x
+ 2;
2197 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2198 rectBorder
.width
= size
.x
;
2199 rectBorder
.height
= size
.y
;
2201 else if (hAlign
== wxALIGN_RIGHT
)
2203 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2204 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2205 rectBorder
.width
= size
.x
;
2206 rectBorder
.height
= size
.y
;
2210 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2212 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2216 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2217 value
= !( !cellval
|| (cellval
== wxT("0")) );
2222 wxRect rectMark
= rectBorder
;
2225 // MSW DrawCheckMark() is weird (and should probably be changed...)
2226 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2230 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2233 dc
.SetTextForeground(attr
.GetTextColour());
2234 dc
.DrawCheckMark(rectMark
);
2237 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2238 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2239 dc
.DrawRectangle(rectBorder
);
2242 // ----------------------------------------------------------------------------
2244 // ----------------------------------------------------------------------------
2246 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2250 m_isReadOnly
= Unset
;
2255 m_attrkind
= wxGridCellAttr::Cell
;
2257 m_sizeRows
= m_sizeCols
= 1;
2258 m_overflow
= UnsetOverflow
;
2260 SetDefAttr(attrDefault
);
2263 wxGridCellAttr
*wxGridCellAttr::Clone() const
2265 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2267 if ( HasTextColour() )
2268 attr
->SetTextColour(GetTextColour());
2269 if ( HasBackgroundColour() )
2270 attr
->SetBackgroundColour(GetBackgroundColour());
2272 attr
->SetFont(GetFont());
2273 if ( HasAlignment() )
2274 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2276 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2280 attr
->SetRenderer(m_renderer
);
2281 m_renderer
->IncRef();
2285 attr
->SetEditor(m_editor
);
2290 attr
->SetReadOnly();
2292 attr
->SetKind( m_attrkind
);
2297 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2299 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2300 SetTextColour(mergefrom
->GetTextColour());
2301 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2302 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2303 if ( !HasFont() && mergefrom
->HasFont() )
2304 SetFont(mergefrom
->GetFont());
2305 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2308 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2309 SetAlignment(hAlign
, vAlign
);
2311 if ( !HasSize() && mergefrom
->HasSize() )
2312 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2314 // Directly access member functions as GetRender/Editor don't just return
2315 // m_renderer/m_editor
2317 // Maybe add support for merge of Render and Editor?
2318 if (!HasRenderer() && mergefrom
->HasRenderer() )
2320 m_renderer
= mergefrom
->m_renderer
;
2321 m_renderer
->IncRef();
2323 if ( !HasEditor() && mergefrom
->HasEditor() )
2325 m_editor
= mergefrom
->m_editor
;
2328 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2329 SetReadOnly(mergefrom
->IsReadOnly());
2331 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2332 SetOverflow(mergefrom
->GetOverflow());
2334 SetDefAttr(mergefrom
->m_defGridAttr
);
2337 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2339 // The size of a cell is normally 1,1
2341 // If this cell is larger (2,2) then this is the top left cell
2342 // the other cells that will be covered (lower right cells) must be
2343 // set to negative or zero values such that
2344 // row + num_rows of the covered cell points to the larger cell (this cell)
2345 // same goes for the col + num_cols.
2347 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2349 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2350 !((num_rows
<= 0) && (num_cols
> 0)) ||
2351 !((num_rows
== 0) && (num_cols
== 0))),
2352 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2354 m_sizeRows
= num_rows
;
2355 m_sizeCols
= num_cols
;
2358 const wxColour
& wxGridCellAttr::GetTextColour() const
2360 if (HasTextColour())
2364 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2366 return m_defGridAttr
->GetTextColour();
2370 wxFAIL_MSG(wxT("Missing default cell attribute"));
2371 return wxNullColour
;
2375 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2377 if (HasBackgroundColour())
2381 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2383 return m_defGridAttr
->GetBackgroundColour();
2387 wxFAIL_MSG(wxT("Missing default cell attribute"));
2388 return wxNullColour
;
2392 const wxFont
& wxGridCellAttr::GetFont() const
2398 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2400 return m_defGridAttr
->GetFont();
2404 wxFAIL_MSG(wxT("Missing default cell attribute"));
2409 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2418 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2420 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2424 wxFAIL_MSG(wxT("Missing default cell attribute"));
2428 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2431 *num_rows
= m_sizeRows
;
2433 *num_cols
= m_sizeCols
;
2436 // GetRenderer and GetEditor use a slightly different decision path about
2437 // which attribute to use. If a non-default attr object has one then it is
2438 // used, otherwise the default editor or renderer is fetched from the grid and
2439 // used. It should be the default for the data type of the cell. If it is
2440 // NULL (because the table has a type that the grid does not have in its
2441 // registry), then the grid's default editor or renderer is used.
2443 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2445 wxGridCellRenderer
*renderer
= NULL
;
2447 if ( m_renderer
&& this != m_defGridAttr
)
2449 // use the cells renderer if it has one
2450 renderer
= m_renderer
;
2453 else // no non-default cell renderer
2455 // get default renderer for the data type
2458 // GetDefaultRendererForCell() will do IncRef() for us
2459 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2462 if ( renderer
== NULL
)
2464 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2466 // if we still don't have one then use the grid default
2467 // (no need for IncRef() here neither)
2468 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2470 else // default grid attr
2472 // use m_renderer which we had decided not to use initially
2473 renderer
= m_renderer
;
2480 // we're supposed to always find something
2481 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2486 // same as above, except for s/renderer/editor/g
2487 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2489 wxGridCellEditor
*editor
= NULL
;
2491 if ( m_editor
&& this != m_defGridAttr
)
2493 // use the cells editor if it has one
2497 else // no non default cell editor
2499 // get default editor for the data type
2502 // GetDefaultEditorForCell() will do IncRef() for us
2503 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2506 if ( editor
== NULL
)
2508 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2510 // if we still don't have one then use the grid default
2511 // (no need for IncRef() here neither)
2512 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2514 else // default grid attr
2516 // use m_editor which we had decided not to use initially
2524 // we're supposed to always find something
2525 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2530 // ----------------------------------------------------------------------------
2531 // wxGridCellAttrData
2532 // ----------------------------------------------------------------------------
2534 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2536 int n
= FindIndex(row
, col
);
2537 if ( n
== wxNOT_FOUND
)
2539 // add the attribute
2540 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2544 // free the old attribute
2545 m_attrs
[(size_t)n
].attr
->DecRef();
2549 // change the attribute
2550 m_attrs
[(size_t)n
].attr
= attr
;
2554 // remove this attribute
2555 m_attrs
.RemoveAt((size_t)n
);
2560 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2562 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2564 int n
= FindIndex(row
, col
);
2565 if ( n
!= wxNOT_FOUND
)
2567 attr
= m_attrs
[(size_t)n
].attr
;
2574 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2576 size_t count
= m_attrs
.GetCount();
2577 for ( size_t n
= 0; n
< count
; n
++ )
2579 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2580 wxCoord row
= coords
.GetRow();
2581 if ((size_t)row
>= pos
)
2585 // If rows inserted, include row counter where necessary
2586 coords
.SetRow(row
+ numRows
);
2588 else if (numRows
< 0)
2590 // If rows deleted ...
2591 if ((size_t)row
>= pos
- numRows
)
2593 // ...either decrement row counter (if row still exists)...
2594 coords
.SetRow(row
+ numRows
);
2598 // ...or remove the attribute
2599 // No need to DecRef the attribute itself since this is
2600 // done be wxGridCellWithAttr's destructor!
2601 m_attrs
.RemoveAt(n
);
2610 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2612 size_t count
= m_attrs
.GetCount();
2613 for ( size_t n
= 0; n
< count
; n
++ )
2615 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2616 wxCoord col
= coords
.GetCol();
2617 if ( (size_t)col
>= pos
)
2621 // If rows inserted, include row counter where necessary
2622 coords
.SetCol(col
+ numCols
);
2624 else if (numCols
< 0)
2626 // If rows deleted ...
2627 if ((size_t)col
>= pos
- numCols
)
2629 // ...either decrement row counter (if row still exists)...
2630 coords
.SetCol(col
+ numCols
);
2634 // ...or remove the attribute
2635 // No need to DecRef the attribute itself since this is
2636 // done be wxGridCellWithAttr's destructor!
2637 m_attrs
.RemoveAt(n
);
2646 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2648 size_t count
= m_attrs
.GetCount();
2649 for ( size_t n
= 0; n
< count
; n
++ )
2651 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2652 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2661 // ----------------------------------------------------------------------------
2662 // wxGridRowOrColAttrData
2663 // ----------------------------------------------------------------------------
2665 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2667 size_t count
= m_attrs
.Count();
2668 for ( size_t n
= 0; n
< count
; n
++ )
2670 m_attrs
[n
]->DecRef();
2674 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2676 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2678 int n
= m_rowsOrCols
.Index(rowOrCol
);
2679 if ( n
!= wxNOT_FOUND
)
2681 attr
= m_attrs
[(size_t)n
];
2688 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2690 int i
= m_rowsOrCols
.Index(rowOrCol
);
2691 if ( i
== wxNOT_FOUND
)
2693 // add the attribute
2694 m_rowsOrCols
.Add(rowOrCol
);
2699 size_t n
= (size_t)i
;
2702 // change the attribute
2703 m_attrs
[n
]->DecRef();
2708 // remove this attribute
2709 m_attrs
[n
]->DecRef();
2710 m_rowsOrCols
.RemoveAt(n
);
2711 m_attrs
.RemoveAt(n
);
2716 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2718 size_t count
= m_attrs
.GetCount();
2719 for ( size_t n
= 0; n
< count
; n
++ )
2721 int & rowOrCol
= m_rowsOrCols
[n
];
2722 if ( (size_t)rowOrCol
>= pos
)
2724 if ( numRowsOrCols
> 0 )
2726 // If rows inserted, include row counter where necessary
2727 rowOrCol
+= numRowsOrCols
;
2729 else if ( numRowsOrCols
< 0)
2731 // If rows deleted, either decrement row counter (if row still exists)
2732 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2733 rowOrCol
+= numRowsOrCols
;
2736 m_rowsOrCols
.RemoveAt(n
);
2737 m_attrs
[n
]->DecRef();
2738 m_attrs
.RemoveAt(n
);
2747 // ----------------------------------------------------------------------------
2748 // wxGridCellAttrProvider
2749 // ----------------------------------------------------------------------------
2751 wxGridCellAttrProvider::wxGridCellAttrProvider()
2753 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2756 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2761 void wxGridCellAttrProvider::InitData()
2763 m_data
= new wxGridCellAttrProviderData
;
2766 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2767 wxGridCellAttr::wxAttrKind kind
) const
2769 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2774 case (wxGridCellAttr::Any
):
2775 // Get cached merge attributes.
2776 // Currently not used as no cache implemented as not mutable
2777 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2780 // Basically implement old version.
2781 // Also check merge cache, so we don't have to re-merge every time..
2782 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2783 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2784 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2786 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2788 // Two or more are non NULL
2789 attr
= new wxGridCellAttr
;
2790 attr
->SetKind(wxGridCellAttr::Merged
);
2792 // Order is important..
2795 attr
->MergeWith(attrcell
);
2800 attr
->MergeWith(attrcol
);
2805 attr
->MergeWith(attrrow
);
2809 // store merge attr if cache implemented
2811 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2815 // one or none is non null return it or null.
2834 case (wxGridCellAttr::Cell
):
2835 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2838 case (wxGridCellAttr::Col
):
2839 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2842 case (wxGridCellAttr::Row
):
2843 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2848 // (wxGridCellAttr::Default):
2849 // (wxGridCellAttr::Merged):
2857 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2863 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2866 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2871 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2874 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2879 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2882 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2886 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2888 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2892 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2896 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2898 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2902 // ----------------------------------------------------------------------------
2903 // wxGridTypeRegistry
2904 // ----------------------------------------------------------------------------
2906 wxGridTypeRegistry::~wxGridTypeRegistry()
2908 size_t count
= m_typeinfo
.Count();
2909 for ( size_t i
= 0; i
< count
; i
++ )
2910 delete m_typeinfo
[i
];
2913 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2914 wxGridCellRenderer
* renderer
,
2915 wxGridCellEditor
* editor
)
2917 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2919 // is it already registered?
2920 int loc
= FindRegisteredDataType(typeName
);
2921 if ( loc
!= wxNOT_FOUND
)
2923 delete m_typeinfo
[loc
];
2924 m_typeinfo
[loc
] = info
;
2928 m_typeinfo
.Add(info
);
2932 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2934 size_t count
= m_typeinfo
.GetCount();
2935 for ( size_t i
= 0; i
< count
; i
++ )
2937 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2946 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2948 int index
= FindRegisteredDataType(typeName
);
2949 if ( index
== wxNOT_FOUND
)
2951 // check whether this is one of the standard ones, in which case
2952 // register it "on the fly"
2954 if ( typeName
== wxGRID_VALUE_STRING
)
2956 RegisterDataType(wxGRID_VALUE_STRING
,
2957 new wxGridCellStringRenderer
,
2958 new wxGridCellTextEditor
);
2961 #endif // wxUSE_TEXTCTRL
2963 if ( typeName
== wxGRID_VALUE_BOOL
)
2965 RegisterDataType(wxGRID_VALUE_BOOL
,
2966 new wxGridCellBoolRenderer
,
2967 new wxGridCellBoolEditor
);
2970 #endif // wxUSE_CHECKBOX
2972 if ( typeName
== wxGRID_VALUE_NUMBER
)
2974 RegisterDataType(wxGRID_VALUE_NUMBER
,
2975 new wxGridCellNumberRenderer
,
2976 new wxGridCellNumberEditor
);
2978 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2980 RegisterDataType(wxGRID_VALUE_FLOAT
,
2981 new wxGridCellFloatRenderer
,
2982 new wxGridCellFloatEditor
);
2985 #endif // wxUSE_TEXTCTRL
2987 if ( typeName
== wxGRID_VALUE_CHOICE
)
2989 RegisterDataType(wxGRID_VALUE_CHOICE
,
2990 new wxGridCellStringRenderer
,
2991 new wxGridCellChoiceEditor
);
2994 #endif // wxUSE_COMBOBOX
2999 // we get here only if just added the entry for this type, so return
3001 index
= m_typeinfo
.GetCount() - 1;
3007 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3009 int index
= FindDataType(typeName
);
3010 if ( index
== wxNOT_FOUND
)
3012 // the first part of the typename is the "real" type, anything after ':'
3013 // are the parameters for the renderer
3014 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3015 if ( index
== wxNOT_FOUND
)
3020 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3021 wxGridCellRenderer
*rendererOld
= renderer
;
3022 renderer
= renderer
->Clone();
3023 rendererOld
->DecRef();
3025 wxGridCellEditor
*editor
= GetEditor(index
);
3026 wxGridCellEditor
*editorOld
= editor
;
3027 editor
= editor
->Clone();
3028 editorOld
->DecRef();
3030 // do it even if there are no parameters to reset them to defaults
3031 wxString params
= typeName
.AfterFirst(_T(':'));
3032 renderer
->SetParameters(params
);
3033 editor
->SetParameters(params
);
3035 // register the new typename
3036 RegisterDataType(typeName
, renderer
, editor
);
3038 // we just registered it, it's the last one
3039 index
= m_typeinfo
.GetCount() - 1;
3045 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3047 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3054 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3056 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3063 // ----------------------------------------------------------------------------
3065 // ----------------------------------------------------------------------------
3067 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3069 wxGridTableBase::wxGridTableBase()
3071 m_view
= (wxGrid
*) NULL
;
3072 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3075 wxGridTableBase::~wxGridTableBase()
3077 delete m_attrProvider
;
3080 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3082 delete m_attrProvider
;
3083 m_attrProvider
= attrProvider
;
3086 bool wxGridTableBase::CanHaveAttributes()
3088 if ( ! GetAttrProvider() )
3090 // use the default attr provider by default
3091 SetAttrProvider(new wxGridCellAttrProvider
);
3097 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3099 if ( m_attrProvider
)
3100 return m_attrProvider
->GetAttr(row
, col
, kind
);
3102 return (wxGridCellAttr
*)NULL
;
3105 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3107 if ( m_attrProvider
)
3109 attr
->SetKind(wxGridCellAttr::Cell
);
3110 m_attrProvider
->SetAttr(attr
, row
, col
);
3114 // as we take ownership of the pointer and don't store it, we must
3120 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3122 if ( m_attrProvider
)
3124 attr
->SetKind(wxGridCellAttr::Row
);
3125 m_attrProvider
->SetRowAttr(attr
, row
);
3129 // as we take ownership of the pointer and don't store it, we must
3135 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3137 if ( m_attrProvider
)
3139 attr
->SetKind(wxGridCellAttr::Col
);
3140 m_attrProvider
->SetColAttr(attr
, col
);
3144 // as we take ownership of the pointer and don't store it, we must
3150 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3151 size_t WXUNUSED(numRows
) )
3153 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3158 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3160 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3165 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3166 size_t WXUNUSED(numRows
) )
3168 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3173 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3174 size_t WXUNUSED(numCols
) )
3176 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3181 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3183 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3188 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3189 size_t WXUNUSED(numCols
) )
3191 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3196 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3200 // RD: Starting the rows at zero confuses users,
3201 // no matter how much it makes sense to us geeks.
3207 wxString
wxGridTableBase::GetColLabelValue( int col
)
3209 // default col labels are:
3210 // cols 0 to 25 : A-Z
3211 // cols 26 to 675 : AA-ZZ
3216 for ( n
= 1; ; n
++ )
3218 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3224 // reverse the string...
3226 for ( i
= 0; i
< n
; i
++ )
3234 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3236 return wxGRID_VALUE_STRING
;
3239 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3240 const wxString
& typeName
)
3242 return typeName
== wxGRID_VALUE_STRING
;
3245 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3247 return CanGetValueAs(row
, col
, typeName
);
3250 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3255 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3260 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3265 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3266 long WXUNUSED(value
) )
3270 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3271 double WXUNUSED(value
) )
3275 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3276 bool WXUNUSED(value
) )
3280 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3281 const wxString
& WXUNUSED(typeName
) )
3286 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3287 const wxString
& WXUNUSED(typeName
),
3288 void* WXUNUSED(value
) )
3292 //////////////////////////////////////////////////////////////////////
3294 // Message class for the grid table to send requests and notifications
3298 wxGridTableMessage::wxGridTableMessage()
3300 m_table
= (wxGridTableBase
*) NULL
;
3306 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3307 int commandInt1
, int commandInt2
)
3311 m_comInt1
= commandInt1
;
3312 m_comInt2
= commandInt2
;
3315 //////////////////////////////////////////////////////////////////////
3317 // A basic grid table for string data. An object of this class will
3318 // created by wxGrid if you don't specify an alternative table class.
3321 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3323 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3325 wxGridStringTable::wxGridStringTable()
3330 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3333 m_data
.Alloc( numRows
);
3336 sa
.Alloc( numCols
);
3337 sa
.Add( wxEmptyString
, numCols
);
3339 m_data
.Add( sa
, numRows
);
3342 wxGridStringTable::~wxGridStringTable()
3346 int wxGridStringTable::GetNumberRows()
3348 return m_data
.GetCount();
3351 int wxGridStringTable::GetNumberCols()
3353 if ( m_data
.GetCount() > 0 )
3354 return m_data
[0].GetCount();
3359 wxString
wxGridStringTable::GetValue( int row
, int col
)
3361 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3363 _T("invalid row or column index in wxGridStringTable") );
3365 return m_data
[row
][col
];
3368 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3370 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3371 _T("invalid row or column index in wxGridStringTable") );
3373 m_data
[row
][col
] = value
;
3376 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3378 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3380 _T("invalid row or column index in wxGridStringTable") );
3382 return (m_data
[row
][col
] == wxEmptyString
);
3385 void wxGridStringTable::Clear()
3388 int numRows
, numCols
;
3390 numRows
= m_data
.GetCount();
3393 numCols
= m_data
[0].GetCount();
3395 for ( row
= 0; row
< numRows
; row
++ )
3397 for ( col
= 0; col
< numCols
; col
++ )
3399 m_data
[row
][col
] = wxEmptyString
;
3405 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3407 size_t curNumRows
= m_data
.GetCount();
3408 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3409 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3411 if ( pos
>= curNumRows
)
3413 return AppendRows( numRows
);
3417 sa
.Alloc( curNumCols
);
3418 sa
.Add( wxEmptyString
, curNumCols
);
3419 m_data
.Insert( sa
, pos
, numRows
);
3423 wxGridTableMessage
msg( this,
3424 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3428 GetView()->ProcessTableMessage( msg
);
3434 bool wxGridStringTable::AppendRows( size_t numRows
)
3436 size_t curNumRows
= m_data
.GetCount();
3437 size_t curNumCols
= ( curNumRows
> 0
3438 ? m_data
[0].GetCount()
3439 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3442 if ( curNumCols
> 0 )
3444 sa
.Alloc( curNumCols
);
3445 sa
.Add( wxEmptyString
, curNumCols
);
3448 m_data
.Add( sa
, numRows
);
3452 wxGridTableMessage
msg( this,
3453 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3456 GetView()->ProcessTableMessage( msg
);
3462 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3464 size_t curNumRows
= m_data
.GetCount();
3466 if ( pos
>= curNumRows
)
3468 wxFAIL_MSG( wxString::Format
3470 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3472 (unsigned long)numRows
,
3473 (unsigned long)curNumRows
3479 if ( numRows
> curNumRows
- pos
)
3481 numRows
= curNumRows
- pos
;
3484 if ( numRows
>= curNumRows
)
3490 m_data
.RemoveAt( pos
, numRows
);
3495 wxGridTableMessage
msg( this,
3496 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3500 GetView()->ProcessTableMessage( msg
);
3506 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3510 size_t curNumRows
= m_data
.GetCount();
3511 size_t curNumCols
= ( curNumRows
> 0
3512 ? m_data
[0].GetCount()
3513 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3515 if ( pos
>= curNumCols
)
3517 return AppendCols( numCols
);
3520 if ( !m_colLabels
.IsEmpty() )
3522 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3525 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3526 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3529 for ( row
= 0; row
< curNumRows
; row
++ )
3531 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3533 m_data
[row
].Insert( wxEmptyString
, col
);
3539 wxGridTableMessage
msg( this,
3540 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3544 GetView()->ProcessTableMessage( msg
);
3550 bool wxGridStringTable::AppendCols( size_t numCols
)
3554 size_t curNumRows
= m_data
.GetCount();
3559 // TODO: something better than this ?
3561 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3566 for ( row
= 0; row
< curNumRows
; row
++ )
3568 m_data
[row
].Add( wxEmptyString
, numCols
);
3573 wxGridTableMessage
msg( this,
3574 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3577 GetView()->ProcessTableMessage( msg
);
3583 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3587 size_t curNumRows
= m_data
.GetCount();
3588 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3589 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3591 if ( pos
>= curNumCols
)
3593 wxFAIL_MSG( wxString::Format
3595 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3597 (unsigned long)numCols
,
3598 (unsigned long)curNumCols
3605 colID
= GetView()->GetColAt( pos
);
3609 if ( numCols
> curNumCols
- colID
)
3611 numCols
= curNumCols
- colID
;
3614 if ( !m_colLabels
.IsEmpty() )
3616 m_colLabels
.RemoveAt( colID
, numCols
);
3619 for ( row
= 0; row
< curNumRows
; row
++ )
3621 if ( numCols
>= curNumCols
)
3623 m_data
[row
].Clear();
3627 m_data
[row
].RemoveAt( colID
, numCols
);
3633 wxGridTableMessage
msg( this,
3634 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3638 GetView()->ProcessTableMessage( msg
);
3644 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3646 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3648 // using default label
3650 return wxGridTableBase::GetRowLabelValue( row
);
3654 return m_rowLabels
[row
];
3658 wxString
wxGridStringTable::GetColLabelValue( int col
)
3660 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3662 // using default label
3664 return wxGridTableBase::GetColLabelValue( col
);
3668 return m_colLabels
[col
];
3672 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3674 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3676 int n
= m_rowLabels
.GetCount();
3679 for ( i
= n
; i
<= row
; i
++ )
3681 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3685 m_rowLabels
[row
] = value
;
3688 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3690 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3692 int n
= m_colLabels
.GetCount();
3695 for ( i
= n
; i
<= col
; i
++ )
3697 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3701 m_colLabels
[col
] = value
;
3705 //////////////////////////////////////////////////////////////////////
3706 //////////////////////////////////////////////////////////////////////
3708 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3710 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3711 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3712 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3713 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3714 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3715 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3716 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3719 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3721 const wxPoint
&pos
, const wxSize
&size
)
3722 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3727 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3731 // NO - don't do this because it will set both the x and y origin
3732 // coords to match the parent scrolled window and we just want to
3733 // set the y coord - MB
3735 // m_owner->PrepareDC( dc );
3738 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3739 wxPoint pt
= dc
.GetDeviceOrigin();
3740 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3742 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3743 m_owner
->DrawRowLabels( dc
, rows
);
3746 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3748 m_owner
->ProcessRowLabelMouseEvent( event
);
3751 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3753 m_owner
->GetEventHandler()->ProcessEvent( event
);
3756 // This seems to be required for wxMotif otherwise the mouse
3757 // cursor must be in the cell edit control to get key events
3759 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3761 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3765 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3767 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3771 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3773 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3777 //////////////////////////////////////////////////////////////////////
3779 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3781 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3782 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3783 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3784 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3785 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3786 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3787 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3790 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3792 const wxPoint
&pos
, const wxSize
&size
)
3793 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3798 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3802 // NO - don't do this because it will set both the x and y origin
3803 // coords to match the parent scrolled window and we just want to
3804 // set the x coord - MB
3806 // m_owner->PrepareDC( dc );
3809 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3810 wxPoint pt
= dc
.GetDeviceOrigin();
3811 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3812 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3814 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3816 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3817 m_owner
->DrawColLabels( dc
, cols
);
3820 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3822 m_owner
->ProcessColLabelMouseEvent( event
);
3825 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3827 m_owner
->GetEventHandler()->ProcessEvent( event
);
3830 // This seems to be required for wxMotif otherwise the mouse
3831 // cursor must be in the cell edit control to get key events
3833 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3835 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3839 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3841 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3845 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3847 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3851 //////////////////////////////////////////////////////////////////////
3853 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3855 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3856 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3857 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3858 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3859 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3860 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3861 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3864 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3866 const wxPoint
&pos
, const wxSize
&size
)
3867 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxFULL_REPAINT_ON_RESIZE
)
3872 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3876 int client_height
= 0;
3877 int client_width
= 0;
3878 GetClientSize( &client_width
, &client_height
);
3880 // VZ: any reason for this ifdef? (FIXME)
3885 rect
.SetWidth( client_width
- 2 );
3886 rect
.SetHeight( client_height
- 2 );
3888 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3890 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3891 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3892 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3893 dc
.DrawLine( 0, 0, client_width
, 0 );
3894 dc
.DrawLine( 0, 0, 0, client_height
);
3896 dc
.SetPen( *wxWHITE_PEN
);
3897 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3898 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3902 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3904 m_owner
->ProcessCornerLabelMouseEvent( event
);
3907 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3909 m_owner
->GetEventHandler()->ProcessEvent(event
);
3912 // This seems to be required for wxMotif otherwise the mouse
3913 // cursor must be in the cell edit control to get key events
3915 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3917 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3921 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3923 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3927 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3929 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3933 //////////////////////////////////////////////////////////////////////
3935 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3937 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3938 EVT_PAINT( wxGridWindow::OnPaint
)
3939 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3940 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3941 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3942 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3943 EVT_CHAR( wxGridWindow::OnChar
)
3944 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3945 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3946 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3949 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3950 wxGridRowLabelWindow
*rowLblWin
,
3951 wxGridColLabelWindow
*colLblWin
,
3954 const wxSize
&size
)
3956 parent
, id
, pos
, size
,
3957 wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
| wxFULL_REPAINT_ON_RESIZE
,
3958 wxT("grid window") )
3961 m_rowLabelWin
= rowLblWin
;
3962 m_colLabelWin
= colLblWin
;
3965 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3967 wxPaintDC
dc( this );
3968 m_owner
->PrepareDC( dc
);
3969 wxRegion reg
= GetUpdateRegion();
3970 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3971 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3973 #if WXGRID_DRAW_LINES
3974 m_owner
->DrawAllGridLines( dc
, reg
);
3977 m_owner
->DrawGridSpace( dc
);
3978 m_owner
->DrawHighlight( dc
, dirtyCells
);
3981 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3983 wxWindow::ScrollWindow( dx
, dy
, rect
);
3984 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3985 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3988 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3990 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3993 m_owner
->ProcessGridCellMouseEvent( event
);
3996 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3998 m_owner
->GetEventHandler()->ProcessEvent( event
);
4001 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4002 // cursor must be in the cell edit control to get key events
4004 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4006 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4010 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4012 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4016 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4018 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4022 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4026 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4028 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4032 //////////////////////////////////////////////////////////////////////
4034 // Internal Helper function for computing row or column from some
4035 // (unscrolled) coordinate value, using either
4036 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4037 // of m_rowBottoms/m_ColRights to speed up the search!
4039 // Internal helper macros for simpler use of that function
4041 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4042 const wxArrayInt
& BorderArray
, int nMax
,
4045 #define internalXToCol(x) XToCol(x, true)
4046 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4047 m_minAcceptableRowHeight, \
4048 m_rowBottoms, m_numRows, true)
4050 /////////////////////////////////////////////////////////////////////
4052 #if wxUSE_EXTENDED_RTTI
4053 WX_DEFINE_FLAGS( wxGridStyle
)
4055 wxBEGIN_FLAGS( wxGridStyle
)
4056 // new style border flags, we put them first to
4057 // use them for streaming out
4058 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4059 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4060 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4061 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4062 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4063 wxFLAGS_MEMBER(wxBORDER_NONE
)
4065 // old style border flags
4066 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4067 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4068 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4069 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4070 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4071 wxFLAGS_MEMBER(wxBORDER
)
4073 // standard window styles
4074 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4075 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4076 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4077 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4078 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4079 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4080 wxFLAGS_MEMBER(wxVSCROLL
)
4081 wxFLAGS_MEMBER(wxHSCROLL
)
4083 wxEND_FLAGS( wxGridStyle
)
4085 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4087 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4088 wxHIDE_PROPERTY( Children
)
4089 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4090 wxEND_PROPERTIES_TABLE()
4092 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4093 wxEND_HANDLERS_TABLE()
4095 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4098 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4101 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4104 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4105 EVT_PAINT( wxGrid::OnPaint
)
4106 EVT_SIZE( wxGrid::OnSize
)
4107 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4108 EVT_KEY_UP( wxGrid::OnKeyUp
)
4109 EVT_CHAR ( wxGrid::OnChar
)
4110 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4115 // in order to make sure that a size event is not
4116 // trigerred in a unfinished state
4117 m_cornerLabelWin
= NULL
;
4118 m_rowLabelWin
= NULL
;
4119 m_colLabelWin
= NULL
;
4123 wxGrid::wxGrid( wxWindow
*parent
,
4128 const wxString
& name
)
4129 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4130 m_colMinWidths(GRID_HASH_SIZE
),
4131 m_rowMinHeights(GRID_HASH_SIZE
)
4134 SetBestFittingSize(size
);
4137 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4138 const wxPoint
& pos
, const wxSize
& size
,
4139 long style
, const wxString
& name
)
4141 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4142 style
| wxWANTS_CHARS
, name
))
4145 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4146 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4149 SetBestFittingSize(size
);
4156 // Must do this or ~wxScrollHelper will pop the wrong event handler
4157 SetTargetWindow(this);
4159 wxSafeDecRef(m_defaultCellAttr
);
4161 #ifdef DEBUG_ATTR_CACHE
4162 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4163 wxPrintf(_T("wxGrid attribute cache statistics: "
4164 "total: %u, hits: %u (%u%%)\n"),
4165 total
, gs_nAttrCacheHits
,
4166 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4172 delete m_typeRegistry
;
4177 // ----- internal init and update functions
4180 // NOTE: If using the default visual attributes works everywhere then this can
4181 // be removed as well as the #else cases below.
4182 #define _USE_VISATTR 0
4184 void wxGrid::Create()
4186 // set to true by CreateGrid
4189 // create the type registry
4190 m_typeRegistry
= new wxGridTypeRegistry
;
4193 m_table
= (wxGridTableBase
*) NULL
;
4196 m_cellEditCtrlEnabled
= false;
4198 m_defaultCellAttr
= new wxGridCellAttr();
4200 // Set default cell attributes
4201 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4202 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4203 m_defaultCellAttr
->SetFont(GetFont());
4204 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4205 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4206 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4209 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4210 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4212 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4213 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4216 m_defaultCellAttr
->SetTextColour(
4217 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4218 m_defaultCellAttr
->SetBackgroundColour(
4219 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4224 m_currentCellCoords
= wxGridNoCellCoords
;
4226 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4227 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4229 // subwindow components that make up the wxGrid
4230 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4235 m_colLabelWin
= new wxGridColLabelWindow( this,
4240 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4245 m_gridWin
= new wxGridWindow( this,
4252 SetTargetWindow( m_gridWin
);
4255 wxColour gfg
= gva
.colFg
;
4256 wxColour gbg
= gva
.colBg
;
4257 wxColour lfg
= lva
.colFg
;
4258 wxColour lbg
= lva
.colBg
;
4260 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4261 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4262 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4263 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4266 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4267 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4268 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4269 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4270 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4271 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4273 m_gridWin
->SetOwnForegroundColour(gfg
);
4274 m_gridWin
->SetOwnBackgroundColour(gbg
);
4279 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4280 wxGrid::wxGridSelectionModes selmode
)
4282 wxCHECK_MSG( !m_created
,
4284 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4286 m_numRows
= numRows
;
4287 m_numCols
= numCols
;
4289 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4290 m_table
->SetView( this );
4292 m_selection
= new wxGridSelection( this, selmode
);
4301 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4303 wxCHECK_RET( m_created
,
4304 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4306 m_selection
->SetSelectionMode( selmode
);
4309 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4311 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4312 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4314 return m_selection
->GetSelectionMode();
4317 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4318 wxGrid::wxGridSelectionModes selmode
)
4322 // stop all processing
4327 wxGridTableBase
*t
= m_table
;
4342 m_numRows
= table
->GetNumberRows();
4343 m_numCols
= table
->GetNumberCols();
4346 m_table
->SetView( this );
4347 m_ownTable
= takeOwnership
;
4348 m_selection
= new wxGridSelection( this, selmode
);
4360 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4361 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4363 if ( m_rowLabelWin
)
4365 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4369 m_labelBackgroundColour
= *wxWHITE
;
4372 m_labelTextColour
= *wxBLACK
;
4375 m_attrCache
.row
= -1;
4376 m_attrCache
.col
= -1;
4377 m_attrCache
.attr
= NULL
;
4379 // TODO: something better than this ?
4381 m_labelFont
= this->GetFont();
4382 m_labelFont
.SetWeight( wxBOLD
);
4384 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4385 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4387 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4388 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4389 m_colLabelTextOrientation
= wxHORIZONTAL
;
4391 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4392 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4394 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4395 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4397 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4398 m_defaultRowHeight
+= 8;
4400 m_defaultRowHeight
+= 4;
4403 m_gridLineColour
= wxColour( 192,192,192 );
4404 m_gridLinesEnabled
= true;
4405 m_cellHighlightColour
= *wxBLACK
;
4406 m_cellHighlightPenWidth
= 2;
4407 m_cellHighlightROPenWidth
= 1;
4409 m_canDragColMove
= false;
4411 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4412 m_winCapture
= (wxWindow
*)NULL
;
4413 m_canDragRowSize
= true;
4414 m_canDragColSize
= true;
4415 m_canDragGridSize
= true;
4416 m_canDragCell
= false;
4418 m_dragRowOrCol
= -1;
4419 m_isDragging
= false;
4420 m_startDragPos
= wxDefaultPosition
;
4422 m_waitForSlowClick
= false;
4424 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4425 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4427 m_currentCellCoords
= wxGridNoCellCoords
;
4431 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4432 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4434 m_editable
= true; // default for whole grid
4436 m_inOnKeyDown
= false;
4442 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4443 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4446 // ----------------------------------------------------------------------------
4447 // the idea is to call these functions only when necessary because they create
4448 // quite big arrays which eat memory mostly unnecessary - in particular, if
4449 // default widths/heights are used for all rows/columns, we may not use these
4452 // with some extra code, it should be possible to only store the
4453 // widths/heights different from default ones but this will be done later...
4454 // ----------------------------------------------------------------------------
4456 void wxGrid::InitRowHeights()
4458 m_rowHeights
.Empty();
4459 m_rowBottoms
.Empty();
4461 m_rowHeights
.Alloc( m_numRows
);
4462 m_rowBottoms
.Alloc( m_numRows
);
4466 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4468 for ( int i
= 0; i
< m_numRows
; i
++ )
4470 rowBottom
+= m_defaultRowHeight
;
4471 m_rowBottoms
.Add( rowBottom
);
4475 void wxGrid::InitColWidths()
4477 m_colWidths
.Empty();
4478 m_colRights
.Empty();
4480 m_colWidths
.Alloc( m_numCols
);
4481 m_colRights
.Alloc( m_numCols
);
4484 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4486 for ( int i
= 0; i
< m_numCols
; i
++ )
4488 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4489 m_colRights
.Add( colRight
);
4493 int wxGrid::GetColWidth(int col
) const
4495 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4498 int wxGrid::GetColLeft(int col
) const
4500 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4501 : m_colRights
[col
] - m_colWidths
[col
];
4504 int wxGrid::GetColRight(int col
) const
4506 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4510 int wxGrid::GetRowHeight(int row
) const
4512 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4515 int wxGrid::GetRowTop(int row
) const
4517 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4518 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4521 int wxGrid::GetRowBottom(int row
) const
4523 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4524 : m_rowBottoms
[row
];
4527 void wxGrid::CalcDimensions()
4530 GetClientSize( &cw
, &ch
);
4532 if ( m_rowLabelWin
->IsShown() )
4533 cw
-= m_rowLabelWidth
;
4534 if ( m_colLabelWin
->IsShown() )
4535 ch
-= m_colLabelHeight
;
4538 int w
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) + m_extraWidth
+ 1 : 0;
4539 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4541 // take into account editor if shown
4542 if ( IsCellEditControlShown() )
4545 int r
= m_currentCellCoords
.GetRow();
4546 int c
= m_currentCellCoords
.GetCol();
4547 int x
= GetColLeft(c
);
4548 int y
= GetRowTop(r
);
4550 // how big is the editor
4551 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4552 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4553 editor
->GetControl()->GetSize(&w2
, &h2
);
4564 // preserve (more or less) the previous position
4566 GetViewStart( &x
, &y
);
4568 // ensure the position is valid for the new scroll ranges
4570 x
= wxMax( w
- 1, 0 );
4572 y
= wxMax( h
- 1, 0 );
4574 // do set scrollbar parameters
4575 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4576 GetScrollX(w
), GetScrollY(h
), x
, y
,
4577 GetBatchCount() != 0);
4579 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4580 // still must reposition the children
4584 void wxGrid::CalcWindowSizes()
4586 // escape if the window is has not been fully created yet
4588 if ( m_cornerLabelWin
== NULL
)
4592 GetClientSize( &cw
, &ch
);
4594 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4595 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4597 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4598 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4600 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4601 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4603 if ( m_gridWin
&& m_gridWin
->IsShown() )
4604 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4607 // this is called when the grid table sends a message
4608 // to indicate that it has been redimensioned
4610 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4613 bool result
= false;
4615 // Clear the attribute cache as the attribute might refer to a different
4616 // cell than stored in the cache after adding/removing rows/columns.
4619 // By the same reasoning, the editor should be dismissed if columns are
4620 // added or removed. And for consistency, it should IMHO always be
4621 // removed, not only if the cell "underneath" it actually changes.
4622 // For now, I intentionally do not save the editor's content as the
4623 // cell it might want to save that stuff to might no longer exist.
4624 HideCellEditControl();
4627 // if we were using the default widths/heights so far, we must change them
4629 if ( m_colWidths
.IsEmpty() )
4634 if ( m_rowHeights
.IsEmpty() )
4640 switch ( msg
.GetId() )
4642 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4644 size_t pos
= msg
.GetCommandInt();
4645 int numRows
= msg
.GetCommandInt2();
4647 m_numRows
+= numRows
;
4649 if ( !m_rowHeights
.IsEmpty() )
4651 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4652 m_rowBottoms
.Insert( 0, pos
, numRows
);
4656 bottom
= m_rowBottoms
[pos
- 1];
4658 for ( i
= pos
; i
< m_numRows
; i
++ )
4660 bottom
+= m_rowHeights
[i
];
4661 m_rowBottoms
[i
] = bottom
;
4665 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4667 // if we have just inserted cols into an empty grid the current
4668 // cell will be undefined...
4670 SetCurrentCell( 0, 0 );
4674 m_selection
->UpdateRows( pos
, numRows
);
4675 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4677 attrProvider
->UpdateAttrRows( pos
, numRows
);
4679 if ( !GetBatchCount() )
4682 m_rowLabelWin
->Refresh();
4688 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4690 int numRows
= msg
.GetCommandInt();
4691 int oldNumRows
= m_numRows
;
4692 m_numRows
+= numRows
;
4694 if ( !m_rowHeights
.IsEmpty() )
4696 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4697 m_rowBottoms
.Add( 0, numRows
);
4700 if ( oldNumRows
> 0 )
4701 bottom
= m_rowBottoms
[oldNumRows
- 1];
4703 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4705 bottom
+= m_rowHeights
[i
];
4706 m_rowBottoms
[i
] = bottom
;
4710 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4712 // if we have just inserted cols into an empty grid the current
4713 // cell will be undefined...
4715 SetCurrentCell( 0, 0 );
4718 if ( !GetBatchCount() )
4721 m_rowLabelWin
->Refresh();
4727 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4729 size_t pos
= msg
.GetCommandInt();
4730 int numRows
= msg
.GetCommandInt2();
4731 m_numRows
-= numRows
;
4733 if ( !m_rowHeights
.IsEmpty() )
4735 m_rowHeights
.RemoveAt( pos
, numRows
);
4736 m_rowBottoms
.RemoveAt( pos
, numRows
);
4739 for ( i
= 0; i
< m_numRows
; i
++ )
4741 h
+= m_rowHeights
[i
];
4742 m_rowBottoms
[i
] = h
;
4748 m_currentCellCoords
= wxGridNoCellCoords
;
4752 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4753 m_currentCellCoords
.Set( 0, 0 );
4757 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4758 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4761 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4763 // ifdef'd out following patch from Paul Gammans
4765 // No need to touch column attributes, unless we
4766 // removed _all_ rows, in this case, we remove
4767 // all column attributes.
4768 // I hate to do this here, but the
4769 // needed data is not available inside UpdateAttrRows.
4770 if ( !GetNumberRows() )
4771 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4775 if ( !GetBatchCount() )
4778 m_rowLabelWin
->Refresh();
4784 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4786 size_t pos
= msg
.GetCommandInt();
4787 int numCols
= msg
.GetCommandInt2();
4788 m_numCols
+= numCols
;
4790 if ( !m_colAt
.IsEmpty() )
4792 //Shift the column IDs
4794 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4796 if ( m_colAt
[i
] >= (int)pos
)
4797 m_colAt
[i
] += numCols
;
4800 m_colAt
.Insert( pos
, pos
, numCols
);
4802 //Set the new columns' positions
4803 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4809 if ( !m_colWidths
.IsEmpty() )
4811 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4812 m_colRights
.Insert( 0, pos
, numCols
);
4816 right
= m_colRights
[GetColAt( pos
- 1 )];
4819 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4821 i
= GetColAt( colPos
);
4823 right
+= m_colWidths
[i
];
4824 m_colRights
[i
] = right
;
4828 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4830 // if we have just inserted cols into an empty grid the current
4831 // cell will be undefined...
4833 SetCurrentCell( 0, 0 );
4837 m_selection
->UpdateCols( pos
, numCols
);
4838 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4840 attrProvider
->UpdateAttrCols( pos
, numCols
);
4841 if ( !GetBatchCount() )
4844 m_colLabelWin
->Refresh();
4850 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4852 int numCols
= msg
.GetCommandInt();
4853 int oldNumCols
= m_numCols
;
4854 m_numCols
+= numCols
;
4856 if ( !m_colAt
.IsEmpty() )
4858 m_colAt
.Add( 0, numCols
);
4860 //Set the new columns' positions
4862 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4868 if ( !m_colWidths
.IsEmpty() )
4870 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4871 m_colRights
.Add( 0, numCols
);
4874 if ( oldNumCols
> 0 )
4875 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
4878 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
4880 i
= GetColAt( colPos
);
4882 right
+= m_colWidths
[i
];
4883 m_colRights
[i
] = right
;
4887 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4889 // if we have just inserted cols into an empty grid the current
4890 // cell will be undefined...
4892 SetCurrentCell( 0, 0 );
4894 if ( !GetBatchCount() )
4897 m_colLabelWin
->Refresh();
4903 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4905 size_t pos
= msg
.GetCommandInt();
4906 int numCols
= msg
.GetCommandInt2();
4907 m_numCols
-= numCols
;
4909 if ( !m_colAt
.IsEmpty() )
4911 int colID
= GetColAt( pos
);
4913 m_colAt
.RemoveAt( pos
, numCols
);
4915 //Shift the column IDs
4917 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4919 if ( m_colAt
[colPos
] > colID
)
4920 m_colAt
[colPos
] -= numCols
;
4924 if ( !m_colWidths
.IsEmpty() )
4926 m_colWidths
.RemoveAt( pos
, numCols
);
4927 m_colRights
.RemoveAt( pos
, numCols
);
4931 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
4933 i
= GetColAt( colPos
);
4935 w
+= m_colWidths
[i
];
4942 m_currentCellCoords
= wxGridNoCellCoords
;
4946 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4947 m_currentCellCoords
.Set( 0, 0 );
4951 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4952 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4955 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4957 // ifdef'd out following patch from Paul Gammans
4959 // No need to touch row attributes, unless we
4960 // removed _all_ columns, in this case, we remove
4961 // all row attributes.
4962 // I hate to do this here, but the
4963 // needed data is not available inside UpdateAttrCols.
4964 if ( !GetNumberCols() )
4965 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4969 if ( !GetBatchCount() )
4972 m_colLabelWin
->Refresh();
4979 if (result
&& !GetBatchCount() )
4980 m_gridWin
->Refresh();
4985 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4987 wxRegionIterator
iter( reg
);
4990 wxArrayInt rowlabels
;
4997 // TODO: remove this when we can...
4998 // There is a bug in wxMotif that gives garbage update
4999 // rectangles if you jump-scroll a long way by clicking the
5000 // scrollbar with middle button. This is a work-around
5002 #if defined(__WXMOTIF__)
5004 m_gridWin
->GetClientSize( &cw
, &ch
);
5005 if ( r
.GetTop() > ch
)
5007 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5010 // logical bounds of update region
5013 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5014 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5016 // find the row labels within these bounds
5019 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5021 if ( GetRowBottom(row
) < top
)
5024 if ( GetRowTop(row
) > bottom
)
5027 rowlabels
.Add( row
);
5036 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
5038 wxRegionIterator
iter( reg
);
5041 wxArrayInt colLabels
;
5048 // TODO: remove this when we can...
5049 // There is a bug in wxMotif that gives garbage update
5050 // rectangles if you jump-scroll a long way by clicking the
5051 // scrollbar with middle button. This is a work-around
5053 #if defined(__WXMOTIF__)
5055 m_gridWin
->GetClientSize( &cw
, &ch
);
5056 if ( r
.GetLeft() > cw
)
5058 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5061 // logical bounds of update region
5064 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5065 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5067 // find the cells within these bounds
5071 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5073 col
= GetColAt( colPos
);
5075 if ( GetColRight(col
) < left
)
5078 if ( GetColLeft(col
) > right
)
5081 colLabels
.Add( col
);
5090 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
5092 wxRegionIterator
iter( reg
);
5095 wxGridCellCoordsArray cellsExposed
;
5097 int left
, top
, right
, bottom
;
5102 // TODO: remove this when we can...
5103 // There is a bug in wxMotif that gives garbage update
5104 // rectangles if you jump-scroll a long way by clicking the
5105 // scrollbar with middle button. This is a work-around
5107 #if defined(__WXMOTIF__)
5109 m_gridWin
->GetClientSize( &cw
, &ch
);
5110 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5111 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5112 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5113 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5116 // logical bounds of update region
5118 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5119 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5121 // find the cells within these bounds
5124 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5126 if ( GetRowBottom(row
) <= top
)
5129 if ( GetRowTop(row
) > bottom
)
5133 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5135 col
= GetColAt( colPos
);
5137 if ( GetColRight(col
) <= left
)
5140 if ( GetColLeft(col
) > right
)
5143 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5150 return cellsExposed
;
5154 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5157 wxPoint
pos( event
.GetPosition() );
5158 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5160 if ( event
.Dragging() )
5164 m_isDragging
= true;
5165 m_rowLabelWin
->CaptureMouse();
5168 if ( event
.LeftIsDown() )
5170 switch ( m_cursorMode
)
5172 case WXGRID_CURSOR_RESIZE_ROW
:
5174 int cw
, ch
, left
, dummy
;
5175 m_gridWin
->GetClientSize( &cw
, &ch
);
5176 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5178 wxClientDC
dc( m_gridWin
);
5181 GetRowTop(m_dragRowOrCol
) +
5182 GetRowMinimalHeight(m_dragRowOrCol
) );
5183 dc
.SetLogicalFunction(wxINVERT
);
5184 if ( m_dragLastPos
>= 0 )
5186 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5188 dc
.DrawLine( left
, y
, left
+cw
, y
);
5193 case WXGRID_CURSOR_SELECT_ROW
:
5195 if ( (row
= YToRow( y
)) >= 0 )
5199 m_selection
->SelectRow( row
,
5200 event
.ControlDown(),
5209 // default label to suppress warnings about "enumeration value
5210 // 'xxx' not handled in switch
5218 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5223 if (m_rowLabelWin
->HasCapture())
5224 m_rowLabelWin
->ReleaseMouse();
5225 m_isDragging
= false;
5228 // ------------ Entering or leaving the window
5230 if ( event
.Entering() || event
.Leaving() )
5232 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5235 // ------------ Left button pressed
5237 else if ( event
.LeftDown() )
5239 // don't send a label click event for a hit on the
5240 // edge of the row label - this is probably the user
5241 // wanting to resize the row
5243 if ( YToEdgeOfRow(y
) < 0 )
5247 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5249 if ( !event
.ShiftDown() && !event
.CmdDown() )
5253 if ( event
.ShiftDown() )
5255 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5258 GetNumberCols() - 1,
5259 event
.ControlDown(),
5266 m_selection
->SelectRow( row
,
5267 event
.ControlDown(),
5274 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5279 // starting to drag-resize a row
5280 if ( CanDragRowSize() )
5281 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5285 // ------------ Left double click
5287 else if (event
.LeftDClick() )
5289 row
= YToEdgeOfRow(y
);
5294 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5296 // no default action at the moment
5301 // adjust row height depending on label text
5302 AutoSizeRowLabelSize( row
);
5304 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5309 // ------------ Left button released
5311 else if ( event
.LeftUp() )
5313 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5315 DoEndDragResizeRow();
5317 // Note: we are ending the event *after* doing
5318 // default processing in this case
5320 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5323 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5327 // ------------ Right button down
5329 else if ( event
.RightDown() )
5333 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5335 // no default action at the moment
5339 // ------------ Right double click
5341 else if ( event
.RightDClick() )
5345 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5347 // no default action at the moment
5351 // ------------ No buttons down and mouse moving
5353 else if ( event
.Moving() )
5355 m_dragRowOrCol
= YToEdgeOfRow( y
);
5356 if ( m_dragRowOrCol
>= 0 )
5358 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5360 // don't capture the mouse yet
5361 if ( CanDragRowSize() )
5362 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5365 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5367 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5372 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5375 wxPoint
pos( event
.GetPosition() );
5376 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5378 if ( event
.Dragging() )
5382 m_isDragging
= true;
5383 m_colLabelWin
->CaptureMouse();
5385 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5386 m_dragRowOrCol
= XToCol( x
);
5389 if ( event
.LeftIsDown() )
5391 switch ( m_cursorMode
)
5393 case WXGRID_CURSOR_RESIZE_COL
:
5395 int cw
, ch
, dummy
, top
;
5396 m_gridWin
->GetClientSize( &cw
, &ch
);
5397 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5399 wxClientDC
dc( m_gridWin
);
5402 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5403 GetColMinimalWidth(m_dragRowOrCol
));
5404 dc
.SetLogicalFunction(wxINVERT
);
5405 if ( m_dragLastPos
>= 0 )
5407 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5409 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5414 case WXGRID_CURSOR_SELECT_COL
:
5416 if ( (col
= XToCol( x
)) >= 0 )
5420 m_selection
->SelectCol( col
,
5421 event
.ControlDown(),
5430 case WXGRID_CURSOR_MOVE_COL
:
5433 m_moveToCol
= GetColAt( 0 );
5435 m_moveToCol
= XToCol( x
);
5439 if ( m_moveToCol
< 0 )
5440 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5442 markerX
= GetColLeft( m_moveToCol
);
5444 if ( markerX
!= m_dragLastPos
)
5446 wxClientDC
dc( m_colLabelWin
);
5449 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5453 //Clean up the last indicator
5454 if ( m_dragLastPos
>= 0 )
5456 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5458 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5459 dc
.SetPen(wxNullPen
);
5461 if ( XToCol( m_dragLastPos
) != -1 )
5462 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5465 //Moving to the same place? Don't draw a marker
5466 if ( (m_moveToCol
== m_dragRowOrCol
)
5467 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5468 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5475 wxPen
pen( *wxBLUE
, 2 );
5478 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5480 dc
.SetPen(wxNullPen
);
5482 m_dragLastPos
= markerX
- 1;
5487 // default label to suppress warnings about "enumeration value
5488 // 'xxx' not handled in switch
5496 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5501 if (m_colLabelWin
->HasCapture())
5502 m_colLabelWin
->ReleaseMouse();
5503 m_isDragging
= false;
5506 // ------------ Entering or leaving the window
5508 if ( event
.Entering() || event
.Leaving() )
5510 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5513 // ------------ Left button pressed
5515 else if ( event
.LeftDown() )
5517 // don't send a label click event for a hit on the
5518 // edge of the col label - this is probably the user
5519 // wanting to resize the col
5521 if ( XToEdgeOfCol(x
) < 0 )
5525 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5527 if ( m_canDragColMove
)
5529 //Show button as pressed
5530 wxClientDC
dc( m_colLabelWin
);
5531 int colLeft
= GetColLeft( col
);
5532 int colRight
= GetColRight( col
) - 1;
5533 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5534 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5535 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5537 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5541 if ( !event
.ShiftDown() && !event
.CmdDown() )
5545 if ( event
.ShiftDown() )
5547 m_selection
->SelectBlock( 0,
5548 m_currentCellCoords
.GetCol(),
5549 GetNumberRows() - 1, col
,
5550 event
.ControlDown(),
5557 m_selection
->SelectCol( col
,
5558 event
.ControlDown(),
5565 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5571 // starting to drag-resize a col
5573 if ( CanDragColSize() )
5574 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5578 // ------------ Left double click
5580 if ( event
.LeftDClick() )
5582 col
= XToEdgeOfCol(x
);
5587 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5589 // no default action at the moment
5594 // adjust column width depending on label text
5595 AutoSizeColLabelSize( col
);
5597 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5602 // ------------ Left button released
5604 else if ( event
.LeftUp() )
5606 switch ( m_cursorMode
)
5608 case WXGRID_CURSOR_RESIZE_COL
:
5609 DoEndDragResizeCol();
5611 // Note: we are ending the event *after* doing
5612 // default processing in this case
5614 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5617 case WXGRID_CURSOR_MOVE_COL
:
5620 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5623 case WXGRID_CURSOR_SELECT_COL
:
5624 case WXGRID_CURSOR_SELECT_CELL
:
5625 case WXGRID_CURSOR_RESIZE_ROW
:
5626 case WXGRID_CURSOR_SELECT_ROW
:
5627 // nothing to do (?)
5631 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5635 // ------------ Right button down
5637 else if ( event
.RightDown() )
5641 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5643 // no default action at the moment
5647 // ------------ Right double click
5649 else if ( event
.RightDClick() )
5653 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5655 // no default action at the moment
5659 // ------------ No buttons down and mouse moving
5661 else if ( event
.Moving() )
5663 m_dragRowOrCol
= XToEdgeOfCol( x
);
5664 if ( m_dragRowOrCol
>= 0 )
5666 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5668 // don't capture the cursor yet
5669 if ( CanDragColSize() )
5670 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5673 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5675 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5680 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5682 if ( event
.LeftDown() )
5684 // indicate corner label by having both row and
5687 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5692 else if ( event
.LeftDClick() )
5694 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5696 else if ( event
.RightDown() )
5698 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5700 // no default action at the moment
5703 else if ( event
.RightDClick() )
5705 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5707 // no default action at the moment
5712 void wxGrid::ChangeCursorMode(CursorMode mode
,
5717 static const wxChar
*cursorModes
[] =
5727 wxLogTrace(_T("grid"),
5728 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5729 win
== m_colLabelWin
? _T("colLabelWin")
5730 : win
? _T("rowLabelWin")
5732 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5735 if ( mode
== m_cursorMode
&&
5736 win
== m_winCapture
&&
5737 captureMouse
== (m_winCapture
!= NULL
))
5742 // by default use the grid itself
5748 if (m_winCapture
->HasCapture())
5749 m_winCapture
->ReleaseMouse();
5750 m_winCapture
= (wxWindow
*)NULL
;
5753 m_cursorMode
= mode
;
5755 switch ( m_cursorMode
)
5757 case WXGRID_CURSOR_RESIZE_ROW
:
5758 win
->SetCursor( m_rowResizeCursor
);
5761 case WXGRID_CURSOR_RESIZE_COL
:
5762 win
->SetCursor( m_colResizeCursor
);
5765 case WXGRID_CURSOR_MOVE_COL
:
5766 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5770 win
->SetCursor( *wxSTANDARD_CURSOR
);
5774 // we need to capture mouse when resizing
5775 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5776 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5778 if ( captureMouse
&& resize
)
5780 win
->CaptureMouse();
5785 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5788 wxPoint
pos( event
.GetPosition() );
5789 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5791 wxGridCellCoords coords
;
5792 XYToCell( x
, y
, coords
);
5794 int cell_rows
, cell_cols
;
5795 bool isFirstDrag
= !m_isDragging
;
5796 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5797 if ((cell_rows
< 0) || (cell_cols
< 0))
5799 coords
.SetRow(coords
.GetRow() + cell_rows
);
5800 coords
.SetCol(coords
.GetCol() + cell_cols
);
5803 if ( event
.Dragging() )
5805 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5807 // Don't start doing anything until the mouse has been dragged at
5808 // least 3 pixels in any direction...
5811 if (m_startDragPos
== wxDefaultPosition
)
5813 m_startDragPos
= pos
;
5816 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5820 m_isDragging
= true;
5821 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5823 // Hide the edit control, so it
5824 // won't interfere with drag-shrinking.
5825 if ( IsCellEditControlShown() )
5827 HideCellEditControl();
5828 SaveEditControlValue();
5831 // Have we captured the mouse yet?
5834 m_winCapture
= m_gridWin
;
5835 m_winCapture
->CaptureMouse();
5838 if ( coords
!= wxGridNoCellCoords
)
5840 if ( event
.CmdDown() )
5842 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5843 m_selectingKeyboard
= coords
;
5844 HighlightBlock( m_selectingKeyboard
, coords
);
5846 else if ( CanDragCell() )
5850 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5851 m_selectingKeyboard
= coords
;
5853 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5861 if ( !IsSelection() )
5863 HighlightBlock( coords
, coords
);
5867 HighlightBlock( m_currentCellCoords
, coords
);
5871 if (! IsVisible(coords
))
5873 MakeCellVisible(coords
);
5874 // TODO: need to introduce a delay or something here. The
5875 // scrolling is way to fast, at least on MSW - also on GTK.
5879 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5881 int cw
, ch
, left
, dummy
;
5882 m_gridWin
->GetClientSize( &cw
, &ch
);
5883 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5885 wxClientDC
dc( m_gridWin
);
5887 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5888 GetRowMinimalHeight(m_dragRowOrCol
) );
5889 dc
.SetLogicalFunction(wxINVERT
);
5890 if ( m_dragLastPos
>= 0 )
5892 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5894 dc
.DrawLine( left
, y
, left
+cw
, y
);
5897 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5899 int cw
, ch
, dummy
, top
;
5900 m_gridWin
->GetClientSize( &cw
, &ch
);
5901 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5903 wxClientDC
dc( m_gridWin
);
5905 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5906 GetColMinimalWidth(m_dragRowOrCol
) );
5907 dc
.SetLogicalFunction(wxINVERT
);
5908 if ( m_dragLastPos
>= 0 )
5910 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5912 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5919 m_isDragging
= false;
5920 m_startDragPos
= wxDefaultPosition
;
5922 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5923 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5926 if ( event
.Entering() || event
.Leaving() )
5928 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5929 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5934 // ------------ Left button pressed
5936 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5938 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5943 if ( !event
.CmdDown() )
5945 if ( event
.ShiftDown() )
5949 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5950 m_currentCellCoords
.GetCol(),
5953 event
.ControlDown(),
5959 else if ( XToEdgeOfCol(x
) < 0 &&
5960 YToEdgeOfRow(y
) < 0 )
5962 DisableCellEditControl();
5963 MakeCellVisible( coords
);
5965 if ( event
.CmdDown() )
5969 m_selection
->ToggleCellSelection( coords
.GetRow(),
5971 event
.ControlDown(),
5976 m_selectingTopLeft
= wxGridNoCellCoords
;
5977 m_selectingBottomRight
= wxGridNoCellCoords
;
5978 m_selectingKeyboard
= coords
;
5982 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5983 SetCurrentCell( coords
);
5986 if ( m_selection
->GetSelectionMode() !=
5987 wxGrid::wxGridSelectCells
)
5989 HighlightBlock( coords
, coords
);
5997 // ------------ Left double click
5999 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6001 DisableCellEditControl();
6003 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6005 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6010 // we want double click to select a cell and start editing
6011 // (i.e. to behave in same way as sequence of two slow clicks):
6012 m_waitForSlowClick
= true;
6017 // ------------ Left button released
6019 else if ( event
.LeftUp() )
6021 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6025 if (m_winCapture
->HasCapture())
6026 m_winCapture
->ReleaseMouse();
6027 m_winCapture
= NULL
;
6030 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6033 EnableCellEditControl();
6035 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6036 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6037 editor
->StartingClick();
6041 m_waitForSlowClick
= false;
6043 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6044 m_selectingBottomRight
!= wxGridNoCellCoords
)
6048 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6049 m_selectingTopLeft
.GetCol(),
6050 m_selectingBottomRight
.GetRow(),
6051 m_selectingBottomRight
.GetCol(),
6052 event
.ControlDown(),
6058 m_selectingTopLeft
= wxGridNoCellCoords
;
6059 m_selectingBottomRight
= wxGridNoCellCoords
;
6061 // Show the edit control, if it has been hidden for
6063 ShowCellEditControl();
6066 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6068 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6069 DoEndDragResizeRow();
6071 // Note: we are ending the event *after* doing
6072 // default processing in this case
6074 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6076 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6078 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6079 DoEndDragResizeCol();
6081 // Note: we are ending the event *after* doing
6082 // default processing in this case
6084 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6090 // ------------ Right button down
6092 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6094 DisableCellEditControl();
6095 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6100 // no default action at the moment
6104 // ------------ Right double click
6106 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6108 DisableCellEditControl();
6109 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6114 // no default action at the moment
6118 // ------------ Moving and no button action
6120 else if ( event
.Moving() && !event
.IsButton() )
6122 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6124 // out of grid cell area
6125 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6129 int dragRow
= YToEdgeOfRow( y
);
6130 int dragCol
= XToEdgeOfCol( x
);
6132 // Dragging on the corner of a cell to resize in both
6133 // directions is not implemented yet...
6135 if ( dragRow
>= 0 && dragCol
>= 0 )
6137 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6143 m_dragRowOrCol
= dragRow
;
6145 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6147 if ( CanDragRowSize() && CanDragGridSize() )
6148 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
6151 else if ( dragCol
>= 0 )
6153 m_dragRowOrCol
= dragCol
;
6155 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6157 if ( CanDragColSize() && CanDragGridSize() )
6158 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
6161 else // Neither on a row or col edge
6163 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6165 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6171 void wxGrid::DoEndDragResizeRow()
6173 if ( m_dragLastPos
>= 0 )
6175 // erase the last line and resize the row
6177 int cw
, ch
, left
, dummy
;
6178 m_gridWin
->GetClientSize( &cw
, &ch
);
6179 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6181 wxClientDC
dc( m_gridWin
);
6183 dc
.SetLogicalFunction( wxINVERT
);
6184 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6185 HideCellEditControl();
6186 SaveEditControlValue();
6188 int rowTop
= GetRowTop(m_dragRowOrCol
);
6189 SetRowSize( m_dragRowOrCol
,
6190 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6192 if ( !GetBatchCount() )
6194 // Only needed to get the correct rect.y:
6195 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6197 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6198 rect
.width
= m_rowLabelWidth
;
6199 rect
.height
= ch
- rect
.y
;
6200 m_rowLabelWin
->Refresh( true, &rect
);
6203 // if there is a multicell block, paint all of it
6206 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6207 int leftCol
= XToCol(left
);
6208 int rightCol
= internalXToCol(left
+ cw
);
6211 for (i
=leftCol
; i
<rightCol
; i
++)
6213 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6214 if (cell_rows
< subtract_rows
)
6215 subtract_rows
= cell_rows
;
6217 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6218 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6219 rect
.height
= ch
- rect
.y
;
6222 m_gridWin
->Refresh( false, &rect
);
6225 ShowCellEditControl();
6230 void wxGrid::DoEndDragResizeCol()
6232 if ( m_dragLastPos
>= 0 )
6234 // erase the last line and resize the col
6236 int cw
, ch
, dummy
, top
;
6237 m_gridWin
->GetClientSize( &cw
, &ch
);
6238 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6240 wxClientDC
dc( m_gridWin
);
6242 dc
.SetLogicalFunction( wxINVERT
);
6243 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6244 HideCellEditControl();
6245 SaveEditControlValue();
6247 int colLeft
= GetColLeft(m_dragRowOrCol
);
6248 SetColSize( m_dragRowOrCol
,
6249 wxMax( m_dragLastPos
- colLeft
,
6250 GetColMinimalWidth(m_dragRowOrCol
) ) );
6252 if ( !GetBatchCount() )
6254 // Only needed to get the correct rect.x:
6255 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6257 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6258 rect
.width
= cw
- rect
.x
;
6259 rect
.height
= m_colLabelHeight
;
6260 m_colLabelWin
->Refresh( true, &rect
);
6263 // if there is a multicell block, paint all of it
6266 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6267 int topRow
= YToRow(top
);
6268 int bottomRow
= internalYToRow(top
+ cw
);
6271 for (i
=topRow
; i
<bottomRow
; i
++)
6273 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6274 if (cell_cols
< subtract_cols
)
6275 subtract_cols
= cell_cols
;
6278 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6279 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6280 rect
.width
= cw
- rect
.x
;
6284 m_gridWin
->Refresh( false, &rect
);
6287 ShowCellEditControl();
6291 void wxGrid::DoEndDragMoveCol()
6293 //The user clicked on the column but didn't actually drag
6294 if ( m_dragLastPos
< 0 )
6296 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6301 if ( m_moveToCol
== -1 )
6302 newPos
= m_numCols
- 1;
6305 newPos
= GetColPos( m_moveToCol
);
6306 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6310 SetColPos( m_dragRowOrCol
, newPos
);
6313 void wxGrid::SetColPos( int colID
, int newPos
)
6315 if ( m_colAt
.IsEmpty() )
6317 m_colAt
.Alloc( m_numCols
);
6320 for ( i
= 0; i
< m_numCols
; i
++ )
6326 int oldPos
= GetColPos( colID
);
6328 //Reshuffle the m_colAt array
6329 if ( newPos
> oldPos
)
6332 for ( i
= oldPos
; i
< newPos
; i
++ )
6334 m_colAt
[i
] = m_colAt
[i
+1];
6340 for ( i
= oldPos
; i
> newPos
; i
-- )
6342 m_colAt
[i
] = m_colAt
[i
-1];
6346 m_colAt
[newPos
] = colID
;
6348 //Recalculate the column rights
6349 if ( !m_colWidths
.IsEmpty() )
6353 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6355 int colID
= GetColAt( colPos
);
6357 colRight
+= m_colWidths
[colID
];
6358 m_colRights
[colID
] = colRight
;
6362 m_colLabelWin
->Refresh();
6363 m_gridWin
->Refresh();
6368 void wxGrid::EnableDragColMove( bool enable
)
6370 if ( m_canDragColMove
== enable
)
6373 m_canDragColMove
= enable
;
6375 if ( !m_canDragColMove
)
6379 //Recalculate the column rights
6380 if ( !m_colWidths
.IsEmpty() )
6384 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6386 colRight
+= m_colWidths
[colPos
];
6387 m_colRights
[colPos
] = colRight
;
6391 m_colLabelWin
->Refresh();
6392 m_gridWin
->Refresh();
6398 // ------ interaction with data model
6400 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6402 switch ( msg
.GetId() )
6404 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6405 return GetModelValues();
6407 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6408 return SetModelValues();
6410 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6411 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6412 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6413 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6414 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6415 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6416 return Redimension( msg
);
6423 // The behaviour of this function depends on the grid table class
6424 // Clear() function. For the default wxGridStringTable class the
6425 // behavious is to replace all cell contents with wxEmptyString but
6426 // not to change the number of rows or cols.
6428 void wxGrid::ClearGrid()
6432 if (IsCellEditControlEnabled())
6433 DisableCellEditControl();
6436 if (!GetBatchCount())
6437 m_gridWin
->Refresh();
6441 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6443 // TODO: something with updateLabels flag
6447 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6453 if (IsCellEditControlEnabled())
6454 DisableCellEditControl();
6456 bool done
= m_table
->InsertRows( pos
, numRows
);
6459 // the table will have sent the results of the insert row
6460 // operation to this view object as a grid table message
6466 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6468 // TODO: something with updateLabels flag
6472 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6478 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6481 // the table will have sent the results of the append row
6482 // operation to this view object as a grid table message
6488 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6490 // TODO: something with updateLabels flag
6494 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6500 if (IsCellEditControlEnabled())
6501 DisableCellEditControl();
6503 bool done
= m_table
->DeleteRows( pos
, numRows
);
6505 // the table will have sent the results of the delete row
6506 // operation to this view object as a grid table message
6512 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6514 // TODO: something with updateLabels flag
6518 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6524 if (IsCellEditControlEnabled())
6525 DisableCellEditControl();
6527 bool done
= m_table
->InsertCols( pos
, numCols
);
6529 // the table will have sent the results of the insert col
6530 // operation to this view object as a grid table message
6536 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6538 // TODO: something with updateLabels flag
6542 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6548 bool done
= m_table
->AppendCols( numCols
);
6550 // the table will have sent the results of the append col
6551 // operation to this view object as a grid table message
6557 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6559 // TODO: something with updateLabels flag
6563 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6569 if (IsCellEditControlEnabled())
6570 DisableCellEditControl();
6572 bool done
= m_table
->DeleteCols( pos
, numCols
);
6574 // the table will have sent the results of the delete col
6575 // operation to this view object as a grid table message
6582 // ----- event handlers
6585 // Generate a grid event based on a mouse event and
6586 // return the result of ProcessEvent()
6588 int wxGrid::SendEvent( const wxEventType type
,
6590 wxMouseEvent
& mouseEv
)
6592 bool claimed
, vetoed
;
6594 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6596 int rowOrCol
= (row
== -1 ? col
: row
);
6598 wxGridSizeEvent
gridEvt( GetId(),
6602 mouseEv
.GetX() + GetRowLabelSize(),
6603 mouseEv
.GetY() + GetColLabelSize(),
6604 mouseEv
.ControlDown(),
6605 mouseEv
.ShiftDown(),
6607 mouseEv
.MetaDown() );
6609 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6610 vetoed
= !gridEvt
.IsAllowed();
6612 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6614 // Right now, it should _never_ end up here!
6615 wxGridRangeSelectEvent
gridEvt( GetId(),
6619 m_selectingBottomRight
,
6621 mouseEv
.ControlDown(),
6622 mouseEv
.ShiftDown(),
6624 mouseEv
.MetaDown() );
6626 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6627 vetoed
= !gridEvt
.IsAllowed();
6629 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6630 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6631 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6632 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6634 wxPoint pos
= mouseEv
.GetPosition();
6636 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6637 pos
.y
+= GetColLabelSize();
6638 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6639 pos
.x
+= GetRowLabelSize();
6641 wxGridEvent
gridEvt( GetId(),
6648 mouseEv
.ControlDown(),
6649 mouseEv
.ShiftDown(),
6651 mouseEv
.MetaDown() );
6652 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6653 vetoed
= !gridEvt
.IsAllowed();
6657 wxGridEvent
gridEvt( GetId(),
6661 mouseEv
.GetX() + GetRowLabelSize(),
6662 mouseEv
.GetY() + GetColLabelSize(),
6664 mouseEv
.ControlDown(),
6665 mouseEv
.ShiftDown(),
6667 mouseEv
.MetaDown() );
6668 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6669 vetoed
= !gridEvt
.IsAllowed();
6672 // A Veto'd event may not be `claimed' so test this first
6676 return claimed
? 1 : 0;
6679 // Generate a grid event of specified type and return the result
6680 // of ProcessEvent().
6682 int wxGrid::SendEvent( const wxEventType type
,
6685 bool claimed
, vetoed
;
6687 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6689 int rowOrCol
= (row
== -1 ? col
: row
);
6691 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6693 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6694 vetoed
= !gridEvt
.IsAllowed();
6698 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6700 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6701 vetoed
= !gridEvt
.IsAllowed();
6704 // A Veto'd event may not be `claimed' so test this first
6708 return claimed
? 1 : 0;
6711 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6713 // needed to prevent zillions of paint events on MSW
6717 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6719 // Don't do anything if between Begin/EndBatch...
6720 // EndBatch() will do all this on the last nested one anyway.
6721 if (! GetBatchCount())
6723 // Refresh to get correct scrolled position:
6724 wxScrolledWindow::Refresh(eraseb
, rect
);
6728 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6729 int width_label
, width_cell
, height_label
, height_cell
;
6732 // Copy rectangle can get scroll offsets..
6733 rect_x
= rect
->GetX();
6734 rect_y
= rect
->GetY();
6735 rectWidth
= rect
->GetWidth();
6736 rectHeight
= rect
->GetHeight();
6738 width_label
= m_rowLabelWidth
- rect_x
;
6739 if (width_label
> rectWidth
)
6740 width_label
= rectWidth
;
6742 height_label
= m_colLabelHeight
- rect_y
;
6743 if (height_label
> rectHeight
)
6744 height_label
= rectHeight
;
6746 if (rect_x
> m_rowLabelWidth
)
6748 x
= rect_x
- m_rowLabelWidth
;
6749 width_cell
= rectWidth
;
6754 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6757 if (rect_y
> m_colLabelHeight
)
6759 y
= rect_y
- m_colLabelHeight
;
6760 height_cell
= rectHeight
;
6765 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6768 // Paint corner label part intersecting rect.
6769 if ( width_label
> 0 && height_label
> 0 )
6771 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6772 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6775 // Paint col labels part intersecting rect.
6776 if ( width_cell
> 0 && height_label
> 0 )
6778 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6779 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6782 // Paint row labels part intersecting rect.
6783 if ( width_label
> 0 && height_cell
> 0 )
6785 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6786 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6789 // Paint cell area part intersecting rect.
6790 if ( width_cell
> 0 && height_cell
> 0 )
6792 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6793 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6798 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6799 m_colLabelWin
->Refresh(eraseb
, NULL
);
6800 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6801 m_gridWin
->Refresh(eraseb
, NULL
);
6806 void wxGrid::OnSize( wxSizeEvent
& event
)
6808 // position the child windows
6811 // don't call CalcDimensions() from here, the base class handles the size
6816 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6818 if ( m_inOnKeyDown
)
6820 // shouldn't be here - we are going round in circles...
6822 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6825 m_inOnKeyDown
= true;
6827 // propagate the event up and see if it gets processed
6828 wxWindow
*parent
= GetParent();
6829 wxKeyEvent
keyEvt( event
);
6830 keyEvt
.SetEventObject( parent
);
6832 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6834 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6836 if (event
.GetKeyCode() == WXK_RIGHT
)
6837 event
.m_keyCode
= WXK_LEFT
;
6838 else if (event
.GetKeyCode() == WXK_LEFT
)
6839 event
.m_keyCode
= WXK_RIGHT
;
6842 // try local handlers
6843 switch ( event
.GetKeyCode() )
6846 if ( event
.ControlDown() )
6847 MoveCursorUpBlock( event
.ShiftDown() );
6849 MoveCursorUp( event
.ShiftDown() );
6853 if ( event
.ControlDown() )
6854 MoveCursorDownBlock( event
.ShiftDown() );
6856 MoveCursorDown( event
.ShiftDown() );
6860 if ( event
.ControlDown() )
6861 MoveCursorLeftBlock( event
.ShiftDown() );
6863 MoveCursorLeft( event
.ShiftDown() );
6867 if ( event
.ControlDown() )
6868 MoveCursorRightBlock( event
.ShiftDown() );
6870 MoveCursorRight( event
.ShiftDown() );
6874 case WXK_NUMPAD_ENTER
:
6875 if ( event
.ControlDown() )
6877 event
.Skip(); // to let the edit control have the return
6881 if ( GetGridCursorRow() < GetNumberRows()-1 )
6883 MoveCursorDown( event
.ShiftDown() );
6887 // at the bottom of a column
6888 DisableCellEditControl();
6898 if (event
.ShiftDown())
6900 if ( GetGridCursorCol() > 0 )
6902 MoveCursorLeft( false );
6907 DisableCellEditControl();
6912 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6914 MoveCursorRight( false );
6919 DisableCellEditControl();
6925 if ( event
.ControlDown() )
6927 MakeCellVisible( 0, 0 );
6928 SetCurrentCell( 0, 0 );
6937 if ( event
.ControlDown() )
6939 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
6940 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
6957 if ( event
.ControlDown() )
6961 m_selection
->ToggleCellSelection(
6962 m_currentCellCoords
.GetRow(),
6963 m_currentCellCoords
.GetCol(),
6964 event
.ControlDown(),
6972 if ( !IsEditable() )
6973 MoveCursorRight( false );
6984 m_inOnKeyDown
= false;
6987 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6989 // try local handlers
6991 if ( event
.GetKeyCode() == WXK_SHIFT
)
6993 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6994 m_selectingBottomRight
!= wxGridNoCellCoords
)
6998 m_selection
->SelectBlock(
6999 m_selectingTopLeft
.GetRow(),
7000 m_selectingTopLeft
.GetCol(),
7001 m_selectingBottomRight
.GetRow(),
7002 m_selectingBottomRight
.GetCol(),
7003 event
.ControlDown(),
7010 m_selectingTopLeft
= wxGridNoCellCoords
;
7011 m_selectingBottomRight
= wxGridNoCellCoords
;
7012 m_selectingKeyboard
= wxGridNoCellCoords
;
7016 void wxGrid::OnChar( wxKeyEvent
& event
)
7018 // is it possible to edit the current cell at all?
7019 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7021 // yes, now check whether the cells editor accepts the key
7022 int row
= m_currentCellCoords
.GetRow();
7023 int col
= m_currentCellCoords
.GetCol();
7024 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7025 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7027 // <F2> is special and will always start editing, for
7028 // other keys - ask the editor itself
7029 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7030 || editor
->IsAcceptedKey(event
) )
7032 // ensure cell is visble
7033 MakeCellVisible(row
, col
);
7034 EnableCellEditControl();
7036 // a problem can arise if the cell is not completely
7037 // visible (even after calling MakeCellVisible the
7038 // control is not created and calling StartingKey will
7040 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7041 editor
->StartingKey(event
);
7057 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7061 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7063 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7065 // the event has been intercepted - do nothing
7069 wxClientDC
dc( m_gridWin
);
7072 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7074 DisableCellEditControl();
7076 if ( IsVisible( m_currentCellCoords
, false ) )
7079 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7080 if ( !m_gridLinesEnabled
)
7088 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7090 // Otherwise refresh redraws the highlight!
7091 m_currentCellCoords
= coords
;
7093 DrawGridCellArea( dc
, cells
);
7094 DrawAllGridLines( dc
, r
);
7098 m_currentCellCoords
= coords
;
7100 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7101 DrawCellHighlight( dc
, attr
);
7105 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7108 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7112 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7115 rightCol
= GetNumberCols() - 1;
7117 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7120 bottomRow
= GetNumberRows() - 1;
7124 if ( topRow
> bottomRow
)
7131 if ( leftCol
> rightCol
)
7138 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7139 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7141 // First the case that we selected a completely new area
7142 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7143 m_selectingBottomRight
== wxGridNoCellCoords
)
7146 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7147 wxGridCellCoords ( bottomRow
, rightCol
) );
7148 m_gridWin
->Refresh( false, &rect
);
7151 // Now handle changing an existing selection area.
7152 else if ( m_selectingTopLeft
!= updateTopLeft
||
7153 m_selectingBottomRight
!= updateBottomRight
)
7155 // Compute two optimal update rectangles:
7156 // Either one rectangle is a real subset of the
7157 // other, or they are (almost) disjoint!
7159 bool need_refresh
[4];
7163 need_refresh
[3] = false;
7166 // Store intermediate values
7167 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7168 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7169 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7170 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7172 // Determine the outer/inner coordinates.
7173 if (oldLeft
> leftCol
)
7179 if (oldTop
> topRow
)
7185 if (oldRight
< rightCol
)
7188 oldRight
= rightCol
;
7191 if (oldBottom
< bottomRow
)
7194 oldBottom
= bottomRow
;
7198 // Now, either the stuff marked old is the outer
7199 // rectangle or we don't have a situation where one
7200 // is contained in the other.
7202 if ( oldLeft
< leftCol
)
7204 // Refresh the newly selected or deselected
7205 // area to the left of the old or new selection.
7206 need_refresh
[0] = true;
7207 rect
[0] = BlockToDeviceRect(
7208 wxGridCellCoords( oldTop
, oldLeft
),
7209 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7212 if ( oldTop
< topRow
)
7214 // Refresh the newly selected or deselected
7215 // area above the old or new selection.
7216 need_refresh
[1] = true;
7217 rect
[1] = BlockToDeviceRect(
7218 wxGridCellCoords( oldTop
, leftCol
),
7219 wxGridCellCoords( topRow
- 1, rightCol
) );
7222 if ( oldRight
> rightCol
)
7224 // Refresh the newly selected or deselected
7225 // area to the right of the old or new selection.
7226 need_refresh
[2] = true;
7227 rect
[2] = BlockToDeviceRect(
7228 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7229 wxGridCellCoords( oldBottom
, oldRight
) );
7232 if ( oldBottom
> bottomRow
)
7234 // Refresh the newly selected or deselected
7235 // area below the old or new selection.
7236 need_refresh
[3] = true;
7237 rect
[3] = BlockToDeviceRect(
7238 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7239 wxGridCellCoords( oldBottom
, rightCol
) );
7242 // various Refresh() calls
7243 for (i
= 0; i
< 4; i
++ )
7244 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7245 m_gridWin
->Refresh( false, &(rect
[i
]) );
7249 m_selectingTopLeft
= updateTopLeft
;
7250 m_selectingBottomRight
= updateBottomRight
;
7254 // ------ functions to get/send data (see also public functions)
7257 bool wxGrid::GetModelValues()
7259 // Hide the editor, so it won't hide a changed value.
7260 HideCellEditControl();
7264 // all we need to do is repaint the grid
7266 m_gridWin
->Refresh();
7273 bool wxGrid::SetModelValues()
7277 // Disable the editor, so it won't hide a changed value.
7278 // Do we also want to save the current value of the editor first?
7280 DisableCellEditControl();
7284 for ( row
= 0; row
< m_numRows
; row
++ )
7286 for ( col
= 0; col
< m_numCols
; col
++ )
7288 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7298 // Note - this function only draws cells that are in the list of
7299 // exposed cells (usually set from the update region by
7300 // CalcExposedCells)
7302 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7304 if ( !m_numRows
|| !m_numCols
)
7307 int i
, numCells
= cells
.GetCount();
7308 int row
, col
, cell_rows
, cell_cols
;
7309 wxGridCellCoordsArray redrawCells
;
7311 for ( i
= numCells
- 1; i
>= 0; i
-- )
7313 row
= cells
[i
].GetRow();
7314 col
= cells
[i
].GetCol();
7315 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7317 // If this cell is part of a multicell block, find owner for repaint
7318 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7320 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7321 bool marked
= false;
7322 for ( int j
= 0; j
< numCells
; j
++ )
7324 if ( cell
== cells
[j
] )
7333 int count
= redrawCells
.GetCount();
7334 for (int j
= 0; j
< count
; j
++)
7336 if ( cell
== redrawCells
[j
] )
7344 redrawCells
.Add( cell
);
7347 // don't bother drawing this cell
7351 // If this cell is empty, find cell to left that might want to overflow
7352 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7354 for ( int l
= 0; l
< cell_rows
; l
++ )
7356 // find a cell in this row to leave already marked for repaint
7358 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7359 if ((redrawCells
[k
].GetCol() < left
) &&
7360 (redrawCells
[k
].GetRow() == row
))
7362 left
= redrawCells
[k
].GetCol();
7366 left
= 0; // oh well
7368 for (int j
= col
- 1; j
>= left
; j
--)
7370 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7372 if (GetCellOverflow(row
+ l
, j
))
7374 wxGridCellCoords
cell(row
+ l
, j
);
7375 bool marked
= false;
7377 for (int k
= 0; k
< numCells
; k
++)
7379 if ( cell
== cells
[k
] )
7388 int count
= redrawCells
.GetCount();
7389 for (int k
= 0; k
< count
; k
++)
7391 if ( cell
== redrawCells
[k
] )
7398 redrawCells
.Add( cell
);
7407 DrawCell( dc
, cells
[i
] );
7410 numCells
= redrawCells
.GetCount();
7412 for ( i
= numCells
- 1; i
>= 0; i
-- )
7414 DrawCell( dc
, redrawCells
[i
] );
7418 void wxGrid::DrawGridSpace( wxDC
& dc
)
7421 m_gridWin
->GetClientSize( &cw
, &ch
);
7424 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7426 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7427 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7429 if ( right
> rightCol
|| bottom
> bottomRow
)
7432 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7434 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7435 dc
.SetPen( *wxTRANSPARENT_PEN
);
7437 if ( right
> rightCol
)
7439 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7442 if ( bottom
> bottomRow
)
7444 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7449 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7451 int row
= coords
.GetRow();
7452 int col
= coords
.GetCol();
7454 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7457 // we draw the cell border ourselves
7458 #if !WXGRID_DRAW_LINES
7459 if ( m_gridLinesEnabled
)
7460 DrawCellBorder( dc
, coords
);
7463 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7465 bool isCurrent
= coords
== m_currentCellCoords
;
7467 wxRect rect
= CellToRect( row
, col
);
7469 // if the editor is shown, we should use it and not the renderer
7470 // Note: However, only if it is really _shown_, i.e. not hidden!
7471 if ( isCurrent
&& IsCellEditControlShown() )
7473 // NB: this "#if..." is temporary and fixes a problem where the
7474 // edit control is erased by this code after being rendered.
7475 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7476 // implicitly, causing this out-of order render.
7477 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7478 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7479 editor
->PaintBackground(rect
, attr
);
7485 // but all the rest is drawn by the cell renderer and hence may be customized
7486 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7487 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7494 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7496 int row
= m_currentCellCoords
.GetRow();
7497 int col
= m_currentCellCoords
.GetCol();
7499 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7502 wxRect rect
= CellToRect(row
, col
);
7504 // hmmm... what could we do here to show that the cell is disabled?
7505 // for now, I just draw a thinner border than for the other ones, but
7506 // it doesn't look really good
7508 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7512 // The center of the drawn line is where the position/width/height of
7513 // the rectangle is actually at (on wxMSW at least), so the
7514 // size of the rectangle is reduced to compensate for the thickness of
7515 // the line. If this is too strange on non-wxMSW platforms then
7516 // please #ifdef this appropriately.
7517 rect
.x
+= penWidth
/ 2;
7518 rect
.y
+= penWidth
/ 2;
7519 rect
.width
-= penWidth
- 1;
7520 rect
.height
-= penWidth
- 1;
7522 // Now draw the rectangle
7523 // use the cellHighlightColour if the cell is inside a selection, this
7524 // will ensure the cell is always visible.
7525 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7526 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7527 dc
.DrawRectangle(rect
);
7531 // VZ: my experiments with 3D borders...
7533 // how to properly set colours for arbitrary bg?
7534 wxCoord x1
= rect
.x
,
7536 x2
= rect
.x
+ rect
.width
- 1,
7537 y2
= rect
.y
+ rect
.height
- 1;
7539 dc
.SetPen(*wxWHITE_PEN
);
7540 dc
.DrawLine(x1
, y1
, x2
, y1
);
7541 dc
.DrawLine(x1
, y1
, x1
, y2
);
7543 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7544 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7546 dc
.SetPen(*wxBLACK_PEN
);
7547 dc
.DrawLine(x1
, y2
, x2
, y2
);
7548 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7552 wxPen
wxGrid::GetDefaultGridLinePen()
7554 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7557 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7559 return GetDefaultGridLinePen();
7562 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7564 return GetDefaultGridLinePen();
7567 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7569 int row
= coords
.GetRow();
7570 int col
= coords
.GetCol();
7571 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7575 wxRect rect
= CellToRect( row
, col
);
7577 // right hand border
7578 dc
.SetPen( GetColGridLinePen(col
) );
7579 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7580 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7583 dc
.SetPen( GetRowGridLinePen(row
) );
7584 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7585 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7588 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7590 // This if block was previously in wxGrid::OnPaint but that doesn't
7591 // seem to get called under wxGTK - MB
7593 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7594 m_numRows
&& m_numCols
)
7596 m_currentCellCoords
.Set(0, 0);
7599 if ( IsCellEditControlShown() )
7601 // don't show highlight when the edit control is shown
7605 // if the active cell was repainted, repaint its highlight too because it
7606 // might have been damaged by the grid lines
7607 size_t count
= cells
.GetCount();
7608 for ( size_t n
= 0; n
< count
; n
++ )
7610 if ( cells
[n
] == m_currentCellCoords
)
7612 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7613 DrawCellHighlight(dc
, attr
);
7621 // TODO: remove this ???
7622 // This is used to redraw all grid lines e.g. when the grid line colour
7625 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7627 #if !WXGRID_DRAW_LINES
7631 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7634 int top
, bottom
, left
, right
;
7636 #if 0 //#ifndef __WXGTK__
7640 m_gridWin
->GetClientSize(&cw
, &ch
);
7642 // virtual coords of visible area
7644 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7645 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7650 reg
.GetBox(x
, y
, w
, h
);
7651 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7652 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7656 m_gridWin
->GetClientSize(&cw
, &ch
);
7657 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7658 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7661 // avoid drawing grid lines past the last row and col
7663 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7664 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7666 // no gridlines inside multicells, clip them out
7667 int leftCol
= GetColPos( internalXToCol(left
) );
7668 int topRow
= internalYToRow(top
);
7669 int rightCol
= GetColPos( internalXToCol(right
) );
7670 int bottomRow
= internalYToRow(bottom
);
7673 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7674 wxRegion
clippedcells(0, 0, cw
, ch
);
7676 int i
, j
, cell_rows
, cell_cols
;
7679 for (j
=topRow
; j
<bottomRow
; j
++)
7682 for (colPos
=leftCol
; colPos
<rightCol
; colPos
++)
7684 i
= GetColAt( colPos
);
7686 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7687 if ((cell_rows
> 1) || (cell_cols
> 1))
7689 rect
= CellToRect(j
,i
);
7690 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7691 clippedcells
.Subtract(rect
);
7693 else if ((cell_rows
< 0) || (cell_cols
< 0))
7695 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7696 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7697 clippedcells
.Subtract(rect
);
7702 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7704 int i
, j
, cell_rows
, cell_cols
;
7707 for (j
=topRow
; j
<bottomRow
; j
++)
7709 for (i
=leftCol
; i
<rightCol
; i
++)
7711 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7712 if ((cell_rows
> 1) || (cell_cols
> 1))
7714 rect
= CellToRect(j
, i
);
7715 clippedcells
.Subtract(rect
);
7717 else if ((cell_rows
< 0) || (cell_cols
< 0))
7719 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7720 clippedcells
.Subtract(rect
);
7726 dc
.SetClippingRegion( clippedcells
);
7729 // horizontal grid lines
7731 // already declared above - int i;
7732 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7734 int bot
= GetRowBottom(i
) - 1;
7743 dc
.SetPen( GetRowGridLinePen(i
) );
7744 dc
.DrawLine( left
, bot
, right
, bot
);
7748 // vertical grid lines
7751 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7753 i
= GetColAt( colPos
);
7755 int colRight
= GetColRight(i
);
7757 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7761 if ( colRight
> right
)
7766 if ( colRight
>= left
)
7768 dc
.SetPen( GetColGridLinePen(i
) );
7769 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7773 dc
.DestroyClippingRegion();
7776 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7782 size_t numLabels
= rows
.GetCount();
7784 for ( i
= 0; i
< numLabels
; i
++ )
7786 DrawRowLabel( dc
, rows
[i
] );
7790 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7792 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7799 rect
.SetY( GetRowTop(row
) + 1 );
7800 rect
.SetWidth( m_rowLabelWidth
- 2 );
7801 rect
.SetHeight( GetRowHeight(row
) - 2 );
7803 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7805 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7807 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7809 int rowTop
= GetRowTop(row
),
7810 rowBottom
= GetRowBottom(row
) - 1;
7812 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7813 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7814 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7815 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7817 dc
.SetPen( *wxWHITE_PEN
);
7818 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7819 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7822 dc
.SetBackgroundMode( wxTRANSPARENT
);
7823 dc
.SetTextForeground( GetLabelTextColour() );
7824 dc
.SetFont( GetLabelFont() );
7827 GetRowLabelAlignment( &hAlign
, &vAlign
);
7830 rect
.SetY( GetRowTop(row
) + 2 );
7831 rect
.SetWidth( m_rowLabelWidth
- 4 );
7832 rect
.SetHeight( GetRowHeight(row
) - 4 );
7833 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7836 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7842 size_t numLabels
= cols
.GetCount();
7844 for ( i
= 0; i
< numLabels
; i
++ )
7846 DrawColLabel( dc
, cols
[i
] );
7850 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7852 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7855 int colLeft
= GetColLeft(col
);
7860 rect
.SetX( colLeft
+ 1 );
7862 rect
.SetWidth( GetColWidth(col
) - 2 );
7863 rect
.SetHeight( m_colLabelHeight
- 2 );
7865 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7867 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7869 int colRight
= GetColRight(col
) - 1;
7871 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7872 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7873 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7874 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7875 colRight
+ 1, m_colLabelHeight
- 1 );
7877 dc
.SetPen( *wxWHITE_PEN
);
7878 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7879 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7882 dc
.SetBackgroundMode( wxTRANSPARENT
);
7883 dc
.SetTextForeground( GetLabelTextColour() );
7884 dc
.SetFont( GetLabelFont() );
7886 int hAlign
, vAlign
, orient
;
7887 GetColLabelAlignment( &hAlign
, &vAlign
);
7888 orient
= GetColLabelTextOrientation();
7890 rect
.SetX( colLeft
+ 2 );
7892 rect
.SetWidth( GetColWidth(col
) - 4 );
7893 rect
.SetHeight( m_colLabelHeight
- 4 );
7894 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7897 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7898 const wxString
& value
,
7902 int textOrientation
)
7904 wxArrayString lines
;
7906 StringToLines( value
, lines
);
7908 // Forward to new API.
7909 DrawTextRectangle( dc
,
7917 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
7918 // add textOrientation support
7919 void wxGrid::DrawTextRectangle(wxDC
& dc
,
7920 const wxArrayString
& lines
,
7924 int textOrientation
)
7926 if ( lines
.empty() )
7929 wxDCClipper
clip(dc
, rect
);
7934 if ( textOrientation
== wxHORIZONTAL
)
7935 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
7937 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7941 switch ( vertAlign
)
7943 case wxALIGN_BOTTOM
:
7944 if ( textOrientation
== wxHORIZONTAL
)
7945 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7947 x
= rect
.x
+ rect
.width
- textWidth
;
7950 case wxALIGN_CENTRE
:
7951 if ( textOrientation
== wxHORIZONTAL
)
7952 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
7954 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
7959 if ( textOrientation
== wxHORIZONTAL
)
7966 // Align each line of a multi-line label
7967 size_t nLines
= lines
.GetCount();
7968 for ( size_t l
= 0; l
< nLines
; l
++ )
7970 const wxString
& line
= lines
[l
];
7974 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
7980 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
7982 switch ( horizAlign
)
7985 if ( textOrientation
== wxHORIZONTAL
)
7986 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7988 y
= rect
.y
+ lineWidth
+ 1;
7991 case wxALIGN_CENTRE
:
7992 if ( textOrientation
== wxHORIZONTAL
)
7993 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
7995 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8000 if ( textOrientation
== wxHORIZONTAL
)
8003 y
= rect
.y
+ rect
.height
- 1;
8007 if ( textOrientation
== wxHORIZONTAL
)
8009 dc
.DrawText( line
, x
, y
);
8014 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8020 // Split multi-line text up into an array of strings.
8021 // Any existing contents of the string array are preserved.
8023 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
8027 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8028 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8030 while ( startPos
< (int)tVal
.length() )
8032 pos
= tVal
.Mid(startPos
).Find( eol
);
8037 else if ( pos
== 0 )
8039 lines
.Add( wxEmptyString
);
8043 lines
.Add( value
.Mid(startPos
, pos
) );
8046 startPos
+= pos
+ 1;
8049 if ( startPos
< (int)value
.length() )
8051 lines
.Add( value
.Mid( startPos
) );
8055 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8056 const wxArrayString
& lines
,
8057 long *width
, long *height
)
8061 long lineW
= 0, lineH
= 0;
8064 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8066 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8067 w
= wxMax( w
, lineW
);
8076 // ------ Batch processing.
8078 void wxGrid::EndBatch()
8080 if ( m_batchCount
> 0 )
8083 if ( !m_batchCount
)
8086 m_rowLabelWin
->Refresh();
8087 m_colLabelWin
->Refresh();
8088 m_cornerLabelWin
->Refresh();
8089 m_gridWin
->Refresh();
8094 // Use this, rather than wxWindow::Refresh(), to force an immediate
8095 // repainting of the grid. Has no effect if you are already inside a
8096 // BeginBatch / EndBatch block.
8098 void wxGrid::ForceRefresh()
8104 bool wxGrid::Enable(bool enable
)
8106 if ( !wxScrolledWindow::Enable(enable
) )
8109 // redraw in the new state
8110 m_gridWin
->Refresh();
8116 // ------ Edit control functions
8119 void wxGrid::EnableEditing( bool edit
)
8121 // TODO: improve this ?
8123 if ( edit
!= m_editable
)
8126 EnableCellEditControl(edit
);
8131 void wxGrid::EnableCellEditControl( bool enable
)
8136 if ( enable
!= m_cellEditCtrlEnabled
)
8140 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8143 // this should be checked by the caller!
8144 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8146 // do it before ShowCellEditControl()
8147 m_cellEditCtrlEnabled
= enable
;
8149 ShowCellEditControl();
8153 //FIXME:add veto support
8154 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8156 HideCellEditControl();
8157 SaveEditControlValue();
8159 // do it after HideCellEditControl()
8160 m_cellEditCtrlEnabled
= enable
;
8165 bool wxGrid::IsCurrentCellReadOnly() const
8168 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8169 bool readonly
= attr
->IsReadOnly();
8175 bool wxGrid::CanEnableCellControl() const
8177 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8178 !IsCurrentCellReadOnly();
8181 bool wxGrid::IsCellEditControlEnabled() const
8183 // the cell edit control might be disable for all cells or just for the
8184 // current one if it's read only
8185 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8188 bool wxGrid::IsCellEditControlShown() const
8190 bool isShown
= false;
8192 if ( m_cellEditCtrlEnabled
)
8194 int row
= m_currentCellCoords
.GetRow();
8195 int col
= m_currentCellCoords
.GetCol();
8196 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8197 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8202 if ( editor
->IsCreated() )
8204 isShown
= editor
->GetControl()->IsShown();
8214 void wxGrid::ShowCellEditControl()
8216 if ( IsCellEditControlEnabled() )
8218 if ( !IsVisible( m_currentCellCoords
, false ) )
8220 m_cellEditCtrlEnabled
= false;
8225 wxRect rect
= CellToRect( m_currentCellCoords
);
8226 int row
= m_currentCellCoords
.GetRow();
8227 int col
= m_currentCellCoords
.GetCol();
8229 // if this is part of a multicell, find owner (topleft)
8230 int cell_rows
, cell_cols
;
8231 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8232 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8236 m_currentCellCoords
.SetRow( row
);
8237 m_currentCellCoords
.SetCol( col
);
8240 // erase the highlight and the cell contents because the editor
8241 // might not cover the entire cell
8242 wxClientDC
dc( m_gridWin
);
8244 dc
.SetBrush(wxBrush(GetCellAttr(row
, col
)->GetBackgroundColour(), wxSOLID
));
8245 dc
.SetPen(*wxTRANSPARENT_PEN
);
8246 dc
.DrawRectangle(rect
);
8248 // convert to scrolled coords
8249 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8255 // cell is shifted by one pixel
8256 // However, don't allow x or y to become negative
8257 // since the SetSize() method interprets that as
8264 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8265 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8266 if ( !editor
->IsCreated() )
8268 editor
->Create(m_gridWin
, wxID_ANY
,
8269 new wxGridCellEditorEvtHandler(this, editor
));
8271 wxGridEditorCreatedEvent
evt(GetId(),
8272 wxEVT_GRID_EDITOR_CREATED
,
8276 editor
->GetControl());
8277 GetEventHandler()->ProcessEvent(evt
);
8280 // resize editor to overflow into righthand cells if allowed
8281 int maxWidth
= rect
.width
;
8282 wxString value
= GetCellValue(row
, col
);
8283 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8286 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8287 if (maxWidth
< rect
.width
)
8288 maxWidth
= rect
.width
;
8291 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8292 if (rect
.x
+ maxWidth
> client_right
)
8293 maxWidth
= client_right
- rect
.x
;
8295 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8297 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8298 // may have changed earlier
8299 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8302 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8304 // looks weird going over a multicell
8305 if (m_table
->IsEmptyCell( row
, i
) &&
8306 (rect
.width
< maxWidth
) && (c_rows
== 1))
8308 rect
.width
+= GetColWidth( i
);
8314 if (rect
.GetRight() > client_right
)
8315 rect
.SetRight( client_right
- 1 );
8318 editor
->SetCellAttr( attr
);
8319 editor
->SetSize( rect
);
8321 editor
->GetControl()->Move(
8322 editor
->GetControl()->GetPosition().x
+ nXMove
,
8323 editor
->GetControl()->GetPosition().y
);
8324 editor
->Show( true, attr
);
8326 // recalc dimensions in case we need to
8327 // expand the scrolled window to account for editor
8330 editor
->BeginEdit(row
, col
, this);
8331 editor
->SetCellAttr(NULL
);
8339 void wxGrid::HideCellEditControl()
8341 if ( IsCellEditControlEnabled() )
8343 int row
= m_currentCellCoords
.GetRow();
8344 int col
= m_currentCellCoords
.GetCol();
8346 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8347 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8348 editor
->Show( false );
8352 m_gridWin
->SetFocus();
8354 // refresh whole row to the right
8355 wxRect
rect( CellToRect(row
, col
) );
8356 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8357 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8360 // ensure that the pixels under the focus ring get refreshed as well
8361 rect
.Inflate(10, 10);
8364 m_gridWin
->Refresh( false, &rect
);
8368 void wxGrid::SaveEditControlValue()
8370 if ( IsCellEditControlEnabled() )
8372 int row
= m_currentCellCoords
.GetRow();
8373 int col
= m_currentCellCoords
.GetCol();
8375 wxString oldval
= GetCellValue(row
, col
);
8377 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8378 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8379 bool changed
= editor
->EndEdit(row
, col
, this);
8386 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8387 m_currentCellCoords
.GetRow(),
8388 m_currentCellCoords
.GetCol() ) < 0 )
8390 // Event has been vetoed, set the data back.
8391 SetCellValue(row
, col
, oldval
);
8398 // ------ Grid location functions
8399 // Note that all of these functions work with the logical coordinates of
8400 // grid cells and labels so you will need to convert from device
8401 // coordinates for mouse events etc.
8404 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
8406 int row
= YToRow(y
);
8407 int col
= XToCol(x
);
8409 if ( row
== -1 || col
== -1 )
8411 coords
= wxGridNoCellCoords
;
8415 coords
.Set( row
, col
);
8419 // Internal Helper function for computing row or column from some
8420 // (unscrolled) coordinate value, using either
8421 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8422 // of m_rowBottoms/m_ColRights to speed up the search!
8424 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8425 const wxArrayInt
& BorderArray
, int nMax
,
8429 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8434 size_t i_max
= coord
/ defaultDist
,
8437 if (BorderArray
.IsEmpty())
8439 if ((int) i_max
< nMax
)
8441 return clipToMinMax
? nMax
- 1 : -1;
8444 if ( i_max
>= BorderArray
.GetCount())
8446 i_max
= BorderArray
.GetCount() - 1;
8450 if ( coord
>= BorderArray
[i_max
])
8454 i_max
= coord
/ minDist
;
8456 i_max
= BorderArray
.GetCount() - 1;
8459 if ( i_max
>= BorderArray
.GetCount())
8460 i_max
= BorderArray
.GetCount() - 1;
8463 if ( coord
>= BorderArray
[i_max
])
8464 return clipToMinMax
? (int)i_max
: -1;
8465 if ( coord
< BorderArray
[0] )
8468 while ( i_max
- i_min
> 0 )
8470 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8471 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8472 if (coord
>= BorderArray
[ i_max
- 1])
8476 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8477 if (coord
< BorderArray
[median
])
8486 int wxGrid::YToRow( int y
)
8488 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8489 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8492 int wxGrid::XToCol( int x
, bool clipToMinMax
)
8495 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8497 if (!m_defaultColWidth
)
8498 m_defaultColWidth
= 1;
8500 int maxPos
= x
/ m_defaultColWidth
;
8503 if (m_colRights
.IsEmpty())
8505 if(maxPos
< m_numCols
)
8506 return GetColAt( maxPos
);
8507 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8510 if ( maxPos
>= m_numCols
)
8511 maxPos
= m_numCols
- 1;
8514 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8517 if (m_minAcceptableColWidth
)
8518 maxPos
= x
/ m_minAcceptableColWidth
;
8520 maxPos
= m_numCols
- 1;
8522 if ( maxPos
>= m_numCols
)
8523 maxPos
= m_numCols
- 1;
8526 //X is beyond the last column
8527 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8528 return clipToMinMax
? GetColAt( maxPos
) : -1;
8530 //X is before the first column
8531 if ( x
< m_colRights
[GetColAt( 0 )] )
8532 return GetColAt( 0 );
8534 //Perform a binary search
8535 while ( maxPos
- minPos
> 0 )
8537 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8538 0, _T("wxGrid: internal error in XToCol"));
8540 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8541 return GetColAt( maxPos
);
8544 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8545 if (x
< m_colRights
[GetColAt( median
)])
8550 return GetColAt( maxPos
);
8553 // return the row number that that the y coord is near
8554 // the edge of, or -1 if not near an edge.
8555 // coords can only possibly be near an edge if
8556 // (a) the row/column is large enough to still allow for an "inner" area
8557 // that is _not_ nead the edge (i.e., if the height/width is smaller
8558 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8561 // (b) resizing rows/columns (the thing for which edge detection is
8562 // relevant at all) is enabled.
8564 int wxGrid::YToEdgeOfRow( int y
)
8567 i
= internalYToRow(y
);
8569 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8571 // We know that we are in row i, test whether we are
8572 // close enough to lower or upper border, respectively.
8573 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8575 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8582 // return the col number that that the x coord is near the edge of, or
8583 // -1 if not near an edge
8584 // See comment at YToEdgeOfRow for conditions on edge detection.
8586 int wxGrid::XToEdgeOfCol( int x
)
8589 i
= internalXToCol(x
);
8591 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8593 // We know that we are in column i; test whether we are
8594 // close enough to right or left border, respectively.
8595 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8597 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8604 wxRect
wxGrid::CellToRect( int row
, int col
)
8606 wxRect
rect( -1, -1, -1, -1 );
8608 if ( row
>= 0 && row
< m_numRows
&&
8609 col
>= 0 && col
< m_numCols
)
8611 int i
, cell_rows
, cell_cols
;
8612 rect
.width
= rect
.height
= 0;
8613 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8614 // if negative then find multicell owner
8619 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8621 rect
.x
= GetColLeft(col
);
8622 rect
.y
= GetRowTop(row
);
8623 for (i
=col
; i
< col
+ cell_cols
; i
++)
8624 rect
.width
+= GetColWidth(i
);
8625 for (i
=row
; i
< row
+ cell_rows
; i
++)
8626 rect
.height
+= GetRowHeight(i
);
8629 // if grid lines are enabled, then the area of the cell is a bit smaller
8630 if (m_gridLinesEnabled
)
8639 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8641 // get the cell rectangle in logical coords
8643 wxRect
r( CellToRect( row
, col
) );
8645 // convert to device coords
8647 int left
, top
, right
, bottom
;
8648 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8649 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8651 // check against the client area of the grid window
8653 m_gridWin
->GetClientSize( &cw
, &ch
);
8655 if ( wholeCellVisible
)
8657 // is the cell wholly visible ?
8658 return ( left
>= 0 && right
<= cw
&&
8659 top
>= 0 && bottom
<= ch
);
8663 // is the cell partly visible ?
8665 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8666 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8670 // make the specified cell location visible by doing a minimal amount
8673 void wxGrid::MakeCellVisible( int row
, int col
)
8676 int xpos
= -1, ypos
= -1;
8678 if ( row
>= 0 && row
< m_numRows
&&
8679 col
>= 0 && col
< m_numCols
)
8681 // get the cell rectangle in logical coords
8682 wxRect
r( CellToRect( row
, col
) );
8684 // convert to device coords
8685 int left
, top
, right
, bottom
;
8686 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8687 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8690 m_gridWin
->GetClientSize( &cw
, &ch
);
8696 else if ( bottom
> ch
)
8698 int h
= r
.GetHeight();
8700 for ( i
= row
- 1; i
>= 0; i
-- )
8702 int rowHeight
= GetRowHeight(i
);
8703 if ( h
+ rowHeight
> ch
)
8710 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8711 // have rounding errors (this is important, because if we do,
8712 // we might not scroll at all and some cells won't be redrawn)
8714 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8715 // so just add a full scroll unit...
8716 ypos
+= m_scrollLineY
;
8719 // special handling for wide cells - show always left part of the cell!
8720 // Otherwise, e.g. when stepping from row to row, it would jump between
8721 // left and right part of the cell on every step!
8723 if ( left
< 0 || (right
- left
) >= cw
)
8727 else if ( right
> cw
)
8729 // position the view so that the cell is on the right
8731 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8732 xpos
= x0
+ (right
- cw
);
8734 // see comment for ypos above
8735 xpos
+= m_scrollLineX
;
8738 if ( xpos
!= -1 || ypos
!= -1 )
8741 xpos
/= m_scrollLineX
;
8743 ypos
/= m_scrollLineY
;
8744 Scroll( xpos
, ypos
);
8751 // ------ Grid cursor movement functions
8754 bool wxGrid::MoveCursorUp( bool expandSelection
)
8756 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8757 m_currentCellCoords
.GetRow() >= 0 )
8759 if ( expandSelection
)
8761 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8762 m_selectingKeyboard
= m_currentCellCoords
;
8763 if ( m_selectingKeyboard
.GetRow() > 0 )
8765 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8766 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8767 m_selectingKeyboard
.GetCol() );
8768 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8771 else if ( m_currentCellCoords
.GetRow() > 0 )
8773 int row
= m_currentCellCoords
.GetRow() - 1;
8774 int col
= m_currentCellCoords
.GetCol();
8776 MakeCellVisible( row
, col
);
8777 SetCurrentCell( row
, col
);
8788 bool wxGrid::MoveCursorDown( bool expandSelection
)
8790 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8791 m_currentCellCoords
.GetRow() < m_numRows
)
8793 if ( expandSelection
)
8795 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8796 m_selectingKeyboard
= m_currentCellCoords
;
8797 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8799 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8800 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8801 m_selectingKeyboard
.GetCol() );
8802 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8805 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8807 int row
= m_currentCellCoords
.GetRow() + 1;
8808 int col
= m_currentCellCoords
.GetCol();
8810 MakeCellVisible( row
, col
);
8811 SetCurrentCell( row
, col
);
8822 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8824 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8825 m_currentCellCoords
.GetCol() >= 0 )
8827 if ( expandSelection
)
8829 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8830 m_selectingKeyboard
= m_currentCellCoords
;
8831 if ( m_selectingKeyboard
.GetCol() > 0 )
8833 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8834 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8835 m_selectingKeyboard
.GetCol() );
8836 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8839 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8841 int row
= m_currentCellCoords
.GetRow();
8842 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8845 MakeCellVisible( row
, col
);
8846 SetCurrentCell( row
, col
);
8857 bool wxGrid::MoveCursorRight( bool expandSelection
)
8859 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8860 m_currentCellCoords
.GetCol() < m_numCols
)
8862 if ( expandSelection
)
8864 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8865 m_selectingKeyboard
= m_currentCellCoords
;
8866 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8868 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8869 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8870 m_selectingKeyboard
.GetCol() );
8871 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8874 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
8876 int row
= m_currentCellCoords
.GetRow();
8877 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
8880 MakeCellVisible( row
, col
);
8881 SetCurrentCell( row
, col
);
8892 bool wxGrid::MovePageUp()
8894 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8897 int row
= m_currentCellCoords
.GetRow();
8901 m_gridWin
->GetClientSize( &cw
, &ch
);
8903 int y
= GetRowTop(row
);
8904 int newRow
= internalYToRow( y
- ch
+ 1 );
8906 if ( newRow
== row
)
8908 // row > 0, so newRow can never be less than 0 here.
8912 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8913 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8921 bool wxGrid::MovePageDown()
8923 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8926 int row
= m_currentCellCoords
.GetRow();
8927 if ( (row
+ 1) < m_numRows
)
8930 m_gridWin
->GetClientSize( &cw
, &ch
);
8932 int y
= GetRowTop(row
);
8933 int newRow
= internalYToRow( y
+ ch
);
8934 if ( newRow
== row
)
8936 // row < m_numRows, so newRow can't overflow here.
8940 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8941 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8949 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8952 m_currentCellCoords
!= wxGridNoCellCoords
&&
8953 m_currentCellCoords
.GetRow() > 0 )
8955 int row
= m_currentCellCoords
.GetRow();
8956 int col
= m_currentCellCoords
.GetCol();
8958 if ( m_table
->IsEmptyCell(row
, col
) )
8960 // starting in an empty cell: find the next block of
8966 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8970 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
8972 // starting at the top of a block: find the next block
8978 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8984 // starting within a block: find the top of the block
8989 if ( m_table
->IsEmptyCell(row
, col
) )
8997 MakeCellVisible( row
, col
);
8998 if ( expandSelection
)
9000 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9001 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9006 SetCurrentCell( row
, col
);
9015 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9018 m_currentCellCoords
!= wxGridNoCellCoords
&&
9019 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9021 int row
= m_currentCellCoords
.GetRow();
9022 int col
= m_currentCellCoords
.GetCol();
9024 if ( m_table
->IsEmptyCell(row
, col
) )
9026 // starting in an empty cell: find the next block of
9029 while ( row
< m_numRows
- 1 )
9032 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9036 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9038 // starting at the bottom of a block: find the next block
9041 while ( row
< m_numRows
- 1 )
9044 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9050 // starting within a block: find the bottom of the block
9052 while ( row
< m_numRows
- 1 )
9055 if ( m_table
->IsEmptyCell(row
, col
) )
9063 MakeCellVisible( row
, col
);
9064 if ( expandSelection
)
9066 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9067 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9072 SetCurrentCell( row
, col
);
9081 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9084 m_currentCellCoords
!= wxGridNoCellCoords
&&
9085 m_currentCellCoords
.GetCol() > 0 )
9087 int row
= m_currentCellCoords
.GetRow();
9088 int col
= m_currentCellCoords
.GetCol();
9090 if ( m_table
->IsEmptyCell(row
, col
) )
9092 // starting in an empty cell: find the next block of
9098 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9102 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9104 // starting at the left of a block: find the next block
9110 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9116 // starting within a block: find the left of the block
9121 if ( m_table
->IsEmptyCell(row
, col
) )
9129 MakeCellVisible( row
, col
);
9130 if ( expandSelection
)
9132 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9133 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9138 SetCurrentCell( row
, col
);
9147 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9150 m_currentCellCoords
!= wxGridNoCellCoords
&&
9151 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9153 int row
= m_currentCellCoords
.GetRow();
9154 int col
= m_currentCellCoords
.GetCol();
9156 if ( m_table
->IsEmptyCell(row
, col
) )
9158 // starting in an empty cell: find the next block of
9161 while ( col
< m_numCols
- 1 )
9164 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9168 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9170 // starting at the right of a block: find the next block
9173 while ( col
< m_numCols
- 1 )
9176 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9182 // starting within a block: find the right of the block
9184 while ( col
< m_numCols
- 1 )
9187 if ( m_table
->IsEmptyCell(row
, col
) )
9195 MakeCellVisible( row
, col
);
9196 if ( expandSelection
)
9198 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9199 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9204 SetCurrentCell( row
, col
);
9214 // ------ Label values and formatting
9217 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
9220 *horiz
= m_rowLabelHorizAlign
;
9222 *vert
= m_rowLabelVertAlign
;
9225 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
9228 *horiz
= m_colLabelHorizAlign
;
9230 *vert
= m_colLabelVertAlign
;
9233 int wxGrid::GetColLabelTextOrientation()
9235 return m_colLabelTextOrientation
;
9238 wxString
wxGrid::GetRowLabelValue( int row
)
9242 return m_table
->GetRowLabelValue( row
);
9252 wxString
wxGrid::GetColLabelValue( int col
)
9256 return m_table
->GetColLabelValue( col
);
9266 void wxGrid::SetRowLabelSize( int width
)
9268 width
= wxMax( width
, 0 );
9269 if ( width
!= m_rowLabelWidth
)
9273 m_rowLabelWin
->Show( false );
9274 m_cornerLabelWin
->Show( false );
9276 else if ( m_rowLabelWidth
== 0 )
9278 m_rowLabelWin
->Show( true );
9279 if ( m_colLabelHeight
> 0 )
9280 m_cornerLabelWin
->Show( true );
9283 m_rowLabelWidth
= width
;
9285 wxScrolledWindow::Refresh( true );
9289 void wxGrid::SetColLabelSize( int height
)
9291 height
= wxMax( height
, 0 );
9292 if ( height
!= m_colLabelHeight
)
9296 m_colLabelWin
->Show( false );
9297 m_cornerLabelWin
->Show( false );
9299 else if ( m_colLabelHeight
== 0 )
9301 m_colLabelWin
->Show( true );
9302 if ( m_rowLabelWidth
> 0 )
9303 m_cornerLabelWin
->Show( true );
9306 m_colLabelHeight
= height
;
9308 wxScrolledWindow::Refresh( true );
9312 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9314 if ( m_labelBackgroundColour
!= colour
)
9316 m_labelBackgroundColour
= colour
;
9317 m_rowLabelWin
->SetBackgroundColour( colour
);
9318 m_colLabelWin
->SetBackgroundColour( colour
);
9319 m_cornerLabelWin
->SetBackgroundColour( colour
);
9321 if ( !GetBatchCount() )
9323 m_rowLabelWin
->Refresh();
9324 m_colLabelWin
->Refresh();
9325 m_cornerLabelWin
->Refresh();
9330 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9332 if ( m_labelTextColour
!= colour
)
9334 m_labelTextColour
= colour
;
9335 if ( !GetBatchCount() )
9337 m_rowLabelWin
->Refresh();
9338 m_colLabelWin
->Refresh();
9343 void wxGrid::SetLabelFont( const wxFont
& font
)
9346 if ( !GetBatchCount() )
9348 m_rowLabelWin
->Refresh();
9349 m_colLabelWin
->Refresh();
9353 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9355 // allow old (incorrect) defs to be used
9358 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9359 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9360 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9365 case wxTOP
: vert
= wxALIGN_TOP
; break;
9366 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9367 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9370 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9372 m_rowLabelHorizAlign
= horiz
;
9375 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9377 m_rowLabelVertAlign
= vert
;
9380 if ( !GetBatchCount() )
9382 m_rowLabelWin
->Refresh();
9386 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9388 // allow old (incorrect) defs to be used
9391 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9392 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9393 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9398 case wxTOP
: vert
= wxALIGN_TOP
; break;
9399 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9400 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9403 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9405 m_colLabelHorizAlign
= horiz
;
9408 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9410 m_colLabelVertAlign
= vert
;
9413 if ( !GetBatchCount() )
9415 m_colLabelWin
->Refresh();
9419 // Note: under MSW, the default column label font must be changed because it
9420 // does not support vertical printing
9422 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9423 // pGrid->SetLabelFont(font);
9424 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9426 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9428 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9429 m_colLabelTextOrientation
= textOrientation
;
9431 if ( !GetBatchCount() )
9432 m_colLabelWin
->Refresh();
9435 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9439 m_table
->SetRowLabelValue( row
, s
);
9440 if ( !GetBatchCount() )
9442 wxRect rect
= CellToRect( row
, 0 );
9443 if ( rect
.height
> 0 )
9445 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9447 rect
.width
= m_rowLabelWidth
;
9448 m_rowLabelWin
->Refresh( true, &rect
);
9454 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9458 m_table
->SetColLabelValue( col
, s
);
9459 if ( !GetBatchCount() )
9461 wxRect rect
= CellToRect( 0, col
);
9462 if ( rect
.width
> 0 )
9464 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9466 rect
.height
= m_colLabelHeight
;
9467 m_colLabelWin
->Refresh( true, &rect
);
9473 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9475 if ( m_gridLineColour
!= colour
)
9477 m_gridLineColour
= colour
;
9479 wxClientDC
dc( m_gridWin
);
9481 DrawAllGridLines( dc
, wxRegion() );
9485 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9487 if ( m_cellHighlightColour
!= colour
)
9489 m_cellHighlightColour
= colour
;
9491 wxClientDC
dc( m_gridWin
);
9493 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9494 DrawCellHighlight(dc
, attr
);
9499 void wxGrid::SetCellHighlightPenWidth(int width
)
9501 if (m_cellHighlightPenWidth
!= width
)
9503 m_cellHighlightPenWidth
= width
;
9505 // Just redrawing the cell highlight is not enough since that won't
9506 // make any visible change if the the thickness is getting smaller.
9507 int row
= m_currentCellCoords
.GetRow();
9508 int col
= m_currentCellCoords
.GetCol();
9509 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9512 wxRect rect
= CellToRect(row
, col
);
9513 m_gridWin
->Refresh(true, &rect
);
9517 void wxGrid::SetCellHighlightROPenWidth(int width
)
9519 if (m_cellHighlightROPenWidth
!= width
)
9521 m_cellHighlightROPenWidth
= width
;
9523 // Just redrawing the cell highlight is not enough since that won't
9524 // make any visible change if the the thickness is getting smaller.
9525 int row
= m_currentCellCoords
.GetRow();
9526 int col
= m_currentCellCoords
.GetCol();
9527 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9530 wxRect rect
= CellToRect(row
, col
);
9531 m_gridWin
->Refresh(true, &rect
);
9535 void wxGrid::EnableGridLines( bool enable
)
9537 if ( enable
!= m_gridLinesEnabled
)
9539 m_gridLinesEnabled
= enable
;
9541 if ( !GetBatchCount() )
9545 wxClientDC
dc( m_gridWin
);
9547 DrawAllGridLines( dc
, wxRegion() );
9551 m_gridWin
->Refresh();
9557 int wxGrid::GetDefaultRowSize()
9559 return m_defaultRowHeight
;
9562 int wxGrid::GetRowSize( int row
)
9564 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9566 return GetRowHeight(row
);
9569 int wxGrid::GetDefaultColSize()
9571 return m_defaultColWidth
;
9574 int wxGrid::GetColSize( int col
)
9576 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9578 return GetColWidth(col
);
9581 // ============================================================================
9582 // access to the grid attributes: each of them has a default value in the grid
9583 // itself and may be overidden on a per-cell basis
9584 // ============================================================================
9586 // ----------------------------------------------------------------------------
9587 // setting default attributes
9588 // ----------------------------------------------------------------------------
9590 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9592 m_defaultCellAttr
->SetBackgroundColour(col
);
9594 m_gridWin
->SetBackgroundColour(col
);
9598 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9600 m_defaultCellAttr
->SetTextColour(col
);
9603 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9605 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9608 void wxGrid::SetDefaultCellOverflow( bool allow
)
9610 m_defaultCellAttr
->SetOverflow(allow
);
9613 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9615 m_defaultCellAttr
->SetFont(font
);
9618 // For editors and renderers the type registry takes precedence over the
9619 // default attr, so we need to register the new editor/renderer for the string
9620 // data type in order to make setting a default editor/renderer appear to
9623 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9625 RegisterDataType(wxGRID_VALUE_STRING
,
9627 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9630 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9632 RegisterDataType(wxGRID_VALUE_STRING
,
9633 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9637 // ----------------------------------------------------------------------------
9638 // access to the default attrbiutes
9639 // ----------------------------------------------------------------------------
9641 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9643 return m_defaultCellAttr
->GetBackgroundColour();
9646 wxColour
wxGrid::GetDefaultCellTextColour()
9648 return m_defaultCellAttr
->GetTextColour();
9651 wxFont
wxGrid::GetDefaultCellFont()
9653 return m_defaultCellAttr
->GetFont();
9656 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9658 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9661 bool wxGrid::GetDefaultCellOverflow()
9663 return m_defaultCellAttr
->GetOverflow();
9666 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9668 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9671 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9673 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9676 // ----------------------------------------------------------------------------
9677 // access to cell attributes
9678 // ----------------------------------------------------------------------------
9680 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9682 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9683 wxColour colour
= attr
->GetBackgroundColour();
9689 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9691 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9692 wxColour colour
= attr
->GetTextColour();
9698 wxFont
wxGrid::GetCellFont( int row
, int col
)
9700 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9701 wxFont font
= attr
->GetFont();
9707 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9709 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9710 attr
->GetAlignment(horiz
, vert
);
9714 bool wxGrid::GetCellOverflow( int row
, int col
)
9716 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9717 bool allow
= attr
->GetOverflow();
9723 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9725 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9726 attr
->GetSize( num_rows
, num_cols
);
9730 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9732 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9733 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9739 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9741 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9742 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9748 bool wxGrid::IsReadOnly(int row
, int col
) const
9750 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9751 bool isReadOnly
= attr
->IsReadOnly();
9757 // ----------------------------------------------------------------------------
9758 // attribute support: cache, automatic provider creation, ...
9759 // ----------------------------------------------------------------------------
9761 bool wxGrid::CanHaveAttributes()
9768 return m_table
->CanHaveAttributes();
9771 void wxGrid::ClearAttrCache()
9773 if ( m_attrCache
.row
!= -1 )
9775 wxSafeDecRef(m_attrCache
.attr
);
9776 m_attrCache
.attr
= NULL
;
9777 m_attrCache
.row
= -1;
9781 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9785 wxGrid
*self
= (wxGrid
*)this; // const_cast
9787 self
->ClearAttrCache();
9788 self
->m_attrCache
.row
= row
;
9789 self
->m_attrCache
.col
= col
;
9790 self
->m_attrCache
.attr
= attr
;
9795 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9797 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9799 *attr
= m_attrCache
.attr
;
9800 wxSafeIncRef(m_attrCache
.attr
);
9802 #ifdef DEBUG_ATTR_CACHE
9803 gs_nAttrCacheHits
++;
9810 #ifdef DEBUG_ATTR_CACHE
9811 gs_nAttrCacheMisses
++;
9818 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9820 wxGridCellAttr
*attr
= NULL
;
9821 // Additional test to avoid looking at the cache e.g. for
9822 // wxNoCellCoords, as this will confuse memory management.
9825 if ( !LookupAttr(row
, col
, &attr
) )
9827 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9828 : (wxGridCellAttr
*)NULL
;
9829 CacheAttr(row
, col
, attr
);
9835 attr
->SetDefAttr(m_defaultCellAttr
);
9839 attr
= m_defaultCellAttr
;
9846 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9848 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9849 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9851 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9852 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9854 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9857 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9859 // artificially inc the ref count to match DecRef() in caller
9861 m_table
->SetAttr(attr
, row
, col
);
9867 // ----------------------------------------------------------------------------
9868 // setting column attributes (wrappers around SetColAttr)
9869 // ----------------------------------------------------------------------------
9871 void wxGrid::SetColFormatBool(int col
)
9873 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9876 void wxGrid::SetColFormatNumber(int col
)
9878 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9881 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9883 wxString typeName
= wxGRID_VALUE_FLOAT
;
9884 if ( (width
!= -1) || (precision
!= -1) )
9886 typeName
<< _T(':') << width
<< _T(',') << precision
;
9889 SetColFormatCustom(col
, typeName
);
9892 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9894 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9896 attr
= new wxGridCellAttr
;
9897 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9898 attr
->SetRenderer(renderer
);
9900 SetColAttr(col
, attr
);
9904 // ----------------------------------------------------------------------------
9905 // setting cell attributes: this is forwarded to the table
9906 // ----------------------------------------------------------------------------
9908 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9910 if ( CanHaveAttributes() )
9912 m_table
->SetAttr(attr
, row
, col
);
9921 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9923 if ( CanHaveAttributes() )
9925 m_table
->SetRowAttr(attr
, row
);
9934 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9936 if ( CanHaveAttributes() )
9938 m_table
->SetColAttr(attr
, col
);
9947 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9949 if ( CanHaveAttributes() )
9951 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9952 attr
->SetBackgroundColour(colour
);
9957 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9959 if ( CanHaveAttributes() )
9961 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9962 attr
->SetTextColour(colour
);
9967 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9969 if ( CanHaveAttributes() )
9971 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9972 attr
->SetFont(font
);
9977 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9979 if ( CanHaveAttributes() )
9981 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9982 attr
->SetAlignment(horiz
, vert
);
9987 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9989 if ( CanHaveAttributes() )
9991 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9992 attr
->SetOverflow(allow
);
9997 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9999 if ( CanHaveAttributes() )
10001 int cell_rows
, cell_cols
;
10003 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10004 attr
->GetSize(&cell_rows
, &cell_cols
);
10005 attr
->SetSize(num_rows
, num_cols
);
10008 // Cannot set the size of a cell to 0 or negative values
10009 // While it is perfectly legal to do that, this function cannot
10010 // handle all the possibilies, do it by hand by getting the CellAttr.
10011 // You can only set the size of a cell to 1,1 or greater with this fn
10012 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10013 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10014 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10015 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10017 // if this was already a multicell then "turn off" the other cells first
10018 if ((cell_rows
> 1) || (cell_rows
> 1))
10021 for (j
=row
; j
< row
+ cell_rows
; j
++)
10023 for (i
=col
; i
< col
+ cell_cols
; i
++)
10025 if ((i
!= col
) || (j
!= row
))
10027 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10028 attr_stub
->SetSize( 1, 1 );
10029 attr_stub
->DecRef();
10035 // mark the cells that will be covered by this cell to
10036 // negative or zero values to point back at this cell
10037 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10040 for (j
=row
; j
< row
+ num_rows
; j
++)
10042 for (i
=col
; i
< col
+ num_cols
; i
++)
10044 if ((i
!= col
) || (j
!= row
))
10046 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10047 attr_stub
->SetSize( row
- j
, col
- i
);
10048 attr_stub
->DecRef();
10056 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10058 if ( CanHaveAttributes() )
10060 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10061 attr
->SetRenderer(renderer
);
10066 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10068 if ( CanHaveAttributes() )
10070 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10071 attr
->SetEditor(editor
);
10076 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10078 if ( CanHaveAttributes() )
10080 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10081 attr
->SetReadOnly(isReadOnly
);
10086 // ----------------------------------------------------------------------------
10087 // Data type registration
10088 // ----------------------------------------------------------------------------
10090 void wxGrid::RegisterDataType(const wxString
& typeName
,
10091 wxGridCellRenderer
* renderer
,
10092 wxGridCellEditor
* editor
)
10094 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10098 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10100 wxString typeName
= m_table
->GetTypeName(row
, col
);
10101 return GetDefaultEditorForType(typeName
);
10104 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10106 wxString typeName
= m_table
->GetTypeName(row
, col
);
10107 return GetDefaultRendererForType(typeName
);
10110 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10112 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10113 if ( index
== wxNOT_FOUND
)
10117 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10118 wxFAIL_MSG(errStr
.c_str());
10123 return m_typeRegistry
->GetEditor(index
);
10126 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10128 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10129 if ( index
== wxNOT_FOUND
)
10133 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10134 wxFAIL_MSG(errStr
.c_str());
10139 return m_typeRegistry
->GetRenderer(index
);
10142 // ----------------------------------------------------------------------------
10144 // ----------------------------------------------------------------------------
10146 void wxGrid::EnableDragRowSize( bool enable
)
10148 m_canDragRowSize
= enable
;
10151 void wxGrid::EnableDragColSize( bool enable
)
10153 m_canDragColSize
= enable
;
10156 void wxGrid::EnableDragGridSize( bool enable
)
10158 m_canDragGridSize
= enable
;
10161 void wxGrid::EnableDragCell( bool enable
)
10163 m_canDragCell
= enable
;
10166 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10168 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10170 if ( resizeExistingRows
)
10172 // since we are resizing all rows to the default row size,
10173 // we can simply clear the row heights and row bottoms
10174 // arrays (which also allows us to take advantage of
10175 // some speed optimisations)
10176 m_rowHeights
.Empty();
10177 m_rowBottoms
.Empty();
10178 if ( !GetBatchCount() )
10183 void wxGrid::SetRowSize( int row
, int height
)
10185 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10187 // See comment in SetColSize
10188 if ( height
< GetRowMinimalAcceptableHeight())
10191 if ( m_rowHeights
.IsEmpty() )
10193 // need to really create the array
10197 int h
= wxMax( 0, height
);
10198 int diff
= h
- m_rowHeights
[row
];
10200 m_rowHeights
[row
] = h
;
10202 for ( i
= row
; i
< m_numRows
; i
++ )
10204 m_rowBottoms
[i
] += diff
;
10207 if ( !GetBatchCount() )
10211 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10213 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
10215 if ( resizeExistingCols
)
10217 // since we are resizing all columns to the default column size,
10218 // we can simply clear the col widths and col rights
10219 // arrays (which also allows us to take advantage of
10220 // some speed optimisations)
10221 m_colWidths
.Empty();
10222 m_colRights
.Empty();
10223 if ( !GetBatchCount() )
10228 void wxGrid::SetColSize( int col
, int width
)
10230 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10232 // should we check that it's bigger than GetColMinimalWidth(col) here?
10234 // No, because it is reasonable to assume the library user know's
10235 // what he is doing. However we should test against the weaker
10236 // constraint of minimalAcceptableWidth, as this breaks rendering
10238 // This test then fixes sf.net bug #645734
10240 if ( width
< GetColMinimalAcceptableWidth() )
10243 if ( m_colWidths
.IsEmpty() )
10245 // need to really create the array
10249 // if < 0 then calculate new width from label
10253 wxArrayString lines
;
10254 wxClientDC
dc(m_colLabelWin
);
10255 dc
.SetFont(GetLabelFont());
10256 StringToLines(GetColLabelValue(col
), lines
);
10257 GetTextBoxSize(dc
, lines
, &w
, &h
);
10261 int w
= wxMax( 0, width
);
10262 int diff
= w
- m_colWidths
[col
];
10263 m_colWidths
[col
] = w
;
10267 for ( colPos
= GetColPos( col
); colPos
< m_numCols
; colPos
++ )
10269 i
= GetColAt( colPos
);
10270 m_colRights
[i
] += diff
;
10273 if ( !GetBatchCount() )
10277 void wxGrid::SetColMinimalWidth( int col
, int width
)
10279 if (width
> GetColMinimalAcceptableWidth())
10281 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10282 m_colMinWidths
[key
] = width
;
10286 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10288 if (width
> GetRowMinimalAcceptableHeight())
10290 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10291 m_rowMinHeights
[key
] = width
;
10295 int wxGrid::GetColMinimalWidth(int col
) const
10297 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10298 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10300 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10303 int wxGrid::GetRowMinimalHeight(int row
) const
10305 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10306 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10308 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10311 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10313 // We do allow a width of 0 since this gives us
10314 // an easy way to temporarily hiding columns.
10316 m_minAcceptableColWidth
= width
;
10319 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10321 // We do allow a height of 0 since this gives us
10322 // an easy way to temporarily hiding rows.
10324 m_minAcceptableRowHeight
= height
;
10327 int wxGrid::GetColMinimalAcceptableWidth() const
10329 return m_minAcceptableColWidth
;
10332 int wxGrid::GetRowMinimalAcceptableHeight() const
10334 return m_minAcceptableRowHeight
;
10337 // ----------------------------------------------------------------------------
10339 // ----------------------------------------------------------------------------
10341 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
10343 wxClientDC
dc(m_gridWin
);
10345 // cancel editing of cell
10346 HideCellEditControl();
10347 SaveEditControlValue();
10349 // init both of them to avoid compiler warnings, even if we only need one
10357 wxCoord extent
, extentMax
= 0;
10358 int max
= column
? m_numRows
: m_numCols
;
10359 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10366 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10367 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10370 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10371 extent
= column
? size
.x
: size
.y
;
10372 if ( extent
> extentMax
)
10373 extentMax
= extent
;
10375 renderer
->DecRef();
10381 // now also compare with the column label extent
10383 dc
.SetFont( GetLabelFont() );
10387 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
10388 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10392 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
10394 extent
= column
? w
: h
;
10395 if ( extent
> extentMax
)
10396 extentMax
= extent
;
10400 // empty column - give default extent (notice that if extentMax is less
10401 // than default extent but != 0, it's OK)
10402 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10407 // leave some space around text
10415 SetColSize( col
, extentMax
);
10416 if ( !GetBatchCount() )
10419 m_gridWin
->GetClientSize( &cw
, &ch
);
10420 wxRect
rect ( CellToRect( 0, col
) );
10422 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10423 rect
.width
= cw
- rect
.x
;
10424 rect
.height
= m_colLabelHeight
;
10425 m_colLabelWin
->Refresh( true, &rect
);
10430 SetRowSize(row
, extentMax
);
10431 if ( !GetBatchCount() )
10434 m_gridWin
->GetClientSize( &cw
, &ch
);
10435 wxRect
rect( CellToRect( row
, 0 ) );
10437 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10438 rect
.width
= m_rowLabelWidth
;
10439 rect
.height
= ch
- rect
.y
;
10440 m_rowLabelWin
->Refresh( true, &rect
);
10447 SetColMinimalWidth(col
, extentMax
);
10449 SetRowMinimalHeight(row
, extentMax
);
10453 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10455 int width
= m_rowLabelWidth
;
10460 for ( int col
= 0; col
< m_numCols
; col
++ )
10463 AutoSizeColumn(col
, setAsMin
);
10465 width
+= GetColWidth(col
);
10474 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10476 int height
= m_colLabelHeight
;
10481 for ( int row
= 0; row
< m_numRows
; row
++ )
10484 AutoSizeRow(row
, setAsMin
);
10486 height
+= GetRowHeight(row
);
10495 void wxGrid::AutoSize()
10499 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
10501 // round up the size to a multiple of scroll step - this ensures that we
10502 // won't get the scrollbars if we're sized exactly to this width
10503 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10506 GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
10507 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10509 // distribute the extra space between the columns/rows to avoid having
10510 // extra white space
10512 // Remove the extra m_extraWidth + 1 added above
10513 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10514 if ( diff
&& m_numCols
)
10516 // try to resize the columns uniformly
10517 wxCoord diffPerCol
= diff
/ m_numCols
;
10520 for ( int col
= 0; col
< m_numCols
; col
++ )
10522 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10526 // add remaining amount to the last columns
10527 diff
-= diffPerCol
* m_numCols
;
10530 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10532 SetColSize(col
, GetColWidth(col
) + 1);
10538 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10539 if ( diff
&& m_numRows
)
10541 // try to resize the columns uniformly
10542 wxCoord diffPerRow
= diff
/ m_numRows
;
10545 for ( int row
= 0; row
< m_numRows
; row
++ )
10547 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10551 // add remaining amount to the last rows
10552 diff
-= diffPerRow
* m_numRows
;
10555 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10557 SetRowSize(row
, GetRowHeight(row
) + 1);
10564 SetClientSize(sizeFit
);
10567 void wxGrid::AutoSizeRowLabelSize( int row
)
10569 wxArrayString lines
;
10572 // Hide the edit control, so it
10573 // won't interfere with drag-shrinking.
10574 if ( IsCellEditControlShown() )
10576 HideCellEditControl();
10577 SaveEditControlValue();
10580 // autosize row height depending on label text
10581 StringToLines( GetRowLabelValue( row
), lines
);
10582 wxClientDC
dc( m_rowLabelWin
);
10583 GetTextBoxSize( dc
, lines
, &w
, &h
);
10584 if ( h
< m_defaultRowHeight
)
10585 h
= m_defaultRowHeight
;
10586 SetRowSize(row
, h
);
10590 void wxGrid::AutoSizeColLabelSize( int col
)
10592 wxArrayString lines
;
10595 // Hide the edit control, so it
10596 // won't interfere with drag-shrinking.
10597 if ( IsCellEditControlShown() )
10599 HideCellEditControl();
10600 SaveEditControlValue();
10603 // autosize column width depending on label text
10604 StringToLines( GetColLabelValue( col
), lines
);
10605 wxClientDC
dc( m_colLabelWin
);
10606 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10607 GetTextBoxSize( dc
, lines
, &w
, &h
);
10609 GetTextBoxSize( dc
, lines
, &h
, &w
);
10610 if ( w
< m_defaultColWidth
)
10611 w
= m_defaultColWidth
;
10612 SetColSize(col
, w
);
10616 wxSize
wxGrid::DoGetBestSize() const
10618 // don't set sizes, only calculate them
10619 wxGrid
*self
= (wxGrid
*)this; // const_cast
10622 width
= self
->SetOrCalcColumnSizes(true);
10623 height
= self
->SetOrCalcRowSizes(true);
10630 // Round up to a multiple the scroll rate
10631 // NOTE: this still doesn't get rid of the scrollbars;
10632 // is there any magic incantation for that?
10634 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10636 width
+= 1 + xpu
- (width
% xpu
);
10638 height
+= 1 + ypu
- (height
% ypu
);
10640 // limit to 1/4 of the screen size
10641 int maxwidth
, maxheight
;
10642 wxDisplaySize( &maxwidth
, &maxheight
);
10645 if ( width
> maxwidth
)
10647 if ( height
> maxheight
)
10648 height
= maxheight
;
10650 wxSize
best(width
, height
);
10652 // NOTE: This size should be cached, but first we need to add calls to
10653 // InvalidateBestSize everywhere that could change the results of this
10655 // CacheBestSize(size);
10665 wxPen
& wxGrid::GetDividerPen() const
10670 // ----------------------------------------------------------------------------
10671 // cell value accessor functions
10672 // ----------------------------------------------------------------------------
10674 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10678 m_table
->SetValue( row
, col
, s
);
10679 if ( !GetBatchCount() )
10682 wxRect
rect( CellToRect( row
, col
) );
10684 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10685 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10686 m_gridWin
->Refresh( false, &rect
);
10689 if ( m_currentCellCoords
.GetRow() == row
&&
10690 m_currentCellCoords
.GetCol() == col
&&
10691 IsCellEditControlShown())
10692 // Note: If we are using IsCellEditControlEnabled,
10693 // this interacts badly with calling SetCellValue from
10694 // an EVT_GRID_CELL_CHANGE handler.
10696 HideCellEditControl();
10697 ShowCellEditControl(); // will reread data from table
10702 // ----------------------------------------------------------------------------
10703 // block, row and column selection
10704 // ----------------------------------------------------------------------------
10706 void wxGrid::SelectRow( int row
, bool addToSelected
)
10708 if ( IsSelection() && !addToSelected
)
10712 m_selection
->SelectRow( row
, false, addToSelected
);
10715 void wxGrid::SelectCol( int col
, bool addToSelected
)
10717 if ( IsSelection() && !addToSelected
)
10721 m_selection
->SelectCol( col
, false, addToSelected
);
10724 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10725 bool addToSelected
)
10727 if ( IsSelection() && !addToSelected
)
10731 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10732 false, addToSelected
);
10735 void wxGrid::SelectAll()
10737 if ( m_numRows
> 0 && m_numCols
> 0 )
10740 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10744 // ----------------------------------------------------------------------------
10745 // cell, row and col deselection
10746 // ----------------------------------------------------------------------------
10748 void wxGrid::DeselectRow( int row
)
10750 if ( !m_selection
)
10753 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10755 if ( m_selection
->IsInSelection(row
, 0 ) )
10756 m_selection
->ToggleCellSelection(row
, 0);
10760 int nCols
= GetNumberCols();
10761 for ( int i
= 0; i
< nCols
; i
++ )
10763 if ( m_selection
->IsInSelection(row
, i
) )
10764 m_selection
->ToggleCellSelection(row
, i
);
10769 void wxGrid::DeselectCol( int col
)
10771 if ( !m_selection
)
10774 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10776 if ( m_selection
->IsInSelection(0, col
) )
10777 m_selection
->ToggleCellSelection(0, col
);
10781 int nRows
= GetNumberRows();
10782 for ( int i
= 0; i
< nRows
; i
++ )
10784 if ( m_selection
->IsInSelection(i
, col
) )
10785 m_selection
->ToggleCellSelection(i
, col
);
10790 void wxGrid::DeselectCell( int row
, int col
)
10792 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10793 m_selection
->ToggleCellSelection(row
, col
);
10796 bool wxGrid::IsSelection()
10798 return ( m_selection
&& (m_selection
->IsSelection() ||
10799 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10800 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10803 bool wxGrid::IsInSelection( int row
, int col
) const
10805 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10806 ( row
>= m_selectingTopLeft
.GetRow() &&
10807 col
>= m_selectingTopLeft
.GetCol() &&
10808 row
<= m_selectingBottomRight
.GetRow() &&
10809 col
<= m_selectingBottomRight
.GetCol() )) );
10812 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10816 wxGridCellCoordsArray a
;
10820 return m_selection
->m_cellSelection
;
10823 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10827 wxGridCellCoordsArray a
;
10831 return m_selection
->m_blockSelectionTopLeft
;
10834 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10838 wxGridCellCoordsArray a
;
10842 return m_selection
->m_blockSelectionBottomRight
;
10845 wxArrayInt
wxGrid::GetSelectedRows() const
10853 return m_selection
->m_rowSelection
;
10856 wxArrayInt
wxGrid::GetSelectedCols() const
10864 return m_selection
->m_colSelection
;
10867 void wxGrid::ClearSelection()
10869 m_selectingTopLeft
=
10870 m_selectingBottomRight
=
10871 m_selectingKeyboard
= wxGridNoCellCoords
;
10873 m_selection
->ClearSelection();
10876 // This function returns the rectangle that encloses the given block
10877 // in device coords clipped to the client size of the grid window.
10879 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10880 const wxGridCellCoords
&bottomRight
)
10882 wxRect
rect( wxGridNoCellRect
);
10885 cellRect
= CellToRect( topLeft
);
10886 if ( cellRect
!= wxGridNoCellRect
)
10892 rect
= wxRect(0, 0, 0, 0);
10895 cellRect
= CellToRect( bottomRight
);
10896 if ( cellRect
!= wxGridNoCellRect
)
10902 return wxGridNoCellRect
;
10906 int left
= rect
.GetLeft();
10907 int top
= rect
.GetTop();
10908 int right
= rect
.GetRight();
10909 int bottom
= rect
.GetBottom();
10911 int leftCol
= topLeft
.GetCol();
10912 int topRow
= topLeft
.GetRow();
10913 int rightCol
= bottomRight
.GetCol();
10914 int bottomRow
= bottomRight
.GetRow();
10922 leftCol
= rightCol
;
10932 topRow
= bottomRow
;
10936 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10938 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10940 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10942 cellRect
= CellToRect( j
, i
);
10944 if (cellRect
.x
< left
)
10946 if (cellRect
.y
< top
)
10948 if (cellRect
.x
+ cellRect
.width
> right
)
10949 right
= cellRect
.x
+ cellRect
.width
;
10950 if (cellRect
.y
+ cellRect
.height
> bottom
)
10951 bottom
= cellRect
.y
+ cellRect
.height
;
10955 i
= rightCol
; // jump over inner cells.
10960 // convert to scrolled coords
10962 CalcScrolledPosition( left
, top
, &left
, &top
);
10963 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10966 m_gridWin
->GetClientSize( &cw
, &ch
);
10968 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10969 return wxRect(0,0,0,0);
10971 rect
.SetLeft( wxMax(0, left
) );
10972 rect
.SetTop( wxMax(0, top
) );
10973 rect
.SetRight( wxMin(cw
, right
) );
10974 rect
.SetBottom( wxMin(ch
, bottom
) );
10979 // ----------------------------------------------------------------------------
10980 // grid event classes
10981 // ----------------------------------------------------------------------------
10983 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10985 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10986 int row
, int col
, int x
, int y
, bool sel
,
10987 bool control
, bool shift
, bool alt
, bool meta
)
10988 : wxNotifyEvent( type
, id
)
10995 m_control
= control
;
11000 SetEventObject(obj
);
11004 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11006 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11007 int rowOrCol
, int x
, int y
,
11008 bool control
, bool shift
, bool alt
, bool meta
)
11009 : wxNotifyEvent( type
, id
)
11011 m_rowOrCol
= rowOrCol
;
11014 m_control
= control
;
11019 SetEventObject(obj
);
11023 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11025 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11026 const wxGridCellCoords
& topLeft
,
11027 const wxGridCellCoords
& bottomRight
,
11028 bool sel
, bool control
,
11029 bool shift
, bool alt
, bool meta
)
11030 : wxNotifyEvent( type
, id
)
11032 m_topLeft
= topLeft
;
11033 m_bottomRight
= bottomRight
;
11035 m_control
= control
;
11040 SetEventObject(obj
);
11044 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11046 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11047 wxObject
* obj
, int row
,
11048 int col
, wxControl
* ctrl
)
11049 : wxCommandEvent(type
, id
)
11051 SetEventObject(obj
);
11057 #endif // wxUSE_GRID