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
, wxGrid
* grid
)
698 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
700 bool changed
= false;
701 wxString value
= Text()->GetValue();
702 if (value
!= m_startValue
)
706 grid
->GetTable()->SetValue(row
, col
, value
);
708 m_startValue
= wxEmptyString
;
709 // No point in setting the text of the hidden control
710 //Text()->SetValue(m_startValue);
715 void wxGridCellTextEditor::Reset()
717 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
719 DoReset(m_startValue
);
722 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
724 Text()->SetValue(startValue
);
725 Text()->SetInsertionPointEnd();
728 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
730 return wxGridCellEditor::IsAcceptedKey(event
);
733 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
735 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
736 // longer an appropriate way to get the character into the text control.
737 // Do it ourselves instead. We know that if we get this far that we have
738 // a valid character, so not a whole lot of testing needs to be done.
740 wxTextCtrl
* tc
= Text();
745 ch
= event
.GetUnicodeKey();
747 ch
= (wxChar
)event
.GetKeyCode();
749 ch
= (wxChar
)event
.GetKeyCode();
755 // delete the character at the cursor
756 pos
= tc
->GetInsertionPoint();
757 if (pos
< tc
->GetLastPosition())
758 tc
->Remove(pos
, pos
+ 1);
762 // delete the character before the cursor
763 pos
= tc
->GetInsertionPoint();
765 tc
->Remove(pos
- 1, pos
);
774 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
775 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
777 #if defined(__WXMOTIF__) || defined(__WXGTK__)
778 // wxMotif needs a little extra help...
779 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
780 wxString
s( Text()->GetValue() );
781 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
783 Text()->SetInsertionPoint( pos
);
785 // the other ports can handle a Return key press
791 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
801 if ( !params
.ToLong(&tmp
) )
803 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
807 m_maxChars
= (size_t)tmp
;
812 // return the value in the text control
813 wxString
wxGridCellTextEditor::GetValue() const
815 return Text()->GetValue();
818 // ----------------------------------------------------------------------------
819 // wxGridCellNumberEditor
820 // ----------------------------------------------------------------------------
822 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
828 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
830 wxEvtHandler
* evtHandler
)
835 // create a spin ctrl
836 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
837 wxDefaultPosition
, wxDefaultSize
,
841 wxGridCellEditor::Create(parent
, id
, evtHandler
);
846 // just a text control
847 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
850 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
851 #endif // wxUSE_VALIDATORS
855 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
857 // first get the value
858 wxGridTableBase
*table
= grid
->GetTable();
859 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
861 m_valueOld
= table
->GetValueAsLong(row
, col
);
866 wxString sValue
= table
->GetValue(row
, col
);
867 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
869 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
877 Spin()->SetValue((int)m_valueOld
);
883 DoBeginEdit(GetString());
887 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
897 value
= Spin()->GetValue();
898 changed
= value
!= m_valueOld
;
900 text
= wxString::Format(wxT("%ld"), value
);
905 text
= Text()->GetValue();
906 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
911 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
912 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
914 grid
->GetTable()->SetValue(row
, col
, text
);
920 void wxGridCellNumberEditor::Reset()
925 Spin()->SetValue((int)m_valueOld
);
930 DoReset(GetString());
934 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
936 if ( wxGridCellEditor::IsAcceptedKey(event
) )
938 int keycode
= event
.GetKeyCode();
939 if ( (keycode
< 128) &&
940 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
949 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
951 int keycode
= event
.GetKeyCode();
954 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
956 wxGridCellTextEditor::StartingKey(event
);
965 if ( wxIsdigit(keycode
) )
967 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
968 spin
->SetValue(keycode
- '0');
969 spin
->SetSelection(1,1);
978 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
989 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
993 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
997 // skip the error message below
1002 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1006 // return the value in the spin control if it is there (the text control otherwise)
1007 wxString
wxGridCellNumberEditor::GetValue() const
1014 long value
= Spin()->GetValue();
1015 s
.Printf(wxT("%ld"), value
);
1020 s
= Text()->GetValue();
1026 // ----------------------------------------------------------------------------
1027 // wxGridCellFloatEditor
1028 // ----------------------------------------------------------------------------
1030 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1033 m_precision
= precision
;
1036 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1038 wxEvtHandler
* evtHandler
)
1040 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1042 #if wxUSE_VALIDATORS
1043 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1044 #endif // wxUSE_VALIDATORS
1047 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1049 // first get the value
1050 wxGridTableBase
*table
= grid
->GetTable();
1051 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1053 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1058 wxString sValue
= table
->GetValue(row
, col
);
1059 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1061 wxFAIL_MSG( _T("this cell doesn't have float value") );
1066 DoBeginEdit(GetString());
1069 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1073 wxString
text(Text()->GetValue());
1075 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1076 !wxIsSameDouble(value
, m_valueOld
) )
1078 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1079 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1081 grid
->GetTable()->SetValue(row
, col
, text
);
1089 void wxGridCellFloatEditor::Reset()
1091 DoReset(GetString());
1094 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1096 int keycode
= event
.GetKeyCode();
1098 tmpbuf
[0] = (char) keycode
;
1100 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1103 bool is_decimal_point
= ( strbuf
==
1104 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1106 bool is_decimal_point
= ( strbuf
== _T(".") );
1109 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1110 || is_decimal_point
)
1112 wxGridCellTextEditor::StartingKey(event
);
1114 // skip Skip() below
1121 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1132 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1136 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1138 m_precision
= (int)tmp
;
1140 // skip the error message below
1145 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1149 wxString
wxGridCellFloatEditor::GetString() const
1152 if ( m_precision
== -1 && m_width
!= -1)
1154 // default precision
1155 fmt
.Printf(_T("%%%d.f"), m_width
);
1157 else if ( m_precision
!= -1 && m_width
== -1)
1160 fmt
.Printf(_T("%%.%df"), m_precision
);
1162 else if ( m_precision
!= -1 && m_width
!= -1 )
1164 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1168 // default width/precision
1172 return wxString::Format(fmt
, m_valueOld
);
1175 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1177 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1179 int keycode
= event
.GetKeyCode();
1180 printf("%d\n", keycode
);
1181 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1183 tmpbuf
[0] = (char) keycode
;
1185 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1188 bool is_decimal_point
=
1189 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1190 wxLOCALE_CAT_NUMBER
) );
1192 bool is_decimal_point
= ( strbuf
== _T(".") );
1195 if ( (keycode
< 128) &&
1196 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1197 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1206 #endif // wxUSE_TEXTCTRL
1210 // ----------------------------------------------------------------------------
1211 // wxGridCellBoolEditor
1212 // ----------------------------------------------------------------------------
1214 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1216 wxEvtHandler
* evtHandler
)
1218 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1219 wxDefaultPosition
, wxDefaultSize
,
1222 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1225 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1227 bool resize
= false;
1228 wxSize size
= m_control
->GetSize();
1229 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1231 // check if the checkbox is not too big/small for this cell
1232 wxSize sizeBest
= m_control
->GetBestSize();
1233 if ( !(size
== sizeBest
) )
1235 // reset to default size if it had been made smaller
1241 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1243 // leave 1 pixel margin
1244 size
.x
= size
.y
= minSize
- 2;
1251 m_control
->SetSize(size
);
1254 // position it in the centre of the rectangle (TODO: support alignment?)
1256 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1257 // the checkbox without label still has some space to the right in wxGTK,
1258 // so shift it to the right
1260 #elif defined(__WXMSW__)
1261 // here too, but in other way
1266 int hAlign
= wxALIGN_CENTRE
;
1267 int vAlign
= wxALIGN_CENTRE
;
1269 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1272 if (hAlign
== wxALIGN_LEFT
)
1280 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1282 else if (hAlign
== wxALIGN_RIGHT
)
1284 x
= r
.x
+ r
.width
- size
.x
- 2;
1285 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1287 else if (hAlign
== wxALIGN_CENTRE
)
1289 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1290 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1293 m_control
->Move(x
, y
);
1296 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1298 m_control
->Show(show
);
1302 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1303 CBox()->SetBackgroundColour(colBg
);
1307 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1309 wxASSERT_MSG(m_control
,
1310 wxT("The wxGridCellEditor must be Created first!"));
1312 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1314 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1318 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1319 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1322 CBox()->SetValue(m_startValue
);
1326 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1329 wxASSERT_MSG(m_control
,
1330 wxT("The wxGridCellEditor must be Created first!"));
1332 bool changed
= false;
1333 bool value
= CBox()->GetValue();
1334 if ( value
!= m_startValue
)
1339 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1340 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1342 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1348 void wxGridCellBoolEditor::Reset()
1350 wxASSERT_MSG(m_control
,
1351 wxT("The wxGridCellEditor must be Created first!"));
1353 CBox()->SetValue(m_startValue
);
1356 void wxGridCellBoolEditor::StartingClick()
1358 CBox()->SetValue(!CBox()->GetValue());
1361 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1363 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1365 int keycode
= event
.GetKeyCode();
1378 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1380 int keycode
= event
.GetKeyCode();
1384 CBox()->SetValue(!CBox()->GetValue());
1388 CBox()->SetValue(true);
1392 CBox()->SetValue(false);
1398 // return the value as "1" for true and the empty string for false
1399 wxString
wxGridCellBoolEditor::GetValue() const
1401 bool bSet
= CBox()->GetValue();
1402 return bSet
? _T("1") : wxEmptyString
;
1405 #endif // wxUSE_CHECKBOX
1409 // ----------------------------------------------------------------------------
1410 // wxGridCellChoiceEditor
1411 // ----------------------------------------------------------------------------
1413 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1415 : m_choices(choices
),
1416 m_allowOthers(allowOthers
) { }
1418 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1419 const wxString choices
[],
1421 : m_allowOthers(allowOthers
)
1425 m_choices
.Alloc(count
);
1426 for ( size_t n
= 0; n
< count
; n
++ )
1428 m_choices
.Add(choices
[n
]);
1433 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1435 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1436 editor
->m_allowOthers
= m_allowOthers
;
1437 editor
->m_choices
= m_choices
;
1442 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1444 wxEvtHandler
* evtHandler
)
1446 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1447 wxDefaultPosition
, wxDefaultSize
,
1449 m_allowOthers
? 0 : wxCB_READONLY
);
1451 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1454 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1455 wxGridCellAttr
* attr
)
1457 // as we fill the entire client area, don't do anything here to minimize
1460 // TODO: It doesn't actually fill the client area since the height of a
1461 // combo always defaults to the standard... Until someone has time to
1462 // figure out the right rectangle to paint, just do it the normal way...
1463 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1466 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1468 wxASSERT_MSG(m_control
,
1469 wxT("The wxGridCellEditor must be Created first!"));
1471 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1473 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1475 // Don't immediately end if we get a kill focus event within BeginEdit
1477 evtHandler
->SetInSetFocus(true);
1479 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1483 Combo()->SetValue(m_startValue
);
1487 // find the right position, or default to the first if not found
1488 int pos
= Combo()->FindString(m_startValue
);
1489 if (pos
== wxNOT_FOUND
)
1491 Combo()->SetSelection(pos
);
1494 Combo()->SetInsertionPointEnd();
1495 Combo()->SetFocus();
1499 // When dropping down the menu, a kill focus event
1500 // happens after this point, so we can't reset the flag yet.
1501 #if !defined(__WXGTK20__)
1502 evtHandler
->SetInSetFocus(false);
1507 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1510 wxString value
= Combo()->GetValue();
1511 if ( value
== m_startValue
)
1514 grid
->GetTable()->SetValue(row
, col
, value
);
1519 void wxGridCellChoiceEditor::Reset()
1521 Combo()->SetValue(m_startValue
);
1522 Combo()->SetInsertionPointEnd();
1525 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1535 wxStringTokenizer
tk(params
, _T(','));
1536 while ( tk
.HasMoreTokens() )
1538 m_choices
.Add(tk
.GetNextToken());
1542 // return the value in the text control
1543 wxString
wxGridCellChoiceEditor::GetValue() const
1545 return Combo()->GetValue();
1548 #endif // wxUSE_COMBOBOX
1550 // ----------------------------------------------------------------------------
1551 // wxGridCellEditorEvtHandler
1552 // ----------------------------------------------------------------------------
1554 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1556 // Don't disable the cell if we're just starting to edit it
1561 m_grid
->DisableCellEditControl();
1566 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1568 switch ( event
.GetKeyCode() )
1572 m_grid
->DisableCellEditControl();
1576 m_grid
->GetEventHandler()->ProcessEvent( event
);
1580 case WXK_NUMPAD_ENTER
:
1581 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1582 m_editor
->HandleReturn(event
);
1591 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1593 int row
= m_grid
->GetGridCursorRow();
1594 int col
= m_grid
->GetGridCursorCol();
1595 wxRect rect
= m_grid
->CellToRect( row
, col
);
1597 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1599 // if cell width is smaller than grid client area, cell is wholly visible
1600 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1602 switch ( event
.GetKeyCode() )
1607 case WXK_NUMPAD_ENTER
:
1612 if ( wholeCellVisible
)
1614 // no special processing needed...
1619 // do special processing for partly visible cell...
1621 // get the widths of all cells previous to this one
1623 for ( int i
= 0; i
< col
; i
++ )
1625 colXPos
+= m_grid
->GetColSize(i
);
1628 int xUnit
= 1, yUnit
= 1;
1629 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1632 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1636 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1643 if ( wholeCellVisible
)
1645 // no special processing needed...
1650 // do special processing for partly visible cell...
1653 wxString value
= m_grid
->GetCellValue(row
, col
);
1654 if ( wxEmptyString
!= value
)
1656 // get width of cell CONTENTS (text)
1658 wxFont font
= m_grid
->GetCellFont(row
, col
);
1659 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1661 // try to RIGHT align the text by scrolling
1662 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1664 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1665 // otherwise the last part of the cell content might be hidden below the scroll bar
1666 // FIXME: maybe there is a more suitable correction?
1667 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1668 if ( textWidth
< 0 )
1674 // get the widths of all cells previous to this one
1676 for ( int i
= 0; i
< col
; i
++ )
1678 colXPos
+= m_grid
->GetColSize(i
);
1681 // and add the (modified) text width of the cell contents
1682 // as we'd like to see the last part of the cell contents
1683 colXPos
+= textWidth
;
1685 int xUnit
= 1, yUnit
= 1;
1686 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1687 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1697 // ----------------------------------------------------------------------------
1698 // wxGridCellWorker is an (almost) empty common base class for
1699 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1700 // ----------------------------------------------------------------------------
1702 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1707 wxGridCellWorker::~wxGridCellWorker()
1711 // ============================================================================
1713 // ============================================================================
1715 // ----------------------------------------------------------------------------
1716 // wxGridCellRenderer
1717 // ----------------------------------------------------------------------------
1719 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1720 wxGridCellAttr
& attr
,
1723 int WXUNUSED(row
), int WXUNUSED(col
),
1726 dc
.SetBackgroundMode( wxSOLID
);
1728 // grey out fields if the grid is disabled
1729 if ( grid
.IsEnabled() )
1733 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1737 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1742 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1745 dc
.SetPen( *wxTRANSPARENT_PEN
);
1746 dc
.DrawRectangle(rect
);
1749 // ----------------------------------------------------------------------------
1750 // wxGridCellStringRenderer
1751 // ----------------------------------------------------------------------------
1753 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1754 const wxGridCellAttr
& attr
,
1758 dc
.SetBackgroundMode( wxTRANSPARENT
);
1760 // TODO some special colours for attr.IsReadOnly() case?
1762 // different coloured text when the grid is disabled
1763 if ( grid
.IsEnabled() )
1767 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1768 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1772 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1773 dc
.SetTextForeground( attr
.GetTextColour() );
1778 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1779 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1782 dc
.SetFont( attr
.GetFont() );
1785 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1787 const wxString
& text
)
1789 wxCoord x
= 0, y
= 0, max_x
= 0;
1790 dc
.SetFont(attr
.GetFont());
1791 wxStringTokenizer
tk(text
, _T('\n'));
1792 while ( tk
.HasMoreTokens() )
1794 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1795 max_x
= wxMax(max_x
, x
);
1798 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1800 return wxSize(max_x
, y
);
1803 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1804 wxGridCellAttr
& attr
,
1808 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1811 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1812 wxGridCellAttr
& attr
,
1814 const wxRect
& rectCell
,
1818 wxRect rect
= rectCell
;
1821 // erase only this cells background, overflow cells should have been erased
1822 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1825 attr
.GetAlignment(&hAlign
, &vAlign
);
1827 int overflowCols
= 0;
1829 if (attr
.GetOverflow())
1831 int cols
= grid
.GetNumberCols();
1832 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1833 int cell_rows
, cell_cols
;
1834 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1835 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1837 int i
, c_cols
, c_rows
;
1838 for (i
= col
+cell_cols
; i
< cols
; i
++)
1840 bool is_empty
= true;
1841 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1843 // check w/ anchor cell for multicell block
1844 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1847 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1855 rect
.width
+= grid
.GetColSize(i
);
1862 if (rect
.width
>= best_width
)
1866 overflowCols
= i
- col
- cell_cols
+ 1;
1867 if (overflowCols
>= cols
)
1868 overflowCols
= cols
- 1;
1871 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1873 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1875 clip
.x
+= rectCell
.width
;
1876 // draw each overflow cell individually
1877 int col_end
= col
+cell_cols
+ overflowCols
;
1878 if (col_end
>= grid
.GetNumberCols())
1879 col_end
= grid
.GetNumberCols() - 1;
1880 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1882 clip
.width
= grid
.GetColSize(i
) - 1;
1883 dc
.DestroyClippingRegion();
1884 dc
.SetClippingRegion(clip
);
1886 SetTextColoursAndFont(grid
, attr
, dc
,
1887 grid
.IsInSelection(row
,i
));
1889 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1890 rect
, hAlign
, vAlign
);
1891 clip
.x
+= grid
.GetColSize(i
) - 1;
1897 dc
.DestroyClippingRegion();
1901 // now we only have to draw the text
1902 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1904 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1905 rect
, hAlign
, vAlign
);
1908 // ----------------------------------------------------------------------------
1909 // wxGridCellNumberRenderer
1910 // ----------------------------------------------------------------------------
1912 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1914 wxGridTableBase
*table
= grid
.GetTable();
1916 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1918 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1922 text
= table
->GetValue(row
, col
);
1928 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1929 wxGridCellAttr
& attr
,
1931 const wxRect
& rectCell
,
1935 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1937 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1939 // draw the text right aligned by default
1941 attr
.GetAlignment(&hAlign
, &vAlign
);
1942 hAlign
= wxALIGN_RIGHT
;
1944 wxRect rect
= rectCell
;
1947 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1950 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1951 wxGridCellAttr
& attr
,
1955 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1958 // ----------------------------------------------------------------------------
1959 // wxGridCellFloatRenderer
1960 // ----------------------------------------------------------------------------
1962 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1965 SetPrecision(precision
);
1968 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1970 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1971 renderer
->m_width
= m_width
;
1972 renderer
->m_precision
= m_precision
;
1973 renderer
->m_format
= m_format
;
1978 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1980 wxGridTableBase
*table
= grid
.GetTable();
1985 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1987 val
= table
->GetValueAsDouble(row
, col
);
1992 text
= table
->GetValue(row
, col
);
1993 hasDouble
= text
.ToDouble(&val
);
2000 if ( m_width
== -1 )
2002 if ( m_precision
== -1 )
2004 // default width/precision
2005 m_format
= _T("%f");
2009 m_format
.Printf(_T("%%.%df"), m_precision
);
2012 else if ( m_precision
== -1 )
2014 // default precision
2015 m_format
.Printf(_T("%%%d.f"), m_width
);
2019 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2023 text
.Printf(m_format
, val
);
2026 //else: text already contains the string
2031 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2032 wxGridCellAttr
& attr
,
2034 const wxRect
& rectCell
,
2038 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2040 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2042 // draw the text right aligned by default
2044 attr
.GetAlignment(&hAlign
, &vAlign
);
2045 hAlign
= wxALIGN_RIGHT
;
2047 wxRect rect
= rectCell
;
2050 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2053 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2054 wxGridCellAttr
& attr
,
2058 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2061 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2065 // reset to defaults
2071 wxString tmp
= params
.BeforeFirst(_T(','));
2075 if ( tmp
.ToLong(&width
) )
2077 SetWidth((int)width
);
2081 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2085 tmp
= params
.AfterFirst(_T(','));
2089 if ( tmp
.ToLong(&precision
) )
2091 SetPrecision((int)precision
);
2095 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2101 // ----------------------------------------------------------------------------
2102 // wxGridCellBoolRenderer
2103 // ----------------------------------------------------------------------------
2105 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2107 // FIXME these checkbox size calculations are really ugly...
2109 // between checkmark and box
2110 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2112 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2113 wxGridCellAttr
& WXUNUSED(attr
),
2118 // compute it only once (no locks for MT safeness in GUI thread...)
2119 if ( !ms_sizeCheckMark
.x
)
2121 // get checkbox size
2122 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2123 wxSize size
= checkbox
->GetBestSize();
2124 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2126 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2127 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2128 checkSize
-= size
.y
/ 2;
2133 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2136 return ms_sizeCheckMark
;
2139 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2140 wxGridCellAttr
& attr
,
2146 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2148 // draw a check mark in the centre (ignoring alignment - TODO)
2149 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2151 // don't draw outside the cell
2152 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2153 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2155 // and even leave (at least) 1 pixel margin
2156 size
.x
= size
.y
= minSize
- 2;
2159 // draw a border around checkmark
2161 attr
.GetAlignment(& hAlign
, &vAlign
);
2164 if (hAlign
== wxALIGN_CENTRE
)
2166 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2167 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2168 rectBorder
.width
= size
.x
;
2169 rectBorder
.height
= size
.y
;
2171 else if (hAlign
== wxALIGN_LEFT
)
2173 rectBorder
.x
= rect
.x
+ 2;
2174 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2175 rectBorder
.width
= size
.x
;
2176 rectBorder
.height
= size
.y
;
2178 else if (hAlign
== wxALIGN_RIGHT
)
2180 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2181 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2182 rectBorder
.width
= size
.x
;
2183 rectBorder
.height
= size
.y
;
2187 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2188 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2191 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2192 value
= !( !cellval
|| (cellval
== wxT("0")) );
2197 wxRect rectMark
= rectBorder
;
2200 // MSW DrawCheckMark() is weird (and should probably be changed...)
2201 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2205 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2208 dc
.SetTextForeground(attr
.GetTextColour());
2209 dc
.DrawCheckMark(rectMark
);
2212 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2213 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2214 dc
.DrawRectangle(rectBorder
);
2217 // ----------------------------------------------------------------------------
2219 // ----------------------------------------------------------------------------
2221 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2225 m_isReadOnly
= Unset
;
2230 m_attrkind
= wxGridCellAttr::Cell
;
2232 m_sizeRows
= m_sizeCols
= 1;
2233 m_overflow
= UnsetOverflow
;
2235 SetDefAttr(attrDefault
);
2238 wxGridCellAttr
*wxGridCellAttr::Clone() const
2240 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2242 if ( HasTextColour() )
2243 attr
->SetTextColour(GetTextColour());
2244 if ( HasBackgroundColour() )
2245 attr
->SetBackgroundColour(GetBackgroundColour());
2247 attr
->SetFont(GetFont());
2248 if ( HasAlignment() )
2249 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2251 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2255 attr
->SetRenderer(m_renderer
);
2256 m_renderer
->IncRef();
2260 attr
->SetEditor(m_editor
);
2265 attr
->SetReadOnly();
2267 attr
->SetKind( m_attrkind
);
2272 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2274 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2275 SetTextColour(mergefrom
->GetTextColour());
2276 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2277 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2278 if ( !HasFont() && mergefrom
->HasFont() )
2279 SetFont(mergefrom
->GetFont());
2280 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2283 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2284 SetAlignment(hAlign
, vAlign
);
2286 if ( !HasSize() && mergefrom
->HasSize() )
2287 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2289 // Directly access member functions as GetRender/Editor don't just return
2290 // m_renderer/m_editor
2292 // Maybe add support for merge of Render and Editor?
2293 if (!HasRenderer() && mergefrom
->HasRenderer() )
2295 m_renderer
= mergefrom
->m_renderer
;
2296 m_renderer
->IncRef();
2298 if ( !HasEditor() && mergefrom
->HasEditor() )
2300 m_editor
= mergefrom
->m_editor
;
2303 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2304 SetReadOnly(mergefrom
->IsReadOnly());
2306 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2307 SetOverflow(mergefrom
->GetOverflow());
2309 SetDefAttr(mergefrom
->m_defGridAttr
);
2312 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2314 // The size of a cell is normally 1,1
2316 // If this cell is larger (2,2) then this is the top left cell
2317 // the other cells that will be covered (lower right cells) must be
2318 // set to negative or zero values such that
2319 // row + num_rows of the covered cell points to the larger cell (this cell)
2320 // same goes for the col + num_cols.
2322 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2324 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2325 !((num_rows
<= 0) && (num_cols
> 0)) ||
2326 !((num_rows
== 0) && (num_cols
== 0))),
2327 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2329 m_sizeRows
= num_rows
;
2330 m_sizeCols
= num_cols
;
2333 const wxColour
& wxGridCellAttr::GetTextColour() const
2335 if (HasTextColour())
2339 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2341 return m_defGridAttr
->GetTextColour();
2345 wxFAIL_MSG(wxT("Missing default cell attribute"));
2346 return wxNullColour
;
2350 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2352 if (HasBackgroundColour())
2356 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2358 return m_defGridAttr
->GetBackgroundColour();
2362 wxFAIL_MSG(wxT("Missing default cell attribute"));
2363 return wxNullColour
;
2367 const wxFont
& wxGridCellAttr::GetFont() const
2373 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2375 return m_defGridAttr
->GetFont();
2379 wxFAIL_MSG(wxT("Missing default cell attribute"));
2384 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2393 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2394 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2397 wxFAIL_MSG(wxT("Missing default cell attribute"));
2401 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2404 *num_rows
= m_sizeRows
;
2406 *num_cols
= m_sizeCols
;
2409 // GetRenderer and GetEditor use a slightly different decision path about
2410 // which attribute to use. If a non-default attr object has one then it is
2411 // used, otherwise the default editor or renderer is fetched from the grid and
2412 // used. It should be the default for the data type of the cell. If it is
2413 // NULL (because the table has a type that the grid does not have in its
2414 // registry,) then the grid's default editor or renderer is used.
2416 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2418 wxGridCellRenderer
*renderer
;
2420 if ( m_renderer
&& this != m_defGridAttr
)
2422 // use the cells renderer if it has one
2423 renderer
= m_renderer
;
2426 else // no non-default cell renderer
2428 // get default renderer for the data type
2431 // GetDefaultRendererForCell() will do IncRef() for us
2432 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2441 if (m_defGridAttr
&& this != m_defGridAttr
)
2443 // if we still don't have one then use the grid default
2444 // (no need for IncRef() here neither)
2445 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2447 else // default grid attr
2449 // use m_renderer which we had decided not to use initially
2450 renderer
= m_renderer
;
2457 // we're supposed to always find something
2458 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2463 // same as above, except for s/renderer/editor/g
2464 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2466 wxGridCellEditor
*editor
;
2468 if ( m_editor
&& this != m_defGridAttr
)
2470 // use the cells editor if it has one
2474 else // no non default cell editor
2476 // get default editor for the data type
2479 // GetDefaultEditorForCell() will do IncRef() for us
2480 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2489 if ( m_defGridAttr
&& this != m_defGridAttr
)
2491 // if we still don't have one then use the grid default
2492 // (no need for IncRef() here neither)
2493 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2495 else // default grid attr
2497 // use m_editor which we had decided not to use initially
2505 // we're supposed to always find something
2506 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2511 // ----------------------------------------------------------------------------
2512 // wxGridCellAttrData
2513 // ----------------------------------------------------------------------------
2515 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2517 int n
= FindIndex(row
, col
);
2518 if ( n
== wxNOT_FOUND
)
2520 // add the attribute
2521 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2525 // free the old attribute
2526 m_attrs
[(size_t)n
].attr
->DecRef();
2530 // change the attribute
2531 m_attrs
[(size_t)n
].attr
= attr
;
2535 // remove this attribute
2536 m_attrs
.RemoveAt((size_t)n
);
2541 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2543 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2545 int n
= FindIndex(row
, col
);
2546 if ( n
!= wxNOT_FOUND
)
2548 attr
= m_attrs
[(size_t)n
].attr
;
2555 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2557 size_t count
= m_attrs
.GetCount();
2558 for ( size_t n
= 0; n
< count
; n
++ )
2560 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2561 wxCoord row
= coords
.GetRow();
2562 if ((size_t)row
>= pos
)
2566 // If rows inserted, include row counter where necessary
2567 coords
.SetRow(row
+ numRows
);
2569 else if (numRows
< 0)
2571 // If rows deleted ...
2572 if ((size_t)row
>= pos
- numRows
)
2574 // ...either decrement row counter (if row still exists)...
2575 coords
.SetRow(row
+ numRows
);
2579 // ...or remove the attribute
2580 // No need to DecRef the attribute itself since this is
2581 // done be wxGridCellWithAttr's destructor!
2582 m_attrs
.RemoveAt(n
);
2591 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2593 size_t count
= m_attrs
.GetCount();
2594 for ( size_t n
= 0; n
< count
; n
++ )
2596 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2597 wxCoord col
= coords
.GetCol();
2598 if ( (size_t)col
>= pos
)
2602 // If rows inserted, include row counter where necessary
2603 coords
.SetCol(col
+ numCols
);
2605 else if (numCols
< 0)
2607 // If rows deleted ...
2608 if ((size_t)col
>= pos
- numCols
)
2610 // ...either decrement row counter (if row still exists)...
2611 coords
.SetCol(col
+ numCols
);
2615 // ...or remove the attribute
2616 // No need to DecRef the attribute itself since this is
2617 // done be wxGridCellWithAttr's destructor!
2618 m_attrs
.RemoveAt(n
);
2627 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2629 size_t count
= m_attrs
.GetCount();
2630 for ( size_t n
= 0; n
< count
; n
++ )
2632 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2633 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2642 // ----------------------------------------------------------------------------
2643 // wxGridRowOrColAttrData
2644 // ----------------------------------------------------------------------------
2646 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2648 size_t count
= m_attrs
.Count();
2649 for ( size_t n
= 0; n
< count
; n
++ )
2651 m_attrs
[n
]->DecRef();
2655 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2657 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2659 int n
= m_rowsOrCols
.Index(rowOrCol
);
2660 if ( n
!= wxNOT_FOUND
)
2662 attr
= m_attrs
[(size_t)n
];
2669 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2671 int i
= m_rowsOrCols
.Index(rowOrCol
);
2672 if ( i
== wxNOT_FOUND
)
2674 // add the attribute
2675 m_rowsOrCols
.Add(rowOrCol
);
2680 size_t n
= (size_t)i
;
2683 // change the attribute
2684 m_attrs
[n
]->DecRef();
2689 // remove this attribute
2690 m_attrs
[n
]->DecRef();
2691 m_rowsOrCols
.RemoveAt(n
);
2692 m_attrs
.RemoveAt(n
);
2697 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2699 size_t count
= m_attrs
.GetCount();
2700 for ( size_t n
= 0; n
< count
; n
++ )
2702 int & rowOrCol
= m_rowsOrCols
[n
];
2703 if ( (size_t)rowOrCol
>= pos
)
2705 if ( numRowsOrCols
> 0 )
2707 // If rows inserted, include row counter where necessary
2708 rowOrCol
+= numRowsOrCols
;
2710 else if ( numRowsOrCols
< 0)
2712 // If rows deleted, either decrement row counter (if row still exists)
2713 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2714 rowOrCol
+= numRowsOrCols
;
2717 m_rowsOrCols
.RemoveAt(n
);
2718 m_attrs
[n
]->DecRef();
2719 m_attrs
.RemoveAt(n
);
2728 // ----------------------------------------------------------------------------
2729 // wxGridCellAttrProvider
2730 // ----------------------------------------------------------------------------
2732 wxGridCellAttrProvider::wxGridCellAttrProvider()
2734 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2737 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2742 void wxGridCellAttrProvider::InitData()
2744 m_data
= new wxGridCellAttrProviderData
;
2747 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2748 wxGridCellAttr::wxAttrKind kind
) const
2750 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2755 case (wxGridCellAttr::Any
):
2756 // Get cached merge attributes.
2757 // Currently not used as no cache implemented as not mutable
2758 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2761 // Basically implement old version.
2762 // Also check merge cache, so we don't have to re-merge every time..
2763 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2764 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2765 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2767 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2769 // Two or more are non NULL
2770 attr
= new wxGridCellAttr
;
2771 attr
->SetKind(wxGridCellAttr::Merged
);
2773 // Order is important..
2776 attr
->MergeWith(attrcell
);
2781 attr
->MergeWith(attrcol
);
2786 attr
->MergeWith(attrrow
);
2790 // store merge attr if cache implemented
2792 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2796 // one or none is non null return it or null.
2815 case (wxGridCellAttr::Cell
):
2816 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2819 case (wxGridCellAttr::Col
):
2820 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2823 case (wxGridCellAttr::Row
):
2824 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2829 // (wxGridCellAttr::Default):
2830 // (wxGridCellAttr::Merged):
2838 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2844 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2847 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2852 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2855 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2860 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2863 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2867 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2869 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2873 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2877 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2879 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2883 // ----------------------------------------------------------------------------
2884 // wxGridTypeRegistry
2885 // ----------------------------------------------------------------------------
2887 wxGridTypeRegistry::~wxGridTypeRegistry()
2889 size_t count
= m_typeinfo
.Count();
2890 for ( size_t i
= 0; i
< count
; i
++ )
2891 delete m_typeinfo
[i
];
2894 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2895 wxGridCellRenderer
* renderer
,
2896 wxGridCellEditor
* editor
)
2898 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2900 // is it already registered?
2901 int loc
= FindRegisteredDataType(typeName
);
2902 if ( loc
!= wxNOT_FOUND
)
2904 delete m_typeinfo
[loc
];
2905 m_typeinfo
[loc
] = info
;
2909 m_typeinfo
.Add(info
);
2913 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2915 size_t count
= m_typeinfo
.GetCount();
2916 for ( size_t i
= 0; i
< count
; i
++ )
2918 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2927 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2929 int index
= FindRegisteredDataType(typeName
);
2930 if ( index
== wxNOT_FOUND
)
2932 // check whether this is one of the standard ones, in which case
2933 // register it "on the fly"
2935 if ( typeName
== wxGRID_VALUE_STRING
)
2937 RegisterDataType(wxGRID_VALUE_STRING
,
2938 new wxGridCellStringRenderer
,
2939 new wxGridCellTextEditor
);
2942 #endif // wxUSE_TEXTCTRL
2944 if ( typeName
== wxGRID_VALUE_BOOL
)
2946 RegisterDataType(wxGRID_VALUE_BOOL
,
2947 new wxGridCellBoolRenderer
,
2948 new wxGridCellBoolEditor
);
2951 #endif // wxUSE_CHECKBOX
2953 if ( typeName
== wxGRID_VALUE_NUMBER
)
2955 RegisterDataType(wxGRID_VALUE_NUMBER
,
2956 new wxGridCellNumberRenderer
,
2957 new wxGridCellNumberEditor
);
2959 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2961 RegisterDataType(wxGRID_VALUE_FLOAT
,
2962 new wxGridCellFloatRenderer
,
2963 new wxGridCellFloatEditor
);
2966 #endif // wxUSE_TEXTCTRL
2968 if ( typeName
== wxGRID_VALUE_CHOICE
)
2970 RegisterDataType(wxGRID_VALUE_CHOICE
,
2971 new wxGridCellStringRenderer
,
2972 new wxGridCellChoiceEditor
);
2975 #endif // wxUSE_COMBOBOX
2980 // we get here only if just added the entry for this type, so return
2982 index
= m_typeinfo
.GetCount() - 1;
2988 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2990 int index
= FindDataType(typeName
);
2991 if ( index
== wxNOT_FOUND
)
2993 // the first part of the typename is the "real" type, anything after ':'
2994 // are the parameters for the renderer
2995 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2996 if ( index
== wxNOT_FOUND
)
3001 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3002 wxGridCellRenderer
*rendererOld
= renderer
;
3003 renderer
= renderer
->Clone();
3004 rendererOld
->DecRef();
3006 wxGridCellEditor
*editor
= GetEditor(index
);
3007 wxGridCellEditor
*editorOld
= editor
;
3008 editor
= editor
->Clone();
3009 editorOld
->DecRef();
3011 // do it even if there are no parameters to reset them to defaults
3012 wxString params
= typeName
.AfterFirst(_T(':'));
3013 renderer
->SetParameters(params
);
3014 editor
->SetParameters(params
);
3016 // register the new typename
3017 RegisterDataType(typeName
, renderer
, editor
);
3019 // we just registered it, it's the last one
3020 index
= m_typeinfo
.GetCount() - 1;
3026 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3028 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3035 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3037 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3044 // ----------------------------------------------------------------------------
3046 // ----------------------------------------------------------------------------
3048 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3050 wxGridTableBase::wxGridTableBase()
3052 m_view
= (wxGrid
*) NULL
;
3053 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3056 wxGridTableBase::~wxGridTableBase()
3058 delete m_attrProvider
;
3061 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3063 delete m_attrProvider
;
3064 m_attrProvider
= attrProvider
;
3067 bool wxGridTableBase::CanHaveAttributes()
3069 if ( ! GetAttrProvider() )
3071 // use the default attr provider by default
3072 SetAttrProvider(new wxGridCellAttrProvider
);
3078 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3080 if ( m_attrProvider
)
3081 return m_attrProvider
->GetAttr(row
, col
, kind
);
3083 return (wxGridCellAttr
*)NULL
;
3086 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3088 if ( m_attrProvider
)
3090 attr
->SetKind(wxGridCellAttr::Cell
);
3091 m_attrProvider
->SetAttr(attr
, row
, col
);
3095 // as we take ownership of the pointer and don't store it, we must
3101 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3103 if ( m_attrProvider
)
3105 attr
->SetKind(wxGridCellAttr::Row
);
3106 m_attrProvider
->SetRowAttr(attr
, row
);
3110 // as we take ownership of the pointer and don't store it, we must
3116 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3118 if ( m_attrProvider
)
3120 attr
->SetKind(wxGridCellAttr::Col
);
3121 m_attrProvider
->SetColAttr(attr
, col
);
3125 // as we take ownership of the pointer and don't store it, we must
3131 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3132 size_t WXUNUSED(numRows
) )
3134 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3139 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3141 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3146 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3147 size_t WXUNUSED(numRows
) )
3149 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3154 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3155 size_t WXUNUSED(numCols
) )
3157 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3162 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3164 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3169 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3170 size_t WXUNUSED(numCols
) )
3172 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3177 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3181 // RD: Starting the rows at zero confuses users,
3182 // no matter how much it makes sense to us geeks.
3188 wxString
wxGridTableBase::GetColLabelValue( int col
)
3190 // default col labels are:
3191 // cols 0 to 25 : A-Z
3192 // cols 26 to 675 : AA-ZZ
3197 for ( n
= 1; ; n
++ )
3199 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3205 // reverse the string...
3207 for ( i
= 0; i
< n
; i
++ )
3215 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3217 return wxGRID_VALUE_STRING
;
3220 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3221 const wxString
& typeName
)
3223 return typeName
== wxGRID_VALUE_STRING
;
3226 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3228 return CanGetValueAs(row
, col
, typeName
);
3231 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3236 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3241 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3246 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3247 long WXUNUSED(value
) )
3251 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3252 double WXUNUSED(value
) )
3256 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3257 bool WXUNUSED(value
) )
3261 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3262 const wxString
& WXUNUSED(typeName
) )
3267 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3268 const wxString
& WXUNUSED(typeName
),
3269 void* WXUNUSED(value
) )
3273 //////////////////////////////////////////////////////////////////////
3275 // Message class for the grid table to send requests and notifications
3279 wxGridTableMessage::wxGridTableMessage()
3281 m_table
= (wxGridTableBase
*) NULL
;
3287 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3288 int commandInt1
, int commandInt2
)
3292 m_comInt1
= commandInt1
;
3293 m_comInt2
= commandInt2
;
3296 //////////////////////////////////////////////////////////////////////
3298 // A basic grid table for string data. An object of this class will
3299 // created by wxGrid if you don't specify an alternative table class.
3302 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3304 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3306 wxGridStringTable::wxGridStringTable()
3311 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3314 m_data
.Alloc( numRows
);
3317 sa
.Alloc( numCols
);
3318 sa
.Add( wxEmptyString
, numCols
);
3320 m_data
.Add( sa
, numRows
);
3323 wxGridStringTable::~wxGridStringTable()
3327 int wxGridStringTable::GetNumberRows()
3329 return m_data
.GetCount();
3332 int wxGridStringTable::GetNumberCols()
3334 if ( m_data
.GetCount() > 0 )
3335 return m_data
[0].GetCount();
3340 wxString
wxGridStringTable::GetValue( int row
, int col
)
3342 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3344 _T("invalid row or column index in wxGridStringTable") );
3346 return m_data
[row
][col
];
3349 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3351 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3352 _T("invalid row or column index in wxGridStringTable") );
3354 m_data
[row
][col
] = value
;
3357 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3359 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3361 _T("invalid row or column index in wxGridStringTable") );
3363 return (m_data
[row
][col
] == wxEmptyString
);
3366 void wxGridStringTable::Clear()
3369 int numRows
, numCols
;
3371 numRows
= m_data
.GetCount();
3374 numCols
= m_data
[0].GetCount();
3376 for ( row
= 0; row
< numRows
; row
++ )
3378 for ( col
= 0; col
< numCols
; col
++ )
3380 m_data
[row
][col
] = wxEmptyString
;
3386 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3388 size_t curNumRows
= m_data
.GetCount();
3389 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3390 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3392 if ( pos
>= curNumRows
)
3394 return AppendRows( numRows
);
3398 sa
.Alloc( curNumCols
);
3399 sa
.Add( wxEmptyString
, curNumCols
);
3400 m_data
.Insert( sa
, pos
, numRows
);
3404 wxGridTableMessage
msg( this,
3405 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3409 GetView()->ProcessTableMessage( msg
);
3415 bool wxGridStringTable::AppendRows( size_t numRows
)
3417 size_t curNumRows
= m_data
.GetCount();
3418 size_t curNumCols
= ( curNumRows
> 0
3419 ? m_data
[0].GetCount()
3420 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3423 if ( curNumCols
> 0 )
3425 sa
.Alloc( curNumCols
);
3426 sa
.Add( wxEmptyString
, curNumCols
);
3429 m_data
.Add( sa
, numRows
);
3433 wxGridTableMessage
msg( this,
3434 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3437 GetView()->ProcessTableMessage( msg
);
3443 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3445 size_t curNumRows
= m_data
.GetCount();
3447 if ( pos
>= curNumRows
)
3449 wxFAIL_MSG( wxString::Format
3451 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3453 (unsigned long)numRows
,
3454 (unsigned long)curNumRows
3460 if ( numRows
> curNumRows
- pos
)
3462 numRows
= curNumRows
- pos
;
3465 if ( numRows
>= curNumRows
)
3471 m_data
.RemoveAt( pos
, numRows
);
3476 wxGridTableMessage
msg( this,
3477 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3481 GetView()->ProcessTableMessage( msg
);
3487 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3491 size_t curNumRows
= m_data
.GetCount();
3492 size_t curNumCols
= ( curNumRows
> 0
3493 ? m_data
[0].GetCount()
3494 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3496 if ( pos
>= curNumCols
)
3498 return AppendCols( numCols
);
3501 for ( row
= 0; row
< curNumRows
; row
++ )
3503 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3505 m_data
[row
].Insert( wxEmptyString
, col
);
3511 wxGridTableMessage
msg( this,
3512 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3516 GetView()->ProcessTableMessage( msg
);
3522 bool wxGridStringTable::AppendCols( size_t numCols
)
3526 size_t curNumRows
= m_data
.GetCount();
3531 // TODO: something better than this ?
3533 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3538 for ( row
= 0; row
< curNumRows
; row
++ )
3540 m_data
[row
].Add( wxEmptyString
, numCols
);
3545 wxGridTableMessage
msg( this,
3546 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3549 GetView()->ProcessTableMessage( msg
);
3555 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3559 size_t curNumRows
= m_data
.GetCount();
3560 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3561 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3563 if ( pos
>= curNumCols
)
3565 wxFAIL_MSG( wxString::Format
3567 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3569 (unsigned long)numCols
,
3570 (unsigned long)curNumCols
3575 if ( numCols
> curNumCols
- pos
)
3577 numCols
= curNumCols
- pos
;
3580 for ( row
= 0; row
< curNumRows
; row
++ )
3582 if ( numCols
>= curNumCols
)
3584 m_data
[row
].Clear();
3588 m_data
[row
].RemoveAt( pos
, numCols
);
3594 wxGridTableMessage
msg( this,
3595 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3599 GetView()->ProcessTableMessage( msg
);
3605 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3607 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3609 // using default label
3611 return wxGridTableBase::GetRowLabelValue( row
);
3615 return m_rowLabels
[row
];
3619 wxString
wxGridStringTable::GetColLabelValue( int col
)
3621 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3623 // using default label
3625 return wxGridTableBase::GetColLabelValue( col
);
3629 return m_colLabels
[col
];
3633 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3635 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3637 int n
= m_rowLabels
.GetCount();
3640 for ( i
= n
; i
<= row
; i
++ )
3642 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3646 m_rowLabels
[row
] = value
;
3649 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3651 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3653 int n
= m_colLabels
.GetCount();
3656 for ( i
= n
; i
<= col
; i
++ )
3658 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3662 m_colLabels
[col
] = value
;
3666 //////////////////////////////////////////////////////////////////////
3667 //////////////////////////////////////////////////////////////////////
3669 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3671 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3672 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3673 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3674 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3675 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3676 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3677 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3680 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3682 const wxPoint
&pos
, const wxSize
&size
)
3683 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3688 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3692 // NO - don't do this because it will set both the x and y origin
3693 // coords to match the parent scrolled window and we just want to
3694 // set the y coord - MB
3696 // m_owner->PrepareDC( dc );
3699 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3700 dc
.SetDeviceOrigin( 0, -y
);
3702 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3703 m_owner
->DrawRowLabels( dc
, rows
);
3706 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3708 m_owner
->ProcessRowLabelMouseEvent( event
);
3711 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3713 m_owner
->GetEventHandler()->ProcessEvent(event
);
3716 // This seems to be required for wxMotif otherwise the mouse
3717 // cursor must be in the cell edit control to get key events
3719 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3721 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3725 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3727 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3731 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3733 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3737 //////////////////////////////////////////////////////////////////////
3739 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3741 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3742 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3743 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3744 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3745 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3746 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3747 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3750 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3752 const wxPoint
&pos
, const wxSize
&size
)
3753 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3758 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3762 // NO - don't do this because it will set both the x and y origin
3763 // coords to match the parent scrolled window and we just want to
3764 // set the x coord - MB
3766 // m_owner->PrepareDC( dc );
3769 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3770 dc
.SetDeviceOrigin( -x
, 0 );
3772 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3773 m_owner
->DrawColLabels( dc
, cols
);
3776 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3778 m_owner
->ProcessColLabelMouseEvent( event
);
3781 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3783 m_owner
->GetEventHandler()->ProcessEvent(event
);
3786 // This seems to be required for wxMotif otherwise the mouse
3787 // cursor must be in the cell edit control to get key events
3789 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3791 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3795 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3797 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3801 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3803 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3807 //////////////////////////////////////////////////////////////////////
3809 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3811 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3812 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3813 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3814 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3815 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3816 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3817 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3820 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3822 const wxPoint
&pos
, const wxSize
&size
)
3823 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3828 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3832 int client_height
= 0;
3833 int client_width
= 0;
3834 GetClientSize( &client_width
, &client_height
);
3836 // VZ: any reason for this ifdef? (FIXME)
3841 rect
.SetWidth( client_width
- 2 );
3842 rect
.SetHeight( client_height
- 2 );
3844 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3846 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
), 1, wxSOLID
) );
3847 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3848 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3849 dc
.DrawLine( 0, 0, client_width
, 0 );
3850 dc
.DrawLine( 0, 0, 0, client_height
);
3852 dc
.SetPen( *wxWHITE_PEN
);
3853 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3854 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3858 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3860 m_owner
->ProcessCornerLabelMouseEvent( event
);
3863 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3865 m_owner
->GetEventHandler()->ProcessEvent(event
);
3868 // This seems to be required for wxMotif otherwise the mouse
3869 // cursor must be in the cell edit control to get key events
3871 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3873 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3877 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3879 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3883 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3885 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3889 //////////////////////////////////////////////////////////////////////
3891 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3893 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3894 EVT_PAINT( wxGridWindow::OnPaint
)
3895 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3896 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3897 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3898 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3899 EVT_CHAR ( wxGridWindow::OnChar
)
3900 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3901 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3902 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3905 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3906 wxGridRowLabelWindow
*rowLblWin
,
3907 wxGridColLabelWindow
*colLblWin
,
3910 const wxSize
&size
)
3912 parent
, id
, pos
, size
,
3913 wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
| wxFULL_REPAINT_ON_RESIZE
,
3914 wxT("grid window") )
3917 m_rowLabelWin
= rowLblWin
;
3918 m_colLabelWin
= colLblWin
;
3921 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3923 wxPaintDC
dc( this );
3924 m_owner
->PrepareDC( dc
);
3925 wxRegion reg
= GetUpdateRegion();
3926 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
3927 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
3929 #if WXGRID_DRAW_LINES
3930 m_owner
->DrawAllGridLines( dc
, reg
);
3933 m_owner
->DrawGridSpace( dc
);
3934 m_owner
->DrawHighlight( dc
, dirtyCells
);
3937 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3939 wxWindow::ScrollWindow( dx
, dy
, rect
);
3940 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3941 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3944 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3946 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3949 m_owner
->ProcessGridCellMouseEvent( event
);
3952 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3954 m_owner
->GetEventHandler()->ProcessEvent(event
);
3957 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3958 // cursor must be in the cell edit control to get key events
3960 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3962 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3966 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3968 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3972 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3974 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3978 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3982 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3984 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3988 //////////////////////////////////////////////////////////////////////
3990 // Internal Helper function for computing row or column from some
3991 // (unscrolled) coordinate value, using either
3992 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3993 // of m_rowBottoms/m_ColRights to speed up the search!
3995 // Internal helper macros for simpler use of that function
3997 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3998 const wxArrayInt
& BorderArray
, int nMax
,
4001 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
4002 m_minAcceptableColWidth, \
4003 m_colRights, m_numCols, true)
4004 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4005 m_minAcceptableRowHeight, \
4006 m_rowBottoms, m_numRows, true)
4008 /////////////////////////////////////////////////////////////////////
4010 #if wxUSE_EXTENDED_RTTI
4011 WX_DEFINE_FLAGS( wxGridStyle
)
4013 wxBEGIN_FLAGS( wxGridStyle
)
4014 // new style border flags, we put them first to
4015 // use them for streaming out
4016 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4017 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4018 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4019 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4020 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4021 wxFLAGS_MEMBER(wxBORDER_NONE
)
4023 // old style border flags
4024 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4025 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4026 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4027 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4028 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4029 wxFLAGS_MEMBER(wxBORDER
)
4031 // standard window styles
4032 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4033 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4034 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4035 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4036 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4037 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4038 wxFLAGS_MEMBER(wxVSCROLL
)
4039 wxFLAGS_MEMBER(wxHSCROLL
)
4041 wxEND_FLAGS( wxGridStyle
)
4043 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4045 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4046 wxHIDE_PROPERTY( Children
)
4047 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4048 wxEND_PROPERTIES_TABLE()
4050 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4051 wxEND_HANDLERS_TABLE()
4053 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4056 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4059 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4062 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4063 EVT_PAINT( wxGrid::OnPaint
)
4064 EVT_SIZE( wxGrid::OnSize
)
4065 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4066 EVT_KEY_UP( wxGrid::OnKeyUp
)
4067 EVT_CHAR ( wxGrid::OnChar
)
4068 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4073 // in order to make sure that a size event is not
4074 // trigerred in a unfinished state
4075 m_cornerLabelWin
= NULL
;
4076 m_rowLabelWin
= NULL
;
4077 m_colLabelWin
= NULL
;
4081 wxGrid::wxGrid( wxWindow
*parent
,
4086 const wxString
& name
)
4087 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4088 m_colMinWidths(GRID_HASH_SIZE
),
4089 m_rowMinHeights(GRID_HASH_SIZE
)
4092 SetBestFittingSize(size
);
4095 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4096 const wxPoint
& pos
, const wxSize
& size
,
4097 long style
, const wxString
& name
)
4099 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4100 style
| wxWANTS_CHARS
, name
))
4103 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4104 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4107 SetBestFittingSize(size
);
4114 // Must do this or ~wxScrollHelper will pop the wrong event handler
4115 SetTargetWindow(this);
4117 wxSafeDecRef(m_defaultCellAttr
);
4119 #ifdef DEBUG_ATTR_CACHE
4120 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4121 wxPrintf(_T("wxGrid attribute cache statistics: "
4122 "total: %u, hits: %u (%u%%)\n"),
4123 total
, gs_nAttrCacheHits
,
4124 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4130 delete m_typeRegistry
;
4135 // ----- internal init and update functions
4138 // NOTE: If using the default visual attributes works everywhere then this can
4139 // be removed as well as the #else cases below.
4140 #define _USE_VISATTR 0
4143 #include "wx/listbox.h"
4146 void wxGrid::Create()
4148 m_created
= false; // set to true by CreateGrid
4150 m_table
= (wxGridTableBase
*) NULL
;
4153 m_cellEditCtrlEnabled
= false;
4155 m_defaultCellAttr
= new wxGridCellAttr();
4157 // Set default cell attributes
4158 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4159 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4160 m_defaultCellAttr
->SetFont(GetFont());
4161 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4162 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4163 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4166 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4167 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4169 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4170 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4173 m_defaultCellAttr
->SetTextColour(
4174 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4175 m_defaultCellAttr
->SetBackgroundColour(
4176 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4181 m_currentCellCoords
= wxGridNoCellCoords
;
4183 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4184 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4186 // create the type registry
4187 m_typeRegistry
= new wxGridTypeRegistry
;
4190 // subwindow components that make up the wxGrid
4191 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4196 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4201 m_colLabelWin
= new wxGridColLabelWindow( this,
4206 m_gridWin
= new wxGridWindow( this,
4213 SetTargetWindow( m_gridWin
);
4216 wxColour gfg
= gva
.colFg
;
4217 wxColour gbg
= gva
.colBg
;
4218 wxColour lfg
= lva
.colFg
;
4219 wxColour lbg
= lva
.colBg
;
4221 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4222 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4223 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4224 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4227 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4228 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4229 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4230 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4231 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4232 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4234 m_gridWin
->SetOwnForegroundColour(gfg
);
4235 m_gridWin
->SetOwnBackgroundColour(gbg
);
4240 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4241 wxGrid::wxGridSelectionModes selmode
)
4243 wxCHECK_MSG( !m_created
,
4245 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4247 m_numRows
= numRows
;
4248 m_numCols
= numCols
;
4250 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4251 m_table
->SetView( this );
4253 m_selection
= new wxGridSelection( this, selmode
);
4262 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4264 wxCHECK_RET( m_created
,
4265 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4267 m_selection
->SetSelectionMode( selmode
);
4270 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4272 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4273 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4275 return m_selection
->GetSelectionMode();
4278 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4279 wxGrid::wxGridSelectionModes selmode
)
4283 // stop all processing
4288 wxGridTableBase
*t
=m_table
;
4303 m_numRows
= table
->GetNumberRows();
4304 m_numCols
= table
->GetNumberCols();
4307 m_table
->SetView( this );
4308 m_ownTable
= takeOwnership
;
4309 m_selection
= new wxGridSelection( this, selmode
);
4321 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4322 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4324 if ( m_rowLabelWin
)
4326 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4330 m_labelBackgroundColour
= wxColour( wxT("WHITE") );
4333 m_labelTextColour
= wxColour( wxT("BLACK") );
4336 m_attrCache
.row
= -1;
4337 m_attrCache
.col
= -1;
4338 m_attrCache
.attr
= NULL
;
4340 // TODO: something better than this ?
4342 m_labelFont
= this->GetFont();
4343 m_labelFont
.SetWeight( wxBOLD
);
4345 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4346 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4348 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4349 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4350 m_colLabelTextOrientation
= wxHORIZONTAL
;
4352 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4353 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4355 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4356 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4358 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4359 m_defaultRowHeight
+= 8;
4361 m_defaultRowHeight
+= 4;
4364 m_gridLineColour
= wxColour( 192,192,192 );
4365 m_gridLinesEnabled
= true;
4366 m_cellHighlightColour
= *wxBLACK
;
4367 m_cellHighlightPenWidth
= 2;
4368 m_cellHighlightROPenWidth
= 1;
4370 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4371 m_winCapture
= (wxWindow
*)NULL
;
4372 m_canDragRowSize
= true;
4373 m_canDragColSize
= true;
4374 m_canDragGridSize
= true;
4375 m_canDragCell
= false;
4377 m_dragRowOrCol
= -1;
4378 m_isDragging
= false;
4379 m_startDragPos
= wxDefaultPosition
;
4381 m_waitForSlowClick
= false;
4383 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4384 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4386 m_currentCellCoords
= wxGridNoCellCoords
;
4388 m_selectingTopLeft
= wxGridNoCellCoords
;
4389 m_selectingBottomRight
= wxGridNoCellCoords
;
4390 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4391 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4393 m_editable
= true; // default for whole grid
4395 m_inOnKeyDown
= false;
4401 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4402 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4405 // ----------------------------------------------------------------------------
4406 // the idea is to call these functions only when necessary because they create
4407 // quite big arrays which eat memory mostly unnecessary - in particular, if
4408 // default widths/heights are used for all rows/columns, we may not use these
4411 // with some extra code, it should be possible to only store the
4412 // widths/heights different from default ones but this will be done later...
4413 // ----------------------------------------------------------------------------
4415 void wxGrid::InitRowHeights()
4417 m_rowHeights
.Empty();
4418 m_rowBottoms
.Empty();
4420 m_rowHeights
.Alloc( m_numRows
);
4421 m_rowBottoms
.Alloc( m_numRows
);
4425 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4427 for ( int i
= 0; i
< m_numRows
; i
++ )
4429 rowBottom
+= m_defaultRowHeight
;
4430 m_rowBottoms
.Add( rowBottom
);
4434 void wxGrid::InitColWidths()
4436 m_colWidths
.Empty();
4437 m_colRights
.Empty();
4439 m_colWidths
.Alloc( m_numCols
);
4440 m_colRights
.Alloc( m_numCols
);
4443 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4445 for ( int i
= 0; i
< m_numCols
; i
++ )
4447 colRight
+= m_defaultColWidth
;
4448 m_colRights
.Add( colRight
);
4452 int wxGrid::GetColWidth(int col
) const
4454 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4457 int wxGrid::GetColLeft(int col
) const
4459 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4460 : m_colRights
[col
] - m_colWidths
[col
];
4463 int wxGrid::GetColRight(int col
) const
4465 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4469 int wxGrid::GetRowHeight(int row
) const
4471 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4474 int wxGrid::GetRowTop(int row
) const
4476 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4477 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4480 int wxGrid::GetRowBottom(int row
) const
4482 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4483 : m_rowBottoms
[row
];
4486 void wxGrid::CalcDimensions()
4489 GetClientSize( &cw
, &ch
);
4491 if ( m_rowLabelWin
->IsShown() )
4492 cw
-= m_rowLabelWidth
;
4493 if ( m_colLabelWin
->IsShown() )
4494 ch
-= m_colLabelHeight
;
4497 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4498 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4500 // take into account editor if shown
4501 if ( IsCellEditControlShown() )
4504 int r
= m_currentCellCoords
.GetRow();
4505 int c
= m_currentCellCoords
.GetCol();
4506 int x
= GetColLeft(c
);
4507 int y
= GetRowTop(r
);
4509 // how big is the editor
4510 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4511 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4512 editor
->GetControl()->GetSize(&w2
, &h2
);
4523 // preserve (more or less) the previous position
4525 GetViewStart( &x
, &y
);
4527 // ensure the position is valid for the new scroll ranges
4529 x
= wxMax( w
- 1, 0 );
4531 y
= wxMax( h
- 1, 0 );
4533 // do set scrollbar parameters
4534 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4535 GetScrollX(w
), GetScrollY(h
), x
, y
,
4536 GetBatchCount() != 0);
4538 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4539 // still must reposition the children
4544 void wxGrid::CalcWindowSizes()
4546 // escape if the window is has not been fully created yet
4548 if ( m_cornerLabelWin
== NULL
)
4552 GetClientSize( &cw
, &ch
);
4554 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4555 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4557 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4558 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4560 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4561 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4563 if ( m_gridWin
&& m_gridWin
->IsShown() )
4564 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4568 // this is called when the grid table sends a message to say that it
4569 // has been redimensioned
4571 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4574 bool result
= false;
4576 // Clear the attribute cache as the attribute might refer to a different
4577 // cell than stored in the cache after adding/removing rows/columns.
4580 // By the same reasoning, the editor should be dismissed if columns are
4581 // added or removed. And for consistency, it should IMHO always be
4582 // removed, not only if the cell "underneath" it actually changes.
4583 // For now, I intentionally do not save the editor's content as the
4584 // cell it might want to save that stuff to might no longer exist.
4585 HideCellEditControl();
4588 // if we were using the default widths/heights so far, we must change them
4590 if ( m_colWidths
.IsEmpty() )
4595 if ( m_rowHeights
.IsEmpty() )
4601 switch ( msg
.GetId() )
4603 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4605 size_t pos
= msg
.GetCommandInt();
4606 int numRows
= msg
.GetCommandInt2();
4608 m_numRows
+= numRows
;
4610 if ( !m_rowHeights
.IsEmpty() )
4612 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4613 m_rowBottoms
.Insert( 0, pos
, numRows
);
4617 bottom
= m_rowBottoms
[pos
- 1];
4619 for ( i
= pos
; i
< m_numRows
; i
++ )
4621 bottom
+= m_rowHeights
[i
];
4622 m_rowBottoms
[i
] = bottom
;
4626 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4628 // if we have just inserted cols into an empty grid the current
4629 // cell will be undefined...
4631 SetCurrentCell( 0, 0 );
4635 m_selection
->UpdateRows( pos
, numRows
);
4636 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4638 attrProvider
->UpdateAttrRows( pos
, numRows
);
4640 if ( !GetBatchCount() )
4643 m_rowLabelWin
->Refresh();
4649 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4651 int numRows
= msg
.GetCommandInt();
4652 int oldNumRows
= m_numRows
;
4653 m_numRows
+= numRows
;
4655 if ( !m_rowHeights
.IsEmpty() )
4657 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4658 m_rowBottoms
.Add( 0, numRows
);
4661 if ( oldNumRows
> 0 )
4662 bottom
= m_rowBottoms
[oldNumRows
- 1];
4664 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4666 bottom
+= m_rowHeights
[i
];
4667 m_rowBottoms
[i
] = bottom
;
4671 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4673 // if we have just inserted cols into an empty grid the current
4674 // cell will be undefined...
4676 SetCurrentCell( 0, 0 );
4679 if ( !GetBatchCount() )
4682 m_rowLabelWin
->Refresh();
4688 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4690 size_t pos
= msg
.GetCommandInt();
4691 int numRows
= msg
.GetCommandInt2();
4692 m_numRows
-= numRows
;
4694 if ( !m_rowHeights
.IsEmpty() )
4696 m_rowHeights
.RemoveAt( pos
, numRows
);
4697 m_rowBottoms
.RemoveAt( pos
, numRows
);
4700 for ( i
= 0; i
< m_numRows
; i
++ )
4702 h
+= m_rowHeights
[i
];
4703 m_rowBottoms
[i
] = h
;
4708 m_currentCellCoords
= wxGridNoCellCoords
;
4712 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4713 m_currentCellCoords
.Set( 0, 0 );
4717 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4718 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4721 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4722 // ifdef'd out following patch from Paul Gammans
4724 // No need to touch column attributes, unless we
4725 // removed _all_ rows, in this case, we remove
4726 // all column attributes.
4727 // I hate to do this here, but the
4728 // needed data is not available inside UpdateAttrRows.
4729 if ( !GetNumberRows() )
4730 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4734 if ( !GetBatchCount() )
4737 m_rowLabelWin
->Refresh();
4743 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4745 size_t pos
= msg
.GetCommandInt();
4746 int numCols
= msg
.GetCommandInt2();
4747 m_numCols
+= numCols
;
4749 if ( !m_colWidths
.IsEmpty() )
4751 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4752 m_colRights
.Insert( 0, pos
, numCols
);
4756 right
= m_colRights
[pos
- 1];
4758 for ( i
= pos
; i
< m_numCols
; i
++ )
4760 right
+= m_colWidths
[i
];
4761 m_colRights
[i
] = right
;
4765 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4767 // if we have just inserted cols into an empty grid the current
4768 // cell will be undefined...
4770 SetCurrentCell( 0, 0 );
4774 m_selection
->UpdateCols( pos
, numCols
);
4775 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4777 attrProvider
->UpdateAttrCols( pos
, numCols
);
4778 if ( !GetBatchCount() )
4781 m_colLabelWin
->Refresh();
4788 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4790 int numCols
= msg
.GetCommandInt();
4791 int oldNumCols
= m_numCols
;
4792 m_numCols
+= numCols
;
4793 if ( !m_colWidths
.IsEmpty() )
4795 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4796 m_colRights
.Add( 0, numCols
);
4799 if ( oldNumCols
> 0 )
4800 right
= m_colRights
[oldNumCols
- 1];
4802 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4804 right
+= m_colWidths
[i
];
4805 m_colRights
[i
] = right
;
4809 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4811 // if we have just inserted cols into an empty grid the current
4812 // cell will be undefined...
4814 SetCurrentCell( 0, 0 );
4816 if ( !GetBatchCount() )
4819 m_colLabelWin
->Refresh();
4825 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4827 size_t pos
= msg
.GetCommandInt();
4828 int numCols
= msg
.GetCommandInt2();
4829 m_numCols
-= numCols
;
4831 if ( !m_colWidths
.IsEmpty() )
4833 m_colWidths
.RemoveAt( pos
, numCols
);
4834 m_colRights
.RemoveAt( pos
, numCols
);
4837 for ( i
= 0; i
< m_numCols
; i
++ )
4839 w
+= m_colWidths
[i
];
4846 m_currentCellCoords
= wxGridNoCellCoords
;
4850 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4851 m_currentCellCoords
.Set( 0, 0 );
4855 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4856 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4859 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4861 // ifdef'd out following patch from Paul Gammans
4863 // No need to touch row attributes, unless we
4864 // removed _all_ columns, in this case, we remove
4865 // all row attributes.
4866 // I hate to do this here, but the
4867 // needed data is not available inside UpdateAttrCols.
4868 if ( !GetNumberCols() )
4869 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4873 if ( !GetBatchCount() )
4876 m_colLabelWin
->Refresh();
4883 if (result
&& !GetBatchCount() )
4884 m_gridWin
->Refresh();
4889 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4891 wxRegionIterator
iter( reg
);
4894 wxArrayInt rowlabels
;
4901 // TODO: remove this when we can...
4902 // There is a bug in wxMotif that gives garbage update
4903 // rectangles if you jump-scroll a long way by clicking the
4904 // scrollbar with middle button. This is a work-around
4906 #if defined(__WXMOTIF__)
4908 m_gridWin
->GetClientSize( &cw
, &ch
);
4909 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4910 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4913 // logical bounds of update region
4916 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4917 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4919 // find the row labels within these bounds
4922 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4924 if ( GetRowBottom(row
) < top
)
4927 if ( GetRowTop(row
) > bottom
)
4930 rowlabels
.Add( row
);
4939 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4941 wxRegionIterator
iter( reg
);
4944 wxArrayInt colLabels
;
4951 // TODO: remove this when we can...
4952 // There is a bug in wxMotif that gives garbage update
4953 // rectangles if you jump-scroll a long way by clicking the
4954 // scrollbar with middle button. This is a work-around
4956 #if defined(__WXMOTIF__)
4958 m_gridWin
->GetClientSize( &cw
, &ch
);
4959 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4960 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4963 // logical bounds of update region
4966 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4967 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4969 // find the cells within these bounds
4972 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4974 if ( GetColRight(col
) < left
)
4977 if ( GetColLeft(col
) > right
)
4980 colLabels
.Add( col
);
4989 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4991 wxRegionIterator
iter( reg
);
4994 wxGridCellCoordsArray cellsExposed
;
4996 int left
, top
, right
, bottom
;
5001 // TODO: remove this when we can...
5002 // There is a bug in wxMotif that gives garbage update
5003 // rectangles if you jump-scroll a long way by clicking the
5004 // scrollbar with middle button. This is a work-around
5006 #if defined(__WXMOTIF__)
5008 m_gridWin
->GetClientSize( &cw
, &ch
);
5009 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5010 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5011 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5012 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5015 // logical bounds of update region
5017 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5018 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5020 // find the cells within these bounds
5023 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5025 if ( GetRowBottom(row
) <= top
)
5028 if ( GetRowTop(row
) > bottom
)
5031 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
5033 if ( GetColRight(col
) <= left
)
5036 if ( GetColLeft(col
) > right
)
5039 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5046 return cellsExposed
;
5050 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5053 wxPoint
pos( event
.GetPosition() );
5054 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5056 if ( event
.Dragging() )
5060 m_isDragging
= true;
5061 m_rowLabelWin
->CaptureMouse();
5064 if ( event
.LeftIsDown() )
5066 switch ( m_cursorMode
)
5068 case WXGRID_CURSOR_RESIZE_ROW
:
5070 int cw
, ch
, left
, dummy
;
5071 m_gridWin
->GetClientSize( &cw
, &ch
);
5072 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5074 wxClientDC
dc( m_gridWin
);
5077 GetRowTop(m_dragRowOrCol
) +
5078 GetRowMinimalHeight(m_dragRowOrCol
) );
5079 dc
.SetLogicalFunction(wxINVERT
);
5080 if ( m_dragLastPos
>= 0 )
5082 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5084 dc
.DrawLine( left
, y
, left
+cw
, y
);
5089 case WXGRID_CURSOR_SELECT_ROW
:
5091 if ( (row
= YToRow( y
)) >= 0 )
5095 m_selection
->SelectRow( row
,
5096 event
.ControlDown(),
5105 // default label to suppress warnings about "enumeration value
5106 // 'xxx' not handled in switch
5114 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5119 if (m_rowLabelWin
->HasCapture())
5120 m_rowLabelWin
->ReleaseMouse();
5121 m_isDragging
= false;
5124 // ------------ Entering or leaving the window
5126 if ( event
.Entering() || event
.Leaving() )
5128 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5131 // ------------ Left button pressed
5133 else if ( event
.LeftDown() )
5135 // don't send a label click event for a hit on the
5136 // edge of the row label - this is probably the user
5137 // wanting to resize the row
5139 if ( YToEdgeOfRow(y
) < 0 )
5143 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5145 if ( !event
.ShiftDown() && !event
.CmdDown() )
5149 if ( event
.ShiftDown() )
5151 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5154 GetNumberCols() - 1,
5155 event
.ControlDown(),
5162 m_selection
->SelectRow( row
,
5163 event
.ControlDown(),
5170 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5175 // starting to drag-resize a row
5176 if ( CanDragRowSize() )
5177 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5181 // ------------ Left double click
5183 else if (event
.LeftDClick() )
5185 row
= YToEdgeOfRow(y
);
5190 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5192 // no default action at the moment
5197 // adjust row height depending on label text
5198 AutoSizeRowLabelSize( row
);
5200 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5205 // ------------ Left button released
5207 else if ( event
.LeftUp() )
5209 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5211 DoEndDragResizeRow();
5213 // Note: we are ending the event *after* doing
5214 // default processing in this case
5216 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5219 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5223 // ------------ Right button down
5225 else if ( event
.RightDown() )
5229 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5231 // no default action at the moment
5235 // ------------ Right double click
5237 else if ( event
.RightDClick() )
5241 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5243 // no default action at the moment
5247 // ------------ No buttons down and mouse moving
5249 else if ( event
.Moving() )
5251 m_dragRowOrCol
= YToEdgeOfRow( y
);
5252 if ( m_dragRowOrCol
>= 0 )
5254 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5256 // don't capture the mouse yet
5257 if ( CanDragRowSize() )
5258 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5261 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5263 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5268 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5271 wxPoint
pos( event
.GetPosition() );
5272 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5274 if ( event
.Dragging() )
5278 m_isDragging
= true;
5279 m_colLabelWin
->CaptureMouse();
5282 if ( event
.LeftIsDown() )
5284 switch ( m_cursorMode
)
5286 case WXGRID_CURSOR_RESIZE_COL
:
5288 int cw
, ch
, dummy
, top
;
5289 m_gridWin
->GetClientSize( &cw
, &ch
);
5290 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5292 wxClientDC
dc( m_gridWin
);
5295 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5296 GetColMinimalWidth(m_dragRowOrCol
));
5297 dc
.SetLogicalFunction(wxINVERT
);
5298 if ( m_dragLastPos
>= 0 )
5300 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5302 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5307 case WXGRID_CURSOR_SELECT_COL
:
5309 if ( (col
= XToCol( x
)) >= 0 )
5313 m_selection
->SelectCol( col
,
5314 event
.ControlDown(),
5323 // default label to suppress warnings about "enumeration value
5324 // 'xxx' not handled in switch
5332 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5337 if (m_colLabelWin
->HasCapture())
5338 m_colLabelWin
->ReleaseMouse();
5339 m_isDragging
= false;
5342 // ------------ Entering or leaving the window
5344 if ( event
.Entering() || event
.Leaving() )
5346 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5349 // ------------ Left button pressed
5351 else if ( event
.LeftDown() )
5353 // don't send a label click event for a hit on the
5354 // edge of the col label - this is probably the user
5355 // wanting to resize the col
5357 if ( XToEdgeOfCol(x
) < 0 )
5361 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5363 if ( !event
.ShiftDown() && !event
.CmdDown() )
5367 if ( event
.ShiftDown() )
5369 m_selection
->SelectBlock( 0,
5370 m_currentCellCoords
.GetCol(),
5371 GetNumberRows() - 1, col
,
5372 event
.ControlDown(),
5379 m_selection
->SelectCol( col
,
5380 event
.ControlDown(),
5387 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5392 // starting to drag-resize a col
5394 if ( CanDragColSize() )
5395 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5399 // ------------ Left double click
5401 if ( event
.LeftDClick() )
5403 col
= XToEdgeOfCol(x
);
5408 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5410 // no default action at the moment
5415 // adjust column width depending on label text
5416 AutoSizeColLabelSize( col
);
5418 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5423 // ------------ Left button released
5425 else if ( event
.LeftUp() )
5427 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5429 DoEndDragResizeCol();
5431 // Note: we are ending the event *after* doing
5432 // default processing in this case
5434 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5437 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5441 // ------------ Right button down
5443 else if ( event
.RightDown() )
5447 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5449 // no default action at the moment
5453 // ------------ Right double click
5455 else if ( event
.RightDClick() )
5459 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5461 // no default action at the moment
5465 // ------------ No buttons down and mouse moving
5467 else if ( event
.Moving() )
5469 m_dragRowOrCol
= XToEdgeOfCol( x
);
5470 if ( m_dragRowOrCol
>= 0 )
5472 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5474 // don't capture the cursor yet
5475 if ( CanDragColSize() )
5476 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5479 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5481 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5486 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5488 if ( event
.LeftDown() )
5490 // indicate corner label by having both row and
5493 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5498 else if ( event
.LeftDClick() )
5500 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5502 else if ( event
.RightDown() )
5504 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5506 // no default action at the moment
5509 else if ( event
.RightDClick() )
5511 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5513 // no default action at the moment
5518 void wxGrid::ChangeCursorMode(CursorMode mode
,
5523 static const wxChar
*cursorModes
[] =
5532 wxLogTrace(_T("grid"),
5533 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5534 win
== m_colLabelWin
? _T("colLabelWin")
5535 : win
? _T("rowLabelWin")
5537 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5540 if ( mode
== m_cursorMode
&&
5541 win
== m_winCapture
&&
5542 captureMouse
== (m_winCapture
!= NULL
))
5547 // by default use the grid itself
5553 if (m_winCapture
->HasCapture())
5554 m_winCapture
->ReleaseMouse();
5555 m_winCapture
= (wxWindow
*)NULL
;
5558 m_cursorMode
= mode
;
5560 switch ( m_cursorMode
)
5562 case WXGRID_CURSOR_RESIZE_ROW
:
5563 win
->SetCursor( m_rowResizeCursor
);
5566 case WXGRID_CURSOR_RESIZE_COL
:
5567 win
->SetCursor( m_colResizeCursor
);
5571 win
->SetCursor( *wxSTANDARD_CURSOR
);
5575 // we need to capture mouse when resizing
5576 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5577 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5579 if ( captureMouse
&& resize
)
5581 win
->CaptureMouse();
5586 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5589 wxPoint
pos( event
.GetPosition() );
5590 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5592 wxGridCellCoords coords
;
5593 XYToCell( x
, y
, coords
);
5595 int cell_rows
, cell_cols
;
5596 bool isFirstDrag
= !m_isDragging
;
5597 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5598 if ((cell_rows
< 0) || (cell_cols
< 0))
5600 coords
.SetRow(coords
.GetRow() + cell_rows
);
5601 coords
.SetCol(coords
.GetCol() + cell_cols
);
5604 if ( event
.Dragging() )
5606 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5608 // Don't start doing anything until the mouse has been dragged at
5609 // least 3 pixels in any direction...
5612 if (m_startDragPos
== wxDefaultPosition
)
5614 m_startDragPos
= pos
;
5617 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5621 m_isDragging
= true;
5622 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5624 // Hide the edit control, so it
5625 // won't interfere with drag-shrinking.
5626 if ( IsCellEditControlShown() )
5628 HideCellEditControl();
5629 SaveEditControlValue();
5632 // Have we captured the mouse yet?
5635 m_winCapture
= m_gridWin
;
5636 m_winCapture
->CaptureMouse();
5639 if ( coords
!= wxGridNoCellCoords
)
5641 if ( event
.CmdDown() )
5643 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5644 m_selectingKeyboard
= coords
;
5645 HighlightBlock ( m_selectingKeyboard
, coords
);
5647 else if ( CanDragCell() )
5651 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5652 m_selectingKeyboard
= coords
;
5654 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5662 if ( !IsSelection() )
5664 HighlightBlock( coords
, coords
);
5668 HighlightBlock( m_currentCellCoords
, coords
);
5672 if (! IsVisible(coords
))
5674 MakeCellVisible(coords
);
5675 // TODO: need to introduce a delay or something here. The
5676 // scrolling is way to fast, at least on MSW - also on GTK.
5680 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5682 int cw
, ch
, left
, dummy
;
5683 m_gridWin
->GetClientSize( &cw
, &ch
);
5684 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5686 wxClientDC
dc( m_gridWin
);
5688 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5689 GetRowMinimalHeight(m_dragRowOrCol
) );
5690 dc
.SetLogicalFunction(wxINVERT
);
5691 if ( m_dragLastPos
>= 0 )
5693 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5695 dc
.DrawLine( left
, y
, left
+cw
, y
);
5698 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5700 int cw
, ch
, dummy
, top
;
5701 m_gridWin
->GetClientSize( &cw
, &ch
);
5702 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5704 wxClientDC
dc( m_gridWin
);
5706 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5707 GetColMinimalWidth(m_dragRowOrCol
) );
5708 dc
.SetLogicalFunction(wxINVERT
);
5709 if ( m_dragLastPos
>= 0 )
5711 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5713 dc
.DrawLine( x
, top
, x
, top
+ch
);
5720 m_isDragging
= false;
5721 m_startDragPos
= wxDefaultPosition
;
5723 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5724 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5727 if ( event
.Entering() || event
.Leaving() )
5729 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5730 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5735 // ------------ Left button pressed
5737 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5739 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5744 if ( !event
.CmdDown() )
5746 if ( event
.ShiftDown() )
5750 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5751 m_currentCellCoords
.GetCol(),
5754 event
.ControlDown(),
5760 else if ( XToEdgeOfCol(x
) < 0 &&
5761 YToEdgeOfRow(y
) < 0 )
5763 DisableCellEditControl();
5764 MakeCellVisible( coords
);
5766 if ( event
.CmdDown() )
5770 m_selection
->ToggleCellSelection( coords
.GetRow(),
5772 event
.ControlDown(),
5777 m_selectingTopLeft
= wxGridNoCellCoords
;
5778 m_selectingBottomRight
= wxGridNoCellCoords
;
5779 m_selectingKeyboard
= coords
;
5783 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5784 SetCurrentCell( coords
);
5787 if ( m_selection
->GetSelectionMode() !=
5788 wxGrid::wxGridSelectCells
)
5790 HighlightBlock( coords
, coords
);
5799 // ------------ Left double click
5801 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5803 DisableCellEditControl();
5805 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5807 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5812 // we want double click to select a cell and start editing
5813 // (i.e. to behave in same way as sequence of two slow clicks):
5814 m_waitForSlowClick
= true;
5820 // ------------ Left button released
5822 else if ( event
.LeftUp() )
5824 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5828 if (m_winCapture
->HasCapture())
5829 m_winCapture
->ReleaseMouse();
5830 m_winCapture
= NULL
;
5833 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
5836 EnableCellEditControl();
5838 wxGridCellAttr
*attr
= GetCellAttr(coords
);
5839 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5840 editor
->StartingClick();
5844 m_waitForSlowClick
= false;
5846 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5847 m_selectingBottomRight
!= wxGridNoCellCoords
)
5851 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5852 m_selectingTopLeft
.GetCol(),
5853 m_selectingBottomRight
.GetRow(),
5854 m_selectingBottomRight
.GetCol(),
5855 event
.ControlDown(),
5861 m_selectingTopLeft
= wxGridNoCellCoords
;
5862 m_selectingBottomRight
= wxGridNoCellCoords
;
5864 // Show the edit control, if it has been hidden for
5866 ShowCellEditControl();
5869 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5871 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5872 DoEndDragResizeRow();
5874 // Note: we are ending the event *after* doing
5875 // default processing in this case
5877 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5879 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5881 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5882 DoEndDragResizeCol();
5884 // Note: we are ending the event *after* doing
5885 // default processing in this case
5887 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5893 // ------------ Right button down
5895 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5897 DisableCellEditControl();
5898 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5903 // no default action at the moment
5907 // ------------ Right double click
5909 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5911 DisableCellEditControl();
5912 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5917 // no default action at the moment
5921 // ------------ Moving and no button action
5923 else if ( event
.Moving() && !event
.IsButton() )
5925 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5927 // out of grid cell area
5928 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5932 int dragRow
= YToEdgeOfRow( y
);
5933 int dragCol
= XToEdgeOfCol( x
);
5935 // Dragging on the corner of a cell to resize in both
5936 // directions is not implemented yet...
5938 if ( dragRow
>= 0 && dragCol
>= 0 )
5940 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5946 m_dragRowOrCol
= dragRow
;
5948 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5950 if ( CanDragRowSize() && CanDragGridSize() )
5951 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5954 else if ( dragCol
>= 0 )
5956 m_dragRowOrCol
= dragCol
;
5958 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5960 if ( CanDragColSize() && CanDragGridSize() )
5961 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5964 else // Neither on a row or col edge
5966 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5968 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5974 void wxGrid::DoEndDragResizeRow()
5976 if ( m_dragLastPos
>= 0 )
5978 // erase the last line and resize the row
5980 int cw
, ch
, left
, dummy
;
5981 m_gridWin
->GetClientSize( &cw
, &ch
);
5982 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5984 wxClientDC
dc( m_gridWin
);
5986 dc
.SetLogicalFunction( wxINVERT
);
5987 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5988 HideCellEditControl();
5989 SaveEditControlValue();
5991 int rowTop
= GetRowTop(m_dragRowOrCol
);
5992 SetRowSize( m_dragRowOrCol
,
5993 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5995 if ( !GetBatchCount() )
5997 // Only needed to get the correct rect.y:
5998 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6000 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6001 rect
.width
= m_rowLabelWidth
;
6002 rect
.height
= ch
- rect
.y
;
6003 m_rowLabelWin
->Refresh( true, &rect
);
6006 // if there is a multicell block, paint all of it
6009 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6010 int leftCol
= XToCol(left
);
6011 int rightCol
= internalXToCol(left
+cw
);
6014 for (i
=leftCol
; i
<rightCol
; i
++)
6016 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6017 if (cell_rows
< subtract_rows
)
6018 subtract_rows
= cell_rows
;
6020 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6021 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6022 rect
.height
= ch
- rect
.y
;
6025 m_gridWin
->Refresh( false, &rect
);
6028 ShowCellEditControl();
6033 void wxGrid::DoEndDragResizeCol()
6035 if ( m_dragLastPos
>= 0 )
6037 // erase the last line and resize the col
6039 int cw
, ch
, dummy
, top
;
6040 m_gridWin
->GetClientSize( &cw
, &ch
);
6041 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6043 wxClientDC
dc( m_gridWin
);
6045 dc
.SetLogicalFunction( wxINVERT
);
6046 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
6047 HideCellEditControl();
6048 SaveEditControlValue();
6050 int colLeft
= GetColLeft(m_dragRowOrCol
);
6051 SetColSize( m_dragRowOrCol
,
6052 wxMax( m_dragLastPos
- colLeft
,
6053 GetColMinimalWidth(m_dragRowOrCol
) ) );
6055 if ( !GetBatchCount() )
6057 // Only needed to get the correct rect.x:
6058 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6060 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6061 rect
.width
= cw
- rect
.x
;
6062 rect
.height
= m_colLabelHeight
;
6063 m_colLabelWin
->Refresh( true, &rect
);
6066 // if there is a multicell block, paint all of it
6069 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6070 int topRow
= YToRow(top
);
6071 int bottomRow
= internalYToRow(top
+cw
);
6074 for (i
=topRow
; i
<bottomRow
; i
++)
6076 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6077 if (cell_cols
< subtract_cols
)
6078 subtract_cols
= cell_cols
;
6080 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6081 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6082 rect
.width
= cw
- rect
.x
;
6086 m_gridWin
->Refresh( false, &rect
);
6089 ShowCellEditControl();
6094 // ------ interaction with data model
6096 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6098 switch ( msg
.GetId() )
6100 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6101 return GetModelValues();
6103 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6104 return SetModelValues();
6106 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6107 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6108 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6109 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6110 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6111 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6112 return Redimension( msg
);
6119 // The behaviour of this function depends on the grid table class
6120 // Clear() function. For the default wxGridStringTable class the
6121 // behavious is to replace all cell contents with wxEmptyString but
6122 // not to change the number of rows or cols.
6124 void wxGrid::ClearGrid()
6128 if (IsCellEditControlEnabled())
6129 DisableCellEditControl();
6132 if ( !GetBatchCount() ) m_gridWin
->Refresh();
6136 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6138 // TODO: something with updateLabels flag
6142 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6148 if (IsCellEditControlEnabled())
6149 DisableCellEditControl();
6151 bool done
= m_table
->InsertRows( pos
, numRows
);
6154 // the table will have sent the results of the insert row
6155 // operation to this view object as a grid table message
6160 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6162 // TODO: something with updateLabels flag
6166 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6172 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6174 // the table will have sent the results of the append row
6175 // operation to this view object as a grid table message
6180 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6182 // TODO: something with updateLabels flag
6186 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6192 if (IsCellEditControlEnabled())
6193 DisableCellEditControl();
6195 bool done
= m_table
->DeleteRows( pos
, numRows
);
6197 // the table will have sent the results of the delete row
6198 // operation to this view object as a grid table message
6203 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6205 // TODO: something with updateLabels flag
6209 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6215 if (IsCellEditControlEnabled())
6216 DisableCellEditControl();
6218 bool done
= m_table
->InsertCols( pos
, numCols
);
6220 // the table will have sent the results of the insert col
6221 // operation to this view object as a grid table message
6227 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6229 // TODO: something with updateLabels flag
6233 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6239 bool done
= m_table
->AppendCols( numCols
);
6241 // the table will have sent the results of the append col
6242 // operation to this view object as a grid table message
6248 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6250 // TODO: something with updateLabels flag
6254 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6260 if (IsCellEditControlEnabled())
6261 DisableCellEditControl();
6263 bool done
= m_table
->DeleteCols( pos
, numCols
);
6265 // the table will have sent the results of the delete col
6266 // operation to this view object as a grid table message
6273 // ----- event handlers
6276 // Generate a grid event based on a mouse event and
6277 // return the result of ProcessEvent()
6279 int wxGrid::SendEvent( const wxEventType type
,
6281 wxMouseEvent
& mouseEv
)
6283 bool claimed
, vetoed
;
6285 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6287 int rowOrCol
= (row
== -1 ? col
: row
);
6289 wxGridSizeEvent
gridEvt( GetId(),
6293 mouseEv
.GetX() + GetRowLabelSize(),
6294 mouseEv
.GetY() + GetColLabelSize(),
6295 mouseEv
.ControlDown(),
6296 mouseEv
.ShiftDown(),
6298 mouseEv
.MetaDown() );
6300 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6301 vetoed
= !gridEvt
.IsAllowed();
6303 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6305 // Right now, it should _never_ end up here!
6306 wxGridRangeSelectEvent
gridEvt( GetId(),
6310 m_selectingBottomRight
,
6312 mouseEv
.ControlDown(),
6313 mouseEv
.ShiftDown(),
6315 mouseEv
.MetaDown() );
6317 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6318 vetoed
= !gridEvt
.IsAllowed();
6322 wxGridEvent
gridEvt( GetId(),
6326 mouseEv
.GetX() + GetRowLabelSize(),
6327 mouseEv
.GetY() + GetColLabelSize(),
6329 mouseEv
.ControlDown(),
6330 mouseEv
.ShiftDown(),
6332 mouseEv
.MetaDown() );
6333 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6334 vetoed
= !gridEvt
.IsAllowed();
6337 // A Veto'd event may not be `claimed' so test this first
6341 return claimed
? 1 : 0;
6344 // Generate a grid event of specified type and return the result
6345 // of ProcessEvent().
6347 int wxGrid::SendEvent( const wxEventType type
,
6353 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6355 int rowOrCol
= (row
== -1 ? col
: row
);
6357 wxGridSizeEvent
gridEvt( GetId(),
6362 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6363 vetoed
= !gridEvt
.IsAllowed();
6367 wxGridEvent
gridEvt( GetId(),
6372 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6373 vetoed
= !gridEvt
.IsAllowed();
6376 // A Veto'd event may not be `claimed' so test this first
6380 return claimed
? 1 : 0;
6383 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6385 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6388 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6390 // Don't do anything if between Begin/EndBatch...
6391 // EndBatch() will do all this on the last nested one anyway.
6392 if (! GetBatchCount())
6394 // Refresh to get correct scrolled position:
6395 wxScrolledWindow::Refresh(eraseb
, rect
);
6399 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6400 int width_label
, width_cell
, height_label
, height_cell
;
6403 // Copy rectangle can get scroll offsets..
6404 rect_x
= rect
->GetX();
6405 rect_y
= rect
->GetY();
6406 rectWidth
= rect
->GetWidth();
6407 rectHeight
= rect
->GetHeight();
6409 width_label
= m_rowLabelWidth
- rect_x
;
6410 if (width_label
> rectWidth
)
6411 width_label
= rectWidth
;
6413 height_label
= m_colLabelHeight
- rect_y
;
6414 if (height_label
> rectHeight
) height_label
= rectHeight
;
6416 if (rect_x
> m_rowLabelWidth
)
6418 x
= rect_x
- m_rowLabelWidth
;
6419 width_cell
= rectWidth
;
6424 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6427 if (rect_y
> m_colLabelHeight
)
6429 y
= rect_y
- m_colLabelHeight
;
6430 height_cell
= rectHeight
;
6435 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6438 // Paint corner label part intersecting rect.
6439 if ( width_label
> 0 && height_label
> 0 )
6441 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6442 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6445 // Paint col labels part intersecting rect.
6446 if ( width_cell
> 0 && height_label
> 0 )
6448 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6449 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6452 // Paint row labels part intersecting rect.
6453 if ( width_label
> 0 && height_cell
> 0 )
6455 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6456 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6459 // Paint cell area part intersecting rect.
6460 if ( width_cell
> 0 && height_cell
> 0 )
6462 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6463 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6468 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6469 m_colLabelWin
->Refresh(eraseb
, NULL
);
6470 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6471 m_gridWin
->Refresh(eraseb
, NULL
);
6476 void wxGrid::OnSize( wxSizeEvent
& event
)
6478 // position the child windows
6481 // don't call CalcDimensions() from here, the base class handles the size
6486 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6488 if ( m_inOnKeyDown
)
6490 // shouldn't be here - we are going round in circles...
6492 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6495 m_inOnKeyDown
= true;
6497 // propagate the event up and see if it gets processed
6499 wxWindow
*parent
= GetParent();
6500 wxKeyEvent
keyEvt( event
);
6501 keyEvt
.SetEventObject( parent
);
6503 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6505 // try local handlers
6507 switch ( event
.GetKeyCode() )
6510 if ( event
.ControlDown() )
6511 MoveCursorUpBlock( event
.ShiftDown() );
6513 MoveCursorUp( event
.ShiftDown() );
6517 if ( event
.ControlDown() )
6518 MoveCursorDownBlock( event
.ShiftDown() );
6520 MoveCursorDown( event
.ShiftDown() );
6524 if ( event
.ControlDown() )
6525 MoveCursorLeftBlock( event
.ShiftDown() );
6527 MoveCursorLeft( event
.ShiftDown() );
6531 if ( event
.ControlDown() )
6532 MoveCursorRightBlock( event
.ShiftDown() );
6534 MoveCursorRight( event
.ShiftDown() );
6538 case WXK_NUMPAD_ENTER
:
6539 if ( event
.ControlDown() )
6541 event
.Skip(); // to let the edit control have the return
6545 if ( GetGridCursorRow() < GetNumberRows()-1 )
6547 MoveCursorDown( event
.ShiftDown() );
6551 // at the bottom of a column
6552 DisableCellEditControl();
6562 if (event
.ShiftDown())
6564 if ( GetGridCursorCol() > 0 )
6566 MoveCursorLeft( false );
6571 DisableCellEditControl();
6576 if ( GetGridCursorCol() < GetNumberCols() - 1 )
6578 MoveCursorRight( false );
6583 DisableCellEditControl();
6589 if ( event
.ControlDown() )
6591 MakeCellVisible( 0, 0 );
6592 SetCurrentCell( 0, 0 );
6601 if ( event
.ControlDown() )
6603 MakeCellVisible( m_numRows
-1, m_numCols
- 1 );
6604 SetCurrentCell( m_numRows
-1, m_numCols
- 1 );
6621 if ( event
.ControlDown() )
6625 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6626 m_currentCellCoords
.GetCol(),
6627 event
.ControlDown(),
6635 if ( !IsEditable() )
6636 MoveCursorRight( false );
6647 m_inOnKeyDown
= false;
6650 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6652 // try local handlers
6654 if ( event
.GetKeyCode() == WXK_SHIFT
)
6656 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6657 m_selectingBottomRight
!= wxGridNoCellCoords
)
6661 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6662 m_selectingTopLeft
.GetCol(),
6663 m_selectingBottomRight
.GetRow(),
6664 m_selectingBottomRight
.GetCol(),
6665 event
.ControlDown(),
6672 m_selectingTopLeft
= wxGridNoCellCoords
;
6673 m_selectingBottomRight
= wxGridNoCellCoords
;
6674 m_selectingKeyboard
= wxGridNoCellCoords
;
6678 void wxGrid::OnChar( wxKeyEvent
& event
)
6680 // is it possible to edit the current cell at all?
6681 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6683 // yes, now check whether the cells editor accepts the key
6684 int row
= m_currentCellCoords
.GetRow();
6685 int col
= m_currentCellCoords
.GetCol();
6686 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6687 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6689 // <F2> is special and will always start editing, for
6690 // other keys - ask the editor itself
6691 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6692 || editor
->IsAcceptedKey(event
) )
6694 // ensure cell is visble
6695 MakeCellVisible(row
, col
);
6696 EnableCellEditControl();
6698 // a problem can arise if the cell is not completely
6699 // visible (even after calling MakeCellVisible the
6700 // control is not created and calling StartingKey will
6702 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6703 editor
->StartingKey(event
);
6719 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6723 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6725 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6727 // the event has been intercepted - do nothing
6731 wxClientDC
dc( m_gridWin
);
6734 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6736 DisableCellEditControl();
6738 if ( IsVisible( m_currentCellCoords
, false ) )
6741 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
6742 if ( !m_gridLinesEnabled
)
6750 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6752 // Otherwise refresh redraws the highlight!
6753 m_currentCellCoords
= coords
;
6755 DrawGridCellArea( dc
, cells
);
6756 DrawAllGridLines( dc
, r
);
6760 m_currentCellCoords
= coords
;
6762 wxGridCellAttr
*attr
= GetCellAttr( coords
);
6763 DrawCellHighlight( dc
, attr
);
6767 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6770 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6774 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6777 rightCol
= GetNumberCols() - 1;
6779 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6782 bottomRow
= GetNumberRows() - 1;
6786 if ( topRow
> bottomRow
)
6793 if ( leftCol
> rightCol
)
6800 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6801 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6803 // First the case that we selected a completely new area
6804 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6805 m_selectingBottomRight
== wxGridNoCellCoords
)
6808 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6809 wxGridCellCoords ( bottomRow
, rightCol
) );
6810 m_gridWin
->Refresh( false, &rect
);
6813 // Now handle changing an existing selection area.
6814 else if ( m_selectingTopLeft
!= updateTopLeft
||
6815 m_selectingBottomRight
!= updateBottomRight
)
6817 // Compute two optimal update rectangles:
6818 // Either one rectangle is a real subset of the
6819 // other, or they are (almost) disjoint!
6821 bool need_refresh
[4];
6825 need_refresh
[3] = false;
6828 // Store intermediate values
6829 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6830 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6831 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6832 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6834 // Determine the outer/inner coordinates.
6835 if (oldLeft
> leftCol
)
6841 if (oldTop
> topRow
)
6847 if (oldRight
< rightCol
)
6850 oldRight
= rightCol
;
6853 if (oldBottom
< bottomRow
)
6856 oldBottom
= bottomRow
;
6860 // Now, either the stuff marked old is the outer
6861 // rectangle or we don't have a situation where one
6862 // is contained in the other.
6864 if ( oldLeft
< leftCol
)
6866 // Refresh the newly selected or deselected
6867 // area to the left of the old or new selection.
6868 need_refresh
[0] = true;
6869 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6871 wxGridCellCoords ( oldBottom
,
6875 if ( oldTop
< topRow
)
6877 // Refresh the newly selected or deselected
6878 // area above the old or new selection.
6879 need_refresh
[1] = true;
6880 rect
[1] = BlockToDeviceRect(
6881 wxGridCellCoords( oldTop
, leftCol
),
6882 wxGridCellCoords( topRow
- 1, rightCol
) );
6885 if ( oldRight
> rightCol
)
6887 // Refresh the newly selected or deselected
6888 // area to the right of the old or new selection.
6889 need_refresh
[2] = true;
6890 rect
[2] = BlockToDeviceRect(
6891 wxGridCellCoords ( oldTop
, rightCol
+ 1 ),
6892 wxGridCellCoords ( oldBottom
, oldRight
) );
6895 if ( oldBottom
> bottomRow
)
6897 // Refresh the newly selected or deselected
6898 // area below the old or new selection.
6899 need_refresh
[3] = true;
6900 rect
[3] = BlockToDeviceRect(
6901 wxGridCellCoords ( bottomRow
+ 1, leftCol
),
6902 wxGridCellCoords ( oldBottom
, rightCol
) );
6905 // various Refresh() calls
6906 for (i
= 0; i
< 4; i
++ )
6907 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6908 m_gridWin
->Refresh( false, &(rect
[i
]) );
6912 m_selectingTopLeft
= updateTopLeft
;
6913 m_selectingBottomRight
= updateBottomRight
;
6917 // ------ functions to get/send data (see also public functions)
6920 bool wxGrid::GetModelValues()
6922 // Hide the editor, so it won't hide a changed value.
6923 HideCellEditControl();
6927 // all we need to do is repaint the grid
6929 m_gridWin
->Refresh();
6936 bool wxGrid::SetModelValues()
6940 // Disable the editor, so it won't hide a changed value.
6941 // Do we also want to save the current value of the editor first?
6943 DisableCellEditControl();
6947 for ( row
= 0; row
< m_numRows
; row
++ )
6949 for ( col
= 0; col
< m_numCols
; col
++ )
6951 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6961 // Note - this function only draws cells that are in the list of
6962 // exposed cells (usually set from the update region by
6963 // CalcExposedCells)
6965 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6967 if ( !m_numRows
|| !m_numCols
)
6970 int i
, numCells
= cells
.GetCount();
6971 int row
, col
, cell_rows
, cell_cols
;
6972 wxGridCellCoordsArray redrawCells
;
6974 for ( i
= numCells
-1; i
>= 0; i
-- )
6976 row
= cells
[i
].GetRow();
6977 col
= cells
[i
].GetCol();
6978 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6980 // If this cell is part of a multicell block, find owner for repaint
6981 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6983 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6984 bool marked
= false;
6985 for ( int j
= 0; j
< numCells
; j
++ )
6987 if ( cell
== cells
[j
] )
6996 int count
= redrawCells
.GetCount();
6997 for (int j
= 0; j
< count
; j
++)
6999 if ( cell
== redrawCells
[j
] )
7007 redrawCells
.Add( cell
);
7010 // don't bother drawing this cell
7014 // If this cell is empty, find cell to left that might want to overflow
7015 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7017 for ( int l
= 0; l
< cell_rows
; l
++ )
7019 // find a cell in this row to left alreay marked for repaint
7021 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7022 if ((redrawCells
[k
].GetCol() < left
) &&
7023 (redrawCells
[k
].GetRow() == row
))
7025 left
= redrawCells
[k
].GetCol();
7029 left
= 0; // oh well
7031 for (int j
= col
- 1; j
>= left
; j
--)
7033 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7035 if (GetCellOverflow(row
+ l
, j
))
7037 wxGridCellCoords
cell(row
+ l
, j
);
7038 bool marked
= false;
7040 for (int k
= 0; k
< numCells
; k
++)
7042 if ( cell
== cells
[k
] )
7051 int count
= redrawCells
.GetCount();
7052 for (int k
= 0; k
< count
; k
++)
7054 if ( cell
== redrawCells
[k
] )
7061 redrawCells
.Add( cell
);
7070 DrawCell( dc
, cells
[i
] );
7073 numCells
= redrawCells
.GetCount();
7075 for ( i
= numCells
- 1; i
>= 0; i
-- )
7077 DrawCell( dc
, redrawCells
[i
] );
7081 void wxGrid::DrawGridSpace( wxDC
& dc
)
7084 m_gridWin
->GetClientSize( &cw
, &ch
);
7087 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7089 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
7090 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7092 if ( right
> rightCol
|| bottom
> bottomRow
)
7095 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7097 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7098 dc
.SetPen( *wxTRANSPARENT_PEN
);
7100 if ( right
> rightCol
)
7102 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7105 if ( bottom
> bottomRow
)
7107 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7112 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7114 int row
= coords
.GetRow();
7115 int col
= coords
.GetCol();
7117 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7120 // we draw the cell border ourselves
7121 #if !WXGRID_DRAW_LINES
7122 if ( m_gridLinesEnabled
)
7123 DrawCellBorder( dc
, coords
);
7126 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7128 bool isCurrent
= coords
== m_currentCellCoords
;
7130 wxRect rect
= CellToRect( row
, col
);
7132 // if the editor is shown, we should use it and not the renderer
7133 // Note: However, only if it is really _shown_, i.e. not hidden!
7134 if ( isCurrent
&& IsCellEditControlShown() )
7136 // OSAF NB: this "#if..." is temporary and fixes a problem where the
7137 // edit control is erased by this code after being rendered.
7138 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7139 // implicitly, causing this out-of order render.
7140 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7141 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7142 editor
->PaintBackground(rect
, attr
);
7148 // but all the rest is drawn by the cell renderer and hence may be
7150 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7151 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7158 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7160 int row
= m_currentCellCoords
.GetRow();
7161 int col
= m_currentCellCoords
.GetCol();
7163 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7166 wxRect rect
= CellToRect(row
, col
);
7168 // hmmm... what could we do here to show that the cell is disabled?
7169 // for now, I just draw a thinner border than for the other ones, but
7170 // it doesn't look really good
7172 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7176 // The center of th drawn line is where the position/width/height of
7177 // the rectangle is actually at, (on wxMSW atr least,) so we will
7178 // reduce the size of the rectangle to compensate for the thickness of
7179 // the line. If this is too strange on non wxMSW platforms then
7180 // please #ifdef this appropriately.
7181 rect
.x
+= penWidth
/ 2;
7182 rect
.y
+= penWidth
/ 2;
7183 rect
.width
-= penWidth
- 1;
7184 rect
.height
-= penWidth
- 1;
7186 // Now draw the rectangle
7187 // use the cellHighlightColour if the cell is inside a selection, this
7188 // will ensure the cell is always visible.
7189 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7190 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7191 dc
.DrawRectangle(rect
);
7195 // VZ: my experiments with 3D borders...
7197 // how to properly set colours for arbitrary bg?
7198 wxCoord x1
= rect
.x
,
7200 x2
= rect
.x
+ rect
.width
- 1,
7201 y2
= rect
.y
+ rect
.height
- 1;
7203 dc
.SetPen(*wxWHITE_PEN
);
7204 dc
.DrawLine(x1
, y1
, x2
, y1
);
7205 dc
.DrawLine(x1
, y1
, x1
, y2
);
7207 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7208 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7210 dc
.SetPen(*wxBLACK_PEN
);
7211 dc
.DrawLine(x1
, y2
, x2
, y2
);
7212 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7216 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7218 int row
= coords
.GetRow();
7219 int col
= coords
.GetCol();
7220 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7223 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7225 wxRect rect
= CellToRect( row
, col
);
7227 // right hand border
7229 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7230 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7234 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7235 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7238 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7240 // This if block was previously in wxGrid::OnPaint but that doesn't
7241 // seem to get called under wxGTK - MB
7243 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7244 m_numRows
&& m_numCols
)
7246 m_currentCellCoords
.Set(0, 0);
7249 if ( IsCellEditControlShown() )
7251 // don't show highlight when the edit control is shown
7255 // if the active cell was repainted, repaint its highlight too because it
7256 // might have been damaged by the grid lines
7257 size_t count
= cells
.GetCount();
7258 for ( size_t n
= 0; n
< count
; n
++ )
7260 if ( cells
[n
] == m_currentCellCoords
)
7262 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7263 DrawCellHighlight(dc
, attr
);
7271 // TODO: remove this ???
7272 // This is used to redraw all grid lines e.g. when the grid line colour
7275 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7277 #if !WXGRID_DRAW_LINES
7281 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7284 int top
, bottom
, left
, right
;
7286 #if 0 //#ifndef __WXGTK__
7290 m_gridWin
->GetClientSize(&cw
, &ch
);
7292 // virtual coords of visible area
7294 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7295 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7300 reg
.GetBox(x
, y
, w
, h
);
7301 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7302 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7306 m_gridWin
->GetClientSize(&cw
, &ch
);
7307 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7308 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7311 // avoid drawing grid lines past the last row and col
7313 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7314 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7316 // no gridlines inside multicells, clip them out
7317 int leftCol
= internalXToCol(left
);
7318 int topRow
= internalYToRow(top
);
7319 int rightCol
= internalXToCol(right
);
7320 int bottomRow
= internalYToRow(bottom
);
7323 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7324 wxRegion
clippedcells(0, 0, cw
, ch
);
7326 int i
, j
, cell_rows
, cell_cols
;
7329 for (j
=topRow
; j
<bottomRow
; j
++)
7331 for (i
=leftCol
; i
<rightCol
; i
++)
7333 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7334 if ((cell_rows
> 1) || (cell_cols
> 1))
7336 rect
= CellToRect(j
,i
);
7337 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7338 clippedcells
.Subtract(rect
);
7340 else if ((cell_rows
< 0) || (cell_cols
< 0))
7342 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7343 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7344 clippedcells
.Subtract(rect
);
7349 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7351 int i
, j
, cell_rows
, cell_cols
;
7354 for (j
=topRow
; j
<bottomRow
; j
++)
7356 for (i
=leftCol
; i
<rightCol
; i
++)
7358 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7359 if ((cell_rows
> 1) || (cell_cols
> 1))
7361 rect
= CellToRect(j
, i
);
7362 clippedcells
.Subtract(rect
);
7364 else if ((cell_rows
< 0) || (cell_cols
< 0))
7366 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7367 clippedcells
.Subtract(rect
);
7372 dc
.SetClippingRegion( clippedcells
);
7374 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7376 // horizontal grid lines
7378 // already declared above - int i;
7379 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7381 int bot
= GetRowBottom(i
) - 1;
7390 dc
.DrawLine( left
, bot
, right
, bot
);
7394 // vertical grid lines
7396 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7398 int colRight
= GetColRight(i
) - 1;
7399 if ( colRight
> right
)
7404 if ( colRight
>= left
)
7406 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7409 dc
.DestroyClippingRegion();
7412 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7418 size_t numLabels
= rows
.GetCount();
7420 for ( i
= 0; i
< numLabels
; i
++ )
7422 DrawRowLabel( dc
, rows
[i
] );
7426 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7428 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7435 rect
.SetY( GetRowTop(row
) + 1 );
7436 rect
.SetWidth( m_rowLabelWidth
- 2 );
7437 rect
.SetHeight( GetRowHeight(row
) - 2 );
7439 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7441 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7443 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7445 int rowTop
= GetRowTop(row
),
7446 rowBottom
= GetRowBottom(row
) - 1;
7448 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7449 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7450 m_rowLabelWidth
-1, rowBottom
);
7452 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7454 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7456 dc
.SetPen( *wxWHITE_PEN
);
7457 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7458 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7461 dc
.SetBackgroundMode( wxTRANSPARENT
);
7462 dc
.SetTextForeground( GetLabelTextColour() );
7463 dc
.SetFont( GetLabelFont() );
7466 GetRowLabelAlignment( &hAlign
, &vAlign
);
7469 rect
.SetY( GetRowTop(row
) + 2 );
7470 rect
.SetWidth( m_rowLabelWidth
- 4 );
7471 rect
.SetHeight( GetRowHeight(row
) - 4 );
7472 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7475 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7481 size_t numLabels
= cols
.GetCount();
7483 for ( i
= 0; i
< numLabels
; i
++ )
7485 DrawColLabel( dc
, cols
[i
] );
7489 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7491 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7494 int colLeft
= GetColLeft(col
);
7499 rect
.SetX( colLeft
+ 1 );
7501 rect
.SetWidth( GetColWidth(col
) - 2 );
7502 rect
.SetHeight( m_colLabelHeight
- 2 );
7504 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7506 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7508 int colRight
= GetColRight(col
) - 1;
7510 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
), 1, wxSOLID
) );
7511 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
7513 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7515 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
7516 colRight
+ 1, m_colLabelHeight
- 1 );
7518 dc
.SetPen( *wxWHITE_PEN
);
7519 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
7520 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7523 dc
.SetBackgroundMode( wxTRANSPARENT
);
7524 dc
.SetTextForeground( GetLabelTextColour() );
7525 dc
.SetFont( GetLabelFont() );
7527 int hAlign
, vAlign
, orient
;
7528 GetColLabelAlignment( &hAlign
, &vAlign
);
7529 orient
= GetColLabelTextOrientation();
7531 rect
.SetX( colLeft
+ 2 );
7533 rect
.SetWidth( GetColWidth(col
) - 4 );
7534 rect
.SetHeight( m_colLabelHeight
- 4 );
7535 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7538 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7539 const wxString
& value
,
7543 int textOrientation
)
7545 wxArrayString lines
;
7547 StringToLines( value
, lines
);
7549 // Forward to new API.
7550 DrawTextRectangle( dc
,
7558 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7559 const wxArrayString
& lines
,
7563 int textOrientation
)
7565 long textWidth
= 0, textHeight
= 0;
7566 long lineWidth
= 0, lineHeight
= 0;
7569 dc
.SetClippingRegion( rect
);
7571 nLines
= lines
.GetCount();
7575 float x
= 0.0, y
= 0.0;
7577 if ( textOrientation
== wxHORIZONTAL
)
7578 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7580 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7582 switch ( vertAlign
)
7584 case wxALIGN_BOTTOM
:
7585 if ( textOrientation
== wxHORIZONTAL
)
7586 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7588 x
= rect
.x
+ rect
.width
- textWidth
;
7591 case wxALIGN_CENTRE
:
7592 if ( textOrientation
== wxHORIZONTAL
)
7593 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7595 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7600 if ( textOrientation
== wxHORIZONTAL
)
7607 // Align each line of a multi-line label
7608 for ( l
= 0; l
< nLines
; l
++ )
7610 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7612 switch ( horizAlign
)
7615 if ( textOrientation
== wxHORIZONTAL
)
7616 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7618 y
= rect
.y
+ lineWidth
+ 1;
7621 case wxALIGN_CENTRE
:
7622 if ( textOrientation
== wxHORIZONTAL
)
7623 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
7625 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
7630 if ( textOrientation
== wxHORIZONTAL
)
7633 y
= rect
.y
+ rect
.height
- 1;
7637 if ( textOrientation
== wxHORIZONTAL
)
7639 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7644 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7650 dc
.DestroyClippingRegion();
7653 // Split multi-line text up into an array of strings.
7654 // Any existing contents of the string array are preserved.
7656 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7660 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7661 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7663 while ( startPos
< (int)tVal
.length() )
7665 pos
= tVal
.Mid(startPos
).Find( eol
);
7670 else if ( pos
== 0 )
7672 lines
.Add( wxEmptyString
);
7676 lines
.Add( value
.Mid(startPos
, pos
) );
7678 startPos
+= pos
+ 1;
7681 if ( startPos
< (int)value
.length() )
7683 lines
.Add( value
.Mid( startPos
) );
7687 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
7688 const wxArrayString
& lines
,
7689 long *width
, long *height
)
7693 long lineW
= 0, lineH
= 0;
7696 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7698 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7699 w
= wxMax( w
, lineW
);
7708 // ------ Batch processing.
7710 void wxGrid::EndBatch()
7712 if ( m_batchCount
> 0 )
7715 if ( !m_batchCount
)
7718 m_rowLabelWin
->Refresh();
7719 m_colLabelWin
->Refresh();
7720 m_cornerLabelWin
->Refresh();
7721 m_gridWin
->Refresh();
7726 // Use this, rather than wxWindow::Refresh(), to force an immediate
7727 // repainting of the grid. Has no effect if you are already inside a
7728 // BeginBatch / EndBatch block.
7730 void wxGrid::ForceRefresh()
7736 bool wxGrid::Enable(bool enable
)
7738 if ( !wxScrolledWindow::Enable(enable
) )
7741 // redraw in the new state
7742 m_gridWin
->Refresh();
7748 // ------ Edit control functions
7751 void wxGrid::EnableEditing( bool edit
)
7753 // TODO: improve this ?
7755 if ( edit
!= m_editable
)
7758 EnableCellEditControl(edit
);
7763 void wxGrid::EnableCellEditControl( bool enable
)
7768 if ( enable
!= m_cellEditCtrlEnabled
)
7772 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7775 // this should be checked by the caller!
7776 wxASSERT_MSG( CanEnableCellControl(),
7777 _T("can't enable editing for this cell!") );
7779 // do it before ShowCellEditControl()
7780 m_cellEditCtrlEnabled
= enable
;
7782 ShowCellEditControl();
7786 //FIXME:add veto support
7787 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7789 HideCellEditControl();
7790 SaveEditControlValue();
7792 // do it after HideCellEditControl()
7793 m_cellEditCtrlEnabled
= enable
;
7798 bool wxGrid::IsCurrentCellReadOnly() const
7801 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7802 bool readonly
= attr
->IsReadOnly();
7808 bool wxGrid::CanEnableCellControl() const
7810 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7811 !IsCurrentCellReadOnly();
7815 bool wxGrid::IsCellEditControlEnabled() const
7817 // the cell edit control might be disable for all cells or just for the
7818 // current one if it's read only
7819 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7822 bool wxGrid::IsCellEditControlShown() const
7824 bool isShown
= false;
7826 if ( m_cellEditCtrlEnabled
)
7828 int row
= m_currentCellCoords
.GetRow();
7829 int col
= m_currentCellCoords
.GetCol();
7830 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7831 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7836 if ( editor
->IsCreated() )
7838 isShown
= editor
->GetControl()->IsShown();
7848 void wxGrid::ShowCellEditControl()
7850 if ( IsCellEditControlEnabled() )
7852 if ( !IsVisible( m_currentCellCoords
, false ) )
7854 m_cellEditCtrlEnabled
= false;
7859 wxRect rect
= CellToRect( m_currentCellCoords
);
7860 int row
= m_currentCellCoords
.GetRow();
7861 int col
= m_currentCellCoords
.GetCol();
7863 // if this is part of a multicell, find owner (topleft)
7864 int cell_rows
, cell_cols
;
7865 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7866 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7870 m_currentCellCoords
.SetRow( row
);
7871 m_currentCellCoords
.SetCol( col
);
7874 // convert to scrolled coords
7876 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7882 // done in PaintBackground()
7884 // erase the highlight and the cell contents because the editor
7885 // might not cover the entire cell
7886 wxClientDC
dc( m_gridWin
);
7888 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7889 dc
.SetPen(*wxTRANSPARENT_PEN
);
7890 dc
.DrawRectangle(rect
);
7893 // cell is shifted by one pixel
7894 // However, don't allow x or y to become negative
7895 // since the SetSize() method interprets that as
7902 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7903 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7904 if ( !editor
->IsCreated() )
7906 editor
->Create(m_gridWin
, wxID_ANY
,
7907 new wxGridCellEditorEvtHandler(this, editor
));
7909 wxGridEditorCreatedEvent
evt(GetId(),
7910 wxEVT_GRID_EDITOR_CREATED
,
7914 editor
->GetControl());
7915 GetEventHandler()->ProcessEvent(evt
);
7918 // resize editor to overflow into righthand cells if allowed
7919 int maxWidth
= rect
.width
;
7920 wxString value
= GetCellValue(row
, col
);
7921 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7924 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
7925 if (maxWidth
< rect
.width
)
7926 maxWidth
= rect
.width
;
7929 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7930 if (rect
.x
+ maxWidth
> client_right
)
7931 maxWidth
= client_right
- rect
.x
;
7933 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7935 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7936 // may have changed earlier
7937 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
7940 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7942 // looks weird going over a multicell
7943 if (m_table
->IsEmptyCell( row
, i
) &&
7944 (rect
.width
< maxWidth
) && (c_rows
== 1))
7946 rect
.width
+= GetColWidth( i
);
7952 if (rect
.GetRight() > client_right
)
7953 rect
.SetRight( client_right
- 1 );
7956 editor
->SetCellAttr( attr
);
7957 editor
->SetSize( rect
);
7958 editor
->GetControl()->Move(
7959 editor
->GetControl()->GetPosition().x
+ nXMove
,
7960 editor
->GetControl()->GetPosition().y
);
7961 editor
->Show( true, attr
);
7964 for (int i
= 0; i
< m_currentCellCoords
.GetCol(); i
++)
7966 colXPos
+= GetColSize( i
);
7969 int xUnit
= 1, yUnit
= 1;
7970 GetScrollPixelsPerUnit( &xUnit
, &yUnit
);
7971 if (m_currentCellCoords
.GetCol() != 0)
7972 Scroll( colXPos
/ xUnit
- 1, GetScrollPos( wxVERTICAL
) );
7974 Scroll( colXPos
/ xUnit
, GetScrollPos( wxVERTICAL
) );
7976 // recalc dimensions in case we need to
7977 // expand the scrolled window to account for editor
7980 editor
->BeginEdit(row
, col
, this);
7981 editor
->SetCellAttr(NULL
);
7989 void wxGrid::HideCellEditControl()
7991 if ( IsCellEditControlEnabled() )
7993 int row
= m_currentCellCoords
.GetRow();
7994 int col
= m_currentCellCoords
.GetCol();
7996 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7997 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7998 editor
->Show( false );
8002 m_gridWin
->SetFocus();
8004 // refresh whole row to the right
8005 wxRect
rect( CellToRect(row
, col
) );
8006 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8007 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8010 // ensure that the pixels under the focus ring get refreshed as well
8011 rect
.Inflate(10, 10);
8014 m_gridWin
->Refresh( false, &rect
);
8018 void wxGrid::SaveEditControlValue()
8020 if ( IsCellEditControlEnabled() )
8022 int row
= m_currentCellCoords
.GetRow();
8023 int col
= m_currentCellCoords
.GetCol();
8025 wxString oldval
= GetCellValue(row
, col
);
8027 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8028 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8029 bool changed
= editor
->EndEdit(row
, col
, this);
8036 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8037 m_currentCellCoords
.GetRow(),
8038 m_currentCellCoords
.GetCol() ) < 0 )
8040 // Event has been vetoed, set the data back.
8041 SetCellValue(row
, col
, oldval
);
8048 // ------ Grid location functions
8049 // Note that all of these functions work with the logical coordinates of
8050 // grid cells and labels so you will need to convert from device
8051 // coordinates for mouse events etc.
8054 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
8056 int row
= YToRow(y
);
8057 int col
= XToCol(x
);
8059 if ( row
== -1 || col
== -1 )
8061 coords
= wxGridNoCellCoords
;
8065 coords
.Set( row
, col
);
8069 // Internal Helper function for computing row or column from some
8070 // (unscrolled) coordinate value, using either
8071 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8072 // of m_rowBottoms/m_ColRights to speed up the search!
8074 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8075 const wxArrayInt
& BorderArray
, int nMax
,
8079 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8084 size_t i_max
= coord
/ defaultDist
,
8087 if (BorderArray
.IsEmpty())
8089 if ((int) i_max
< nMax
)
8091 return clipToMinMax
? nMax
- 1 : -1;
8094 if ( i_max
>= BorderArray
.GetCount())
8096 i_max
= BorderArray
.GetCount() - 1;
8100 if ( coord
>= BorderArray
[i_max
])
8104 i_max
= coord
/ minDist
;
8106 i_max
= BorderArray
.GetCount() - 1;
8109 if ( i_max
>= BorderArray
.GetCount())
8110 i_max
= BorderArray
.GetCount() - 1;
8113 if ( coord
>= BorderArray
[i_max
])
8114 return clipToMinMax
? (int)i_max
: -1;
8115 if ( coord
< BorderArray
[0] )
8118 while ( i_max
- i_min
> 0 )
8120 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8121 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8122 if (coord
>= BorderArray
[ i_max
- 1])
8126 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8127 if (coord
< BorderArray
[median
])
8136 int wxGrid::YToRow( int y
)
8138 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8139 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8142 int wxGrid::XToCol( int x
)
8144 return CoordToRowOrCol(x
, m_defaultColWidth
,
8145 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8148 // return the row number that that the y coord is near the edge of, or
8149 // -1 if not near an edge
8151 int wxGrid::YToEdgeOfRow( int y
)
8154 i
= internalYToRow(y
);
8156 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8158 // We know that we are in row i, test whether we are
8159 // close enough to lower or upper border, respectively.
8160 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8162 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8169 // return the col number that that the x coord is near the edge of, or
8170 // -1 if not near an edge
8172 int wxGrid::XToEdgeOfCol( int x
)
8175 i
= internalXToCol(x
);
8177 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8179 // We know that we are in column i, test whether we are
8180 // close enough to right or left border, respectively.
8181 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8183 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8190 wxRect
wxGrid::CellToRect( int row
, int col
)
8192 wxRect
rect( -1, -1, -1, -1 );
8194 if ( row
>= 0 && row
< m_numRows
&&
8195 col
>= 0 && col
< m_numCols
)
8197 int i
, cell_rows
, cell_cols
;
8198 rect
.width
= rect
.height
= 0;
8199 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8200 // if negative then find multicell owner
8205 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8207 rect
.x
= GetColLeft(col
);
8208 rect
.y
= GetRowTop(row
);
8209 for (i
=col
; i
< col
+ cell_cols
; i
++)
8210 rect
.width
+= GetColWidth(i
);
8211 for (i
=row
; i
< row
+ cell_rows
; i
++)
8212 rect
.height
+= GetRowHeight(i
);
8215 // if grid lines are enabled, then the area of the cell is a bit smaller
8216 if (m_gridLinesEnabled
)
8225 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8227 // get the cell rectangle in logical coords
8229 wxRect
r( CellToRect( row
, col
) );
8231 // convert to device coords
8233 int left
, top
, right
, bottom
;
8234 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8235 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8237 // check against the client area of the grid window
8239 m_gridWin
->GetClientSize( &cw
, &ch
);
8241 if ( wholeCellVisible
)
8243 // is the cell wholly visible ?
8245 return ( left
>= 0 && right
<= cw
&&
8246 top
>= 0 && bottom
<= ch
);
8250 // is the cell partly visible ?
8252 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8253 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8257 // make the specified cell location visible by doing a minimal amount
8260 void wxGrid::MakeCellVisible( int row
, int col
)
8263 int xpos
= -1, ypos
= -1;
8265 if ( row
>= 0 && row
< m_numRows
&&
8266 col
>= 0 && col
< m_numCols
)
8268 // get the cell rectangle in logical coords
8269 wxRect
r( CellToRect( row
, col
) );
8271 // convert to device coords
8272 int left
, top
, right
, bottom
;
8273 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8274 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8277 m_gridWin
->GetClientSize( &cw
, &ch
);
8283 else if ( bottom
> ch
)
8285 int h
= r
.GetHeight();
8287 for ( i
= row
-1; i
>= 0; i
-- )
8289 int rowHeight
= GetRowHeight(i
);
8290 if ( h
+ rowHeight
> ch
)
8297 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8298 // have rounding errors (this is important, because if we do, we
8299 // might not scroll at all and some cells won't be redrawn)
8301 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8303 ypos
+= m_scrollLineY
;
8306 // special handling for wide cells - show always left part of the cell!
8307 // Otherwise, e.g. when stepping from row to row, it would jump between
8308 // left and right part of the cell on every step!
8310 if ( left
< 0 || (right
- left
) >= cw
)
8314 else if ( right
> cw
)
8316 // position the view so that the cell is on the right
8318 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8319 xpos
= x0
+ (right
- cw
);
8321 // see comment for ypos above
8322 xpos
+= m_scrollLineX
;
8325 if ( xpos
!= -1 || ypos
!= -1 )
8328 xpos
/= m_scrollLineX
;
8330 ypos
/= m_scrollLineY
;
8331 Scroll( xpos
, ypos
);
8338 // ------ Grid cursor movement functions
8341 bool wxGrid::MoveCursorUp( bool expandSelection
)
8343 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8344 m_currentCellCoords
.GetRow() >= 0 )
8346 if ( expandSelection
)
8348 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8349 m_selectingKeyboard
= m_currentCellCoords
;
8350 if ( m_selectingKeyboard
.GetRow() > 0 )
8352 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8353 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8354 m_selectingKeyboard
.GetCol() );
8355 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8358 else if ( m_currentCellCoords
.GetRow() > 0 )
8360 int row
= m_currentCellCoords
.GetRow() - 1;
8361 int col
= m_currentCellCoords
.GetCol();
8363 MakeCellVisible( row
, col
);
8364 SetCurrentCell( row
, col
);
8375 bool wxGrid::MoveCursorDown( bool expandSelection
)
8377 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8378 m_currentCellCoords
.GetRow() < m_numRows
)
8380 if ( expandSelection
)
8382 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8383 m_selectingKeyboard
= m_currentCellCoords
;
8384 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8386 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8387 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8388 m_selectingKeyboard
.GetCol() );
8389 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8392 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8394 int row
= m_currentCellCoords
.GetRow() + 1;
8395 int col
= m_currentCellCoords
.GetCol();
8397 MakeCellVisible( row
, col
);
8398 SetCurrentCell( row
, col
);
8409 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8411 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8412 m_currentCellCoords
.GetCol() >= 0 )
8414 if ( expandSelection
)
8416 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8417 m_selectingKeyboard
= m_currentCellCoords
;
8418 if ( m_selectingKeyboard
.GetCol() > 0 )
8420 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8421 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8422 m_selectingKeyboard
.GetCol() );
8423 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8426 else if ( m_currentCellCoords
.GetCol() > 0 )
8428 int row
= m_currentCellCoords
.GetRow();
8429 int col
= m_currentCellCoords
.GetCol() - 1;
8431 MakeCellVisible( row
, col
);
8432 SetCurrentCell( row
, col
);
8443 bool wxGrid::MoveCursorRight( bool expandSelection
)
8445 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8446 m_currentCellCoords
.GetCol() < m_numCols
)
8448 if ( expandSelection
)
8450 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8451 m_selectingKeyboard
= m_currentCellCoords
;
8452 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8454 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8455 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8456 m_selectingKeyboard
.GetCol() );
8457 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8460 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8462 int row
= m_currentCellCoords
.GetRow();
8463 int col
= m_currentCellCoords
.GetCol() + 1;
8465 MakeCellVisible( row
, col
);
8466 SetCurrentCell( row
, col
);
8477 bool wxGrid::MovePageUp()
8479 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8482 int row
= m_currentCellCoords
.GetRow();
8486 m_gridWin
->GetClientSize( &cw
, &ch
);
8488 int y
= GetRowTop(row
);
8489 int newRow
= internalYToRow( y
- ch
+ 1 );
8491 if ( newRow
== row
)
8493 //row > 0 , so newrow can never be less than 0 here.
8497 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8498 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8506 bool wxGrid::MovePageDown()
8508 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8511 int row
= m_currentCellCoords
.GetRow();
8512 if ( (row
+ 1) < m_numRows
)
8515 m_gridWin
->GetClientSize( &cw
, &ch
);
8517 int y
= GetRowTop(row
);
8518 int newRow
= internalYToRow( y
+ ch
);
8519 if ( newRow
== row
)
8521 // row < m_numRows , so newrow can't overflow here.
8525 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8526 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8534 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8537 m_currentCellCoords
!= wxGridNoCellCoords
&&
8538 m_currentCellCoords
.GetRow() > 0 )
8540 int row
= m_currentCellCoords
.GetRow();
8541 int col
= m_currentCellCoords
.GetCol();
8543 if ( m_table
->IsEmptyCell(row
, col
) )
8545 // starting in an empty cell: find the next block of
8551 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8555 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
8557 // starting at the top of a block: find the next block
8563 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8569 // starting within a block: find the top of the block
8574 if ( m_table
->IsEmptyCell(row
, col
) )
8582 MakeCellVisible( row
, col
);
8583 if ( expandSelection
)
8585 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8586 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8591 SetCurrentCell( row
, col
);
8600 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8603 m_currentCellCoords
!= wxGridNoCellCoords
&&
8604 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8606 int row
= m_currentCellCoords
.GetRow();
8607 int col
= m_currentCellCoords
.GetCol();
8609 if ( m_table
->IsEmptyCell(row
, col
) )
8611 // starting in an empty cell: find the next block of
8614 while ( row
< m_numRows
- 1 )
8617 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8621 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
8623 // starting at the bottom of a block: find the next block
8626 while ( row
< m_numRows
- 1 )
8629 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8635 // starting within a block: find the bottom of the block
8637 while ( row
< m_numRows
- 1 )
8640 if ( m_table
->IsEmptyCell(row
, col
) )
8648 MakeCellVisible( row
, col
);
8649 if ( expandSelection
)
8651 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8652 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8657 SetCurrentCell( row
, col
);
8666 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8669 m_currentCellCoords
!= wxGridNoCellCoords
&&
8670 m_currentCellCoords
.GetCol() > 0 )
8672 int row
= m_currentCellCoords
.GetRow();
8673 int col
= m_currentCellCoords
.GetCol();
8675 if ( m_table
->IsEmptyCell(row
, col
) )
8677 // starting in an empty cell: find the next block of
8683 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8687 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
8689 // starting at the left of a block: find the next block
8695 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8701 // starting within a block: find the left of the block
8706 if ( m_table
->IsEmptyCell(row
, col
) )
8714 MakeCellVisible( row
, col
);
8715 if ( expandSelection
)
8717 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8718 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8723 SetCurrentCell( row
, col
);
8732 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8735 m_currentCellCoords
!= wxGridNoCellCoords
&&
8736 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8738 int row
= m_currentCellCoords
.GetRow();
8739 int col
= m_currentCellCoords
.GetCol();
8741 if ( m_table
->IsEmptyCell(row
, col
) )
8743 // starting in an empty cell: find the next block of
8746 while ( col
< m_numCols
- 1 )
8749 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8753 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
8755 // starting at the right of a block: find the next block
8758 while ( col
< m_numCols
- 1 )
8761 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8767 // starting within a block: find the right of the block
8769 while ( col
< m_numCols
- 1 )
8772 if ( m_table
->IsEmptyCell(row
, col
) )
8780 MakeCellVisible( row
, col
);
8781 if ( expandSelection
)
8783 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8784 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8789 SetCurrentCell( row
, col
);
8799 // ------ Label values and formatting
8802 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8805 *horiz
= m_rowLabelHorizAlign
;
8807 *vert
= m_rowLabelVertAlign
;
8810 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8813 *horiz
= m_colLabelHorizAlign
;
8815 *vert
= m_colLabelVertAlign
;
8818 int wxGrid::GetColLabelTextOrientation()
8820 return m_colLabelTextOrientation
;
8823 wxString
wxGrid::GetRowLabelValue( int row
)
8827 return m_table
->GetRowLabelValue( row
);
8837 wxString
wxGrid::GetColLabelValue( int col
)
8841 return m_table
->GetColLabelValue( col
);
8851 void wxGrid::SetRowLabelSize( int width
)
8853 width
= wxMax( width
, 0 );
8854 if ( width
!= m_rowLabelWidth
)
8858 m_rowLabelWin
->Show( false );
8859 m_cornerLabelWin
->Show( false );
8861 else if ( m_rowLabelWidth
== 0 )
8863 m_rowLabelWin
->Show( true );
8864 if ( m_colLabelHeight
> 0 )
8865 m_cornerLabelWin
->Show( true );
8868 m_rowLabelWidth
= width
;
8870 wxScrolledWindow::Refresh( true );
8874 void wxGrid::SetColLabelSize( int height
)
8876 height
= wxMax( height
, 0 );
8877 if ( height
!= m_colLabelHeight
)
8881 m_colLabelWin
->Show( false );
8882 m_cornerLabelWin
->Show( false );
8884 else if ( m_colLabelHeight
== 0 )
8886 m_colLabelWin
->Show( true );
8887 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8890 m_colLabelHeight
= height
;
8892 wxScrolledWindow::Refresh( true );
8896 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8898 if ( m_labelBackgroundColour
!= colour
)
8900 m_labelBackgroundColour
= colour
;
8901 m_rowLabelWin
->SetBackgroundColour( colour
);
8902 m_colLabelWin
->SetBackgroundColour( colour
);
8903 m_cornerLabelWin
->SetBackgroundColour( colour
);
8905 if ( !GetBatchCount() )
8907 m_rowLabelWin
->Refresh();
8908 m_colLabelWin
->Refresh();
8909 m_cornerLabelWin
->Refresh();
8914 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8916 if ( m_labelTextColour
!= colour
)
8918 m_labelTextColour
= colour
;
8919 if ( !GetBatchCount() )
8921 m_rowLabelWin
->Refresh();
8922 m_colLabelWin
->Refresh();
8927 void wxGrid::SetLabelFont( const wxFont
& font
)
8930 if ( !GetBatchCount() )
8932 m_rowLabelWin
->Refresh();
8933 m_colLabelWin
->Refresh();
8937 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8939 // allow old (incorrect) defs to be used
8942 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8943 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8944 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8949 case wxTOP
: vert
= wxALIGN_TOP
; break;
8950 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8951 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8954 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8956 m_rowLabelHorizAlign
= horiz
;
8959 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8961 m_rowLabelVertAlign
= vert
;
8964 if ( !GetBatchCount() )
8966 m_rowLabelWin
->Refresh();
8970 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8972 // allow old (incorrect) defs to be used
8975 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8976 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8977 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8982 case wxTOP
: vert
= wxALIGN_TOP
; break;
8983 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8984 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8987 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8989 m_colLabelHorizAlign
= horiz
;
8992 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8994 m_colLabelVertAlign
= vert
;
8997 if ( !GetBatchCount() )
8999 m_colLabelWin
->Refresh();
9003 // Note: under MSW, the default column label font must be changed because it
9004 // does not support vertical printing
9006 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9007 // pGrid->SetLabelFont(font);
9008 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9010 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9012 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9014 m_colLabelTextOrientation
= textOrientation
;
9017 if ( !GetBatchCount() )
9019 m_colLabelWin
->Refresh();
9023 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9027 m_table
->SetRowLabelValue( row
, s
);
9028 if ( !GetBatchCount() )
9030 wxRect rect
= CellToRect( row
, 0 );
9031 if ( rect
.height
> 0 )
9033 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9035 rect
.width
= m_rowLabelWidth
;
9036 m_rowLabelWin
->Refresh( true, &rect
);
9042 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9046 m_table
->SetColLabelValue( col
, s
);
9047 if ( !GetBatchCount() )
9049 wxRect rect
= CellToRect( 0, col
);
9050 if ( rect
.width
> 0 )
9052 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9054 rect
.height
= m_colLabelHeight
;
9055 m_colLabelWin
->Refresh( true, &rect
);
9061 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9063 if ( m_gridLineColour
!= colour
)
9065 m_gridLineColour
= colour
;
9067 wxClientDC
dc( m_gridWin
);
9069 DrawAllGridLines( dc
, wxRegion() );
9073 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9075 if ( m_cellHighlightColour
!= colour
)
9077 m_cellHighlightColour
= colour
;
9079 wxClientDC
dc( m_gridWin
);
9081 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9082 DrawCellHighlight(dc
, attr
);
9087 void wxGrid::SetCellHighlightPenWidth(int width
)
9089 if (m_cellHighlightPenWidth
!= width
)
9091 m_cellHighlightPenWidth
= width
;
9093 // Just redrawing the cell highlight is not enough since that won't
9094 // make any visible change if the the thickness is getting smaller.
9095 int row
= m_currentCellCoords
.GetRow();
9096 int col
= m_currentCellCoords
.GetCol();
9097 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9100 wxRect rect
= CellToRect(row
, col
);
9101 m_gridWin
->Refresh(true, &rect
);
9105 void wxGrid::SetCellHighlightROPenWidth(int width
)
9107 if (m_cellHighlightROPenWidth
!= width
)
9109 m_cellHighlightROPenWidth
= width
;
9111 // Just redrawing the cell highlight is not enough since that won't
9112 // make any visible change if the the thickness is getting smaller.
9113 int row
= m_currentCellCoords
.GetRow();
9114 int col
= m_currentCellCoords
.GetCol();
9115 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9118 wxRect rect
= CellToRect(row
, col
);
9119 m_gridWin
->Refresh(true, &rect
);
9123 void wxGrid::EnableGridLines( bool enable
)
9125 if ( enable
!= m_gridLinesEnabled
)
9127 m_gridLinesEnabled
= enable
;
9129 if ( !GetBatchCount() )
9133 wxClientDC
dc( m_gridWin
);
9135 DrawAllGridLines( dc
, wxRegion() );
9139 m_gridWin
->Refresh();
9145 int wxGrid::GetDefaultRowSize()
9147 return m_defaultRowHeight
;
9150 int wxGrid::GetRowSize( int row
)
9152 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9154 return GetRowHeight(row
);
9157 int wxGrid::GetDefaultColSize()
9159 return m_defaultColWidth
;
9162 int wxGrid::GetColSize( int col
)
9164 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9166 return GetColWidth(col
);
9169 // ============================================================================
9170 // access to the grid attributes: each of them has a default value in the grid
9171 // itself and may be overidden on a per-cell basis
9172 // ============================================================================
9174 // ----------------------------------------------------------------------------
9175 // setting default attributes
9176 // ----------------------------------------------------------------------------
9178 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9180 m_defaultCellAttr
->SetBackgroundColour(col
);
9182 m_gridWin
->SetBackgroundColour(col
);
9186 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9188 m_defaultCellAttr
->SetTextColour(col
);
9191 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9193 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9196 void wxGrid::SetDefaultCellOverflow( bool allow
)
9198 m_defaultCellAttr
->SetOverflow(allow
);
9201 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9203 m_defaultCellAttr
->SetFont(font
);
9206 // For editors and renderers the type registry takes precedence over the
9207 // default attr, so we need to register the new editor/renderer for the string
9208 // data type in order to make setting a default editor/renderer appear to
9211 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9213 RegisterDataType(wxGRID_VALUE_STRING
,
9215 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9218 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9220 RegisterDataType(wxGRID_VALUE_STRING
,
9221 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9225 // ----------------------------------------------------------------------------
9226 // access to the default attrbiutes
9227 // ----------------------------------------------------------------------------
9229 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9231 return m_defaultCellAttr
->GetBackgroundColour();
9234 wxColour
wxGrid::GetDefaultCellTextColour()
9236 return m_defaultCellAttr
->GetTextColour();
9239 wxFont
wxGrid::GetDefaultCellFont()
9241 return m_defaultCellAttr
->GetFont();
9244 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9246 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9249 bool wxGrid::GetDefaultCellOverflow()
9251 return m_defaultCellAttr
->GetOverflow();
9254 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9256 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9259 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9261 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9264 // ----------------------------------------------------------------------------
9265 // access to cell attributes
9266 // ----------------------------------------------------------------------------
9268 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9270 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9271 wxColour colour
= attr
->GetBackgroundColour();
9277 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9279 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9280 wxColour colour
= attr
->GetTextColour();
9286 wxFont
wxGrid::GetCellFont( int row
, int col
)
9288 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9289 wxFont font
= attr
->GetFont();
9295 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9297 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9298 attr
->GetAlignment(horiz
, vert
);
9302 bool wxGrid::GetCellOverflow( int row
, int col
)
9304 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9305 bool allow
= attr
->GetOverflow();
9311 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9313 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9314 attr
->GetSize( num_rows
, num_cols
);
9318 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9320 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9321 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9327 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9329 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9330 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9336 bool wxGrid::IsReadOnly(int row
, int col
) const
9338 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9339 bool isReadOnly
= attr
->IsReadOnly();
9345 // ----------------------------------------------------------------------------
9346 // attribute support: cache, automatic provider creation, ...
9347 // ----------------------------------------------------------------------------
9349 bool wxGrid::CanHaveAttributes()
9356 return m_table
->CanHaveAttributes();
9359 void wxGrid::ClearAttrCache()
9361 if ( m_attrCache
.row
!= -1 )
9363 wxSafeDecRef(m_attrCache
.attr
);
9364 m_attrCache
.attr
= NULL
;
9365 m_attrCache
.row
= -1;
9369 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9373 wxGrid
*self
= (wxGrid
*)this; // const_cast
9375 self
->ClearAttrCache();
9376 self
->m_attrCache
.row
= row
;
9377 self
->m_attrCache
.col
= col
;
9378 self
->m_attrCache
.attr
= attr
;
9383 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9385 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9387 *attr
= m_attrCache
.attr
;
9388 wxSafeIncRef(m_attrCache
.attr
);
9390 #ifdef DEBUG_ATTR_CACHE
9391 gs_nAttrCacheHits
++;
9398 #ifdef DEBUG_ATTR_CACHE
9399 gs_nAttrCacheMisses
++;
9406 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9408 wxGridCellAttr
*attr
= NULL
;
9409 // Additional test to avoid looking at the cache e.g. for
9410 // wxNoCellCoords, as this will confuse memory management.
9413 if ( !LookupAttr(row
, col
, &attr
) )
9415 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9416 : (wxGridCellAttr
*)NULL
;
9417 CacheAttr(row
, col
, attr
);
9423 attr
->SetDefAttr(m_defaultCellAttr
);
9427 attr
= m_defaultCellAttr
;
9434 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9436 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9437 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9439 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9440 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9442 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9445 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9447 // artificially inc the ref count to match DecRef() in caller
9449 m_table
->SetAttr(attr
, row
, col
);
9455 // ----------------------------------------------------------------------------
9456 // setting column attributes (wrappers around SetColAttr)
9457 // ----------------------------------------------------------------------------
9459 void wxGrid::SetColFormatBool(int col
)
9461 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9464 void wxGrid::SetColFormatNumber(int col
)
9466 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9469 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9471 wxString typeName
= wxGRID_VALUE_FLOAT
;
9472 if ( (width
!= -1) || (precision
!= -1) )
9474 typeName
<< _T(':') << width
<< _T(',') << precision
;
9477 SetColFormatCustom(col
, typeName
);
9480 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9482 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9484 attr
= new wxGridCellAttr
;
9485 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9486 attr
->SetRenderer(renderer
);
9488 SetColAttr(col
, attr
);
9492 // ----------------------------------------------------------------------------
9493 // setting cell attributes: this is forwarded to the table
9494 // ----------------------------------------------------------------------------
9496 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9498 if ( CanHaveAttributes() )
9500 m_table
->SetAttr(attr
, row
, col
);
9509 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9511 if ( CanHaveAttributes() )
9513 m_table
->SetRowAttr(attr
, row
);
9522 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9524 if ( CanHaveAttributes() )
9526 m_table
->SetColAttr(attr
, col
);
9535 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9537 if ( CanHaveAttributes() )
9539 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9540 attr
->SetBackgroundColour(colour
);
9545 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9547 if ( CanHaveAttributes() )
9549 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9550 attr
->SetTextColour(colour
);
9555 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9557 if ( CanHaveAttributes() )
9559 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9560 attr
->SetFont(font
);
9565 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9567 if ( CanHaveAttributes() )
9569 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9570 attr
->SetAlignment(horiz
, vert
);
9575 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9577 if ( CanHaveAttributes() )
9579 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9580 attr
->SetOverflow(allow
);
9585 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9587 if ( CanHaveAttributes() )
9589 int cell_rows
, cell_cols
;
9591 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9592 attr
->GetSize(&cell_rows
, &cell_cols
);
9593 attr
->SetSize(num_rows
, num_cols
);
9596 // Cannot set the size of a cell to 0 or negative values
9597 // While it is perfectly legal to do that, this function cannot
9598 // handle all the possibilies, do it by hand by getting the CellAttr.
9599 // You can only set the size of a cell to 1,1 or greater with this fn
9600 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9601 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9602 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9603 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9605 // if this was already a multicell then "turn off" the other cells first
9606 if ((cell_rows
> 1) || (cell_rows
> 1))
9609 for (j
=row
; j
< row
+ cell_rows
; j
++)
9611 for (i
=col
; i
< col
+ cell_cols
; i
++)
9613 if ((i
!= col
) || (j
!= row
))
9615 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9616 attr_stub
->SetSize( 1, 1 );
9617 attr_stub
->DecRef();
9623 // mark the cells that will be covered by this cell to
9624 // negative or zero values to point back at this cell
9625 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9628 for (j
=row
; j
< row
+ num_rows
; j
++)
9630 for (i
=col
; i
< col
+ num_cols
; i
++)
9632 if ((i
!= col
) || (j
!= row
))
9634 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9635 attr_stub
->SetSize( row
- j
, col
- i
);
9636 attr_stub
->DecRef();
9644 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9646 if ( CanHaveAttributes() )
9648 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9649 attr
->SetRenderer(renderer
);
9654 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9656 if ( CanHaveAttributes() )
9658 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9659 attr
->SetEditor(editor
);
9664 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9666 if ( CanHaveAttributes() )
9668 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9669 attr
->SetReadOnly(isReadOnly
);
9674 // ----------------------------------------------------------------------------
9675 // Data type registration
9676 // ----------------------------------------------------------------------------
9678 void wxGrid::RegisterDataType(const wxString
& typeName
,
9679 wxGridCellRenderer
* renderer
,
9680 wxGridCellEditor
* editor
)
9682 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9686 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9688 wxString typeName
= m_table
->GetTypeName(row
, col
);
9689 return GetDefaultEditorForType(typeName
);
9692 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9694 wxString typeName
= m_table
->GetTypeName(row
, col
);
9695 return GetDefaultRendererForType(typeName
);
9698 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9700 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9701 if ( index
== wxNOT_FOUND
)
9705 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9706 wxFAIL_MSG(errStr
.c_str());
9711 return m_typeRegistry
->GetEditor(index
);
9714 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9716 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9717 if ( index
== wxNOT_FOUND
)
9721 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9722 wxFAIL_MSG(errStr
.c_str());
9727 return m_typeRegistry
->GetRenderer(index
);
9730 // ----------------------------------------------------------------------------
9732 // ----------------------------------------------------------------------------
9734 void wxGrid::EnableDragRowSize( bool enable
)
9736 m_canDragRowSize
= enable
;
9739 void wxGrid::EnableDragColSize( bool enable
)
9741 m_canDragColSize
= enable
;
9744 void wxGrid::EnableDragGridSize( bool enable
)
9746 m_canDragGridSize
= enable
;
9749 void wxGrid::EnableDragCell( bool enable
)
9751 m_canDragCell
= enable
;
9754 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9756 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9758 if ( resizeExistingRows
)
9760 // since we are resizing all rows to the default row size,
9761 // we can simply clear the row heights and row bottoms
9762 // arrays (which also allows us to take advantage of
9763 // some speed optimisations)
9764 m_rowHeights
.Empty();
9765 m_rowBottoms
.Empty();
9766 if ( !GetBatchCount() )
9771 void wxGrid::SetRowSize( int row
, int height
)
9773 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9775 // See comment in SetColSize
9776 if ( height
< GetRowMinimalAcceptableHeight())
9779 if ( m_rowHeights
.IsEmpty() )
9781 // need to really create the array
9785 int h
= wxMax( 0, height
);
9786 int diff
= h
- m_rowHeights
[row
];
9788 m_rowHeights
[row
] = h
;
9790 for ( i
= row
; i
< m_numRows
; i
++ )
9792 m_rowBottoms
[i
] += diff
;
9795 if ( !GetBatchCount() )
9799 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9801 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9803 if ( resizeExistingCols
)
9805 // since we are resizing all columns to the default column size,
9806 // we can simply clear the col widths and col rights
9807 // arrays (which also allows us to take advantage of
9808 // some speed optimisations)
9809 m_colWidths
.Empty();
9810 m_colRights
.Empty();
9811 if ( !GetBatchCount() )
9816 void wxGrid::SetColSize( int col
, int width
)
9818 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9820 // should we check that it's bigger than GetColMinimalWidth(col) here?
9822 // No, because it is reasonable to assume the library user know's
9823 // what he is doing. However whe should test against the weaker
9824 // constraint of minimalAcceptableWidth, as this breaks rendering
9826 // This test then fixes sf.net bug #645734
9828 if ( width
< GetColMinimalAcceptableWidth() )
9831 if ( m_colWidths
.IsEmpty() )
9833 // need to really create the array
9837 // if < 0 calc new width from label
9841 wxArrayString lines
;
9842 wxClientDC
dc(m_colLabelWin
);
9843 dc
.SetFont(GetLabelFont());
9844 StringToLines(GetColLabelValue(col
), lines
);
9845 GetTextBoxSize(dc
, lines
, &w
, &h
);
9849 int w
= wxMax( 0, width
);
9850 int diff
= w
- m_colWidths
[col
];
9851 m_colWidths
[col
] = w
;
9854 for ( i
= col
; i
< m_numCols
; i
++ )
9856 m_colRights
[i
] += diff
;
9859 if ( !GetBatchCount() )
9863 void wxGrid::SetColMinimalWidth( int col
, int width
)
9865 if (width
> GetColMinimalAcceptableWidth())
9867 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9868 m_colMinWidths
[key
] = width
;
9872 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9874 if (width
> GetRowMinimalAcceptableHeight())
9876 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9877 m_rowMinHeights
[key
] = width
;
9881 int wxGrid::GetColMinimalWidth(int col
) const
9883 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9884 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9886 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9889 int wxGrid::GetRowMinimalHeight(int row
) const
9891 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9892 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9894 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9897 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9899 // We do allow a width of 0 since this gives us
9900 // an easy way to temporarily hiding columns.
9902 m_minAcceptableColWidth
= width
;
9905 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9907 // We do allow a height of 0 since this gives us
9908 // an easy way to temporarily hiding rows.
9910 m_minAcceptableRowHeight
= height
;
9913 int wxGrid::GetColMinimalAcceptableWidth() const
9915 return m_minAcceptableColWidth
;
9918 int wxGrid::GetRowMinimalAcceptableHeight() const
9920 return m_minAcceptableRowHeight
;
9923 // ----------------------------------------------------------------------------
9925 // ----------------------------------------------------------------------------
9927 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9929 wxClientDC
dc(m_gridWin
);
9931 // cancel editing of cell
9932 HideCellEditControl();
9933 SaveEditControlValue();
9935 // init both of them to avoid compiler warnings, even if we only need one
9943 wxCoord extent
, extentMax
= 0;
9944 int max
= column
? m_numRows
: m_numCols
;
9945 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9952 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9953 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
9956 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9957 extent
= column
? size
.x
: size
.y
;
9958 if ( extent
> extentMax
)
9969 // now also compare with the column label extent
9971 dc
.SetFont( GetLabelFont() );
9975 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9976 if ( GetColLabelTextOrientation() == wxVERTICAL
)
9980 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9982 extent
= column
? w
: h
;
9983 if ( extent
> extentMax
)
9990 // empty column - give default extent (notice that if extentMax is less
9991 // than default extent but != 0, it's OK)
9992 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9997 // leave some space around text
10005 SetColSize( col
, extentMax
);
10006 if ( !GetBatchCount() )
10009 m_gridWin
->GetClientSize( &cw
, &ch
);
10010 wxRect
rect ( CellToRect( 0, col
) );
10012 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10013 rect
.width
= cw
- rect
.x
;
10014 rect
.height
= m_colLabelHeight
;
10015 m_colLabelWin
->Refresh( true, &rect
);
10020 SetRowSize(row
, extentMax
);
10021 if ( !GetBatchCount() )
10024 m_gridWin
->GetClientSize( &cw
, &ch
);
10025 wxRect
rect( CellToRect( row
, 0 ) );
10027 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10028 rect
.width
= m_rowLabelWidth
;
10029 rect
.height
= ch
- rect
.y
;
10030 m_rowLabelWin
->Refresh( true, &rect
);
10037 SetColMinimalWidth(col
, extentMax
);
10039 SetRowMinimalHeight(row
, extentMax
);
10043 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10045 int width
= m_rowLabelWidth
;
10050 for ( int col
= 0; col
< m_numCols
; col
++ )
10054 AutoSizeColumn(col
, setAsMin
);
10057 width
+= GetColWidth(col
);
10066 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10068 int height
= m_colLabelHeight
;
10073 for ( int row
= 0; row
< m_numRows
; row
++ )
10076 AutoSizeRow(row
, setAsMin
);
10078 height
+= GetRowHeight(row
);
10087 void wxGrid::AutoSize()
10091 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
10093 // round up the size to a multiple of scroll step - this ensures that we
10094 // won't get the scrollbars if we're sized exactly to this width
10095 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10098 GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
10099 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10101 // distribute the extra space between the columns/rows to avoid having
10102 // extra white space
10104 // Remove the extra m_extraWidth + 1 added above
10105 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10106 if ( diff
&& m_numCols
)
10108 // try to resize the columns uniformly
10109 wxCoord diffPerCol
= diff
/ m_numCols
;
10112 for ( int col
= 0; col
< m_numCols
; col
++ )
10114 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10118 // add remaining amount to the last columns
10119 diff
-= diffPerCol
* m_numCols
;
10122 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10124 SetColSize(col
, GetColWidth(col
) + 1);
10130 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10131 if ( diff
&& m_numRows
)
10133 // try to resize the columns uniformly
10134 wxCoord diffPerRow
= diff
/ m_numRows
;
10137 for ( int row
= 0; row
< m_numRows
; row
++ )
10139 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10143 // add remaining amount to the last rows
10144 diff
-= diffPerRow
* m_numRows
;
10147 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10149 SetRowSize(row
, GetRowHeight(row
) + 1);
10156 SetClientSize(sizeFit
);
10159 void wxGrid::AutoSizeRowLabelSize( int row
)
10161 wxArrayString lines
;
10164 // Hide the edit control, so it
10165 // won't interfere with drag-shrinking.
10166 if ( IsCellEditControlShown() )
10168 HideCellEditControl();
10169 SaveEditControlValue();
10172 // autosize row height depending on label text
10173 StringToLines( GetRowLabelValue( row
), lines
);
10174 wxClientDC
dc( m_rowLabelWin
);
10175 GetTextBoxSize( dc
, lines
, &w
, &h
);
10176 if ( h
< m_defaultRowHeight
)
10177 h
= m_defaultRowHeight
;
10178 SetRowSize(row
, h
);
10182 void wxGrid::AutoSizeColLabelSize( int col
)
10184 wxArrayString lines
;
10187 // Hide the edit control, so it
10188 // won't interfer with drag-shrinking.
10189 if ( IsCellEditControlShown() )
10191 HideCellEditControl();
10192 SaveEditControlValue();
10195 // autosize column width depending on label text
10196 StringToLines( GetColLabelValue( col
), lines
);
10197 wxClientDC
dc( m_colLabelWin
);
10198 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10199 GetTextBoxSize( dc
, lines
, &w
, &h
);
10201 GetTextBoxSize( dc
, lines
, &h
, &w
);
10202 if ( w
< m_defaultColWidth
)
10203 w
= m_defaultColWidth
;
10204 SetColSize(col
, w
);
10208 wxSize
wxGrid::DoGetBestSize() const
10210 // don't set sizes, only calculate them
10211 wxGrid
*self
= (wxGrid
*)this; // const_cast
10214 width
= self
->SetOrCalcColumnSizes(true);
10215 height
= self
->SetOrCalcRowSizes(true);
10222 // Round up to a multiple the scroll rate
10223 // NOTE: this still doesn't get rid of the scrollbars;
10224 // is there any magic incantation for that?
10226 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10228 width
+= 1 + xpu
- (width
% xpu
);
10230 height
+= 1 + ypu
- (height
% ypu
);
10232 // limit to 1/4 of the screen size
10233 int maxwidth
, maxheight
;
10234 wxDisplaySize( &maxwidth
, &maxheight
);
10237 if ( width
> maxwidth
)
10239 if ( height
> maxheight
)
10240 height
= maxheight
;
10242 wxSize
best(width
, height
);
10244 // NOTE: This size should be cached, but first we need to add calls to
10245 // InvalidateBestSize everywhere that could change the results of this
10247 // CacheBestSize(size);
10257 wxPen
& wxGrid::GetDividerPen() const
10262 // ----------------------------------------------------------------------------
10263 // cell value accessor functions
10264 // ----------------------------------------------------------------------------
10266 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10270 m_table
->SetValue( row
, col
, s
);
10271 if ( !GetBatchCount() )
10274 wxRect
rect( CellToRect( row
, col
) );
10276 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10277 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10278 m_gridWin
->Refresh( false, &rect
);
10281 if ( m_currentCellCoords
.GetRow() == row
&&
10282 m_currentCellCoords
.GetCol() == col
&&
10283 IsCellEditControlShown())
10284 // Note: If we are using IsCellEditControlEnabled,
10285 // this interacts badly with calling SetCellValue from
10286 // an EVT_GRID_CELL_CHANGE handler.
10288 HideCellEditControl();
10289 ShowCellEditControl(); // will reread data from table
10294 // ----------------------------------------------------------------------------
10295 // block, row and column selection
10296 // ----------------------------------------------------------------------------
10298 void wxGrid::SelectRow( int row
, bool addToSelected
)
10300 if ( IsSelection() && !addToSelected
)
10304 m_selection
->SelectRow( row
, false, addToSelected
);
10307 void wxGrid::SelectCol( int col
, bool addToSelected
)
10309 if ( IsSelection() && !addToSelected
)
10313 m_selection
->SelectCol( col
, false, addToSelected
);
10316 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10317 bool addToSelected
)
10319 if ( IsSelection() && !addToSelected
)
10323 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10324 false, addToSelected
);
10327 void wxGrid::SelectAll()
10329 if ( m_numRows
> 0 && m_numCols
> 0 )
10332 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10336 // ----------------------------------------------------------------------------
10337 // cell, row and col deselection
10338 // ----------------------------------------------------------------------------
10340 void wxGrid::DeselectRow( int row
)
10342 if ( !m_selection
)
10345 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10347 if ( m_selection
->IsInSelection(row
, 0 ) )
10348 m_selection
->ToggleCellSelection(row
, 0);
10352 int nCols
= GetNumberCols();
10353 for ( int i
= 0; i
< nCols
; i
++ )
10355 if ( m_selection
->IsInSelection(row
, i
) )
10356 m_selection
->ToggleCellSelection(row
, i
);
10361 void wxGrid::DeselectCol( int col
)
10363 if ( !m_selection
)
10366 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10368 if ( m_selection
->IsInSelection(0, col
) )
10369 m_selection
->ToggleCellSelection(0, col
);
10373 int nRows
= GetNumberRows();
10374 for ( int i
= 0; i
< nRows
; i
++ )
10376 if ( m_selection
->IsInSelection(i
, col
) )
10377 m_selection
->ToggleCellSelection(i
, col
);
10382 void wxGrid::DeselectCell( int row
, int col
)
10384 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10385 m_selection
->ToggleCellSelection(row
, col
);
10388 bool wxGrid::IsSelection()
10390 return ( m_selection
&& (m_selection
->IsSelection() ||
10391 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10392 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10395 bool wxGrid::IsInSelection( int row
, int col
) const
10397 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10398 ( row
>= m_selectingTopLeft
.GetRow() &&
10399 col
>= m_selectingTopLeft
.GetCol() &&
10400 row
<= m_selectingBottomRight
.GetRow() &&
10401 col
<= m_selectingBottomRight
.GetCol() )) );
10404 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10408 wxGridCellCoordsArray a
;
10412 return m_selection
->m_cellSelection
;
10415 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10419 wxGridCellCoordsArray a
;
10423 return m_selection
->m_blockSelectionTopLeft
;
10426 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10430 wxGridCellCoordsArray a
;
10434 return m_selection
->m_blockSelectionBottomRight
;
10437 wxArrayInt
wxGrid::GetSelectedRows() const
10445 return m_selection
->m_rowSelection
;
10448 wxArrayInt
wxGrid::GetSelectedCols() const
10456 return m_selection
->m_colSelection
;
10459 void wxGrid::ClearSelection()
10461 m_selectingTopLeft
= wxGridNoCellCoords
;
10462 m_selectingBottomRight
= wxGridNoCellCoords
;
10464 m_selection
->ClearSelection();
10467 // This function returns the rectangle that encloses the given block
10468 // in device coords clipped to the client size of the grid window.
10470 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10471 const wxGridCellCoords
&bottomRight
)
10473 wxRect
rect( wxGridNoCellRect
);
10476 cellRect
= CellToRect( topLeft
);
10477 if ( cellRect
!= wxGridNoCellRect
)
10483 rect
= wxRect(0, 0, 0, 0);
10486 cellRect
= CellToRect( bottomRight
);
10487 if ( cellRect
!= wxGridNoCellRect
)
10493 return wxGridNoCellRect
;
10497 int left
= rect
.GetLeft();
10498 int top
= rect
.GetTop();
10499 int right
= rect
.GetRight();
10500 int bottom
= rect
.GetBottom();
10502 int leftCol
= topLeft
.GetCol();
10503 int topRow
= topLeft
.GetRow();
10504 int rightCol
= bottomRight
.GetCol();
10505 int bottomRow
= bottomRight
.GetRow();
10513 leftCol
= rightCol
;
10523 topRow
= bottomRow
;
10527 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10529 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10531 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10533 cellRect
= CellToRect( j
, i
);
10535 if (cellRect
.x
< left
)
10537 if (cellRect
.y
< top
)
10539 if (cellRect
.x
+ cellRect
.width
> right
)
10540 right
= cellRect
.x
+ cellRect
.width
;
10541 if (cellRect
.y
+ cellRect
.height
> bottom
)
10542 bottom
= cellRect
.y
+ cellRect
.height
;
10546 i
= rightCol
; // jump over inner cells.
10551 // convert to scrolled coords
10553 CalcScrolledPosition( left
, top
, &left
, &top
);
10554 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10557 m_gridWin
->GetClientSize( &cw
, &ch
);
10559 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10560 return wxRect(0,0,0,0);
10562 rect
.SetLeft( wxMax(0, left
) );
10563 rect
.SetTop( wxMax(0, top
) );
10564 rect
.SetRight( wxMin(cw
, right
) );
10565 rect
.SetBottom( wxMin(ch
, bottom
) );
10570 // ----------------------------------------------------------------------------
10571 // grid event classes
10572 // ----------------------------------------------------------------------------
10574 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10576 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10577 int row
, int col
, int x
, int y
, bool sel
,
10578 bool control
, bool shift
, bool alt
, bool meta
)
10579 : wxNotifyEvent( type
, id
)
10586 m_control
= control
;
10591 SetEventObject(obj
);
10595 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10597 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10598 int rowOrCol
, int x
, int y
,
10599 bool control
, bool shift
, bool alt
, bool meta
)
10600 : wxNotifyEvent( type
, id
)
10602 m_rowOrCol
= rowOrCol
;
10605 m_control
= control
;
10610 SetEventObject(obj
);
10614 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10616 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10617 const wxGridCellCoords
& topLeft
,
10618 const wxGridCellCoords
& bottomRight
,
10619 bool sel
, bool control
,
10620 bool shift
, bool alt
, bool meta
)
10621 : wxNotifyEvent( type
, id
)
10623 m_topLeft
= topLeft
;
10624 m_bottomRight
= bottomRight
;
10626 m_control
= control
;
10631 SetEventObject(obj
);
10635 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10637 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10638 wxObject
* obj
, int row
,
10639 int col
, wxControl
* ctrl
)
10640 : wxCommandEvent(type
, id
)
10642 SetEventObject(obj
);
10648 #endif // wxUSE_GRID