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
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilatixon, 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"
34 #include "wx/textfile.h"
35 #include "wx/spinctrl.h"
36 #include "wx/tokenzr.h"
37 #include "wx/renderer.h"
40 #include "wx/generic/gridsel.h"
42 #if defined(__WXMOTIF__)
43 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
45 #define WXUNUSED_MOTIF(identifier) identifier
48 #if defined(__WXGTK__)
49 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
51 #define WXUNUSED_GTK(identifier) identifier
54 // Required for wxIs... functions
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
62 class WXDLLIMPEXP_ADV
);
64 struct wxGridCellWithAttr
66 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
67 : coords(row
, col
), attr(attr_
)
76 wxGridCellCoords coords
;
80 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
81 // without rewriting the macros, which require a public copy constructor.
84 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
85 class WXDLLIMPEXP_ADV
);
87 #include "wx/arrimpl.cpp"
89 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
90 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
97 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
98 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
99 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
100 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
101 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
102 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
103 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
121 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
122 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
123 const wxPoint
&pos
, const wxSize
&size
);
128 void OnPaint( wxPaintEvent
& event
);
129 void OnMouseEvent( wxMouseEvent
& event
);
130 void OnMouseWheel( wxMouseEvent
& event
);
131 void OnKeyDown( wxKeyEvent
& event
);
132 void OnKeyUp( wxKeyEvent
& );
133 void OnChar( wxKeyEvent
& );
135 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
136 DECLARE_EVENT_TABLE()
137 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
141 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
144 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
145 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
146 const wxPoint
&pos
, const wxSize
&size
);
151 void OnPaint( wxPaintEvent
&event
);
152 void OnMouseEvent( wxMouseEvent
& event
);
153 void OnMouseWheel( wxMouseEvent
& event
);
154 void OnKeyDown( wxKeyEvent
& event
);
155 void OnKeyUp( wxKeyEvent
& );
156 void OnChar( wxKeyEvent
& );
158 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
159 DECLARE_EVENT_TABLE()
160 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
164 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
167 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
168 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
169 const wxPoint
&pos
, const wxSize
&size
);
174 void OnMouseEvent( wxMouseEvent
& event
);
175 void OnMouseWheel( wxMouseEvent
& event
);
176 void OnKeyDown( wxKeyEvent
& event
);
177 void OnKeyUp( wxKeyEvent
& );
178 void OnChar( wxKeyEvent
& );
179 void OnPaint( wxPaintEvent
& event
);
181 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
182 DECLARE_EVENT_TABLE()
183 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
186 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
191 m_owner
= (wxGrid
*)NULL
;
192 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
193 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
196 wxGridWindow( wxGrid
*parent
,
197 wxGridRowLabelWindow
*rowLblWin
,
198 wxGridColLabelWindow
*colLblWin
,
199 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
202 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
204 wxGrid
* GetOwner() { return m_owner
; }
208 wxGridRowLabelWindow
*m_rowLabelWin
;
209 wxGridColLabelWindow
*m_colLabelWin
;
211 void OnPaint( wxPaintEvent
&event
);
212 void OnMouseWheel( wxMouseEvent
& event
);
213 void OnMouseEvent( wxMouseEvent
& event
);
214 void OnKeyDown( wxKeyEvent
& );
215 void OnKeyUp( wxKeyEvent
& );
216 void OnChar( wxKeyEvent
& );
217 void OnEraseBackground( wxEraseEvent
& );
218 void OnFocus( wxFocusEvent
& );
220 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
221 DECLARE_EVENT_TABLE()
222 DECLARE_NO_COPY_CLASS(wxGridWindow
)
226 class wxGridCellEditorEvtHandler
: public wxEvtHandler
229 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
236 void OnKillFocus(wxFocusEvent
& event
);
237 void OnKeyDown(wxKeyEvent
& event
);
238 void OnChar(wxKeyEvent
& event
);
240 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
244 wxGridCellEditor
*m_editor
;
246 // Work around the fact that a focus kill event can be sent to
247 // a combobox within a set focus event.
250 DECLARE_EVENT_TABLE()
251 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
252 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
256 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
258 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
259 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
260 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
261 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
266 // ----------------------------------------------------------------------------
267 // the internal data representation used by wxGridCellAttrProvider
268 // ----------------------------------------------------------------------------
270 // this class stores attributes set for cells
271 class WXDLLIMPEXP_ADV wxGridCellAttrData
274 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
275 wxGridCellAttr
*GetAttr(int row
, int col
) const;
276 void UpdateAttrRows( size_t pos
, int numRows
);
277 void UpdateAttrCols( size_t pos
, int numCols
);
280 // searches for the attr for given cell, returns wxNOT_FOUND if not found
281 int FindIndex(int row
, int col
) const;
283 wxGridCellWithAttrArray m_attrs
;
286 // this class stores attributes set for rows or columns
287 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
290 // empty ctor to suppress warnings
291 wxGridRowOrColAttrData() {}
292 ~wxGridRowOrColAttrData();
294 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
295 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
296 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
299 wxArrayInt m_rowsOrCols
;
300 wxArrayAttrs m_attrs
;
303 // NB: this is just a wrapper around 3 objects: one which stores cell
304 // attributes, and 2 others for row/col ones
305 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
308 wxGridCellAttrData m_cellAttrs
;
309 wxGridRowOrColAttrData m_rowAttrs
,
314 // ----------------------------------------------------------------------------
315 // data structures used for the data type registry
316 // ----------------------------------------------------------------------------
318 struct wxGridDataTypeInfo
320 wxGridDataTypeInfo(const wxString
& typeName
,
321 wxGridCellRenderer
* renderer
,
322 wxGridCellEditor
* editor
)
323 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
326 ~wxGridDataTypeInfo()
328 wxSafeDecRef(m_renderer
);
329 wxSafeDecRef(m_editor
);
333 wxGridCellRenderer
* m_renderer
;
334 wxGridCellEditor
* m_editor
;
336 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
340 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
341 class WXDLLIMPEXP_ADV
);
344 class WXDLLIMPEXP_ADV wxGridTypeRegistry
347 wxGridTypeRegistry() {}
348 ~wxGridTypeRegistry();
350 void RegisterDataType(const wxString
& typeName
,
351 wxGridCellRenderer
* renderer
,
352 wxGridCellEditor
* editor
);
354 // find one of already registered data types
355 int FindRegisteredDataType(const wxString
& typeName
);
357 // try to FindRegisteredDataType(), if this fails and typeName is one of
358 // standard typenames, register it and return its index
359 int FindDataType(const wxString
& typeName
);
361 // try to FindDataType(), if it fails see if it is not one of already
362 // registered data types with some params in which case clone the
363 // registered data type and set params for it
364 int FindOrCloneDataType(const wxString
& typeName
);
366 wxGridCellRenderer
* GetRenderer(int index
);
367 wxGridCellEditor
* GetEditor(int index
);
370 wxGridDataTypeInfoArray m_typeinfo
;
373 // ----------------------------------------------------------------------------
374 // conditional compilation
375 // ----------------------------------------------------------------------------
377 #ifndef WXGRID_DRAW_LINES
378 #define WXGRID_DRAW_LINES 1
381 // ----------------------------------------------------------------------------
383 // ----------------------------------------------------------------------------
385 //#define DEBUG_ATTR_CACHE
386 #ifdef DEBUG_ATTR_CACHE
387 static size_t gs_nAttrCacheHits
= 0;
388 static size_t gs_nAttrCacheMisses
= 0;
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
395 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
396 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
399 // TODO: this doesn't work at all, grid cells have different sizes and approx
400 // calculations don't work as because of the size mismatch scrollbars
401 // sometimes fail to be shown when they should be or vice versa
403 // The scroll bars may be a little flakey once in a while, but that is
404 // surely much less horrible than having scroll lines of only 1!!!
407 // Well, it's still seriously broken so it might be better but needs
410 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
411 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
413 // the size of hash tables used a bit everywhere (the max number of elements
414 // in these hash tables is the number of rows/columns)
415 static const int GRID_HASH_SIZE
= 100;
418 // ----------------------------------------------------------------------------
420 // ----------------------------------------------------------------------------
422 static inline int GetScrollX(int x
)
424 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
427 static inline int GetScrollY(int y
)
429 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
433 // ============================================================================
435 // ============================================================================
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 wxGridCellEditor::wxGridCellEditor()
447 wxGridCellEditor::~wxGridCellEditor()
452 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
453 wxWindowID
WXUNUSED(id
),
454 wxEvtHandler
* evtHandler
)
457 m_control
->PushEventHandler(evtHandler
);
460 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
461 wxGridCellAttr
*attr
)
463 // erase the background because we might not fill the cell
464 wxClientDC
dc(m_control
->GetParent());
465 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
467 gridWindow
->GetOwner()->PrepareDC(dc
);
469 dc
.SetPen(*wxTRANSPARENT_PEN
);
470 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
471 dc
.DrawRectangle(rectCell
);
473 // redraw the control we just painted over
474 m_control
->Refresh();
477 void wxGridCellEditor::Destroy()
481 m_control
->PopEventHandler(true /* delete it*/);
483 m_control
->Destroy();
488 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
490 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
492 m_control
->Show(show
);
496 // set the colours/fonts if we have any
499 m_colFgOld
= m_control
->GetForegroundColour();
500 m_control
->SetForegroundColour(attr
->GetTextColour());
502 m_colBgOld
= m_control
->GetBackgroundColour();
503 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
505 // Workaround for GTK+1 font setting problem on some platforms
506 #if !defined(__WXGTK__) || defined(__WXGTK20__)
507 m_fontOld
= m_control
->GetFont();
508 m_control
->SetFont(attr
->GetFont());
510 // can't do anything more in the base class version, the other
511 // attributes may only be used by the derived classes
516 // restore the standard colours fonts
517 if ( m_colFgOld
.Ok() )
519 m_control
->SetForegroundColour(m_colFgOld
);
520 m_colFgOld
= wxNullColour
;
523 if ( m_colBgOld
.Ok() )
525 m_control
->SetBackgroundColour(m_colBgOld
);
526 m_colBgOld
= wxNullColour
;
529 // Workaround for GTK+1 font setting problem on some platforms
530 #if !defined(__WXGTK__) || defined(__WXGTK20__)
531 if ( m_fontOld
.Ok() )
533 m_control
->SetFont(m_fontOld
);
534 m_fontOld
= wxNullFont
;
540 void wxGridCellEditor::SetSize(const wxRect
& rect
)
542 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
544 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
547 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
552 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
554 bool ctrl
= event
.ControlDown();
555 bool alt
= event
.AltDown();
558 // On the Mac the Alt key is more like shift and is used for entry of
559 // valid characters, so check for Ctrl and Meta instead.
560 alt
= event
.MetaDown();
563 // Assume it's not a valid char if ctrl or alt is down, but if both are
564 // down then it may be because of an AltGr key combination, so let them
565 // through in that case.
566 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
573 // if the unicode key code is not really a unicode character (it may
574 // be a function key or etc., the platforms appear to always give us a
575 // small value in this case) then fallback to the ASCII key code but
576 // don't do anything for function keys or etc.
577 key
= event
.GetUnicodeKey();
580 key
= event
.GetKeyCode();
581 keyOk
= (key
<= 127);
584 key
= event
.GetKeyCode();
585 keyOk
= (key
<= 255);
591 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
596 void wxGridCellEditor::StartingClick()
602 // ----------------------------------------------------------------------------
603 // wxGridCellTextEditor
604 // ----------------------------------------------------------------------------
606 wxGridCellTextEditor::wxGridCellTextEditor()
611 void wxGridCellTextEditor::Create(wxWindow
* parent
,
613 wxEvtHandler
* evtHandler
)
615 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
616 wxDefaultPosition
, wxDefaultSize
617 #if defined(__WXMSW__)
618 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
622 // set max length allowed in the textctrl, if the parameter was set
625 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
628 wxGridCellEditor::Create(parent
, id
, evtHandler
);
631 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
632 wxGridCellAttr
* WXUNUSED(attr
))
634 // as we fill the entire client area, don't do anything here to minimize
638 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
640 wxRect
rect(rectOrig
);
642 // Make the edit control large enough to allow for internal margins
644 // TODO: remove this if the text ctrl sizing is improved esp. for unix
646 #if defined(__WXGTK__)
655 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
657 // MB: treat MSW separately here otherwise the caret doesn't show
658 // when the editor is in the first row.
659 #if defined(__WXMSW__)
662 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
665 #if defined(__WXMOTIF__)
670 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
671 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
672 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
673 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
676 wxGridCellEditor::SetSize(rect
);
679 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
681 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
683 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
685 DoBeginEdit(m_startValue
);
688 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
690 Text()->SetValue(startValue
);
691 Text()->SetInsertionPointEnd();
692 Text()->SetSelection(-1, -1);
696 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
699 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
701 bool changed
= false;
702 wxString value
= Text()->GetValue();
703 if (value
!= m_startValue
)
707 grid
->GetTable()->SetValue(row
, col
, value
);
709 m_startValue
= wxEmptyString
;
710 // No point in setting the text of the hidden control
711 //Text()->SetValue(m_startValue);
716 void wxGridCellTextEditor::Reset()
718 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
720 DoReset(m_startValue
);
723 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
725 Text()->SetValue(startValue
);
726 Text()->SetInsertionPointEnd();
729 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
731 return wxGridCellEditor::IsAcceptedKey(event
);
734 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
736 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
737 // longer an appropriate way to get the character into the text control.
738 // Do it ourselves instead. We know that if we get this far that we have
739 // a valid character, so not a whole lot of testing needs to be done.
741 wxTextCtrl
* tc
= Text();
746 ch
= event
.GetUnicodeKey();
748 ch
= (wxChar
)event
.GetKeyCode();
750 ch
= (wxChar
)event
.GetKeyCode();
756 // delete the character at the cursor
757 pos
= tc
->GetInsertionPoint();
758 if (pos
< tc
->GetLastPosition())
759 tc
->Remove(pos
, pos
+ 1);
763 // delete the character before the cursor
764 pos
= tc
->GetInsertionPoint();
766 tc
->Remove(pos
- 1, pos
);
775 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
776 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
778 #if defined(__WXMOTIF__) || defined(__WXGTK__)
779 // wxMotif needs a little extra help...
780 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
781 wxString
s( Text()->GetValue() );
782 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
784 Text()->SetInsertionPoint( pos
);
786 // the other ports can handle a Return key press
792 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
802 if ( !params
.ToLong(&tmp
) )
804 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
808 m_maxChars
= (size_t)tmp
;
813 // return the value in the text control
814 wxString
wxGridCellTextEditor::GetValue() const
816 return Text()->GetValue();
819 // ----------------------------------------------------------------------------
820 // wxGridCellNumberEditor
821 // ----------------------------------------------------------------------------
823 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
829 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
831 wxEvtHandler
* evtHandler
)
836 // create a spin ctrl
837 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
838 wxDefaultPosition
, wxDefaultSize
,
842 wxGridCellEditor::Create(parent
, id
, evtHandler
);
847 // just a text control
848 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
851 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
852 #endif // wxUSE_VALIDATORS
856 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
858 // first get the value
859 wxGridTableBase
*table
= grid
->GetTable();
860 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
862 m_valueOld
= table
->GetValueAsLong(row
, col
);
867 wxString sValue
= table
->GetValue(row
, col
);
868 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
870 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
878 Spin()->SetValue((int)m_valueOld
);
884 DoBeginEdit(GetString());
888 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
898 value
= Spin()->GetValue();
899 changed
= value
!= m_valueOld
;
901 text
= wxString::Format(wxT("%ld"), value
);
906 text
= Text()->GetValue();
907 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
912 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
913 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
915 grid
->GetTable()->SetValue(row
, col
, text
);
921 void wxGridCellNumberEditor::Reset()
926 Spin()->SetValue((int)m_valueOld
);
931 DoReset(GetString());
935 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
937 if ( wxGridCellEditor::IsAcceptedKey(event
) )
939 int keycode
= event
.GetKeyCode();
940 if ( (keycode
< 128) &&
941 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
950 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
952 int keycode
= event
.GetKeyCode();
955 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
957 wxGridCellTextEditor::StartingKey(event
);
966 if ( wxIsdigit(keycode
) )
968 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
969 spin
->SetValue(keycode
- '0');
970 spin
->SetSelection(1,1);
979 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
990 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
994 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
998 // skip the error message below
1003 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1007 // return the value in the spin control if it is there (the text control otherwise)
1008 wxString
wxGridCellNumberEditor::GetValue() const
1015 long value
= Spin()->GetValue();
1016 s
.Printf(wxT("%ld"), value
);
1021 s
= Text()->GetValue();
1027 // ----------------------------------------------------------------------------
1028 // wxGridCellFloatEditor
1029 // ----------------------------------------------------------------------------
1031 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1034 m_precision
= precision
;
1037 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1039 wxEvtHandler
* evtHandler
)
1041 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1043 #if wxUSE_VALIDATORS
1044 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1045 #endif // wxUSE_VALIDATORS
1048 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1050 // first get the value
1051 wxGridTableBase
*table
= grid
->GetTable();
1052 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1054 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1059 wxString sValue
= table
->GetValue(row
, col
);
1060 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1062 wxFAIL_MSG( _T("this cell doesn't have float value") );
1067 DoBeginEdit(GetString());
1070 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1074 wxString
text(Text()->GetValue());
1076 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1077 !wxIsSameDouble(value
, m_valueOld
) )
1079 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1080 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1082 grid
->GetTable()->SetValue(row
, col
, text
);
1090 void wxGridCellFloatEditor::Reset()
1092 DoReset(GetString());
1095 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1097 int keycode
= event
.GetKeyCode();
1099 tmpbuf
[0] = (char) keycode
;
1101 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1104 bool is_decimal_point
= ( strbuf
==
1105 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1107 bool is_decimal_point
= ( strbuf
== _T(".") );
1110 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1111 || is_decimal_point
)
1113 wxGridCellTextEditor::StartingKey(event
);
1115 // skip Skip() below
1122 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1133 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1137 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1139 m_precision
= (int)tmp
;
1141 // skip the error message below
1146 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1150 wxString
wxGridCellFloatEditor::GetString() const
1153 if ( m_precision
== -1 && m_width
!= -1)
1155 // default precision
1156 fmt
.Printf(_T("%%%d.f"), m_width
);
1158 else if ( m_precision
!= -1 && m_width
== -1)
1161 fmt
.Printf(_T("%%.%df"), m_precision
);
1163 else if ( m_precision
!= -1 && m_width
!= -1 )
1165 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1169 // default width/precision
1173 return wxString::Format(fmt
, m_valueOld
);
1176 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1178 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1180 int keycode
= event
.GetKeyCode();
1181 printf("%d\n", keycode
);
1182 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1184 tmpbuf
[0] = (char) keycode
;
1186 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1189 bool is_decimal_point
=
1190 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1191 wxLOCALE_CAT_NUMBER
) );
1193 bool is_decimal_point
= ( strbuf
== _T(".") );
1196 if ( (keycode
< 128) &&
1197 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1198 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1207 #endif // wxUSE_TEXTCTRL
1211 // ----------------------------------------------------------------------------
1212 // wxGridCellBoolEditor
1213 // ----------------------------------------------------------------------------
1215 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1217 wxEvtHandler
* evtHandler
)
1219 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1220 wxDefaultPosition
, wxDefaultSize
,
1223 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1226 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1228 bool resize
= false;
1229 wxSize size
= m_control
->GetSize();
1230 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1232 // check if the checkbox is not too big/small for this cell
1233 wxSize sizeBest
= m_control
->GetBestSize();
1234 if ( !(size
== sizeBest
) )
1236 // reset to default size if it had been made smaller
1242 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1244 // leave 1 pixel margin
1245 size
.x
= size
.y
= minSize
- 2;
1252 m_control
->SetSize(size
);
1255 // position it in the centre of the rectangle (TODO: support alignment?)
1257 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1258 // the checkbox without label still has some space to the right in wxGTK,
1259 // so shift it to the right
1261 #elif defined(__WXMSW__)
1262 // here too, but in other way
1267 int hAlign
= wxALIGN_CENTRE
;
1268 int vAlign
= wxALIGN_CENTRE
;
1270 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1273 if (hAlign
== wxALIGN_LEFT
)
1281 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1283 else if (hAlign
== wxALIGN_RIGHT
)
1285 x
= r
.x
+ r
.width
- size
.x
- 2;
1286 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1288 else if (hAlign
== wxALIGN_CENTRE
)
1290 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1291 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1294 m_control
->Move(x
, y
);
1297 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1299 m_control
->Show(show
);
1303 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1304 CBox()->SetBackgroundColour(colBg
);
1308 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1310 wxASSERT_MSG(m_control
,
1311 wxT("The wxGridCellEditor must be Created first!"));
1313 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1315 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1319 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1320 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1323 CBox()->SetValue(m_startValue
);
1327 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1330 wxASSERT_MSG(m_control
,
1331 wxT("The wxGridCellEditor must be Created first!"));
1333 bool changed
= false;
1334 bool value
= CBox()->GetValue();
1335 if ( value
!= m_startValue
)
1340 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1341 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1343 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1349 void wxGridCellBoolEditor::Reset()
1351 wxASSERT_MSG(m_control
,
1352 wxT("The wxGridCellEditor must be Created first!"));
1354 CBox()->SetValue(m_startValue
);
1357 void wxGridCellBoolEditor::StartingClick()
1359 CBox()->SetValue(!CBox()->GetValue());
1362 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1364 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1366 int keycode
= event
.GetKeyCode();
1379 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1381 int keycode
= event
.GetKeyCode();
1385 CBox()->SetValue(!CBox()->GetValue());
1389 CBox()->SetValue(true);
1393 CBox()->SetValue(false);
1399 // return the value as "1" for true and the empty string for false
1400 wxString
wxGridCellBoolEditor::GetValue() const
1402 bool bSet
= CBox()->GetValue();
1403 return bSet
? _T("1") : wxEmptyString
;
1406 #endif // wxUSE_CHECKBOX
1410 // ----------------------------------------------------------------------------
1411 // wxGridCellChoiceEditor
1412 // ----------------------------------------------------------------------------
1414 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1416 : m_choices(choices
),
1417 m_allowOthers(allowOthers
) { }
1419 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1420 const wxString choices
[],
1422 : m_allowOthers(allowOthers
)
1426 m_choices
.Alloc(count
);
1427 for ( size_t n
= 0; n
< count
; n
++ )
1429 m_choices
.Add(choices
[n
]);
1434 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1436 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1437 editor
->m_allowOthers
= m_allowOthers
;
1438 editor
->m_choices
= m_choices
;
1443 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1445 wxEvtHandler
* evtHandler
)
1447 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1448 wxDefaultPosition
, wxDefaultSize
,
1450 m_allowOthers
? 0 : wxCB_READONLY
);
1452 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1455 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1456 wxGridCellAttr
* attr
)
1458 // as we fill the entire client area, don't do anything here to minimize
1461 // TODO: It doesn't actually fill the client area since the height of a
1462 // combo always defaults to the standard... Until someone has time to
1463 // figure out the right rectangle to paint, just do it the normal way...
1464 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1467 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1469 wxASSERT_MSG(m_control
,
1470 wxT("The wxGridCellEditor must be Created first!"));
1472 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1474 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1476 // Don't immediately end if we get a kill focus event within BeginEdit
1478 evtHandler
->SetInSetFocus(true);
1480 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1484 Combo()->SetValue(m_startValue
);
1488 // find the right position, or default to the first if not found
1489 int pos
= Combo()->FindString(m_startValue
);
1490 if (pos
== wxNOT_FOUND
)
1492 Combo()->SetSelection(pos
);
1495 Combo()->SetInsertionPointEnd();
1496 Combo()->SetFocus();
1500 // When dropping down the menu, a kill focus event
1501 // happens after this point, so we can't reset the flag yet.
1502 #if !defined(__WXGTK20__)
1503 evtHandler
->SetInSetFocus(false);
1508 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1511 wxString value
= Combo()->GetValue();
1512 if ( value
== m_startValue
)
1515 grid
->GetTable()->SetValue(row
, col
, value
);
1520 void wxGridCellChoiceEditor::Reset()
1522 Combo()->SetValue(m_startValue
);
1523 Combo()->SetInsertionPointEnd();
1526 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1536 wxStringTokenizer
tk(params
, _T(','));
1537 while ( tk
.HasMoreTokens() )
1539 m_choices
.Add(tk
.GetNextToken());
1543 // return the value in the text control
1544 wxString
wxGridCellChoiceEditor::GetValue() const
1546 return Combo()->GetValue();
1549 #endif // wxUSE_COMBOBOX
1551 // ----------------------------------------------------------------------------
1552 // wxGridCellEditorEvtHandler
1553 // ----------------------------------------------------------------------------
1555 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1557 // Don't disable the cell if we're just starting to edit it
1562 m_grid
->DisableCellEditControl();
1567 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1569 switch ( event
.GetKeyCode() )
1573 m_grid
->DisableCellEditControl();
1577 m_grid
->GetEventHandler()->ProcessEvent( event
);
1581 case WXK_NUMPAD_ENTER
:
1582 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1583 m_editor
->HandleReturn(event
);
1592 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1594 int row
= m_grid
->GetGridCursorRow();
1595 int col
= m_grid
->GetGridCursorCol();
1596 wxRect rect
= m_grid
->CellToRect( row
, col
);
1598 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1600 // if cell width is smaller than grid client area, cell is wholly visible
1601 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1603 switch ( event
.GetKeyCode() )
1608 case WXK_NUMPAD_ENTER
:
1613 if ( wholeCellVisible
)
1615 // no special processing needed...
1620 // do special processing for partly visible cell...
1622 // get the widths of all cells previous to this one
1624 for ( int i
= 0; i
< col
; i
++ )
1626 colXPos
+= m_grid
->GetColSize(i
);
1629 int xUnit
= 1, yUnit
= 1;
1630 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1633 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1637 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1644 if ( wholeCellVisible
)
1646 // no special processing needed...
1651 // do special processing for partly visible cell...
1654 wxString value
= m_grid
->GetCellValue(row
, col
);
1655 if ( wxEmptyString
!= value
)
1657 // get width of cell CONTENTS (text)
1659 wxFont font
= m_grid
->GetCellFont(row
, col
);
1660 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1662 // try to RIGHT align the text by scrolling
1663 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1665 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1666 // otherwise the last part of the cell content might be hidden below the scroll bar
1667 // FIXME: maybe there is a more suitable correction?
1668 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1669 if ( textWidth
< 0 )
1675 // get the widths of all cells previous to this one
1677 for ( int i
= 0; i
< col
; i
++ )
1679 colXPos
+= m_grid
->GetColSize(i
);
1682 // and add the (modified) text width of the cell contents
1683 // as we'd like to see the last part of the cell contents
1684 colXPos
+= textWidth
;
1686 int xUnit
= 1, yUnit
= 1;
1687 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1688 m_grid
->Scroll(colXPos
/xUnit
-1, m_grid
->GetScrollPos(wxVERTICAL
));
1698 // ----------------------------------------------------------------------------
1699 // wxGridCellWorker is an (almost) empty common base class for
1700 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1701 // ----------------------------------------------------------------------------
1703 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1708 wxGridCellWorker::~wxGridCellWorker()
1712 // ============================================================================
1714 // ============================================================================
1716 // ----------------------------------------------------------------------------
1717 // wxGridCellRenderer
1718 // ----------------------------------------------------------------------------
1720 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1721 wxGridCellAttr
& attr
,
1724 int WXUNUSED(row
), int WXUNUSED(col
),
1727 dc
.SetBackgroundMode( wxSOLID
);
1729 // grey out fields if the grid is disabled
1730 if ( grid
.IsEnabled() )
1734 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1738 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1743 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1746 dc
.SetPen( *wxTRANSPARENT_PEN
);
1747 dc
.DrawRectangle(rect
);
1750 // ----------------------------------------------------------------------------
1751 // wxGridCellStringRenderer
1752 // ----------------------------------------------------------------------------
1754 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1755 const wxGridCellAttr
& attr
,
1759 dc
.SetBackgroundMode( wxTRANSPARENT
);
1761 // TODO some special colours for attr.IsReadOnly() case?
1763 // different coloured text when the grid is disabled
1764 if ( grid
.IsEnabled() )
1768 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1769 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1773 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1774 dc
.SetTextForeground( attr
.GetTextColour() );
1779 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1780 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1783 dc
.SetFont( attr
.GetFont() );
1786 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1788 const wxString
& text
)
1790 wxCoord x
= 0, y
= 0, max_x
= 0;
1791 dc
.SetFont(attr
.GetFont());
1792 wxStringTokenizer
tk(text
, _T('\n'));
1793 while ( tk
.HasMoreTokens() )
1795 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1796 max_x
= wxMax(max_x
, x
);
1799 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1801 return wxSize(max_x
, y
);
1804 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1805 wxGridCellAttr
& attr
,
1809 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1812 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1813 wxGridCellAttr
& attr
,
1815 const wxRect
& rectCell
,
1819 wxRect rect
= rectCell
;
1822 // erase only this cells background, overflow cells should have been erased
1823 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1826 attr
.GetAlignment(&hAlign
, &vAlign
);
1828 int overflowCols
= 0;
1830 if (attr
.GetOverflow())
1832 int cols
= grid
.GetNumberCols();
1833 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1834 int cell_rows
, cell_cols
;
1835 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1836 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1838 int i
, c_cols
, c_rows
;
1839 for (i
= col
+cell_cols
; i
< cols
; i
++)
1841 bool is_empty
= true;
1842 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1844 // check w/ anchor cell for multicell block
1845 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1848 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1856 rect
.width
+= grid
.GetColSize(i
);
1863 if (rect
.width
>= best_width
)
1867 overflowCols
= i
- col
- cell_cols
+ 1;
1868 if (overflowCols
>= cols
)
1869 overflowCols
= cols
- 1;
1872 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1874 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1876 clip
.x
+= rectCell
.width
;
1877 // draw each overflow cell individually
1878 int col_end
= col
+cell_cols
+ overflowCols
;
1879 if (col_end
>= grid
.GetNumberCols())
1880 col_end
= grid
.GetNumberCols() - 1;
1881 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1883 clip
.width
= grid
.GetColSize(i
) - 1;
1884 dc
.DestroyClippingRegion();
1885 dc
.SetClippingRegion(clip
);
1887 SetTextColoursAndFont(grid
, attr
, dc
,
1888 grid
.IsInSelection(row
,i
));
1890 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1891 rect
, hAlign
, vAlign
);
1892 clip
.x
+= grid
.GetColSize(i
) - 1;
1898 dc
.DestroyClippingRegion();
1902 // now we only have to draw the text
1903 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1905 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1906 rect
, hAlign
, vAlign
);
1909 // ----------------------------------------------------------------------------
1910 // wxGridCellNumberRenderer
1911 // ----------------------------------------------------------------------------
1913 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1915 wxGridTableBase
*table
= grid
.GetTable();
1917 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1919 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1923 text
= table
->GetValue(row
, col
);
1929 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1930 wxGridCellAttr
& attr
,
1932 const wxRect
& rectCell
,
1936 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1938 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1940 // draw the text right aligned by default
1942 attr
.GetAlignment(&hAlign
, &vAlign
);
1943 hAlign
= wxALIGN_RIGHT
;
1945 wxRect rect
= rectCell
;
1948 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1951 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1952 wxGridCellAttr
& attr
,
1956 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1959 // ----------------------------------------------------------------------------
1960 // wxGridCellFloatRenderer
1961 // ----------------------------------------------------------------------------
1963 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1966 SetPrecision(precision
);
1969 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1971 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1972 renderer
->m_width
= m_width
;
1973 renderer
->m_precision
= m_precision
;
1974 renderer
->m_format
= m_format
;
1979 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1981 wxGridTableBase
*table
= grid
.GetTable();
1986 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1988 val
= table
->GetValueAsDouble(row
, col
);
1993 text
= table
->GetValue(row
, col
);
1994 hasDouble
= text
.ToDouble(&val
);
2001 if ( m_width
== -1 )
2003 if ( m_precision
== -1 )
2005 // default width/precision
2006 m_format
= _T("%f");
2010 m_format
.Printf(_T("%%.%df"), m_precision
);
2013 else if ( m_precision
== -1 )
2015 // default precision
2016 m_format
.Printf(_T("%%%d.f"), m_width
);
2020 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2024 text
.Printf(m_format
, val
);
2027 //else: text already contains the string
2032 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2033 wxGridCellAttr
& attr
,
2035 const wxRect
& rectCell
,
2039 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2041 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2043 // draw the text right aligned by default
2045 attr
.GetAlignment(&hAlign
, &vAlign
);
2046 hAlign
= wxALIGN_RIGHT
;
2048 wxRect rect
= rectCell
;
2051 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2054 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2055 wxGridCellAttr
& attr
,
2059 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2062 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2066 // reset to defaults
2072 wxString tmp
= params
.BeforeFirst(_T(','));
2076 if ( tmp
.ToLong(&width
) )
2078 SetWidth((int)width
);
2082 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2086 tmp
= params
.AfterFirst(_T(','));
2090 if ( tmp
.ToLong(&precision
) )
2092 SetPrecision((int)precision
);
2096 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2102 // ----------------------------------------------------------------------------
2103 // wxGridCellBoolRenderer
2104 // ----------------------------------------------------------------------------
2106 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2108 // FIXME these checkbox size calculations are really ugly...
2110 // between checkmark and box
2111 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2113 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2114 wxGridCellAttr
& WXUNUSED(attr
),
2119 // compute it only once (no locks for MT safeness in GUI thread...)
2120 if ( !ms_sizeCheckMark
.x
)
2122 // get checkbox size
2123 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2124 wxSize size
= checkbox
->GetBestSize();
2125 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2127 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2128 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2129 checkSize
-= size
.y
/ 2;
2134 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2137 return ms_sizeCheckMark
;
2140 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2141 wxGridCellAttr
& attr
,
2147 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2149 // draw a check mark in the centre (ignoring alignment - TODO)
2150 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2152 // don't draw outside the cell
2153 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2154 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2156 // and even leave (at least) 1 pixel margin
2157 size
.x
= size
.y
= minSize
- 2;
2160 // draw a border around checkmark
2162 attr
.GetAlignment(& hAlign
, &vAlign
);
2165 if (hAlign
== wxALIGN_CENTRE
)
2167 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2168 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2169 rectBorder
.width
= size
.x
;
2170 rectBorder
.height
= size
.y
;
2172 else if (hAlign
== wxALIGN_LEFT
)
2174 rectBorder
.x
= rect
.x
+ 2;
2175 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2176 rectBorder
.width
= size
.x
;
2177 rectBorder
.height
= size
.y
;
2179 else if (hAlign
== wxALIGN_RIGHT
)
2181 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2182 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2183 rectBorder
.width
= size
.x
;
2184 rectBorder
.height
= size
.y
;
2188 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2189 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2192 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2193 value
= !( !cellval
|| (cellval
== wxT("0")) );
2198 wxRect rectMark
= rectBorder
;
2201 // MSW DrawCheckMark() is weird (and should probably be changed...)
2202 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2206 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2209 dc
.SetTextForeground(attr
.GetTextColour());
2210 dc
.DrawCheckMark(rectMark
);
2213 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2214 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2215 dc
.DrawRectangle(rectBorder
);
2218 // ----------------------------------------------------------------------------
2220 // ----------------------------------------------------------------------------
2222 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2226 m_isReadOnly
= Unset
;
2231 m_attrkind
= wxGridCellAttr::Cell
;
2233 m_sizeRows
= m_sizeCols
= 1;
2234 m_overflow
= UnsetOverflow
;
2236 SetDefAttr(attrDefault
);
2239 wxGridCellAttr
*wxGridCellAttr::Clone() const
2241 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2243 if ( HasTextColour() )
2244 attr
->SetTextColour(GetTextColour());
2245 if ( HasBackgroundColour() )
2246 attr
->SetBackgroundColour(GetBackgroundColour());
2248 attr
->SetFont(GetFont());
2249 if ( HasAlignment() )
2250 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2252 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2256 attr
->SetRenderer(m_renderer
);
2257 m_renderer
->IncRef();
2261 attr
->SetEditor(m_editor
);
2266 attr
->SetReadOnly();
2268 attr
->SetKind( m_attrkind
);
2273 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2275 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2276 SetTextColour(mergefrom
->GetTextColour());
2277 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2278 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2279 if ( !HasFont() && mergefrom
->HasFont() )
2280 SetFont(mergefrom
->GetFont());
2281 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2284 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2285 SetAlignment(hAlign
, vAlign
);
2287 if ( !HasSize() && mergefrom
->HasSize() )
2288 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2290 // Directly access member functions as GetRender/Editor don't just return
2291 // m_renderer/m_editor
2293 // Maybe add support for merge of Render and Editor?
2294 if (!HasRenderer() && mergefrom
->HasRenderer() )
2296 m_renderer
= mergefrom
->m_renderer
;
2297 m_renderer
->IncRef();
2299 if ( !HasEditor() && mergefrom
->HasEditor() )
2301 m_editor
= mergefrom
->m_editor
;
2304 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2305 SetReadOnly(mergefrom
->IsReadOnly());
2307 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2308 SetOverflow(mergefrom
->GetOverflow());
2310 SetDefAttr(mergefrom
->m_defGridAttr
);
2313 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2315 // The size of a cell is normally 1,1
2317 // If this cell is larger (2,2) then this is the top left cell
2318 // the other cells that will be covered (lower right cells) must be
2319 // set to negative or zero values such that
2320 // row + num_rows of the covered cell points to the larger cell (this cell)
2321 // same goes for the col + num_cols.
2323 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2325 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2326 !((num_rows
<= 0) && (num_cols
> 0)) ||
2327 !((num_rows
== 0) && (num_cols
== 0))),
2328 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2330 m_sizeRows
= num_rows
;
2331 m_sizeCols
= num_cols
;
2334 const wxColour
& wxGridCellAttr::GetTextColour() const
2336 if (HasTextColour())
2340 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2342 return m_defGridAttr
->GetTextColour();
2346 wxFAIL_MSG(wxT("Missing default cell attribute"));
2347 return wxNullColour
;
2351 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2353 if (HasBackgroundColour())
2357 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2359 return m_defGridAttr
->GetBackgroundColour();
2363 wxFAIL_MSG(wxT("Missing default cell attribute"));
2364 return wxNullColour
;
2368 const wxFont
& wxGridCellAttr::GetFont() const
2374 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2376 return m_defGridAttr
->GetFont();
2380 wxFAIL_MSG(wxT("Missing default cell attribute"));
2385 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2394 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2395 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2398 wxFAIL_MSG(wxT("Missing default cell attribute"));
2402 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2405 *num_rows
= m_sizeRows
;
2407 *num_cols
= m_sizeCols
;
2410 // GetRenderer and GetEditor use a slightly different decision path about
2411 // which attribute to use. If a non-default attr object has one then it is
2412 // used, otherwise the default editor or renderer is fetched from the grid and
2413 // used. It should be the default for the data type of the cell. If it is
2414 // NULL (because the table has a type that the grid does not have in its
2415 // registry,) then the grid's default editor or renderer is used.
2417 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2419 wxGridCellRenderer
*renderer
;
2421 if ( m_renderer
&& this != m_defGridAttr
)
2423 // use the cells renderer if it has one
2424 renderer
= m_renderer
;
2427 else // no non-default cell renderer
2429 // get default renderer for the data type
2432 // GetDefaultRendererForCell() will do IncRef() for us
2433 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2442 if (m_defGridAttr
&& this != m_defGridAttr
)
2444 // if we still don't have one then use the grid default
2445 // (no need for IncRef() here neither)
2446 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2448 else // default grid attr
2450 // use m_renderer which we had decided not to use initially
2451 renderer
= m_renderer
;
2458 // we're supposed to always find something
2459 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2464 // same as above, except for s/renderer/editor/g
2465 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2467 wxGridCellEditor
*editor
;
2469 if ( m_editor
&& this != m_defGridAttr
)
2471 // use the cells editor if it has one
2475 else // no non default cell editor
2477 // get default editor for the data type
2480 // GetDefaultEditorForCell() will do IncRef() for us
2481 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2490 if ( m_defGridAttr
&& this != m_defGridAttr
)
2492 // if we still don't have one then use the grid default
2493 // (no need for IncRef() here neither)
2494 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2496 else // default grid attr
2498 // use m_editor which we had decided not to use initially
2506 // we're supposed to always find something
2507 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2512 // ----------------------------------------------------------------------------
2513 // wxGridCellAttrData
2514 // ----------------------------------------------------------------------------
2516 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2518 int n
= FindIndex(row
, col
);
2519 if ( n
== wxNOT_FOUND
)
2521 // add the attribute
2522 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2526 // free the old attribute
2527 m_attrs
[(size_t)n
].attr
->DecRef();
2531 // change the attribute
2532 m_attrs
[(size_t)n
].attr
= attr
;
2536 // remove this attribute
2537 m_attrs
.RemoveAt((size_t)n
);
2542 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2544 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2546 int n
= FindIndex(row
, col
);
2547 if ( n
!= wxNOT_FOUND
)
2549 attr
= m_attrs
[(size_t)n
].attr
;
2556 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2558 size_t count
= m_attrs
.GetCount();
2559 for ( size_t n
= 0; n
< count
; n
++ )
2561 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2562 wxCoord row
= coords
.GetRow();
2563 if ((size_t)row
>= pos
)
2567 // If rows inserted, include row counter where necessary
2568 coords
.SetRow(row
+ numRows
);
2570 else if (numRows
< 0)
2572 // If rows deleted ...
2573 if ((size_t)row
>= pos
- numRows
)
2575 // ...either decrement row counter (if row still exists)...
2576 coords
.SetRow(row
+ numRows
);
2580 // ...or remove the attribute
2581 // No need to DecRef the attribute itself since this is
2582 // done be wxGridCellWithAttr's destructor!
2583 m_attrs
.RemoveAt(n
);
2592 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2594 size_t count
= m_attrs
.GetCount();
2595 for ( size_t n
= 0; n
< count
; n
++ )
2597 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2598 wxCoord col
= coords
.GetCol();
2599 if ( (size_t)col
>= pos
)
2603 // If rows inserted, include row counter where necessary
2604 coords
.SetCol(col
+ numCols
);
2606 else if (numCols
< 0)
2608 // If rows deleted ...
2609 if ((size_t)col
>= pos
- numCols
)
2611 // ...either decrement row counter (if row still exists)...
2612 coords
.SetCol(col
+ numCols
);
2616 // ...or remove the attribute
2617 // No need to DecRef the attribute itself since this is
2618 // done be wxGridCellWithAttr's destructor!
2619 m_attrs
.RemoveAt(n
);
2628 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2630 size_t count
= m_attrs
.GetCount();
2631 for ( size_t n
= 0; n
< count
; n
++ )
2633 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2634 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2643 // ----------------------------------------------------------------------------
2644 // wxGridRowOrColAttrData
2645 // ----------------------------------------------------------------------------
2647 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2649 size_t count
= m_attrs
.Count();
2650 for ( size_t n
= 0; n
< count
; n
++ )
2652 m_attrs
[n
]->DecRef();
2656 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2658 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2660 int n
= m_rowsOrCols
.Index(rowOrCol
);
2661 if ( n
!= wxNOT_FOUND
)
2663 attr
= m_attrs
[(size_t)n
];
2670 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2672 int i
= m_rowsOrCols
.Index(rowOrCol
);
2673 if ( i
== wxNOT_FOUND
)
2675 // add the attribute
2676 m_rowsOrCols
.Add(rowOrCol
);
2681 size_t n
= (size_t)i
;
2684 // change the attribute
2685 m_attrs
[n
]->DecRef();
2690 // remove this attribute
2691 m_attrs
[n
]->DecRef();
2692 m_rowsOrCols
.RemoveAt(n
);
2693 m_attrs
.RemoveAt(n
);
2698 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2700 size_t count
= m_attrs
.GetCount();
2701 for ( size_t n
= 0; n
< count
; n
++ )
2703 int & rowOrCol
= m_rowsOrCols
[n
];
2704 if ( (size_t)rowOrCol
>= pos
)
2706 if ( numRowsOrCols
> 0 )
2708 // If rows inserted, include row counter where necessary
2709 rowOrCol
+= numRowsOrCols
;
2711 else if ( numRowsOrCols
< 0)
2713 // If rows deleted, either decrement row counter (if row still exists)
2714 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2715 rowOrCol
+= numRowsOrCols
;
2718 m_rowsOrCols
.RemoveAt(n
);
2719 m_attrs
[n
]->DecRef();
2720 m_attrs
.RemoveAt(n
);
2729 // ----------------------------------------------------------------------------
2730 // wxGridCellAttrProvider
2731 // ----------------------------------------------------------------------------
2733 wxGridCellAttrProvider::wxGridCellAttrProvider()
2735 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2738 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2743 void wxGridCellAttrProvider::InitData()
2745 m_data
= new wxGridCellAttrProviderData
;
2748 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2749 wxGridCellAttr::wxAttrKind kind
) const
2751 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2756 case (wxGridCellAttr::Any
):
2757 // Get cached merge attributes.
2758 // Currently not used as no cache implemented as not mutable
2759 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2762 // Basically implement old version.
2763 // Also check merge cache, so we don't have to re-merge every time..
2764 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2765 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2766 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2768 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2770 // Two or more are non NULL
2771 attr
= new wxGridCellAttr
;
2772 attr
->SetKind(wxGridCellAttr::Merged
);
2774 // Order is important..
2777 attr
->MergeWith(attrcell
);
2782 attr
->MergeWith(attrcol
);
2787 attr
->MergeWith(attrrow
);
2791 // store merge attr if cache implemented
2793 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2797 // one or none is non null return it or null.
2816 case (wxGridCellAttr::Cell
):
2817 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2820 case (wxGridCellAttr::Col
):
2821 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2824 case (wxGridCellAttr::Row
):
2825 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2830 // (wxGridCellAttr::Default):
2831 // (wxGridCellAttr::Merged):
2839 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2845 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2848 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2853 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2856 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2861 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2864 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2868 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2870 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2874 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2878 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2880 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2884 // ----------------------------------------------------------------------------
2885 // wxGridTypeRegistry
2886 // ----------------------------------------------------------------------------
2888 wxGridTypeRegistry::~wxGridTypeRegistry()
2890 size_t count
= m_typeinfo
.Count();
2891 for ( size_t i
= 0; i
< count
; i
++ )
2892 delete m_typeinfo
[i
];
2895 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2896 wxGridCellRenderer
* renderer
,
2897 wxGridCellEditor
* editor
)
2899 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2901 // is it already registered?
2902 int loc
= FindRegisteredDataType(typeName
);
2903 if ( loc
!= wxNOT_FOUND
)
2905 delete m_typeinfo
[loc
];
2906 m_typeinfo
[loc
] = info
;
2910 m_typeinfo
.Add(info
);
2914 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2916 size_t count
= m_typeinfo
.GetCount();
2917 for ( size_t i
= 0; i
< count
; i
++ )
2919 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2928 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2930 int index
= FindRegisteredDataType(typeName
);
2931 if ( index
== wxNOT_FOUND
)
2933 // check whether this is one of the standard ones, in which case
2934 // register it "on the fly"
2936 if ( typeName
== wxGRID_VALUE_STRING
)
2938 RegisterDataType(wxGRID_VALUE_STRING
,
2939 new wxGridCellStringRenderer
,
2940 new wxGridCellTextEditor
);
2943 #endif // wxUSE_TEXTCTRL
2945 if ( typeName
== wxGRID_VALUE_BOOL
)
2947 RegisterDataType(wxGRID_VALUE_BOOL
,
2948 new wxGridCellBoolRenderer
,
2949 new wxGridCellBoolEditor
);
2952 #endif // wxUSE_CHECKBOX
2954 if ( typeName
== wxGRID_VALUE_NUMBER
)
2956 RegisterDataType(wxGRID_VALUE_NUMBER
,
2957 new wxGridCellNumberRenderer
,
2958 new wxGridCellNumberEditor
);
2960 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2962 RegisterDataType(wxGRID_VALUE_FLOAT
,
2963 new wxGridCellFloatRenderer
,
2964 new wxGridCellFloatEditor
);
2967 #endif // wxUSE_TEXTCTRL
2969 if ( typeName
== wxGRID_VALUE_CHOICE
)
2971 RegisterDataType(wxGRID_VALUE_CHOICE
,
2972 new wxGridCellStringRenderer
,
2973 new wxGridCellChoiceEditor
);
2976 #endif // wxUSE_COMBOBOX
2981 // we get here only if just added the entry for this type, so return
2983 index
= m_typeinfo
.GetCount() - 1;
2989 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2991 int index
= FindDataType(typeName
);
2992 if ( index
== wxNOT_FOUND
)
2994 // the first part of the typename is the "real" type, anything after ':'
2995 // are the parameters for the renderer
2996 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2997 if ( index
== wxNOT_FOUND
)
3002 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3003 wxGridCellRenderer
*rendererOld
= renderer
;
3004 renderer
= renderer
->Clone();
3005 rendererOld
->DecRef();
3007 wxGridCellEditor
*editor
= GetEditor(index
);
3008 wxGridCellEditor
*editorOld
= editor
;
3009 editor
= editor
->Clone();
3010 editorOld
->DecRef();
3012 // do it even if there are no parameters to reset them to defaults
3013 wxString params
= typeName
.AfterFirst(_T(':'));
3014 renderer
->SetParameters(params
);
3015 editor
->SetParameters(params
);
3017 // register the new typename
3018 RegisterDataType(typeName
, renderer
, editor
);
3020 // we just registered it, it's the last one
3021 index
= m_typeinfo
.GetCount() - 1;
3027 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3029 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3036 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3038 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3045 // ----------------------------------------------------------------------------
3047 // ----------------------------------------------------------------------------
3049 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3051 wxGridTableBase::wxGridTableBase()
3053 m_view
= (wxGrid
*) NULL
;
3054 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3057 wxGridTableBase::~wxGridTableBase()
3059 delete m_attrProvider
;
3062 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3064 delete m_attrProvider
;
3065 m_attrProvider
= attrProvider
;
3068 bool wxGridTableBase::CanHaveAttributes()
3070 if ( ! GetAttrProvider() )
3072 // use the default attr provider by default
3073 SetAttrProvider(new wxGridCellAttrProvider
);
3079 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3081 if ( m_attrProvider
)
3082 return m_attrProvider
->GetAttr(row
, col
, kind
);
3084 return (wxGridCellAttr
*)NULL
;
3087 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3089 if ( m_attrProvider
)
3091 attr
->SetKind(wxGridCellAttr::Cell
);
3092 m_attrProvider
->SetAttr(attr
, row
, col
);
3096 // as we take ownership of the pointer and don't store it, we must
3102 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3104 if ( m_attrProvider
)
3106 attr
->SetKind(wxGridCellAttr::Row
);
3107 m_attrProvider
->SetRowAttr(attr
, row
);
3111 // as we take ownership of the pointer and don't store it, we must
3117 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3119 if ( m_attrProvider
)
3121 attr
->SetKind(wxGridCellAttr::Col
);
3122 m_attrProvider
->SetColAttr(attr
, col
);
3126 // as we take ownership of the pointer and don't store it, we must
3132 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3133 size_t WXUNUSED(numRows
) )
3135 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3140 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3142 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3147 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3148 size_t WXUNUSED(numRows
) )
3150 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3155 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3156 size_t WXUNUSED(numCols
) )
3158 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3163 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3165 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3170 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3171 size_t WXUNUSED(numCols
) )
3173 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3178 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3182 // RD: Starting the rows at zero confuses users,
3183 // no matter how much it makes sense to us geeks.
3189 wxString
wxGridTableBase::GetColLabelValue( int col
)
3191 // default col labels are:
3192 // cols 0 to 25 : A-Z
3193 // cols 26 to 675 : AA-ZZ
3198 for ( n
= 1; ; n
++ )
3200 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3206 // reverse the string...
3208 for ( i
= 0; i
< n
; i
++ )
3216 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3218 return wxGRID_VALUE_STRING
;
3221 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3222 const wxString
& typeName
)
3224 return typeName
== wxGRID_VALUE_STRING
;
3227 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3229 return CanGetValueAs(row
, col
, typeName
);
3232 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3237 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3242 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3247 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3248 long WXUNUSED(value
) )
3252 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3253 double WXUNUSED(value
) )
3257 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3258 bool WXUNUSED(value
) )
3262 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3263 const wxString
& WXUNUSED(typeName
) )
3268 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3269 const wxString
& WXUNUSED(typeName
),
3270 void* WXUNUSED(value
) )
3274 //////////////////////////////////////////////////////////////////////
3276 // Message class for the grid table to send requests and notifications
3280 wxGridTableMessage::wxGridTableMessage()
3282 m_table
= (wxGridTableBase
*) NULL
;
3288 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3289 int commandInt1
, int commandInt2
)
3293 m_comInt1
= commandInt1
;
3294 m_comInt2
= commandInt2
;
3297 //////////////////////////////////////////////////////////////////////
3299 // A basic grid table for string data. An object of this class will
3300 // created by wxGrid if you don't specify an alternative table class.
3303 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3305 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3307 wxGridStringTable::wxGridStringTable()
3312 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3315 m_data
.Alloc( numRows
);
3318 sa
.Alloc( numCols
);
3319 sa
.Add( wxEmptyString
, numCols
);
3321 m_data
.Add( sa
, numRows
);
3324 wxGridStringTable::~wxGridStringTable()
3328 int wxGridStringTable::GetNumberRows()
3330 return m_data
.GetCount();
3333 int wxGridStringTable::GetNumberCols()
3335 if ( m_data
.GetCount() > 0 )
3336 return m_data
[0].GetCount();
3341 wxString
wxGridStringTable::GetValue( int row
, int col
)
3343 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3345 _T("invalid row or column index in wxGridStringTable") );
3347 return m_data
[row
][col
];
3350 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3352 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3353 _T("invalid row or column index in wxGridStringTable") );
3355 m_data
[row
][col
] = value
;
3358 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3360 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3362 _T("invalid row or column index in wxGridStringTable") );
3364 return (m_data
[row
][col
] == wxEmptyString
);
3367 void wxGridStringTable::Clear()
3370 int numRows
, numCols
;
3372 numRows
= m_data
.GetCount();
3375 numCols
= m_data
[0].GetCount();
3377 for ( row
= 0; row
< numRows
; row
++ )
3379 for ( col
= 0; col
< numCols
; col
++ )
3381 m_data
[row
][col
] = wxEmptyString
;
3387 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3389 size_t curNumRows
= m_data
.GetCount();
3390 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3391 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3393 if ( pos
>= curNumRows
)
3395 return AppendRows( numRows
);
3399 sa
.Alloc( curNumCols
);
3400 sa
.Add( wxEmptyString
, curNumCols
);
3401 m_data
.Insert( sa
, pos
, numRows
);
3405 wxGridTableMessage
msg( this,
3406 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3410 GetView()->ProcessTableMessage( msg
);
3416 bool wxGridStringTable::AppendRows( size_t numRows
)
3418 size_t curNumRows
= m_data
.GetCount();
3419 size_t curNumCols
= ( curNumRows
> 0
3420 ? m_data
[0].GetCount()
3421 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3424 if ( curNumCols
> 0 )
3426 sa
.Alloc( curNumCols
);
3427 sa
.Add( wxEmptyString
, curNumCols
);
3430 m_data
.Add( sa
, numRows
);
3434 wxGridTableMessage
msg( this,
3435 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3438 GetView()->ProcessTableMessage( msg
);
3444 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3446 size_t curNumRows
= m_data
.GetCount();
3448 if ( pos
>= curNumRows
)
3450 wxFAIL_MSG( wxString::Format
3452 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3454 (unsigned long)numRows
,
3455 (unsigned long)curNumRows
3461 if ( numRows
> curNumRows
- pos
)
3463 numRows
= curNumRows
- pos
;
3466 if ( numRows
>= curNumRows
)
3472 m_data
.RemoveAt( pos
, numRows
);
3477 wxGridTableMessage
msg( this,
3478 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3482 GetView()->ProcessTableMessage( msg
);
3488 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3492 size_t curNumRows
= m_data
.GetCount();
3493 size_t curNumCols
= ( curNumRows
> 0
3494 ? m_data
[0].GetCount()
3495 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3497 if ( pos
>= curNumCols
)
3499 return AppendCols( numCols
);
3502 for ( row
= 0; row
< curNumRows
; row
++ )
3504 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3506 m_data
[row
].Insert( wxEmptyString
, col
);
3512 wxGridTableMessage
msg( this,
3513 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3517 GetView()->ProcessTableMessage( msg
);
3523 bool wxGridStringTable::AppendCols( size_t numCols
)
3527 size_t curNumRows
= m_data
.GetCount();
3532 // TODO: something better than this ?
3534 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3539 for ( row
= 0; row
< curNumRows
; row
++ )
3541 m_data
[row
].Add( wxEmptyString
, numCols
);
3546 wxGridTableMessage
msg( this,
3547 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3550 GetView()->ProcessTableMessage( msg
);
3556 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3560 size_t curNumRows
= m_data
.GetCount();
3561 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3562 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3564 if ( pos
>= curNumCols
)
3566 wxFAIL_MSG( wxString::Format
3568 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3570 (unsigned long)numCols
,
3571 (unsigned long)curNumCols
3576 if ( numCols
> curNumCols
- pos
)
3578 numCols
= curNumCols
- pos
;
3581 for ( row
= 0; row
< curNumRows
; row
++ )
3583 if ( numCols
>= curNumCols
)
3585 m_data
[row
].Clear();
3589 m_data
[row
].RemoveAt( pos
, numCols
);
3595 wxGridTableMessage
msg( this,
3596 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3600 GetView()->ProcessTableMessage( msg
);
3606 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3608 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3610 // using default label
3612 return wxGridTableBase::GetRowLabelValue( row
);
3616 return m_rowLabels
[row
];
3620 wxString
wxGridStringTable::GetColLabelValue( int col
)
3622 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3624 // using default label
3626 return wxGridTableBase::GetColLabelValue( col
);
3630 return m_colLabels
[col
];
3634 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3636 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3638 int n
= m_rowLabels
.GetCount();
3641 for ( i
= n
; i
<= row
; i
++ )
3643 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3647 m_rowLabels
[row
] = value
;
3650 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3652 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3654 int n
= m_colLabels
.GetCount();
3657 for ( i
= n
; i
<= col
; i
++ )
3659 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3663 m_colLabels
[col
] = value
;
3667 //////////////////////////////////////////////////////////////////////
3668 //////////////////////////////////////////////////////////////////////
3670 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3672 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3673 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3674 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3675 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3676 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3677 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3678 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3681 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3683 const wxPoint
&pos
, const wxSize
&size
)
3684 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3689 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3693 // NO - don't do this because it will set both the x and y origin
3694 // coords to match the parent scrolled window and we just want to
3695 // set the y coord - MB
3697 // m_owner->PrepareDC( dc );
3700 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3701 dc
.SetDeviceOrigin( 0, -y
);
3703 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3704 m_owner
->DrawRowLabels( dc
, rows
);
3707 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3709 m_owner
->ProcessRowLabelMouseEvent( event
);
3712 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3714 m_owner
->GetEventHandler()->ProcessEvent(event
);
3717 // This seems to be required for wxMotif otherwise the mouse
3718 // cursor must be in the cell edit control to get key events
3720 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3722 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3726 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3728 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3732 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3734 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3738 //////////////////////////////////////////////////////////////////////
3740 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3742 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3743 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3744 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3745 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3746 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3747 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3748 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3751 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3753 const wxPoint
&pos
, const wxSize
&size
)
3754 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3759 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3763 // NO - don't do this because it will set both the x and y origin
3764 // coords to match the parent scrolled window and we just want to
3765 // set the x coord - MB
3767 // m_owner->PrepareDC( dc );
3770 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3771 dc
.SetDeviceOrigin( -x
, 0 );
3773 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3774 m_owner
->DrawColLabels( dc
, cols
);
3777 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3779 m_owner
->ProcessColLabelMouseEvent( event
);
3782 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3784 m_owner
->GetEventHandler()->ProcessEvent(event
);
3787 // This seems to be required for wxMotif otherwise the mouse
3788 // cursor must be in the cell edit control to get key events
3790 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3792 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3796 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3798 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3802 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3804 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3808 //////////////////////////////////////////////////////////////////////
3810 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3812 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3813 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3814 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3815 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3816 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3817 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3818 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3821 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3823 const wxPoint
&pos
, const wxSize
&size
)
3824 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3829 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3833 int client_height
= 0;
3834 int client_width
= 0;
3835 GetClientSize( &client_width
, &client_height
);
3837 // VZ: any reason for this ifdef? (FIXME)
3842 rect
.SetWidth( client_width
- 2 );
3843 rect
.SetHeight( client_height
- 2 );
3845 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3847 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3848 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3849 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3850 dc
.DrawLine( 0, 0, client_width
, 0 );
3851 dc
.DrawLine( 0, 0, 0, client_height
);
3853 dc
.SetPen( *wxWHITE_PEN
);
3854 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3855 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3856 #endif // __WXGTK__/!__WXGTK__
3859 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3861 m_owner
->ProcessCornerLabelMouseEvent( event
);
3864 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3866 m_owner
->GetEventHandler()->ProcessEvent(event
);
3869 // This seems to be required for wxMotif otherwise the mouse
3870 // cursor must be in the cell edit control to get key events
3872 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3874 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3878 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3880 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3884 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3886 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3890 //////////////////////////////////////////////////////////////////////
3892 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3894 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3895 EVT_PAINT( wxGridWindow::OnPaint
)
3896 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3897 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3898 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3899 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3900 EVT_CHAR ( wxGridWindow::OnChar
)
3901 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3902 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3903 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3906 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3907 wxGridRowLabelWindow
*rowLblWin
,
3908 wxGridColLabelWindow
*colLblWin
,
3911 const wxSize
&size
)
3912 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3913 wxT("grid window") )
3916 m_rowLabelWin
= rowLblWin
;
3917 m_colLabelWin
= colLblWin
;
3920 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3922 wxPaintDC
dc( this );
3923 m_owner
->PrepareDC( dc
);
3924 wxRegion reg
= GetUpdateRegion();
3925 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3926 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3928 #if WXGRID_DRAW_LINES
3929 m_owner
->DrawAllGridLines( dc
, reg
);
3932 m_owner
->DrawGridSpace( dc
);
3933 m_owner
->DrawHighlight( dc
, DirtyCells
);
3936 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3938 wxWindow::ScrollWindow( dx
, dy
, rect
);
3939 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3940 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3943 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3945 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3948 m_owner
->ProcessGridCellMouseEvent( event
);
3951 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3953 m_owner
->GetEventHandler()->ProcessEvent(event
);
3956 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3957 // cursor must be in the cell edit control to get key events
3959 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3961 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3965 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3967 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3971 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3973 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3977 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3981 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3983 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3987 //////////////////////////////////////////////////////////////////////
3989 // Internal Helper function for computing row or column from some
3990 // (unscrolled) coordinate value, using either
3991 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3992 // of m_rowBottoms/m_ColRights to speed up the search!
3994 // Internal helper macros for simpler use of that function
3996 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3997 const wxArrayInt
& BorderArray
, int nMax
,
4000 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
4001 m_minAcceptableColWidth, \
4002 m_colRights, m_numCols, true)
4003 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4004 m_minAcceptableRowHeight, \
4005 m_rowBottoms, m_numRows, true)
4006 /////////////////////////////////////////////////////////////////////
4008 #if wxUSE_EXTENDED_RTTI
4009 WX_DEFINE_FLAGS( wxGridStyle
)
4011 wxBEGIN_FLAGS( wxGridStyle
)
4012 // new style border flags, we put them first to
4013 // use them for streaming out
4014 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4015 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4016 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4017 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4018 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4019 wxFLAGS_MEMBER(wxBORDER_NONE
)
4021 // old style border flags
4022 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4023 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4024 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4025 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4026 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4027 wxFLAGS_MEMBER(wxBORDER
)
4029 // standard window styles
4030 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4031 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4032 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4033 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4034 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4035 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4036 wxFLAGS_MEMBER(wxVSCROLL
)
4037 wxFLAGS_MEMBER(wxHSCROLL
)
4039 wxEND_FLAGS( wxGridStyle
)
4041 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4043 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4044 wxHIDE_PROPERTY( Children
)
4045 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4046 wxEND_PROPERTIES_TABLE()
4048 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4049 wxEND_HANDLERS_TABLE()
4051 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4054 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
4057 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4060 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4061 EVT_PAINT( wxGrid::OnPaint
)
4062 EVT_SIZE( wxGrid::OnSize
)
4063 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4064 EVT_KEY_UP( wxGrid::OnKeyUp
)
4065 EVT_CHAR ( wxGrid::OnChar
)
4066 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4071 // in order to make sure that a size event is not
4072 // trigerred in a unfinished state
4073 m_cornerLabelWin
= NULL
;
4074 m_rowLabelWin
= NULL
;
4075 m_colLabelWin
= NULL
;
4079 wxGrid::wxGrid( wxWindow
*parent
,
4084 const wxString
& name
)
4085 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4086 m_colMinWidths(GRID_HASH_SIZE
),
4087 m_rowMinHeights(GRID_HASH_SIZE
)
4090 SetBestFittingSize(size
);
4093 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4094 const wxPoint
& pos
, const wxSize
& size
,
4095 long style
, const wxString
& name
)
4097 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4098 style
| wxWANTS_CHARS
, name
))
4101 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4102 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4105 SetBestFittingSize(size
);
4112 // Must do this or ~wxScrollHelper will pop the wrong event handler
4113 SetTargetWindow(this);
4115 wxSafeDecRef(m_defaultCellAttr
);
4117 #ifdef DEBUG_ATTR_CACHE
4118 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4119 wxPrintf(_T("wxGrid attribute cache statistics: "
4120 "total: %u, hits: %u (%u%%)\n"),
4121 total
, gs_nAttrCacheHits
,
4122 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4128 delete m_typeRegistry
;
4133 // ----- internal init and update functions
4136 // NOTE: If using the default visual attributes works everywhere then this can
4137 // be removed as well as the #else cases below.
4138 #define _USE_VISATTR 0
4141 #include "wx/listbox.h"
4144 void wxGrid::Create()
4146 m_created
= false; // set to true by CreateGrid
4148 m_table
= (wxGridTableBase
*) NULL
;
4151 m_cellEditCtrlEnabled
= false;
4153 m_defaultCellAttr
= new wxGridCellAttr();
4155 // Set default cell attributes
4156 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4157 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4158 m_defaultCellAttr
->SetFont(GetFont());
4159 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4160 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4161 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4164 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4165 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4167 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4168 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4171 m_defaultCellAttr
->SetTextColour(
4172 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4173 m_defaultCellAttr
->SetBackgroundColour(
4174 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4179 m_currentCellCoords
= wxGridNoCellCoords
;
4181 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4182 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4184 // create the type registry
4185 m_typeRegistry
= new wxGridTypeRegistry
;
4188 // subwindow components that make up the wxGrid
4189 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4194 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4199 m_colLabelWin
= new wxGridColLabelWindow( this,
4204 m_gridWin
= new wxGridWindow( this,
4211 SetTargetWindow( m_gridWin
);
4214 wxColour gfg
= gva
.colFg
;
4215 wxColour gbg
= gva
.colBg
;
4216 wxColour lfg
= lva
.colFg
;
4217 wxColour lbg
= lva
.colBg
;
4219 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4220 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4221 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4222 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4225 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4226 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4227 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4228 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4229 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4230 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4232 m_gridWin
->SetOwnForegroundColour(gfg
);
4233 m_gridWin
->SetOwnBackgroundColour(gbg
);
4238 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4239 wxGrid::wxGridSelectionModes selmode
)
4241 wxCHECK_MSG( !m_created
,
4243 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4245 m_numRows
= numRows
;
4246 m_numCols
= numCols
;
4248 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4249 m_table
->SetView( this );
4251 m_selection
= new wxGridSelection( this, selmode
);
4260 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4262 wxCHECK_RET( m_created
,
4263 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4265 m_selection
->SetSelectionMode( selmode
);
4268 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4270 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4271 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4273 return m_selection
->GetSelectionMode();
4276 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4277 wxGrid::wxGridSelectionModes selmode
)
4281 // stop all processing
4286 wxGridTableBase
*t
=m_table
;
4301 m_numRows
= table
->GetNumberRows();
4302 m_numCols
= table
->GetNumberCols();
4305 m_table
->SetView( this );
4306 m_ownTable
= takeOwnership
;
4307 m_selection
= new wxGridSelection( this, selmode
);
4319 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4320 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4322 if ( m_rowLabelWin
)
4324 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4328 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4331 m_labelTextColour
= wxColour( _T("BLACK") );
4334 m_attrCache
.row
= -1;
4335 m_attrCache
.col
= -1;
4336 m_attrCache
.attr
= NULL
;
4338 // TODO: something better than this ?
4340 m_labelFont
= this->GetFont();
4341 m_labelFont
.SetWeight( wxBOLD
);
4343 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4344 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4346 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4347 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4348 m_colLabelTextOrientation
= wxHORIZONTAL
;
4350 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4351 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4353 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4354 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4356 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4357 m_defaultRowHeight
+= 8;
4359 m_defaultRowHeight
+= 4;
4362 m_gridLineColour
= wxColour( 192,192,192 );
4363 m_gridLinesEnabled
= true;
4364 m_cellHighlightColour
= *wxBLACK
;
4365 m_cellHighlightPenWidth
= 2;
4366 m_cellHighlightROPenWidth
= 1;
4368 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4369 m_winCapture
= (wxWindow
*)NULL
;
4370 m_canDragRowSize
= true;
4371 m_canDragColSize
= true;
4372 m_canDragGridSize
= true;
4373 m_canDragCell
= false;
4375 m_dragRowOrCol
= -1;
4376 m_isDragging
= false;
4377 m_startDragPos
= wxDefaultPosition
;
4379 m_waitForSlowClick
= false;
4381 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4382 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4384 m_currentCellCoords
= wxGridNoCellCoords
;
4386 m_selectingTopLeft
= wxGridNoCellCoords
;
4387 m_selectingBottomRight
= wxGridNoCellCoords
;
4388 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4389 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4391 m_editable
= true; // default for whole grid
4393 m_inOnKeyDown
= false;
4399 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4400 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4403 // ----------------------------------------------------------------------------
4404 // the idea is to call these functions only when necessary because they create
4405 // quite big arrays which eat memory mostly unnecessary - in particular, if
4406 // default widths/heights are used for all rows/columns, we may not use these
4409 // with some extra code, it should be possible to only store the
4410 // widths/heights different from default ones but this will be done later...
4411 // ----------------------------------------------------------------------------
4413 void wxGrid::InitRowHeights()
4415 m_rowHeights
.Empty();
4416 m_rowBottoms
.Empty();
4418 m_rowHeights
.Alloc( m_numRows
);
4419 m_rowBottoms
.Alloc( m_numRows
);
4423 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4425 for ( int i
= 0; i
< m_numRows
; i
++ )
4427 rowBottom
+= m_defaultRowHeight
;
4428 m_rowBottoms
.Add( rowBottom
);
4432 void wxGrid::InitColWidths()
4434 m_colWidths
.Empty();
4435 m_colRights
.Empty();
4437 m_colWidths
.Alloc( m_numCols
);
4438 m_colRights
.Alloc( m_numCols
);
4441 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4443 for ( int i
= 0; i
< m_numCols
; i
++ )
4445 colRight
+= m_defaultColWidth
;
4446 m_colRights
.Add( colRight
);
4450 int wxGrid::GetColWidth(int col
) const
4452 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4455 int wxGrid::GetColLeft(int col
) const
4457 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4458 : m_colRights
[col
] - m_colWidths
[col
];
4461 int wxGrid::GetColRight(int col
) const
4463 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4467 int wxGrid::GetRowHeight(int row
) const
4469 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4472 int wxGrid::GetRowTop(int row
) const
4474 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4475 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4478 int wxGrid::GetRowBottom(int row
) const
4480 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4481 : m_rowBottoms
[row
];
4484 void wxGrid::CalcDimensions()
4487 GetClientSize( &cw
, &ch
);
4489 if ( m_rowLabelWin
->IsShown() )
4490 cw
-= m_rowLabelWidth
;
4491 if ( m_colLabelWin
->IsShown() )
4492 ch
-= m_colLabelHeight
;
4495 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4496 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4498 // take into account editor if shown
4499 if ( IsCellEditControlShown() )
4502 int r
= m_currentCellCoords
.GetRow();
4503 int c
= m_currentCellCoords
.GetCol();
4504 int x
= GetColLeft(c
);
4505 int y
= GetRowTop(r
);
4507 // how big is the editor
4508 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4509 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4510 editor
->GetControl()->GetSize(&w2
, &h2
);
4521 // preserve (more or less) the previous position
4523 GetViewStart( &x
, &y
);
4525 // ensure the position is valid for the new scroll ranges
4527 x
= wxMax( w
- 1, 0 );
4529 y
= wxMax( h
- 1, 0 );
4531 // do set scrollbar parameters
4532 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4533 GetScrollX(w
), GetScrollY(h
), x
, y
,
4534 GetBatchCount() != 0);
4536 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4537 // still must reposition the children
4542 void wxGrid::CalcWindowSizes()
4544 // escape if the window is has not been fully created yet
4546 if ( m_cornerLabelWin
== NULL
)
4550 GetClientSize( &cw
, &ch
);
4552 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4553 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4555 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4556 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4558 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4559 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4561 if ( m_gridWin
&& m_gridWin
->IsShown() )
4562 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4566 // this is called when the grid table sends a message to say that it
4567 // has been redimensioned
4569 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4572 bool result
= false;
4574 // Clear the attribute cache as the attribute might refer to a different
4575 // cell than stored in the cache after adding/removing rows/columns.
4578 // By the same reasoning, the editor should be dismissed if columns are
4579 // added or removed. And for consistency, it should IMHO always be
4580 // removed, not only if the cell "underneath" it actually changes.
4581 // For now, I intentionally do not save the editor's content as the
4582 // cell it might want to save that stuff to might no longer exist.
4583 HideCellEditControl();
4586 // if we were using the default widths/heights so far, we must change them
4588 if ( m_colWidths
.IsEmpty() )
4593 if ( m_rowHeights
.IsEmpty() )
4599 switch ( msg
.GetId() )
4601 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4603 size_t pos
= msg
.GetCommandInt();
4604 int numRows
= msg
.GetCommandInt2();
4606 m_numRows
+= numRows
;
4608 if ( !m_rowHeights
.IsEmpty() )
4610 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4611 m_rowBottoms
.Insert( 0, pos
, numRows
);
4615 bottom
= m_rowBottoms
[pos
- 1];
4617 for ( i
= pos
; i
< m_numRows
; i
++ )
4619 bottom
+= m_rowHeights
[i
];
4620 m_rowBottoms
[i
] = bottom
;
4624 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4626 // if we have just inserted cols into an empty grid the current
4627 // cell will be undefined...
4629 SetCurrentCell( 0, 0 );
4633 m_selection
->UpdateRows( pos
, numRows
);
4634 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4636 attrProvider
->UpdateAttrRows( pos
, numRows
);
4638 if ( !GetBatchCount() )
4641 m_rowLabelWin
->Refresh();
4647 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4649 int numRows
= msg
.GetCommandInt();
4650 int oldNumRows
= m_numRows
;
4651 m_numRows
+= numRows
;
4653 if ( !m_rowHeights
.IsEmpty() )
4655 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4656 m_rowBottoms
.Add( 0, numRows
);
4659 if ( oldNumRows
> 0 )
4660 bottom
= m_rowBottoms
[oldNumRows
- 1];
4662 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4664 bottom
+= m_rowHeights
[i
];
4665 m_rowBottoms
[i
] = bottom
;
4669 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4671 // if we have just inserted cols into an empty grid the current
4672 // cell will be undefined...
4674 SetCurrentCell( 0, 0 );
4677 if ( !GetBatchCount() )
4680 m_rowLabelWin
->Refresh();
4686 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4688 size_t pos
= msg
.GetCommandInt();
4689 int numRows
= msg
.GetCommandInt2();
4690 m_numRows
-= numRows
;
4692 if ( !m_rowHeights
.IsEmpty() )
4694 m_rowHeights
.RemoveAt( pos
, numRows
);
4695 m_rowBottoms
.RemoveAt( pos
, numRows
);
4698 for ( i
= 0; i
< m_numRows
; i
++ )
4700 h
+= m_rowHeights
[i
];
4701 m_rowBottoms
[i
] = h
;
4706 m_currentCellCoords
= wxGridNoCellCoords
;
4710 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4711 m_currentCellCoords
.Set( 0, 0 );
4715 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4716 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4719 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4720 // ifdef'd out following patch from Paul Gammans
4722 // No need to touch column attributes, unless we
4723 // removed _all_ rows, in this case, we remove
4724 // all column attributes.
4725 // I hate to do this here, but the
4726 // needed data is not available inside UpdateAttrRows.
4727 if ( !GetNumberRows() )
4728 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4731 if ( !GetBatchCount() )
4734 m_rowLabelWin
->Refresh();
4740 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4742 size_t pos
= msg
.GetCommandInt();
4743 int numCols
= msg
.GetCommandInt2();
4744 m_numCols
+= numCols
;
4746 if ( !m_colWidths
.IsEmpty() )
4748 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4749 m_colRights
.Insert( 0, pos
, numCols
);
4753 right
= m_colRights
[pos
- 1];
4755 for ( i
= pos
; i
< m_numCols
; i
++ )
4757 right
+= m_colWidths
[i
];
4758 m_colRights
[i
] = right
;
4762 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4764 // if we have just inserted cols into an empty grid the current
4765 // cell will be undefined...
4767 SetCurrentCell( 0, 0 );
4771 m_selection
->UpdateCols( pos
, numCols
);
4772 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4774 attrProvider
->UpdateAttrCols( pos
, numCols
);
4775 if ( !GetBatchCount() )
4778 m_colLabelWin
->Refresh();
4785 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4787 int numCols
= msg
.GetCommandInt();
4788 int oldNumCols
= m_numCols
;
4789 m_numCols
+= numCols
;
4790 if ( !m_colWidths
.IsEmpty() )
4792 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4793 m_colRights
.Add( 0, numCols
);
4796 if ( oldNumCols
> 0 )
4797 right
= m_colRights
[oldNumCols
- 1];
4799 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4801 right
+= m_colWidths
[i
];
4802 m_colRights
[i
] = right
;
4806 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4808 // if we have just inserted cols into an empty grid the current
4809 // cell will be undefined...
4811 SetCurrentCell( 0, 0 );
4813 if ( !GetBatchCount() )
4816 m_colLabelWin
->Refresh();
4822 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4824 size_t pos
= msg
.GetCommandInt();
4825 int numCols
= msg
.GetCommandInt2();
4826 m_numCols
-= numCols
;
4828 if ( !m_colWidths
.IsEmpty() )
4830 m_colWidths
.RemoveAt( pos
, numCols
);
4831 m_colRights
.RemoveAt( pos
, numCols
);
4834 for ( i
= 0; i
< m_numCols
; i
++ )
4836 w
+= m_colWidths
[i
];
4843 m_currentCellCoords
= wxGridNoCellCoords
;
4847 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4848 m_currentCellCoords
.Set( 0, 0 );
4852 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4853 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4856 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4857 // ifdef'd out following patch from Paul Gammans
4859 // No need to touch row attributes, unless we
4860 // removed _all_ columns, in this case, we remove
4861 // all row attributes.
4862 // I hate to do this here, but the
4863 // needed data is not available inside UpdateAttrCols.
4864 if ( !GetNumberCols() )
4865 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4868 if ( !GetBatchCount() )
4871 m_colLabelWin
->Refresh();
4878 if (result
&& !GetBatchCount() )
4879 m_gridWin
->Refresh();
4884 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4886 wxRegionIterator
iter( reg
);
4889 wxArrayInt rowlabels
;
4896 // TODO: remove this when we can...
4897 // There is a bug in wxMotif that gives garbage update
4898 // rectangles if you jump-scroll a long way by clicking the
4899 // scrollbar with middle button. This is a work-around
4901 #if defined(__WXMOTIF__)
4903 m_gridWin
->GetClientSize( &cw
, &ch
);
4904 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4905 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4908 // logical bounds of update region
4911 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4912 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4914 // find the row labels within these bounds
4917 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4919 if ( GetRowBottom(row
) < top
)
4922 if ( GetRowTop(row
) > bottom
)
4925 rowlabels
.Add( row
);
4934 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4936 wxRegionIterator
iter( reg
);
4939 wxArrayInt colLabels
;
4946 // TODO: remove this when we can...
4947 // There is a bug in wxMotif that gives garbage update
4948 // rectangles if you jump-scroll a long way by clicking the
4949 // scrollbar with middle button. This is a work-around
4951 #if defined(__WXMOTIF__)
4953 m_gridWin
->GetClientSize( &cw
, &ch
);
4954 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4955 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4958 // logical bounds of update region
4961 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4962 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4964 // find the cells within these bounds
4967 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4969 if ( GetColRight(col
) < left
)
4972 if ( GetColLeft(col
) > right
)
4975 colLabels
.Add( col
);
4984 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4986 wxRegionIterator
iter( reg
);
4989 wxGridCellCoordsArray cellsExposed
;
4991 int left
, top
, right
, bottom
;
4996 // TODO: remove this when we can...
4997 // There is a bug in wxMotif that gives garbage update
4998 // rectangles if you jump-scroll a long way by clicking the
4999 // scrollbar with middle button. This is a work-around
5001 #if defined(__WXMOTIF__)
5003 m_gridWin
->GetClientSize( &cw
, &ch
);
5004 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5005 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5006 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5007 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5010 // logical bounds of update region
5012 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5013 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5015 // find the cells within these bounds
5018 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5020 if ( GetRowBottom(row
) <= top
)
5023 if ( GetRowTop(row
) > bottom
)
5026 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
5028 if ( GetColRight(col
) <= left
)
5031 if ( GetColLeft(col
) > right
)
5034 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5041 return cellsExposed
;
5045 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5048 wxPoint
pos( event
.GetPosition() );
5049 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5051 if ( event
.Dragging() )
5055 m_isDragging
= true;
5056 m_rowLabelWin
->CaptureMouse();
5059 if ( event
.LeftIsDown() )
5061 switch ( m_cursorMode
)
5063 case WXGRID_CURSOR_RESIZE_ROW
:
5065 int cw
, ch
, left
, dummy
;
5066 m_gridWin
->GetClientSize( &cw
, &ch
);
5067 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5069 wxClientDC
dc( m_gridWin
);
5072 GetRowTop(m_dragRowOrCol
) +
5073 GetRowMinimalHeight(m_dragRowOrCol
) );
5074 dc
.SetLogicalFunction(wxINVERT
);
5075 if ( m_dragLastPos
>= 0 )
5077 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5079 dc
.DrawLine( left
, y
, left
+cw
, y
);
5084 case WXGRID_CURSOR_SELECT_ROW
:
5086 if ( (row
= YToRow( y
)) >= 0 )
5090 m_selection
->SelectRow( row
,
5091 event
.ControlDown(),
5100 // default label to suppress warnings about "enumeration value
5101 // 'xxx' not handled in switch
5109 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5114 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
5115 m_isDragging
= false;
5118 // ------------ Entering or leaving the window
5120 if ( event
.Entering() || event
.Leaving() )
5122 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5125 // ------------ Left button pressed
5127 else if ( event
.LeftDown() )
5129 // don't send a label click event for a hit on the
5130 // edge of the row label - this is probably the user
5131 // wanting to resize the row
5133 if ( YToEdgeOfRow(y
) < 0 )
5137 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5139 if ( !event
.ShiftDown() && !event
.CmdDown() )
5143 if ( event
.ShiftDown() )
5145 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5148 GetNumberCols() - 1,
5149 event
.ControlDown(),
5156 m_selection
->SelectRow( row
,
5157 event
.ControlDown(),
5164 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5169 // starting to drag-resize a row
5171 if ( CanDragRowSize() )
5172 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5176 // ------------ Left double click
5178 else if (event
.LeftDClick() )
5180 row
= YToEdgeOfRow(y
);
5185 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5187 // no default action at the moment
5192 // adjust row height depending on label text
5193 AutoSizeRowLabelSize( row
);
5195 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5200 // ------------ Left button released
5202 else if ( event
.LeftUp() )
5204 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5206 DoEndDragResizeRow();
5208 // Note: we are ending the event *after* doing
5209 // default processing in this case
5211 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5214 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5218 // ------------ Right button down
5220 else if ( event
.RightDown() )
5224 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5226 // no default action at the moment
5230 // ------------ Right double click
5232 else if ( event
.RightDClick() )
5236 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5238 // no default action at the moment
5242 // ------------ No buttons down and mouse moving
5244 else if ( event
.Moving() )
5246 m_dragRowOrCol
= YToEdgeOfRow( y
);
5247 if ( m_dragRowOrCol
>= 0 )
5249 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5251 // don't capture the mouse yet
5252 if ( CanDragRowSize() )
5253 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5256 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5258 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5263 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5266 wxPoint
pos( event
.GetPosition() );
5267 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5269 if ( event
.Dragging() )
5273 m_isDragging
= true;
5274 m_colLabelWin
->CaptureMouse();
5277 if ( event
.LeftIsDown() )
5279 switch ( m_cursorMode
)
5281 case WXGRID_CURSOR_RESIZE_COL
:
5283 int cw
, ch
, dummy
, top
;
5284 m_gridWin
->GetClientSize( &cw
, &ch
);
5285 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5287 wxClientDC
dc( m_gridWin
);
5290 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5291 GetColMinimalWidth(m_dragRowOrCol
));
5292 dc
.SetLogicalFunction(wxINVERT
);
5293 if ( m_dragLastPos
>= 0 )
5295 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5297 dc
.DrawLine( x
, top
, x
, top
+ch
);
5302 case WXGRID_CURSOR_SELECT_COL
:
5304 if ( (col
= XToCol( x
)) >= 0 )
5308 m_selection
->SelectCol( col
,
5309 event
.ControlDown(),
5318 // default label to suppress warnings about "enumeration value
5319 // 'xxx' not handled in switch
5327 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5332 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5333 m_isDragging
= false;
5336 // ------------ Entering or leaving the window
5338 if ( event
.Entering() || event
.Leaving() )
5340 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5343 // ------------ Left button pressed
5345 else if ( event
.LeftDown() )
5347 // don't send a label click event for a hit on the
5348 // edge of the col label - this is probably the user
5349 // wanting to resize the col
5351 if ( XToEdgeOfCol(x
) < 0 )
5355 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5357 if ( !event
.ShiftDown() && !event
.CmdDown() )
5361 if ( event
.ShiftDown() )
5363 m_selection
->SelectBlock( 0,
5364 m_currentCellCoords
.GetCol(),
5365 GetNumberRows() - 1, col
,
5366 event
.ControlDown(),
5373 m_selection
->SelectCol( col
,
5374 event
.ControlDown(),
5381 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5386 // starting to drag-resize a col
5388 if ( CanDragColSize() )
5389 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5393 // ------------ Left double click
5395 if ( event
.LeftDClick() )
5397 col
= XToEdgeOfCol(x
);
5402 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5404 // no default action at the moment
5409 // adjust column width depending on label text
5410 AutoSizeColLabelSize( col
);
5412 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5417 // ------------ Left button released
5419 else if ( event
.LeftUp() )
5421 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5423 DoEndDragResizeCol();
5425 // Note: we are ending the event *after* doing
5426 // default processing in this case
5428 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5431 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5435 // ------------ Right button down
5437 else if ( event
.RightDown() )
5441 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5443 // no default action at the moment
5447 // ------------ Right double click
5449 else if ( event
.RightDClick() )
5453 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5455 // no default action at the moment
5459 // ------------ No buttons down and mouse moving
5461 else if ( event
.Moving() )
5463 m_dragRowOrCol
= XToEdgeOfCol( x
);
5464 if ( m_dragRowOrCol
>= 0 )
5466 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5468 // don't capture the cursor yet
5469 if ( CanDragColSize() )
5470 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5473 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5475 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5480 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5482 if ( event
.LeftDown() )
5484 // indicate corner label by having both row and
5487 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5492 else if ( event
.LeftDClick() )
5494 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5496 else if ( event
.RightDown() )
5498 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5500 // no default action at the moment
5503 else if ( event
.RightDClick() )
5505 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5507 // no default action at the moment
5512 void wxGrid::ChangeCursorMode(CursorMode mode
,
5517 static const wxChar
*cursorModes
[] =
5526 wxLogTrace(_T("grid"),
5527 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5528 win
== m_colLabelWin
? _T("colLabelWin")
5529 : win
? _T("rowLabelWin")
5531 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5534 if ( mode
== m_cursorMode
&&
5535 win
== m_winCapture
&&
5536 captureMouse
== (m_winCapture
!= NULL
))
5541 // by default use the grid itself
5547 if (m_winCapture
->HasCapture())
5548 m_winCapture
->ReleaseMouse();
5549 m_winCapture
= (wxWindow
*)NULL
;
5552 m_cursorMode
= mode
;
5554 switch ( m_cursorMode
)
5556 case WXGRID_CURSOR_RESIZE_ROW
:
5557 win
->SetCursor( m_rowResizeCursor
);
5560 case WXGRID_CURSOR_RESIZE_COL
:
5561 win
->SetCursor( m_colResizeCursor
);
5565 win
->SetCursor( *wxSTANDARD_CURSOR
);
5569 // we need to capture mouse when resizing
5570 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5571 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5573 if ( captureMouse
&& resize
)
5575 win
->CaptureMouse();
5580 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5583 wxPoint
pos( event
.GetPosition() );
5584 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5586 wxGridCellCoords coords
;
5587 XYToCell( x
, y
, coords
);
5589 int cell_rows
, cell_cols
;
5590 bool isFirstDrag
= !m_isDragging
;
5591 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5592 if ((cell_rows
< 0) || (cell_cols
< 0))
5594 coords
.SetRow(coords
.GetRow() + cell_rows
);
5595 coords
.SetCol(coords
.GetCol() + cell_cols
);
5598 if ( event
.Dragging() )
5600 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5602 // Don't start doing anything until the mouse has been dragged at
5603 // least 3 pixels in any direction...
5606 if (m_startDragPos
== wxDefaultPosition
)
5608 m_startDragPos
= pos
;
5611 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5615 m_isDragging
= true;
5616 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5618 // Hide the edit control, so it
5619 // won't interfere with drag-shrinking.
5620 if ( IsCellEditControlShown() )
5622 HideCellEditControl();
5623 SaveEditControlValue();
5626 // Have we captured the mouse yet?
5629 m_winCapture
= m_gridWin
;
5630 m_winCapture
->CaptureMouse();
5633 if ( coords
!= wxGridNoCellCoords
)
5635 if ( event
.CmdDown() )
5637 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5638 m_selectingKeyboard
= coords
;
5639 HighlightBlock ( m_selectingKeyboard
, coords
);
5641 else if ( CanDragCell() )
5645 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5646 m_selectingKeyboard
= coords
;
5648 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5656 if ( !IsSelection() )
5658 HighlightBlock( coords
, coords
);
5662 HighlightBlock( m_currentCellCoords
, coords
);
5666 if (! IsVisible(coords
))
5668 MakeCellVisible(coords
);
5669 // TODO: need to introduce a delay or something here. The
5670 // scrolling is way to fast, at least on MSW - also on GTK.
5674 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5676 int cw
, ch
, left
, dummy
;
5677 m_gridWin
->GetClientSize( &cw
, &ch
);
5678 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5680 wxClientDC
dc( m_gridWin
);
5682 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5683 GetRowMinimalHeight(m_dragRowOrCol
) );
5684 dc
.SetLogicalFunction(wxINVERT
);
5685 if ( m_dragLastPos
>= 0 )
5687 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5689 dc
.DrawLine( left
, y
, left
+cw
, y
);
5692 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5694 int cw
, ch
, dummy
, top
;
5695 m_gridWin
->GetClientSize( &cw
, &ch
);
5696 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5698 wxClientDC
dc( m_gridWin
);
5700 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5701 GetColMinimalWidth(m_dragRowOrCol
) );
5702 dc
.SetLogicalFunction(wxINVERT
);
5703 if ( m_dragLastPos
>= 0 )
5705 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5707 dc
.DrawLine( x
, top
, x
, top
+ch
);
5714 m_isDragging
= false;
5715 m_startDragPos
= wxDefaultPosition
;
5717 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5718 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5721 if ( event
.Entering() || event
.Leaving() )
5723 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5724 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5729 // ------------ Left button pressed
5731 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5733 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5738 if ( !event
.CmdDown() )
5740 if ( event
.ShiftDown() )
5744 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5745 m_currentCellCoords
.GetCol(),
5748 event
.ControlDown(),
5754 else if ( XToEdgeOfCol(x
) < 0 &&
5755 YToEdgeOfRow(y
) < 0 )
5757 DisableCellEditControl();
5758 MakeCellVisible( coords
);
5760 if ( event
.CmdDown() )
5764 m_selection
->ToggleCellSelection( coords
.GetRow(),
5766 event
.ControlDown(),
5771 m_selectingTopLeft
= wxGridNoCellCoords
;
5772 m_selectingBottomRight
= wxGridNoCellCoords
;
5773 m_selectingKeyboard
= coords
;
5777 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5778 SetCurrentCell( coords
);
5781 if ( m_selection
->GetSelectionMode() !=
5782 wxGrid::wxGridSelectCells
)
5784 HighlightBlock( coords
, coords
);
5793 // ------------ Left double click
5795 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5797 DisableCellEditControl();
5799 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5801 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5806 // we want double click to select a cell and start editing
5807 // (i.e. to behave in same way as sequence of two slow clicks):
5808 m_waitForSlowClick
= true;
5814 // ------------ Left button released
5816 else if ( event
.LeftUp() )
5818 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5822 if (m_winCapture
->HasCapture())
5823 m_winCapture
->ReleaseMouse();
5824 m_winCapture
= NULL
;
5827 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
5830 EnableCellEditControl();
5832 wxGridCellAttr
*attr
= GetCellAttr(coords
);
5833 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5834 editor
->StartingClick();
5838 m_waitForSlowClick
= false;
5840 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5841 m_selectingBottomRight
!= wxGridNoCellCoords
)
5845 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5846 m_selectingTopLeft
.GetCol(),
5847 m_selectingBottomRight
.GetRow(),
5848 m_selectingBottomRight
.GetCol(),
5849 event
.ControlDown(),
5855 m_selectingTopLeft
= wxGridNoCellCoords
;
5856 m_selectingBottomRight
= wxGridNoCellCoords
;
5858 // Show the edit control, if it has been hidden for
5860 ShowCellEditControl();
5863 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5865 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5866 DoEndDragResizeRow();
5868 // Note: we are ending the event *after* doing
5869 // default processing in this case
5871 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5873 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5875 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5876 DoEndDragResizeCol();
5878 // Note: we are ending the event *after* doing
5879 // default processing in this case
5881 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5887 // ------------ Right button down
5889 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5891 DisableCellEditControl();
5892 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5897 // no default action at the moment
5901 // ------------ Right double click
5903 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5905 DisableCellEditControl();
5906 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5911 // no default action at the moment
5915 // ------------ Moving and no button action
5917 else if ( event
.Moving() && !event
.IsButton() )
5919 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5921 // out of grid cell area
5922 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5926 int dragRow
= YToEdgeOfRow( y
);
5927 int dragCol
= XToEdgeOfCol( x
);
5929 // Dragging on the corner of a cell to resize in both
5930 // directions is not implemented yet...
5932 if ( dragRow
>= 0 && dragCol
>= 0 )
5934 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5940 m_dragRowOrCol
= dragRow
;
5942 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5944 if ( CanDragRowSize() && CanDragGridSize() )
5945 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5948 else if ( dragCol
>= 0 )
5950 m_dragRowOrCol
= dragCol
;
5952 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5954 if ( CanDragColSize() && CanDragGridSize() )
5955 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5958 else // Neither on a row or col edge
5960 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5962 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5968 void wxGrid::DoEndDragResizeRow()
5970 if ( m_dragLastPos
>= 0 )
5972 // erase the last line and resize the row
5974 int cw
, ch
, left
, dummy
;
5975 m_gridWin
->GetClientSize( &cw
, &ch
);
5976 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5978 wxClientDC
dc( m_gridWin
);
5980 dc
.SetLogicalFunction( wxINVERT
);
5981 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5982 HideCellEditControl();
5983 SaveEditControlValue();
5985 int rowTop
= GetRowTop(m_dragRowOrCol
);
5986 SetRowSize( m_dragRowOrCol
,
5987 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5989 if ( !GetBatchCount() )
5991 // Only needed to get the correct rect.y:
5992 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5994 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5995 rect
.width
= m_rowLabelWidth
;
5996 rect
.height
= ch
- rect
.y
;
5997 m_rowLabelWin
->Refresh( true, &rect
);
5999 // if there is a multicell block, paint all of it
6002 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6003 int leftCol
= XToCol(left
);
6004 int rightCol
= internalXToCol(left
+cw
);
6007 for (i
=leftCol
; i
<rightCol
; i
++)
6009 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6010 if (cell_rows
< subtract_rows
)
6011 subtract_rows
= cell_rows
;
6013 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6014 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6015 rect
.height
= ch
- rect
.y
;
6018 m_gridWin
->Refresh( false, &rect
);
6021 ShowCellEditControl();
6026 void wxGrid::DoEndDragResizeCol()
6028 if ( m_dragLastPos
>= 0 )
6030 // erase the last line and resize the col
6032 int cw
, ch
, dummy
, top
;
6033 m_gridWin
->GetClientSize( &cw
, &ch
);
6034 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6036 wxClientDC
dc( m_gridWin
);
6038 dc
.SetLogicalFunction( wxINVERT
);
6039 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
6040 HideCellEditControl();
6041 SaveEditControlValue();
6043 int colLeft
= GetColLeft(m_dragRowOrCol
);
6044 SetColSize( m_dragRowOrCol
,
6045 wxMax( m_dragLastPos
- colLeft
,
6046 GetColMinimalWidth(m_dragRowOrCol
) ) );
6048 if ( !GetBatchCount() )
6050 // Only needed to get the correct rect.x:
6051 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6053 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6054 rect
.width
= cw
- rect
.x
;
6055 rect
.height
= m_colLabelHeight
;
6056 m_colLabelWin
->Refresh( true, &rect
);
6059 // if there is a multicell block, paint all of it
6062 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6063 int topRow
= YToRow(top
);
6064 int bottomRow
= internalYToRow(top
+cw
);
6067 for (i
=topRow
; i
<bottomRow
; i
++)
6069 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6070 if (cell_cols
< subtract_cols
)
6071 subtract_cols
= cell_cols
;
6073 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6074 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6075 rect
.width
= cw
- rect
.x
;
6079 m_gridWin
->Refresh( false, &rect
);
6082 ShowCellEditControl();
6087 // ------ interaction with data model
6089 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6091 switch ( msg
.GetId() )
6093 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6094 return GetModelValues();
6096 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6097 return SetModelValues();
6099 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6100 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6101 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6102 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6103 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6104 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6105 return Redimension( msg
);
6112 // The behaviour of this function depends on the grid table class
6113 // Clear() function. For the default wxGridStringTable class the
6114 // behavious is to replace all cell contents with wxEmptyString but
6115 // not to change the number of rows or cols.
6117 void wxGrid::ClearGrid()
6121 if (IsCellEditControlEnabled())
6122 DisableCellEditControl();
6125 if ( !GetBatchCount() ) m_gridWin
->Refresh();
6129 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6131 // TODO: something with updateLabels flag
6135 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6141 if (IsCellEditControlEnabled())
6142 DisableCellEditControl();
6144 bool done
= m_table
->InsertRows( pos
, numRows
);
6147 // the table will have sent the results of the insert row
6148 // operation to this view object as a grid table message
6153 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6155 // TODO: something with updateLabels flag
6159 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6165 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6167 // the table will have sent the results of the append row
6168 // operation to this view object as a grid table message
6173 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6175 // TODO: something with updateLabels flag
6179 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6185 if (IsCellEditControlEnabled())
6186 DisableCellEditControl();
6188 bool done
= m_table
->DeleteRows( pos
, numRows
);
6190 // the table will have sent the results of the delete row
6191 // operation to this view object as a grid table message
6196 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6198 // TODO: something with updateLabels flag
6202 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6208 if (IsCellEditControlEnabled())
6209 DisableCellEditControl();
6211 bool done
= m_table
->InsertCols( pos
, numCols
);
6213 // the table will have sent the results of the insert col
6214 // operation to this view object as a grid table message
6220 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6222 // TODO: something with updateLabels flag
6226 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6232 bool done
= m_table
->AppendCols( numCols
);
6234 // the table will have sent the results of the append col
6235 // operation to this view object as a grid table message
6241 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6243 // TODO: something with updateLabels flag
6247 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6253 if (IsCellEditControlEnabled())
6254 DisableCellEditControl();
6256 bool done
= m_table
->DeleteCols( pos
, numCols
);
6258 // the table will have sent the results of the delete col
6259 // operation to this view object as a grid table message
6266 // ----- event handlers
6269 // Generate a grid event based on a mouse event and
6270 // return the result of ProcessEvent()
6272 int wxGrid::SendEvent( const wxEventType type
,
6274 wxMouseEvent
& mouseEv
)
6276 bool claimed
, vetoed
;
6278 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6280 int rowOrCol
= (row
== -1 ? col
: row
);
6282 wxGridSizeEvent
gridEvt( GetId(),
6286 mouseEv
.GetX() + GetRowLabelSize(),
6287 mouseEv
.GetY() + GetColLabelSize(),
6288 mouseEv
.ControlDown(),
6289 mouseEv
.ShiftDown(),
6291 mouseEv
.MetaDown() );
6293 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6294 vetoed
= !gridEvt
.IsAllowed();
6296 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6298 // Right now, it should _never_ end up here!
6299 wxGridRangeSelectEvent
gridEvt( GetId(),
6303 m_selectingBottomRight
,
6305 mouseEv
.ControlDown(),
6306 mouseEv
.ShiftDown(),
6308 mouseEv
.MetaDown() );
6310 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6311 vetoed
= !gridEvt
.IsAllowed();
6315 wxGridEvent
gridEvt( GetId(),
6319 mouseEv
.GetX() + GetRowLabelSize(),
6320 mouseEv
.GetY() + GetColLabelSize(),
6322 mouseEv
.ControlDown(),
6323 mouseEv
.ShiftDown(),
6325 mouseEv
.MetaDown() );
6326 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6327 vetoed
= !gridEvt
.IsAllowed();
6330 // A Veto'd event may not be `claimed' so test this first
6334 return claimed
? 1 : 0;
6337 // Generate a grid event of specified type and return the result
6338 // of ProcessEvent().
6340 int wxGrid::SendEvent( const wxEventType type
,
6346 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6348 int rowOrCol
= (row
== -1 ? col
: row
);
6350 wxGridSizeEvent
gridEvt( GetId(),
6355 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6356 vetoed
= !gridEvt
.IsAllowed();
6360 wxGridEvent
gridEvt( GetId(),
6365 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6366 vetoed
= !gridEvt
.IsAllowed();
6369 // A Veto'd event may not be `claimed' so test this first
6373 return claimed
? 1 : 0;
6376 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6378 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6381 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6383 // Don't do anything if between Begin/EndBatch...
6384 // EndBatch() will do all this on the last nested one anyway.
6385 if (! GetBatchCount())
6387 // Refresh to get correct scrolled position:
6388 wxScrolledWindow::Refresh(eraseb
, rect
);
6392 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6393 int width_label
, width_cell
, height_label
, height_cell
;
6396 // Copy rectangle can get scroll offsets..
6397 rect_x
= rect
->GetX();
6398 rect_y
= rect
->GetY();
6399 rectWidth
= rect
->GetWidth();
6400 rectHeight
= rect
->GetHeight();
6402 width_label
= m_rowLabelWidth
- rect_x
;
6403 if (width_label
> rectWidth
)
6404 width_label
= rectWidth
;
6406 height_label
= m_colLabelHeight
- rect_y
;
6407 if (height_label
> rectHeight
) height_label
= rectHeight
;
6409 if (rect_x
> m_rowLabelWidth
)
6411 x
= rect_x
- m_rowLabelWidth
;
6412 width_cell
= rectWidth
;
6417 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6420 if (rect_y
> m_colLabelHeight
)
6422 y
= rect_y
- m_colLabelHeight
;
6423 height_cell
= rectHeight
;
6428 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6431 // Paint corner label part intersecting rect.
6432 if ( width_label
> 0 && height_label
> 0 )
6434 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6435 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6438 // Paint col labels part intersecting rect.
6439 if ( width_cell
> 0 && height_label
> 0 )
6441 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6442 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6445 // Paint row labels part intersecting rect.
6446 if ( width_label
> 0 && height_cell
> 0 )
6448 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6449 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6452 // Paint cell area part intersecting rect.
6453 if ( width_cell
> 0 && height_cell
> 0 )
6455 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6456 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6461 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6462 m_colLabelWin
->Refresh(eraseb
, NULL
);
6463 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6464 m_gridWin
->Refresh(eraseb
, NULL
);
6469 void wxGrid::OnSize( wxSizeEvent
& event
)
6471 // position the child windows
6474 // don't call CalcDimensions() from here, the base class handles the size
6479 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6481 if ( m_inOnKeyDown
)
6483 // shouldn't be here - we are going round in circles...
6485 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6488 m_inOnKeyDown
= true;
6490 // propagate the event up and see if it gets processed
6492 wxWindow
*parent
= GetParent();
6493 wxKeyEvent
keyEvt( event
);
6494 keyEvt
.SetEventObject( parent
);
6496 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6499 // try local handlers
6501 switch ( event
.GetKeyCode() )
6504 if ( event
.ControlDown() )
6506 MoveCursorUpBlock( event
.ShiftDown() );
6510 MoveCursorUp( event
.ShiftDown() );
6515 if ( event
.ControlDown() )
6517 MoveCursorDownBlock( event
.ShiftDown() );
6521 MoveCursorDown( event
.ShiftDown() );
6526 if ( event
.ControlDown() )
6528 MoveCursorLeftBlock( event
.ShiftDown() );
6532 MoveCursorLeft( event
.ShiftDown() );
6537 if ( event
.ControlDown() )
6539 MoveCursorRightBlock( event
.ShiftDown() );
6543 MoveCursorRight( event
.ShiftDown() );
6548 case WXK_NUMPAD_ENTER
:
6549 if ( event
.ControlDown() )
6551 event
.Skip(); // to let the edit control have the return
6555 if ( GetGridCursorRow() < GetNumberRows()-1 )
6557 MoveCursorDown( event
.ShiftDown() );
6561 // at the bottom of a column
6562 DisableCellEditControl();
6572 if (event
.ShiftDown())
6574 if ( GetGridCursorCol() > 0 )
6576 MoveCursorLeft( false );
6581 DisableCellEditControl();
6586 if ( GetGridCursorCol() < GetNumberCols()-1 )
6588 MoveCursorRight( false );
6593 DisableCellEditControl();
6599 if ( event
.ControlDown() )
6601 MakeCellVisible( 0, 0 );
6602 SetCurrentCell( 0, 0 );
6611 if ( event
.ControlDown() )
6613 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6614 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6631 if ( event
.ControlDown() )
6635 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6636 m_currentCellCoords
.GetCol(),
6637 event
.ControlDown(),
6644 if ( !IsEditable() )
6646 MoveCursorRight( false );
6649 // Otherwise fall through to default
6657 m_inOnKeyDown
= false;
6660 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6662 // try local handlers
6664 if ( event
.GetKeyCode() == WXK_SHIFT
)
6666 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6667 m_selectingBottomRight
!= wxGridNoCellCoords
)
6671 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6672 m_selectingTopLeft
.GetCol(),
6673 m_selectingBottomRight
.GetRow(),
6674 m_selectingBottomRight
.GetCol(),
6675 event
.ControlDown(),
6682 m_selectingTopLeft
= wxGridNoCellCoords
;
6683 m_selectingBottomRight
= wxGridNoCellCoords
;
6684 m_selectingKeyboard
= wxGridNoCellCoords
;
6688 void wxGrid::OnChar( wxKeyEvent
& event
)
6690 // is it possible to edit the current cell at all?
6691 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6693 // yes, now check whether the cells editor accepts the key
6694 int row
= m_currentCellCoords
.GetRow();
6695 int col
= m_currentCellCoords
.GetCol();
6696 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6697 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6699 // <F2> is special and will always start editing, for
6700 // other keys - ask the editor itself
6701 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6702 || editor
->IsAcceptedKey(event
) )
6704 // ensure cell is visble
6705 MakeCellVisible(row
, col
);
6706 EnableCellEditControl();
6708 // a problem can arise if the cell is not completely
6709 // visible (even after calling MakeCellVisible the
6710 // control is not created and calling StartingKey will
6712 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6713 editor
->StartingKey(event
);
6729 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6733 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6735 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6737 // the event has been intercepted - do nothing
6741 wxClientDC
dc( m_gridWin
);
6744 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6746 DisableCellEditControl();
6748 if ( IsVisible( m_currentCellCoords
, false ) )
6751 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
6752 if ( !m_gridLinesEnabled
)
6760 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6762 // Otherwise refresh redraws the highlight!
6763 m_currentCellCoords
= coords
;
6765 DrawGridCellArea( dc
, cells
);
6766 DrawAllGridLines( dc
, r
);
6770 m_currentCellCoords
= coords
;
6772 wxGridCellAttr
*attr
= GetCellAttr( coords
);
6773 DrawCellHighlight( dc
, attr
);
6777 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6780 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6784 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6787 rightCol
= GetNumberCols() - 1;
6789 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6792 bottomRow
= GetNumberRows() - 1;
6796 if ( topRow
> bottomRow
)
6803 if ( leftCol
> rightCol
)
6810 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6811 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6813 // First the case that we selected a completely new area
6814 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6815 m_selectingBottomRight
== wxGridNoCellCoords
)
6818 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6819 wxGridCellCoords ( bottomRow
, rightCol
) );
6820 m_gridWin
->Refresh( false, &rect
);
6823 // Now handle changing an existing selection area.
6824 else if ( m_selectingTopLeft
!= updateTopLeft
||
6825 m_selectingBottomRight
!= updateBottomRight
)
6827 // Compute two optimal update rectangles:
6828 // Either one rectangle is a real subset of the
6829 // other, or they are (almost) disjoint!
6831 bool need_refresh
[4];
6835 need_refresh
[3] = false;
6838 // Store intermediate values
6839 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6840 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6841 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6842 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6844 // Determine the outer/inner coordinates.
6845 if (oldLeft
> leftCol
)
6851 if (oldTop
> topRow
)
6857 if (oldRight
< rightCol
)
6860 oldRight
= rightCol
;
6863 if (oldBottom
< bottomRow
)
6866 oldBottom
= bottomRow
;
6870 // Now, either the stuff marked old is the outer
6871 // rectangle or we don't have a situation where one
6872 // is contained in the other.
6874 if ( oldLeft
< leftCol
)
6876 // Refresh the newly selected or deselected
6877 // area to the left of the old or new selection.
6878 need_refresh
[0] = true;
6879 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6881 wxGridCellCoords ( oldBottom
,
6885 if ( oldTop
< topRow
)
6887 // Refresh the newly selected or deselected
6888 // area above the old or new selection.
6889 need_refresh
[1] = true;
6890 rect
[1] = BlockToDeviceRect(
6891 wxGridCellCoords( oldTop
, leftCol
),
6892 wxGridCellCoords( topRow
- 1, rightCol
) );
6895 if ( oldRight
> rightCol
)
6897 // Refresh the newly selected or deselected
6898 // area to the right of the old or new selection.
6899 need_refresh
[2] = true;
6900 rect
[2] = BlockToDeviceRect(
6901 wxGridCellCoords ( oldTop
, rightCol
+ 1 ),
6902 wxGridCellCoords ( oldBottom
, oldRight
) );
6905 if ( oldBottom
> bottomRow
)
6907 // Refresh the newly selected or deselected
6908 // area below the old or new selection.
6909 need_refresh
[3] = true;
6910 rect
[3] = BlockToDeviceRect(
6911 wxGridCellCoords ( bottomRow
+ 1, leftCol
),
6912 wxGridCellCoords ( oldBottom
, rightCol
) );
6915 // various Refresh() calls
6916 for (i
= 0; i
< 4; i
++ )
6917 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6918 m_gridWin
->Refresh( false, &(rect
[i
]) );
6922 m_selectingTopLeft
= updateTopLeft
;
6923 m_selectingBottomRight
= updateBottomRight
;
6927 // ------ functions to get/send data (see also public functions)
6930 bool wxGrid::GetModelValues()
6932 // Hide the editor, so it won't hide a changed value.
6933 HideCellEditControl();
6937 // all we need to do is repaint the grid
6939 m_gridWin
->Refresh();
6946 bool wxGrid::SetModelValues()
6950 // Disable the editor, so it won't hide a changed value.
6951 // Do we also want to save the current value of the editor first?
6953 DisableCellEditControl();
6957 for ( row
= 0; row
< m_numRows
; row
++ )
6959 for ( col
= 0; col
< m_numCols
; col
++ )
6961 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6971 // Note - this function only draws cells that are in the list of
6972 // exposed cells (usually set from the update region by
6973 // CalcExposedCells)
6975 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6977 if ( !m_numRows
|| !m_numCols
)
6980 int i
, numCells
= cells
.GetCount();
6981 int row
, col
, cell_rows
, cell_cols
;
6982 wxGridCellCoordsArray redrawCells
;
6984 for ( i
= numCells
-1; i
>= 0; i
-- )
6986 row
= cells
[i
].GetRow();
6987 col
= cells
[i
].GetCol();
6988 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6990 // If this cell is part of a multicell block, find owner for repaint
6991 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6993 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6994 bool marked
= false;
6995 for ( int j
= 0; j
< numCells
; j
++ )
6997 if ( cell
== cells
[j
] )
7006 int count
= redrawCells
.GetCount();
7007 for (int j
= 0; j
< count
; j
++)
7009 if ( cell
== redrawCells
[j
] )
7017 redrawCells
.Add( cell
);
7020 // don't bother drawing this cell
7024 // If this cell is empty, find cell to left that might want to overflow
7025 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7027 for ( int l
= 0; l
< cell_rows
; l
++ )
7029 // find a cell in this row to left alreay marked for repaint
7031 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7032 if ((redrawCells
[k
].GetCol() < left
) &&
7033 (redrawCells
[k
].GetRow() == row
))
7035 left
= redrawCells
[k
].GetCol();
7039 left
= 0; // oh well
7041 for (int j
= col
- 1; j
>= left
; j
--)
7043 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7045 if (GetCellOverflow(row
+ l
, j
))
7047 wxGridCellCoords
cell(row
+ l
, j
);
7048 bool marked
= false;
7050 for (int k
= 0; k
< numCells
; k
++)
7052 if ( cell
== cells
[k
] )
7061 int count
= redrawCells
.GetCount();
7062 for (int k
= 0; k
< count
; k
++)
7064 if ( cell
== redrawCells
[k
] )
7071 redrawCells
.Add( cell
);
7080 DrawCell( dc
, cells
[i
] );
7083 numCells
= redrawCells
.GetCount();
7085 for ( i
= numCells
- 1; i
>= 0; i
-- )
7087 DrawCell( dc
, redrawCells
[i
] );
7091 void wxGrid::DrawGridSpace( wxDC
& dc
)
7094 m_gridWin
->GetClientSize( &cw
, &ch
);
7097 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7099 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
7100 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7102 if ( right
> rightCol
|| bottom
> bottomRow
)
7105 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7107 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7108 dc
.SetPen( *wxTRANSPARENT_PEN
);
7110 if ( right
> rightCol
)
7112 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7115 if ( bottom
> bottomRow
)
7117 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7122 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7124 int row
= coords
.GetRow();
7125 int col
= coords
.GetCol();
7127 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7130 // we draw the cell border ourselves
7131 #if !WXGRID_DRAW_LINES
7132 if ( m_gridLinesEnabled
)
7133 DrawCellBorder( dc
, coords
);
7136 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7138 bool isCurrent
= coords
== m_currentCellCoords
;
7140 wxRect rect
= CellToRect( row
, col
);
7142 // if the editor is shown, we should use it and not the renderer
7143 // Note: However, only if it is really _shown_, i.e. not hidden!
7144 if ( isCurrent
&& IsCellEditControlShown() )
7146 // OSAF NB: this "#if..." is temporary and fixes a problem where the
7147 // edit control is erased by this code after being rendered.
7148 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7149 // implicitly, causing this out-of order render.
7150 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7151 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7152 editor
->PaintBackground(rect
, attr
);
7158 // but all the rest is drawn by the cell renderer and hence may be
7160 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7161 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7168 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7170 int row
= m_currentCellCoords
.GetRow();
7171 int col
= m_currentCellCoords
.GetCol();
7173 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7176 wxRect rect
= CellToRect(row
, col
);
7178 // hmmm... what could we do here to show that the cell is disabled?
7179 // for now, I just draw a thinner border than for the other ones, but
7180 // it doesn't look really good
7182 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7186 // The center of th drawn line is where the position/width/height of
7187 // the rectangle is actually at, (on wxMSW atr least,) so we will
7188 // reduce the size of the rectangle to compensate for the thickness of
7189 // the line. If this is too strange on non wxMSW platforms then
7190 // please #ifdef this appropriately.
7191 rect
.x
+= penWidth
/ 2;
7192 rect
.y
+= penWidth
/ 2;
7193 rect
.width
-= penWidth
- 1;
7194 rect
.height
-= penWidth
- 1;
7196 // Now draw the rectangle
7197 // use the cellHighlightColour if the cell is inside a selection, this
7198 // will ensure the cell is always visible.
7199 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7200 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7201 dc
.DrawRectangle(rect
);
7205 // VZ: my experiments with 3D borders...
7207 // how to properly set colours for arbitrary bg?
7208 wxCoord x1
= rect
.x
,
7210 x2
= rect
.x
+ rect
.width
- 1,
7211 y2
= rect
.y
+ rect
.height
- 1;
7213 dc
.SetPen(*wxWHITE_PEN
);
7214 dc
.DrawLine(x1
, y1
, x2
, y1
);
7215 dc
.DrawLine(x1
, y1
, x1
, y2
);
7217 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7218 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7220 dc
.SetPen(*wxBLACK_PEN
);
7221 dc
.DrawLine(x1
, y2
, x2
, y2
);
7222 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7226 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7228 int row
= coords
.GetRow();
7229 int col
= coords
.GetCol();
7230 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7233 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7235 wxRect rect
= CellToRect( row
, col
);
7237 // right hand border
7239 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7240 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7244 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7245 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7248 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7250 // This if block was previously in wxGrid::OnPaint but that doesn't
7251 // seem to get called under wxGTK - MB
7253 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7254 m_numRows
&& m_numCols
)
7256 m_currentCellCoords
.Set(0, 0);
7259 if ( IsCellEditControlShown() )
7261 // don't show highlight when the edit control is shown
7265 // if the active cell was repainted, repaint its highlight too because it
7266 // might have been damaged by the grid lines
7267 size_t count
= cells
.GetCount();
7268 for ( size_t n
= 0; n
< count
; n
++ )
7270 if ( cells
[n
] == m_currentCellCoords
)
7272 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7273 DrawCellHighlight(dc
, attr
);
7281 // TODO: remove this ???
7282 // This is used to redraw all grid lines e.g. when the grid line colour
7285 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7287 #if !WXGRID_DRAW_LINES
7291 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7294 int top
, bottom
, left
, right
;
7296 #if 0 //#ifndef __WXGTK__
7300 m_gridWin
->GetClientSize(&cw
, &ch
);
7302 // virtual coords of visible area
7304 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7305 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7310 reg
.GetBox(x
, y
, w
, h
);
7311 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7312 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7316 m_gridWin
->GetClientSize(&cw
, &ch
);
7317 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7318 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7321 // avoid drawing grid lines past the last row and col
7323 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7324 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7326 // no gridlines inside multicells, clip them out
7327 int leftCol
= internalXToCol(left
);
7328 int topRow
= internalYToRow(top
);
7329 int rightCol
= internalXToCol(right
);
7330 int bottomRow
= internalYToRow(bottom
);
7333 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7334 wxRegion
clippedcells(0, 0, cw
, ch
);
7336 int i
, j
, cell_rows
, cell_cols
;
7339 for (j
=topRow
; j
<bottomRow
; j
++)
7341 for (i
=leftCol
; i
<rightCol
; i
++)
7343 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7344 if ((cell_rows
> 1) || (cell_cols
> 1))
7346 rect
= CellToRect(j
,i
);
7347 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7348 clippedcells
.Subtract(rect
);
7350 else if ((cell_rows
< 0) || (cell_cols
< 0))
7352 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7353 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7354 clippedcells
.Subtract(rect
);
7359 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7361 int i
, j
, cell_rows
, cell_cols
;
7364 for (j
=topRow
; j
<bottomRow
; j
++)
7366 for (i
=leftCol
; i
<rightCol
; i
++)
7368 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7369 if ((cell_rows
> 1) || (cell_cols
> 1))
7371 rect
= CellToRect(j
, i
);
7372 clippedcells
.Subtract(rect
);
7374 else if ((cell_rows
< 0) || (cell_cols
< 0))
7376 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7377 clippedcells
.Subtract(rect
);
7382 dc
.SetClippingRegion( clippedcells
);
7384 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7386 // horizontal grid lines
7388 // already declared above - int i;
7389 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7391 int bot
= GetRowBottom(i
) - 1;
7400 dc
.DrawLine( left
, bot
, right
, bot
);
7404 // vertical grid lines
7406 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7408 int colRight
= GetColRight(i
) - 1;
7409 if ( colRight
> right
)
7414 if ( colRight
>= left
)
7416 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7419 dc
.DestroyClippingRegion();
7422 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7428 size_t numLabels
= rows
.GetCount();
7430 for ( i
= 0; i
< numLabels
; i
++ )
7432 DrawRowLabel( dc
, rows
[i
] );
7436 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7438 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7445 rect
.SetY( GetRowTop(row
) + 1 );
7446 rect
.SetWidth( m_rowLabelWidth
- 2 );
7447 rect
.SetHeight( GetRowHeight(row
) - 2 );
7449 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7451 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7453 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7455 int rowTop
= GetRowTop(row
),
7456 rowBottom
= GetRowBottom(row
) - 1;
7458 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7459 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7460 m_rowLabelWidth
-1, rowBottom
);
7462 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7464 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7466 dc
.SetPen( *wxWHITE_PEN
);
7467 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7468 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7471 dc
.SetBackgroundMode( wxTRANSPARENT
);
7472 dc
.SetTextForeground( GetLabelTextColour() );
7473 dc
.SetFont( GetLabelFont() );
7476 GetRowLabelAlignment( &hAlign
, &vAlign
);
7479 rect
.SetY( GetRowTop(row
) + 2 );
7480 rect
.SetWidth( m_rowLabelWidth
- 4 );
7481 rect
.SetHeight( GetRowHeight(row
) - 4 );
7482 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7485 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7491 size_t numLabels
= cols
.GetCount();
7493 for ( i
= 0; i
< numLabels
; i
++ )
7495 DrawColLabel( dc
, cols
[i
] );
7499 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7501 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7504 int colLeft
= GetColLeft(col
);
7509 rect
.SetX( colLeft
+ 1 );
7511 rect
.SetWidth( GetColWidth(col
) - 2 );
7512 rect
.SetHeight( m_colLabelHeight
- 2 );
7514 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7516 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7518 int colRight
= GetColRight(col
) - 1;
7520 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
), 1, wxSOLID
) );
7521 dc
.DrawLine( colRight
, 0,
7522 colRight
, m_colLabelHeight
-1 );
7524 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7526 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7527 colRight
+1, m_colLabelHeight
-1 );
7529 dc
.SetPen( *wxWHITE_PEN
);
7530 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7531 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7534 dc
.SetBackgroundMode( wxTRANSPARENT
);
7535 dc
.SetTextForeground( GetLabelTextColour() );
7536 dc
.SetFont( GetLabelFont() );
7538 int hAlign
, vAlign
, orient
;
7539 GetColLabelAlignment( &hAlign
, &vAlign
);
7540 orient
= GetColLabelTextOrientation();
7542 rect
.SetX( colLeft
+ 2 );
7544 rect
.SetWidth( GetColWidth(col
) - 4 );
7545 rect
.SetHeight( m_colLabelHeight
- 4 );
7546 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7549 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7550 const wxString
& value
,
7554 int textOrientation
)
7556 wxArrayString lines
;
7558 StringToLines( value
, lines
);
7560 // Forward to new API.
7561 DrawTextRectangle( dc
,
7569 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7570 const wxArrayString
& lines
,
7574 int textOrientation
)
7576 long textWidth
= 0, textHeight
= 0;
7577 long lineWidth
= 0, lineHeight
= 0;
7580 dc
.SetClippingRegion( rect
);
7582 nLines
= lines
.GetCount();
7586 float x
= 0.0, y
= 0.0;
7588 if ( textOrientation
== wxHORIZONTAL
)
7589 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7591 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7593 switch ( vertAlign
)
7595 case wxALIGN_BOTTOM
:
7596 if ( textOrientation
== wxHORIZONTAL
)
7597 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7599 x
= rect
.x
+ rect
.width
- textWidth
;
7602 case wxALIGN_CENTRE
:
7603 if ( textOrientation
== wxHORIZONTAL
)
7604 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7606 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7611 if ( textOrientation
== wxHORIZONTAL
)
7618 // Align each line of a multi-line label
7619 for ( l
= 0; l
< nLines
; l
++ )
7621 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7623 switch ( horizAlign
)
7626 if ( textOrientation
== wxHORIZONTAL
)
7627 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7629 y
= rect
.y
+ lineWidth
+ 1;
7632 case wxALIGN_CENTRE
:
7633 if ( textOrientation
== wxHORIZONTAL
)
7634 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
7636 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
7641 if ( textOrientation
== wxHORIZONTAL
)
7644 y
= rect
.y
+ rect
.height
- 1;
7648 if ( textOrientation
== wxHORIZONTAL
)
7650 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7655 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7661 dc
.DestroyClippingRegion();
7664 // Split multi-line text up into an array of strings.
7665 // Any existing contents of the string array are preserved.
7667 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7671 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7672 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7674 while ( startPos
< (int)tVal
.length() )
7676 pos
= tVal
.Mid(startPos
).Find( eol
);
7681 else if ( pos
== 0 )
7683 lines
.Add( wxEmptyString
);
7687 lines
.Add( value
.Mid(startPos
, pos
) );
7689 startPos
+= pos
+ 1;
7692 if ( startPos
< (int)value
.length() )
7694 lines
.Add( value
.Mid( startPos
) );
7698 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
7699 const wxArrayString
& lines
,
7700 long *width
, long *height
)
7704 long lineW
= 0, lineH
= 0;
7707 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7709 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7710 w
= wxMax( w
, lineW
);
7719 // ------ Batch processing.
7721 void wxGrid::EndBatch()
7723 if ( m_batchCount
> 0 )
7726 if ( !m_batchCount
)
7729 m_rowLabelWin
->Refresh();
7730 m_colLabelWin
->Refresh();
7731 m_cornerLabelWin
->Refresh();
7732 m_gridWin
->Refresh();
7737 // Use this, rather than wxWindow::Refresh(), to force an immediate
7738 // repainting of the grid. Has no effect if you are already inside a
7739 // BeginBatch / EndBatch block.
7741 void wxGrid::ForceRefresh()
7747 bool wxGrid::Enable(bool enable
)
7749 if ( !wxScrolledWindow::Enable(enable
) )
7752 // redraw in the new state
7753 m_gridWin
->Refresh();
7759 // ------ Edit control functions
7762 void wxGrid::EnableEditing( bool edit
)
7764 // TODO: improve this ?
7766 if ( edit
!= m_editable
)
7769 EnableCellEditControl(edit
);
7774 void wxGrid::EnableCellEditControl( bool enable
)
7779 if ( enable
!= m_cellEditCtrlEnabled
)
7783 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7786 // this should be checked by the caller!
7787 wxASSERT_MSG( CanEnableCellControl(),
7788 _T("can't enable editing for this cell!") );
7790 // do it before ShowCellEditControl()
7791 m_cellEditCtrlEnabled
= enable
;
7793 ShowCellEditControl();
7797 //FIXME:add veto support
7798 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7800 HideCellEditControl();
7801 SaveEditControlValue();
7803 // do it after HideCellEditControl()
7804 m_cellEditCtrlEnabled
= enable
;
7809 bool wxGrid::IsCurrentCellReadOnly() const
7812 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7813 bool readonly
= attr
->IsReadOnly();
7819 bool wxGrid::CanEnableCellControl() const
7821 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7822 !IsCurrentCellReadOnly();
7826 bool wxGrid::IsCellEditControlEnabled() const
7828 // the cell edit control might be disable for all cells or just for the
7829 // current one if it's read only
7830 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7833 bool wxGrid::IsCellEditControlShown() const
7835 bool isShown
= false;
7837 if ( m_cellEditCtrlEnabled
)
7839 int row
= m_currentCellCoords
.GetRow();
7840 int col
= m_currentCellCoords
.GetCol();
7841 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7842 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7847 if ( editor
->IsCreated() )
7849 isShown
= editor
->GetControl()->IsShown();
7859 void wxGrid::ShowCellEditControl()
7861 if ( IsCellEditControlEnabled() )
7863 if ( !IsVisible( m_currentCellCoords
, false ) )
7865 m_cellEditCtrlEnabled
= false;
7870 wxRect rect
= CellToRect( m_currentCellCoords
);
7871 int row
= m_currentCellCoords
.GetRow();
7872 int col
= m_currentCellCoords
.GetCol();
7874 // if this is part of a multicell, find owner (topleft)
7875 int cell_rows
, cell_cols
;
7876 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7877 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7881 m_currentCellCoords
.SetRow( row
);
7882 m_currentCellCoords
.SetCol( col
);
7885 // convert to scrolled coords
7887 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7893 // done in PaintBackground()
7895 // erase the highlight and the cell contents because the editor
7896 // might not cover the entire cell
7897 wxClientDC
dc( m_gridWin
);
7899 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7900 dc
.SetPen(*wxTRANSPARENT_PEN
);
7901 dc
.DrawRectangle(rect
);
7904 // cell is shifted by one pixel
7905 // However, don't allow x or y to become negative
7906 // since the SetSize() method interprets that as
7913 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7914 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7915 if ( !editor
->IsCreated() )
7917 editor
->Create(m_gridWin
, wxID_ANY
,
7918 new wxGridCellEditorEvtHandler(this, editor
));
7920 wxGridEditorCreatedEvent
evt(GetId(),
7921 wxEVT_GRID_EDITOR_CREATED
,
7925 editor
->GetControl());
7926 GetEventHandler()->ProcessEvent(evt
);
7929 // resize editor to overflow into righthand cells if allowed
7930 int maxWidth
= rect
.width
;
7931 wxString value
= GetCellValue(row
, col
);
7932 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7935 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
7936 if (maxWidth
< rect
.width
)
7937 maxWidth
= rect
.width
;
7940 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7941 if (rect
.x
+ maxWidth
> client_right
)
7942 maxWidth
= client_right
- rect
.x
;
7944 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7946 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7947 // may have changed earlier
7948 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
7951 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7953 // looks weird going over a multicell
7954 if (m_table
->IsEmptyCell( row
, i
) &&
7955 (rect
.width
< maxWidth
) && (c_rows
== 1))
7957 rect
.width
+= GetColWidth( i
);
7963 if (rect
.GetRight() > client_right
)
7964 rect
.SetRight( client_right
- 1 );
7967 editor
->SetCellAttr( attr
);
7968 editor
->SetSize( rect
);
7969 editor
->GetControl()->Move(
7970 editor
->GetControl()->GetPosition().x
+ nXMove
,
7971 editor
->GetControl()->GetPosition().y
);
7972 editor
->Show( true, attr
);
7975 for (int i
= 0; i
< m_currentCellCoords
.GetCol(); i
++)
7977 colXPos
+= GetColSize( i
);
7980 int xUnit
= 1, yUnit
= 1;
7981 GetScrollPixelsPerUnit( &xUnit
, &yUnit
);
7982 if (m_currentCellCoords
.GetCol() != 0)
7983 Scroll( colXPos
/ xUnit
- 1, GetScrollPos( wxVERTICAL
) );
7985 Scroll( colXPos
/ xUnit
, GetScrollPos( wxVERTICAL
) );
7987 // recalc dimensions in case we need to
7988 // expand the scrolled window to account for editor
7991 editor
->BeginEdit(row
, col
, this);
7992 editor
->SetCellAttr(NULL
);
8000 void wxGrid::HideCellEditControl()
8002 if ( IsCellEditControlEnabled() )
8004 int row
= m_currentCellCoords
.GetRow();
8005 int col
= m_currentCellCoords
.GetCol();
8007 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8008 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8009 editor
->Show( false );
8013 m_gridWin
->SetFocus();
8015 // refresh whole row to the right
8016 wxRect
rect( CellToRect(row
, col
) );
8017 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8018 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8021 // ensure that the pixels under the focus ring get refreshed as well
8022 rect
.Inflate(10, 10);
8025 m_gridWin
->Refresh( false, &rect
);
8029 void wxGrid::SaveEditControlValue()
8031 if ( IsCellEditControlEnabled() )
8033 int row
= m_currentCellCoords
.GetRow();
8034 int col
= m_currentCellCoords
.GetCol();
8036 wxString oldval
= GetCellValue(row
, col
);
8038 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8039 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8040 bool changed
= editor
->EndEdit(row
, col
, this);
8047 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8048 m_currentCellCoords
.GetRow(),
8049 m_currentCellCoords
.GetCol() ) < 0 )
8051 // Event has been vetoed, set the data back.
8052 SetCellValue(row
, col
, oldval
);
8059 // ------ Grid location functions
8060 // Note that all of these functions work with the logical coordinates of
8061 // grid cells and labels so you will need to convert from device
8062 // coordinates for mouse events etc.
8065 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
8067 int row
= YToRow(y
);
8068 int col
= XToCol(x
);
8070 if ( row
== -1 || col
== -1 )
8072 coords
= wxGridNoCellCoords
;
8076 coords
.Set( row
, col
);
8080 // Internal Helper function for computing row or column from some
8081 // (unscrolled) coordinate value, using either
8082 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8083 // of m_rowBottoms/m_ColRights to speed up the search!
8085 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8086 const wxArrayInt
& BorderArray
, int nMax
,
8090 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8095 size_t i_max
= coord
/ defaultDist
,
8098 if (BorderArray
.IsEmpty())
8100 if ((int) i_max
< nMax
)
8102 return clipToMinMax
? nMax
- 1 : -1;
8105 if ( i_max
>= BorderArray
.GetCount())
8107 i_max
= BorderArray
.GetCount() - 1;
8111 if ( coord
>= BorderArray
[i_max
])
8115 i_max
= coord
/ minDist
;
8117 i_max
= BorderArray
.GetCount() - 1;
8120 if ( i_max
>= BorderArray
.GetCount())
8121 i_max
= BorderArray
.GetCount() - 1;
8124 if ( coord
>= BorderArray
[i_max
])
8125 return clipToMinMax
? (int)i_max
: -1;
8126 if ( coord
< BorderArray
[0] )
8129 while ( i_max
- i_min
> 0 )
8131 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8132 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8133 if (coord
>= BorderArray
[ i_max
- 1])
8137 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8138 if (coord
< BorderArray
[median
])
8147 int wxGrid::YToRow( int y
)
8149 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8150 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8153 int wxGrid::XToCol( int x
)
8155 return CoordToRowOrCol(x
, m_defaultColWidth
,
8156 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8159 // return the row number that that the y coord is near the edge of, or
8160 // -1 if not near an edge
8162 int wxGrid::YToEdgeOfRow( int y
)
8165 i
= internalYToRow(y
);
8167 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8169 // We know that we are in row i, test whether we are
8170 // close enough to lower or upper border, respectively.
8171 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8173 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8180 // return the col number that that the x coord is near the edge of, or
8181 // -1 if not near an edge
8183 int wxGrid::XToEdgeOfCol( int x
)
8186 i
= internalXToCol(x
);
8188 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8190 // We know that we are in column i, test whether we are
8191 // close enough to right or left border, respectively.
8192 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8194 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8201 wxRect
wxGrid::CellToRect( int row
, int col
)
8203 wxRect
rect( -1, -1, -1, -1 );
8205 if ( row
>= 0 && row
< m_numRows
&&
8206 col
>= 0 && col
< m_numCols
)
8208 int i
, cell_rows
, cell_cols
;
8209 rect
.width
= rect
.height
= 0;
8210 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8211 // if negative then find multicell owner
8216 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8218 rect
.x
= GetColLeft(col
);
8219 rect
.y
= GetRowTop(row
);
8220 for (i
=col
; i
< col
+ cell_cols
; i
++)
8221 rect
.width
+= GetColWidth(i
);
8222 for (i
=row
; i
< row
+ cell_rows
; i
++)
8223 rect
.height
+= GetRowHeight(i
);
8226 // if grid lines are enabled, then the area of the cell is a bit smaller
8227 if (m_gridLinesEnabled
)
8236 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8238 // get the cell rectangle in logical coords
8240 wxRect
r( CellToRect( row
, col
) );
8242 // convert to device coords
8244 int left
, top
, right
, bottom
;
8245 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8246 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8248 // check against the client area of the grid window
8250 m_gridWin
->GetClientSize( &cw
, &ch
);
8252 if ( wholeCellVisible
)
8254 // is the cell wholly visible ?
8256 return ( left
>= 0 && right
<= cw
&&
8257 top
>= 0 && bottom
<= ch
);
8261 // is the cell partly visible ?
8263 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8264 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8268 // make the specified cell location visible by doing a minimal amount
8271 void wxGrid::MakeCellVisible( int row
, int col
)
8274 int xpos
= -1, ypos
= -1;
8276 if ( row
>= 0 && row
< m_numRows
&&
8277 col
>= 0 && col
< m_numCols
)
8279 // get the cell rectangle in logical coords
8280 wxRect
r( CellToRect( row
, col
) );
8282 // convert to device coords
8283 int left
, top
, right
, bottom
;
8284 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8285 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8288 m_gridWin
->GetClientSize( &cw
, &ch
);
8294 else if ( bottom
> ch
)
8296 int h
= r
.GetHeight();
8298 for ( i
= row
-1; i
>= 0; i
-- )
8300 int rowHeight
= GetRowHeight(i
);
8301 if ( h
+ rowHeight
> ch
)
8308 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8309 // have rounding errors (this is important, because if we do, we
8310 // might not scroll at all and some cells won't be redrawn)
8312 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8314 ypos
+= m_scrollLineY
;
8317 // special handling for wide cells - show always left part of the cell!
8318 // Otherwise, e.g. when stepping from row to row, it would jump between
8319 // left and right part of the cell on every step!
8321 if ( left
< 0 || (right
-left
) >= cw
)
8325 else if ( right
> cw
)
8327 // position the view so that the cell is on the right
8329 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8330 xpos
= x0
+ (right
- cw
);
8332 // see comment for ypos above
8333 xpos
+= m_scrollLineX
;
8336 if ( xpos
!= -1 || ypos
!= -1 )
8339 xpos
/= m_scrollLineX
;
8341 ypos
/= m_scrollLineY
;
8342 Scroll( xpos
, ypos
);
8349 // ------ Grid cursor movement functions
8352 bool wxGrid::MoveCursorUp( bool expandSelection
)
8354 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8355 m_currentCellCoords
.GetRow() >= 0 )
8357 if ( expandSelection
)
8359 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8360 m_selectingKeyboard
= m_currentCellCoords
;
8361 if ( m_selectingKeyboard
.GetRow() > 0 )
8363 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8364 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8365 m_selectingKeyboard
.GetCol() );
8366 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8369 else if ( m_currentCellCoords
.GetRow() > 0 )
8371 int row
= m_currentCellCoords
.GetRow() - 1;
8372 int col
= m_currentCellCoords
.GetCol();
8374 MakeCellVisible( row
, col
);
8375 SetCurrentCell( row
, col
);
8386 bool wxGrid::MoveCursorDown( bool expandSelection
)
8388 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8389 m_currentCellCoords
.GetRow() < m_numRows
)
8391 if ( expandSelection
)
8393 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8394 m_selectingKeyboard
= m_currentCellCoords
;
8395 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8397 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8398 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8399 m_selectingKeyboard
.GetCol() );
8400 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8403 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8405 int row
= m_currentCellCoords
.GetRow() + 1;
8406 int col
= m_currentCellCoords
.GetCol();
8408 MakeCellVisible( row
, col
);
8409 SetCurrentCell( row
, col
);
8420 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8422 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8423 m_currentCellCoords
.GetCol() >= 0 )
8425 if ( expandSelection
)
8427 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8428 m_selectingKeyboard
= m_currentCellCoords
;
8429 if ( m_selectingKeyboard
.GetCol() > 0 )
8431 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8432 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8433 m_selectingKeyboard
.GetCol() );
8434 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8437 else if ( m_currentCellCoords
.GetCol() > 0 )
8439 int row
= m_currentCellCoords
.GetRow();
8440 int col
= m_currentCellCoords
.GetCol() - 1;
8442 MakeCellVisible( row
, col
);
8443 SetCurrentCell( row
, col
);
8454 bool wxGrid::MoveCursorRight( bool expandSelection
)
8456 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8457 m_currentCellCoords
.GetCol() < m_numCols
)
8459 if ( expandSelection
)
8461 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8462 m_selectingKeyboard
= m_currentCellCoords
;
8463 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8465 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8466 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8467 m_selectingKeyboard
.GetCol() );
8468 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8471 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8473 int row
= m_currentCellCoords
.GetRow();
8474 int col
= m_currentCellCoords
.GetCol() + 1;
8476 MakeCellVisible( row
, col
);
8477 SetCurrentCell( row
, col
);
8488 bool wxGrid::MovePageUp()
8490 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8493 int row
= m_currentCellCoords
.GetRow();
8497 m_gridWin
->GetClientSize( &cw
, &ch
);
8499 int y
= GetRowTop(row
);
8500 int newRow
= internalYToRow( y
- ch
+ 1 );
8502 if ( newRow
== row
)
8504 //row > 0 , so newrow can never be less than 0 here.
8508 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8509 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8517 bool wxGrid::MovePageDown()
8519 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8522 int row
= m_currentCellCoords
.GetRow();
8523 if ( (row
+1) < m_numRows
)
8526 m_gridWin
->GetClientSize( &cw
, &ch
);
8528 int y
= GetRowTop(row
);
8529 int newRow
= internalYToRow( y
+ ch
);
8530 if ( newRow
== row
)
8532 // row < m_numRows , so newrow can't overflow here.
8536 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8537 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8545 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8548 m_currentCellCoords
!= wxGridNoCellCoords
&&
8549 m_currentCellCoords
.GetRow() > 0 )
8551 int row
= m_currentCellCoords
.GetRow();
8552 int col
= m_currentCellCoords
.GetCol();
8554 if ( m_table
->IsEmptyCell(row
, col
) )
8556 // starting in an empty cell: find the next block of
8562 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8566 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
8568 // starting at the top of a block: find the next block
8574 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8580 // starting within a block: find the top of the block
8585 if ( m_table
->IsEmptyCell(row
, col
) )
8593 MakeCellVisible( row
, col
);
8594 if ( expandSelection
)
8596 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8597 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8602 SetCurrentCell( row
, col
);
8611 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8614 m_currentCellCoords
!= wxGridNoCellCoords
&&
8615 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8617 int row
= m_currentCellCoords
.GetRow();
8618 int col
= m_currentCellCoords
.GetCol();
8620 if ( m_table
->IsEmptyCell(row
, col
) )
8622 // starting in an empty cell: find the next block of
8625 while ( row
< m_numRows
- 1 )
8628 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8632 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
8634 // starting at the bottom of a block: find the next block
8637 while ( row
< m_numRows
- 1 )
8640 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8646 // starting within a block: find the bottom of the block
8648 while ( row
< m_numRows
- 1 )
8651 if ( m_table
->IsEmptyCell(row
, col
) )
8659 MakeCellVisible( row
, col
);
8660 if ( expandSelection
)
8662 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8663 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8668 SetCurrentCell( row
, col
);
8677 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8680 m_currentCellCoords
!= wxGridNoCellCoords
&&
8681 m_currentCellCoords
.GetCol() > 0 )
8683 int row
= m_currentCellCoords
.GetRow();
8684 int col
= m_currentCellCoords
.GetCol();
8686 if ( m_table
->IsEmptyCell(row
, col
) )
8688 // starting in an empty cell: find the next block of
8694 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8698 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
8700 // starting at the left of a block: find the next block
8706 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8712 // starting within a block: find the left of the block
8717 if ( m_table
->IsEmptyCell(row
, col
) )
8725 MakeCellVisible( row
, col
);
8726 if ( expandSelection
)
8728 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8729 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8734 SetCurrentCell( row
, col
);
8743 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8746 m_currentCellCoords
!= wxGridNoCellCoords
&&
8747 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8749 int row
= m_currentCellCoords
.GetRow();
8750 int col
= m_currentCellCoords
.GetCol();
8752 if ( m_table
->IsEmptyCell(row
, col
) )
8754 // starting in an empty cell: find the next block of
8757 while ( col
< m_numCols
- 1 )
8760 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8764 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
8766 // starting at the right of a block: find the next block
8769 while ( col
< m_numCols
- 1 )
8772 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8778 // starting within a block: find the right of the block
8780 while ( col
< m_numCols
- 1 )
8783 if ( m_table
->IsEmptyCell(row
, col
) )
8791 MakeCellVisible( row
, col
);
8792 if ( expandSelection
)
8794 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8795 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8800 SetCurrentCell( row
, col
);
8810 // ------ Label values and formatting
8813 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8816 *horiz
= m_rowLabelHorizAlign
;
8818 *vert
= m_rowLabelVertAlign
;
8821 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8824 *horiz
= m_colLabelHorizAlign
;
8826 *vert
= m_colLabelVertAlign
;
8829 int wxGrid::GetColLabelTextOrientation()
8831 return m_colLabelTextOrientation
;
8834 wxString
wxGrid::GetRowLabelValue( int row
)
8838 return m_table
->GetRowLabelValue( row
);
8848 wxString
wxGrid::GetColLabelValue( int col
)
8852 return m_table
->GetColLabelValue( col
);
8862 void wxGrid::SetRowLabelSize( int width
)
8864 width
= wxMax( width
, 0 );
8865 if ( width
!= m_rowLabelWidth
)
8869 m_rowLabelWin
->Show( false );
8870 m_cornerLabelWin
->Show( false );
8872 else if ( m_rowLabelWidth
== 0 )
8874 m_rowLabelWin
->Show( true );
8875 if ( m_colLabelHeight
> 0 )
8876 m_cornerLabelWin
->Show( true );
8879 m_rowLabelWidth
= width
;
8881 wxScrolledWindow::Refresh( true );
8885 void wxGrid::SetColLabelSize( int height
)
8887 height
= wxMax( height
, 0 );
8888 if ( height
!= m_colLabelHeight
)
8892 m_colLabelWin
->Show( false );
8893 m_cornerLabelWin
->Show( false );
8895 else if ( m_colLabelHeight
== 0 )
8897 m_colLabelWin
->Show( true );
8898 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8901 m_colLabelHeight
= height
;
8903 wxScrolledWindow::Refresh( true );
8907 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8909 if ( m_labelBackgroundColour
!= colour
)
8911 m_labelBackgroundColour
= colour
;
8912 m_rowLabelWin
->SetBackgroundColour( colour
);
8913 m_colLabelWin
->SetBackgroundColour( colour
);
8914 m_cornerLabelWin
->SetBackgroundColour( colour
);
8916 if ( !GetBatchCount() )
8918 m_rowLabelWin
->Refresh();
8919 m_colLabelWin
->Refresh();
8920 m_cornerLabelWin
->Refresh();
8925 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8927 if ( m_labelTextColour
!= colour
)
8929 m_labelTextColour
= colour
;
8930 if ( !GetBatchCount() )
8932 m_rowLabelWin
->Refresh();
8933 m_colLabelWin
->Refresh();
8938 void wxGrid::SetLabelFont( const wxFont
& font
)
8941 if ( !GetBatchCount() )
8943 m_rowLabelWin
->Refresh();
8944 m_colLabelWin
->Refresh();
8948 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8950 // allow old (incorrect) defs to be used
8953 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8954 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8955 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8960 case wxTOP
: vert
= wxALIGN_TOP
; break;
8961 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8962 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8965 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8967 m_rowLabelHorizAlign
= horiz
;
8970 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8972 m_rowLabelVertAlign
= vert
;
8975 if ( !GetBatchCount() )
8977 m_rowLabelWin
->Refresh();
8981 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8983 // allow old (incorrect) defs to be used
8986 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8987 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8988 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8993 case wxTOP
: vert
= wxALIGN_TOP
; break;
8994 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8995 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8998 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9000 m_colLabelHorizAlign
= horiz
;
9003 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9005 m_colLabelVertAlign
= vert
;
9008 if ( !GetBatchCount() )
9010 m_colLabelWin
->Refresh();
9014 // Note: under MSW, the default column label font must be changed because it
9015 // does not support vertical printing
9017 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9018 // pGrid->SetLabelFont(font);
9019 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9021 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9023 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9025 m_colLabelTextOrientation
= textOrientation
;
9028 if ( !GetBatchCount() )
9030 m_colLabelWin
->Refresh();
9034 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9038 m_table
->SetRowLabelValue( row
, s
);
9039 if ( !GetBatchCount() )
9041 wxRect rect
= CellToRect( row
, 0);
9042 if ( rect
.height
> 0 )
9044 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9046 rect
.width
= m_rowLabelWidth
;
9047 m_rowLabelWin
->Refresh( true, &rect
);
9053 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9057 m_table
->SetColLabelValue( col
, s
);
9058 if ( !GetBatchCount() )
9060 wxRect rect
= CellToRect( 0, col
);
9061 if ( rect
.width
> 0 )
9063 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9065 rect
.height
= m_colLabelHeight
;
9066 m_colLabelWin
->Refresh( true, &rect
);
9072 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9074 if ( m_gridLineColour
!= colour
)
9076 m_gridLineColour
= colour
;
9078 wxClientDC
dc( m_gridWin
);
9080 DrawAllGridLines( dc
, wxRegion() );
9084 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9086 if ( m_cellHighlightColour
!= colour
)
9088 m_cellHighlightColour
= colour
;
9090 wxClientDC
dc( m_gridWin
);
9092 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9093 DrawCellHighlight(dc
, attr
);
9098 void wxGrid::SetCellHighlightPenWidth(int width
)
9100 if (m_cellHighlightPenWidth
!= width
)
9102 m_cellHighlightPenWidth
= width
;
9104 // Just redrawing the cell highlight is not enough since that won't
9105 // make any visible change if the the thickness is getting smaller.
9106 int row
= m_currentCellCoords
.GetRow();
9107 int col
= m_currentCellCoords
.GetCol();
9108 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9111 wxRect rect
= CellToRect(row
, col
);
9112 m_gridWin
->Refresh(true, &rect
);
9116 void wxGrid::SetCellHighlightROPenWidth(int width
)
9118 if (m_cellHighlightROPenWidth
!= width
)
9120 m_cellHighlightROPenWidth
= width
;
9122 // Just redrawing the cell highlight is not enough since that won't
9123 // make any visible change if the the thickness is getting smaller.
9124 int row
= m_currentCellCoords
.GetRow();
9125 int col
= m_currentCellCoords
.GetCol();
9126 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9128 wxRect rect
= CellToRect(row
, col
);
9129 m_gridWin
->Refresh(true, &rect
);
9133 void wxGrid::EnableGridLines( bool enable
)
9135 if ( enable
!= m_gridLinesEnabled
)
9137 m_gridLinesEnabled
= enable
;
9139 if ( !GetBatchCount() )
9143 wxClientDC
dc( m_gridWin
);
9145 DrawAllGridLines( dc
, wxRegion() );
9149 m_gridWin
->Refresh();
9155 int wxGrid::GetDefaultRowSize()
9157 return m_defaultRowHeight
;
9160 int wxGrid::GetRowSize( int row
)
9162 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9164 return GetRowHeight(row
);
9167 int wxGrid::GetDefaultColSize()
9169 return m_defaultColWidth
;
9172 int wxGrid::GetColSize( int col
)
9174 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9176 return GetColWidth(col
);
9179 // ============================================================================
9180 // access to the grid attributes: each of them has a default value in the grid
9181 // itself and may be overidden on a per-cell basis
9182 // ============================================================================
9184 // ----------------------------------------------------------------------------
9185 // setting default attributes
9186 // ----------------------------------------------------------------------------
9188 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9190 m_defaultCellAttr
->SetBackgroundColour(col
);
9192 m_gridWin
->SetBackgroundColour(col
);
9196 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9198 m_defaultCellAttr
->SetTextColour(col
);
9201 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9203 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9206 void wxGrid::SetDefaultCellOverflow( bool allow
)
9208 m_defaultCellAttr
->SetOverflow(allow
);
9211 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9213 m_defaultCellAttr
->SetFont(font
);
9216 // For editors and renderers the type registry takes precedence over the
9217 // default attr, so we need to register the new editor/renderer for the string
9218 // data type in order to make setting a default editor/renderer appear to
9221 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9223 RegisterDataType(wxGRID_VALUE_STRING
,
9225 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9228 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9230 RegisterDataType(wxGRID_VALUE_STRING
,
9231 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9235 // ----------------------------------------------------------------------------
9236 // access to the default attrbiutes
9237 // ----------------------------------------------------------------------------
9239 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9241 return m_defaultCellAttr
->GetBackgroundColour();
9244 wxColour
wxGrid::GetDefaultCellTextColour()
9246 return m_defaultCellAttr
->GetTextColour();
9249 wxFont
wxGrid::GetDefaultCellFont()
9251 return m_defaultCellAttr
->GetFont();
9254 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9256 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9259 bool wxGrid::GetDefaultCellOverflow()
9261 return m_defaultCellAttr
->GetOverflow();
9264 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9266 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9269 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9271 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9274 // ----------------------------------------------------------------------------
9275 // access to cell attributes
9276 // ----------------------------------------------------------------------------
9278 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9280 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9281 wxColour colour
= attr
->GetBackgroundColour();
9287 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9289 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9290 wxColour colour
= attr
->GetTextColour();
9296 wxFont
wxGrid::GetCellFont( int row
, int col
)
9298 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9299 wxFont font
= attr
->GetFont();
9305 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9307 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9308 attr
->GetAlignment(horiz
, vert
);
9312 bool wxGrid::GetCellOverflow( int row
, int col
)
9314 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9315 bool allow
= attr
->GetOverflow();
9321 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9323 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9324 attr
->GetSize( num_rows
, num_cols
);
9328 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9330 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9331 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9337 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9339 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9340 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9346 bool wxGrid::IsReadOnly(int row
, int col
) const
9348 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9349 bool isReadOnly
= attr
->IsReadOnly();
9355 // ----------------------------------------------------------------------------
9356 // attribute support: cache, automatic provider creation, ...
9357 // ----------------------------------------------------------------------------
9359 bool wxGrid::CanHaveAttributes()
9366 return m_table
->CanHaveAttributes();
9369 void wxGrid::ClearAttrCache()
9371 if ( m_attrCache
.row
!= -1 )
9373 wxSafeDecRef(m_attrCache
.attr
);
9374 m_attrCache
.attr
= NULL
;
9375 m_attrCache
.row
= -1;
9379 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9383 wxGrid
*self
= (wxGrid
*)this; // const_cast
9385 self
->ClearAttrCache();
9386 self
->m_attrCache
.row
= row
;
9387 self
->m_attrCache
.col
= col
;
9388 self
->m_attrCache
.attr
= attr
;
9393 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9395 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9397 *attr
= m_attrCache
.attr
;
9398 wxSafeIncRef(m_attrCache
.attr
);
9400 #ifdef DEBUG_ATTR_CACHE
9401 gs_nAttrCacheHits
++;
9408 #ifdef DEBUG_ATTR_CACHE
9409 gs_nAttrCacheMisses
++;
9416 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9418 wxGridCellAttr
*attr
= NULL
;
9419 // Additional test to avoid looking at the cache e.g. for
9420 // wxNoCellCoords, as this will confuse memory management.
9423 if ( !LookupAttr(row
, col
, &attr
) )
9425 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9426 : (wxGridCellAttr
*)NULL
;
9427 CacheAttr(row
, col
, attr
);
9433 attr
->SetDefAttr(m_defaultCellAttr
);
9437 attr
= m_defaultCellAttr
;
9444 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9446 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9447 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9449 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9450 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9452 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9455 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9457 // artificially inc the ref count to match DecRef() in caller
9459 m_table
->SetAttr(attr
, row
, col
);
9465 // ----------------------------------------------------------------------------
9466 // setting column attributes (wrappers around SetColAttr)
9467 // ----------------------------------------------------------------------------
9469 void wxGrid::SetColFormatBool(int col
)
9471 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9474 void wxGrid::SetColFormatNumber(int col
)
9476 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9479 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9481 wxString typeName
= wxGRID_VALUE_FLOAT
;
9482 if ( (width
!= -1) || (precision
!= -1) )
9484 typeName
<< _T(':') << width
<< _T(',') << precision
;
9487 SetColFormatCustom(col
, typeName
);
9490 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9492 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9494 attr
= new wxGridCellAttr
;
9495 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9496 attr
->SetRenderer(renderer
);
9498 SetColAttr(col
, attr
);
9502 // ----------------------------------------------------------------------------
9503 // setting cell attributes: this is forwarded to the table
9504 // ----------------------------------------------------------------------------
9506 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9508 if ( CanHaveAttributes() )
9510 m_table
->SetAttr(attr
, row
, col
);
9519 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9521 if ( CanHaveAttributes() )
9523 m_table
->SetRowAttr(attr
, row
);
9532 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9534 if ( CanHaveAttributes() )
9536 m_table
->SetColAttr(attr
, col
);
9545 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9547 if ( CanHaveAttributes() )
9549 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9550 attr
->SetBackgroundColour(colour
);
9555 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9557 if ( CanHaveAttributes() )
9559 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9560 attr
->SetTextColour(colour
);
9565 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9567 if ( CanHaveAttributes() )
9569 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9570 attr
->SetFont(font
);
9575 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9577 if ( CanHaveAttributes() )
9579 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9580 attr
->SetAlignment(horiz
, vert
);
9585 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9587 if ( CanHaveAttributes() )
9589 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9590 attr
->SetOverflow(allow
);
9595 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9597 if ( CanHaveAttributes() )
9599 int cell_rows
, cell_cols
;
9601 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9602 attr
->GetSize(&cell_rows
, &cell_cols
);
9603 attr
->SetSize(num_rows
, num_cols
);
9606 // Cannot set the size of a cell to 0 or negative values
9607 // While it is perfectly legal to do that, this function cannot
9608 // handle all the possibilies, do it by hand by getting the CellAttr.
9609 // You can only set the size of a cell to 1,1 or greater with this fn
9610 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9611 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9612 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9613 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9615 // if this was already a multicell then "turn off" the other cells first
9616 if ((cell_rows
> 1) || (cell_rows
> 1))
9619 for (j
=row
; j
< row
+ cell_rows
; j
++)
9621 for (i
=col
; i
< col
+ cell_cols
; i
++)
9623 if ((i
!= col
) || (j
!= row
))
9625 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9626 attr_stub
->SetSize( 1, 1 );
9627 attr_stub
->DecRef();
9633 // mark the cells that will be covered by this cell to
9634 // negative or zero values to point back at this cell
9635 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9638 for (j
=row
; j
< row
+ num_rows
; j
++)
9640 for (i
=col
; i
< col
+ num_cols
; i
++)
9642 if ((i
!= col
) || (j
!= row
))
9644 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9645 attr_stub
->SetSize( row
- j
, col
- i
);
9646 attr_stub
->DecRef();
9654 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9656 if ( CanHaveAttributes() )
9658 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9659 attr
->SetRenderer(renderer
);
9664 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9666 if ( CanHaveAttributes() )
9668 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9669 attr
->SetEditor(editor
);
9674 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9676 if ( CanHaveAttributes() )
9678 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9679 attr
->SetReadOnly(isReadOnly
);
9684 // ----------------------------------------------------------------------------
9685 // Data type registration
9686 // ----------------------------------------------------------------------------
9688 void wxGrid::RegisterDataType(const wxString
& typeName
,
9689 wxGridCellRenderer
* renderer
,
9690 wxGridCellEditor
* editor
)
9692 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9696 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9698 wxString typeName
= m_table
->GetTypeName(row
, col
);
9699 return GetDefaultEditorForType(typeName
);
9702 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9704 wxString typeName
= m_table
->GetTypeName(row
, col
);
9705 return GetDefaultRendererForType(typeName
);
9708 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9710 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9711 if ( index
== wxNOT_FOUND
)
9715 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9716 wxFAIL_MSG(errStr
.c_str());
9721 return m_typeRegistry
->GetEditor(index
);
9724 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9726 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9727 if ( index
== wxNOT_FOUND
)
9731 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9732 wxFAIL_MSG(errStr
.c_str());
9737 return m_typeRegistry
->GetRenderer(index
);
9740 // ----------------------------------------------------------------------------
9742 // ----------------------------------------------------------------------------
9744 void wxGrid::EnableDragRowSize( bool enable
)
9746 m_canDragRowSize
= enable
;
9749 void wxGrid::EnableDragColSize( bool enable
)
9751 m_canDragColSize
= enable
;
9754 void wxGrid::EnableDragGridSize( bool enable
)
9756 m_canDragGridSize
= enable
;
9759 void wxGrid::EnableDragCell( bool enable
)
9761 m_canDragCell
= enable
;
9764 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9766 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9768 if ( resizeExistingRows
)
9770 // since we are resizing all rows to the default row size,
9771 // we can simply clear the row heights and row bottoms
9772 // arrays (which also allows us to take advantage of
9773 // some speed optimisations)
9774 m_rowHeights
.Empty();
9775 m_rowBottoms
.Empty();
9776 if ( !GetBatchCount() )
9781 void wxGrid::SetRowSize( int row
, int height
)
9783 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9785 // See comment in SetColSize
9786 if ( height
< GetRowMinimalAcceptableHeight())
9789 if ( m_rowHeights
.IsEmpty() )
9791 // need to really create the array
9795 int h
= wxMax( 0, height
);
9796 int diff
= h
- m_rowHeights
[row
];
9798 m_rowHeights
[row
] = h
;
9800 for ( i
= row
; i
< m_numRows
; i
++ )
9802 m_rowBottoms
[i
] += diff
;
9805 if ( !GetBatchCount() )
9809 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9811 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9813 if ( resizeExistingCols
)
9815 // since we are resizing all columns to the default column size,
9816 // we can simply clear the col widths and col rights
9817 // arrays (which also allows us to take advantage of
9818 // some speed optimisations)
9819 m_colWidths
.Empty();
9820 m_colRights
.Empty();
9821 if ( !GetBatchCount() )
9826 void wxGrid::SetColSize( int col
, int width
)
9828 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9830 // should we check that it's bigger than GetColMinimalWidth(col) here?
9832 // No, because it is reasonable to assume the library user know's
9833 // what he is doing. However whe should test against the weaker
9834 // constariant of minimalAcceptableWidth, as this breaks rendering
9836 // This test then fixes sf.net bug #645734
9838 if ( width
< GetColMinimalAcceptableWidth() )
9841 if ( m_colWidths
.IsEmpty() )
9843 // need to really create the array
9847 // if < 0 calc new width from label
9851 wxArrayString lines
;
9852 wxClientDC
dc(m_colLabelWin
);
9853 dc
.SetFont(GetLabelFont());
9854 StringToLines(GetColLabelValue(col
), lines
);
9855 GetTextBoxSize(dc
, lines
, &w
, &h
);
9859 int w
= wxMax( 0, width
);
9860 int diff
= w
- m_colWidths
[col
];
9861 m_colWidths
[col
] = w
;
9864 for ( i
= col
; i
< m_numCols
; i
++ )
9866 m_colRights
[i
] += diff
;
9869 if ( !GetBatchCount() )
9873 void wxGrid::SetColMinimalWidth( int col
, int width
)
9875 if (width
> GetColMinimalAcceptableWidth())
9877 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9878 m_colMinWidths
[key
] = width
;
9882 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9884 if (width
> GetRowMinimalAcceptableHeight())
9886 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9887 m_rowMinHeights
[key
] = width
;
9891 int wxGrid::GetColMinimalWidth(int col
) const
9893 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9894 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9896 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9899 int wxGrid::GetRowMinimalHeight(int row
) const
9901 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9902 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9904 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9907 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9909 // We do allow a width of 0 since this gives us
9910 // an easy way to temporarily hiding columns.
9912 m_minAcceptableColWidth
= width
;
9915 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9917 // We do allow a height of 0 since this gives us
9918 // an easy way to temporarily hiding rows.
9920 m_minAcceptableRowHeight
= height
;
9923 int wxGrid::GetColMinimalAcceptableWidth() const
9925 return m_minAcceptableColWidth
;
9928 int wxGrid::GetRowMinimalAcceptableHeight() const
9930 return m_minAcceptableRowHeight
;
9933 // ----------------------------------------------------------------------------
9935 // ----------------------------------------------------------------------------
9937 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9939 wxClientDC
dc(m_gridWin
);
9941 // cancel editing of cell
9942 HideCellEditControl();
9943 SaveEditControlValue();
9945 // init both of them to avoid compiler warnings, even if we only need one
9953 wxCoord extent
, extentMax
= 0;
9954 int max
= column
? m_numRows
: m_numCols
;
9955 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9962 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9963 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
9966 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9967 extent
= column
? size
.x
: size
.y
;
9968 if ( extent
> extentMax
)
9979 // now also compare with the column label extent
9981 dc
.SetFont( GetLabelFont() );
9985 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9986 if ( GetColLabelTextOrientation() == wxVERTICAL
)
9990 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9992 extent
= column
? w
: h
;
9993 if ( extent
> extentMax
)
10000 // empty column - give default extent (notice that if extentMax is less
10001 // than default extent but != 0, it's OK)
10002 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10007 // leave some space around text
10015 SetColSize( col
, extentMax
);
10016 if ( !GetBatchCount() )
10019 m_gridWin
->GetClientSize( &cw
, &ch
);
10020 wxRect
rect ( CellToRect( 0, col
) );
10022 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10023 rect
.width
= cw
- rect
.x
;
10024 rect
.height
= m_colLabelHeight
;
10025 m_colLabelWin
->Refresh( true, &rect
);
10030 SetRowSize(row
, extentMax
);
10031 if ( !GetBatchCount() )
10034 m_gridWin
->GetClientSize( &cw
, &ch
);
10035 wxRect
rect ( CellToRect( row
, 0 ) );
10037 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10038 rect
.width
= m_rowLabelWidth
;
10039 rect
.height
= ch
- rect
.y
;
10040 m_rowLabelWin
->Refresh( true, &rect
);
10047 SetColMinimalWidth(col
, extentMax
);
10049 SetRowMinimalHeight(row
, extentMax
);
10053 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10055 int width
= m_rowLabelWidth
;
10060 for ( int col
= 0; col
< m_numCols
; col
++ )
10064 AutoSizeColumn(col
, setAsMin
);
10067 width
+= GetColWidth(col
);
10076 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10078 int height
= m_colLabelHeight
;
10083 for ( int row
= 0; row
< m_numRows
; row
++ )
10086 AutoSizeRow(row
, setAsMin
);
10088 height
+= GetRowHeight(row
);
10097 void wxGrid::AutoSize()
10101 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
10103 // round up the size to a multiple of scroll step - this ensures that we
10104 // won't get the scrollbars if we're sized exactly to this width
10105 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10108 GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
10109 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10111 // distribute the extra space between the columns/rows to avoid having
10112 // extra white space
10114 // Remove the extra m_extraWidth + 1 added above
10115 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10116 if ( diff
&& m_numCols
)
10118 // try to resize the columns uniformly
10119 wxCoord diffPerCol
= diff
/ m_numCols
;
10122 for ( int col
= 0; col
< m_numCols
; col
++ )
10124 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10128 // add remaining amount to the last columns
10129 diff
-= diffPerCol
* m_numCols
;
10132 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10134 SetColSize(col
, GetColWidth(col
) + 1);
10140 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10141 if ( diff
&& m_numRows
)
10143 // try to resize the columns uniformly
10144 wxCoord diffPerRow
= diff
/ m_numRows
;
10147 for ( int row
= 0; row
< m_numRows
; row
++ )
10149 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10153 // add remaining amount to the last rows
10154 diff
-= diffPerRow
* m_numRows
;
10157 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10159 SetRowSize(row
, GetRowHeight(row
) + 1);
10166 SetClientSize(sizeFit
);
10169 void wxGrid::AutoSizeRowLabelSize( int row
)
10171 wxArrayString lines
;
10174 // Hide the edit control, so it
10175 // won't interfere with drag-shrinking.
10176 if ( IsCellEditControlShown() )
10178 HideCellEditControl();
10179 SaveEditControlValue();
10182 // autosize row height depending on label text
10183 StringToLines( GetRowLabelValue( row
), lines
);
10184 wxClientDC
dc( m_rowLabelWin
);
10185 GetTextBoxSize( dc
, lines
, &w
, &h
);
10186 if ( h
< m_defaultRowHeight
)
10187 h
= m_defaultRowHeight
;
10188 SetRowSize(row
, h
);
10192 void wxGrid::AutoSizeColLabelSize( int col
)
10194 wxArrayString lines
;
10197 // Hide the edit control, so it
10198 // won't interfer with drag-shrinking.
10199 if ( IsCellEditControlShown() )
10201 HideCellEditControl();
10202 SaveEditControlValue();
10205 // autosize column width depending on label text
10206 StringToLines( GetColLabelValue( col
), lines
);
10207 wxClientDC
dc( m_colLabelWin
);
10208 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10209 GetTextBoxSize( dc
, lines
, &w
, &h
);
10211 GetTextBoxSize( dc
, lines
, &h
, &w
);
10212 if ( w
< m_defaultColWidth
)
10213 w
= m_defaultColWidth
;
10214 SetColSize(col
, w
);
10218 wxSize
wxGrid::DoGetBestSize() const
10220 // don't set sizes, only calculate them
10221 wxGrid
*self
= (wxGrid
*)this; // const_cast
10224 width
= self
->SetOrCalcColumnSizes(true);
10225 height
= self
->SetOrCalcRowSizes(true);
10232 // Round up to a multiple the scroll rate
10233 // NOTE: this still doesn't get rid of the scrollbars;
10234 // is there any magic incantation for that?
10236 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10238 width
+= 1 + xpu
- (width
% xpu
);
10240 height
+= 1 + ypu
- (height
% ypu
);
10242 // limit to 1/4 of the screen size
10243 int maxwidth
, maxheight
;
10244 wxDisplaySize( &maxwidth
, &maxheight
);
10247 if ( width
> maxwidth
)
10249 if ( height
> maxheight
)
10250 height
= maxheight
;
10252 wxSize
best(width
, height
);
10254 // NOTE: This size should be cached, but first we need to add calls to
10255 // InvalidateBestSize everywhere that could change the results of this
10257 // CacheBestSize(size);
10267 wxPen
& wxGrid::GetDividerPen() const
10272 // ----------------------------------------------------------------------------
10273 // cell value accessor functions
10274 // ----------------------------------------------------------------------------
10276 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10280 m_table
->SetValue( row
, col
, s
);
10281 if ( !GetBatchCount() )
10284 wxRect
rect( CellToRect( row
, col
) );
10286 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10287 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10288 m_gridWin
->Refresh( false, &rect
);
10291 if ( m_currentCellCoords
.GetRow() == row
&&
10292 m_currentCellCoords
.GetCol() == col
&&
10293 IsCellEditControlShown())
10294 // Note: If we are using IsCellEditControlEnabled,
10295 // this interacts badly with calling SetCellValue from
10296 // an EVT_GRID_CELL_CHANGE handler.
10298 HideCellEditControl();
10299 ShowCellEditControl(); // will reread data from table
10304 // ----------------------------------------------------------------------------
10305 // block, row and column selection
10306 // ----------------------------------------------------------------------------
10308 void wxGrid::SelectRow( int row
, bool addToSelected
)
10310 if ( IsSelection() && !addToSelected
)
10314 m_selection
->SelectRow( row
, false, addToSelected
);
10317 void wxGrid::SelectCol( int col
, bool addToSelected
)
10319 if ( IsSelection() && !addToSelected
)
10323 m_selection
->SelectCol( col
, false, addToSelected
);
10326 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10327 bool addToSelected
)
10329 if ( IsSelection() && !addToSelected
)
10333 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10334 false, addToSelected
);
10337 void wxGrid::SelectAll()
10339 if ( m_numRows
> 0 && m_numCols
> 0 )
10342 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10346 // ----------------------------------------------------------------------------
10347 // cell, row and col deselection
10348 // ----------------------------------------------------------------------------
10350 void wxGrid::DeselectRow( int row
)
10352 if ( !m_selection
)
10355 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10357 if ( m_selection
->IsInSelection(row
, 0 ) )
10358 m_selection
->ToggleCellSelection(row
, 0);
10362 int nCols
= GetNumberCols();
10363 for ( int i
= 0; i
< nCols
; i
++ )
10365 if ( m_selection
->IsInSelection(row
, i
) )
10366 m_selection
->ToggleCellSelection(row
, i
);
10371 void wxGrid::DeselectCol( int col
)
10373 if ( !m_selection
)
10376 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10378 if ( m_selection
->IsInSelection(0, col
) )
10379 m_selection
->ToggleCellSelection(0, col
);
10383 int nRows
= GetNumberRows();
10384 for ( int i
= 0; i
< nRows
; i
++ )
10386 if ( m_selection
->IsInSelection(i
, col
) )
10387 m_selection
->ToggleCellSelection(i
, col
);
10392 void wxGrid::DeselectCell( int row
, int col
)
10394 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10395 m_selection
->ToggleCellSelection(row
, col
);
10398 bool wxGrid::IsSelection()
10400 return ( m_selection
&& (m_selection
->IsSelection() ||
10401 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10402 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10405 bool wxGrid::IsInSelection( int row
, int col
) const
10407 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10408 ( row
>= m_selectingTopLeft
.GetRow() &&
10409 col
>= m_selectingTopLeft
.GetCol() &&
10410 row
<= m_selectingBottomRight
.GetRow() &&
10411 col
<= m_selectingBottomRight
.GetCol() )) );
10414 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10418 wxGridCellCoordsArray a
;
10422 return m_selection
->m_cellSelection
;
10425 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10429 wxGridCellCoordsArray a
;
10433 return m_selection
->m_blockSelectionTopLeft
;
10436 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10440 wxGridCellCoordsArray a
;
10444 return m_selection
->m_blockSelectionBottomRight
;
10447 wxArrayInt
wxGrid::GetSelectedRows() const
10455 return m_selection
->m_rowSelection
;
10458 wxArrayInt
wxGrid::GetSelectedCols() const
10466 return m_selection
->m_colSelection
;
10469 void wxGrid::ClearSelection()
10471 m_selectingTopLeft
= wxGridNoCellCoords
;
10472 m_selectingBottomRight
= wxGridNoCellCoords
;
10474 m_selection
->ClearSelection();
10477 // This function returns the rectangle that encloses the given block
10478 // in device coords clipped to the client size of the grid window.
10480 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10481 const wxGridCellCoords
&bottomRight
)
10483 wxRect
rect( wxGridNoCellRect
);
10486 cellRect
= CellToRect( topLeft
);
10487 if ( cellRect
!= wxGridNoCellRect
)
10493 rect
= wxRect(0, 0, 0, 0);
10496 cellRect
= CellToRect( bottomRight
);
10497 if ( cellRect
!= wxGridNoCellRect
)
10503 return wxGridNoCellRect
;
10507 int left
= rect
.GetLeft();
10508 int top
= rect
.GetTop();
10509 int right
= rect
.GetRight();
10510 int bottom
= rect
.GetBottom();
10512 int leftCol
= topLeft
.GetCol();
10513 int topRow
= topLeft
.GetRow();
10514 int rightCol
= bottomRight
.GetCol();
10515 int bottomRow
= bottomRight
.GetRow();
10523 leftCol
= rightCol
;
10533 topRow
= bottomRow
;
10537 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10539 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10541 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10543 cellRect
= CellToRect( j
, i
);
10545 if (cellRect
.x
< left
)
10547 if (cellRect
.y
< top
)
10549 if (cellRect
.x
+ cellRect
.width
> right
)
10550 right
= cellRect
.x
+ cellRect
.width
;
10551 if (cellRect
.y
+ cellRect
.height
> bottom
)
10552 bottom
= cellRect
.y
+ cellRect
.height
;
10556 i
= rightCol
; // jump over inner cells.
10561 // convert to scrolled coords
10563 CalcScrolledPosition( left
, top
, &left
, &top
);
10564 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10567 m_gridWin
->GetClientSize( &cw
, &ch
);
10569 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10570 return wxRect(0,0,0,0);
10572 rect
.SetLeft( wxMax(0, left
) );
10573 rect
.SetTop( wxMax(0, top
) );
10574 rect
.SetRight( wxMin(cw
, right
) );
10575 rect
.SetBottom( wxMin(ch
, bottom
) );
10580 // ----------------------------------------------------------------------------
10581 // grid event classes
10582 // ----------------------------------------------------------------------------
10584 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10586 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10587 int row
, int col
, int x
, int y
, bool sel
,
10588 bool control
, bool shift
, bool alt
, bool meta
)
10589 : wxNotifyEvent( type
, id
)
10596 m_control
= control
;
10601 SetEventObject(obj
);
10605 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10607 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10608 int rowOrCol
, int x
, int y
,
10609 bool control
, bool shift
, bool alt
, bool meta
)
10610 : wxNotifyEvent( type
, id
)
10612 m_rowOrCol
= rowOrCol
;
10615 m_control
= control
;
10620 SetEventObject(obj
);
10624 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10626 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10627 const wxGridCellCoords
& topLeft
,
10628 const wxGridCellCoords
& bottomRight
,
10629 bool sel
, bool control
,
10630 bool shift
, bool alt
, bool meta
)
10631 : wxNotifyEvent( type
, id
)
10633 m_topLeft
= topLeft
;
10634 m_bottomRight
= bottomRight
;
10636 m_control
= control
;
10641 SetEventObject(obj
);
10645 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10647 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10648 wxObject
* obj
, int row
,
10649 int col
, wxControl
* ctrl
)
10650 : wxCommandEvent(type
, id
)
10652 SetEventObject(obj
);
10658 #endif // wxUSE_GRID