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 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
106 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
107 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
108 const wxPoint
&pos
, const wxSize
&size
);
113 void OnPaint( wxPaintEvent
& event
);
114 void OnMouseEvent( wxMouseEvent
& event
);
115 void OnKeyDown( wxKeyEvent
& event
);
116 void OnKeyUp( wxKeyEvent
& );
118 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
119 DECLARE_EVENT_TABLE()
123 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
126 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
127 wxGridColLabelWindow( 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(wxGridColLabelWindow
)
139 DECLARE_EVENT_TABLE()
143 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
146 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
147 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
148 const wxPoint
&pos
, const wxSize
&size
);
153 void OnMouseEvent( wxMouseEvent
& event
);
154 void OnKeyDown( wxKeyEvent
& event
);
155 void OnKeyUp( wxKeyEvent
& );
156 void OnPaint( wxPaintEvent
& event
);
158 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
159 DECLARE_EVENT_TABLE()
162 class WXDLLEXPORT wxGridWindow
: public wxPanel
167 m_owner
= (wxGrid
*)NULL
;
168 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
169 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
172 wxGridWindow( wxGrid
*parent
,
173 wxGridRowLabelWindow
*rowLblWin
,
174 wxGridColLabelWindow
*colLblWin
,
175 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
178 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
182 wxGridRowLabelWindow
*m_rowLabelWin
;
183 wxGridColLabelWindow
*m_colLabelWin
;
185 void OnPaint( wxPaintEvent
&event
);
186 void OnMouseEvent( wxMouseEvent
& event
);
187 void OnKeyDown( wxKeyEvent
& );
188 void OnKeyUp( wxKeyEvent
& );
189 void OnEraseBackground( wxEraseEvent
& );
192 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
193 DECLARE_EVENT_TABLE()
198 class wxGridCellEditorEvtHandler
: public wxEvtHandler
201 wxGridCellEditorEvtHandler()
202 : m_grid(0), m_editor(0)
204 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
205 : m_grid(grid
), m_editor(editor
)
208 void OnKeyDown(wxKeyEvent
& event
);
209 void OnChar(wxKeyEvent
& event
);
213 wxGridCellEditor
* m_editor
;
214 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
215 DECLARE_EVENT_TABLE()
219 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
220 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
221 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
222 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
227 // ----------------------------------------------------------------------------
228 // the internal data representation used by wxGridCellAttrProvider
229 // ----------------------------------------------------------------------------
231 // this class stores attributes set for cells
232 class WXDLLEXPORT wxGridCellAttrData
235 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
236 wxGridCellAttr
*GetAttr(int row
, int col
) const;
237 void UpdateAttrRows( size_t pos
, int numRows
);
238 void UpdateAttrCols( size_t pos
, int numCols
);
241 // searches for the attr for given cell, returns wxNOT_FOUND if not found
242 int FindIndex(int row
, int col
) const;
244 wxGridCellWithAttrArray m_attrs
;
247 // this class stores attributes set for rows or columns
248 class WXDLLEXPORT wxGridRowOrColAttrData
251 // empty ctor to suppress warnings
252 wxGridRowOrColAttrData() { }
253 ~wxGridRowOrColAttrData();
255 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
256 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
257 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
260 wxArrayInt m_rowsOrCols
;
261 wxArrayAttrs m_attrs
;
264 // NB: this is just a wrapper around 3 objects: one which stores cell
265 // attributes, and 2 others for row/col ones
266 class WXDLLEXPORT wxGridCellAttrProviderData
269 wxGridCellAttrData m_cellAttrs
;
270 wxGridRowOrColAttrData m_rowAttrs
,
275 // ----------------------------------------------------------------------------
276 // data structures used for the data type registry
277 // ----------------------------------------------------------------------------
279 struct wxGridDataTypeInfo
281 wxGridDataTypeInfo(const wxString
& typeName
,
282 wxGridCellRenderer
* renderer
,
283 wxGridCellEditor
* editor
)
284 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
287 ~wxGridDataTypeInfo()
289 wxSafeDecRef(m_renderer
);
290 wxSafeDecRef(m_editor
);
294 wxGridCellRenderer
* m_renderer
;
295 wxGridCellEditor
* m_editor
;
299 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
302 class WXDLLEXPORT wxGridTypeRegistry
305 wxGridTypeRegistry() {}
306 ~wxGridTypeRegistry();
308 void RegisterDataType(const wxString
& typeName
,
309 wxGridCellRenderer
* renderer
,
310 wxGridCellEditor
* editor
);
312 // find one of already registered data types
313 int FindRegisteredDataType(const wxString
& typeName
);
315 // try to FindRegisteredDataType(), if this fails and typeName is one of
316 // standard typenames, register it and return its index
317 int FindDataType(const wxString
& typeName
);
319 // try to FindDataType(), if it fails see if it is not one of already
320 // registered data types with some params in which case clone the
321 // registered data type and set params for it
322 int FindOrCloneDataType(const wxString
& typeName
);
324 wxGridCellRenderer
* GetRenderer(int index
);
325 wxGridCellEditor
* GetEditor(int index
);
328 wxGridDataTypeInfoArray m_typeinfo
;
331 // ----------------------------------------------------------------------------
332 // conditional compilation
333 // ----------------------------------------------------------------------------
335 #ifndef WXGRID_DRAW_LINES
336 #define WXGRID_DRAW_LINES 1
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
343 //#define DEBUG_ATTR_CACHE
344 #ifdef DEBUG_ATTR_CACHE
345 static size_t gs_nAttrCacheHits
= 0;
346 static size_t gs_nAttrCacheMisses
= 0;
347 #endif // DEBUG_ATTR_CACHE
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
353 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
354 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
357 // TODO: fixed so far - make configurable later (and also different for x/y)
358 static const size_t GRID_SCROLL_LINE
= 10;
360 // the size of hash tables used a bit everywhere (the max number of elements
361 // in these hash tables is the number of rows/columns)
362 static const int GRID_HASH_SIZE
= 100;
364 // ============================================================================
366 // ============================================================================
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
372 wxGridCellEditor::wxGridCellEditor()
378 wxGridCellEditor::~wxGridCellEditor()
383 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
384 wxWindowID
WXUNUSED(id
),
385 wxEvtHandler
* evtHandler
)
388 m_control
->PushEventHandler(evtHandler
);
391 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
392 wxGridCellAttr
*attr
)
394 // erase the background because we might not fill the cell
395 wxClientDC
dc(m_control
->GetParent());
396 dc
.SetPen(*wxTRANSPARENT_PEN
);
397 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
398 dc
.DrawRectangle(rectCell
);
400 // redraw the control we just painted over
401 m_control
->Refresh();
404 void wxGridCellEditor::Destroy()
408 m_control
->PopEventHandler(TRUE
/* delete it*/);
410 m_control
->Destroy();
415 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
417 wxASSERT_MSG(m_control
,
418 wxT("The wxGridCellEditor must be Created first!"));
419 m_control
->Show(show
);
423 // set the colours/fonts if we have any
426 m_colFgOld
= m_control
->GetForegroundColour();
427 m_control
->SetForegroundColour(attr
->GetTextColour());
429 m_colBgOld
= m_control
->GetBackgroundColour();
430 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
432 m_fontOld
= m_control
->GetFont();
433 m_control
->SetFont(attr
->GetFont());
435 // can't do anything more in the base class version, the other
436 // attributes may only be used by the derived classes
441 // restore the standard colours fonts
442 if ( m_colFgOld
.Ok() )
444 m_control
->SetForegroundColour(m_colFgOld
);
445 m_colFgOld
= wxNullColour
;
448 if ( m_colBgOld
.Ok() )
450 m_control
->SetBackgroundColour(m_colBgOld
);
451 m_colBgOld
= wxNullColour
;
454 if ( m_fontOld
.Ok() )
456 m_control
->SetFont(m_fontOld
);
457 m_fontOld
= wxNullFont
;
462 void wxGridCellEditor::SetSize(const wxRect
& rect
)
464 wxASSERT_MSG(m_control
,
465 wxT("The wxGridCellEditor must be Created first!"));
466 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
469 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
474 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
476 // accept the simple key presses, not anything with Ctrl/Alt/Meta
477 return !(event
.ControlDown() || event
.AltDown());
480 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
485 void wxGridCellEditor::StartingClick()
489 // ----------------------------------------------------------------------------
490 // wxGridCellTextEditor
491 // ----------------------------------------------------------------------------
493 wxGridCellTextEditor::wxGridCellTextEditor()
498 void wxGridCellTextEditor::Create(wxWindow
* parent
,
500 wxEvtHandler
* evtHandler
)
502 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
503 wxDefaultPosition
, wxDefaultSize
504 #if defined(__WXMSW__)
505 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
506 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
510 // TODO: use m_maxChars
512 wxGridCellEditor::Create(parent
, id
, evtHandler
);
515 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
516 wxGridCellAttr
* WXUNUSED(attr
))
518 // as we fill the entire client area, don't do anything here to minimize
522 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
524 wxRect
rect(rectOrig
);
526 // Make the edit control large enough to allow for internal
529 // TODO: remove this if the text ctrl sizing is improved esp. for
532 #if defined(__WXGTK__)
541 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
543 // MB: treat MSW separately here otherwise the caret doesn't show
544 // when the editor is in the first row.
545 #if defined(__WXMSW__)
548 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
551 #if defined(__WXMOTIF__)
555 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
556 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
557 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
558 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
561 wxGridCellEditor::SetSize(rect
);
564 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
566 wxASSERT_MSG(m_control
,
567 wxT("The wxGridCellEditor must be Created first!"));
569 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
571 DoBeginEdit(m_startValue
);
574 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
576 Text()->SetValue(startValue
);
577 Text()->SetInsertionPointEnd();
581 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
584 wxASSERT_MSG(m_control
,
585 wxT("The wxGridCellEditor must be Created first!"));
587 bool changed
= FALSE
;
588 wxString value
= Text()->GetValue();
589 if (value
!= m_startValue
)
593 grid
->GetTable()->SetValue(row
, col
, value
);
595 m_startValue
= wxEmptyString
;
596 Text()->SetValue(m_startValue
);
602 void wxGridCellTextEditor::Reset()
604 wxASSERT_MSG(m_control
,
605 wxT("The wxGridCellEditor must be Created first!"));
607 DoReset(m_startValue
);
610 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
612 Text()->SetValue(startValue
);
613 Text()->SetInsertionPointEnd();
616 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
618 if ( wxGridCellEditor::IsAcceptedKey(event
) )
620 int keycode
= event
.GetKeyCode();
634 case WXK_NUMPAD_MULTIPLY
:
638 case WXK_NUMPAD_SUBTRACT
:
640 case WXK_NUMPAD_DECIMAL
:
642 case WXK_NUMPAD_DIVIDE
:
646 // accept 8 bit chars too if isprint() agrees
647 if ( (keycode
< 255) && (isprint(keycode
)) )
655 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
657 // we don't check for !HasModifiers() because IsAcceptedKey() did it
659 // insert the key in the control
661 int keycode
= event
.GetKeyCode();
674 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
678 case WXK_NUMPAD_MULTIPLY
:
688 case WXK_NUMPAD_SUBTRACT
:
693 case WXK_NUMPAD_DECIMAL
:
698 case WXK_NUMPAD_DIVIDE
:
703 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
705 // FIXME this is not going to work for non letters...
706 if ( !event
.ShiftDown() )
708 keycode
= tolower(keycode
);
711 ch
= (wxChar
)keycode
;
721 Text()->AppendText(ch
);
729 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
730 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
732 #if defined(__WXMOTIF__) || defined(__WXGTK__)
733 // wxMotif needs a little extra help...
734 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
735 wxString
s( Text()->GetValue() );
736 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
738 Text()->SetInsertionPoint( pos
);
740 // the other ports can handle a Return key press
746 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
756 if ( !params
.ToLong(&tmp
) )
758 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
762 m_maxChars
= (size_t)tmp
;
767 // ----------------------------------------------------------------------------
768 // wxGridCellNumberEditor
769 // ----------------------------------------------------------------------------
771 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
777 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
779 wxEvtHandler
* evtHandler
)
783 // create a spin ctrl
784 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
785 wxDefaultPosition
, wxDefaultSize
,
789 wxGridCellEditor::Create(parent
, id
, evtHandler
);
793 // just a text control
794 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
797 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
798 #endif // wxUSE_VALIDATORS
802 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
804 // first get the value
805 wxGridTableBase
*table
= grid
->GetTable();
806 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
808 m_valueOld
= table
->GetValueAsLong(row
, col
);
812 wxString sValue
= table
->GetValue(row
, col
);
813 if (! sValue
.ToLong(&m_valueOld
))
815 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
822 Spin()->SetValue((int)m_valueOld
);
827 DoBeginEdit(GetString());
831 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
839 value
= Spin()->GetValue();
840 changed
= value
!= m_valueOld
;
844 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
849 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
850 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
852 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%ld"), value
));
858 void wxGridCellNumberEditor::Reset()
862 Spin()->SetValue((int)m_valueOld
);
866 DoReset(GetString());
870 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
872 if ( wxGridCellEditor::IsAcceptedKey(event
) )
874 int keycode
= event
.GetKeyCode();
890 case WXK_NUMPAD_SUBTRACT
:
896 if ( (keycode
< 128) && isdigit(keycode
) )
904 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
908 int keycode
= (int) event
.KeyCode();
909 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
911 wxGridCellTextEditor::StartingKey(event
);
921 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
932 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
936 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
940 // skip the error message below
945 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
949 // ----------------------------------------------------------------------------
950 // wxGridCellFloatEditor
951 // ----------------------------------------------------------------------------
953 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
956 m_precision
= precision
;
959 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
961 wxEvtHandler
* evtHandler
)
963 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
966 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
967 #endif // wxUSE_VALIDATORS
970 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
972 // first get the value
973 wxGridTableBase
*table
= grid
->GetTable();
974 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
976 m_valueOld
= table
->GetValueAsDouble(row
, col
);
980 wxString sValue
= table
->GetValue(row
, col
);
981 if (! sValue
.ToDouble(&m_valueOld
))
983 wxFAIL_MSG( _T("this cell doesn't have float value") );
988 DoBeginEdit(GetString());
991 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
995 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
997 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
998 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1000 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%f"), value
));
1010 void wxGridCellFloatEditor::Reset()
1012 DoReset(GetString());
1015 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1017 int keycode
= (int)event
.KeyCode();
1018 if ( isdigit(keycode
) ||
1019 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1021 wxGridCellTextEditor::StartingKey(event
);
1023 // skip Skip() below
1030 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1041 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1045 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1047 m_precision
= (int)tmp
;
1049 // skip the error message below
1054 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1058 wxString
wxGridCellFloatEditor::GetString() const
1061 if ( m_width
== -1 )
1063 // default width/precision
1066 else if ( m_precision
== -1 )
1068 // default precision
1069 fmt
.Printf(_T("%%%d.g"), m_width
);
1073 fmt
.Printf(_T("%%%d.%dg"), m_width
, m_precision
);
1076 return wxString::Format(fmt
, m_valueOld
);
1079 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1081 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1083 int keycode
= event
.GetKeyCode();
1097 case WXK_NUMPAD_ADD
:
1099 case WXK_NUMPAD_SUBTRACT
:
1101 case WXK_NUMPAD_DECIMAL
:
1105 // additionally accept 'e' as in '1e+6'
1106 if ( (keycode
< 128) &&
1107 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1115 // ----------------------------------------------------------------------------
1116 // wxGridCellBoolEditor
1117 // ----------------------------------------------------------------------------
1119 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1121 wxEvtHandler
* evtHandler
)
1123 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1124 wxDefaultPosition
, wxDefaultSize
,
1127 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1130 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1132 bool resize
= FALSE
;
1133 wxSize size
= m_control
->GetSize();
1134 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1136 // check if the checkbox is not too big/small for this cell
1137 wxSize sizeBest
= m_control
->GetBestSize();
1138 if ( !(size
== sizeBest
) )
1140 // reset to default size if it had been made smaller
1146 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1148 // leave 1 pixel margin
1149 size
.x
= size
.y
= minSize
- 2;
1156 m_control
->SetSize(size
);
1159 // position it in the centre of the rectangle (TODO: support alignment?)
1161 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1162 // the checkbox without label still has some space to the right in wxGTK,
1163 // so shift it to the right
1165 #elif defined(__WXMSW__)
1166 // here too, but in other way
1171 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1174 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1176 m_control
->Show(show
);
1180 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1181 CBox()->SetBackgroundColour(colBg
);
1185 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1187 wxASSERT_MSG(m_control
,
1188 wxT("The wxGridCellEditor must be Created first!"));
1190 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1191 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1194 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1195 m_startValue
= !( !cellval
|| (cellval
== "0") );
1197 CBox()->SetValue(m_startValue
);
1201 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1204 wxASSERT_MSG(m_control
,
1205 wxT("The wxGridCellEditor must be Created first!"));
1207 bool changed
= FALSE
;
1208 bool value
= CBox()->GetValue();
1209 if ( value
!= m_startValue
)
1214 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1215 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1217 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1223 void wxGridCellBoolEditor::Reset()
1225 wxASSERT_MSG(m_control
,
1226 wxT("The wxGridCellEditor must be Created first!"));
1228 CBox()->SetValue(m_startValue
);
1231 void wxGridCellBoolEditor::StartingClick()
1233 CBox()->SetValue(!CBox()->GetValue());
1236 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1238 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1240 int keycode
= event
.GetKeyCode();
1244 case WXK_NUMPAD_MULTIPLY
:
1246 case WXK_NUMPAD_ADD
:
1248 case WXK_NUMPAD_SUBTRACT
:
1259 // ----------------------------------------------------------------------------
1260 // wxGridCellChoiceEditor
1261 // ----------------------------------------------------------------------------
1263 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1264 const wxString choices
[],
1266 : m_allowOthers(allowOthers
)
1270 m_choices
.Alloc(count
);
1271 for ( size_t n
= 0; n
< count
; n
++ )
1273 m_choices
.Add(choices
[n
]);
1278 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1280 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1281 editor
->m_allowOthers
= m_allowOthers
;
1282 editor
->m_choices
= m_choices
;
1287 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1289 wxEvtHandler
* evtHandler
)
1291 size_t count
= m_choices
.GetCount();
1292 wxString
*choices
= new wxString
[count
];
1293 for ( size_t n
= 0; n
< count
; n
++ )
1295 choices
[n
] = m_choices
[n
];
1298 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1299 wxDefaultPosition
, wxDefaultSize
,
1301 m_allowOthers
? 0 : wxCB_READONLY
);
1305 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1308 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1309 wxGridCellAttr
* attr
)
1311 // as we fill the entire client area, don't do anything here to minimize
1314 // TODO: It doesn't actually fill the client area since the height of a
1315 // combo always defaults to the standard... Until someone has time to
1316 // figure out the right rectangle to paint, just do it the normal way...
1317 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1320 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1322 wxASSERT_MSG(m_control
,
1323 wxT("The wxGridCellEditor must be Created first!"));
1325 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1327 Combo()->SetValue(m_startValue
);
1328 size_t count
= m_choices
.GetCount();
1329 for (size_t i
=0; i
<count
; i
++)
1331 if (m_startValue
== m_choices
[i
])
1333 Combo()->SetSelection(i
);
1337 Combo()->SetInsertionPointEnd();
1338 Combo()->SetFocus();
1341 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1344 wxString value
= Combo()->GetValue();
1345 bool changed
= value
!= m_startValue
;
1348 grid
->GetTable()->SetValue(row
, col
, value
);
1350 m_startValue
= wxEmptyString
;
1351 Combo()->SetValue(m_startValue
);
1356 void wxGridCellChoiceEditor::Reset()
1358 Combo()->SetValue(m_startValue
);
1359 Combo()->SetInsertionPointEnd();
1362 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1372 wxStringTokenizer
tk(params
, _T(','));
1373 while ( tk
.HasMoreTokens() )
1375 m_choices
.Add(tk
.GetNextToken());
1379 // ----------------------------------------------------------------------------
1380 // wxGridCellEditorEvtHandler
1381 // ----------------------------------------------------------------------------
1383 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1385 switch ( event
.KeyCode() )
1389 m_grid
->DisableCellEditControl();
1393 event
.Skip( m_grid
->ProcessEvent( event
) );
1396 case WXK_NUMPAD_ENTER
:
1398 if (!m_grid
->ProcessEvent(event
))
1399 m_editor
->HandleReturn(event
);
1408 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1410 switch ( event
.KeyCode() )
1415 case WXK_NUMPAD_ENTER
:
1423 // ----------------------------------------------------------------------------
1424 // wxGridCellWorker is an (almost) empty common base class for
1425 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1426 // ----------------------------------------------------------------------------
1428 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1433 wxGridCellWorker::~wxGridCellWorker()
1437 // ============================================================================
1439 // ============================================================================
1441 // ----------------------------------------------------------------------------
1442 // wxGridCellRenderer
1443 // ----------------------------------------------------------------------------
1445 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1446 wxGridCellAttr
& attr
,
1449 int WXUNUSED(row
), int WXUNUSED(col
),
1452 dc
.SetBackgroundMode( wxSOLID
);
1456 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1460 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1463 dc
.SetPen( *wxTRANSPARENT_PEN
);
1464 dc
.DrawRectangle(rect
);
1467 // ----------------------------------------------------------------------------
1468 // wxGridCellStringRenderer
1469 // ----------------------------------------------------------------------------
1471 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1472 wxGridCellAttr
& attr
,
1476 dc
.SetBackgroundMode( wxTRANSPARENT
);
1478 // TODO some special colours for attr.IsReadOnly() case?
1482 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1483 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1487 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1488 dc
.SetTextForeground( attr
.GetTextColour() );
1491 dc
.SetFont( attr
.GetFont() );
1494 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1496 const wxString
& text
)
1498 wxCoord x
= 0, y
= 0, max_x
= 0;
1499 dc
.SetFont(attr
.GetFont());
1500 wxStringTokenizer
tk(text
, _T('\n'));
1501 while ( tk
.HasMoreTokens() )
1503 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1504 max_x
= wxMax(max_x
, x
);
1507 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1509 return wxSize(max_x
, y
);
1512 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1513 wxGridCellAttr
& attr
,
1517 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1520 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1521 wxGridCellAttr
& attr
,
1523 const wxRect
& rectCell
,
1527 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1529 // now we only have to draw the text
1530 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1533 attr
.GetAlignment(&hAlign
, &vAlign
);
1535 wxRect rect
= rectCell
;
1538 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1539 rect
, hAlign
, vAlign
);
1542 // ----------------------------------------------------------------------------
1543 // wxGridCellNumberRenderer
1544 // ----------------------------------------------------------------------------
1546 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1548 wxGridTableBase
*table
= grid
.GetTable();
1550 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1552 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1556 text
= table
->GetValue(row
, col
);
1562 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1563 wxGridCellAttr
& attr
,
1565 const wxRect
& rectCell
,
1569 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1571 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1573 // draw the text right aligned by default
1575 attr
.GetAlignment(&hAlign
, &vAlign
);
1576 hAlign
= wxALIGN_RIGHT
;
1578 wxRect rect
= rectCell
;
1581 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1584 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1585 wxGridCellAttr
& attr
,
1589 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1592 // ----------------------------------------------------------------------------
1593 // wxGridCellFloatRenderer
1594 // ----------------------------------------------------------------------------
1596 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1599 SetPrecision(precision
);
1602 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1604 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1605 renderer
->m_width
= m_width
;
1606 renderer
->m_precision
= m_precision
;
1607 renderer
->m_format
= m_format
;
1612 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1614 wxGridTableBase
*table
= grid
.GetTable();
1619 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1621 val
= table
->GetValueAsDouble(row
, col
);
1626 text
= table
->GetValue(row
, col
);
1627 hasDouble
= text
.ToDouble(&val
);
1634 if ( m_width
== -1 )
1636 // default width/precision
1637 m_format
= _T("%f");
1639 else if ( m_precision
== -1 )
1641 // default precision
1642 m_format
.Printf(_T("%%%d.f"), m_width
);
1646 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1650 text
.Printf(m_format
, val
);
1652 //else: text already contains the string
1657 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1658 wxGridCellAttr
& attr
,
1660 const wxRect
& rectCell
,
1664 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1666 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1668 // draw the text right aligned by default
1670 attr
.GetAlignment(&hAlign
, &vAlign
);
1671 hAlign
= wxALIGN_RIGHT
;
1673 wxRect rect
= rectCell
;
1676 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1679 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1680 wxGridCellAttr
& attr
,
1684 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1687 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1693 // reset to defaults
1699 wxString tmp
= params
.BeforeFirst(_T(','));
1703 if ( !tmp
.ToLong(&width
) )
1709 SetWidth((int)width
);
1711 tmp
= params
.AfterFirst(_T(','));
1715 if ( !tmp
.ToLong(&precision
) )
1721 SetPrecision((int)precision
);
1729 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string '%s ignored"), params
.c_str());
1734 // ----------------------------------------------------------------------------
1735 // wxGridCellBoolRenderer
1736 // ----------------------------------------------------------------------------
1738 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1740 // FIXME these checkbox size calculations are really ugly...
1742 // between checkmark and box
1743 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1745 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1746 wxGridCellAttr
& WXUNUSED(attr
),
1751 // compute it only once (no locks for MT safeness in GUI thread...)
1752 if ( !ms_sizeCheckMark
.x
)
1754 // get checkbox size
1755 wxCoord checkSize
= 0;
1756 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1757 wxSize size
= checkbox
->GetBestSize();
1758 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1760 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1761 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1762 checkSize
-= size
.y
/ 2;
1767 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1770 return ms_sizeCheckMark
;
1773 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1774 wxGridCellAttr
& attr
,
1780 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1782 // draw a check mark in the centre (ignoring alignment - TODO)
1783 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1785 // don't draw outside the cell
1786 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1787 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1789 // and even leave (at least) 1 pixel margin
1790 size
.x
= size
.y
= minSize
- 2;
1793 // draw a border around checkmark
1795 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1796 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1797 rectBorder
.width
= size
.x
;
1798 rectBorder
.height
= size
.y
;
1801 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1802 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1805 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1806 value
= !( !cellval
|| (cellval
== "0") );
1811 wxRect rectMark
= rectBorder
;
1813 // MSW DrawCheckMark() is weird (and should probably be changed...)
1814 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1818 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1821 dc
.SetTextForeground(attr
.GetTextColour());
1822 dc
.DrawCheckMark(rectMark
);
1825 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1826 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1827 dc
.DrawRectangle(rectBorder
);
1830 // ----------------------------------------------------------------------------
1832 // ----------------------------------------------------------------------------
1834 wxGridCellAttr
*wxGridCellAttr::Clone() const
1836 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1837 if ( HasTextColour() )
1838 attr
->SetTextColour(GetTextColour());
1839 if ( HasBackgroundColour() )
1840 attr
->SetBackgroundColour(GetBackgroundColour());
1842 attr
->SetFont(GetFont());
1843 if ( HasAlignment() )
1844 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1848 attr
->SetRenderer(m_renderer
);
1849 m_renderer
->IncRef();
1853 attr
->SetEditor(m_editor
);
1858 attr
->SetReadOnly();
1860 attr
->SetDefAttr(m_defGridAttr
);
1865 const wxColour
& wxGridCellAttr::GetTextColour() const
1867 if (HasTextColour())
1871 else if (m_defGridAttr
!= this)
1873 return m_defGridAttr
->GetTextColour();
1877 wxFAIL_MSG(wxT("Missing default cell attribute"));
1878 return wxNullColour
;
1883 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1885 if (HasBackgroundColour())
1887 else if (m_defGridAttr
!= this)
1888 return m_defGridAttr
->GetBackgroundColour();
1891 wxFAIL_MSG(wxT("Missing default cell attribute"));
1892 return wxNullColour
;
1897 const wxFont
& wxGridCellAttr::GetFont() const
1901 else if (m_defGridAttr
!= this)
1902 return m_defGridAttr
->GetFont();
1905 wxFAIL_MSG(wxT("Missing default cell attribute"));
1911 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1915 if ( hAlign
) *hAlign
= m_hAlign
;
1916 if ( vAlign
) *vAlign
= m_vAlign
;
1918 else if (m_defGridAttr
!= this)
1919 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1922 wxFAIL_MSG(wxT("Missing default cell attribute"));
1927 // GetRenderer and GetEditor use a slightly different decision path about
1928 // which attribute to use. If a non-default attr object has one then it is
1929 // used, otherwise the default editor or renderer is fetched from the grid and
1930 // used. It should be the default for the data type of the cell. If it is
1931 // NULL (because the table has a type that the grid does not have in its
1932 // registry,) then the grid's default editor or renderer is used.
1934 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1936 wxGridCellRenderer
* renderer
= NULL
;
1938 if ( m_defGridAttr
!= this || grid
== NULL
)
1940 renderer
= m_renderer
; // use local attribute
1945 if ( !renderer
&& grid
) // get renderer for the data type
1947 // GetDefaultRendererForCell() will do IncRef() for us
1948 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1953 // if we still don't have one then use the grid default
1954 // (no need for IncRef() here neither)
1955 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1960 wxFAIL_MSG(wxT("Missing default cell attribute"));
1966 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1968 wxGridCellEditor
* editor
= NULL
;
1970 if ( m_defGridAttr
!= this || grid
== NULL
)
1972 editor
= m_editor
; // use local attribute
1977 if ( !editor
&& grid
) // get renderer for the data type
1978 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1981 // if we still don't have one then use the grid default
1982 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1986 wxFAIL_MSG(wxT("Missing default cell attribute"));
1992 // ----------------------------------------------------------------------------
1993 // wxGridCellAttrData
1994 // ----------------------------------------------------------------------------
1996 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1998 int n
= FindIndex(row
, col
);
1999 if ( n
== wxNOT_FOUND
)
2001 // add the attribute
2002 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2008 // change the attribute
2009 m_attrs
[(size_t)n
].attr
= attr
;
2013 // remove this attribute
2014 m_attrs
.RemoveAt((size_t)n
);
2019 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2021 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2023 int n
= FindIndex(row
, col
);
2024 if ( n
!= wxNOT_FOUND
)
2026 attr
= m_attrs
[(size_t)n
].attr
;
2033 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2035 size_t count
= m_attrs
.GetCount();
2036 for ( size_t n
= 0; n
< count
; n
++ )
2038 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2039 wxCoord row
= coords
.GetRow();
2040 if ((size_t)row
>= pos
)
2044 // If rows inserted, include row counter where necessary
2045 coords
.SetRow(row
+ numRows
);
2047 else if (numRows
< 0)
2049 // If rows deleted ...
2050 if ((size_t)row
>= pos
- numRows
)
2052 // ...either decrement row counter (if row still exists)...
2053 coords
.SetRow(row
+ numRows
);
2057 // ...or remove the attribute
2058 m_attrs
.RemoveAt((size_t)n
);
2066 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2068 size_t count
= m_attrs
.GetCount();
2069 for ( size_t n
= 0; n
< count
; n
++ )
2071 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2072 wxCoord col
= coords
.GetCol();
2073 if ( (size_t)col
>= pos
)
2077 // If rows inserted, include row counter where necessary
2078 coords
.SetCol(col
+ numCols
);
2080 else if (numCols
< 0)
2082 // If rows deleted ...
2083 if ((size_t)col
>= pos
- numCols
)
2085 // ...either decrement row counter (if row still exists)...
2086 coords
.SetCol(col
+ numCols
);
2090 // ...or remove the attribute
2091 m_attrs
.RemoveAt((size_t)n
);
2099 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2101 size_t count
= m_attrs
.GetCount();
2102 for ( size_t n
= 0; n
< count
; n
++ )
2104 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2105 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2114 // ----------------------------------------------------------------------------
2115 // wxGridRowOrColAttrData
2116 // ----------------------------------------------------------------------------
2118 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2120 size_t count
= m_attrs
.Count();
2121 for ( size_t n
= 0; n
< count
; n
++ )
2123 m_attrs
[n
]->DecRef();
2127 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2129 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2131 int n
= m_rowsOrCols
.Index(rowOrCol
);
2132 if ( n
!= wxNOT_FOUND
)
2134 attr
= m_attrs
[(size_t)n
];
2141 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2143 int i
= m_rowsOrCols
.Index(rowOrCol
);
2144 if ( i
== wxNOT_FOUND
)
2146 // add the attribute
2147 m_rowsOrCols
.Add(rowOrCol
);
2152 size_t n
= (size_t)i
;
2155 // change the attribute
2156 m_attrs
[n
]->DecRef();
2161 // remove this attribute
2162 m_attrs
[n
]->DecRef();
2163 m_rowsOrCols
.RemoveAt(n
);
2164 m_attrs
.RemoveAt(n
);
2169 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2171 size_t count
= m_attrs
.GetCount();
2172 for ( size_t n
= 0; n
< count
; n
++ )
2174 int & rowOrCol
= m_rowsOrCols
[n
];
2175 if ( (size_t)rowOrCol
>= pos
)
2177 if ( numRowsOrCols
> 0 )
2179 // If rows inserted, include row counter where necessary
2180 rowOrCol
+= numRowsOrCols
;
2182 else if ( numRowsOrCols
< 0)
2184 // If rows deleted, either decrement row counter (if row still exists)
2185 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2186 rowOrCol
+= numRowsOrCols
;
2189 m_rowsOrCols
.RemoveAt((size_t)n
);
2190 m_attrs
.RemoveAt((size_t)n
);
2198 // ----------------------------------------------------------------------------
2199 // wxGridCellAttrProvider
2200 // ----------------------------------------------------------------------------
2202 wxGridCellAttrProvider::wxGridCellAttrProvider()
2204 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2207 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2212 void wxGridCellAttrProvider::InitData()
2214 m_data
= new wxGridCellAttrProviderData
;
2217 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
2219 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2222 // first look for the attribute of this specific cell
2223 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2227 // then look for the col attr (col attributes are more common than
2228 // the row ones, hence they have priority)
2229 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2234 // finally try the row attributes
2235 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2242 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2248 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2251 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2256 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2259 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2264 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2267 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2271 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2273 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2277 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2281 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2283 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2287 // ----------------------------------------------------------------------------
2288 // wxGridTypeRegistry
2289 // ----------------------------------------------------------------------------
2291 wxGridTypeRegistry::~wxGridTypeRegistry()
2293 size_t count
= m_typeinfo
.Count();
2294 for ( size_t i
= 0; i
< count
; i
++ )
2295 delete m_typeinfo
[i
];
2299 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2300 wxGridCellRenderer
* renderer
,
2301 wxGridCellEditor
* editor
)
2303 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2305 // is it already registered?
2306 int loc
= FindRegisteredDataType(typeName
);
2307 if ( loc
!= wxNOT_FOUND
)
2309 delete m_typeinfo
[loc
];
2310 m_typeinfo
[loc
] = info
;
2314 m_typeinfo
.Add(info
);
2318 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2320 size_t count
= m_typeinfo
.GetCount();
2321 for ( size_t i
= 0; i
< count
; i
++ )
2323 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2332 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2334 int index
= FindRegisteredDataType(typeName
);
2335 if ( index
== wxNOT_FOUND
)
2337 // check whether this is one of the standard ones, in which case
2338 // register it "on the fly"
2339 if ( typeName
== wxGRID_VALUE_STRING
)
2341 RegisterDataType(wxGRID_VALUE_STRING
,
2342 new wxGridCellStringRenderer
,
2343 new wxGridCellTextEditor
);
2345 else if ( typeName
== wxGRID_VALUE_BOOL
)
2347 RegisterDataType(wxGRID_VALUE_BOOL
,
2348 new wxGridCellBoolRenderer
,
2349 new wxGridCellBoolEditor
);
2351 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2353 RegisterDataType(wxGRID_VALUE_NUMBER
,
2354 new wxGridCellNumberRenderer
,
2355 new wxGridCellNumberEditor
);
2357 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2359 RegisterDataType(wxGRID_VALUE_FLOAT
,
2360 new wxGridCellFloatRenderer
,
2361 new wxGridCellFloatEditor
);
2363 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2365 RegisterDataType(wxGRID_VALUE_CHOICE
,
2366 new wxGridCellStringRenderer
,
2367 new wxGridCellChoiceEditor
);
2374 // we get here only if just added the entry for this type, so return
2376 index
= m_typeinfo
.GetCount() - 1;
2382 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2384 int index
= FindDataType(typeName
);
2385 if ( index
== wxNOT_FOUND
)
2387 // the first part of the typename is the "real" type, anything after ':'
2388 // are the parameters for the renderer
2389 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2390 if ( index
== wxNOT_FOUND
)
2395 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2396 wxGridCellRenderer
*rendererOld
= renderer
;
2397 renderer
= renderer
->Clone();
2398 rendererOld
->DecRef();
2400 wxGridCellEditor
*editor
= GetEditor(index
);
2401 wxGridCellEditor
*editorOld
= editor
;
2402 editor
= editor
->Clone();
2403 editorOld
->DecRef();
2405 // do it even if there are no parameters to reset them to defaults
2406 wxString params
= typeName
.AfterFirst(_T(':'));
2407 renderer
->SetParameters(params
);
2408 editor
->SetParameters(params
);
2410 // register the new typename
2411 RegisterDataType(typeName
, renderer
, editor
);
2413 // we just registered it, it's the last one
2414 index
= m_typeinfo
.GetCount() - 1;
2420 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2422 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2428 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2430 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2436 // ----------------------------------------------------------------------------
2438 // ----------------------------------------------------------------------------
2440 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2443 wxGridTableBase::wxGridTableBase()
2445 m_view
= (wxGrid
*) NULL
;
2446 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2449 wxGridTableBase::~wxGridTableBase()
2451 delete m_attrProvider
;
2454 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2456 delete m_attrProvider
;
2457 m_attrProvider
= attrProvider
;
2460 bool wxGridTableBase::CanHaveAttributes()
2462 if ( ! GetAttrProvider() )
2464 // use the default attr provider by default
2465 SetAttrProvider(new wxGridCellAttrProvider
);
2470 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
2472 if ( m_attrProvider
)
2473 return m_attrProvider
->GetAttr(row
, col
);
2475 return (wxGridCellAttr
*)NULL
;
2478 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2480 if ( m_attrProvider
)
2482 m_attrProvider
->SetAttr(attr
, row
, col
);
2486 // as we take ownership of the pointer and don't store it, we must
2492 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2494 if ( m_attrProvider
)
2496 m_attrProvider
->SetRowAttr(attr
, row
);
2500 // as we take ownership of the pointer and don't store it, we must
2506 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2508 if ( m_attrProvider
)
2510 m_attrProvider
->SetColAttr(attr
, col
);
2514 // as we take ownership of the pointer and don't store it, we must
2520 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2521 size_t WXUNUSED(numRows
) )
2523 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2528 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2530 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2535 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2536 size_t WXUNUSED(numRows
) )
2538 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2543 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2544 size_t WXUNUSED(numCols
) )
2546 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2551 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2553 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2558 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2559 size_t WXUNUSED(numCols
) )
2561 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2567 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2570 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2571 // how much it makes sense to us geeks.
2575 wxString
wxGridTableBase::GetColLabelValue( int col
)
2577 // default col labels are:
2578 // cols 0 to 25 : A-Z
2579 // cols 26 to 675 : AA-ZZ
2584 for ( n
= 1; ; n
++ )
2586 s
+= (_T('A') + (wxChar
)( col%26
));
2588 if ( col
< 0 ) break;
2591 // reverse the string...
2593 for ( i
= 0; i
< n
; i
++ )
2602 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2604 return wxGRID_VALUE_STRING
;
2607 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2608 const wxString
& typeName
)
2610 return typeName
== wxGRID_VALUE_STRING
;
2613 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2615 return CanGetValueAs(row
, col
, typeName
);
2618 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2623 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2628 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2633 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2634 long WXUNUSED(value
) )
2638 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2639 double WXUNUSED(value
) )
2643 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2644 bool WXUNUSED(value
) )
2649 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2650 const wxString
& WXUNUSED(typeName
) )
2655 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2656 const wxString
& WXUNUSED(typeName
),
2657 void* WXUNUSED(value
) )
2661 //////////////////////////////////////////////////////////////////////
2663 // Message class for the grid table to send requests and notifications
2667 wxGridTableMessage::wxGridTableMessage()
2669 m_table
= (wxGridTableBase
*) NULL
;
2675 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2676 int commandInt1
, int commandInt2
)
2680 m_comInt1
= commandInt1
;
2681 m_comInt2
= commandInt2
;
2686 //////////////////////////////////////////////////////////////////////
2688 // A basic grid table for string data. An object of this class will
2689 // created by wxGrid if you don't specify an alternative table class.
2692 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2694 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2696 wxGridStringTable::wxGridStringTable()
2701 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2706 m_data
.Alloc( numRows
);
2709 sa
.Alloc( numCols
);
2710 for ( col
= 0; col
< numCols
; col
++ )
2712 sa
.Add( wxEmptyString
);
2715 for ( row
= 0; row
< numRows
; row
++ )
2721 wxGridStringTable::~wxGridStringTable()
2725 int wxGridStringTable::GetNumberRows()
2727 return m_data
.GetCount();
2730 int wxGridStringTable::GetNumberCols()
2732 if ( m_data
.GetCount() > 0 )
2733 return m_data
[0].GetCount();
2738 wxString
wxGridStringTable::GetValue( int row
, int col
)
2740 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2741 _T("invalid row or column index in wxGridStringTable") );
2743 return m_data
[row
][col
];
2746 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2748 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2749 _T("invalid row or column index in wxGridStringTable") );
2751 m_data
[row
][col
] = value
;
2754 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2756 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2757 _T("invalid row or column index in wxGridStringTable") );
2759 return (m_data
[row
][col
] == wxEmptyString
);
2762 void wxGridStringTable::Clear()
2765 int numRows
, numCols
;
2767 numRows
= m_data
.GetCount();
2770 numCols
= m_data
[0].GetCount();
2772 for ( row
= 0; row
< numRows
; row
++ )
2774 for ( col
= 0; col
< numCols
; col
++ )
2776 m_data
[row
][col
] = wxEmptyString
;
2783 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2787 size_t curNumRows
= m_data
.GetCount();
2788 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2789 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2791 if ( pos
>= curNumRows
)
2793 return AppendRows( numRows
);
2797 sa
.Alloc( curNumCols
);
2798 for ( col
= 0; col
< curNumCols
; col
++ )
2800 sa
.Add( wxEmptyString
);
2803 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2805 m_data
.Insert( sa
, row
);
2809 wxGridTableMessage
msg( this,
2810 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2814 GetView()->ProcessTableMessage( msg
);
2820 bool wxGridStringTable::AppendRows( size_t numRows
)
2824 size_t curNumRows
= m_data
.GetCount();
2825 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2826 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2829 if ( curNumCols
> 0 )
2831 sa
.Alloc( curNumCols
);
2832 for ( col
= 0; col
< curNumCols
; col
++ )
2834 sa
.Add( wxEmptyString
);
2838 for ( row
= 0; row
< numRows
; row
++ )
2845 wxGridTableMessage
msg( this,
2846 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2849 GetView()->ProcessTableMessage( msg
);
2855 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2859 size_t curNumRows
= m_data
.GetCount();
2861 if ( pos
>= curNumRows
)
2864 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
2865 pos
, numRows
, curNumRows
);
2866 wxFAIL_MSG( errmsg
);
2870 if ( numRows
> curNumRows
- pos
)
2872 numRows
= curNumRows
- pos
;
2875 if ( numRows
>= curNumRows
)
2877 m_data
.Empty(); // don't release memory just yet
2881 for ( n
= 0; n
< numRows
; n
++ )
2883 m_data
.Remove( pos
);
2888 wxGridTableMessage
msg( this,
2889 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2893 GetView()->ProcessTableMessage( msg
);
2899 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2903 size_t curNumRows
= m_data
.GetCount();
2904 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2905 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2907 if ( pos
>= curNumCols
)
2909 return AppendCols( numCols
);
2912 for ( row
= 0; row
< curNumRows
; row
++ )
2914 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2916 m_data
[row
].Insert( wxEmptyString
, col
);
2921 wxGridTableMessage
msg( this,
2922 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2926 GetView()->ProcessTableMessage( msg
);
2932 bool wxGridStringTable::AppendCols( size_t numCols
)
2936 size_t curNumRows
= m_data
.GetCount();
2940 // TODO: something better than this ?
2942 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
2947 for ( row
= 0; row
< curNumRows
; row
++ )
2949 for ( n
= 0; n
< numCols
; n
++ )
2951 m_data
[row
].Add( wxEmptyString
);
2957 wxGridTableMessage
msg( this,
2958 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2961 GetView()->ProcessTableMessage( msg
);
2967 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2971 size_t curNumRows
= m_data
.GetCount();
2972 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2973 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2975 if ( pos
>= curNumCols
)
2978 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
2979 pos
, numCols
, curNumCols
);
2980 wxFAIL_MSG( errmsg
);
2984 if ( numCols
> curNumCols
- pos
)
2986 numCols
= curNumCols
- pos
;
2989 for ( row
= 0; row
< curNumRows
; row
++ )
2991 if ( numCols
>= curNumCols
)
2993 m_data
[row
].Clear();
2997 for ( n
= 0; n
< numCols
; n
++ )
2999 m_data
[row
].Remove( pos
);
3005 wxGridTableMessage
msg( this,
3006 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3010 GetView()->ProcessTableMessage( msg
);
3016 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3018 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3020 // using default label
3022 return wxGridTableBase::GetRowLabelValue( row
);
3026 return m_rowLabels
[ row
];
3030 wxString
wxGridStringTable::GetColLabelValue( int col
)
3032 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3034 // using default label
3036 return wxGridTableBase::GetColLabelValue( col
);
3040 return m_colLabels
[ col
];
3044 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3046 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3048 int n
= m_rowLabels
.GetCount();
3050 for ( i
= n
; i
<= row
; i
++ )
3052 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3056 m_rowLabels
[row
] = value
;
3059 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3061 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3063 int n
= m_colLabels
.GetCount();
3065 for ( i
= n
; i
<= col
; i
++ )
3067 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3071 m_colLabels
[col
] = value
;
3076 //////////////////////////////////////////////////////////////////////
3077 //////////////////////////////////////////////////////////////////////
3079 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3081 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3082 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3083 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3084 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3085 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3088 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3090 const wxPoint
&pos
, const wxSize
&size
)
3091 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3096 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3100 // NO - don't do this because it will set both the x and y origin
3101 // coords to match the parent scrolled window and we just want to
3102 // set the y coord - MB
3104 // m_owner->PrepareDC( dc );
3107 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3108 dc
.SetDeviceOrigin( 0, -y
);
3110 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3111 m_owner
->DrawRowLabels( dc
);
3115 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3117 m_owner
->ProcessRowLabelMouseEvent( event
);
3121 // This seems to be required for wxMotif otherwise the mouse
3122 // cursor must be in the cell edit control to get key events
3124 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3126 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3129 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3131 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3136 //////////////////////////////////////////////////////////////////////
3138 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3140 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3141 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3142 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3143 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3144 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3147 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3149 const wxPoint
&pos
, const wxSize
&size
)
3150 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3155 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3159 // NO - don't do this because it will set both the x and y origin
3160 // coords to match the parent scrolled window and we just want to
3161 // set the x coord - MB
3163 // m_owner->PrepareDC( dc );
3166 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3167 dc
.SetDeviceOrigin( -x
, 0 );
3169 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3170 m_owner
->DrawColLabels( dc
);
3174 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3176 m_owner
->ProcessColLabelMouseEvent( event
);
3180 // This seems to be required for wxMotif otherwise the mouse
3181 // cursor must be in the cell edit control to get key events
3183 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3185 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3188 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3190 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3195 //////////////////////////////////////////////////////////////////////
3197 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3199 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3200 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3201 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3202 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3203 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3206 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3208 const wxPoint
&pos
, const wxSize
&size
)
3209 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3214 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3218 int client_height
= 0;
3219 int client_width
= 0;
3220 GetClientSize( &client_width
, &client_height
);
3222 dc
.SetPen( *wxBLACK_PEN
);
3223 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3224 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3226 dc
.SetPen( *wxWHITE_PEN
);
3227 dc
.DrawLine( 0, 0, client_width
, 0 );
3228 dc
.DrawLine( 0, 0, 0, client_height
);
3232 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3234 m_owner
->ProcessCornerLabelMouseEvent( event
);
3238 // This seems to be required for wxMotif otherwise the mouse
3239 // cursor must be in the cell edit control to get key events
3241 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3243 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3246 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3248 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3253 //////////////////////////////////////////////////////////////////////
3255 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3257 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3258 EVT_PAINT( wxGridWindow::OnPaint
)
3259 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3260 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3261 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3262 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3265 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3266 wxGridRowLabelWindow
*rowLblWin
,
3267 wxGridColLabelWindow
*colLblWin
,
3268 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3269 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3272 m_rowLabelWin
= rowLblWin
;
3273 m_colLabelWin
= colLblWin
;
3274 SetBackgroundColour( "WHITE" );
3278 wxGridWindow::~wxGridWindow()
3283 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3285 wxPaintDC
dc( this );
3286 m_owner
->PrepareDC( dc
);
3287 wxRegion reg
= GetUpdateRegion();
3288 m_owner
->CalcCellsExposed( reg
);
3289 m_owner
->DrawGridCellArea( dc
);
3290 #if WXGRID_DRAW_LINES
3291 m_owner
->DrawAllGridLines( dc
, reg
);
3293 m_owner
->DrawGridSpace( dc
);
3294 m_owner
->DrawHighlight( dc
);
3298 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3300 wxPanel::ScrollWindow( dx
, dy
, rect
);
3301 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3302 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3306 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3308 m_owner
->ProcessGridCellMouseEvent( event
);
3312 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3313 // cursor must be in the cell edit control to get key events
3315 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3317 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3320 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3322 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3325 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3330 //////////////////////////////////////////////////////////////////////
3333 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3335 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3336 EVT_PAINT( wxGrid::OnPaint
)
3337 EVT_SIZE( wxGrid::OnSize
)
3338 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3339 EVT_KEY_UP( wxGrid::OnKeyUp
)
3340 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3343 wxGrid::wxGrid( wxWindow
*parent
,
3348 const wxString
& name
)
3349 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3350 m_colMinWidths(GRID_HASH_SIZE
),
3351 m_rowMinHeights(GRID_HASH_SIZE
)
3360 wxSafeDecRef(m_defaultCellAttr
);
3362 #ifdef DEBUG_ATTR_CACHE
3363 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3364 wxPrintf(_T("wxGrid attribute cache statistics: "
3365 "total: %u, hits: %u (%u%%)\n"),
3366 total
, gs_nAttrCacheHits
,
3367 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3373 delete m_typeRegistry
;
3379 // ----- internal init and update functions
3382 void wxGrid::Create()
3384 m_created
= FALSE
; // set to TRUE by CreateGrid
3386 m_table
= (wxGridTableBase
*) NULL
;
3389 m_cellEditCtrlEnabled
= FALSE
;
3391 m_defaultCellAttr
= new wxGridCellAttr
;
3392 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3394 // Set default cell attributes
3395 m_defaultCellAttr
->SetFont(GetFont());
3396 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3397 m_defaultCellAttr
->SetTextColour(
3398 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3399 m_defaultCellAttr
->SetBackgroundColour(
3400 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3401 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3402 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3407 m_currentCellCoords
= wxGridNoCellCoords
;
3409 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3410 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3412 // create the type registry
3413 m_typeRegistry
= new wxGridTypeRegistry
;
3415 // subwindow components that make up the wxGrid
3416 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3421 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3426 m_colLabelWin
= new wxGridColLabelWindow( this,
3431 m_gridWin
= new wxGridWindow( this,
3438 SetTargetWindow( m_gridWin
);
3442 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3443 wxGrid::wxGridSelectionModes selmode
)
3445 wxCHECK_MSG( !m_created
,
3447 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3449 m_numRows
= numRows
;
3450 m_numCols
= numCols
;
3452 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3453 m_table
->SetView( this );
3455 m_selection
= new wxGridSelection( this, selmode
);
3462 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3466 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3469 m_selection
->SetSelectionMode( selmode
);
3472 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3473 wxGrid::wxGridSelectionModes selmode
)
3477 // RD: Actually, this should probably be allowed. I think it would be
3478 // nice to be able to switch multiple Tables in and out of a single
3479 // View at runtime. Is there anything in the implmentation that would
3482 // At least, you now have to cope with m_selection
3483 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3488 m_numRows
= table
->GetNumberRows();
3489 m_numCols
= table
->GetNumberCols();
3492 m_table
->SetView( this );
3495 m_selection
= new wxGridSelection( this, selmode
);
3506 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3507 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3509 if ( m_rowLabelWin
)
3511 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3515 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3518 m_labelTextColour
= wxColour( _T("BLACK") );
3521 m_attrCache
.row
= -1;
3523 // TODO: something better than this ?
3525 m_labelFont
= this->GetFont();
3526 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3528 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3529 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3531 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3532 m_colLabelVertAlign
= wxALIGN_TOP
;
3534 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3535 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3537 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3538 m_defaultRowHeight
+= 8;
3540 m_defaultRowHeight
+= 4;
3543 m_gridLineColour
= wxColour( 128, 128, 255 );
3544 m_gridLinesEnabled
= TRUE
;
3545 m_cellHighlightColour
= m_gridLineColour
;
3547 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3548 m_winCapture
= (wxWindow
*)NULL
;
3549 m_canDragRowSize
= TRUE
;
3550 m_canDragColSize
= TRUE
;
3551 m_canDragGridSize
= TRUE
;
3553 m_dragRowOrCol
= -1;
3554 m_isDragging
= FALSE
;
3555 m_startDragPos
= wxDefaultPosition
;
3557 m_waitForSlowClick
= FALSE
;
3559 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3560 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3562 m_currentCellCoords
= wxGridNoCellCoords
;
3564 m_selectingTopLeft
= wxGridNoCellCoords
;
3565 m_selectingBottomRight
= wxGridNoCellCoords
;
3566 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3567 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3569 m_editable
= TRUE
; // default for whole grid
3571 m_inOnKeyDown
= FALSE
;
3580 // ----------------------------------------------------------------------------
3581 // the idea is to call these functions only when necessary because they create
3582 // quite big arrays which eat memory mostly unnecessary - in particular, if
3583 // default widths/heights are used for all rows/columns, we may not use these
3586 // with some extra code, it should be possible to only store the
3587 // widths/heights different from default ones but this will be done later...
3588 // ----------------------------------------------------------------------------
3590 void wxGrid::InitRowHeights()
3592 m_rowHeights
.Empty();
3593 m_rowBottoms
.Empty();
3595 m_rowHeights
.Alloc( m_numRows
);
3596 m_rowBottoms
.Alloc( m_numRows
);
3599 for ( int i
= 0; i
< m_numRows
; i
++ )
3601 m_rowHeights
.Add( m_defaultRowHeight
);
3602 rowBottom
+= m_defaultRowHeight
;
3603 m_rowBottoms
.Add( rowBottom
);
3607 void wxGrid::InitColWidths()
3609 m_colWidths
.Empty();
3610 m_colRights
.Empty();
3612 m_colWidths
.Alloc( m_numCols
);
3613 m_colRights
.Alloc( m_numCols
);
3615 for ( int i
= 0; i
< m_numCols
; i
++ )
3617 m_colWidths
.Add( m_defaultColWidth
);
3618 colRight
+= m_defaultColWidth
;
3619 m_colRights
.Add( colRight
);
3623 int wxGrid::GetColWidth(int col
) const
3625 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3628 int wxGrid::GetColLeft(int col
) const
3630 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3631 : m_colRights
[col
] - m_colWidths
[col
];
3634 int wxGrid::GetColRight(int col
) const
3636 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3640 int wxGrid::GetRowHeight(int row
) const
3642 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3645 int wxGrid::GetRowTop(int row
) const
3647 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3648 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3651 int wxGrid::GetRowBottom(int row
) const
3653 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3654 : m_rowBottoms
[row
];
3657 void wxGrid::CalcDimensions()
3660 GetClientSize( &cw
, &ch
);
3662 if ( m_numRows
> 0 || m_numCols
> 0 )
3664 int right
= m_numCols
> 0 ? GetColRight( m_numCols
-1 ) + m_extraWidth
: 0;
3665 int bottom
= m_numRows
> 0 ? GetRowBottom( m_numRows
-1 ) + m_extraHeight
: 0;
3667 // TODO: restore the scroll position that we had before sizing
3670 GetViewStart( &x
, &y
);
3671 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3672 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3678 void wxGrid::CalcWindowSizes()
3681 GetClientSize( &cw
, &ch
);
3683 if ( m_cornerLabelWin
->IsShown() )
3684 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3686 if ( m_colLabelWin
->IsShown() )
3687 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3689 if ( m_rowLabelWin
->IsShown() )
3690 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3692 if ( m_gridWin
->IsShown() )
3693 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3697 // this is called when the grid table sends a message to say that it
3698 // has been redimensioned
3700 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3703 bool result
= FALSE
;
3706 // if we were using the default widths/heights so far, we must change them
3708 if ( m_colWidths
.IsEmpty() )
3713 if ( m_rowHeights
.IsEmpty() )
3719 switch ( msg
.GetId() )
3721 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3723 size_t pos
= msg
.GetCommandInt();
3724 int numRows
= msg
.GetCommandInt2();
3726 m_numRows
+= numRows
;
3728 if ( !m_rowHeights
.IsEmpty() )
3730 for ( i
= 0; i
< numRows
; i
++ )
3732 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3733 m_rowBottoms
.Insert( 0, pos
);
3737 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3739 for ( i
= pos
; i
< m_numRows
; i
++ )
3741 bottom
+= m_rowHeights
[i
];
3742 m_rowBottoms
[i
] = bottom
;
3745 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3747 // if we have just inserted cols into an empty grid the current
3748 // cell will be undefined...
3750 SetCurrentCell( 0, 0 );
3752 m_selection
->UpdateRows( pos
, numRows
);
3753 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3755 attrProvider
->UpdateAttrRows( pos
, numRows
);
3757 if ( !GetBatchCount() )
3760 m_rowLabelWin
->Refresh();
3766 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3768 int numRows
= msg
.GetCommandInt();
3769 int oldNumRows
= m_numRows
;
3770 m_numRows
+= numRows
;
3772 if ( !m_rowHeights
.IsEmpty() )
3774 for ( i
= 0; i
< numRows
; i
++ )
3776 m_rowHeights
.Add( m_defaultRowHeight
);
3777 m_rowBottoms
.Add( 0 );
3781 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3783 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3785 bottom
+= m_rowHeights
[i
];
3786 m_rowBottoms
[i
] = bottom
;
3789 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3791 // if we have just inserted cols into an empty grid the current
3792 // cell will be undefined...
3794 SetCurrentCell( 0, 0 );
3796 if ( !GetBatchCount() )
3799 m_rowLabelWin
->Refresh();
3805 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3807 size_t pos
= msg
.GetCommandInt();
3808 int numRows
= msg
.GetCommandInt2();
3809 m_numRows
-= numRows
;
3811 if ( !m_rowHeights
.IsEmpty() )
3813 for ( i
= 0; i
< numRows
; i
++ )
3815 m_rowHeights
.Remove( pos
);
3816 m_rowBottoms
.Remove( pos
);
3820 for ( i
= 0; i
< m_numRows
; i
++ )
3822 h
+= m_rowHeights
[i
];
3823 m_rowBottoms
[i
] = h
;
3828 m_currentCellCoords
= wxGridNoCellCoords
;
3832 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3833 m_currentCellCoords
.Set( 0, 0 );
3835 m_selection
->UpdateRows( pos
, -((int)numRows
) );
3836 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3838 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
3839 // ifdef'd out following patch from Paul Gammans
3841 // No need to touch column attributes, unless we
3842 // removed _all_ rows, in this case, we remove
3843 // all column attributes.
3844 // I hate to do this here, but the
3845 // needed data is not available inside UpdateAttrRows.
3846 if ( !GetNumberRows() )
3847 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
3850 if ( !GetBatchCount() )
3853 m_rowLabelWin
->Refresh();
3859 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3861 size_t pos
= msg
.GetCommandInt();
3862 int numCols
= msg
.GetCommandInt2();
3863 m_numCols
+= numCols
;
3865 if ( !m_colWidths
.IsEmpty() )
3867 for ( i
= 0; i
< numCols
; i
++ )
3869 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3870 m_colRights
.Insert( 0, pos
);
3874 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3876 for ( i
= pos
; i
< m_numCols
; i
++ )
3878 right
+= m_colWidths
[i
];
3879 m_colRights
[i
] = right
;
3882 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3884 // if we have just inserted cols into an empty grid the current
3885 // cell will be undefined...
3887 SetCurrentCell( 0, 0 );
3889 m_selection
->UpdateCols( pos
, numCols
);
3890 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3892 attrProvider
->UpdateAttrCols( pos
, numCols
);
3893 if ( !GetBatchCount() )
3896 m_colLabelWin
->Refresh();
3903 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3905 int numCols
= msg
.GetCommandInt();
3906 int oldNumCols
= m_numCols
;
3907 m_numCols
+= numCols
;
3908 if ( !m_colWidths
.IsEmpty() )
3910 for ( i
= 0; i
< numCols
; i
++ )
3912 m_colWidths
.Add( m_defaultColWidth
);
3913 m_colRights
.Add( 0 );
3917 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3919 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3921 right
+= m_colWidths
[i
];
3922 m_colRights
[i
] = right
;
3925 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3927 // if we have just inserted cols into an empty grid the current
3928 // cell will be undefined...
3930 SetCurrentCell( 0, 0 );
3932 if ( !GetBatchCount() )
3935 m_colLabelWin
->Refresh();
3941 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3943 size_t pos
= msg
.GetCommandInt();
3944 int numCols
= msg
.GetCommandInt2();
3945 m_numCols
-= numCols
;
3947 if ( !m_colWidths
.IsEmpty() )
3949 for ( i
= 0; i
< numCols
; i
++ )
3951 m_colWidths
.Remove( pos
);
3952 m_colRights
.Remove( pos
);
3956 for ( i
= 0; i
< m_numCols
; i
++ )
3958 w
+= m_colWidths
[i
];
3964 m_currentCellCoords
= wxGridNoCellCoords
;
3968 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3969 m_currentCellCoords
.Set( 0, 0 );
3971 m_selection
->UpdateCols( pos
, -((int)numCols
) );
3972 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3974 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
3975 // ifdef'd out following patch from Paul Gammans
3977 // No need to touch row attributes, unless we
3978 // removed _all_ columns, in this case, we remove
3979 // all row attributes.
3980 // I hate to do this here, but the
3981 // needed data is not available inside UpdateAttrCols.
3982 if ( !GetNumberCols() )
3983 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
3986 if ( !GetBatchCount() )
3989 m_colLabelWin
->Refresh();
3994 // There is no path to this code !!!!!!
4000 if (result
&& !GetBatchCount() )
4001 m_gridWin
->Refresh();
4006 void wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4008 wxRegionIterator
iter( reg
);
4011 m_rowLabelsExposed
.Empty();
4018 // TODO: remove this when we can...
4019 // There is a bug in wxMotif that gives garbage update
4020 // rectangles if you jump-scroll a long way by clicking the
4021 // scrollbar with middle button. This is a work-around
4023 #if defined(__WXMOTIF__)
4025 m_gridWin
->GetClientSize( &cw
, &ch
);
4026 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4027 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4030 // logical bounds of update region
4033 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4034 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4036 // find the row labels within these bounds
4039 for ( row
= 0; row
< m_numRows
; row
++ )
4041 if ( GetRowBottom(row
) < top
)
4044 if ( GetRowTop(row
) > bottom
)
4047 m_rowLabelsExposed
.Add( row
);
4055 void wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4057 wxRegionIterator
iter( reg
);
4060 m_colLabelsExposed
.Empty();
4067 // TODO: remove this when we can...
4068 // There is a bug in wxMotif that gives garbage update
4069 // rectangles if you jump-scroll a long way by clicking the
4070 // scrollbar with middle button. This is a work-around
4072 #if defined(__WXMOTIF__)
4074 m_gridWin
->GetClientSize( &cw
, &ch
);
4075 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4076 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4079 // logical bounds of update region
4082 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4083 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4085 // find the cells within these bounds
4088 for ( col
= 0; col
< m_numCols
; col
++ )
4090 if ( GetColRight(col
) < left
)
4093 if ( GetColLeft(col
) > right
)
4096 m_colLabelsExposed
.Add( col
);
4104 void wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4106 wxRegionIterator
iter( reg
);
4109 m_cellsExposed
.Empty();
4110 m_rowsExposed
.Empty();
4111 m_colsExposed
.Empty();
4113 int left
, top
, right
, bottom
;
4118 // TODO: remove this when we can...
4119 // There is a bug in wxMotif that gives garbage update
4120 // rectangles if you jump-scroll a long way by clicking the
4121 // scrollbar with middle button. This is a work-around
4123 #if defined(__WXMOTIF__)
4125 m_gridWin
->GetClientSize( &cw
, &ch
);
4126 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4127 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4128 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4129 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4132 // logical bounds of update region
4134 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4135 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4137 // find the cells within these bounds
4140 for ( row
= 0; row
< m_numRows
; row
++ )
4142 if ( GetRowBottom(row
) <= top
)
4145 if ( GetRowTop(row
) > bottom
)
4148 m_rowsExposed
.Add( row
);
4150 for ( col
= 0; col
< m_numCols
; col
++ )
4152 if ( GetColRight(col
) <= left
)
4155 if ( GetColLeft(col
) > right
)
4158 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
4159 m_colsExposed
.Add( col
);
4160 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4169 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4172 wxPoint
pos( event
.GetPosition() );
4173 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4175 if ( event
.Dragging() )
4177 m_isDragging
= TRUE
;
4179 if ( event
.LeftIsDown() )
4181 switch( m_cursorMode
)
4183 case WXGRID_CURSOR_RESIZE_ROW
:
4185 int cw
, ch
, left
, dummy
;
4186 m_gridWin
->GetClientSize( &cw
, &ch
);
4187 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4189 wxClientDC
dc( m_gridWin
);
4192 GetRowTop(m_dragRowOrCol
) +
4193 GetRowMinimalHeight(m_dragRowOrCol
) );
4194 dc
.SetLogicalFunction(wxINVERT
);
4195 if ( m_dragLastPos
>= 0 )
4197 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4199 dc
.DrawLine( left
, y
, left
+cw
, y
);
4204 case WXGRID_CURSOR_SELECT_ROW
:
4205 if ( (row
= YToRow( y
)) >= 0 )
4207 m_selection
->SelectRow( row
,
4208 event
.ControlDown(),
4214 // default label to suppress warnings about "enumeration value
4215 // 'xxx' not handled in switch
4223 m_isDragging
= FALSE
;
4226 // ------------ Entering or leaving the window
4228 if ( event
.Entering() || event
.Leaving() )
4230 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4234 // ------------ Left button pressed
4236 else if ( event
.LeftDown() )
4238 // don't send a label click event for a hit on the
4239 // edge of the row label - this is probably the user
4240 // wanting to resize the row
4242 if ( YToEdgeOfRow(y
) < 0 )
4246 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4248 if ( !event
.ShiftDown() && !event
.ControlDown() )
4250 if ( event
.ShiftDown() )
4251 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4254 GetNumberCols() - 1,
4255 event
.ControlDown(),
4260 m_selection
->SelectRow( row
,
4261 event
.ControlDown(),
4265 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4270 // starting to drag-resize a row
4272 if ( CanDragRowSize() )
4273 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4278 // ------------ Left double click
4280 else if (event
.LeftDClick() )
4282 if ( YToEdgeOfRow(y
) < 0 )
4285 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4290 // ------------ Left button released
4292 else if ( event
.LeftUp() )
4294 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4296 DoEndDragResizeRow();
4298 // Note: we are ending the event *after* doing
4299 // default processing in this case
4301 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4304 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4309 // ------------ Right button down
4311 else if ( event
.RightDown() )
4314 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4316 // no default action at the moment
4321 // ------------ Right double click
4323 else if ( event
.RightDClick() )
4326 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4328 // no default action at the moment
4333 // ------------ No buttons down and mouse moving
4335 else if ( event
.Moving() )
4337 m_dragRowOrCol
= YToEdgeOfRow( y
);
4338 if ( m_dragRowOrCol
>= 0 )
4340 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4342 // don't capture the mouse yet
4343 if ( CanDragRowSize() )
4344 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4347 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4349 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4355 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4358 wxPoint
pos( event
.GetPosition() );
4359 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4361 if ( event
.Dragging() )
4363 m_isDragging
= TRUE
;
4365 if ( event
.LeftIsDown() )
4367 switch( m_cursorMode
)
4369 case WXGRID_CURSOR_RESIZE_COL
:
4371 int cw
, ch
, dummy
, top
;
4372 m_gridWin
->GetClientSize( &cw
, &ch
);
4373 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4375 wxClientDC
dc( m_gridWin
);
4378 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4379 GetColMinimalWidth(m_dragRowOrCol
));
4380 dc
.SetLogicalFunction(wxINVERT
);
4381 if ( m_dragLastPos
>= 0 )
4383 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4385 dc
.DrawLine( x
, top
, x
, top
+ch
);
4390 case WXGRID_CURSOR_SELECT_COL
:
4391 if ( (col
= XToCol( x
)) >= 0 )
4393 m_selection
->SelectCol( col
,
4394 event
.ControlDown(),
4400 // default label to suppress warnings about "enumeration value
4401 // 'xxx' not handled in switch
4409 m_isDragging
= FALSE
;
4412 // ------------ Entering or leaving the window
4414 if ( event
.Entering() || event
.Leaving() )
4416 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4420 // ------------ Left button pressed
4422 else if ( event
.LeftDown() )
4424 // don't send a label click event for a hit on the
4425 // edge of the col label - this is probably the user
4426 // wanting to resize the col
4428 if ( XToEdgeOfCol(x
) < 0 )
4432 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4434 if ( !event
.ShiftDown() && !event
.ControlDown() )
4436 if ( event
.ShiftDown() )
4437 m_selection
->SelectBlock( 0,
4438 m_currentCellCoords
.GetCol(),
4439 GetNumberRows() - 1, col
,
4440 event
.ControlDown(),
4445 m_selection
->SelectCol( col
,
4446 event
.ControlDown(),
4450 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4455 // starting to drag-resize a col
4457 if ( CanDragColSize() )
4458 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4463 // ------------ Left double click
4465 if ( event
.LeftDClick() )
4467 if ( XToEdgeOfCol(x
) < 0 )
4470 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4475 // ------------ Left button released
4477 else if ( event
.LeftUp() )
4479 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4481 DoEndDragResizeCol();
4483 // Note: we are ending the event *after* doing
4484 // default processing in this case
4486 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4489 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4494 // ------------ Right button down
4496 else if ( event
.RightDown() )
4499 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4501 // no default action at the moment
4506 // ------------ Right double click
4508 else if ( event
.RightDClick() )
4511 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4513 // no default action at the moment
4518 // ------------ No buttons down and mouse moving
4520 else if ( event
.Moving() )
4522 m_dragRowOrCol
= XToEdgeOfCol( x
);
4523 if ( m_dragRowOrCol
>= 0 )
4525 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4527 // don't capture the cursor yet
4528 if ( CanDragColSize() )
4529 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4532 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4534 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4540 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4542 if ( event
.LeftDown() )
4544 // indicate corner label by having both row and
4547 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4553 else if ( event
.LeftDClick() )
4555 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4558 else if ( event
.RightDown() )
4560 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4562 // no default action at the moment
4566 else if ( event
.RightDClick() )
4568 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4570 // no default action at the moment
4575 void wxGrid::ChangeCursorMode(CursorMode mode
,
4580 static const wxChar
*cursorModes
[] =
4589 wxLogTrace(_T("grid"),
4590 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4591 win
== m_colLabelWin
? _T("colLabelWin")
4592 : win
? _T("rowLabelWin")
4594 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4595 #endif // __WXDEBUG__
4597 if ( mode
== m_cursorMode
)
4602 // by default use the grid itself
4608 m_winCapture
->ReleaseMouse();
4609 m_winCapture
= (wxWindow
*)NULL
;
4612 m_cursorMode
= mode
;
4614 switch ( m_cursorMode
)
4616 case WXGRID_CURSOR_RESIZE_ROW
:
4617 win
->SetCursor( m_rowResizeCursor
);
4620 case WXGRID_CURSOR_RESIZE_COL
:
4621 win
->SetCursor( m_colResizeCursor
);
4625 win
->SetCursor( *wxSTANDARD_CURSOR
);
4628 // we need to capture mouse when resizing
4629 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4630 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4632 if ( captureMouse
&& resize
)
4634 win
->CaptureMouse();
4639 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4642 wxPoint
pos( event
.GetPosition() );
4643 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4645 wxGridCellCoords coords
;
4646 XYToCell( x
, y
, coords
);
4648 if ( event
.Dragging() )
4650 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4652 // Don't start doing anything until the mouse has been drug at
4653 // least 3 pixels in any direction...
4656 if (m_startDragPos
== wxDefaultPosition
)
4658 m_startDragPos
= pos
;
4661 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4665 m_isDragging
= TRUE
;
4666 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4668 // Hide the edit control, so it
4669 // won't interfer with drag-shrinking.
4670 if ( IsCellEditControlShown() )
4672 HideCellEditControl();
4673 SaveEditControlValue();
4676 // Have we captured the mouse yet?
4679 m_winCapture
= m_gridWin
;
4680 m_winCapture
->CaptureMouse();
4683 if ( coords
!= wxGridNoCellCoords
)
4685 if ( event
.ControlDown() )
4687 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4688 m_selectingKeyboard
= coords
;
4689 HighlightBlock ( m_selectingKeyboard
, coords
);
4693 if ( !IsSelection() )
4695 HighlightBlock( coords
, coords
);
4699 HighlightBlock( m_currentCellCoords
, coords
);
4703 if (! IsVisible(coords
))
4705 MakeCellVisible(coords
);
4706 // TODO: need to introduce a delay or something here. The
4707 // scrolling is way to fast, at least on MSW - also on GTK.
4711 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4713 int cw
, ch
, left
, dummy
;
4714 m_gridWin
->GetClientSize( &cw
, &ch
);
4715 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4717 wxClientDC
dc( m_gridWin
);
4719 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4720 GetRowMinimalHeight(m_dragRowOrCol
) );
4721 dc
.SetLogicalFunction(wxINVERT
);
4722 if ( m_dragLastPos
>= 0 )
4724 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4726 dc
.DrawLine( left
, y
, left
+cw
, y
);
4729 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4731 int cw
, ch
, dummy
, top
;
4732 m_gridWin
->GetClientSize( &cw
, &ch
);
4733 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4735 wxClientDC
dc( m_gridWin
);
4737 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4738 GetColMinimalWidth(m_dragRowOrCol
) );
4739 dc
.SetLogicalFunction(wxINVERT
);
4740 if ( m_dragLastPos
>= 0 )
4742 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4744 dc
.DrawLine( x
, top
, x
, top
+ch
);
4751 m_isDragging
= FALSE
;
4752 m_startDragPos
= wxDefaultPosition
;
4754 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4755 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4758 if ( event
.Entering() || event
.Leaving() )
4760 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4761 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4766 // ------------ Left button pressed
4768 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4770 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4775 if ( !event
.ControlDown() )
4777 if ( event
.ShiftDown() )
4779 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4780 m_currentCellCoords
.GetCol(),
4783 event
.ControlDown(),
4788 else if ( XToEdgeOfCol(x
) < 0 &&
4789 YToEdgeOfRow(y
) < 0 )
4791 DisableCellEditControl();
4792 MakeCellVisible( coords
);
4794 // if this is the second click on this cell then start
4796 if ( m_waitForSlowClick
&&
4797 (coords
== m_currentCellCoords
) &&
4798 CanEnableCellControl())
4800 EnableCellEditControl();
4802 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4803 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4806 editor
->StartingClick();
4810 m_waitForSlowClick
= FALSE
;
4814 if ( event
.ControlDown() )
4816 m_selection
->ToggleCellSelection( coords
.GetRow(),
4818 event
.ControlDown(),
4822 m_selectingTopLeft
= wxGridNoCellCoords
;
4823 m_selectingBottomRight
= wxGridNoCellCoords
;
4824 m_selectingKeyboard
= coords
;
4828 SetCurrentCell( coords
);
4829 if ( m_selection
->GetSelectionMode()
4830 != wxGrid::wxGridSelectCells
)
4831 HighlightBlock( coords
, coords
);
4833 m_waitForSlowClick
= TRUE
;
4840 // ------------ Left double click
4842 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4844 DisableCellEditControl();
4846 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4848 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4856 // ------------ Left button released
4858 else if ( event
.LeftUp() )
4860 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4862 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
4863 m_selectingBottomRight
!= wxGridNoCellCoords
)
4867 m_winCapture
->ReleaseMouse();
4868 m_winCapture
= NULL
;
4870 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
4871 m_selectingTopLeft
.GetCol(),
4872 m_selectingBottomRight
.GetRow(),
4873 m_selectingBottomRight
.GetCol(),
4874 event
.ControlDown(),
4878 m_selectingTopLeft
= wxGridNoCellCoords
;
4879 m_selectingBottomRight
= wxGridNoCellCoords
;
4882 // Show the edit control, if it has been hidden for
4884 ShowCellEditControl();
4886 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4888 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4889 DoEndDragResizeRow();
4891 // Note: we are ending the event *after* doing
4892 // default processing in this case
4894 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4896 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4898 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4899 DoEndDragResizeCol();
4901 // Note: we are ending the event *after* doing
4902 // default processing in this case
4904 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4911 // ------------ Right button down
4913 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4915 DisableCellEditControl();
4916 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4921 // no default action at the moment
4926 // ------------ Right double click
4928 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4930 DisableCellEditControl();
4931 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4936 // no default action at the moment
4940 // ------------ Moving and no button action
4942 else if ( event
.Moving() && !event
.IsButton() )
4944 int dragRow
= YToEdgeOfRow( y
);
4945 int dragCol
= XToEdgeOfCol( x
);
4947 // Dragging on the corner of a cell to resize in both
4948 // directions is not implemented yet...
4950 if ( dragRow
>= 0 && dragCol
>= 0 )
4952 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4958 m_dragRowOrCol
= dragRow
;
4960 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4962 if ( CanDragRowSize() && CanDragGridSize() )
4963 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4968 m_dragRowOrCol
= dragCol
;
4976 m_dragRowOrCol
= dragCol
;
4978 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4980 if ( CanDragColSize() && CanDragGridSize() )
4981 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4987 // Neither on a row or col edge
4989 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4991 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4997 void wxGrid::DoEndDragResizeRow()
4999 if ( m_dragLastPos
>= 0 )
5001 // erase the last line and resize the row
5003 int cw
, ch
, left
, dummy
;
5004 m_gridWin
->GetClientSize( &cw
, &ch
);
5005 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5007 wxClientDC
dc( m_gridWin
);
5009 dc
.SetLogicalFunction( wxINVERT
);
5010 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5011 HideCellEditControl();
5012 SaveEditControlValue();
5014 int rowTop
= GetRowTop(m_dragRowOrCol
);
5015 SetRowSize( m_dragRowOrCol
,
5016 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5018 if ( !GetBatchCount() )
5020 // Only needed to get the correct rect.y:
5021 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5023 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5024 rect
.width
= m_rowLabelWidth
;
5025 rect
.height
= ch
- rect
.y
;
5026 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5028 m_gridWin
->Refresh( FALSE
, &rect
);
5031 ShowCellEditControl();
5036 void wxGrid::DoEndDragResizeCol()
5038 if ( m_dragLastPos
>= 0 )
5040 // erase the last line and resize the col
5042 int cw
, ch
, dummy
, top
;
5043 m_gridWin
->GetClientSize( &cw
, &ch
);
5044 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5046 wxClientDC
dc( m_gridWin
);
5048 dc
.SetLogicalFunction( wxINVERT
);
5049 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5050 HideCellEditControl();
5051 SaveEditControlValue();
5053 int colLeft
= GetColLeft(m_dragRowOrCol
);
5054 SetColSize( m_dragRowOrCol
,
5055 wxMax( m_dragLastPos
- colLeft
,
5056 GetColMinimalWidth(m_dragRowOrCol
) ) );
5058 if ( !GetBatchCount() )
5060 // Only needed to get the correct rect.x:
5061 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5063 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5064 rect
.width
= cw
- rect
.x
;
5065 rect
.height
= m_colLabelHeight
;
5066 m_colLabelWin
->Refresh( TRUE
, &rect
);
5068 m_gridWin
->Refresh( FALSE
, &rect
);
5071 ShowCellEditControl();
5078 // ------ interaction with data model
5080 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5082 switch ( msg
.GetId() )
5084 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5085 return GetModelValues();
5087 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5088 return SetModelValues();
5090 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5091 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5092 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5093 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5094 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5095 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5096 return Redimension( msg
);
5105 // The behaviour of this function depends on the grid table class
5106 // Clear() function. For the default wxGridStringTable class the
5107 // behavious is to replace all cell contents with wxEmptyString but
5108 // not to change the number of rows or cols.
5110 void wxGrid::ClearGrid()
5114 if (IsCellEditControlEnabled())
5115 DisableCellEditControl();
5118 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5123 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5125 // TODO: something with updateLabels flag
5129 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5135 if (IsCellEditControlEnabled())
5136 DisableCellEditControl();
5138 return m_table
->InsertRows( pos
, numRows
);
5140 // the table will have sent the results of the insert row
5141 // operation to this view object as a grid table message
5147 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5149 // TODO: something with updateLabels flag
5153 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5157 return ( m_table
&& m_table
->AppendRows( numRows
) );
5158 // the table will have sent the results of the append row
5159 // operation to this view object as a grid table message
5163 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5165 // TODO: something with updateLabels flag
5169 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5175 if (IsCellEditControlEnabled())
5176 DisableCellEditControl();
5178 return (m_table
->DeleteRows( pos
, numRows
));
5179 // the table will have sent the results of the delete row
5180 // operation to this view object as a grid table message
5186 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5188 // TODO: something with updateLabels flag
5192 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5198 if (IsCellEditControlEnabled())
5199 DisableCellEditControl();
5201 return m_table
->InsertCols( pos
, numCols
);
5202 // the table will have sent the results of the insert col
5203 // operation to this view object as a grid table message
5209 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5211 // TODO: something with updateLabels flag
5215 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5219 return ( m_table
&& m_table
->AppendCols( numCols
) );
5220 // the table will have sent the results of the append col
5221 // operation to this view object as a grid table message
5225 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5227 // TODO: something with updateLabels flag
5231 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5237 if (IsCellEditControlEnabled())
5238 DisableCellEditControl();
5240 return ( m_table
->DeleteCols( pos
, numCols
) );
5241 // the table will have sent the results of the delete col
5242 // operation to this view object as a grid table message
5250 // ----- event handlers
5253 // Generate a grid event based on a mouse event and
5254 // return the result of ProcessEvent()
5256 bool wxGrid::SendEvent( const wxEventType type
,
5258 wxMouseEvent
& mouseEv
)
5260 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5262 int rowOrCol
= (row
== -1 ? col
: row
);
5264 wxGridSizeEvent
gridEvt( GetId(),
5268 mouseEv
.GetX() + GetColLabelSize(),
5269 mouseEv
.GetY() + GetRowLabelSize(),
5270 mouseEv
.ControlDown(),
5271 mouseEv
.ShiftDown(),
5273 mouseEv
.MetaDown() );
5275 return GetEventHandler()->ProcessEvent(gridEvt
);
5277 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5279 // Right now, it should _never_ end up here!
5280 wxGridRangeSelectEvent
gridEvt( GetId(),
5284 m_selectingBottomRight
,
5286 mouseEv
.ControlDown(),
5287 mouseEv
.ShiftDown(),
5289 mouseEv
.MetaDown() );
5291 return GetEventHandler()->ProcessEvent(gridEvt
);
5295 wxGridEvent
gridEvt( GetId(),
5299 mouseEv
.GetX() + GetColLabelSize(),
5300 mouseEv
.GetY() + GetRowLabelSize(),
5302 mouseEv
.ControlDown(),
5303 mouseEv
.ShiftDown(),
5305 mouseEv
.MetaDown() );
5307 return GetEventHandler()->ProcessEvent(gridEvt
);
5312 // Generate a grid event of specified type and return the result
5313 // of ProcessEvent().
5315 bool wxGrid::SendEvent( const wxEventType type
,
5318 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5320 int rowOrCol
= (row
== -1 ? col
: row
);
5322 wxGridSizeEvent
gridEvt( GetId(),
5327 return GetEventHandler()->ProcessEvent(gridEvt
);
5331 wxGridEvent
gridEvt( GetId(),
5336 return GetEventHandler()->ProcessEvent(gridEvt
);
5341 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5343 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5347 // This is just here to make sure that CalcDimensions gets called when
5348 // the grid view is resized... then the size event is skipped to allow
5349 // the box sizers to handle everything
5351 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5358 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5360 if ( m_inOnKeyDown
)
5362 // shouldn't be here - we are going round in circles...
5364 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5367 m_inOnKeyDown
= TRUE
;
5369 // propagate the event up and see if it gets processed
5371 wxWindow
*parent
= GetParent();
5372 wxKeyEvent
keyEvt( event
);
5373 keyEvt
.SetEventObject( parent
);
5375 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5378 // try local handlers
5380 switch ( event
.KeyCode() )
5383 if ( event
.ControlDown() )
5385 MoveCursorUpBlock( event
.ShiftDown() );
5389 MoveCursorUp( event
.ShiftDown() );
5394 if ( event
.ControlDown() )
5396 MoveCursorDownBlock( event
.ShiftDown() );
5400 MoveCursorDown( event
.ShiftDown() );
5405 if ( event
.ControlDown() )
5407 MoveCursorLeftBlock( event
.ShiftDown() );
5411 MoveCursorLeft( event
.ShiftDown() );
5416 if ( event
.ControlDown() )
5418 MoveCursorRightBlock( event
.ShiftDown() );
5422 MoveCursorRight( event
.ShiftDown() );
5427 case WXK_NUMPAD_ENTER
:
5428 if ( event
.ControlDown() )
5430 event
.Skip(); // to let the edit control have the return
5434 if ( GetGridCursorRow() < GetNumberRows()-1 )
5436 MoveCursorDown( event
.ShiftDown() );
5440 // at the bottom of a column
5441 HideCellEditControl();
5442 SaveEditControlValue();
5452 if (event
.ShiftDown())
5454 if ( GetGridCursorCol() > 0 )
5456 MoveCursorLeft( FALSE
);
5461 HideCellEditControl();
5462 SaveEditControlValue();
5467 if ( GetGridCursorCol() < GetNumberCols()-1 )
5469 MoveCursorRight( FALSE
);
5474 HideCellEditControl();
5475 SaveEditControlValue();
5481 if ( event
.ControlDown() )
5483 MakeCellVisible( 0, 0 );
5484 SetCurrentCell( 0, 0 );
5493 if ( event
.ControlDown() )
5495 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5496 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5513 if ( event
.ControlDown() )
5515 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5516 m_currentCellCoords
.GetCol(),
5517 event
.ControlDown(),
5523 if ( !IsEditable() )
5525 MoveCursorRight( FALSE
);
5528 // Otherwise fall through to default
5531 // is it possible to edit the current cell at all?
5532 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5534 // yes, now check whether the cells editor accepts the key
5535 int row
= m_currentCellCoords
.GetRow();
5536 int col
= m_currentCellCoords
.GetCol();
5537 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5538 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5540 // <F2> is special and will always start editing, for
5541 // other keys - ask the editor itself
5542 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5543 || editor
->IsAcceptedKey(event
) )
5545 EnableCellEditControl();
5546 editor
->StartingKey(event
);
5558 // let others process char events with modifiers or all
5559 // char events for readonly cells
5566 m_inOnKeyDown
= FALSE
;
5569 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5571 // try local handlers
5573 if ( event
.KeyCode() == WXK_SHIFT
)
5575 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5576 m_selectingBottomRight
!= wxGridNoCellCoords
)
5577 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5578 m_selectingTopLeft
.GetCol(),
5579 m_selectingBottomRight
.GetRow(),
5580 m_selectingBottomRight
.GetCol(),
5581 event
.ControlDown(),
5585 m_selectingTopLeft
= wxGridNoCellCoords
;
5586 m_selectingBottomRight
= wxGridNoCellCoords
;
5587 m_selectingKeyboard
= wxGridNoCellCoords
;
5591 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5595 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5597 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5599 // the event has been intercepted - do nothing
5603 wxClientDC
dc(m_gridWin
);
5606 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5608 HideCellEditControl();
5609 DisableCellEditControl();
5611 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5614 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5615 if ( !m_gridLinesEnabled
)
5623 CalcCellsExposed( r
);
5625 // Otherwise refresh redraws the highlight!
5626 m_currentCellCoords
= coords
;
5628 DrawGridCellArea(dc
);
5629 DrawAllGridLines( dc
, r
);
5633 m_currentCellCoords
= coords
;
5635 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5636 DrawCellHighlight(dc
, attr
);
5641 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5644 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5646 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5649 rightCol
= GetNumberCols() - 1;
5651 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5654 bottomRow
= GetNumberRows() - 1;
5656 if ( topRow
> bottomRow
)
5663 if ( leftCol
> rightCol
)
5670 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5671 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5673 if ( m_selectingTopLeft
!= updateTopLeft
||
5674 m_selectingBottomRight
!= updateBottomRight
)
5676 // Compute two optimal update rectangles:
5677 // Either one rectangle is a real subset of the
5678 // other, or they are (almost) disjoint!
5680 bool need_refresh
[4];
5684 need_refresh
[3] = FALSE
;
5687 // Store intermediate values
5688 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5689 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5690 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5691 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5693 // Determine the outer/inner coordinates.
5694 if (oldLeft
> leftCol
)
5700 if (oldTop
> topRow
)
5706 if (oldRight
< rightCol
)
5709 oldRight
= rightCol
;
5712 if (oldBottom
< bottomRow
)
5715 oldBottom
= bottomRow
;
5719 // Now, either the stuff marked old is the outer
5720 // rectangle or we don't have a situation where one
5721 // is contained in the other.
5723 if ( oldLeft
< leftCol
)
5725 need_refresh
[0] = TRUE
;
5726 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5728 wxGridCellCoords ( oldBottom
,
5732 if ( oldTop
< topRow
)
5734 need_refresh
[1] = TRUE
;
5735 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5737 wxGridCellCoords ( topRow
- 1,
5741 if ( oldRight
> rightCol
)
5743 need_refresh
[2] = TRUE
;
5744 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5746 wxGridCellCoords ( oldBottom
,
5750 if ( oldBottom
> bottomRow
)
5752 need_refresh
[3] = TRUE
;
5753 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5755 wxGridCellCoords ( oldBottom
,
5761 m_selectingTopLeft
= updateTopLeft
;
5762 m_selectingBottomRight
= updateBottomRight
;
5764 // various Refresh() calls
5765 for (i
= 0; i
< 4; i
++ )
5766 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5767 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5770 // never generate an event as it will be generated from
5771 // wxGridSelection::SelectBlock!
5772 // (old comment from when this was the body of SelectBlock)
5777 // ------ functions to get/send data (see also public functions)
5780 bool wxGrid::GetModelValues()
5784 // all we need to do is repaint the grid
5786 m_gridWin
->Refresh();
5794 bool wxGrid::SetModelValues()
5800 for ( row
= 0; row
< m_numRows
; row
++ )
5802 for ( col
= 0; col
< m_numCols
; col
++ )
5804 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5816 // Note - this function only draws cells that are in the list of
5817 // exposed cells (usually set from the update region by
5818 // CalcExposedCells)
5820 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5822 if ( !m_numRows
|| !m_numCols
) return;
5825 size_t numCells
= m_cellsExposed
.GetCount();
5827 for ( i
= 0; i
< numCells
; i
++ )
5829 DrawCell( dc
, m_cellsExposed
[i
] );
5834 void wxGrid::DrawGridSpace( wxDC
& dc
)
5837 m_gridWin
->GetClientSize( &cw
, &ch
);
5840 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5842 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
5843 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
5845 if ( right
> rightCol
|| bottom
> bottomRow
)
5848 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5850 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5851 dc
.SetPen( *wxTRANSPARENT_PEN
);
5853 if ( right
> rightCol
)
5855 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5858 if ( bottom
> bottomRow
)
5860 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5866 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5868 int row
= coords
.GetRow();
5869 int col
= coords
.GetCol();
5871 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5874 // we draw the cell border ourselves
5875 #if !WXGRID_DRAW_LINES
5876 if ( m_gridLinesEnabled
)
5877 DrawCellBorder( dc
, coords
);
5880 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5882 bool isCurrent
= coords
== m_currentCellCoords
;
5884 wxRect rect
= CellToRect( row
, col
);
5886 // if the editor is shown, we should use it and not the renderer
5887 // Note: However, only if it is really _shown_, i.e. not hidden!
5888 if ( isCurrent
&& IsCellEditControlShown() )
5890 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5891 editor
->PaintBackground(rect
, attr
);
5896 // but all the rest is drawn by the cell renderer and hence may be
5898 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5899 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5906 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5908 int row
= m_currentCellCoords
.GetRow();
5909 int col
= m_currentCellCoords
.GetCol();
5911 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5914 wxRect rect
= CellToRect(row
, col
);
5916 // hmmm... what could we do here to show that the cell is disabled?
5917 // for now, I just draw a thinner border than for the other ones, but
5918 // it doesn't look really good
5919 dc
.SetPen(wxPen(m_cellHighlightColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5920 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5922 dc
.DrawRectangle(rect
);
5925 // VZ: my experiments with 3d borders...
5927 // how to properly set colours for arbitrary bg?
5928 wxCoord x1
= rect
.x
,
5930 x2
= rect
.x
+ rect
.width
-1,
5931 y2
= rect
.y
+ rect
.height
-1;
5933 dc
.SetPen(*wxWHITE_PEN
);
5934 dc
.DrawLine(x1
, y1
, x2
, y1
);
5935 dc
.DrawLine(x1
, y1
, x1
, y2
);
5937 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5938 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5940 dc
.SetPen(*wxBLACK_PEN
);
5941 dc
.DrawLine(x1
, y2
, x2
, y2
);
5942 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5947 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5949 int row
= coords
.GetRow();
5950 int col
= coords
.GetCol();
5951 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5954 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5956 // right hand border
5958 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5959 GetColRight(col
), GetRowBottom(row
) );
5963 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5964 GetColRight(col
), GetRowBottom(row
) );
5967 void wxGrid::DrawHighlight(wxDC
& dc
)
5969 // This if block was previously in wxGrid::OnPaint but that doesn't
5970 // seem to get called under wxGTK - MB
5972 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5973 m_numRows
&& m_numCols
)
5975 m_currentCellCoords
.Set(0, 0);
5978 if ( IsCellEditControlShown() )
5980 // don't show highlight when the edit control is shown
5984 // if the active cell was repainted, repaint its highlight too because it
5985 // might have been damaged by the grid lines
5986 size_t count
= m_cellsExposed
.GetCount();
5987 for ( size_t n
= 0; n
< count
; n
++ )
5989 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5991 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5992 DrawCellHighlight(dc
, attr
);
6000 // TODO: remove this ???
6001 // This is used to redraw all grid lines e.g. when the grid line colour
6004 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED_GTK(reg
) )
6006 if ( !m_gridLinesEnabled
||
6008 !m_numCols
) return;
6010 int top
, bottom
, left
, right
;
6012 #if 0 //#ifndef __WXGTK__
6016 m_gridWin
->GetClientSize(&cw
, &ch
);
6018 // virtual coords of visible area
6020 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6021 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6026 reg
.GetBox(x
, y
, w
, h
);
6027 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6028 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6032 m_gridWin
->GetClientSize(&cw
, &ch
);
6033 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6034 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6037 // avoid drawing grid lines past the last row and col
6039 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6040 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6042 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6044 // horizontal grid lines
6047 for ( i
= 0; i
< m_numRows
; i
++ )
6049 int bot
= GetRowBottom(i
) - 1;
6058 dc
.DrawLine( left
, bot
, right
, bot
);
6063 // vertical grid lines
6065 for ( i
= 0; i
< m_numCols
; i
++ )
6067 int colRight
= GetColRight(i
) - 1;
6068 if ( colRight
> right
)
6073 if ( colRight
>= left
)
6075 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6081 void wxGrid::DrawRowLabels( wxDC
& dc
)
6083 if ( !m_numRows
) return;
6086 size_t numLabels
= m_rowLabelsExposed
.GetCount();
6088 for ( i
= 0; i
< numLabels
; i
++ )
6090 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
6095 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6097 if ( GetRowHeight(row
) <= 0 )
6100 int rowTop
= GetRowTop(row
),
6101 rowBottom
= GetRowBottom(row
) - 1;
6103 dc
.SetPen( *wxBLACK_PEN
);
6104 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6105 m_rowLabelWidth
-1, rowBottom
);
6107 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6109 dc
.SetPen( *wxWHITE_PEN
);
6110 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6111 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6113 dc
.SetBackgroundMode( wxTRANSPARENT
);
6114 dc
.SetTextForeground( GetLabelTextColour() );
6115 dc
.SetFont( GetLabelFont() );
6118 GetRowLabelAlignment( &hAlign
, &vAlign
);
6122 rect
.SetY( GetRowTop(row
) + 2 );
6123 rect
.SetWidth( m_rowLabelWidth
- 4 );
6124 rect
.SetHeight( GetRowHeight(row
) - 4 );
6125 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6129 void wxGrid::DrawColLabels( wxDC
& dc
)
6131 if ( !m_numCols
) return;
6134 size_t numLabels
= m_colLabelsExposed
.GetCount();
6136 for ( i
= 0; i
< numLabels
; i
++ )
6138 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
6143 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6145 if ( GetColWidth(col
) <= 0 )
6148 int colLeft
= GetColLeft(col
),
6149 colRight
= GetColRight(col
) - 1;
6151 dc
.SetPen( *wxBLACK_PEN
);
6152 dc
.DrawLine( colRight
, 0,
6153 colRight
, m_colLabelHeight
-1 );
6155 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6156 colRight
, m_colLabelHeight
-1 );
6158 dc
.SetPen( *wxWHITE_PEN
);
6159 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6160 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6162 dc
.SetBackgroundMode( wxTRANSPARENT
);
6163 dc
.SetTextForeground( GetLabelTextColour() );
6164 dc
.SetFont( GetLabelFont() );
6166 dc
.SetBackgroundMode( wxTRANSPARENT
);
6167 dc
.SetTextForeground( GetLabelTextColour() );
6168 dc
.SetFont( GetLabelFont() );
6171 GetColLabelAlignment( &hAlign
, &vAlign
);
6174 rect
.SetX( colLeft
+ 2 );
6176 rect
.SetWidth( GetColWidth(col
) - 4 );
6177 rect
.SetHeight( m_colLabelHeight
- 4 );
6178 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6182 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6183 const wxString
& value
,
6188 long textWidth
, textHeight
;
6189 long lineWidth
, lineHeight
;
6190 wxArrayString lines
;
6192 dc
.SetClippingRegion( rect
);
6193 StringToLines( value
, lines
);
6194 if ( lines
.GetCount() )
6196 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6197 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6200 switch ( horizAlign
)
6203 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6206 case wxALIGN_CENTRE
:
6207 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6216 switch ( vertAlign
)
6218 case wxALIGN_BOTTOM
:
6219 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6222 case wxALIGN_CENTRE
:
6223 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6232 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6234 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6239 dc
.DestroyClippingRegion();
6243 // Split multi line text up into an array of strings. Any existing
6244 // contents of the string array are preserved.
6246 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6250 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6251 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6253 while ( startPos
< (int)tVal
.Length() )
6255 pos
= tVal
.Mid(startPos
).Find( eol
);
6260 else if ( pos
== 0 )
6262 lines
.Add( wxEmptyString
);
6266 lines
.Add( value
.Mid(startPos
, pos
) );
6270 if ( startPos
< (int)value
.Length() )
6272 lines
.Add( value
.Mid( startPos
) );
6277 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6278 wxArrayString
& lines
,
6279 long *width
, long *height
)
6286 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6288 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6289 w
= wxMax( w
, lineW
);
6298 // ------ Batch processing.
6300 void wxGrid::EndBatch()
6302 if ( m_batchCount
> 0 )
6305 if ( !m_batchCount
)
6308 m_rowLabelWin
->Refresh();
6309 m_colLabelWin
->Refresh();
6310 m_cornerLabelWin
->Refresh();
6311 m_gridWin
->Refresh();
6316 // Use this, rather than wxWindow::Refresh(), to force an immediate
6317 // repainting of the grid. Has no effect if you are already inside a
6318 // BeginBatch / EndBatch block.
6320 void wxGrid::ForceRefresh()
6328 // ------ Edit control functions
6332 void wxGrid::EnableEditing( bool edit
)
6334 // TODO: improve this ?
6336 if ( edit
!= m_editable
)
6338 if(!edit
) EnableCellEditControl(edit
);
6344 void wxGrid::EnableCellEditControl( bool enable
)
6349 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6350 SetCurrentCell( 0, 0 );
6352 if ( enable
!= m_cellEditCtrlEnabled
)
6354 // TODO allow the app to Veto() this event?
6355 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
6359 // this should be checked by the caller!
6360 wxASSERT_MSG( CanEnableCellControl(),
6361 _T("can't enable editing for this cell!") );
6363 // do it before ShowCellEditControl()
6364 m_cellEditCtrlEnabled
= enable
;
6366 ShowCellEditControl();
6370 HideCellEditControl();
6371 SaveEditControlValue();
6373 // do it after HideCellEditControl()
6374 m_cellEditCtrlEnabled
= enable
;
6379 bool wxGrid::IsCurrentCellReadOnly() const
6382 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6383 bool readonly
= attr
->IsReadOnly();
6389 bool wxGrid::CanEnableCellControl() const
6391 return m_editable
&& !IsCurrentCellReadOnly();
6394 bool wxGrid::IsCellEditControlEnabled() const
6396 // the cell edit control might be disable for all cells or just for the
6397 // current one if it's read only
6398 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6401 bool wxGrid::IsCellEditControlShown() const
6403 bool isShown
= FALSE
;
6405 if ( m_cellEditCtrlEnabled
)
6407 int row
= m_currentCellCoords
.GetRow();
6408 int col
= m_currentCellCoords
.GetCol();
6409 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6410 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6415 if ( editor
->IsCreated() )
6417 isShown
= editor
->GetControl()->IsShown();
6427 void wxGrid::ShowCellEditControl()
6429 if ( IsCellEditControlEnabled() )
6431 if ( !IsVisible( m_currentCellCoords
) )
6433 m_cellEditCtrlEnabled
= FALSE
;
6438 wxRect rect
= CellToRect( m_currentCellCoords
);
6439 int row
= m_currentCellCoords
.GetRow();
6440 int col
= m_currentCellCoords
.GetCol();
6442 // convert to scrolled coords
6444 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6446 // done in PaintBackground()
6448 // erase the highlight and the cell contents because the editor
6449 // might not cover the entire cell
6450 wxClientDC
dc( m_gridWin
);
6452 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6453 dc
.SetPen(*wxTRANSPARENT_PEN
);
6454 dc
.DrawRectangle(rect
);
6457 // cell is shifted by one pixel
6461 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6462 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6463 if ( !editor
->IsCreated() )
6465 editor
->Create(m_gridWin
, -1,
6466 new wxGridCellEditorEvtHandler(this, editor
));
6469 editor
->Show( TRUE
, attr
);
6471 editor
->SetSize( rect
);
6473 editor
->BeginEdit(row
, col
, this);
6482 void wxGrid::HideCellEditControl()
6484 if ( IsCellEditControlEnabled() )
6486 int row
= m_currentCellCoords
.GetRow();
6487 int col
= m_currentCellCoords
.GetCol();
6489 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6490 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6491 editor
->Show( FALSE
);
6494 m_gridWin
->SetFocus();
6495 wxRect
rect( CellToRect( row
, col
) );
6496 m_gridWin
->Refresh( FALSE
, &rect
);
6501 void wxGrid::SaveEditControlValue()
6503 if ( IsCellEditControlEnabled() )
6505 int row
= m_currentCellCoords
.GetRow();
6506 int col
= m_currentCellCoords
.GetCol();
6508 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6509 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6510 bool changed
= editor
->EndEdit(row
, col
, this);
6517 SendEvent( wxEVT_GRID_CELL_CHANGE
,
6518 m_currentCellCoords
.GetRow(),
6519 m_currentCellCoords
.GetCol() );
6526 // ------ Grid location functions
6527 // Note that all of these functions work with the logical coordinates of
6528 // grid cells and labels so you will need to convert from device
6529 // coordinates for mouse events etc.
6532 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6534 int row
= YToRow(y
);
6535 int col
= XToCol(x
);
6537 if ( row
== -1 || col
== -1 )
6539 coords
= wxGridNoCellCoords
;
6543 coords
.Set( row
, col
);
6548 int wxGrid::YToRow( int y
)
6552 for ( i
= 0; i
< m_numRows
; i
++ )
6554 if ( y
< GetRowBottom(i
) )
6562 int wxGrid::XToCol( int x
)
6566 for ( i
= 0; i
< m_numCols
; i
++ )
6568 if ( x
< GetColRight(i
) )
6576 // return the row number that that the y coord is near the edge of, or
6577 // -1 if not near an edge
6579 int wxGrid::YToEdgeOfRow( int y
)
6583 for ( i
= 0; i
< m_numRows
; i
++ )
6585 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6587 d
= abs( y
- GetRowBottom(i
) );
6588 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6597 // return the col number that that the x coord is near the edge of, or
6598 // -1 if not near an edge
6600 int wxGrid::XToEdgeOfCol( int x
)
6604 for ( i
= 0; i
< m_numCols
; i
++ )
6606 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6608 d
= abs( x
- GetColRight(i
) );
6609 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6618 wxRect
wxGrid::CellToRect( int row
, int col
)
6620 wxRect
rect( -1, -1, -1, -1 );
6622 if ( row
>= 0 && row
< m_numRows
&&
6623 col
>= 0 && col
< m_numCols
)
6625 rect
.x
= GetColLeft(col
);
6626 rect
.y
= GetRowTop(row
);
6627 rect
.width
= GetColWidth(col
);
6628 rect
.height
= GetRowHeight(row
);
6631 // if grid lines are enabled, then the area of the cell is a bit smaller
6632 if (m_gridLinesEnabled
) {
6640 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6642 // get the cell rectangle in logical coords
6644 wxRect
r( CellToRect( row
, col
) );
6646 // convert to device coords
6648 int left
, top
, right
, bottom
;
6649 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6650 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6652 // check against the client area of the grid window
6655 m_gridWin
->GetClientSize( &cw
, &ch
);
6657 if ( wholeCellVisible
)
6659 // is the cell wholly visible ?
6661 return ( left
>= 0 && right
<= cw
&&
6662 top
>= 0 && bottom
<= ch
);
6666 // is the cell partly visible ?
6668 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6669 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6674 // make the specified cell location visible by doing a minimal amount
6677 void wxGrid::MakeCellVisible( int row
, int col
)
6680 int xpos
= -1, ypos
= -1;
6682 if ( row
>= 0 && row
< m_numRows
&&
6683 col
>= 0 && col
< m_numCols
)
6685 // get the cell rectangle in logical coords
6687 wxRect
r( CellToRect( row
, col
) );
6689 // convert to device coords
6691 int left
, top
, right
, bottom
;
6692 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6693 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6696 m_gridWin
->GetClientSize( &cw
, &ch
);
6702 else if ( bottom
> ch
)
6704 int h
= r
.GetHeight();
6706 for ( i
= row
-1; i
>= 0; i
-- )
6708 int rowHeight
= GetRowHeight(i
);
6709 if ( h
+ rowHeight
> ch
)
6716 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6717 // have rounding errors (this is important, because if we do, we
6718 // might not scroll at all and some cells won't be redrawn)
6719 ypos
+= GRID_SCROLL_LINE
/ 2;
6726 else if ( right
> cw
)
6728 int w
= r
.GetWidth();
6730 for ( i
= col
-1; i
>= 0; i
-- )
6732 int colWidth
= GetColWidth(i
);
6733 if ( w
+ colWidth
> cw
)
6740 // see comment for ypos above
6741 xpos
+= GRID_SCROLL_LINE
/ 2;
6744 if ( xpos
!= -1 || ypos
!= -1 )
6746 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6747 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6748 Scroll( xpos
, ypos
);
6756 // ------ Grid cursor movement functions
6759 bool wxGrid::MoveCursorUp( bool expandSelection
)
6761 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6762 m_currentCellCoords
.GetRow() >= 0 )
6764 if ( expandSelection
)
6766 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6767 m_selectingKeyboard
= m_currentCellCoords
;
6768 if ( m_selectingKeyboard
.GetRow() > 0 )
6770 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6771 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6772 m_selectingKeyboard
.GetCol() );
6773 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6776 else if ( m_currentCellCoords
.GetRow() > 0 )
6779 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6780 m_currentCellCoords
.GetCol() );
6781 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6782 m_currentCellCoords
.GetCol() );
6793 bool wxGrid::MoveCursorDown( bool expandSelection
)
6795 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6796 m_currentCellCoords
.GetRow() < m_numRows
)
6798 if ( expandSelection
)
6800 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6801 m_selectingKeyboard
= m_currentCellCoords
;
6802 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
6804 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
6805 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6806 m_selectingKeyboard
.GetCol() );
6807 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6810 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
6813 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
6814 m_currentCellCoords
.GetCol() );
6815 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
6816 m_currentCellCoords
.GetCol() );
6827 bool wxGrid::MoveCursorLeft( bool expandSelection
)
6829 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6830 m_currentCellCoords
.GetCol() >= 0 )
6832 if ( expandSelection
)
6834 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6835 m_selectingKeyboard
= m_currentCellCoords
;
6836 if ( m_selectingKeyboard
.GetCol() > 0 )
6838 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
6839 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6840 m_selectingKeyboard
.GetCol() );
6841 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6844 else if ( m_currentCellCoords
.GetCol() > 0 )
6847 MakeCellVisible( m_currentCellCoords
.GetRow(),
6848 m_currentCellCoords
.GetCol() - 1 );
6849 SetCurrentCell( m_currentCellCoords
.GetRow(),
6850 m_currentCellCoords
.GetCol() - 1 );
6861 bool wxGrid::MoveCursorRight( bool expandSelection
)
6863 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6864 m_currentCellCoords
.GetCol() < m_numCols
)
6866 if ( expandSelection
)
6868 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6869 m_selectingKeyboard
= m_currentCellCoords
;
6870 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
6872 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
6873 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6874 m_selectingKeyboard
.GetCol() );
6875 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6878 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
6881 MakeCellVisible( m_currentCellCoords
.GetRow(),
6882 m_currentCellCoords
.GetCol() + 1 );
6883 SetCurrentCell( m_currentCellCoords
.GetRow(),
6884 m_currentCellCoords
.GetCol() + 1 );
6895 bool wxGrid::MovePageUp()
6897 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6899 int row
= m_currentCellCoords
.GetRow();
6903 m_gridWin
->GetClientSize( &cw
, &ch
);
6905 int y
= GetRowTop(row
);
6906 int newRow
= YToRow( y
- ch
+ 1 );
6911 else if ( newRow
== row
)
6916 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6917 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6925 bool wxGrid::MovePageDown()
6927 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6929 int row
= m_currentCellCoords
.GetRow();
6930 if ( row
< m_numRows
)
6933 m_gridWin
->GetClientSize( &cw
, &ch
);
6935 int y
= GetRowTop(row
);
6936 int newRow
= YToRow( y
+ ch
);
6939 newRow
= m_numRows
- 1;
6941 else if ( newRow
== row
)
6946 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6947 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6955 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
6958 m_currentCellCoords
!= wxGridNoCellCoords
&&
6959 m_currentCellCoords
.GetRow() > 0 )
6961 int row
= m_currentCellCoords
.GetRow();
6962 int col
= m_currentCellCoords
.GetCol();
6964 if ( m_table
->IsEmptyCell(row
, col
) )
6966 // starting in an empty cell: find the next block of
6972 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6975 else if ( m_table
->IsEmptyCell(row
-1, col
) )
6977 // starting at the top of a block: find the next block
6983 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6988 // starting within a block: find the top of the block
6993 if ( m_table
->IsEmptyCell(row
, col
) )
7001 MakeCellVisible( row
, col
);
7002 if ( expandSelection
)
7004 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7005 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7010 SetCurrentCell( row
, col
);
7018 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7021 m_currentCellCoords
!= wxGridNoCellCoords
&&
7022 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7024 int row
= m_currentCellCoords
.GetRow();
7025 int col
= m_currentCellCoords
.GetCol();
7027 if ( m_table
->IsEmptyCell(row
, col
) )
7029 // starting in an empty cell: find the next block of
7032 while ( row
< m_numRows
-1 )
7035 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7038 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7040 // starting at the bottom of a block: find the next block
7043 while ( row
< m_numRows
-1 )
7046 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7051 // starting within a block: find the bottom of the block
7053 while ( row
< m_numRows
-1 )
7056 if ( m_table
->IsEmptyCell(row
, col
) )
7064 MakeCellVisible( row
, col
);
7065 if ( expandSelection
)
7067 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7068 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7073 SetCurrentCell( row
, col
);
7082 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7085 m_currentCellCoords
!= wxGridNoCellCoords
&&
7086 m_currentCellCoords
.GetCol() > 0 )
7088 int row
= m_currentCellCoords
.GetRow();
7089 int col
= m_currentCellCoords
.GetCol();
7091 if ( m_table
->IsEmptyCell(row
, col
) )
7093 // starting in an empty cell: find the next block of
7099 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7102 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7104 // starting at the left of a block: find the next block
7110 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7115 // starting within a block: find the left of the block
7120 if ( m_table
->IsEmptyCell(row
, col
) )
7128 MakeCellVisible( row
, col
);
7129 if ( expandSelection
)
7131 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7132 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7137 SetCurrentCell( row
, col
);
7146 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7149 m_currentCellCoords
!= wxGridNoCellCoords
&&
7150 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7152 int row
= m_currentCellCoords
.GetRow();
7153 int col
= m_currentCellCoords
.GetCol();
7155 if ( m_table
->IsEmptyCell(row
, col
) )
7157 // starting in an empty cell: find the next block of
7160 while ( col
< m_numCols
-1 )
7163 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7166 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7168 // starting at the right of a block: find the next block
7171 while ( col
< m_numCols
-1 )
7174 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7179 // starting within a block: find the right of the block
7181 while ( col
< m_numCols
-1 )
7184 if ( m_table
->IsEmptyCell(row
, col
) )
7192 MakeCellVisible( row
, col
);
7193 if ( expandSelection
)
7195 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7196 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7201 SetCurrentCell( row
, col
);
7213 // ------ Label values and formatting
7216 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7218 *horiz
= m_rowLabelHorizAlign
;
7219 *vert
= m_rowLabelVertAlign
;
7222 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7224 *horiz
= m_colLabelHorizAlign
;
7225 *vert
= m_colLabelVertAlign
;
7228 wxString
wxGrid::GetRowLabelValue( int row
)
7232 return m_table
->GetRowLabelValue( row
);
7242 wxString
wxGrid::GetColLabelValue( int col
)
7246 return m_table
->GetColLabelValue( col
);
7257 void wxGrid::SetRowLabelSize( int width
)
7259 width
= wxMax( width
, 0 );
7260 if ( width
!= m_rowLabelWidth
)
7264 m_rowLabelWin
->Show( FALSE
);
7265 m_cornerLabelWin
->Show( FALSE
);
7267 else if ( m_rowLabelWidth
== 0 )
7269 m_rowLabelWin
->Show( TRUE
);
7270 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7273 m_rowLabelWidth
= width
;
7280 void wxGrid::SetColLabelSize( int height
)
7282 height
= wxMax( height
, 0 );
7283 if ( height
!= m_colLabelHeight
)
7287 m_colLabelWin
->Show( FALSE
);
7288 m_cornerLabelWin
->Show( FALSE
);
7290 else if ( m_colLabelHeight
== 0 )
7292 m_colLabelWin
->Show( TRUE
);
7293 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7296 m_colLabelHeight
= height
;
7303 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7305 if ( m_labelBackgroundColour
!= colour
)
7307 m_labelBackgroundColour
= colour
;
7308 m_rowLabelWin
->SetBackgroundColour( colour
);
7309 m_colLabelWin
->SetBackgroundColour( colour
);
7310 m_cornerLabelWin
->SetBackgroundColour( colour
);
7312 if ( !GetBatchCount() )
7314 m_rowLabelWin
->Refresh();
7315 m_colLabelWin
->Refresh();
7316 m_cornerLabelWin
->Refresh();
7321 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7323 if ( m_labelTextColour
!= colour
)
7325 m_labelTextColour
= colour
;
7326 if ( !GetBatchCount() )
7328 m_rowLabelWin
->Refresh();
7329 m_colLabelWin
->Refresh();
7334 void wxGrid::SetLabelFont( const wxFont
& font
)
7337 if ( !GetBatchCount() )
7339 m_rowLabelWin
->Refresh();
7340 m_colLabelWin
->Refresh();
7344 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7346 // allow old (incorrect) defs to be used
7349 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7350 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7351 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7356 case wxTOP
: vert
= wxALIGN_TOP
; break;
7357 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7358 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7361 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7363 m_rowLabelHorizAlign
= horiz
;
7366 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7368 m_rowLabelVertAlign
= vert
;
7371 if ( !GetBatchCount() )
7373 m_rowLabelWin
->Refresh();
7377 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7379 // allow old (incorrect) defs to be used
7382 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7383 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7384 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7389 case wxTOP
: vert
= wxALIGN_TOP
; break;
7390 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7391 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7394 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7396 m_colLabelHorizAlign
= horiz
;
7399 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7401 m_colLabelVertAlign
= vert
;
7404 if ( !GetBatchCount() )
7406 m_colLabelWin
->Refresh();
7410 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7414 m_table
->SetRowLabelValue( row
, s
);
7415 if ( !GetBatchCount() )
7417 wxRect rect
= CellToRect( row
, 0);
7418 if ( rect
.height
> 0 )
7420 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7422 rect
.width
= m_rowLabelWidth
;
7423 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7429 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7433 m_table
->SetColLabelValue( col
, s
);
7434 if ( !GetBatchCount() )
7436 wxRect rect
= CellToRect( 0, col
);
7437 if ( rect
.width
> 0 )
7439 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7441 rect
.height
= m_colLabelHeight
;
7442 m_colLabelWin
->Refresh( TRUE
, &rect
);
7448 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7450 if ( m_gridLineColour
!= colour
)
7452 m_gridLineColour
= colour
;
7454 wxClientDC
dc( m_gridWin
);
7456 DrawAllGridLines( dc
, wxRegion() );
7461 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7463 if ( m_cellHighlightColour
!= colour
)
7465 m_cellHighlightColour
= colour
;
7467 wxClientDC
dc( m_gridWin
);
7469 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7470 DrawCellHighlight(dc
, attr
);
7475 void wxGrid::EnableGridLines( bool enable
)
7477 if ( enable
!= m_gridLinesEnabled
)
7479 m_gridLinesEnabled
= enable
;
7481 if ( !GetBatchCount() )
7485 wxClientDC
dc( m_gridWin
);
7487 DrawAllGridLines( dc
, wxRegion() );
7491 m_gridWin
->Refresh();
7498 int wxGrid::GetDefaultRowSize()
7500 return m_defaultRowHeight
;
7503 int wxGrid::GetRowSize( int row
)
7505 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7507 return GetRowHeight(row
);
7510 int wxGrid::GetDefaultColSize()
7512 return m_defaultColWidth
;
7515 int wxGrid::GetColSize( int col
)
7517 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7519 return GetColWidth(col
);
7522 // ============================================================================
7523 // access to the grid attributes: each of them has a default value in the grid
7524 // itself and may be overidden on a per-cell basis
7525 // ============================================================================
7527 // ----------------------------------------------------------------------------
7528 // setting default attributes
7529 // ----------------------------------------------------------------------------
7531 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7533 m_defaultCellAttr
->SetBackgroundColour(col
);
7535 m_gridWin
->SetBackgroundColour(col
);
7539 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7541 m_defaultCellAttr
->SetTextColour(col
);
7544 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7546 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7549 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7551 m_defaultCellAttr
->SetFont(font
);
7554 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7556 m_defaultCellAttr
->SetRenderer(renderer
);
7559 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7561 m_defaultCellAttr
->SetEditor(editor
);
7564 // ----------------------------------------------------------------------------
7565 // access to the default attrbiutes
7566 // ----------------------------------------------------------------------------
7568 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7570 return m_defaultCellAttr
->GetBackgroundColour();
7573 wxColour
wxGrid::GetDefaultCellTextColour()
7575 return m_defaultCellAttr
->GetTextColour();
7578 wxFont
wxGrid::GetDefaultCellFont()
7580 return m_defaultCellAttr
->GetFont();
7583 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7585 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7588 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7590 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7593 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7595 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7598 // ----------------------------------------------------------------------------
7599 // access to cell attributes
7600 // ----------------------------------------------------------------------------
7602 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7604 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7605 wxColour colour
= attr
->GetBackgroundColour();
7610 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7612 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7613 wxColour colour
= attr
->GetTextColour();
7618 wxFont
wxGrid::GetCellFont( int row
, int col
)
7620 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7621 wxFont font
= attr
->GetFont();
7626 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7628 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7629 attr
->GetAlignment(horiz
, vert
);
7633 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7635 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7636 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7642 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7644 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7645 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7651 bool wxGrid::IsReadOnly(int row
, int col
) const
7653 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7654 bool isReadOnly
= attr
->IsReadOnly();
7659 // ----------------------------------------------------------------------------
7660 // attribute support: cache, automatic provider creation, ...
7661 // ----------------------------------------------------------------------------
7663 bool wxGrid::CanHaveAttributes()
7670 return m_table
->CanHaveAttributes();
7673 void wxGrid::ClearAttrCache()
7675 if ( m_attrCache
.row
!= -1 )
7677 wxSafeDecRef(m_attrCache
.attr
);
7678 m_attrCache
.row
= -1;
7682 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7684 wxGrid
*self
= (wxGrid
*)this; // const_cast
7686 self
->ClearAttrCache();
7687 self
->m_attrCache
.row
= row
;
7688 self
->m_attrCache
.col
= col
;
7689 self
->m_attrCache
.attr
= attr
;
7693 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7695 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7697 *attr
= m_attrCache
.attr
;
7698 wxSafeIncRef(m_attrCache
.attr
);
7700 #ifdef DEBUG_ATTR_CACHE
7701 gs_nAttrCacheHits
++;
7708 #ifdef DEBUG_ATTR_CACHE
7709 gs_nAttrCacheMisses
++;
7715 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7717 wxGridCellAttr
*attr
;
7718 if ( !LookupAttr(row
, col
, &attr
) )
7720 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
7721 CacheAttr(row
, col
, attr
);
7725 attr
->SetDefAttr(m_defaultCellAttr
);
7729 attr
= m_defaultCellAttr
;
7736 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7738 wxGridCellAttr
*attr
;
7739 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
7741 wxASSERT_MSG( m_table
,
7742 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
7744 attr
= m_table
->GetAttr(row
, col
);
7747 attr
= new wxGridCellAttr
;
7749 // artificially inc the ref count to match DecRef() in caller
7752 m_table
->SetAttr(attr
, row
, col
);
7755 CacheAttr(row
, col
, attr
);
7757 attr
->SetDefAttr(m_defaultCellAttr
);
7761 // ----------------------------------------------------------------------------
7762 // setting column attributes (wrappers around SetColAttr)
7763 // ----------------------------------------------------------------------------
7765 void wxGrid::SetColFormatBool(int col
)
7767 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7770 void wxGrid::SetColFormatNumber(int col
)
7772 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7775 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7777 wxString typeName
= wxGRID_VALUE_FLOAT
;
7778 if ( (width
!= -1) || (precision
!= -1) )
7780 typeName
<< _T(':') << width
<< _T(',') << precision
;
7783 SetColFormatCustom(col
, typeName
);
7786 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7788 wxGridCellAttr
*attr
= new wxGridCellAttr
;
7789 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7790 attr
->SetRenderer(renderer
);
7792 SetColAttr(col
, attr
);
7795 // ----------------------------------------------------------------------------
7796 // setting cell attributes: this is forwarded to the table
7797 // ----------------------------------------------------------------------------
7799 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7801 if ( CanHaveAttributes() )
7803 m_table
->SetRowAttr(attr
, row
);
7811 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7813 if ( CanHaveAttributes() )
7815 m_table
->SetColAttr(attr
, col
);
7823 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7825 if ( CanHaveAttributes() )
7827 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7828 attr
->SetBackgroundColour(colour
);
7833 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7835 if ( CanHaveAttributes() )
7837 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7838 attr
->SetTextColour(colour
);
7843 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7845 if ( CanHaveAttributes() )
7847 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7848 attr
->SetFont(font
);
7853 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7855 if ( CanHaveAttributes() )
7857 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7858 attr
->SetAlignment(horiz
, vert
);
7863 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7865 if ( CanHaveAttributes() )
7867 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7868 attr
->SetRenderer(renderer
);
7873 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7875 if ( CanHaveAttributes() )
7877 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7878 attr
->SetEditor(editor
);
7883 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7885 if ( CanHaveAttributes() )
7887 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7888 attr
->SetReadOnly(isReadOnly
);
7893 // ----------------------------------------------------------------------------
7894 // Data type registration
7895 // ----------------------------------------------------------------------------
7897 void wxGrid::RegisterDataType(const wxString
& typeName
,
7898 wxGridCellRenderer
* renderer
,
7899 wxGridCellEditor
* editor
)
7901 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7905 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7907 wxString typeName
= m_table
->GetTypeName(row
, col
);
7908 return GetDefaultEditorForType(typeName
);
7911 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7913 wxString typeName
= m_table
->GetTypeName(row
, col
);
7914 return GetDefaultRendererForType(typeName
);
7918 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7920 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7921 if ( index
== wxNOT_FOUND
)
7923 wxFAIL_MSG(wxT("Unknown data type name"));
7928 return m_typeRegistry
->GetEditor(index
);
7932 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7934 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7935 if ( index
== wxNOT_FOUND
)
7937 wxFAIL_MSG(wxT("Unknown data type name"));
7942 return m_typeRegistry
->GetRenderer(index
);
7946 // ----------------------------------------------------------------------------
7948 // ----------------------------------------------------------------------------
7950 void wxGrid::EnableDragRowSize( bool enable
)
7952 m_canDragRowSize
= enable
;
7956 void wxGrid::EnableDragColSize( bool enable
)
7958 m_canDragColSize
= enable
;
7961 void wxGrid::EnableDragGridSize( bool enable
)
7963 m_canDragGridSize
= enable
;
7967 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7969 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
7971 if ( resizeExistingRows
)
7979 void wxGrid::SetRowSize( int row
, int height
)
7981 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
7983 if ( m_rowHeights
.IsEmpty() )
7985 // need to really create the array
7989 int h
= wxMax( 0, height
);
7990 int diff
= h
- m_rowHeights
[row
];
7992 m_rowHeights
[row
] = h
;
7994 for ( i
= row
; i
< m_numRows
; i
++ )
7996 m_rowBottoms
[i
] += diff
;
8001 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8003 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8005 if ( resizeExistingCols
)
8013 void wxGrid::SetColSize( int col
, int width
)
8015 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8017 // should we check that it's bigger than GetColMinimalWidth(col) here?
8019 if ( m_colWidths
.IsEmpty() )
8021 // need to really create the array
8025 int w
= wxMax( 0, width
);
8026 int diff
= w
- m_colWidths
[col
];
8027 m_colWidths
[col
] = w
;
8030 for ( i
= col
; i
< m_numCols
; i
++ )
8032 m_colRights
[i
] += diff
;
8038 void wxGrid::SetColMinimalWidth( int col
, int width
)
8040 m_colMinWidths
.Put(col
, width
);
8043 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8045 m_rowMinHeights
.Put(row
, width
);
8048 int wxGrid::GetColMinimalWidth(int col
) const
8050 long value
= m_colMinWidths
.Get(col
);
8051 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8054 int wxGrid::GetRowMinimalHeight(int row
) const
8056 long value
= m_rowMinHeights
.Get(row
);
8057 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8060 // ----------------------------------------------------------------------------
8062 // ----------------------------------------------------------------------------
8064 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8066 wxClientDC
dc(m_gridWin
);
8068 // init both of them to avoid compiler warnings, even if weo nly need one
8076 wxCoord extent
, extentMax
= 0;
8077 int max
= column
? m_numRows
: m_numCols
;
8078 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8085 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8086 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8089 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8090 extent
= column
? size
.x
: size
.y
;
8091 if ( extent
> extentMax
)
8102 // now also compare with the column label extent
8104 dc
.SetFont( GetLabelFont() );
8107 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8109 dc
.GetTextExtent( GetRowLabelValue(col
), &w
, &h
);
8111 extent
= column
? w
: h
;
8112 if ( extent
> extentMax
)
8119 // empty column - give default extent (notice that if extentMax is less
8120 // than default extent but != 0, it's ok)
8121 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8127 // leave some space around text
8137 SetColSize(col
, extentMax
);
8139 SetRowSize(row
, extentMax
);
8144 SetColMinimalWidth(col
, extentMax
);
8146 SetRowMinimalHeight(row
, extentMax
);
8150 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8152 int width
= m_rowLabelWidth
;
8154 for ( int col
= 0; col
< m_numCols
; col
++ )
8158 AutoSizeColumn(col
, setAsMin
);
8161 width
+= GetColWidth(col
);
8167 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8169 int height
= m_colLabelHeight
;
8171 for ( int row
= 0; row
< m_numRows
; row
++ )
8175 AutoSizeRow(row
, setAsMin
);
8178 height
+= GetRowHeight(row
);
8184 void wxGrid::AutoSize()
8187 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8190 wxSize
wxGrid::DoGetBestSize() const
8192 // don't set sizes, only calculate them
8193 wxGrid
*self
= (wxGrid
*)this; // const_cast
8195 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8196 self
->SetOrCalcRowSizes(TRUE
));
8205 wxPen
& wxGrid::GetDividerPen() const
8210 // ----------------------------------------------------------------------------
8211 // cell value accessor functions
8212 // ----------------------------------------------------------------------------
8214 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8218 m_table
->SetValue( row
, col
, s
);
8219 if ( !GetBatchCount() )
8221 wxClientDC
dc( m_gridWin
);
8223 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8226 if ( m_currentCellCoords
.GetRow() == row
&&
8227 m_currentCellCoords
.GetCol() == col
&&
8228 IsCellEditControlShown())
8229 // Note: If we are using IsCellEditControlEnabled,
8230 // this interacts badly with calling SetCellValue from
8231 // an EVT_GRID_CELL_CHANGE handler.
8233 HideCellEditControl();
8234 ShowCellEditControl(); // will reread data from table
8241 // ------ Block, row and col selection
8244 void wxGrid::SelectRow( int row
, bool addToSelected
)
8246 if ( IsSelection() && !addToSelected
)
8249 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8253 void wxGrid::SelectCol( int col
, bool addToSelected
)
8255 if ( IsSelection() && !addToSelected
)
8258 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8262 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8263 bool addToSelected
)
8265 if ( IsSelection() && !addToSelected
)
8268 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8269 FALSE
, addToSelected
);
8273 void wxGrid::SelectAll()
8275 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8278 bool wxGrid::IsSelection()
8280 return ( m_selection
->IsSelection() ||
8281 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8282 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8285 bool wxGrid::IsInSelection( int row
, int col
)
8287 return ( m_selection
->IsInSelection( row
, col
) ||
8288 ( row
>= m_selectingTopLeft
.GetRow() &&
8289 col
>= m_selectingTopLeft
.GetCol() &&
8290 row
<= m_selectingBottomRight
.GetRow() &&
8291 col
<= m_selectingBottomRight
.GetCol() ) );
8294 void wxGrid::ClearSelection()
8296 m_selectingTopLeft
= wxGridNoCellCoords
;
8297 m_selectingBottomRight
= wxGridNoCellCoords
;
8298 m_selection
->ClearSelection();
8302 // This function returns the rectangle that encloses the given block
8303 // in device coords clipped to the client size of the grid window.
8305 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8306 const wxGridCellCoords
&bottomRight
)
8308 wxRect
rect( wxGridNoCellRect
);
8311 cellRect
= CellToRect( topLeft
);
8312 if ( cellRect
!= wxGridNoCellRect
)
8318 rect
= wxRect( 0, 0, 0, 0 );
8321 cellRect
= CellToRect( bottomRight
);
8322 if ( cellRect
!= wxGridNoCellRect
)
8328 return wxGridNoCellRect
;
8331 // convert to scrolled coords
8333 int left
, top
, right
, bottom
;
8334 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8335 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8338 m_gridWin
->GetClientSize( &cw
, &ch
);
8340 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8341 return wxRect( 0, 0, 0, 0);
8343 rect
.SetLeft( wxMax(0, left
) );
8344 rect
.SetTop( wxMax(0, top
) );
8345 rect
.SetRight( wxMin(cw
, right
) );
8346 rect
.SetBottom( wxMin(ch
, bottom
) );
8354 // ------ Grid event classes
8357 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
8359 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8360 int row
, int col
, int x
, int y
, bool sel
,
8361 bool control
, bool shift
, bool alt
, bool meta
)
8362 : wxNotifyEvent( type
, id
)
8369 m_control
= control
;
8374 SetEventObject(obj
);
8378 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
8380 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8381 int rowOrCol
, int x
, int y
,
8382 bool control
, bool shift
, bool alt
, bool meta
)
8383 : wxNotifyEvent( type
, id
)
8385 m_rowOrCol
= rowOrCol
;
8388 m_control
= control
;
8393 SetEventObject(obj
);
8397 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
8399 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8400 const wxGridCellCoords
& topLeft
,
8401 const wxGridCellCoords
& bottomRight
,
8402 bool sel
, bool control
,
8403 bool shift
, bool alt
, bool meta
)
8404 : wxNotifyEvent( type
, id
)
8406 m_topLeft
= topLeft
;
8407 m_bottomRight
= bottomRight
;
8409 m_control
= control
;
8414 SetEventObject(obj
);
8418 #endif // ifndef wxUSE_NEW_GRID