1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
35 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
37 #else // wxUSE_NEW_GRID
41 #include "wx/dcclient.h"
42 #include "wx/settings.h"
44 #include "wx/textctrl.h"
45 #include "wx/checkbox.h"
46 #include "wx/combobox.h"
47 #include "wx/valtext.h"
50 #include "wx/textfile.h"
51 #include "wx/spinctrl.h"
52 #include "wx/tokenzr.h"
55 #include "wx/generic/gridsel.h"
57 #if defined(__WXMOTIF__)
58 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
60 #define WXUNUSED_MOTIF(identifier) identifier
63 #if defined(__WXGTK__)
64 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
66 #define WXUNUSED_GTK(identifier) identifier
69 // Required for wxIs... functions
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
78 struct wxGridCellWithAttr
80 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
81 : coords(row
, col
), attr(attr_
)
90 wxGridCellCoords coords
;
94 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
96 #include "wx/arrimpl.cpp"
98 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
99 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
129 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
130 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
131 const wxPoint
&pos
, const wxSize
&size
);
136 void OnPaint( wxPaintEvent
& event
);
137 void OnMouseEvent( wxMouseEvent
& event
);
138 void OnMouseWheel( wxMouseEvent
& event
);
139 void OnKeyDown( wxKeyEvent
& event
);
140 void OnKeyUp( wxKeyEvent
& );
142 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
143 DECLARE_EVENT_TABLE()
147 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
150 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
151 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
152 const wxPoint
&pos
, const wxSize
&size
);
157 void OnPaint( wxPaintEvent
&event
);
158 void OnMouseEvent( wxMouseEvent
& event
);
159 void OnMouseWheel( wxMouseEvent
& event
);
160 void OnKeyDown( wxKeyEvent
& event
);
161 void OnKeyUp( wxKeyEvent
& );
163 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
164 DECLARE_EVENT_TABLE()
168 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
171 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
172 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
173 const wxPoint
&pos
, const wxSize
&size
);
178 void OnMouseEvent( wxMouseEvent
& event
);
179 void OnMouseWheel( wxMouseEvent
& event
);
180 void OnKeyDown( wxKeyEvent
& event
);
181 void OnKeyUp( wxKeyEvent
& );
182 void OnPaint( wxPaintEvent
& event
);
184 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
185 DECLARE_EVENT_TABLE()
188 class WXDLLEXPORT wxGridWindow
: public wxWindow
193 m_owner
= (wxGrid
*)NULL
;
194 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
195 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
198 wxGridWindow( wxGrid
*parent
,
199 wxGridRowLabelWindow
*rowLblWin
,
200 wxGridColLabelWindow
*colLblWin
,
201 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
204 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
208 wxGridRowLabelWindow
*m_rowLabelWin
;
209 wxGridColLabelWindow
*m_colLabelWin
;
211 void OnPaint( wxPaintEvent
&event
);
212 void OnMouseWheel( wxMouseEvent
& event
);
213 void OnMouseEvent( wxMouseEvent
& event
);
214 void OnKeyDown( wxKeyEvent
& );
215 void OnKeyUp( wxKeyEvent
& );
216 void OnEraseBackground( wxEraseEvent
& );
219 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
220 DECLARE_EVENT_TABLE()
225 class wxGridCellEditorEvtHandler
: public wxEvtHandler
228 wxGridCellEditorEvtHandler()
229 : m_grid(0), m_editor(0)
231 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
232 : m_grid(grid
), m_editor(editor
)
235 void OnKeyDown(wxKeyEvent
& event
);
236 void OnChar(wxKeyEvent
& event
);
240 wxGridCellEditor
* m_editor
;
241 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
242 DECLARE_EVENT_TABLE()
246 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
247 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
248 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
249 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
254 // ----------------------------------------------------------------------------
255 // the internal data representation used by wxGridCellAttrProvider
256 // ----------------------------------------------------------------------------
258 // this class stores attributes set for cells
259 class WXDLLEXPORT wxGridCellAttrData
262 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
263 wxGridCellAttr
*GetAttr(int row
, int col
) const;
264 void UpdateAttrRows( size_t pos
, int numRows
);
265 void UpdateAttrCols( size_t pos
, int numCols
);
268 // searches for the attr for given cell, returns wxNOT_FOUND if not found
269 int FindIndex(int row
, int col
) const;
271 wxGridCellWithAttrArray m_attrs
;
274 // this class stores attributes set for rows or columns
275 class WXDLLEXPORT wxGridRowOrColAttrData
278 // empty ctor to suppress warnings
279 wxGridRowOrColAttrData() { }
280 ~wxGridRowOrColAttrData();
282 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
283 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
284 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
287 wxArrayInt m_rowsOrCols
;
288 wxArrayAttrs m_attrs
;
291 // NB: this is just a wrapper around 3 objects: one which stores cell
292 // attributes, and 2 others for row/col ones
293 class WXDLLEXPORT wxGridCellAttrProviderData
296 wxGridCellAttrData m_cellAttrs
;
297 wxGridRowOrColAttrData m_rowAttrs
,
302 // ----------------------------------------------------------------------------
303 // data structures used for the data type registry
304 // ----------------------------------------------------------------------------
306 struct wxGridDataTypeInfo
308 wxGridDataTypeInfo(const wxString
& typeName
,
309 wxGridCellRenderer
* renderer
,
310 wxGridCellEditor
* editor
)
311 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
314 ~wxGridDataTypeInfo()
316 wxSafeDecRef(m_renderer
);
317 wxSafeDecRef(m_editor
);
321 wxGridCellRenderer
* m_renderer
;
322 wxGridCellEditor
* m_editor
;
326 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
329 class WXDLLEXPORT wxGridTypeRegistry
332 wxGridTypeRegistry() {}
333 ~wxGridTypeRegistry();
335 void RegisterDataType(const wxString
& typeName
,
336 wxGridCellRenderer
* renderer
,
337 wxGridCellEditor
* editor
);
339 // find one of already registered data types
340 int FindRegisteredDataType(const wxString
& typeName
);
342 // try to FindRegisteredDataType(), if this fails and typeName is one of
343 // standard typenames, register it and return its index
344 int FindDataType(const wxString
& typeName
);
346 // try to FindDataType(), if it fails see if it is not one of already
347 // registered data types with some params in which case clone the
348 // registered data type and set params for it
349 int FindOrCloneDataType(const wxString
& typeName
);
351 wxGridCellRenderer
* GetRenderer(int index
);
352 wxGridCellEditor
* GetEditor(int index
);
355 wxGridDataTypeInfoArray m_typeinfo
;
358 // ----------------------------------------------------------------------------
359 // conditional compilation
360 // ----------------------------------------------------------------------------
362 #ifndef WXGRID_DRAW_LINES
363 #define WXGRID_DRAW_LINES 1
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 //#define DEBUG_ATTR_CACHE
371 #ifdef DEBUG_ATTR_CACHE
372 static size_t gs_nAttrCacheHits
= 0;
373 static size_t gs_nAttrCacheMisses
= 0;
374 #endif // DEBUG_ATTR_CACHE
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
381 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
384 // TODO: this doesn't work at all, grid cells have different sizes and approx
385 // calculations don't work as because of the size mismatch scrollbars
386 // sometimes fail to be shown when they should be or vice versa
388 // The scroll bars may be a little flakey once in a while, but that is
389 // surely much less horrible than having scroll lines of only 1!!!
392 // Well, it's still seriously broken so it might be better but needs
395 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
396 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
398 // the size of hash tables used a bit everywhere (the max number of elements
399 // in these hash tables is the number of rows/columns)
400 static const int GRID_HASH_SIZE
= 100;
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 static inline int GetScrollX(int x
)
408 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
411 static inline int GetScrollY(int y
)
413 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
416 // ============================================================================
418 // ============================================================================
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 wxGridCellEditor::wxGridCellEditor()
431 wxGridCellEditor::~wxGridCellEditor()
436 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
437 wxWindowID
WXUNUSED(id
),
438 wxEvtHandler
* evtHandler
)
441 m_control
->PushEventHandler(evtHandler
);
444 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
445 wxGridCellAttr
*attr
)
447 // erase the background because we might not fill the cell
448 wxClientDC
dc(m_control
->GetParent());
449 dc
.SetPen(*wxTRANSPARENT_PEN
);
450 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
451 dc
.DrawRectangle(rectCell
);
453 // redraw the control we just painted over
454 m_control
->Refresh();
457 void wxGridCellEditor::Destroy()
461 m_control
->PopEventHandler(TRUE
/* delete it*/);
463 m_control
->Destroy();
468 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
470 wxASSERT_MSG(m_control
,
471 wxT("The wxGridCellEditor must be Created first!"));
472 m_control
->Show(show
);
476 // set the colours/fonts if we have any
479 m_colFgOld
= m_control
->GetForegroundColour();
480 m_control
->SetForegroundColour(attr
->GetTextColour());
482 m_colBgOld
= m_control
->GetBackgroundColour();
483 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
485 m_fontOld
= m_control
->GetFont();
486 m_control
->SetFont(attr
->GetFont());
488 // can't do anything more in the base class version, the other
489 // attributes may only be used by the derived classes
494 // restore the standard colours fonts
495 if ( m_colFgOld
.Ok() )
497 m_control
->SetForegroundColour(m_colFgOld
);
498 m_colFgOld
= wxNullColour
;
501 if ( m_colBgOld
.Ok() )
503 m_control
->SetBackgroundColour(m_colBgOld
);
504 m_colBgOld
= wxNullColour
;
507 if ( m_fontOld
.Ok() )
509 m_control
->SetFont(m_fontOld
);
510 m_fontOld
= wxNullFont
;
515 void wxGridCellEditor::SetSize(const wxRect
& rect
)
517 wxASSERT_MSG(m_control
,
518 wxT("The wxGridCellEditor must be Created first!"));
519 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
522 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
527 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
529 // accept the simple key presses, not anything with Ctrl/Alt/Meta
530 return !(event
.ControlDown() || event
.AltDown());
533 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
538 void wxGridCellEditor::StartingClick()
544 // ----------------------------------------------------------------------------
545 // wxGridCellTextEditor
546 // ----------------------------------------------------------------------------
548 wxGridCellTextEditor::wxGridCellTextEditor()
553 void wxGridCellTextEditor::Create(wxWindow
* parent
,
555 wxEvtHandler
* evtHandler
)
557 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
558 wxDefaultPosition
, wxDefaultSize
559 #if defined(__WXMSW__)
560 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
561 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
565 // TODO: use m_maxChars
567 wxGridCellEditor::Create(parent
, id
, evtHandler
);
570 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
571 wxGridCellAttr
* WXUNUSED(attr
))
573 // as we fill the entire client area, don't do anything here to minimize
577 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
579 wxRect
rect(rectOrig
);
581 // Make the edit control large enough to allow for internal
584 // TODO: remove this if the text ctrl sizing is improved esp. for
587 #if defined(__WXGTK__)
596 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
598 // MB: treat MSW separately here otherwise the caret doesn't show
599 // when the editor is in the first row.
600 #if defined(__WXMSW__)
603 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
606 #if defined(__WXMOTIF__)
610 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
611 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
612 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
613 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
616 wxGridCellEditor::SetSize(rect
);
619 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
621 wxASSERT_MSG(m_control
,
622 wxT("The wxGridCellEditor must be Created first!"));
624 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
626 DoBeginEdit(m_startValue
);
629 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
631 Text()->SetValue(startValue
);
632 Text()->SetInsertionPointEnd();
633 Text()->SetSelection(-1,-1);
637 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
640 wxASSERT_MSG(m_control
,
641 wxT("The wxGridCellEditor must be Created first!"));
643 bool changed
= FALSE
;
644 wxString value
= Text()->GetValue();
645 if (value
!= m_startValue
)
649 grid
->GetTable()->SetValue(row
, col
, value
);
651 m_startValue
= wxEmptyString
;
652 Text()->SetValue(m_startValue
);
658 void wxGridCellTextEditor::Reset()
660 wxASSERT_MSG(m_control
,
661 wxT("The wxGridCellEditor must be Created first!"));
663 DoReset(m_startValue
);
666 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
668 Text()->SetValue(startValue
);
669 Text()->SetInsertionPointEnd();
672 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
674 if ( wxGridCellEditor::IsAcceptedKey(event
) )
676 int keycode
= event
.GetKeyCode();
690 case WXK_NUMPAD_MULTIPLY
:
694 case WXK_NUMPAD_SUBTRACT
:
696 case WXK_NUMPAD_DECIMAL
:
698 case WXK_NUMPAD_DIVIDE
:
702 // accept 8 bit chars too if isprint() agrees
703 if ( (keycode
< 255) && (isprint(keycode
)) )
711 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
713 if ( !Text()->EmulateKeyPress(event
) )
719 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
720 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
722 #if defined(__WXMOTIF__) || defined(__WXGTK__)
723 // wxMotif needs a little extra help...
724 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
725 wxString
s( Text()->GetValue() );
726 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
728 Text()->SetInsertionPoint( pos
);
730 // the other ports can handle a Return key press
736 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
746 if ( !params
.ToLong(&tmp
) )
748 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
752 m_maxChars
= (size_t)tmp
;
757 // return the value in the text control
758 wxString
wxGridCellTextEditor::GetValue() const
760 return Text()->GetValue();
763 // ----------------------------------------------------------------------------
764 // wxGridCellNumberEditor
765 // ----------------------------------------------------------------------------
767 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
773 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
775 wxEvtHandler
* evtHandler
)
779 // create a spin ctrl
780 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
781 wxDefaultPosition
, wxDefaultSize
,
785 wxGridCellEditor::Create(parent
, id
, evtHandler
);
789 // just a text control
790 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
793 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
794 #endif // wxUSE_VALIDATORS
798 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
800 // first get the value
801 wxGridTableBase
*table
= grid
->GetTable();
802 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
804 m_valueOld
= table
->GetValueAsLong(row
, col
);
809 wxString sValue
= table
->GetValue(row
, col
);
810 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
812 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
819 Spin()->SetValue((int)m_valueOld
);
824 DoBeginEdit(GetString());
828 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
837 value
= Spin()->GetValue();
838 changed
= value
!= m_valueOld
;
840 text
= wxString::Format(wxT("%ld"), value
);
844 text
= Text()->GetValue();
845 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
850 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
851 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
853 grid
->GetTable()->SetValue(row
, col
, text
);
859 void wxGridCellNumberEditor::Reset()
863 Spin()->SetValue((int)m_valueOld
);
867 DoReset(GetString());
871 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
873 if ( wxGridCellEditor::IsAcceptedKey(event
) )
875 int keycode
= event
.GetKeyCode();
891 case WXK_NUMPAD_SUBTRACT
:
897 if ( (keycode
< 128) && isdigit(keycode
) )
905 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
909 int keycode
= (int) event
.KeyCode();
910 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-'
911 || keycode
== WXK_NUMPAD0
912 || keycode
== WXK_NUMPAD1
913 || keycode
== WXK_NUMPAD2
914 || keycode
== WXK_NUMPAD3
915 || keycode
== WXK_NUMPAD4
916 || keycode
== WXK_NUMPAD5
917 || keycode
== WXK_NUMPAD6
918 || keycode
== WXK_NUMPAD7
919 || keycode
== WXK_NUMPAD8
920 || keycode
== WXK_NUMPAD9
921 || keycode
== WXK_ADD
922 || keycode
== WXK_NUMPAD_ADD
923 || keycode
== WXK_SUBTRACT
924 || keycode
== WXK_NUMPAD_SUBTRACT
)
926 wxGridCellTextEditor::StartingKey(event
);
936 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
947 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
951 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
955 // skip the error message below
960 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
964 // return the value in the spin control if it is there (the text control otherwise)
965 wxString
wxGridCellNumberEditor::GetValue() const
971 long value
= Spin()->GetValue();
972 s
.Printf(wxT("%ld"), value
);
976 s
= Text()->GetValue();
981 // ----------------------------------------------------------------------------
982 // wxGridCellFloatEditor
983 // ----------------------------------------------------------------------------
985 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
988 m_precision
= precision
;
991 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
993 wxEvtHandler
* evtHandler
)
995 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
998 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
999 #endif // wxUSE_VALIDATORS
1002 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1004 // first get the value
1005 wxGridTableBase
*table
= grid
->GetTable();
1006 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1008 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1013 wxString sValue
= table
->GetValue(row
, col
);
1014 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1016 wxFAIL_MSG( _T("this cell doesn't have float value") );
1021 DoBeginEdit(GetString());
1024 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1028 wxString
text(Text()->GetValue());
1030 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1032 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1033 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1035 grid
->GetTable()->SetValue(row
, col
, text
);
1042 void wxGridCellFloatEditor::Reset()
1044 DoReset(GetString());
1047 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1049 int keycode
= (int)event
.KeyCode();
1050 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1051 || keycode
== WXK_NUMPAD0
1052 || keycode
== WXK_NUMPAD1
1053 || keycode
== WXK_NUMPAD2
1054 || keycode
== WXK_NUMPAD3
1055 || keycode
== WXK_NUMPAD4
1056 || keycode
== WXK_NUMPAD5
1057 || keycode
== WXK_NUMPAD6
1058 || keycode
== WXK_NUMPAD7
1059 || keycode
== WXK_NUMPAD8
1060 || keycode
== WXK_NUMPAD9
1061 || keycode
== WXK_ADD
1062 || keycode
== WXK_NUMPAD_ADD
1063 || keycode
== WXK_SUBTRACT
1064 || keycode
== WXK_NUMPAD_SUBTRACT
)
1066 wxGridCellTextEditor::StartingKey(event
);
1068 // skip Skip() below
1075 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1086 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1090 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1092 m_precision
= (int)tmp
;
1094 // skip the error message below
1099 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1103 wxString
wxGridCellFloatEditor::GetString() const
1106 if ( m_width
== -1 )
1108 // default width/precision
1111 else if ( m_precision
== -1 )
1113 // default precision
1114 fmt
.Printf(_T("%%%d.f"), m_width
);
1118 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1121 return wxString::Format(fmt
, m_valueOld
);
1124 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1126 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1128 int keycode
= event
.GetKeyCode();
1142 case WXK_NUMPAD_ADD
:
1144 case WXK_NUMPAD_SUBTRACT
:
1146 case WXK_NUMPAD_DECIMAL
:
1150 // additionally accept 'e' as in '1e+6'
1151 if ( (keycode
< 128) &&
1152 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1160 #endif // wxUSE_TEXTCTRL
1164 // ----------------------------------------------------------------------------
1165 // wxGridCellBoolEditor
1166 // ----------------------------------------------------------------------------
1168 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1170 wxEvtHandler
* evtHandler
)
1172 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1173 wxDefaultPosition
, wxDefaultSize
,
1176 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1179 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1181 bool resize
= FALSE
;
1182 wxSize size
= m_control
->GetSize();
1183 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1185 // check if the checkbox is not too big/small for this cell
1186 wxSize sizeBest
= m_control
->GetBestSize();
1187 if ( !(size
== sizeBest
) )
1189 // reset to default size if it had been made smaller
1195 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1197 // leave 1 pixel margin
1198 size
.x
= size
.y
= minSize
- 2;
1205 m_control
->SetSize(size
);
1208 // position it in the centre of the rectangle (TODO: support alignment?)
1210 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1211 // the checkbox without label still has some space to the right in wxGTK,
1212 // so shift it to the right
1214 #elif defined(__WXMSW__)
1215 // here too, but in other way
1220 int hAlign
= wxALIGN_CENTRE
;
1221 int vAlign
= wxALIGN_CENTRE
;
1223 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1226 if (hAlign
== wxALIGN_LEFT
)
1232 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1234 else if (hAlign
== wxALIGN_RIGHT
)
1236 x
= r
.x
+ r
.width
- size
.x
- 2;
1237 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1239 else if (hAlign
== wxALIGN_CENTRE
)
1241 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1242 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1245 m_control
->Move(x
, y
);
1248 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1250 m_control
->Show(show
);
1254 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1255 CBox()->SetBackgroundColour(colBg
);
1259 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1261 wxASSERT_MSG(m_control
,
1262 wxT("The wxGridCellEditor must be Created first!"));
1264 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1265 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1268 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1269 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1271 CBox()->SetValue(m_startValue
);
1275 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1278 wxASSERT_MSG(m_control
,
1279 wxT("The wxGridCellEditor must be Created first!"));
1281 bool changed
= FALSE
;
1282 bool value
= CBox()->GetValue();
1283 if ( value
!= m_startValue
)
1288 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1289 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1291 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1297 void wxGridCellBoolEditor::Reset()
1299 wxASSERT_MSG(m_control
,
1300 wxT("The wxGridCellEditor must be Created first!"));
1302 CBox()->SetValue(m_startValue
);
1305 void wxGridCellBoolEditor::StartingClick()
1307 CBox()->SetValue(!CBox()->GetValue());
1310 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1312 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1314 int keycode
= event
.GetKeyCode();
1318 case WXK_NUMPAD_MULTIPLY
:
1320 case WXK_NUMPAD_ADD
:
1322 case WXK_NUMPAD_SUBTRACT
:
1333 // return the value as "1" for true and the empty string for false
1334 wxString
wxGridCellBoolEditor::GetValue() const
1336 bool bSet
= CBox()->GetValue();
1337 return bSet
? _T("1") : wxEmptyString
;
1340 #endif // wxUSE_CHECKBOX
1344 // ----------------------------------------------------------------------------
1345 // wxGridCellChoiceEditor
1346 // ----------------------------------------------------------------------------
1348 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1349 const wxString choices
[],
1351 : m_allowOthers(allowOthers
)
1355 m_choices
.Alloc(count
);
1356 for ( size_t n
= 0; n
< count
; n
++ )
1358 m_choices
.Add(choices
[n
]);
1363 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1365 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1366 editor
->m_allowOthers
= m_allowOthers
;
1367 editor
->m_choices
= m_choices
;
1372 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1374 wxEvtHandler
* evtHandler
)
1376 size_t count
= m_choices
.GetCount();
1377 wxString
*choices
= new wxString
[count
];
1378 for ( size_t n
= 0; n
< count
; n
++ )
1380 choices
[n
] = m_choices
[n
];
1383 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1384 wxDefaultPosition
, wxDefaultSize
,
1386 m_allowOthers
? 0 : wxCB_READONLY
);
1390 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1393 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1394 wxGridCellAttr
* attr
)
1396 // as we fill the entire client area, don't do anything here to minimize
1399 // TODO: It doesn't actually fill the client area since the height of a
1400 // combo always defaults to the standard... Until someone has time to
1401 // figure out the right rectangle to paint, just do it the normal way...
1402 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1405 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1407 wxASSERT_MSG(m_control
,
1408 wxT("The wxGridCellEditor must be Created first!"));
1410 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1413 Combo()->SetValue(m_startValue
);
1416 // find the right position, or default to the first if not found
1417 int pos
= Combo()->FindString(m_startValue
);
1420 Combo()->SetSelection(pos
);
1422 Combo()->SetInsertionPointEnd();
1423 Combo()->SetFocus();
1426 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1429 wxString value
= Combo()->GetValue();
1430 bool changed
= value
!= m_startValue
;
1433 grid
->GetTable()->SetValue(row
, col
, value
);
1435 m_startValue
= wxEmptyString
;
1437 Combo()->SetValue(m_startValue
);
1439 Combo()->SetSelection(0);
1444 void wxGridCellChoiceEditor::Reset()
1446 Combo()->SetValue(m_startValue
);
1447 Combo()->SetInsertionPointEnd();
1450 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1460 wxStringTokenizer
tk(params
, _T(','));
1461 while ( tk
.HasMoreTokens() )
1463 m_choices
.Add(tk
.GetNextToken());
1467 // return the value in the text control
1468 wxString
wxGridCellChoiceEditor::GetValue() const
1470 return Combo()->GetValue();
1473 #endif // wxUSE_COMBOBOX
1475 // ----------------------------------------------------------------------------
1476 // wxGridCellEditorEvtHandler
1477 // ----------------------------------------------------------------------------
1479 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1481 switch ( event
.KeyCode() )
1485 m_grid
->DisableCellEditControl();
1489 m_grid
->GetEventHandler()->ProcessEvent( event
);
1493 case WXK_NUMPAD_ENTER
:
1494 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1495 m_editor
->HandleReturn(event
);
1504 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1506 switch ( event
.KeyCode() )
1511 case WXK_NUMPAD_ENTER
:
1519 // ----------------------------------------------------------------------------
1520 // wxGridCellWorker is an (almost) empty common base class for
1521 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1522 // ----------------------------------------------------------------------------
1524 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1529 wxGridCellWorker::~wxGridCellWorker()
1533 // ============================================================================
1535 // ============================================================================
1537 // ----------------------------------------------------------------------------
1538 // wxGridCellRenderer
1539 // ----------------------------------------------------------------------------
1541 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1542 wxGridCellAttr
& attr
,
1545 int WXUNUSED(row
), int WXUNUSED(col
),
1548 dc
.SetBackgroundMode( wxSOLID
);
1550 // grey out fields if the grid is disabled
1551 if( grid
.IsEnabled() )
1555 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1559 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1564 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1567 dc
.SetPen( *wxTRANSPARENT_PEN
);
1568 dc
.DrawRectangle(rect
);
1571 // ----------------------------------------------------------------------------
1572 // wxGridCellStringRenderer
1573 // ----------------------------------------------------------------------------
1575 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1576 wxGridCellAttr
& attr
,
1580 dc
.SetBackgroundMode( wxTRANSPARENT
);
1582 // TODO some special colours for attr.IsReadOnly() case?
1584 // different coloured text when the grid is disabled
1585 if( grid
.IsEnabled() )
1589 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1590 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1594 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1595 dc
.SetTextForeground( attr
.GetTextColour() );
1600 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1601 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1604 dc
.SetFont( attr
.GetFont() );
1607 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1609 const wxString
& text
)
1611 wxCoord x
= 0, y
= 0, max_x
= 0;
1612 dc
.SetFont(attr
.GetFont());
1613 wxStringTokenizer
tk(text
, _T('\n'));
1614 while ( tk
.HasMoreTokens() )
1616 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1617 max_x
= wxMax(max_x
, x
);
1620 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1622 return wxSize(max_x
, y
);
1625 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1626 wxGridCellAttr
& attr
,
1630 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1633 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1634 wxGridCellAttr
& attr
,
1636 const wxRect
& rectCell
,
1640 wxRect rect
= rectCell
;
1643 // erase only this cells background, overflow cells should have been erased
1644 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1647 attr
.GetAlignment(&hAlign
, &vAlign
);
1649 int overflowCols
= 0;
1651 if (attr
.GetOverflow())
1653 int cols
= grid
.GetNumberCols();
1654 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1655 int cell_rows
, cell_cols
;
1656 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1657 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1659 int i
, c_cols
, c_rows
;
1660 for (i
= col
+cell_cols
; i
< cols
; i
++)
1662 bool is_empty
= TRUE
;
1663 for (int j
=row
; j
<row
+cell_rows
; j
++)
1665 // check w/ anchor cell for multicell block
1666 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1667 if (c_rows
> 0) c_rows
= 0;
1668 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1675 rect
.width
+= grid
.GetColSize(i
);
1681 if (rect
.width
>= best_width
) break;
1683 overflowCols
= i
- col
- cell_cols
+ 1;
1684 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1687 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1689 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1691 clip
.x
+= rectCell
.width
;
1692 // draw each overflow cell individually
1693 int col_end
= col
+cell_cols
+overflowCols
;
1694 if (col_end
>= grid
.GetNumberCols())
1695 col_end
= grid
.GetNumberCols() - 1;
1696 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1698 clip
.width
= grid
.GetColSize(i
) - 1;
1699 dc
.DestroyClippingRegion();
1700 dc
.SetClippingRegion(clip
);
1702 SetTextColoursAndFont(grid
, attr
, dc
,
1703 grid
.IsInSelection(row
,i
));
1705 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1706 rect
, hAlign
, vAlign
);
1707 clip
.x
+= grid
.GetColSize(i
) - 1;
1713 dc
.DestroyClippingRegion();
1717 // now we only have to draw the text
1718 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1720 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1721 rect
, hAlign
, vAlign
);
1724 // ----------------------------------------------------------------------------
1725 // wxGridCellNumberRenderer
1726 // ----------------------------------------------------------------------------
1728 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1730 wxGridTableBase
*table
= grid
.GetTable();
1732 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1734 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1738 text
= table
->GetValue(row
, col
);
1744 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1745 wxGridCellAttr
& attr
,
1747 const wxRect
& rectCell
,
1751 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1753 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1755 // draw the text right aligned by default
1757 attr
.GetAlignment(&hAlign
, &vAlign
);
1758 hAlign
= wxALIGN_RIGHT
;
1760 wxRect rect
= rectCell
;
1763 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1766 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1767 wxGridCellAttr
& attr
,
1771 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1774 // ----------------------------------------------------------------------------
1775 // wxGridCellFloatRenderer
1776 // ----------------------------------------------------------------------------
1778 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1781 SetPrecision(precision
);
1784 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1786 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1787 renderer
->m_width
= m_width
;
1788 renderer
->m_precision
= m_precision
;
1789 renderer
->m_format
= m_format
;
1794 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1796 wxGridTableBase
*table
= grid
.GetTable();
1801 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1803 val
= table
->GetValueAsDouble(row
, col
);
1808 text
= table
->GetValue(row
, col
);
1809 hasDouble
= text
.ToDouble(&val
);
1816 if ( m_width
== -1 )
1818 if ( m_precision
== -1 )
1820 // default width/precision
1821 m_format
= _T("%f");
1825 m_format
.Printf(_T("%%.%df"), m_precision
);
1828 else if ( m_precision
== -1 )
1830 // default precision
1831 m_format
.Printf(_T("%%%d.f"), m_width
);
1835 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1839 text
.Printf(m_format
, val
);
1842 //else: text already contains the string
1847 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1848 wxGridCellAttr
& attr
,
1850 const wxRect
& rectCell
,
1854 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1856 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1858 // draw the text right aligned by default
1860 attr
.GetAlignment(&hAlign
, &vAlign
);
1861 hAlign
= wxALIGN_RIGHT
;
1863 wxRect rect
= rectCell
;
1866 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1869 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1870 wxGridCellAttr
& attr
,
1874 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1877 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1881 // reset to defaults
1887 wxString tmp
= params
.BeforeFirst(_T(','));
1891 if ( tmp
.ToLong(&width
) )
1893 SetWidth((int)width
);
1897 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1901 tmp
= params
.AfterFirst(_T(','));
1905 if ( tmp
.ToLong(&precision
) )
1907 SetPrecision((int)precision
);
1911 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1919 // ----------------------------------------------------------------------------
1920 // wxGridCellBoolRenderer
1921 // ----------------------------------------------------------------------------
1923 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1925 // FIXME these checkbox size calculations are really ugly...
1927 // between checkmark and box
1928 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1930 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1931 wxGridCellAttr
& WXUNUSED(attr
),
1936 // compute it only once (no locks for MT safeness in GUI thread...)
1937 if ( !ms_sizeCheckMark
.x
)
1939 // get checkbox size
1940 wxCoord checkSize
= 0;
1941 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1942 wxSize size
= checkbox
->GetBestSize();
1943 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1945 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1946 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1947 checkSize
-= size
.y
/ 2;
1952 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1955 return ms_sizeCheckMark
;
1958 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1959 wxGridCellAttr
& attr
,
1965 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1967 // draw a check mark in the centre (ignoring alignment - TODO)
1968 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1970 // don't draw outside the cell
1971 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1972 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1974 // and even leave (at least) 1 pixel margin
1975 size
.x
= size
.y
= minSize
- 2;
1978 // draw a border around checkmark
1980 attr
.GetAlignment(& hAlign
, &vAlign
);
1983 if (hAlign
== wxALIGN_CENTRE
)
1985 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1986 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1987 rectBorder
.width
= size
.x
;
1988 rectBorder
.height
= size
.y
;
1990 else if (hAlign
== wxALIGN_LEFT
)
1992 rectBorder
.x
= rect
.x
+ 2;
1993 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1994 rectBorder
.width
= size
.x
;
1995 rectBorder
.height
= size
.y
;
1997 else if (hAlign
== wxALIGN_RIGHT
)
1999 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2000 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2001 rectBorder
.width
= size
.x
;
2002 rectBorder
.height
= size
.y
;
2006 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2007 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2010 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2011 value
= !( !cellval
|| (cellval
== wxT("0")) );
2016 wxRect rectMark
= rectBorder
;
2018 // MSW DrawCheckMark() is weird (and should probably be changed...)
2019 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2023 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2026 dc
.SetTextForeground(attr
.GetTextColour());
2027 dc
.DrawCheckMark(rectMark
);
2030 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2031 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2032 dc
.DrawRectangle(rectBorder
);
2035 // ----------------------------------------------------------------------------
2037 // ----------------------------------------------------------------------------
2039 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2043 m_isReadOnly
= Unset
;
2048 m_attrkind
= wxGridCellAttr::Cell
;
2050 m_sizeRows
= m_sizeCols
= 1;
2053 SetDefAttr(attrDefault
);
2056 wxGridCellAttr
*wxGridCellAttr::Clone() const
2058 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2060 if ( HasTextColour() )
2061 attr
->SetTextColour(GetTextColour());
2062 if ( HasBackgroundColour() )
2063 attr
->SetBackgroundColour(GetBackgroundColour());
2065 attr
->SetFont(GetFont());
2066 if ( HasAlignment() )
2067 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2069 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2073 attr
->SetRenderer(m_renderer
);
2074 m_renderer
->IncRef();
2078 attr
->SetEditor(m_editor
);
2083 attr
->SetReadOnly();
2085 attr
->SetKind( m_attrkind
);
2090 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2092 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2093 SetTextColour(mergefrom
->GetTextColour());
2094 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2095 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2096 if ( !HasFont() && mergefrom
->HasFont() )
2097 SetFont(mergefrom
->GetFont());
2098 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2100 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2101 SetAlignment(hAlign
, vAlign
);
2104 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2106 // Directly access member functions as GetRender/Editor don't just return
2107 // m_renderer/m_editor
2109 // Maybe add support for merge of Render and Editor?
2110 if (!HasRenderer() && mergefrom
->HasRenderer() )
2112 m_renderer
= mergefrom
->m_renderer
;
2113 m_renderer
->IncRef();
2115 if ( !HasEditor() && mergefrom
->HasEditor() )
2117 m_editor
= mergefrom
->m_editor
;
2120 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2121 SetReadOnly(mergefrom
->IsReadOnly());
2123 SetDefAttr(mergefrom
->m_defGridAttr
);
2126 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2128 // The size of a cell is normally 1,1
2130 // If this cell is larger (2,2) then this is the top left cell
2131 // the other cells that will be covered (lower right cells) must be
2132 // set to negative or zero values such that
2133 // row + num_rows of the covered cell points to the larger cell (this cell)
2134 // same goes for the col + num_cols.
2136 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2138 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2139 !((num_rows
<=0)&&(num_cols
>0)) ||
2140 !((num_rows
==0)&&(num_cols
==0))),
2141 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2143 m_sizeRows
= num_rows
;
2144 m_sizeCols
= num_cols
;
2147 const wxColour
& wxGridCellAttr::GetTextColour() const
2149 if (HasTextColour())
2153 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2155 return m_defGridAttr
->GetTextColour();
2159 wxFAIL_MSG(wxT("Missing default cell attribute"));
2160 return wxNullColour
;
2165 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2167 if (HasBackgroundColour())
2169 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2170 return m_defGridAttr
->GetBackgroundColour();
2173 wxFAIL_MSG(wxT("Missing default cell attribute"));
2174 return wxNullColour
;
2179 const wxFont
& wxGridCellAttr::GetFont() const
2183 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2184 return m_defGridAttr
->GetFont();
2187 wxFAIL_MSG(wxT("Missing default cell attribute"));
2193 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2197 if ( hAlign
) *hAlign
= m_hAlign
;
2198 if ( vAlign
) *vAlign
= m_vAlign
;
2200 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2201 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2204 wxFAIL_MSG(wxT("Missing default cell attribute"));
2208 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2210 if ( num_rows
) *num_rows
= m_sizeRows
;
2211 if ( num_cols
) *num_cols
= m_sizeCols
;
2214 // GetRenderer and GetEditor use a slightly different decision path about
2215 // which attribute to use. If a non-default attr object has one then it is
2216 // used, otherwise the default editor or renderer is fetched from the grid and
2217 // used. It should be the default for the data type of the cell. If it is
2218 // NULL (because the table has a type that the grid does not have in its
2219 // registry,) then the grid's default editor or renderer is used.
2221 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2223 wxGridCellRenderer
*renderer
;
2225 if ( m_renderer
&& this != m_defGridAttr
)
2227 // use the cells renderer if it has one
2228 renderer
= m_renderer
;
2231 else // no non default cell renderer
2233 // get default renderer for the data type
2236 // GetDefaultRendererForCell() will do IncRef() for us
2237 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2246 if (m_defGridAttr
&& this != m_defGridAttr
)
2248 // if we still don't have one then use the grid default
2249 // (no need for IncRef() here neither)
2250 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2252 else // default grid attr
2254 // use m_renderer which we had decided not to use initially
2255 renderer
= m_renderer
;
2262 // we're supposed to always find something
2263 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2268 // same as above, except for s/renderer/editor/g
2269 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2271 wxGridCellEditor
*editor
;
2273 if ( m_editor
&& this != m_defGridAttr
)
2275 // use the cells editor if it has one
2279 else // no non default cell editor
2281 // get default editor for the data type
2284 // GetDefaultEditorForCell() will do IncRef() for us
2285 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2294 if ( m_defGridAttr
&& this != m_defGridAttr
)
2296 // if we still don't have one then use the grid default
2297 // (no need for IncRef() here neither)
2298 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2300 else // default grid attr
2302 // use m_editor which we had decided not to use initially
2310 // we're supposed to always find something
2311 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2316 // ----------------------------------------------------------------------------
2317 // wxGridCellAttrData
2318 // ----------------------------------------------------------------------------
2320 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2322 int n
= FindIndex(row
, col
);
2323 if ( n
== wxNOT_FOUND
)
2325 // add the attribute
2326 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2330 // free the old attribute
2331 m_attrs
[(size_t)n
].attr
->DecRef();
2335 // change the attribute
2336 m_attrs
[(size_t)n
].attr
= attr
;
2340 // remove this attribute
2341 m_attrs
.RemoveAt((size_t)n
);
2346 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2348 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2350 int n
= FindIndex(row
, col
);
2351 if ( n
!= wxNOT_FOUND
)
2353 attr
= m_attrs
[(size_t)n
].attr
;
2360 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2362 size_t count
= m_attrs
.GetCount();
2363 for ( size_t n
= 0; n
< count
; n
++ )
2365 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2366 wxCoord row
= coords
.GetRow();
2367 if ((size_t)row
>= pos
)
2371 // If rows inserted, include row counter where necessary
2372 coords
.SetRow(row
+ numRows
);
2374 else if (numRows
< 0)
2376 // If rows deleted ...
2377 if ((size_t)row
>= pos
- numRows
)
2379 // ...either decrement row counter (if row still exists)...
2380 coords
.SetRow(row
+ numRows
);
2384 // ...or remove the attribute
2385 m_attrs
.RemoveAt((size_t)n
);
2393 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2395 size_t count
= m_attrs
.GetCount();
2396 for ( size_t n
= 0; n
< count
; n
++ )
2398 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2399 wxCoord col
= coords
.GetCol();
2400 if ( (size_t)col
>= pos
)
2404 // If rows inserted, include row counter where necessary
2405 coords
.SetCol(col
+ numCols
);
2407 else if (numCols
< 0)
2409 // If rows deleted ...
2410 if ((size_t)col
>= pos
- numCols
)
2412 // ...either decrement row counter (if row still exists)...
2413 coords
.SetCol(col
+ numCols
);
2417 // ...or remove the attribute
2418 m_attrs
.RemoveAt((size_t)n
);
2426 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2428 size_t count
= m_attrs
.GetCount();
2429 for ( size_t n
= 0; n
< count
; n
++ )
2431 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2432 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2441 // ----------------------------------------------------------------------------
2442 // wxGridRowOrColAttrData
2443 // ----------------------------------------------------------------------------
2445 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2447 size_t count
= m_attrs
.Count();
2448 for ( size_t n
= 0; n
< count
; n
++ )
2450 m_attrs
[n
]->DecRef();
2454 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2456 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2458 int n
= m_rowsOrCols
.Index(rowOrCol
);
2459 if ( n
!= wxNOT_FOUND
)
2461 attr
= m_attrs
[(size_t)n
];
2468 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2470 int i
= m_rowsOrCols
.Index(rowOrCol
);
2471 if ( i
== wxNOT_FOUND
)
2473 // add the attribute
2474 m_rowsOrCols
.Add(rowOrCol
);
2479 size_t n
= (size_t)i
;
2482 // change the attribute
2483 m_attrs
[n
]->DecRef();
2488 // remove this attribute
2489 m_attrs
[n
]->DecRef();
2490 m_rowsOrCols
.RemoveAt(n
);
2491 m_attrs
.RemoveAt(n
);
2496 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2498 size_t count
= m_attrs
.GetCount();
2499 for ( size_t n
= 0; n
< count
; n
++ )
2501 int & rowOrCol
= m_rowsOrCols
[n
];
2502 if ( (size_t)rowOrCol
>= pos
)
2504 if ( numRowsOrCols
> 0 )
2506 // If rows inserted, include row counter where necessary
2507 rowOrCol
+= numRowsOrCols
;
2509 else if ( numRowsOrCols
< 0)
2511 // If rows deleted, either decrement row counter (if row still exists)
2512 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2513 rowOrCol
+= numRowsOrCols
;
2516 m_rowsOrCols
.RemoveAt((size_t)n
);
2517 m_attrs
.RemoveAt((size_t)n
);
2525 // ----------------------------------------------------------------------------
2526 // wxGridCellAttrProvider
2527 // ----------------------------------------------------------------------------
2529 wxGridCellAttrProvider::wxGridCellAttrProvider()
2531 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2534 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2539 void wxGridCellAttrProvider::InitData()
2541 m_data
= new wxGridCellAttrProviderData
;
2544 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2545 wxGridCellAttr::wxAttrKind kind
) const
2547 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2552 case (wxGridCellAttr::Any
):
2553 //Get cached merge attributes.
2554 // Currenlty not used as no cache implemented as not mutiable
2555 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2558 //Basicaly implement old version.
2559 //Also check merge cache, so we don't have to re-merge every time..
2560 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2561 *attrrow
= (wxGridCellAttr
*)NULL
,
2562 *attrcol
= (wxGridCellAttr
*)NULL
;
2564 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2565 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2566 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2568 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2569 // Two or move are non NULL
2570 attr
= new wxGridCellAttr
;
2571 attr
->SetKind(wxGridCellAttr::Merged
);
2575 attr
->MergeWith(attrcell
);
2579 attr
->MergeWith(attrcol
);
2583 attr
->MergeWith(attrrow
);
2586 //store merge attr if cache implemented
2588 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2592 // one or none is non null return it or null.
2593 if(attrrow
) attr
= attrrow
;
2594 if(attrcol
) attr
= attrcol
;
2595 if(attrcell
) attr
= attrcell
;
2599 case (wxGridCellAttr::Cell
):
2600 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2602 case (wxGridCellAttr::Col
):
2603 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2605 case (wxGridCellAttr::Row
):
2606 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2610 // (wxGridCellAttr::Default):
2611 // (wxGridCellAttr::Merged):
2618 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2624 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2627 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2632 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2635 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2640 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2643 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2647 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2649 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2653 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2657 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2659 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2663 // ----------------------------------------------------------------------------
2664 // wxGridTypeRegistry
2665 // ----------------------------------------------------------------------------
2667 wxGridTypeRegistry::~wxGridTypeRegistry()
2669 size_t count
= m_typeinfo
.Count();
2670 for ( size_t i
= 0; i
< count
; i
++ )
2671 delete m_typeinfo
[i
];
2675 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2676 wxGridCellRenderer
* renderer
,
2677 wxGridCellEditor
* editor
)
2679 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2681 // is it already registered?
2682 int loc
= FindRegisteredDataType(typeName
);
2683 if ( loc
!= wxNOT_FOUND
)
2685 delete m_typeinfo
[loc
];
2686 m_typeinfo
[loc
] = info
;
2690 m_typeinfo
.Add(info
);
2694 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2696 size_t count
= m_typeinfo
.GetCount();
2697 for ( size_t i
= 0; i
< count
; i
++ )
2699 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2708 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2710 int index
= FindRegisteredDataType(typeName
);
2711 if ( index
== wxNOT_FOUND
)
2713 // check whether this is one of the standard ones, in which case
2714 // register it "on the fly"
2716 if ( typeName
== wxGRID_VALUE_STRING
)
2718 RegisterDataType(wxGRID_VALUE_STRING
,
2719 new wxGridCellStringRenderer
,
2720 new wxGridCellTextEditor
);
2722 #endif // wxUSE_TEXTCTRL
2724 if ( typeName
== wxGRID_VALUE_BOOL
)
2726 RegisterDataType(wxGRID_VALUE_BOOL
,
2727 new wxGridCellBoolRenderer
,
2728 new wxGridCellBoolEditor
);
2730 #endif // wxUSE_CHECKBOX
2732 if ( typeName
== wxGRID_VALUE_NUMBER
)
2734 RegisterDataType(wxGRID_VALUE_NUMBER
,
2735 new wxGridCellNumberRenderer
,
2736 new wxGridCellNumberEditor
);
2738 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2740 RegisterDataType(wxGRID_VALUE_FLOAT
,
2741 new wxGridCellFloatRenderer
,
2742 new wxGridCellFloatEditor
);
2744 #endif // wxUSE_TEXTCTRL
2746 if ( typeName
== wxGRID_VALUE_CHOICE
)
2748 RegisterDataType(wxGRID_VALUE_CHOICE
,
2749 new wxGridCellStringRenderer
,
2750 new wxGridCellChoiceEditor
);
2752 #endif // wxUSE_COMBOBOX
2757 // we get here only if just added the entry for this type, so return
2759 index
= m_typeinfo
.GetCount() - 1;
2765 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2767 int index
= FindDataType(typeName
);
2768 if ( index
== wxNOT_FOUND
)
2770 // the first part of the typename is the "real" type, anything after ':'
2771 // are the parameters for the renderer
2772 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2773 if ( index
== wxNOT_FOUND
)
2778 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2779 wxGridCellRenderer
*rendererOld
= renderer
;
2780 renderer
= renderer
->Clone();
2781 rendererOld
->DecRef();
2783 wxGridCellEditor
*editor
= GetEditor(index
);
2784 wxGridCellEditor
*editorOld
= editor
;
2785 editor
= editor
->Clone();
2786 editorOld
->DecRef();
2788 // do it even if there are no parameters to reset them to defaults
2789 wxString params
= typeName
.AfterFirst(_T(':'));
2790 renderer
->SetParameters(params
);
2791 editor
->SetParameters(params
);
2793 // register the new typename
2794 RegisterDataType(typeName
, renderer
, editor
);
2796 // we just registered it, it's the last one
2797 index
= m_typeinfo
.GetCount() - 1;
2803 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2805 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2811 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2813 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2819 // ----------------------------------------------------------------------------
2821 // ----------------------------------------------------------------------------
2823 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2826 wxGridTableBase::wxGridTableBase()
2828 m_view
= (wxGrid
*) NULL
;
2829 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2832 wxGridTableBase::~wxGridTableBase()
2834 delete m_attrProvider
;
2837 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2839 delete m_attrProvider
;
2840 m_attrProvider
= attrProvider
;
2843 bool wxGridTableBase::CanHaveAttributes()
2845 if ( ! GetAttrProvider() )
2847 // use the default attr provider by default
2848 SetAttrProvider(new wxGridCellAttrProvider
);
2853 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2855 if ( m_attrProvider
)
2856 return m_attrProvider
->GetAttr(row
, col
, kind
);
2858 return (wxGridCellAttr
*)NULL
;
2861 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2863 if ( m_attrProvider
)
2865 attr
->SetKind(wxGridCellAttr::Cell
);
2866 m_attrProvider
->SetAttr(attr
, row
, col
);
2870 // as we take ownership of the pointer and don't store it, we must
2876 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2878 if ( m_attrProvider
)
2880 attr
->SetKind(wxGridCellAttr::Row
);
2881 m_attrProvider
->SetRowAttr(attr
, row
);
2885 // as we take ownership of the pointer and don't store it, we must
2891 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2893 if ( m_attrProvider
)
2895 attr
->SetKind(wxGridCellAttr::Col
);
2896 m_attrProvider
->SetColAttr(attr
, col
);
2900 // as we take ownership of the pointer and don't store it, we must
2906 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2907 size_t WXUNUSED(numRows
) )
2909 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2914 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2916 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2921 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2922 size_t WXUNUSED(numRows
) )
2924 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2929 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2930 size_t WXUNUSED(numCols
) )
2932 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2937 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2939 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2944 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2945 size_t WXUNUSED(numCols
) )
2947 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2953 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2956 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2957 // how much it makes sense to us geeks.
2961 wxString
wxGridTableBase::GetColLabelValue( int col
)
2963 // default col labels are:
2964 // cols 0 to 25 : A-Z
2965 // cols 26 to 675 : AA-ZZ
2970 for ( n
= 1; ; n
++ )
2972 s
+= (_T('A') + (wxChar
)( col%26
));
2974 if ( col
< 0 ) break;
2977 // reverse the string...
2979 for ( i
= 0; i
< n
; i
++ )
2988 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2990 return wxGRID_VALUE_STRING
;
2993 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2994 const wxString
& typeName
)
2996 return typeName
== wxGRID_VALUE_STRING
;
2999 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3001 return CanGetValueAs(row
, col
, typeName
);
3004 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3009 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3014 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3019 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3020 long WXUNUSED(value
) )
3024 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3025 double WXUNUSED(value
) )
3029 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3030 bool WXUNUSED(value
) )
3035 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3036 const wxString
& WXUNUSED(typeName
) )
3041 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3042 const wxString
& WXUNUSED(typeName
),
3043 void* WXUNUSED(value
) )
3047 //////////////////////////////////////////////////////////////////////
3049 // Message class for the grid table to send requests and notifications
3053 wxGridTableMessage::wxGridTableMessage()
3055 m_table
= (wxGridTableBase
*) NULL
;
3061 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3062 int commandInt1
, int commandInt2
)
3066 m_comInt1
= commandInt1
;
3067 m_comInt2
= commandInt2
;
3072 //////////////////////////////////////////////////////////////////////
3074 // A basic grid table for string data. An object of this class will
3075 // created by wxGrid if you don't specify an alternative table class.
3078 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3080 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3082 wxGridStringTable::wxGridStringTable()
3087 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3090 m_data
.Alloc( numRows
);
3093 sa
.Alloc( numCols
);
3094 sa
.Add( wxEmptyString
, numCols
);
3096 m_data
.Add( sa
, numRows
);
3099 wxGridStringTable::~wxGridStringTable()
3103 int wxGridStringTable::GetNumberRows()
3105 return m_data
.GetCount();
3108 int wxGridStringTable::GetNumberCols()
3110 if ( m_data
.GetCount() > 0 )
3111 return m_data
[0].GetCount();
3116 wxString
wxGridStringTable::GetValue( int row
, int col
)
3118 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3119 _T("invalid row or column index in wxGridStringTable") );
3121 return m_data
[row
][col
];
3124 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3126 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3127 _T("invalid row or column index in wxGridStringTable") );
3129 m_data
[row
][col
] = value
;
3132 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3134 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3135 _T("invalid row or column index in wxGridStringTable") );
3137 return (m_data
[row
][col
] == wxEmptyString
);
3140 void wxGridStringTable::Clear()
3143 int numRows
, numCols
;
3145 numRows
= m_data
.GetCount();
3148 numCols
= m_data
[0].GetCount();
3150 for ( row
= 0; row
< numRows
; row
++ )
3152 for ( col
= 0; col
< numCols
; col
++ )
3154 m_data
[row
][col
] = wxEmptyString
;
3161 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3163 size_t curNumRows
= m_data
.GetCount();
3164 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3165 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3167 if ( pos
>= curNumRows
)
3169 return AppendRows( numRows
);
3173 sa
.Alloc( curNumCols
);
3174 sa
.Add( wxEmptyString
, curNumCols
);
3175 m_data
.Insert( sa
, pos
, numRows
);
3178 wxGridTableMessage
msg( this,
3179 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3183 GetView()->ProcessTableMessage( msg
);
3189 bool wxGridStringTable::AppendRows( size_t numRows
)
3191 size_t curNumRows
= m_data
.GetCount();
3192 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3193 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3196 if ( curNumCols
> 0 )
3198 sa
.Alloc( curNumCols
);
3199 sa
.Add( wxEmptyString
, curNumCols
);
3202 m_data
.Add( sa
, numRows
);
3206 wxGridTableMessage
msg( this,
3207 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3210 GetView()->ProcessTableMessage( msg
);
3216 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3218 size_t curNumRows
= m_data
.GetCount();
3220 if ( pos
>= curNumRows
)
3222 wxFAIL_MSG( wxString::Format
3224 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3226 (unsigned long)numRows
,
3227 (unsigned long)curNumRows
3233 if ( numRows
> curNumRows
- pos
)
3235 numRows
= curNumRows
- pos
;
3238 if ( numRows
>= curNumRows
)
3244 m_data
.RemoveAt( pos
, numRows
);
3248 wxGridTableMessage
msg( this,
3249 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3253 GetView()->ProcessTableMessage( msg
);
3259 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3263 size_t curNumRows
= m_data
.GetCount();
3264 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3265 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3267 if ( pos
>= curNumCols
)
3269 return AppendCols( numCols
);
3272 for ( row
= 0; row
< curNumRows
; row
++ )
3274 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3276 m_data
[row
].Insert( wxEmptyString
, col
);
3281 wxGridTableMessage
msg( this,
3282 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3286 GetView()->ProcessTableMessage( msg
);
3292 bool wxGridStringTable::AppendCols( size_t numCols
)
3296 size_t curNumRows
= m_data
.GetCount();
3300 // TODO: something better than this ?
3302 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3307 for ( row
= 0; row
< curNumRows
; row
++ )
3309 m_data
[row
].Add( wxEmptyString
, numCols
);
3314 wxGridTableMessage
msg( this,
3315 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3318 GetView()->ProcessTableMessage( msg
);
3324 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3328 size_t curNumRows
= m_data
.GetCount();
3329 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3330 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3332 if ( pos
>= curNumCols
)
3334 wxFAIL_MSG( wxString::Format
3336 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3338 (unsigned long)numCols
,
3339 (unsigned long)curNumCols
3344 if ( numCols
> curNumCols
- pos
)
3346 numCols
= curNumCols
- pos
;
3349 for ( row
= 0; row
< curNumRows
; row
++ )
3351 if ( numCols
>= curNumCols
)
3353 m_data
[row
].Clear();
3357 m_data
[row
].RemoveAt( pos
, numCols
);
3362 wxGridTableMessage
msg( this,
3363 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3367 GetView()->ProcessTableMessage( msg
);
3373 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3375 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3377 // using default label
3379 return wxGridTableBase::GetRowLabelValue( row
);
3383 return m_rowLabels
[ row
];
3387 wxString
wxGridStringTable::GetColLabelValue( int col
)
3389 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3391 // using default label
3393 return wxGridTableBase::GetColLabelValue( col
);
3397 return m_colLabels
[ col
];
3401 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3403 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3405 int n
= m_rowLabels
.GetCount();
3407 for ( i
= n
; i
<= row
; i
++ )
3409 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3413 m_rowLabels
[row
] = value
;
3416 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3418 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3420 int n
= m_colLabels
.GetCount();
3422 for ( i
= n
; i
<= col
; i
++ )
3424 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3428 m_colLabels
[col
] = value
;
3433 //////////////////////////////////////////////////////////////////////
3434 //////////////////////////////////////////////////////////////////////
3436 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3438 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3439 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3440 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3441 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3442 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3443 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3446 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3448 const wxPoint
&pos
, const wxSize
&size
)
3449 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3454 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3458 // NO - don't do this because it will set both the x and y origin
3459 // coords to match the parent scrolled window and we just want to
3460 // set the y coord - MB
3462 // m_owner->PrepareDC( dc );
3465 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3466 dc
.SetDeviceOrigin( 0, -y
);
3468 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3469 m_owner
->DrawRowLabels( dc
, rows
);
3473 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3475 m_owner
->ProcessRowLabelMouseEvent( event
);
3479 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3481 m_owner
->GetEventHandler()->ProcessEvent(event
);
3485 // This seems to be required for wxMotif otherwise the mouse
3486 // cursor must be in the cell edit control to get key events
3488 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3490 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3493 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3495 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3500 //////////////////////////////////////////////////////////////////////
3502 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3504 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3505 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3506 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3507 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3508 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3509 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3512 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3514 const wxPoint
&pos
, const wxSize
&size
)
3515 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3520 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3524 // NO - don't do this because it will set both the x and y origin
3525 // coords to match the parent scrolled window and we just want to
3526 // set the x coord - MB
3528 // m_owner->PrepareDC( dc );
3531 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3532 dc
.SetDeviceOrigin( -x
, 0 );
3534 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3535 m_owner
->DrawColLabels( dc
, cols
);
3539 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3541 m_owner
->ProcessColLabelMouseEvent( event
);
3544 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3546 m_owner
->GetEventHandler()->ProcessEvent(event
);
3550 // This seems to be required for wxMotif otherwise the mouse
3551 // cursor must be in the cell edit control to get key events
3553 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3555 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3558 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3560 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3565 //////////////////////////////////////////////////////////////////////
3567 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3569 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3570 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3571 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3572 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3573 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3574 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3577 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3579 const wxPoint
&pos
, const wxSize
&size
)
3580 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3585 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3589 int client_height
= 0;
3590 int client_width
= 0;
3591 GetClientSize( &client_width
, &client_height
);
3593 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3594 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3595 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3596 dc
.DrawLine( 0, 0, client_width
, 0 );
3597 dc
.DrawLine( 0, 0, 0, client_height
);
3599 dc
.SetPen( *wxWHITE_PEN
);
3600 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3601 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3605 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3607 m_owner
->ProcessCornerLabelMouseEvent( event
);
3611 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3613 m_owner
->GetEventHandler()->ProcessEvent(event
);
3616 // This seems to be required for wxMotif otherwise the mouse
3617 // cursor must be in the cell edit control to get key events
3619 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3621 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3624 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3626 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3631 //////////////////////////////////////////////////////////////////////
3633 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3635 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3636 EVT_PAINT( wxGridWindow::OnPaint
)
3637 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3638 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3639 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3640 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3641 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3644 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3645 wxGridRowLabelWindow
*rowLblWin
,
3646 wxGridColLabelWindow
*colLblWin
,
3649 const wxSize
&size
)
3650 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3651 wxT("grid window") )
3655 m_rowLabelWin
= rowLblWin
;
3656 m_colLabelWin
= colLblWin
;
3657 SetBackgroundColour(_T("WHITE"));
3661 wxGridWindow::~wxGridWindow()
3666 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3668 wxPaintDC
dc( this );
3669 m_owner
->PrepareDC( dc
);
3670 wxRegion reg
= GetUpdateRegion();
3671 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3672 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3673 #if WXGRID_DRAW_LINES
3674 m_owner
->DrawAllGridLines( dc
, reg
);
3676 m_owner
->DrawGridSpace( dc
);
3677 m_owner
->DrawHighlight( dc
, DirtyCells
);
3681 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3683 wxWindow::ScrollWindow( dx
, dy
, rect
);
3684 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3685 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3689 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3691 m_owner
->ProcessGridCellMouseEvent( event
);
3694 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3696 m_owner
->GetEventHandler()->ProcessEvent(event
);
3699 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3700 // cursor must be in the cell edit control to get key events
3702 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3704 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3707 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3709 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3712 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3717 //////////////////////////////////////////////////////////////////////
3719 // Internal Helper function for computing row or column from some
3720 // (unscrolled) coordinate value, using either
3721 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3722 // of m_rowBottoms/m_ColRights to speed up the search!
3724 // Internal helper macros for simpler use of that function
3726 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3727 const wxArrayInt
& BorderArray
, int nMax
,
3728 bool maxOnOverflow
);
3730 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3731 WXGRID_MIN_COL_WIDTH, \
3732 m_colRights, m_numCols, TRUE)
3733 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3734 WXGRID_MIN_ROW_HEIGHT, \
3735 m_rowBottoms, m_numRows, TRUE)
3736 /////////////////////////////////////////////////////////////////////
3738 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3740 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3741 EVT_PAINT( wxGrid::OnPaint
)
3742 EVT_SIZE( wxGrid::OnSize
)
3743 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3744 EVT_KEY_UP( wxGrid::OnKeyUp
)
3745 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3748 wxGrid::wxGrid( wxWindow
*parent
,
3753 const wxString
& name
)
3754 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3755 m_colMinWidths(GRID_HASH_SIZE
),
3756 m_rowMinHeights(GRID_HASH_SIZE
)
3764 // Must do this or ~wxScrollHelper will pop the wrong event handler
3765 SetTargetWindow(this);
3767 wxSafeDecRef(m_defaultCellAttr
);
3769 #ifdef DEBUG_ATTR_CACHE
3770 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3771 wxPrintf(_T("wxGrid attribute cache statistics: "
3772 "total: %u, hits: %u (%u%%)\n"),
3773 total
, gs_nAttrCacheHits
,
3774 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3780 delete m_typeRegistry
;
3786 // ----- internal init and update functions
3789 void wxGrid::Create()
3791 m_created
= FALSE
; // set to TRUE by CreateGrid
3793 m_table
= (wxGridTableBase
*) NULL
;
3796 m_cellEditCtrlEnabled
= FALSE
;
3798 m_defaultCellAttr
= new wxGridCellAttr();
3800 // Set default cell attributes
3801 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3802 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3803 m_defaultCellAttr
->SetFont(GetFont());
3804 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3805 m_defaultCellAttr
->SetTextColour(
3806 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3807 m_defaultCellAttr
->SetBackgroundColour(
3808 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3809 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3810 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3815 m_currentCellCoords
= wxGridNoCellCoords
;
3817 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3818 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3820 // create the type registry
3821 m_typeRegistry
= new wxGridTypeRegistry
;
3824 // subwindow components that make up the wxGrid
3825 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3830 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3835 m_colLabelWin
= new wxGridColLabelWindow( this,
3840 m_gridWin
= new wxGridWindow( this,
3847 SetTargetWindow( m_gridWin
);
3853 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3854 wxGrid::wxGridSelectionModes selmode
)
3856 wxCHECK_MSG( !m_created
,
3858 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3860 m_numRows
= numRows
;
3861 m_numCols
= numCols
;
3863 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3864 m_table
->SetView( this );
3866 m_selection
= new wxGridSelection( this, selmode
);
3875 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3877 wxCHECK_RET( m_created
,
3878 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3880 m_selection
->SetSelectionMode( selmode
);
3883 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3885 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3886 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3888 return m_selection
->GetSelectionMode();
3891 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3892 wxGrid::wxGridSelectionModes selmode
)
3896 // RD: Actually, this should probably be allowed. I think it would be
3897 // nice to be able to switch multiple Tables in and out of a single
3898 // View at runtime. Is there anything in the implementation that
3899 // would prevent this?
3901 // At least, you now have to cope with m_selection
3902 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3907 m_numRows
= table
->GetNumberRows();
3908 m_numCols
= table
->GetNumberCols();
3911 m_table
->SetView( this );
3914 m_selection
= new wxGridSelection( this, selmode
);
3927 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3928 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3930 if ( m_rowLabelWin
)
3932 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3936 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3939 m_labelTextColour
= wxColour( _T("BLACK") );
3942 m_attrCache
.row
= -1;
3943 m_attrCache
.col
= -1;
3944 m_attrCache
.attr
= NULL
;
3946 // TODO: something better than this ?
3948 m_labelFont
= this->GetFont();
3949 // m_labelFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
3950 // m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
3952 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
3953 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3955 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3956 m_colLabelVertAlign
= wxALIGN_CENTRE
;
3958 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3959 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3961 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3962 m_defaultRowHeight
+= 8;
3964 m_defaultRowHeight
+= 4;
3967 m_gridLineColour
= wxColour( 192,192,192 );
3968 m_gridLinesEnabled
= TRUE
;
3969 m_cellHighlightColour
= *wxBLACK
;
3970 m_cellHighlightPenWidth
= 2;
3971 m_cellHighlightROPenWidth
= 1;
3973 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3974 m_winCapture
= (wxWindow
*)NULL
;
3975 m_canDragRowSize
= TRUE
;
3976 m_canDragColSize
= TRUE
;
3977 m_canDragGridSize
= TRUE
;
3979 m_dragRowOrCol
= -1;
3980 m_isDragging
= FALSE
;
3981 m_startDragPos
= wxDefaultPosition
;
3983 m_waitForSlowClick
= FALSE
;
3985 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3986 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3988 m_currentCellCoords
= wxGridNoCellCoords
;
3990 m_selectingTopLeft
= wxGridNoCellCoords
;
3991 m_selectingBottomRight
= wxGridNoCellCoords
;
3992 // m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
3993 // m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
3994 m_selectionBackground
= *wxBLACK
;
3995 m_selectionForeground
= *wxWHITE
;
3997 m_editable
= TRUE
; // default for whole grid
3999 m_inOnKeyDown
= FALSE
;
4006 // ----------------------------------------------------------------------------
4007 // the idea is to call these functions only when necessary because they create
4008 // quite big arrays which eat memory mostly unnecessary - in particular, if
4009 // default widths/heights are used for all rows/columns, we may not use these
4012 // with some extra code, it should be possible to only store the
4013 // widths/heights different from default ones but this will be done later...
4014 // ----------------------------------------------------------------------------
4016 void wxGrid::InitRowHeights()
4018 m_rowHeights
.Empty();
4019 m_rowBottoms
.Empty();
4021 m_rowHeights
.Alloc( m_numRows
);
4022 m_rowBottoms
.Alloc( m_numRows
);
4026 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4028 for ( int i
= 0; i
< m_numRows
; i
++ )
4030 rowBottom
+= m_defaultRowHeight
;
4031 m_rowBottoms
.Add( rowBottom
);
4035 void wxGrid::InitColWidths()
4037 m_colWidths
.Empty();
4038 m_colRights
.Empty();
4040 m_colWidths
.Alloc( m_numCols
);
4041 m_colRights
.Alloc( m_numCols
);
4044 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4046 for ( int i
= 0; i
< m_numCols
; i
++ )
4048 colRight
+= m_defaultColWidth
;
4049 m_colRights
.Add( colRight
);
4053 int wxGrid::GetColWidth(int col
) const
4055 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4058 int wxGrid::GetColLeft(int col
) const
4060 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4061 : m_colRights
[col
] - m_colWidths
[col
];
4064 int wxGrid::GetColRight(int col
) const
4066 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4070 int wxGrid::GetRowHeight(int row
) const
4072 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4075 int wxGrid::GetRowTop(int row
) const
4077 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4078 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4081 int wxGrid::GetRowBottom(int row
) const
4083 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4084 : m_rowBottoms
[row
];
4087 void wxGrid::CalcDimensions()
4090 GetClientSize( &cw
, &ch
);
4092 if ( m_rowLabelWin
->IsShown() )
4093 cw
-= m_rowLabelWidth
;
4094 if ( m_colLabelWin
->IsShown() )
4095 ch
-= m_colLabelHeight
;
4098 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4099 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4101 // take into account editor if shown
4102 if( IsCellEditControlShown() )
4105 int r
= m_currentCellCoords
.GetRow();
4106 int c
= m_currentCellCoords
.GetCol();
4107 int x
= GetColLeft(c
);
4108 int y
= GetRowTop(r
);
4110 // how big is the editor
4111 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4112 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4113 editor
->GetControl()->GetSize(&w2
, &h2
);
4116 if( w2
> w
) w
= w2
;
4117 if( h2
> h
) h
= h2
;
4122 // preserve (more or less) the previous position
4124 GetViewStart( &x
, &y
);
4126 // maybe we don't need scrollbars at all?
4128 // also adjust the position to be valid for the new scroll rangs
4149 // do set scrollbar parameters
4150 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4151 GetScrollX(w
), GetScrollY(h
), x
, y
,
4152 GetBatchCount() != 0);
4154 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4155 // still must reposition the children
4160 void wxGrid::CalcWindowSizes()
4163 GetClientSize( &cw
, &ch
);
4165 if ( m_cornerLabelWin
->IsShown() )
4166 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4168 if ( m_colLabelWin
->IsShown() )
4169 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4171 if ( m_rowLabelWin
->IsShown() )
4172 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4174 if ( m_gridWin
->IsShown() )
4175 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4179 // this is called when the grid table sends a message to say that it
4180 // has been redimensioned
4182 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4185 bool result
= FALSE
;
4187 // Clear the attribute cache as the attribute might refer to a different
4188 // cell than stored in the cache after adding/removing rows/columns.
4190 // By the same reasoning, the editor should be dismissed if columns are
4191 // added or removed. And for consistency, it should IMHO always be
4192 // removed, not only if the cell "underneath" it actually changes.
4193 // For now, I intentionally do not save the editor's content as the
4194 // cell it might want to save that stuff to might no longer exist.
4195 HideCellEditControl();
4197 // if we were using the default widths/heights so far, we must change them
4199 if ( m_colWidths
.IsEmpty() )
4204 if ( m_rowHeights
.IsEmpty() )
4210 switch ( msg
.GetId() )
4212 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4214 size_t pos
= msg
.GetCommandInt();
4215 int numRows
= msg
.GetCommandInt2();
4217 m_numRows
+= numRows
;
4219 if ( !m_rowHeights
.IsEmpty() )
4221 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4222 m_rowBottoms
.Insert( 0, pos
, numRows
);
4225 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4227 for ( i
= pos
; i
< m_numRows
; i
++ )
4229 bottom
+= m_rowHeights
[i
];
4230 m_rowBottoms
[i
] = bottom
;
4233 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4235 // if we have just inserted cols into an empty grid the current
4236 // cell will be undefined...
4238 SetCurrentCell( 0, 0 );
4242 m_selection
->UpdateRows( pos
, numRows
);
4243 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4245 attrProvider
->UpdateAttrRows( pos
, numRows
);
4247 if ( !GetBatchCount() )
4250 m_rowLabelWin
->Refresh();
4256 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4258 int numRows
= msg
.GetCommandInt();
4259 int oldNumRows
= m_numRows
;
4260 m_numRows
+= numRows
;
4262 if ( !m_rowHeights
.IsEmpty() )
4264 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4265 m_rowBottoms
.Add( 0, numRows
);
4268 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4270 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4272 bottom
+= m_rowHeights
[i
];
4273 m_rowBottoms
[i
] = bottom
;
4276 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4278 // if we have just inserted cols into an empty grid the current
4279 // cell will be undefined...
4281 SetCurrentCell( 0, 0 );
4283 if ( !GetBatchCount() )
4286 m_rowLabelWin
->Refresh();
4292 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4294 size_t pos
= msg
.GetCommandInt();
4295 int numRows
= msg
.GetCommandInt2();
4296 m_numRows
-= numRows
;
4298 if ( !m_rowHeights
.IsEmpty() )
4300 m_rowHeights
.RemoveAt( pos
, numRows
);
4301 m_rowBottoms
.RemoveAt( pos
, numRows
);
4304 for ( i
= 0; i
< m_numRows
; i
++ )
4306 h
+= m_rowHeights
[i
];
4307 m_rowBottoms
[i
] = h
;
4312 m_currentCellCoords
= wxGridNoCellCoords
;
4316 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4317 m_currentCellCoords
.Set( 0, 0 );
4321 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4322 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4324 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4325 // ifdef'd out following patch from Paul Gammans
4327 // No need to touch column attributes, unless we
4328 // removed _all_ rows, in this case, we remove
4329 // all column attributes.
4330 // I hate to do this here, but the
4331 // needed data is not available inside UpdateAttrRows.
4332 if ( !GetNumberRows() )
4333 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4336 if ( !GetBatchCount() )
4339 m_rowLabelWin
->Refresh();
4345 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4347 size_t pos
= msg
.GetCommandInt();
4348 int numCols
= msg
.GetCommandInt2();
4349 m_numCols
+= numCols
;
4351 if ( !m_colWidths
.IsEmpty() )
4353 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4354 m_colRights
.Insert( 0, pos
, numCols
);
4357 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4359 for ( i
= pos
; i
< m_numCols
; i
++ )
4361 right
+= m_colWidths
[i
];
4362 m_colRights
[i
] = right
;
4365 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4367 // if we have just inserted cols into an empty grid the current
4368 // cell will be undefined...
4370 SetCurrentCell( 0, 0 );
4374 m_selection
->UpdateCols( pos
, numCols
);
4375 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4377 attrProvider
->UpdateAttrCols( pos
, numCols
);
4378 if ( !GetBatchCount() )
4381 m_colLabelWin
->Refresh();
4388 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4390 int numCols
= msg
.GetCommandInt();
4391 int oldNumCols
= m_numCols
;
4392 m_numCols
+= numCols
;
4393 if ( !m_colWidths
.IsEmpty() )
4395 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4396 m_colRights
.Add( 0, numCols
);
4399 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4401 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4403 right
+= m_colWidths
[i
];
4404 m_colRights
[i
] = right
;
4407 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4409 // if we have just inserted cols into an empty grid the current
4410 // cell will be undefined...
4412 SetCurrentCell( 0, 0 );
4414 if ( !GetBatchCount() )
4417 m_colLabelWin
->Refresh();
4423 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4425 size_t pos
= msg
.GetCommandInt();
4426 int numCols
= msg
.GetCommandInt2();
4427 m_numCols
-= numCols
;
4429 if ( !m_colWidths
.IsEmpty() )
4431 m_colWidths
.RemoveAt( pos
, numCols
);
4432 m_colRights
.RemoveAt( pos
, numCols
);
4435 for ( i
= 0; i
< m_numCols
; i
++ )
4437 w
+= m_colWidths
[i
];
4443 m_currentCellCoords
= wxGridNoCellCoords
;
4447 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4448 m_currentCellCoords
.Set( 0, 0 );
4452 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4453 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4455 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4456 // ifdef'd out following patch from Paul Gammans
4458 // No need to touch row attributes, unless we
4459 // removed _all_ columns, in this case, we remove
4460 // all row attributes.
4461 // I hate to do this here, but the
4462 // needed data is not available inside UpdateAttrCols.
4463 if ( !GetNumberCols() )
4464 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4467 if ( !GetBatchCount() )
4470 m_colLabelWin
->Refresh();
4477 if (result
&& !GetBatchCount() )
4478 m_gridWin
->Refresh();
4483 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4485 wxRegionIterator
iter( reg
);
4488 wxArrayInt rowlabels
;
4495 // TODO: remove this when we can...
4496 // There is a bug in wxMotif that gives garbage update
4497 // rectangles if you jump-scroll a long way by clicking the
4498 // scrollbar with middle button. This is a work-around
4500 #if defined(__WXMOTIF__)
4502 m_gridWin
->GetClientSize( &cw
, &ch
);
4503 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4504 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4507 // logical bounds of update region
4510 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4511 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4513 // find the row labels within these bounds
4516 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4518 if ( GetRowBottom(row
) < top
)
4521 if ( GetRowTop(row
) > bottom
)
4524 rowlabels
.Add( row
);
4534 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4536 wxRegionIterator
iter( reg
);
4539 wxArrayInt colLabels
;
4546 // TODO: remove this when we can...
4547 // There is a bug in wxMotif that gives garbage update
4548 // rectangles if you jump-scroll a long way by clicking the
4549 // scrollbar with middle button. This is a work-around
4551 #if defined(__WXMOTIF__)
4553 m_gridWin
->GetClientSize( &cw
, &ch
);
4554 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4555 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4558 // logical bounds of update region
4561 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4562 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4564 // find the cells within these bounds
4567 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4569 if ( GetColRight(col
) < left
)
4572 if ( GetColLeft(col
) > right
)
4575 colLabels
.Add( col
);
4584 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4586 wxRegionIterator
iter( reg
);
4589 wxGridCellCoordsArray cellsExposed
;
4591 int left
, top
, right
, bottom
;
4596 // TODO: remove this when we can...
4597 // There is a bug in wxMotif that gives garbage update
4598 // rectangles if you jump-scroll a long way by clicking the
4599 // scrollbar with middle button. This is a work-around
4601 #if defined(__WXMOTIF__)
4603 m_gridWin
->GetClientSize( &cw
, &ch
);
4604 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4605 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4606 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4607 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4610 // logical bounds of update region
4612 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4613 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4615 // find the cells within these bounds
4618 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4620 if ( GetRowBottom(row
) <= top
)
4623 if ( GetRowTop(row
) > bottom
)
4626 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4628 if ( GetColRight(col
) <= left
)
4631 if ( GetColLeft(col
) > right
)
4634 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4641 return cellsExposed
;
4645 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4648 wxPoint
pos( event
.GetPosition() );
4649 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4651 if ( event
.Dragging() )
4655 m_isDragging
= TRUE
;
4656 m_rowLabelWin
->CaptureMouse();
4659 if ( event
.LeftIsDown() )
4661 switch( m_cursorMode
)
4663 case WXGRID_CURSOR_RESIZE_ROW
:
4665 int cw
, ch
, left
, dummy
;
4666 m_gridWin
->GetClientSize( &cw
, &ch
);
4667 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4669 wxClientDC
dc( m_gridWin
);
4672 GetRowTop(m_dragRowOrCol
) +
4673 GetRowMinimalHeight(m_dragRowOrCol
) );
4674 dc
.SetLogicalFunction(wxINVERT
);
4675 if ( m_dragLastPos
>= 0 )
4677 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4679 dc
.DrawLine( left
, y
, left
+cw
, y
);
4684 case WXGRID_CURSOR_SELECT_ROW
:
4685 if ( (row
= YToRow( y
)) >= 0 )
4689 m_selection
->SelectRow( row
,
4690 event
.ControlDown(),
4697 // default label to suppress warnings about "enumeration value
4698 // 'xxx' not handled in switch
4706 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4711 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4712 m_isDragging
= FALSE
;
4715 // ------------ Entering or leaving the window
4717 if ( event
.Entering() || event
.Leaving() )
4719 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4723 // ------------ Left button pressed
4725 else if ( event
.LeftDown() )
4727 // don't send a label click event for a hit on the
4728 // edge of the row label - this is probably the user
4729 // wanting to resize the row
4731 if ( YToEdgeOfRow(y
) < 0 )
4735 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4737 if ( !event
.ShiftDown() && !event
.ControlDown() )
4741 if ( event
.ShiftDown() )
4743 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4746 GetNumberCols() - 1,
4747 event
.ControlDown(),
4754 m_selection
->SelectRow( row
,
4755 event
.ControlDown(),
4762 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4767 // starting to drag-resize a row
4769 if ( CanDragRowSize() )
4770 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4775 // ------------ Left double click
4777 else if (event
.LeftDClick() )
4779 if ( YToEdgeOfRow(y
) < 0 )
4782 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4787 // ------------ Left button released
4789 else if ( event
.LeftUp() )
4791 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4793 DoEndDragResizeRow();
4795 // Note: we are ending the event *after* doing
4796 // default processing in this case
4798 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4801 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4806 // ------------ Right button down
4808 else if ( event
.RightDown() )
4811 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4813 // no default action at the moment
4818 // ------------ Right double click
4820 else if ( event
.RightDClick() )
4823 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4825 // no default action at the moment
4830 // ------------ No buttons down and mouse moving
4832 else if ( event
.Moving() )
4834 m_dragRowOrCol
= YToEdgeOfRow( y
);
4835 if ( m_dragRowOrCol
>= 0 )
4837 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4839 // don't capture the mouse yet
4840 if ( CanDragRowSize() )
4841 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4844 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4846 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4852 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4855 wxPoint
pos( event
.GetPosition() );
4856 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4858 if ( event
.Dragging() )
4862 m_isDragging
= TRUE
;
4863 m_colLabelWin
->CaptureMouse();
4866 if ( event
.LeftIsDown() )
4868 switch( m_cursorMode
)
4870 case WXGRID_CURSOR_RESIZE_COL
:
4872 int cw
, ch
, dummy
, top
;
4873 m_gridWin
->GetClientSize( &cw
, &ch
);
4874 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4876 wxClientDC
dc( m_gridWin
);
4879 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4880 GetColMinimalWidth(m_dragRowOrCol
));
4881 dc
.SetLogicalFunction(wxINVERT
);
4882 if ( m_dragLastPos
>= 0 )
4884 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4886 dc
.DrawLine( x
, top
, x
, top
+ch
);
4891 case WXGRID_CURSOR_SELECT_COL
:
4892 if ( (col
= XToCol( x
)) >= 0 )
4896 m_selection
->SelectCol( col
,
4897 event
.ControlDown(),
4904 // default label to suppress warnings about "enumeration value
4905 // 'xxx' not handled in switch
4913 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4918 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4919 m_isDragging
= FALSE
;
4922 // ------------ Entering or leaving the window
4924 if ( event
.Entering() || event
.Leaving() )
4926 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4930 // ------------ Left button pressed
4932 else if ( event
.LeftDown() )
4934 // don't send a label click event for a hit on the
4935 // edge of the col label - this is probably the user
4936 // wanting to resize the col
4938 if ( XToEdgeOfCol(x
) < 0 )
4942 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4944 if ( !event
.ShiftDown() && !event
.ControlDown() )
4948 if ( event
.ShiftDown() )
4950 m_selection
->SelectBlock( 0,
4951 m_currentCellCoords
.GetCol(),
4952 GetNumberRows() - 1, col
,
4953 event
.ControlDown(),
4960 m_selection
->SelectCol( col
,
4961 event
.ControlDown(),
4968 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4973 // starting to drag-resize a col
4975 if ( CanDragColSize() )
4976 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4981 // ------------ Left double click
4983 if ( event
.LeftDClick() )
4985 if ( XToEdgeOfCol(x
) < 0 )
4988 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4993 // ------------ Left button released
4995 else if ( event
.LeftUp() )
4997 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4999 DoEndDragResizeCol();
5001 // Note: we are ending the event *after* doing
5002 // default processing in this case
5004 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5007 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5012 // ------------ Right button down
5014 else if ( event
.RightDown() )
5017 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5019 // no default action at the moment
5024 // ------------ Right double click
5026 else if ( event
.RightDClick() )
5029 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5031 // no default action at the moment
5036 // ------------ No buttons down and mouse moving
5038 else if ( event
.Moving() )
5040 m_dragRowOrCol
= XToEdgeOfCol( x
);
5041 if ( m_dragRowOrCol
>= 0 )
5043 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5045 // don't capture the cursor yet
5046 if ( CanDragColSize() )
5047 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
5050 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5052 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
5058 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5060 if ( event
.LeftDown() )
5062 // indicate corner label by having both row and
5065 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5071 else if ( event
.LeftDClick() )
5073 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5076 else if ( event
.RightDown() )
5078 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5080 // no default action at the moment
5084 else if ( event
.RightDClick() )
5086 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5088 // no default action at the moment
5093 void wxGrid::ChangeCursorMode(CursorMode mode
,
5098 static const wxChar
*cursorModes
[] =
5107 wxLogTrace(_T("grid"),
5108 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5109 win
== m_colLabelWin
? _T("colLabelWin")
5110 : win
? _T("rowLabelWin")
5112 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5113 #endif // __WXDEBUG__
5115 if ( mode
== m_cursorMode
&&
5116 win
== m_winCapture
&&
5117 captureMouse
== (m_winCapture
!= NULL
))
5122 // by default use the grid itself
5128 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5129 m_winCapture
= (wxWindow
*)NULL
;
5132 m_cursorMode
= mode
;
5134 switch ( m_cursorMode
)
5136 case WXGRID_CURSOR_RESIZE_ROW
:
5137 win
->SetCursor( m_rowResizeCursor
);
5140 case WXGRID_CURSOR_RESIZE_COL
:
5141 win
->SetCursor( m_colResizeCursor
);
5145 win
->SetCursor( *wxSTANDARD_CURSOR
);
5148 // we need to capture mouse when resizing
5149 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5150 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5152 if ( captureMouse
&& resize
)
5154 win
->CaptureMouse();
5159 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5162 wxPoint
pos( event
.GetPosition() );
5163 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5165 wxGridCellCoords coords
;
5166 XYToCell( x
, y
, coords
);
5168 int cell_rows
, cell_cols
;
5169 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5170 if ((cell_rows
< 0) || (cell_cols
< 0))
5172 coords
.SetRow(coords
.GetRow() + cell_rows
);
5173 coords
.SetCol(coords
.GetCol() + cell_cols
);
5176 if ( event
.Dragging() )
5178 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5180 // Don't start doing anything until the mouse has been drug at
5181 // least 3 pixels in any direction...
5184 if (m_startDragPos
== wxDefaultPosition
)
5186 m_startDragPos
= pos
;
5189 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5193 m_isDragging
= TRUE
;
5194 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5196 // Hide the edit control, so it
5197 // won't interfer with drag-shrinking.
5198 if ( IsCellEditControlShown() )
5200 HideCellEditControl();
5201 SaveEditControlValue();
5204 // Have we captured the mouse yet?
5207 m_winCapture
= m_gridWin
;
5208 m_winCapture
->CaptureMouse();
5211 if ( coords
!= wxGridNoCellCoords
)
5213 if ( event
.ControlDown() )
5215 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5216 m_selectingKeyboard
= coords
;
5217 HighlightBlock ( m_selectingKeyboard
, coords
);
5221 if ( !IsSelection() )
5223 HighlightBlock( coords
, coords
);
5227 HighlightBlock( m_currentCellCoords
, coords
);
5231 if (! IsVisible(coords
))
5233 MakeCellVisible(coords
);
5234 // TODO: need to introduce a delay or something here. The
5235 // scrolling is way to fast, at least on MSW - also on GTK.
5239 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5241 int cw
, ch
, left
, dummy
;
5242 m_gridWin
->GetClientSize( &cw
, &ch
);
5243 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5245 wxClientDC
dc( m_gridWin
);
5247 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5248 GetRowMinimalHeight(m_dragRowOrCol
) );
5249 dc
.SetLogicalFunction(wxINVERT
);
5250 if ( m_dragLastPos
>= 0 )
5252 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5254 dc
.DrawLine( left
, y
, left
+cw
, y
);
5257 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5259 int cw
, ch
, dummy
, top
;
5260 m_gridWin
->GetClientSize( &cw
, &ch
);
5261 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5263 wxClientDC
dc( m_gridWin
);
5265 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5266 GetColMinimalWidth(m_dragRowOrCol
) );
5267 dc
.SetLogicalFunction(wxINVERT
);
5268 if ( m_dragLastPos
>= 0 )
5270 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5272 dc
.DrawLine( x
, top
, x
, top
+ch
);
5279 m_isDragging
= FALSE
;
5280 m_startDragPos
= wxDefaultPosition
;
5282 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5283 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5286 if ( event
.Entering() || event
.Leaving() )
5288 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5289 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5294 // ------------ Left button pressed
5296 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5298 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5303 if ( !event
.ControlDown() )
5305 if ( event
.ShiftDown() )
5309 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5310 m_currentCellCoords
.GetCol(),
5313 event
.ControlDown(),
5319 else if ( XToEdgeOfCol(x
) < 0 &&
5320 YToEdgeOfRow(y
) < 0 )
5322 DisableCellEditControl();
5323 MakeCellVisible( coords
);
5325 if ( event
.ControlDown() )
5329 m_selection
->ToggleCellSelection( coords
.GetRow(),
5331 event
.ControlDown(),
5336 m_selectingTopLeft
= wxGridNoCellCoords
;
5337 m_selectingBottomRight
= wxGridNoCellCoords
;
5338 m_selectingKeyboard
= coords
;
5342 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5343 SetCurrentCell( coords
);
5346 if ( m_selection
->GetSelectionMode() !=
5347 wxGrid::wxGridSelectCells
)
5349 HighlightBlock( coords
, coords
);
5358 // ------------ Left double click
5360 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5362 DisableCellEditControl();
5364 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5366 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5374 // ------------ Left button released
5376 else if ( event
.LeftUp() )
5378 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5380 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5381 m_selectingBottomRight
!= wxGridNoCellCoords
)
5385 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5386 m_winCapture
= NULL
;
5391 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5392 m_selectingTopLeft
.GetCol(),
5393 m_selectingBottomRight
.GetRow(),
5394 m_selectingBottomRight
.GetCol(),
5395 event
.ControlDown(),
5401 m_selectingTopLeft
= wxGridNoCellCoords
;
5402 m_selectingBottomRight
= wxGridNoCellCoords
;
5404 // Show the edit control, if it has been hidden for
5406 ShowCellEditControl();
5410 if( m_waitForSlowClick
&& CanEnableCellControl())
5412 EnableCellEditControl();
5414 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5415 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5416 editor
->StartingClick();
5420 m_waitForSlowClick
= FALSE
;
5424 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5426 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5427 DoEndDragResizeRow();
5429 // Note: we are ending the event *after* doing
5430 // default processing in this case
5432 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5434 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5436 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5437 DoEndDragResizeCol();
5439 // Note: we are ending the event *after* doing
5440 // default processing in this case
5442 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5449 // ------------ Right button down
5451 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5453 DisableCellEditControl();
5454 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5459 // no default action at the moment
5464 // ------------ Right double click
5466 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5468 DisableCellEditControl();
5469 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5474 // no default action at the moment
5478 // ------------ Moving and no button action
5480 else if ( event
.Moving() && !event
.IsButton() )
5482 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5484 // out of grid cell area
5485 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5489 int dragRow
= YToEdgeOfRow( y
);
5490 int dragCol
= XToEdgeOfCol( x
);
5492 // Dragging on the corner of a cell to resize in both
5493 // directions is not implemented yet...
5495 if ( dragRow
>= 0 && dragCol
>= 0 )
5497 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5503 m_dragRowOrCol
= dragRow
;
5505 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5507 if ( CanDragRowSize() && CanDragGridSize() )
5508 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5513 m_dragRowOrCol
= dragCol
;
5521 m_dragRowOrCol
= dragCol
;
5523 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5525 if ( CanDragColSize() && CanDragGridSize() )
5526 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5532 // Neither on a row or col edge
5534 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5536 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5542 void wxGrid::DoEndDragResizeRow()
5544 if ( m_dragLastPos
>= 0 )
5546 // erase the last line and resize the row
5548 int cw
, ch
, left
, dummy
;
5549 m_gridWin
->GetClientSize( &cw
, &ch
);
5550 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5552 wxClientDC
dc( m_gridWin
);
5554 dc
.SetLogicalFunction( wxINVERT
);
5555 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5556 HideCellEditControl();
5557 SaveEditControlValue();
5559 int rowTop
= GetRowTop(m_dragRowOrCol
);
5560 SetRowSize( m_dragRowOrCol
,
5561 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5563 if ( !GetBatchCount() )
5565 // Only needed to get the correct rect.y:
5566 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5568 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5569 rect
.width
= m_rowLabelWidth
;
5570 rect
.height
= ch
- rect
.y
;
5571 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5573 // if there is a multicell block, paint all of it
5576 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5577 int leftCol
= XToCol(left
);
5578 int rightCol
= XToCol(left
+cw
);
5581 if (rightCol
< 0) rightCol
= m_numCols
;
5582 for (i
=leftCol
; i
<rightCol
; i
++)
5584 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5585 if (cell_rows
< subtract_rows
)
5586 subtract_rows
= cell_rows
;
5588 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5589 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5590 rect
.height
= ch
- rect
.y
;
5593 m_gridWin
->Refresh( FALSE
, &rect
);
5596 ShowCellEditControl();
5601 void wxGrid::DoEndDragResizeCol()
5603 if ( m_dragLastPos
>= 0 )
5605 // erase the last line and resize the col
5607 int cw
, ch
, dummy
, top
;
5608 m_gridWin
->GetClientSize( &cw
, &ch
);
5609 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5611 wxClientDC
dc( m_gridWin
);
5613 dc
.SetLogicalFunction( wxINVERT
);
5614 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5615 HideCellEditControl();
5616 SaveEditControlValue();
5618 int colLeft
= GetColLeft(m_dragRowOrCol
);
5619 SetColSize( m_dragRowOrCol
,
5620 wxMax( m_dragLastPos
- colLeft
,
5621 GetColMinimalWidth(m_dragRowOrCol
) ) );
5623 if ( !GetBatchCount() )
5625 // Only needed to get the correct rect.x:
5626 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5628 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5629 rect
.width
= cw
- rect
.x
;
5630 rect
.height
= m_colLabelHeight
;
5631 m_colLabelWin
->Refresh( TRUE
, &rect
);
5633 // if there is a multicell block, paint all of it
5636 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5637 int topRow
= YToRow(top
);
5638 int bottomRow
= YToRow(top
+cw
);
5641 if (bottomRow
< 0) bottomRow
= m_numRows
;
5642 for (i
=topRow
; i
<bottomRow
; i
++)
5644 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5645 if (cell_cols
< subtract_cols
)
5646 subtract_cols
= cell_cols
;
5648 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5649 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5650 rect
.width
= cw
- rect
.x
;
5653 m_gridWin
->Refresh( FALSE
, &rect
);
5656 ShowCellEditControl();
5663 // ------ interaction with data model
5665 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5667 switch ( msg
.GetId() )
5669 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5670 return GetModelValues();
5672 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5673 return SetModelValues();
5675 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5676 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5677 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5678 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5679 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5680 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5681 return Redimension( msg
);
5690 // The behaviour of this function depends on the grid table class
5691 // Clear() function. For the default wxGridStringTable class the
5692 // behavious is to replace all cell contents with wxEmptyString but
5693 // not to change the number of rows or cols.
5695 void wxGrid::ClearGrid()
5699 if (IsCellEditControlEnabled())
5700 DisableCellEditControl();
5703 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5708 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5710 // TODO: something with updateLabels flag
5714 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5720 if (IsCellEditControlEnabled())
5721 DisableCellEditControl();
5723 return m_table
->InsertRows( pos
, numRows
);
5725 // the table will have sent the results of the insert row
5726 // operation to this view object as a grid table message
5732 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5734 // TODO: something with updateLabels flag
5738 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5742 return ( m_table
&& m_table
->AppendRows( numRows
) );
5743 // the table will have sent the results of the append row
5744 // operation to this view object as a grid table message
5748 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5750 // TODO: something with updateLabels flag
5754 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5760 if (IsCellEditControlEnabled())
5761 DisableCellEditControl();
5763 return (m_table
->DeleteRows( pos
, numRows
));
5764 // the table will have sent the results of the delete row
5765 // operation to this view object as a grid table message
5771 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5773 // TODO: something with updateLabels flag
5777 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5783 if (IsCellEditControlEnabled())
5784 DisableCellEditControl();
5786 return m_table
->InsertCols( pos
, numCols
);
5787 // the table will have sent the results of the insert col
5788 // operation to this view object as a grid table message
5794 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5796 // TODO: something with updateLabels flag
5800 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5804 return ( m_table
&& m_table
->AppendCols( numCols
) );
5805 // the table will have sent the results of the append col
5806 // operation to this view object as a grid table message
5810 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5812 // TODO: something with updateLabels flag
5816 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5822 if (IsCellEditControlEnabled())
5823 DisableCellEditControl();
5825 return ( m_table
->DeleteCols( pos
, numCols
) );
5826 // the table will have sent the results of the delete col
5827 // operation to this view object as a grid table message
5835 // ----- event handlers
5838 // Generate a grid event based on a mouse event and
5839 // return the result of ProcessEvent()
5841 int wxGrid::SendEvent( const wxEventType type
,
5843 wxMouseEvent
& mouseEv
)
5848 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5850 int rowOrCol
= (row
== -1 ? col
: row
);
5852 wxGridSizeEvent
gridEvt( GetId(),
5856 mouseEv
.GetX() + GetRowLabelSize(),
5857 mouseEv
.GetY() + GetColLabelSize(),
5858 mouseEv
.ControlDown(),
5859 mouseEv
.ShiftDown(),
5861 mouseEv
.MetaDown() );
5863 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5864 vetoed
= !gridEvt
.IsAllowed();
5866 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5868 // Right now, it should _never_ end up here!
5869 wxGridRangeSelectEvent
gridEvt( GetId(),
5873 m_selectingBottomRight
,
5875 mouseEv
.ControlDown(),
5876 mouseEv
.ShiftDown(),
5878 mouseEv
.MetaDown() );
5880 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5881 vetoed
= !gridEvt
.IsAllowed();
5885 wxGridEvent
gridEvt( GetId(),
5889 mouseEv
.GetX() + GetRowLabelSize(),
5890 mouseEv
.GetY() + GetColLabelSize(),
5892 mouseEv
.ControlDown(),
5893 mouseEv
.ShiftDown(),
5895 mouseEv
.MetaDown() );
5896 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5897 vetoed
= !gridEvt
.IsAllowed();
5900 // A Veto'd event may not be `claimed' so test this first
5901 if (vetoed
) return -1;
5902 return claimed
? 1 : 0;
5906 // Generate a grid event of specified type and return the result
5907 // of ProcessEvent().
5909 int wxGrid::SendEvent( const wxEventType type
,
5915 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5917 int rowOrCol
= (row
== -1 ? col
: row
);
5919 wxGridSizeEvent
gridEvt( GetId(),
5924 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5925 vetoed
= !gridEvt
.IsAllowed();
5929 wxGridEvent
gridEvt( GetId(),
5934 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5935 vetoed
= !gridEvt
.IsAllowed();
5938 // A Veto'd event may not be `claimed' so test this first
5939 if (vetoed
) return -1;
5940 return claimed
? 1 : 0;
5944 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5946 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5949 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
5951 // Don't do anything if between Begin/EndBatch...
5952 // EndBatch() will do all this on the last nested one anyway.
5953 if (! GetBatchCount())
5955 // Refresh to get correct scrolled position:
5956 wxScrolledWindow::Refresh(eraseb
,rect
);
5960 int rect_x
, rect_y
, rectWidth
, rectHeight
;
5961 int width_label
, width_cell
, height_label
, height_cell
;
5964 //Copy rectangle can get scroll offsets..
5965 rect_x
= rect
->GetX();
5966 rect_y
= rect
->GetY();
5967 rectWidth
= rect
->GetWidth();
5968 rectHeight
= rect
->GetHeight();
5970 width_label
= m_rowLabelWidth
- rect_x
;
5971 if (width_label
> rectWidth
) width_label
= rectWidth
;
5973 height_label
= m_colLabelHeight
- rect_y
;
5974 if (height_label
> rectHeight
) height_label
= rectHeight
;
5976 if (rect_x
> m_rowLabelWidth
)
5978 x
= rect_x
- m_rowLabelWidth
;
5979 width_cell
= rectWidth
;
5984 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
5987 if (rect_y
> m_colLabelHeight
)
5989 y
= rect_y
- m_colLabelHeight
;
5990 height_cell
= rectHeight
;
5995 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
5998 // Paint corner label part intersecting rect.
5999 if ( width_label
> 0 && height_label
> 0 )
6001 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6002 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6005 // Paint col labels part intersecting rect.
6006 if ( width_cell
> 0 && height_label
> 0 )
6008 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6009 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6012 // Paint row labels part intersecting rect.
6013 if ( width_label
> 0 && height_cell
> 0 )
6015 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6016 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6019 // Paint cell area part intersecting rect.
6020 if ( width_cell
> 0 && height_cell
> 0 )
6022 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6023 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6028 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6029 m_colLabelWin
->Refresh(eraseb
, NULL
);
6030 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6031 m_gridWin
->Refresh(eraseb
, NULL
);
6036 void wxGrid::OnSize( wxSizeEvent
& event
)
6038 // position the child windows
6041 // don't call CalcDimensions() from here, the base class handles the size
6047 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6049 if ( m_inOnKeyDown
)
6051 // shouldn't be here - we are going round in circles...
6053 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6056 m_inOnKeyDown
= TRUE
;
6058 // propagate the event up and see if it gets processed
6060 wxWindow
*parent
= GetParent();
6061 wxKeyEvent
keyEvt( event
);
6062 keyEvt
.SetEventObject( parent
);
6064 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6067 // try local handlers
6069 switch ( event
.KeyCode() )
6072 if ( event
.ControlDown() )
6074 MoveCursorUpBlock( event
.ShiftDown() );
6078 MoveCursorUp( event
.ShiftDown() );
6083 if ( event
.ControlDown() )
6085 MoveCursorDownBlock( event
.ShiftDown() );
6089 MoveCursorDown( event
.ShiftDown() );
6094 if ( event
.ControlDown() )
6096 MoveCursorLeftBlock( event
.ShiftDown() );
6100 MoveCursorLeft( event
.ShiftDown() );
6105 if ( event
.ControlDown() )
6107 MoveCursorRightBlock( event
.ShiftDown() );
6111 MoveCursorRight( event
.ShiftDown() );
6116 case WXK_NUMPAD_ENTER
:
6117 if ( event
.ControlDown() )
6119 event
.Skip(); // to let the edit control have the return
6123 if ( GetGridCursorRow() < GetNumberRows()-1 )
6125 MoveCursorDown( event
.ShiftDown() );
6129 // at the bottom of a column
6130 HideCellEditControl();
6131 SaveEditControlValue();
6141 if (event
.ShiftDown())
6143 if ( GetGridCursorCol() > 0 )
6145 MoveCursorLeft( FALSE
);
6150 HideCellEditControl();
6151 SaveEditControlValue();
6156 if ( GetGridCursorCol() < GetNumberCols()-1 )
6158 MoveCursorRight( FALSE
);
6163 HideCellEditControl();
6164 SaveEditControlValue();
6170 if ( event
.ControlDown() )
6172 MakeCellVisible( 0, 0 );
6173 SetCurrentCell( 0, 0 );
6182 if ( event
.ControlDown() )
6184 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6185 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6202 if ( event
.ControlDown() )
6206 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6207 m_currentCellCoords
.GetCol(),
6208 event
.ControlDown(),
6215 if ( !IsEditable() )
6217 MoveCursorRight( FALSE
);
6220 // Otherwise fall through to default
6223 // is it possible to edit the current cell at all?
6224 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6226 // yes, now check whether the cells editor accepts the key
6227 int row
= m_currentCellCoords
.GetRow();
6228 int col
= m_currentCellCoords
.GetCol();
6229 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6230 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6232 // <F2> is special and will always start editing, for
6233 // other keys - ask the editor itself
6234 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
6235 || editor
->IsAcceptedKey(event
) )
6237 // ensure cell is visble
6238 MakeCellVisible(row
, col
);
6239 EnableCellEditControl();
6241 // a problem can arise if the cell is not completely
6242 // visible (even after calling MakeCellVisible the
6243 // control is not created and calling StartingKey will
6245 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6257 // let others process char events with modifiers or all
6258 // char events for readonly cells
6265 m_inOnKeyDown
= FALSE
;
6268 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6270 // try local handlers
6272 if ( event
.KeyCode() == WXK_SHIFT
)
6274 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6275 m_selectingBottomRight
!= wxGridNoCellCoords
)
6279 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6280 m_selectingTopLeft
.GetCol(),
6281 m_selectingBottomRight
.GetRow(),
6282 m_selectingBottomRight
.GetCol(),
6283 event
.ControlDown(),
6290 m_selectingTopLeft
= wxGridNoCellCoords
;
6291 m_selectingBottomRight
= wxGridNoCellCoords
;
6292 m_selectingKeyboard
= wxGridNoCellCoords
;
6296 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6300 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6302 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6304 // the event has been intercepted - do nothing
6308 wxClientDC
dc(m_gridWin
);
6311 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6313 HideCellEditControl();
6314 DisableCellEditControl();
6316 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6319 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6320 if ( !m_gridLinesEnabled
)
6328 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6330 // Otherwise refresh redraws the highlight!
6331 m_currentCellCoords
= coords
;
6333 DrawGridCellArea(dc
,cells
);
6334 DrawAllGridLines( dc
, r
);
6338 m_currentCellCoords
= coords
;
6340 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6341 DrawCellHighlight(dc
, attr
);
6346 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6349 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6353 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6356 rightCol
= GetNumberCols() - 1;
6358 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6361 bottomRow
= GetNumberRows() - 1;
6365 if ( topRow
> bottomRow
)
6372 if ( leftCol
> rightCol
)
6379 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6380 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6382 // First the case that we selected a completely new area
6383 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6384 m_selectingBottomRight
== wxGridNoCellCoords
)
6387 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6388 wxGridCellCoords ( bottomRow
, rightCol
) );
6389 m_gridWin
->Refresh( FALSE
, &rect
);
6391 // Now handle changing an existing selection area.
6392 else if ( m_selectingTopLeft
!= updateTopLeft
||
6393 m_selectingBottomRight
!= updateBottomRight
)
6395 // Compute two optimal update rectangles:
6396 // Either one rectangle is a real subset of the
6397 // other, or they are (almost) disjoint!
6399 bool need_refresh
[4];
6403 need_refresh
[3] = FALSE
;
6406 // Store intermediate values
6407 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6408 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6409 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6410 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6412 // Determine the outer/inner coordinates.
6413 if (oldLeft
> leftCol
)
6419 if (oldTop
> topRow
)
6425 if (oldRight
< rightCol
)
6428 oldRight
= rightCol
;
6431 if (oldBottom
< bottomRow
)
6434 oldBottom
= bottomRow
;
6438 // Now, either the stuff marked old is the outer
6439 // rectangle or we don't have a situation where one
6440 // is contained in the other.
6442 if ( oldLeft
< leftCol
)
6444 // Refresh the newly selected or deselected
6445 // area to the left of the old or new selection.
6446 need_refresh
[0] = TRUE
;
6447 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6449 wxGridCellCoords ( oldBottom
,
6453 if ( oldTop
< topRow
)
6455 // Refresh the newly selected or deselected
6456 // area above the old or new selection.
6457 need_refresh
[1] = TRUE
;
6458 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6460 wxGridCellCoords ( topRow
- 1,
6464 if ( oldRight
> rightCol
)
6466 // Refresh the newly selected or deselected
6467 // area to the right of the old or new selection.
6468 need_refresh
[2] = TRUE
;
6469 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6471 wxGridCellCoords ( oldBottom
,
6475 if ( oldBottom
> bottomRow
)
6477 // Refresh the newly selected or deselected
6478 // area below the old or new selection.
6479 need_refresh
[3] = TRUE
;
6480 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6482 wxGridCellCoords ( oldBottom
,
6486 // various Refresh() calls
6487 for (i
= 0; i
< 4; i
++ )
6488 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6489 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6492 m_selectingTopLeft
= updateTopLeft
;
6493 m_selectingBottomRight
= updateBottomRight
;
6497 // ------ functions to get/send data (see also public functions)
6500 bool wxGrid::GetModelValues()
6502 // Hide the editor, so it won't hide a changed value.
6503 HideCellEditControl();
6507 // all we need to do is repaint the grid
6509 m_gridWin
->Refresh();
6517 bool wxGrid::SetModelValues()
6521 // Disable the editor, so it won't hide a changed value.
6522 // Do we also want to save the current value of the editor first?
6524 DisableCellEditControl();
6528 for ( row
= 0; row
< m_numRows
; row
++ )
6530 for ( col
= 0; col
< m_numCols
; col
++ )
6532 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6544 // Note - this function only draws cells that are in the list of
6545 // exposed cells (usually set from the update region by
6546 // CalcExposedCells)
6548 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6550 if ( !m_numRows
|| !m_numCols
) return;
6552 int i
, numCells
= cells
.GetCount();
6553 int row
, col
, cell_rows
, cell_cols
;
6554 wxGridCellCoordsArray redrawCells
;
6556 for ( i
= numCells
-1; i
>= 0; i
-- )
6558 row
= cells
[i
].GetRow();
6559 col
= cells
[i
].GetCol();
6560 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6562 // If this cell is part of a multicell block, find owner for repaint
6563 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6565 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6566 bool marked
= FALSE
;
6567 for ( int j
= 0; j
< numCells
; j
++ )
6569 if ( cell
== cells
[j
] )
6577 int count
= redrawCells
.GetCount();
6578 for (int j
= 0; j
< count
; j
++)
6580 if ( cell
== redrawCells
[j
] )
6586 if (!marked
) redrawCells
.Add( cell
);
6588 continue; // don't bother drawing this cell
6591 // If this cell is empty, find cell to left that might want to overflow
6592 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6594 for ( int l
= 0; l
< cell_rows
; l
++ )
6596 // find a cell in this row to left alreay marked for repaint
6598 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6599 if ((redrawCells
[k
].GetCol() < left
) &&
6600 (redrawCells
[k
].GetRow() == row
))
6601 left
=redrawCells
[k
].GetCol();
6603 if (left
== col
) left
= 0; // oh well
6605 for (int j
= col
-1; j
>= left
; j
--)
6607 if (!m_table
->IsEmptyCell(row
+l
, j
))
6609 if (GetCellOverflow(row
+l
, j
))
6611 wxGridCellCoords
cell(row
+l
, j
);
6612 bool marked
= FALSE
;
6614 for (int k
= 0; k
< numCells
; k
++)
6616 if ( cell
== cells
[k
] )
6624 int count
= redrawCells
.GetCount();
6625 for (int k
= 0; k
< count
; k
++)
6627 if ( cell
== redrawCells
[k
] )
6633 if (!marked
) redrawCells
.Add( cell
);
6641 DrawCell( dc
, cells
[i
] );
6644 numCells
= redrawCells
.GetCount();
6646 for ( i
= numCells
- 1; i
>= 0; i
-- )
6648 DrawCell( dc
, redrawCells
[i
] );
6653 void wxGrid::DrawGridSpace( wxDC
& dc
)
6656 m_gridWin
->GetClientSize( &cw
, &ch
);
6659 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6661 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6662 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6664 if ( right
> rightCol
|| bottom
> bottomRow
)
6667 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6669 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6670 dc
.SetPen( *wxTRANSPARENT_PEN
);
6672 if ( right
> rightCol
)
6674 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6677 if ( bottom
> bottomRow
)
6679 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6685 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6687 int row
= coords
.GetRow();
6688 int col
= coords
.GetCol();
6690 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6693 // we draw the cell border ourselves
6694 #if !WXGRID_DRAW_LINES
6695 if ( m_gridLinesEnabled
)
6696 DrawCellBorder( dc
, coords
);
6699 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6701 bool isCurrent
= coords
== m_currentCellCoords
;
6703 wxRect rect
= CellToRect( row
, col
);
6705 // if the editor is shown, we should use it and not the renderer
6706 // Note: However, only if it is really _shown_, i.e. not hidden!
6707 if ( isCurrent
&& IsCellEditControlShown() )
6709 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6710 editor
->PaintBackground(rect
, attr
);
6715 // but all the rest is drawn by the cell renderer and hence may be
6717 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6718 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6725 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6727 int row
= m_currentCellCoords
.GetRow();
6728 int col
= m_currentCellCoords
.GetCol();
6730 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6733 wxRect rect
= CellToRect(row
, col
);
6735 // hmmm... what could we do here to show that the cell is disabled?
6736 // for now, I just draw a thinner border than for the other ones, but
6737 // it doesn't look really good
6739 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6743 // The center of th drawn line is where the position/width/height of
6744 // the rectangle is actually at, (on wxMSW atr least,) so we will
6745 // reduce the size of the rectangle to compensate for the thickness of
6746 // the line. If this is too strange on non wxMSW platforms then
6747 // please #ifdef this appropriately.
6748 rect
.x
+= penWidth
/2;
6749 rect
.y
+= penWidth
/2;
6750 rect
.width
-= penWidth
-1;
6751 rect
.height
-= penWidth
-1;
6754 // Now draw the rectangle
6755 // use the cellHighlightColour if the cell is inside a selection, this
6756 // will ensure the cell is always visible.
6757 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6758 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6759 dc
.DrawRectangle(rect
);
6763 // VZ: my experiments with 3d borders...
6765 // how to properly set colours for arbitrary bg?
6766 wxCoord x1
= rect
.x
,
6768 x2
= rect
.x
+ rect
.width
-1,
6769 y2
= rect
.y
+ rect
.height
-1;
6771 dc
.SetPen(*wxWHITE_PEN
);
6772 dc
.DrawLine(x1
, y1
, x2
, y1
);
6773 dc
.DrawLine(x1
, y1
, x1
, y2
);
6775 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6776 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6778 dc
.SetPen(*wxBLACK_PEN
);
6779 dc
.DrawLine(x1
, y2
, x2
, y2
);
6780 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6785 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6787 int row
= coords
.GetRow();
6788 int col
= coords
.GetCol();
6789 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6792 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6794 wxRect rect
= CellToRect( row
, col
);
6796 // right hand border
6798 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6799 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6803 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6804 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6807 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6809 // This if block was previously in wxGrid::OnPaint but that doesn't
6810 // seem to get called under wxGTK - MB
6812 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6813 m_numRows
&& m_numCols
)
6815 m_currentCellCoords
.Set(0, 0);
6818 if ( IsCellEditControlShown() )
6820 // don't show highlight when the edit control is shown
6824 // if the active cell was repainted, repaint its highlight too because it
6825 // might have been damaged by the grid lines
6826 size_t count
= cells
.GetCount();
6827 for ( size_t n
= 0; n
< count
; n
++ )
6829 if ( cells
[n
] == m_currentCellCoords
)
6831 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6832 DrawCellHighlight(dc
, attr
);
6840 // TODO: remove this ???
6841 // This is used to redraw all grid lines e.g. when the grid line colour
6844 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6846 #if !WXGRID_DRAW_LINES
6850 if ( !m_gridLinesEnabled
||
6852 !m_numCols
) return;
6854 int top
, bottom
, left
, right
;
6856 #if 0 //#ifndef __WXGTK__
6860 m_gridWin
->GetClientSize(&cw
, &ch
);
6862 // virtual coords of visible area
6864 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6865 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6870 reg
.GetBox(x
, y
, w
, h
);
6871 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6872 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6876 m_gridWin
->GetClientSize(&cw
, &ch
);
6877 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6878 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6881 // avoid drawing grid lines past the last row and col
6883 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6884 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6886 // no gridlines inside multicells, clip them out
6887 int leftCol
= XToCol(left
);
6888 int topRow
= YToRow(top
);
6889 int rightCol
= XToCol(right
);
6890 int bottomRow
= YToRow(bottom
);
6891 wxRegion
clippedcells(0, 0, cw
, ch
);
6893 if ((leftCol
>= 0) && (topRow
>= 0))
6895 if (rightCol
< 0) rightCol
= m_numCols
;
6896 if (bottomRow
< 0) bottomRow
= m_numRows
;
6898 int i
, j
, cell_rows
, cell_cols
;
6901 for (j
=topRow
; j
<bottomRow
; j
++)
6903 for (i
=leftCol
; i
<rightCol
; i
++)
6905 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
6906 if ((cell_rows
> 1) || (cell_cols
> 1))
6908 rect
= CellToRect(j
,i
);
6909 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6910 clippedcells
.Subtract(rect
);
6912 else if ((cell_rows
< 0) || (cell_cols
< 0))
6914 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
6915 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6916 clippedcells
.Subtract(rect
);
6921 dc
.SetClippingRegion( clippedcells
);
6923 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6925 // horizontal grid lines
6928 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6930 int bot
= GetRowBottom(i
) - 1;
6939 dc
.DrawLine( left
, bot
, right
, bot
);
6944 // vertical grid lines
6946 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6948 int colRight
= GetColRight(i
) - 1;
6949 if ( colRight
> right
)
6954 if ( colRight
>= left
)
6956 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6959 dc
.DestroyClippingRegion();
6963 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6965 if ( !m_numRows
) return;
6968 size_t numLabels
= rows
.GetCount();
6970 for ( i
= 0; i
< numLabels
; i
++ )
6972 DrawRowLabel( dc
, rows
[i
] );
6977 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6979 if ( GetRowHeight(row
) <= 0 )
6982 int rowTop
= GetRowTop(row
),
6983 rowBottom
= GetRowBottom(row
) - 1;
6985 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
6986 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6987 m_rowLabelWidth
-1, rowBottom
);
6989 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6991 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
6993 dc
.SetPen( *wxWHITE_PEN
);
6994 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
6995 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
6997 dc
.SetBackgroundMode( wxTRANSPARENT
);
6998 dc
.SetTextForeground( GetLabelTextColour() );
6999 dc
.SetFont( GetLabelFont() );
7002 GetRowLabelAlignment( &hAlign
, &vAlign
);
7006 rect
.SetY( GetRowTop(row
) + 2 );
7007 rect
.SetWidth( m_rowLabelWidth
- 4 );
7008 rect
.SetHeight( GetRowHeight(row
) - 4 );
7009 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7013 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7015 if ( !m_numCols
) return;
7018 size_t numLabels
= cols
.GetCount();
7020 for ( i
= 0; i
< numLabels
; i
++ )
7022 DrawColLabel( dc
, cols
[i
] );
7027 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7029 if ( GetColWidth(col
) <= 0 )
7032 int colLeft
= GetColLeft(col
),
7033 colRight
= GetColRight(col
) - 1;
7035 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7036 dc
.DrawLine( colRight
, 0,
7037 colRight
, m_colLabelHeight
-1 );
7039 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7041 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7042 colRight
+1, m_colLabelHeight
-1 );
7044 dc
.SetPen( *wxWHITE_PEN
);
7045 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7046 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7048 dc
.SetBackgroundMode( wxTRANSPARENT
);
7049 dc
.SetTextForeground( GetLabelTextColour() );
7050 dc
.SetFont( GetLabelFont() );
7052 dc
.SetBackgroundMode( wxTRANSPARENT
);
7053 dc
.SetTextForeground( GetLabelTextColour() );
7054 dc
.SetFont( GetLabelFont() );
7057 GetColLabelAlignment( &hAlign
, &vAlign
);
7060 rect
.SetX( colLeft
+ 2 );
7062 rect
.SetWidth( GetColWidth(col
) - 4 );
7063 rect
.SetHeight( m_colLabelHeight
- 4 );
7064 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
7067 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7068 const wxString
& value
,
7073 wxArrayString lines
;
7075 StringToLines( value
, lines
);
7078 //Forward to new API.
7079 DrawTextRectangle( dc
,
7087 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7088 const wxArrayString
& lines
,
7093 long textWidth
, textHeight
;
7094 long lineWidth
, lineHeight
;
7097 dc
.SetClippingRegion( rect
);
7099 nLines
= lines
.GetCount();
7104 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7107 case wxALIGN_BOTTOM
:
7108 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7111 case wxALIGN_CENTRE
:
7112 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7121 // Align each line of a multi-line label
7122 for( l
= 0; l
< nLines
; l
++ )
7124 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7126 switch( horizAlign
)
7129 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7132 case wxALIGN_CENTRE
:
7133 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7142 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7146 dc
.DestroyClippingRegion();
7150 // Split multi line text up into an array of strings. Any existing
7151 // contents of the string array are preserved.
7153 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7157 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7158 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7160 while ( startPos
< (int)tVal
.Length() )
7162 pos
= tVal
.Mid(startPos
).Find( eol
);
7167 else if ( pos
== 0 )
7169 lines
.Add( wxEmptyString
);
7173 lines
.Add( value
.Mid(startPos
, pos
) );
7177 if ( startPos
< (int)value
.Length() )
7179 lines
.Add( value
.Mid( startPos
) );
7184 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7185 const wxArrayString
& lines
,
7186 long *width
, long *height
)
7193 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7195 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7196 w
= wxMax( w
, lineW
);
7205 // ------ Batch processing.
7207 void wxGrid::EndBatch()
7209 if ( m_batchCount
> 0 )
7212 if ( !m_batchCount
)
7215 m_rowLabelWin
->Refresh();
7216 m_colLabelWin
->Refresh();
7217 m_cornerLabelWin
->Refresh();
7218 m_gridWin
->Refresh();
7223 // Use this, rather than wxWindow::Refresh(), to force an immediate
7224 // repainting of the grid. Has no effect if you are already inside a
7225 // BeginBatch / EndBatch block.
7227 void wxGrid::ForceRefresh()
7235 // ------ Edit control functions
7239 void wxGrid::EnableEditing( bool edit
)
7241 // TODO: improve this ?
7243 if ( edit
!= m_editable
)
7245 if(!edit
) EnableCellEditControl(edit
);
7251 void wxGrid::EnableCellEditControl( bool enable
)
7256 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7257 SetCurrentCell( 0, 0 );
7259 if ( enable
!= m_cellEditCtrlEnabled
)
7263 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7266 // this should be checked by the caller!
7267 wxASSERT_MSG( CanEnableCellControl(),
7268 _T("can't enable editing for this cell!") );
7270 // do it before ShowCellEditControl()
7271 m_cellEditCtrlEnabled
= enable
;
7273 ShowCellEditControl();
7277 //FIXME:add veto support
7278 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7280 HideCellEditControl();
7281 SaveEditControlValue();
7283 // do it after HideCellEditControl()
7284 m_cellEditCtrlEnabled
= enable
;
7289 bool wxGrid::IsCurrentCellReadOnly() const
7292 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7293 bool readonly
= attr
->IsReadOnly();
7299 bool wxGrid::CanEnableCellControl() const
7301 return m_editable
&& !IsCurrentCellReadOnly();
7304 bool wxGrid::IsCellEditControlEnabled() const
7306 // the cell edit control might be disable for all cells or just for the
7307 // current one if it's read only
7308 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7311 bool wxGrid::IsCellEditControlShown() const
7313 bool isShown
= FALSE
;
7315 if ( m_cellEditCtrlEnabled
)
7317 int row
= m_currentCellCoords
.GetRow();
7318 int col
= m_currentCellCoords
.GetCol();
7319 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7320 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7325 if ( editor
->IsCreated() )
7327 isShown
= editor
->GetControl()->IsShown();
7337 void wxGrid::ShowCellEditControl()
7339 if ( IsCellEditControlEnabled() )
7341 if ( !IsVisible( m_currentCellCoords
) )
7343 m_cellEditCtrlEnabled
= FALSE
;
7348 wxRect rect
= CellToRect( m_currentCellCoords
);
7349 int row
= m_currentCellCoords
.GetRow();
7350 int col
= m_currentCellCoords
.GetCol();
7352 // if this is part of a multicell, find owner (topleft)
7353 int cell_rows
, cell_cols
;
7354 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7355 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7359 m_currentCellCoords
.SetRow( row
);
7360 m_currentCellCoords
.SetCol( col
);
7363 // convert to scrolled coords
7365 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7367 // done in PaintBackground()
7369 // erase the highlight and the cell contents because the editor
7370 // might not cover the entire cell
7371 wxClientDC
dc( m_gridWin
);
7373 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7374 dc
.SetPen(*wxTRANSPARENT_PEN
);
7375 dc
.DrawRectangle(rect
);
7378 // cell is shifted by one pixel
7379 // However, don't allow x or y to become negative
7380 // since the SetSize() method interprets that as
7387 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7388 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7389 if ( !editor
->IsCreated() )
7391 editor
->Create(m_gridWin
, -1,
7392 new wxGridCellEditorEvtHandler(this, editor
));
7394 wxGridEditorCreatedEvent
evt(GetId(),
7395 wxEVT_GRID_EDITOR_CREATED
,
7399 editor
->GetControl());
7400 GetEventHandler()->ProcessEvent(evt
);
7404 // resize editor to overflow into righthand cells if allowed
7405 int maxWidth
= rect
.width
;
7406 wxString value
= GetCellValue(row
, col
);
7407 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7410 GetTextExtent(value
, &maxWidth
, &y
,
7411 NULL
, NULL
, &attr
->GetFont());
7412 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7414 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7415 if (rect
.x
+maxWidth
> client_right
)
7416 maxWidth
= client_right
- rect
.x
;
7418 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7420 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7421 // may have changed earlier
7422 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7425 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7426 // looks weird going over a multicell
7427 if (m_table
->IsEmptyCell(row
,i
) &&
7428 (rect
.width
< maxWidth
) && (c_rows
== 1))
7429 rect
.width
+= GetColWidth(i
);
7433 if (rect
.GetRight() > client_right
)
7434 rect
.SetRight(client_right
-1);
7437 editor
->SetCellAttr(attr
);
7438 editor
->SetSize( rect
);
7439 editor
->Show( TRUE
, attr
);
7441 // recalc dimensions in case we need to
7442 // expand the scrolled window to account for editor
7445 editor
->BeginEdit(row
, col
, this);
7446 editor
->SetCellAttr(NULL
);
7455 void wxGrid::HideCellEditControl()
7457 if ( IsCellEditControlEnabled() )
7459 int row
= m_currentCellCoords
.GetRow();
7460 int col
= m_currentCellCoords
.GetCol();
7462 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7463 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7464 editor
->Show( FALSE
);
7467 m_gridWin
->SetFocus();
7468 // refresh whole row to the right
7469 wxRect
rect( CellToRect(row
, col
) );
7470 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7471 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7472 m_gridWin
->Refresh( FALSE
, &rect
);
7477 void wxGrid::SaveEditControlValue()
7479 if ( IsCellEditControlEnabled() )
7481 int row
= m_currentCellCoords
.GetRow();
7482 int col
= m_currentCellCoords
.GetCol();
7484 wxString oldval
= GetCellValue(row
,col
);
7486 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7487 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7488 bool changed
= editor
->EndEdit(row
, col
, this);
7495 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7496 m_currentCellCoords
.GetRow(),
7497 m_currentCellCoords
.GetCol() ) < 0 ) {
7499 // Event has been vetoed, set the data back.
7500 SetCellValue(row
,col
,oldval
);
7508 // ------ Grid location functions
7509 // Note that all of these functions work with the logical coordinates of
7510 // grid cells and labels so you will need to convert from device
7511 // coordinates for mouse events etc.
7514 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7516 int row
= YToRow(y
);
7517 int col
= XToCol(x
);
7519 if ( row
== -1 || col
== -1 )
7521 coords
= wxGridNoCellCoords
;
7525 coords
.Set( row
, col
);
7530 // Internal Helper function for computing row or column from some
7531 // (unscrolled) coordinate value, using either
7532 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7533 // of m_rowBottoms/m_ColRights to speed up the search!
7535 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7536 const wxArrayInt
& BorderArray
, int nMax
,
7541 size_t i_max
= coord
/ defaultDist
,
7544 if (BorderArray
.IsEmpty())
7546 if((int) i_max
< nMax
)
7548 return maxOnOverflow
? nMax
- 1 : -1;
7551 if ( i_max
>= BorderArray
.GetCount())
7552 i_max
= BorderArray
.GetCount() - 1;
7555 if ( coord
>= BorderArray
[i_max
])
7558 i_max
= coord
/ minDist
;
7560 if ( i_max
>= BorderArray
.GetCount())
7561 i_max
= BorderArray
.GetCount() - 1;
7563 if ( coord
>= BorderArray
[i_max
])
7564 return maxOnOverflow
? (int)i_max
: -1;
7565 if ( coord
< BorderArray
[0] )
7568 while ( i_max
- i_min
> 0 )
7570 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7571 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7572 if (coord
>= BorderArray
[ i_max
- 1])
7576 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7577 if (coord
< BorderArray
[median
])
7585 int wxGrid::YToRow( int y
)
7587 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7588 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, m_numRows
, FALSE
);
7592 int wxGrid::XToCol( int x
)
7594 return CoordToRowOrCol(x
, m_defaultColWidth
,
7595 WXGRID_MIN_COL_WIDTH
, m_colRights
, m_numCols
, FALSE
);
7599 // return the row number that that the y coord is near the edge of, or
7600 // -1 if not near an edge
7602 int wxGrid::YToEdgeOfRow( int y
)
7605 i
= internalYToRow(y
);
7607 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7609 // We know that we are in row i, test whether we are
7610 // close enough to lower or upper border, respectively.
7611 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7613 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7621 // return the col number that that the x coord is near the edge of, or
7622 // -1 if not near an edge
7624 int wxGrid::XToEdgeOfCol( int x
)
7627 i
= internalXToCol(x
);
7629 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7631 // We know that we are in column i, test whether we are
7632 // close enough to right or left border, respectively.
7633 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7635 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7643 wxRect
wxGrid::CellToRect( int row
, int col
)
7645 wxRect
rect( -1, -1, -1, -1 );
7647 if ( row
>= 0 && row
< m_numRows
&&
7648 col
>= 0 && col
< m_numCols
)
7650 int i
, cell_rows
, cell_cols
;
7651 rect
.width
= rect
.height
= 0;
7652 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7653 // if negative then find multicell owner
7654 if (cell_rows
< 0) row
+= cell_rows
;
7655 if (cell_cols
< 0) col
+= cell_cols
;
7656 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7658 rect
.x
= GetColLeft(col
);
7659 rect
.y
= GetRowTop(row
);
7660 for (i
=col
; i
<col
+cell_cols
; i
++)
7661 rect
.width
+= GetColWidth(i
);
7662 for (i
=row
; i
<row
+cell_rows
; i
++)
7663 rect
.height
+= GetRowHeight(i
);
7666 // if grid lines are enabled, then the area of the cell is a bit smaller
7667 if (m_gridLinesEnabled
) {
7675 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7677 // get the cell rectangle in logical coords
7679 wxRect
r( CellToRect( row
, col
) );
7681 // convert to device coords
7683 int left
, top
, right
, bottom
;
7684 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7685 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7687 // check against the client area of the grid window
7690 m_gridWin
->GetClientSize( &cw
, &ch
);
7692 if ( wholeCellVisible
)
7694 // is the cell wholly visible ?
7696 return ( left
>= 0 && right
<= cw
&&
7697 top
>= 0 && bottom
<= ch
);
7701 // is the cell partly visible ?
7703 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7704 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7709 // make the specified cell location visible by doing a minimal amount
7712 void wxGrid::MakeCellVisible( int row
, int col
)
7716 int xpos
= -1, ypos
= -1;
7718 if ( row
>= 0 && row
< m_numRows
&&
7719 col
>= 0 && col
< m_numCols
)
7721 // get the cell rectangle in logical coords
7723 wxRect
r( CellToRect( row
, col
) );
7725 // convert to device coords
7727 int left
, top
, right
, bottom
;
7728 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7729 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7732 m_gridWin
->GetClientSize( &cw
, &ch
);
7738 else if ( bottom
> ch
)
7740 int h
= r
.GetHeight();
7742 for ( i
= row
-1; i
>= 0; i
-- )
7744 int rowHeight
= GetRowHeight(i
);
7745 if ( h
+ rowHeight
> ch
)
7752 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7753 // have rounding errors (this is important, because if we do, we
7754 // might not scroll at all and some cells won't be redrawn)
7756 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7758 ypos
+= GRID_SCROLL_LINE_Y
;
7765 else if ( right
> cw
)
7767 // position the view so that the cell is on the right
7769 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
7770 xpos
= x0
+ (right
- cw
);
7772 // see comment for ypos above
7773 xpos
+= GRID_SCROLL_LINE_X
;
7776 if ( xpos
!= -1 || ypos
!= -1 )
7779 xpos
/= GRID_SCROLL_LINE_X
;
7781 ypos
/= GRID_SCROLL_LINE_Y
;
7782 Scroll( xpos
, ypos
);
7790 // ------ Grid cursor movement functions
7793 bool wxGrid::MoveCursorUp( bool expandSelection
)
7795 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7796 m_currentCellCoords
.GetRow() >= 0 )
7798 if ( expandSelection
)
7800 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7801 m_selectingKeyboard
= m_currentCellCoords
;
7802 if ( m_selectingKeyboard
.GetRow() > 0 )
7804 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7805 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7806 m_selectingKeyboard
.GetCol() );
7807 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7810 else if ( m_currentCellCoords
.GetRow() > 0 )
7813 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7814 m_currentCellCoords
.GetCol() );
7815 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7816 m_currentCellCoords
.GetCol() );
7827 bool wxGrid::MoveCursorDown( bool expandSelection
)
7829 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7830 m_currentCellCoords
.GetRow() < m_numRows
)
7832 if ( expandSelection
)
7834 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7835 m_selectingKeyboard
= m_currentCellCoords
;
7836 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7838 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7839 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7840 m_selectingKeyboard
.GetCol() );
7841 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7844 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7847 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7848 m_currentCellCoords
.GetCol() );
7849 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7850 m_currentCellCoords
.GetCol() );
7861 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7863 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7864 m_currentCellCoords
.GetCol() >= 0 )
7866 if ( expandSelection
)
7868 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7869 m_selectingKeyboard
= m_currentCellCoords
;
7870 if ( m_selectingKeyboard
.GetCol() > 0 )
7872 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7873 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7874 m_selectingKeyboard
.GetCol() );
7875 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7878 else if ( m_currentCellCoords
.GetCol() > 0 )
7881 MakeCellVisible( m_currentCellCoords
.GetRow(),
7882 m_currentCellCoords
.GetCol() - 1 );
7883 SetCurrentCell( m_currentCellCoords
.GetRow(),
7884 m_currentCellCoords
.GetCol() - 1 );
7895 bool wxGrid::MoveCursorRight( bool expandSelection
)
7897 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7898 m_currentCellCoords
.GetCol() < m_numCols
)
7900 if ( expandSelection
)
7902 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7903 m_selectingKeyboard
= m_currentCellCoords
;
7904 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7906 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7907 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7908 m_selectingKeyboard
.GetCol() );
7909 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7912 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7915 MakeCellVisible( m_currentCellCoords
.GetRow(),
7916 m_currentCellCoords
.GetCol() + 1 );
7917 SetCurrentCell( m_currentCellCoords
.GetRow(),
7918 m_currentCellCoords
.GetCol() + 1 );
7929 bool wxGrid::MovePageUp()
7931 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7933 int row
= m_currentCellCoords
.GetRow();
7937 m_gridWin
->GetClientSize( &cw
, &ch
);
7939 int y
= GetRowTop(row
);
7940 int newRow
= YToRow( y
- ch
+ 1 );
7945 else if ( newRow
== row
)
7950 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7951 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7959 bool wxGrid::MovePageDown()
7961 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7963 int row
= m_currentCellCoords
.GetRow();
7964 if ( row
< m_numRows
)
7967 m_gridWin
->GetClientSize( &cw
, &ch
);
7969 int y
= GetRowTop(row
);
7970 int newRow
= YToRow( y
+ ch
);
7973 newRow
= m_numRows
- 1;
7975 else if ( newRow
== row
)
7980 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7981 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7989 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7992 m_currentCellCoords
!= wxGridNoCellCoords
&&
7993 m_currentCellCoords
.GetRow() > 0 )
7995 int row
= m_currentCellCoords
.GetRow();
7996 int col
= m_currentCellCoords
.GetCol();
7998 if ( m_table
->IsEmptyCell(row
, col
) )
8000 // starting in an empty cell: find the next block of
8006 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8009 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8011 // starting at the top of a block: find the next block
8017 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8022 // starting within a block: find the top of the block
8027 if ( m_table
->IsEmptyCell(row
, col
) )
8035 MakeCellVisible( row
, col
);
8036 if ( expandSelection
)
8038 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8039 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8044 SetCurrentCell( row
, col
);
8052 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8055 m_currentCellCoords
!= wxGridNoCellCoords
&&
8056 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8058 int row
= m_currentCellCoords
.GetRow();
8059 int col
= m_currentCellCoords
.GetCol();
8061 if ( m_table
->IsEmptyCell(row
, col
) )
8063 // starting in an empty cell: find the next block of
8066 while ( row
< m_numRows
-1 )
8069 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8072 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8074 // starting at the bottom of a block: find the next block
8077 while ( row
< m_numRows
-1 )
8080 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8085 // starting within a block: find the bottom of the block
8087 while ( row
< m_numRows
-1 )
8090 if ( m_table
->IsEmptyCell(row
, col
) )
8098 MakeCellVisible( row
, col
);
8099 if ( expandSelection
)
8101 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8102 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8107 SetCurrentCell( row
, col
);
8116 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8119 m_currentCellCoords
!= wxGridNoCellCoords
&&
8120 m_currentCellCoords
.GetCol() > 0 )
8122 int row
= m_currentCellCoords
.GetRow();
8123 int col
= m_currentCellCoords
.GetCol();
8125 if ( m_table
->IsEmptyCell(row
, col
) )
8127 // starting in an empty cell: find the next block of
8133 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8136 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8138 // starting at the left of a block: find the next block
8144 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8149 // starting within a block: find the left of the block
8154 if ( m_table
->IsEmptyCell(row
, col
) )
8162 MakeCellVisible( row
, col
);
8163 if ( expandSelection
)
8165 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8166 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8171 SetCurrentCell( row
, col
);
8180 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8183 m_currentCellCoords
!= wxGridNoCellCoords
&&
8184 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8186 int row
= m_currentCellCoords
.GetRow();
8187 int col
= m_currentCellCoords
.GetCol();
8189 if ( m_table
->IsEmptyCell(row
, col
) )
8191 // starting in an empty cell: find the next block of
8194 while ( col
< m_numCols
-1 )
8197 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8200 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8202 // starting at the right of a block: find the next block
8205 while ( col
< m_numCols
-1 )
8208 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8213 // starting within a block: find the right of the block
8215 while ( col
< m_numCols
-1 )
8218 if ( m_table
->IsEmptyCell(row
, col
) )
8226 MakeCellVisible( row
, col
);
8227 if ( expandSelection
)
8229 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8230 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8235 SetCurrentCell( row
, col
);
8247 // ------ Label values and formatting
8250 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8252 *horiz
= m_rowLabelHorizAlign
;
8253 *vert
= m_rowLabelVertAlign
;
8256 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8258 *horiz
= m_colLabelHorizAlign
;
8259 *vert
= m_colLabelVertAlign
;
8262 wxString
wxGrid::GetRowLabelValue( int row
)
8266 return m_table
->GetRowLabelValue( row
);
8276 wxString
wxGrid::GetColLabelValue( int col
)
8280 return m_table
->GetColLabelValue( col
);
8291 void wxGrid::SetRowLabelSize( int width
)
8293 width
= wxMax( width
, 0 );
8294 if ( width
!= m_rowLabelWidth
)
8298 m_rowLabelWin
->Show( FALSE
);
8299 m_cornerLabelWin
->Show( FALSE
);
8301 else if ( m_rowLabelWidth
== 0 )
8303 m_rowLabelWin
->Show( TRUE
);
8304 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8307 m_rowLabelWidth
= width
;
8309 wxScrolledWindow::Refresh( TRUE
);
8314 void wxGrid::SetColLabelSize( int height
)
8316 height
= wxMax( height
, 0 );
8317 if ( height
!= m_colLabelHeight
)
8321 m_colLabelWin
->Show( FALSE
);
8322 m_cornerLabelWin
->Show( FALSE
);
8324 else if ( m_colLabelHeight
== 0 )
8326 m_colLabelWin
->Show( TRUE
);
8327 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8330 m_colLabelHeight
= height
;
8332 wxScrolledWindow::Refresh( TRUE
);
8337 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8339 if ( m_labelBackgroundColour
!= colour
)
8341 m_labelBackgroundColour
= colour
;
8342 m_rowLabelWin
->SetBackgroundColour( colour
);
8343 m_colLabelWin
->SetBackgroundColour( colour
);
8344 m_cornerLabelWin
->SetBackgroundColour( colour
);
8346 if ( !GetBatchCount() )
8348 m_rowLabelWin
->Refresh();
8349 m_colLabelWin
->Refresh();
8350 m_cornerLabelWin
->Refresh();
8355 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8357 if ( m_labelTextColour
!= colour
)
8359 m_labelTextColour
= colour
;
8360 if ( !GetBatchCount() )
8362 m_rowLabelWin
->Refresh();
8363 m_colLabelWin
->Refresh();
8368 void wxGrid::SetLabelFont( const wxFont
& font
)
8371 if ( !GetBatchCount() )
8373 m_rowLabelWin
->Refresh();
8374 m_colLabelWin
->Refresh();
8378 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8380 // allow old (incorrect) defs to be used
8383 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8384 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8385 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8390 case wxTOP
: vert
= wxALIGN_TOP
; break;
8391 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8392 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8395 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8397 m_rowLabelHorizAlign
= horiz
;
8400 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8402 m_rowLabelVertAlign
= vert
;
8405 if ( !GetBatchCount() )
8407 m_rowLabelWin
->Refresh();
8411 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8413 // allow old (incorrect) defs to be used
8416 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8417 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8418 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8423 case wxTOP
: vert
= wxALIGN_TOP
; break;
8424 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8425 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8428 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8430 m_colLabelHorizAlign
= horiz
;
8433 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8435 m_colLabelVertAlign
= vert
;
8438 if ( !GetBatchCount() )
8440 m_colLabelWin
->Refresh();
8444 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8448 m_table
->SetRowLabelValue( row
, s
);
8449 if ( !GetBatchCount() )
8451 wxRect rect
= CellToRect( row
, 0);
8452 if ( rect
.height
> 0 )
8454 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8456 rect
.width
= m_rowLabelWidth
;
8457 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8463 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8467 m_table
->SetColLabelValue( col
, s
);
8468 if ( !GetBatchCount() )
8470 wxRect rect
= CellToRect( 0, col
);
8471 if ( rect
.width
> 0 )
8473 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8475 rect
.height
= m_colLabelHeight
;
8476 m_colLabelWin
->Refresh( TRUE
, &rect
);
8482 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8484 if ( m_gridLineColour
!= colour
)
8486 m_gridLineColour
= colour
;
8488 wxClientDC
dc( m_gridWin
);
8490 DrawAllGridLines( dc
, wxRegion() );
8495 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8497 if ( m_cellHighlightColour
!= colour
)
8499 m_cellHighlightColour
= colour
;
8501 wxClientDC
dc( m_gridWin
);
8503 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8504 DrawCellHighlight(dc
, attr
);
8509 void wxGrid::SetCellHighlightPenWidth(int width
)
8511 if (m_cellHighlightPenWidth
!= width
) {
8512 m_cellHighlightPenWidth
= width
;
8514 // Just redrawing the cell highlight is not enough since that won't
8515 // make any visible change if the the thickness is getting smaller.
8516 int row
= m_currentCellCoords
.GetRow();
8517 int col
= m_currentCellCoords
.GetCol();
8518 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8520 wxRect rect
= CellToRect(row
, col
);
8521 m_gridWin
->Refresh(TRUE
, &rect
);
8525 void wxGrid::SetCellHighlightROPenWidth(int width
)
8527 if (m_cellHighlightROPenWidth
!= width
) {
8528 m_cellHighlightROPenWidth
= width
;
8530 // Just redrawing the cell highlight is not enough since that won't
8531 // make any visible change if the the thickness is getting smaller.
8532 int row
= m_currentCellCoords
.GetRow();
8533 int col
= m_currentCellCoords
.GetCol();
8534 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8536 wxRect rect
= CellToRect(row
, col
);
8537 m_gridWin
->Refresh(TRUE
, &rect
);
8541 void wxGrid::EnableGridLines( bool enable
)
8543 if ( enable
!= m_gridLinesEnabled
)
8545 m_gridLinesEnabled
= enable
;
8547 if ( !GetBatchCount() )
8551 wxClientDC
dc( m_gridWin
);
8553 DrawAllGridLines( dc
, wxRegion() );
8557 m_gridWin
->Refresh();
8564 int wxGrid::GetDefaultRowSize()
8566 return m_defaultRowHeight
;
8569 int wxGrid::GetRowSize( int row
)
8571 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8573 return GetRowHeight(row
);
8576 int wxGrid::GetDefaultColSize()
8578 return m_defaultColWidth
;
8581 int wxGrid::GetColSize( int col
)
8583 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8585 return GetColWidth(col
);
8588 // ============================================================================
8589 // access to the grid attributes: each of them has a default value in the grid
8590 // itself and may be overidden on a per-cell basis
8591 // ============================================================================
8593 // ----------------------------------------------------------------------------
8594 // setting default attributes
8595 // ----------------------------------------------------------------------------
8597 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8599 m_defaultCellAttr
->SetBackgroundColour(col
);
8601 m_gridWin
->SetBackgroundColour(col
);
8605 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8607 m_defaultCellAttr
->SetTextColour(col
);
8610 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8612 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8615 void wxGrid::SetDefaultCellOverflow( bool allow
)
8617 m_defaultCellAttr
->SetOverflow(allow
);
8620 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8622 m_defaultCellAttr
->SetFont(font
);
8625 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8627 m_defaultCellAttr
->SetRenderer(renderer
);
8630 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8632 m_defaultCellAttr
->SetEditor(editor
);
8635 // ----------------------------------------------------------------------------
8636 // access to the default attrbiutes
8637 // ----------------------------------------------------------------------------
8639 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8641 return m_defaultCellAttr
->GetBackgroundColour();
8644 wxColour
wxGrid::GetDefaultCellTextColour()
8646 return m_defaultCellAttr
->GetTextColour();
8649 wxFont
wxGrid::GetDefaultCellFont()
8651 return m_defaultCellAttr
->GetFont();
8654 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8656 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8659 bool wxGrid::GetDefaultCellOverflow()
8661 return m_defaultCellAttr
->GetOverflow();
8664 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8666 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8669 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8671 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8674 // ----------------------------------------------------------------------------
8675 // access to cell attributes
8676 // ----------------------------------------------------------------------------
8678 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8680 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8681 wxColour colour
= attr
->GetBackgroundColour();
8686 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8688 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8689 wxColour colour
= attr
->GetTextColour();
8694 wxFont
wxGrid::GetCellFont( int row
, int col
)
8696 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8697 wxFont font
= attr
->GetFont();
8702 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8704 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8705 attr
->GetAlignment(horiz
, vert
);
8709 bool wxGrid::GetCellOverflow( int row
, int col
)
8711 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8712 bool allow
= attr
->GetOverflow();
8717 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8719 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8720 attr
->GetSize( num_rows
, num_cols
);
8724 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8726 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8727 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8733 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8735 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8736 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8742 bool wxGrid::IsReadOnly(int row
, int col
) const
8744 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8745 bool isReadOnly
= attr
->IsReadOnly();
8750 // ----------------------------------------------------------------------------
8751 // attribute support: cache, automatic provider creation, ...
8752 // ----------------------------------------------------------------------------
8754 bool wxGrid::CanHaveAttributes()
8761 return m_table
->CanHaveAttributes();
8764 void wxGrid::ClearAttrCache()
8766 if ( m_attrCache
.row
!= -1 )
8768 wxSafeDecRef(m_attrCache
.attr
);
8769 m_attrCache
.attr
= NULL
;
8770 m_attrCache
.row
= -1;
8774 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8778 wxGrid
*self
= (wxGrid
*)this; // const_cast
8780 self
->ClearAttrCache();
8781 self
->m_attrCache
.row
= row
;
8782 self
->m_attrCache
.col
= col
;
8783 self
->m_attrCache
.attr
= attr
;
8788 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8790 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8792 *attr
= m_attrCache
.attr
;
8793 wxSafeIncRef(m_attrCache
.attr
);
8795 #ifdef DEBUG_ATTR_CACHE
8796 gs_nAttrCacheHits
++;
8803 #ifdef DEBUG_ATTR_CACHE
8804 gs_nAttrCacheMisses
++;
8810 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8812 wxGridCellAttr
*attr
= NULL
;
8813 // Additional test to avoid looking at the cache e.g. for
8814 // wxNoCellCoords, as this will confuse memory management.
8817 if ( !LookupAttr(row
, col
, &attr
) )
8819 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8820 : (wxGridCellAttr
*)NULL
;
8821 CacheAttr(row
, col
, attr
);
8826 attr
->SetDefAttr(m_defaultCellAttr
);
8830 attr
= m_defaultCellAttr
;
8837 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8839 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8841 wxCHECK_MSG( m_table
, attr
,
8842 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8844 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8847 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8849 // artificially inc the ref count to match DecRef() in caller
8851 m_table
->SetAttr(attr
, row
, col
);
8857 // ----------------------------------------------------------------------------
8858 // setting column attributes (wrappers around SetColAttr)
8859 // ----------------------------------------------------------------------------
8861 void wxGrid::SetColFormatBool(int col
)
8863 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8866 void wxGrid::SetColFormatNumber(int col
)
8868 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8871 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8873 wxString typeName
= wxGRID_VALUE_FLOAT
;
8874 if ( (width
!= -1) || (precision
!= -1) )
8876 typeName
<< _T(':') << width
<< _T(',') << precision
;
8879 SetColFormatCustom(col
, typeName
);
8882 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8884 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8886 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8888 attr
= new wxGridCellAttr
;
8889 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8890 attr
->SetRenderer(renderer
);
8892 SetColAttr(col
, attr
);
8896 // ----------------------------------------------------------------------------
8897 // setting cell attributes: this is forwarded to the table
8898 // ----------------------------------------------------------------------------
8900 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
8902 if ( CanHaveAttributes() )
8904 m_table
->SetAttr(attr
, row
, col
);
8913 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8915 if ( CanHaveAttributes() )
8917 m_table
->SetRowAttr(attr
, row
);
8926 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8928 if ( CanHaveAttributes() )
8930 m_table
->SetColAttr(attr
, col
);
8939 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8941 if ( CanHaveAttributes() )
8943 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8944 attr
->SetBackgroundColour(colour
);
8949 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8951 if ( CanHaveAttributes() )
8953 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8954 attr
->SetTextColour(colour
);
8959 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8961 if ( CanHaveAttributes() )
8963 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8964 attr
->SetFont(font
);
8969 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8971 if ( CanHaveAttributes() )
8973 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8974 attr
->SetAlignment(horiz
, vert
);
8979 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
8981 if ( CanHaveAttributes() )
8983 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8984 attr
->SetOverflow(allow
);
8989 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
8991 if ( CanHaveAttributes() )
8993 int cell_rows
, cell_cols
;
8995 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8996 attr
->GetSize(&cell_rows
, &cell_cols
);
8997 attr
->SetSize(num_rows
, num_cols
);
9000 // Cannot set the size of a cell to 0 or negative values
9001 // While it is perfectly legal to do that, this function cannot
9002 // handle all the possibilies, do it by hand by getting the CellAttr.
9003 // You can only set the size of a cell to 1,1 or greater with this fn
9004 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9005 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9006 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9007 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9009 // if this was already a multicell then "turn off" the other cells first
9010 if ((cell_rows
> 1) || (cell_rows
> 1))
9013 for (j
=row
; j
<row
+cell_rows
; j
++)
9015 for (i
=col
; i
<col
+cell_cols
; i
++)
9017 if ((i
!= col
) || (j
!= row
))
9019 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9020 attr_stub
->SetSize( 1, 1 );
9021 attr_stub
->DecRef();
9027 // mark the cells that will be covered by this cell to
9028 // negative or zero values to point back at this cell
9029 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9032 for (j
=row
; j
<row
+num_rows
; j
++)
9034 for (i
=col
; i
<col
+num_cols
; i
++)
9036 if ((i
!= col
) || (j
!= row
))
9038 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9039 attr_stub
->SetSize( row
-j
, col
-i
);
9040 attr_stub
->DecRef();
9048 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9050 if ( CanHaveAttributes() )
9052 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9053 attr
->SetRenderer(renderer
);
9058 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9060 if ( CanHaveAttributes() )
9062 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9063 attr
->SetEditor(editor
);
9068 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9070 if ( CanHaveAttributes() )
9072 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9073 attr
->SetReadOnly(isReadOnly
);
9078 // ----------------------------------------------------------------------------
9079 // Data type registration
9080 // ----------------------------------------------------------------------------
9082 void wxGrid::RegisterDataType(const wxString
& typeName
,
9083 wxGridCellRenderer
* renderer
,
9084 wxGridCellEditor
* editor
)
9086 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9090 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9092 wxString typeName
= m_table
->GetTypeName(row
, col
);
9093 return GetDefaultEditorForType(typeName
);
9096 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9098 wxString typeName
= m_table
->GetTypeName(row
, col
);
9099 return GetDefaultRendererForType(typeName
);
9103 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9105 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9106 if ( index
== wxNOT_FOUND
)
9108 wxFAIL_MSG(wxT("Unknown data type name"));
9113 return m_typeRegistry
->GetEditor(index
);
9117 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9119 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9120 if ( index
== wxNOT_FOUND
)
9122 wxFAIL_MSG(wxT("Unknown data type name"));
9127 return m_typeRegistry
->GetRenderer(index
);
9131 // ----------------------------------------------------------------------------
9133 // ----------------------------------------------------------------------------
9135 void wxGrid::EnableDragRowSize( bool enable
)
9137 m_canDragRowSize
= enable
;
9141 void wxGrid::EnableDragColSize( bool enable
)
9143 m_canDragColSize
= enable
;
9146 void wxGrid::EnableDragGridSize( bool enable
)
9148 m_canDragGridSize
= enable
;
9152 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9154 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
9156 if ( resizeExistingRows
)
9158 // since we are resizing all rows to the default row size,
9159 // we can simply clear the row heights and row bottoms
9160 // arrays (which also allows us to take advantage of
9161 // some speed optimisations)
9162 m_rowHeights
.Empty();
9163 m_rowBottoms
.Empty();
9164 if ( !GetBatchCount() )
9169 void wxGrid::SetRowSize( int row
, int height
)
9171 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9173 if ( m_rowHeights
.IsEmpty() )
9175 // need to really create the array
9179 int h
= wxMax( 0, height
);
9180 int diff
= h
- m_rowHeights
[row
];
9182 m_rowHeights
[row
] = h
;
9184 for ( i
= row
; i
< m_numRows
; i
++ )
9186 m_rowBottoms
[i
] += diff
;
9188 if ( !GetBatchCount() )
9192 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9194 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
9196 if ( resizeExistingCols
)
9198 // since we are resizing all columns to the default column size,
9199 // we can simply clear the col widths and col rights
9200 // arrays (which also allows us to take advantage of
9201 // some speed optimisations)
9202 m_colWidths
.Empty();
9203 m_colRights
.Empty();
9204 if ( !GetBatchCount() )
9209 void wxGrid::SetColSize( int col
, int width
)
9211 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9213 // should we check that it's bigger than GetColMinimalWidth(col) here?
9215 if ( m_colWidths
.IsEmpty() )
9217 // need to really create the array
9221 // if < 0 calc new width from label
9225 wxArrayString lines
;
9226 wxClientDC
dc(m_colLabelWin
);
9227 dc
.SetFont(GetLabelFont());
9228 StringToLines(GetColLabelValue(col
), lines
);
9229 GetTextBoxSize(dc
, lines
, &w
, &h
);
9232 int w
= wxMax( 0, width
);
9233 int diff
= w
- m_colWidths
[col
];
9234 m_colWidths
[col
] = w
;
9237 for ( i
= col
; i
< m_numCols
; i
++ )
9239 m_colRights
[i
] += diff
;
9241 if ( !GetBatchCount() )
9246 void wxGrid::SetColMinimalWidth( int col
, int width
)
9248 m_colMinWidths
.Put(col
, width
);
9251 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9253 m_rowMinHeights
.Put(row
, width
);
9256 int wxGrid::GetColMinimalWidth(int col
) const
9258 long value
= m_colMinWidths
.Get(col
);
9259 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
9262 int wxGrid::GetRowMinimalHeight(int row
) const
9264 long value
= m_rowMinHeights
.Get(row
);
9265 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
9268 // ----------------------------------------------------------------------------
9270 // ----------------------------------------------------------------------------
9272 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9274 wxClientDC
dc(m_gridWin
);
9276 // init both of them to avoid compiler warnings, even if weo nly need one
9284 wxCoord extent
, extentMax
= 0;
9285 int max
= column
? m_numRows
: m_numCols
;
9286 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9293 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9294 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9297 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9298 extent
= column
? size
.x
: size
.y
;
9299 if ( extent
> extentMax
)
9310 // now also compare with the column label extent
9312 dc
.SetFont( GetLabelFont() );
9315 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9317 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9319 extent
= column
? w
: h
;
9320 if ( extent
> extentMax
)
9327 // empty column - give default extent (notice that if extentMax is less
9328 // than default extent but != 0, it's ok)
9329 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9335 // leave some space around text
9346 SetColSize(col
, extentMax
);
9347 if ( !GetBatchCount() )
9350 m_gridWin
->GetClientSize( &cw
, &ch
);
9351 wxRect
rect ( CellToRect( 0, col
) );
9353 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9354 rect
.width
= cw
- rect
.x
;
9355 rect
.height
= m_colLabelHeight
;
9356 m_colLabelWin
->Refresh( TRUE
, &rect
);
9361 SetRowSize(row
, extentMax
);
9362 if ( !GetBatchCount() )
9365 m_gridWin
->GetClientSize( &cw
, &ch
);
9366 wxRect
rect ( CellToRect( row
, 0 ) );
9368 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9369 rect
.width
= m_rowLabelWidth
;
9370 rect
.height
= ch
- rect
.y
;
9371 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9377 SetColMinimalWidth(col
, extentMax
);
9379 SetRowMinimalHeight(row
, extentMax
);
9383 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9385 int width
= m_rowLabelWidth
;
9390 for ( int col
= 0; col
< m_numCols
; col
++ )
9394 AutoSizeColumn(col
, setAsMin
);
9397 width
+= GetColWidth(col
);
9406 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9408 int height
= m_colLabelHeight
;
9413 for ( int row
= 0; row
< m_numRows
; row
++ )
9417 AutoSizeRow(row
, setAsMin
);
9420 height
+= GetRowHeight(row
);
9429 void wxGrid::AutoSize()
9433 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9435 // round up the size to a multiple of scroll step - this ensures that we
9436 // won't get the scrollbars if we're sized exactly to this width
9437 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9439 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9440 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9442 // distribute the extra space between the columns/rows to avoid having
9443 // extra white space
9445 // Remove the extra m_extraWidth + 1 added above
9446 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9447 if ( diff
&& m_numCols
)
9449 // try to resize the columns uniformly
9450 wxCoord diffPerCol
= diff
/ m_numCols
;
9453 for ( int col
= 0; col
< m_numCols
; col
++ )
9455 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9459 // add remaining amount to the last columns
9460 diff
-= diffPerCol
* m_numCols
;
9463 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9465 SetColSize(col
, GetColWidth(col
) + 1);
9471 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9472 if ( diff
&& m_numRows
)
9474 // try to resize the columns uniformly
9475 wxCoord diffPerRow
= diff
/ m_numRows
;
9478 for ( int row
= 0; row
< m_numRows
; row
++ )
9480 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9484 // add remaining amount to the last rows
9485 diff
-= diffPerRow
* m_numRows
;
9488 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9490 SetRowSize(row
, GetRowHeight(row
) + 1);
9497 SetClientSize(sizeFit
);
9500 wxSize
wxGrid::DoGetBestSize() const
9502 // don't set sizes, only calculate them
9503 wxGrid
*self
= (wxGrid
*)this; // const_cast
9506 width
= self
->SetOrCalcColumnSizes(TRUE
);
9507 height
= self
->SetOrCalcRowSizes(TRUE
);
9509 int maxwidth
, maxheight
;
9510 wxDisplaySize( & maxwidth
, & maxheight
);
9512 if ( width
> maxwidth
) width
= maxwidth
;
9513 if ( height
> maxheight
) height
= maxheight
;
9515 return wxSize( width
, height
);
9524 wxPen
& wxGrid::GetDividerPen() const
9529 // ----------------------------------------------------------------------------
9530 // cell value accessor functions
9531 // ----------------------------------------------------------------------------
9533 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9537 m_table
->SetValue( row
, col
, s
);
9538 if ( !GetBatchCount() )
9541 wxRect
rect( CellToRect( row
, col
) );
9543 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9544 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9545 m_gridWin
->Refresh( FALSE
, &rect
);
9548 if ( m_currentCellCoords
.GetRow() == row
&&
9549 m_currentCellCoords
.GetCol() == col
&&
9550 IsCellEditControlShown())
9551 // Note: If we are using IsCellEditControlEnabled,
9552 // this interacts badly with calling SetCellValue from
9553 // an EVT_GRID_CELL_CHANGE handler.
9555 HideCellEditControl();
9556 ShowCellEditControl(); // will reread data from table
9563 // ------ Block, row and col selection
9566 void wxGrid::SelectRow( int row
, bool addToSelected
)
9568 if ( IsSelection() && !addToSelected
)
9572 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9576 void wxGrid::SelectCol( int col
, bool addToSelected
)
9578 if ( IsSelection() && !addToSelected
)
9582 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9586 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9587 bool addToSelected
)
9589 if ( IsSelection() && !addToSelected
)
9593 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9594 FALSE
, addToSelected
);
9598 void wxGrid::SelectAll()
9600 if ( m_numRows
> 0 && m_numCols
> 0 )
9603 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9608 // ------ Cell, row and col deselection
9611 void wxGrid::DeselectRow( int row
)
9616 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9618 if ( m_selection
->IsInSelection(row
, 0 ) )
9619 m_selection
->ToggleCellSelection( row
, 0);
9623 int nCols
= GetNumberCols();
9624 for ( int i
= 0; i
< nCols
; i
++ )
9626 if ( m_selection
->IsInSelection(row
, i
) )
9627 m_selection
->ToggleCellSelection( row
, i
);
9632 void wxGrid::DeselectCol( int col
)
9637 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9639 if ( m_selection
->IsInSelection(0, col
) )
9640 m_selection
->ToggleCellSelection( 0, col
);
9644 int nRows
= GetNumberRows();
9645 for ( int i
= 0; i
< nRows
; i
++ )
9647 if ( m_selection
->IsInSelection(i
, col
) )
9648 m_selection
->ToggleCellSelection(i
, col
);
9653 void wxGrid::DeselectCell( int row
, int col
)
9655 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9656 m_selection
->ToggleCellSelection(row
, col
);
9659 bool wxGrid::IsSelection()
9661 return ( m_selection
&& (m_selection
->IsSelection() ||
9662 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9663 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9666 bool wxGrid::IsInSelection( int row
, int col
) const
9668 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9669 ( row
>= m_selectingTopLeft
.GetRow() &&
9670 col
>= m_selectingTopLeft
.GetCol() &&
9671 row
<= m_selectingBottomRight
.GetRow() &&
9672 col
<= m_selectingBottomRight
.GetCol() )) );
9675 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9677 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9678 return m_selection
->m_cellSelection
;
9680 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9682 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9683 return m_selection
->m_blockSelectionTopLeft
;
9685 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9687 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9688 return m_selection
->m_blockSelectionBottomRight
;
9690 wxArrayInt
wxGrid::GetSelectedRows() const
9692 if (!m_selection
) { wxArrayInt a
; return a
; }
9693 return m_selection
->m_rowSelection
;
9695 wxArrayInt
wxGrid::GetSelectedCols() const
9697 if (!m_selection
) { wxArrayInt a
; return a
; }
9698 return m_selection
->m_colSelection
;
9702 void wxGrid::ClearSelection()
9704 m_selectingTopLeft
= wxGridNoCellCoords
;
9705 m_selectingBottomRight
= wxGridNoCellCoords
;
9707 m_selection
->ClearSelection();
9711 // This function returns the rectangle that encloses the given block
9712 // in device coords clipped to the client size of the grid window.
9714 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9715 const wxGridCellCoords
&bottomRight
)
9717 wxRect
rect( wxGridNoCellRect
);
9720 cellRect
= CellToRect( topLeft
);
9721 if ( cellRect
!= wxGridNoCellRect
)
9727 rect
= wxRect( 0, 0, 0, 0 );
9730 cellRect
= CellToRect( bottomRight
);
9731 if ( cellRect
!= wxGridNoCellRect
)
9737 return wxGridNoCellRect
;
9741 int left
= rect
.GetLeft();
9742 int top
= rect
.GetTop();
9743 int right
= rect
.GetRight();
9744 int bottom
= rect
.GetBottom();
9746 int leftCol
= topLeft
.GetCol();
9747 int topRow
= topLeft
.GetRow();
9748 int rightCol
= bottomRight
.GetCol();
9749 int bottomRow
= bottomRight
.GetRow();
9772 for ( j
= topRow
; j
<= bottomRow
; j
++ )
9774 for ( i
= leftCol
; i
<= rightCol
; i
++ )
9776 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
9778 cellRect
= CellToRect( j
, i
);
9780 if (cellRect
.x
< left
)
9782 if (cellRect
.y
< top
)
9784 if (cellRect
.x
+ cellRect
.width
> right
)
9785 right
= cellRect
.x
+ cellRect
.width
;
9786 if (cellRect
.y
+ cellRect
.height
> bottom
)
9787 bottom
= cellRect
.y
+ cellRect
.height
;
9789 else i
= rightCol
; // jump over inner cells.
9793 // convert to scrolled coords
9795 CalcScrolledPosition( left
, top
, &left
, &top
);
9796 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9799 m_gridWin
->GetClientSize( &cw
, &ch
);
9801 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9802 return wxRect( 0, 0, 0, 0);
9804 rect
.SetLeft( wxMax(0, left
) );
9805 rect
.SetTop( wxMax(0, top
) );
9806 rect
.SetRight( wxMin(cw
, right
) );
9807 rect
.SetBottom( wxMin(ch
, bottom
) );
9815 // ------ Grid event classes
9818 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9820 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9821 int row
, int col
, int x
, int y
, bool sel
,
9822 bool control
, bool shift
, bool alt
, bool meta
)
9823 : wxNotifyEvent( type
, id
)
9830 m_control
= control
;
9835 SetEventObject(obj
);
9839 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9841 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9842 int rowOrCol
, int x
, int y
,
9843 bool control
, bool shift
, bool alt
, bool meta
)
9844 : wxNotifyEvent( type
, id
)
9846 m_rowOrCol
= rowOrCol
;
9849 m_control
= control
;
9854 SetEventObject(obj
);
9858 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9860 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9861 const wxGridCellCoords
& topLeft
,
9862 const wxGridCellCoords
& bottomRight
,
9863 bool sel
, bool control
,
9864 bool shift
, bool alt
, bool meta
)
9865 : wxNotifyEvent( type
, id
)
9867 m_topLeft
= topLeft
;
9868 m_bottomRight
= bottomRight
;
9870 m_control
= control
;
9875 SetEventObject(obj
);
9879 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9881 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9882 wxObject
* obj
, int row
,
9883 int col
, wxControl
* ctrl
)
9884 : wxCommandEvent(type
, id
)
9886 SetEventObject(obj
);
9893 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9895 #endif // wxUSE_GRID