1 ///////////////////////////////////////////////////////////////////////////
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"
44 // this include needs to be outside precomp for BCC
45 #include "wx/textfile.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
56 struct wxGridCellWithAttr
58 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
59 : coords(row
, col
), attr(attr_
)
68 wxGridCellCoords coords
;
72 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
74 #include "wx/arrimpl.cpp"
76 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
77 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
86 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
87 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
88 const wxPoint
&pos
, const wxSize
&size
);
93 void OnPaint( wxPaintEvent
& event
);
94 void OnMouseEvent( wxMouseEvent
& event
);
95 void OnKeyDown( wxKeyEvent
& event
);
97 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
102 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
105 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
106 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
107 const wxPoint
&pos
, const wxSize
&size
);
112 void OnPaint( wxPaintEvent
&event
);
113 void OnMouseEvent( wxMouseEvent
& event
);
114 void OnKeyDown( wxKeyEvent
& event
);
116 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
117 DECLARE_EVENT_TABLE()
121 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
124 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
125 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
126 const wxPoint
&pos
, const wxSize
&size
);
131 void OnMouseEvent( wxMouseEvent
& event
);
132 void OnKeyDown( wxKeyEvent
& event
);
133 void OnPaint( wxPaintEvent
& event
);
135 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
136 DECLARE_EVENT_TABLE()
139 class WXDLLEXPORT wxGridWindow
: public wxPanel
144 m_owner
= (wxGrid
*)NULL
;
145 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
146 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
149 wxGridWindow( wxGrid
*parent
,
150 wxGridRowLabelWindow
*rowLblWin
,
151 wxGridColLabelWindow
*colLblWin
,
152 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
155 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
159 wxGridRowLabelWindow
*m_rowLabelWin
;
160 wxGridColLabelWindow
*m_colLabelWin
;
162 void OnPaint( wxPaintEvent
&event
);
163 void OnMouseEvent( wxMouseEvent
& event
);
164 void OnKeyDown( wxKeyEvent
& );
165 void OnEraseBackground( wxEraseEvent
& );
168 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
169 DECLARE_EVENT_TABLE()
174 class wxGridCellEditorEvtHandler
: public wxEvtHandler
177 wxGridCellEditorEvtHandler()
178 : m_grid(0), m_editor(0)
180 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
181 : m_grid(grid
), m_editor(editor
)
184 void OnKeyDown(wxKeyEvent
& event
);
185 void OnChar(wxKeyEvent
& event
);
189 wxGridCellEditor
* m_editor
;
190 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
191 DECLARE_EVENT_TABLE()
195 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
196 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
197 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
198 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
203 // ----------------------------------------------------------------------------
204 // the internal data representation used by wxGridCellAttrProvider
205 // ----------------------------------------------------------------------------
207 // this class stores attributes set for cells
208 class WXDLLEXPORT wxGridCellAttrData
211 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
212 wxGridCellAttr
*GetAttr(int row
, int col
) const;
213 void UpdateAttrRows( size_t pos
, int numRows
);
214 void UpdateAttrCols( size_t pos
, int numCols
);
217 // searches for the attr for given cell, returns wxNOT_FOUND if not found
218 int FindIndex(int row
, int col
) const;
220 wxGridCellWithAttrArray m_attrs
;
223 // this class stores attributes set for rows or columns
224 class WXDLLEXPORT wxGridRowOrColAttrData
227 ~wxGridRowOrColAttrData();
229 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
230 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
231 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
234 wxArrayInt m_rowsOrCols
;
235 wxArrayAttrs m_attrs
;
238 // NB: this is just a wrapper around 3 objects: one which stores cell
239 // attributes, and 2 others for row/col ones
240 class WXDLLEXPORT wxGridCellAttrProviderData
243 wxGridCellAttrData m_cellAttrs
;
244 wxGridRowOrColAttrData m_rowAttrs
,
248 // ----------------------------------------------------------------------------
249 // conditional compilation
250 // ----------------------------------------------------------------------------
252 #ifndef WXGRID_DRAW_LINES
253 #define WXGRID_DRAW_LINES 1
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 //#define DEBUG_ATTR_CACHE
261 #ifdef DEBUG_ATTR_CACHE
262 static size_t gs_nAttrCacheHits
= 0;
263 static size_t gs_nAttrCacheMisses
= 0;
264 #endif // DEBUG_ATTR_CACHE
266 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
267 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
270 // TODO: fixed so far - make configurable later (and also different for x/y)
271 static const size_t GRID_SCROLL_LINE
= 10;
273 // ============================================================================
275 // ============================================================================
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 wxGridCellEditor::wxGridCellEditor()
288 wxGridCellEditor::~wxGridCellEditor()
294 void wxGridCellEditor::Destroy()
297 m_control
->Destroy();
302 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
304 wxASSERT_MSG(m_control
,
305 wxT("The wxGridCellEditor must be Created first!"));
306 m_control
->Show(show
);
310 // set the colours/fonts if we have any
313 if ( attr
->HasTextColour() )
315 m_colFgOld
= m_control
->GetForegroundColour();
316 m_control
->SetForegroundColour(attr
->GetTextColour());
319 if ( attr
->HasBackgroundColour() )
321 m_colBgOld
= m_control
->GetBackgroundColour();
322 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
325 if ( attr
->HasFont() )
327 m_fontOld
= m_control
->GetFont();
328 m_control
->SetFont(attr
->GetFont());
331 // can't do anything more in the base class version, the other
332 // attributes may only be used by the derived classes
337 // restore the standard colours fonts
338 if ( m_colFgOld
.Ok() )
340 m_control
->SetForegroundColour(m_colFgOld
);
341 m_colFgOld
= wxNullColour
;
344 if ( m_colBgOld
.Ok() )
346 m_control
->SetBackgroundColour(m_colBgOld
);
347 m_colBgOld
= wxNullColour
;
350 if ( m_fontOld
.Ok() )
352 m_control
->SetFont(m_fontOld
);
353 m_fontOld
= wxNullFont
;
358 void wxGridCellEditor::SetSize(const wxRect
& rect
)
360 wxASSERT_MSG(m_control
,
361 wxT("The wxGridCellEditor must be Created first!"));
362 m_control
->SetSize(rect
);
365 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
371 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
377 wxGridCellTextEditor::wxGridCellTextEditor()
381 void wxGridCellTextEditor::Create(wxWindow
* parent
,
383 wxEvtHandler
* evtHandler
)
385 m_control
= new wxTextCtrl(parent
, -1, "",
386 wxDefaultPosition
, wxDefaultSize
387 #if defined(__WXMSW__)
388 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
393 m_control
->PushEventHandler(evtHandler
);
397 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
399 wxASSERT_MSG(m_control
,
400 wxT("The wxGridCellEditor must be Created first!"));
402 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
403 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
404 ((wxTextCtrl
*)m_control
)->SetInsertionPointEnd();
405 ((wxTextCtrl
*)m_control
)->SetFocus();
410 bool wxGridCellTextEditor::EndEdit(int row
, int col
, bool saveValue
,
413 wxASSERT_MSG(m_control
,
414 wxT("The wxGridCellEditor must be Created first!"));
416 bool changed
= FALSE
;
417 wxString value
= ((wxTextCtrl
*)m_control
)->GetValue();
418 if (value
!= m_startValue
)
422 grid
->GetTable()->SetValue(row
, col
, value
);
424 m_startValue
= wxEmptyString
;
425 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
431 void wxGridCellTextEditor::Reset()
433 wxASSERT_MSG(m_control
,
434 wxT("The wxGridCellEditor must be Created first!"));
436 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
437 ((wxTextCtrl
*)m_control
)->SetInsertionPointEnd();
441 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
443 wxASSERT_MSG(m_control
,
444 wxT("The wxGridCellEditor must be Created first!"));
446 int code
= event
.KeyCode();
447 if (code
>= 32 && code
< 255) {
448 wxString
st((char)code
);
449 if (! event
.ShiftDown())
451 ((wxTextCtrl
*)m_control
)->AppendText(st
);
456 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
458 #if defined(__WXMOTIF__) || defined(__WXGTK__)
459 // wxMotif needs a little extra help...
460 int pos
= ((wxTextCtrl
*)m_control
)->GetInsertionPoint();
461 wxString
s( ((wxTextCtrl
*)m_control
)->GetValue() );
462 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
463 ((wxTextCtrl
*)m_control
)->SetValue(s
);
464 ((wxTextCtrl
*)m_control
)->SetInsertionPoint( pos
);
466 // the other ports can handle a Return key press
473 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
475 switch ( event
.KeyCode() )
479 m_grid
->EnableCellEditControl(FALSE
);
483 event
.Skip( m_grid
->ProcessEvent( event
) );
487 if (!m_grid
->ProcessEvent(event
))
488 m_editor
->HandleReturn(event
);
497 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
499 switch ( event
.KeyCode() )
511 // ----------------------------------------------------------------------------
512 // wxGridCellRenderer
513 // ----------------------------------------------------------------------------
515 void wxGridCellRenderer::Draw(wxGrid
& grid
,
516 wxGridCellAttr
& attr
,
522 dc
.SetBackgroundMode( wxSOLID
);
526 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
530 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
533 dc
.SetPen( *wxTRANSPARENT_PEN
);
534 dc
.DrawRectangle(rect
);
537 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
538 wxGridCellAttr
& attr
,
540 const wxRect
& rectCell
,
544 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
546 // now we only have to draw the text
547 dc
.SetBackgroundMode( wxTRANSPARENT
);
549 // TODO some special colours for attr.IsReadOnly() case?
553 dc
.SetTextBackground( grid
.GetSelectionBackground() );
554 dc
.SetTextForeground( grid
.GetSelectionForeground() );
558 dc
.SetTextBackground( attr
.GetBackgroundColour() );
559 dc
.SetTextForeground( attr
.GetTextColour() );
561 dc
.SetFont( attr
.GetFont() );
564 attr
.GetAlignment(&hAlign
, &vAlign
);
566 wxRect rect
= rectCell
;
572 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
573 rect
, hAlign
, vAlign
);
576 // ----------------------------------------------------------------------------
578 // ----------------------------------------------------------------------------
580 const wxColour
& wxGridCellAttr::GetTextColour() const
584 else if (m_defGridAttr
!= this)
585 return m_defGridAttr
->GetTextColour();
587 wxFAIL_MSG(wxT("Missing default cell attribute"));
593 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
595 if (HasBackgroundColour())
597 else if (m_defGridAttr
!= this)
598 return m_defGridAttr
->GetBackgroundColour();
600 wxFAIL_MSG(wxT("Missing default cell attribute"));
606 const wxFont
& wxGridCellAttr::GetFont() const
610 else if (m_defGridAttr
!= this)
611 return m_defGridAttr
->GetFont();
613 wxFAIL_MSG(wxT("Missing default cell attribute"));
619 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
621 if (HasAlignment()) {
622 if ( hAlign
) *hAlign
= m_hAlign
;
623 if ( vAlign
) *vAlign
= m_vAlign
;
625 else if (m_defGridAttr
!= this)
626 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
628 wxFAIL_MSG(wxT("Missing default cell attribute"));
633 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
637 else if (m_defGridAttr
!= this)
638 return m_defGridAttr
->GetRenderer();
640 wxFAIL_MSG(wxT("Missing default cell attribute"));
645 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
649 else if (m_defGridAttr
!= this)
650 return m_defGridAttr
->GetEditor();
652 wxFAIL_MSG(wxT("Missing default cell attribute"));
657 // ----------------------------------------------------------------------------
658 // wxGridCellAttrData
659 // ----------------------------------------------------------------------------
661 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
663 int n
= FindIndex(row
, col
);
664 if ( n
== wxNOT_FOUND
)
667 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
673 // change the attribute
674 m_attrs
[(size_t)n
].attr
= attr
;
678 // remove this attribute
679 m_attrs
.RemoveAt((size_t)n
);
684 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
686 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
688 int n
= FindIndex(row
, col
);
689 if ( n
!= wxNOT_FOUND
)
691 attr
= m_attrs
[(size_t)n
].attr
;
698 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
700 size_t count
= m_attrs
.GetCount();
701 for ( size_t n
= 0; n
< count
; n
++ )
703 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
704 wxCoord row
= coords
.GetRow();
705 if ((size_t)row
>= pos
)
709 // If rows inserted, include row counter where necessary
710 coords
.SetRow(row
+ numRows
);
712 else if (numRows
< 0)
714 // If rows deleted ...
715 if ((size_t)row
>= pos
- numRows
)
717 // ...either decrement row counter (if row still exists)...
718 coords
.SetRow(row
+ numRows
);
722 // ...or remove the attribute
723 m_attrs
.RemoveAt((size_t)n
);
731 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
733 size_t count
= m_attrs
.GetCount();
734 for ( size_t n
= 0; n
< count
; n
++ )
736 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
737 wxCoord col
= coords
.GetCol();
738 if ( (size_t)col
>= pos
)
742 // If rows inserted, include row counter where necessary
743 coords
.SetCol(col
+ numCols
);
745 else if (numCols
< 0)
747 // If rows deleted ...
748 if ((size_t)col
>= pos
- numCols
)
750 // ...either decrement row counter (if row still exists)...
751 coords
.SetCol(col
+ numCols
);
755 // ...or remove the attribute
756 m_attrs
.RemoveAt((size_t)n
);
764 int wxGridCellAttrData::FindIndex(int row
, int col
) const
766 size_t count
= m_attrs
.GetCount();
767 for ( size_t n
= 0; n
< count
; n
++ )
769 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
770 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
779 // ----------------------------------------------------------------------------
780 // wxGridRowOrColAttrData
781 // ----------------------------------------------------------------------------
783 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
785 size_t count
= m_attrs
.Count();
786 for ( size_t n
= 0; n
< count
; n
++ )
788 m_attrs
[n
]->DecRef();
792 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
794 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
796 int n
= m_rowsOrCols
.Index(rowOrCol
);
797 if ( n
!= wxNOT_FOUND
)
799 attr
= m_attrs
[(size_t)n
];
806 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
808 int n
= m_rowsOrCols
.Index(rowOrCol
);
809 if ( n
== wxNOT_FOUND
)
812 m_rowsOrCols
.Add(rowOrCol
);
819 // change the attribute
820 m_attrs
[(size_t)n
] = attr
;
824 // remove this attribute
825 m_attrs
[(size_t)n
]->DecRef();
826 m_rowsOrCols
.RemoveAt((size_t)n
);
827 m_attrs
.RemoveAt((size_t)n
);
832 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
834 size_t count
= m_attrs
.GetCount();
835 for ( size_t n
= 0; n
< count
; n
++ )
837 int & rowOrCol
= m_rowsOrCols
[n
];
838 if ( (size_t)rowOrCol
>= pos
)
840 if ( numRowsOrCols
> 0 )
842 // If rows inserted, include row counter where necessary
843 rowOrCol
+= numRowsOrCols
;
845 else if ( numRowsOrCols
< 0)
847 // If rows deleted, either decrement row counter (if row still exists)
848 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
849 rowOrCol
+= numRowsOrCols
;
852 m_rowsOrCols
.RemoveAt((size_t)n
);
853 m_attrs
.RemoveAt((size_t)n
);
861 // ----------------------------------------------------------------------------
862 // wxGridCellAttrProvider
863 // ----------------------------------------------------------------------------
865 wxGridCellAttrProvider::wxGridCellAttrProvider()
867 m_data
= (wxGridCellAttrProviderData
*)NULL
;
870 wxGridCellAttrProvider::~wxGridCellAttrProvider()
875 void wxGridCellAttrProvider::InitData()
877 m_data
= new wxGridCellAttrProviderData
;
880 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
882 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
885 // first look for the attribute of this specific cell
886 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
890 // then look for the col attr (col attributes are more common than
891 // the row ones, hence they have priority)
892 attr
= m_data
->m_colAttrs
.GetAttr(col
);
897 // finally try the row attributes
898 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
905 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
911 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
914 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
919 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
922 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
927 m_data
->m_colAttrs
.SetAttr(attr
, col
);
930 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
934 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
936 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
940 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
944 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
946 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
950 // ----------------------------------------------------------------------------
952 // ----------------------------------------------------------------------------
954 //////////////////////////////////////////////////////////////////////
956 // Abstract base class for grid data (the model)
958 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
961 wxGridTableBase::wxGridTableBase()
963 m_view
= (wxGrid
*) NULL
;
964 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
967 wxGridTableBase::~wxGridTableBase()
969 delete m_attrProvider
;
972 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
974 delete m_attrProvider
;
975 m_attrProvider
= attrProvider
;
978 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
980 if ( m_attrProvider
)
981 return m_attrProvider
->GetAttr(row
, col
);
983 return (wxGridCellAttr
*)NULL
;
986 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
988 if ( m_attrProvider
)
990 m_attrProvider
->SetAttr(attr
, row
, col
);
994 // as we take ownership of the pointer and don't store it, we must
1000 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1002 if ( m_attrProvider
)
1004 m_attrProvider
->SetRowAttr(attr
, row
);
1008 // as we take ownership of the pointer and don't store it, we must
1014 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1016 if ( m_attrProvider
)
1018 m_attrProvider
->SetColAttr(attr
, col
);
1022 // as we take ownership of the pointer and don't store it, we must
1028 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1030 if ( m_attrProvider
)
1032 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1036 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1038 if ( m_attrProvider
)
1040 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1044 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1046 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1047 "but your derived table class does not override this function") );
1052 bool wxGridTableBase::AppendRows( size_t numRows
)
1054 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1055 "but your derived table class does not override this function"));
1060 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1062 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1063 "but your derived table class does not override this function"));
1068 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1070 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1071 "but your derived table class does not override this function"));
1076 bool wxGridTableBase::AppendCols( size_t numCols
)
1078 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1079 "but your derived table class does not override this function"));
1084 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1086 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1087 "but your derived table class does not override this function"));
1093 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1100 wxString
wxGridTableBase::GetColLabelValue( int col
)
1102 // default col labels are:
1103 // cols 0 to 25 : A-Z
1104 // cols 26 to 675 : AA-ZZ
1109 for ( n
= 1; ; n
++ )
1111 s
+= (_T('A') + (wxChar
)( col%26
));
1113 if ( col
< 0 ) break;
1116 // reverse the string...
1118 for ( i
= 0; i
< n
; i
++ )
1128 //////////////////////////////////////////////////////////////////////
1130 // Message class for the grid table to send requests and notifications
1134 wxGridTableMessage::wxGridTableMessage()
1136 m_table
= (wxGridTableBase
*) NULL
;
1142 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1143 int commandInt1
, int commandInt2
)
1147 m_comInt1
= commandInt1
;
1148 m_comInt2
= commandInt2
;
1153 //////////////////////////////////////////////////////////////////////
1155 // A basic grid table for string data. An object of this class will
1156 // created by wxGrid if you don't specify an alternative table class.
1159 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1161 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1163 wxGridStringTable::wxGridStringTable()
1168 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1173 m_data
.Alloc( numRows
);
1176 sa
.Alloc( numCols
);
1177 for ( col
= 0; col
< numCols
; col
++ )
1179 sa
.Add( wxEmptyString
);
1182 for ( row
= 0; row
< numRows
; row
++ )
1188 wxGridStringTable::~wxGridStringTable()
1192 long wxGridStringTable::GetNumberRows()
1194 return m_data
.GetCount();
1197 long wxGridStringTable::GetNumberCols()
1199 if ( m_data
.GetCount() > 0 )
1200 return m_data
[0].GetCount();
1205 wxString
wxGridStringTable::GetValue( int row
, int col
)
1207 // TODO: bounds checking
1209 return m_data
[row
][col
];
1212 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1214 // TODO: bounds checking
1216 m_data
[row
][col
] = s
;
1219 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1221 // TODO: bounds checking
1223 return (m_data
[row
][col
] == wxEmptyString
);
1227 void wxGridStringTable::Clear()
1230 int numRows
, numCols
;
1232 numRows
= m_data
.GetCount();
1235 numCols
= m_data
[0].GetCount();
1237 for ( row
= 0; row
< numRows
; row
++ )
1239 for ( col
= 0; col
< numCols
; col
++ )
1241 m_data
[row
][col
] = wxEmptyString
;
1248 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1252 size_t curNumRows
= m_data
.GetCount();
1253 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1255 if ( pos
>= curNumRows
)
1257 return AppendRows( numRows
);
1261 sa
.Alloc( curNumCols
);
1262 for ( col
= 0; col
< curNumCols
; col
++ )
1264 sa
.Add( wxEmptyString
);
1267 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1269 m_data
.Insert( sa
, row
);
1271 UpdateAttrRows( pos
, numRows
);
1274 wxGridTableMessage
msg( this,
1275 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1279 GetView()->ProcessTableMessage( msg
);
1285 bool wxGridStringTable::AppendRows( size_t numRows
)
1289 size_t curNumRows
= m_data
.GetCount();
1290 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1293 if ( curNumCols
> 0 )
1295 sa
.Alloc( curNumCols
);
1296 for ( col
= 0; col
< curNumCols
; col
++ )
1298 sa
.Add( wxEmptyString
);
1302 for ( row
= 0; row
< numRows
; row
++ )
1309 wxGridTableMessage
msg( this,
1310 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1313 GetView()->ProcessTableMessage( msg
);
1319 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1323 size_t curNumRows
= m_data
.GetCount();
1325 if ( pos
>= curNumRows
)
1328 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1329 "Pos value is invalid for present table with %d rows",
1330 pos
, numRows
, curNumRows
);
1331 wxFAIL_MSG( wxT(errmsg
) );
1335 if ( numRows
> curNumRows
- pos
)
1337 numRows
= curNumRows
- pos
;
1340 if ( numRows
>= curNumRows
)
1342 m_data
.Empty(); // don't release memory just yet
1346 for ( n
= 0; n
< numRows
; n
++ )
1348 m_data
.Remove( pos
);
1351 UpdateAttrRows( pos
, -((int)numRows
) );
1354 wxGridTableMessage
msg( this,
1355 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1359 GetView()->ProcessTableMessage( msg
);
1365 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1369 size_t curNumRows
= m_data
.GetCount();
1370 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1372 if ( pos
>= curNumCols
)
1374 return AppendCols( numCols
);
1377 for ( row
= 0; row
< curNumRows
; row
++ )
1379 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1381 m_data
[row
].Insert( wxEmptyString
, col
);
1384 UpdateAttrCols( pos
, numCols
);
1387 wxGridTableMessage
msg( this,
1388 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1392 GetView()->ProcessTableMessage( msg
);
1398 bool wxGridStringTable::AppendCols( size_t numCols
)
1402 size_t curNumRows
= m_data
.GetCount();
1405 // TODO: something better than this ?
1407 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1408 "Call AppendRows() first") );
1412 for ( row
= 0; row
< curNumRows
; row
++ )
1414 for ( n
= 0; n
< numCols
; n
++ )
1416 m_data
[row
].Add( wxEmptyString
);
1422 wxGridTableMessage
msg( this,
1423 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1426 GetView()->ProcessTableMessage( msg
);
1432 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1436 size_t curNumRows
= m_data
.GetCount();
1437 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1439 if ( pos
>= curNumCols
)
1442 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1443 "Pos value is invalid for present table with %d cols",
1444 pos
, numCols
, curNumCols
);
1445 wxFAIL_MSG( wxT( errmsg
) );
1449 if ( numCols
> curNumCols
- pos
)
1451 numCols
= curNumCols
- pos
;
1454 for ( row
= 0; row
< curNumRows
; row
++ )
1456 if ( numCols
>= curNumCols
)
1458 m_data
[row
].Clear();
1462 for ( n
= 0; n
< numCols
; n
++ )
1464 m_data
[row
].Remove( pos
);
1468 UpdateAttrCols( pos
, -((int)numCols
) );
1471 wxGridTableMessage
msg( this,
1472 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1476 GetView()->ProcessTableMessage( msg
);
1482 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1484 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1486 // using default label
1488 return wxGridTableBase::GetRowLabelValue( row
);
1492 return m_rowLabels
[ row
];
1496 wxString
wxGridStringTable::GetColLabelValue( int col
)
1498 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1500 // using default label
1502 return wxGridTableBase::GetColLabelValue( col
);
1506 return m_colLabels
[ col
];
1510 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1512 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1514 int n
= m_rowLabels
.GetCount();
1516 for ( i
= n
; i
<= row
; i
++ )
1518 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1522 m_rowLabels
[row
] = value
;
1525 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1527 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1529 int n
= m_colLabels
.GetCount();
1531 for ( i
= n
; i
<= col
; i
++ )
1533 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1537 m_colLabels
[col
] = value
;
1542 //////////////////////////////////////////////////////////////////////
1543 //////////////////////////////////////////////////////////////////////
1545 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1547 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1548 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1549 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1550 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1553 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1555 const wxPoint
&pos
, const wxSize
&size
)
1556 : wxWindow( parent
, id
, pos
, size
)
1561 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1565 // NO - don't do this because it will set both the x and y origin
1566 // coords to match the parent scrolled window and we just want to
1567 // set the y coord - MB
1569 // m_owner->PrepareDC( dc );
1572 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1573 dc
.SetDeviceOrigin( 0, -y
);
1575 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1576 m_owner
->DrawRowLabels( dc
);
1580 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1582 m_owner
->ProcessRowLabelMouseEvent( event
);
1586 // This seems to be required for wxMotif otherwise the mouse
1587 // cursor must be in the cell edit control to get key events
1589 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1591 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1596 //////////////////////////////////////////////////////////////////////
1598 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1600 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1601 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1602 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1603 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1606 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1608 const wxPoint
&pos
, const wxSize
&size
)
1609 : wxWindow( parent
, id
, pos
, size
)
1614 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1618 // NO - don't do this because it will set both the x and y origin
1619 // coords to match the parent scrolled window and we just want to
1620 // set the x coord - MB
1622 // m_owner->PrepareDC( dc );
1625 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1626 dc
.SetDeviceOrigin( -x
, 0 );
1628 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1629 m_owner
->DrawColLabels( dc
);
1633 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1635 m_owner
->ProcessColLabelMouseEvent( event
);
1639 // This seems to be required for wxMotif otherwise the mouse
1640 // cursor must be in the cell edit control to get key events
1642 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1644 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1649 //////////////////////////////////////////////////////////////////////
1651 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1653 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1654 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1655 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1656 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1659 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1661 const wxPoint
&pos
, const wxSize
&size
)
1662 : wxWindow( parent
, id
, pos
, size
)
1667 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1671 int client_height
= 0;
1672 int client_width
= 0;
1673 GetClientSize( &client_width
, &client_height
);
1675 dc
.SetPen( *wxBLACK_PEN
);
1676 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1677 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1679 dc
.SetPen( *wxWHITE_PEN
);
1680 dc
.DrawLine( 0, 0, client_width
, 0 );
1681 dc
.DrawLine( 0, 0, 0, client_height
);
1685 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1687 m_owner
->ProcessCornerLabelMouseEvent( event
);
1691 // This seems to be required for wxMotif otherwise the mouse
1692 // cursor must be in the cell edit control to get key events
1694 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1696 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1701 //////////////////////////////////////////////////////////////////////
1703 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1705 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1706 EVT_PAINT( wxGridWindow::OnPaint
)
1707 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1708 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1709 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1712 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1713 wxGridRowLabelWindow
*rowLblWin
,
1714 wxGridColLabelWindow
*colLblWin
,
1715 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1716 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1719 m_rowLabelWin
= rowLblWin
;
1720 m_colLabelWin
= colLblWin
;
1721 SetBackgroundColour( "WHITE" );
1725 wxGridWindow::~wxGridWindow()
1730 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1732 wxPaintDC
dc( this );
1733 m_owner
->PrepareDC( dc
);
1734 wxRegion reg
= GetUpdateRegion();
1735 m_owner
->CalcCellsExposed( reg
);
1736 m_owner
->DrawGridCellArea( dc
);
1737 #if WXGRID_DRAW_LINES
1738 m_owner
->DrawAllGridLines( dc
, reg
);
1743 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1745 wxPanel::ScrollWindow( dx
, dy
, rect
);
1746 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1747 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1751 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1753 m_owner
->ProcessGridCellMouseEvent( event
);
1757 // This seems to be required for wxMotif otherwise the mouse
1758 // cursor must be in the cell edit control to get key events
1760 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1762 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1765 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1771 //////////////////////////////////////////////////////////////////////
1774 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1776 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1777 EVT_PAINT( wxGrid::OnPaint
)
1778 EVT_SIZE( wxGrid::OnSize
)
1779 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1780 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1783 wxGrid::wxGrid( wxWindow
*parent
,
1788 const wxString
& name
)
1789 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1798 m_defaultCellAttr
->SafeDecRef();
1800 #ifdef DEBUG_ATTR_CACHE
1801 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1802 wxPrintf(_T("wxGrid attribute cache statistics: "
1803 "total: %u, hits: %u (%u%%)\n"),
1804 total
, gs_nAttrCacheHits
,
1805 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1814 // ----- internal init and update functions
1817 void wxGrid::Create()
1819 m_created
= FALSE
; // set to TRUE by CreateGrid
1820 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
1822 m_table
= (wxGridTableBase
*) NULL
;
1825 m_cellEditCtrlEnabled
= FALSE
;
1827 m_defaultCellAttr
= new wxGridCellAttr
;
1828 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1829 // RD: Should we fill the default attrs now or is waiting until Init() okay?
1834 m_currentCellCoords
= wxGridNoCellCoords
;
1836 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1837 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1839 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1844 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1849 m_colLabelWin
= new wxGridColLabelWindow( this,
1854 m_gridWin
= new wxGridWindow( this,
1861 SetTargetWindow( m_gridWin
);
1865 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1869 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1874 m_numRows
= numRows
;
1875 m_numCols
= numCols
;
1877 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1878 m_table
->SetView( this );
1887 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
1891 // RD: Actually, this should probably be allowed. I think it would be
1892 // nice to be able to switch multiple Tables in and out of a single
1893 // View at runtime. Is there anything in the implmentation that would
1896 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1901 m_numRows
= table
->GetNumberRows();
1902 m_numCols
= table
->GetNumberCols();
1905 m_table
->SetView( this );
1920 if ( m_numRows
<= 0 )
1921 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1923 if ( m_numCols
<= 0 )
1924 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1926 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1927 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1929 if ( m_rowLabelWin
)
1931 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1935 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1938 m_labelTextColour
= wxColour( _T("BLACK") );
1941 m_attrCache
.row
= -1;
1943 // TODO: something better than this ?
1945 m_labelFont
= this->GetFont();
1946 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1948 m_rowLabelHorizAlign
= wxLEFT
;
1949 m_rowLabelVertAlign
= wxCENTRE
;
1951 m_colLabelHorizAlign
= wxCENTRE
;
1952 m_colLabelVertAlign
= wxTOP
;
1954 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1955 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1957 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1958 m_defaultRowHeight
+= 8;
1960 m_defaultRowHeight
+= 4;
1963 m_rowHeights
.Alloc( m_numRows
);
1964 m_rowBottoms
.Alloc( m_numRows
);
1966 for ( i
= 0; i
< m_numRows
; i
++ )
1968 m_rowHeights
.Add( m_defaultRowHeight
);
1969 rowBottom
+= m_defaultRowHeight
;
1970 m_rowBottoms
.Add( rowBottom
);
1973 m_colWidths
.Alloc( m_numCols
);
1974 m_colRights
.Alloc( m_numCols
);
1976 for ( i
= 0; i
< m_numCols
; i
++ )
1978 m_colWidths
.Add( m_defaultColWidth
);
1979 colRight
+= m_defaultColWidth
;
1980 m_colRights
.Add( colRight
);
1983 // Set default cell attributes
1984 m_defaultCellAttr
->SetFont(GetFont());
1985 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
1986 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1987 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1988 m_defaultCellAttr
->SetTextColour(
1989 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
1990 m_defaultCellAttr
->SetBackgroundColour(
1991 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
1994 m_gridLineColour
= wxColour( 128, 128, 255 );
1995 m_gridLinesEnabled
= TRUE
;
1997 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1998 m_winCapture
= (wxWindow
*)NULL
;
2000 m_dragRowOrCol
= -1;
2001 m_isDragging
= FALSE
;
2002 m_startDragPos
= wxDefaultPosition
;
2004 m_waitForSlowClick
= FALSE
;
2006 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2007 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2009 m_currentCellCoords
= wxGridNoCellCoords
;
2011 m_selectedTopLeft
= wxGridNoCellCoords
;
2012 m_selectedBottomRight
= wxGridNoCellCoords
;
2013 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2014 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2016 m_editable
= TRUE
; // default for whole grid
2018 m_inOnKeyDown
= FALSE
;
2024 void wxGrid::CalcDimensions()
2027 GetClientSize( &cw
, &ch
);
2029 if ( m_numRows
> 0 && m_numCols
> 0 )
2031 int right
= m_colRights
[ m_numCols
-1 ] + 50;
2032 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
2034 // TODO: restore the scroll position that we had before sizing
2037 GetViewStart( &x
, &y
);
2038 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2039 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2045 void wxGrid::CalcWindowSizes()
2048 GetClientSize( &cw
, &ch
);
2050 if ( m_cornerLabelWin
->IsShown() )
2051 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2053 if ( m_colLabelWin
->IsShown() )
2054 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2056 if ( m_rowLabelWin
->IsShown() )
2057 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2059 if ( m_gridWin
->IsShown() )
2060 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2064 // this is called when the grid table sends a message to say that it
2065 // has been redimensioned
2067 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2071 switch ( msg
.GetId() )
2073 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2075 size_t pos
= msg
.GetCommandInt();
2076 int numRows
= msg
.GetCommandInt2();
2077 for ( i
= 0; i
< numRows
; i
++ )
2079 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2080 m_rowBottoms
.Insert( 0, pos
);
2082 m_numRows
+= numRows
;
2085 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2087 for ( i
= pos
; i
< m_numRows
; i
++ )
2089 bottom
+= m_rowHeights
[i
];
2090 m_rowBottoms
[i
] = bottom
;
2096 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2098 int numRows
= msg
.GetCommandInt();
2099 for ( i
= 0; i
< numRows
; i
++ )
2101 m_rowHeights
.Add( m_defaultRowHeight
);
2102 m_rowBottoms
.Add( 0 );
2105 int oldNumRows
= m_numRows
;
2106 m_numRows
+= numRows
;
2109 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2111 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2113 bottom
+= m_rowHeights
[i
];
2114 m_rowBottoms
[i
] = bottom
;
2120 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2122 size_t pos
= msg
.GetCommandInt();
2123 int numRows
= msg
.GetCommandInt2();
2124 for ( i
= 0; i
< numRows
; i
++ )
2126 m_rowHeights
.Remove( pos
);
2127 m_rowBottoms
.Remove( pos
);
2129 m_numRows
-= numRows
;
2134 m_colWidths
.Clear();
2135 m_colRights
.Clear();
2136 m_currentCellCoords
= wxGridNoCellCoords
;
2140 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2141 m_currentCellCoords
.Set( 0, 0 );
2144 for ( i
= 0; i
< m_numRows
; i
++ )
2146 h
+= m_rowHeights
[i
];
2147 m_rowBottoms
[i
] = h
;
2155 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2157 size_t pos
= msg
.GetCommandInt();
2158 int numCols
= msg
.GetCommandInt2();
2159 for ( i
= 0; i
< numCols
; i
++ )
2161 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2162 m_colRights
.Insert( 0, pos
);
2164 m_numCols
+= numCols
;
2167 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2169 for ( i
= pos
; i
< m_numCols
; i
++ )
2171 right
+= m_colWidths
[i
];
2172 m_colRights
[i
] = right
;
2178 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2180 int numCols
= msg
.GetCommandInt();
2181 for ( i
= 0; i
< numCols
; i
++ )
2183 m_colWidths
.Add( m_defaultColWidth
);
2184 m_colRights
.Add( 0 );
2187 int oldNumCols
= m_numCols
;
2188 m_numCols
+= numCols
;
2191 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2193 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2195 right
+= m_colWidths
[i
];
2196 m_colRights
[i
] = right
;
2202 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2204 size_t pos
= msg
.GetCommandInt();
2205 int numCols
= msg
.GetCommandInt2();
2206 for ( i
= 0; i
< numCols
; i
++ )
2208 m_colWidths
.Remove( pos
);
2209 m_colRights
.Remove( pos
);
2211 m_numCols
-= numCols
;
2215 #if 0 // leave the row alone here so that AppendCols will work subsequently
2217 m_rowHeights
.Clear();
2218 m_rowBottoms
.Clear();
2220 m_currentCellCoords
= wxGridNoCellCoords
;
2224 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2225 m_currentCellCoords
.Set( 0, 0 );
2228 for ( i
= 0; i
< m_numCols
; i
++ )
2230 w
+= m_colWidths
[i
];
2243 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2245 wxRegionIterator
iter( reg
);
2248 m_rowLabelsExposed
.Empty();
2255 // TODO: remove this when we can...
2256 // There is a bug in wxMotif that gives garbage update
2257 // rectangles if you jump-scroll a long way by clicking the
2258 // scrollbar with middle button. This is a work-around
2260 #if defined(__WXMOTIF__)
2262 m_gridWin
->GetClientSize( &cw
, &ch
);
2263 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2264 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2267 // logical bounds of update region
2270 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2271 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2273 // find the row labels within these bounds
2277 for ( row
= 0; row
< m_numRows
; row
++ )
2279 if ( m_rowBottoms
[row
] < top
) continue;
2281 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2282 if ( rowTop
> bottom
) break;
2284 m_rowLabelsExposed
.Add( row
);
2292 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2294 wxRegionIterator
iter( reg
);
2297 m_colLabelsExposed
.Empty();
2304 // TODO: remove this when we can...
2305 // There is a bug in wxMotif that gives garbage update
2306 // rectangles if you jump-scroll a long way by clicking the
2307 // scrollbar with middle button. This is a work-around
2309 #if defined(__WXMOTIF__)
2311 m_gridWin
->GetClientSize( &cw
, &ch
);
2312 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2313 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2316 // logical bounds of update region
2319 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2320 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2322 // find the cells within these bounds
2326 for ( col
= 0; col
< m_numCols
; col
++ )
2328 if ( m_colRights
[col
] < left
) continue;
2330 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2331 if ( colLeft
> right
) break;
2333 m_colLabelsExposed
.Add( col
);
2341 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2343 wxRegionIterator
iter( reg
);
2346 m_cellsExposed
.Empty();
2347 m_rowsExposed
.Empty();
2348 m_colsExposed
.Empty();
2350 int left
, top
, right
, bottom
;
2355 // TODO: remove this when we can...
2356 // There is a bug in wxMotif that gives garbage update
2357 // rectangles if you jump-scroll a long way by clicking the
2358 // scrollbar with middle button. This is a work-around
2360 #if defined(__WXMOTIF__)
2362 m_gridWin
->GetClientSize( &cw
, &ch
);
2363 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2364 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2365 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2366 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2369 // logical bounds of update region
2371 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2372 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2374 // find the cells within these bounds
2377 int colLeft
, rowTop
;
2378 for ( row
= 0; row
< m_numRows
; row
++ )
2380 if ( m_rowBottoms
[row
] <= top
) continue;
2382 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2383 if ( rowTop
> bottom
) break;
2385 m_rowsExposed
.Add( row
);
2387 for ( col
= 0; col
< m_numCols
; col
++ )
2389 if ( m_colRights
[col
] <= left
) continue;
2391 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2392 if ( colLeft
> right
) break;
2394 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2395 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2404 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2407 wxPoint
pos( event
.GetPosition() );
2408 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2410 if ( event
.Dragging() )
2412 m_isDragging
= TRUE
;
2414 if ( event
.LeftIsDown() )
2416 switch( m_cursorMode
)
2418 case WXGRID_CURSOR_RESIZE_ROW
:
2420 int cw
, ch
, left
, dummy
;
2421 m_gridWin
->GetClientSize( &cw
, &ch
);
2422 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2424 wxClientDC
dc( m_gridWin
);
2427 m_rowBottoms
[m_dragRowOrCol
] -
2428 m_rowHeights
[m_dragRowOrCol
] +
2429 WXGRID_MIN_ROW_HEIGHT
);
2430 dc
.SetLogicalFunction(wxINVERT
);
2431 if ( m_dragLastPos
>= 0 )
2433 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2435 dc
.DrawLine( left
, y
, left
+cw
, y
);
2440 case WXGRID_CURSOR_SELECT_ROW
:
2441 if ( (row
= YToRow( y
)) >= 0 &&
2442 !IsInSelection( row
, 0 ) )
2444 SelectRow( row
, TRUE
);
2447 // default label to suppress warnings about "enumeration value
2448 // 'xxx' not handled in switch
2456 m_isDragging
= FALSE
;
2459 // ------------ Entering or leaving the window
2461 if ( event
.Entering() || event
.Leaving() )
2463 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2467 // ------------ Left button pressed
2469 else if ( event
.LeftDown() )
2471 // don't send a label click event for a hit on the
2472 // edge of the row label - this is probably the user
2473 // wanting to resize the row
2475 if ( YToEdgeOfRow(y
) < 0 )
2479 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2481 SelectRow( row
, event
.ShiftDown() );
2482 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2487 // starting to drag-resize a row
2489 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2494 // ------------ Left double click
2496 else if (event
.LeftDClick() )
2498 if ( YToEdgeOfRow(y
) < 0 )
2501 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2506 // ------------ Left button released
2508 else if ( event
.LeftUp() )
2510 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2512 DoEndDragResizeRow();
2514 // Note: we are ending the event *after* doing
2515 // default processing in this case
2517 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2520 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2525 // ------------ Right button down
2527 else if ( event
.RightDown() )
2530 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2532 // no default action at the moment
2537 // ------------ Right double click
2539 else if ( event
.RightDClick() )
2542 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2544 // no default action at the moment
2549 // ------------ No buttons down and mouse moving
2551 else if ( event
.Moving() )
2553 m_dragRowOrCol
= YToEdgeOfRow( y
);
2554 if ( m_dragRowOrCol
>= 0 )
2556 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2558 // don't capture the mouse yet
2559 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2562 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2564 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2570 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2573 wxPoint
pos( event
.GetPosition() );
2574 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2576 if ( event
.Dragging() )
2578 m_isDragging
= TRUE
;
2580 if ( event
.LeftIsDown() )
2582 switch( m_cursorMode
)
2584 case WXGRID_CURSOR_RESIZE_COL
:
2586 int cw
, ch
, dummy
, top
;
2587 m_gridWin
->GetClientSize( &cw
, &ch
);
2588 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2590 wxClientDC
dc( m_gridWin
);
2593 m_colRights
[m_dragRowOrCol
] -
2594 m_colWidths
[m_dragRowOrCol
] +
2595 WXGRID_MIN_COL_WIDTH
);
2596 dc
.SetLogicalFunction(wxINVERT
);
2597 if ( m_dragLastPos
>= 0 )
2599 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2601 dc
.DrawLine( x
, top
, x
, top
+ch
);
2606 case WXGRID_CURSOR_SELECT_COL
:
2607 if ( (col
= XToCol( x
)) >= 0 &&
2608 !IsInSelection( 0, col
) )
2610 SelectCol( col
, TRUE
);
2613 // default label to suppress warnings about "enumeration value
2614 // 'xxx' not handled in switch
2622 m_isDragging
= FALSE
;
2625 // ------------ Entering or leaving the window
2627 if ( event
.Entering() || event
.Leaving() )
2629 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2633 // ------------ Left button pressed
2635 else if ( event
.LeftDown() )
2637 // don't send a label click event for a hit on the
2638 // edge of the col label - this is probably the user
2639 // wanting to resize the col
2641 if ( XToEdgeOfCol(x
) < 0 )
2645 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2647 SelectCol( col
, event
.ShiftDown() );
2648 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2653 // starting to drag-resize a col
2655 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2660 // ------------ Left double click
2662 if ( event
.LeftDClick() )
2664 if ( XToEdgeOfCol(x
) < 0 )
2667 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2672 // ------------ Left button released
2674 else if ( event
.LeftUp() )
2676 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2678 DoEndDragResizeCol();
2680 // Note: we are ending the event *after* doing
2681 // default processing in this case
2683 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2686 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2691 // ------------ Right button down
2693 else if ( event
.RightDown() )
2696 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2698 // no default action at the moment
2703 // ------------ Right double click
2705 else if ( event
.RightDClick() )
2708 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2710 // no default action at the moment
2715 // ------------ No buttons down and mouse moving
2717 else if ( event
.Moving() )
2719 m_dragRowOrCol
= XToEdgeOfCol( x
);
2720 if ( m_dragRowOrCol
>= 0 )
2722 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2724 // don't capture the cursor yet
2725 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2728 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2730 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2736 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2738 if ( event
.LeftDown() )
2740 // indicate corner label by having both row and
2743 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2749 else if ( event
.LeftDClick() )
2751 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2754 else if ( event
.RightDown() )
2756 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2758 // no default action at the moment
2762 else if ( event
.RightDClick() )
2764 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2766 // no default action at the moment
2771 void wxGrid::ChangeCursorMode(CursorMode mode
,
2776 static const wxChar
*cursorModes
[] =
2785 wxLogTrace(_T("grid"),
2786 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2787 win
== m_colLabelWin
? _T("colLabelWin")
2788 : win
? _T("rowLabelWin")
2790 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2791 #endif // __WXDEBUG__
2793 if ( mode
== m_cursorMode
)
2798 // by default use the grid itself
2804 m_winCapture
->ReleaseMouse();
2805 m_winCapture
= (wxWindow
*)NULL
;
2808 m_cursorMode
= mode
;
2810 switch ( m_cursorMode
)
2812 case WXGRID_CURSOR_RESIZE_ROW
:
2813 win
->SetCursor( m_rowResizeCursor
);
2816 case WXGRID_CURSOR_RESIZE_COL
:
2817 win
->SetCursor( m_colResizeCursor
);
2821 win
->SetCursor( *wxSTANDARD_CURSOR
);
2824 // we need to capture mouse when resizing
2825 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2826 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2828 if ( captureMouse
&& resize
)
2830 win
->CaptureMouse();
2835 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2838 wxPoint
pos( event
.GetPosition() );
2839 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2841 wxGridCellCoords coords
;
2842 XYToCell( x
, y
, coords
);
2844 if ( event
.Dragging() )
2846 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
2848 // Don't start doing anything until the mouse has been drug at
2849 // least 3 pixels in any direction...
2850 if (! m_isDragging
) {
2851 if (m_startDragPos
== wxDefaultPosition
) {
2852 m_startDragPos
= pos
;
2855 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
2859 m_isDragging
= TRUE
;
2860 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2862 // Hide the edit control, so it
2863 // won't interfer with drag-shrinking.
2864 if ( IsCellEditControlEnabled() )
2865 HideCellEditControl();
2867 // Have we captured the mouse yet?
2868 if (! m_winCapture
) {
2869 m_winCapture
= m_gridWin
;
2870 m_winCapture
->CaptureMouse();
2873 if ( coords
!= wxGridNoCellCoords
)
2875 if ( !IsSelection() )
2877 SelectBlock( coords
, coords
);
2881 SelectBlock( m_currentCellCoords
, coords
);
2884 if (! IsVisible(coords
)) {
2885 MakeCellVisible(coords
);
2886 // TODO: need to introduce a delay or something here. The
2887 // scrolling is way to fast, at least on MSW.
2891 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2893 int cw
, ch
, left
, dummy
;
2894 m_gridWin
->GetClientSize( &cw
, &ch
);
2895 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2897 wxClientDC
dc( m_gridWin
);
2900 m_rowBottoms
[m_dragRowOrCol
] -
2901 m_rowHeights
[m_dragRowOrCol
] +
2902 WXGRID_MIN_ROW_HEIGHT
);
2903 dc
.SetLogicalFunction(wxINVERT
);
2904 if ( m_dragLastPos
>= 0 )
2906 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2908 dc
.DrawLine( left
, y
, left
+cw
, y
);
2911 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2913 int cw
, ch
, dummy
, top
;
2914 m_gridWin
->GetClientSize( &cw
, &ch
);
2915 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2917 wxClientDC
dc( m_gridWin
);
2920 m_colRights
[m_dragRowOrCol
] -
2921 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
2922 dc
.SetLogicalFunction(wxINVERT
);
2923 if ( m_dragLastPos
>= 0 )
2925 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2927 dc
.DrawLine( x
, top
, x
, top
+ch
);
2934 m_isDragging
= FALSE
;
2935 m_startDragPos
= wxDefaultPosition
;
2938 if ( coords
!= wxGridNoCellCoords
)
2940 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2941 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2944 if ( event
.Entering() || event
.Leaving() )
2946 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2947 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2952 // ------------ Left button pressed
2954 if ( event
.LeftDown() )
2956 EnableCellEditControl( FALSE
);
2957 if ( event
.ShiftDown() )
2959 SelectBlock( m_currentCellCoords
, coords
);
2961 else if ( XToEdgeOfCol(x
) < 0 &&
2962 YToEdgeOfRow(y
) < 0 )
2964 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2969 MakeCellVisible( coords
);
2971 // if this is the second click on this cell then start
2973 if (m_waitForSlowClick
&& coords
== m_currentCellCoords
) {
2974 EnableCellEditControl(TRUE
);
2975 // VZ: this is done by the call above, so why do it
2976 // again? please remove this line if it's ok
2977 //ShowCellEditControl();
2978 m_waitForSlowClick
= FALSE
;
2981 SetCurrentCell( coords
);
2982 m_waitForSlowClick
= TRUE
;
2989 // ------------ Left double click
2991 else if ( event
.LeftDClick() )
2993 EnableCellEditControl( FALSE
);
2994 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2996 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
3004 // ------------ Left button released
3006 else if ( event
.LeftUp() )
3008 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3010 if ( IsSelection() )
3013 m_winCapture
->ReleaseMouse();
3014 m_winCapture
= NULL
;
3016 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
3019 // Show the edit control, if it has been hidden for
3021 ShowCellEditControl();
3023 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3025 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3026 DoEndDragResizeRow();
3028 // Note: we are ending the event *after* doing
3029 // default processing in this case
3031 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3033 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3035 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3036 DoEndDragResizeCol();
3038 // Note: we are ending the event *after* doing
3039 // default processing in this case
3041 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3048 // ------------ Right button down
3050 else if ( event
.RightDown() )
3052 EnableCellEditControl( FALSE
);
3053 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
3058 // no default action at the moment
3063 // ------------ Right double click
3065 else if ( event
.RightDClick() )
3067 EnableCellEditControl( FALSE
);
3068 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
3073 // no default action at the moment
3077 // ------------ Moving and no button action
3079 else if ( event
.Moving() && !event
.IsButton() )
3081 int dragRow
= YToEdgeOfRow( y
);
3082 int dragCol
= XToEdgeOfCol( x
);
3084 // Dragging on the corner of a cell to resize in both
3085 // directions is not implemented yet...
3087 if ( dragRow
>= 0 && dragCol
>= 0 )
3089 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3095 m_dragRowOrCol
= dragRow
;
3097 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3099 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3107 m_dragRowOrCol
= dragCol
;
3109 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3111 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3117 // Neither on a row or col edge
3119 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3121 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3128 void wxGrid::DoEndDragResizeRow()
3130 if ( m_dragLastPos
>= 0 )
3132 // erase the last line and resize the row
3134 int cw
, ch
, left
, dummy
;
3135 m_gridWin
->GetClientSize( &cw
, &ch
);
3136 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3138 wxClientDC
dc( m_gridWin
);
3140 dc
.SetLogicalFunction( wxINVERT
);
3141 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3142 HideCellEditControl();
3144 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3145 SetRowSize( m_dragRowOrCol
,
3146 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3148 if ( !GetBatchCount() )
3150 // Only needed to get the correct rect.y:
3151 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3153 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3154 rect
.width
= m_rowLabelWidth
;
3155 rect
.height
= ch
- rect
.y
;
3156 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3158 m_gridWin
->Refresh( FALSE
, &rect
);
3161 ShowCellEditControl();
3166 void wxGrid::DoEndDragResizeCol()
3168 if ( m_dragLastPos
>= 0 )
3170 // erase the last line and resize the col
3172 int cw
, ch
, dummy
, top
;
3173 m_gridWin
->GetClientSize( &cw
, &ch
);
3174 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3176 wxClientDC
dc( m_gridWin
);
3178 dc
.SetLogicalFunction( wxINVERT
);
3179 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3180 HideCellEditControl();
3182 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3183 SetColSize( m_dragRowOrCol
,
3184 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3186 if ( !GetBatchCount() )
3188 // Only needed to get the correct rect.x:
3189 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3191 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3192 rect
.width
= cw
- rect
.x
;
3193 rect
.height
= m_colLabelHeight
;
3194 m_colLabelWin
->Refresh( TRUE
, &rect
);
3196 m_gridWin
->Refresh( FALSE
, &rect
);
3199 ShowCellEditControl();
3206 // ------ interaction with data model
3208 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3210 switch ( msg
.GetId() )
3212 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3213 return GetModelValues();
3215 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3216 return SetModelValues();
3218 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3219 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3220 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3221 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3222 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3223 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3224 return Redimension( msg
);
3233 // The behaviour of this function depends on the grid table class
3234 // Clear() function. For the default wxGridStringTable class the
3235 // behavious is to replace all cell contents with wxEmptyString but
3236 // not to change the number of rows or cols.
3238 void wxGrid::ClearGrid()
3243 SetEditControlValue();
3244 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3249 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3251 // TODO: something with updateLabels flag
3255 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3261 if (IsCellEditControlEnabled())
3262 EnableCellEditControl(FALSE
);
3264 bool ok
= m_table
->InsertRows( pos
, numRows
);
3266 // the table will have sent the results of the insert row
3267 // operation to this view object as a grid table message
3271 if ( m_numCols
== 0 )
3273 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3275 // TODO: perhaps instead of appending the default number of cols
3276 // we should remember what the last non-zero number of cols was ?
3280 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3282 // if we have just inserted cols into an empty grid the current
3283 // cell will be undefined...
3285 SetCurrentCell( 0, 0 );
3289 if ( !GetBatchCount() ) Refresh();
3292 SetEditControlValue();
3302 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3304 // TODO: something with updateLabels flag
3308 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3312 if ( m_table
&& m_table
->AppendRows( numRows
) )
3314 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3316 // if we have just inserted cols into an empty grid the current
3317 // cell will be undefined...
3319 SetCurrentCell( 0, 0 );
3322 // the table will have sent the results of the append row
3323 // operation to this view object as a grid table message
3326 if ( !GetBatchCount() ) Refresh();
3336 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3338 // TODO: something with updateLabels flag
3342 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3348 if (IsCellEditControlEnabled())
3349 EnableCellEditControl(FALSE
);
3351 if (m_table
->DeleteRows( pos
, numRows
))
3354 // the table will have sent the results of the delete row
3355 // operation to this view object as a grid table message
3358 if ( !GetBatchCount() ) Refresh();
3366 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3368 // TODO: something with updateLabels flag
3372 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3378 if (IsCellEditControlEnabled())
3379 EnableCellEditControl(FALSE
);
3381 bool ok
= m_table
->InsertCols( pos
, numCols
);
3383 // the table will have sent the results of the insert col
3384 // operation to this view object as a grid table message
3388 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3390 // if we have just inserted cols into an empty grid the current
3391 // cell will be undefined...
3393 SetCurrentCell( 0, 0 );
3397 if ( !GetBatchCount() ) Refresh();
3400 SetEditControlValue();
3410 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3412 // TODO: something with updateLabels flag
3416 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3420 if ( m_table
&& m_table
->AppendCols( numCols
) )
3422 // the table will have sent the results of the append col
3423 // operation to this view object as a grid table message
3425 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3427 // if we have just inserted cols into an empty grid the current
3428 // cell will be undefined...
3430 SetCurrentCell( 0, 0 );
3434 if ( !GetBatchCount() ) Refresh();
3444 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3446 // TODO: something with updateLabels flag
3450 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3456 if (IsCellEditControlEnabled())
3457 EnableCellEditControl(FALSE
);
3459 if ( m_table
->DeleteCols( pos
, numCols
) )
3461 // the table will have sent the results of the delete col
3462 // operation to this view object as a grid table message
3465 if ( !GetBatchCount() ) Refresh();
3475 // ----- event handlers
3478 // Generate a grid event based on a mouse event and
3479 // return the result of ProcessEvent()
3481 bool wxGrid::SendEvent( const wxEventType type
,
3483 wxMouseEvent
& mouseEv
)
3485 if ( type
== EVT_GRID_ROW_SIZE
||
3486 type
== EVT_GRID_COL_SIZE
)
3488 int rowOrCol
= (row
== -1 ? col
: row
);
3490 wxGridSizeEvent
gridEvt( GetId(),
3494 mouseEv
.GetX(), mouseEv
.GetY(),
3495 mouseEv
.ControlDown(),
3496 mouseEv
.ShiftDown(),
3498 mouseEv
.MetaDown() );
3500 return GetEventHandler()->ProcessEvent(gridEvt
);
3502 else if ( type
== EVT_GRID_RANGE_SELECT
)
3504 wxGridRangeSelectEvent
gridEvt( GetId(),
3508 m_selectedBottomRight
,
3509 mouseEv
.ControlDown(),
3510 mouseEv
.ShiftDown(),
3512 mouseEv
.MetaDown() );
3514 return GetEventHandler()->ProcessEvent(gridEvt
);
3518 wxGridEvent
gridEvt( GetId(),
3522 mouseEv
.GetX(), mouseEv
.GetY(),
3523 mouseEv
.ControlDown(),
3524 mouseEv
.ShiftDown(),
3526 mouseEv
.MetaDown() );
3528 return GetEventHandler()->ProcessEvent(gridEvt
);
3533 // Generate a grid event of specified type and return the result
3534 // of ProcessEvent().
3536 bool wxGrid::SendEvent( const wxEventType type
,
3539 if ( type
== EVT_GRID_ROW_SIZE
||
3540 type
== EVT_GRID_COL_SIZE
)
3542 int rowOrCol
= (row
== -1 ? col
: row
);
3544 wxGridSizeEvent
gridEvt( GetId(),
3549 return GetEventHandler()->ProcessEvent(gridEvt
);
3553 wxGridEvent
gridEvt( GetId(),
3558 return GetEventHandler()->ProcessEvent(gridEvt
);
3563 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3565 wxPaintDC
dc( this );
3567 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3568 m_numRows
&& m_numCols
)
3570 m_currentCellCoords
.Set(0, 0);
3571 SetEditControlValue();
3572 ShowCellEditControl();
3579 // This is just here to make sure that CalcDimensions gets called when
3580 // the grid view is resized... then the size event is skipped to allow
3581 // the box sizers to handle everything
3583 void wxGrid::OnSize( wxSizeEvent
& event
)
3590 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3592 if ( m_inOnKeyDown
)
3594 // shouldn't be here - we are going round in circles...
3596 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3599 m_inOnKeyDown
= TRUE
;
3601 // propagate the event up and see if it gets processed
3603 wxWindow
*parent
= GetParent();
3604 wxKeyEvent
keyEvt( event
);
3605 keyEvt
.SetEventObject( parent
);
3607 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3610 // TODO: Should also support Shift-cursor keys for
3611 // extending the selection. Maybe add a flag to
3612 // MoveCursorXXX() and MoveCursorXXXBlock() and
3613 // just send event.ShiftDown().
3615 // try local handlers
3617 switch ( event
.KeyCode() )
3620 if ( event
.ControlDown() )
3622 MoveCursorUpBlock();
3631 if ( event
.ControlDown() )
3633 MoveCursorDownBlock();
3642 if ( event
.ControlDown() )
3644 MoveCursorLeftBlock();
3653 if ( event
.ControlDown() )
3655 MoveCursorRightBlock();
3664 if ( event
.ControlDown() )
3666 event
.Skip(); // to let the edit control have the return
3675 if (event
.ShiftDown())
3682 if ( event
.ControlDown() )
3684 MakeCellVisible( 0, 0 );
3685 SetCurrentCell( 0, 0 );
3694 if ( event
.ControlDown() )
3696 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3697 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3713 // We don't want these keys to trigger the edit control, any others?
3722 if ( !IsEditable() )
3727 // Otherwise fall through to default
3730 // now try the cell edit control
3732 if ( !IsCellEditControlEnabled() )
3733 EnableCellEditControl( TRUE
);
3734 if (IsCellEditControlEnabled()) {
3735 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3736 attr
->GetEditor()->StartingKey(event
);
3743 m_inOnKeyDown
= FALSE
;
3747 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3753 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3755 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3757 // the event has been intercepted - do nothing
3762 m_currentCellCoords
!= wxGridNoCellCoords
)
3764 HideCellEditControl();
3765 SaveEditControlValue();
3766 EnableCellEditControl(FALSE
);
3768 // Clear the old current cell highlight
3769 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3771 // Otherwise refresh redraws the highlight!
3772 m_currentCellCoords
= coords
;
3774 m_gridWin
->Refresh( FALSE
, &r
);
3777 m_currentCellCoords
= coords
;
3779 SetEditControlValue();
3783 wxClientDC
dc(m_gridWin
);
3786 wxGridCellAttr
* attr
= GetCellAttr(coords
);
3787 DrawCellHighlight(dc
, attr
);
3790 if ( IsSelection() )
3792 wxRect
r( SelectionToDeviceRect() );
3794 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3801 // ------ functions to get/send data (see also public functions)
3804 bool wxGrid::GetModelValues()
3808 // all we need to do is repaint the grid
3810 m_gridWin
->Refresh();
3818 bool wxGrid::SetModelValues()
3824 for ( row
= 0; row
< m_numRows
; row
++ )
3826 for ( col
= 0; col
< m_numCols
; col
++ )
3828 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3840 // Note - this function only draws cells that are in the list of
3841 // exposed cells (usually set from the update region by
3842 // CalcExposedCells)
3844 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3846 if ( !m_numRows
|| !m_numCols
) return;
3849 size_t numCells
= m_cellsExposed
.GetCount();
3851 for ( i
= 0; i
< numCells
; i
++ )
3853 DrawCell( dc
, m_cellsExposed
[i
] );
3858 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3860 int row
= coords
.GetRow();
3861 int col
= coords
.GetCol();
3863 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3866 // we draw the cell border ourselves
3867 #if !WXGRID_DRAW_LINES
3868 if ( m_gridLinesEnabled
)
3869 DrawCellBorder( dc
, coords
);
3872 // but all the rest is drawn by the cell renderer and hence may be
3875 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3876 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3877 rect
.width
= m_colWidths
[col
]-1;
3878 rect
.height
= m_rowHeights
[row
]-1;
3880 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
3881 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
3883 if (m_currentCellCoords
== coords
)
3884 DrawCellHighlight(dc
, attr
);
3889 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
3891 int row
= m_currentCellCoords
.GetRow();
3892 int col
= m_currentCellCoords
.GetCol();
3894 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3898 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3899 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3900 rect
.width
= m_colWidths
[col
] - 1;
3901 rect
.height
= m_rowHeights
[row
] - 1;
3903 if ( attr
->IsReadOnly() )
3905 // hmmm... what could we do here to show that the cell is disabled?
3906 // for now, I just draw a thinner border than for the other ones, but
3907 // it doesn't look really good
3908 dc
.SetPen(wxPen(m_gridLineColour
, 2, wxSOLID
));
3909 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
3911 dc
.DrawRectangle(rect
);
3915 // VZ: my experiments with 3d borders...
3917 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
3918 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
3920 dc
.DrawRectangle(rect
);
3922 // FIXME we should properly set colours for arbitrary bg
3923 wxCoord x1
= rect
.x
,
3925 x2
= rect
.x
+ rect
.width
,
3926 y2
= rect
.y
+ rect
.height
;
3928 dc
.SetPen(*wxWHITE_PEN
);
3929 dc
.DrawLine(x1
, y1
, x2
- 1, y1
);
3930 dc
.DrawLine(x1
, y1
, x1
, y2
- 1);
3932 dc
.SetPen(*wxLIGHT_GREY_PEN
);
3933 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
3934 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
- 1);
3936 dc
.SetPen(*wxBLACK_PEN
);
3937 dc
.DrawLine(x1
, y2
, x2
, y2
);
3938 dc
.DrawLine(x2
, y1
, x2
, y2
);
3943 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3945 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3946 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3948 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3949 int row
= coords
.GetRow();
3950 int col
= coords
.GetCol();
3952 // right hand border
3954 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3955 m_colRights
[col
], m_rowBottoms
[row
] );
3959 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3960 m_colRights
[col
], m_rowBottoms
[row
] );
3964 // TODO: remove this ???
3965 // This is used to redraw all grid lines e.g. when the grid line colour
3968 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3970 if ( !m_gridLinesEnabled
||
3972 !m_numCols
) return;
3974 int top
, bottom
, left
, right
;
3978 m_gridWin
->GetClientSize(&cw
, &ch
);
3980 // virtual coords of visible area
3982 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3983 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3987 reg
.GetBox(x
, y
, w
, h
);
3988 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3989 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3992 // avoid drawing grid lines past the last row and col
3994 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3995 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3997 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3999 // horizontal grid lines
4002 for ( i
= 0; i
< m_numRows
; i
++ )
4004 if ( m_rowBottoms
[i
]-1 > bottom
)
4008 else if ( m_rowBottoms
[i
]-1 >= top
)
4010 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
4015 // vertical grid lines
4017 for ( i
= 0; i
< m_numCols
; i
++ )
4019 if ( m_colRights
[i
]-1 > right
)
4023 else if ( m_colRights
[i
]-1 >= left
)
4025 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
4031 void wxGrid::DrawRowLabels( wxDC
& dc
)
4033 if ( !m_numRows
|| !m_numCols
) return;
4036 size_t numLabels
= m_rowLabelsExposed
.GetCount();
4038 for ( i
= 0; i
< numLabels
; i
++ )
4040 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
4045 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
4047 if ( m_rowHeights
[row
] <= 0 ) return;
4049 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4051 dc
.SetPen( *wxBLACK_PEN
);
4052 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
4053 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4055 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
4056 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4058 dc
.SetPen( *wxWHITE_PEN
);
4059 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
4060 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4062 dc
.SetBackgroundMode( wxTRANSPARENT
);
4063 dc
.SetTextForeground( GetLabelTextColour() );
4064 dc
.SetFont( GetLabelFont() );
4067 GetRowLabelAlignment( &hAlign
, &vAlign
);
4071 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
4072 rect
.SetWidth( m_rowLabelWidth
- 4 );
4073 rect
.SetHeight( m_rowHeights
[row
] - 4 );
4074 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4078 void wxGrid::DrawColLabels( wxDC
& dc
)
4080 if ( !m_numRows
|| !m_numCols
) return;
4083 size_t numLabels
= m_colLabelsExposed
.GetCount();
4085 for ( i
= 0; i
< numLabels
; i
++ )
4087 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4092 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4094 if ( m_colWidths
[col
] <= 0 ) return;
4096 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
4098 dc
.SetPen( *wxBLACK_PEN
);
4099 dc
.DrawLine( m_colRights
[col
]-1, 0,
4100 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4102 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4103 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4105 dc
.SetPen( *wxWHITE_PEN
);
4106 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4107 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4109 dc
.SetBackgroundMode( wxTRANSPARENT
);
4110 dc
.SetTextForeground( GetLabelTextColour() );
4111 dc
.SetFont( GetLabelFont() );
4113 dc
.SetBackgroundMode( wxTRANSPARENT
);
4114 dc
.SetTextForeground( GetLabelTextColour() );
4115 dc
.SetFont( GetLabelFont() );
4118 GetColLabelAlignment( &hAlign
, &vAlign
);
4121 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4123 rect
.SetWidth( m_colWidths
[col
] - 4 );
4124 rect
.SetHeight( m_colLabelHeight
- 4 );
4125 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4129 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4130 const wxString
& value
,
4135 long textWidth
, textHeight
;
4136 long lineWidth
, lineHeight
;
4137 wxArrayString lines
;
4139 dc
.SetClippingRegion( rect
);
4140 StringToLines( value
, lines
);
4141 if ( lines
.GetCount() )
4143 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4144 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4147 switch ( horizAlign
)
4150 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4154 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4163 switch ( vertAlign
)
4166 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4170 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4179 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4181 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4186 dc
.DestroyClippingRegion();
4190 // Split multi line text up into an array of strings. Any existing
4191 // contents of the string array are preserved.
4193 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4197 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4198 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4200 while ( startPos
< (int)tVal
.Length() )
4202 pos
= tVal
.Mid(startPos
).Find( eol
);
4207 else if ( pos
== 0 )
4209 lines
.Add( wxEmptyString
);
4213 lines
.Add( value
.Mid(startPos
, pos
) );
4217 if ( startPos
< (int)value
.Length() )
4219 lines
.Add( value
.Mid( startPos
) );
4224 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4225 wxArrayString
& lines
,
4226 long *width
, long *height
)
4233 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4235 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4236 w
= wxMax( w
, lineW
);
4246 // ------ Edit control functions
4250 void wxGrid::EnableEditing( bool edit
)
4252 // TODO: improve this ?
4254 if ( edit
!= m_editable
)
4258 EnableCellEditControl(m_editable
);
4263 void wxGrid::EnableCellEditControl( bool enable
)
4268 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4269 SetCurrentCell( 0, 0 );
4271 if ( enable
!= m_cellEditCtrlEnabled
)
4275 m_cellEditCtrlEnabled
= enable
;
4276 SetEditControlValue();
4277 ShowCellEditControl();
4281 HideCellEditControl();
4282 SaveEditControlValue();
4283 m_cellEditCtrlEnabled
= enable
;
4289 bool wxGrid::IsCellEditControlEnabled()
4293 if ( m_cellEditCtrlEnabled
)
4295 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4296 enabled
= !attr
->IsReadOnly();
4307 void wxGrid::ShowCellEditControl()
4309 if ( IsCellEditControlEnabled() )
4311 if ( !IsVisible( m_currentCellCoords
) )
4317 wxRect rect
= CellToRect( m_currentCellCoords
);
4318 int row
= m_currentCellCoords
.GetRow();
4319 int col
= m_currentCellCoords
.GetCol();
4321 // convert to scrolled coords
4323 int left
, top
, right
, bottom
;
4324 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4325 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4326 left
--; top
--; right
--; bottom
--; // cell is shifted by one pixel
4328 m_gridWin
->GetClientSize( &cw
, &ch
);
4330 // Make the edit control large enough to allow for internal
4333 // TODO: remove this if the text ctrl sizing is improved esp. for
4337 #if defined(__WXMOTIF__)
4338 if ( row
== 0 || col
== 0 )
4347 if ( row
== 0 || col
== 0 )
4357 #if defined(__WXGTK__)
4360 if (left
!= 0) left_diff
++;
4361 if (top
!= 0) top_diff
++;
4362 rect
.SetLeft( left
+ left_diff
);
4363 rect
.SetTop( top
+ top_diff
);
4364 rect
.SetRight( rect
.GetRight() - left_diff
);
4365 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4367 rect
.SetLeft( wxMax(0, left
- extra
) );
4368 rect
.SetTop( wxMax(0, top
- extra
) );
4369 rect
.SetRight( rect
.GetRight() + 2*extra
);
4370 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4373 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4374 wxGridCellEditor
* editor
= attr
->GetEditor();
4375 if ( !editor
->IsCreated() )
4377 editor
->Create(m_gridWin
, -1,
4378 new wxGridCellEditorEvtHandler(this, editor
));
4381 editor
->SetSize( rect
);
4382 editor
->Show( TRUE
, attr
);
4383 editor
->BeginEdit(row
, col
, this);
4390 void wxGrid::HideCellEditControl()
4392 if ( IsCellEditControlEnabled() )
4394 int row
= m_currentCellCoords
.GetRow();
4395 int col
= m_currentCellCoords
.GetCol();
4397 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4398 attr
->GetEditor()->Show( FALSE
);
4400 m_gridWin
->SetFocus();
4405 void wxGrid::SetEditControlValue( const wxString
& value
)
4407 // RD: The new Editors get the value from the table themselves now. This
4408 // method can probably be removed...
4412 void wxGrid::SaveEditControlValue()
4414 if ( IsCellEditControlEnabled() )
4416 int row
= m_currentCellCoords
.GetRow();
4417 int col
= m_currentCellCoords
.GetCol();
4419 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4420 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4426 SendEvent( EVT_GRID_CELL_CHANGE
,
4427 m_currentCellCoords
.GetRow(),
4428 m_currentCellCoords
.GetCol() );
4435 // ------ Grid location functions
4436 // Note that all of these functions work with the logical coordinates of
4437 // grid cells and labels so you will need to convert from device
4438 // coordinates for mouse events etc.
4441 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4443 int row
= YToRow(y
);
4444 int col
= XToCol(x
);
4446 if ( row
== -1 || col
== -1 )
4448 coords
= wxGridNoCellCoords
;
4452 coords
.Set( row
, col
);
4457 int wxGrid::YToRow( int y
)
4461 for ( i
= 0; i
< m_numRows
; i
++ )
4463 if ( y
< m_rowBottoms
[i
] ) return i
;
4466 return m_numRows
; //-1;
4470 int wxGrid::XToCol( int x
)
4474 for ( i
= 0; i
< m_numCols
; i
++ )
4476 if ( x
< m_colRights
[i
] ) return i
;
4479 return m_numCols
; //-1;
4483 // return the row number that that the y coord is near the edge of, or
4484 // -1 if not near an edge
4486 int wxGrid::YToEdgeOfRow( int y
)
4490 for ( i
= 0; i
< m_numRows
; i
++ )
4492 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4494 d
= abs( y
- m_rowBottoms
[i
] );
4496 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4505 // return the col number that that the x coord is near the edge of, or
4506 // -1 if not near an edge
4508 int wxGrid::XToEdgeOfCol( int x
)
4512 for ( i
= 0; i
< m_numCols
; i
++ )
4514 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4516 d
= abs( x
- m_colRights
[i
] );
4518 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4527 wxRect
wxGrid::CellToRect( int row
, int col
)
4529 wxRect
rect( -1, -1, -1, -1 );
4531 if ( row
>= 0 && row
< m_numRows
&&
4532 col
>= 0 && col
< m_numCols
)
4534 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4535 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4536 rect
.width
= m_colWidths
[col
];
4537 rect
.height
= m_rowHeights
[ row
];
4544 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4546 // get the cell rectangle in logical coords
4548 wxRect
r( CellToRect( row
, col
) );
4550 // convert to device coords
4552 int left
, top
, right
, bottom
;
4553 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4554 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4556 // check against the client area of the grid window
4559 m_gridWin
->GetClientSize( &cw
, &ch
);
4561 if ( wholeCellVisible
)
4563 // is the cell wholly visible ?
4565 return ( left
>= 0 && right
<= cw
&&
4566 top
>= 0 && bottom
<= ch
);
4570 // is the cell partly visible ?
4572 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4573 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4578 // make the specified cell location visible by doing a minimal amount
4581 void wxGrid::MakeCellVisible( int row
, int col
)
4584 int xpos
= -1, ypos
= -1;
4586 if ( row
>= 0 && row
< m_numRows
&&
4587 col
>= 0 && col
< m_numCols
)
4589 // get the cell rectangle in logical coords
4591 wxRect
r( CellToRect( row
, col
) );
4593 // convert to device coords
4595 int left
, top
, right
, bottom
;
4596 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4597 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4600 m_gridWin
->GetClientSize( &cw
, &ch
);
4606 else if ( bottom
> ch
)
4608 int h
= r
.GetHeight();
4610 for ( i
= row
-1; i
>= 0; i
-- )
4612 if ( h
+ m_rowHeights
[i
] > ch
) break;
4614 h
+= m_rowHeights
[i
];
4615 ypos
-= m_rowHeights
[i
];
4618 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4619 // have rounding errors (this is important, because if we do, we
4620 // might not scroll at all and some cells won't be redrawn)
4621 ypos
+= GRID_SCROLL_LINE
/ 2;
4628 else if ( right
> cw
)
4630 int w
= r
.GetWidth();
4632 for ( i
= col
-1; i
>= 0; i
-- )
4634 if ( w
+ m_colWidths
[i
] > cw
) break;
4636 w
+= m_colWidths
[i
];
4637 xpos
-= m_colWidths
[i
];
4640 // see comment for ypos above
4641 xpos
+= GRID_SCROLL_LINE
/ 2;
4644 if ( xpos
!= -1 || ypos
!= -1 )
4646 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4647 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4648 Scroll( xpos
, ypos
);
4656 // ------ Grid cursor movement functions
4659 bool wxGrid::MoveCursorUp()
4661 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4662 m_currentCellCoords
.GetRow() > 0 )
4664 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4665 m_currentCellCoords
.GetCol() );
4667 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4668 m_currentCellCoords
.GetCol() );
4677 bool wxGrid::MoveCursorDown()
4679 // TODO: allow for scrolling
4681 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4682 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4684 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4685 m_currentCellCoords
.GetCol() );
4687 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4688 m_currentCellCoords
.GetCol() );
4697 bool wxGrid::MoveCursorLeft()
4699 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4700 m_currentCellCoords
.GetCol() > 0 )
4702 MakeCellVisible( m_currentCellCoords
.GetRow(),
4703 m_currentCellCoords
.GetCol() - 1 );
4705 SetCurrentCell( m_currentCellCoords
.GetRow(),
4706 m_currentCellCoords
.GetCol() - 1 );
4715 bool wxGrid::MoveCursorRight()
4717 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4718 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4720 MakeCellVisible( m_currentCellCoords
.GetRow(),
4721 m_currentCellCoords
.GetCol() + 1 );
4723 SetCurrentCell( m_currentCellCoords
.GetRow(),
4724 m_currentCellCoords
.GetCol() + 1 );
4733 bool wxGrid::MovePageUp()
4735 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4737 int row
= m_currentCellCoords
.GetRow();
4741 m_gridWin
->GetClientSize( &cw
, &ch
);
4743 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4744 int newRow
= YToRow( y
- ch
+ 1 );
4749 else if ( newRow
== row
)
4754 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4755 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4763 bool wxGrid::MovePageDown()
4765 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4767 int row
= m_currentCellCoords
.GetRow();
4768 if ( row
< m_numRows
)
4771 m_gridWin
->GetClientSize( &cw
, &ch
);
4773 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4774 int newRow
= YToRow( y
+ ch
);
4777 newRow
= m_numRows
- 1;
4779 else if ( newRow
== row
)
4784 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4785 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4793 bool wxGrid::MoveCursorUpBlock()
4796 m_currentCellCoords
!= wxGridNoCellCoords
&&
4797 m_currentCellCoords
.GetRow() > 0 )
4799 int row
= m_currentCellCoords
.GetRow();
4800 int col
= m_currentCellCoords
.GetCol();
4802 if ( m_table
->IsEmptyCell(row
, col
) )
4804 // starting in an empty cell: find the next block of
4810 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4813 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4815 // starting at the top of a block: find the next block
4821 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4826 // starting within a block: find the top of the block
4831 if ( m_table
->IsEmptyCell(row
, col
) )
4839 MakeCellVisible( row
, col
);
4840 SetCurrentCell( row
, col
);
4848 bool wxGrid::MoveCursorDownBlock()
4851 m_currentCellCoords
!= wxGridNoCellCoords
&&
4852 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4854 int row
= m_currentCellCoords
.GetRow();
4855 int col
= m_currentCellCoords
.GetCol();
4857 if ( m_table
->IsEmptyCell(row
, col
) )
4859 // starting in an empty cell: find the next block of
4862 while ( row
< m_numRows
-1 )
4865 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4868 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4870 // starting at the bottom of a block: find the next block
4873 while ( row
< m_numRows
-1 )
4876 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4881 // starting within a block: find the bottom of the block
4883 while ( row
< m_numRows
-1 )
4886 if ( m_table
->IsEmptyCell(row
, col
) )
4894 MakeCellVisible( row
, col
);
4895 SetCurrentCell( row
, col
);
4903 bool wxGrid::MoveCursorLeftBlock()
4906 m_currentCellCoords
!= wxGridNoCellCoords
&&
4907 m_currentCellCoords
.GetCol() > 0 )
4909 int row
= m_currentCellCoords
.GetRow();
4910 int col
= m_currentCellCoords
.GetCol();
4912 if ( m_table
->IsEmptyCell(row
, col
) )
4914 // starting in an empty cell: find the next block of
4920 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4923 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4925 // starting at the left of a block: find the next block
4931 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4936 // starting within a block: find the left of the block
4941 if ( m_table
->IsEmptyCell(row
, col
) )
4949 MakeCellVisible( row
, col
);
4950 SetCurrentCell( row
, col
);
4958 bool wxGrid::MoveCursorRightBlock()
4961 m_currentCellCoords
!= wxGridNoCellCoords
&&
4962 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4964 int row
= m_currentCellCoords
.GetRow();
4965 int col
= m_currentCellCoords
.GetCol();
4967 if ( m_table
->IsEmptyCell(row
, col
) )
4969 // starting in an empty cell: find the next block of
4972 while ( col
< m_numCols
-1 )
4975 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4978 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4980 // starting at the right of a block: find the next block
4983 while ( col
< m_numCols
-1 )
4986 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4991 // starting within a block: find the right of the block
4993 while ( col
< m_numCols
-1 )
4996 if ( m_table
->IsEmptyCell(row
, col
) )
5004 MakeCellVisible( row
, col
);
5005 SetCurrentCell( row
, col
);
5016 // ------ Label values and formatting
5019 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
5021 *horiz
= m_rowLabelHorizAlign
;
5022 *vert
= m_rowLabelVertAlign
;
5025 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
5027 *horiz
= m_colLabelHorizAlign
;
5028 *vert
= m_colLabelVertAlign
;
5031 wxString
wxGrid::GetRowLabelValue( int row
)
5035 return m_table
->GetRowLabelValue( row
);
5045 wxString
wxGrid::GetColLabelValue( int col
)
5049 return m_table
->GetColLabelValue( col
);
5060 void wxGrid::SetRowLabelSize( int width
)
5062 width
= wxMax( width
, 0 );
5063 if ( width
!= m_rowLabelWidth
)
5067 m_rowLabelWin
->Show( FALSE
);
5068 m_cornerLabelWin
->Show( FALSE
);
5070 else if ( m_rowLabelWidth
== 0 )
5072 m_rowLabelWin
->Show( TRUE
);
5073 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5076 m_rowLabelWidth
= width
;
5083 void wxGrid::SetColLabelSize( int height
)
5085 height
= wxMax( height
, 0 );
5086 if ( height
!= m_colLabelHeight
)
5090 m_colLabelWin
->Show( FALSE
);
5091 m_cornerLabelWin
->Show( FALSE
);
5093 else if ( m_colLabelHeight
== 0 )
5095 m_colLabelWin
->Show( TRUE
);
5096 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5099 m_colLabelHeight
= height
;
5106 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5108 if ( m_labelBackgroundColour
!= colour
)
5110 m_labelBackgroundColour
= colour
;
5111 m_rowLabelWin
->SetBackgroundColour( colour
);
5112 m_colLabelWin
->SetBackgroundColour( colour
);
5113 m_cornerLabelWin
->SetBackgroundColour( colour
);
5115 if ( !GetBatchCount() )
5117 m_rowLabelWin
->Refresh();
5118 m_colLabelWin
->Refresh();
5119 m_cornerLabelWin
->Refresh();
5124 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5126 if ( m_labelTextColour
!= colour
)
5128 m_labelTextColour
= colour
;
5129 if ( !GetBatchCount() )
5131 m_rowLabelWin
->Refresh();
5132 m_colLabelWin
->Refresh();
5137 void wxGrid::SetLabelFont( const wxFont
& font
)
5140 if ( !GetBatchCount() )
5142 m_rowLabelWin
->Refresh();
5143 m_colLabelWin
->Refresh();
5147 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5149 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5151 m_rowLabelHorizAlign
= horiz
;
5154 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5156 m_rowLabelVertAlign
= vert
;
5159 if ( !GetBatchCount() )
5161 m_rowLabelWin
->Refresh();
5165 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5167 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5169 m_colLabelHorizAlign
= horiz
;
5172 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5174 m_colLabelVertAlign
= vert
;
5177 if ( !GetBatchCount() )
5179 m_colLabelWin
->Refresh();
5183 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5187 m_table
->SetRowLabelValue( row
, s
);
5188 if ( !GetBatchCount() )
5190 wxRect rect
= CellToRect( row
, 0);
5191 if ( rect
.height
> 0 )
5193 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5195 rect
.width
= m_rowLabelWidth
;
5196 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5202 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5206 m_table
->SetColLabelValue( col
, s
);
5207 if ( !GetBatchCount() )
5209 wxRect rect
= CellToRect( 0, col
);
5210 if ( rect
.width
> 0 )
5212 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5214 rect
.height
= m_colLabelHeight
;
5215 m_colLabelWin
->Refresh( TRUE
, &rect
);
5221 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5223 if ( m_gridLineColour
!= colour
)
5225 m_gridLineColour
= colour
;
5227 wxClientDC
dc( m_gridWin
);
5229 DrawAllGridLines( dc
, wxRegion() );
5233 void wxGrid::EnableGridLines( bool enable
)
5235 if ( enable
!= m_gridLinesEnabled
)
5237 m_gridLinesEnabled
= enable
;
5239 if ( !GetBatchCount() )
5243 wxClientDC
dc( m_gridWin
);
5245 DrawAllGridLines( dc
, wxRegion() );
5249 m_gridWin
->Refresh();
5256 int wxGrid::GetDefaultRowSize()
5258 return m_defaultRowHeight
;
5261 int wxGrid::GetRowSize( int row
)
5263 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5265 return m_rowHeights
[row
];
5268 int wxGrid::GetDefaultColSize()
5270 return m_defaultColWidth
;
5273 int wxGrid::GetColSize( int col
)
5275 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5277 return m_colWidths
[col
];
5280 // ============================================================================
5281 // access to the grid attributes: each of them has a default value in the grid
5282 // itself and may be overidden on a per-cell basis
5283 // ============================================================================
5285 // ----------------------------------------------------------------------------
5286 // setting default attributes
5287 // ----------------------------------------------------------------------------
5289 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5291 m_defaultCellAttr
->SetBackgroundColour(col
);
5294 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5296 m_defaultCellAttr
->SetTextColour(col
);
5299 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5301 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5304 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5306 m_defaultCellAttr
->SetFont(font
);
5309 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5311 m_defaultCellAttr
->SetRenderer(renderer
);
5314 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5316 m_defaultCellAttr
->SetEditor(editor
);
5319 // ----------------------------------------------------------------------------
5320 // access to the default attrbiutes
5321 // ----------------------------------------------------------------------------
5323 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5325 return m_defaultCellAttr
->GetBackgroundColour();
5328 wxColour
wxGrid::GetDefaultCellTextColour()
5330 return m_defaultCellAttr
->GetTextColour();
5333 wxFont
wxGrid::GetDefaultCellFont()
5335 return m_defaultCellAttr
->GetFont();
5338 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5340 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5343 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5345 return m_defaultCellAttr
->GetRenderer();
5348 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5350 return m_defaultCellAttr
->GetEditor();
5353 // ----------------------------------------------------------------------------
5354 // access to cell attributes
5355 // ----------------------------------------------------------------------------
5357 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5359 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5360 wxColour colour
= attr
->GetBackgroundColour();
5365 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5367 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5368 wxColour colour
= attr
->GetTextColour();
5373 wxFont
wxGrid::GetCellFont( int row
, int col
)
5375 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5376 wxFont font
= attr
->GetFont();
5381 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5383 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5384 attr
->GetAlignment(horiz
, vert
);
5388 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5390 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5391 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5396 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5398 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5399 wxGridCellEditor
* editor
= attr
->GetEditor();
5404 bool wxGrid::IsReadOnly(int row
, int col
) const
5406 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5407 bool isReadOnly
= attr
->IsReadOnly();
5412 // ----------------------------------------------------------------------------
5413 // attribute support: cache, automatic provider creation, ...
5414 // ----------------------------------------------------------------------------
5416 bool wxGrid::CanHaveAttributes()
5423 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5424 // table is providing the attributes itself??? In which case
5425 // I don't think the grid should create a Provider object for the
5426 // table but the table should be smart enough to do that on its own.
5427 if ( !m_table
->GetAttrProvider() )
5429 // use the default attr provider by default
5430 // (another choice would be to just return FALSE thus forcing the user
5432 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5438 void wxGrid::ClearAttrCache()
5440 if ( m_attrCache
.row
!= -1 )
5442 m_attrCache
.attr
->SafeDecRef();
5443 m_attrCache
.row
= -1;
5447 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5449 wxGrid
*self
= (wxGrid
*)this; // const_cast
5451 self
->ClearAttrCache();
5452 self
->m_attrCache
.row
= row
;
5453 self
->m_attrCache
.col
= col
;
5454 self
->m_attrCache
.attr
= attr
;
5458 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5460 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5462 *attr
= m_attrCache
.attr
;
5463 (*attr
)->SafeIncRef();
5465 #ifdef DEBUG_ATTR_CACHE
5466 gs_nAttrCacheHits
++;
5473 #ifdef DEBUG_ATTR_CACHE
5474 gs_nAttrCacheMisses
++;
5480 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5482 wxGridCellAttr
*attr
;
5483 if ( !LookupAttr(row
, col
, &attr
) )
5485 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5486 CacheAttr(row
, col
, attr
);
5489 attr
->SetDefAttr(m_defaultCellAttr
);
5491 attr
= m_defaultCellAttr
;
5498 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5500 wxGridCellAttr
*attr
;
5501 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5503 wxASSERT_MSG( m_table
,
5504 _T("we may only be called if CanHaveAttributes() "
5505 "returned TRUE and then m_table should be !NULL") );
5507 attr
= m_table
->GetAttr(row
, col
);
5510 attr
= new wxGridCellAttr
;
5512 // artificially inc the ref count to match DecRef() in caller
5515 m_table
->SetAttr(attr
, row
, col
);
5518 CacheAttr(row
, col
, attr
);
5520 attr
->SetDefAttr(m_defaultCellAttr
);
5524 // ----------------------------------------------------------------------------
5525 // setting cell attributes: this is forwarded to the table
5526 // ----------------------------------------------------------------------------
5528 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5530 if ( CanHaveAttributes() )
5532 m_table
->SetRowAttr(attr
, row
);
5540 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5542 if ( CanHaveAttributes() )
5544 m_table
->SetColAttr(attr
, col
);
5552 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5554 if ( CanHaveAttributes() )
5556 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5557 attr
->SetBackgroundColour(colour
);
5562 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5564 if ( CanHaveAttributes() )
5566 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5567 attr
->SetTextColour(colour
);
5572 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5574 if ( CanHaveAttributes() )
5576 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5577 attr
->SetFont(font
);
5582 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5584 if ( CanHaveAttributes() )
5586 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5587 attr
->SetAlignment(horiz
, vert
);
5592 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5594 if ( CanHaveAttributes() )
5596 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5597 attr
->SetRenderer(renderer
);
5602 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5604 if ( CanHaveAttributes() )
5606 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5607 attr
->SetEditor(editor
);
5612 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
5614 if ( CanHaveAttributes() )
5616 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5617 attr
->SetReadOnly(isReadOnly
);
5622 // ----------------------------------------------------------------------------
5624 // ----------------------------------------------------------------------------
5626 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5628 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5630 if ( resizeExistingRows
)
5634 for ( row
= 0; row
< m_numRows
; row
++ )
5636 m_rowHeights
[row
] = m_defaultRowHeight
;
5637 bottom
+= m_defaultRowHeight
;
5638 m_rowBottoms
[row
] = bottom
;
5644 void wxGrid::SetRowSize( int row
, int height
)
5646 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5650 int h
= wxMax( 0, height
);
5651 int diff
= h
- m_rowHeights
[row
];
5653 m_rowHeights
[row
] = h
;
5654 for ( i
= row
; i
< m_numRows
; i
++ )
5656 m_rowBottoms
[i
] += diff
;
5661 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5663 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5665 if ( resizeExistingCols
)
5669 for ( col
= 0; col
< m_numCols
; col
++ )
5671 m_colWidths
[col
] = m_defaultColWidth
;
5672 right
+= m_defaultColWidth
;
5673 m_colRights
[col
] = right
;
5679 void wxGrid::SetColSize( int col
, int width
)
5681 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5685 int w
= wxMax( 0, width
);
5686 int diff
= w
- m_colWidths
[col
];
5687 m_colWidths
[col
] = w
;
5689 for ( i
= col
; i
< m_numCols
; i
++ )
5691 m_colRights
[i
] += diff
;
5698 // ------ cell value accessor functions
5701 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5705 m_table
->SetValue( row
, col
, s
.c_str() );
5706 if ( !GetBatchCount() )
5708 wxClientDC
dc( m_gridWin
);
5710 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5713 #if 0 // TODO: edit in place
5715 if ( m_currentCellCoords
.GetRow() == row
&&
5716 m_currentCellCoords
.GetCol() == col
)
5718 SetEditControlValue( s
);
5727 // ------ Block, row and col selection
5730 void wxGrid::SelectRow( int row
, bool addToSelected
)
5734 if ( IsSelection() && addToSelected
)
5737 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5740 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5741 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5742 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5743 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5747 need_refresh
[0] = TRUE
;
5748 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5749 wxGridCellCoords ( oldTop
- 1,
5751 m_selectedTopLeft
.SetRow( row
);
5756 need_refresh
[1] = TRUE
;
5757 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5758 wxGridCellCoords ( oldBottom
,
5761 m_selectedTopLeft
.SetCol( 0 );
5764 if ( oldBottom
< row
)
5766 need_refresh
[2] = TRUE
;
5767 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5768 wxGridCellCoords ( row
,
5770 m_selectedBottomRight
.SetRow( row
);
5773 if ( oldRight
< m_numCols
- 1 )
5775 need_refresh
[3] = TRUE
;
5776 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5778 wxGridCellCoords ( oldBottom
,
5780 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5783 for (i
= 0; i
< 4; i
++ )
5784 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5785 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5789 r
= SelectionToDeviceRect();
5791 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5793 m_selectedTopLeft
.Set( row
, 0 );
5794 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5795 r
= SelectionToDeviceRect();
5796 m_gridWin
->Refresh( FALSE
, &r
);
5799 wxGridRangeSelectEvent
gridEvt( GetId(),
5800 EVT_GRID_RANGE_SELECT
,
5803 m_selectedBottomRight
);
5805 GetEventHandler()->ProcessEvent(gridEvt
);
5809 void wxGrid::SelectCol( int col
, bool addToSelected
)
5811 if ( IsSelection() && addToSelected
)
5814 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5817 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5818 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5819 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5820 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5822 if ( oldLeft
> col
)
5824 need_refresh
[0] = TRUE
;
5825 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5826 wxGridCellCoords ( m_numRows
- 1,
5828 m_selectedTopLeft
.SetCol( col
);
5833 need_refresh
[1] = TRUE
;
5834 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5835 wxGridCellCoords ( oldTop
- 1,
5837 m_selectedTopLeft
.SetRow( 0 );
5840 if ( oldRight
< col
)
5842 need_refresh
[2] = TRUE
;
5843 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5844 wxGridCellCoords ( m_numRows
- 1,
5846 m_selectedBottomRight
.SetCol( col
);
5849 if ( oldBottom
< m_numRows
- 1 )
5851 need_refresh
[3] = TRUE
;
5852 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5854 wxGridCellCoords ( m_numRows
- 1,
5856 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5859 for (i
= 0; i
< 4; i
++ )
5860 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5861 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5867 r
= SelectionToDeviceRect();
5869 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5871 m_selectedTopLeft
.Set( 0, col
);
5872 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5873 r
= SelectionToDeviceRect();
5874 m_gridWin
->Refresh( FALSE
, &r
);
5877 wxGridRangeSelectEvent
gridEvt( GetId(),
5878 EVT_GRID_RANGE_SELECT
,
5881 m_selectedBottomRight
);
5883 GetEventHandler()->ProcessEvent(gridEvt
);
5887 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5890 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5892 if ( topRow
> bottomRow
)
5899 if ( leftCol
> rightCol
)
5906 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5907 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5909 if ( m_selectedTopLeft
!= updateTopLeft
||
5910 m_selectedBottomRight
!= updateBottomRight
)
5912 // Compute two optimal update rectangles:
5913 // Either one rectangle is a real subset of the
5914 // other, or they are (almost) disjoint!
5916 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5919 // Store intermediate values
5920 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5921 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5922 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5923 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5925 // Determine the outer/inner coordinates.
5926 if (oldLeft
> leftCol
)
5932 if (oldTop
> topRow
)
5938 if (oldRight
< rightCol
)
5941 oldRight
= rightCol
;
5944 if (oldBottom
< bottomRow
)
5947 oldBottom
= bottomRow
;
5951 // Now, either the stuff marked old is the outer
5952 // rectangle or we don't have a situation where one
5953 // is contained in the other.
5955 if ( oldLeft
< leftCol
)
5957 need_refresh
[0] = TRUE
;
5958 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5960 wxGridCellCoords ( oldBottom
,
5964 if ( oldTop
< topRow
)
5966 need_refresh
[1] = TRUE
;
5967 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5969 wxGridCellCoords ( topRow
- 1,
5973 if ( oldRight
> rightCol
)
5975 need_refresh
[2] = TRUE
;
5976 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5978 wxGridCellCoords ( oldBottom
,
5982 if ( oldBottom
> bottomRow
)
5984 need_refresh
[3] = TRUE
;
5985 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5987 wxGridCellCoords ( oldBottom
,
5993 m_selectedTopLeft
= updateTopLeft
;
5994 m_selectedBottomRight
= updateBottomRight
;
5996 // various Refresh() calls
5997 for (i
= 0; i
< 4; i
++ )
5998 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5999 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6002 // only generate an event if the block is not being selected by
6003 // dragging the mouse (in which case the event will be generated in
6004 // the mouse event handler)
6005 if ( !m_isDragging
)
6007 wxGridRangeSelectEvent
gridEvt( GetId(),
6008 EVT_GRID_RANGE_SELECT
,
6011 m_selectedBottomRight
);
6013 GetEventHandler()->ProcessEvent(gridEvt
);
6017 void wxGrid::SelectAll()
6019 m_selectedTopLeft
.Set( 0, 0 );
6020 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
6022 m_gridWin
->Refresh();
6026 void wxGrid::ClearSelection()
6028 m_selectedTopLeft
= wxGridNoCellCoords
;
6029 m_selectedBottomRight
= wxGridNoCellCoords
;
6033 // This function returns the rectangle that encloses the given block
6034 // in device coords clipped to the client size of the grid window.
6036 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
6037 const wxGridCellCoords
&bottomRight
)
6039 wxRect
rect( wxGridNoCellRect
);
6042 cellRect
= CellToRect( topLeft
);
6043 if ( cellRect
!= wxGridNoCellRect
)
6049 rect
= wxRect( 0, 0, 0, 0 );
6052 cellRect
= CellToRect( bottomRight
);
6053 if ( cellRect
!= wxGridNoCellRect
)
6059 return wxGridNoCellRect
;
6062 // convert to scrolled coords
6064 int left
, top
, right
, bottom
;
6065 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
6066 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
6069 m_gridWin
->GetClientSize( &cw
, &ch
);
6071 rect
.SetLeft( wxMax(0, left
) );
6072 rect
.SetTop( wxMax(0, top
) );
6073 rect
.SetRight( wxMin(cw
, right
) );
6074 rect
.SetBottom( wxMin(ch
, bottom
) );
6082 // ------ Grid event classes
6085 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
6087 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
6088 int row
, int col
, int x
, int y
,
6089 bool control
, bool shift
, bool alt
, bool meta
)
6090 : wxNotifyEvent( type
, id
)
6096 m_control
= control
;
6101 SetEventObject(obj
);
6105 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6107 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6108 int rowOrCol
, int x
, int y
,
6109 bool control
, bool shift
, bool alt
, bool meta
)
6110 : wxNotifyEvent( type
, id
)
6112 m_rowOrCol
= rowOrCol
;
6115 m_control
= control
;
6120 SetEventObject(obj
);
6124 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6126 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6127 const wxGridCellCoords
& topLeft
,
6128 const wxGridCellCoords
& bottomRight
,
6129 bool control
, bool shift
, bool alt
, bool meta
)
6130 : wxNotifyEvent( type
, id
)
6132 m_topLeft
= topLeft
;
6133 m_bottomRight
= bottomRight
;
6134 m_control
= control
;
6139 SetEventObject(obj
);
6143 #endif // ifndef wxUSE_NEW_GRID