1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
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"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
44 #include "wx/combobox.h"
45 #include "wx/valtext.h"
48 #include "wx/textfile.h"
49 #include "wx/spinctrl.h"
50 #include "wx/tokenzr.h"
53 #include "wx/generic/gridsel.h"
55 #if defined(__WXMOTIF__)
56 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
58 #define WXUNUSED_MOTIF(identifier) identifier
61 #if defined(__WXGTK__)
62 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
64 #define WXUNUSED_GTK(identifier) identifier
67 // Required for wxIs... functions
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
76 struct wxGridCellWithAttr
78 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
79 : coords(row
, col
), attr(attr_
)
88 wxGridCellCoords coords
;
92 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
94 #include "wx/arrimpl.cpp"
96 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
97 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
126 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
127 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
128 const wxPoint
&pos
, const wxSize
&size
);
133 void OnPaint( wxPaintEvent
& event
);
134 void OnMouseEvent( wxMouseEvent
& event
);
135 void OnKeyDown( wxKeyEvent
& event
);
136 void OnKeyUp( wxKeyEvent
& );
138 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
139 DECLARE_EVENT_TABLE()
143 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
146 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
147 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
148 const wxPoint
&pos
, const wxSize
&size
);
153 void OnPaint( wxPaintEvent
&event
);
154 void OnMouseEvent( wxMouseEvent
& event
);
155 void OnKeyDown( wxKeyEvent
& event
);
156 void OnKeyUp( wxKeyEvent
& );
158 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
159 DECLARE_EVENT_TABLE()
163 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
166 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
167 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
168 const wxPoint
&pos
, const wxSize
&size
);
173 void OnMouseEvent( wxMouseEvent
& event
);
174 void OnKeyDown( wxKeyEvent
& event
);
175 void OnKeyUp( wxKeyEvent
& );
176 void OnPaint( wxPaintEvent
& event
);
178 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
179 DECLARE_EVENT_TABLE()
182 class WXDLLEXPORT wxGridWindow
: public wxPanel
187 m_owner
= (wxGrid
*)NULL
;
188 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
189 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
192 wxGridWindow( wxGrid
*parent
,
193 wxGridRowLabelWindow
*rowLblWin
,
194 wxGridColLabelWindow
*colLblWin
,
195 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
198 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
202 wxGridRowLabelWindow
*m_rowLabelWin
;
203 wxGridColLabelWindow
*m_colLabelWin
;
205 void OnPaint( wxPaintEvent
&event
);
206 void OnMouseEvent( wxMouseEvent
& event
);
207 void OnKeyDown( wxKeyEvent
& );
208 void OnKeyUp( wxKeyEvent
& );
209 void OnEraseBackground( wxEraseEvent
& );
212 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
213 DECLARE_EVENT_TABLE()
218 class wxGridCellEditorEvtHandler
: public wxEvtHandler
221 wxGridCellEditorEvtHandler()
222 : m_grid(0), m_editor(0)
224 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
225 : m_grid(grid
), m_editor(editor
)
228 void OnKeyDown(wxKeyEvent
& event
);
229 void OnChar(wxKeyEvent
& event
);
233 wxGridCellEditor
* m_editor
;
234 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
235 DECLARE_EVENT_TABLE()
239 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
240 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
241 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
242 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
247 // ----------------------------------------------------------------------------
248 // the internal data representation used by wxGridCellAttrProvider
249 // ----------------------------------------------------------------------------
251 // this class stores attributes set for cells
252 class WXDLLEXPORT wxGridCellAttrData
255 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
256 wxGridCellAttr
*GetAttr(int row
, int col
) const;
257 void UpdateAttrRows( size_t pos
, int numRows
);
258 void UpdateAttrCols( size_t pos
, int numCols
);
261 // searches for the attr for given cell, returns wxNOT_FOUND if not found
262 int FindIndex(int row
, int col
) const;
264 wxGridCellWithAttrArray m_attrs
;
267 // this class stores attributes set for rows or columns
268 class WXDLLEXPORT wxGridRowOrColAttrData
271 // empty ctor to suppress warnings
272 wxGridRowOrColAttrData() { }
273 ~wxGridRowOrColAttrData();
275 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
276 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
277 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
280 wxArrayInt m_rowsOrCols
;
281 wxArrayAttrs m_attrs
;
284 // NB: this is just a wrapper around 3 objects: one which stores cell
285 // attributes, and 2 others for row/col ones
286 class WXDLLEXPORT wxGridCellAttrProviderData
289 wxGridCellAttrData m_cellAttrs
;
290 wxGridRowOrColAttrData m_rowAttrs
,
295 // ----------------------------------------------------------------------------
296 // data structures used for the data type registry
297 // ----------------------------------------------------------------------------
299 struct wxGridDataTypeInfo
301 wxGridDataTypeInfo(const wxString
& typeName
,
302 wxGridCellRenderer
* renderer
,
303 wxGridCellEditor
* editor
)
304 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
307 ~wxGridDataTypeInfo()
309 wxSafeDecRef(m_renderer
);
310 wxSafeDecRef(m_editor
);
314 wxGridCellRenderer
* m_renderer
;
315 wxGridCellEditor
* m_editor
;
319 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
322 class WXDLLEXPORT wxGridTypeRegistry
325 wxGridTypeRegistry() {}
326 ~wxGridTypeRegistry();
328 void RegisterDataType(const wxString
& typeName
,
329 wxGridCellRenderer
* renderer
,
330 wxGridCellEditor
* editor
);
332 // find one of already registered data types
333 int FindRegisteredDataType(const wxString
& typeName
);
335 // try to FindRegisteredDataType(), if this fails and typeName is one of
336 // standard typenames, register it and return its index
337 int FindDataType(const wxString
& typeName
);
339 // try to FindDataType(), if it fails see if it is not one of already
340 // registered data types with some params in which case clone the
341 // registered data type and set params for it
342 int FindOrCloneDataType(const wxString
& typeName
);
344 wxGridCellRenderer
* GetRenderer(int index
);
345 wxGridCellEditor
* GetEditor(int index
);
348 wxGridDataTypeInfoArray m_typeinfo
;
351 // ----------------------------------------------------------------------------
352 // conditional compilation
353 // ----------------------------------------------------------------------------
355 #ifndef WXGRID_DRAW_LINES
356 #define WXGRID_DRAW_LINES 1
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
363 //#define DEBUG_ATTR_CACHE
364 #ifdef DEBUG_ATTR_CACHE
365 static size_t gs_nAttrCacheHits
= 0;
366 static size_t gs_nAttrCacheMisses
= 0;
367 #endif // DEBUG_ATTR_CACHE
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
374 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
377 // TODO: this doesn't work at all, grid cells have different sizes and approx
378 // calculations don't work as because of the size mismatch scrollbars
379 // sometimes fail to be shown when they should be or vice versa
380 static const size_t GRID_SCROLL_LINE
= 1;
382 // the size of hash tables used a bit everywhere (the max number of elements
383 // in these hash tables is the number of rows/columns)
384 static const int GRID_HASH_SIZE
= 100;
386 // ============================================================================
388 // ============================================================================
390 // ----------------------------------------------------------------------------
392 // ----------------------------------------------------------------------------
394 wxGridCellEditor::wxGridCellEditor()
400 wxGridCellEditor::~wxGridCellEditor()
405 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
406 wxWindowID
WXUNUSED(id
),
407 wxEvtHandler
* evtHandler
)
410 m_control
->PushEventHandler(evtHandler
);
413 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
414 wxGridCellAttr
*attr
)
416 // erase the background because we might not fill the cell
417 wxClientDC
dc(m_control
->GetParent());
418 dc
.SetPen(*wxTRANSPARENT_PEN
);
419 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
420 dc
.DrawRectangle(rectCell
);
422 // redraw the control we just painted over
423 m_control
->Refresh();
426 void wxGridCellEditor::Destroy()
430 m_control
->PopEventHandler(TRUE
/* delete it*/);
432 m_control
->Destroy();
437 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
439 wxASSERT_MSG(m_control
,
440 wxT("The wxGridCellEditor must be Created first!"));
441 m_control
->Show(show
);
445 // set the colours/fonts if we have any
448 m_colFgOld
= m_control
->GetForegroundColour();
449 m_control
->SetForegroundColour(attr
->GetTextColour());
451 m_colBgOld
= m_control
->GetBackgroundColour();
452 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
454 m_fontOld
= m_control
->GetFont();
455 m_control
->SetFont(attr
->GetFont());
457 // can't do anything more in the base class version, the other
458 // attributes may only be used by the derived classes
463 // restore the standard colours fonts
464 if ( m_colFgOld
.Ok() )
466 m_control
->SetForegroundColour(m_colFgOld
);
467 m_colFgOld
= wxNullColour
;
470 if ( m_colBgOld
.Ok() )
472 m_control
->SetBackgroundColour(m_colBgOld
);
473 m_colBgOld
= wxNullColour
;
476 if ( m_fontOld
.Ok() )
478 m_control
->SetFont(m_fontOld
);
479 m_fontOld
= wxNullFont
;
484 void wxGridCellEditor::SetSize(const wxRect
& rect
)
486 wxASSERT_MSG(m_control
,
487 wxT("The wxGridCellEditor must be Created first!"));
488 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
491 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
496 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
498 // accept the simple key presses, not anything with Ctrl/Alt/Meta
499 return !(event
.ControlDown() || event
.AltDown());
502 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
507 void wxGridCellEditor::StartingClick()
511 // ----------------------------------------------------------------------------
512 // wxGridCellTextEditor
513 // ----------------------------------------------------------------------------
515 wxGridCellTextEditor::wxGridCellTextEditor()
520 void wxGridCellTextEditor::Create(wxWindow
* parent
,
522 wxEvtHandler
* evtHandler
)
524 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
525 wxDefaultPosition
, wxDefaultSize
526 #if defined(__WXMSW__)
527 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
528 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
532 // TODO: use m_maxChars
534 wxGridCellEditor::Create(parent
, id
, evtHandler
);
537 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
538 wxGridCellAttr
* WXUNUSED(attr
))
540 // as we fill the entire client area, don't do anything here to minimize
544 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
546 wxRect
rect(rectOrig
);
548 // Make the edit control large enough to allow for internal
551 // TODO: remove this if the text ctrl sizing is improved esp. for
554 #if defined(__WXGTK__)
563 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
565 // MB: treat MSW separately here otherwise the caret doesn't show
566 // when the editor is in the first row.
567 #if defined(__WXMSW__)
570 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
573 #if defined(__WXMOTIF__)
577 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
578 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
579 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
580 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
583 wxGridCellEditor::SetSize(rect
);
586 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
588 wxASSERT_MSG(m_control
,
589 wxT("The wxGridCellEditor must be Created first!"));
591 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
593 DoBeginEdit(m_startValue
);
596 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
598 Text()->SetValue(startValue
);
599 Text()->SetInsertionPointEnd();
603 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
606 wxASSERT_MSG(m_control
,
607 wxT("The wxGridCellEditor must be Created first!"));
609 bool changed
= FALSE
;
610 wxString value
= Text()->GetValue();
611 if (value
!= m_startValue
)
615 grid
->GetTable()->SetValue(row
, col
, value
);
617 m_startValue
= wxEmptyString
;
618 Text()->SetValue(m_startValue
);
624 void wxGridCellTextEditor::Reset()
626 wxASSERT_MSG(m_control
,
627 wxT("The wxGridCellEditor must be Created first!"));
629 DoReset(m_startValue
);
632 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
634 Text()->SetValue(startValue
);
635 Text()->SetInsertionPointEnd();
638 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
640 if ( wxGridCellEditor::IsAcceptedKey(event
) )
642 int keycode
= event
.GetKeyCode();
656 case WXK_NUMPAD_MULTIPLY
:
660 case WXK_NUMPAD_SUBTRACT
:
662 case WXK_NUMPAD_DECIMAL
:
664 case WXK_NUMPAD_DIVIDE
:
668 // accept 8 bit chars too if isprint() agrees
669 if ( (keycode
< 255) && (isprint(keycode
)) )
677 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
679 // we don't check for !HasModifiers() because IsAcceptedKey() did it
681 // insert the key in the control
683 int keycode
= event
.GetKeyCode();
696 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
700 case WXK_NUMPAD_MULTIPLY
:
710 case WXK_NUMPAD_SUBTRACT
:
715 case WXK_NUMPAD_DECIMAL
:
720 case WXK_NUMPAD_DIVIDE
:
725 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
727 // FIXME this is not going to work for non letters...
728 if ( !event
.ShiftDown() )
730 keycode
= tolower(keycode
);
733 ch
= (wxChar
)keycode
;
743 Text()->AppendText(ch
);
751 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
752 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
754 #if defined(__WXMOTIF__) || defined(__WXGTK__)
755 // wxMotif needs a little extra help...
756 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
757 wxString
s( Text()->GetValue() );
758 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
760 Text()->SetInsertionPoint( pos
);
762 // the other ports can handle a Return key press
768 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
778 if ( !params
.ToLong(&tmp
) )
780 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
784 m_maxChars
= (size_t)tmp
;
789 // ----------------------------------------------------------------------------
790 // wxGridCellNumberEditor
791 // ----------------------------------------------------------------------------
793 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
799 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
801 wxEvtHandler
* evtHandler
)
805 // create a spin ctrl
806 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
807 wxDefaultPosition
, wxDefaultSize
,
811 wxGridCellEditor::Create(parent
, id
, evtHandler
);
815 // just a text control
816 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
819 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
820 #endif // wxUSE_VALIDATORS
824 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
826 // first get the value
827 wxGridTableBase
*table
= grid
->GetTable();
828 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
830 m_valueOld
= table
->GetValueAsLong(row
, col
);
834 wxString sValue
= table
->GetValue(row
, col
);
835 if (! sValue
.ToLong(&m_valueOld
))
837 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
844 Spin()->SetValue((int)m_valueOld
);
849 DoBeginEdit(GetString());
853 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
861 value
= Spin()->GetValue();
862 changed
= value
!= m_valueOld
;
866 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
871 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
872 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
874 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%ld"), value
));
880 void wxGridCellNumberEditor::Reset()
884 Spin()->SetValue((int)m_valueOld
);
888 DoReset(GetString());
892 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
894 if ( wxGridCellEditor::IsAcceptedKey(event
) )
896 int keycode
= event
.GetKeyCode();
912 case WXK_NUMPAD_SUBTRACT
:
918 if ( (keycode
< 128) && isdigit(keycode
) )
926 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
930 int keycode
= (int) event
.KeyCode();
931 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
933 wxGridCellTextEditor::StartingKey(event
);
943 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
954 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
958 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
962 // skip the error message below
967 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
971 // ----------------------------------------------------------------------------
972 // wxGridCellFloatEditor
973 // ----------------------------------------------------------------------------
975 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
978 m_precision
= precision
;
981 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
983 wxEvtHandler
* evtHandler
)
985 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
988 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
989 #endif // wxUSE_VALIDATORS
992 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
994 // first get the value
995 wxGridTableBase
*table
= grid
->GetTable();
996 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
998 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1002 wxString sValue
= table
->GetValue(row
, col
);
1003 if (! sValue
.ToDouble(&m_valueOld
))
1005 wxFAIL_MSG( _T("this cell doesn't have float value") );
1010 DoBeginEdit(GetString());
1013 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1017 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
1019 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1020 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1022 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%f"), value
));
1032 void wxGridCellFloatEditor::Reset()
1034 DoReset(GetString());
1037 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1039 int keycode
= (int)event
.KeyCode();
1040 if ( isdigit(keycode
) ||
1041 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1043 wxGridCellTextEditor::StartingKey(event
);
1045 // skip Skip() below
1052 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1063 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1067 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1069 m_precision
= (int)tmp
;
1071 // skip the error message below
1076 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1080 wxString
wxGridCellFloatEditor::GetString() const
1083 if ( m_width
== -1 )
1085 // default width/precision
1088 else if ( m_precision
== -1 )
1090 // default precision
1091 fmt
.Printf(_T("%%%d.g"), m_width
);
1095 fmt
.Printf(_T("%%%d.%dg"), m_width
, m_precision
);
1098 return wxString::Format(fmt
, m_valueOld
);
1101 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1103 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1105 int keycode
= event
.GetKeyCode();
1119 case WXK_NUMPAD_ADD
:
1121 case WXK_NUMPAD_SUBTRACT
:
1123 case WXK_NUMPAD_DECIMAL
:
1127 // additionally accept 'e' as in '1e+6'
1128 if ( (keycode
< 128) &&
1129 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1137 // ----------------------------------------------------------------------------
1138 // wxGridCellBoolEditor
1139 // ----------------------------------------------------------------------------
1141 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1143 wxEvtHandler
* evtHandler
)
1145 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1146 wxDefaultPosition
, wxDefaultSize
,
1149 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1152 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1154 bool resize
= FALSE
;
1155 wxSize size
= m_control
->GetSize();
1156 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1158 // check if the checkbox is not too big/small for this cell
1159 wxSize sizeBest
= m_control
->GetBestSize();
1160 if ( !(size
== sizeBest
) )
1162 // reset to default size if it had been made smaller
1168 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1170 // leave 1 pixel margin
1171 size
.x
= size
.y
= minSize
- 2;
1178 m_control
->SetSize(size
);
1181 // position it in the centre of the rectangle (TODO: support alignment?)
1183 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1184 // the checkbox without label still has some space to the right in wxGTK,
1185 // so shift it to the right
1187 #elif defined(__WXMSW__)
1188 // here too, but in other way
1193 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1196 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1198 m_control
->Show(show
);
1202 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1203 CBox()->SetBackgroundColour(colBg
);
1207 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1209 wxASSERT_MSG(m_control
,
1210 wxT("The wxGridCellEditor must be Created first!"));
1212 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1213 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1216 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1217 m_startValue
= !( !cellval
|| (cellval
== "0") );
1219 CBox()->SetValue(m_startValue
);
1223 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1226 wxASSERT_MSG(m_control
,
1227 wxT("The wxGridCellEditor must be Created first!"));
1229 bool changed
= FALSE
;
1230 bool value
= CBox()->GetValue();
1231 if ( value
!= m_startValue
)
1236 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1237 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1239 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1245 void wxGridCellBoolEditor::Reset()
1247 wxASSERT_MSG(m_control
,
1248 wxT("The wxGridCellEditor must be Created first!"));
1250 CBox()->SetValue(m_startValue
);
1253 void wxGridCellBoolEditor::StartingClick()
1255 CBox()->SetValue(!CBox()->GetValue());
1258 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1260 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1262 int keycode
= event
.GetKeyCode();
1266 case WXK_NUMPAD_MULTIPLY
:
1268 case WXK_NUMPAD_ADD
:
1270 case WXK_NUMPAD_SUBTRACT
:
1281 // ----------------------------------------------------------------------------
1282 // wxGridCellChoiceEditor
1283 // ----------------------------------------------------------------------------
1285 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1286 const wxString choices
[],
1288 : m_allowOthers(allowOthers
)
1292 m_choices
.Alloc(count
);
1293 for ( size_t n
= 0; n
< count
; n
++ )
1295 m_choices
.Add(choices
[n
]);
1300 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1302 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1303 editor
->m_allowOthers
= m_allowOthers
;
1304 editor
->m_choices
= m_choices
;
1309 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1311 wxEvtHandler
* evtHandler
)
1313 size_t count
= m_choices
.GetCount();
1314 wxString
*choices
= new wxString
[count
];
1315 for ( size_t n
= 0; n
< count
; n
++ )
1317 choices
[n
] = m_choices
[n
];
1320 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1321 wxDefaultPosition
, wxDefaultSize
,
1323 m_allowOthers
? 0 : wxCB_READONLY
);
1327 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1330 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1331 wxGridCellAttr
* attr
)
1333 // as we fill the entire client area, don't do anything here to minimize
1336 // TODO: It doesn't actually fill the client area since the height of a
1337 // combo always defaults to the standard... Until someone has time to
1338 // figure out the right rectangle to paint, just do it the normal way...
1339 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1342 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1344 wxASSERT_MSG(m_control
,
1345 wxT("The wxGridCellEditor must be Created first!"));
1347 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1349 Combo()->SetValue(m_startValue
);
1350 size_t count
= m_choices
.GetCount();
1351 for (size_t i
=0; i
<count
; i
++)
1353 if (m_startValue
== m_choices
[i
])
1355 Combo()->SetSelection(i
);
1359 Combo()->SetInsertionPointEnd();
1360 Combo()->SetFocus();
1363 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1366 wxString value
= Combo()->GetValue();
1367 bool changed
= value
!= m_startValue
;
1370 grid
->GetTable()->SetValue(row
, col
, value
);
1372 m_startValue
= wxEmptyString
;
1373 Combo()->SetValue(m_startValue
);
1378 void wxGridCellChoiceEditor::Reset()
1380 Combo()->SetValue(m_startValue
);
1381 Combo()->SetInsertionPointEnd();
1384 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1394 wxStringTokenizer
tk(params
, _T(','));
1395 while ( tk
.HasMoreTokens() )
1397 m_choices
.Add(tk
.GetNextToken());
1401 // ----------------------------------------------------------------------------
1402 // wxGridCellEditorEvtHandler
1403 // ----------------------------------------------------------------------------
1405 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1407 switch ( event
.KeyCode() )
1411 m_grid
->DisableCellEditControl();
1415 event
.Skip( m_grid
->GetEventHandler()->ProcessEvent( event
) );
1419 case WXK_NUMPAD_ENTER
:
1420 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1421 m_editor
->HandleReturn(event
);
1430 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1432 switch ( event
.KeyCode() )
1437 case WXK_NUMPAD_ENTER
:
1445 // ----------------------------------------------------------------------------
1446 // wxGridCellWorker is an (almost) empty common base class for
1447 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1448 // ----------------------------------------------------------------------------
1450 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1455 wxGridCellWorker::~wxGridCellWorker()
1459 // ============================================================================
1461 // ============================================================================
1463 // ----------------------------------------------------------------------------
1464 // wxGridCellRenderer
1465 // ----------------------------------------------------------------------------
1467 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1468 wxGridCellAttr
& attr
,
1471 int WXUNUSED(row
), int WXUNUSED(col
),
1474 dc
.SetBackgroundMode( wxSOLID
);
1478 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1482 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1485 dc
.SetPen( *wxTRANSPARENT_PEN
);
1486 dc
.DrawRectangle(rect
);
1489 // ----------------------------------------------------------------------------
1490 // wxGridCellStringRenderer
1491 // ----------------------------------------------------------------------------
1493 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1494 wxGridCellAttr
& attr
,
1498 dc
.SetBackgroundMode( wxTRANSPARENT
);
1500 // TODO some special colours for attr.IsReadOnly() case?
1504 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1505 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1509 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1510 dc
.SetTextForeground( attr
.GetTextColour() );
1513 dc
.SetFont( attr
.GetFont() );
1516 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1518 const wxString
& text
)
1520 wxCoord x
= 0, y
= 0, max_x
= 0;
1521 dc
.SetFont(attr
.GetFont());
1522 wxStringTokenizer
tk(text
, _T('\n'));
1523 while ( tk
.HasMoreTokens() )
1525 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1526 max_x
= wxMax(max_x
, x
);
1529 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1531 return wxSize(max_x
, y
);
1534 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1535 wxGridCellAttr
& attr
,
1539 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1542 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1543 wxGridCellAttr
& attr
,
1545 const wxRect
& rectCell
,
1549 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1551 // now we only have to draw the text
1552 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1555 attr
.GetAlignment(&hAlign
, &vAlign
);
1557 wxRect rect
= rectCell
;
1560 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1561 rect
, hAlign
, vAlign
);
1564 // ----------------------------------------------------------------------------
1565 // wxGridCellNumberRenderer
1566 // ----------------------------------------------------------------------------
1568 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1570 wxGridTableBase
*table
= grid
.GetTable();
1572 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1574 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1578 text
= table
->GetValue(row
, col
);
1584 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1585 wxGridCellAttr
& attr
,
1587 const wxRect
& rectCell
,
1591 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1593 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1595 // draw the text right aligned by default
1597 attr
.GetAlignment(&hAlign
, &vAlign
);
1598 hAlign
= wxALIGN_RIGHT
;
1600 wxRect rect
= rectCell
;
1603 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1606 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1607 wxGridCellAttr
& attr
,
1611 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1614 // ----------------------------------------------------------------------------
1615 // wxGridCellFloatRenderer
1616 // ----------------------------------------------------------------------------
1618 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1621 SetPrecision(precision
);
1624 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1626 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1627 renderer
->m_width
= m_width
;
1628 renderer
->m_precision
= m_precision
;
1629 renderer
->m_format
= m_format
;
1634 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1636 wxGridTableBase
*table
= grid
.GetTable();
1641 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1643 val
= table
->GetValueAsDouble(row
, col
);
1648 text
= table
->GetValue(row
, col
);
1649 hasDouble
= text
.ToDouble(&val
);
1656 if ( m_width
== -1 )
1658 // default width/precision
1659 m_format
= _T("%f");
1661 else if ( m_precision
== -1 )
1663 // default precision
1664 m_format
.Printf(_T("%%%d.f"), m_width
);
1668 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1672 text
.Printf(m_format
, val
);
1674 //else: text already contains the string
1679 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1680 wxGridCellAttr
& attr
,
1682 const wxRect
& rectCell
,
1686 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1688 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1690 // draw the text right aligned by default
1692 attr
.GetAlignment(&hAlign
, &vAlign
);
1693 hAlign
= wxALIGN_RIGHT
;
1695 wxRect rect
= rectCell
;
1698 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1701 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1702 wxGridCellAttr
& attr
,
1706 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1709 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1715 // reset to defaults
1721 wxString tmp
= params
.BeforeFirst(_T(','));
1725 if ( !tmp
.ToLong(&width
) )
1731 SetWidth((int)width
);
1733 tmp
= params
.AfterFirst(_T(','));
1737 if ( !tmp
.ToLong(&precision
) )
1743 SetPrecision((int)precision
);
1751 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string '%s ignored"), params
.c_str());
1756 // ----------------------------------------------------------------------------
1757 // wxGridCellBoolRenderer
1758 // ----------------------------------------------------------------------------
1760 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1762 // FIXME these checkbox size calculations are really ugly...
1764 // between checkmark and box
1765 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1767 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1768 wxGridCellAttr
& WXUNUSED(attr
),
1773 // compute it only once (no locks for MT safeness in GUI thread...)
1774 if ( !ms_sizeCheckMark
.x
)
1776 // get checkbox size
1777 wxCoord checkSize
= 0;
1778 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1779 wxSize size
= checkbox
->GetBestSize();
1780 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1782 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1783 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1784 checkSize
-= size
.y
/ 2;
1789 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1792 return ms_sizeCheckMark
;
1795 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1796 wxGridCellAttr
& attr
,
1802 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1804 // draw a check mark in the centre (ignoring alignment - TODO)
1805 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1807 // don't draw outside the cell
1808 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1809 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1811 // and even leave (at least) 1 pixel margin
1812 size
.x
= size
.y
= minSize
- 2;
1815 // draw a border around checkmark
1817 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1818 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1819 rectBorder
.width
= size
.x
;
1820 rectBorder
.height
= size
.y
;
1823 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1824 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1827 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1828 value
= !( !cellval
|| (cellval
== "0") );
1833 wxRect rectMark
= rectBorder
;
1835 // MSW DrawCheckMark() is weird (and should probably be changed...)
1836 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1840 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1843 dc
.SetTextForeground(attr
.GetTextColour());
1844 dc
.DrawCheckMark(rectMark
);
1847 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1848 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1849 dc
.DrawRectangle(rectBorder
);
1852 // ----------------------------------------------------------------------------
1854 // ----------------------------------------------------------------------------
1856 wxGridCellAttr
*wxGridCellAttr::Clone() const
1858 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1859 if ( HasTextColour() )
1860 attr
->SetTextColour(GetTextColour());
1861 if ( HasBackgroundColour() )
1862 attr
->SetBackgroundColour(GetBackgroundColour());
1864 attr
->SetFont(GetFont());
1865 if ( HasAlignment() )
1866 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1870 attr
->SetRenderer(m_renderer
);
1871 m_renderer
->IncRef();
1875 attr
->SetEditor(m_editor
);
1880 attr
->SetReadOnly();
1882 attr
->SetDefAttr(m_defGridAttr
);
1887 const wxColour
& wxGridCellAttr::GetTextColour() const
1889 if (HasTextColour())
1893 else if (m_defGridAttr
!= this)
1895 return m_defGridAttr
->GetTextColour();
1899 wxFAIL_MSG(wxT("Missing default cell attribute"));
1900 return wxNullColour
;
1905 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1907 if (HasBackgroundColour())
1909 else if (m_defGridAttr
!= this)
1910 return m_defGridAttr
->GetBackgroundColour();
1913 wxFAIL_MSG(wxT("Missing default cell attribute"));
1914 return wxNullColour
;
1919 const wxFont
& wxGridCellAttr::GetFont() const
1923 else if (m_defGridAttr
!= this)
1924 return m_defGridAttr
->GetFont();
1927 wxFAIL_MSG(wxT("Missing default cell attribute"));
1933 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1937 if ( hAlign
) *hAlign
= m_hAlign
;
1938 if ( vAlign
) *vAlign
= m_vAlign
;
1940 else if (m_defGridAttr
!= this)
1941 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1944 wxFAIL_MSG(wxT("Missing default cell attribute"));
1949 // GetRenderer and GetEditor use a slightly different decision path about
1950 // which attribute to use. If a non-default attr object has one then it is
1951 // used, otherwise the default editor or renderer is fetched from the grid and
1952 // used. It should be the default for the data type of the cell. If it is
1953 // NULL (because the table has a type that the grid does not have in its
1954 // registry,) then the grid's default editor or renderer is used.
1956 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1958 wxGridCellRenderer
* renderer
= NULL
;
1960 if ( m_defGridAttr
!= this || grid
== NULL
)
1962 renderer
= m_renderer
; // use local attribute
1967 if ( !renderer
&& grid
) // get renderer for the data type
1969 // GetDefaultRendererForCell() will do IncRef() for us
1970 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1975 // if we still don't have one then use the grid default
1976 // (no need for IncRef() here neither)
1977 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1982 wxFAIL_MSG(wxT("Missing default cell attribute"));
1988 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1990 wxGridCellEditor
* editor
= NULL
;
1992 if ( m_defGridAttr
!= this || grid
== NULL
)
1994 editor
= m_editor
; // use local attribute
1999 if ( !editor
&& grid
) // get renderer for the data type
2000 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2003 // if we still don't have one then use the grid default
2004 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
2008 wxFAIL_MSG(wxT("Missing default cell attribute"));
2014 // ----------------------------------------------------------------------------
2015 // wxGridCellAttrData
2016 // ----------------------------------------------------------------------------
2018 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2020 int n
= FindIndex(row
, col
);
2021 if ( n
== wxNOT_FOUND
)
2023 // add the attribute
2024 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2030 // change the attribute
2031 m_attrs
[(size_t)n
].attr
= attr
;
2035 // remove this attribute
2036 m_attrs
.RemoveAt((size_t)n
);
2041 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2043 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2045 int n
= FindIndex(row
, col
);
2046 if ( n
!= wxNOT_FOUND
)
2048 attr
= m_attrs
[(size_t)n
].attr
;
2055 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2057 size_t count
= m_attrs
.GetCount();
2058 for ( size_t n
= 0; n
< count
; n
++ )
2060 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2061 wxCoord row
= coords
.GetRow();
2062 if ((size_t)row
>= pos
)
2066 // If rows inserted, include row counter where necessary
2067 coords
.SetRow(row
+ numRows
);
2069 else if (numRows
< 0)
2071 // If rows deleted ...
2072 if ((size_t)row
>= pos
- numRows
)
2074 // ...either decrement row counter (if row still exists)...
2075 coords
.SetRow(row
+ numRows
);
2079 // ...or remove the attribute
2080 m_attrs
.RemoveAt((size_t)n
);
2088 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2090 size_t count
= m_attrs
.GetCount();
2091 for ( size_t n
= 0; n
< count
; n
++ )
2093 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2094 wxCoord col
= coords
.GetCol();
2095 if ( (size_t)col
>= pos
)
2099 // If rows inserted, include row counter where necessary
2100 coords
.SetCol(col
+ numCols
);
2102 else if (numCols
< 0)
2104 // If rows deleted ...
2105 if ((size_t)col
>= pos
- numCols
)
2107 // ...either decrement row counter (if row still exists)...
2108 coords
.SetCol(col
+ numCols
);
2112 // ...or remove the attribute
2113 m_attrs
.RemoveAt((size_t)n
);
2121 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2123 size_t count
= m_attrs
.GetCount();
2124 for ( size_t n
= 0; n
< count
; n
++ )
2126 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2127 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2136 // ----------------------------------------------------------------------------
2137 // wxGridRowOrColAttrData
2138 // ----------------------------------------------------------------------------
2140 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2142 size_t count
= m_attrs
.Count();
2143 for ( size_t n
= 0; n
< count
; n
++ )
2145 m_attrs
[n
]->DecRef();
2149 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2151 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2153 int n
= m_rowsOrCols
.Index(rowOrCol
);
2154 if ( n
!= wxNOT_FOUND
)
2156 attr
= m_attrs
[(size_t)n
];
2163 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2165 int i
= m_rowsOrCols
.Index(rowOrCol
);
2166 if ( i
== wxNOT_FOUND
)
2168 // add the attribute
2169 m_rowsOrCols
.Add(rowOrCol
);
2174 size_t n
= (size_t)i
;
2177 // change the attribute
2178 m_attrs
[n
]->DecRef();
2183 // remove this attribute
2184 m_attrs
[n
]->DecRef();
2185 m_rowsOrCols
.RemoveAt(n
);
2186 m_attrs
.RemoveAt(n
);
2191 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2193 size_t count
= m_attrs
.GetCount();
2194 for ( size_t n
= 0; n
< count
; n
++ )
2196 int & rowOrCol
= m_rowsOrCols
[n
];
2197 if ( (size_t)rowOrCol
>= pos
)
2199 if ( numRowsOrCols
> 0 )
2201 // If rows inserted, include row counter where necessary
2202 rowOrCol
+= numRowsOrCols
;
2204 else if ( numRowsOrCols
< 0)
2206 // If rows deleted, either decrement row counter (if row still exists)
2207 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2208 rowOrCol
+= numRowsOrCols
;
2211 m_rowsOrCols
.RemoveAt((size_t)n
);
2212 m_attrs
.RemoveAt((size_t)n
);
2220 // ----------------------------------------------------------------------------
2221 // wxGridCellAttrProvider
2222 // ----------------------------------------------------------------------------
2224 wxGridCellAttrProvider::wxGridCellAttrProvider()
2226 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2229 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2234 void wxGridCellAttrProvider::InitData()
2236 m_data
= new wxGridCellAttrProviderData
;
2239 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
2241 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2244 // first look for the attribute of this specific cell
2245 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2249 // then look for the col attr (col attributes are more common than
2250 // the row ones, hence they have priority)
2251 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2256 // finally try the row attributes
2257 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2264 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2270 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2273 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2278 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2281 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2286 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2289 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2293 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2295 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2299 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2303 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2305 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2309 // ----------------------------------------------------------------------------
2310 // wxGridTypeRegistry
2311 // ----------------------------------------------------------------------------
2313 wxGridTypeRegistry::~wxGridTypeRegistry()
2315 size_t count
= m_typeinfo
.Count();
2316 for ( size_t i
= 0; i
< count
; i
++ )
2317 delete m_typeinfo
[i
];
2321 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2322 wxGridCellRenderer
* renderer
,
2323 wxGridCellEditor
* editor
)
2325 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2327 // is it already registered?
2328 int loc
= FindRegisteredDataType(typeName
);
2329 if ( loc
!= wxNOT_FOUND
)
2331 delete m_typeinfo
[loc
];
2332 m_typeinfo
[loc
] = info
;
2336 m_typeinfo
.Add(info
);
2340 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2342 size_t count
= m_typeinfo
.GetCount();
2343 for ( size_t i
= 0; i
< count
; i
++ )
2345 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2354 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2356 int index
= FindRegisteredDataType(typeName
);
2357 if ( index
== wxNOT_FOUND
)
2359 // check whether this is one of the standard ones, in which case
2360 // register it "on the fly"
2361 if ( typeName
== wxGRID_VALUE_STRING
)
2363 RegisterDataType(wxGRID_VALUE_STRING
,
2364 new wxGridCellStringRenderer
,
2365 new wxGridCellTextEditor
);
2367 else if ( typeName
== wxGRID_VALUE_BOOL
)
2369 RegisterDataType(wxGRID_VALUE_BOOL
,
2370 new wxGridCellBoolRenderer
,
2371 new wxGridCellBoolEditor
);
2373 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2375 RegisterDataType(wxGRID_VALUE_NUMBER
,
2376 new wxGridCellNumberRenderer
,
2377 new wxGridCellNumberEditor
);
2379 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2381 RegisterDataType(wxGRID_VALUE_FLOAT
,
2382 new wxGridCellFloatRenderer
,
2383 new wxGridCellFloatEditor
);
2385 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2387 RegisterDataType(wxGRID_VALUE_CHOICE
,
2388 new wxGridCellStringRenderer
,
2389 new wxGridCellChoiceEditor
);
2396 // we get here only if just added the entry for this type, so return
2398 index
= m_typeinfo
.GetCount() - 1;
2404 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2406 int index
= FindDataType(typeName
);
2407 if ( index
== wxNOT_FOUND
)
2409 // the first part of the typename is the "real" type, anything after ':'
2410 // are the parameters for the renderer
2411 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2412 if ( index
== wxNOT_FOUND
)
2417 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2418 wxGridCellRenderer
*rendererOld
= renderer
;
2419 renderer
= renderer
->Clone();
2420 rendererOld
->DecRef();
2422 wxGridCellEditor
*editor
= GetEditor(index
);
2423 wxGridCellEditor
*editorOld
= editor
;
2424 editor
= editor
->Clone();
2425 editorOld
->DecRef();
2427 // do it even if there are no parameters to reset them to defaults
2428 wxString params
= typeName
.AfterFirst(_T(':'));
2429 renderer
->SetParameters(params
);
2430 editor
->SetParameters(params
);
2432 // register the new typename
2433 RegisterDataType(typeName
, renderer
, editor
);
2435 // we just registered it, it's the last one
2436 index
= m_typeinfo
.GetCount() - 1;
2442 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2444 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2450 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2452 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2458 // ----------------------------------------------------------------------------
2460 // ----------------------------------------------------------------------------
2462 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2465 wxGridTableBase::wxGridTableBase()
2467 m_view
= (wxGrid
*) NULL
;
2468 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2471 wxGridTableBase::~wxGridTableBase()
2473 delete m_attrProvider
;
2476 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2478 delete m_attrProvider
;
2479 m_attrProvider
= attrProvider
;
2482 bool wxGridTableBase::CanHaveAttributes()
2484 if ( ! GetAttrProvider() )
2486 // use the default attr provider by default
2487 SetAttrProvider(new wxGridCellAttrProvider
);
2492 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
2494 if ( m_attrProvider
)
2495 return m_attrProvider
->GetAttr(row
, col
);
2497 return (wxGridCellAttr
*)NULL
;
2500 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2502 if ( m_attrProvider
)
2504 m_attrProvider
->SetAttr(attr
, row
, col
);
2508 // as we take ownership of the pointer and don't store it, we must
2514 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2516 if ( m_attrProvider
)
2518 m_attrProvider
->SetRowAttr(attr
, row
);
2522 // as we take ownership of the pointer and don't store it, we must
2528 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2530 if ( m_attrProvider
)
2532 m_attrProvider
->SetColAttr(attr
, col
);
2536 // as we take ownership of the pointer and don't store it, we must
2542 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2543 size_t WXUNUSED(numRows
) )
2545 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2550 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2552 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2557 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2558 size_t WXUNUSED(numRows
) )
2560 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2565 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2566 size_t WXUNUSED(numCols
) )
2568 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2573 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2575 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2580 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2581 size_t WXUNUSED(numCols
) )
2583 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2589 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2592 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2593 // how much it makes sense to us geeks.
2597 wxString
wxGridTableBase::GetColLabelValue( int col
)
2599 // default col labels are:
2600 // cols 0 to 25 : A-Z
2601 // cols 26 to 675 : AA-ZZ
2606 for ( n
= 1; ; n
++ )
2608 s
+= (_T('A') + (wxChar
)( col%26
));
2610 if ( col
< 0 ) break;
2613 // reverse the string...
2615 for ( i
= 0; i
< n
; i
++ )
2624 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2626 return wxGRID_VALUE_STRING
;
2629 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2630 const wxString
& typeName
)
2632 return typeName
== wxGRID_VALUE_STRING
;
2635 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2637 return CanGetValueAs(row
, col
, typeName
);
2640 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2645 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2650 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2655 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2656 long WXUNUSED(value
) )
2660 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2661 double WXUNUSED(value
) )
2665 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2666 bool WXUNUSED(value
) )
2671 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2672 const wxString
& WXUNUSED(typeName
) )
2677 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2678 const wxString
& WXUNUSED(typeName
),
2679 void* WXUNUSED(value
) )
2683 //////////////////////////////////////////////////////////////////////
2685 // Message class for the grid table to send requests and notifications
2689 wxGridTableMessage::wxGridTableMessage()
2691 m_table
= (wxGridTableBase
*) NULL
;
2697 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2698 int commandInt1
, int commandInt2
)
2702 m_comInt1
= commandInt1
;
2703 m_comInt2
= commandInt2
;
2708 //////////////////////////////////////////////////////////////////////
2710 // A basic grid table for string data. An object of this class will
2711 // created by wxGrid if you don't specify an alternative table class.
2714 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2716 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2718 wxGridStringTable::wxGridStringTable()
2723 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2728 m_data
.Alloc( numRows
);
2731 sa
.Alloc( numCols
);
2732 for ( col
= 0; col
< numCols
; col
++ )
2734 sa
.Add( wxEmptyString
);
2737 for ( row
= 0; row
< numRows
; row
++ )
2743 wxGridStringTable::~wxGridStringTable()
2747 int wxGridStringTable::GetNumberRows()
2749 return m_data
.GetCount();
2752 int wxGridStringTable::GetNumberCols()
2754 if ( m_data
.GetCount() > 0 )
2755 return m_data
[0].GetCount();
2760 wxString
wxGridStringTable::GetValue( int row
, int col
)
2762 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2763 _T("invalid row or column index in wxGridStringTable") );
2765 return m_data
[row
][col
];
2768 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2770 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2771 _T("invalid row or column index in wxGridStringTable") );
2773 m_data
[row
][col
] = value
;
2776 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2778 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2779 _T("invalid row or column index in wxGridStringTable") );
2781 return (m_data
[row
][col
] == wxEmptyString
);
2784 void wxGridStringTable::Clear()
2787 int numRows
, numCols
;
2789 numRows
= m_data
.GetCount();
2792 numCols
= m_data
[0].GetCount();
2794 for ( row
= 0; row
< numRows
; row
++ )
2796 for ( col
= 0; col
< numCols
; col
++ )
2798 m_data
[row
][col
] = wxEmptyString
;
2805 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2809 size_t curNumRows
= m_data
.GetCount();
2810 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2811 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2813 if ( pos
>= curNumRows
)
2815 return AppendRows( numRows
);
2819 sa
.Alloc( curNumCols
);
2820 for ( col
= 0; col
< curNumCols
; col
++ )
2822 sa
.Add( wxEmptyString
);
2825 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2827 m_data
.Insert( sa
, row
);
2831 wxGridTableMessage
msg( this,
2832 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2836 GetView()->ProcessTableMessage( msg
);
2842 bool wxGridStringTable::AppendRows( size_t numRows
)
2846 size_t curNumRows
= m_data
.GetCount();
2847 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2848 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2851 if ( curNumCols
> 0 )
2853 sa
.Alloc( curNumCols
);
2854 for ( col
= 0; col
< curNumCols
; col
++ )
2856 sa
.Add( wxEmptyString
);
2860 for ( row
= 0; row
< numRows
; row
++ )
2867 wxGridTableMessage
msg( this,
2868 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2871 GetView()->ProcessTableMessage( msg
);
2877 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2881 size_t curNumRows
= m_data
.GetCount();
2883 if ( pos
>= curNumRows
)
2886 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
2887 pos
, numRows
, curNumRows
);
2888 wxFAIL_MSG( errmsg
);
2892 if ( numRows
> curNumRows
- pos
)
2894 numRows
= curNumRows
- pos
;
2897 if ( numRows
>= curNumRows
)
2899 m_data
.Empty(); // don't release memory just yet
2903 for ( n
= 0; n
< numRows
; n
++ )
2905 m_data
.Remove( pos
);
2910 wxGridTableMessage
msg( this,
2911 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2915 GetView()->ProcessTableMessage( msg
);
2921 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2925 size_t curNumRows
= m_data
.GetCount();
2926 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2927 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2929 if ( pos
>= curNumCols
)
2931 return AppendCols( numCols
);
2934 for ( row
= 0; row
< curNumRows
; row
++ )
2936 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2938 m_data
[row
].Insert( wxEmptyString
, col
);
2943 wxGridTableMessage
msg( this,
2944 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2948 GetView()->ProcessTableMessage( msg
);
2954 bool wxGridStringTable::AppendCols( size_t numCols
)
2958 size_t curNumRows
= m_data
.GetCount();
2962 // TODO: something better than this ?
2964 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
2969 for ( row
= 0; row
< curNumRows
; row
++ )
2971 for ( n
= 0; n
< numCols
; n
++ )
2973 m_data
[row
].Add( wxEmptyString
);
2979 wxGridTableMessage
msg( this,
2980 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2983 GetView()->ProcessTableMessage( msg
);
2989 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2993 size_t curNumRows
= m_data
.GetCount();
2994 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2995 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2997 if ( pos
>= curNumCols
)
3000 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3001 pos
, numCols
, curNumCols
);
3002 wxFAIL_MSG( errmsg
);
3006 if ( numCols
> curNumCols
- pos
)
3008 numCols
= curNumCols
- pos
;
3011 for ( row
= 0; row
< curNumRows
; row
++ )
3013 if ( numCols
>= curNumCols
)
3015 m_data
[row
].Clear();
3019 for ( n
= 0; n
< numCols
; n
++ )
3021 m_data
[row
].Remove( pos
);
3027 wxGridTableMessage
msg( this,
3028 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3032 GetView()->ProcessTableMessage( msg
);
3038 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3040 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3042 // using default label
3044 return wxGridTableBase::GetRowLabelValue( row
);
3048 return m_rowLabels
[ row
];
3052 wxString
wxGridStringTable::GetColLabelValue( int col
)
3054 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3056 // using default label
3058 return wxGridTableBase::GetColLabelValue( col
);
3062 return m_colLabels
[ col
];
3066 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3068 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3070 int n
= m_rowLabels
.GetCount();
3072 for ( i
= n
; i
<= row
; i
++ )
3074 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3078 m_rowLabels
[row
] = value
;
3081 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3083 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3085 int n
= m_colLabels
.GetCount();
3087 for ( i
= n
; i
<= col
; i
++ )
3089 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3093 m_colLabels
[col
] = value
;
3098 //////////////////////////////////////////////////////////////////////
3099 //////////////////////////////////////////////////////////////////////
3101 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3103 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3104 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3105 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3106 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3107 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3110 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3112 const wxPoint
&pos
, const wxSize
&size
)
3113 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3118 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3122 // NO - don't do this because it will set both the x and y origin
3123 // coords to match the parent scrolled window and we just want to
3124 // set the y coord - MB
3126 // m_owner->PrepareDC( dc );
3129 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3130 dc
.SetDeviceOrigin( 0, -y
);
3132 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3133 m_owner
->DrawRowLabels( dc
);
3137 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3139 m_owner
->ProcessRowLabelMouseEvent( event
);
3143 // This seems to be required for wxMotif otherwise the mouse
3144 // cursor must be in the cell edit control to get key events
3146 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3148 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3151 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3153 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3158 //////////////////////////////////////////////////////////////////////
3160 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3162 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3163 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3164 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3165 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3166 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3169 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3171 const wxPoint
&pos
, const wxSize
&size
)
3172 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3177 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3181 // NO - don't do this because it will set both the x and y origin
3182 // coords to match the parent scrolled window and we just want to
3183 // set the x coord - MB
3185 // m_owner->PrepareDC( dc );
3188 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3189 dc
.SetDeviceOrigin( -x
, 0 );
3191 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3192 m_owner
->DrawColLabels( dc
);
3196 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3198 m_owner
->ProcessColLabelMouseEvent( event
);
3202 // This seems to be required for wxMotif otherwise the mouse
3203 // cursor must be in the cell edit control to get key events
3205 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3207 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3210 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3212 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3217 //////////////////////////////////////////////////////////////////////
3219 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3221 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3222 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3223 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3224 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3225 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3228 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3230 const wxPoint
&pos
, const wxSize
&size
)
3231 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3236 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3240 int client_height
= 0;
3241 int client_width
= 0;
3242 GetClientSize( &client_width
, &client_height
);
3244 dc
.SetPen( *wxBLACK_PEN
);
3245 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3246 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3248 dc
.SetPen( *wxWHITE_PEN
);
3249 dc
.DrawLine( 0, 0, client_width
, 0 );
3250 dc
.DrawLine( 0, 0, 0, client_height
);
3254 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3256 m_owner
->ProcessCornerLabelMouseEvent( event
);
3260 // This seems to be required for wxMotif otherwise the mouse
3261 // cursor must be in the cell edit control to get key events
3263 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3265 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3268 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3270 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3275 //////////////////////////////////////////////////////////////////////
3277 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3279 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3280 EVT_PAINT( wxGridWindow::OnPaint
)
3281 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3282 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3283 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3284 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3287 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3288 wxGridRowLabelWindow
*rowLblWin
,
3289 wxGridColLabelWindow
*colLblWin
,
3290 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3291 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3294 m_rowLabelWin
= rowLblWin
;
3295 m_colLabelWin
= colLblWin
;
3296 SetBackgroundColour( "WHITE" );
3300 wxGridWindow::~wxGridWindow()
3305 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3307 wxPaintDC
dc( this );
3308 m_owner
->PrepareDC( dc
);
3309 wxRegion reg
= GetUpdateRegion();
3310 m_owner
->CalcCellsExposed( reg
);
3311 m_owner
->DrawGridCellArea( dc
);
3312 #if WXGRID_DRAW_LINES
3313 m_owner
->DrawAllGridLines( dc
, reg
);
3315 m_owner
->DrawGridSpace( dc
);
3316 m_owner
->DrawHighlight( dc
);
3320 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3322 wxPanel::ScrollWindow( dx
, dy
, rect
);
3323 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3324 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3328 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3330 m_owner
->ProcessGridCellMouseEvent( event
);
3334 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3335 // cursor must be in the cell edit control to get key events
3337 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3339 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3342 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3344 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3347 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3352 //////////////////////////////////////////////////////////////////////
3355 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3357 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3358 EVT_PAINT( wxGrid::OnPaint
)
3359 EVT_SIZE( wxGrid::OnSize
)
3360 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3361 EVT_KEY_UP( wxGrid::OnKeyUp
)
3362 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3365 wxGrid::wxGrid( wxWindow
*parent
,
3370 const wxString
& name
)
3371 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3372 m_colMinWidths(GRID_HASH_SIZE
),
3373 m_rowMinHeights(GRID_HASH_SIZE
)
3382 wxSafeDecRef(m_defaultCellAttr
);
3384 #ifdef DEBUG_ATTR_CACHE
3385 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3386 wxPrintf(_T("wxGrid attribute cache statistics: "
3387 "total: %u, hits: %u (%u%%)\n"),
3388 total
, gs_nAttrCacheHits
,
3389 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3395 delete m_typeRegistry
;
3401 // ----- internal init and update functions
3404 void wxGrid::Create()
3406 m_created
= FALSE
; // set to TRUE by CreateGrid
3408 m_table
= (wxGridTableBase
*) NULL
;
3411 m_cellEditCtrlEnabled
= FALSE
;
3413 m_defaultCellAttr
= new wxGridCellAttr
;
3414 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3416 // Set default cell attributes
3417 m_defaultCellAttr
->SetFont(GetFont());
3418 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3419 m_defaultCellAttr
->SetTextColour(
3420 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3421 m_defaultCellAttr
->SetBackgroundColour(
3422 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3423 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3424 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3429 m_currentCellCoords
= wxGridNoCellCoords
;
3431 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3432 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3434 // create the type registry
3435 m_typeRegistry
= new wxGridTypeRegistry
;
3437 // subwindow components that make up the wxGrid
3438 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3443 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3448 m_colLabelWin
= new wxGridColLabelWindow( this,
3453 m_gridWin
= new wxGridWindow( this,
3460 SetTargetWindow( m_gridWin
);
3464 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3465 wxGrid::wxGridSelectionModes selmode
)
3467 wxCHECK_MSG( !m_created
,
3469 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3471 m_numRows
= numRows
;
3472 m_numCols
= numCols
;
3474 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3475 m_table
->SetView( this );
3477 m_selection
= new wxGridSelection( this, selmode
);
3484 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3488 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3491 m_selection
->SetSelectionMode( selmode
);
3494 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3495 wxGrid::wxGridSelectionModes selmode
)
3499 // RD: Actually, this should probably be allowed. I think it would be
3500 // nice to be able to switch multiple Tables in and out of a single
3501 // View at runtime. Is there anything in the implmentation that would
3504 // At least, you now have to cope with m_selection
3505 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3510 m_numRows
= table
->GetNumberRows();
3511 m_numCols
= table
->GetNumberCols();
3514 m_table
->SetView( this );
3517 m_selection
= new wxGridSelection( this, selmode
);
3528 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3529 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3531 if ( m_rowLabelWin
)
3533 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3537 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3540 m_labelTextColour
= wxColour( _T("BLACK") );
3543 m_attrCache
.row
= -1;
3545 // TODO: something better than this ?
3547 m_labelFont
= this->GetFont();
3548 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3550 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3551 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3553 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3554 m_colLabelVertAlign
= wxALIGN_TOP
;
3556 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3557 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3559 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3560 m_defaultRowHeight
+= 8;
3562 m_defaultRowHeight
+= 4;
3565 m_gridLineColour
= wxColour( 128, 128, 255 );
3566 m_gridLinesEnabled
= TRUE
;
3567 m_cellHighlightColour
= m_gridLineColour
;
3569 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3570 m_winCapture
= (wxWindow
*)NULL
;
3571 m_canDragRowSize
= TRUE
;
3572 m_canDragColSize
= TRUE
;
3573 m_canDragGridSize
= TRUE
;
3575 m_dragRowOrCol
= -1;
3576 m_isDragging
= FALSE
;
3577 m_startDragPos
= wxDefaultPosition
;
3579 m_waitForSlowClick
= FALSE
;
3581 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3582 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3584 m_currentCellCoords
= wxGridNoCellCoords
;
3586 m_selectingTopLeft
= wxGridNoCellCoords
;
3587 m_selectingBottomRight
= wxGridNoCellCoords
;
3588 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3589 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3591 m_editable
= TRUE
; // default for whole grid
3593 m_inOnKeyDown
= FALSE
;
3602 // ----------------------------------------------------------------------------
3603 // the idea is to call these functions only when necessary because they create
3604 // quite big arrays which eat memory mostly unnecessary - in particular, if
3605 // default widths/heights are used for all rows/columns, we may not use these
3608 // with some extra code, it should be possible to only store the
3609 // widths/heights different from default ones but this will be done later...
3610 // ----------------------------------------------------------------------------
3612 void wxGrid::InitRowHeights()
3614 m_rowHeights
.Empty();
3615 m_rowBottoms
.Empty();
3617 m_rowHeights
.Alloc( m_numRows
);
3618 m_rowBottoms
.Alloc( m_numRows
);
3621 for ( int i
= 0; i
< m_numRows
; i
++ )
3623 m_rowHeights
.Add( m_defaultRowHeight
);
3624 rowBottom
+= m_defaultRowHeight
;
3625 m_rowBottoms
.Add( rowBottom
);
3629 void wxGrid::InitColWidths()
3631 m_colWidths
.Empty();
3632 m_colRights
.Empty();
3634 m_colWidths
.Alloc( m_numCols
);
3635 m_colRights
.Alloc( m_numCols
);
3637 for ( int i
= 0; i
< m_numCols
; i
++ )
3639 m_colWidths
.Add( m_defaultColWidth
);
3640 colRight
+= m_defaultColWidth
;
3641 m_colRights
.Add( colRight
);
3645 int wxGrid::GetColWidth(int col
) const
3647 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3650 int wxGrid::GetColLeft(int col
) const
3652 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3653 : m_colRights
[col
] - m_colWidths
[col
];
3656 int wxGrid::GetColRight(int col
) const
3658 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3662 int wxGrid::GetRowHeight(int row
) const
3664 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3667 int wxGrid::GetRowTop(int row
) const
3669 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3670 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3673 int wxGrid::GetRowBottom(int row
) const
3675 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3676 : m_rowBottoms
[row
];
3679 void wxGrid::CalcDimensions()
3682 GetClientSize( &cw
, &ch
);
3684 if ( m_colLabelWin
->IsShown() )
3685 cw
-= m_rowLabelWidth
;
3686 if ( m_rowLabelWin
->IsShown() )
3687 ch
-= m_colLabelHeight
;
3690 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3691 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3693 // preserve (more or less) the previous position
3695 GetViewStart( &x
, &y
);
3696 // maybe we don't need scrollbars at all? and if we do, transform w and h
3697 // from pixels into logical units
3704 w
= (w
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3714 h
= (h
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3719 // do set scrollbar parameters
3720 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3721 w
, h
, x
, y
, (GetBatchCount() != 0));
3725 void wxGrid::CalcWindowSizes()
3728 GetClientSize( &cw
, &ch
);
3730 if ( m_cornerLabelWin
->IsShown() )
3731 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3733 if ( m_colLabelWin
->IsShown() )
3734 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3736 if ( m_rowLabelWin
->IsShown() )
3737 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3739 if ( m_gridWin
->IsShown() )
3740 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3744 // this is called when the grid table sends a message to say that it
3745 // has been redimensioned
3747 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3750 bool result
= FALSE
;
3753 // if we were using the default widths/heights so far, we must change them
3755 if ( m_colWidths
.IsEmpty() )
3760 if ( m_rowHeights
.IsEmpty() )
3766 switch ( msg
.GetId() )
3768 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3770 size_t pos
= msg
.GetCommandInt();
3771 int numRows
= msg
.GetCommandInt2();
3773 m_numRows
+= numRows
;
3775 if ( !m_rowHeights
.IsEmpty() )
3777 for ( i
= 0; i
< numRows
; i
++ )
3779 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3780 m_rowBottoms
.Insert( 0, pos
);
3784 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3786 for ( i
= pos
; i
< m_numRows
; i
++ )
3788 bottom
+= m_rowHeights
[i
];
3789 m_rowBottoms
[i
] = bottom
;
3792 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3794 // if we have just inserted cols into an empty grid the current
3795 // cell will be undefined...
3797 SetCurrentCell( 0, 0 );
3799 m_selection
->UpdateRows( pos
, numRows
);
3800 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3802 attrProvider
->UpdateAttrRows( pos
, numRows
);
3804 if ( !GetBatchCount() )
3807 m_rowLabelWin
->Refresh();
3813 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3815 int numRows
= msg
.GetCommandInt();
3816 int oldNumRows
= m_numRows
;
3817 m_numRows
+= numRows
;
3819 if ( !m_rowHeights
.IsEmpty() )
3821 for ( i
= 0; i
< numRows
; i
++ )
3823 m_rowHeights
.Add( m_defaultRowHeight
);
3824 m_rowBottoms
.Add( 0 );
3828 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3830 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3832 bottom
+= m_rowHeights
[i
];
3833 m_rowBottoms
[i
] = bottom
;
3836 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3838 // if we have just inserted cols into an empty grid the current
3839 // cell will be undefined...
3841 SetCurrentCell( 0, 0 );
3843 if ( !GetBatchCount() )
3846 m_rowLabelWin
->Refresh();
3852 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3854 size_t pos
= msg
.GetCommandInt();
3855 int numRows
= msg
.GetCommandInt2();
3856 m_numRows
-= numRows
;
3858 if ( !m_rowHeights
.IsEmpty() )
3860 for ( i
= 0; i
< numRows
; i
++ )
3862 m_rowHeights
.Remove( pos
);
3863 m_rowBottoms
.Remove( pos
);
3867 for ( i
= 0; i
< m_numRows
; i
++ )
3869 h
+= m_rowHeights
[i
];
3870 m_rowBottoms
[i
] = h
;
3875 m_currentCellCoords
= wxGridNoCellCoords
;
3879 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3880 m_currentCellCoords
.Set( 0, 0 );
3882 m_selection
->UpdateRows( pos
, -((int)numRows
) );
3883 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3885 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
3886 // ifdef'd out following patch from Paul Gammans
3888 // No need to touch column attributes, unless we
3889 // removed _all_ rows, in this case, we remove
3890 // all column attributes.
3891 // I hate to do this here, but the
3892 // needed data is not available inside UpdateAttrRows.
3893 if ( !GetNumberRows() )
3894 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
3897 if ( !GetBatchCount() )
3900 m_rowLabelWin
->Refresh();
3906 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3908 size_t pos
= msg
.GetCommandInt();
3909 int numCols
= msg
.GetCommandInt2();
3910 m_numCols
+= numCols
;
3912 if ( !m_colWidths
.IsEmpty() )
3914 for ( i
= 0; i
< numCols
; i
++ )
3916 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3917 m_colRights
.Insert( 0, pos
);
3921 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3923 for ( i
= pos
; i
< m_numCols
; i
++ )
3925 right
+= m_colWidths
[i
];
3926 m_colRights
[i
] = right
;
3929 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3931 // if we have just inserted cols into an empty grid the current
3932 // cell will be undefined...
3934 SetCurrentCell( 0, 0 );
3936 m_selection
->UpdateCols( pos
, numCols
);
3937 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3939 attrProvider
->UpdateAttrCols( pos
, numCols
);
3940 if ( !GetBatchCount() )
3943 m_colLabelWin
->Refresh();
3950 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3952 int numCols
= msg
.GetCommandInt();
3953 int oldNumCols
= m_numCols
;
3954 m_numCols
+= numCols
;
3955 if ( !m_colWidths
.IsEmpty() )
3957 for ( i
= 0; i
< numCols
; i
++ )
3959 m_colWidths
.Add( m_defaultColWidth
);
3960 m_colRights
.Add( 0 );
3964 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3966 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3968 right
+= m_colWidths
[i
];
3969 m_colRights
[i
] = right
;
3972 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3974 // if we have just inserted cols into an empty grid the current
3975 // cell will be undefined...
3977 SetCurrentCell( 0, 0 );
3979 if ( !GetBatchCount() )
3982 m_colLabelWin
->Refresh();
3988 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3990 size_t pos
= msg
.GetCommandInt();
3991 int numCols
= msg
.GetCommandInt2();
3992 m_numCols
-= numCols
;
3994 if ( !m_colWidths
.IsEmpty() )
3996 for ( i
= 0; i
< numCols
; i
++ )
3998 m_colWidths
.Remove( pos
);
3999 m_colRights
.Remove( pos
);
4003 for ( i
= 0; i
< m_numCols
; i
++ )
4005 w
+= m_colWidths
[i
];
4011 m_currentCellCoords
= wxGridNoCellCoords
;
4015 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4016 m_currentCellCoords
.Set( 0, 0 );
4018 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4019 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4021 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4022 // ifdef'd out following patch from Paul Gammans
4024 // No need to touch row attributes, unless we
4025 // removed _all_ columns, in this case, we remove
4026 // all row attributes.
4027 // I hate to do this here, but the
4028 // needed data is not available inside UpdateAttrCols.
4029 if ( !GetNumberCols() )
4030 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4033 if ( !GetBatchCount() )
4036 m_colLabelWin
->Refresh();
4043 if (result
&& !GetBatchCount() )
4044 m_gridWin
->Refresh();
4049 void wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4051 wxRegionIterator
iter( reg
);
4054 m_rowLabelsExposed
.Empty();
4061 // TODO: remove this when we can...
4062 // There is a bug in wxMotif that gives garbage update
4063 // rectangles if you jump-scroll a long way by clicking the
4064 // scrollbar with middle button. This is a work-around
4066 #if defined(__WXMOTIF__)
4068 m_gridWin
->GetClientSize( &cw
, &ch
);
4069 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4070 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4073 // logical bounds of update region
4076 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4077 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4079 // find the row labels within these bounds
4082 for ( row
= 0; row
< m_numRows
; row
++ )
4084 if ( GetRowBottom(row
) < top
)
4087 if ( GetRowTop(row
) > bottom
)
4090 m_rowLabelsExposed
.Add( row
);
4098 void wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4100 wxRegionIterator
iter( reg
);
4103 m_colLabelsExposed
.Empty();
4110 // TODO: remove this when we can...
4111 // There is a bug in wxMotif that gives garbage update
4112 // rectangles if you jump-scroll a long way by clicking the
4113 // scrollbar with middle button. This is a work-around
4115 #if defined(__WXMOTIF__)
4117 m_gridWin
->GetClientSize( &cw
, &ch
);
4118 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4119 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4122 // logical bounds of update region
4125 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4126 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4128 // find the cells within these bounds
4131 for ( col
= 0; col
< m_numCols
; col
++ )
4133 if ( GetColRight(col
) < left
)
4136 if ( GetColLeft(col
) > right
)
4139 m_colLabelsExposed
.Add( col
);
4147 void wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4149 wxRegionIterator
iter( reg
);
4152 m_cellsExposed
.Empty();
4153 m_rowsExposed
.Empty();
4154 m_colsExposed
.Empty();
4156 int left
, top
, right
, bottom
;
4161 // TODO: remove this when we can...
4162 // There is a bug in wxMotif that gives garbage update
4163 // rectangles if you jump-scroll a long way by clicking the
4164 // scrollbar with middle button. This is a work-around
4166 #if defined(__WXMOTIF__)
4168 m_gridWin
->GetClientSize( &cw
, &ch
);
4169 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4170 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4171 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4172 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4175 // logical bounds of update region
4177 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4178 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4180 // find the cells within these bounds
4183 for ( row
= 0; row
< m_numRows
; row
++ )
4185 if ( GetRowBottom(row
) <= top
)
4188 if ( GetRowTop(row
) > bottom
)
4191 m_rowsExposed
.Add( row
);
4193 for ( col
= 0; col
< m_numCols
; col
++ )
4195 if ( GetColRight(col
) <= left
)
4198 if ( GetColLeft(col
) > right
)
4201 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
4202 m_colsExposed
.Add( col
);
4203 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4212 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4215 wxPoint
pos( event
.GetPosition() );
4216 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4218 if ( event
.Dragging() )
4220 m_isDragging
= TRUE
;
4222 if ( event
.LeftIsDown() )
4224 switch( m_cursorMode
)
4226 case WXGRID_CURSOR_RESIZE_ROW
:
4228 int cw
, ch
, left
, dummy
;
4229 m_gridWin
->GetClientSize( &cw
, &ch
);
4230 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4232 wxClientDC
dc( m_gridWin
);
4235 GetRowTop(m_dragRowOrCol
) +
4236 GetRowMinimalHeight(m_dragRowOrCol
) );
4237 dc
.SetLogicalFunction(wxINVERT
);
4238 if ( m_dragLastPos
>= 0 )
4240 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4242 dc
.DrawLine( left
, y
, left
+cw
, y
);
4247 case WXGRID_CURSOR_SELECT_ROW
:
4248 if ( (row
= YToRow( y
)) >= 0 )
4250 m_selection
->SelectRow( row
,
4251 event
.ControlDown(),
4257 // default label to suppress warnings about "enumeration value
4258 // 'xxx' not handled in switch
4266 m_isDragging
= FALSE
;
4269 // ------------ Entering or leaving the window
4271 if ( event
.Entering() || event
.Leaving() )
4273 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4277 // ------------ Left button pressed
4279 else if ( event
.LeftDown() )
4281 // don't send a label click event for a hit on the
4282 // edge of the row label - this is probably the user
4283 // wanting to resize the row
4285 if ( YToEdgeOfRow(y
) < 0 )
4289 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4291 if ( !event
.ShiftDown() && !event
.ControlDown() )
4293 if ( event
.ShiftDown() )
4294 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4297 GetNumberCols() - 1,
4298 event
.ControlDown(),
4303 m_selection
->SelectRow( row
,
4304 event
.ControlDown(),
4308 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4313 // starting to drag-resize a row
4315 if ( CanDragRowSize() )
4316 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4321 // ------------ Left double click
4323 else if (event
.LeftDClick() )
4325 if ( YToEdgeOfRow(y
) < 0 )
4328 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4333 // ------------ Left button released
4335 else if ( event
.LeftUp() )
4337 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4339 DoEndDragResizeRow();
4341 // Note: we are ending the event *after* doing
4342 // default processing in this case
4344 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4347 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4352 // ------------ Right button down
4354 else if ( event
.RightDown() )
4357 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4359 // no default action at the moment
4364 // ------------ Right double click
4366 else if ( event
.RightDClick() )
4369 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4371 // no default action at the moment
4376 // ------------ No buttons down and mouse moving
4378 else if ( event
.Moving() )
4380 m_dragRowOrCol
= YToEdgeOfRow( y
);
4381 if ( m_dragRowOrCol
>= 0 )
4383 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4385 // don't capture the mouse yet
4386 if ( CanDragRowSize() )
4387 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4390 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4392 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4398 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4401 wxPoint
pos( event
.GetPosition() );
4402 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4404 if ( event
.Dragging() )
4406 m_isDragging
= TRUE
;
4408 if ( event
.LeftIsDown() )
4410 switch( m_cursorMode
)
4412 case WXGRID_CURSOR_RESIZE_COL
:
4414 int cw
, ch
, dummy
, top
;
4415 m_gridWin
->GetClientSize( &cw
, &ch
);
4416 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4418 wxClientDC
dc( m_gridWin
);
4421 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4422 GetColMinimalWidth(m_dragRowOrCol
));
4423 dc
.SetLogicalFunction(wxINVERT
);
4424 if ( m_dragLastPos
>= 0 )
4426 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4428 dc
.DrawLine( x
, top
, x
, top
+ch
);
4433 case WXGRID_CURSOR_SELECT_COL
:
4434 if ( (col
= XToCol( x
)) >= 0 )
4436 m_selection
->SelectCol( col
,
4437 event
.ControlDown(),
4443 // default label to suppress warnings about "enumeration value
4444 // 'xxx' not handled in switch
4452 m_isDragging
= FALSE
;
4455 // ------------ Entering or leaving the window
4457 if ( event
.Entering() || event
.Leaving() )
4459 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4463 // ------------ Left button pressed
4465 else if ( event
.LeftDown() )
4467 // don't send a label click event for a hit on the
4468 // edge of the col label - this is probably the user
4469 // wanting to resize the col
4471 if ( XToEdgeOfCol(x
) < 0 )
4475 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4477 if ( !event
.ShiftDown() && !event
.ControlDown() )
4479 if ( event
.ShiftDown() )
4480 m_selection
->SelectBlock( 0,
4481 m_currentCellCoords
.GetCol(),
4482 GetNumberRows() - 1, col
,
4483 event
.ControlDown(),
4488 m_selection
->SelectCol( col
,
4489 event
.ControlDown(),
4493 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4498 // starting to drag-resize a col
4500 if ( CanDragColSize() )
4501 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4506 // ------------ Left double click
4508 if ( event
.LeftDClick() )
4510 if ( XToEdgeOfCol(x
) < 0 )
4513 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4518 // ------------ Left button released
4520 else if ( event
.LeftUp() )
4522 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4524 DoEndDragResizeCol();
4526 // Note: we are ending the event *after* doing
4527 // default processing in this case
4529 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4532 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4537 // ------------ Right button down
4539 else if ( event
.RightDown() )
4542 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4544 // no default action at the moment
4549 // ------------ Right double click
4551 else if ( event
.RightDClick() )
4554 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4556 // no default action at the moment
4561 // ------------ No buttons down and mouse moving
4563 else if ( event
.Moving() )
4565 m_dragRowOrCol
= XToEdgeOfCol( x
);
4566 if ( m_dragRowOrCol
>= 0 )
4568 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4570 // don't capture the cursor yet
4571 if ( CanDragColSize() )
4572 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4575 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4577 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4583 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4585 if ( event
.LeftDown() )
4587 // indicate corner label by having both row and
4590 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4596 else if ( event
.LeftDClick() )
4598 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4601 else if ( event
.RightDown() )
4603 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4605 // no default action at the moment
4609 else if ( event
.RightDClick() )
4611 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4613 // no default action at the moment
4618 void wxGrid::ChangeCursorMode(CursorMode mode
,
4623 static const wxChar
*cursorModes
[] =
4632 wxLogTrace(_T("grid"),
4633 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4634 win
== m_colLabelWin
? _T("colLabelWin")
4635 : win
? _T("rowLabelWin")
4637 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4638 #endif // __WXDEBUG__
4640 if ( mode
== m_cursorMode
&&
4641 win
== m_winCapture
&&
4642 captureMouse
== (m_winCapture
!= NULL
))
4647 // by default use the grid itself
4653 m_winCapture
->ReleaseMouse();
4654 m_winCapture
= (wxWindow
*)NULL
;
4657 m_cursorMode
= mode
;
4659 switch ( m_cursorMode
)
4661 case WXGRID_CURSOR_RESIZE_ROW
:
4662 win
->SetCursor( m_rowResizeCursor
);
4665 case WXGRID_CURSOR_RESIZE_COL
:
4666 win
->SetCursor( m_colResizeCursor
);
4670 win
->SetCursor( *wxSTANDARD_CURSOR
);
4673 // we need to capture mouse when resizing
4674 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4675 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4677 if ( captureMouse
&& resize
)
4679 win
->CaptureMouse();
4684 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4687 wxPoint
pos( event
.GetPosition() );
4688 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4690 wxGridCellCoords coords
;
4691 XYToCell( x
, y
, coords
);
4693 if ( event
.Dragging() )
4695 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4697 // Don't start doing anything until the mouse has been drug at
4698 // least 3 pixels in any direction...
4701 if (m_startDragPos
== wxDefaultPosition
)
4703 m_startDragPos
= pos
;
4706 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4710 m_isDragging
= TRUE
;
4711 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4713 // Hide the edit control, so it
4714 // won't interfer with drag-shrinking.
4715 if ( IsCellEditControlShown() )
4717 HideCellEditControl();
4718 SaveEditControlValue();
4721 // Have we captured the mouse yet?
4724 m_winCapture
= m_gridWin
;
4725 m_winCapture
->CaptureMouse();
4728 if ( coords
!= wxGridNoCellCoords
)
4730 if ( event
.ControlDown() )
4732 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4733 m_selectingKeyboard
= coords
;
4734 HighlightBlock ( m_selectingKeyboard
, coords
);
4738 if ( !IsSelection() )
4740 HighlightBlock( coords
, coords
);
4744 HighlightBlock( m_currentCellCoords
, coords
);
4748 if (! IsVisible(coords
))
4750 MakeCellVisible(coords
);
4751 // TODO: need to introduce a delay or something here. The
4752 // scrolling is way to fast, at least on MSW - also on GTK.
4756 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4758 int cw
, ch
, left
, dummy
;
4759 m_gridWin
->GetClientSize( &cw
, &ch
);
4760 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4762 wxClientDC
dc( m_gridWin
);
4764 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4765 GetRowMinimalHeight(m_dragRowOrCol
) );
4766 dc
.SetLogicalFunction(wxINVERT
);
4767 if ( m_dragLastPos
>= 0 )
4769 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4771 dc
.DrawLine( left
, y
, left
+cw
, y
);
4774 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4776 int cw
, ch
, dummy
, top
;
4777 m_gridWin
->GetClientSize( &cw
, &ch
);
4778 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4780 wxClientDC
dc( m_gridWin
);
4782 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4783 GetColMinimalWidth(m_dragRowOrCol
) );
4784 dc
.SetLogicalFunction(wxINVERT
);
4785 if ( m_dragLastPos
>= 0 )
4787 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4789 dc
.DrawLine( x
, top
, x
, top
+ch
);
4796 m_isDragging
= FALSE
;
4797 m_startDragPos
= wxDefaultPosition
;
4799 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4800 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4803 if ( event
.Entering() || event
.Leaving() )
4805 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4806 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4811 // ------------ Left button pressed
4813 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4815 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4820 if ( !event
.ControlDown() )
4822 if ( event
.ShiftDown() )
4824 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4825 m_currentCellCoords
.GetCol(),
4828 event
.ControlDown(),
4833 else if ( XToEdgeOfCol(x
) < 0 &&
4834 YToEdgeOfRow(y
) < 0 )
4836 DisableCellEditControl();
4837 MakeCellVisible( coords
);
4839 // if this is the second click on this cell then start
4841 if ( m_waitForSlowClick
&&
4842 (coords
== m_currentCellCoords
) &&
4843 CanEnableCellControl())
4845 EnableCellEditControl();
4847 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4848 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4851 editor
->StartingClick();
4855 m_waitForSlowClick
= FALSE
;
4859 if ( event
.ControlDown() )
4861 m_selection
->ToggleCellSelection( coords
.GetRow(),
4863 event
.ControlDown(),
4867 m_selectingTopLeft
= wxGridNoCellCoords
;
4868 m_selectingBottomRight
= wxGridNoCellCoords
;
4869 m_selectingKeyboard
= coords
;
4873 SetCurrentCell( coords
);
4874 if ( m_selection
->GetSelectionMode()
4875 != wxGrid::wxGridSelectCells
)
4876 HighlightBlock( coords
, coords
);
4878 m_waitForSlowClick
= TRUE
;
4885 // ------------ Left double click
4887 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4889 DisableCellEditControl();
4891 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4893 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4901 // ------------ Left button released
4903 else if ( event
.LeftUp() )
4905 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4907 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
4908 m_selectingBottomRight
!= wxGridNoCellCoords
)
4912 m_winCapture
->ReleaseMouse();
4913 m_winCapture
= NULL
;
4915 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
4916 m_selectingTopLeft
.GetCol(),
4917 m_selectingBottomRight
.GetRow(),
4918 m_selectingBottomRight
.GetCol(),
4919 event
.ControlDown(),
4923 m_selectingTopLeft
= wxGridNoCellCoords
;
4924 m_selectingBottomRight
= wxGridNoCellCoords
;
4927 // Show the edit control, if it has been hidden for
4929 ShowCellEditControl();
4931 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4933 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4934 DoEndDragResizeRow();
4936 // Note: we are ending the event *after* doing
4937 // default processing in this case
4939 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4941 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4943 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4944 DoEndDragResizeCol();
4946 // Note: we are ending the event *after* doing
4947 // default processing in this case
4949 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4956 // ------------ Right button down
4958 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4960 DisableCellEditControl();
4961 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4966 // no default action at the moment
4971 // ------------ Right double click
4973 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4975 DisableCellEditControl();
4976 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4981 // no default action at the moment
4985 // ------------ Moving and no button action
4987 else if ( event
.Moving() && !event
.IsButton() )
4989 int dragRow
= YToEdgeOfRow( y
);
4990 int dragCol
= XToEdgeOfCol( x
);
4992 // Dragging on the corner of a cell to resize in both
4993 // directions is not implemented yet...
4995 if ( dragRow
>= 0 && dragCol
>= 0 )
4997 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5003 m_dragRowOrCol
= dragRow
;
5005 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5007 if ( CanDragRowSize() && CanDragGridSize() )
5008 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5013 m_dragRowOrCol
= dragCol
;
5021 m_dragRowOrCol
= dragCol
;
5023 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5025 if ( CanDragColSize() && CanDragGridSize() )
5026 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5032 // Neither on a row or col edge
5034 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5036 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5042 void wxGrid::DoEndDragResizeRow()
5044 if ( m_dragLastPos
>= 0 )
5046 // erase the last line and resize the row
5048 int cw
, ch
, left
, dummy
;
5049 m_gridWin
->GetClientSize( &cw
, &ch
);
5050 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5052 wxClientDC
dc( m_gridWin
);
5054 dc
.SetLogicalFunction( wxINVERT
);
5055 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5056 HideCellEditControl();
5057 SaveEditControlValue();
5059 int rowTop
= GetRowTop(m_dragRowOrCol
);
5060 SetRowSize( m_dragRowOrCol
,
5061 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5063 if ( !GetBatchCount() )
5065 // Only needed to get the correct rect.y:
5066 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5068 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5069 rect
.width
= m_rowLabelWidth
;
5070 rect
.height
= ch
- rect
.y
;
5071 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5073 m_gridWin
->Refresh( FALSE
, &rect
);
5076 ShowCellEditControl();
5081 void wxGrid::DoEndDragResizeCol()
5083 if ( m_dragLastPos
>= 0 )
5085 // erase the last line and resize the col
5087 int cw
, ch
, dummy
, top
;
5088 m_gridWin
->GetClientSize( &cw
, &ch
);
5089 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5091 wxClientDC
dc( m_gridWin
);
5093 dc
.SetLogicalFunction( wxINVERT
);
5094 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5095 HideCellEditControl();
5096 SaveEditControlValue();
5098 int colLeft
= GetColLeft(m_dragRowOrCol
);
5099 SetColSize( m_dragRowOrCol
,
5100 wxMax( m_dragLastPos
- colLeft
,
5101 GetColMinimalWidth(m_dragRowOrCol
) ) );
5103 if ( !GetBatchCount() )
5105 // Only needed to get the correct rect.x:
5106 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5108 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5109 rect
.width
= cw
- rect
.x
;
5110 rect
.height
= m_colLabelHeight
;
5111 m_colLabelWin
->Refresh( TRUE
, &rect
);
5113 m_gridWin
->Refresh( FALSE
, &rect
);
5116 ShowCellEditControl();
5123 // ------ interaction with data model
5125 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5127 switch ( msg
.GetId() )
5129 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5130 return GetModelValues();
5132 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5133 return SetModelValues();
5135 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5136 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5137 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5138 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5139 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5140 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5141 return Redimension( msg
);
5150 // The behaviour of this function depends on the grid table class
5151 // Clear() function. For the default wxGridStringTable class the
5152 // behavious is to replace all cell contents with wxEmptyString but
5153 // not to change the number of rows or cols.
5155 void wxGrid::ClearGrid()
5159 if (IsCellEditControlEnabled())
5160 DisableCellEditControl();
5163 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5168 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5170 // TODO: something with updateLabels flag
5174 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5180 if (IsCellEditControlEnabled())
5181 DisableCellEditControl();
5183 return m_table
->InsertRows( pos
, numRows
);
5185 // the table will have sent the results of the insert row
5186 // operation to this view object as a grid table message
5192 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5194 // TODO: something with updateLabels flag
5198 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5202 return ( m_table
&& m_table
->AppendRows( numRows
) );
5203 // the table will have sent the results of the append row
5204 // operation to this view object as a grid table message
5208 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5210 // TODO: something with updateLabels flag
5214 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5220 if (IsCellEditControlEnabled())
5221 DisableCellEditControl();
5223 return (m_table
->DeleteRows( pos
, numRows
));
5224 // the table will have sent the results of the delete row
5225 // operation to this view object as a grid table message
5231 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5233 // TODO: something with updateLabels flag
5237 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5243 if (IsCellEditControlEnabled())
5244 DisableCellEditControl();
5246 return m_table
->InsertCols( pos
, numCols
);
5247 // the table will have sent the results of the insert col
5248 // operation to this view object as a grid table message
5254 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5256 // TODO: something with updateLabels flag
5260 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5264 return ( m_table
&& m_table
->AppendCols( numCols
) );
5265 // the table will have sent the results of the append col
5266 // operation to this view object as a grid table message
5270 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5272 // TODO: something with updateLabels flag
5276 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5282 if (IsCellEditControlEnabled())
5283 DisableCellEditControl();
5285 return ( m_table
->DeleteCols( pos
, numCols
) );
5286 // the table will have sent the results of the delete col
5287 // operation to this view object as a grid table message
5295 // ----- event handlers
5298 // Generate a grid event based on a mouse event and
5299 // return the result of ProcessEvent()
5301 bool wxGrid::SendEvent( const wxEventType type
,
5303 wxMouseEvent
& mouseEv
)
5305 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5307 int rowOrCol
= (row
== -1 ? col
: row
);
5309 wxGridSizeEvent
gridEvt( GetId(),
5313 mouseEv
.GetX() + GetRowLabelSize(),
5314 mouseEv
.GetY() + GetColLabelSize(),
5315 mouseEv
.ControlDown(),
5316 mouseEv
.ShiftDown(),
5318 mouseEv
.MetaDown() );
5319 return GetEventHandler()->ProcessEvent(gridEvt
);
5321 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5323 // Right now, it should _never_ end up here!
5324 wxGridRangeSelectEvent
gridEvt( GetId(),
5328 m_selectingBottomRight
,
5330 mouseEv
.ControlDown(),
5331 mouseEv
.ShiftDown(),
5333 mouseEv
.MetaDown() );
5335 return GetEventHandler()->ProcessEvent(gridEvt
);
5339 wxGridEvent
gridEvt( GetId(),
5343 mouseEv
.GetX() + GetRowLabelSize(),
5344 mouseEv
.GetY() + GetColLabelSize(),
5346 mouseEv
.ControlDown(),
5347 mouseEv
.ShiftDown(),
5349 mouseEv
.MetaDown() );
5350 return GetEventHandler()->ProcessEvent(gridEvt
);
5355 // Generate a grid event of specified type and return the result
5356 // of ProcessEvent().
5358 bool wxGrid::SendEvent( const wxEventType type
,
5361 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5363 int rowOrCol
= (row
== -1 ? col
: row
);
5365 wxGridSizeEvent
gridEvt( GetId(),
5370 return GetEventHandler()->ProcessEvent(gridEvt
);
5374 wxGridEvent
gridEvt( GetId(),
5379 return GetEventHandler()->ProcessEvent(gridEvt
);
5384 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5386 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5390 // This is just here to make sure that CalcDimensions gets called when
5391 // the grid view is resized... then the size event is skipped to allow
5392 // the box sizers to handle everything
5394 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5401 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5403 if ( m_inOnKeyDown
)
5405 // shouldn't be here - we are going round in circles...
5407 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5410 m_inOnKeyDown
= TRUE
;
5412 // propagate the event up and see if it gets processed
5414 wxWindow
*parent
= GetParent();
5415 wxKeyEvent
keyEvt( event
);
5416 keyEvt
.SetEventObject( parent
);
5418 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5421 // try local handlers
5423 switch ( event
.KeyCode() )
5426 if ( event
.ControlDown() )
5428 MoveCursorUpBlock( event
.ShiftDown() );
5432 MoveCursorUp( event
.ShiftDown() );
5437 if ( event
.ControlDown() )
5439 MoveCursorDownBlock( event
.ShiftDown() );
5443 MoveCursorDown( event
.ShiftDown() );
5448 if ( event
.ControlDown() )
5450 MoveCursorLeftBlock( event
.ShiftDown() );
5454 MoveCursorLeft( event
.ShiftDown() );
5459 if ( event
.ControlDown() )
5461 MoveCursorRightBlock( event
.ShiftDown() );
5465 MoveCursorRight( event
.ShiftDown() );
5470 case WXK_NUMPAD_ENTER
:
5471 if ( event
.ControlDown() )
5473 event
.Skip(); // to let the edit control have the return
5477 if ( GetGridCursorRow() < GetNumberRows()-1 )
5479 MoveCursorDown( event
.ShiftDown() );
5483 // at the bottom of a column
5484 HideCellEditControl();
5485 SaveEditControlValue();
5495 if (event
.ShiftDown())
5497 if ( GetGridCursorCol() > 0 )
5499 MoveCursorLeft( FALSE
);
5504 HideCellEditControl();
5505 SaveEditControlValue();
5510 if ( GetGridCursorCol() < GetNumberCols()-1 )
5512 MoveCursorRight( FALSE
);
5517 HideCellEditControl();
5518 SaveEditControlValue();
5524 if ( event
.ControlDown() )
5526 MakeCellVisible( 0, 0 );
5527 SetCurrentCell( 0, 0 );
5536 if ( event
.ControlDown() )
5538 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5539 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5556 if ( event
.ControlDown() )
5558 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5559 m_currentCellCoords
.GetCol(),
5560 event
.ControlDown(),
5566 if ( !IsEditable() )
5568 MoveCursorRight( FALSE
);
5571 // Otherwise fall through to default
5574 // is it possible to edit the current cell at all?
5575 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5577 // yes, now check whether the cells editor accepts the key
5578 int row
= m_currentCellCoords
.GetRow();
5579 int col
= m_currentCellCoords
.GetCol();
5580 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5581 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5583 // <F2> is special and will always start editing, for
5584 // other keys - ask the editor itself
5585 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5586 || editor
->IsAcceptedKey(event
) )
5588 EnableCellEditControl();
5589 editor
->StartingKey(event
);
5601 // let others process char events with modifiers or all
5602 // char events for readonly cells
5609 m_inOnKeyDown
= FALSE
;
5612 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5614 // try local handlers
5616 if ( event
.KeyCode() == WXK_SHIFT
)
5618 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5619 m_selectingBottomRight
!= wxGridNoCellCoords
)
5620 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5621 m_selectingTopLeft
.GetCol(),
5622 m_selectingBottomRight
.GetRow(),
5623 m_selectingBottomRight
.GetCol(),
5624 event
.ControlDown(),
5628 m_selectingTopLeft
= wxGridNoCellCoords
;
5629 m_selectingBottomRight
= wxGridNoCellCoords
;
5630 m_selectingKeyboard
= wxGridNoCellCoords
;
5634 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5638 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5640 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5642 // the event has been intercepted - do nothing
5646 wxClientDC
dc(m_gridWin
);
5649 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5651 HideCellEditControl();
5652 DisableCellEditControl();
5654 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5657 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5658 if ( !m_gridLinesEnabled
)
5666 CalcCellsExposed( r
);
5668 // Otherwise refresh redraws the highlight!
5669 m_currentCellCoords
= coords
;
5671 DrawGridCellArea(dc
);
5672 DrawAllGridLines( dc
, r
);
5676 m_currentCellCoords
= coords
;
5678 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5679 DrawCellHighlight(dc
, attr
);
5684 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5687 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5689 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5692 rightCol
= GetNumberCols() - 1;
5694 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5697 bottomRow
= GetNumberRows() - 1;
5699 if ( topRow
> bottomRow
)
5706 if ( leftCol
> rightCol
)
5713 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5714 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5716 if ( m_selectingTopLeft
!= updateTopLeft
||
5717 m_selectingBottomRight
!= updateBottomRight
)
5719 // Compute two optimal update rectangles:
5720 // Either one rectangle is a real subset of the
5721 // other, or they are (almost) disjoint!
5723 bool need_refresh
[4];
5727 need_refresh
[3] = FALSE
;
5730 // Store intermediate values
5731 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5732 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5733 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5734 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5736 // Determine the outer/inner coordinates.
5737 if (oldLeft
> leftCol
)
5743 if (oldTop
> topRow
)
5749 if (oldRight
< rightCol
)
5752 oldRight
= rightCol
;
5755 if (oldBottom
< bottomRow
)
5758 oldBottom
= bottomRow
;
5762 // Now, either the stuff marked old is the outer
5763 // rectangle or we don't have a situation where one
5764 // is contained in the other.
5766 if ( oldLeft
< leftCol
)
5768 need_refresh
[0] = TRUE
;
5769 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5771 wxGridCellCoords ( oldBottom
,
5775 if ( oldTop
< topRow
)
5777 need_refresh
[1] = TRUE
;
5778 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5780 wxGridCellCoords ( topRow
- 1,
5784 if ( oldRight
> rightCol
)
5786 need_refresh
[2] = TRUE
;
5787 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5789 wxGridCellCoords ( oldBottom
,
5793 if ( oldBottom
> bottomRow
)
5795 need_refresh
[3] = TRUE
;
5796 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5798 wxGridCellCoords ( oldBottom
,
5804 m_selectingTopLeft
= updateTopLeft
;
5805 m_selectingBottomRight
= updateBottomRight
;
5807 // various Refresh() calls
5808 for (i
= 0; i
< 4; i
++ )
5809 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5810 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5813 // never generate an event as it will be generated from
5814 // wxGridSelection::SelectBlock!
5815 // (old comment from when this was the body of SelectBlock)
5819 // ------ functions to get/send data (see also public functions)
5822 bool wxGrid::GetModelValues()
5826 // all we need to do is repaint the grid
5828 m_gridWin
->Refresh();
5836 bool wxGrid::SetModelValues()
5842 for ( row
= 0; row
< m_numRows
; row
++ )
5844 for ( col
= 0; col
< m_numCols
; col
++ )
5846 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5858 // Note - this function only draws cells that are in the list of
5859 // exposed cells (usually set from the update region by
5860 // CalcExposedCells)
5862 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5864 if ( !m_numRows
|| !m_numCols
) return;
5867 size_t numCells
= m_cellsExposed
.GetCount();
5869 for ( i
= 0; i
< numCells
; i
++ )
5871 DrawCell( dc
, m_cellsExposed
[i
] );
5876 void wxGrid::DrawGridSpace( wxDC
& dc
)
5879 m_gridWin
->GetClientSize( &cw
, &ch
);
5882 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5884 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
5885 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
5887 if ( right
> rightCol
|| bottom
> bottomRow
)
5890 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5892 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5893 dc
.SetPen( *wxTRANSPARENT_PEN
);
5895 if ( right
> rightCol
)
5897 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5900 if ( bottom
> bottomRow
)
5902 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5908 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5910 int row
= coords
.GetRow();
5911 int col
= coords
.GetCol();
5913 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5916 // we draw the cell border ourselves
5917 #if !WXGRID_DRAW_LINES
5918 if ( m_gridLinesEnabled
)
5919 DrawCellBorder( dc
, coords
);
5922 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5924 bool isCurrent
= coords
== m_currentCellCoords
;
5926 wxRect rect
= CellToRect( row
, col
);
5928 // if the editor is shown, we should use it and not the renderer
5929 // Note: However, only if it is really _shown_, i.e. not hidden!
5930 if ( isCurrent
&& IsCellEditControlShown() )
5932 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5933 editor
->PaintBackground(rect
, attr
);
5938 // but all the rest is drawn by the cell renderer and hence may be
5940 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5941 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5948 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5950 int row
= m_currentCellCoords
.GetRow();
5951 int col
= m_currentCellCoords
.GetCol();
5953 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5956 wxRect rect
= CellToRect(row
, col
);
5958 // hmmm... what could we do here to show that the cell is disabled?
5959 // for now, I just draw a thinner border than for the other ones, but
5960 // it doesn't look really good
5961 dc
.SetPen(wxPen(m_cellHighlightColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5962 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5964 dc
.DrawRectangle(rect
);
5967 // VZ: my experiments with 3d borders...
5969 // how to properly set colours for arbitrary bg?
5970 wxCoord x1
= rect
.x
,
5972 x2
= rect
.x
+ rect
.width
-1,
5973 y2
= rect
.y
+ rect
.height
-1;
5975 dc
.SetPen(*wxWHITE_PEN
);
5976 dc
.DrawLine(x1
, y1
, x2
, y1
);
5977 dc
.DrawLine(x1
, y1
, x1
, y2
);
5979 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5980 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5982 dc
.SetPen(*wxBLACK_PEN
);
5983 dc
.DrawLine(x1
, y2
, x2
, y2
);
5984 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5989 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5991 int row
= coords
.GetRow();
5992 int col
= coords
.GetCol();
5993 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5996 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5998 // right hand border
6000 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6001 GetColRight(col
), GetRowBottom(row
) );
6005 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6006 GetColRight(col
), GetRowBottom(row
) );
6009 void wxGrid::DrawHighlight(wxDC
& dc
)
6011 // This if block was previously in wxGrid::OnPaint but that doesn't
6012 // seem to get called under wxGTK - MB
6014 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6015 m_numRows
&& m_numCols
)
6017 m_currentCellCoords
.Set(0, 0);
6020 if ( IsCellEditControlShown() )
6022 // don't show highlight when the edit control is shown
6026 // if the active cell was repainted, repaint its highlight too because it
6027 // might have been damaged by the grid lines
6028 size_t count
= m_cellsExposed
.GetCount();
6029 for ( size_t n
= 0; n
< count
; n
++ )
6031 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
6033 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6034 DrawCellHighlight(dc
, attr
);
6042 // TODO: remove this ???
6043 // This is used to redraw all grid lines e.g. when the grid line colour
6046 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6048 if ( !m_gridLinesEnabled
||
6050 !m_numCols
) return;
6052 int top
, bottom
, left
, right
;
6054 #if 0 //#ifndef __WXGTK__
6058 m_gridWin
->GetClientSize(&cw
, &ch
);
6060 // virtual coords of visible area
6062 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6063 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6068 reg
.GetBox(x
, y
, w
, h
);
6069 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6070 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6074 m_gridWin
->GetClientSize(&cw
, &ch
);
6075 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6076 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6079 // avoid drawing grid lines past the last row and col
6081 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6082 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6084 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6086 // horizontal grid lines
6089 for ( i
= 0; i
< m_numRows
; i
++ )
6091 int bot
= GetRowBottom(i
) - 1;
6100 dc
.DrawLine( left
, bot
, right
, bot
);
6105 // vertical grid lines
6107 for ( i
= 0; i
< m_numCols
; i
++ )
6109 int colRight
= GetColRight(i
) - 1;
6110 if ( colRight
> right
)
6115 if ( colRight
>= left
)
6117 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6123 void wxGrid::DrawRowLabels( wxDC
& dc
)
6125 if ( !m_numRows
) return;
6128 size_t numLabels
= m_rowLabelsExposed
.GetCount();
6130 for ( i
= 0; i
< numLabels
; i
++ )
6132 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
6137 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6139 if ( GetRowHeight(row
) <= 0 )
6142 int rowTop
= GetRowTop(row
),
6143 rowBottom
= GetRowBottom(row
) - 1;
6145 dc
.SetPen( *wxBLACK_PEN
);
6146 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6147 m_rowLabelWidth
-1, rowBottom
);
6149 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6151 dc
.SetPen( *wxWHITE_PEN
);
6152 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6153 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6155 dc
.SetBackgroundMode( wxTRANSPARENT
);
6156 dc
.SetTextForeground( GetLabelTextColour() );
6157 dc
.SetFont( GetLabelFont() );
6160 GetRowLabelAlignment( &hAlign
, &vAlign
);
6164 rect
.SetY( GetRowTop(row
) + 2 );
6165 rect
.SetWidth( m_rowLabelWidth
- 4 );
6166 rect
.SetHeight( GetRowHeight(row
) - 4 );
6167 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6171 void wxGrid::DrawColLabels( wxDC
& dc
)
6173 if ( !m_numCols
) return;
6176 size_t numLabels
= m_colLabelsExposed
.GetCount();
6178 for ( i
= 0; i
< numLabels
; i
++ )
6180 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
6185 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6187 if ( GetColWidth(col
) <= 0 )
6190 int colLeft
= GetColLeft(col
),
6191 colRight
= GetColRight(col
) - 1;
6193 dc
.SetPen( *wxBLACK_PEN
);
6194 dc
.DrawLine( colRight
, 0,
6195 colRight
, m_colLabelHeight
-1 );
6197 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6198 colRight
, m_colLabelHeight
-1 );
6200 dc
.SetPen( *wxWHITE_PEN
);
6201 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6202 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6204 dc
.SetBackgroundMode( wxTRANSPARENT
);
6205 dc
.SetTextForeground( GetLabelTextColour() );
6206 dc
.SetFont( GetLabelFont() );
6208 dc
.SetBackgroundMode( wxTRANSPARENT
);
6209 dc
.SetTextForeground( GetLabelTextColour() );
6210 dc
.SetFont( GetLabelFont() );
6213 GetColLabelAlignment( &hAlign
, &vAlign
);
6216 rect
.SetX( colLeft
+ 2 );
6218 rect
.SetWidth( GetColWidth(col
) - 4 );
6219 rect
.SetHeight( m_colLabelHeight
- 4 );
6220 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6224 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6225 const wxString
& value
,
6230 long textWidth
, textHeight
;
6231 long lineWidth
, lineHeight
;
6232 wxArrayString lines
;
6234 dc
.SetClippingRegion( rect
);
6235 StringToLines( value
, lines
);
6236 if ( lines
.GetCount() )
6238 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6239 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6242 switch ( horizAlign
)
6245 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6248 case wxALIGN_CENTRE
:
6249 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6258 switch ( vertAlign
)
6260 case wxALIGN_BOTTOM
:
6261 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6264 case wxALIGN_CENTRE
:
6265 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6274 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6276 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6281 dc
.DestroyClippingRegion();
6285 // Split multi line text up into an array of strings. Any existing
6286 // contents of the string array are preserved.
6288 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6292 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6293 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6295 while ( startPos
< (int)tVal
.Length() )
6297 pos
= tVal
.Mid(startPos
).Find( eol
);
6302 else if ( pos
== 0 )
6304 lines
.Add( wxEmptyString
);
6308 lines
.Add( value
.Mid(startPos
, pos
) );
6312 if ( startPos
< (int)value
.Length() )
6314 lines
.Add( value
.Mid( startPos
) );
6319 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6320 wxArrayString
& lines
,
6321 long *width
, long *height
)
6328 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6330 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6331 w
= wxMax( w
, lineW
);
6340 // ------ Batch processing.
6342 void wxGrid::EndBatch()
6344 if ( m_batchCount
> 0 )
6347 if ( !m_batchCount
)
6350 m_rowLabelWin
->Refresh();
6351 m_colLabelWin
->Refresh();
6352 m_cornerLabelWin
->Refresh();
6353 m_gridWin
->Refresh();
6358 // Use this, rather than wxWindow::Refresh(), to force an immediate
6359 // repainting of the grid. Has no effect if you are already inside a
6360 // BeginBatch / EndBatch block.
6362 void wxGrid::ForceRefresh()
6370 // ------ Edit control functions
6374 void wxGrid::EnableEditing( bool edit
)
6376 // TODO: improve this ?
6378 if ( edit
!= m_editable
)
6380 if(!edit
) EnableCellEditControl(edit
);
6386 void wxGrid::EnableCellEditControl( bool enable
)
6391 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6392 SetCurrentCell( 0, 0 );
6394 if ( enable
!= m_cellEditCtrlEnabled
)
6396 // TODO allow the app to Veto() this event?
6397 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
6401 // this should be checked by the caller!
6402 wxASSERT_MSG( CanEnableCellControl(),
6403 _T("can't enable editing for this cell!") );
6405 // do it before ShowCellEditControl()
6406 m_cellEditCtrlEnabled
= enable
;
6408 ShowCellEditControl();
6412 HideCellEditControl();
6413 SaveEditControlValue();
6415 // do it after HideCellEditControl()
6416 m_cellEditCtrlEnabled
= enable
;
6421 bool wxGrid::IsCurrentCellReadOnly() const
6424 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6425 bool readonly
= attr
->IsReadOnly();
6431 bool wxGrid::CanEnableCellControl() const
6433 return m_editable
&& !IsCurrentCellReadOnly();
6436 bool wxGrid::IsCellEditControlEnabled() const
6438 // the cell edit control might be disable for all cells or just for the
6439 // current one if it's read only
6440 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6443 bool wxGrid::IsCellEditControlShown() const
6445 bool isShown
= FALSE
;
6447 if ( m_cellEditCtrlEnabled
)
6449 int row
= m_currentCellCoords
.GetRow();
6450 int col
= m_currentCellCoords
.GetCol();
6451 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6452 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6457 if ( editor
->IsCreated() )
6459 isShown
= editor
->GetControl()->IsShown();
6469 void wxGrid::ShowCellEditControl()
6471 if ( IsCellEditControlEnabled() )
6473 if ( !IsVisible( m_currentCellCoords
) )
6475 m_cellEditCtrlEnabled
= FALSE
;
6480 wxRect rect
= CellToRect( m_currentCellCoords
);
6481 int row
= m_currentCellCoords
.GetRow();
6482 int col
= m_currentCellCoords
.GetCol();
6484 // convert to scrolled coords
6486 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6488 // done in PaintBackground()
6490 // erase the highlight and the cell contents because the editor
6491 // might not cover the entire cell
6492 wxClientDC
dc( m_gridWin
);
6494 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6495 dc
.SetPen(*wxTRANSPARENT_PEN
);
6496 dc
.DrawRectangle(rect
);
6499 // cell is shifted by one pixel
6503 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6504 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6505 if ( !editor
->IsCreated() )
6507 editor
->Create(m_gridWin
, -1,
6508 new wxGridCellEditorEvtHandler(this, editor
));
6511 editor
->Show( TRUE
, attr
);
6513 editor
->SetSize( rect
);
6515 editor
->BeginEdit(row
, col
, this);
6524 void wxGrid::HideCellEditControl()
6526 if ( IsCellEditControlEnabled() )
6528 int row
= m_currentCellCoords
.GetRow();
6529 int col
= m_currentCellCoords
.GetCol();
6531 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6532 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6533 editor
->Show( FALSE
);
6536 m_gridWin
->SetFocus();
6537 wxRect
rect( CellToRect( row
, col
) );
6538 m_gridWin
->Refresh( FALSE
, &rect
);
6543 void wxGrid::SaveEditControlValue()
6545 if ( IsCellEditControlEnabled() )
6547 int row
= m_currentCellCoords
.GetRow();
6548 int col
= m_currentCellCoords
.GetCol();
6550 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6551 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6552 bool changed
= editor
->EndEdit(row
, col
, this);
6559 SendEvent( wxEVT_GRID_CELL_CHANGE
,
6560 m_currentCellCoords
.GetRow(),
6561 m_currentCellCoords
.GetCol() );
6568 // ------ Grid location functions
6569 // Note that all of these functions work with the logical coordinates of
6570 // grid cells and labels so you will need to convert from device
6571 // coordinates for mouse events etc.
6574 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6576 int row
= YToRow(y
);
6577 int col
= XToCol(x
);
6579 if ( row
== -1 || col
== -1 )
6581 coords
= wxGridNoCellCoords
;
6585 coords
.Set( row
, col
);
6590 int wxGrid::YToRow( int y
)
6594 for ( i
= 0; i
< m_numRows
; i
++ )
6596 if ( y
< GetRowBottom(i
) )
6604 int wxGrid::XToCol( int x
)
6608 for ( i
= 0; i
< m_numCols
; i
++ )
6610 if ( x
< GetColRight(i
) )
6618 // return the row number that that the y coord is near the edge of, or
6619 // -1 if not near an edge
6621 int wxGrid::YToEdgeOfRow( int y
)
6625 for ( i
= 0; i
< m_numRows
; i
++ )
6627 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6629 d
= abs( y
- GetRowBottom(i
) );
6630 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6639 // return the col number that that the x coord is near the edge of, or
6640 // -1 if not near an edge
6642 int wxGrid::XToEdgeOfCol( int x
)
6646 for ( i
= 0; i
< m_numCols
; i
++ )
6648 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6650 d
= abs( x
- GetColRight(i
) );
6651 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6660 wxRect
wxGrid::CellToRect( int row
, int col
)
6662 wxRect
rect( -1, -1, -1, -1 );
6664 if ( row
>= 0 && row
< m_numRows
&&
6665 col
>= 0 && col
< m_numCols
)
6667 rect
.x
= GetColLeft(col
);
6668 rect
.y
= GetRowTop(row
);
6669 rect
.width
= GetColWidth(col
);
6670 rect
.height
= GetRowHeight(row
);
6673 // if grid lines are enabled, then the area of the cell is a bit smaller
6674 if (m_gridLinesEnabled
) {
6682 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6684 // get the cell rectangle in logical coords
6686 wxRect
r( CellToRect( row
, col
) );
6688 // convert to device coords
6690 int left
, top
, right
, bottom
;
6691 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6692 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6694 // check against the client area of the grid window
6697 m_gridWin
->GetClientSize( &cw
, &ch
);
6699 if ( wholeCellVisible
)
6701 // is the cell wholly visible ?
6703 return ( left
>= 0 && right
<= cw
&&
6704 top
>= 0 && bottom
<= ch
);
6708 // is the cell partly visible ?
6710 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6711 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6716 // make the specified cell location visible by doing a minimal amount
6719 void wxGrid::MakeCellVisible( int row
, int col
)
6722 int xpos
= -1, ypos
= -1;
6724 if ( row
>= 0 && row
< m_numRows
&&
6725 col
>= 0 && col
< m_numCols
)
6727 // get the cell rectangle in logical coords
6729 wxRect
r( CellToRect( row
, col
) );
6731 // convert to device coords
6733 int left
, top
, right
, bottom
;
6734 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6735 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6738 m_gridWin
->GetClientSize( &cw
, &ch
);
6744 else if ( bottom
> ch
)
6746 int h
= r
.GetHeight();
6748 for ( i
= row
-1; i
>= 0; i
-- )
6750 int rowHeight
= GetRowHeight(i
);
6751 if ( h
+ rowHeight
> ch
)
6758 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6759 // have rounding errors (this is important, because if we do, we
6760 // might not scroll at all and some cells won't be redrawn)
6761 ypos
+= GRID_SCROLL_LINE
/ 2;
6768 else if ( right
> cw
)
6770 int w
= r
.GetWidth();
6772 for ( i
= col
-1; i
>= 0; i
-- )
6774 int colWidth
= GetColWidth(i
);
6775 if ( w
+ colWidth
> cw
)
6782 // see comment for ypos above
6783 xpos
+= GRID_SCROLL_LINE
/ 2;
6786 if ( xpos
!= -1 || ypos
!= -1 )
6788 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6789 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6790 Scroll( xpos
, ypos
);
6798 // ------ Grid cursor movement functions
6801 bool wxGrid::MoveCursorUp( bool expandSelection
)
6803 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6804 m_currentCellCoords
.GetRow() >= 0 )
6806 if ( expandSelection
)
6808 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6809 m_selectingKeyboard
= m_currentCellCoords
;
6810 if ( m_selectingKeyboard
.GetRow() > 0 )
6812 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6813 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6814 m_selectingKeyboard
.GetCol() );
6815 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6818 else if ( m_currentCellCoords
.GetRow() > 0 )
6821 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6822 m_currentCellCoords
.GetCol() );
6823 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6824 m_currentCellCoords
.GetCol() );
6835 bool wxGrid::MoveCursorDown( bool expandSelection
)
6837 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6838 m_currentCellCoords
.GetRow() < m_numRows
)
6840 if ( expandSelection
)
6842 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6843 m_selectingKeyboard
= m_currentCellCoords
;
6844 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
6846 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
6847 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6848 m_selectingKeyboard
.GetCol() );
6849 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6852 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
6855 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
6856 m_currentCellCoords
.GetCol() );
6857 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
6858 m_currentCellCoords
.GetCol() );
6869 bool wxGrid::MoveCursorLeft( bool expandSelection
)
6871 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6872 m_currentCellCoords
.GetCol() >= 0 )
6874 if ( expandSelection
)
6876 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6877 m_selectingKeyboard
= m_currentCellCoords
;
6878 if ( m_selectingKeyboard
.GetCol() > 0 )
6880 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
6881 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6882 m_selectingKeyboard
.GetCol() );
6883 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6886 else if ( m_currentCellCoords
.GetCol() > 0 )
6889 MakeCellVisible( m_currentCellCoords
.GetRow(),
6890 m_currentCellCoords
.GetCol() - 1 );
6891 SetCurrentCell( m_currentCellCoords
.GetRow(),
6892 m_currentCellCoords
.GetCol() - 1 );
6903 bool wxGrid::MoveCursorRight( bool expandSelection
)
6905 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6906 m_currentCellCoords
.GetCol() < m_numCols
)
6908 if ( expandSelection
)
6910 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6911 m_selectingKeyboard
= m_currentCellCoords
;
6912 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
6914 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
6915 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6916 m_selectingKeyboard
.GetCol() );
6917 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6920 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
6923 MakeCellVisible( m_currentCellCoords
.GetRow(),
6924 m_currentCellCoords
.GetCol() + 1 );
6925 SetCurrentCell( m_currentCellCoords
.GetRow(),
6926 m_currentCellCoords
.GetCol() + 1 );
6937 bool wxGrid::MovePageUp()
6939 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6941 int row
= m_currentCellCoords
.GetRow();
6945 m_gridWin
->GetClientSize( &cw
, &ch
);
6947 int y
= GetRowTop(row
);
6948 int newRow
= YToRow( y
- ch
+ 1 );
6953 else if ( newRow
== row
)
6958 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6959 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6967 bool wxGrid::MovePageDown()
6969 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6971 int row
= m_currentCellCoords
.GetRow();
6972 if ( row
< m_numRows
)
6975 m_gridWin
->GetClientSize( &cw
, &ch
);
6977 int y
= GetRowTop(row
);
6978 int newRow
= YToRow( y
+ ch
);
6981 newRow
= m_numRows
- 1;
6983 else if ( newRow
== row
)
6988 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6989 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6997 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7000 m_currentCellCoords
!= wxGridNoCellCoords
&&
7001 m_currentCellCoords
.GetRow() > 0 )
7003 int row
= m_currentCellCoords
.GetRow();
7004 int col
= m_currentCellCoords
.GetCol();
7006 if ( m_table
->IsEmptyCell(row
, col
) )
7008 // starting in an empty cell: find the next block of
7014 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7017 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7019 // starting at the top of a block: find the next block
7025 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7030 // starting within a block: find the top of the block
7035 if ( m_table
->IsEmptyCell(row
, col
) )
7043 MakeCellVisible( row
, col
);
7044 if ( expandSelection
)
7046 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7047 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7052 SetCurrentCell( row
, col
);
7060 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7063 m_currentCellCoords
!= wxGridNoCellCoords
&&
7064 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7066 int row
= m_currentCellCoords
.GetRow();
7067 int col
= m_currentCellCoords
.GetCol();
7069 if ( m_table
->IsEmptyCell(row
, col
) )
7071 // starting in an empty cell: find the next block of
7074 while ( row
< m_numRows
-1 )
7077 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7080 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7082 // starting at the bottom of a block: find the next block
7085 while ( row
< m_numRows
-1 )
7088 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7093 // starting within a block: find the bottom of the block
7095 while ( row
< m_numRows
-1 )
7098 if ( m_table
->IsEmptyCell(row
, col
) )
7106 MakeCellVisible( row
, col
);
7107 if ( expandSelection
)
7109 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7110 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7115 SetCurrentCell( row
, col
);
7124 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7127 m_currentCellCoords
!= wxGridNoCellCoords
&&
7128 m_currentCellCoords
.GetCol() > 0 )
7130 int row
= m_currentCellCoords
.GetRow();
7131 int col
= m_currentCellCoords
.GetCol();
7133 if ( m_table
->IsEmptyCell(row
, col
) )
7135 // starting in an empty cell: find the next block of
7141 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7144 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7146 // starting at the left of a block: find the next block
7152 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7157 // starting within a block: find the left of the block
7162 if ( m_table
->IsEmptyCell(row
, col
) )
7170 MakeCellVisible( row
, col
);
7171 if ( expandSelection
)
7173 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7174 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7179 SetCurrentCell( row
, col
);
7188 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7191 m_currentCellCoords
!= wxGridNoCellCoords
&&
7192 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7194 int row
= m_currentCellCoords
.GetRow();
7195 int col
= m_currentCellCoords
.GetCol();
7197 if ( m_table
->IsEmptyCell(row
, col
) )
7199 // starting in an empty cell: find the next block of
7202 while ( col
< m_numCols
-1 )
7205 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7208 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7210 // starting at the right of a block: find the next block
7213 while ( col
< m_numCols
-1 )
7216 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7221 // starting within a block: find the right of the block
7223 while ( col
< m_numCols
-1 )
7226 if ( m_table
->IsEmptyCell(row
, col
) )
7234 MakeCellVisible( row
, col
);
7235 if ( expandSelection
)
7237 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7238 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7243 SetCurrentCell( row
, col
);
7255 // ------ Label values and formatting
7258 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7260 *horiz
= m_rowLabelHorizAlign
;
7261 *vert
= m_rowLabelVertAlign
;
7264 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7266 *horiz
= m_colLabelHorizAlign
;
7267 *vert
= m_colLabelVertAlign
;
7270 wxString
wxGrid::GetRowLabelValue( int row
)
7274 return m_table
->GetRowLabelValue( row
);
7284 wxString
wxGrid::GetColLabelValue( int col
)
7288 return m_table
->GetColLabelValue( col
);
7299 void wxGrid::SetRowLabelSize( int width
)
7301 width
= wxMax( width
, 0 );
7302 if ( width
!= m_rowLabelWidth
)
7306 m_rowLabelWin
->Show( FALSE
);
7307 m_cornerLabelWin
->Show( FALSE
);
7309 else if ( m_rowLabelWidth
== 0 )
7311 m_rowLabelWin
->Show( TRUE
);
7312 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7315 m_rowLabelWidth
= width
;
7322 void wxGrid::SetColLabelSize( int height
)
7324 height
= wxMax( height
, 0 );
7325 if ( height
!= m_colLabelHeight
)
7329 m_colLabelWin
->Show( FALSE
);
7330 m_cornerLabelWin
->Show( FALSE
);
7332 else if ( m_colLabelHeight
== 0 )
7334 m_colLabelWin
->Show( TRUE
);
7335 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7338 m_colLabelHeight
= height
;
7345 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7347 if ( m_labelBackgroundColour
!= colour
)
7349 m_labelBackgroundColour
= colour
;
7350 m_rowLabelWin
->SetBackgroundColour( colour
);
7351 m_colLabelWin
->SetBackgroundColour( colour
);
7352 m_cornerLabelWin
->SetBackgroundColour( colour
);
7354 if ( !GetBatchCount() )
7356 m_rowLabelWin
->Refresh();
7357 m_colLabelWin
->Refresh();
7358 m_cornerLabelWin
->Refresh();
7363 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7365 if ( m_labelTextColour
!= colour
)
7367 m_labelTextColour
= colour
;
7368 if ( !GetBatchCount() )
7370 m_rowLabelWin
->Refresh();
7371 m_colLabelWin
->Refresh();
7376 void wxGrid::SetLabelFont( const wxFont
& font
)
7379 if ( !GetBatchCount() )
7381 m_rowLabelWin
->Refresh();
7382 m_colLabelWin
->Refresh();
7386 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7388 // allow old (incorrect) defs to be used
7391 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7392 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7393 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7398 case wxTOP
: vert
= wxALIGN_TOP
; break;
7399 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7400 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7403 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7405 m_rowLabelHorizAlign
= horiz
;
7408 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7410 m_rowLabelVertAlign
= vert
;
7413 if ( !GetBatchCount() )
7415 m_rowLabelWin
->Refresh();
7419 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7421 // allow old (incorrect) defs to be used
7424 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7425 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7426 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7431 case wxTOP
: vert
= wxALIGN_TOP
; break;
7432 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7433 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7436 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7438 m_colLabelHorizAlign
= horiz
;
7441 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7443 m_colLabelVertAlign
= vert
;
7446 if ( !GetBatchCount() )
7448 m_colLabelWin
->Refresh();
7452 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7456 m_table
->SetRowLabelValue( row
, s
);
7457 if ( !GetBatchCount() )
7459 wxRect rect
= CellToRect( row
, 0);
7460 if ( rect
.height
> 0 )
7462 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7464 rect
.width
= m_rowLabelWidth
;
7465 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7471 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7475 m_table
->SetColLabelValue( col
, s
);
7476 if ( !GetBatchCount() )
7478 wxRect rect
= CellToRect( 0, col
);
7479 if ( rect
.width
> 0 )
7481 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7483 rect
.height
= m_colLabelHeight
;
7484 m_colLabelWin
->Refresh( TRUE
, &rect
);
7490 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7492 if ( m_gridLineColour
!= colour
)
7494 m_gridLineColour
= colour
;
7496 wxClientDC
dc( m_gridWin
);
7498 DrawAllGridLines( dc
, wxRegion() );
7503 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7505 if ( m_cellHighlightColour
!= colour
)
7507 m_cellHighlightColour
= colour
;
7509 wxClientDC
dc( m_gridWin
);
7511 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7512 DrawCellHighlight(dc
, attr
);
7517 void wxGrid::EnableGridLines( bool enable
)
7519 if ( enable
!= m_gridLinesEnabled
)
7521 m_gridLinesEnabled
= enable
;
7523 if ( !GetBatchCount() )
7527 wxClientDC
dc( m_gridWin
);
7529 DrawAllGridLines( dc
, wxRegion() );
7533 m_gridWin
->Refresh();
7540 int wxGrid::GetDefaultRowSize()
7542 return m_defaultRowHeight
;
7545 int wxGrid::GetRowSize( int row
)
7547 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7549 return GetRowHeight(row
);
7552 int wxGrid::GetDefaultColSize()
7554 return m_defaultColWidth
;
7557 int wxGrid::GetColSize( int col
)
7559 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7561 return GetColWidth(col
);
7564 // ============================================================================
7565 // access to the grid attributes: each of them has a default value in the grid
7566 // itself and may be overidden on a per-cell basis
7567 // ============================================================================
7569 // ----------------------------------------------------------------------------
7570 // setting default attributes
7571 // ----------------------------------------------------------------------------
7573 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7575 m_defaultCellAttr
->SetBackgroundColour(col
);
7577 m_gridWin
->SetBackgroundColour(col
);
7581 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7583 m_defaultCellAttr
->SetTextColour(col
);
7586 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7588 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7591 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7593 m_defaultCellAttr
->SetFont(font
);
7596 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7598 m_defaultCellAttr
->SetRenderer(renderer
);
7601 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7603 m_defaultCellAttr
->SetEditor(editor
);
7606 // ----------------------------------------------------------------------------
7607 // access to the default attrbiutes
7608 // ----------------------------------------------------------------------------
7610 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7612 return m_defaultCellAttr
->GetBackgroundColour();
7615 wxColour
wxGrid::GetDefaultCellTextColour()
7617 return m_defaultCellAttr
->GetTextColour();
7620 wxFont
wxGrid::GetDefaultCellFont()
7622 return m_defaultCellAttr
->GetFont();
7625 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7627 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7630 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7632 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7635 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7637 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7640 // ----------------------------------------------------------------------------
7641 // access to cell attributes
7642 // ----------------------------------------------------------------------------
7644 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7646 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7647 wxColour colour
= attr
->GetBackgroundColour();
7652 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7654 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7655 wxColour colour
= attr
->GetTextColour();
7660 wxFont
wxGrid::GetCellFont( int row
, int col
)
7662 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7663 wxFont font
= attr
->GetFont();
7668 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7670 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7671 attr
->GetAlignment(horiz
, vert
);
7675 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7677 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7678 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7684 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7686 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7687 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7693 bool wxGrid::IsReadOnly(int row
, int col
) const
7695 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7696 bool isReadOnly
= attr
->IsReadOnly();
7701 // ----------------------------------------------------------------------------
7702 // attribute support: cache, automatic provider creation, ...
7703 // ----------------------------------------------------------------------------
7705 bool wxGrid::CanHaveAttributes()
7712 return m_table
->CanHaveAttributes();
7715 void wxGrid::ClearAttrCache()
7717 if ( m_attrCache
.row
!= -1 )
7719 wxSafeDecRef(m_attrCache
.attr
);
7720 m_attrCache
.row
= -1;
7724 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7726 wxGrid
*self
= (wxGrid
*)this; // const_cast
7728 self
->ClearAttrCache();
7729 self
->m_attrCache
.row
= row
;
7730 self
->m_attrCache
.col
= col
;
7731 self
->m_attrCache
.attr
= attr
;
7735 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7737 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7739 *attr
= m_attrCache
.attr
;
7740 wxSafeIncRef(m_attrCache
.attr
);
7742 #ifdef DEBUG_ATTR_CACHE
7743 gs_nAttrCacheHits
++;
7750 #ifdef DEBUG_ATTR_CACHE
7751 gs_nAttrCacheMisses
++;
7757 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7759 wxGridCellAttr
*attr
;
7760 if ( !LookupAttr(row
, col
, &attr
) )
7762 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
7763 CacheAttr(row
, col
, attr
);
7767 attr
->SetDefAttr(m_defaultCellAttr
);
7771 attr
= m_defaultCellAttr
;
7778 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7780 wxGridCellAttr
*attr
;
7781 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
7783 wxASSERT_MSG( m_table
,
7784 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
7786 attr
= m_table
->GetAttr(row
, col
);
7789 attr
= new wxGridCellAttr
;
7791 // artificially inc the ref count to match DecRef() in caller
7794 m_table
->SetAttr(attr
, row
, col
);
7797 CacheAttr(row
, col
, attr
);
7799 attr
->SetDefAttr(m_defaultCellAttr
);
7803 // ----------------------------------------------------------------------------
7804 // setting column attributes (wrappers around SetColAttr)
7805 // ----------------------------------------------------------------------------
7807 void wxGrid::SetColFormatBool(int col
)
7809 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7812 void wxGrid::SetColFormatNumber(int col
)
7814 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7817 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7819 wxString typeName
= wxGRID_VALUE_FLOAT
;
7820 if ( (width
!= -1) || (precision
!= -1) )
7822 typeName
<< _T(':') << width
<< _T(',') << precision
;
7825 SetColFormatCustom(col
, typeName
);
7828 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7830 wxGridCellAttr
*attr
= new wxGridCellAttr
;
7831 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7832 attr
->SetRenderer(renderer
);
7834 SetColAttr(col
, attr
);
7837 // ----------------------------------------------------------------------------
7838 // setting cell attributes: this is forwarded to the table
7839 // ----------------------------------------------------------------------------
7841 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7843 if ( CanHaveAttributes() )
7845 m_table
->SetRowAttr(attr
, row
);
7853 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7855 if ( CanHaveAttributes() )
7857 m_table
->SetColAttr(attr
, col
);
7865 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7867 if ( CanHaveAttributes() )
7869 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7870 attr
->SetBackgroundColour(colour
);
7875 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7877 if ( CanHaveAttributes() )
7879 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7880 attr
->SetTextColour(colour
);
7885 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7887 if ( CanHaveAttributes() )
7889 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7890 attr
->SetFont(font
);
7895 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7897 if ( CanHaveAttributes() )
7899 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7900 attr
->SetAlignment(horiz
, vert
);
7905 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7907 if ( CanHaveAttributes() )
7909 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7910 attr
->SetRenderer(renderer
);
7915 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7917 if ( CanHaveAttributes() )
7919 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7920 attr
->SetEditor(editor
);
7925 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7927 if ( CanHaveAttributes() )
7929 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7930 attr
->SetReadOnly(isReadOnly
);
7935 // ----------------------------------------------------------------------------
7936 // Data type registration
7937 // ----------------------------------------------------------------------------
7939 void wxGrid::RegisterDataType(const wxString
& typeName
,
7940 wxGridCellRenderer
* renderer
,
7941 wxGridCellEditor
* editor
)
7943 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7947 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7949 wxString typeName
= m_table
->GetTypeName(row
, col
);
7950 return GetDefaultEditorForType(typeName
);
7953 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7955 wxString typeName
= m_table
->GetTypeName(row
, col
);
7956 return GetDefaultRendererForType(typeName
);
7960 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7962 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7963 if ( index
== wxNOT_FOUND
)
7965 wxFAIL_MSG(wxT("Unknown data type name"));
7970 return m_typeRegistry
->GetEditor(index
);
7974 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7976 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7977 if ( index
== wxNOT_FOUND
)
7979 wxFAIL_MSG(wxT("Unknown data type name"));
7984 return m_typeRegistry
->GetRenderer(index
);
7988 // ----------------------------------------------------------------------------
7990 // ----------------------------------------------------------------------------
7992 void wxGrid::EnableDragRowSize( bool enable
)
7994 m_canDragRowSize
= enable
;
7998 void wxGrid::EnableDragColSize( bool enable
)
8000 m_canDragColSize
= enable
;
8003 void wxGrid::EnableDragGridSize( bool enable
)
8005 m_canDragGridSize
= enable
;
8009 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8011 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8013 if ( resizeExistingRows
)
8016 if ( !GetBatchCount() )
8021 void wxGrid::SetRowSize( int row
, int height
)
8023 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8025 if ( m_rowHeights
.IsEmpty() )
8027 // need to really create the array
8031 int h
= wxMax( 0, height
);
8032 int diff
= h
- m_rowHeights
[row
];
8034 m_rowHeights
[row
] = h
;
8036 for ( i
= row
; i
< m_numRows
; i
++ )
8038 m_rowBottoms
[i
] += diff
;
8040 if ( !GetBatchCount() )
8044 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8046 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8048 if ( resizeExistingCols
)
8051 if ( !GetBatchCount() )
8056 void wxGrid::SetColSize( int col
, int width
)
8058 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8060 // should we check that it's bigger than GetColMinimalWidth(col) here?
8062 if ( m_colWidths
.IsEmpty() )
8064 // need to really create the array
8068 int w
= wxMax( 0, width
);
8069 int diff
= w
- m_colWidths
[col
];
8070 m_colWidths
[col
] = w
;
8073 for ( i
= col
; i
< m_numCols
; i
++ )
8075 m_colRights
[i
] += diff
;
8077 if ( !GetBatchCount() )
8082 void wxGrid::SetColMinimalWidth( int col
, int width
)
8084 m_colMinWidths
.Put(col
, width
);
8087 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8089 m_rowMinHeights
.Put(row
, width
);
8092 int wxGrid::GetColMinimalWidth(int col
) const
8094 long value
= m_colMinWidths
.Get(col
);
8095 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8098 int wxGrid::GetRowMinimalHeight(int row
) const
8100 long value
= m_rowMinHeights
.Get(row
);
8101 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8104 // ----------------------------------------------------------------------------
8106 // ----------------------------------------------------------------------------
8108 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8110 wxClientDC
dc(m_gridWin
);
8112 // init both of them to avoid compiler warnings, even if weo nly need one
8120 wxCoord extent
, extentMax
= 0;
8121 int max
= column
? m_numRows
: m_numCols
;
8122 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8129 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8130 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8133 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8134 extent
= column
? size
.x
: size
.y
;
8135 if ( extent
> extentMax
)
8146 // now also compare with the column label extent
8148 dc
.SetFont( GetLabelFont() );
8151 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8153 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8155 extent
= column
? w
: h
;
8156 if ( extent
> extentMax
)
8163 // empty column - give default extent (notice that if extentMax is less
8164 // than default extent but != 0, it's ok)
8165 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8171 // leave some space around text
8181 SetColSize(col
, extentMax
);
8182 if ( !GetBatchCount() )
8185 m_gridWin
->GetClientSize( &cw
, &ch
);
8186 wxRect
rect ( CellToRect( 0, col
) );
8188 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8189 rect
.width
= cw
- rect
.x
;
8190 rect
.height
= m_colLabelHeight
;
8191 m_colLabelWin
->Refresh( TRUE
, &rect
);
8195 SetRowSize(row
, extentMax
);
8196 if ( !GetBatchCount() )
8199 m_gridWin
->GetClientSize( &cw
, &ch
);
8200 wxRect
rect ( CellToRect( row
, 0 ) );
8202 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8203 rect
.width
= m_rowLabelWidth
;
8204 rect
.height
= ch
- rect
.y
;
8205 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8211 SetColMinimalWidth(col
, extentMax
);
8213 SetRowMinimalHeight(row
, extentMax
);
8217 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8219 int width
= m_rowLabelWidth
;
8223 for ( int col
= 0; col
< m_numCols
; col
++ )
8227 AutoSizeColumn(col
, setAsMin
);
8230 width
+= GetColWidth(col
);
8237 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8239 int height
= m_colLabelHeight
;
8243 for ( int row
= 0; row
< m_numRows
; row
++ )
8247 AutoSizeRow(row
, setAsMin
);
8250 height
+= GetRowHeight(row
);
8257 void wxGrid::AutoSize()
8260 SetClientSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8263 wxSize
wxGrid::DoGetBestSize() const
8265 // don't set sizes, only calculate them
8266 wxGrid
*self
= (wxGrid
*)this; // const_cast
8268 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8269 self
->SetOrCalcRowSizes(TRUE
));
8278 wxPen
& wxGrid::GetDividerPen() const
8283 // ----------------------------------------------------------------------------
8284 // cell value accessor functions
8285 // ----------------------------------------------------------------------------
8287 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8291 m_table
->SetValue( row
, col
, s
);
8292 if ( !GetBatchCount() )
8294 wxClientDC
dc( m_gridWin
);
8296 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8299 if ( m_currentCellCoords
.GetRow() == row
&&
8300 m_currentCellCoords
.GetCol() == col
&&
8301 IsCellEditControlShown())
8302 // Note: If we are using IsCellEditControlEnabled,
8303 // this interacts badly with calling SetCellValue from
8304 // an EVT_GRID_CELL_CHANGE handler.
8306 HideCellEditControl();
8307 ShowCellEditControl(); // will reread data from table
8314 // ------ Block, row and col selection
8317 void wxGrid::SelectRow( int row
, bool addToSelected
)
8319 if ( IsSelection() && !addToSelected
)
8322 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8326 void wxGrid::SelectCol( int col
, bool addToSelected
)
8328 if ( IsSelection() && !addToSelected
)
8331 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8335 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8336 bool addToSelected
)
8338 if ( IsSelection() && !addToSelected
)
8341 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8342 FALSE
, addToSelected
);
8346 void wxGrid::SelectAll()
8348 if ( m_numRows
> 0 && m_numCols
> 0 )
8349 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8353 // ------ Cell, row and col deselection
8356 void wxGrid::DeselectRow( int row
)
8358 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8360 if ( m_selection
->IsInSelection(row
, 0 ) )
8361 m_selection
->ToggleCellSelection( row
, 0);
8365 int nCols
= GetNumberCols();
8366 for ( int i
= 0; i
< nCols
; i
++ )
8368 if ( m_selection
->IsInSelection(row
, i
) )
8369 m_selection
->ToggleCellSelection( row
, i
);
8374 void wxGrid::DeselectCol( int col
)
8376 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8378 if ( m_selection
->IsInSelection(0, col
) )
8379 m_selection
->ToggleCellSelection( 0, col
);
8383 int nRows
= GetNumberRows();
8384 for ( int i
= 0; i
< nRows
; i
++ )
8386 if ( m_selection
->IsInSelection(i
, col
) )
8387 m_selection
->ToggleCellSelection(i
, col
);
8392 void wxGrid::DeselectCell( int row
, int col
)
8394 if ( m_selection
->IsInSelection(row
, col
) )
8395 m_selection
->ToggleCellSelection(row
, col
);
8398 bool wxGrid::IsSelection()
8400 return ( m_selection
->IsSelection() ||
8401 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8402 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8405 bool wxGrid::IsInSelection( int row
, int col
)
8407 return ( m_selection
->IsInSelection( row
, col
) ||
8408 ( row
>= m_selectingTopLeft
.GetRow() &&
8409 col
>= m_selectingTopLeft
.GetCol() &&
8410 row
<= m_selectingBottomRight
.GetRow() &&
8411 col
<= m_selectingBottomRight
.GetCol() ) );
8414 void wxGrid::ClearSelection()
8416 m_selectingTopLeft
= wxGridNoCellCoords
;
8417 m_selectingBottomRight
= wxGridNoCellCoords
;
8418 m_selection
->ClearSelection();
8422 // This function returns the rectangle that encloses the given block
8423 // in device coords clipped to the client size of the grid window.
8425 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8426 const wxGridCellCoords
&bottomRight
)
8428 wxRect
rect( wxGridNoCellRect
);
8431 cellRect
= CellToRect( topLeft
);
8432 if ( cellRect
!= wxGridNoCellRect
)
8438 rect
= wxRect( 0, 0, 0, 0 );
8441 cellRect
= CellToRect( bottomRight
);
8442 if ( cellRect
!= wxGridNoCellRect
)
8448 return wxGridNoCellRect
;
8451 // convert to scrolled coords
8453 int left
, top
, right
, bottom
;
8454 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8455 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8458 m_gridWin
->GetClientSize( &cw
, &ch
);
8460 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8461 return wxRect( 0, 0, 0, 0);
8463 rect
.SetLeft( wxMax(0, left
) );
8464 rect
.SetTop( wxMax(0, top
) );
8465 rect
.SetRight( wxMin(cw
, right
) );
8466 rect
.SetBottom( wxMin(ch
, bottom
) );
8474 // ------ Grid event classes
8477 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
8479 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8480 int row
, int col
, int x
, int y
, bool sel
,
8481 bool control
, bool shift
, bool alt
, bool meta
)
8482 : wxNotifyEvent( type
, id
)
8489 m_control
= control
;
8494 SetEventObject(obj
);
8498 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
8500 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8501 int rowOrCol
, int x
, int y
,
8502 bool control
, bool shift
, bool alt
, bool meta
)
8503 : wxNotifyEvent( type
, id
)
8505 m_rowOrCol
= rowOrCol
;
8508 m_control
= control
;
8513 SetEventObject(obj
);
8517 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
8519 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8520 const wxGridCellCoords
& topLeft
,
8521 const wxGridCellCoords
& bottomRight
,
8522 bool sel
, bool control
,
8523 bool shift
, bool alt
, bool meta
)
8524 : wxNotifyEvent( type
, id
)
8526 m_topLeft
= topLeft
;
8527 m_bottomRight
= bottomRight
;
8529 m_control
= control
;
8534 SetEventObject(obj
);
8538 #endif // ifndef wxUSE_NEW_GRID