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 precompilation, 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_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_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
);
117 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
118 DECLARE_EVENT_TABLE()
122 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
125 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
126 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
127 const wxPoint
&pos
, const wxSize
&size
);
132 void OnPaint( wxPaintEvent
&event
);
133 void OnMouseEvent( wxMouseEvent
& event
);
134 void OnKeyDown( wxKeyEvent
& event
);
136 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
137 DECLARE_EVENT_TABLE()
141 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
144 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
145 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
146 const wxPoint
&pos
, const wxSize
&size
);
151 void OnMouseEvent( wxMouseEvent
& event
);
152 void OnKeyDown( wxKeyEvent
& event
);
153 void OnPaint( wxPaintEvent
& event
);
155 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
156 DECLARE_EVENT_TABLE()
159 class WXDLLEXPORT wxGridWindow
: public wxPanel
164 m_owner
= (wxGrid
*)NULL
;
165 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
166 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
169 wxGridWindow( wxGrid
*parent
,
170 wxGridRowLabelWindow
*rowLblWin
,
171 wxGridColLabelWindow
*colLblWin
,
172 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
175 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
179 wxGridRowLabelWindow
*m_rowLabelWin
;
180 wxGridColLabelWindow
*m_colLabelWin
;
182 void OnPaint( wxPaintEvent
&event
);
183 void OnMouseEvent( wxMouseEvent
& event
);
184 void OnKeyDown( wxKeyEvent
& );
185 void OnEraseBackground( wxEraseEvent
& );
188 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
189 DECLARE_EVENT_TABLE()
194 class wxGridCellEditorEvtHandler
: public wxEvtHandler
197 wxGridCellEditorEvtHandler()
198 : m_grid(0), m_editor(0)
200 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
201 : m_grid(grid
), m_editor(editor
)
204 void OnKeyDown(wxKeyEvent
& event
);
205 void OnChar(wxKeyEvent
& event
);
209 wxGridCellEditor
* m_editor
;
210 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
211 DECLARE_EVENT_TABLE()
215 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
216 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
217 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
218 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
223 // ----------------------------------------------------------------------------
224 // the internal data representation used by wxGridCellAttrProvider
225 // ----------------------------------------------------------------------------
227 // this class stores attributes set for cells
228 class WXDLLEXPORT wxGridCellAttrData
231 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
232 wxGridCellAttr
*GetAttr(int row
, int col
) const;
233 void UpdateAttrRows( size_t pos
, int numRows
);
234 void UpdateAttrCols( size_t pos
, int numCols
);
237 // searches for the attr for given cell, returns wxNOT_FOUND if not found
238 int FindIndex(int row
, int col
) const;
240 wxGridCellWithAttrArray m_attrs
;
243 // this class stores attributes set for rows or columns
244 class WXDLLEXPORT wxGridRowOrColAttrData
247 // empty ctor to suppress warnings
248 wxGridRowOrColAttrData() { }
249 ~wxGridRowOrColAttrData();
251 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
252 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
253 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
256 wxArrayInt m_rowsOrCols
;
257 wxArrayAttrs m_attrs
;
260 // NB: this is just a wrapper around 3 objects: one which stores cell
261 // attributes, and 2 others for row/col ones
262 class WXDLLEXPORT wxGridCellAttrProviderData
265 wxGridCellAttrData m_cellAttrs
;
266 wxGridRowOrColAttrData m_rowAttrs
,
271 // ----------------------------------------------------------------------------
272 // data structures used for the data type registry
273 // ----------------------------------------------------------------------------
275 struct wxGridDataTypeInfo
277 wxGridDataTypeInfo(const wxString
& typeName
,
278 wxGridCellRenderer
* renderer
,
279 wxGridCellEditor
* editor
)
280 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
283 ~wxGridDataTypeInfo()
285 wxSafeDecRef(m_renderer
);
286 wxSafeDecRef(m_editor
);
290 wxGridCellRenderer
* m_renderer
;
291 wxGridCellEditor
* m_editor
;
295 WX_DEFINE_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
298 class WXDLLEXPORT wxGridTypeRegistry
301 wxGridTypeRegistry() {}
302 ~wxGridTypeRegistry();
304 void RegisterDataType(const wxString
& typeName
,
305 wxGridCellRenderer
* renderer
,
306 wxGridCellEditor
* editor
);
308 // find one of already registered data types
309 int FindRegisteredDataType(const wxString
& typeName
);
311 // try to FindRegisteredDataType(), if this fails and typeName is one of
312 // standard typenames, register it and return its index
313 int FindDataType(const wxString
& typeName
);
315 // try to FindDataType(), if it fails see if it is not one of already
316 // registered data types with some params in which case clone the
317 // registered data type and set params for it
318 int FindOrCloneDataType(const wxString
& typeName
);
320 wxGridCellRenderer
* GetRenderer(int index
);
321 wxGridCellEditor
* GetEditor(int index
);
324 wxGridDataTypeInfoArray m_typeinfo
;
327 // ----------------------------------------------------------------------------
328 // conditional compilation
329 // ----------------------------------------------------------------------------
331 #ifndef WXGRID_DRAW_LINES
332 #define WXGRID_DRAW_LINES 1
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
339 //#define DEBUG_ATTR_CACHE
340 #ifdef DEBUG_ATTR_CACHE
341 static size_t gs_nAttrCacheHits
= 0;
342 static size_t gs_nAttrCacheMisses
= 0;
343 #endif // DEBUG_ATTR_CACHE
345 // ----------------------------------------------------------------------------
347 // ----------------------------------------------------------------------------
349 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
350 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
353 // TODO: fixed so far - make configurable later (and also different for x/y)
354 static const size_t GRID_SCROLL_LINE
= 10;
356 // the size of hash tables used a bit everywhere (the max number of elements
357 // in these hash tables is the number of rows/columns)
358 static const int GRID_HASH_SIZE
= 100;
360 // ============================================================================
362 // ============================================================================
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 wxGridCellEditor::wxGridCellEditor()
374 wxGridCellEditor::~wxGridCellEditor()
379 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
380 wxWindowID
WXUNUSED(id
),
381 wxEvtHandler
* evtHandler
)
384 m_control
->PushEventHandler(evtHandler
);
387 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
388 wxGridCellAttr
*attr
)
390 // erase the background because we might not fill the cell
391 wxClientDC
dc(m_control
->GetParent());
392 dc
.SetPen(*wxTRANSPARENT_PEN
);
393 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
394 dc
.DrawRectangle(rectCell
);
396 // redraw the control we just painted over
397 m_control
->Refresh();
400 void wxGridCellEditor::Destroy()
404 m_control
->PopEventHandler(TRUE
/* delete it*/);
406 m_control
->Destroy();
411 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
413 wxASSERT_MSG(m_control
,
414 wxT("The wxGridCellEditor must be Created first!"));
415 m_control
->Show(show
);
419 // set the colours/fonts if we have any
422 m_colFgOld
= m_control
->GetForegroundColour();
423 m_control
->SetForegroundColour(attr
->GetTextColour());
425 m_colBgOld
= m_control
->GetBackgroundColour();
426 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
428 m_fontOld
= m_control
->GetFont();
429 m_control
->SetFont(attr
->GetFont());
431 // can't do anything more in the base class version, the other
432 // attributes may only be used by the derived classes
437 // restore the standard colours fonts
438 if ( m_colFgOld
.Ok() )
440 m_control
->SetForegroundColour(m_colFgOld
);
441 m_colFgOld
= wxNullColour
;
444 if ( m_colBgOld
.Ok() )
446 m_control
->SetBackgroundColour(m_colBgOld
);
447 m_colBgOld
= wxNullColour
;
450 if ( m_fontOld
.Ok() )
452 m_control
->SetFont(m_fontOld
);
453 m_fontOld
= wxNullFont
;
458 void wxGridCellEditor::SetSize(const wxRect
& rect
)
460 wxASSERT_MSG(m_control
,
461 wxT("The wxGridCellEditor must be Created first!"));
462 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
465 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
471 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
476 void wxGridCellEditor::StartingClick()
480 // ----------------------------------------------------------------------------
481 // wxGridCellTextEditor
482 // ----------------------------------------------------------------------------
484 wxGridCellTextEditor::wxGridCellTextEditor()
489 void wxGridCellTextEditor::Create(wxWindow
* parent
,
491 wxEvtHandler
* evtHandler
)
493 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
494 wxDefaultPosition
, wxDefaultSize
495 #if defined(__WXMSW__)
496 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
500 // TODO: use m_maxChars
502 wxGridCellEditor::Create(parent
, id
, evtHandler
);
505 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
506 wxGridCellAttr
* WXUNUSED(attr
))
508 // as we fill the entire client area, don't do anything here to minimize
512 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
514 wxRect
rect(rectOrig
);
516 // Make the edit control large enough to allow for internal
519 // TODO: remove this if the text ctrl sizing is improved esp. for
522 #if defined(__WXGTK__)
531 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
532 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
533 #if defined(__WXMOTIF__)
537 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
538 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
539 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
540 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
543 wxGridCellEditor::SetSize(rect
);
546 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
548 wxASSERT_MSG(m_control
,
549 wxT("The wxGridCellEditor must be Created first!"));
551 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
553 DoBeginEdit(m_startValue
);
556 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
558 Text()->SetValue(startValue
);
559 Text()->SetInsertionPointEnd();
563 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
566 wxASSERT_MSG(m_control
,
567 wxT("The wxGridCellEditor must be Created first!"));
569 bool changed
= FALSE
;
570 wxString value
= Text()->GetValue();
571 if (value
!= m_startValue
)
575 grid
->GetTable()->SetValue(row
, col
, value
);
577 m_startValue
= wxEmptyString
;
578 Text()->SetValue(m_startValue
);
584 void wxGridCellTextEditor::Reset()
586 wxASSERT_MSG(m_control
,
587 wxT("The wxGridCellEditor must be Created first!"));
589 DoReset(m_startValue
);
592 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
594 Text()->SetValue(startValue
);
595 Text()->SetInsertionPointEnd();
598 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
600 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
602 // insert the key in the control
603 int keycode
= (int)event
.KeyCode();
604 if ( isprint(keycode
) )
606 // FIXME this is not going to work for non letters...
607 if ( !event
.ShiftDown() )
609 keycode
= tolower(keycode
);
612 Text()->AppendText((wxChar
)keycode
);
622 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
623 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
625 #if defined(__WXMOTIF__) || defined(__WXGTK__)
626 // wxMotif needs a little extra help...
627 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
628 wxString
s( Text()->GetValue() );
629 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
631 Text()->SetInsertionPoint( pos
);
633 // the other ports can handle a Return key press
639 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
649 if ( !params
.ToLong(&tmp
) )
651 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string "
652 "'%s' ignored"), params
.c_str());
656 m_maxChars
= (size_t)tmp
;
661 // ----------------------------------------------------------------------------
662 // wxGridCellNumberEditor
663 // ----------------------------------------------------------------------------
665 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
671 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
673 wxEvtHandler
* evtHandler
)
677 // create a spin ctrl
678 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
679 wxDefaultPosition
, wxDefaultSize
,
683 wxGridCellEditor::Create(parent
, id
, evtHandler
);
687 // just a text control
688 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
691 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
692 #endif // wxUSE_VALIDATORS
696 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
698 // first get the value
699 wxGridTableBase
*table
= grid
->GetTable();
700 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
702 m_valueOld
= table
->GetValueAsLong(row
, col
);
706 wxString sValue
= table
->GetValue(row
, col
);
707 if (! sValue
.ToLong(&m_valueOld
))
709 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
716 Spin()->SetValue((int)m_valueOld
);
720 DoBeginEdit(GetString());
724 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
732 value
= Spin()->GetValue();
733 changed
= value
!= m_valueOld
;
737 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
742 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
743 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
745 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%ld", value
));
751 void wxGridCellNumberEditor::Reset()
755 Spin()->SetValue((int)m_valueOld
);
759 DoReset(GetString());
763 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
767 int keycode
= (int) event
.KeyCode();
768 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
770 wxGridCellTextEditor::StartingKey(event
);
780 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
791 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
795 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
799 // skip the error message below
804 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string "
805 "'%s' ignored"), params
.c_str());
809 // ----------------------------------------------------------------------------
810 // wxGridCellFloatEditor
811 // ----------------------------------------------------------------------------
813 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
815 wxEvtHandler
* evtHandler
)
817 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
820 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
821 #endif // wxUSE_VALIDATORS
824 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
826 // first get the value
827 wxGridTableBase
*table
= grid
->GetTable();
828 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
830 m_valueOld
= table
->GetValueAsDouble(row
, col
);
834 wxString sValue
= table
->GetValue(row
, col
);
835 if (! sValue
.ToDouble(&m_valueOld
))
837 wxFAIL_MSG( _T("this cell doesn't have float value") );
842 DoBeginEdit(GetString());
845 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
849 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
851 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
852 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
854 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%f", value
));
864 void wxGridCellFloatEditor::Reset()
866 DoReset(GetString());
869 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
871 int keycode
= (int)event
.KeyCode();
872 if ( isdigit(keycode
) ||
873 keycode
== '+' || keycode
== '-' || keycode
== '.' )
875 wxGridCellTextEditor::StartingKey(event
);
884 // ----------------------------------------------------------------------------
885 // wxGridCellBoolEditor
886 // ----------------------------------------------------------------------------
888 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
890 wxEvtHandler
* evtHandler
)
892 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
893 wxDefaultPosition
, wxDefaultSize
,
896 wxGridCellEditor::Create(parent
, id
, evtHandler
);
899 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
902 wxSize size
= m_control
->GetSize();
903 wxCoord minSize
= wxMin(r
.width
, r
.height
);
905 // check if the checkbox is not too big/small for this cell
906 wxSize sizeBest
= m_control
->GetBestSize();
907 if ( !(size
== sizeBest
) )
909 // reset to default size if it had been made smaller
915 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
917 // leave 1 pixel margin
918 size
.x
= size
.y
= minSize
- 2;
925 m_control
->SetSize(size
);
928 // position it in the centre of the rectangle (TODO: support alignment?)
930 #if defined(__WXGTK__) || defined (__WXMOTIF__)
931 // the checkbox without label still has some space to the right in wxGTK,
932 // so shift it to the right
934 #elif defined(__WXMSW__)
935 // here too, but in other way
940 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
943 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
945 m_control
->Show(show
);
949 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
950 CBox()->SetBackgroundColour(colBg
);
954 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
956 wxASSERT_MSG(m_control
,
957 wxT("The wxGridCellEditor must be Created first!"));
959 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
960 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
962 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
963 CBox()->SetValue(m_startValue
);
967 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
970 wxASSERT_MSG(m_control
,
971 wxT("The wxGridCellEditor must be Created first!"));
973 bool changed
= FALSE
;
974 bool value
= CBox()->GetValue();
975 if ( value
!= m_startValue
)
980 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
981 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
983 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
989 void wxGridCellBoolEditor::Reset()
991 wxASSERT_MSG(m_control
,
992 wxT("The wxGridCellEditor must be Created first!"));
994 CBox()->SetValue(m_startValue
);
997 void wxGridCellBoolEditor::StartingClick()
999 CBox()->SetValue(!CBox()->GetValue());
1002 // ----------------------------------------------------------------------------
1003 // wxGridCellChoiceEditor
1004 // ----------------------------------------------------------------------------
1006 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1007 const wxChar
* choices
[],
1009 : m_allowOthers(allowOthers
)
1013 m_choices
.Alloc(count
);
1014 for ( size_t n
= 0; n
< count
; n
++ )
1016 m_choices
.Add(choices
[n
]);
1021 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1023 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1024 editor
->m_allowOthers
= m_allowOthers
;
1025 editor
->m_choices
= m_choices
;
1030 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1032 wxEvtHandler
* evtHandler
)
1034 size_t count
= m_choices
.GetCount();
1035 wxString
*choices
= new wxString
[count
];
1036 for ( size_t n
= 0; n
< count
; n
++ )
1038 choices
[n
] = m_choices
[n
];
1041 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1042 wxDefaultPosition
, wxDefaultSize
,
1044 m_allowOthers
? 0 : wxCB_READONLY
);
1048 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1051 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1052 wxGridCellAttr
* attr
)
1054 // as we fill the entire client area, don't do anything here to minimize
1057 // TODO: It doesn't actually fill the client area since the height of a
1058 // combo always defaults to the standard... Until someone has time to
1059 // figure out the right rectangle to paint, just do it the normal way...
1060 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1063 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1065 wxASSERT_MSG(m_control
,
1066 wxT("The wxGridCellEditor must be Created first!"));
1068 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1070 Combo()->SetValue(m_startValue
);
1071 size_t count
= m_choices
.GetCount();
1072 for (size_t i
=0; i
<count
; i
++)
1074 if (m_startValue
== m_choices
[i
])
1076 Combo()->SetSelection(i
);
1080 Combo()->SetInsertionPointEnd();
1081 Combo()->SetFocus();
1084 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1087 wxString value
= Combo()->GetValue();
1088 bool changed
= value
!= m_startValue
;
1091 grid
->GetTable()->SetValue(row
, col
, value
);
1093 m_startValue
= wxEmptyString
;
1094 Combo()->SetValue(m_startValue
);
1099 void wxGridCellChoiceEditor::Reset()
1101 Combo()->SetValue(m_startValue
);
1102 Combo()->SetInsertionPointEnd();
1105 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1115 wxStringTokenizer
tk(params
, _T(','));
1116 while ( tk
.HasMoreTokens() )
1118 m_choices
.Add(tk
.GetNextToken());
1122 // ----------------------------------------------------------------------------
1123 // wxGridCellEditorEvtHandler
1124 // ----------------------------------------------------------------------------
1126 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1128 switch ( event
.KeyCode() )
1132 m_grid
->DisableCellEditControl();
1136 event
.Skip( m_grid
->ProcessEvent( event
) );
1140 if (!m_grid
->ProcessEvent(event
))
1141 m_editor
->HandleReturn(event
);
1150 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1152 switch ( event
.KeyCode() )
1164 // ----------------------------------------------------------------------------
1165 // wxGridCellWorker is an (almost) empty common base class for
1166 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1167 // ----------------------------------------------------------------------------
1169 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1174 wxGridCellWorker::~wxGridCellWorker()
1178 // ============================================================================
1180 // ============================================================================
1182 // ----------------------------------------------------------------------------
1183 // wxGridCellRenderer
1184 // ----------------------------------------------------------------------------
1186 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1187 wxGridCellAttr
& attr
,
1190 int WXUNUSED(row
), int WXUNUSED(col
),
1193 dc
.SetBackgroundMode( wxSOLID
);
1197 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1201 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1204 dc
.SetPen( *wxTRANSPARENT_PEN
);
1205 dc
.DrawRectangle(rect
);
1208 // ----------------------------------------------------------------------------
1209 // wxGridCellStringRenderer
1210 // ----------------------------------------------------------------------------
1212 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1213 wxGridCellAttr
& attr
,
1217 dc
.SetBackgroundMode( wxTRANSPARENT
);
1219 // TODO some special colours for attr.IsReadOnly() case?
1223 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1224 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1228 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1229 dc
.SetTextForeground( attr
.GetTextColour() );
1232 dc
.SetFont( attr
.GetFont() );
1235 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1237 const wxString
& text
)
1240 dc
.SetFont(attr
.GetFont());
1241 dc
.GetTextExtent(text
, &x
, &y
);
1243 return wxSize(x
, y
);
1246 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1247 wxGridCellAttr
& attr
,
1251 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1254 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1255 wxGridCellAttr
& attr
,
1257 const wxRect
& rectCell
,
1261 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1263 // now we only have to draw the text
1264 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1267 attr
.GetAlignment(&hAlign
, &vAlign
);
1269 wxRect rect
= rectCell
;
1272 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1273 rect
, hAlign
, vAlign
);
1276 // ----------------------------------------------------------------------------
1277 // wxGridCellNumberRenderer
1278 // ----------------------------------------------------------------------------
1280 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1282 wxGridTableBase
*table
= grid
.GetTable();
1284 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1286 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1290 text
= table
->GetValue(row
, col
);
1296 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1297 wxGridCellAttr
& attr
,
1299 const wxRect
& rectCell
,
1303 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1305 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1307 // draw the text right aligned by default
1309 attr
.GetAlignment(&hAlign
, &vAlign
);
1312 wxRect rect
= rectCell
;
1315 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1318 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1319 wxGridCellAttr
& attr
,
1323 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1326 // ----------------------------------------------------------------------------
1327 // wxGridCellFloatRenderer
1328 // ----------------------------------------------------------------------------
1330 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1333 SetPrecision(precision
);
1336 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1338 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1339 renderer
->m_width
= m_width
;
1340 renderer
->m_precision
= m_precision
;
1341 renderer
->m_format
= m_format
;
1346 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1348 wxGridTableBase
*table
= grid
.GetTable();
1353 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1355 val
= table
->GetValueAsDouble(row
, col
);
1360 text
= table
->GetValue(row
, col
);
1361 hasDouble
= text
.ToDouble(&val
);
1368 if ( m_width
== -1 )
1370 // default width/precision
1371 m_format
= _T("%f");
1373 else if ( m_precision
== -1 )
1375 // default precision
1376 m_format
.Printf(_T("%%%d.f"), m_width
);
1380 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1384 text
.Printf(m_format
, val
);
1386 //else: text already contains the string
1391 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1392 wxGridCellAttr
& attr
,
1394 const wxRect
& rectCell
,
1398 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1400 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1402 // draw the text right aligned by default
1404 attr
.GetAlignment(&hAlign
, &vAlign
);
1407 wxRect rect
= rectCell
;
1410 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1413 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1414 wxGridCellAttr
& attr
,
1418 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1421 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1427 // reset to defaults
1433 wxString tmp
= params
.BeforeFirst(_T(','));
1437 if ( !tmp
.ToLong(&width
) )
1443 SetWidth((int)width
);
1445 tmp
= params
.AfterFirst(_T(','));
1449 if ( !tmp
.ToLong(&precision
) )
1455 SetPrecision((int)precision
);
1463 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string "
1464 "'%s ignored"), params
.c_str());
1469 // ----------------------------------------------------------------------------
1470 // wxGridCellBoolRenderer
1471 // ----------------------------------------------------------------------------
1473 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1475 // FIXME these checkbox size calculations are really ugly...
1477 // between checkmark and box
1478 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1480 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1481 wxGridCellAttr
& WXUNUSED(attr
),
1486 // compute it only once (no locks for MT safeness in GUI thread...)
1487 if ( !ms_sizeCheckMark
.x
)
1489 // get checkbox size
1490 wxCoord checkSize
= 0;
1491 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1492 wxSize size
= checkbox
->GetBestSize();
1493 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1495 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1496 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1497 checkSize
-= size
.y
/ 2;
1502 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1505 return ms_sizeCheckMark
;
1508 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1509 wxGridCellAttr
& attr
,
1515 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1517 // draw a check mark in the centre (ignoring alignment - TODO)
1518 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1520 // don't draw outside the cell
1521 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1522 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1524 // and even leave (at least) 1 pixel margin
1525 size
.x
= size
.y
= minSize
- 2;
1528 // draw a border around checkmark
1530 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1531 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1532 rectBorder
.width
= size
.x
;
1533 rectBorder
.height
= size
.y
;
1536 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1537 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1539 value
= !!grid
.GetTable()->GetValue(row
, col
);
1543 wxRect rectMark
= rectBorder
;
1545 // MSW DrawCheckMark() is weird (and should probably be changed...)
1546 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1550 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1553 dc
.SetTextForeground(attr
.GetTextColour());
1554 dc
.DrawCheckMark(rectMark
);
1557 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1558 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1559 dc
.DrawRectangle(rectBorder
);
1562 // ----------------------------------------------------------------------------
1564 // ----------------------------------------------------------------------------
1566 wxGridCellAttr
*wxGridCellAttr::Clone() const
1568 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1569 if ( HasTextColour() )
1570 attr
->SetTextColour(GetTextColour());
1571 if ( HasBackgroundColour() )
1572 attr
->SetBackgroundColour(GetBackgroundColour());
1574 attr
->SetFont(GetFont());
1575 if ( HasAlignment() )
1576 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1580 attr
->SetRenderer(m_renderer
);
1581 m_renderer
->IncRef();
1585 attr
->SetEditor(m_editor
);
1590 attr
->SetReadOnly();
1592 attr
->SetDefAttr(m_defGridAttr
);
1597 const wxColour
& wxGridCellAttr::GetTextColour() const
1599 if (HasTextColour())
1603 else if (m_defGridAttr
!= this)
1605 return m_defGridAttr
->GetTextColour();
1609 wxFAIL_MSG(wxT("Missing default cell attribute"));
1610 return wxNullColour
;
1615 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1617 if (HasBackgroundColour())
1619 else if (m_defGridAttr
!= this)
1620 return m_defGridAttr
->GetBackgroundColour();
1623 wxFAIL_MSG(wxT("Missing default cell attribute"));
1624 return wxNullColour
;
1629 const wxFont
& wxGridCellAttr::GetFont() const
1633 else if (m_defGridAttr
!= this)
1634 return m_defGridAttr
->GetFont();
1637 wxFAIL_MSG(wxT("Missing default cell attribute"));
1643 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1647 if ( hAlign
) *hAlign
= m_hAlign
;
1648 if ( vAlign
) *vAlign
= m_vAlign
;
1650 else if (m_defGridAttr
!= this)
1651 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1654 wxFAIL_MSG(wxT("Missing default cell attribute"));
1659 // GetRenderer and GetEditor use a slightly different decision path about
1660 // which attribute to use. If a non-default attr object has one then it is
1661 // used, otherwise the default editor or renderer is fetched from the grid and
1662 // used. It should be the default for the data type of the cell. If it is
1663 // NULL (because the table has a type that the grid does not have in its
1664 // registry,) then the grid's default editor or renderer is used.
1666 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1668 wxGridCellRenderer
* renderer
= NULL
;
1670 if ( m_defGridAttr
!= this || grid
== NULL
)
1672 renderer
= m_renderer
; // use local attribute
1677 if ( !renderer
&& grid
) // get renderer for the data type
1679 // GetDefaultRendererForCell() will do IncRef() for us
1680 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1685 // if we still don't have one then use the grid default
1686 // (no need for IncRef() here neither)
1687 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1692 wxFAIL_MSG(wxT("Missing default cell attribute"));
1698 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1700 wxGridCellEditor
* editor
= NULL
;
1702 if ( m_defGridAttr
!= this || grid
== NULL
)
1704 editor
= m_editor
; // use local attribute
1709 if ( !editor
&& grid
) // get renderer for the data type
1710 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1713 // if we still don't have one then use the grid default
1714 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1718 wxFAIL_MSG(wxT("Missing default cell attribute"));
1724 // ----------------------------------------------------------------------------
1725 // wxGridCellAttrData
1726 // ----------------------------------------------------------------------------
1728 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1730 int n
= FindIndex(row
, col
);
1731 if ( n
== wxNOT_FOUND
)
1733 // add the attribute
1734 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1740 // change the attribute
1741 m_attrs
[(size_t)n
].attr
= attr
;
1745 // remove this attribute
1746 m_attrs
.RemoveAt((size_t)n
);
1751 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1753 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1755 int n
= FindIndex(row
, col
);
1756 if ( n
!= wxNOT_FOUND
)
1758 attr
= m_attrs
[(size_t)n
].attr
;
1765 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1767 size_t count
= m_attrs
.GetCount();
1768 for ( size_t n
= 0; n
< count
; n
++ )
1770 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1771 wxCoord row
= coords
.GetRow();
1772 if ((size_t)row
>= pos
)
1776 // If rows inserted, include row counter where necessary
1777 coords
.SetRow(row
+ numRows
);
1779 else if (numRows
< 0)
1781 // If rows deleted ...
1782 if ((size_t)row
>= pos
- numRows
)
1784 // ...either decrement row counter (if row still exists)...
1785 coords
.SetRow(row
+ numRows
);
1789 // ...or remove the attribute
1790 m_attrs
.RemoveAt((size_t)n
);
1798 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1800 size_t count
= m_attrs
.GetCount();
1801 for ( size_t n
= 0; n
< count
; n
++ )
1803 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1804 wxCoord col
= coords
.GetCol();
1805 if ( (size_t)col
>= pos
)
1809 // If rows inserted, include row counter where necessary
1810 coords
.SetCol(col
+ numCols
);
1812 else if (numCols
< 0)
1814 // If rows deleted ...
1815 if ((size_t)col
>= pos
- numCols
)
1817 // ...either decrement row counter (if row still exists)...
1818 coords
.SetCol(col
+ numCols
);
1822 // ...or remove the attribute
1823 m_attrs
.RemoveAt((size_t)n
);
1831 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1833 size_t count
= m_attrs
.GetCount();
1834 for ( size_t n
= 0; n
< count
; n
++ )
1836 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1837 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1846 // ----------------------------------------------------------------------------
1847 // wxGridRowOrColAttrData
1848 // ----------------------------------------------------------------------------
1850 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1852 size_t count
= m_attrs
.Count();
1853 for ( size_t n
= 0; n
< count
; n
++ )
1855 m_attrs
[n
]->DecRef();
1859 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1861 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1863 int n
= m_rowsOrCols
.Index(rowOrCol
);
1864 if ( n
!= wxNOT_FOUND
)
1866 attr
= m_attrs
[(size_t)n
];
1873 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1875 int i
= m_rowsOrCols
.Index(rowOrCol
);
1876 if ( i
== wxNOT_FOUND
)
1878 // add the attribute
1879 m_rowsOrCols
.Add(rowOrCol
);
1884 size_t n
= (size_t)i
;
1887 // change the attribute
1888 m_attrs
[n
]->DecRef();
1893 // remove this attribute
1894 m_attrs
[n
]->DecRef();
1895 m_rowsOrCols
.RemoveAt(n
);
1896 m_attrs
.RemoveAt(n
);
1901 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1903 size_t count
= m_attrs
.GetCount();
1904 for ( size_t n
= 0; n
< count
; n
++ )
1906 int & rowOrCol
= m_rowsOrCols
[n
];
1907 if ( (size_t)rowOrCol
>= pos
)
1909 if ( numRowsOrCols
> 0 )
1911 // If rows inserted, include row counter where necessary
1912 rowOrCol
+= numRowsOrCols
;
1914 else if ( numRowsOrCols
< 0)
1916 // If rows deleted, either decrement row counter (if row still exists)
1917 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1918 rowOrCol
+= numRowsOrCols
;
1921 m_rowsOrCols
.RemoveAt((size_t)n
);
1922 m_attrs
.RemoveAt((size_t)n
);
1930 // ----------------------------------------------------------------------------
1931 // wxGridCellAttrProvider
1932 // ----------------------------------------------------------------------------
1934 wxGridCellAttrProvider::wxGridCellAttrProvider()
1936 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1939 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1944 void wxGridCellAttrProvider::InitData()
1946 m_data
= new wxGridCellAttrProviderData
;
1949 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1951 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1954 // first look for the attribute of this specific cell
1955 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1959 // then look for the col attr (col attributes are more common than
1960 // the row ones, hence they have priority)
1961 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1966 // finally try the row attributes
1967 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1974 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1980 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1983 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1988 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1991 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1996 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1999 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2003 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2005 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2009 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2013 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2015 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2019 // ----------------------------------------------------------------------------
2020 // wxGridTypeRegistry
2021 // ----------------------------------------------------------------------------
2023 wxGridTypeRegistry::~wxGridTypeRegistry()
2025 size_t count
= m_typeinfo
.Count();
2026 for ( size_t i
= 0; i
< count
; i
++ )
2027 delete m_typeinfo
[i
];
2031 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2032 wxGridCellRenderer
* renderer
,
2033 wxGridCellEditor
* editor
)
2035 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2037 // is it already registered?
2038 int loc
= FindRegisteredDataType(typeName
);
2039 if ( loc
!= wxNOT_FOUND
)
2041 delete m_typeinfo
[loc
];
2042 m_typeinfo
[loc
] = info
;
2046 m_typeinfo
.Add(info
);
2050 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2052 size_t count
= m_typeinfo
.GetCount();
2053 for ( size_t i
= 0; i
< count
; i
++ )
2055 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2064 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2066 int index
= FindRegisteredDataType(typeName
);
2067 if ( index
== wxNOT_FOUND
)
2069 // check whether this is one of the standard ones, in which case
2070 // register it "on the fly"
2071 if ( typeName
== wxGRID_VALUE_STRING
)
2073 RegisterDataType(wxGRID_VALUE_STRING
,
2074 new wxGridCellStringRenderer
,
2075 new wxGridCellTextEditor
);
2077 else if ( typeName
== wxGRID_VALUE_BOOL
)
2079 RegisterDataType(wxGRID_VALUE_BOOL
,
2080 new wxGridCellBoolRenderer
,
2081 new wxGridCellBoolEditor
);
2083 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2085 RegisterDataType(wxGRID_VALUE_NUMBER
,
2086 new wxGridCellNumberRenderer
,
2087 new wxGridCellNumberEditor
);
2089 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2091 RegisterDataType(wxGRID_VALUE_FLOAT
,
2092 new wxGridCellFloatRenderer
,
2093 new wxGridCellFloatEditor
);
2095 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2097 RegisterDataType(wxGRID_VALUE_CHOICE
,
2098 new wxGridCellStringRenderer
,
2099 new wxGridCellChoiceEditor
);
2106 // we get here only if just added the entry for this type, so return
2108 index
= m_typeinfo
.GetCount() - 1;
2114 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2116 int index
= FindDataType(typeName
);
2117 if ( index
== wxNOT_FOUND
)
2119 // the first part of the typename is the "real" type, anything after ':'
2120 // are the parameters for the renderer
2121 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2122 if ( index
== wxNOT_FOUND
)
2127 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2128 wxGridCellRenderer
*rendererOld
= renderer
;
2129 renderer
= renderer
->Clone();
2130 rendererOld
->DecRef();
2132 wxGridCellEditor
*editor
= GetEditor(index
);
2133 wxGridCellEditor
*editorOld
= editor
;
2134 editor
= editor
->Clone();
2135 editorOld
->DecRef();
2137 // do it even if there are no parameters to reset them to defaults
2138 wxString params
= typeName
.AfterFirst(_T(':'));
2139 renderer
->SetParameters(params
);
2140 editor
->SetParameters(params
);
2142 // register the new typename
2143 RegisterDataType(typeName
, renderer
, editor
);
2145 // we just registered it, it's the last one
2146 index
= m_typeinfo
.GetCount() - 1;
2152 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2154 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2159 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2161 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2166 // ----------------------------------------------------------------------------
2168 // ----------------------------------------------------------------------------
2170 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2173 wxGridTableBase::wxGridTableBase()
2175 m_view
= (wxGrid
*) NULL
;
2176 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2179 wxGridTableBase::~wxGridTableBase()
2181 delete m_attrProvider
;
2184 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2186 delete m_attrProvider
;
2187 m_attrProvider
= attrProvider
;
2190 bool wxGridTableBase::CanHaveAttributes()
2192 if ( ! GetAttrProvider() )
2194 // use the default attr provider by default
2195 SetAttrProvider(new wxGridCellAttrProvider
);
2200 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
2202 if ( m_attrProvider
)
2203 return m_attrProvider
->GetAttr(row
, col
);
2205 return (wxGridCellAttr
*)NULL
;
2208 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2210 if ( m_attrProvider
)
2212 m_attrProvider
->SetAttr(attr
, row
, col
);
2216 // as we take ownership of the pointer and don't store it, we must
2222 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2224 if ( m_attrProvider
)
2226 m_attrProvider
->SetRowAttr(attr
, row
);
2230 // as we take ownership of the pointer and don't store it, we must
2236 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2238 if ( m_attrProvider
)
2240 m_attrProvider
->SetColAttr(attr
, col
);
2244 // as we take ownership of the pointer and don't store it, we must
2250 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
2252 if ( m_attrProvider
)
2254 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
2258 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
2260 if ( m_attrProvider
)
2262 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
2266 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2267 size_t WXUNUSED(numRows
) )
2269 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
2270 "but your derived table class does not override this function") );
2275 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2277 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
2278 "but your derived table class does not override this function"));
2283 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2284 size_t WXUNUSED(numRows
) )
2286 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
2287 "but your derived table class does not override this function"));
2292 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2293 size_t WXUNUSED(numCols
) )
2295 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
2296 "but your derived table class does not override this function"));
2301 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2303 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
2304 "but your derived table class does not override this function"));
2309 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2310 size_t WXUNUSED(numCols
) )
2312 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
2313 "but your derived table class does not override this function"));
2319 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2322 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2323 // how much it makes sense to us geeks.
2327 wxString
wxGridTableBase::GetColLabelValue( int col
)
2329 // default col labels are:
2330 // cols 0 to 25 : A-Z
2331 // cols 26 to 675 : AA-ZZ
2336 for ( n
= 1; ; n
++ )
2338 s
+= (_T('A') + (wxChar
)( col%26
));
2340 if ( col
< 0 ) break;
2343 // reverse the string...
2345 for ( i
= 0; i
< n
; i
++ )
2354 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2356 return wxGRID_VALUE_STRING
;
2359 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2360 const wxString
& typeName
)
2362 return typeName
== wxGRID_VALUE_STRING
;
2365 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2367 return CanGetValueAs(row
, col
, typeName
);
2370 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2375 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2380 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2385 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2386 long WXUNUSED(value
) )
2390 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2391 double WXUNUSED(value
) )
2395 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2396 bool WXUNUSED(value
) )
2401 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2402 const wxString
& WXUNUSED(typeName
) )
2407 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2408 const wxString
& WXUNUSED(typeName
),
2409 void* WXUNUSED(value
) )
2413 //////////////////////////////////////////////////////////////////////
2415 // Message class for the grid table to send requests and notifications
2419 wxGridTableMessage::wxGridTableMessage()
2421 m_table
= (wxGridTableBase
*) NULL
;
2427 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2428 int commandInt1
, int commandInt2
)
2432 m_comInt1
= commandInt1
;
2433 m_comInt2
= commandInt2
;
2438 //////////////////////////////////////////////////////////////////////
2440 // A basic grid table for string data. An object of this class will
2441 // created by wxGrid if you don't specify an alternative table class.
2444 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2446 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2448 wxGridStringTable::wxGridStringTable()
2453 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2458 m_data
.Alloc( numRows
);
2461 sa
.Alloc( numCols
);
2462 for ( col
= 0; col
< numCols
; col
++ )
2464 sa
.Add( wxEmptyString
);
2467 for ( row
= 0; row
< numRows
; row
++ )
2473 wxGridStringTable::~wxGridStringTable()
2477 int wxGridStringTable::GetNumberRows()
2479 return m_data
.GetCount();
2482 int wxGridStringTable::GetNumberCols()
2484 if ( m_data
.GetCount() > 0 )
2485 return m_data
[0].GetCount();
2490 wxString
wxGridStringTable::GetValue( int row
, int col
)
2492 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2493 _T("invalid row or column index in wxGridStringTable") );
2495 return m_data
[row
][col
];
2498 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2500 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2501 _T("invalid row or column index in wxGridStringTable") );
2503 m_data
[row
][col
] = value
;
2506 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2508 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2509 _T("invalid row or column index in wxGridStringTable") );
2511 return (m_data
[row
][col
] == wxEmptyString
);
2514 void wxGridStringTable::Clear()
2517 int numRows
, numCols
;
2519 numRows
= m_data
.GetCount();
2522 numCols
= m_data
[0].GetCount();
2524 for ( row
= 0; row
< numRows
; row
++ )
2526 for ( col
= 0; col
< numCols
; col
++ )
2528 m_data
[row
][col
] = wxEmptyString
;
2535 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2539 size_t curNumRows
= m_data
.GetCount();
2540 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2542 if ( pos
>= curNumRows
)
2544 return AppendRows( numRows
);
2548 sa
.Alloc( curNumCols
);
2549 for ( col
= 0; col
< curNumCols
; col
++ )
2551 sa
.Add( wxEmptyString
);
2554 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2556 m_data
.Insert( sa
, row
);
2558 UpdateAttrRows( pos
, numRows
);
2561 wxGridTableMessage
msg( this,
2562 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2566 GetView()->ProcessTableMessage( msg
);
2572 bool wxGridStringTable::AppendRows( size_t numRows
)
2576 size_t curNumRows
= m_data
.GetCount();
2577 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2580 if ( curNumCols
> 0 )
2582 sa
.Alloc( curNumCols
);
2583 for ( col
= 0; col
< curNumCols
; col
++ )
2585 sa
.Add( wxEmptyString
);
2589 for ( row
= 0; row
< numRows
; row
++ )
2596 wxGridTableMessage
msg( this,
2597 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2600 GetView()->ProcessTableMessage( msg
);
2606 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2610 size_t curNumRows
= m_data
.GetCount();
2612 if ( pos
>= curNumRows
)
2615 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2616 "Pos value is invalid for present table with %d rows",
2617 pos
, numRows
, curNumRows
);
2618 wxFAIL_MSG( wxT(errmsg
) );
2622 if ( numRows
> curNumRows
- pos
)
2624 numRows
= curNumRows
- pos
;
2627 if ( numRows
>= curNumRows
)
2629 m_data
.Empty(); // don't release memory just yet
2633 for ( n
= 0; n
< numRows
; n
++ )
2635 m_data
.Remove( pos
);
2638 UpdateAttrRows( pos
, -((int)numRows
) );
2641 wxGridTableMessage
msg( this,
2642 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2646 GetView()->ProcessTableMessage( msg
);
2652 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2656 size_t curNumRows
= m_data
.GetCount();
2657 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2659 if ( pos
>= curNumCols
)
2661 return AppendCols( numCols
);
2664 for ( row
= 0; row
< curNumRows
; row
++ )
2666 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2668 m_data
[row
].Insert( wxEmptyString
, col
);
2671 UpdateAttrCols( pos
, numCols
);
2674 wxGridTableMessage
msg( this,
2675 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2679 GetView()->ProcessTableMessage( msg
);
2685 bool wxGridStringTable::AppendCols( size_t numCols
)
2689 size_t curNumRows
= m_data
.GetCount();
2692 // TODO: something better than this ?
2694 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2695 "Call AppendRows() first") );
2699 for ( row
= 0; row
< curNumRows
; row
++ )
2701 for ( n
= 0; n
< numCols
; n
++ )
2703 m_data
[row
].Add( wxEmptyString
);
2709 wxGridTableMessage
msg( this,
2710 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2713 GetView()->ProcessTableMessage( msg
);
2719 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2723 size_t curNumRows
= m_data
.GetCount();
2724 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2726 if ( pos
>= curNumCols
)
2729 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2730 "Pos value is invalid for present table with %d cols",
2731 pos
, numCols
, curNumCols
);
2732 wxFAIL_MSG( wxT( errmsg
) );
2736 if ( numCols
> curNumCols
- pos
)
2738 numCols
= curNumCols
- pos
;
2741 for ( row
= 0; row
< curNumRows
; row
++ )
2743 if ( numCols
>= curNumCols
)
2745 m_data
[row
].Clear();
2749 for ( n
= 0; n
< numCols
; n
++ )
2751 m_data
[row
].Remove( pos
);
2755 UpdateAttrCols( pos
, -((int)numCols
) );
2758 wxGridTableMessage
msg( this,
2759 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2763 GetView()->ProcessTableMessage( msg
);
2769 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2771 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2773 // using default label
2775 return wxGridTableBase::GetRowLabelValue( row
);
2779 return m_rowLabels
[ row
];
2783 wxString
wxGridStringTable::GetColLabelValue( int col
)
2785 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2787 // using default label
2789 return wxGridTableBase::GetColLabelValue( col
);
2793 return m_colLabels
[ col
];
2797 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2799 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2801 int n
= m_rowLabels
.GetCount();
2803 for ( i
= n
; i
<= row
; i
++ )
2805 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2809 m_rowLabels
[row
] = value
;
2812 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2814 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2816 int n
= m_colLabels
.GetCount();
2818 for ( i
= n
; i
<= col
; i
++ )
2820 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2824 m_colLabels
[col
] = value
;
2829 //////////////////////////////////////////////////////////////////////
2830 //////////////////////////////////////////////////////////////////////
2832 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2834 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2835 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2836 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2837 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2840 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2842 const wxPoint
&pos
, const wxSize
&size
)
2843 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2848 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2852 // NO - don't do this because it will set both the x and y origin
2853 // coords to match the parent scrolled window and we just want to
2854 // set the y coord - MB
2856 // m_owner->PrepareDC( dc );
2859 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2860 dc
.SetDeviceOrigin( 0, -y
);
2862 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2863 m_owner
->DrawRowLabels( dc
);
2867 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2869 m_owner
->ProcessRowLabelMouseEvent( event
);
2873 // This seems to be required for wxMotif otherwise the mouse
2874 // cursor must be in the cell edit control to get key events
2876 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2878 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2883 //////////////////////////////////////////////////////////////////////
2885 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2887 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2888 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2889 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2890 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2893 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2895 const wxPoint
&pos
, const wxSize
&size
)
2896 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2901 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2905 // NO - don't do this because it will set both the x and y origin
2906 // coords to match the parent scrolled window and we just want to
2907 // set the x coord - MB
2909 // m_owner->PrepareDC( dc );
2912 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2913 dc
.SetDeviceOrigin( -x
, 0 );
2915 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2916 m_owner
->DrawColLabels( dc
);
2920 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2922 m_owner
->ProcessColLabelMouseEvent( event
);
2926 // This seems to be required for wxMotif otherwise the mouse
2927 // cursor must be in the cell edit control to get key events
2929 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2931 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2936 //////////////////////////////////////////////////////////////////////
2938 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2940 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2941 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2942 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2943 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2946 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2948 const wxPoint
&pos
, const wxSize
&size
)
2949 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2954 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2958 int client_height
= 0;
2959 int client_width
= 0;
2960 GetClientSize( &client_width
, &client_height
);
2962 dc
.SetPen( *wxBLACK_PEN
);
2963 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2964 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2966 dc
.SetPen( *wxWHITE_PEN
);
2967 dc
.DrawLine( 0, 0, client_width
, 0 );
2968 dc
.DrawLine( 0, 0, 0, client_height
);
2972 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2974 m_owner
->ProcessCornerLabelMouseEvent( event
);
2978 // This seems to be required for wxMotif otherwise the mouse
2979 // cursor must be in the cell edit control to get key events
2981 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2983 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2988 //////////////////////////////////////////////////////////////////////
2990 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2992 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2993 EVT_PAINT( wxGridWindow::OnPaint
)
2994 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2995 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2996 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2999 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3000 wxGridRowLabelWindow
*rowLblWin
,
3001 wxGridColLabelWindow
*colLblWin
,
3002 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3003 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3006 m_rowLabelWin
= rowLblWin
;
3007 m_colLabelWin
= colLblWin
;
3008 SetBackgroundColour( "WHITE" );
3012 wxGridWindow::~wxGridWindow()
3017 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3019 wxPaintDC
dc( this );
3020 m_owner
->PrepareDC( dc
);
3021 wxRegion reg
= GetUpdateRegion();
3022 m_owner
->CalcCellsExposed( reg
);
3023 m_owner
->DrawGridCellArea( dc
);
3024 #if WXGRID_DRAW_LINES
3025 m_owner
->DrawAllGridLines( dc
, reg
);
3027 m_owner
->DrawGridSpace( dc
);
3028 m_owner
->DrawHighlight( dc
);
3032 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3034 wxPanel::ScrollWindow( dx
, dy
, rect
);
3035 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3036 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3040 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3042 m_owner
->ProcessGridCellMouseEvent( event
);
3046 // This seems to be required for wxMotif otherwise the mouse
3047 // cursor must be in the cell edit control to get key events
3049 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3051 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3055 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3060 //////////////////////////////////////////////////////////////////////
3063 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3065 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3066 EVT_PAINT( wxGrid::OnPaint
)
3067 EVT_SIZE( wxGrid::OnSize
)
3068 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3069 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3072 wxGrid::wxGrid( wxWindow
*parent
,
3077 const wxString
& name
)
3078 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3079 m_colMinWidths(GRID_HASH_SIZE
),
3080 m_rowMinHeights(GRID_HASH_SIZE
)
3089 wxSafeDecRef(m_defaultCellAttr
);
3091 #ifdef DEBUG_ATTR_CACHE
3092 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3093 wxPrintf(_T("wxGrid attribute cache statistics: "
3094 "total: %u, hits: %u (%u%%)\n"),
3095 total
, gs_nAttrCacheHits
,
3096 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3102 delete m_typeRegistry
;
3108 // ----- internal init and update functions
3111 void wxGrid::Create()
3113 m_created
= FALSE
; // set to TRUE by CreateGrid
3115 m_table
= (wxGridTableBase
*) NULL
;
3118 m_cellEditCtrlEnabled
= FALSE
;
3120 m_defaultCellAttr
= new wxGridCellAttr
;
3121 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3123 // Set default cell attributes
3124 m_defaultCellAttr
->SetFont(GetFont());
3125 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
3126 m_defaultCellAttr
->SetTextColour(
3127 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3128 m_defaultCellAttr
->SetBackgroundColour(
3129 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3130 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3131 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3136 m_currentCellCoords
= wxGridNoCellCoords
;
3138 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3139 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3141 // create the type registry
3142 m_typeRegistry
= new wxGridTypeRegistry
;
3144 // subwindow components that make up the wxGrid
3145 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3150 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3155 m_colLabelWin
= new wxGridColLabelWindow( this,
3160 m_gridWin
= new wxGridWindow( this,
3167 SetTargetWindow( m_gridWin
);
3171 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3172 wxGrid::wxGridSelectionModes selmode
)
3176 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3181 m_numRows
= numRows
;
3182 m_numCols
= numCols
;
3184 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3185 m_table
->SetView( this );
3188 m_selection
= new wxGridSelection( this, selmode
);
3194 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3198 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3201 m_selection
->SetSelectionMode( selmode
);
3204 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3205 wxGrid::wxGridSelectionModes selmode
)
3209 // RD: Actually, this should probably be allowed. I think it would be
3210 // nice to be able to switch multiple Tables in and out of a single
3211 // View at runtime. Is there anything in the implmentation that would
3214 // At least, you now have to cope with m_selection
3215 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3220 m_numRows
= table
->GetNumberRows();
3221 m_numCols
= table
->GetNumberCols();
3224 m_table
->SetView( this );
3228 m_selection
= new wxGridSelection( this, selmode
);
3238 if ( m_numRows
<= 0 )
3239 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
3241 if ( m_numCols
<= 0 )
3242 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
3244 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3245 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3247 if ( m_rowLabelWin
)
3249 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3253 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3256 m_labelTextColour
= wxColour( _T("BLACK") );
3259 m_attrCache
.row
= -1;
3261 // TODO: something better than this ?
3263 m_labelFont
= this->GetFont();
3264 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3266 m_rowLabelHorizAlign
= wxLEFT
;
3267 m_rowLabelVertAlign
= wxCENTRE
;
3269 m_colLabelHorizAlign
= wxCENTRE
;
3270 m_colLabelVertAlign
= wxTOP
;
3272 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3273 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3275 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3276 m_defaultRowHeight
+= 8;
3278 m_defaultRowHeight
+= 4;
3281 m_gridLineColour
= wxColour( 128, 128, 255 );
3282 m_gridLinesEnabled
= TRUE
;
3284 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3285 m_winCapture
= (wxWindow
*)NULL
;
3286 m_canDragRowSize
= TRUE
;
3287 m_canDragColSize
= TRUE
;
3288 m_canDragGridSize
= TRUE
;
3290 m_dragRowOrCol
= -1;
3291 m_isDragging
= FALSE
;
3292 m_startDragPos
= wxDefaultPosition
;
3294 m_waitForSlowClick
= FALSE
;
3296 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3297 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3299 m_currentCellCoords
= wxGridNoCellCoords
;
3301 m_selectingTopLeft
= wxGridNoCellCoords
;
3302 m_selectingBottomRight
= wxGridNoCellCoords
;
3303 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3304 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3306 m_editable
= TRUE
; // default for whole grid
3308 m_inOnKeyDown
= FALSE
;
3317 // ----------------------------------------------------------------------------
3318 // the idea is to call these functions only when necessary because they create
3319 // quite big arrays which eat memory mostly unnecessary - in particular, if
3320 // default widths/heights are used for all rows/columns, we may not use these
3323 // with some extra code, it should be possible to only store the
3324 // widths/heights different from default ones but this will be done later...
3325 // ----------------------------------------------------------------------------
3327 void wxGrid::InitRowHeights()
3329 m_rowHeights
.Empty();
3330 m_rowBottoms
.Empty();
3332 m_rowHeights
.Alloc( m_numRows
);
3333 m_rowBottoms
.Alloc( m_numRows
);
3336 for ( int i
= 0; i
< m_numRows
; i
++ )
3338 m_rowHeights
.Add( m_defaultRowHeight
);
3339 rowBottom
+= m_defaultRowHeight
;
3340 m_rowBottoms
.Add( rowBottom
);
3344 void wxGrid::InitColWidths()
3346 m_colWidths
.Empty();
3347 m_colRights
.Empty();
3349 m_colWidths
.Alloc( m_numCols
);
3350 m_colRights
.Alloc( m_numCols
);
3352 for ( int i
= 0; i
< m_numCols
; i
++ )
3354 m_colWidths
.Add( m_defaultColWidth
);
3355 colRight
+= m_defaultColWidth
;
3356 m_colRights
.Add( colRight
);
3360 int wxGrid::GetColWidth(int col
) const
3362 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3365 int wxGrid::GetColLeft(int col
) const
3367 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3368 : m_colRights
[col
] - m_colWidths
[col
];
3371 int wxGrid::GetColRight(int col
) const
3373 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3377 int wxGrid::GetRowHeight(int row
) const
3379 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3382 int wxGrid::GetRowTop(int row
) const
3384 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3385 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3388 int wxGrid::GetRowBottom(int row
) const
3390 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3391 : m_rowBottoms
[row
];
3394 void wxGrid::CalcDimensions()
3397 GetClientSize( &cw
, &ch
);
3399 if ( m_numRows
> 0 && m_numCols
> 0 )
3401 int right
= GetColRight( m_numCols
-1 ) + m_extraWidth
;
3402 int bottom
= GetRowBottom( m_numRows
-1 ) + m_extraHeight
;
3404 // TODO: restore the scroll position that we had before sizing
3407 GetViewStart( &x
, &y
);
3408 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3409 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3415 void wxGrid::CalcWindowSizes()
3418 GetClientSize( &cw
, &ch
);
3420 if ( m_cornerLabelWin
->IsShown() )
3421 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3423 if ( m_colLabelWin
->IsShown() )
3424 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3426 if ( m_rowLabelWin
->IsShown() )
3427 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3429 if ( m_gridWin
->IsShown() )
3430 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3434 // this is called when the grid table sends a message to say that it
3435 // has been redimensioned
3437 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3441 // if we were using the default widths/heights so far, we must change them
3443 if ( m_colWidths
.IsEmpty() )
3448 if ( m_rowHeights
.IsEmpty() )
3453 switch ( msg
.GetId() )
3455 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3457 size_t pos
= msg
.GetCommandInt();
3458 int numRows
= msg
.GetCommandInt2();
3459 for ( i
= 0; i
< numRows
; i
++ )
3461 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3462 m_rowBottoms
.Insert( 0, pos
);
3464 m_numRows
+= numRows
;
3467 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3469 for ( i
= pos
; i
< m_numRows
; i
++ )
3471 bottom
+= m_rowHeights
[i
];
3472 m_rowBottoms
[i
] = bottom
;
3478 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3480 int numRows
= msg
.GetCommandInt();
3481 for ( i
= 0; i
< numRows
; i
++ )
3483 m_rowHeights
.Add( m_defaultRowHeight
);
3484 m_rowBottoms
.Add( 0 );
3487 int oldNumRows
= m_numRows
;
3488 m_numRows
+= numRows
;
3491 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3493 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3495 bottom
+= m_rowHeights
[i
];
3496 m_rowBottoms
[i
] = bottom
;
3502 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3504 size_t pos
= msg
.GetCommandInt();
3505 int numRows
= msg
.GetCommandInt2();
3506 for ( i
= 0; i
< numRows
; i
++ )
3508 m_rowHeights
.Remove( pos
);
3509 m_rowBottoms
.Remove( pos
);
3511 m_numRows
-= numRows
;
3516 m_colWidths
.Clear();
3517 m_colRights
.Clear();
3518 m_currentCellCoords
= wxGridNoCellCoords
;
3522 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3523 m_currentCellCoords
.Set( 0, 0 );
3526 for ( i
= 0; i
< m_numRows
; i
++ )
3528 h
+= m_rowHeights
[i
];
3529 m_rowBottoms
[i
] = h
;
3537 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3539 size_t pos
= msg
.GetCommandInt();
3540 int numCols
= msg
.GetCommandInt2();
3541 for ( i
= 0; i
< numCols
; i
++ )
3543 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3544 m_colRights
.Insert( 0, pos
);
3546 m_numCols
+= numCols
;
3549 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3551 for ( i
= pos
; i
< m_numCols
; i
++ )
3553 right
+= m_colWidths
[i
];
3554 m_colRights
[i
] = right
;
3560 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3562 int numCols
= msg
.GetCommandInt();
3563 for ( i
= 0; i
< numCols
; i
++ )
3565 m_colWidths
.Add( m_defaultColWidth
);
3566 m_colRights
.Add( 0 );
3569 int oldNumCols
= m_numCols
;
3570 m_numCols
+= numCols
;
3573 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3575 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3577 right
+= m_colWidths
[i
];
3578 m_colRights
[i
] = right
;
3584 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3586 size_t pos
= msg
.GetCommandInt();
3587 int numCols
= msg
.GetCommandInt2();
3588 for ( i
= 0; i
< numCols
; i
++ )
3590 m_colWidths
.Remove( pos
);
3591 m_colRights
.Remove( pos
);
3593 m_numCols
-= numCols
;
3597 #if 0 // leave the row alone here so that AppendCols will work subsequently
3599 m_rowHeights
.Clear();
3600 m_rowBottoms
.Clear();
3602 m_currentCellCoords
= wxGridNoCellCoords
;
3606 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3607 m_currentCellCoords
.Set( 0, 0 );
3610 for ( i
= 0; i
< m_numCols
; i
++ )
3612 w
+= m_colWidths
[i
];
3625 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3627 wxRegionIterator
iter( reg
);
3630 m_rowLabelsExposed
.Empty();
3637 // TODO: remove this when we can...
3638 // There is a bug in wxMotif that gives garbage update
3639 // rectangles if you jump-scroll a long way by clicking the
3640 // scrollbar with middle button. This is a work-around
3642 #if defined(__WXMOTIF__)
3644 m_gridWin
->GetClientSize( &cw
, &ch
);
3645 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3646 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3649 // logical bounds of update region
3652 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3653 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3655 // find the row labels within these bounds
3658 for ( row
= 0; row
< m_numRows
; row
++ )
3660 if ( GetRowBottom(row
) < top
)
3663 if ( GetRowTop(row
) > bottom
)
3666 m_rowLabelsExposed
.Add( row
);
3674 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3676 wxRegionIterator
iter( reg
);
3679 m_colLabelsExposed
.Empty();
3686 // TODO: remove this when we can...
3687 // There is a bug in wxMotif that gives garbage update
3688 // rectangles if you jump-scroll a long way by clicking the
3689 // scrollbar with middle button. This is a work-around
3691 #if defined(__WXMOTIF__)
3693 m_gridWin
->GetClientSize( &cw
, &ch
);
3694 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3695 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3698 // logical bounds of update region
3701 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3702 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3704 // find the cells within these bounds
3707 for ( col
= 0; col
< m_numCols
; col
++ )
3709 if ( GetColRight(col
) < left
)
3712 if ( GetColLeft(col
) > right
)
3715 m_colLabelsExposed
.Add( col
);
3723 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3725 wxRegionIterator
iter( reg
);
3728 m_cellsExposed
.Empty();
3729 m_rowsExposed
.Empty();
3730 m_colsExposed
.Empty();
3732 int left
, top
, right
, bottom
;
3737 // TODO: remove this when we can...
3738 // There is a bug in wxMotif that gives garbage update
3739 // rectangles if you jump-scroll a long way by clicking the
3740 // scrollbar with middle button. This is a work-around
3742 #if defined(__WXMOTIF__)
3744 m_gridWin
->GetClientSize( &cw
, &ch
);
3745 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3746 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3747 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3748 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3751 // logical bounds of update region
3753 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3754 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3756 // find the cells within these bounds
3759 for ( row
= 0; row
< m_numRows
; row
++ )
3761 if ( GetRowBottom(row
) <= top
)
3764 if ( GetRowTop(row
) > bottom
)
3767 m_rowsExposed
.Add( row
);
3769 for ( col
= 0; col
< m_numCols
; col
++ )
3771 if ( GetColRight(col
) <= left
)
3774 if ( GetColLeft(col
) > right
)
3777 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3778 m_colsExposed
.Add( col
);
3779 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3788 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3791 wxPoint
pos( event
.GetPosition() );
3792 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3794 if ( event
.Dragging() )
3796 m_isDragging
= TRUE
;
3798 if ( event
.LeftIsDown() )
3800 switch( m_cursorMode
)
3802 case WXGRID_CURSOR_RESIZE_ROW
:
3804 int cw
, ch
, left
, dummy
;
3805 m_gridWin
->GetClientSize( &cw
, &ch
);
3806 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3808 wxClientDC
dc( m_gridWin
);
3811 GetRowTop(m_dragRowOrCol
) +
3812 GetRowMinimalHeight(m_dragRowOrCol
) );
3813 dc
.SetLogicalFunction(wxINVERT
);
3814 if ( m_dragLastPos
>= 0 )
3816 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3818 dc
.DrawLine( left
, y
, left
+cw
, y
);
3823 case WXGRID_CURSOR_SELECT_ROW
:
3824 if ( (row
= YToRow( y
)) >= 0 )
3826 m_selection
->SelectRow( row
,
3827 event
.ControlDown(),
3833 // default label to suppress warnings about "enumeration value
3834 // 'xxx' not handled in switch
3842 m_isDragging
= FALSE
;
3845 // ------------ Entering or leaving the window
3847 if ( event
.Entering() || event
.Leaving() )
3849 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3853 // ------------ Left button pressed
3855 else if ( event
.LeftDown() )
3857 // don't send a label click event for a hit on the
3858 // edge of the row label - this is probably the user
3859 // wanting to resize the row
3861 if ( YToEdgeOfRow(y
) < 0 )
3865 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3867 if ( !event
.ShiftDown() && !event
.ControlDown() )
3869 if ( event
.ShiftDown() )
3870 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
3873 GetNumberCols() - 1,
3874 event
.ControlDown(),
3879 m_selection
->SelectRow( row
,
3880 event
.ControlDown(),
3884 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3889 // starting to drag-resize a row
3891 if ( CanDragRowSize() )
3892 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3897 // ------------ Left double click
3899 else if (event
.LeftDClick() )
3901 if ( YToEdgeOfRow(y
) < 0 )
3904 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3909 // ------------ Left button released
3911 else if ( event
.LeftUp() )
3913 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3915 DoEndDragResizeRow();
3917 // Note: we are ending the event *after* doing
3918 // default processing in this case
3920 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3923 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3928 // ------------ Right button down
3930 else if ( event
.RightDown() )
3933 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3935 // no default action at the moment
3940 // ------------ Right double click
3942 else if ( event
.RightDClick() )
3945 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3947 // no default action at the moment
3952 // ------------ No buttons down and mouse moving
3954 else if ( event
.Moving() )
3956 m_dragRowOrCol
= YToEdgeOfRow( y
);
3957 if ( m_dragRowOrCol
>= 0 )
3959 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3961 // don't capture the mouse yet
3962 if ( CanDragRowSize() )
3963 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3966 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3968 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3974 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3977 wxPoint
pos( event
.GetPosition() );
3978 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3980 if ( event
.Dragging() )
3982 m_isDragging
= TRUE
;
3984 if ( event
.LeftIsDown() )
3986 switch( m_cursorMode
)
3988 case WXGRID_CURSOR_RESIZE_COL
:
3990 int cw
, ch
, dummy
, top
;
3991 m_gridWin
->GetClientSize( &cw
, &ch
);
3992 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3994 wxClientDC
dc( m_gridWin
);
3997 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3998 GetColMinimalWidth(m_dragRowOrCol
));
3999 dc
.SetLogicalFunction(wxINVERT
);
4000 if ( m_dragLastPos
>= 0 )
4002 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4004 dc
.DrawLine( x
, top
, x
, top
+ch
);
4009 case WXGRID_CURSOR_SELECT_COL
:
4010 if ( (col
= XToCol( x
)) >= 0 )
4012 m_selection
->SelectCol( col
,
4013 event
.ControlDown(),
4019 // default label to suppress warnings about "enumeration value
4020 // 'xxx' not handled in switch
4028 m_isDragging
= FALSE
;
4031 // ------------ Entering or leaving the window
4033 if ( event
.Entering() || event
.Leaving() )
4035 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4039 // ------------ Left button pressed
4041 else if ( event
.LeftDown() )
4043 // don't send a label click event for a hit on the
4044 // edge of the col label - this is probably the user
4045 // wanting to resize the col
4047 if ( XToEdgeOfCol(x
) < 0 )
4051 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4053 if ( !event
.ShiftDown() && !event
.ControlDown() )
4055 if ( event
.ShiftDown() )
4056 m_selection
->SelectBlock( 0,
4057 m_currentCellCoords
.GetCol(),
4058 GetNumberRows() - 1, col
,
4059 event
.ControlDown(),
4064 m_selection
->SelectCol( col
,
4065 event
.ControlDown(),
4069 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4074 // starting to drag-resize a col
4076 if ( CanDragColSize() )
4077 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4082 // ------------ Left double click
4084 if ( event
.LeftDClick() )
4086 if ( XToEdgeOfCol(x
) < 0 )
4089 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4094 // ------------ Left button released
4096 else if ( event
.LeftUp() )
4098 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4100 DoEndDragResizeCol();
4102 // Note: we are ending the event *after* doing
4103 // default processing in this case
4105 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4108 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4113 // ------------ Right button down
4115 else if ( event
.RightDown() )
4118 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4120 // no default action at the moment
4125 // ------------ Right double click
4127 else if ( event
.RightDClick() )
4130 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4132 // no default action at the moment
4137 // ------------ No buttons down and mouse moving
4139 else if ( event
.Moving() )
4141 m_dragRowOrCol
= XToEdgeOfCol( x
);
4142 if ( m_dragRowOrCol
>= 0 )
4144 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4146 // don't capture the cursor yet
4147 if ( CanDragColSize() )
4148 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4151 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4153 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4159 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4161 if ( event
.LeftDown() )
4163 // indicate corner label by having both row and
4166 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4172 else if ( event
.LeftDClick() )
4174 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4177 else if ( event
.RightDown() )
4179 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4181 // no default action at the moment
4185 else if ( event
.RightDClick() )
4187 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4189 // no default action at the moment
4194 void wxGrid::ChangeCursorMode(CursorMode mode
,
4199 static const wxChar
*cursorModes
[] =
4208 wxLogTrace(_T("grid"),
4209 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4210 win
== m_colLabelWin
? _T("colLabelWin")
4211 : win
? _T("rowLabelWin")
4213 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4214 #endif // __WXDEBUG__
4216 if ( mode
== m_cursorMode
)
4221 // by default use the grid itself
4227 m_winCapture
->ReleaseMouse();
4228 m_winCapture
= (wxWindow
*)NULL
;
4231 m_cursorMode
= mode
;
4233 switch ( m_cursorMode
)
4235 case WXGRID_CURSOR_RESIZE_ROW
:
4236 win
->SetCursor( m_rowResizeCursor
);
4239 case WXGRID_CURSOR_RESIZE_COL
:
4240 win
->SetCursor( m_colResizeCursor
);
4244 win
->SetCursor( *wxSTANDARD_CURSOR
);
4247 // we need to capture mouse when resizing
4248 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4249 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4251 if ( captureMouse
&& resize
)
4253 win
->CaptureMouse();
4258 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4261 wxPoint
pos( event
.GetPosition() );
4262 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4264 wxGridCellCoords coords
;
4265 XYToCell( x
, y
, coords
);
4267 if ( event
.Dragging() )
4269 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4271 // Don't start doing anything until the mouse has been drug at
4272 // least 3 pixels in any direction...
4275 if (m_startDragPos
== wxDefaultPosition
)
4277 m_startDragPos
= pos
;
4280 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4284 m_isDragging
= TRUE
;
4285 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4287 // Hide the edit control, so it
4288 // won't interfer with drag-shrinking.
4289 if ( IsCellEditControlEnabled() )
4291 HideCellEditControl();
4292 SaveEditControlValue();
4295 // Have we captured the mouse yet?
4298 m_winCapture
= m_gridWin
;
4299 m_winCapture
->CaptureMouse();
4302 if ( coords
!= wxGridNoCellCoords
)
4304 if ( event
.ControlDown() )
4306 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4307 m_selectingKeyboard
= coords
;
4308 SelectBlock ( m_selectingKeyboard
, coords
);
4312 if ( !IsSelection() )
4314 SelectBlock( coords
, coords
);
4318 SelectBlock( m_currentCellCoords
, coords
);
4322 if (! IsVisible(coords
))
4324 MakeCellVisible(coords
);
4325 // TODO: need to introduce a delay or something here. The
4326 // scrolling is way to fast, at least on MSW - also on GTK.
4330 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4332 int cw
, ch
, left
, dummy
;
4333 m_gridWin
->GetClientSize( &cw
, &ch
);
4334 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4336 wxClientDC
dc( m_gridWin
);
4338 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4339 GetRowMinimalHeight(m_dragRowOrCol
) );
4340 dc
.SetLogicalFunction(wxINVERT
);
4341 if ( m_dragLastPos
>= 0 )
4343 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4345 dc
.DrawLine( left
, y
, left
+cw
, y
);
4348 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4350 int cw
, ch
, dummy
, top
;
4351 m_gridWin
->GetClientSize( &cw
, &ch
);
4352 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4354 wxClientDC
dc( m_gridWin
);
4356 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4357 GetColMinimalWidth(m_dragRowOrCol
) );
4358 dc
.SetLogicalFunction(wxINVERT
);
4359 if ( m_dragLastPos
>= 0 )
4361 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4363 dc
.DrawLine( x
, top
, x
, top
+ch
);
4370 m_isDragging
= FALSE
;
4371 m_startDragPos
= wxDefaultPosition
;
4373 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4374 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4377 if ( event
.Entering() || event
.Leaving() )
4379 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4380 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4385 // ------------ Left button pressed
4387 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4389 if ( !event
.ShiftDown() && !event
.ControlDown() )
4391 if ( event
.ShiftDown() )
4393 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4394 m_currentCellCoords
.GetCol(),
4397 event
.ControlDown(),
4402 else if ( XToEdgeOfCol(x
) < 0 &&
4403 YToEdgeOfRow(y
) < 0 )
4405 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4410 DisableCellEditControl();
4411 MakeCellVisible( coords
);
4413 // if this is the second click on this cell then start
4415 if ( m_waitForSlowClick
&&
4416 (coords
== m_currentCellCoords
) &&
4417 CanEnableCellControl())
4419 EnableCellEditControl();
4421 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4422 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4425 editor
->StartingClick();
4429 m_waitForSlowClick
= FALSE
;
4433 if ( event
.ControlDown() )
4435 m_selection
->ToggleCellSelection( coords
.GetRow(),
4437 event
.ControlDown(),
4441 m_selectingTopLeft
= wxGridNoCellCoords
;
4442 m_selectingBottomRight
= wxGridNoCellCoords
;
4443 m_selectingKeyboard
= coords
;
4446 SetCurrentCell( coords
);
4447 m_waitForSlowClick
= TRUE
;
4454 // ------------ Left double click
4456 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4458 DisableCellEditControl();
4460 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4462 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4470 // ------------ Left button released
4472 else if ( event
.LeftUp() )
4474 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4476 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
4477 m_selectingBottomRight
!= wxGridNoCellCoords
)
4481 m_winCapture
->ReleaseMouse();
4482 m_winCapture
= NULL
;
4484 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
4485 m_selectingTopLeft
.GetCol(),
4486 m_selectingBottomRight
.GetRow(),
4487 m_selectingBottomRight
.GetCol(),
4488 event
.ControlDown(),
4492 m_selectingTopLeft
= wxGridNoCellCoords
;
4493 m_selectingBottomRight
= wxGridNoCellCoords
;
4496 // Show the edit control, if it has been hidden for
4498 ShowCellEditControl();
4500 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4502 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4503 DoEndDragResizeRow();
4505 // Note: we are ending the event *after* doing
4506 // default processing in this case
4508 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4510 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4512 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4513 DoEndDragResizeCol();
4515 // Note: we are ending the event *after* doing
4516 // default processing in this case
4518 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4525 // ------------ Right button down
4527 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4529 DisableCellEditControl();
4530 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4535 // no default action at the moment
4540 // ------------ Right double click
4542 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4544 DisableCellEditControl();
4545 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4550 // no default action at the moment
4554 // ------------ Moving and no button action
4556 else if ( event
.Moving() && !event
.IsButton() )
4558 int dragRow
= YToEdgeOfRow( y
);
4559 int dragCol
= XToEdgeOfCol( x
);
4561 // Dragging on the corner of a cell to resize in both
4562 // directions is not implemented yet...
4564 if ( dragRow
>= 0 && dragCol
>= 0 )
4566 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4572 m_dragRowOrCol
= dragRow
;
4574 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4576 if ( CanDragRowSize() && CanDragGridSize() )
4577 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4582 m_dragRowOrCol
= dragCol
;
4590 m_dragRowOrCol
= dragCol
;
4592 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4594 if ( CanDragColSize() && CanDragGridSize() )
4595 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4601 // Neither on a row or col edge
4603 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4605 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4611 void wxGrid::DoEndDragResizeRow()
4613 if ( m_dragLastPos
>= 0 )
4615 // erase the last line and resize the row
4617 int cw
, ch
, left
, dummy
;
4618 m_gridWin
->GetClientSize( &cw
, &ch
);
4619 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4621 wxClientDC
dc( m_gridWin
);
4623 dc
.SetLogicalFunction( wxINVERT
);
4624 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4625 HideCellEditControl();
4626 SaveEditControlValue();
4628 int rowTop
= GetRowTop(m_dragRowOrCol
);
4629 SetRowSize( m_dragRowOrCol
,
4630 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4632 if ( !GetBatchCount() )
4634 // Only needed to get the correct rect.y:
4635 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4637 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4638 rect
.width
= m_rowLabelWidth
;
4639 rect
.height
= ch
- rect
.y
;
4640 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4642 m_gridWin
->Refresh( FALSE
, &rect
);
4645 ShowCellEditControl();
4650 void wxGrid::DoEndDragResizeCol()
4652 if ( m_dragLastPos
>= 0 )
4654 // erase the last line and resize the col
4656 int cw
, ch
, dummy
, top
;
4657 m_gridWin
->GetClientSize( &cw
, &ch
);
4658 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4660 wxClientDC
dc( m_gridWin
);
4662 dc
.SetLogicalFunction( wxINVERT
);
4663 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4664 HideCellEditControl();
4665 SaveEditControlValue();
4667 int colLeft
= GetColLeft(m_dragRowOrCol
);
4668 SetColSize( m_dragRowOrCol
,
4669 wxMax( m_dragLastPos
- colLeft
,
4670 GetColMinimalWidth(m_dragRowOrCol
) ) );
4672 if ( !GetBatchCount() )
4674 // Only needed to get the correct rect.x:
4675 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4677 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4678 rect
.width
= cw
- rect
.x
;
4679 rect
.height
= m_colLabelHeight
;
4680 m_colLabelWin
->Refresh( TRUE
, &rect
);
4682 m_gridWin
->Refresh( FALSE
, &rect
);
4685 ShowCellEditControl();
4692 // ------ interaction with data model
4694 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4696 switch ( msg
.GetId() )
4698 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4699 return GetModelValues();
4701 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4702 return SetModelValues();
4704 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4705 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4706 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4707 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4708 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4709 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4710 return Redimension( msg
);
4719 // The behaviour of this function depends on the grid table class
4720 // Clear() function. For the default wxGridStringTable class the
4721 // behavious is to replace all cell contents with wxEmptyString but
4722 // not to change the number of rows or cols.
4724 void wxGrid::ClearGrid()
4728 if (IsCellEditControlEnabled())
4729 DisableCellEditControl();
4732 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4737 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4739 // TODO: something with updateLabels flag
4743 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4749 if (IsCellEditControlEnabled())
4750 DisableCellEditControl();
4752 bool ok
= m_table
->InsertRows( pos
, numRows
);
4754 // the table will have sent the results of the insert row
4755 // operation to this view object as a grid table message
4759 if ( m_numCols
== 0 )
4761 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4763 // TODO: perhaps instead of appending the default number of cols
4764 // we should remember what the last non-zero number of cols was ?
4768 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4770 // if we have just inserted cols into an empty grid the current
4771 // cell will be undefined...
4773 SetCurrentCell( 0, 0 );
4776 m_selection
->UpdateRows( pos
, numRows
);
4777 if ( !GetBatchCount() ) Refresh();
4789 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4791 // TODO: something with updateLabels flag
4795 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4799 if ( m_table
&& m_table
->AppendRows( numRows
) )
4801 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4803 // if we have just inserted cols into an empty grid the current
4804 // cell will be undefined...
4806 SetCurrentCell( 0, 0 );
4809 // the table will have sent the results of the append row
4810 // operation to this view object as a grid table message
4812 if ( !GetBatchCount() ) Refresh();
4822 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4824 // TODO: something with updateLabels flag
4828 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4834 if (IsCellEditControlEnabled())
4835 DisableCellEditControl();
4837 if (m_table
->DeleteRows( pos
, numRows
))
4840 // the table will have sent the results of the delete row
4841 // operation to this view object as a grid table message
4843 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4844 if ( !GetBatchCount() ) Refresh();
4852 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4854 // TODO: something with updateLabels flag
4858 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4864 if (IsCellEditControlEnabled())
4865 DisableCellEditControl();
4867 bool ok
= m_table
->InsertCols( pos
, numCols
);
4869 // the table will have sent the results of the insert col
4870 // operation to this view object as a grid table message
4874 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4876 // if we have just inserted cols into an empty grid the current
4877 // cell will be undefined...
4879 SetCurrentCell( 0, 0 );
4882 m_selection
->UpdateCols( pos
, numCols
);
4883 if ( !GetBatchCount() ) Refresh();
4895 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4897 // TODO: something with updateLabels flag
4901 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4905 if ( m_table
&& m_table
->AppendCols( numCols
) )
4907 // the table will have sent the results of the append col
4908 // operation to this view object as a grid table message
4910 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4912 // if we have just inserted cols into an empty grid the current
4913 // cell will be undefined...
4915 SetCurrentCell( 0, 0 );
4918 if ( !GetBatchCount() ) Refresh();
4928 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4930 // TODO: something with updateLabels flag
4934 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4940 if (IsCellEditControlEnabled())
4941 DisableCellEditControl();
4943 if ( m_table
->DeleteCols( pos
, numCols
) )
4945 // the table will have sent the results of the delete col
4946 // operation to this view object as a grid table message
4948 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4949 if ( !GetBatchCount() ) Refresh();
4959 // ----- event handlers
4962 // Generate a grid event based on a mouse event and
4963 // return the result of ProcessEvent()
4965 bool wxGrid::SendEvent( const wxEventType type
,
4967 wxMouseEvent
& mouseEv
)
4969 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4971 int rowOrCol
= (row
== -1 ? col
: row
);
4973 wxGridSizeEvent
gridEvt( GetId(),
4977 mouseEv
.GetX(), mouseEv
.GetY(),
4978 mouseEv
.ControlDown(),
4979 mouseEv
.ShiftDown(),
4981 mouseEv
.MetaDown() );
4983 return GetEventHandler()->ProcessEvent(gridEvt
);
4985 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4987 // Right now, it should _never_ end up here!
4988 wxGridRangeSelectEvent
gridEvt( GetId(),
4992 m_selectingBottomRight
,
4994 mouseEv
.ControlDown(),
4995 mouseEv
.ShiftDown(),
4997 mouseEv
.MetaDown() );
4999 return GetEventHandler()->ProcessEvent(gridEvt
);
5003 wxGridEvent
gridEvt( GetId(),
5007 mouseEv
.GetX(), mouseEv
.GetY(),
5009 mouseEv
.ControlDown(),
5010 mouseEv
.ShiftDown(),
5012 mouseEv
.MetaDown() );
5014 return GetEventHandler()->ProcessEvent(gridEvt
);
5019 // Generate a grid event of specified type and return the result
5020 // of ProcessEvent().
5022 bool wxGrid::SendEvent( const wxEventType type
,
5025 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5027 int rowOrCol
= (row
== -1 ? col
: row
);
5029 wxGridSizeEvent
gridEvt( GetId(),
5034 return GetEventHandler()->ProcessEvent(gridEvt
);
5038 wxGridEvent
gridEvt( GetId(),
5043 return GetEventHandler()->ProcessEvent(gridEvt
);
5048 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5050 wxPaintDC
dc( this );
5054 // This is just here to make sure that CalcDimensions gets called when
5055 // the grid view is resized... then the size event is skipped to allow
5056 // the box sizers to handle everything
5058 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5065 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5067 if ( m_inOnKeyDown
)
5069 // shouldn't be here - we are going round in circles...
5071 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5074 m_inOnKeyDown
= TRUE
;
5076 // propagate the event up and see if it gets processed
5078 wxWindow
*parent
= GetParent();
5079 wxKeyEvent
keyEvt( event
);
5080 keyEvt
.SetEventObject( parent
);
5082 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5085 // try local handlers
5087 if ( !event
.ShiftDown() &&
5088 m_selectingKeyboard
!= wxGridNoCellCoords
)
5090 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5091 m_selectingBottomRight
!= wxGridNoCellCoords
)
5092 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5093 m_selectingTopLeft
.GetCol(),
5094 m_selectingBottomRight
.GetRow(),
5095 m_selectingBottomRight
.GetCol(),
5096 event
.ControlDown(),
5100 m_selectingTopLeft
= wxGridNoCellCoords
;
5101 m_selectingBottomRight
= wxGridNoCellCoords
;
5102 m_selectingKeyboard
= wxGridNoCellCoords
;
5105 switch ( event
.KeyCode() )
5108 if ( event
.ControlDown() )
5110 MoveCursorUpBlock( event
.ShiftDown() );
5114 MoveCursorUp( event
.ShiftDown() );
5119 if ( event
.ControlDown() )
5121 MoveCursorDownBlock( event
.ShiftDown() );
5125 MoveCursorDown( event
.ShiftDown() );
5130 if ( event
.ControlDown() )
5132 MoveCursorLeftBlock( event
.ShiftDown() );
5136 MoveCursorLeft( event
.ShiftDown() );
5141 if ( event
.ControlDown() )
5143 MoveCursorRightBlock( event
.ShiftDown() );
5147 MoveCursorRight( event
.ShiftDown() );
5152 if ( event
.ControlDown() )
5154 event
.Skip(); // to let the edit control have the return
5158 MoveCursorDown( event
.ShiftDown() );
5167 if (event
.ShiftDown())
5168 MoveCursorLeft( FALSE
);
5170 MoveCursorRight( FALSE
);
5174 if ( event
.ControlDown() )
5176 MakeCellVisible( 0, 0 );
5177 SetCurrentCell( 0, 0 );
5186 if ( event
.ControlDown() )
5188 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5189 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5206 if ( event
.ControlDown() )
5208 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5209 m_currentCellCoords
.GetCol(),
5210 event
.ControlDown(),
5216 if ( !IsEditable() )
5218 MoveCursorRight( FALSE
);
5221 // Otherwise fall through to default
5224 // alphanumeric keys or F2 (special key just for this) enable
5225 // the cell edit control
5226 // On just Shift/Control I get values for event.KeyCode()
5227 // that are outside the range where isalnum's behaviour is
5228 // well defined, so do an additional sanity check.
5229 if ( !(event
.AltDown() ||
5231 event
.ControlDown()) &&
5232 ((isalnum((int)event
.KeyCode()) &&
5233 (event
.KeyCode() < 256 && event
.KeyCode() >= 0)) ||
5234 event
.KeyCode() == WXK_F2
) &&
5235 !IsCellEditControlEnabled() &&
5236 CanEnableCellControl() )
5238 EnableCellEditControl();
5239 int row
= m_currentCellCoords
.GetRow();
5240 int col
= m_currentCellCoords
.GetCol();
5241 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5242 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5243 editor
->StartingKey(event
);
5249 // let others process char events with modifiers or all
5250 // char events for readonly cells
5257 m_inOnKeyDown
= FALSE
;
5260 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5264 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5266 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5268 HideCellEditControl();
5269 DisableCellEditControl();
5271 // Clear the old current cell highlight
5272 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5274 // Otherwise refresh redraws the highlight!
5275 m_currentCellCoords
= coords
;
5277 m_gridWin
->Refresh( FALSE
, &r
);
5280 m_currentCellCoords
= coords
;
5282 wxClientDC
dc(m_gridWin
);
5285 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5286 DrawCellHighlight(dc
, attr
);
5290 // SN: For my extended selection code, automatic
5291 // deselection is definitely not a good idea.
5292 if ( IsSelection() )
5294 wxRect
r( SelectionToDeviceRect() );
5296 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
5303 // ------ functions to get/send data (see also public functions)
5306 bool wxGrid::GetModelValues()
5310 // all we need to do is repaint the grid
5312 m_gridWin
->Refresh();
5320 bool wxGrid::SetModelValues()
5326 for ( row
= 0; row
< m_numRows
; row
++ )
5328 for ( col
= 0; col
< m_numCols
; col
++ )
5330 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5342 // Note - this function only draws cells that are in the list of
5343 // exposed cells (usually set from the update region by
5344 // CalcExposedCells)
5346 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5348 if ( !m_numRows
|| !m_numCols
) return;
5351 size_t numCells
= m_cellsExposed
.GetCount();
5353 for ( i
= 0; i
< numCells
; i
++ )
5355 DrawCell( dc
, m_cellsExposed
[i
] );
5360 void wxGrid::DrawGridSpace( wxDC
& dc
)
5362 if ( m_numRows
&& m_numCols
)
5365 m_gridWin
->GetClientSize( &cw
, &ch
);
5368 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5370 if ( right
> GetColRight(m_numCols
-1) ||
5371 bottom
> GetRowBottom(m_numRows
-1) )
5374 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5376 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5377 dc
.SetPen( *wxTRANSPARENT_PEN
);
5379 if ( right
> GetColRight(m_numCols
-1) )
5380 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
5381 right
- GetColRight(m_numCols
-1), ch
);
5383 if ( bottom
> GetRowBottom(m_numRows
-1) )
5384 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
5385 cw
, bottom
- GetRowBottom(m_numRows
-1) );
5391 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5393 int row
= coords
.GetRow();
5394 int col
= coords
.GetCol();
5396 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5399 // we draw the cell border ourselves
5400 #if !WXGRID_DRAW_LINES
5401 if ( m_gridLinesEnabled
)
5402 DrawCellBorder( dc
, coords
);
5405 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5407 bool isCurrent
= coords
== m_currentCellCoords
;
5410 rect
.x
= GetColLeft(col
);
5411 rect
.y
= GetRowTop(row
);
5412 rect
.width
= GetColWidth(col
) - 1;
5413 rect
.height
= GetRowHeight(row
) - 1;
5415 // if the editor is shown, we should use it and not the renderer
5416 if ( isCurrent
&& IsCellEditControlEnabled() )
5418 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5419 editor
->PaintBackground(rect
, attr
);
5424 // but all the rest is drawn by the cell renderer and hence may be
5426 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5427 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5434 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5436 int row
= m_currentCellCoords
.GetRow();
5437 int col
= m_currentCellCoords
.GetCol();
5439 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5443 rect
.x
= GetColLeft(col
);
5444 rect
.y
= GetRowTop(row
);
5445 rect
.width
= GetColWidth(col
) - 1;
5446 rect
.height
= GetRowHeight(row
) - 1;
5448 // hmmm... what could we do here to show that the cell is disabled?
5449 // for now, I just draw a thinner border than for the other ones, but
5450 // it doesn't look really good
5451 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5452 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5454 dc
.DrawRectangle(rect
);
5457 // VZ: my experiments with 3d borders...
5459 // how to properly set colours for arbitrary bg?
5460 wxCoord x1
= rect
.x
,
5462 x2
= rect
.x
+ rect
.width
-1,
5463 y2
= rect
.y
+ rect
.height
-1;
5465 dc
.SetPen(*wxWHITE_PEN
);
5466 dc
.DrawLine(x1
, y1
, x2
, y1
);
5467 dc
.DrawLine(x1
, y1
, x1
, y2
);
5469 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5470 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5472 dc
.SetPen(*wxBLACK_PEN
);
5473 dc
.DrawLine(x1
, y2
, x2
, y2
);
5474 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5479 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5481 int row
= coords
.GetRow();
5482 int col
= coords
.GetCol();
5483 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5486 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5488 // right hand border
5490 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5491 GetColRight(col
), GetRowBottom(row
) );
5495 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5496 GetColRight(col
), GetRowBottom(row
) );
5499 void wxGrid::DrawHighlight(wxDC
& dc
)
5501 // This if block was previously in wxGrid::OnPaint but that doesn't
5502 // seem to get called under wxGTK - MB
5504 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5505 m_numRows
&& m_numCols
)
5507 m_currentCellCoords
.Set(0, 0);
5510 if ( IsCellEditControlEnabled() )
5512 // don't show highlight when the edit control is shown
5516 // if the active cell was repainted, repaint its highlight too because it
5517 // might have been damaged by the grid lines
5518 size_t count
= m_cellsExposed
.GetCount();
5519 for ( size_t n
= 0; n
< count
; n
++ )
5521 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5523 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5524 DrawCellHighlight(dc
, attr
);
5532 // TODO: remove this ???
5533 // This is used to redraw all grid lines e.g. when the grid line colour
5536 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED_GTK(reg
) )
5538 if ( !m_gridLinesEnabled
||
5540 !m_numCols
) return;
5542 int top
, bottom
, left
, right
;
5548 m_gridWin
->GetClientSize(&cw
, &ch
);
5550 // virtual coords of visible area
5552 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5553 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5558 reg
.GetBox(x
, y
, w
, h
);
5559 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5560 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5564 m_gridWin
->GetClientSize(&cw
, &ch
);
5565 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5566 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5569 // avoid drawing grid lines past the last row and col
5571 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5572 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5574 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5576 // horizontal grid lines
5579 for ( i
= 0; i
< m_numRows
; i
++ )
5581 int bot
= GetRowBottom(i
) - 1;
5590 dc
.DrawLine( left
, bot
, right
, bot
);
5595 // vertical grid lines
5597 for ( i
= 0; i
< m_numCols
; i
++ )
5599 int colRight
= GetColRight(i
) - 1;
5600 if ( colRight
> right
)
5605 if ( colRight
>= left
)
5607 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5613 void wxGrid::DrawRowLabels( wxDC
& dc
)
5615 if ( !m_numRows
|| !m_numCols
) return;
5618 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5620 for ( i
= 0; i
< numLabels
; i
++ )
5622 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5627 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5629 if ( GetRowHeight(row
) <= 0 )
5632 int rowTop
= GetRowTop(row
),
5633 rowBottom
= GetRowBottom(row
) - 1;
5635 dc
.SetPen( *wxBLACK_PEN
);
5636 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5637 m_rowLabelWidth
-1, rowBottom
);
5639 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5641 dc
.SetPen( *wxWHITE_PEN
);
5642 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5643 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5645 dc
.SetBackgroundMode( wxTRANSPARENT
);
5646 dc
.SetTextForeground( GetLabelTextColour() );
5647 dc
.SetFont( GetLabelFont() );
5650 GetRowLabelAlignment( &hAlign
, &vAlign
);
5654 rect
.SetY( GetRowTop(row
) + 2 );
5655 rect
.SetWidth( m_rowLabelWidth
- 4 );
5656 rect
.SetHeight( GetRowHeight(row
) - 4 );
5657 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5661 void wxGrid::DrawColLabels( wxDC
& dc
)
5663 if ( !m_numRows
|| !m_numCols
) return;
5666 size_t numLabels
= m_colLabelsExposed
.GetCount();
5668 for ( i
= 0; i
< numLabels
; i
++ )
5670 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5675 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5677 if ( GetColWidth(col
) <= 0 )
5680 int colLeft
= GetColLeft(col
),
5681 colRight
= GetColRight(col
) - 1;
5683 dc
.SetPen( *wxBLACK_PEN
);
5684 dc
.DrawLine( colRight
, 0,
5685 colRight
, m_colLabelHeight
-1 );
5687 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5688 colRight
, m_colLabelHeight
-1 );
5690 dc
.SetPen( *wxWHITE_PEN
);
5691 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5692 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5694 dc
.SetBackgroundMode( wxTRANSPARENT
);
5695 dc
.SetTextForeground( GetLabelTextColour() );
5696 dc
.SetFont( GetLabelFont() );
5698 dc
.SetBackgroundMode( wxTRANSPARENT
);
5699 dc
.SetTextForeground( GetLabelTextColour() );
5700 dc
.SetFont( GetLabelFont() );
5703 GetColLabelAlignment( &hAlign
, &vAlign
);
5706 rect
.SetX( colLeft
+ 2 );
5708 rect
.SetWidth( GetColWidth(col
) - 4 );
5709 rect
.SetHeight( m_colLabelHeight
- 4 );
5710 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5714 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5715 const wxString
& value
,
5720 long textWidth
, textHeight
;
5721 long lineWidth
, lineHeight
;
5722 wxArrayString lines
;
5724 dc
.SetClippingRegion( rect
);
5725 StringToLines( value
, lines
);
5726 if ( lines
.GetCount() )
5728 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5729 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5732 switch ( horizAlign
)
5735 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5739 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5748 switch ( vertAlign
)
5751 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5755 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5764 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5766 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
5771 dc
.DestroyClippingRegion();
5775 // Split multi line text up into an array of strings. Any existing
5776 // contents of the string array are preserved.
5778 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5782 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5783 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5785 while ( startPos
< (int)tVal
.Length() )
5787 pos
= tVal
.Mid(startPos
).Find( eol
);
5792 else if ( pos
== 0 )
5794 lines
.Add( wxEmptyString
);
5798 lines
.Add( value
.Mid(startPos
, pos
) );
5802 if ( startPos
< (int)value
.Length() )
5804 lines
.Add( value
.Mid( startPos
) );
5809 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5810 wxArrayString
& lines
,
5811 long *width
, long *height
)
5818 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5820 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5821 w
= wxMax( w
, lineW
);
5831 // ------ Edit control functions
5835 void wxGrid::EnableEditing( bool edit
)
5837 // TODO: improve this ?
5839 if ( edit
!= m_editable
)
5843 // FIXME IMHO this won't disable the edit control if edit == FALSE
5844 // because of the check in the beginning of
5845 // EnableCellEditControl() just below (VZ)
5846 EnableCellEditControl(m_editable
);
5851 void wxGrid::EnableCellEditControl( bool enable
)
5856 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5857 SetCurrentCell( 0, 0 );
5859 if ( enable
!= m_cellEditCtrlEnabled
)
5861 // TODO allow the app to Veto() this event?
5862 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5866 // this should be checked by the caller!
5867 wxASSERT_MSG( CanEnableCellControl(),
5868 _T("can't enable editing for this cell!") );
5870 // do it before ShowCellEditControl()
5871 m_cellEditCtrlEnabled
= enable
;
5873 ShowCellEditControl();
5877 HideCellEditControl();
5878 SaveEditControlValue();
5880 // do it after HideCellEditControl()
5881 m_cellEditCtrlEnabled
= enable
;
5886 bool wxGrid::IsCurrentCellReadOnly() const
5889 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5890 bool readonly
= attr
->IsReadOnly();
5896 bool wxGrid::CanEnableCellControl() const
5898 return m_editable
&& !IsCurrentCellReadOnly();
5901 bool wxGrid::IsCellEditControlEnabled() const
5903 // the cell edit control might be disable for all cells or just for the
5904 // current one if it's read only
5905 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5908 void wxGrid::ShowCellEditControl()
5910 if ( IsCellEditControlEnabled() )
5912 if ( !IsVisible( m_currentCellCoords
) )
5918 wxRect rect
= CellToRect( m_currentCellCoords
);
5919 int row
= m_currentCellCoords
.GetRow();
5920 int col
= m_currentCellCoords
.GetCol();
5922 // convert to scrolled coords
5924 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5926 // done in PaintBackground()
5928 // erase the highlight and the cell contents because the editor
5929 // might not cover the entire cell
5930 wxClientDC
dc( m_gridWin
);
5932 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5933 dc
.SetPen(*wxTRANSPARENT_PEN
);
5934 dc
.DrawRectangle(rect
);
5937 // cell is shifted by one pixel
5941 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5942 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5943 if ( !editor
->IsCreated() )
5945 editor
->Create(m_gridWin
, -1,
5946 new wxGridCellEditorEvtHandler(this, editor
));
5949 editor
->Show( TRUE
, attr
);
5951 editor
->SetSize( rect
);
5953 editor
->BeginEdit(row
, col
, this);
5962 void wxGrid::HideCellEditControl()
5964 if ( IsCellEditControlEnabled() )
5966 int row
= m_currentCellCoords
.GetRow();
5967 int col
= m_currentCellCoords
.GetCol();
5969 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5970 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5971 editor
->Show( FALSE
);
5974 m_gridWin
->SetFocus();
5979 void wxGrid::SaveEditControlValue()
5981 if ( IsCellEditControlEnabled() )
5983 int row
= m_currentCellCoords
.GetRow();
5984 int col
= m_currentCellCoords
.GetCol();
5986 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5987 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5988 bool changed
= editor
->EndEdit(row
, col
, this);
5995 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5996 m_currentCellCoords
.GetRow(),
5997 m_currentCellCoords
.GetCol() );
6004 // ------ Grid location functions
6005 // Note that all of these functions work with the logical coordinates of
6006 // grid cells and labels so you will need to convert from device
6007 // coordinates for mouse events etc.
6010 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6012 int row
= YToRow(y
);
6013 int col
= XToCol(x
);
6015 if ( row
== -1 || col
== -1 )
6017 coords
= wxGridNoCellCoords
;
6021 coords
.Set( row
, col
);
6026 int wxGrid::YToRow( int y
)
6030 for ( i
= 0; i
< m_numRows
; i
++ )
6032 if ( y
< GetRowBottom(i
) )
6040 int wxGrid::XToCol( int x
)
6044 for ( i
= 0; i
< m_numCols
; i
++ )
6046 if ( x
< GetColRight(i
) )
6054 // return the row number that that the y coord is near the edge of, or
6055 // -1 if not near an edge
6057 int wxGrid::YToEdgeOfRow( int y
)
6061 for ( i
= 0; i
< m_numRows
; i
++ )
6063 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6065 d
= abs( y
- GetRowBottom(i
) );
6066 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6075 // return the col number that that the x coord is near the edge of, or
6076 // -1 if not near an edge
6078 int wxGrid::XToEdgeOfCol( int x
)
6082 for ( i
= 0; i
< m_numCols
; i
++ )
6084 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6086 d
= abs( x
- GetColRight(i
) );
6087 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6096 wxRect
wxGrid::CellToRect( int row
, int col
)
6098 wxRect
rect( -1, -1, -1, -1 );
6100 if ( row
>= 0 && row
< m_numRows
&&
6101 col
>= 0 && col
< m_numCols
)
6103 rect
.x
= GetColLeft(col
);
6104 rect
.y
= GetRowTop(row
);
6105 rect
.width
= GetColWidth(col
);
6106 rect
.height
= GetRowHeight(row
);
6113 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6115 // get the cell rectangle in logical coords
6117 wxRect
r( CellToRect( row
, col
) );
6119 // convert to device coords
6121 int left
, top
, right
, bottom
;
6122 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6123 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6125 // check against the client area of the grid window
6128 m_gridWin
->GetClientSize( &cw
, &ch
);
6130 if ( wholeCellVisible
)
6132 // is the cell wholly visible ?
6134 return ( left
>= 0 && right
<= cw
&&
6135 top
>= 0 && bottom
<= ch
);
6139 // is the cell partly visible ?
6141 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6142 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6147 // make the specified cell location visible by doing a minimal amount
6150 void wxGrid::MakeCellVisible( int row
, int col
)
6153 int xpos
= -1, ypos
= -1;
6155 if ( row
>= 0 && row
< m_numRows
&&
6156 col
>= 0 && col
< m_numCols
)
6158 // get the cell rectangle in logical coords
6160 wxRect
r( CellToRect( row
, col
) );
6162 // convert to device coords
6164 int left
, top
, right
, bottom
;
6165 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6166 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6169 m_gridWin
->GetClientSize( &cw
, &ch
);
6175 else if ( bottom
> ch
)
6177 int h
= r
.GetHeight();
6179 for ( i
= row
-1; i
>= 0; i
-- )
6181 int rowHeight
= GetRowHeight(i
);
6182 if ( h
+ rowHeight
> ch
)
6189 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6190 // have rounding errors (this is important, because if we do, we
6191 // might not scroll at all and some cells won't be redrawn)
6192 ypos
+= GRID_SCROLL_LINE
/ 2;
6199 else if ( right
> cw
)
6201 int w
= r
.GetWidth();
6203 for ( i
= col
-1; i
>= 0; i
-- )
6205 int colWidth
= GetColWidth(i
);
6206 if ( w
+ colWidth
> cw
)
6213 // see comment for ypos above
6214 xpos
+= GRID_SCROLL_LINE
/ 2;
6217 if ( xpos
!= -1 || ypos
!= -1 )
6219 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6220 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6221 Scroll( xpos
, ypos
);
6229 // ------ Grid cursor movement functions
6232 bool wxGrid::MoveCursorUp( bool expandSelection
)
6234 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6235 m_currentCellCoords
.GetRow() > 0 )
6237 if ( expandSelection
)
6239 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6240 m_selectingKeyboard
= m_currentCellCoords
;
6241 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6242 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6243 m_selectingKeyboard
.GetCol() );
6244 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6249 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6250 m_currentCellCoords
.GetCol() );
6251 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6252 m_currentCellCoords
.GetCol() );
6261 bool wxGrid::MoveCursorDown( bool expandSelection
)
6263 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6264 m_currentCellCoords
.GetRow() < m_numRows
-1 )
6266 if ( expandSelection
)
6268 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6269 m_selectingKeyboard
= m_currentCellCoords
;
6270 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
6271 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6272 m_selectingKeyboard
.GetCol() );
6273 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6278 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
6279 m_currentCellCoords
.GetCol() );
6280 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
6281 m_currentCellCoords
.GetCol() );
6290 bool wxGrid::MoveCursorLeft( bool expandSelection
)
6292 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6293 m_currentCellCoords
.GetCol() > 0 )
6295 if ( expandSelection
)
6297 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6298 m_selectingKeyboard
= m_currentCellCoords
;
6299 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
6300 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6301 m_selectingKeyboard
.GetCol() );
6302 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6307 MakeCellVisible( m_currentCellCoords
.GetRow(),
6308 m_currentCellCoords
.GetCol() - 1 );
6309 SetCurrentCell( m_currentCellCoords
.GetRow(),
6310 m_currentCellCoords
.GetCol() - 1 );
6319 bool wxGrid::MoveCursorRight( bool expandSelection
)
6321 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6322 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
6324 if ( expandSelection
)
6326 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6327 m_selectingKeyboard
= m_currentCellCoords
;
6328 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
6329 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6330 m_selectingKeyboard
.GetCol() );
6331 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6336 MakeCellVisible( m_currentCellCoords
.GetRow(),
6337 m_currentCellCoords
.GetCol() + 1 );
6338 SetCurrentCell( m_currentCellCoords
.GetRow(),
6339 m_currentCellCoords
.GetCol() + 1 );
6348 bool wxGrid::MovePageUp()
6350 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6352 int row
= m_currentCellCoords
.GetRow();
6356 m_gridWin
->GetClientSize( &cw
, &ch
);
6358 int y
= GetRowTop(row
);
6359 int newRow
= YToRow( y
- ch
+ 1 );
6364 else if ( newRow
== row
)
6369 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6370 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6378 bool wxGrid::MovePageDown()
6380 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6382 int row
= m_currentCellCoords
.GetRow();
6383 if ( row
< m_numRows
)
6386 m_gridWin
->GetClientSize( &cw
, &ch
);
6388 int y
= GetRowTop(row
);
6389 int newRow
= YToRow( y
+ ch
);
6392 newRow
= m_numRows
- 1;
6394 else if ( newRow
== row
)
6399 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6400 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6408 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
6411 m_currentCellCoords
!= wxGridNoCellCoords
&&
6412 m_currentCellCoords
.GetRow() > 0 )
6414 int row
= m_currentCellCoords
.GetRow();
6415 int col
= m_currentCellCoords
.GetCol();
6417 if ( m_table
->IsEmptyCell(row
, col
) )
6419 // starting in an empty cell: find the next block of
6425 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6428 else if ( m_table
->IsEmptyCell(row
-1, col
) )
6430 // starting at the top of a block: find the next block
6436 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6441 // starting within a block: find the top of the block
6446 if ( m_table
->IsEmptyCell(row
, col
) )
6454 MakeCellVisible( row
, col
);
6455 if ( expandSelection
)
6457 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6458 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6463 SetCurrentCell( row
, col
);
6471 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6474 m_currentCellCoords
!= wxGridNoCellCoords
&&
6475 m_currentCellCoords
.GetRow() < m_numRows
-1 )
6477 int row
= m_currentCellCoords
.GetRow();
6478 int col
= m_currentCellCoords
.GetCol();
6480 if ( m_table
->IsEmptyCell(row
, col
) )
6482 // starting in an empty cell: find the next block of
6485 while ( row
< m_numRows
-1 )
6488 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6491 else if ( m_table
->IsEmptyCell(row
+1, col
) )
6493 // starting at the bottom of a block: find the next block
6496 while ( row
< m_numRows
-1 )
6499 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6504 // starting within a block: find the bottom of the block
6506 while ( row
< m_numRows
-1 )
6509 if ( m_table
->IsEmptyCell(row
, col
) )
6517 MakeCellVisible( row
, col
);
6518 if ( expandSelection
)
6520 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6521 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6526 SetCurrentCell( row
, col
);
6535 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6538 m_currentCellCoords
!= wxGridNoCellCoords
&&
6539 m_currentCellCoords
.GetCol() > 0 )
6541 int row
= m_currentCellCoords
.GetRow();
6542 int col
= m_currentCellCoords
.GetCol();
6544 if ( m_table
->IsEmptyCell(row
, col
) )
6546 // starting in an empty cell: find the next block of
6552 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6555 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6557 // starting at the left of a block: find the next block
6563 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6568 // starting within a block: find the left of the block
6573 if ( m_table
->IsEmptyCell(row
, col
) )
6581 MakeCellVisible( row
, col
);
6582 if ( expandSelection
)
6584 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6585 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6590 SetCurrentCell( row
, col
);
6599 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6602 m_currentCellCoords
!= wxGridNoCellCoords
&&
6603 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6605 int row
= m_currentCellCoords
.GetRow();
6606 int col
= m_currentCellCoords
.GetCol();
6608 if ( m_table
->IsEmptyCell(row
, col
) )
6610 // starting in an empty cell: find the next block of
6613 while ( col
< m_numCols
-1 )
6616 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6619 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6621 // starting at the right of a block: find the next block
6624 while ( col
< m_numCols
-1 )
6627 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6632 // starting within a block: find the right of the block
6634 while ( col
< m_numCols
-1 )
6637 if ( m_table
->IsEmptyCell(row
, col
) )
6645 MakeCellVisible( row
, col
);
6646 if ( expandSelection
)
6648 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6649 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6654 SetCurrentCell( row
, col
);
6666 // ------ Label values and formatting
6669 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6671 *horiz
= m_rowLabelHorizAlign
;
6672 *vert
= m_rowLabelVertAlign
;
6675 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6677 *horiz
= m_colLabelHorizAlign
;
6678 *vert
= m_colLabelVertAlign
;
6681 wxString
wxGrid::GetRowLabelValue( int row
)
6685 return m_table
->GetRowLabelValue( row
);
6695 wxString
wxGrid::GetColLabelValue( int col
)
6699 return m_table
->GetColLabelValue( col
);
6710 void wxGrid::SetRowLabelSize( int width
)
6712 width
= wxMax( width
, 0 );
6713 if ( width
!= m_rowLabelWidth
)
6717 m_rowLabelWin
->Show( FALSE
);
6718 m_cornerLabelWin
->Show( FALSE
);
6720 else if ( m_rowLabelWidth
== 0 )
6722 m_rowLabelWin
->Show( TRUE
);
6723 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6726 m_rowLabelWidth
= width
;
6733 void wxGrid::SetColLabelSize( int height
)
6735 height
= wxMax( height
, 0 );
6736 if ( height
!= m_colLabelHeight
)
6740 m_colLabelWin
->Show( FALSE
);
6741 m_cornerLabelWin
->Show( FALSE
);
6743 else if ( m_colLabelHeight
== 0 )
6745 m_colLabelWin
->Show( TRUE
);
6746 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6749 m_colLabelHeight
= height
;
6756 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6758 if ( m_labelBackgroundColour
!= colour
)
6760 m_labelBackgroundColour
= colour
;
6761 m_rowLabelWin
->SetBackgroundColour( colour
);
6762 m_colLabelWin
->SetBackgroundColour( colour
);
6763 m_cornerLabelWin
->SetBackgroundColour( colour
);
6765 if ( !GetBatchCount() )
6767 m_rowLabelWin
->Refresh();
6768 m_colLabelWin
->Refresh();
6769 m_cornerLabelWin
->Refresh();
6774 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6776 if ( m_labelTextColour
!= colour
)
6778 m_labelTextColour
= colour
;
6779 if ( !GetBatchCount() )
6781 m_rowLabelWin
->Refresh();
6782 m_colLabelWin
->Refresh();
6787 void wxGrid::SetLabelFont( const wxFont
& font
)
6790 if ( !GetBatchCount() )
6792 m_rowLabelWin
->Refresh();
6793 m_colLabelWin
->Refresh();
6797 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6799 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6801 m_rowLabelHorizAlign
= horiz
;
6804 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6806 m_rowLabelVertAlign
= vert
;
6809 if ( !GetBatchCount() )
6811 m_rowLabelWin
->Refresh();
6815 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6817 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6819 m_colLabelHorizAlign
= horiz
;
6822 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6824 m_colLabelVertAlign
= vert
;
6827 if ( !GetBatchCount() )
6829 m_colLabelWin
->Refresh();
6833 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6837 m_table
->SetRowLabelValue( row
, s
);
6838 if ( !GetBatchCount() )
6840 wxRect rect
= CellToRect( row
, 0);
6841 if ( rect
.height
> 0 )
6843 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6845 rect
.width
= m_rowLabelWidth
;
6846 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6852 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6856 m_table
->SetColLabelValue( col
, s
);
6857 if ( !GetBatchCount() )
6859 wxRect rect
= CellToRect( 0, col
);
6860 if ( rect
.width
> 0 )
6862 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6864 rect
.height
= m_colLabelHeight
;
6865 m_colLabelWin
->Refresh( TRUE
, &rect
);
6871 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6873 if ( m_gridLineColour
!= colour
)
6875 m_gridLineColour
= colour
;
6877 wxClientDC
dc( m_gridWin
);
6879 DrawAllGridLines( dc
, wxRegion() );
6883 void wxGrid::EnableGridLines( bool enable
)
6885 if ( enable
!= m_gridLinesEnabled
)
6887 m_gridLinesEnabled
= enable
;
6889 if ( !GetBatchCount() )
6893 wxClientDC
dc( m_gridWin
);
6895 DrawAllGridLines( dc
, wxRegion() );
6899 m_gridWin
->Refresh();
6906 int wxGrid::GetDefaultRowSize()
6908 return m_defaultRowHeight
;
6911 int wxGrid::GetRowSize( int row
)
6913 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6915 return GetRowHeight(row
);
6918 int wxGrid::GetDefaultColSize()
6920 return m_defaultColWidth
;
6923 int wxGrid::GetColSize( int col
)
6925 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6927 return GetColWidth(col
);
6930 // ============================================================================
6931 // access to the grid attributes: each of them has a default value in the grid
6932 // itself and may be overidden on a per-cell basis
6933 // ============================================================================
6935 // ----------------------------------------------------------------------------
6936 // setting default attributes
6937 // ----------------------------------------------------------------------------
6939 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6941 m_defaultCellAttr
->SetBackgroundColour(col
);
6943 m_gridWin
->SetBackgroundColour(col
);
6947 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6949 m_defaultCellAttr
->SetTextColour(col
);
6952 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6954 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6957 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6959 m_defaultCellAttr
->SetFont(font
);
6962 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6964 m_defaultCellAttr
->SetRenderer(renderer
);
6967 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6969 m_defaultCellAttr
->SetEditor(editor
);
6972 // ----------------------------------------------------------------------------
6973 // access to the default attrbiutes
6974 // ----------------------------------------------------------------------------
6976 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6978 return m_defaultCellAttr
->GetBackgroundColour();
6981 wxColour
wxGrid::GetDefaultCellTextColour()
6983 return m_defaultCellAttr
->GetTextColour();
6986 wxFont
wxGrid::GetDefaultCellFont()
6988 return m_defaultCellAttr
->GetFont();
6991 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6993 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6996 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6998 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7001 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7003 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7006 // ----------------------------------------------------------------------------
7007 // access to cell attributes
7008 // ----------------------------------------------------------------------------
7010 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7012 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7013 wxColour colour
= attr
->GetBackgroundColour();
7018 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7020 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7021 wxColour colour
= attr
->GetTextColour();
7026 wxFont
wxGrid::GetCellFont( int row
, int col
)
7028 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7029 wxFont font
= attr
->GetFont();
7034 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7036 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7037 attr
->GetAlignment(horiz
, vert
);
7041 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7043 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7044 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7050 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7052 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7053 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7059 bool wxGrid::IsReadOnly(int row
, int col
) const
7061 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7062 bool isReadOnly
= attr
->IsReadOnly();
7067 // ----------------------------------------------------------------------------
7068 // attribute support: cache, automatic provider creation, ...
7069 // ----------------------------------------------------------------------------
7071 bool wxGrid::CanHaveAttributes()
7078 return m_table
->CanHaveAttributes();
7081 void wxGrid::ClearAttrCache()
7083 if ( m_attrCache
.row
!= -1 )
7085 wxSafeDecRef(m_attrCache
.attr
);
7086 m_attrCache
.row
= -1;
7090 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7092 wxGrid
*self
= (wxGrid
*)this; // const_cast
7094 self
->ClearAttrCache();
7095 self
->m_attrCache
.row
= row
;
7096 self
->m_attrCache
.col
= col
;
7097 self
->m_attrCache
.attr
= attr
;
7101 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7103 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7105 *attr
= m_attrCache
.attr
;
7106 wxSafeIncRef(m_attrCache
.attr
);
7108 #ifdef DEBUG_ATTR_CACHE
7109 gs_nAttrCacheHits
++;
7116 #ifdef DEBUG_ATTR_CACHE
7117 gs_nAttrCacheMisses
++;
7123 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7125 wxGridCellAttr
*attr
;
7126 if ( !LookupAttr(row
, col
, &attr
) )
7128 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
7129 CacheAttr(row
, col
, attr
);
7133 attr
->SetDefAttr(m_defaultCellAttr
);
7137 attr
= m_defaultCellAttr
;
7144 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7146 wxGridCellAttr
*attr
;
7147 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
7149 wxASSERT_MSG( m_table
,
7150 _T("we may only be called if CanHaveAttributes() "
7151 "returned TRUE and then m_table should be !NULL") );
7153 attr
= m_table
->GetAttr(row
, col
);
7156 attr
= new wxGridCellAttr
;
7158 // artificially inc the ref count to match DecRef() in caller
7161 m_table
->SetAttr(attr
, row
, col
);
7164 CacheAttr(row
, col
, attr
);
7166 attr
->SetDefAttr(m_defaultCellAttr
);
7170 // ----------------------------------------------------------------------------
7171 // setting column attributes (wrappers around SetColAttr)
7172 // ----------------------------------------------------------------------------
7174 void wxGrid::SetColFormatBool(int col
)
7176 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7179 void wxGrid::SetColFormatNumber(int col
)
7181 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7184 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7186 wxString typeName
= wxGRID_VALUE_FLOAT
;
7187 if ( (width
!= -1) || (precision
!= -1) )
7189 typeName
<< _T(':') << width
<< _T(',') << precision
;
7192 SetColFormatCustom(col
, typeName
);
7195 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7197 wxGridCellAttr
*attr
= new wxGridCellAttr
;
7198 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7199 attr
->SetRenderer(renderer
);
7201 SetColAttr(col
, attr
);
7204 // ----------------------------------------------------------------------------
7205 // setting cell attributes: this is forwarded to the table
7206 // ----------------------------------------------------------------------------
7208 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7210 if ( CanHaveAttributes() )
7212 m_table
->SetRowAttr(attr
, row
);
7220 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7222 if ( CanHaveAttributes() )
7224 m_table
->SetColAttr(attr
, col
);
7232 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7234 if ( CanHaveAttributes() )
7236 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7237 attr
->SetBackgroundColour(colour
);
7242 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7244 if ( CanHaveAttributes() )
7246 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7247 attr
->SetTextColour(colour
);
7252 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7254 if ( CanHaveAttributes() )
7256 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7257 attr
->SetFont(font
);
7262 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7264 if ( CanHaveAttributes() )
7266 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7267 attr
->SetAlignment(horiz
, vert
);
7272 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7274 if ( CanHaveAttributes() )
7276 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7277 attr
->SetRenderer(renderer
);
7282 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7284 if ( CanHaveAttributes() )
7286 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7287 attr
->SetEditor(editor
);
7292 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7294 if ( CanHaveAttributes() )
7296 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7297 attr
->SetReadOnly(isReadOnly
);
7302 // ----------------------------------------------------------------------------
7303 // Data type registration
7304 // ----------------------------------------------------------------------------
7306 void wxGrid::RegisterDataType(const wxString
& typeName
,
7307 wxGridCellRenderer
* renderer
,
7308 wxGridCellEditor
* editor
)
7310 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7314 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7316 wxString typeName
= m_table
->GetTypeName(row
, col
);
7317 return GetDefaultEditorForType(typeName
);
7320 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7322 wxString typeName
= m_table
->GetTypeName(row
, col
);
7323 return GetDefaultRendererForType(typeName
);
7327 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7329 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7330 if ( index
== wxNOT_FOUND
)
7332 wxFAIL_MSG(wxT("Unknown data type name"));
7337 return m_typeRegistry
->GetEditor(index
);
7341 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7343 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7344 if ( index
== wxNOT_FOUND
)
7346 wxFAIL_MSG(wxT("Unknown data type name"));
7351 return m_typeRegistry
->GetRenderer(index
);
7355 // ----------------------------------------------------------------------------
7357 // ----------------------------------------------------------------------------
7359 void wxGrid::EnableDragRowSize( bool enable
)
7361 m_canDragRowSize
= enable
;
7365 void wxGrid::EnableDragColSize( bool enable
)
7367 m_canDragColSize
= enable
;
7370 void wxGrid::EnableDragGridSize( bool enable
)
7372 m_canDragGridSize
= enable
;
7376 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7378 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
7380 if ( resizeExistingRows
)
7388 void wxGrid::SetRowSize( int row
, int height
)
7390 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
7392 if ( m_rowHeights
.IsEmpty() )
7394 // need to really create the array
7398 int h
= wxMax( 0, height
);
7399 int diff
= h
- m_rowHeights
[row
];
7401 m_rowHeights
[row
] = h
;
7403 for ( i
= row
; i
< m_numRows
; i
++ )
7405 m_rowBottoms
[i
] += diff
;
7410 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7412 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
7414 if ( resizeExistingCols
)
7422 void wxGrid::SetColSize( int col
, int width
)
7424 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
7426 // should we check that it's bigger than GetColMinimalWidth(col) here?
7428 if ( m_colWidths
.IsEmpty() )
7430 // need to really create the array
7434 int w
= wxMax( 0, width
);
7435 int diff
= w
- m_colWidths
[col
];
7436 m_colWidths
[col
] = w
;
7439 for ( i
= col
; i
< m_numCols
; i
++ )
7441 m_colRights
[i
] += diff
;
7447 void wxGrid::SetColMinimalWidth( int col
, int width
)
7449 m_colMinWidths
.Put(col
, width
);
7452 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7454 m_rowMinHeights
.Put(row
, width
);
7457 int wxGrid::GetColMinimalWidth(int col
) const
7459 long value
= m_colMinWidths
.Get(col
);
7460 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
7463 int wxGrid::GetRowMinimalHeight(int row
) const
7465 long value
= m_rowMinHeights
.Get(row
);
7466 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
7469 // ----------------------------------------------------------------------------
7471 // ----------------------------------------------------------------------------
7473 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
7475 wxClientDC
dc(m_gridWin
);
7477 // init both of them to avoid compiler warnings, even if weo nly need one
7485 wxCoord extent
, extentMax
= 0;
7486 int max
= column
? m_numRows
: m_numCols
;
7487 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7494 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7495 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7498 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7499 extent
= column
? size
.x
: size
.y
;
7500 if ( extent
> extentMax
)
7511 // now also compare with the column label extent
7513 dc
.SetFont( GetLabelFont() );
7516 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
7518 dc
.GetTextExtent( GetRowLabelValue(col
), &w
, &h
);
7520 extent
= column
? w
: h
;
7521 if ( extent
> extentMax
)
7528 // empty column - give default extent (notice that if extentMax is less
7529 // than default extent but != 0, it's ok)
7530 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7536 // leave some space around text
7542 SetColSize(col
, extentMax
);
7544 SetRowSize(row
, extentMax
);
7549 SetColMinimalWidth(col
, extentMax
);
7551 SetRowMinimalHeight(row
, extentMax
);
7555 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
7557 int width
= m_rowLabelWidth
;
7559 for ( int col
= 0; col
< m_numCols
; col
++ )
7563 AutoSizeColumn(col
, setAsMin
);
7566 width
+= GetColWidth(col
);
7572 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7574 int height
= m_colLabelHeight
;
7576 for ( int row
= 0; row
< m_numRows
; row
++ )
7580 AutoSizeRow(row
, setAsMin
);
7583 height
+= GetRowHeight(row
);
7589 void wxGrid::AutoSize()
7592 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
7595 wxSize
wxGrid::DoGetBestSize() const
7597 // don't set sizes, only calculate them
7598 wxGrid
*self
= (wxGrid
*)this; // const_cast
7600 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
7601 self
->SetOrCalcRowSizes(TRUE
));
7610 wxPen
& wxGrid::GetDividerPen() const
7615 // ----------------------------------------------------------------------------
7616 // cell value accessor functions
7617 // ----------------------------------------------------------------------------
7619 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
7623 m_table
->SetValue( row
, col
, s
.c_str() );
7624 if ( !GetBatchCount() )
7626 wxClientDC
dc( m_gridWin
);
7628 DrawCell( dc
, wxGridCellCoords(row
, col
) );
7631 if ( m_currentCellCoords
.GetRow() == row
&&
7632 m_currentCellCoords
.GetCol() == col
&&
7633 IsCellEditControlEnabled())
7635 HideCellEditControl();
7636 ShowCellEditControl(); // will reread data from table
7643 // ------ Block, row and col selection
7646 void wxGrid::SelectRow( int row
, bool addToSelected
)
7648 if ( IsSelection() && !addToSelected
)
7651 m_selection
->SelectRow( row
);
7655 void wxGrid::SelectCol( int col
, bool addToSelected
)
7657 if ( IsSelection() && !addToSelected
)
7660 m_selection
->SelectCol( col
);
7664 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7667 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7669 if ( topRow
> bottomRow
)
7676 if ( leftCol
> rightCol
)
7683 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7684 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7686 if ( m_selectingTopLeft
!= updateTopLeft
||
7687 m_selectingBottomRight
!= updateBottomRight
)
7689 // Compute two optimal update rectangles:
7690 // Either one rectangle is a real subset of the
7691 // other, or they are (almost) disjoint!
7693 bool need_refresh
[4];
7697 need_refresh
[3] = FALSE
;
7700 // Store intermediate values
7701 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
7702 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
7703 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
7704 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
7706 // Determine the outer/inner coordinates.
7707 if (oldLeft
> leftCol
)
7713 if (oldTop
> topRow
)
7719 if (oldRight
< rightCol
)
7722 oldRight
= rightCol
;
7725 if (oldBottom
< bottomRow
)
7728 oldBottom
= bottomRow
;
7732 // Now, either the stuff marked old is the outer
7733 // rectangle or we don't have a situation where one
7734 // is contained in the other.
7736 if ( oldLeft
< leftCol
)
7738 need_refresh
[0] = TRUE
;
7739 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7741 wxGridCellCoords ( oldBottom
,
7745 if ( oldTop
< topRow
)
7747 need_refresh
[1] = TRUE
;
7748 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7750 wxGridCellCoords ( topRow
- 1,
7754 if ( oldRight
> rightCol
)
7756 need_refresh
[2] = TRUE
;
7757 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7759 wxGridCellCoords ( oldBottom
,
7763 if ( oldBottom
> bottomRow
)
7765 need_refresh
[3] = TRUE
;
7766 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7768 wxGridCellCoords ( oldBottom
,
7774 m_selectingTopLeft
= updateTopLeft
;
7775 m_selectingBottomRight
= updateBottomRight
;
7777 // various Refresh() calls
7778 for (i
= 0; i
< 4; i
++ )
7779 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7780 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7783 // never generate an event as it will be generated from
7784 // wxGridSelection::SelectBlock!
7787 void wxGrid::SelectAll()
7789 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
7792 bool wxGrid::IsSelection()
7794 return ( m_selection
->IsSelection() ||
7795 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
7796 m_selectingBottomRight
!= wxGridNoCellCoords
) );
7799 bool wxGrid::IsInSelection( int row
, int col
)
7801 return ( m_selection
->IsInSelection( row
, col
) ||
7802 ( row
>= m_selectingTopLeft
.GetRow() &&
7803 col
>= m_selectingTopLeft
.GetCol() &&
7804 row
<= m_selectingBottomRight
.GetRow() &&
7805 col
<= m_selectingBottomRight
.GetCol() ) );
7808 void wxGrid::ClearSelection()
7810 m_selectingTopLeft
= wxGridNoCellCoords
;
7811 m_selectingBottomRight
= wxGridNoCellCoords
;
7812 m_selection
->ClearSelection();
7816 // This function returns the rectangle that encloses the given block
7817 // in device coords clipped to the client size of the grid window.
7819 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7820 const wxGridCellCoords
&bottomRight
)
7822 wxRect
rect( wxGridNoCellRect
);
7825 cellRect
= CellToRect( topLeft
);
7826 if ( cellRect
!= wxGridNoCellRect
)
7832 rect
= wxRect( 0, 0, 0, 0 );
7835 cellRect
= CellToRect( bottomRight
);
7836 if ( cellRect
!= wxGridNoCellRect
)
7842 return wxGridNoCellRect
;
7845 // convert to scrolled coords
7847 int left
, top
, right
, bottom
;
7848 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7849 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7852 m_gridWin
->GetClientSize( &cw
, &ch
);
7854 rect
.SetLeft( wxMax(0, left
) );
7855 rect
.SetTop( wxMax(0, top
) );
7856 rect
.SetRight( wxMin(cw
, right
) );
7857 rect
.SetBottom( wxMin(ch
, bottom
) );
7865 // ------ Grid event classes
7868 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7870 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7871 int row
, int col
, int x
, int y
, bool sel
,
7872 bool control
, bool shift
, bool alt
, bool meta
)
7873 : wxNotifyEvent( type
, id
)
7880 m_control
= control
;
7885 SetEventObject(obj
);
7889 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7891 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7892 int rowOrCol
, int x
, int y
,
7893 bool control
, bool shift
, bool alt
, bool meta
)
7894 : wxNotifyEvent( type
, id
)
7896 m_rowOrCol
= rowOrCol
;
7899 m_control
= control
;
7904 SetEventObject(obj
);
7908 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7910 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7911 const wxGridCellCoords
& topLeft
,
7912 const wxGridCellCoords
& bottomRight
,
7913 bool sel
, bool control
,
7914 bool shift
, bool alt
, bool meta
)
7915 : wxNotifyEvent( type
, id
)
7917 m_topLeft
= topLeft
;
7918 m_bottomRight
= bottomRight
;
7920 m_control
= control
;
7925 SetEventObject(obj
);
7929 #endif // ifndef wxUSE_NEW_GRID