1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
26 #include "wx/settings.h"
28 #include "wx/textctrl.h"
29 #include "wx/checkbox.h"
30 #include "wx/combobox.h"
31 #include "wx/valtext.h"
34 #include "wx/listbox.h"
37 #include "wx/textfile.h"
38 #include "wx/spinctrl.h"
39 #include "wx/tokenzr.h"
40 #include "wx/renderer.h"
42 #include "wx/generic/gridsel.h"
44 const wxChar wxGridNameStr
[] = wxT("grid");
46 #if defined(__WXMOTIF__)
47 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
49 #define WXUNUSED_MOTIF(identifier) identifier
52 #if defined(__WXGTK__)
53 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
55 #define WXUNUSED_GTK(identifier) identifier
58 // Required for wxIs... functions
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
66 class WXDLLIMPEXP_ADV
);
68 struct wxGridCellWithAttr
70 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
71 : coords(row
, col
), attr(attr_
)
80 wxGridCellCoords coords
;
84 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
85 // without rewriting the macros, which require a public copy constructor.
88 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
89 class WXDLLIMPEXP_ADV
);
91 #include "wx/arrimpl.cpp"
93 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
94 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
101 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
102 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
103 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_MOVE
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 // common base class for various grid subwindows
124 class WXDLLIMPEXP_ADV wxGridSubwindow
: public wxWindow
127 wxGridSubwindow() { m_owner
= NULL
; }
128 wxGridSubwindow(wxGrid
*owner
,
132 int additionalStyle
= 0,
133 const wxString
& name
= wxPanelNameStr
)
134 : wxWindow(owner
, id
, pos
, size
,
135 wxWANTS_CHARS
| wxBORDER_NONE
| additionalStyle
,
141 wxGrid
*GetOwner() { return m_owner
; }
144 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
148 DECLARE_EVENT_TABLE()
149 DECLARE_NO_COPY_CLASS(wxGridSubwindow
)
152 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxGridSubwindow
155 wxGridRowLabelWindow() { }
156 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
157 const wxPoint
&pos
, const wxSize
&size
);
160 void OnPaint( wxPaintEvent
& event
);
161 void OnMouseEvent( wxMouseEvent
& event
);
162 void OnMouseWheel( wxMouseEvent
& event
);
163 void OnKeyDown( wxKeyEvent
& event
);
164 void OnKeyUp( wxKeyEvent
& );
165 void OnChar( wxKeyEvent
& );
167 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
168 DECLARE_EVENT_TABLE()
169 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
173 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxGridSubwindow
176 wxGridColLabelWindow() { }
177 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
178 const wxPoint
&pos
, const wxSize
&size
);
181 void OnPaint( wxPaintEvent
& event
);
182 void OnMouseEvent( wxMouseEvent
& event
);
183 void OnMouseWheel( wxMouseEvent
& event
);
184 void OnKeyDown( wxKeyEvent
& event
);
185 void OnKeyUp( wxKeyEvent
& );
186 void OnChar( wxKeyEvent
& );
188 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
189 DECLARE_EVENT_TABLE()
190 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
194 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxGridSubwindow
197 wxGridCornerLabelWindow() { }
198 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
199 const wxPoint
&pos
, const wxSize
&size
);
202 void OnMouseEvent( wxMouseEvent
& event
);
203 void OnMouseWheel( wxMouseEvent
& event
);
204 void OnKeyDown( wxKeyEvent
& event
);
205 void OnKeyUp( wxKeyEvent
& );
206 void OnChar( wxKeyEvent
& );
207 void OnPaint( wxPaintEvent
& event
);
209 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
210 DECLARE_EVENT_TABLE()
211 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
214 class WXDLLIMPEXP_ADV wxGridWindow
: public wxGridSubwindow
219 m_rowLabelWin
= NULL
;
220 m_colLabelWin
= NULL
;
223 wxGridWindow( wxGrid
*parent
,
224 wxGridRowLabelWindow
*rowLblWin
,
225 wxGridColLabelWindow
*colLblWin
,
226 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
228 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
231 wxGridRowLabelWindow
*m_rowLabelWin
;
232 wxGridColLabelWindow
*m_colLabelWin
;
234 void OnPaint( wxPaintEvent
&event
);
235 void OnMouseWheel( wxMouseEvent
& event
);
236 void OnMouseEvent( wxMouseEvent
& event
);
237 void OnKeyDown( wxKeyEvent
& );
238 void OnKeyUp( wxKeyEvent
& );
239 void OnChar( wxKeyEvent
& );
240 void OnEraseBackground( wxEraseEvent
& );
241 void OnFocus( wxFocusEvent
& );
243 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
244 DECLARE_EVENT_TABLE()
245 DECLARE_NO_COPY_CLASS(wxGridWindow
)
249 class wxGridCellEditorEvtHandler
: public wxEvtHandler
252 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
259 void OnKillFocus(wxFocusEvent
& event
);
260 void OnKeyDown(wxKeyEvent
& event
);
261 void OnChar(wxKeyEvent
& event
);
263 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
267 wxGridCellEditor
*m_editor
;
269 // Work around the fact that a focus kill event can be sent to
270 // a combobox within a set focus event.
273 DECLARE_EVENT_TABLE()
274 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
275 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
279 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
281 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
282 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
283 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
284 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
288 // ----------------------------------------------------------------------------
289 // the internal data representation used by wxGridCellAttrProvider
290 // ----------------------------------------------------------------------------
292 // this class stores attributes set for cells
293 class WXDLLIMPEXP_ADV wxGridCellAttrData
296 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
297 wxGridCellAttr
*GetAttr(int row
, int col
) const;
298 void UpdateAttrRows( size_t pos
, int numRows
);
299 void UpdateAttrCols( size_t pos
, int numCols
);
302 // searches for the attr for given cell, returns wxNOT_FOUND if not found
303 int FindIndex(int row
, int col
) const;
305 wxGridCellWithAttrArray m_attrs
;
308 // this class stores attributes set for rows or columns
309 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
312 // empty ctor to suppress warnings
313 wxGridRowOrColAttrData() {}
314 ~wxGridRowOrColAttrData();
316 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
317 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
318 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
321 wxArrayInt m_rowsOrCols
;
322 wxArrayAttrs m_attrs
;
325 // NB: this is just a wrapper around 3 objects: one which stores cell
326 // attributes, and 2 others for row/col ones
327 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
330 wxGridCellAttrData m_cellAttrs
;
331 wxGridRowOrColAttrData m_rowAttrs
,
336 // ----------------------------------------------------------------------------
337 // data structures used for the data type registry
338 // ----------------------------------------------------------------------------
340 struct wxGridDataTypeInfo
342 wxGridDataTypeInfo(const wxString
& typeName
,
343 wxGridCellRenderer
* renderer
,
344 wxGridCellEditor
* editor
)
345 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
348 ~wxGridDataTypeInfo()
350 wxSafeDecRef(m_renderer
);
351 wxSafeDecRef(m_editor
);
355 wxGridCellRenderer
* m_renderer
;
356 wxGridCellEditor
* m_editor
;
358 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
362 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
363 class WXDLLIMPEXP_ADV
);
366 class WXDLLIMPEXP_ADV wxGridTypeRegistry
369 wxGridTypeRegistry() {}
370 ~wxGridTypeRegistry();
372 void RegisterDataType(const wxString
& typeName
,
373 wxGridCellRenderer
* renderer
,
374 wxGridCellEditor
* editor
);
376 // find one of already registered data types
377 int FindRegisteredDataType(const wxString
& typeName
);
379 // try to FindRegisteredDataType(), if this fails and typeName is one of
380 // standard typenames, register it and return its index
381 int FindDataType(const wxString
& typeName
);
383 // try to FindDataType(), if it fails see if it is not one of already
384 // registered data types with some params in which case clone the
385 // registered data type and set params for it
386 int FindOrCloneDataType(const wxString
& typeName
);
388 wxGridCellRenderer
* GetRenderer(int index
);
389 wxGridCellEditor
* GetEditor(int index
);
392 wxGridDataTypeInfoArray m_typeinfo
;
396 // ----------------------------------------------------------------------------
397 // conditional compilation
398 // ----------------------------------------------------------------------------
400 #ifndef WXGRID_DRAW_LINES
401 #define WXGRID_DRAW_LINES 1
404 // ----------------------------------------------------------------------------
406 // ----------------------------------------------------------------------------
408 //#define DEBUG_ATTR_CACHE
409 #ifdef DEBUG_ATTR_CACHE
410 static size_t gs_nAttrCacheHits
= 0;
411 static size_t gs_nAttrCacheMisses
= 0;
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
418 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
419 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
422 // TODO: this doesn't work at all, grid cells have different sizes and approx
423 // calculations don't work as because of the size mismatch scrollbars
424 // sometimes fail to be shown when they should be or vice versa
426 // The scroll bars may be a little flakey once in a while, but that is
427 // surely much less horrible than having scroll lines of only 1!!!
430 // Well, it's still seriously broken so it might be better but needs
433 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
434 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
436 // the size of hash tables used a bit everywhere (the max number of elements
437 // in these hash tables is the number of rows/columns)
438 static const int GRID_HASH_SIZE
= 100;
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 static inline int GetScrollX(int x
)
447 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
450 static inline int GetScrollY(int y
)
452 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
456 // ============================================================================
458 // ============================================================================
460 // ----------------------------------------------------------------------------
462 // ----------------------------------------------------------------------------
464 wxGridCellEditor::wxGridCellEditor()
470 wxGridCellEditor::~wxGridCellEditor()
475 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
476 wxWindowID
WXUNUSED(id
),
477 wxEvtHandler
* evtHandler
)
480 m_control
->PushEventHandler(evtHandler
);
483 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
484 wxGridCellAttr
*attr
)
486 // erase the background because we might not fill the cell
487 wxClientDC
dc(m_control
->GetParent());
488 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
490 gridWindow
->GetOwner()->PrepareDC(dc
);
492 dc
.SetPen(*wxTRANSPARENT_PEN
);
493 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
494 dc
.DrawRectangle(rectCell
);
496 // redraw the control we just painted over
497 m_control
->Refresh();
500 void wxGridCellEditor::Destroy()
504 m_control
->PopEventHandler( true /* delete it*/ );
506 m_control
->Destroy();
511 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
513 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
515 m_control
->Show(show
);
519 // set the colours/fonts if we have any
522 m_colFgOld
= m_control
->GetForegroundColour();
523 m_control
->SetForegroundColour(attr
->GetTextColour());
525 m_colBgOld
= m_control
->GetBackgroundColour();
526 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
528 // Workaround for GTK+1 font setting problem on some platforms
529 #if !defined(__WXGTK__) || defined(__WXGTK20__)
530 m_fontOld
= m_control
->GetFont();
531 m_control
->SetFont(attr
->GetFont());
534 // can't do anything more in the base class version, the other
535 // attributes may only be used by the derived classes
540 // restore the standard colours fonts
541 if ( m_colFgOld
.Ok() )
543 m_control
->SetForegroundColour(m_colFgOld
);
544 m_colFgOld
= wxNullColour
;
547 if ( m_colBgOld
.Ok() )
549 m_control
->SetBackgroundColour(m_colBgOld
);
550 m_colBgOld
= wxNullColour
;
553 // Workaround for GTK+1 font setting problem on some platforms
554 #if !defined(__WXGTK__) || defined(__WXGTK20__)
555 if ( m_fontOld
.Ok() )
557 m_control
->SetFont(m_fontOld
);
558 m_fontOld
= wxNullFont
;
564 void wxGridCellEditor::SetSize(const wxRect
& rect
)
566 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
568 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
571 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
576 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
578 bool ctrl
= event
.ControlDown();
579 bool alt
= event
.AltDown();
582 // On the Mac the Alt key is more like shift and is used for entry of
583 // valid characters, so check for Ctrl and Meta instead.
584 alt
= event
.MetaDown();
587 // Assume it's not a valid char if ctrl or alt is down, but if both are
588 // down then it may be because of an AltGr key combination, so let them
589 // through in that case.
590 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
597 // If it's a F-Key or other special key then it shouldn't start the
599 if (event
.GetKeyCode() >= WXK_START
)
603 // if the unicode key code is not really a unicode character (it may
604 // be a function key or etc., the platforms appear to always give us a
605 // small value in this case) then fallback to the ASCII key code but
606 // don't do anything for function keys or etc.
607 key
= event
.GetUnicodeKey();
610 key
= event
.GetKeyCode();
611 keyOk
= (key
<= 127);
614 key
= event
.GetKeyCode();
615 keyOk
= (key
<= 255);
621 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
626 void wxGridCellEditor::StartingClick()
632 // ----------------------------------------------------------------------------
633 // wxGridCellTextEditor
634 // ----------------------------------------------------------------------------
636 wxGridCellTextEditor::wxGridCellTextEditor()
641 void wxGridCellTextEditor::Create(wxWindow
* parent
,
643 wxEvtHandler
* evtHandler
)
645 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
646 wxDefaultPosition
, wxDefaultSize
647 #if defined(__WXMSW__)
656 // set max length allowed in the textctrl, if the parameter was set
659 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
662 wxGridCellEditor::Create(parent
, id
, evtHandler
);
665 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
666 wxGridCellAttr
* WXUNUSED(attr
))
668 // as we fill the entire client area,
669 // don't do anything here to minimize flicker
672 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
674 wxRect
rect(rectOrig
);
676 // Make the edit control large enough to allow for internal margins
678 // TODO: remove this if the text ctrl sizing is improved esp. for unix
680 #if defined(__WXGTK__)
688 #elif defined(__WXMSW__)
702 int extra_x
= ( rect
.x
> 2 ) ? 2 : 1;
703 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
705 #if defined(__WXMOTIF__)
710 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
711 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
712 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
713 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
716 wxGridCellEditor::SetSize(rect
);
719 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
721 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
723 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
725 DoBeginEdit(m_startValue
);
728 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
730 Text()->SetValue(startValue
);
731 Text()->SetInsertionPointEnd();
732 Text()->SetSelection(-1, -1);
736 bool wxGridCellTextEditor::EndEdit(int row
, int col
, wxGrid
* grid
)
738 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
740 bool changed
= false;
741 wxString value
= Text()->GetValue();
742 if (value
!= m_startValue
)
746 grid
->GetTable()->SetValue(row
, col
, value
);
748 m_startValue
= wxEmptyString
;
750 // No point in setting the text of the hidden control
751 //Text()->SetValue(m_startValue);
756 void wxGridCellTextEditor::Reset()
758 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
760 DoReset(m_startValue
);
763 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
765 Text()->SetValue(startValue
);
766 Text()->SetInsertionPointEnd();
769 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
771 return wxGridCellEditor::IsAcceptedKey(event
);
774 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
776 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
777 // longer an appropriate way to get the character into the text control.
778 // Do it ourselves instead. We know that if we get this far that we have
779 // a valid character, so not a whole lot of testing needs to be done.
781 wxTextCtrl
* tc
= Text();
786 ch
= event
.GetUnicodeKey();
788 ch
= (wxChar
)event
.GetKeyCode();
790 ch
= (wxChar
)event
.GetKeyCode();
796 // delete the character at the cursor
797 pos
= tc
->GetInsertionPoint();
798 if (pos
< tc
->GetLastPosition())
799 tc
->Remove(pos
, pos
+ 1);
803 // delete the character before the cursor
804 pos
= tc
->GetInsertionPoint();
806 tc
->Remove(pos
- 1, pos
);
815 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
816 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
818 #if defined(__WXMOTIF__) || defined(__WXGTK__)
819 // wxMotif needs a little extra help...
820 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
821 wxString
s( Text()->GetValue() );
822 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
824 Text()->SetInsertionPoint( pos
);
826 // the other ports can handle a Return key press
832 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
842 if ( params
.ToLong(&tmp
) )
844 m_maxChars
= (size_t)tmp
;
848 wxLogDebug( _T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str() );
853 // return the value in the text control
854 wxString
wxGridCellTextEditor::GetValue() const
856 return Text()->GetValue();
859 // ----------------------------------------------------------------------------
860 // wxGridCellNumberEditor
861 // ----------------------------------------------------------------------------
863 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
869 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
871 wxEvtHandler
* evtHandler
)
876 // create a spin ctrl
877 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
878 wxDefaultPosition
, wxDefaultSize
,
882 wxGridCellEditor::Create(parent
, id
, evtHandler
);
887 // just a text control
888 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
891 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
896 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
898 // first get the value
899 wxGridTableBase
*table
= grid
->GetTable();
900 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
902 m_valueOld
= table
->GetValueAsLong(row
, col
);
907 wxString sValue
= table
->GetValue(row
, col
);
908 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
910 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
918 Spin()->SetValue((int)m_valueOld
);
924 DoBeginEdit(GetString());
928 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
938 value
= Spin()->GetValue();
939 changed
= value
!= m_valueOld
;
941 text
= wxString::Format(wxT("%ld"), value
);
946 text
= Text()->GetValue();
947 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
952 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
953 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
955 grid
->GetTable()->SetValue(row
, col
, text
);
961 void wxGridCellNumberEditor::Reset()
966 Spin()->SetValue((int)m_valueOld
);
971 DoReset(GetString());
975 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
977 if ( wxGridCellEditor::IsAcceptedKey(event
) )
979 int keycode
= event
.GetKeyCode();
980 if ( (keycode
< 128) &&
981 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
990 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
992 int keycode
= event
.GetKeyCode();
995 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
997 wxGridCellTextEditor::StartingKey(event
);
1006 if ( wxIsdigit(keycode
) )
1008 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
1009 spin
->SetValue(keycode
- '0');
1010 spin
->SetSelection(1,1);
1019 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1030 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1034 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1038 // skip the error message below
1043 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1047 // return the value in the spin control if it is there (the text control otherwise)
1048 wxString
wxGridCellNumberEditor::GetValue() const
1055 long value
= Spin()->GetValue();
1056 s
.Printf(wxT("%ld"), value
);
1061 s
= Text()->GetValue();
1067 // ----------------------------------------------------------------------------
1068 // wxGridCellFloatEditor
1069 // ----------------------------------------------------------------------------
1071 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1074 m_precision
= precision
;
1077 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1079 wxEvtHandler
* evtHandler
)
1081 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1083 #if wxUSE_VALIDATORS
1084 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1088 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1090 // first get the value
1091 wxGridTableBase
*table
= grid
->GetTable();
1092 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1094 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1099 wxString sValue
= table
->GetValue(row
, col
);
1100 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1102 wxFAIL_MSG( _T("this cell doesn't have float value") );
1107 DoBeginEdit(GetString());
1110 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1114 wxString
text(Text()->GetValue());
1116 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1117 !wxIsSameDouble(value
, m_valueOld
) )
1119 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1120 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1122 grid
->GetTable()->SetValue(row
, col
, text
);
1130 void wxGridCellFloatEditor::Reset()
1132 DoReset(GetString());
1135 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1137 int keycode
= event
.GetKeyCode();
1139 tmpbuf
[0] = (char) keycode
;
1141 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1144 bool is_decimal_point
= ( strbuf
==
1145 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1147 bool is_decimal_point
= ( strbuf
== _T(".") );
1150 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1151 || is_decimal_point
)
1153 wxGridCellTextEditor::StartingKey(event
);
1155 // skip Skip() below
1162 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1173 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1177 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1179 m_precision
= (int)tmp
;
1181 // skip the error message below
1186 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1190 wxString
wxGridCellFloatEditor::GetString() const
1193 if ( m_precision
== -1 && m_width
!= -1)
1195 // default precision
1196 fmt
.Printf(_T("%%%d.f"), m_width
);
1198 else if ( m_precision
!= -1 && m_width
== -1)
1201 fmt
.Printf(_T("%%.%df"), m_precision
);
1203 else if ( m_precision
!= -1 && m_width
!= -1 )
1205 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1209 // default width/precision
1213 return wxString::Format(fmt
, m_valueOld
);
1216 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1218 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1220 const int keycode
= event
.GetKeyCode();
1221 if ( isascii(keycode
) )
1224 tmpbuf
[0] = (char) keycode
;
1226 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1229 const wxString decimalPoint
=
1230 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
);
1232 const wxString
decimalPoint(_T('.'));
1235 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1236 if ( wxIsdigit(keycode
) ||
1237 tolower(keycode
) == 'e' ||
1238 keycode
== decimalPoint
||
1250 #endif // wxUSE_TEXTCTRL
1254 // ----------------------------------------------------------------------------
1255 // wxGridCellBoolEditor
1256 // ----------------------------------------------------------------------------
1258 // the default values for GetValue()
1259 wxString
wxGridCellBoolEditor::ms_stringValues
[2] = { _T(""), _T("1") };
1261 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1263 wxEvtHandler
* evtHandler
)
1265 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1266 wxDefaultPosition
, wxDefaultSize
,
1269 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1272 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1274 bool resize
= false;
1275 wxSize size
= m_control
->GetSize();
1276 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1278 // check if the checkbox is not too big/small for this cell
1279 wxSize sizeBest
= m_control
->GetBestSize();
1280 if ( !(size
== sizeBest
) )
1282 // reset to default size if it had been made smaller
1288 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1290 // leave 1 pixel margin
1291 size
.x
= size
.y
= minSize
- 2;
1298 m_control
->SetSize(size
);
1301 // position it in the centre of the rectangle (TODO: support alignment?)
1303 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1304 // the checkbox without label still has some space to the right in wxGTK,
1305 // so shift it to the right
1307 #elif defined(__WXMSW__)
1308 // here too, but in other way
1313 int hAlign
= wxALIGN_CENTRE
;
1314 int vAlign
= wxALIGN_CENTRE
;
1316 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1319 if (hAlign
== wxALIGN_LEFT
)
1327 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1329 else if (hAlign
== wxALIGN_RIGHT
)
1331 x
= r
.x
+ r
.width
- size
.x
- 2;
1332 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1334 else if (hAlign
== wxALIGN_CENTRE
)
1336 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1337 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1340 m_control
->Move(x
, y
);
1343 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1345 m_control
->Show(show
);
1349 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1350 CBox()->SetBackgroundColour(colBg
);
1354 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1356 wxASSERT_MSG(m_control
,
1357 wxT("The wxGridCellEditor must be created first!"));
1359 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1361 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1365 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1367 if ( cellval
== ms_stringValues
[false] )
1368 m_startValue
= false;
1369 else if ( cellval
== ms_stringValues
[true] )
1370 m_startValue
= true;
1373 // do not try to be smart here and convert it to true or false
1374 // because we'll still overwrite it with something different and
1375 // this risks to be very surprising for the user code, let them
1377 wxFAIL_MSG( _T("invalid value for a cell with bool editor!") );
1381 CBox()->SetValue(m_startValue
);
1385 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1388 wxASSERT_MSG(m_control
,
1389 wxT("The wxGridCellEditor must be created first!"));
1391 bool changed
= false;
1392 bool value
= CBox()->GetValue();
1393 if ( value
!= m_startValue
)
1398 wxGridTableBase
* const table
= grid
->GetTable();
1399 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1400 table
->SetValueAsBool(row
, col
, value
);
1402 table
->SetValue(row
, col
, GetValue());
1408 void wxGridCellBoolEditor::Reset()
1410 wxASSERT_MSG(m_control
,
1411 wxT("The wxGridCellEditor must be created first!"));
1413 CBox()->SetValue(m_startValue
);
1416 void wxGridCellBoolEditor::StartingClick()
1418 CBox()->SetValue(!CBox()->GetValue());
1421 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1423 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1425 int keycode
= event
.GetKeyCode();
1438 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1440 int keycode
= event
.GetKeyCode();
1444 CBox()->SetValue(!CBox()->GetValue());
1448 CBox()->SetValue(true);
1452 CBox()->SetValue(false);
1457 wxString
wxGridCellBoolEditor::GetValue() const
1459 return ms_stringValues
[CBox()->GetValue()];
1463 wxGridCellBoolEditor::UseStringValues(const wxString
& valueTrue
,
1464 const wxString
& valueFalse
)
1466 ms_stringValues
[false] = valueFalse
;
1467 ms_stringValues
[true] = valueTrue
;
1471 wxGridCellBoolEditor::IsTrueValue(const wxString
& value
)
1473 return value
== ms_stringValues
[true];
1476 #endif // wxUSE_CHECKBOX
1480 // ----------------------------------------------------------------------------
1481 // wxGridCellChoiceEditor
1482 // ----------------------------------------------------------------------------
1484 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1486 : m_choices(choices
),
1487 m_allowOthers(allowOthers
) { }
1489 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1490 const wxString choices
[],
1492 : m_allowOthers(allowOthers
)
1496 m_choices
.Alloc(count
);
1497 for ( size_t n
= 0; n
< count
; n
++ )
1499 m_choices
.Add(choices
[n
]);
1504 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1506 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1507 editor
->m_allowOthers
= m_allowOthers
;
1508 editor
->m_choices
= m_choices
;
1513 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1515 wxEvtHandler
* evtHandler
)
1517 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1518 wxDefaultPosition
, wxDefaultSize
,
1520 m_allowOthers
? 0 : wxCB_READONLY
);
1522 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1525 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1526 wxGridCellAttr
* attr
)
1528 // as we fill the entire client area, don't do anything here to minimize
1531 // TODO: It doesn't actually fill the client area since the height of a
1532 // combo always defaults to the standard. Until someone has time to
1533 // figure out the right rectangle to paint, just do it the normal way.
1534 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1537 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1539 wxASSERT_MSG(m_control
,
1540 wxT("The wxGridCellEditor must be created first!"));
1542 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1544 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1546 // Don't immediately end if we get a kill focus event within BeginEdit
1548 evtHandler
->SetInSetFocus(true);
1550 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1554 Combo()->SetValue(m_startValue
);
1558 // find the right position, or default to the first if not found
1559 int pos
= Combo()->FindString(m_startValue
);
1560 if (pos
== wxNOT_FOUND
)
1562 Combo()->SetSelection(pos
);
1565 Combo()->SetInsertionPointEnd();
1566 Combo()->SetFocus();
1570 // When dropping down the menu, a kill focus event
1571 // happens after this point, so we can't reset the flag yet.
1572 #if !defined(__WXGTK20__)
1573 evtHandler
->SetInSetFocus(false);
1578 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1581 wxString value
= Combo()->GetValue();
1582 if ( value
== m_startValue
)
1585 grid
->GetTable()->SetValue(row
, col
, value
);
1590 void wxGridCellChoiceEditor::Reset()
1592 Combo()->SetValue(m_startValue
);
1593 Combo()->SetInsertionPointEnd();
1596 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1606 wxStringTokenizer
tk(params
, _T(','));
1607 while ( tk
.HasMoreTokens() )
1609 m_choices
.Add(tk
.GetNextToken());
1613 // return the value in the text control
1614 wxString
wxGridCellChoiceEditor::GetValue() const
1616 return Combo()->GetValue();
1619 #endif // wxUSE_COMBOBOX
1621 // ----------------------------------------------------------------------------
1622 // wxGridCellEditorEvtHandler
1623 // ----------------------------------------------------------------------------
1625 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1627 // Don't disable the cell if we're just starting to edit it
1632 m_grid
->DisableCellEditControl();
1637 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1639 switch ( event
.GetKeyCode() )
1643 m_grid
->DisableCellEditControl();
1647 m_grid
->GetEventHandler()->ProcessEvent( event
);
1651 case WXK_NUMPAD_ENTER
:
1652 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1653 m_editor
->HandleReturn(event
);
1662 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1664 int row
= m_grid
->GetGridCursorRow();
1665 int col
= m_grid
->GetGridCursorCol();
1666 wxRect rect
= m_grid
->CellToRect( row
, col
);
1668 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1670 // if cell width is smaller than grid client area, cell is wholly visible
1671 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1673 switch ( event
.GetKeyCode() )
1678 case WXK_NUMPAD_ENTER
:
1683 if ( wholeCellVisible
)
1685 // no special processing needed...
1690 // do special processing for partly visible cell...
1692 // get the widths of all cells previous to this one
1694 for ( int i
= 0; i
< col
; i
++ )
1696 colXPos
+= m_grid
->GetColSize(i
);
1699 int xUnit
= 1, yUnit
= 1;
1700 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1703 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1707 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1715 if ( wholeCellVisible
)
1717 // no special processing needed...
1722 // do special processing for partly visible cell...
1725 wxString value
= m_grid
->GetCellValue(row
, col
);
1726 if ( wxEmptyString
!= value
)
1728 // get width of cell CONTENTS (text)
1730 wxFont font
= m_grid
->GetCellFont(row
, col
);
1731 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1733 // try to RIGHT align the text by scrolling
1734 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1736 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1737 // otherwise the last part of the cell content might be hidden below the scroll bar
1738 // FIXME: maybe there is a more suitable correction?
1739 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1740 if ( textWidth
< 0 )
1746 // get the widths of all cells previous to this one
1748 for ( int i
= 0; i
< col
; i
++ )
1750 colXPos
+= m_grid
->GetColSize(i
);
1753 // and add the (modified) text width of the cell contents
1754 // as we'd like to see the last part of the cell contents
1755 colXPos
+= textWidth
;
1757 int xUnit
= 1, yUnit
= 1;
1758 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1759 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1770 // ----------------------------------------------------------------------------
1771 // wxGridCellWorker is an (almost) empty common base class for
1772 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1773 // ----------------------------------------------------------------------------
1775 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1780 wxGridCellWorker::~wxGridCellWorker()
1784 // ============================================================================
1786 // ============================================================================
1788 // ----------------------------------------------------------------------------
1789 // wxGridCellRenderer
1790 // ----------------------------------------------------------------------------
1792 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1793 wxGridCellAttr
& attr
,
1796 int WXUNUSED(row
), int WXUNUSED(col
),
1799 dc
.SetBackgroundMode( wxSOLID
);
1801 // grey out fields if the grid is disabled
1802 if ( grid
.IsEnabled() )
1806 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1810 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1815 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1818 dc
.SetPen( *wxTRANSPARENT_PEN
);
1819 dc
.DrawRectangle(rect
);
1822 // ----------------------------------------------------------------------------
1823 // wxGridCellStringRenderer
1824 // ----------------------------------------------------------------------------
1826 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1827 const wxGridCellAttr
& attr
,
1831 dc
.SetBackgroundMode( wxTRANSPARENT
);
1833 // TODO some special colours for attr.IsReadOnly() case?
1835 // different coloured text when the grid is disabled
1836 if ( grid
.IsEnabled() )
1840 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1841 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1845 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1846 dc
.SetTextForeground( attr
.GetTextColour() );
1851 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1852 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1855 dc
.SetFont( attr
.GetFont() );
1858 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1860 const wxString
& text
)
1862 wxCoord x
= 0, y
= 0, max_x
= 0;
1863 dc
.SetFont(attr
.GetFont());
1864 wxStringTokenizer
tk(text
, _T('\n'));
1865 while ( tk
.HasMoreTokens() )
1867 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1868 max_x
= wxMax(max_x
, x
);
1871 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1873 return wxSize(max_x
, y
);
1876 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1877 wxGridCellAttr
& attr
,
1881 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1884 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1885 wxGridCellAttr
& attr
,
1887 const wxRect
& rectCell
,
1891 wxRect rect
= rectCell
;
1894 // erase only this cells background, overflow cells should have been erased
1895 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1898 attr
.GetAlignment(&hAlign
, &vAlign
);
1900 int overflowCols
= 0;
1902 if (attr
.GetOverflow())
1904 int cols
= grid
.GetNumberCols();
1905 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1906 int cell_rows
, cell_cols
;
1907 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1908 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1910 int i
, c_cols
, c_rows
;
1911 for (i
= col
+cell_cols
; i
< cols
; i
++)
1913 bool is_empty
= true;
1914 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1916 // check w/ anchor cell for multicell block
1917 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1920 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1929 rect
.width
+= grid
.GetColSize(i
);
1937 if (rect
.width
>= best_width
)
1941 overflowCols
= i
- col
- cell_cols
+ 1;
1942 if (overflowCols
>= cols
)
1943 overflowCols
= cols
- 1;
1946 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1948 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1950 clip
.x
+= rectCell
.width
;
1951 // draw each overflow cell individually
1952 int col_end
= col
+ cell_cols
+ overflowCols
;
1953 if (col_end
>= grid
.GetNumberCols())
1954 col_end
= grid
.GetNumberCols() - 1;
1955 for (int i
= col
+ cell_cols
; i
<= col_end
; i
++)
1957 clip
.width
= grid
.GetColSize(i
) - 1;
1958 dc
.DestroyClippingRegion();
1959 dc
.SetClippingRegion(clip
);
1961 SetTextColoursAndFont(grid
, attr
, dc
,
1962 grid
.IsInSelection(row
,i
));
1964 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1965 rect
, hAlign
, vAlign
);
1966 clip
.x
+= grid
.GetColSize(i
) - 1;
1972 dc
.DestroyClippingRegion();
1976 // now we only have to draw the text
1977 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1979 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1980 rect
, hAlign
, vAlign
);
1983 // ----------------------------------------------------------------------------
1984 // wxGridCellNumberRenderer
1985 // ----------------------------------------------------------------------------
1987 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1989 wxGridTableBase
*table
= grid
.GetTable();
1991 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1993 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1997 text
= table
->GetValue(row
, col
);
2003 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
2004 wxGridCellAttr
& attr
,
2006 const wxRect
& rectCell
,
2010 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2012 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2014 // draw the text right aligned by default
2016 attr
.GetAlignment(&hAlign
, &vAlign
);
2017 hAlign
= wxALIGN_RIGHT
;
2019 wxRect rect
= rectCell
;
2022 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2025 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
2026 wxGridCellAttr
& attr
,
2030 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2033 // ----------------------------------------------------------------------------
2034 // wxGridCellFloatRenderer
2035 // ----------------------------------------------------------------------------
2037 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
2040 SetPrecision(precision
);
2043 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
2045 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
2046 renderer
->m_width
= m_width
;
2047 renderer
->m_precision
= m_precision
;
2048 renderer
->m_format
= m_format
;
2053 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
2055 wxGridTableBase
*table
= grid
.GetTable();
2060 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
2062 val
= table
->GetValueAsDouble(row
, col
);
2067 text
= table
->GetValue(row
, col
);
2068 hasDouble
= text
.ToDouble(&val
);
2075 if ( m_width
== -1 )
2077 if ( m_precision
== -1 )
2079 // default width/precision
2080 m_format
= _T("%f");
2084 m_format
.Printf(_T("%%.%df"), m_precision
);
2087 else if ( m_precision
== -1 )
2089 // default precision
2090 m_format
.Printf(_T("%%%d.f"), m_width
);
2094 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2098 text
.Printf(m_format
, val
);
2101 //else: text already contains the string
2106 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2107 wxGridCellAttr
& attr
,
2109 const wxRect
& rectCell
,
2113 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2115 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2117 // draw the text right aligned by default
2119 attr
.GetAlignment(&hAlign
, &vAlign
);
2120 hAlign
= wxALIGN_RIGHT
;
2122 wxRect rect
= rectCell
;
2125 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2128 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2129 wxGridCellAttr
& attr
,
2133 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2136 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2140 // reset to defaults
2146 wxString tmp
= params
.BeforeFirst(_T(','));
2150 if ( tmp
.ToLong(&width
) )
2152 SetWidth((int)width
);
2156 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2160 tmp
= params
.AfterFirst(_T(','));
2164 if ( tmp
.ToLong(&precision
) )
2166 SetPrecision((int)precision
);
2170 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2176 // ----------------------------------------------------------------------------
2177 // wxGridCellBoolRenderer
2178 // ----------------------------------------------------------------------------
2180 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2182 // FIXME these checkbox size calculations are really ugly...
2184 // between checkmark and box
2185 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2187 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2188 wxGridCellAttr
& WXUNUSED(attr
),
2193 // compute it only once (no locks for MT safeness in GUI thread...)
2194 if ( !ms_sizeCheckMark
.x
)
2196 // get checkbox size
2197 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2198 wxSize size
= checkbox
->GetBestSize();
2199 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2201 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2202 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2203 checkSize
-= size
.y
/ 2;
2208 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2211 return ms_sizeCheckMark
;
2214 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2215 wxGridCellAttr
& attr
,
2221 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2223 // draw a check mark in the centre (ignoring alignment - TODO)
2224 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2226 // don't draw outside the cell
2227 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2228 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2230 // and even leave (at least) 1 pixel margin
2231 size
.x
= size
.y
= minSize
- 2;
2234 // draw a border around checkmark
2236 attr
.GetAlignment(&hAlign
, &vAlign
);
2239 if (hAlign
== wxALIGN_CENTRE
)
2241 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2242 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2243 rectBorder
.width
= size
.x
;
2244 rectBorder
.height
= size
.y
;
2246 else if (hAlign
== wxALIGN_LEFT
)
2248 rectBorder
.x
= rect
.x
+ 2;
2249 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2250 rectBorder
.width
= size
.x
;
2251 rectBorder
.height
= size
.y
;
2253 else if (hAlign
== wxALIGN_RIGHT
)
2255 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2256 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2257 rectBorder
.width
= size
.x
;
2258 rectBorder
.height
= size
.y
;
2262 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2264 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2268 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2269 value
= wxGridCellBoolEditor::IsTrueValue(cellval
);
2274 wxRect rectMark
= rectBorder
;
2277 // MSW DrawCheckMark() is weird (and should probably be changed...)
2278 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2282 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2285 dc
.SetTextForeground(attr
.GetTextColour());
2286 dc
.DrawCheckMark(rectMark
);
2289 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2290 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2291 dc
.DrawRectangle(rectBorder
);
2294 // ----------------------------------------------------------------------------
2296 // ----------------------------------------------------------------------------
2298 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2302 m_isReadOnly
= Unset
;
2307 m_attrkind
= wxGridCellAttr::Cell
;
2309 m_sizeRows
= m_sizeCols
= 1;
2310 m_overflow
= UnsetOverflow
;
2312 SetDefAttr(attrDefault
);
2315 wxGridCellAttr
*wxGridCellAttr::Clone() const
2317 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2319 if ( HasTextColour() )
2320 attr
->SetTextColour(GetTextColour());
2321 if ( HasBackgroundColour() )
2322 attr
->SetBackgroundColour(GetBackgroundColour());
2324 attr
->SetFont(GetFont());
2325 if ( HasAlignment() )
2326 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2328 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2332 attr
->SetRenderer(m_renderer
);
2333 m_renderer
->IncRef();
2337 attr
->SetEditor(m_editor
);
2342 attr
->SetReadOnly();
2344 attr
->SetOverflow( m_overflow
== Overflow
);
2345 attr
->SetKind( m_attrkind
);
2350 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2352 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2353 SetTextColour(mergefrom
->GetTextColour());
2354 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2355 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2356 if ( !HasFont() && mergefrom
->HasFont() )
2357 SetFont(mergefrom
->GetFont());
2358 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2361 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2362 SetAlignment(hAlign
, vAlign
);
2364 if ( !HasSize() && mergefrom
->HasSize() )
2365 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2367 // Directly access member functions as GetRender/Editor don't just return
2368 // m_renderer/m_editor
2370 // Maybe add support for merge of Render and Editor?
2371 if (!HasRenderer() && mergefrom
->HasRenderer() )
2373 m_renderer
= mergefrom
->m_renderer
;
2374 m_renderer
->IncRef();
2376 if ( !HasEditor() && mergefrom
->HasEditor() )
2378 m_editor
= mergefrom
->m_editor
;
2381 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2382 SetReadOnly(mergefrom
->IsReadOnly());
2384 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2385 SetOverflow(mergefrom
->GetOverflow());
2387 SetDefAttr(mergefrom
->m_defGridAttr
);
2390 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2392 // The size of a cell is normally 1,1
2394 // If this cell is larger (2,2) then this is the top left cell
2395 // the other cells that will be covered (lower right cells) must be
2396 // set to negative or zero values such that
2397 // row + num_rows of the covered cell points to the larger cell (this cell)
2398 // same goes for the col + num_cols.
2400 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2402 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2403 !((num_rows
<= 0) && (num_cols
> 0)) ||
2404 !((num_rows
== 0) && (num_cols
== 0))),
2405 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2407 m_sizeRows
= num_rows
;
2408 m_sizeCols
= num_cols
;
2411 const wxColour
& wxGridCellAttr::GetTextColour() const
2413 if (HasTextColour())
2417 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2419 return m_defGridAttr
->GetTextColour();
2423 wxFAIL_MSG(wxT("Missing default cell attribute"));
2424 return wxNullColour
;
2428 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2430 if (HasBackgroundColour())
2434 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2436 return m_defGridAttr
->GetBackgroundColour();
2440 wxFAIL_MSG(wxT("Missing default cell attribute"));
2441 return wxNullColour
;
2445 const wxFont
& wxGridCellAttr::GetFont() const
2451 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2453 return m_defGridAttr
->GetFont();
2457 wxFAIL_MSG(wxT("Missing default cell attribute"));
2462 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2471 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2473 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2477 wxFAIL_MSG(wxT("Missing default cell attribute"));
2481 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2484 *num_rows
= m_sizeRows
;
2486 *num_cols
= m_sizeCols
;
2489 // GetRenderer and GetEditor use a slightly different decision path about
2490 // which attribute to use. If a non-default attr object has one then it is
2491 // used, otherwise the default editor or renderer is fetched from the grid and
2492 // used. It should be the default for the data type of the cell. If it is
2493 // NULL (because the table has a type that the grid does not have in its
2494 // registry), then the grid's default editor or renderer is used.
2496 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
2498 wxGridCellRenderer
*renderer
= NULL
;
2500 if ( m_renderer
&& this != m_defGridAttr
)
2502 // use the cells renderer if it has one
2503 renderer
= m_renderer
;
2506 else // no non-default cell renderer
2508 // get default renderer for the data type
2511 // GetDefaultRendererForCell() will do IncRef() for us
2512 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2515 if ( renderer
== NULL
)
2517 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2519 // if we still don't have one then use the grid default
2520 // (no need for IncRef() here neither)
2521 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2523 else // default grid attr
2525 // use m_renderer which we had decided not to use initially
2526 renderer
= m_renderer
;
2533 // we're supposed to always find something
2534 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2539 // same as above, except for s/renderer/editor/g
2540 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
2542 wxGridCellEditor
*editor
= NULL
;
2544 if ( m_editor
&& this != m_defGridAttr
)
2546 // use the cells editor if it has one
2550 else // no non default cell editor
2552 // get default editor for the data type
2555 // GetDefaultEditorForCell() will do IncRef() for us
2556 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2559 if ( editor
== NULL
)
2561 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
2563 // if we still don't have one then use the grid default
2564 // (no need for IncRef() here neither)
2565 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2567 else // default grid attr
2569 // use m_editor which we had decided not to use initially
2577 // we're supposed to always find something
2578 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2583 // ----------------------------------------------------------------------------
2584 // wxGridCellAttrData
2585 // ----------------------------------------------------------------------------
2587 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2589 int n
= FindIndex(row
, col
);
2590 if ( n
== wxNOT_FOUND
)
2592 // add the attribute
2593 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2597 // free the old attribute
2598 m_attrs
[(size_t)n
].attr
->DecRef();
2602 // change the attribute
2603 m_attrs
[(size_t)n
].attr
= attr
;
2607 // remove this attribute
2608 m_attrs
.RemoveAt((size_t)n
);
2613 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2615 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2617 int n
= FindIndex(row
, col
);
2618 if ( n
!= wxNOT_FOUND
)
2620 attr
= m_attrs
[(size_t)n
].attr
;
2627 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2629 size_t count
= m_attrs
.GetCount();
2630 for ( size_t n
= 0; n
< count
; n
++ )
2632 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2633 wxCoord row
= coords
.GetRow();
2634 if ((size_t)row
>= pos
)
2638 // If rows inserted, include row counter where necessary
2639 coords
.SetRow(row
+ numRows
);
2641 else if (numRows
< 0)
2643 // If rows deleted ...
2644 if ((size_t)row
>= pos
- numRows
)
2646 // ...either decrement row counter (if row still exists)...
2647 coords
.SetRow(row
+ numRows
);
2651 // ...or remove the attribute
2652 // No need to DecRef the attribute itself since this is
2653 // done be wxGridCellWithAttr's destructor!
2654 m_attrs
.RemoveAt(n
);
2663 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2665 size_t count
= m_attrs
.GetCount();
2666 for ( size_t n
= 0; n
< count
; n
++ )
2668 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2669 wxCoord col
= coords
.GetCol();
2670 if ( (size_t)col
>= pos
)
2674 // If rows inserted, include row counter where necessary
2675 coords
.SetCol(col
+ numCols
);
2677 else if (numCols
< 0)
2679 // If rows deleted ...
2680 if ((size_t)col
>= pos
- numCols
)
2682 // ...either decrement row counter (if row still exists)...
2683 coords
.SetCol(col
+ numCols
);
2687 // ...or remove the attribute
2688 // No need to DecRef the attribute itself since this is
2689 // done be wxGridCellWithAttr's destructor!
2690 m_attrs
.RemoveAt(n
);
2699 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2701 size_t count
= m_attrs
.GetCount();
2702 for ( size_t n
= 0; n
< count
; n
++ )
2704 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2705 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2714 // ----------------------------------------------------------------------------
2715 // wxGridRowOrColAttrData
2716 // ----------------------------------------------------------------------------
2718 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2720 size_t count
= m_attrs
.GetCount();
2721 for ( size_t n
= 0; n
< count
; n
++ )
2723 m_attrs
[n
]->DecRef();
2727 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2729 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2731 int n
= m_rowsOrCols
.Index(rowOrCol
);
2732 if ( n
!= wxNOT_FOUND
)
2734 attr
= m_attrs
[(size_t)n
];
2741 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2743 int i
= m_rowsOrCols
.Index(rowOrCol
);
2744 if ( i
== wxNOT_FOUND
)
2746 // add the attribute
2747 m_rowsOrCols
.Add(rowOrCol
);
2752 size_t n
= (size_t)i
;
2755 // change the attribute
2756 m_attrs
[n
]->DecRef();
2761 // remove this attribute
2762 m_attrs
[n
]->DecRef();
2763 m_rowsOrCols
.RemoveAt(n
);
2764 m_attrs
.RemoveAt(n
);
2769 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2771 size_t count
= m_attrs
.GetCount();
2772 for ( size_t n
= 0; n
< count
; n
++ )
2774 int & rowOrCol
= m_rowsOrCols
[n
];
2775 if ( (size_t)rowOrCol
>= pos
)
2777 if ( numRowsOrCols
> 0 )
2779 // If rows inserted, include row counter where necessary
2780 rowOrCol
+= numRowsOrCols
;
2782 else if ( numRowsOrCols
< 0)
2784 // If rows deleted, either decrement row counter (if row still exists)
2785 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2786 rowOrCol
+= numRowsOrCols
;
2789 m_rowsOrCols
.RemoveAt(n
);
2790 m_attrs
[n
]->DecRef();
2791 m_attrs
.RemoveAt(n
);
2800 // ----------------------------------------------------------------------------
2801 // wxGridCellAttrProvider
2802 // ----------------------------------------------------------------------------
2804 wxGridCellAttrProvider::wxGridCellAttrProvider()
2806 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2809 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2814 void wxGridCellAttrProvider::InitData()
2816 m_data
= new wxGridCellAttrProviderData
;
2819 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2820 wxGridCellAttr::wxAttrKind kind
) const
2822 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2827 case (wxGridCellAttr::Any
):
2828 // Get cached merge attributes.
2829 // Currently not used as no cache implemented as not mutable
2830 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2833 // Basically implement old version.
2834 // Also check merge cache, so we don't have to re-merge every time..
2835 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2836 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2837 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2839 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2841 // Two or more are non NULL
2842 attr
= new wxGridCellAttr
;
2843 attr
->SetKind(wxGridCellAttr::Merged
);
2845 // Order is important..
2848 attr
->MergeWith(attrcell
);
2853 attr
->MergeWith(attrcol
);
2858 attr
->MergeWith(attrrow
);
2862 // store merge attr if cache implemented
2864 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2868 // one or none is non null return it or null.
2887 case (wxGridCellAttr::Cell
):
2888 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2891 case (wxGridCellAttr::Col
):
2892 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2895 case (wxGridCellAttr::Row
):
2896 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2901 // (wxGridCellAttr::Default):
2902 // (wxGridCellAttr::Merged):
2910 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2916 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2919 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2924 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2927 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2932 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2935 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2939 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2941 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2945 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2949 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2951 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2955 // ----------------------------------------------------------------------------
2956 // wxGridTypeRegistry
2957 // ----------------------------------------------------------------------------
2959 wxGridTypeRegistry::~wxGridTypeRegistry()
2961 size_t count
= m_typeinfo
.GetCount();
2962 for ( size_t i
= 0; i
< count
; i
++ )
2963 delete m_typeinfo
[i
];
2966 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2967 wxGridCellRenderer
* renderer
,
2968 wxGridCellEditor
* editor
)
2970 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2972 // is it already registered?
2973 int loc
= FindRegisteredDataType(typeName
);
2974 if ( loc
!= wxNOT_FOUND
)
2976 delete m_typeinfo
[loc
];
2977 m_typeinfo
[loc
] = info
;
2981 m_typeinfo
.Add(info
);
2985 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2987 size_t count
= m_typeinfo
.GetCount();
2988 for ( size_t i
= 0; i
< count
; i
++ )
2990 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2999 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
3001 int index
= FindRegisteredDataType(typeName
);
3002 if ( index
== wxNOT_FOUND
)
3004 // check whether this is one of the standard ones, in which case
3005 // register it "on the fly"
3007 if ( typeName
== wxGRID_VALUE_STRING
)
3009 RegisterDataType(wxGRID_VALUE_STRING
,
3010 new wxGridCellStringRenderer
,
3011 new wxGridCellTextEditor
);
3014 #endif // wxUSE_TEXTCTRL
3016 if ( typeName
== wxGRID_VALUE_BOOL
)
3018 RegisterDataType(wxGRID_VALUE_BOOL
,
3019 new wxGridCellBoolRenderer
,
3020 new wxGridCellBoolEditor
);
3023 #endif // wxUSE_CHECKBOX
3025 if ( typeName
== wxGRID_VALUE_NUMBER
)
3027 RegisterDataType(wxGRID_VALUE_NUMBER
,
3028 new wxGridCellNumberRenderer
,
3029 new wxGridCellNumberEditor
);
3031 else if ( typeName
== wxGRID_VALUE_FLOAT
)
3033 RegisterDataType(wxGRID_VALUE_FLOAT
,
3034 new wxGridCellFloatRenderer
,
3035 new wxGridCellFloatEditor
);
3038 #endif // wxUSE_TEXTCTRL
3040 if ( typeName
== wxGRID_VALUE_CHOICE
)
3042 RegisterDataType(wxGRID_VALUE_CHOICE
,
3043 new wxGridCellStringRenderer
,
3044 new wxGridCellChoiceEditor
);
3047 #endif // wxUSE_COMBOBOX
3052 // we get here only if just added the entry for this type, so return
3054 index
= m_typeinfo
.GetCount() - 1;
3060 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
3062 int index
= FindDataType(typeName
);
3063 if ( index
== wxNOT_FOUND
)
3065 // the first part of the typename is the "real" type, anything after ':'
3066 // are the parameters for the renderer
3067 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
3068 if ( index
== wxNOT_FOUND
)
3073 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3074 wxGridCellRenderer
*rendererOld
= renderer
;
3075 renderer
= renderer
->Clone();
3076 rendererOld
->DecRef();
3078 wxGridCellEditor
*editor
= GetEditor(index
);
3079 wxGridCellEditor
*editorOld
= editor
;
3080 editor
= editor
->Clone();
3081 editorOld
->DecRef();
3083 // do it even if there are no parameters to reset them to defaults
3084 wxString params
= typeName
.AfterFirst(_T(':'));
3085 renderer
->SetParameters(params
);
3086 editor
->SetParameters(params
);
3088 // register the new typename
3089 RegisterDataType(typeName
, renderer
, editor
);
3091 // we just registered it, it's the last one
3092 index
= m_typeinfo
.GetCount() - 1;
3098 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3100 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3107 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3109 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3116 // ----------------------------------------------------------------------------
3118 // ----------------------------------------------------------------------------
3120 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3122 wxGridTableBase::wxGridTableBase()
3124 m_view
= (wxGrid
*) NULL
;
3125 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3128 wxGridTableBase::~wxGridTableBase()
3130 delete m_attrProvider
;
3133 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3135 delete m_attrProvider
;
3136 m_attrProvider
= attrProvider
;
3139 bool wxGridTableBase::CanHaveAttributes()
3141 if ( ! GetAttrProvider() )
3143 // use the default attr provider by default
3144 SetAttrProvider(new wxGridCellAttrProvider
);
3150 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3152 if ( m_attrProvider
)
3153 return m_attrProvider
->GetAttr(row
, col
, kind
);
3155 return (wxGridCellAttr
*)NULL
;
3158 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3160 if ( m_attrProvider
)
3162 attr
->SetKind(wxGridCellAttr::Cell
);
3163 m_attrProvider
->SetAttr(attr
, row
, col
);
3167 // as we take ownership of the pointer and don't store it, we must
3173 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3175 if ( m_attrProvider
)
3177 attr
->SetKind(wxGridCellAttr::Row
);
3178 m_attrProvider
->SetRowAttr(attr
, row
);
3182 // as we take ownership of the pointer and don't store it, we must
3188 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3190 if ( m_attrProvider
)
3192 attr
->SetKind(wxGridCellAttr::Col
);
3193 m_attrProvider
->SetColAttr(attr
, col
);
3197 // as we take ownership of the pointer and don't store it, we must
3203 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3204 size_t WXUNUSED(numRows
) )
3206 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3211 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3213 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3218 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3219 size_t WXUNUSED(numRows
) )
3221 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3226 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3227 size_t WXUNUSED(numCols
) )
3229 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3234 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3236 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3241 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3242 size_t WXUNUSED(numCols
) )
3244 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3249 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3253 // RD: Starting the rows at zero confuses users,
3254 // no matter how much it makes sense to us geeks.
3260 wxString
wxGridTableBase::GetColLabelValue( int col
)
3262 // default col labels are:
3263 // cols 0 to 25 : A-Z
3264 // cols 26 to 675 : AA-ZZ
3269 for ( n
= 1; ; n
++ )
3271 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3277 // reverse the string...
3279 for ( i
= 0; i
< n
; i
++ )
3287 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3289 return wxGRID_VALUE_STRING
;
3292 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3293 const wxString
& typeName
)
3295 return typeName
== wxGRID_VALUE_STRING
;
3298 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3300 return CanGetValueAs(row
, col
, typeName
);
3303 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3308 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3313 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3318 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3319 long WXUNUSED(value
) )
3323 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3324 double WXUNUSED(value
) )
3328 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3329 bool WXUNUSED(value
) )
3333 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3334 const wxString
& WXUNUSED(typeName
) )
3339 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3340 const wxString
& WXUNUSED(typeName
),
3341 void* WXUNUSED(value
) )
3345 //////////////////////////////////////////////////////////////////////
3347 // Message class for the grid table to send requests and notifications
3351 wxGridTableMessage::wxGridTableMessage()
3353 m_table
= (wxGridTableBase
*) NULL
;
3359 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3360 int commandInt1
, int commandInt2
)
3364 m_comInt1
= commandInt1
;
3365 m_comInt2
= commandInt2
;
3368 //////////////////////////////////////////////////////////////////////
3370 // A basic grid table for string data. An object of this class will
3371 // created by wxGrid if you don't specify an alternative table class.
3374 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3376 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3378 wxGridStringTable::wxGridStringTable()
3383 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3386 m_data
.Alloc( numRows
);
3389 sa
.Alloc( numCols
);
3390 sa
.Add( wxEmptyString
, numCols
);
3392 m_data
.Add( sa
, numRows
);
3395 wxGridStringTable::~wxGridStringTable()
3399 int wxGridStringTable::GetNumberRows()
3401 return m_data
.GetCount();
3404 int wxGridStringTable::GetNumberCols()
3406 if ( m_data
.GetCount() > 0 )
3407 return m_data
[0].GetCount();
3412 wxString
wxGridStringTable::GetValue( int row
, int col
)
3414 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3416 _T("invalid row or column index in wxGridStringTable") );
3418 return m_data
[row
][col
];
3421 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3423 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3424 _T("invalid row or column index in wxGridStringTable") );
3426 m_data
[row
][col
] = value
;
3429 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3431 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3433 _T("invalid row or column index in wxGridStringTable") );
3435 return (m_data
[row
][col
] == wxEmptyString
);
3438 void wxGridStringTable::Clear()
3441 int numRows
, numCols
;
3443 numRows
= m_data
.GetCount();
3446 numCols
= m_data
[0].GetCount();
3448 for ( row
= 0; row
< numRows
; row
++ )
3450 for ( col
= 0; col
< numCols
; col
++ )
3452 m_data
[row
][col
] = wxEmptyString
;
3458 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3460 size_t curNumRows
= m_data
.GetCount();
3461 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3462 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3464 if ( pos
>= curNumRows
)
3466 return AppendRows( numRows
);
3470 sa
.Alloc( curNumCols
);
3471 sa
.Add( wxEmptyString
, curNumCols
);
3472 m_data
.Insert( sa
, pos
, numRows
);
3476 wxGridTableMessage
msg( this,
3477 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3481 GetView()->ProcessTableMessage( msg
);
3487 bool wxGridStringTable::AppendRows( size_t numRows
)
3489 size_t curNumRows
= m_data
.GetCount();
3490 size_t curNumCols
= ( curNumRows
> 0
3491 ? m_data
[0].GetCount()
3492 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3495 if ( curNumCols
> 0 )
3497 sa
.Alloc( curNumCols
);
3498 sa
.Add( wxEmptyString
, curNumCols
);
3501 m_data
.Add( sa
, numRows
);
3505 wxGridTableMessage
msg( this,
3506 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3509 GetView()->ProcessTableMessage( msg
);
3515 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3517 size_t curNumRows
= m_data
.GetCount();
3519 if ( pos
>= curNumRows
)
3521 wxFAIL_MSG( wxString::Format
3523 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3525 (unsigned long)numRows
,
3526 (unsigned long)curNumRows
3532 if ( numRows
> curNumRows
- pos
)
3534 numRows
= curNumRows
- pos
;
3537 if ( numRows
>= curNumRows
)
3543 m_data
.RemoveAt( pos
, numRows
);
3548 wxGridTableMessage
msg( this,
3549 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3553 GetView()->ProcessTableMessage( msg
);
3559 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3563 size_t curNumRows
= m_data
.GetCount();
3564 size_t curNumCols
= ( curNumRows
> 0
3565 ? m_data
[0].GetCount()
3566 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3568 if ( pos
>= curNumCols
)
3570 return AppendCols( numCols
);
3573 if ( !m_colLabels
.IsEmpty() )
3575 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
3578 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
3579 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
3582 for ( row
= 0; row
< curNumRows
; row
++ )
3584 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3586 m_data
[row
].Insert( wxEmptyString
, col
);
3592 wxGridTableMessage
msg( this,
3593 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3597 GetView()->ProcessTableMessage( msg
);
3603 bool wxGridStringTable::AppendCols( size_t numCols
)
3607 size_t curNumRows
= m_data
.GetCount();
3612 // TODO: something better than this ?
3614 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3619 for ( row
= 0; row
< curNumRows
; row
++ )
3621 m_data
[row
].Add( wxEmptyString
, numCols
);
3626 wxGridTableMessage
msg( this,
3627 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3630 GetView()->ProcessTableMessage( msg
);
3636 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3640 size_t curNumRows
= m_data
.GetCount();
3641 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3642 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3644 if ( pos
>= curNumCols
)
3646 wxFAIL_MSG( wxString::Format
3648 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3650 (unsigned long)numCols
,
3651 (unsigned long)curNumCols
3658 colID
= GetView()->GetColAt( pos
);
3662 if ( numCols
> curNumCols
- colID
)
3664 numCols
= curNumCols
- colID
;
3667 if ( !m_colLabels
.IsEmpty() )
3669 // m_colLabels stores just as many elements as it needs, e.g. if only
3670 // the label of the first column had been set it would have only one
3671 // element and not numCols, so account for it
3672 int nToRm
= m_colLabels
.size() - colID
;
3674 m_colLabels
.RemoveAt( colID
, nToRm
);
3677 for ( row
= 0; row
< curNumRows
; row
++ )
3679 if ( numCols
>= curNumCols
)
3681 m_data
[row
].Clear();
3685 m_data
[row
].RemoveAt( colID
, numCols
);
3691 wxGridTableMessage
msg( this,
3692 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3696 GetView()->ProcessTableMessage( msg
);
3702 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3704 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3706 // using default label
3708 return wxGridTableBase::GetRowLabelValue( row
);
3712 return m_rowLabels
[row
];
3716 wxString
wxGridStringTable::GetColLabelValue( int col
)
3718 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3720 // using default label
3722 return wxGridTableBase::GetColLabelValue( col
);
3726 return m_colLabels
[col
];
3730 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3732 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3734 int n
= m_rowLabels
.GetCount();
3737 for ( i
= n
; i
<= row
; i
++ )
3739 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3743 m_rowLabels
[row
] = value
;
3746 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3748 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3750 int n
= m_colLabels
.GetCount();
3753 for ( i
= n
; i
<= col
; i
++ )
3755 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3759 m_colLabels
[col
] = value
;
3763 //////////////////////////////////////////////////////////////////////
3764 //////////////////////////////////////////////////////////////////////
3766 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
3767 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
3770 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
3772 m_owner
->CancelMouseCapture();
3775 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3777 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
3778 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3779 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3780 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3781 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3782 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3783 EVT_CHAR( wxGridRowLabelWindow::OnChar
)
3786 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3788 const wxPoint
&pos
, const wxSize
&size
)
3789 : wxGridSubwindow(parent
, id
, pos
, size
)
3794 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3798 // NO - don't do this because it will set both the x and y origin
3799 // coords to match the parent scrolled window and we just want to
3800 // set the y coord - MB
3802 // m_owner->PrepareDC( dc );
3805 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3806 wxPoint pt
= dc
.GetDeviceOrigin();
3807 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
3809 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3810 m_owner
->DrawRowLabels( dc
, rows
);
3813 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3815 m_owner
->ProcessRowLabelMouseEvent( event
);
3818 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3820 m_owner
->GetEventHandler()->ProcessEvent( event
);
3823 // This seems to be required for wxMotif otherwise the mouse
3824 // cursor must be in the cell edit control to get key events
3826 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3828 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3832 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3834 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3838 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3840 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3844 //////////////////////////////////////////////////////////////////////
3846 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3848 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
3849 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3850 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3851 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3852 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3853 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3854 EVT_CHAR( wxGridColLabelWindow::OnChar
)
3857 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3859 const wxPoint
&pos
, const wxSize
&size
)
3860 : wxGridSubwindow(parent
, id
, pos
, size
)
3865 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3869 // NO - don't do this because it will set both the x and y origin
3870 // coords to match the parent scrolled window and we just want to
3871 // set the x coord - MB
3873 // m_owner->PrepareDC( dc );
3876 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3877 wxPoint pt
= dc
.GetDeviceOrigin();
3878 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3879 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
3881 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
3883 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3884 m_owner
->DrawColLabels( dc
, cols
);
3887 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3889 m_owner
->ProcessColLabelMouseEvent( event
);
3892 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3894 m_owner
->GetEventHandler()->ProcessEvent( event
);
3897 // This seems to be required for wxMotif otherwise the mouse
3898 // cursor must be in the cell edit control to get key events
3900 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3902 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3906 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3908 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3912 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3914 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3918 //////////////////////////////////////////////////////////////////////
3920 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3922 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
3923 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3924 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3925 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3926 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3927 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3928 EVT_CHAR( wxGridCornerLabelWindow::OnChar
)
3931 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3933 const wxPoint
&pos
, const wxSize
&size
)
3934 : wxGridSubwindow(parent
, id
, pos
, size
)
3939 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3943 int client_height
= 0;
3944 int client_width
= 0;
3945 GetClientSize( &client_width
, &client_height
);
3947 // VZ: any reason for this ifdef? (FIXME)
3953 rect
.SetWidth( client_width
- 2 );
3954 rect
.SetHeight( client_height
- 2 );
3956 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3958 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
3959 dc
.DrawLine( client_width
- 1, client_height
- 1, client_width
- 1, 0 );
3960 dc
.DrawLine( client_width
- 1, client_height
- 1, 0, client_height
- 1 );
3961 dc
.DrawLine( 0, 0, client_width
, 0 );
3962 dc
.DrawLine( 0, 0, 0, client_height
);
3964 dc
.SetPen( *wxWHITE_PEN
);
3965 dc
.DrawLine( 1, 1, client_width
- 1, 1 );
3966 dc
.DrawLine( 1, 1, 1, client_height
- 1 );
3970 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3972 m_owner
->ProcessCornerLabelMouseEvent( event
);
3975 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3977 m_owner
->GetEventHandler()->ProcessEvent(event
);
3980 // This seems to be required for wxMotif otherwise the mouse
3981 // cursor must be in the cell edit control to get key events
3983 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3985 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3989 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3991 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3995 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3997 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4001 //////////////////////////////////////////////////////////////////////
4003 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
4005 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
4006 EVT_PAINT( wxGridWindow::OnPaint
)
4007 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
4008 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
4009 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
4010 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
4011 EVT_CHAR( wxGridWindow::OnChar
)
4012 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
4013 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
4014 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
4017 wxGridWindow::wxGridWindow( wxGrid
*parent
,
4018 wxGridRowLabelWindow
*rowLblWin
,
4019 wxGridColLabelWindow
*colLblWin
,
4022 const wxSize
&size
)
4023 : wxGridSubwindow(parent
, id
, pos
, size
,
4024 wxCLIP_CHILDREN
, wxT("grid window") )
4027 m_rowLabelWin
= rowLblWin
;
4028 m_colLabelWin
= colLblWin
;
4031 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
4033 wxPaintDC
dc( this );
4034 m_owner
->PrepareDC( dc
);
4035 wxRegion reg
= GetUpdateRegion();
4036 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
4037 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
4039 #if WXGRID_DRAW_LINES
4040 m_owner
->DrawAllGridLines( dc
, reg
);
4043 m_owner
->DrawGridSpace( dc
);
4044 m_owner
->DrawHighlight( dc
, dirtyCells
);
4047 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
4049 wxWindow::ScrollWindow( dx
, dy
, rect
);
4050 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
4051 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
4054 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
4056 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
4059 m_owner
->ProcessGridCellMouseEvent( event
);
4062 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
4064 m_owner
->GetEventHandler()->ProcessEvent( event
);
4067 // This seems to be required for wxMotif/wxGTK otherwise the mouse
4068 // cursor must be in the cell edit control to get key events
4070 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
4072 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4076 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
4078 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4082 void wxGridWindow::OnChar( wxKeyEvent
& event
)
4084 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4088 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
4092 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
4094 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
4098 //////////////////////////////////////////////////////////////////////
4100 // Internal Helper function for computing row or column from some
4101 // (unscrolled) coordinate value, using either
4102 // m_defaultRowHeight/m_defaultColWidth or binary search on array
4103 // of m_rowBottoms/m_ColRights to speed up the search!
4105 // Internal helper macros for simpler use of that function
4107 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
4108 const wxArrayInt
& BorderArray
, int nMax
,
4111 #define internalXToCol(x) XToCol(x, true)
4112 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4113 m_minAcceptableRowHeight, \
4114 m_rowBottoms, m_numRows, true)
4116 /////////////////////////////////////////////////////////////////////
4118 #if wxUSE_EXTENDED_RTTI
4119 WX_DEFINE_FLAGS( wxGridStyle
)
4121 wxBEGIN_FLAGS( wxGridStyle
)
4122 // new style border flags, we put them first to
4123 // use them for streaming out
4124 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4125 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4126 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4127 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4128 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4129 wxFLAGS_MEMBER(wxBORDER_NONE
)
4131 // old style border flags
4132 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4133 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4134 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4135 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4136 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4137 wxFLAGS_MEMBER(wxBORDER
)
4139 // standard window styles
4140 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4141 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4142 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4143 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4144 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4145 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4146 wxFLAGS_MEMBER(wxVSCROLL
)
4147 wxFLAGS_MEMBER(wxHSCROLL
)
4149 wxEND_FLAGS( wxGridStyle
)
4151 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4153 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4154 wxHIDE_PROPERTY( Children
)
4155 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4156 wxEND_PROPERTIES_TABLE()
4158 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4159 wxEND_HANDLERS_TABLE()
4161 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4164 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
4167 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4170 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4171 EVT_PAINT( wxGrid::OnPaint
)
4172 EVT_SIZE( wxGrid::OnSize
)
4173 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4174 EVT_KEY_UP( wxGrid::OnKeyUp
)
4175 EVT_CHAR ( wxGrid::OnChar
)
4176 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4181 // in order to make sure that a size event is not
4182 // trigerred in a unfinished state
4183 m_cornerLabelWin
= NULL
;
4184 m_rowLabelWin
= NULL
;
4185 m_colLabelWin
= NULL
;
4189 wxGrid::wxGrid( wxWindow
*parent
,
4194 const wxString
& name
)
4195 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4196 m_colMinWidths(GRID_HASH_SIZE
),
4197 m_rowMinHeights(GRID_HASH_SIZE
)
4200 SetInitialSize(size
);
4203 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4204 const wxPoint
& pos
, const wxSize
& size
,
4205 long style
, const wxString
& name
)
4207 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4208 style
| wxWANTS_CHARS
, name
))
4211 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4212 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4215 SetInitialSize(size
);
4222 // Must do this or ~wxScrollHelper will pop the wrong event handler
4223 SetTargetWindow(this);
4225 wxSafeDecRef(m_defaultCellAttr
);
4227 #ifdef DEBUG_ATTR_CACHE
4228 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4229 wxPrintf(_T("wxGrid attribute cache statistics: "
4230 "total: %u, hits: %u (%u%%)\n"),
4231 total
, gs_nAttrCacheHits
,
4232 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4235 // if we own the table, just delete it, otherwise at least don't leave it
4236 // with dangling view pointer
4239 else if ( m_table
&& m_table
->GetView() == this )
4240 m_table
->SetView(NULL
);
4242 delete m_typeRegistry
;
4247 // ----- internal init and update functions
4250 // NOTE: If using the default visual attributes works everywhere then this can
4251 // be removed as well as the #else cases below.
4252 #define _USE_VISATTR 0
4254 void wxGrid::Create()
4256 // set to true by CreateGrid
4259 // create the type registry
4260 m_typeRegistry
= new wxGridTypeRegistry
;
4263 m_table
= (wxGridTableBase
*) NULL
;
4266 m_cellEditCtrlEnabled
= false;
4268 m_defaultCellAttr
= new wxGridCellAttr();
4270 // Set default cell attributes
4271 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4272 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4273 m_defaultCellAttr
->SetFont(GetFont());
4274 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4275 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4276 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4279 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4280 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4282 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4283 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4286 m_defaultCellAttr
->SetTextColour(
4287 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4288 m_defaultCellAttr
->SetBackgroundColour(
4289 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4294 m_currentCellCoords
= wxGridNoCellCoords
;
4296 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4297 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4299 // subwindow components that make up the wxGrid
4300 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4305 m_colLabelWin
= new wxGridColLabelWindow( this,
4310 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4315 m_gridWin
= new wxGridWindow( this,
4322 SetTargetWindow( m_gridWin
);
4325 wxColour gfg
= gva
.colFg
;
4326 wxColour gbg
= gva
.colBg
;
4327 wxColour lfg
= lva
.colFg
;
4328 wxColour lbg
= lva
.colBg
;
4330 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4331 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4332 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4333 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4336 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4337 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4338 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4339 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4340 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4341 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4343 m_gridWin
->SetOwnForegroundColour(gfg
);
4344 m_gridWin
->SetOwnBackgroundColour(gbg
);
4349 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4350 wxGrid::wxGridSelectionModes selmode
)
4352 wxCHECK_MSG( !m_created
,
4354 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4356 m_numRows
= numRows
;
4357 m_numCols
= numCols
;
4359 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4360 m_table
->SetView( this );
4362 m_selection
= new wxGridSelection( this, selmode
);
4371 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4373 wxCHECK_RET( m_created
,
4374 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4376 m_selection
->SetSelectionMode( selmode
);
4379 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4381 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4382 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4384 return m_selection
->GetSelectionMode();
4387 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4388 wxGrid::wxGridSelectionModes selmode
)
4390 bool checkSelection
= false;
4393 // stop all processing
4398 m_table
->SetView(0);
4410 checkSelection
= true;
4412 // kill row and column size arrays
4413 m_colWidths
.Empty();
4414 m_colRights
.Empty();
4415 m_rowHeights
.Empty();
4416 m_rowBottoms
.Empty();
4421 m_numRows
= table
->GetNumberRows();
4422 m_numCols
= table
->GetNumberCols();
4425 m_table
->SetView( this );
4426 m_ownTable
= takeOwnership
;
4427 m_selection
= new wxGridSelection( this, selmode
);
4430 // If the newly set table is smaller than the
4431 // original one current cell and selection regions
4432 // might be invalid,
4433 m_selectingKeyboard
= wxGridNoCellCoords
;
4434 m_currentCellCoords
=
4435 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
4436 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
4437 if (m_selectingTopLeft
.GetRow() >= m_numRows
||
4438 m_selectingTopLeft
.GetCol() >= m_numCols
)
4440 m_selectingTopLeft
= wxGridNoCellCoords
;
4441 m_selectingBottomRight
= wxGridNoCellCoords
;
4444 m_selectingBottomRight
=
4445 wxGridCellCoords(wxMin(m_numRows
,
4446 m_selectingBottomRight
.GetRow()),
4448 m_selectingBottomRight
.GetCol()));
4460 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4461 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4463 if ( m_rowLabelWin
)
4465 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4469 m_labelBackgroundColour
= *wxWHITE
;
4472 m_labelTextColour
= *wxBLACK
;
4475 m_attrCache
.row
= -1;
4476 m_attrCache
.col
= -1;
4477 m_attrCache
.attr
= NULL
;
4479 // TODO: something better than this ?
4481 m_labelFont
= this->GetFont();
4482 m_labelFont
.SetWeight( wxBOLD
);
4484 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4485 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4487 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4488 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4489 m_colLabelTextOrientation
= wxHORIZONTAL
;
4491 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4492 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4494 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4495 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4497 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4498 m_defaultRowHeight
+= 8;
4500 m_defaultRowHeight
+= 4;
4503 m_gridLineColour
= wxColour( 192,192,192 );
4504 m_gridLinesEnabled
= true;
4505 m_cellHighlightColour
= *wxBLACK
;
4506 m_cellHighlightPenWidth
= 2;
4507 m_cellHighlightROPenWidth
= 1;
4509 m_canDragColMove
= false;
4511 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4512 m_winCapture
= (wxWindow
*)NULL
;
4513 m_canDragRowSize
= true;
4514 m_canDragColSize
= true;
4515 m_canDragGridSize
= true;
4516 m_canDragCell
= false;
4518 m_dragRowOrCol
= -1;
4519 m_isDragging
= false;
4520 m_startDragPos
= wxDefaultPosition
;
4522 m_waitForSlowClick
= false;
4524 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4525 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4527 m_currentCellCoords
= wxGridNoCellCoords
;
4531 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4532 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4534 m_editable
= true; // default for whole grid
4536 m_inOnKeyDown
= false;
4542 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4543 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4546 // ----------------------------------------------------------------------------
4547 // the idea is to call these functions only when necessary because they create
4548 // quite big arrays which eat memory mostly unnecessary - in particular, if
4549 // default widths/heights are used for all rows/columns, we may not use these
4552 // with some extra code, it should be possible to only store the widths/heights
4553 // different from default ones (resulting in space savings for huge grids) but
4554 // this is not done currently
4555 // ----------------------------------------------------------------------------
4557 void wxGrid::InitRowHeights()
4559 m_rowHeights
.Empty();
4560 m_rowBottoms
.Empty();
4562 m_rowHeights
.Alloc( m_numRows
);
4563 m_rowBottoms
.Alloc( m_numRows
);
4565 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4568 for ( int i
= 0; i
< m_numRows
; i
++ )
4570 rowBottom
+= m_defaultRowHeight
;
4571 m_rowBottoms
.Add( rowBottom
);
4575 void wxGrid::InitColWidths()
4577 m_colWidths
.Empty();
4578 m_colRights
.Empty();
4580 m_colWidths
.Alloc( m_numCols
);
4581 m_colRights
.Alloc( m_numCols
);
4583 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4586 for ( int i
= 0; i
< m_numCols
; i
++ )
4588 colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
4589 m_colRights
.Add( colRight
);
4593 int wxGrid::GetColWidth(int col
) const
4595 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4598 int wxGrid::GetColLeft(int col
) const
4600 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
4601 : m_colRights
[col
] - m_colWidths
[col
];
4604 int wxGrid::GetColRight(int col
) const
4606 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
4610 int wxGrid::GetRowHeight(int row
) const
4612 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4615 int wxGrid::GetRowTop(int row
) const
4617 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4618 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4621 int wxGrid::GetRowBottom(int row
) const
4623 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4624 : m_rowBottoms
[row
];
4627 void wxGrid::CalcDimensions()
4629 // compute the size of the scrollable area
4630 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
4631 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
4636 // take into account editor if shown
4637 if ( IsCellEditControlShown() )
4640 int r
= m_currentCellCoords
.GetRow();
4641 int c
= m_currentCellCoords
.GetCol();
4642 int x
= GetColLeft(c
);
4643 int y
= GetRowTop(r
);
4645 // how big is the editor
4646 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4647 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4648 editor
->GetControl()->GetSize(&w2
, &h2
);
4659 // preserve (more or less) the previous position
4661 GetViewStart( &x
, &y
);
4663 // ensure the position is valid for the new scroll ranges
4665 x
= wxMax( w
- 1, 0 );
4667 y
= wxMax( h
- 1, 0 );
4669 // do set scrollbar parameters
4670 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4671 GetScrollX(w
), GetScrollY(h
),
4673 GetBatchCount() != 0);
4675 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4676 // still must reposition the children
4680 void wxGrid::CalcWindowSizes()
4682 // escape if the window is has not been fully created yet
4684 if ( m_cornerLabelWin
== NULL
)
4688 GetClientSize( &cw
, &ch
);
4690 // this block of code tries to work around the following problem: the grid
4691 // could have been just resized to have enough space to show the full grid
4692 // window contents without the scrollbars, but its client size could be
4693 // not big enough because the grid has the scrollbars right now and so the
4694 // scrollbars would remain even though we don't need them any more
4696 // to prevent this from happening, check if we have enough space for
4697 // everything without the scrollbars and explicitly disable them then
4698 wxSize size
= GetSize() - GetWindowBorderSize();
4699 if ( size
!= wxSize(cw
, ch
) )
4701 // check if we have enough space for grid window after accounting for
4702 // the fixed size elements
4703 size
.x
-= m_rowLabelWidth
;
4704 size
.y
-= m_colLabelHeight
;
4706 const wxSize vsize
= m_gridWin
->GetVirtualSize();
4708 if ( size
.x
>= vsize
.x
&& size
.y
>= vsize
.y
)
4710 // yes, we do, so remove the scrollbars and use the new client size
4711 // (which should be the same as full window size - borders now)
4712 SetScrollbars(0, 0, 0, 0);
4713 GetClientSize(&cw
, &ch
);
4717 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4718 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4720 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4721 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
- m_rowLabelWidth
, m_colLabelHeight
);
4723 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4724 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
- m_colLabelHeight
);
4726 if ( m_gridWin
&& m_gridWin
->IsShown() )
4727 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
- m_rowLabelWidth
, ch
- m_colLabelHeight
);
4730 // this is called when the grid table sends a message
4731 // to indicate that it has been redimensioned
4733 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4736 bool result
= false;
4738 // Clear the attribute cache as the attribute might refer to a different
4739 // cell than stored in the cache after adding/removing rows/columns.
4742 // By the same reasoning, the editor should be dismissed if columns are
4743 // added or removed. And for consistency, it should IMHO always be
4744 // removed, not only if the cell "underneath" it actually changes.
4745 // For now, I intentionally do not save the editor's content as the
4746 // cell it might want to save that stuff to might no longer exist.
4747 HideCellEditControl();
4750 // if we were using the default widths/heights so far, we must change them
4752 if ( m_colWidths
.IsEmpty() )
4757 if ( m_rowHeights
.IsEmpty() )
4763 switch ( msg
.GetId() )
4765 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4767 size_t pos
= msg
.GetCommandInt();
4768 int numRows
= msg
.GetCommandInt2();
4770 m_numRows
+= numRows
;
4772 if ( !m_rowHeights
.IsEmpty() )
4774 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4775 m_rowBottoms
.Insert( 0, pos
, numRows
);
4779 bottom
= m_rowBottoms
[pos
- 1];
4781 for ( i
= pos
; i
< m_numRows
; i
++ )
4783 bottom
+= m_rowHeights
[i
];
4784 m_rowBottoms
[i
] = bottom
;
4788 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4790 // if we have just inserted cols into an empty grid the current
4791 // cell will be undefined...
4793 SetCurrentCell( 0, 0 );
4797 m_selection
->UpdateRows( pos
, numRows
);
4798 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4800 attrProvider
->UpdateAttrRows( pos
, numRows
);
4802 if ( !GetBatchCount() )
4805 m_rowLabelWin
->Refresh();
4811 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4813 int numRows
= msg
.GetCommandInt();
4814 int oldNumRows
= m_numRows
;
4815 m_numRows
+= numRows
;
4817 if ( !m_rowHeights
.IsEmpty() )
4819 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4820 m_rowBottoms
.Add( 0, numRows
);
4823 if ( oldNumRows
> 0 )
4824 bottom
= m_rowBottoms
[oldNumRows
- 1];
4826 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4828 bottom
+= m_rowHeights
[i
];
4829 m_rowBottoms
[i
] = bottom
;
4833 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4835 // if we have just inserted cols into an empty grid the current
4836 // cell will be undefined...
4838 SetCurrentCell( 0, 0 );
4841 if ( !GetBatchCount() )
4844 m_rowLabelWin
->Refresh();
4850 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4852 size_t pos
= msg
.GetCommandInt();
4853 int numRows
= msg
.GetCommandInt2();
4854 m_numRows
-= numRows
;
4856 if ( !m_rowHeights
.IsEmpty() )
4858 m_rowHeights
.RemoveAt( pos
, numRows
);
4859 m_rowBottoms
.RemoveAt( pos
, numRows
);
4862 for ( i
= 0; i
< m_numRows
; i
++ )
4864 h
+= m_rowHeights
[i
];
4865 m_rowBottoms
[i
] = h
;
4871 m_currentCellCoords
= wxGridNoCellCoords
;
4875 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4876 m_currentCellCoords
.Set( 0, 0 );
4880 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4881 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4884 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4886 // ifdef'd out following patch from Paul Gammans
4888 // No need to touch column attributes, unless we
4889 // removed _all_ rows, in this case, we remove
4890 // all column attributes.
4891 // I hate to do this here, but the
4892 // needed data is not available inside UpdateAttrRows.
4893 if ( !GetNumberRows() )
4894 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4898 if ( !GetBatchCount() )
4901 m_rowLabelWin
->Refresh();
4907 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4909 size_t pos
= msg
.GetCommandInt();
4910 int numCols
= msg
.GetCommandInt2();
4911 m_numCols
+= numCols
;
4913 if ( !m_colAt
.IsEmpty() )
4915 //Shift the column IDs
4917 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
4919 if ( m_colAt
[i
] >= (int)pos
)
4920 m_colAt
[i
] += numCols
;
4923 m_colAt
.Insert( pos
, pos
, numCols
);
4925 //Set the new columns' positions
4926 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
4932 if ( !m_colWidths
.IsEmpty() )
4934 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4935 m_colRights
.Insert( 0, pos
, numCols
);
4939 right
= m_colRights
[GetColAt( pos
- 1 )];
4942 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
4944 i
= GetColAt( colPos
);
4946 right
+= m_colWidths
[i
];
4947 m_colRights
[i
] = right
;
4951 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4953 // if we have just inserted cols into an empty grid the current
4954 // cell will be undefined...
4956 SetCurrentCell( 0, 0 );
4960 m_selection
->UpdateCols( pos
, numCols
);
4961 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4963 attrProvider
->UpdateAttrCols( pos
, numCols
);
4964 if ( !GetBatchCount() )
4967 m_colLabelWin
->Refresh();
4973 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4975 int numCols
= msg
.GetCommandInt();
4976 int oldNumCols
= m_numCols
;
4977 m_numCols
+= numCols
;
4979 if ( !m_colAt
.IsEmpty() )
4981 m_colAt
.Add( 0, numCols
);
4983 //Set the new columns' positions
4985 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4991 if ( !m_colWidths
.IsEmpty() )
4993 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4994 m_colRights
.Add( 0, numCols
);
4997 if ( oldNumCols
> 0 )
4998 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
5001 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
5003 i
= GetColAt( colPos
);
5005 right
+= m_colWidths
[i
];
5006 m_colRights
[i
] = right
;
5010 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5012 // if we have just inserted cols into an empty grid the current
5013 // cell will be undefined...
5015 SetCurrentCell( 0, 0 );
5017 if ( !GetBatchCount() )
5020 m_colLabelWin
->Refresh();
5026 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5028 size_t pos
= msg
.GetCommandInt();
5029 int numCols
= msg
.GetCommandInt2();
5030 m_numCols
-= numCols
;
5032 if ( !m_colAt
.IsEmpty() )
5034 int colID
= GetColAt( pos
);
5036 m_colAt
.RemoveAt( pos
, numCols
);
5038 //Shift the column IDs
5040 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5042 if ( m_colAt
[colPos
] > colID
)
5043 m_colAt
[colPos
] -= numCols
;
5047 if ( !m_colWidths
.IsEmpty() )
5049 m_colWidths
.RemoveAt( pos
, numCols
);
5050 m_colRights
.RemoveAt( pos
, numCols
);
5054 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
5056 i
= GetColAt( colPos
);
5058 w
+= m_colWidths
[i
];
5065 m_currentCellCoords
= wxGridNoCellCoords
;
5069 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
5070 m_currentCellCoords
.Set( 0, 0 );
5074 m_selection
->UpdateCols( pos
, -((int)numCols
) );
5075 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
5078 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
5080 // ifdef'd out following patch from Paul Gammans
5082 // No need to touch row attributes, unless we
5083 // removed _all_ columns, in this case, we remove
5084 // all row attributes.
5085 // I hate to do this here, but the
5086 // needed data is not available inside UpdateAttrCols.
5087 if ( !GetNumberCols() )
5088 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
5092 if ( !GetBatchCount() )
5095 m_colLabelWin
->Refresh();
5102 if (result
&& !GetBatchCount() )
5103 m_gridWin
->Refresh();
5108 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
5110 wxRegionIterator
iter( reg
);
5113 wxArrayInt rowlabels
;
5120 // TODO: remove this when we can...
5121 // There is a bug in wxMotif that gives garbage update
5122 // rectangles if you jump-scroll a long way by clicking the
5123 // scrollbar with middle button. This is a work-around
5125 #if defined(__WXMOTIF__)
5127 m_gridWin
->GetClientSize( &cw
, &ch
);
5128 if ( r
.GetTop() > ch
)
5130 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5133 // logical bounds of update region
5136 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
5137 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
5139 // find the row labels within these bounds
5142 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5144 if ( GetRowBottom(row
) < top
)
5147 if ( GetRowTop(row
) > bottom
)
5150 rowlabels
.Add( row
);
5159 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
5161 wxRegionIterator
iter( reg
);
5164 wxArrayInt colLabels
;
5171 // TODO: remove this when we can...
5172 // There is a bug in wxMotif that gives garbage update
5173 // rectangles if you jump-scroll a long way by clicking the
5174 // scrollbar with middle button. This is a work-around
5176 #if defined(__WXMOTIF__)
5178 m_gridWin
->GetClientSize( &cw
, &ch
);
5179 if ( r
.GetLeft() > cw
)
5181 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5184 // logical bounds of update region
5187 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
5188 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
5190 // find the cells within these bounds
5194 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5196 col
= GetColAt( colPos
);
5198 if ( GetColRight(col
) < left
)
5201 if ( GetColLeft(col
) > right
)
5204 colLabels
.Add( col
);
5213 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
5215 wxRegionIterator
iter( reg
);
5218 wxGridCellCoordsArray cellsExposed
;
5220 int left
, top
, right
, bottom
;
5225 // TODO: remove this when we can...
5226 // There is a bug in wxMotif that gives garbage update
5227 // rectangles if you jump-scroll a long way by clicking the
5228 // scrollbar with middle button. This is a work-around
5230 #if defined(__WXMOTIF__)
5232 m_gridWin
->GetClientSize( &cw
, &ch
);
5233 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5234 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5235 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5236 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5239 // logical bounds of update region
5241 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5242 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5244 // find the cells within these bounds
5247 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5249 if ( GetRowBottom(row
) <= top
)
5252 if ( GetRowTop(row
) > bottom
)
5256 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
5258 col
= GetColAt( colPos
);
5260 if ( GetColRight(col
) <= left
)
5263 if ( GetColLeft(col
) > right
)
5266 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5273 return cellsExposed
;
5277 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5280 wxPoint
pos( event
.GetPosition() );
5281 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5283 if ( event
.Dragging() )
5287 m_isDragging
= true;
5288 m_rowLabelWin
->CaptureMouse();
5291 if ( event
.LeftIsDown() )
5293 switch ( m_cursorMode
)
5295 case WXGRID_CURSOR_RESIZE_ROW
:
5297 int cw
, ch
, left
, dummy
;
5298 m_gridWin
->GetClientSize( &cw
, &ch
);
5299 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5301 wxClientDC
dc( m_gridWin
);
5304 GetRowTop(m_dragRowOrCol
) +
5305 GetRowMinimalHeight(m_dragRowOrCol
) );
5306 dc
.SetLogicalFunction(wxINVERT
);
5307 if ( m_dragLastPos
>= 0 )
5309 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5311 dc
.DrawLine( left
, y
, left
+cw
, y
);
5316 case WXGRID_CURSOR_SELECT_ROW
:
5318 if ( (row
= YToRow( y
)) >= 0 )
5322 m_selection
->SelectRow( row
,
5323 event
.ControlDown(),
5332 // default label to suppress warnings about "enumeration value
5333 // 'xxx' not handled in switch
5341 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5346 if (m_rowLabelWin
->HasCapture())
5347 m_rowLabelWin
->ReleaseMouse();
5348 m_isDragging
= false;
5351 // ------------ Entering or leaving the window
5353 if ( event
.Entering() || event
.Leaving() )
5355 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5358 // ------------ Left button pressed
5360 else if ( event
.LeftDown() )
5362 // don't send a label click event for a hit on the
5363 // edge of the row label - this is probably the user
5364 // wanting to resize the row
5366 if ( YToEdgeOfRow(y
) < 0 )
5370 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5372 if ( !event
.ShiftDown() && !event
.CmdDown() )
5376 if ( event
.ShiftDown() )
5378 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5381 GetNumberCols() - 1,
5382 event
.ControlDown(),
5389 m_selection
->SelectRow( row
,
5390 event
.ControlDown(),
5397 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5402 // starting to drag-resize a row
5403 if ( CanDragRowSize() )
5404 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5408 // ------------ Left double click
5410 else if (event
.LeftDClick() )
5412 row
= YToEdgeOfRow(y
);
5417 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5419 // no default action at the moment
5424 // adjust row height depending on label text
5425 AutoSizeRowLabelSize( row
);
5427 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5432 // ------------ Left button released
5434 else if ( event
.LeftUp() )
5436 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5438 DoEndDragResizeRow();
5440 // Note: we are ending the event *after* doing
5441 // default processing in this case
5443 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5446 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5450 // ------------ Right button down
5452 else if ( event
.RightDown() )
5456 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5458 // no default action at the moment
5462 // ------------ Right double click
5464 else if ( event
.RightDClick() )
5468 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5470 // no default action at the moment
5474 // ------------ No buttons down and mouse moving
5476 else if ( event
.Moving() )
5478 m_dragRowOrCol
= YToEdgeOfRow( y
);
5479 if ( m_dragRowOrCol
>= 0 )
5481 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5483 // don't capture the mouse yet
5484 if ( CanDragRowSize() )
5485 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5488 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5490 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5495 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5498 wxPoint
pos( event
.GetPosition() );
5499 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5501 if ( event
.Dragging() )
5505 m_isDragging
= true;
5506 m_colLabelWin
->CaptureMouse();
5508 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
)
5509 m_dragRowOrCol
= XToCol( x
);
5512 if ( event
.LeftIsDown() )
5514 switch ( m_cursorMode
)
5516 case WXGRID_CURSOR_RESIZE_COL
:
5518 int cw
, ch
, dummy
, top
;
5519 m_gridWin
->GetClientSize( &cw
, &ch
);
5520 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5522 wxClientDC
dc( m_gridWin
);
5525 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5526 GetColMinimalWidth(m_dragRowOrCol
));
5527 dc
.SetLogicalFunction(wxINVERT
);
5528 if ( m_dragLastPos
>= 0 )
5530 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
5532 dc
.DrawLine( x
, top
, x
, top
+ ch
);
5537 case WXGRID_CURSOR_SELECT_COL
:
5539 if ( (col
= XToCol( x
)) >= 0 )
5543 m_selection
->SelectCol( col
,
5544 event
.ControlDown(),
5553 case WXGRID_CURSOR_MOVE_COL
:
5556 m_moveToCol
= GetColAt( 0 );
5558 m_moveToCol
= XToCol( x
);
5562 if ( m_moveToCol
< 0 )
5563 markerX
= GetColRight( GetColAt( m_numCols
- 1 ) );
5565 markerX
= GetColLeft( m_moveToCol
);
5567 if ( markerX
!= m_dragLastPos
)
5569 wxClientDC
dc( m_colLabelWin
);
5572 m_colLabelWin
->GetClientSize( &cw
, &ch
);
5576 //Clean up the last indicator
5577 if ( m_dragLastPos
>= 0 )
5579 wxPen
pen( m_colLabelWin
->GetBackgroundColour(), 2 );
5581 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
5582 dc
.SetPen(wxNullPen
);
5584 if ( XToCol( m_dragLastPos
) != -1 )
5585 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
5588 //Moving to the same place? Don't draw a marker
5589 if ( (m_moveToCol
== m_dragRowOrCol
)
5590 || (GetColPos( m_moveToCol
) == GetColPos( m_dragRowOrCol
) + 1)
5591 || (m_moveToCol
< 0 && m_dragRowOrCol
== GetColAt( m_numCols
- 1 )))
5598 wxPen
pen( *wxBLUE
, 2 );
5601 dc
.DrawLine( markerX
, 0, markerX
, ch
);
5603 dc
.SetPen(wxNullPen
);
5605 m_dragLastPos
= markerX
- 1;
5610 // default label to suppress warnings about "enumeration value
5611 // 'xxx' not handled in switch
5619 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5624 if (m_colLabelWin
->HasCapture())
5625 m_colLabelWin
->ReleaseMouse();
5626 m_isDragging
= false;
5629 // ------------ Entering or leaving the window
5631 if ( event
.Entering() || event
.Leaving() )
5633 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5636 // ------------ Left button pressed
5638 else if ( event
.LeftDown() )
5640 // don't send a label click event for a hit on the
5641 // edge of the col label - this is probably the user
5642 // wanting to resize the col
5644 if ( XToEdgeOfCol(x
) < 0 )
5648 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5650 if ( m_canDragColMove
)
5652 //Show button as pressed
5653 wxClientDC
dc( m_colLabelWin
);
5654 int colLeft
= GetColLeft( col
);
5655 int colRight
= GetColRight( col
) - 1;
5656 dc
.SetPen( wxPen( m_colLabelWin
->GetBackgroundColour(), 1 ) );
5657 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
5658 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5660 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, m_colLabelWin
);
5664 if ( !event
.ShiftDown() && !event
.CmdDown() )
5668 if ( event
.ShiftDown() )
5670 m_selection
->SelectBlock( 0,
5671 m_currentCellCoords
.GetCol(),
5672 GetNumberRows() - 1, col
,
5673 event
.ControlDown(),
5680 m_selection
->SelectCol( col
,
5681 event
.ControlDown(),
5688 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5694 // starting to drag-resize a col
5696 if ( CanDragColSize() )
5697 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5701 // ------------ Left double click
5703 if ( event
.LeftDClick() )
5705 col
= XToEdgeOfCol(x
);
5710 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5712 // no default action at the moment
5717 // adjust column width depending on label text
5718 AutoSizeColLabelSize( col
);
5720 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5725 // ------------ Left button released
5727 else if ( event
.LeftUp() )
5729 switch ( m_cursorMode
)
5731 case WXGRID_CURSOR_RESIZE_COL
:
5732 DoEndDragResizeCol();
5734 // Note: we are ending the event *after* doing
5735 // default processing in this case
5737 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5740 case WXGRID_CURSOR_MOVE_COL
:
5743 SendEvent( wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
, event
);
5746 case WXGRID_CURSOR_SELECT_COL
:
5747 case WXGRID_CURSOR_SELECT_CELL
:
5748 case WXGRID_CURSOR_RESIZE_ROW
:
5749 case WXGRID_CURSOR_SELECT_ROW
:
5750 // nothing to do (?)
5754 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5758 // ------------ Right button down
5760 else if ( event
.RightDown() )
5764 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5766 // no default action at the moment
5770 // ------------ Right double click
5772 else if ( event
.RightDClick() )
5776 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5778 // no default action at the moment
5782 // ------------ No buttons down and mouse moving
5784 else if ( event
.Moving() )
5786 m_dragRowOrCol
= XToEdgeOfCol( x
);
5787 if ( m_dragRowOrCol
>= 0 )
5789 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5791 // don't capture the cursor yet
5792 if ( CanDragColSize() )
5793 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5796 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5798 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5803 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5805 if ( event
.LeftDown() )
5807 // indicate corner label by having both row and
5810 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5815 else if ( event
.LeftDClick() )
5817 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5819 else if ( event
.RightDown() )
5821 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5823 // no default action at the moment
5826 else if ( event
.RightDClick() )
5828 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5830 // no default action at the moment
5835 void wxGrid::CancelMouseCapture()
5837 // cancel operation currently in progress, whatever it is
5840 m_isDragging
= false;
5841 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
5842 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
5843 m_winCapture
= NULL
;
5845 // remove traces of whatever we drew on screen
5850 void wxGrid::ChangeCursorMode(CursorMode mode
,
5855 static const wxChar
*cursorModes
[] =
5865 wxLogTrace(_T("grid"),
5866 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5867 win
== m_colLabelWin
? _T("colLabelWin")
5868 : win
? _T("rowLabelWin")
5870 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5873 if ( mode
== m_cursorMode
&&
5874 win
== m_winCapture
&&
5875 captureMouse
== (m_winCapture
!= NULL
))
5880 // by default use the grid itself
5886 if (m_winCapture
->HasCapture())
5887 m_winCapture
->ReleaseMouse();
5888 m_winCapture
= (wxWindow
*)NULL
;
5891 m_cursorMode
= mode
;
5893 switch ( m_cursorMode
)
5895 case WXGRID_CURSOR_RESIZE_ROW
:
5896 win
->SetCursor( m_rowResizeCursor
);
5899 case WXGRID_CURSOR_RESIZE_COL
:
5900 win
->SetCursor( m_colResizeCursor
);
5903 case WXGRID_CURSOR_MOVE_COL
:
5904 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
5908 win
->SetCursor( *wxSTANDARD_CURSOR
);
5912 // we need to capture mouse when resizing
5913 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5914 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5916 if ( captureMouse
&& resize
)
5918 win
->CaptureMouse();
5923 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5926 wxPoint
pos( event
.GetPosition() );
5927 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5929 wxGridCellCoords coords
;
5930 XYToCell( x
, y
, coords
);
5932 int cell_rows
, cell_cols
;
5933 bool isFirstDrag
= !m_isDragging
;
5934 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5935 if ((cell_rows
< 0) || (cell_cols
< 0))
5937 coords
.SetRow(coords
.GetRow() + cell_rows
);
5938 coords
.SetCol(coords
.GetCol() + cell_cols
);
5941 if ( event
.Dragging() )
5943 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5945 // Don't start doing anything until the mouse has been dragged at
5946 // least 3 pixels in any direction...
5949 if (m_startDragPos
== wxDefaultPosition
)
5951 m_startDragPos
= pos
;
5954 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5958 m_isDragging
= true;
5959 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5961 // Hide the edit control, so it
5962 // won't interfere with drag-shrinking.
5963 if ( IsCellEditControlShown() )
5965 HideCellEditControl();
5966 SaveEditControlValue();
5969 if ( coords
!= wxGridNoCellCoords
)
5971 if ( event
.CmdDown() )
5973 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5974 m_selectingKeyboard
= coords
;
5975 HighlightBlock( m_selectingKeyboard
, coords
);
5977 else if ( CanDragCell() )
5981 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5982 m_selectingKeyboard
= coords
;
5984 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5993 if ( !IsSelection() )
5995 HighlightBlock( coords
, coords
);
5999 HighlightBlock( m_currentCellCoords
, coords
);
6003 if (! IsVisible(coords
))
6005 MakeCellVisible(coords
);
6006 // TODO: need to introduce a delay or something here. The
6007 // scrolling is way to fast, at least on MSW - also on GTK.
6010 // Have we captured the mouse yet?
6013 m_winCapture
= m_gridWin
;
6014 m_winCapture
->CaptureMouse();
6019 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6021 int cw
, ch
, left
, dummy
;
6022 m_gridWin
->GetClientSize( &cw
, &ch
);
6023 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6025 wxClientDC
dc( m_gridWin
);
6027 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
6028 GetRowMinimalHeight(m_dragRowOrCol
) );
6029 dc
.SetLogicalFunction(wxINVERT
);
6030 if ( m_dragLastPos
>= 0 )
6032 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
6034 dc
.DrawLine( left
, y
, left
+cw
, y
);
6037 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_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 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
6046 GetColMinimalWidth(m_dragRowOrCol
) );
6047 dc
.SetLogicalFunction(wxINVERT
);
6048 if ( m_dragLastPos
>= 0 )
6050 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6052 dc
.DrawLine( x
, top
, x
, top
+ ch
);
6059 m_isDragging
= false;
6060 m_startDragPos
= wxDefaultPosition
;
6062 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
6063 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
6066 if ( event
.Entering() || event
.Leaving() )
6068 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6069 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
6074 // ------------ Left button pressed
6076 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
6078 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
6083 if ( !event
.CmdDown() )
6085 if ( event
.ShiftDown() )
6089 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
6090 m_currentCellCoords
.GetCol(),
6093 event
.ControlDown(),
6099 else if ( XToEdgeOfCol(x
) < 0 &&
6100 YToEdgeOfRow(y
) < 0 )
6102 DisableCellEditControl();
6103 MakeCellVisible( coords
);
6105 if ( event
.CmdDown() )
6109 m_selection
->ToggleCellSelection( coords
.GetRow(),
6111 event
.ControlDown(),
6116 m_selectingTopLeft
= wxGridNoCellCoords
;
6117 m_selectingBottomRight
= wxGridNoCellCoords
;
6118 m_selectingKeyboard
= coords
;
6122 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
6123 SetCurrentCell( coords
);
6126 if ( m_selection
->GetSelectionMode() !=
6127 wxGrid::wxGridSelectCells
)
6129 HighlightBlock( coords
, coords
);
6137 // ------------ Left double click
6139 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
6141 DisableCellEditControl();
6143 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
6145 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
6150 // we want double click to select a cell and start editing
6151 // (i.e. to behave in same way as sequence of two slow clicks):
6152 m_waitForSlowClick
= true;
6157 // ------------ Left button released
6159 else if ( event
.LeftUp() )
6161 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6165 if (m_winCapture
->HasCapture())
6166 m_winCapture
->ReleaseMouse();
6167 m_winCapture
= NULL
;
6170 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
6173 EnableCellEditControl();
6175 wxGridCellAttr
*attr
= GetCellAttr(coords
);
6176 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
6177 editor
->StartingClick();
6181 m_waitForSlowClick
= false;
6183 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6184 m_selectingBottomRight
!= wxGridNoCellCoords
)
6188 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6189 m_selectingTopLeft
.GetCol(),
6190 m_selectingBottomRight
.GetRow(),
6191 m_selectingBottomRight
.GetCol(),
6192 event
.ControlDown(),
6198 m_selectingTopLeft
= wxGridNoCellCoords
;
6199 m_selectingBottomRight
= wxGridNoCellCoords
;
6201 // Show the edit control, if it has been hidden for
6203 ShowCellEditControl();
6206 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
6208 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6209 DoEndDragResizeRow();
6211 // Note: we are ending the event *after* doing
6212 // default processing in this case
6214 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
6216 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
6218 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6219 DoEndDragResizeCol();
6221 // Note: we are ending the event *after* doing
6222 // default processing in this case
6224 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
6230 // ------------ Right button down
6232 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
6234 DisableCellEditControl();
6235 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
6240 // no default action at the moment
6244 // ------------ Right double click
6246 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
6248 DisableCellEditControl();
6249 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
6254 // no default action at the moment
6258 // ------------ Moving and no button action
6260 else if ( event
.Moving() && !event
.IsButton() )
6262 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
6264 // out of grid cell area
6265 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6269 int dragRow
= YToEdgeOfRow( y
);
6270 int dragCol
= XToEdgeOfCol( x
);
6272 // Dragging on the corner of a cell to resize in both
6273 // directions is not implemented yet...
6275 if ( dragRow
>= 0 && dragCol
>= 0 )
6277 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6283 m_dragRowOrCol
= dragRow
;
6285 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6287 if ( CanDragRowSize() && CanDragGridSize() )
6288 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
6291 else if ( dragCol
>= 0 )
6293 m_dragRowOrCol
= dragCol
;
6295 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
6297 if ( CanDragColSize() && CanDragGridSize() )
6298 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
6301 else // Neither on a row or col edge
6303 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
6305 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
6311 void wxGrid::DoEndDragResizeRow()
6313 if ( m_dragLastPos
>= 0 )
6315 // erase the last line and resize the row
6317 int cw
, ch
, left
, dummy
;
6318 m_gridWin
->GetClientSize( &cw
, &ch
);
6319 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
6321 wxClientDC
dc( m_gridWin
);
6323 dc
.SetLogicalFunction( wxINVERT
);
6324 dc
.DrawLine( left
, m_dragLastPos
, left
+ cw
, m_dragLastPos
);
6325 HideCellEditControl();
6326 SaveEditControlValue();
6328 int rowTop
= GetRowTop(m_dragRowOrCol
);
6329 SetRowSize( m_dragRowOrCol
,
6330 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
6332 if ( !GetBatchCount() )
6334 // Only needed to get the correct rect.y:
6335 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
6337 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6338 rect
.width
= m_rowLabelWidth
;
6339 rect
.height
= ch
- rect
.y
;
6340 m_rowLabelWin
->Refresh( true, &rect
);
6343 // if there is a multicell block, paint all of it
6346 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6347 int leftCol
= XToCol(left
);
6348 int rightCol
= internalXToCol(left
+ cw
);
6351 for (i
=leftCol
; i
<rightCol
; i
++)
6353 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6354 if (cell_rows
< subtract_rows
)
6355 subtract_rows
= cell_rows
;
6357 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6358 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6359 rect
.height
= ch
- rect
.y
;
6362 m_gridWin
->Refresh( false, &rect
);
6365 ShowCellEditControl();
6370 void wxGrid::DoEndDragResizeCol()
6372 if ( m_dragLastPos
>= 0 )
6374 // erase the last line and resize the col
6376 int cw
, ch
, dummy
, top
;
6377 m_gridWin
->GetClientSize( &cw
, &ch
);
6378 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6380 wxClientDC
dc( m_gridWin
);
6382 dc
.SetLogicalFunction( wxINVERT
);
6383 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
6384 HideCellEditControl();
6385 SaveEditControlValue();
6387 int colLeft
= GetColLeft(m_dragRowOrCol
);
6388 SetColSize( m_dragRowOrCol
,
6389 wxMax( m_dragLastPos
- colLeft
,
6390 GetColMinimalWidth(m_dragRowOrCol
) ) );
6392 if ( !GetBatchCount() )
6394 // Only needed to get the correct rect.x:
6395 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6397 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6398 rect
.width
= cw
- rect
.x
;
6399 rect
.height
= m_colLabelHeight
;
6400 m_colLabelWin
->Refresh( true, &rect
);
6403 // if there is a multicell block, paint all of it
6406 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6407 int topRow
= YToRow(top
);
6408 int bottomRow
= internalYToRow(top
+ cw
);
6411 for (i
=topRow
; i
<bottomRow
; i
++)
6413 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6414 if (cell_cols
< subtract_cols
)
6415 subtract_cols
= cell_cols
;
6418 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6419 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6420 rect
.width
= cw
- rect
.x
;
6424 m_gridWin
->Refresh( false, &rect
);
6427 ShowCellEditControl();
6431 void wxGrid::DoEndDragMoveCol()
6433 //The user clicked on the column but didn't actually drag
6434 if ( m_dragLastPos
< 0 )
6436 m_colLabelWin
->Refresh(); //Do this to "unpress" the column
6441 if ( m_moveToCol
== -1 )
6442 newPos
= m_numCols
- 1;
6445 newPos
= GetColPos( m_moveToCol
);
6446 if ( newPos
> GetColPos( m_dragRowOrCol
) )
6450 SetColPos( m_dragRowOrCol
, newPos
);
6453 void wxGrid::SetColPos( int colID
, int newPos
)
6455 if ( m_colAt
.IsEmpty() )
6457 m_colAt
.Alloc( m_numCols
);
6460 for ( i
= 0; i
< m_numCols
; i
++ )
6466 int oldPos
= GetColPos( colID
);
6468 //Reshuffle the m_colAt array
6469 if ( newPos
> oldPos
)
6472 for ( i
= oldPos
; i
< newPos
; i
++ )
6474 m_colAt
[i
] = m_colAt
[i
+1];
6480 for ( i
= oldPos
; i
> newPos
; i
-- )
6482 m_colAt
[i
] = m_colAt
[i
-1];
6486 m_colAt
[newPos
] = colID
;
6488 //Recalculate the column rights
6489 if ( !m_colWidths
.IsEmpty() )
6493 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6495 int colID
= GetColAt( colPos
);
6497 colRight
+= m_colWidths
[colID
];
6498 m_colRights
[colID
] = colRight
;
6502 m_colLabelWin
->Refresh();
6503 m_gridWin
->Refresh();
6508 void wxGrid::EnableDragColMove( bool enable
)
6510 if ( m_canDragColMove
== enable
)
6513 m_canDragColMove
= enable
;
6515 if ( !m_canDragColMove
)
6519 //Recalculate the column rights
6520 if ( !m_colWidths
.IsEmpty() )
6524 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
6526 colRight
+= m_colWidths
[colPos
];
6527 m_colRights
[colPos
] = colRight
;
6531 m_colLabelWin
->Refresh();
6532 m_gridWin
->Refresh();
6538 // ------ interaction with data model
6540 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6542 switch ( msg
.GetId() )
6544 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6545 return GetModelValues();
6547 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6548 return SetModelValues();
6550 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6551 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6552 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6553 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6554 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6555 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6556 return Redimension( msg
);
6563 // The behaviour of this function depends on the grid table class
6564 // Clear() function. For the default wxGridStringTable class the
6565 // behavious is to replace all cell contents with wxEmptyString but
6566 // not to change the number of rows or cols.
6568 void wxGrid::ClearGrid()
6572 if (IsCellEditControlEnabled())
6573 DisableCellEditControl();
6576 if (!GetBatchCount())
6577 m_gridWin
->Refresh();
6581 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6583 // TODO: something with updateLabels flag
6587 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6593 if (IsCellEditControlEnabled())
6594 DisableCellEditControl();
6596 bool done
= m_table
->InsertRows( pos
, numRows
);
6599 // the table will have sent the results of the insert row
6600 // operation to this view object as a grid table message
6606 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6608 // TODO: something with updateLabels flag
6612 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6618 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6621 // the table will have sent the results of the append row
6622 // operation to this view object as a grid table message
6628 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6630 // TODO: something with updateLabels flag
6634 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6640 if (IsCellEditControlEnabled())
6641 DisableCellEditControl();
6643 bool done
= m_table
->DeleteRows( pos
, numRows
);
6645 // the table will have sent the results of the delete row
6646 // operation to this view object as a grid table message
6652 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6654 // TODO: something with updateLabels flag
6658 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6664 if (IsCellEditControlEnabled())
6665 DisableCellEditControl();
6667 bool done
= m_table
->InsertCols( pos
, numCols
);
6669 // the table will have sent the results of the insert col
6670 // operation to this view object as a grid table message
6676 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6678 // TODO: something with updateLabels flag
6682 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6688 bool done
= m_table
->AppendCols( numCols
);
6690 // the table will have sent the results of the append col
6691 // operation to this view object as a grid table message
6697 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6699 // TODO: something with updateLabels flag
6703 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6709 if (IsCellEditControlEnabled())
6710 DisableCellEditControl();
6712 bool done
= m_table
->DeleteCols( pos
, numCols
);
6714 // the table will have sent the results of the delete col
6715 // operation to this view object as a grid table message
6722 // ----- event handlers
6725 // Generate a grid event based on a mouse event and
6726 // return the result of ProcessEvent()
6728 int wxGrid::SendEvent( const wxEventType type
,
6730 wxMouseEvent
& mouseEv
)
6732 bool claimed
, vetoed
;
6734 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6736 int rowOrCol
= (row
== -1 ? col
: row
);
6738 wxGridSizeEvent
gridEvt( GetId(),
6742 mouseEv
.GetX() + GetRowLabelSize(),
6743 mouseEv
.GetY() + GetColLabelSize(),
6744 mouseEv
.ControlDown(),
6745 mouseEv
.ShiftDown(),
6747 mouseEv
.MetaDown() );
6749 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6750 vetoed
= !gridEvt
.IsAllowed();
6752 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6754 // Right now, it should _never_ end up here!
6755 wxGridRangeSelectEvent
gridEvt( GetId(),
6759 m_selectingBottomRight
,
6761 mouseEv
.ControlDown(),
6762 mouseEv
.ShiftDown(),
6764 mouseEv
.MetaDown() );
6766 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6767 vetoed
= !gridEvt
.IsAllowed();
6769 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
6770 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
6771 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
6772 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
6774 wxPoint pos
= mouseEv
.GetPosition();
6776 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
6777 pos
.y
+= GetColLabelSize();
6778 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
6779 pos
.x
+= GetRowLabelSize();
6781 wxGridEvent
gridEvt( GetId(),
6788 mouseEv
.ControlDown(),
6789 mouseEv
.ShiftDown(),
6791 mouseEv
.MetaDown() );
6792 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6793 vetoed
= !gridEvt
.IsAllowed();
6797 wxGridEvent
gridEvt( GetId(),
6801 mouseEv
.GetX() + GetRowLabelSize(),
6802 mouseEv
.GetY() + GetColLabelSize(),
6804 mouseEv
.ControlDown(),
6805 mouseEv
.ShiftDown(),
6807 mouseEv
.MetaDown() );
6808 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6809 vetoed
= !gridEvt
.IsAllowed();
6812 // A Veto'd event may not be `claimed' so test this first
6816 return claimed
? 1 : 0;
6819 // Generate a grid event of specified type and return the result
6820 // of ProcessEvent().
6822 int wxGrid::SendEvent( const wxEventType type
,
6825 bool claimed
, vetoed
;
6827 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6829 int rowOrCol
= (row
== -1 ? col
: row
);
6831 wxGridSizeEvent
gridEvt( GetId(), type
, this, rowOrCol
);
6833 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6834 vetoed
= !gridEvt
.IsAllowed();
6838 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
6840 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6841 vetoed
= !gridEvt
.IsAllowed();
6844 // A Veto'd event may not be `claimed' so test this first
6848 return claimed
? 1 : 0;
6851 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6853 // needed to prevent zillions of paint events on MSW
6857 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6859 // Don't do anything if between Begin/EndBatch...
6860 // EndBatch() will do all this on the last nested one anyway.
6861 if (! GetBatchCount())
6863 // Refresh to get correct scrolled position:
6864 wxScrolledWindow::Refresh(eraseb
, rect
);
6868 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6869 int width_label
, width_cell
, height_label
, height_cell
;
6872 // Copy rectangle can get scroll offsets..
6873 rect_x
= rect
->GetX();
6874 rect_y
= rect
->GetY();
6875 rectWidth
= rect
->GetWidth();
6876 rectHeight
= rect
->GetHeight();
6878 width_label
= m_rowLabelWidth
- rect_x
;
6879 if (width_label
> rectWidth
)
6880 width_label
= rectWidth
;
6882 height_label
= m_colLabelHeight
- rect_y
;
6883 if (height_label
> rectHeight
)
6884 height_label
= rectHeight
;
6886 if (rect_x
> m_rowLabelWidth
)
6888 x
= rect_x
- m_rowLabelWidth
;
6889 width_cell
= rectWidth
;
6894 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6897 if (rect_y
> m_colLabelHeight
)
6899 y
= rect_y
- m_colLabelHeight
;
6900 height_cell
= rectHeight
;
6905 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6908 // Paint corner label part intersecting rect.
6909 if ( width_label
> 0 && height_label
> 0 )
6911 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6912 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6915 // Paint col labels part intersecting rect.
6916 if ( width_cell
> 0 && height_label
> 0 )
6918 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6919 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6922 // Paint row labels part intersecting rect.
6923 if ( width_label
> 0 && height_cell
> 0 )
6925 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6926 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6929 // Paint cell area part intersecting rect.
6930 if ( width_cell
> 0 && height_cell
> 0 )
6932 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6933 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6938 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6939 m_colLabelWin
->Refresh(eraseb
, NULL
);
6940 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6941 m_gridWin
->Refresh(eraseb
, NULL
);
6946 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
6948 // update our children window positions and scrollbars
6952 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6954 if ( m_inOnKeyDown
)
6956 // shouldn't be here - we are going round in circles...
6958 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6961 m_inOnKeyDown
= true;
6963 // propagate the event up and see if it gets processed
6964 wxWindow
*parent
= GetParent();
6965 wxKeyEvent
keyEvt( event
);
6966 keyEvt
.SetEventObject( parent
);
6968 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6970 if (GetLayoutDirection() == wxLayout_RightToLeft
)
6972 if (event
.GetKeyCode() == WXK_RIGHT
)
6973 event
.m_keyCode
= WXK_LEFT
;
6974 else if (event
.GetKeyCode() == WXK_LEFT
)
6975 event
.m_keyCode
= WXK_RIGHT
;
6978 // try local handlers
6979 switch ( event
.GetKeyCode() )
6982 if ( event
.ControlDown() )
6983 MoveCursorUpBlock( event
.ShiftDown() );
6985 MoveCursorUp( event
.ShiftDown() );
6989 if ( event
.ControlDown() )
6990 MoveCursorDownBlock( event
.ShiftDown() );
6992 MoveCursorDown( event
.ShiftDown() );
6996 if ( event
.ControlDown() )
6997 MoveCursorLeftBlock( event
.ShiftDown() );
6999 MoveCursorLeft( event
.ShiftDown() );
7003 if ( event
.ControlDown() )
7004 MoveCursorRightBlock( event
.ShiftDown() );
7006 MoveCursorRight( event
.ShiftDown() );
7010 case WXK_NUMPAD_ENTER
:
7011 if ( event
.ControlDown() )
7013 event
.Skip(); // to let the edit control have the return
7017 if ( GetGridCursorRow() < GetNumberRows()-1 )
7019 MoveCursorDown( event
.ShiftDown() );
7023 // at the bottom of a column
7024 DisableCellEditControl();
7034 if (event
.ShiftDown())
7036 if ( GetGridCursorCol() > 0 )
7038 MoveCursorLeft( false );
7043 DisableCellEditControl();
7048 if ( GetGridCursorCol() < GetNumberCols() - 1 )
7050 MoveCursorRight( false );
7055 DisableCellEditControl();
7061 if ( event
.ControlDown() )
7063 MakeCellVisible( 0, 0 );
7064 SetCurrentCell( 0, 0 );
7073 if ( event
.ControlDown() )
7075 MakeCellVisible( m_numRows
- 1, m_numCols
- 1 );
7076 SetCurrentCell( m_numRows
- 1, m_numCols
- 1 );
7093 if ( event
.ControlDown() )
7097 m_selection
->ToggleCellSelection(
7098 m_currentCellCoords
.GetRow(),
7099 m_currentCellCoords
.GetCol(),
7100 event
.ControlDown(),
7108 if ( !IsEditable() )
7109 MoveCursorRight( false );
7120 m_inOnKeyDown
= false;
7123 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
7125 // try local handlers
7127 if ( event
.GetKeyCode() == WXK_SHIFT
)
7129 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7130 m_selectingBottomRight
!= wxGridNoCellCoords
)
7134 m_selection
->SelectBlock(
7135 m_selectingTopLeft
.GetRow(),
7136 m_selectingTopLeft
.GetCol(),
7137 m_selectingBottomRight
.GetRow(),
7138 m_selectingBottomRight
.GetCol(),
7139 event
.ControlDown(),
7146 m_selectingTopLeft
= wxGridNoCellCoords
;
7147 m_selectingBottomRight
= wxGridNoCellCoords
;
7148 m_selectingKeyboard
= wxGridNoCellCoords
;
7152 void wxGrid::OnChar( wxKeyEvent
& event
)
7154 // is it possible to edit the current cell at all?
7155 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
7157 // yes, now check whether the cells editor accepts the key
7158 int row
= m_currentCellCoords
.GetRow();
7159 int col
= m_currentCellCoords
.GetCol();
7160 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7161 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7163 // <F2> is special and will always start editing, for
7164 // other keys - ask the editor itself
7165 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
7166 || editor
->IsAcceptedKey(event
) )
7168 // ensure cell is visble
7169 MakeCellVisible(row
, col
);
7170 EnableCellEditControl();
7172 // a problem can arise if the cell is not completely
7173 // visible (even after calling MakeCellVisible the
7174 // control is not created and calling StartingKey will
7176 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
7177 editor
->StartingKey(event
);
7193 void wxGrid::OnEraseBackground(wxEraseEvent
&)
7197 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
7199 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
7201 // the event has been intercepted - do nothing
7205 #if !(defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS)
7206 wxClientDC
dc( m_gridWin
);
7210 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
7212 DisableCellEditControl();
7214 if ( IsVisible( m_currentCellCoords
, false ) )
7217 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
7218 if ( !m_gridLinesEnabled
)
7226 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
7228 // Otherwise refresh redraws the highlight!
7229 m_currentCellCoords
= coords
;
7231 #if defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS
7232 m_gridWin
->Refresh(true /*, & r */);
7234 DrawGridCellArea( dc
, cells
);
7235 DrawAllGridLines( dc
, r
);
7240 m_currentCellCoords
= coords
;
7242 wxGridCellAttr
*attr
= GetCellAttr( coords
);
7243 #if !(defined(__WXMAC__) && wxMAC_USE_CORE_GRAPHICS)
7244 DrawCellHighlight( dc
, attr
);
7249 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7252 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7256 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
7259 rightCol
= GetNumberCols() - 1;
7261 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
7264 bottomRow
= GetNumberRows() - 1;
7268 if ( topRow
> bottomRow
)
7275 if ( leftCol
> rightCol
)
7282 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7283 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7285 // First the case that we selected a completely new area
7286 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
7287 m_selectingBottomRight
== wxGridNoCellCoords
)
7290 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
7291 wxGridCellCoords ( bottomRow
, rightCol
) );
7292 m_gridWin
->Refresh( false, &rect
);
7295 // Now handle changing an existing selection area.
7296 else if ( m_selectingTopLeft
!= updateTopLeft
||
7297 m_selectingBottomRight
!= updateBottomRight
)
7299 // Compute two optimal update rectangles:
7300 // Either one rectangle is a real subset of the
7301 // other, or they are (almost) disjoint!
7303 bool need_refresh
[4];
7307 need_refresh
[3] = false;
7310 // Store intermediate values
7311 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7312 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7313 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7314 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7316 // Determine the outer/inner coordinates.
7317 if (oldLeft
> leftCol
)
7323 if (oldTop
> topRow
)
7329 if (oldRight
< rightCol
)
7332 oldRight
= rightCol
;
7335 if (oldBottom
< bottomRow
)
7338 oldBottom
= bottomRow
;
7342 // Now, either the stuff marked old is the outer
7343 // rectangle or we don't have a situation where one
7344 // is contained in the other.
7346 if ( oldLeft
< leftCol
)
7348 // Refresh the newly selected or deselected
7349 // area to the left of the old or new selection.
7350 need_refresh
[0] = true;
7351 rect
[0] = BlockToDeviceRect(
7352 wxGridCellCoords( oldTop
, oldLeft
),
7353 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
7356 if ( oldTop
< topRow
)
7358 // Refresh the newly selected or deselected
7359 // area above the old or new selection.
7360 need_refresh
[1] = true;
7361 rect
[1] = BlockToDeviceRect(
7362 wxGridCellCoords( oldTop
, leftCol
),
7363 wxGridCellCoords( topRow
- 1, rightCol
) );
7366 if ( oldRight
> rightCol
)
7368 // Refresh the newly selected or deselected
7369 // area to the right of the old or new selection.
7370 need_refresh
[2] = true;
7371 rect
[2] = BlockToDeviceRect(
7372 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
7373 wxGridCellCoords( oldBottom
, oldRight
) );
7376 if ( oldBottom
> bottomRow
)
7378 // Refresh the newly selected or deselected
7379 // area below the old or new selection.
7380 need_refresh
[3] = true;
7381 rect
[3] = BlockToDeviceRect(
7382 wxGridCellCoords( bottomRow
+ 1, leftCol
),
7383 wxGridCellCoords( oldBottom
, rightCol
) );
7386 // various Refresh() calls
7387 for (i
= 0; i
< 4; i
++ )
7388 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7389 m_gridWin
->Refresh( false, &(rect
[i
]) );
7393 m_selectingTopLeft
= updateTopLeft
;
7394 m_selectingBottomRight
= updateBottomRight
;
7398 // ------ functions to get/send data (see also public functions)
7401 bool wxGrid::GetModelValues()
7403 // Hide the editor, so it won't hide a changed value.
7404 HideCellEditControl();
7408 // all we need to do is repaint the grid
7410 m_gridWin
->Refresh();
7417 bool wxGrid::SetModelValues()
7421 // Disable the editor, so it won't hide a changed value.
7422 // Do we also want to save the current value of the editor first?
7424 DisableCellEditControl();
7428 for ( row
= 0; row
< m_numRows
; row
++ )
7430 for ( col
= 0; col
< m_numCols
; col
++ )
7432 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
7442 // Note - this function only draws cells that are in the list of
7443 // exposed cells (usually set from the update region by
7444 // CalcExposedCells)
7446 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7448 if ( !m_numRows
|| !m_numCols
)
7451 int i
, numCells
= cells
.GetCount();
7452 int row
, col
, cell_rows
, cell_cols
;
7453 wxGridCellCoordsArray redrawCells
;
7455 for ( i
= numCells
- 1; i
>= 0; i
-- )
7457 row
= cells
[i
].GetRow();
7458 col
= cells
[i
].GetCol();
7459 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7461 // If this cell is part of a multicell block, find owner for repaint
7462 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7464 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
7465 bool marked
= false;
7466 for ( int j
= 0; j
< numCells
; j
++ )
7468 if ( cell
== cells
[j
] )
7477 int count
= redrawCells
.GetCount();
7478 for (int j
= 0; j
< count
; j
++)
7480 if ( cell
== redrawCells
[j
] )
7488 redrawCells
.Add( cell
);
7491 // don't bother drawing this cell
7495 // If this cell is empty, find cell to left that might want to overflow
7496 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7498 for ( int l
= 0; l
< cell_rows
; l
++ )
7500 // find a cell in this row to leave already marked for repaint
7502 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7503 if ((redrawCells
[k
].GetCol() < left
) &&
7504 (redrawCells
[k
].GetRow() == row
))
7506 left
= redrawCells
[k
].GetCol();
7510 left
= 0; // oh well
7512 for (int j
= col
- 1; j
>= left
; j
--)
7514 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7516 if (GetCellOverflow(row
+ l
, j
))
7518 wxGridCellCoords
cell(row
+ l
, j
);
7519 bool marked
= false;
7521 for (int k
= 0; k
< numCells
; k
++)
7523 if ( cell
== cells
[k
] )
7532 int count
= redrawCells
.GetCount();
7533 for (int k
= 0; k
< count
; k
++)
7535 if ( cell
== redrawCells
[k
] )
7542 redrawCells
.Add( cell
);
7551 DrawCell( dc
, cells
[i
] );
7554 numCells
= redrawCells
.GetCount();
7556 for ( i
= numCells
- 1; i
>= 0; i
-- )
7558 DrawCell( dc
, redrawCells
[i
] );
7562 void wxGrid::DrawGridSpace( wxDC
& dc
)
7565 m_gridWin
->GetClientSize( &cw
, &ch
);
7568 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7570 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
7571 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7573 if ( right
> rightCol
|| bottom
> bottomRow
)
7576 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7578 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7579 dc
.SetPen( *wxTRANSPARENT_PEN
);
7581 if ( right
> rightCol
)
7583 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7586 if ( bottom
> bottomRow
)
7588 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7593 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7595 int row
= coords
.GetRow();
7596 int col
= coords
.GetCol();
7598 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7601 // we draw the cell border ourselves
7602 #if !WXGRID_DRAW_LINES
7603 if ( m_gridLinesEnabled
)
7604 DrawCellBorder( dc
, coords
);
7607 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7609 bool isCurrent
= coords
== m_currentCellCoords
;
7611 wxRect rect
= CellToRect( row
, col
);
7613 // if the editor is shown, we should use it and not the renderer
7614 // Note: However, only if it is really _shown_, i.e. not hidden!
7615 if ( isCurrent
&& IsCellEditControlShown() )
7617 // NB: this "#if..." is temporary and fixes a problem where the
7618 // edit control is erased by this code after being rendered.
7619 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7620 // implicitly, causing this out-of order render.
7621 #if !defined(__WXMAC__)
7622 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7623 editor
->PaintBackground(rect
, attr
);
7629 // but all the rest is drawn by the cell renderer and hence may be customized
7630 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7631 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7638 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7640 int row
= m_currentCellCoords
.GetRow();
7641 int col
= m_currentCellCoords
.GetCol();
7643 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7646 wxRect rect
= CellToRect(row
, col
);
7648 // hmmm... what could we do here to show that the cell is disabled?
7649 // for now, I just draw a thinner border than for the other ones, but
7650 // it doesn't look really good
7652 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7656 // The center of the drawn line is where the position/width/height of
7657 // the rectangle is actually at (on wxMSW at least), so the
7658 // size of the rectangle is reduced to compensate for the thickness of
7659 // the line. If this is too strange on non-wxMSW platforms then
7660 // please #ifdef this appropriately.
7661 rect
.x
+= penWidth
/ 2;
7662 rect
.y
+= penWidth
/ 2;
7663 rect
.width
-= penWidth
- 1;
7664 rect
.height
-= penWidth
- 1;
7666 // Now draw the rectangle
7667 // use the cellHighlightColour if the cell is inside a selection, this
7668 // will ensure the cell is always visible.
7669 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7670 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7671 dc
.DrawRectangle(rect
);
7675 // VZ: my experiments with 3D borders...
7677 // how to properly set colours for arbitrary bg?
7678 wxCoord x1
= rect
.x
,
7680 x2
= rect
.x
+ rect
.width
- 1,
7681 y2
= rect
.y
+ rect
.height
- 1;
7683 dc
.SetPen(*wxWHITE_PEN
);
7684 dc
.DrawLine(x1
, y1
, x2
, y1
);
7685 dc
.DrawLine(x1
, y1
, x1
, y2
);
7687 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7688 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7690 dc
.SetPen(*wxBLACK_PEN
);
7691 dc
.DrawLine(x1
, y2
, x2
, y2
);
7692 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7696 wxPen
wxGrid::GetDefaultGridLinePen()
7698 return wxPen(GetGridLineColour(), 1, wxSOLID
);
7701 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
7703 return GetDefaultGridLinePen();
7706 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
7708 return GetDefaultGridLinePen();
7711 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7713 int row
= coords
.GetRow();
7714 int col
= coords
.GetCol();
7715 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7719 wxRect rect
= CellToRect( row
, col
);
7721 // right hand border
7722 dc
.SetPen( GetColGridLinePen(col
) );
7723 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7724 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7727 dc
.SetPen( GetRowGridLinePen(row
) );
7728 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7729 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7732 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7734 // This if block was previously in wxGrid::OnPaint but that doesn't
7735 // seem to get called under wxGTK - MB
7737 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7738 m_numRows
&& m_numCols
)
7740 m_currentCellCoords
.Set(0, 0);
7743 if ( IsCellEditControlShown() )
7745 // don't show highlight when the edit control is shown
7749 // if the active cell was repainted, repaint its highlight too because it
7750 // might have been damaged by the grid lines
7751 size_t count
= cells
.GetCount();
7752 for ( size_t n
= 0; n
< count
; n
++ )
7754 if ( cells
[n
] == m_currentCellCoords
)
7756 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7757 DrawCellHighlight(dc
, attr
);
7765 // TODO: remove this ???
7766 // This is used to redraw all grid lines e.g. when the grid line colour
7769 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7771 #if !WXGRID_DRAW_LINES
7775 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7778 int top
, bottom
, left
, right
;
7780 #if 0 //#ifndef __WXGTK__
7784 m_gridWin
->GetClientSize(&cw
, &ch
);
7786 // virtual coords of visible area
7788 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7789 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7794 reg
.GetBox(x
, y
, w
, h
);
7795 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7796 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7800 m_gridWin
->GetClientSize(&cw
, &ch
);
7801 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7802 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7805 // avoid drawing grid lines past the last row and col
7807 right
= wxMin( right
, GetColRight(GetColAt( m_numCols
- 1 )) );
7808 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7810 // no gridlines inside multicells, clip them out
7811 int leftCol
= GetColPos( internalXToCol(left
) );
7812 int topRow
= internalYToRow(top
);
7813 int rightCol
= GetColPos( internalXToCol(right
) );
7814 int bottomRow
= internalYToRow(bottom
);
7816 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7817 wxRegion
clippedcells(0, 0, cw
, ch
);
7819 int i
, j
, cell_rows
, cell_cols
;
7822 for (j
=topRow
; j
<=bottomRow
; j
++)
7825 for (colPos
=leftCol
; colPos
<=rightCol
; colPos
++)
7827 i
= GetColAt( colPos
);
7829 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7830 if ((cell_rows
> 1) || (cell_cols
> 1))
7832 rect
= CellToRect(j
,i
);
7833 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7834 clippedcells
.Subtract(rect
);
7836 else if ((cell_rows
< 0) || (cell_cols
< 0))
7838 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7839 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7840 clippedcells
.Subtract(rect
);
7845 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7847 int i
, j
, cell_rows
, cell_cols
;
7850 for (j
=topRow
; j
<=bottomRow
; j
++)
7852 for (i
=leftCol
; i
<=rightCol
; i
++)
7854 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7855 if ((cell_rows
> 1) || (cell_cols
> 1))
7857 rect
= CellToRect(j
, i
);
7858 clippedcells
.Subtract(rect
);
7860 else if ((cell_rows
< 0) || (cell_cols
< 0))
7862 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7863 clippedcells
.Subtract(rect
);
7869 dc
.SetClippingRegion( clippedcells
);
7872 // horizontal grid lines
7874 // already declared above - int i;
7875 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7877 int bot
= GetRowBottom(i
) - 1;
7886 dc
.SetPen( GetRowGridLinePen(i
) );
7887 dc
.DrawLine( left
, bot
, right
, bot
);
7891 // vertical grid lines
7894 for ( colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
7896 i
= GetColAt( colPos
);
7898 int colRight
= GetColRight(i
);
7900 if (GetLayoutDirection() != wxLayout_RightToLeft
)
7904 if ( colRight
> right
)
7909 if ( colRight
>= left
)
7911 dc
.SetPen( GetColGridLinePen(i
) );
7912 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7916 dc
.DestroyClippingRegion();
7919 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
7925 size_t numLabels
= rows
.GetCount();
7927 for ( i
= 0; i
< numLabels
; i
++ )
7929 DrawRowLabel( dc
, rows
[i
] );
7933 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7935 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7943 rect
.SetY( GetRowTop(row
) + 1 );
7944 rect
.SetWidth( m_rowLabelWidth
- 2 );
7945 rect
.SetHeight( GetRowHeight(row
) - 2 );
7947 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7949 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7951 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7953 int rowTop
= GetRowTop(row
),
7954 rowBottom
= GetRowBottom(row
) - 1;
7956 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
7957 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
7958 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7959 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7961 dc
.SetPen( *wxWHITE_PEN
);
7962 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7963 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
7966 dc
.SetBackgroundMode( wxTRANSPARENT
);
7967 dc
.SetTextForeground( GetLabelTextColour() );
7968 dc
.SetFont( GetLabelFont() );
7971 GetRowLabelAlignment( &hAlign
, &vAlign
);
7974 rect
.SetY( GetRowTop(row
) + 2 );
7975 rect
.SetWidth( m_rowLabelWidth
- 4 );
7976 rect
.SetHeight( GetRowHeight(row
) - 4 );
7977 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7980 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7986 size_t numLabels
= cols
.GetCount();
7988 for ( i
= 0; i
< numLabels
; i
++ )
7990 DrawColLabel( dc
, cols
[i
] );
7994 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7996 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7999 int colLeft
= GetColLeft(col
);
8005 rect
.SetX( colLeft
+ 1 );
8007 rect
.SetWidth( GetColWidth(col
) - 2 );
8008 rect
.SetHeight( m_colLabelHeight
- 2 );
8010 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
8012 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
8014 int colRight
= GetColRight(col
) - 1;
8016 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
), 1, wxSOLID
) );
8017 dc
.DrawLine( colRight
, 0, colRight
, m_colLabelHeight
- 1 );
8018 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
8019 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
8020 colRight
+ 1, m_colLabelHeight
- 1 );
8022 dc
.SetPen( *wxWHITE_PEN
);
8023 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
8024 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
8027 dc
.SetBackgroundMode( wxTRANSPARENT
);
8028 dc
.SetTextForeground( GetLabelTextColour() );
8029 dc
.SetFont( GetLabelFont() );
8031 int hAlign
, vAlign
, orient
;
8032 GetColLabelAlignment( &hAlign
, &vAlign
);
8033 orient
= GetColLabelTextOrientation();
8035 rect
.SetX( colLeft
+ 2 );
8037 rect
.SetWidth( GetColWidth(col
) - 4 );
8038 rect
.SetHeight( m_colLabelHeight
- 4 );
8039 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
8042 void wxGrid::DrawTextRectangle( wxDC
& dc
,
8043 const wxString
& value
,
8047 int textOrientation
)
8049 wxArrayString lines
;
8051 StringToLines( value
, lines
);
8053 // Forward to new API.
8054 DrawTextRectangle( dc
,
8062 // VZ: this should be replaced with wxDC::DrawLabel() to which we just have to
8063 // add textOrientation support
8064 void wxGrid::DrawTextRectangle(wxDC
& dc
,
8065 const wxArrayString
& lines
,
8069 int textOrientation
)
8071 if ( lines
.empty() )
8074 wxDCClipper
clip(dc
, rect
);
8079 if ( textOrientation
== wxHORIZONTAL
)
8080 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
8082 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
8086 switch ( vertAlign
)
8088 case wxALIGN_BOTTOM
:
8089 if ( textOrientation
== wxHORIZONTAL
)
8090 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
8092 x
= rect
.x
+ rect
.width
- textWidth
;
8095 case wxALIGN_CENTRE
:
8096 if ( textOrientation
== wxHORIZONTAL
)
8097 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
8099 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
8104 if ( textOrientation
== wxHORIZONTAL
)
8111 // Align each line of a multi-line label
8112 size_t nLines
= lines
.GetCount();
8113 for ( size_t l
= 0; l
< nLines
; l
++ )
8115 const wxString
& line
= lines
[l
];
8119 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
8125 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
8127 switch ( horizAlign
)
8130 if ( textOrientation
== wxHORIZONTAL
)
8131 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
8133 y
= rect
.y
+ lineWidth
+ 1;
8136 case wxALIGN_CENTRE
:
8137 if ( textOrientation
== wxHORIZONTAL
)
8138 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
8140 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
8145 if ( textOrientation
== wxHORIZONTAL
)
8148 y
= rect
.y
+ rect
.height
- 1;
8152 if ( textOrientation
== wxHORIZONTAL
)
8154 dc
.DrawText( line
, x
, y
);
8159 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
8165 // Split multi-line text up into an array of strings.
8166 // Any existing contents of the string array are preserved.
8168 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
8172 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
8173 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
8175 while ( startPos
< (int)tVal
.length() )
8177 pos
= tVal
.Mid(startPos
).Find( eol
);
8182 else if ( pos
== 0 )
8184 lines
.Add( wxEmptyString
);
8188 lines
.Add( value
.Mid(startPos
, pos
) );
8191 startPos
+= pos
+ 1;
8194 if ( startPos
< (int)value
.length() )
8196 lines
.Add( value
.Mid( startPos
) );
8200 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
8201 const wxArrayString
& lines
,
8202 long *width
, long *height
) const
8206 long lineW
= 0, lineH
= 0;
8209 for ( i
= 0; i
< lines
.GetCount(); i
++ )
8211 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
8212 w
= wxMax( w
, lineW
);
8221 // ------ Batch processing.
8223 void wxGrid::EndBatch()
8225 if ( m_batchCount
> 0 )
8228 if ( !m_batchCount
)
8231 m_rowLabelWin
->Refresh();
8232 m_colLabelWin
->Refresh();
8233 m_cornerLabelWin
->Refresh();
8234 m_gridWin
->Refresh();
8239 // Use this, rather than wxWindow::Refresh(), to force an immediate
8240 // repainting of the grid. Has no effect if you are already inside a
8241 // BeginBatch / EndBatch block.
8243 void wxGrid::ForceRefresh()
8249 bool wxGrid::Enable(bool enable
)
8251 if ( !wxScrolledWindow::Enable(enable
) )
8254 // redraw in the new state
8255 m_gridWin
->Refresh();
8261 // ------ Edit control functions
8264 void wxGrid::EnableEditing( bool edit
)
8266 // TODO: improve this ?
8268 if ( edit
!= m_editable
)
8271 EnableCellEditControl(edit
);
8276 void wxGrid::EnableCellEditControl( bool enable
)
8281 if ( enable
!= m_cellEditCtrlEnabled
)
8285 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
8288 // this should be checked by the caller!
8289 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
8291 // do it before ShowCellEditControl()
8292 m_cellEditCtrlEnabled
= enable
;
8294 ShowCellEditControl();
8298 //FIXME:add veto support
8299 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
8301 HideCellEditControl();
8302 SaveEditControlValue();
8304 // do it after HideCellEditControl()
8305 m_cellEditCtrlEnabled
= enable
;
8310 bool wxGrid::IsCurrentCellReadOnly() const
8313 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
8314 bool readonly
= attr
->IsReadOnly();
8320 bool wxGrid::CanEnableCellControl() const
8322 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
8323 !IsCurrentCellReadOnly();
8326 bool wxGrid::IsCellEditControlEnabled() const
8328 // the cell edit control might be disable for all cells or just for the
8329 // current one if it's read only
8330 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
8333 bool wxGrid::IsCellEditControlShown() const
8335 bool isShown
= false;
8337 if ( m_cellEditCtrlEnabled
)
8339 int row
= m_currentCellCoords
.GetRow();
8340 int col
= m_currentCellCoords
.GetCol();
8341 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8342 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
8347 if ( editor
->IsCreated() )
8349 isShown
= editor
->GetControl()->IsShown();
8359 void wxGrid::ShowCellEditControl()
8361 if ( IsCellEditControlEnabled() )
8363 if ( !IsVisible( m_currentCellCoords
, false ) )
8365 m_cellEditCtrlEnabled
= false;
8370 wxRect rect
= CellToRect( m_currentCellCoords
);
8371 int row
= m_currentCellCoords
.GetRow();
8372 int col
= m_currentCellCoords
.GetCol();
8374 // if this is part of a multicell, find owner (topleft)
8375 int cell_rows
, cell_cols
;
8376 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8377 if ( cell_rows
<= 0 || cell_cols
<= 0 )
8381 m_currentCellCoords
.SetRow( row
);
8382 m_currentCellCoords
.SetCol( col
);
8385 // erase the highlight and the cell contents because the editor
8386 // might not cover the entire cell
8387 wxClientDC
dc( m_gridWin
);
8389 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8390 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
8391 dc
.SetPen(*wxTRANSPARENT_PEN
);
8392 dc
.DrawRectangle(rect
);
8394 // convert to scrolled coords
8395 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8401 // cell is shifted by one pixel
8402 // However, don't allow x or y to become negative
8403 // since the SetSize() method interprets that as
8410 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8411 if ( !editor
->IsCreated() )
8413 editor
->Create(m_gridWin
, wxID_ANY
,
8414 new wxGridCellEditorEvtHandler(this, editor
));
8416 wxGridEditorCreatedEvent
evt(GetId(),
8417 wxEVT_GRID_EDITOR_CREATED
,
8421 editor
->GetControl());
8422 GetEventHandler()->ProcessEvent(evt
);
8425 // resize editor to overflow into righthand cells if allowed
8426 int maxWidth
= rect
.width
;
8427 wxString value
= GetCellValue(row
, col
);
8428 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
8431 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
8432 if (maxWidth
< rect
.width
)
8433 maxWidth
= rect
.width
;
8436 int client_right
= m_gridWin
->GetClientSize().GetWidth();
8437 if (rect
.x
+ maxWidth
> client_right
)
8438 maxWidth
= client_right
- rect
.x
;
8440 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
8442 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8443 // may have changed earlier
8444 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
8447 GetCellSize( row
, i
, &c_rows
, &c_cols
);
8449 // looks weird going over a multicell
8450 if (m_table
->IsEmptyCell( row
, i
) &&
8451 (rect
.width
< maxWidth
) && (c_rows
== 1))
8453 rect
.width
+= GetColWidth( i
);
8459 if (rect
.GetRight() > client_right
)
8460 rect
.SetRight( client_right
- 1 );
8463 editor
->SetCellAttr( attr
);
8464 editor
->SetSize( rect
);
8466 editor
->GetControl()->Move(
8467 editor
->GetControl()->GetPosition().x
+ nXMove
,
8468 editor
->GetControl()->GetPosition().y
);
8469 editor
->Show( true, attr
);
8471 // recalc dimensions in case we need to
8472 // expand the scrolled window to account for editor
8475 editor
->BeginEdit(row
, col
, this);
8476 editor
->SetCellAttr(NULL
);
8484 void wxGrid::HideCellEditControl()
8486 if ( IsCellEditControlEnabled() )
8488 int row
= m_currentCellCoords
.GetRow();
8489 int col
= m_currentCellCoords
.GetCol();
8491 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8492 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8493 editor
->Show( false );
8497 m_gridWin
->SetFocus();
8499 // refresh whole row to the right
8500 wxRect
rect( CellToRect(row
, col
) );
8501 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8502 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8505 // ensure that the pixels under the focus ring get refreshed as well
8506 rect
.Inflate(10, 10);
8509 m_gridWin
->Refresh( false, &rect
);
8513 void wxGrid::SaveEditControlValue()
8515 if ( IsCellEditControlEnabled() )
8517 int row
= m_currentCellCoords
.GetRow();
8518 int col
= m_currentCellCoords
.GetCol();
8520 wxString oldval
= GetCellValue(row
, col
);
8522 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8523 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8524 bool changed
= editor
->EndEdit(row
, col
, this);
8531 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8532 m_currentCellCoords
.GetRow(),
8533 m_currentCellCoords
.GetCol() ) < 0 )
8535 // Event has been vetoed, set the data back.
8536 SetCellValue(row
, col
, oldval
);
8543 // ------ Grid location functions
8544 // Note that all of these functions work with the logical coordinates of
8545 // grid cells and labels so you will need to convert from device
8546 // coordinates for mouse events etc.
8549 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
) const
8551 int row
= YToRow(y
);
8552 int col
= XToCol(x
);
8554 if ( row
== -1 || col
== -1 )
8556 coords
= wxGridNoCellCoords
;
8560 coords
.Set( row
, col
);
8564 // Internal Helper function for computing row or column from some
8565 // (unscrolled) coordinate value, using either
8566 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8567 // of m_rowBottoms/m_ColRights to speed up the search!
8569 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8570 const wxArrayInt
& BorderArray
, int nMax
,
8574 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8579 size_t i_max
= coord
/ defaultDist
,
8582 if (BorderArray
.IsEmpty())
8584 if ((int) i_max
< nMax
)
8586 return clipToMinMax
? nMax
- 1 : -1;
8589 if ( i_max
>= BorderArray
.GetCount())
8591 i_max
= BorderArray
.GetCount() - 1;
8595 if ( coord
>= BorderArray
[i_max
])
8599 i_max
= coord
/ minDist
;
8601 i_max
= BorderArray
.GetCount() - 1;
8604 if ( i_max
>= BorderArray
.GetCount())
8605 i_max
= BorderArray
.GetCount() - 1;
8608 if ( coord
>= BorderArray
[i_max
])
8609 return clipToMinMax
? (int)i_max
: -1;
8610 if ( coord
< BorderArray
[0] )
8613 while ( i_max
- i_min
> 0 )
8615 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8616 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8617 if (coord
>= BorderArray
[ i_max
- 1])
8621 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8622 if (coord
< BorderArray
[median
])
8631 int wxGrid::YToRow( int y
) const
8633 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8634 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8637 int wxGrid::XToCol( int x
, bool clipToMinMax
) const
8640 return clipToMinMax
&& (m_numCols
> 0) ? GetColAt( 0 ) : -1;
8642 wxASSERT_MSG(m_defaultColWidth
> 0, wxT("Default column width can not be zero"));
8644 int maxPos
= x
/ m_defaultColWidth
;
8647 if (m_colRights
.IsEmpty())
8649 if(maxPos
< m_numCols
)
8650 return GetColAt( maxPos
);
8651 return clipToMinMax
? GetColAt( m_numCols
- 1 ) : -1;
8654 if ( maxPos
>= m_numCols
)
8655 maxPos
= m_numCols
- 1;
8658 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8661 if (m_minAcceptableColWidth
)
8662 maxPos
= x
/ m_minAcceptableColWidth
;
8664 maxPos
= m_numCols
- 1;
8666 if ( maxPos
>= m_numCols
)
8667 maxPos
= m_numCols
- 1;
8670 //X is beyond the last column
8671 if ( x
>= m_colRights
[GetColAt( maxPos
)])
8672 return clipToMinMax
? GetColAt( maxPos
) : -1;
8674 //X is before the first column
8675 if ( x
< m_colRights
[GetColAt( 0 )] )
8676 return GetColAt( 0 );
8678 //Perform a binary search
8679 while ( maxPos
- minPos
> 0 )
8681 wxCHECK_MSG(m_colRights
[GetColAt( minPos
)] <= x
&& x
< m_colRights
[GetColAt( maxPos
)],
8682 0, _T("wxGrid: internal error in XToCol"));
8684 if (x
>= m_colRights
[GetColAt( maxPos
- 1 )])
8685 return GetColAt( maxPos
);
8688 int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
8689 if (x
< m_colRights
[GetColAt( median
)])
8694 return GetColAt( maxPos
);
8697 // return the row number that that the y coord is near
8698 // the edge of, or -1 if not near an edge.
8699 // coords can only possibly be near an edge if
8700 // (a) the row/column is large enough to still allow for an "inner" area
8701 // that is _not_ nead the edge (i.e., if the height/width is smaller
8702 // than WXGRID_LABEL_EDGE_ZONE, coords are _never_ considered to be
8705 // (b) resizing rows/columns (the thing for which edge detection is
8706 // relevant at all) is enabled.
8708 int wxGrid::YToEdgeOfRow( int y
) const
8711 i
= internalYToRow(y
);
8713 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragRowSize() )
8715 // We know that we are in row i, test whether we are
8716 // close enough to lower or upper border, respectively.
8717 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8719 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8726 // return the col number that that the x coord is near the edge of, or
8727 // -1 if not near an edge
8728 // See comment at YToEdgeOfRow for conditions on edge detection.
8730 int wxGrid::XToEdgeOfCol( int x
) const
8733 i
= internalXToCol(x
);
8735 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
&& CanDragColSize() )
8737 // We know that we are in column i; test whether we are
8738 // close enough to right or left border, respectively.
8739 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8741 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8748 wxRect
wxGrid::CellToRect( int row
, int col
) const
8750 wxRect
rect( -1, -1, -1, -1 );
8752 if ( row
>= 0 && row
< m_numRows
&&
8753 col
>= 0 && col
< m_numCols
)
8755 int i
, cell_rows
, cell_cols
;
8756 rect
.width
= rect
.height
= 0;
8757 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8758 // if negative then find multicell owner
8763 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8765 rect
.x
= GetColLeft(col
);
8766 rect
.y
= GetRowTop(row
);
8767 for (i
=col
; i
< col
+ cell_cols
; i
++)
8768 rect
.width
+= GetColWidth(i
);
8769 for (i
=row
; i
< row
+ cell_rows
; i
++)
8770 rect
.height
+= GetRowHeight(i
);
8773 // if grid lines are enabled, then the area of the cell is a bit smaller
8774 if (m_gridLinesEnabled
)
8783 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
8785 // get the cell rectangle in logical coords
8787 wxRect
r( CellToRect( row
, col
) );
8789 // convert to device coords
8791 int left
, top
, right
, bottom
;
8792 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8793 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8795 // check against the client area of the grid window
8797 m_gridWin
->GetClientSize( &cw
, &ch
);
8799 if ( wholeCellVisible
)
8801 // is the cell wholly visible ?
8802 return ( left
>= 0 && right
<= cw
&&
8803 top
>= 0 && bottom
<= ch
);
8807 // is the cell partly visible ?
8809 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8810 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8814 // make the specified cell location visible by doing a minimal amount
8817 void wxGrid::MakeCellVisible( int row
, int col
)
8820 int xpos
= -1, ypos
= -1;
8822 if ( row
>= 0 && row
< m_numRows
&&
8823 col
>= 0 && col
< m_numCols
)
8825 // get the cell rectangle in logical coords
8826 wxRect
r( CellToRect( row
, col
) );
8828 // convert to device coords
8829 int left
, top
, right
, bottom
;
8830 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8831 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8834 m_gridWin
->GetClientSize( &cw
, &ch
);
8840 else if ( bottom
> ch
)
8842 int h
= r
.GetHeight();
8844 for ( i
= row
- 1; i
>= 0; i
-- )
8846 int rowHeight
= GetRowHeight(i
);
8847 if ( h
+ rowHeight
> ch
)
8854 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8855 // have rounding errors (this is important, because if we do,
8856 // we might not scroll at all and some cells won't be redrawn)
8858 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
8859 // so just add a full scroll unit...
8860 ypos
+= m_scrollLineY
;
8863 // special handling for wide cells - show always left part of the cell!
8864 // Otherwise, e.g. when stepping from row to row, it would jump between
8865 // left and right part of the cell on every step!
8867 if ( left
< 0 || (right
- left
) >= cw
)
8871 else if ( right
> cw
)
8873 // position the view so that the cell is on the right
8875 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8876 xpos
= x0
+ (right
- cw
);
8878 // see comment for ypos above
8879 xpos
+= m_scrollLineX
;
8882 if ( xpos
!= -1 || ypos
!= -1 )
8885 xpos
/= m_scrollLineX
;
8887 ypos
/= m_scrollLineY
;
8888 Scroll( xpos
, ypos
);
8895 // ------ Grid cursor movement functions
8898 bool wxGrid::MoveCursorUp( bool expandSelection
)
8900 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8901 m_currentCellCoords
.GetRow() >= 0 )
8903 if ( expandSelection
)
8905 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8906 m_selectingKeyboard
= m_currentCellCoords
;
8907 if ( m_selectingKeyboard
.GetRow() > 0 )
8909 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8910 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8911 m_selectingKeyboard
.GetCol() );
8912 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8915 else if ( m_currentCellCoords
.GetRow() > 0 )
8917 int row
= m_currentCellCoords
.GetRow() - 1;
8918 int col
= m_currentCellCoords
.GetCol();
8920 MakeCellVisible( row
, col
);
8921 SetCurrentCell( row
, col
);
8932 bool wxGrid::MoveCursorDown( bool expandSelection
)
8934 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8935 m_currentCellCoords
.GetRow() < m_numRows
)
8937 if ( expandSelection
)
8939 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8940 m_selectingKeyboard
= m_currentCellCoords
;
8941 if ( m_selectingKeyboard
.GetRow() < m_numRows
- 1 )
8943 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8944 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8945 m_selectingKeyboard
.GetCol() );
8946 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8949 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8951 int row
= m_currentCellCoords
.GetRow() + 1;
8952 int col
= m_currentCellCoords
.GetCol();
8954 MakeCellVisible( row
, col
);
8955 SetCurrentCell( row
, col
);
8966 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8968 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8969 m_currentCellCoords
.GetCol() >= 0 )
8971 if ( expandSelection
)
8973 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8974 m_selectingKeyboard
= m_currentCellCoords
;
8975 if ( m_selectingKeyboard
.GetCol() > 0 )
8977 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8978 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8979 m_selectingKeyboard
.GetCol() );
8980 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8983 else if ( GetColPos( m_currentCellCoords
.GetCol() ) > 0 )
8985 int row
= m_currentCellCoords
.GetRow();
8986 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) - 1 );
8989 MakeCellVisible( row
, col
);
8990 SetCurrentCell( row
, col
);
9001 bool wxGrid::MoveCursorRight( bool expandSelection
)
9003 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
9004 m_currentCellCoords
.GetCol() < m_numCols
)
9006 if ( expandSelection
)
9008 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
9009 m_selectingKeyboard
= m_currentCellCoords
;
9010 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
9012 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
9013 MakeCellVisible( m_selectingKeyboard
.GetRow(),
9014 m_selectingKeyboard
.GetCol() );
9015 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9018 else if ( GetColPos( m_currentCellCoords
.GetCol() ) < m_numCols
- 1 )
9020 int row
= m_currentCellCoords
.GetRow();
9021 int col
= GetColAt( GetColPos( m_currentCellCoords
.GetCol() ) + 1 );
9024 MakeCellVisible( row
, col
);
9025 SetCurrentCell( row
, col
);
9036 bool wxGrid::MovePageUp()
9038 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9041 int row
= m_currentCellCoords
.GetRow();
9045 m_gridWin
->GetClientSize( &cw
, &ch
);
9047 int y
= GetRowTop(row
);
9048 int newRow
= internalYToRow( y
- ch
+ 1 );
9050 if ( newRow
== row
)
9052 // row > 0, so newRow can never be less than 0 here.
9056 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9057 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9065 bool wxGrid::MovePageDown()
9067 if ( m_currentCellCoords
== wxGridNoCellCoords
)
9070 int row
= m_currentCellCoords
.GetRow();
9071 if ( (row
+ 1) < m_numRows
)
9074 m_gridWin
->GetClientSize( &cw
, &ch
);
9076 int y
= GetRowTop(row
);
9077 int newRow
= internalYToRow( y
+ ch
);
9078 if ( newRow
== row
)
9080 // row < m_numRows, so newRow can't overflow here.
9084 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
9085 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
9093 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
9096 m_currentCellCoords
!= wxGridNoCellCoords
&&
9097 m_currentCellCoords
.GetRow() > 0 )
9099 int row
= m_currentCellCoords
.GetRow();
9100 int col
= m_currentCellCoords
.GetCol();
9102 if ( m_table
->IsEmptyCell(row
, col
) )
9104 // starting in an empty cell: find the next block of
9110 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9114 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
9116 // starting at the top of a block: find the next block
9122 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9128 // starting within a block: find the top of the block
9133 if ( m_table
->IsEmptyCell(row
, col
) )
9141 MakeCellVisible( row
, col
);
9142 if ( expandSelection
)
9144 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9145 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9150 SetCurrentCell( row
, col
);
9159 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
9162 m_currentCellCoords
!= wxGridNoCellCoords
&&
9163 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
9165 int row
= m_currentCellCoords
.GetRow();
9166 int col
= m_currentCellCoords
.GetCol();
9168 if ( m_table
->IsEmptyCell(row
, col
) )
9170 // starting in an empty cell: find the next block of
9173 while ( row
< m_numRows
- 1 )
9176 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9180 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
9182 // starting at the bottom of a block: find the next block
9185 while ( row
< m_numRows
- 1 )
9188 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9194 // starting within a block: find the bottom of the block
9196 while ( row
< m_numRows
- 1 )
9199 if ( m_table
->IsEmptyCell(row
, col
) )
9207 MakeCellVisible( row
, col
);
9208 if ( expandSelection
)
9210 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9211 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9216 SetCurrentCell( row
, col
);
9225 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
9228 m_currentCellCoords
!= wxGridNoCellCoords
&&
9229 m_currentCellCoords
.GetCol() > 0 )
9231 int row
= m_currentCellCoords
.GetRow();
9232 int col
= m_currentCellCoords
.GetCol();
9234 if ( m_table
->IsEmptyCell(row
, col
) )
9236 // starting in an empty cell: find the next block of
9242 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9246 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
9248 // starting at the left of a block: find the next block
9254 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9260 // starting within a block: find the left of the block
9265 if ( m_table
->IsEmptyCell(row
, col
) )
9273 MakeCellVisible( row
, col
);
9274 if ( expandSelection
)
9276 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9277 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9282 SetCurrentCell( row
, col
);
9291 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
9294 m_currentCellCoords
!= wxGridNoCellCoords
&&
9295 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
9297 int row
= m_currentCellCoords
.GetRow();
9298 int col
= m_currentCellCoords
.GetCol();
9300 if ( m_table
->IsEmptyCell(row
, col
) )
9302 // starting in an empty cell: find the next block of
9305 while ( col
< m_numCols
- 1 )
9308 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9312 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
9314 // starting at the right of a block: find the next block
9317 while ( col
< m_numCols
- 1 )
9320 if ( !(m_table
->IsEmptyCell(row
, col
)) )
9326 // starting within a block: find the right of the block
9328 while ( col
< m_numCols
- 1 )
9331 if ( m_table
->IsEmptyCell(row
, col
) )
9339 MakeCellVisible( row
, col
);
9340 if ( expandSelection
)
9342 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
9343 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
9348 SetCurrentCell( row
, col
);
9358 // ------ Label values and formatting
9361 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
9364 *horiz
= m_rowLabelHorizAlign
;
9366 *vert
= m_rowLabelVertAlign
;
9369 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
9372 *horiz
= m_colLabelHorizAlign
;
9374 *vert
= m_colLabelVertAlign
;
9377 int wxGrid::GetColLabelTextOrientation() const
9379 return m_colLabelTextOrientation
;
9382 wxString
wxGrid::GetRowLabelValue( int row
) const
9386 return m_table
->GetRowLabelValue( row
);
9396 wxString
wxGrid::GetColLabelValue( int col
) const
9400 return m_table
->GetColLabelValue( col
);
9410 void wxGrid::SetRowLabelSize( int width
)
9412 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
9414 if ( width
== wxGRID_AUTOSIZE
)
9416 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
9419 if ( width
!= m_rowLabelWidth
)
9423 m_rowLabelWin
->Show( false );
9424 m_cornerLabelWin
->Show( false );
9426 else if ( m_rowLabelWidth
== 0 )
9428 m_rowLabelWin
->Show( true );
9429 if ( m_colLabelHeight
> 0 )
9430 m_cornerLabelWin
->Show( true );
9433 m_rowLabelWidth
= width
;
9435 wxScrolledWindow::Refresh( true );
9439 void wxGrid::SetColLabelSize( int height
)
9441 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
9443 if ( height
== wxGRID_AUTOSIZE
)
9445 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
9448 if ( height
!= m_colLabelHeight
)
9452 m_colLabelWin
->Show( false );
9453 m_cornerLabelWin
->Show( false );
9455 else if ( m_colLabelHeight
== 0 )
9457 m_colLabelWin
->Show( true );
9458 if ( m_rowLabelWidth
> 0 )
9459 m_cornerLabelWin
->Show( true );
9462 m_colLabelHeight
= height
;
9464 wxScrolledWindow::Refresh( true );
9468 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
9470 if ( m_labelBackgroundColour
!= colour
)
9472 m_labelBackgroundColour
= colour
;
9473 m_rowLabelWin
->SetBackgroundColour( colour
);
9474 m_colLabelWin
->SetBackgroundColour( colour
);
9475 m_cornerLabelWin
->SetBackgroundColour( colour
);
9477 if ( !GetBatchCount() )
9479 m_rowLabelWin
->Refresh();
9480 m_colLabelWin
->Refresh();
9481 m_cornerLabelWin
->Refresh();
9486 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
9488 if ( m_labelTextColour
!= colour
)
9490 m_labelTextColour
= colour
;
9491 if ( !GetBatchCount() )
9493 m_rowLabelWin
->Refresh();
9494 m_colLabelWin
->Refresh();
9499 void wxGrid::SetLabelFont( const wxFont
& font
)
9502 if ( !GetBatchCount() )
9504 m_rowLabelWin
->Refresh();
9505 m_colLabelWin
->Refresh();
9509 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
9511 // allow old (incorrect) defs to be used
9514 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9515 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9516 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9521 case wxTOP
: vert
= wxALIGN_TOP
; break;
9522 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9523 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9526 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9528 m_rowLabelHorizAlign
= horiz
;
9531 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9533 m_rowLabelVertAlign
= vert
;
9536 if ( !GetBatchCount() )
9538 m_rowLabelWin
->Refresh();
9542 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
9544 // allow old (incorrect) defs to be used
9547 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
9548 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
9549 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
9554 case wxTOP
: vert
= wxALIGN_TOP
; break;
9555 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
9556 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9559 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9561 m_colLabelHorizAlign
= horiz
;
9564 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9566 m_colLabelVertAlign
= vert
;
9569 if ( !GetBatchCount() )
9571 m_colLabelWin
->Refresh();
9575 // Note: under MSW, the default column label font must be changed because it
9576 // does not support vertical printing
9578 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9579 // pGrid->SetLabelFont(font);
9580 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9582 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9584 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9585 m_colLabelTextOrientation
= textOrientation
;
9587 if ( !GetBatchCount() )
9588 m_colLabelWin
->Refresh();
9591 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9595 m_table
->SetRowLabelValue( row
, s
);
9596 if ( !GetBatchCount() )
9598 wxRect rect
= CellToRect( row
, 0 );
9599 if ( rect
.height
> 0 )
9601 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9603 rect
.width
= m_rowLabelWidth
;
9604 m_rowLabelWin
->Refresh( true, &rect
);
9610 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9614 m_table
->SetColLabelValue( col
, s
);
9615 if ( !GetBatchCount() )
9617 wxRect rect
= CellToRect( 0, col
);
9618 if ( rect
.width
> 0 )
9620 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9622 rect
.height
= m_colLabelHeight
;
9623 m_colLabelWin
->Refresh( true, &rect
);
9629 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9631 if ( m_gridLineColour
!= colour
)
9633 m_gridLineColour
= colour
;
9635 wxClientDC
dc( m_gridWin
);
9637 DrawAllGridLines( dc
, wxRegion() );
9641 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9643 if ( m_cellHighlightColour
!= colour
)
9645 m_cellHighlightColour
= colour
;
9647 wxClientDC
dc( m_gridWin
);
9649 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9650 DrawCellHighlight(dc
, attr
);
9655 void wxGrid::SetCellHighlightPenWidth(int width
)
9657 if (m_cellHighlightPenWidth
!= width
)
9659 m_cellHighlightPenWidth
= width
;
9661 // Just redrawing the cell highlight is not enough since that won't
9662 // make any visible change if the the thickness is getting smaller.
9663 int row
= m_currentCellCoords
.GetRow();
9664 int col
= m_currentCellCoords
.GetCol();
9665 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9668 wxRect rect
= CellToRect(row
, col
);
9669 m_gridWin
->Refresh(true, &rect
);
9673 void wxGrid::SetCellHighlightROPenWidth(int width
)
9675 if (m_cellHighlightROPenWidth
!= width
)
9677 m_cellHighlightROPenWidth
= width
;
9679 // Just redrawing the cell highlight is not enough since that won't
9680 // make any visible change if the the thickness is getting smaller.
9681 int row
= m_currentCellCoords
.GetRow();
9682 int col
= m_currentCellCoords
.GetCol();
9683 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9686 wxRect rect
= CellToRect(row
, col
);
9687 m_gridWin
->Refresh(true, &rect
);
9691 void wxGrid::EnableGridLines( bool enable
)
9693 if ( enable
!= m_gridLinesEnabled
)
9695 m_gridLinesEnabled
= enable
;
9697 if ( !GetBatchCount() )
9701 wxClientDC
dc( m_gridWin
);
9703 DrawAllGridLines( dc
, wxRegion() );
9707 m_gridWin
->Refresh();
9713 int wxGrid::GetDefaultRowSize() const
9715 return m_defaultRowHeight
;
9718 int wxGrid::GetRowSize( int row
) const
9720 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9722 return GetRowHeight(row
);
9725 int wxGrid::GetDefaultColSize() const
9727 return m_defaultColWidth
;
9730 int wxGrid::GetColSize( int col
) const
9732 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9734 return GetColWidth(col
);
9737 // ============================================================================
9738 // access to the grid attributes: each of them has a default value in the grid
9739 // itself and may be overidden on a per-cell basis
9740 // ============================================================================
9742 // ----------------------------------------------------------------------------
9743 // setting default attributes
9744 // ----------------------------------------------------------------------------
9746 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9748 m_defaultCellAttr
->SetBackgroundColour(col
);
9750 m_gridWin
->SetBackgroundColour(col
);
9754 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9756 m_defaultCellAttr
->SetTextColour(col
);
9759 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9761 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9764 void wxGrid::SetDefaultCellOverflow( bool allow
)
9766 m_defaultCellAttr
->SetOverflow(allow
);
9769 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9771 m_defaultCellAttr
->SetFont(font
);
9774 // For editors and renderers the type registry takes precedence over the
9775 // default attr, so we need to register the new editor/renderer for the string
9776 // data type in order to make setting a default editor/renderer appear to
9779 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9781 RegisterDataType(wxGRID_VALUE_STRING
,
9783 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9786 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9788 RegisterDataType(wxGRID_VALUE_STRING
,
9789 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9793 // ----------------------------------------------------------------------------
9794 // access to the default attributes
9795 // ----------------------------------------------------------------------------
9797 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
9799 return m_defaultCellAttr
->GetBackgroundColour();
9802 wxColour
wxGrid::GetDefaultCellTextColour() const
9804 return m_defaultCellAttr
->GetTextColour();
9807 wxFont
wxGrid::GetDefaultCellFont() const
9809 return m_defaultCellAttr
->GetFont();
9812 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
9814 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9817 bool wxGrid::GetDefaultCellOverflow() const
9819 return m_defaultCellAttr
->GetOverflow();
9822 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9824 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9827 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9829 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9832 // ----------------------------------------------------------------------------
9833 // access to cell attributes
9834 // ----------------------------------------------------------------------------
9836 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
9838 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9839 wxColour colour
= attr
->GetBackgroundColour();
9845 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
9847 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9848 wxColour colour
= attr
->GetTextColour();
9854 wxFont
wxGrid::GetCellFont( int row
, int col
) const
9856 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9857 wxFont font
= attr
->GetFont();
9863 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
9865 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9866 attr
->GetAlignment(horiz
, vert
);
9870 bool wxGrid::GetCellOverflow( int row
, int col
) const
9872 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9873 bool allow
= attr
->GetOverflow();
9879 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
9881 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9882 attr
->GetSize( num_rows
, num_cols
);
9886 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
9888 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9889 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9895 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
9897 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9898 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9904 bool wxGrid::IsReadOnly(int row
, int col
) const
9906 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9907 bool isReadOnly
= attr
->IsReadOnly();
9913 // ----------------------------------------------------------------------------
9914 // attribute support: cache, automatic provider creation, ...
9915 // ----------------------------------------------------------------------------
9917 bool wxGrid::CanHaveAttributes() const
9924 return m_table
->CanHaveAttributes();
9927 void wxGrid::ClearAttrCache()
9929 if ( m_attrCache
.row
!= -1 )
9931 wxSafeDecRef(m_attrCache
.attr
);
9932 m_attrCache
.attr
= NULL
;
9933 m_attrCache
.row
= -1;
9937 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9941 wxGrid
*self
= (wxGrid
*)this; // const_cast
9943 self
->ClearAttrCache();
9944 self
->m_attrCache
.row
= row
;
9945 self
->m_attrCache
.col
= col
;
9946 self
->m_attrCache
.attr
= attr
;
9951 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9953 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9955 *attr
= m_attrCache
.attr
;
9956 wxSafeIncRef(m_attrCache
.attr
);
9958 #ifdef DEBUG_ATTR_CACHE
9959 gs_nAttrCacheHits
++;
9966 #ifdef DEBUG_ATTR_CACHE
9967 gs_nAttrCacheMisses
++;
9974 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9976 wxGridCellAttr
*attr
= NULL
;
9977 // Additional test to avoid looking at the cache e.g. for
9978 // wxNoCellCoords, as this will confuse memory management.
9981 if ( !LookupAttr(row
, col
, &attr
) )
9983 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9984 : (wxGridCellAttr
*)NULL
;
9985 CacheAttr(row
, col
, attr
);
9991 attr
->SetDefAttr(m_defaultCellAttr
);
9995 attr
= m_defaultCellAttr
;
10002 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
10004 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
10005 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
10007 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
10008 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
10010 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
10013 attr
= new wxGridCellAttr(m_defaultCellAttr
);
10015 // artificially inc the ref count to match DecRef() in caller
10017 m_table
->SetAttr(attr
, row
, col
);
10023 // ----------------------------------------------------------------------------
10024 // setting column attributes (wrappers around SetColAttr)
10025 // ----------------------------------------------------------------------------
10027 void wxGrid::SetColFormatBool(int col
)
10029 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
10032 void wxGrid::SetColFormatNumber(int col
)
10034 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
10037 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
10039 wxString typeName
= wxGRID_VALUE_FLOAT
;
10040 if ( (width
!= -1) || (precision
!= -1) )
10042 typeName
<< _T(':') << width
<< _T(',') << precision
;
10045 SetColFormatCustom(col
, typeName
);
10048 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
10050 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
10052 attr
= new wxGridCellAttr
;
10053 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
10054 attr
->SetRenderer(renderer
);
10056 SetColAttr(col
, attr
);
10060 // ----------------------------------------------------------------------------
10061 // setting cell attributes: this is forwarded to the table
10062 // ----------------------------------------------------------------------------
10064 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
10066 if ( CanHaveAttributes() )
10068 m_table
->SetAttr(attr
, row
, col
);
10073 wxSafeDecRef(attr
);
10077 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
10079 if ( CanHaveAttributes() )
10081 m_table
->SetRowAttr(attr
, row
);
10086 wxSafeDecRef(attr
);
10090 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
10092 if ( CanHaveAttributes() )
10094 m_table
->SetColAttr(attr
, col
);
10099 wxSafeDecRef(attr
);
10103 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
10105 if ( CanHaveAttributes() )
10107 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10108 attr
->SetBackgroundColour(colour
);
10113 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
10115 if ( CanHaveAttributes() )
10117 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10118 attr
->SetTextColour(colour
);
10123 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
10125 if ( CanHaveAttributes() )
10127 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10128 attr
->SetFont(font
);
10133 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
10135 if ( CanHaveAttributes() )
10137 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10138 attr
->SetAlignment(horiz
, vert
);
10143 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
10145 if ( CanHaveAttributes() )
10147 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10148 attr
->SetOverflow(allow
);
10153 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
10155 if ( CanHaveAttributes() )
10157 int cell_rows
, cell_cols
;
10159 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10160 attr
->GetSize(&cell_rows
, &cell_cols
);
10161 attr
->SetSize(num_rows
, num_cols
);
10164 // Cannot set the size of a cell to 0 or negative values
10165 // While it is perfectly legal to do that, this function cannot
10166 // handle all the possibilies, do it by hand by getting the CellAttr.
10167 // You can only set the size of a cell to 1,1 or greater with this fn
10168 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
10169 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
10170 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
10171 wxT("wxGrid::SetCellSize setting cell size to < 1"));
10173 // if this was already a multicell then "turn off" the other cells first
10174 if ((cell_rows
> 1) || (cell_rows
> 1))
10177 for (j
=row
; j
< row
+ cell_rows
; j
++)
10179 for (i
=col
; i
< col
+ cell_cols
; i
++)
10181 if ((i
!= col
) || (j
!= row
))
10183 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10184 attr_stub
->SetSize( 1, 1 );
10185 attr_stub
->DecRef();
10191 // mark the cells that will be covered by this cell to
10192 // negative or zero values to point back at this cell
10193 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
10196 for (j
=row
; j
< row
+ num_rows
; j
++)
10198 for (i
=col
; i
< col
+ num_cols
; i
++)
10200 if ((i
!= col
) || (j
!= row
))
10202 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
10203 attr_stub
->SetSize( row
- j
, col
- i
);
10204 attr_stub
->DecRef();
10212 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
10214 if ( CanHaveAttributes() )
10216 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10217 attr
->SetRenderer(renderer
);
10222 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
10224 if ( CanHaveAttributes() )
10226 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10227 attr
->SetEditor(editor
);
10232 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
10234 if ( CanHaveAttributes() )
10236 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
10237 attr
->SetReadOnly(isReadOnly
);
10242 // ----------------------------------------------------------------------------
10243 // Data type registration
10244 // ----------------------------------------------------------------------------
10246 void wxGrid::RegisterDataType(const wxString
& typeName
,
10247 wxGridCellRenderer
* renderer
,
10248 wxGridCellEditor
* editor
)
10250 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
10254 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
10256 wxString typeName
= m_table
->GetTypeName(row
, col
);
10257 return GetDefaultEditorForType(typeName
);
10260 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
10262 wxString typeName
= m_table
->GetTypeName(row
, col
);
10263 return GetDefaultRendererForType(typeName
);
10266 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
10268 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10269 if ( index
== wxNOT_FOUND
)
10273 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10274 wxFAIL_MSG(errStr
.c_str());
10279 return m_typeRegistry
->GetEditor(index
);
10282 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
10284 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
10285 if ( index
== wxNOT_FOUND
)
10289 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
10290 wxFAIL_MSG(errStr
.c_str());
10295 return m_typeRegistry
->GetRenderer(index
);
10298 // ----------------------------------------------------------------------------
10300 // ----------------------------------------------------------------------------
10302 void wxGrid::EnableDragRowSize( bool enable
)
10304 m_canDragRowSize
= enable
;
10307 void wxGrid::EnableDragColSize( bool enable
)
10309 m_canDragColSize
= enable
;
10312 void wxGrid::EnableDragGridSize( bool enable
)
10314 m_canDragGridSize
= enable
;
10317 void wxGrid::EnableDragCell( bool enable
)
10319 m_canDragCell
= enable
;
10322 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
10324 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
10326 if ( resizeExistingRows
)
10328 // since we are resizing all rows to the default row size,
10329 // we can simply clear the row heights and row bottoms
10330 // arrays (which also allows us to take advantage of
10331 // some speed optimisations)
10332 m_rowHeights
.Empty();
10333 m_rowBottoms
.Empty();
10334 if ( !GetBatchCount() )
10339 void wxGrid::SetRowSize( int row
, int height
)
10341 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
10343 // See comment in SetColSize
10344 if ( height
< GetRowMinimalAcceptableHeight())
10347 if ( m_rowHeights
.IsEmpty() )
10349 // need to really create the array
10353 int h
= wxMax( 0, height
);
10354 int diff
= h
- m_rowHeights
[row
];
10356 m_rowHeights
[row
] = h
;
10357 for ( int i
= row
; i
< m_numRows
; i
++ )
10359 m_rowBottoms
[i
] += diff
;
10362 if ( !GetBatchCount() )
10366 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
10368 // we dont allow zero default column width
10369 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
10371 if ( resizeExistingCols
)
10373 // since we are resizing all columns to the default column size,
10374 // we can simply clear the col widths and col rights
10375 // arrays (which also allows us to take advantage of
10376 // some speed optimisations)
10377 m_colWidths
.Empty();
10378 m_colRights
.Empty();
10379 if ( !GetBatchCount() )
10384 void wxGrid::SetColSize( int col
, int width
)
10386 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
10388 // should we check that it's bigger than GetColMinimalWidth(col) here?
10390 // No, because it is reasonable to assume the library user know's
10391 // what he is doing. However we should test against the weaker
10392 // constraint of minimalAcceptableWidth, as this breaks rendering
10394 // This test then fixes sf.net bug #645734
10396 if ( width
< GetColMinimalAcceptableWidth() )
10399 if ( m_colWidths
.IsEmpty() )
10401 // need to really create the array
10405 // if < 0 then calculate new width from label
10409 wxArrayString lines
;
10410 wxClientDC
dc(m_colLabelWin
);
10411 dc
.SetFont(GetLabelFont());
10412 StringToLines(GetColLabelValue(col
), lines
);
10413 GetTextBoxSize(dc
, lines
, &w
, &h
);
10417 int w
= wxMax( 0, width
);
10418 int diff
= w
- m_colWidths
[col
];
10419 m_colWidths
[col
] = w
;
10421 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
10423 m_colRights
[GetColAt(colPos
)] += diff
;
10426 if ( !GetBatchCount() )
10430 void wxGrid::SetColMinimalWidth( int col
, int width
)
10432 if (width
> GetColMinimalAcceptableWidth())
10434 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10435 m_colMinWidths
[key
] = width
;
10439 void wxGrid::SetRowMinimalHeight( int row
, int width
)
10441 if (width
> GetRowMinimalAcceptableHeight())
10443 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10444 m_rowMinHeights
[key
] = width
;
10448 int wxGrid::GetColMinimalWidth(int col
) const
10450 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
10451 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
10453 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
10456 int wxGrid::GetRowMinimalHeight(int row
) const
10458 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
10459 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
10461 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
10464 void wxGrid::SetColMinimalAcceptableWidth( int width
)
10466 // We do allow a width of 0 since this gives us
10467 // an easy way to temporarily hiding columns.
10469 m_minAcceptableColWidth
= width
;
10472 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
10474 // We do allow a height of 0 since this gives us
10475 // an easy way to temporarily hiding rows.
10477 m_minAcceptableRowHeight
= height
;
10480 int wxGrid::GetColMinimalAcceptableWidth() const
10482 return m_minAcceptableColWidth
;
10485 int wxGrid::GetRowMinimalAcceptableHeight() const
10487 return m_minAcceptableRowHeight
;
10490 // ----------------------------------------------------------------------------
10492 // ----------------------------------------------------------------------------
10495 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
10497 const bool column
= direction
== wxGRID_COLUMN
;
10499 wxClientDC
dc(m_gridWin
);
10501 // cancel editing of cell
10502 HideCellEditControl();
10503 SaveEditControlValue();
10505 // init both of them to avoid compiler warnings, even if we only need one
10513 wxCoord extent
, extentMax
= 0;
10514 int max
= column
? m_numRows
: m_numCols
;
10515 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
10522 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
10523 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
10526 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
10527 extent
= column
? size
.x
: size
.y
;
10528 if ( extent
> extentMax
)
10529 extentMax
= extent
;
10531 renderer
->DecRef();
10537 // now also compare with the column label extent
10539 dc
.SetFont( GetLabelFont() );
10543 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
10544 if ( GetColLabelTextOrientation() == wxVERTICAL
)
10548 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
10550 extent
= column
? w
: h
;
10551 if ( extent
> extentMax
)
10552 extentMax
= extent
;
10556 // empty column - give default extent (notice that if extentMax is less
10557 // than default extent but != 0, it's OK)
10558 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10563 // leave some space around text
10571 SetColSize( col
, extentMax
);
10572 if ( !GetBatchCount() )
10575 m_gridWin
->GetClientSize( &cw
, &ch
);
10576 wxRect
rect ( CellToRect( 0, col
) );
10578 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10579 rect
.width
= cw
- rect
.x
;
10580 rect
.height
= m_colLabelHeight
;
10581 m_colLabelWin
->Refresh( true, &rect
);
10586 SetRowSize(row
, extentMax
);
10587 if ( !GetBatchCount() )
10590 m_gridWin
->GetClientSize( &cw
, &ch
);
10591 wxRect
rect( CellToRect( row
, 0 ) );
10593 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10594 rect
.width
= m_rowLabelWidth
;
10595 rect
.height
= ch
- rect
.y
;
10596 m_rowLabelWin
->Refresh( true, &rect
);
10603 SetColMinimalWidth(col
, extentMax
);
10605 SetRowMinimalHeight(row
, extentMax
);
10609 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
10611 // calculate size for the rows or columns?
10612 const bool calcRows
= direction
== wxGRID_ROW
;
10614 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
10615 : GetGridColLabelWindow());
10616 dc
.SetFont(GetLabelFont());
10618 // which dimension should we take into account for calculations?
10620 // for columns, the text can be only horizontal so it's easy but for rows
10621 // we also have to take into account the text orientation
10623 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
10625 wxArrayString lines
;
10626 wxCoord extentMax
= 0;
10628 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
10629 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
10633 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
10634 : GetColLabelValue(rowOrCol
);
10635 StringToLines(label
, lines
);
10638 GetTextBoxSize(dc
, lines
, &w
, &h
);
10640 const wxCoord extent
= useWidth
? w
: h
;
10641 if ( extent
> extentMax
)
10642 extentMax
= extent
;
10647 // empty column - give default extent (notice that if extentMax is less
10648 // than default extent but != 0, it's OK)
10649 extentMax
= calcRows
? GetDefaultRowLabelSize()
10650 : GetDefaultColLabelSize();
10653 // leave some space around text (taken from AutoSizeColOrRow)
10662 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10664 int width
= m_rowLabelWidth
;
10666 wxGridUpdateLocker locker
;
10668 locker
.Create(this);
10670 for ( int col
= 0; col
< m_numCols
; col
++ )
10673 AutoSizeColumn(col
, setAsMin
);
10675 width
+= GetColWidth(col
);
10681 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10683 int height
= m_colLabelHeight
;
10685 wxGridUpdateLocker locker
;
10687 locker
.Create(this);
10689 for ( int row
= 0; row
< m_numRows
; row
++ )
10692 AutoSizeRow(row
, setAsMin
);
10694 height
+= GetRowHeight(row
);
10700 void wxGrid::AutoSize()
10702 wxGridUpdateLocker
locker(this);
10704 // we need to round up the size of the scrollable area to a multiple of
10705 // scroll step to ensure that we don't get the scrollbars when we're sized
10706 // exactly to fit our contents
10707 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
10708 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
10709 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10710 GetScrollY(size
.y
) * GetScrollLineY());
10712 // distribute the extra space between the columns/rows to avoid having
10713 // extra white space
10714 wxCoord diff
= sizeFit
.x
- size
.x
;
10715 if ( diff
&& m_numCols
)
10717 // try to resize the columns uniformly
10718 wxCoord diffPerCol
= diff
/ m_numCols
;
10721 for ( int col
= 0; col
< m_numCols
; col
++ )
10723 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10727 // add remaining amount to the last columns
10728 diff
-= diffPerCol
* m_numCols
;
10731 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10733 SetColSize(col
, GetColWidth(col
) + 1);
10739 diff
= sizeFit
.y
- size
.y
;
10740 if ( diff
&& m_numRows
)
10742 // try to resize the columns uniformly
10743 wxCoord diffPerRow
= diff
/ m_numRows
;
10746 for ( int row
= 0; row
< m_numRows
; row
++ )
10748 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10752 // add remaining amount to the last rows
10753 diff
-= diffPerRow
* m_numRows
;
10756 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10758 SetRowSize(row
, GetRowHeight(row
) + 1);
10763 // we know that we're not going to have scrollbars so disable them now to
10764 // avoid trouble in SetClientSize() which can otherwise set the correct
10765 // client size but also leave space for (not needed any more) scrollbars
10766 SetScrollbars(0, 0, 0, 0, 0, 0, true);
10767 SetClientSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
);
10770 void wxGrid::AutoSizeRowLabelSize( int row
)
10772 wxArrayString lines
;
10775 // Hide the edit control, so it
10776 // won't interfere with drag-shrinking.
10777 if ( IsCellEditControlShown() )
10779 HideCellEditControl();
10780 SaveEditControlValue();
10783 // autosize row height depending on label text
10784 StringToLines( GetRowLabelValue( row
), lines
);
10785 wxClientDC
dc( m_rowLabelWin
);
10786 GetTextBoxSize( dc
, lines
, &w
, &h
);
10787 if ( h
< m_defaultRowHeight
)
10788 h
= m_defaultRowHeight
;
10789 SetRowSize(row
, h
);
10793 void wxGrid::AutoSizeColLabelSize( int col
)
10795 wxArrayString lines
;
10798 // Hide the edit control, so it
10799 // won't interfere with drag-shrinking.
10800 if ( IsCellEditControlShown() )
10802 HideCellEditControl();
10803 SaveEditControlValue();
10806 // autosize column width depending on label text
10807 StringToLines( GetColLabelValue( col
), lines
);
10808 wxClientDC
dc( m_colLabelWin
);
10809 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10810 GetTextBoxSize( dc
, lines
, &w
, &h
);
10812 GetTextBoxSize( dc
, lines
, &h
, &w
);
10813 if ( w
< m_defaultColWidth
)
10814 w
= m_defaultColWidth
;
10815 SetColSize(col
, w
);
10819 wxSize
wxGrid::DoGetBestSize() const
10821 wxGrid
*self
= (wxGrid
*)this; // const_cast
10823 // we do the same as in AutoSize() here with the exception that we don't
10824 // change the column/row sizes, only calculate them
10825 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
10826 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
10827 wxSize
sizeFit(GetScrollX(size
.x
) * GetScrollLineX(),
10828 GetScrollY(size
.y
) * GetScrollLineY());
10830 // NOTE: This size should be cached, but first we need to add calls to
10831 // InvalidateBestSize everywhere that could change the results of this
10833 // CacheBestSize(size);
10835 return wxSize(sizeFit
.x
+ m_rowLabelWidth
, sizeFit
.y
+ m_colLabelHeight
)
10836 + GetWindowBorderSize();
10844 wxPen
& wxGrid::GetDividerPen() const
10849 // ----------------------------------------------------------------------------
10850 // cell value accessor functions
10851 // ----------------------------------------------------------------------------
10853 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10857 m_table
->SetValue( row
, col
, s
);
10858 if ( !GetBatchCount() )
10861 wxRect
rect( CellToRect( row
, col
) );
10863 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10864 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10865 m_gridWin
->Refresh( false, &rect
);
10868 if ( m_currentCellCoords
.GetRow() == row
&&
10869 m_currentCellCoords
.GetCol() == col
&&
10870 IsCellEditControlShown())
10871 // Note: If we are using IsCellEditControlEnabled,
10872 // this interacts badly with calling SetCellValue from
10873 // an EVT_GRID_CELL_CHANGE handler.
10875 HideCellEditControl();
10876 ShowCellEditControl(); // will reread data from table
10881 // ----------------------------------------------------------------------------
10882 // block, row and column selection
10883 // ----------------------------------------------------------------------------
10885 void wxGrid::SelectRow( int row
, bool addToSelected
)
10887 if ( IsSelection() && !addToSelected
)
10891 m_selection
->SelectRow( row
, false, addToSelected
);
10894 void wxGrid::SelectCol( int col
, bool addToSelected
)
10896 if ( IsSelection() && !addToSelected
)
10900 m_selection
->SelectCol( col
, false, addToSelected
);
10903 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10904 bool addToSelected
)
10906 if ( IsSelection() && !addToSelected
)
10910 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10911 false, addToSelected
);
10914 void wxGrid::SelectAll()
10916 if ( m_numRows
> 0 && m_numCols
> 0 )
10919 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
10923 // ----------------------------------------------------------------------------
10924 // cell, row and col deselection
10925 // ----------------------------------------------------------------------------
10927 void wxGrid::DeselectRow( int row
)
10929 if ( !m_selection
)
10932 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10934 if ( m_selection
->IsInSelection(row
, 0 ) )
10935 m_selection
->ToggleCellSelection(row
, 0);
10939 int nCols
= GetNumberCols();
10940 for ( int i
= 0; i
< nCols
; i
++ )
10942 if ( m_selection
->IsInSelection(row
, i
) )
10943 m_selection
->ToggleCellSelection(row
, i
);
10948 void wxGrid::DeselectCol( int col
)
10950 if ( !m_selection
)
10953 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10955 if ( m_selection
->IsInSelection(0, col
) )
10956 m_selection
->ToggleCellSelection(0, col
);
10960 int nRows
= GetNumberRows();
10961 for ( int i
= 0; i
< nRows
; i
++ )
10963 if ( m_selection
->IsInSelection(i
, col
) )
10964 m_selection
->ToggleCellSelection(i
, col
);
10969 void wxGrid::DeselectCell( int row
, int col
)
10971 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10972 m_selection
->ToggleCellSelection(row
, col
);
10975 bool wxGrid::IsSelection() const
10977 return ( m_selection
&& (m_selection
->IsSelection() ||
10978 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10979 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10982 bool wxGrid::IsInSelection( int row
, int col
) const
10984 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10985 ( row
>= m_selectingTopLeft
.GetRow() &&
10986 col
>= m_selectingTopLeft
.GetCol() &&
10987 row
<= m_selectingBottomRight
.GetRow() &&
10988 col
<= m_selectingBottomRight
.GetCol() )) );
10991 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10995 wxGridCellCoordsArray a
;
10999 return m_selection
->m_cellSelection
;
11002 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
11006 wxGridCellCoordsArray a
;
11010 return m_selection
->m_blockSelectionTopLeft
;
11013 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
11017 wxGridCellCoordsArray a
;
11021 return m_selection
->m_blockSelectionBottomRight
;
11024 wxArrayInt
wxGrid::GetSelectedRows() const
11032 return m_selection
->m_rowSelection
;
11035 wxArrayInt
wxGrid::GetSelectedCols() const
11043 return m_selection
->m_colSelection
;
11046 void wxGrid::ClearSelection()
11048 m_selectingTopLeft
=
11049 m_selectingBottomRight
=
11050 m_selectingKeyboard
= wxGridNoCellCoords
;
11052 m_selection
->ClearSelection();
11055 // This function returns the rectangle that encloses the given block
11056 // in device coords clipped to the client size of the grid window.
11058 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
11059 const wxGridCellCoords
&bottomRight
) const
11061 wxRect
rect( wxGridNoCellRect
);
11064 cellRect
= CellToRect( topLeft
);
11065 if ( cellRect
!= wxGridNoCellRect
)
11071 rect
= wxRect(0, 0, 0, 0);
11074 cellRect
= CellToRect( bottomRight
);
11075 if ( cellRect
!= wxGridNoCellRect
)
11081 return wxGridNoCellRect
;
11085 int left
= rect
.GetLeft();
11086 int top
= rect
.GetTop();
11087 int right
= rect
.GetRight();
11088 int bottom
= rect
.GetBottom();
11090 int leftCol
= topLeft
.GetCol();
11091 int topRow
= topLeft
.GetRow();
11092 int rightCol
= bottomRight
.GetCol();
11093 int bottomRow
= bottomRight
.GetRow();
11101 leftCol
= rightCol
;
11111 topRow
= bottomRow
;
11115 for ( j
= topRow
; j
<= bottomRow
; j
++ )
11117 for ( i
= leftCol
; i
<= rightCol
; i
++ )
11119 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
11121 cellRect
= CellToRect( j
, i
);
11123 if (cellRect
.x
< left
)
11125 if (cellRect
.y
< top
)
11127 if (cellRect
.x
+ cellRect
.width
> right
)
11128 right
= cellRect
.x
+ cellRect
.width
;
11129 if (cellRect
.y
+ cellRect
.height
> bottom
)
11130 bottom
= cellRect
.y
+ cellRect
.height
;
11134 i
= rightCol
; // jump over inner cells.
11139 // convert to scrolled coords
11141 CalcScrolledPosition( left
, top
, &left
, &top
);
11142 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
11145 m_gridWin
->GetClientSize( &cw
, &ch
);
11147 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
11148 return wxRect(0,0,0,0);
11150 rect
.SetLeft( wxMax(0, left
) );
11151 rect
.SetTop( wxMax(0, top
) );
11152 rect
.SetRight( wxMin(cw
, right
) );
11153 rect
.SetBottom( wxMin(ch
, bottom
) );
11158 // ----------------------------------------------------------------------------
11159 // grid event classes
11160 // ----------------------------------------------------------------------------
11162 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
11164 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
11165 int row
, int col
, int x
, int y
, bool sel
,
11166 bool control
, bool shift
, bool alt
, bool meta
)
11167 : wxNotifyEvent( type
, id
)
11174 m_control
= control
;
11179 SetEventObject(obj
);
11183 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
11185 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
11186 int rowOrCol
, int x
, int y
,
11187 bool control
, bool shift
, bool alt
, bool meta
)
11188 : wxNotifyEvent( type
, id
)
11190 m_rowOrCol
= rowOrCol
;
11193 m_control
= control
;
11198 SetEventObject(obj
);
11202 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
11204 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
11205 const wxGridCellCoords
& topLeft
,
11206 const wxGridCellCoords
& bottomRight
,
11207 bool sel
, bool control
,
11208 bool shift
, bool alt
, bool meta
)
11209 : wxNotifyEvent( type
, id
)
11211 m_topLeft
= topLeft
;
11212 m_bottomRight
= bottomRight
;
11214 m_control
= control
;
11219 SetEventObject(obj
);
11223 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
11225 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
11226 wxObject
* obj
, int row
,
11227 int col
, wxControl
* ctrl
)
11228 : wxCommandEvent(type
, id
)
11230 SetEventObject(obj
);
11236 #endif // wxUSE_GRID