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
);
551 dc
.SetTextBackground( grid
.GetSelectionBackground() );
552 dc
.SetTextForeground( grid
.GetSelectionForeground() );
556 dc
.SetTextBackground( attr
.GetBackgroundColour() );
557 dc
.SetTextForeground( attr
.GetTextColour() );
559 dc
.SetFont( attr
.GetFont() );
562 attr
.GetAlignment(&hAlign
, &vAlign
);
564 wxRect rect
= rectCell
;
570 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
571 rect
, hAlign
, vAlign
);
574 // ----------------------------------------------------------------------------
576 // ----------------------------------------------------------------------------
578 const wxColour
& wxGridCellAttr::GetTextColour() const
582 else if (m_defGridAttr
!= this)
583 return m_defGridAttr
->GetTextColour();
585 wxFAIL_MSG(wxT("Missing default cell attribute"));
591 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
593 if (HasBackgroundColour())
595 else if (m_defGridAttr
!= this)
596 return m_defGridAttr
->GetBackgroundColour();
598 wxFAIL_MSG(wxT("Missing default cell attribute"));
604 const wxFont
& wxGridCellAttr::GetFont() const
608 else if (m_defGridAttr
!= this)
609 return m_defGridAttr
->GetFont();
611 wxFAIL_MSG(wxT("Missing default cell attribute"));
617 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
619 if (HasAlignment()) {
620 if ( hAlign
) *hAlign
= m_hAlign
;
621 if ( vAlign
) *vAlign
= m_vAlign
;
623 else if (m_defGridAttr
!= this)
624 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
626 wxFAIL_MSG(wxT("Missing default cell attribute"));
631 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
635 else if (m_defGridAttr
!= this)
636 return m_defGridAttr
->GetRenderer();
638 wxFAIL_MSG(wxT("Missing default cell attribute"));
643 wxGridCellEditor
* wxGridCellAttr::GetEditor() const
647 else if (m_defGridAttr
!= this)
648 return m_defGridAttr
->GetEditor();
650 wxFAIL_MSG(wxT("Missing default cell attribute"));
655 // ----------------------------------------------------------------------------
656 // wxGridCellAttrData
657 // ----------------------------------------------------------------------------
659 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
661 int n
= FindIndex(row
, col
);
662 if ( n
== wxNOT_FOUND
)
665 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
671 // change the attribute
672 m_attrs
[(size_t)n
].attr
= attr
;
676 // remove this attribute
677 m_attrs
.RemoveAt((size_t)n
);
682 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
684 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
686 int n
= FindIndex(row
, col
);
687 if ( n
!= wxNOT_FOUND
)
689 attr
= m_attrs
[(size_t)n
].attr
;
696 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
698 size_t count
= m_attrs
.GetCount();
699 for ( size_t n
= 0; n
< count
; n
++ )
701 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
702 wxCoord row
= coords
.GetRow();
703 if ((size_t)row
>= pos
)
707 // If rows inserted, include row counter where necessary
708 coords
.SetRow(row
+ numRows
);
710 else if (numRows
< 0)
712 // If rows deleted ...
713 if ((size_t)row
>= pos
- numRows
)
715 // ...either decrement row counter (if row still exists)...
716 coords
.SetRow(row
+ numRows
);
720 // ...or remove the attribute
721 m_attrs
.RemoveAt((size_t)n
);
729 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
731 size_t count
= m_attrs
.GetCount();
732 for ( size_t n
= 0; n
< count
; n
++ )
734 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
735 wxCoord col
= coords
.GetCol();
736 if ( (size_t)col
>= pos
)
740 // If rows inserted, include row counter where necessary
741 coords
.SetCol(col
+ numCols
);
743 else if (numCols
< 0)
745 // If rows deleted ...
746 if ((size_t)col
>= pos
- numCols
)
748 // ...either decrement row counter (if row still exists)...
749 coords
.SetCol(col
+ numCols
);
753 // ...or remove the attribute
754 m_attrs
.RemoveAt((size_t)n
);
762 int wxGridCellAttrData::FindIndex(int row
, int col
) const
764 size_t count
= m_attrs
.GetCount();
765 for ( size_t n
= 0; n
< count
; n
++ )
767 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
768 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
777 // ----------------------------------------------------------------------------
778 // wxGridRowOrColAttrData
779 // ----------------------------------------------------------------------------
781 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
783 size_t count
= m_attrs
.Count();
784 for ( size_t n
= 0; n
< count
; n
++ )
786 m_attrs
[n
]->DecRef();
790 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
792 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
794 int n
= m_rowsOrCols
.Index(rowOrCol
);
795 if ( n
!= wxNOT_FOUND
)
797 attr
= m_attrs
[(size_t)n
];
804 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
806 int n
= m_rowsOrCols
.Index(rowOrCol
);
807 if ( n
== wxNOT_FOUND
)
810 m_rowsOrCols
.Add(rowOrCol
);
817 // change the attribute
818 m_attrs
[(size_t)n
] = attr
;
822 // remove this attribute
823 m_attrs
[(size_t)n
]->DecRef();
824 m_rowsOrCols
.RemoveAt((size_t)n
);
825 m_attrs
.RemoveAt((size_t)n
);
830 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
832 size_t count
= m_attrs
.GetCount();
833 for ( size_t n
= 0; n
< count
; n
++ )
835 int & rowOrCol
= m_rowsOrCols
[n
];
836 if ( (size_t)rowOrCol
>= pos
)
838 if ( numRowsOrCols
> 0 )
840 // If rows inserted, include row counter where necessary
841 rowOrCol
+= numRowsOrCols
;
843 else if ( numRowsOrCols
< 0)
845 // If rows deleted, either decrement row counter (if row still exists)
846 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
847 rowOrCol
+= numRowsOrCols
;
850 m_rowsOrCols
.RemoveAt((size_t)n
);
851 m_attrs
.RemoveAt((size_t)n
);
859 // ----------------------------------------------------------------------------
860 // wxGridCellAttrProvider
861 // ----------------------------------------------------------------------------
863 wxGridCellAttrProvider::wxGridCellAttrProvider()
865 m_data
= (wxGridCellAttrProviderData
*)NULL
;
868 wxGridCellAttrProvider::~wxGridCellAttrProvider()
873 void wxGridCellAttrProvider::InitData()
875 m_data
= new wxGridCellAttrProviderData
;
878 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
880 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
883 // first look for the attribute of this specific cell
884 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
888 // then look for the col attr (col attributes are more common than
889 // the row ones, hence they have priority)
890 attr
= m_data
->m_colAttrs
.GetAttr(col
);
895 // finally try the row attributes
896 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
903 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
909 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
912 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
917 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
920 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
925 m_data
->m_colAttrs
.SetAttr(attr
, col
);
928 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
932 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
934 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
938 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
942 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
944 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
948 // ----------------------------------------------------------------------------
950 // ----------------------------------------------------------------------------
952 //////////////////////////////////////////////////////////////////////
954 // Abstract base class for grid data (the model)
956 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
959 wxGridTableBase::wxGridTableBase()
961 m_view
= (wxGrid
*) NULL
;
962 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
965 wxGridTableBase::~wxGridTableBase()
967 delete m_attrProvider
;
970 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
972 delete m_attrProvider
;
973 m_attrProvider
= attrProvider
;
976 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
978 if ( m_attrProvider
)
979 return m_attrProvider
->GetAttr(row
, col
);
981 return (wxGridCellAttr
*)NULL
;
984 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
986 if ( m_attrProvider
)
988 m_attrProvider
->SetAttr(attr
, row
, col
);
992 // as we take ownership of the pointer and don't store it, we must
998 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1000 if ( m_attrProvider
)
1002 m_attrProvider
->SetRowAttr(attr
, row
);
1006 // as we take ownership of the pointer and don't store it, we must
1012 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1014 if ( m_attrProvider
)
1016 m_attrProvider
->SetColAttr(attr
, col
);
1020 // as we take ownership of the pointer and don't store it, we must
1026 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
1028 if ( m_attrProvider
)
1030 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
1034 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
1036 if ( m_attrProvider
)
1038 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
1042 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
1044 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
1045 "but your derived table class does not override this function") );
1050 bool wxGridTableBase::AppendRows( size_t numRows
)
1052 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
1053 "but your derived table class does not override this function"));
1058 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
1060 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
1061 "but your derived table class does not override this function"));
1066 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
1068 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
1069 "but your derived table class does not override this function"));
1074 bool wxGridTableBase::AppendCols( size_t numCols
)
1076 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
1077 "but your derived table class does not override this function"));
1082 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
1084 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
1085 "but your derived table class does not override this function"));
1091 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1098 wxString
wxGridTableBase::GetColLabelValue( int col
)
1100 // default col labels are:
1101 // cols 0 to 25 : A-Z
1102 // cols 26 to 675 : AA-ZZ
1107 for ( n
= 1; ; n
++ )
1109 s
+= (_T('A') + (wxChar
)( col%26
));
1111 if ( col
< 0 ) break;
1114 // reverse the string...
1116 for ( i
= 0; i
< n
; i
++ )
1126 //////////////////////////////////////////////////////////////////////
1128 // Message class for the grid table to send requests and notifications
1132 wxGridTableMessage::wxGridTableMessage()
1134 m_table
= (wxGridTableBase
*) NULL
;
1140 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1141 int commandInt1
, int commandInt2
)
1145 m_comInt1
= commandInt1
;
1146 m_comInt2
= commandInt2
;
1151 //////////////////////////////////////////////////////////////////////
1153 // A basic grid table for string data. An object of this class will
1154 // created by wxGrid if you don't specify an alternative table class.
1157 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1159 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1161 wxGridStringTable::wxGridStringTable()
1166 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1171 m_data
.Alloc( numRows
);
1174 sa
.Alloc( numCols
);
1175 for ( col
= 0; col
< numCols
; col
++ )
1177 sa
.Add( wxEmptyString
);
1180 for ( row
= 0; row
< numRows
; row
++ )
1186 wxGridStringTable::~wxGridStringTable()
1190 long wxGridStringTable::GetNumberRows()
1192 return m_data
.GetCount();
1195 long wxGridStringTable::GetNumberCols()
1197 if ( m_data
.GetCount() > 0 )
1198 return m_data
[0].GetCount();
1203 wxString
wxGridStringTable::GetValue( int row
, int col
)
1205 // TODO: bounds checking
1207 return m_data
[row
][col
];
1210 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
1212 // TODO: bounds checking
1214 m_data
[row
][col
] = s
;
1217 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1219 // TODO: bounds checking
1221 return (m_data
[row
][col
] == wxEmptyString
);
1225 void wxGridStringTable::Clear()
1228 int numRows
, numCols
;
1230 numRows
= m_data
.GetCount();
1233 numCols
= m_data
[0].GetCount();
1235 for ( row
= 0; row
< numRows
; row
++ )
1237 for ( col
= 0; col
< numCols
; col
++ )
1239 m_data
[row
][col
] = wxEmptyString
;
1246 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1250 size_t curNumRows
= m_data
.GetCount();
1251 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1253 if ( pos
>= curNumRows
)
1255 return AppendRows( numRows
);
1259 sa
.Alloc( curNumCols
);
1260 for ( col
= 0; col
< curNumCols
; col
++ )
1262 sa
.Add( wxEmptyString
);
1265 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1267 m_data
.Insert( sa
, row
);
1269 UpdateAttrRows( pos
, numRows
);
1272 wxGridTableMessage
msg( this,
1273 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1277 GetView()->ProcessTableMessage( msg
);
1283 bool wxGridStringTable::AppendRows( size_t numRows
)
1287 size_t curNumRows
= m_data
.GetCount();
1288 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1291 if ( curNumCols
> 0 )
1293 sa
.Alloc( curNumCols
);
1294 for ( col
= 0; col
< curNumCols
; col
++ )
1296 sa
.Add( wxEmptyString
);
1300 for ( row
= 0; row
< numRows
; row
++ )
1307 wxGridTableMessage
msg( this,
1308 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1311 GetView()->ProcessTableMessage( msg
);
1317 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1321 size_t curNumRows
= m_data
.GetCount();
1323 if ( pos
>= curNumRows
)
1326 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1327 "Pos value is invalid for present table with %d rows",
1328 pos
, numRows
, curNumRows
);
1329 wxFAIL_MSG( wxT(errmsg
) );
1333 if ( numRows
> curNumRows
- pos
)
1335 numRows
= curNumRows
- pos
;
1338 if ( numRows
>= curNumRows
)
1340 m_data
.Empty(); // don't release memory just yet
1344 for ( n
= 0; n
< numRows
; n
++ )
1346 m_data
.Remove( pos
);
1349 UpdateAttrRows( pos
, -((int)numRows
) );
1352 wxGridTableMessage
msg( this,
1353 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1357 GetView()->ProcessTableMessage( msg
);
1363 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1367 size_t curNumRows
= m_data
.GetCount();
1368 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1370 if ( pos
>= curNumCols
)
1372 return AppendCols( numCols
);
1375 for ( row
= 0; row
< curNumRows
; row
++ )
1377 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1379 m_data
[row
].Insert( wxEmptyString
, col
);
1382 UpdateAttrCols( pos
, numCols
);
1385 wxGridTableMessage
msg( this,
1386 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1390 GetView()->ProcessTableMessage( msg
);
1396 bool wxGridStringTable::AppendCols( size_t numCols
)
1400 size_t curNumRows
= m_data
.GetCount();
1403 // TODO: something better than this ?
1405 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1406 "Call AppendRows() first") );
1410 for ( row
= 0; row
< curNumRows
; row
++ )
1412 for ( n
= 0; n
< numCols
; n
++ )
1414 m_data
[row
].Add( wxEmptyString
);
1420 wxGridTableMessage
msg( this,
1421 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1424 GetView()->ProcessTableMessage( msg
);
1430 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1434 size_t curNumRows
= m_data
.GetCount();
1435 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1437 if ( pos
>= curNumCols
)
1440 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1441 "Pos value is invalid for present table with %d cols",
1442 pos
, numCols
, curNumCols
);
1443 wxFAIL_MSG( wxT( errmsg
) );
1447 if ( numCols
> curNumCols
- pos
)
1449 numCols
= curNumCols
- pos
;
1452 for ( row
= 0; row
< curNumRows
; row
++ )
1454 if ( numCols
>= curNumCols
)
1456 m_data
[row
].Clear();
1460 for ( n
= 0; n
< numCols
; n
++ )
1462 m_data
[row
].Remove( pos
);
1466 UpdateAttrCols( pos
, -((int)numCols
) );
1469 wxGridTableMessage
msg( this,
1470 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1474 GetView()->ProcessTableMessage( msg
);
1480 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1482 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1484 // using default label
1486 return wxGridTableBase::GetRowLabelValue( row
);
1490 return m_rowLabels
[ row
];
1494 wxString
wxGridStringTable::GetColLabelValue( int col
)
1496 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1498 // using default label
1500 return wxGridTableBase::GetColLabelValue( col
);
1504 return m_colLabels
[ col
];
1508 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1510 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1512 int n
= m_rowLabels
.GetCount();
1514 for ( i
= n
; i
<= row
; i
++ )
1516 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1520 m_rowLabels
[row
] = value
;
1523 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1525 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1527 int n
= m_colLabels
.GetCount();
1529 for ( i
= n
; i
<= col
; i
++ )
1531 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1535 m_colLabels
[col
] = value
;
1540 //////////////////////////////////////////////////////////////////////
1541 //////////////////////////////////////////////////////////////////////
1543 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1545 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1546 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1547 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1548 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1551 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1553 const wxPoint
&pos
, const wxSize
&size
)
1554 : wxWindow( parent
, id
, pos
, size
)
1559 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1563 // NO - don't do this because it will set both the x and y origin
1564 // coords to match the parent scrolled window and we just want to
1565 // set the y coord - MB
1567 // m_owner->PrepareDC( dc );
1570 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1571 dc
.SetDeviceOrigin( 0, -y
);
1573 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1574 m_owner
->DrawRowLabels( dc
);
1578 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1580 m_owner
->ProcessRowLabelMouseEvent( event
);
1584 // This seems to be required for wxMotif otherwise the mouse
1585 // cursor must be in the cell edit control to get key events
1587 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1589 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1594 //////////////////////////////////////////////////////////////////////
1596 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1598 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1599 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1600 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1601 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1604 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1606 const wxPoint
&pos
, const wxSize
&size
)
1607 : wxWindow( parent
, id
, pos
, size
)
1612 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1616 // NO - don't do this because it will set both the x and y origin
1617 // coords to match the parent scrolled window and we just want to
1618 // set the x coord - MB
1620 // m_owner->PrepareDC( dc );
1623 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1624 dc
.SetDeviceOrigin( -x
, 0 );
1626 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1627 m_owner
->DrawColLabels( dc
);
1631 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1633 m_owner
->ProcessColLabelMouseEvent( event
);
1637 // This seems to be required for wxMotif otherwise the mouse
1638 // cursor must be in the cell edit control to get key events
1640 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1642 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1647 //////////////////////////////////////////////////////////////////////
1649 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1651 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1652 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1653 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1654 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1657 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1659 const wxPoint
&pos
, const wxSize
&size
)
1660 : wxWindow( parent
, id
, pos
, size
)
1665 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1669 int client_height
= 0;
1670 int client_width
= 0;
1671 GetClientSize( &client_width
, &client_height
);
1673 dc
.SetPen( *wxBLACK_PEN
);
1674 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1675 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1677 dc
.SetPen( *wxWHITE_PEN
);
1678 dc
.DrawLine( 0, 0, client_width
, 0 );
1679 dc
.DrawLine( 0, 0, 0, client_height
);
1683 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1685 m_owner
->ProcessCornerLabelMouseEvent( event
);
1689 // This seems to be required for wxMotif otherwise the mouse
1690 // cursor must be in the cell edit control to get key events
1692 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1694 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1699 //////////////////////////////////////////////////////////////////////
1701 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1703 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1704 EVT_PAINT( wxGridWindow::OnPaint
)
1705 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1706 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1707 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1710 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1711 wxGridRowLabelWindow
*rowLblWin
,
1712 wxGridColLabelWindow
*colLblWin
,
1713 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1714 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1717 m_rowLabelWin
= rowLblWin
;
1718 m_colLabelWin
= colLblWin
;
1719 SetBackgroundColour( "WHITE" );
1723 wxGridWindow::~wxGridWindow()
1728 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1730 wxPaintDC
dc( this );
1731 m_owner
->PrepareDC( dc
);
1732 wxRegion reg
= GetUpdateRegion();
1733 m_owner
->CalcCellsExposed( reg
);
1734 m_owner
->DrawGridCellArea( dc
);
1735 #if WXGRID_DRAW_LINES
1736 m_owner
->DrawAllGridLines( dc
, reg
);
1741 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1743 wxPanel::ScrollWindow( dx
, dy
, rect
);
1744 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1745 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1749 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1751 m_owner
->ProcessGridCellMouseEvent( event
);
1755 // This seems to be required for wxMotif otherwise the mouse
1756 // cursor must be in the cell edit control to get key events
1758 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1760 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1763 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1769 //////////////////////////////////////////////////////////////////////
1772 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1774 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1775 EVT_PAINT( wxGrid::OnPaint
)
1776 EVT_SIZE( wxGrid::OnSize
)
1777 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1778 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1781 wxGrid::wxGrid( wxWindow
*parent
,
1786 const wxString
& name
)
1787 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1796 m_defaultCellAttr
->SafeDecRef();
1798 #ifdef DEBUG_ATTR_CACHE
1799 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1800 wxPrintf(_T("wxGrid attribute cache statistics: "
1801 "total: %u, hits: %u (%u%%)\n"),
1802 total
, gs_nAttrCacheHits
,
1803 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1812 // ----- internal init and update functions
1815 void wxGrid::Create()
1817 m_created
= FALSE
; // set to TRUE by CreateGrid
1818 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
1820 m_table
= (wxGridTableBase
*) NULL
;
1823 m_cellEditCtrlEnabled
= FALSE
;
1825 m_defaultCellAttr
= new wxGridCellAttr
;
1826 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1827 // RD: Should we fill the default attrs now or is waiting until Init() okay?
1832 m_currentCellCoords
= wxGridNoCellCoords
;
1834 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1835 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1837 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1842 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1847 m_colLabelWin
= new wxGridColLabelWindow( this,
1852 m_gridWin
= new wxGridWindow( this,
1859 SetTargetWindow( m_gridWin
);
1863 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1867 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1872 m_numRows
= numRows
;
1873 m_numCols
= numCols
;
1875 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1876 m_table
->SetView( this );
1885 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
1889 // RD: Actually, this should probably be allowed. I think it would be
1890 // nice to be able to switch multiple Tables in and out of a single
1891 // View at runtime. Is there anything in the implmentation that would
1894 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1899 m_numRows
= table
->GetNumberRows();
1900 m_numCols
= table
->GetNumberCols();
1903 m_table
->SetView( this );
1918 if ( m_numRows
<= 0 )
1919 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1921 if ( m_numCols
<= 0 )
1922 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1924 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1925 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1927 if ( m_rowLabelWin
)
1929 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1933 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1936 m_labelTextColour
= wxColour( _T("BLACK") );
1939 m_attrCache
.row
= -1;
1941 // TODO: something better than this ?
1943 m_labelFont
= this->GetFont();
1944 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1946 m_rowLabelHorizAlign
= wxLEFT
;
1947 m_rowLabelVertAlign
= wxCENTRE
;
1949 m_colLabelHorizAlign
= wxCENTRE
;
1950 m_colLabelVertAlign
= wxTOP
;
1952 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1953 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1955 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1956 m_defaultRowHeight
+= 8;
1958 m_defaultRowHeight
+= 4;
1961 m_rowHeights
.Alloc( m_numRows
);
1962 m_rowBottoms
.Alloc( m_numRows
);
1964 for ( i
= 0; i
< m_numRows
; i
++ )
1966 m_rowHeights
.Add( m_defaultRowHeight
);
1967 rowBottom
+= m_defaultRowHeight
;
1968 m_rowBottoms
.Add( rowBottom
);
1971 m_colWidths
.Alloc( m_numCols
);
1972 m_colRights
.Alloc( m_numCols
);
1974 for ( i
= 0; i
< m_numCols
; i
++ )
1976 m_colWidths
.Add( m_defaultColWidth
);
1977 colRight
+= m_defaultColWidth
;
1978 m_colRights
.Add( colRight
);
1981 // Set default cell attributes
1982 m_defaultCellAttr
->SetFont(GetFont());
1983 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
1984 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1985 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1986 m_defaultCellAttr
->SetTextColour(
1987 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
1988 m_defaultCellAttr
->SetBackgroundColour(
1989 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
1992 m_gridLineColour
= wxColour( 128, 128, 255 );
1993 m_gridLinesEnabled
= TRUE
;
1995 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1996 m_winCapture
= (wxWindow
*)NULL
;
1998 m_dragRowOrCol
= -1;
1999 m_isDragging
= FALSE
;
2000 m_startDragPos
= wxDefaultPosition
;
2002 m_waitForSlowClick
= FALSE
;
2004 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2005 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2007 m_currentCellCoords
= wxGridNoCellCoords
;
2009 m_selectedTopLeft
= wxGridNoCellCoords
;
2010 m_selectedBottomRight
= wxGridNoCellCoords
;
2011 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
2012 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2014 m_editable
= TRUE
; // default for whole grid
2016 m_inOnKeyDown
= FALSE
;
2022 void wxGrid::CalcDimensions()
2025 GetClientSize( &cw
, &ch
);
2027 if ( m_numRows
> 0 && m_numCols
> 0 )
2029 int right
= m_colRights
[ m_numCols
-1 ] + 50;
2030 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
2032 // TODO: restore the scroll position that we had before sizing
2035 GetViewStart( &x
, &y
);
2036 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
2037 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
2043 void wxGrid::CalcWindowSizes()
2046 GetClientSize( &cw
, &ch
);
2048 if ( m_cornerLabelWin
->IsShown() )
2049 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2051 if ( m_colLabelWin
->IsShown() )
2052 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
2054 if ( m_rowLabelWin
->IsShown() )
2055 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
2057 if ( m_gridWin
->IsShown() )
2058 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
2062 // this is called when the grid table sends a message to say that it
2063 // has been redimensioned
2065 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2069 switch ( msg
.GetId() )
2071 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2073 size_t pos
= msg
.GetCommandInt();
2074 int numRows
= msg
.GetCommandInt2();
2075 for ( i
= 0; i
< numRows
; i
++ )
2077 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
2078 m_rowBottoms
.Insert( 0, pos
);
2080 m_numRows
+= numRows
;
2083 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2085 for ( i
= pos
; i
< m_numRows
; i
++ )
2087 bottom
+= m_rowHeights
[i
];
2088 m_rowBottoms
[i
] = bottom
;
2094 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2096 int numRows
= msg
.GetCommandInt();
2097 for ( i
= 0; i
< numRows
; i
++ )
2099 m_rowHeights
.Add( m_defaultRowHeight
);
2100 m_rowBottoms
.Add( 0 );
2103 int oldNumRows
= m_numRows
;
2104 m_numRows
+= numRows
;
2107 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2109 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2111 bottom
+= m_rowHeights
[i
];
2112 m_rowBottoms
[i
] = bottom
;
2118 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2120 size_t pos
= msg
.GetCommandInt();
2121 int numRows
= msg
.GetCommandInt2();
2122 for ( i
= 0; i
< numRows
; i
++ )
2124 m_rowHeights
.Remove( pos
);
2125 m_rowBottoms
.Remove( pos
);
2127 m_numRows
-= numRows
;
2132 m_colWidths
.Clear();
2133 m_colRights
.Clear();
2134 m_currentCellCoords
= wxGridNoCellCoords
;
2138 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2139 m_currentCellCoords
.Set( 0, 0 );
2142 for ( i
= 0; i
< m_numRows
; i
++ )
2144 h
+= m_rowHeights
[i
];
2145 m_rowBottoms
[i
] = h
;
2153 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2155 size_t pos
= msg
.GetCommandInt();
2156 int numCols
= msg
.GetCommandInt2();
2157 for ( i
= 0; i
< numCols
; i
++ )
2159 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2160 m_colRights
.Insert( 0, pos
);
2162 m_numCols
+= numCols
;
2165 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2167 for ( i
= pos
; i
< m_numCols
; i
++ )
2169 right
+= m_colWidths
[i
];
2170 m_colRights
[i
] = right
;
2176 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2178 int numCols
= msg
.GetCommandInt();
2179 for ( i
= 0; i
< numCols
; i
++ )
2181 m_colWidths
.Add( m_defaultColWidth
);
2182 m_colRights
.Add( 0 );
2185 int oldNumCols
= m_numCols
;
2186 m_numCols
+= numCols
;
2189 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2191 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2193 right
+= m_colWidths
[i
];
2194 m_colRights
[i
] = right
;
2200 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2202 size_t pos
= msg
.GetCommandInt();
2203 int numCols
= msg
.GetCommandInt2();
2204 for ( i
= 0; i
< numCols
; i
++ )
2206 m_colWidths
.Remove( pos
);
2207 m_colRights
.Remove( pos
);
2209 m_numCols
-= numCols
;
2213 #if 0 // leave the row alone here so that AppendCols will work subsequently
2215 m_rowHeights
.Clear();
2216 m_rowBottoms
.Clear();
2218 m_currentCellCoords
= wxGridNoCellCoords
;
2222 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2223 m_currentCellCoords
.Set( 0, 0 );
2226 for ( i
= 0; i
< m_numCols
; i
++ )
2228 w
+= m_colWidths
[i
];
2241 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2243 wxRegionIterator
iter( reg
);
2246 m_rowLabelsExposed
.Empty();
2253 // TODO: remove this when we can...
2254 // There is a bug in wxMotif that gives garbage update
2255 // rectangles if you jump-scroll a long way by clicking the
2256 // scrollbar with middle button. This is a work-around
2258 #if defined(__WXMOTIF__)
2260 m_gridWin
->GetClientSize( &cw
, &ch
);
2261 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2262 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2265 // logical bounds of update region
2268 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2269 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2271 // find the row labels within these bounds
2275 for ( row
= 0; row
< m_numRows
; row
++ )
2277 if ( m_rowBottoms
[row
] < top
) continue;
2279 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2280 if ( rowTop
> bottom
) break;
2282 m_rowLabelsExposed
.Add( row
);
2290 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2292 wxRegionIterator
iter( reg
);
2295 m_colLabelsExposed
.Empty();
2302 // TODO: remove this when we can...
2303 // There is a bug in wxMotif that gives garbage update
2304 // rectangles if you jump-scroll a long way by clicking the
2305 // scrollbar with middle button. This is a work-around
2307 #if defined(__WXMOTIF__)
2309 m_gridWin
->GetClientSize( &cw
, &ch
);
2310 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2311 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2314 // logical bounds of update region
2317 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2318 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2320 // find the cells within these bounds
2324 for ( col
= 0; col
< m_numCols
; col
++ )
2326 if ( m_colRights
[col
] < left
) continue;
2328 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2329 if ( colLeft
> right
) break;
2331 m_colLabelsExposed
.Add( col
);
2339 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2341 wxRegionIterator
iter( reg
);
2344 m_cellsExposed
.Empty();
2345 m_rowsExposed
.Empty();
2346 m_colsExposed
.Empty();
2348 int left
, top
, right
, bottom
;
2353 // TODO: remove this when we can...
2354 // There is a bug in wxMotif that gives garbage update
2355 // rectangles if you jump-scroll a long way by clicking the
2356 // scrollbar with middle button. This is a work-around
2358 #if defined(__WXMOTIF__)
2360 m_gridWin
->GetClientSize( &cw
, &ch
);
2361 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2362 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2363 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2364 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2367 // logical bounds of update region
2369 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2370 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2372 // find the cells within these bounds
2375 int colLeft
, rowTop
;
2376 for ( row
= 0; row
< m_numRows
; row
++ )
2378 if ( m_rowBottoms
[row
] <= top
) continue;
2380 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2381 if ( rowTop
> bottom
) break;
2383 m_rowsExposed
.Add( row
);
2385 for ( col
= 0; col
< m_numCols
; col
++ )
2387 if ( m_colRights
[col
] <= left
) continue;
2389 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2390 if ( colLeft
> right
) break;
2392 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2393 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2402 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2405 wxPoint
pos( event
.GetPosition() );
2406 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2408 if ( event
.Dragging() )
2410 m_isDragging
= TRUE
;
2412 if ( event
.LeftIsDown() )
2414 switch( m_cursorMode
)
2416 case WXGRID_CURSOR_RESIZE_ROW
:
2418 int cw
, ch
, left
, dummy
;
2419 m_gridWin
->GetClientSize( &cw
, &ch
);
2420 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2422 wxClientDC
dc( m_gridWin
);
2425 m_rowBottoms
[m_dragRowOrCol
] -
2426 m_rowHeights
[m_dragRowOrCol
] +
2427 WXGRID_MIN_ROW_HEIGHT
);
2428 dc
.SetLogicalFunction(wxINVERT
);
2429 if ( m_dragLastPos
>= 0 )
2431 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2433 dc
.DrawLine( left
, y
, left
+cw
, y
);
2438 case WXGRID_CURSOR_SELECT_ROW
:
2439 if ( (row
= YToRow( y
)) >= 0 &&
2440 !IsInSelection( row
, 0 ) )
2442 SelectRow( row
, TRUE
);
2445 // default label to suppress warnings about "enumeration value
2446 // 'xxx' not handled in switch
2454 m_isDragging
= FALSE
;
2457 // ------------ Entering or leaving the window
2459 if ( event
.Entering() || event
.Leaving() )
2461 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2465 // ------------ Left button pressed
2467 else if ( event
.LeftDown() )
2469 // don't send a label click event for a hit on the
2470 // edge of the row label - this is probably the user
2471 // wanting to resize the row
2473 if ( YToEdgeOfRow(y
) < 0 )
2477 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2479 SelectRow( row
, event
.ShiftDown() );
2480 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2485 // starting to drag-resize a row
2487 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2492 // ------------ Left double click
2494 else if (event
.LeftDClick() )
2496 if ( YToEdgeOfRow(y
) < 0 )
2499 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2504 // ------------ Left button released
2506 else if ( event
.LeftUp() )
2508 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2510 DoEndDragResizeRow();
2512 // Note: we are ending the event *after* doing
2513 // default processing in this case
2515 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2518 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2523 // ------------ Right button down
2525 else if ( event
.RightDown() )
2528 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2530 // no default action at the moment
2535 // ------------ Right double click
2537 else if ( event
.RightDClick() )
2540 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2542 // no default action at the moment
2547 // ------------ No buttons down and mouse moving
2549 else if ( event
.Moving() )
2551 m_dragRowOrCol
= YToEdgeOfRow( y
);
2552 if ( m_dragRowOrCol
>= 0 )
2554 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2556 // don't capture the mouse yet
2557 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2560 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2562 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2568 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2571 wxPoint
pos( event
.GetPosition() );
2572 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2574 if ( event
.Dragging() )
2576 m_isDragging
= TRUE
;
2578 if ( event
.LeftIsDown() )
2580 switch( m_cursorMode
)
2582 case WXGRID_CURSOR_RESIZE_COL
:
2584 int cw
, ch
, dummy
, top
;
2585 m_gridWin
->GetClientSize( &cw
, &ch
);
2586 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2588 wxClientDC
dc( m_gridWin
);
2591 m_colRights
[m_dragRowOrCol
] -
2592 m_colWidths
[m_dragRowOrCol
] +
2593 WXGRID_MIN_COL_WIDTH
);
2594 dc
.SetLogicalFunction(wxINVERT
);
2595 if ( m_dragLastPos
>= 0 )
2597 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2599 dc
.DrawLine( x
, top
, x
, top
+ch
);
2604 case WXGRID_CURSOR_SELECT_COL
:
2605 if ( (col
= XToCol( x
)) >= 0 &&
2606 !IsInSelection( 0, col
) )
2608 SelectCol( col
, TRUE
);
2611 // default label to suppress warnings about "enumeration value
2612 // 'xxx' not handled in switch
2620 m_isDragging
= FALSE
;
2623 // ------------ Entering or leaving the window
2625 if ( event
.Entering() || event
.Leaving() )
2627 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2631 // ------------ Left button pressed
2633 else if ( event
.LeftDown() )
2635 // don't send a label click event for a hit on the
2636 // edge of the col label - this is probably the user
2637 // wanting to resize the col
2639 if ( XToEdgeOfCol(x
) < 0 )
2643 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2645 SelectCol( col
, event
.ShiftDown() );
2646 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2651 // starting to drag-resize a col
2653 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2658 // ------------ Left double click
2660 if ( event
.LeftDClick() )
2662 if ( XToEdgeOfCol(x
) < 0 )
2665 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2670 // ------------ Left button released
2672 else if ( event
.LeftUp() )
2674 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2676 DoEndDragResizeCol();
2678 // Note: we are ending the event *after* doing
2679 // default processing in this case
2681 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2684 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2689 // ------------ Right button down
2691 else if ( event
.RightDown() )
2694 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2696 // no default action at the moment
2701 // ------------ Right double click
2703 else if ( event
.RightDClick() )
2706 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2708 // no default action at the moment
2713 // ------------ No buttons down and mouse moving
2715 else if ( event
.Moving() )
2717 m_dragRowOrCol
= XToEdgeOfCol( x
);
2718 if ( m_dragRowOrCol
>= 0 )
2720 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2722 // don't capture the cursor yet
2723 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2726 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2728 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2734 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2736 if ( event
.LeftDown() )
2738 // indicate corner label by having both row and
2741 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2747 else if ( event
.LeftDClick() )
2749 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2752 else if ( event
.RightDown() )
2754 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2756 // no default action at the moment
2760 else if ( event
.RightDClick() )
2762 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2764 // no default action at the moment
2769 void wxGrid::ChangeCursorMode(CursorMode mode
,
2774 static const wxChar
*cursorModes
[] =
2783 wxLogTrace(_T("grid"),
2784 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2785 win
== m_colLabelWin
? _T("colLabelWin")
2786 : win
? _T("rowLabelWin")
2788 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2789 #endif // __WXDEBUG__
2791 if ( mode
== m_cursorMode
)
2796 // by default use the grid itself
2802 m_winCapture
->ReleaseMouse();
2803 m_winCapture
= (wxWindow
*)NULL
;
2806 m_cursorMode
= mode
;
2808 switch ( m_cursorMode
)
2810 case WXGRID_CURSOR_RESIZE_ROW
:
2811 win
->SetCursor( m_rowResizeCursor
);
2814 case WXGRID_CURSOR_RESIZE_COL
:
2815 win
->SetCursor( m_colResizeCursor
);
2819 win
->SetCursor( *wxSTANDARD_CURSOR
);
2822 // we need to capture mouse when resizing
2823 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2824 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2826 if ( captureMouse
&& resize
)
2828 win
->CaptureMouse();
2833 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2836 wxPoint
pos( event
.GetPosition() );
2837 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2839 wxGridCellCoords coords
;
2840 XYToCell( x
, y
, coords
);
2842 if ( event
.Dragging() )
2844 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
2846 // Don't start doing anything until the mouse has been drug at
2847 // least 3 pixels in any direction...
2848 if (! m_isDragging
) {
2849 if (m_startDragPos
== wxDefaultPosition
) {
2850 m_startDragPos
= pos
;
2853 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
2857 m_isDragging
= TRUE
;
2858 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2860 // Hide the edit control, so it
2861 // won't interfer with drag-shrinking.
2862 if ( IsCellEditControlEnabled() )
2863 HideCellEditControl();
2865 // Have we captured the mouse yet?
2866 if (! m_winCapture
) {
2867 m_winCapture
= m_gridWin
;
2868 m_winCapture
->CaptureMouse();
2871 if ( coords
!= wxGridNoCellCoords
)
2873 if ( !IsSelection() )
2875 SelectBlock( coords
, coords
);
2879 SelectBlock( m_currentCellCoords
, coords
);
2882 if (! IsVisible(coords
)) {
2883 MakeCellVisible(coords
);
2884 // TODO: need to introduce a delay or something here. The
2885 // scrolling is way to fast, at least on MSW.
2889 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2891 int cw
, ch
, left
, dummy
;
2892 m_gridWin
->GetClientSize( &cw
, &ch
);
2893 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2895 wxClientDC
dc( m_gridWin
);
2898 m_rowBottoms
[m_dragRowOrCol
] -
2899 m_rowHeights
[m_dragRowOrCol
] +
2900 WXGRID_MIN_ROW_HEIGHT
);
2901 dc
.SetLogicalFunction(wxINVERT
);
2902 if ( m_dragLastPos
>= 0 )
2904 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2906 dc
.DrawLine( left
, y
, left
+cw
, y
);
2909 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2911 int cw
, ch
, dummy
, top
;
2912 m_gridWin
->GetClientSize( &cw
, &ch
);
2913 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2915 wxClientDC
dc( m_gridWin
);
2918 m_colRights
[m_dragRowOrCol
] -
2919 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
2920 dc
.SetLogicalFunction(wxINVERT
);
2921 if ( m_dragLastPos
>= 0 )
2923 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2925 dc
.DrawLine( x
, top
, x
, top
+ch
);
2932 m_isDragging
= FALSE
;
2933 m_startDragPos
= wxDefaultPosition
;
2936 if ( coords
!= wxGridNoCellCoords
)
2938 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2939 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2942 if ( event
.Entering() || event
.Leaving() )
2944 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2945 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2950 // ------------ Left button pressed
2952 if ( event
.LeftDown() )
2954 EnableCellEditControl( FALSE
);
2955 if ( event
.ShiftDown() )
2957 SelectBlock( m_currentCellCoords
, coords
);
2959 else if ( XToEdgeOfCol(x
) < 0 &&
2960 YToEdgeOfRow(y
) < 0 )
2962 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2967 MakeCellVisible( coords
);
2969 // if this is the second click on this cell then start
2971 if (m_waitForSlowClick
&& coords
== m_currentCellCoords
) {
2972 EnableCellEditControl(TRUE
);
2973 // VZ: this is done by the call above, so why do it
2974 // again? please remove this line if it's ok
2975 //ShowCellEditControl();
2976 m_waitForSlowClick
= FALSE
;
2979 SetCurrentCell( coords
);
2980 m_waitForSlowClick
= TRUE
;
2987 // ------------ Left double click
2989 else if ( event
.LeftDClick() )
2991 EnableCellEditControl( FALSE
);
2992 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2994 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
3002 // ------------ Left button released
3004 else if ( event
.LeftUp() )
3006 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3008 if ( IsSelection() )
3011 m_winCapture
->ReleaseMouse();
3012 m_winCapture
= NULL
;
3014 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
3017 // Show the edit control, if it has been hidden for
3019 ShowCellEditControl();
3021 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3023 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3024 DoEndDragResizeRow();
3026 // Note: we are ending the event *after* doing
3027 // default processing in this case
3029 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3031 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3033 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3034 DoEndDragResizeCol();
3036 // Note: we are ending the event *after* doing
3037 // default processing in this case
3039 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
3046 // ------------ Right button down
3048 else if ( event
.RightDown() )
3050 EnableCellEditControl( FALSE
);
3051 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
3056 // no default action at the moment
3061 // ------------ Right double click
3063 else if ( event
.RightDClick() )
3065 EnableCellEditControl( FALSE
);
3066 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
3071 // no default action at the moment
3075 // ------------ Moving and no button action
3077 else if ( event
.Moving() && !event
.IsButton() )
3079 int dragRow
= YToEdgeOfRow( y
);
3080 int dragCol
= XToEdgeOfCol( x
);
3082 // Dragging on the corner of a cell to resize in both
3083 // directions is not implemented yet...
3085 if ( dragRow
>= 0 && dragCol
>= 0 )
3087 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3093 m_dragRowOrCol
= dragRow
;
3095 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3097 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
3105 m_dragRowOrCol
= dragCol
;
3107 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3109 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
3115 // Neither on a row or col edge
3117 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3119 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3126 void wxGrid::DoEndDragResizeRow()
3128 if ( m_dragLastPos
>= 0 )
3130 // erase the last line and resize the row
3132 int cw
, ch
, left
, dummy
;
3133 m_gridWin
->GetClientSize( &cw
, &ch
);
3134 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3136 wxClientDC
dc( m_gridWin
);
3138 dc
.SetLogicalFunction( wxINVERT
);
3139 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3140 HideCellEditControl();
3142 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3143 SetRowSize( m_dragRowOrCol
,
3144 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3146 if ( !GetBatchCount() )
3148 // Only needed to get the correct rect.y:
3149 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3151 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3152 rect
.width
= m_rowLabelWidth
;
3153 rect
.height
= ch
- rect
.y
;
3154 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3156 m_gridWin
->Refresh( FALSE
, &rect
);
3159 ShowCellEditControl();
3164 void wxGrid::DoEndDragResizeCol()
3166 if ( m_dragLastPos
>= 0 )
3168 // erase the last line and resize the col
3170 int cw
, ch
, dummy
, top
;
3171 m_gridWin
->GetClientSize( &cw
, &ch
);
3172 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3174 wxClientDC
dc( m_gridWin
);
3176 dc
.SetLogicalFunction( wxINVERT
);
3177 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3178 HideCellEditControl();
3180 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3181 SetColSize( m_dragRowOrCol
,
3182 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3184 if ( !GetBatchCount() )
3186 // Only needed to get the correct rect.x:
3187 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3189 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3190 rect
.width
= cw
- rect
.x
;
3191 rect
.height
= m_colLabelHeight
;
3192 m_colLabelWin
->Refresh( TRUE
, &rect
);
3194 m_gridWin
->Refresh( FALSE
, &rect
);
3197 ShowCellEditControl();
3204 // ------ interaction with data model
3206 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3208 switch ( msg
.GetId() )
3210 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3211 return GetModelValues();
3213 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3214 return SetModelValues();
3216 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3217 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3218 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3219 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3220 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3221 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3222 return Redimension( msg
);
3231 // The behaviour of this function depends on the grid table class
3232 // Clear() function. For the default wxGridStringTable class the
3233 // behavious is to replace all cell contents with wxEmptyString but
3234 // not to change the number of rows or cols.
3236 void wxGrid::ClearGrid()
3241 SetEditControlValue();
3242 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3247 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3249 // TODO: something with updateLabels flag
3253 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3259 if (IsCellEditControlEnabled())
3260 EnableCellEditControl(FALSE
);
3262 bool ok
= m_table
->InsertRows( pos
, numRows
);
3264 // the table will have sent the results of the insert row
3265 // operation to this view object as a grid table message
3269 if ( m_numCols
== 0 )
3271 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3273 // TODO: perhaps instead of appending the default number of cols
3274 // we should remember what the last non-zero number of cols was ?
3278 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3280 // if we have just inserted cols into an empty grid the current
3281 // cell will be undefined...
3283 SetCurrentCell( 0, 0 );
3287 if ( !GetBatchCount() ) Refresh();
3290 SetEditControlValue();
3300 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3302 // TODO: something with updateLabels flag
3306 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3310 if ( m_table
&& m_table
->AppendRows( numRows
) )
3312 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3314 // if we have just inserted cols into an empty grid the current
3315 // cell will be undefined...
3317 SetCurrentCell( 0, 0 );
3320 // the table will have sent the results of the append row
3321 // operation to this view object as a grid table message
3324 if ( !GetBatchCount() ) Refresh();
3334 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3336 // TODO: something with updateLabels flag
3340 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3346 if (IsCellEditControlEnabled())
3347 EnableCellEditControl(FALSE
);
3349 if (m_table
->DeleteRows( pos
, numRows
))
3352 // the table will have sent the results of the delete row
3353 // operation to this view object as a grid table message
3356 if ( !GetBatchCount() ) Refresh();
3364 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3366 // TODO: something with updateLabels flag
3370 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3376 if (IsCellEditControlEnabled())
3377 EnableCellEditControl(FALSE
);
3379 bool ok
= m_table
->InsertCols( pos
, numCols
);
3381 // the table will have sent the results of the insert col
3382 // operation to this view object as a grid table message
3386 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3388 // if we have just inserted cols into an empty grid the current
3389 // cell will be undefined...
3391 SetCurrentCell( 0, 0 );
3395 if ( !GetBatchCount() ) Refresh();
3398 SetEditControlValue();
3408 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3410 // TODO: something with updateLabels flag
3414 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3418 if ( m_table
&& m_table
->AppendCols( numCols
) )
3420 // the table will have sent the results of the append col
3421 // operation to this view object as a grid table message
3423 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3425 // if we have just inserted cols into an empty grid the current
3426 // cell will be undefined...
3428 SetCurrentCell( 0, 0 );
3432 if ( !GetBatchCount() ) Refresh();
3442 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3444 // TODO: something with updateLabels flag
3448 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3454 if (IsCellEditControlEnabled())
3455 EnableCellEditControl(FALSE
);
3457 if ( m_table
->DeleteCols( pos
, numCols
) )
3459 // the table will have sent the results of the delete col
3460 // operation to this view object as a grid table message
3463 if ( !GetBatchCount() ) Refresh();
3473 // ----- event handlers
3476 // Generate a grid event based on a mouse event and
3477 // return the result of ProcessEvent()
3479 bool wxGrid::SendEvent( const wxEventType type
,
3481 wxMouseEvent
& mouseEv
)
3483 if ( type
== EVT_GRID_ROW_SIZE
||
3484 type
== EVT_GRID_COL_SIZE
)
3486 int rowOrCol
= (row
== -1 ? col
: row
);
3488 wxGridSizeEvent
gridEvt( GetId(),
3492 mouseEv
.GetX(), mouseEv
.GetY(),
3493 mouseEv
.ControlDown(),
3494 mouseEv
.ShiftDown(),
3496 mouseEv
.MetaDown() );
3498 return GetEventHandler()->ProcessEvent(gridEvt
);
3500 else if ( type
== EVT_GRID_RANGE_SELECT
)
3502 wxGridRangeSelectEvent
gridEvt( GetId(),
3506 m_selectedBottomRight
,
3507 mouseEv
.ControlDown(),
3508 mouseEv
.ShiftDown(),
3510 mouseEv
.MetaDown() );
3512 return GetEventHandler()->ProcessEvent(gridEvt
);
3516 wxGridEvent
gridEvt( GetId(),
3520 mouseEv
.GetX(), mouseEv
.GetY(),
3521 mouseEv
.ControlDown(),
3522 mouseEv
.ShiftDown(),
3524 mouseEv
.MetaDown() );
3526 return GetEventHandler()->ProcessEvent(gridEvt
);
3531 // Generate a grid event of specified type and return the result
3532 // of ProcessEvent().
3534 bool wxGrid::SendEvent( const wxEventType type
,
3537 if ( type
== EVT_GRID_ROW_SIZE
||
3538 type
== EVT_GRID_COL_SIZE
)
3540 int rowOrCol
= (row
== -1 ? col
: row
);
3542 wxGridSizeEvent
gridEvt( GetId(),
3547 return GetEventHandler()->ProcessEvent(gridEvt
);
3551 wxGridEvent
gridEvt( GetId(),
3556 return GetEventHandler()->ProcessEvent(gridEvt
);
3561 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3563 wxPaintDC
dc( this );
3565 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3566 m_numRows
&& m_numCols
)
3568 m_currentCellCoords
.Set(0, 0);
3569 SetEditControlValue();
3570 ShowCellEditControl();
3577 // This is just here to make sure that CalcDimensions gets called when
3578 // the grid view is resized... then the size event is skipped to allow
3579 // the box sizers to handle everything
3581 void wxGrid::OnSize( wxSizeEvent
& event
)
3588 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3590 if ( m_inOnKeyDown
)
3592 // shouldn't be here - we are going round in circles...
3594 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
3597 m_inOnKeyDown
= TRUE
;
3599 // propagate the event up and see if it gets processed
3601 wxWindow
*parent
= GetParent();
3602 wxKeyEvent
keyEvt( event
);
3603 keyEvt
.SetEventObject( parent
);
3605 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3608 // TODO: Should also support Shift-cursor keys for
3609 // extending the selection. Maybe add a flag to
3610 // MoveCursorXXX() and MoveCursorXXXBlock() and
3611 // just send event.ShiftDown().
3613 // try local handlers
3615 switch ( event
.KeyCode() )
3618 if ( event
.ControlDown() )
3620 MoveCursorUpBlock();
3629 if ( event
.ControlDown() )
3631 MoveCursorDownBlock();
3640 if ( event
.ControlDown() )
3642 MoveCursorLeftBlock();
3651 if ( event
.ControlDown() )
3653 MoveCursorRightBlock();
3662 if ( event
.ControlDown() )
3664 event
.Skip(); // to let the edit control have the return
3673 if (event
.ShiftDown())
3680 if ( event
.ControlDown() )
3682 MakeCellVisible( 0, 0 );
3683 SetCurrentCell( 0, 0 );
3692 if ( event
.ControlDown() )
3694 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3695 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3711 // We don't want these keys to trigger the edit control, any others?
3720 if ( !IsEditable() )
3725 // Otherwise fall through to default
3728 // now try the cell edit control
3730 if ( !IsCellEditControlEnabled() )
3731 EnableCellEditControl( TRUE
);
3732 if (IsCellEditControlEnabled()) {
3733 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
3734 attr
->GetEditor()->StartingKey(event
);
3741 m_inOnKeyDown
= FALSE
;
3745 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3751 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3753 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3755 // the event has been intercepted - do nothing
3760 m_currentCellCoords
!= wxGridNoCellCoords
)
3762 HideCellEditControl();
3763 SaveEditControlValue();
3764 EnableCellEditControl(FALSE
);
3766 // Clear the old current cell highlight
3767 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
3769 // Otherwise refresh redraws the highlight!
3770 m_currentCellCoords
= coords
;
3772 m_gridWin
->Refresh( FALSE
, &r
);
3775 m_currentCellCoords
= coords
;
3777 SetEditControlValue();
3781 wxClientDC
dc(m_gridWin
);
3783 DrawCellHighlight(dc
);
3785 if ( IsSelection() )
3787 wxRect
r( SelectionToDeviceRect() );
3789 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3796 // ------ functions to get/send data (see also public functions)
3799 bool wxGrid::GetModelValues()
3803 // all we need to do is repaint the grid
3805 m_gridWin
->Refresh();
3813 bool wxGrid::SetModelValues()
3819 for ( row
= 0; row
< m_numRows
; row
++ )
3821 for ( col
= 0; col
< m_numCols
; col
++ )
3823 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3835 // Note - this function only draws cells that are in the list of
3836 // exposed cells (usually set from the update region by
3837 // CalcExposedCells)
3839 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3841 if ( !m_numRows
|| !m_numCols
) return;
3844 size_t numCells
= m_cellsExposed
.GetCount();
3846 for ( i
= 0; i
< numCells
; i
++ )
3848 DrawCell( dc
, m_cellsExposed
[i
] );
3853 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3855 int row
= coords
.GetRow();
3856 int col
= coords
.GetCol();
3858 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3861 // we draw the cell border ourselves
3862 #if !WXGRID_DRAW_LINES
3863 if ( m_gridLinesEnabled
)
3864 DrawCellBorder( dc
, coords
);
3867 // but all the rest is drawn by the cell renderer and hence may be
3870 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3871 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3872 rect
.width
= m_colWidths
[col
]-1;
3873 rect
.height
= m_rowHeights
[row
]-1;
3875 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
3876 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
3879 if (m_currentCellCoords
== coords
)
3880 DrawCellHighlight(dc
);
3884 void wxGrid::DrawCellHighlight( wxDC
& dc
)
3886 int row
= m_currentCellCoords
.GetRow();
3887 int col
= m_currentCellCoords
.GetCol();
3889 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3893 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3894 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3895 rect
.width
= m_colWidths
[col
] - 1;
3896 rect
.height
= m_rowHeights
[row
] - 1;
3898 dc
.SetPen(wxPen(m_gridLineColour
, 3, wxSOLID
));
3899 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
3901 dc
.DrawRectangle(rect
);
3904 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3906 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3907 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3909 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3910 int row
= coords
.GetRow();
3911 int col
= coords
.GetCol();
3913 // right hand border
3915 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3916 m_colRights
[col
], m_rowBottoms
[row
] );
3920 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3921 m_colRights
[col
], m_rowBottoms
[row
] );
3925 // TODO: remove this ???
3926 // This is used to redraw all grid lines e.g. when the grid line colour
3929 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3931 if ( !m_gridLinesEnabled
||
3933 !m_numCols
) return;
3935 int top
, bottom
, left
, right
;
3939 m_gridWin
->GetClientSize(&cw
, &ch
);
3941 // virtual coords of visible area
3943 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3944 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3948 reg
.GetBox(x
, y
, w
, h
);
3949 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3950 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3953 // avoid drawing grid lines past the last row and col
3955 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3956 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3958 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3960 // horizontal grid lines
3963 for ( i
= 0; i
< m_numRows
; i
++ )
3965 if ( m_rowBottoms
[i
]-1 > bottom
)
3969 else if ( m_rowBottoms
[i
]-1 >= top
)
3971 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
3976 // vertical grid lines
3978 for ( i
= 0; i
< m_numCols
; i
++ )
3980 if ( m_colRights
[i
]-1 > right
)
3984 else if ( m_colRights
[i
]-1 >= left
)
3986 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
3992 void wxGrid::DrawRowLabels( wxDC
& dc
)
3994 if ( !m_numRows
|| !m_numCols
) return;
3997 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3999 for ( i
= 0; i
< numLabels
; i
++ )
4001 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
4006 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
4008 if ( m_rowHeights
[row
] <= 0 ) return;
4010 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4012 dc
.SetPen( *wxBLACK_PEN
);
4013 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
4014 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4016 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
4017 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
4019 dc
.SetPen( *wxWHITE_PEN
);
4020 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
4021 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
4023 dc
.SetBackgroundMode( wxTRANSPARENT
);
4024 dc
.SetTextForeground( GetLabelTextColour() );
4025 dc
.SetFont( GetLabelFont() );
4028 GetRowLabelAlignment( &hAlign
, &vAlign
);
4032 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
4033 rect
.SetWidth( m_rowLabelWidth
- 4 );
4034 rect
.SetHeight( m_rowHeights
[row
] - 4 );
4035 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
4039 void wxGrid::DrawColLabels( wxDC
& dc
)
4041 if ( !m_numRows
|| !m_numCols
) return;
4044 size_t numLabels
= m_colLabelsExposed
.GetCount();
4046 for ( i
= 0; i
< numLabels
; i
++ )
4048 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
4053 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
4055 if ( m_colWidths
[col
] <= 0 ) return;
4057 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
4059 dc
.SetPen( *wxBLACK_PEN
);
4060 dc
.DrawLine( m_colRights
[col
]-1, 0,
4061 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4063 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
4064 m_colRights
[col
]-1, m_colLabelHeight
-1 );
4066 dc
.SetPen( *wxWHITE_PEN
);
4067 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
4068 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
4070 dc
.SetBackgroundMode( wxTRANSPARENT
);
4071 dc
.SetTextForeground( GetLabelTextColour() );
4072 dc
.SetFont( GetLabelFont() );
4074 dc
.SetBackgroundMode( wxTRANSPARENT
);
4075 dc
.SetTextForeground( GetLabelTextColour() );
4076 dc
.SetFont( GetLabelFont() );
4079 GetColLabelAlignment( &hAlign
, &vAlign
);
4082 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
4084 rect
.SetWidth( m_colWidths
[col
] - 4 );
4085 rect
.SetHeight( m_colLabelHeight
- 4 );
4086 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
4090 void wxGrid::DrawTextRectangle( wxDC
& dc
,
4091 const wxString
& value
,
4096 long textWidth
, textHeight
;
4097 long lineWidth
, lineHeight
;
4098 wxArrayString lines
;
4100 dc
.SetClippingRegion( rect
);
4101 StringToLines( value
, lines
);
4102 if ( lines
.GetCount() )
4104 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
4105 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
4108 switch ( horizAlign
)
4111 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
4115 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
4124 switch ( vertAlign
)
4127 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
4131 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
4140 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
4142 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
4147 dc
.DestroyClippingRegion();
4151 // Split multi line text up into an array of strings. Any existing
4152 // contents of the string array are preserved.
4154 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
4158 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
4159 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
4161 while ( startPos
< (int)tVal
.Length() )
4163 pos
= tVal
.Mid(startPos
).Find( eol
);
4168 else if ( pos
== 0 )
4170 lines
.Add( wxEmptyString
);
4174 lines
.Add( value
.Mid(startPos
, pos
) );
4178 if ( startPos
< (int)value
.Length() )
4180 lines
.Add( value
.Mid( startPos
) );
4185 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4186 wxArrayString
& lines
,
4187 long *width
, long *height
)
4194 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4196 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4197 w
= wxMax( w
, lineW
);
4207 // ------ Edit control functions
4211 void wxGrid::EnableEditing( bool edit
)
4213 // TODO: improve this ?
4215 if ( edit
!= m_editable
)
4219 EnableCellEditControl(m_editable
);
4224 void wxGrid::EnableCellEditControl( bool enable
)
4229 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4230 SetCurrentCell( 0, 0 );
4232 if ( enable
!= m_cellEditCtrlEnabled
)
4236 m_cellEditCtrlEnabled
= enable
;
4237 SetEditControlValue();
4238 ShowCellEditControl();
4242 HideCellEditControl();
4243 SaveEditControlValue();
4244 m_cellEditCtrlEnabled
= enable
;
4250 void wxGrid::ShowCellEditControl()
4252 if ( IsCellEditControlEnabled() )
4254 if ( !IsVisible( m_currentCellCoords
) )
4260 wxRect rect
= CellToRect( m_currentCellCoords
);
4261 int row
= m_currentCellCoords
.GetRow();
4262 int col
= m_currentCellCoords
.GetCol();
4264 // convert to scrolled coords
4266 int left
, top
, right
, bottom
;
4267 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4268 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4269 left
--; top
--; right
--; bottom
--; // cell is shifted by one pixel
4271 m_gridWin
->GetClientSize( &cw
, &ch
);
4273 // Make the edit control large enough to allow for internal
4276 // TODO: remove this if the text ctrl sizing is improved esp. for
4280 #if defined(__WXMOTIF__)
4281 if ( row
== 0 || col
== 0 )
4290 if ( row
== 0 || col
== 0 )
4300 #if defined(__WXGTK__)
4303 if (left
!= 0) left_diff
++;
4304 if (top
!= 0) top_diff
++;
4305 rect
.SetLeft( left
+ left_diff
);
4306 rect
.SetTop( top
+ top_diff
);
4307 rect
.SetRight( rect
.GetRight() - left_diff
);
4308 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4310 rect
.SetLeft( wxMax(0, left
- extra
) );
4311 rect
.SetTop( wxMax(0, top
- extra
) );
4312 rect
.SetRight( rect
.GetRight() + 2*extra
);
4313 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4316 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4317 wxGridCellEditor
* editor
= attr
->GetEditor();
4318 if ( !editor
->IsCreated() )
4320 editor
->Create(m_gridWin
, -1,
4321 new wxGridCellEditorEvtHandler(this, editor
));
4324 editor
->SetSize( rect
);
4325 editor
->Show( TRUE
, attr
);
4326 editor
->BeginEdit(row
, col
, this);
4333 void wxGrid::HideCellEditControl()
4335 if ( IsCellEditControlEnabled() )
4337 int row
= m_currentCellCoords
.GetRow();
4338 int col
= m_currentCellCoords
.GetCol();
4340 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4341 attr
->GetEditor()->Show( FALSE
);
4343 m_gridWin
->SetFocus();
4348 void wxGrid::SetEditControlValue( const wxString
& value
)
4350 // RD: The new Editors get the value from the table themselves now. This
4351 // method can probably be removed...
4355 void wxGrid::SaveEditControlValue()
4357 if ( IsCellEditControlEnabled() )
4359 int row
= m_currentCellCoords
.GetRow();
4360 int col
= m_currentCellCoords
.GetCol();
4362 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
4363 bool changed
= attr
->GetEditor()->EndEdit(row
, col
, TRUE
, this);
4369 SendEvent( EVT_GRID_CELL_CHANGE
,
4370 m_currentCellCoords
.GetRow(),
4371 m_currentCellCoords
.GetCol() );
4378 // ------ Grid location functions
4379 // Note that all of these functions work with the logical coordinates of
4380 // grid cells and labels so you will need to convert from device
4381 // coordinates for mouse events etc.
4384 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4386 int row
= YToRow(y
);
4387 int col
= XToCol(x
);
4389 if ( row
== -1 || col
== -1 )
4391 coords
= wxGridNoCellCoords
;
4395 coords
.Set( row
, col
);
4400 int wxGrid::YToRow( int y
)
4404 for ( i
= 0; i
< m_numRows
; i
++ )
4406 if ( y
< m_rowBottoms
[i
] ) return i
;
4409 return m_numRows
; //-1;
4413 int wxGrid::XToCol( int x
)
4417 for ( i
= 0; i
< m_numCols
; i
++ )
4419 if ( x
< m_colRights
[i
] ) return i
;
4422 return m_numCols
; //-1;
4426 // return the row number that that the y coord is near the edge of, or
4427 // -1 if not near an edge
4429 int wxGrid::YToEdgeOfRow( int y
)
4433 for ( i
= 0; i
< m_numRows
; i
++ )
4435 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4437 d
= abs( y
- m_rowBottoms
[i
] );
4439 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4448 // return the col number that that the x coord is near the edge of, or
4449 // -1 if not near an edge
4451 int wxGrid::XToEdgeOfCol( int x
)
4455 for ( i
= 0; i
< m_numCols
; i
++ )
4457 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4459 d
= abs( x
- m_colRights
[i
] );
4461 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4470 wxRect
wxGrid::CellToRect( int row
, int col
)
4472 wxRect
rect( -1, -1, -1, -1 );
4474 if ( row
>= 0 && row
< m_numRows
&&
4475 col
>= 0 && col
< m_numCols
)
4477 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4478 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4479 rect
.width
= m_colWidths
[col
];
4480 rect
.height
= m_rowHeights
[ row
];
4487 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4489 // get the cell rectangle in logical coords
4491 wxRect
r( CellToRect( row
, col
) );
4493 // convert to device coords
4495 int left
, top
, right
, bottom
;
4496 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4497 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4499 // check against the client area of the grid window
4502 m_gridWin
->GetClientSize( &cw
, &ch
);
4504 if ( wholeCellVisible
)
4506 // is the cell wholly visible ?
4508 return ( left
>= 0 && right
<= cw
&&
4509 top
>= 0 && bottom
<= ch
);
4513 // is the cell partly visible ?
4515 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4516 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4521 // make the specified cell location visible by doing a minimal amount
4524 void wxGrid::MakeCellVisible( int row
, int col
)
4527 int xpos
= -1, ypos
= -1;
4529 if ( row
>= 0 && row
< m_numRows
&&
4530 col
>= 0 && col
< m_numCols
)
4532 // get the cell rectangle in logical coords
4534 wxRect
r( CellToRect( row
, col
) );
4536 // convert to device coords
4538 int left
, top
, right
, bottom
;
4539 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4540 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4543 m_gridWin
->GetClientSize( &cw
, &ch
);
4549 else if ( bottom
> ch
)
4551 int h
= r
.GetHeight();
4553 for ( i
= row
-1; i
>= 0; i
-- )
4555 if ( h
+ m_rowHeights
[i
] > ch
) break;
4557 h
+= m_rowHeights
[i
];
4558 ypos
-= m_rowHeights
[i
];
4561 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4562 // have rounding errors (this is important, because if we do, we
4563 // might not scroll at all and some cells won't be redrawn)
4564 ypos
+= GRID_SCROLL_LINE
/ 2;
4571 else if ( right
> cw
)
4573 int w
= r
.GetWidth();
4575 for ( i
= col
-1; i
>= 0; i
-- )
4577 if ( w
+ m_colWidths
[i
] > cw
) break;
4579 w
+= m_colWidths
[i
];
4580 xpos
-= m_colWidths
[i
];
4583 // see comment for ypos above
4584 xpos
+= GRID_SCROLL_LINE
/ 2;
4587 if ( xpos
!= -1 || ypos
!= -1 )
4589 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4590 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4591 Scroll( xpos
, ypos
);
4599 // ------ Grid cursor movement functions
4602 bool wxGrid::MoveCursorUp()
4604 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4605 m_currentCellCoords
.GetRow() > 0 )
4607 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4608 m_currentCellCoords
.GetCol() );
4610 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4611 m_currentCellCoords
.GetCol() );
4620 bool wxGrid::MoveCursorDown()
4622 // TODO: allow for scrolling
4624 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4625 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4627 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4628 m_currentCellCoords
.GetCol() );
4630 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4631 m_currentCellCoords
.GetCol() );
4640 bool wxGrid::MoveCursorLeft()
4642 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4643 m_currentCellCoords
.GetCol() > 0 )
4645 MakeCellVisible( m_currentCellCoords
.GetRow(),
4646 m_currentCellCoords
.GetCol() - 1 );
4648 SetCurrentCell( m_currentCellCoords
.GetRow(),
4649 m_currentCellCoords
.GetCol() - 1 );
4658 bool wxGrid::MoveCursorRight()
4660 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4661 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4663 MakeCellVisible( m_currentCellCoords
.GetRow(),
4664 m_currentCellCoords
.GetCol() + 1 );
4666 SetCurrentCell( m_currentCellCoords
.GetRow(),
4667 m_currentCellCoords
.GetCol() + 1 );
4676 bool wxGrid::MovePageUp()
4678 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4680 int row
= m_currentCellCoords
.GetRow();
4684 m_gridWin
->GetClientSize( &cw
, &ch
);
4686 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4687 int newRow
= YToRow( y
- ch
+ 1 );
4692 else if ( newRow
== row
)
4697 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4698 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4706 bool wxGrid::MovePageDown()
4708 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4710 int row
= m_currentCellCoords
.GetRow();
4711 if ( row
< m_numRows
)
4714 m_gridWin
->GetClientSize( &cw
, &ch
);
4716 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4717 int newRow
= YToRow( y
+ ch
);
4720 newRow
= m_numRows
- 1;
4722 else if ( newRow
== row
)
4727 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4728 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4736 bool wxGrid::MoveCursorUpBlock()
4739 m_currentCellCoords
!= wxGridNoCellCoords
&&
4740 m_currentCellCoords
.GetRow() > 0 )
4742 int row
= m_currentCellCoords
.GetRow();
4743 int col
= m_currentCellCoords
.GetCol();
4745 if ( m_table
->IsEmptyCell(row
, col
) )
4747 // starting in an empty cell: find the next block of
4753 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4756 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4758 // starting at the top of a block: find the next block
4764 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4769 // starting within a block: find the top of the block
4774 if ( m_table
->IsEmptyCell(row
, col
) )
4782 MakeCellVisible( row
, col
);
4783 SetCurrentCell( row
, col
);
4791 bool wxGrid::MoveCursorDownBlock()
4794 m_currentCellCoords
!= wxGridNoCellCoords
&&
4795 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4797 int row
= m_currentCellCoords
.GetRow();
4798 int col
= m_currentCellCoords
.GetCol();
4800 if ( m_table
->IsEmptyCell(row
, col
) )
4802 // starting in an empty cell: find the next block of
4805 while ( row
< m_numRows
-1 )
4808 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4811 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4813 // starting at the bottom of a block: find the next block
4816 while ( row
< m_numRows
-1 )
4819 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4824 // starting within a block: find the bottom of the block
4826 while ( row
< m_numRows
-1 )
4829 if ( m_table
->IsEmptyCell(row
, col
) )
4837 MakeCellVisible( row
, col
);
4838 SetCurrentCell( row
, col
);
4846 bool wxGrid::MoveCursorLeftBlock()
4849 m_currentCellCoords
!= wxGridNoCellCoords
&&
4850 m_currentCellCoords
.GetCol() > 0 )
4852 int row
= m_currentCellCoords
.GetRow();
4853 int col
= m_currentCellCoords
.GetCol();
4855 if ( m_table
->IsEmptyCell(row
, col
) )
4857 // starting in an empty cell: find the next block of
4863 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4866 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4868 // starting at the left of a block: find the next block
4874 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4879 // starting within a block: find the left of the block
4884 if ( m_table
->IsEmptyCell(row
, col
) )
4892 MakeCellVisible( row
, col
);
4893 SetCurrentCell( row
, col
);
4901 bool wxGrid::MoveCursorRightBlock()
4904 m_currentCellCoords
!= wxGridNoCellCoords
&&
4905 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4907 int row
= m_currentCellCoords
.GetRow();
4908 int col
= m_currentCellCoords
.GetCol();
4910 if ( m_table
->IsEmptyCell(row
, col
) )
4912 // starting in an empty cell: find the next block of
4915 while ( col
< m_numCols
-1 )
4918 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4921 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4923 // starting at the right of a block: find the next block
4926 while ( col
< m_numCols
-1 )
4929 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4934 // starting within a block: find the right of the block
4936 while ( col
< m_numCols
-1 )
4939 if ( m_table
->IsEmptyCell(row
, col
) )
4947 MakeCellVisible( row
, col
);
4948 SetCurrentCell( row
, col
);
4959 // ------ Label values and formatting
4962 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4964 *horiz
= m_rowLabelHorizAlign
;
4965 *vert
= m_rowLabelVertAlign
;
4968 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4970 *horiz
= m_colLabelHorizAlign
;
4971 *vert
= m_colLabelVertAlign
;
4974 wxString
wxGrid::GetRowLabelValue( int row
)
4978 return m_table
->GetRowLabelValue( row
);
4988 wxString
wxGrid::GetColLabelValue( int col
)
4992 return m_table
->GetColLabelValue( col
);
5003 void wxGrid::SetRowLabelSize( int width
)
5005 width
= wxMax( width
, 0 );
5006 if ( width
!= m_rowLabelWidth
)
5010 m_rowLabelWin
->Show( FALSE
);
5011 m_cornerLabelWin
->Show( FALSE
);
5013 else if ( m_rowLabelWidth
== 0 )
5015 m_rowLabelWin
->Show( TRUE
);
5016 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5019 m_rowLabelWidth
= width
;
5026 void wxGrid::SetColLabelSize( int height
)
5028 height
= wxMax( height
, 0 );
5029 if ( height
!= m_colLabelHeight
)
5033 m_colLabelWin
->Show( FALSE
);
5034 m_cornerLabelWin
->Show( FALSE
);
5036 else if ( m_colLabelHeight
== 0 )
5038 m_colLabelWin
->Show( TRUE
);
5039 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
5042 m_colLabelHeight
= height
;
5049 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
5051 if ( m_labelBackgroundColour
!= colour
)
5053 m_labelBackgroundColour
= colour
;
5054 m_rowLabelWin
->SetBackgroundColour( colour
);
5055 m_colLabelWin
->SetBackgroundColour( colour
);
5056 m_cornerLabelWin
->SetBackgroundColour( colour
);
5058 if ( !GetBatchCount() )
5060 m_rowLabelWin
->Refresh();
5061 m_colLabelWin
->Refresh();
5062 m_cornerLabelWin
->Refresh();
5067 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
5069 if ( m_labelTextColour
!= colour
)
5071 m_labelTextColour
= colour
;
5072 if ( !GetBatchCount() )
5074 m_rowLabelWin
->Refresh();
5075 m_colLabelWin
->Refresh();
5080 void wxGrid::SetLabelFont( const wxFont
& font
)
5083 if ( !GetBatchCount() )
5085 m_rowLabelWin
->Refresh();
5086 m_colLabelWin
->Refresh();
5090 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
5092 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5094 m_rowLabelHorizAlign
= horiz
;
5097 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5099 m_rowLabelVertAlign
= vert
;
5102 if ( !GetBatchCount() )
5104 m_rowLabelWin
->Refresh();
5108 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5110 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5112 m_colLabelHorizAlign
= horiz
;
5115 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5117 m_colLabelVertAlign
= vert
;
5120 if ( !GetBatchCount() )
5122 m_colLabelWin
->Refresh();
5126 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5130 m_table
->SetRowLabelValue( row
, s
);
5131 if ( !GetBatchCount() )
5133 wxRect rect
= CellToRect( row
, 0);
5134 if ( rect
.height
> 0 )
5136 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5138 rect
.width
= m_rowLabelWidth
;
5139 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5145 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5149 m_table
->SetColLabelValue( col
, s
);
5150 if ( !GetBatchCount() )
5152 wxRect rect
= CellToRect( 0, col
);
5153 if ( rect
.width
> 0 )
5155 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5157 rect
.height
= m_colLabelHeight
;
5158 m_colLabelWin
->Refresh( TRUE
, &rect
);
5164 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5166 if ( m_gridLineColour
!= colour
)
5168 m_gridLineColour
= colour
;
5170 wxClientDC
dc( m_gridWin
);
5172 DrawAllGridLines( dc
, wxRegion() );
5176 void wxGrid::EnableGridLines( bool enable
)
5178 if ( enable
!= m_gridLinesEnabled
)
5180 m_gridLinesEnabled
= enable
;
5182 if ( !GetBatchCount() )
5186 wxClientDC
dc( m_gridWin
);
5188 DrawAllGridLines( dc
, wxRegion() );
5192 m_gridWin
->Refresh();
5199 int wxGrid::GetDefaultRowSize()
5201 return m_defaultRowHeight
;
5204 int wxGrid::GetRowSize( int row
)
5206 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5208 return m_rowHeights
[row
];
5211 int wxGrid::GetDefaultColSize()
5213 return m_defaultColWidth
;
5216 int wxGrid::GetColSize( int col
)
5218 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5220 return m_colWidths
[col
];
5223 // ============================================================================
5224 // access to the grid attributes: each of them has a default value in the grid
5225 // itself and may be overidden on a per-cell basis
5226 // ============================================================================
5228 // ----------------------------------------------------------------------------
5229 // setting default attributes
5230 // ----------------------------------------------------------------------------
5232 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5234 m_defaultCellAttr
->SetBackgroundColour(col
);
5237 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5239 m_defaultCellAttr
->SetTextColour(col
);
5242 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5244 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5247 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5249 m_defaultCellAttr
->SetFont(font
);
5252 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5254 m_defaultCellAttr
->SetRenderer(renderer
);
5257 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
5259 m_defaultCellAttr
->SetEditor(editor
);
5262 // ----------------------------------------------------------------------------
5263 // access to the default attrbiutes
5264 // ----------------------------------------------------------------------------
5266 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5268 return m_defaultCellAttr
->GetBackgroundColour();
5271 wxColour
wxGrid::GetDefaultCellTextColour()
5273 return m_defaultCellAttr
->GetTextColour();
5276 wxFont
wxGrid::GetDefaultCellFont()
5278 return m_defaultCellAttr
->GetFont();
5281 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5283 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5286 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5288 return m_defaultCellAttr
->GetRenderer();
5291 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
5293 return m_defaultCellAttr
->GetEditor();
5296 // ----------------------------------------------------------------------------
5297 // access to cell attributes
5298 // ----------------------------------------------------------------------------
5300 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5302 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5303 wxColour colour
= attr
->GetBackgroundColour();
5308 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5310 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5311 wxColour colour
= attr
->GetTextColour();
5316 wxFont
wxGrid::GetCellFont( int row
, int col
)
5318 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5319 wxFont font
= attr
->GetFont();
5324 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5326 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5327 attr
->GetAlignment(horiz
, vert
);
5331 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5333 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5334 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5339 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
5341 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5342 wxGridCellEditor
* editor
= attr
->GetEditor();
5347 // ----------------------------------------------------------------------------
5348 // attribute support: cache, automatic provider creation, ...
5349 // ----------------------------------------------------------------------------
5351 bool wxGrid::CanHaveAttributes()
5358 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5359 // table is providing the attributes itself??? In which case
5360 // I don't think the grid should create a Provider object for the
5361 // table but the table should be smart enough to do that on its own.
5362 if ( !m_table
->GetAttrProvider() )
5364 // use the default attr provider by default
5365 // (another choice would be to just return FALSE thus forcing the user
5367 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5373 void wxGrid::ClearAttrCache()
5375 if ( m_attrCache
.row
!= -1 )
5377 m_attrCache
.attr
->SafeDecRef();
5378 m_attrCache
.row
= -1;
5382 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5384 wxGrid
*self
= (wxGrid
*)this; // const_cast
5386 self
->ClearAttrCache();
5387 self
->m_attrCache
.row
= row
;
5388 self
->m_attrCache
.col
= col
;
5389 self
->m_attrCache
.attr
= attr
;
5393 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5395 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5397 *attr
= m_attrCache
.attr
;
5398 (*attr
)->SafeIncRef();
5400 #ifdef DEBUG_ATTR_CACHE
5401 gs_nAttrCacheHits
++;
5408 #ifdef DEBUG_ATTR_CACHE
5409 gs_nAttrCacheMisses
++;
5415 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5417 wxGridCellAttr
*attr
;
5418 if ( !LookupAttr(row
, col
, &attr
) )
5420 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5421 CacheAttr(row
, col
, attr
);
5424 attr
->SetDefAttr(m_defaultCellAttr
);
5426 attr
= m_defaultCellAttr
;
5433 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5435 wxGridCellAttr
*attr
;
5436 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5438 wxASSERT_MSG( m_table
,
5439 _T("we may only be called if CanHaveAttributes() "
5440 "returned TRUE and then m_table should be !NULL") );
5442 attr
= m_table
->GetAttr(row
, col
);
5445 attr
= new wxGridCellAttr
;
5447 // artificially inc the ref count to match DecRef() in caller
5450 m_table
->SetAttr(attr
, row
, col
);
5453 CacheAttr(row
, col
, attr
);
5455 attr
->SetDefAttr(m_defaultCellAttr
);
5459 // ----------------------------------------------------------------------------
5460 // setting cell attributes: this is forwarded to the table
5461 // ----------------------------------------------------------------------------
5463 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5465 if ( CanHaveAttributes() )
5467 m_table
->SetRowAttr(attr
, row
);
5475 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5477 if ( CanHaveAttributes() )
5479 m_table
->SetColAttr(attr
, col
);
5487 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5489 if ( CanHaveAttributes() )
5491 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5492 attr
->SetBackgroundColour(colour
);
5497 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5499 if ( CanHaveAttributes() )
5501 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5502 attr
->SetTextColour(colour
);
5507 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5509 if ( CanHaveAttributes() )
5511 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5512 attr
->SetFont(font
);
5517 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5519 if ( CanHaveAttributes() )
5521 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5522 attr
->SetAlignment(horiz
, vert
);
5527 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5529 if ( CanHaveAttributes() )
5531 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5532 attr
->SetRenderer(renderer
);
5537 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
5539 if ( CanHaveAttributes() )
5541 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5542 attr
->SetEditor(editor
);
5547 // ----------------------------------------------------------------------------
5549 // ----------------------------------------------------------------------------
5551 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5553 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5555 if ( resizeExistingRows
)
5559 for ( row
= 0; row
< m_numRows
; row
++ )
5561 m_rowHeights
[row
] = m_defaultRowHeight
;
5562 bottom
+= m_defaultRowHeight
;
5563 m_rowBottoms
[row
] = bottom
;
5569 void wxGrid::SetRowSize( int row
, int height
)
5571 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5575 int h
= wxMax( 0, height
);
5576 int diff
= h
- m_rowHeights
[row
];
5578 m_rowHeights
[row
] = h
;
5579 for ( i
= row
; i
< m_numRows
; i
++ )
5581 m_rowBottoms
[i
] += diff
;
5586 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5588 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5590 if ( resizeExistingCols
)
5594 for ( col
= 0; col
< m_numCols
; col
++ )
5596 m_colWidths
[col
] = m_defaultColWidth
;
5597 right
+= m_defaultColWidth
;
5598 m_colRights
[col
] = right
;
5604 void wxGrid::SetColSize( int col
, int width
)
5606 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5610 int w
= wxMax( 0, width
);
5611 int diff
= w
- m_colWidths
[col
];
5612 m_colWidths
[col
] = w
;
5614 for ( i
= col
; i
< m_numCols
; i
++ )
5616 m_colRights
[i
] += diff
;
5623 // ------ cell value accessor functions
5626 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5630 m_table
->SetValue( row
, col
, s
.c_str() );
5631 if ( !GetBatchCount() )
5633 wxClientDC
dc( m_gridWin
);
5635 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5638 #if 0 // TODO: edit in place
5640 if ( m_currentCellCoords
.GetRow() == row
&&
5641 m_currentCellCoords
.GetCol() == col
)
5643 SetEditControlValue( s
);
5652 // ------ Block, row and col selection
5655 void wxGrid::SelectRow( int row
, bool addToSelected
)
5659 if ( IsSelection() && addToSelected
)
5662 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5665 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5666 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5667 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5668 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5672 need_refresh
[0] = TRUE
;
5673 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5674 wxGridCellCoords ( oldTop
- 1,
5676 m_selectedTopLeft
.SetRow( row
);
5681 need_refresh
[1] = TRUE
;
5682 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5683 wxGridCellCoords ( oldBottom
,
5686 m_selectedTopLeft
.SetCol( 0 );
5689 if ( oldBottom
< row
)
5691 need_refresh
[2] = TRUE
;
5692 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5693 wxGridCellCoords ( row
,
5695 m_selectedBottomRight
.SetRow( row
);
5698 if ( oldRight
< m_numCols
- 1 )
5700 need_refresh
[3] = TRUE
;
5701 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5703 wxGridCellCoords ( oldBottom
,
5705 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5708 for (i
= 0; i
< 4; i
++ )
5709 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5710 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5714 r
= SelectionToDeviceRect();
5716 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5718 m_selectedTopLeft
.Set( row
, 0 );
5719 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5720 r
= SelectionToDeviceRect();
5721 m_gridWin
->Refresh( FALSE
, &r
);
5724 wxGridRangeSelectEvent
gridEvt( GetId(),
5725 EVT_GRID_RANGE_SELECT
,
5728 m_selectedBottomRight
);
5730 GetEventHandler()->ProcessEvent(gridEvt
);
5734 void wxGrid::SelectCol( int col
, bool addToSelected
)
5736 if ( IsSelection() && addToSelected
)
5739 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5742 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5743 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5744 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5745 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5747 if ( oldLeft
> col
)
5749 need_refresh
[0] = TRUE
;
5750 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5751 wxGridCellCoords ( m_numRows
- 1,
5753 m_selectedTopLeft
.SetCol( col
);
5758 need_refresh
[1] = TRUE
;
5759 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5760 wxGridCellCoords ( oldTop
- 1,
5762 m_selectedTopLeft
.SetRow( 0 );
5765 if ( oldRight
< col
)
5767 need_refresh
[2] = TRUE
;
5768 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5769 wxGridCellCoords ( m_numRows
- 1,
5771 m_selectedBottomRight
.SetCol( col
);
5774 if ( oldBottom
< m_numRows
- 1 )
5776 need_refresh
[3] = TRUE
;
5777 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5779 wxGridCellCoords ( m_numRows
- 1,
5781 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5784 for (i
= 0; i
< 4; i
++ )
5785 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5786 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5792 r
= SelectionToDeviceRect();
5794 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5796 m_selectedTopLeft
.Set( 0, col
);
5797 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5798 r
= SelectionToDeviceRect();
5799 m_gridWin
->Refresh( FALSE
, &r
);
5802 wxGridRangeSelectEvent
gridEvt( GetId(),
5803 EVT_GRID_RANGE_SELECT
,
5806 m_selectedBottomRight
);
5808 GetEventHandler()->ProcessEvent(gridEvt
);
5812 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5815 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5817 if ( topRow
> bottomRow
)
5824 if ( leftCol
> rightCol
)
5831 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5832 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5834 if ( m_selectedTopLeft
!= updateTopLeft
||
5835 m_selectedBottomRight
!= updateBottomRight
)
5837 // Compute two optimal update rectangles:
5838 // Either one rectangle is a real subset of the
5839 // other, or they are (almost) disjoint!
5841 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5844 // Store intermediate values
5845 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5846 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5847 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5848 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5850 // Determine the outer/inner coordinates.
5851 if (oldLeft
> leftCol
)
5857 if (oldTop
> topRow
)
5863 if (oldRight
< rightCol
)
5866 oldRight
= rightCol
;
5869 if (oldBottom
< bottomRow
)
5872 oldBottom
= bottomRow
;
5876 // Now, either the stuff marked old is the outer
5877 // rectangle or we don't have a situation where one
5878 // is contained in the other.
5880 if ( oldLeft
< leftCol
)
5882 need_refresh
[0] = TRUE
;
5883 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5885 wxGridCellCoords ( oldBottom
,
5889 if ( oldTop
< topRow
)
5891 need_refresh
[1] = TRUE
;
5892 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5894 wxGridCellCoords ( topRow
- 1,
5898 if ( oldRight
> rightCol
)
5900 need_refresh
[2] = TRUE
;
5901 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5903 wxGridCellCoords ( oldBottom
,
5907 if ( oldBottom
> bottomRow
)
5909 need_refresh
[3] = TRUE
;
5910 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5912 wxGridCellCoords ( oldBottom
,
5918 m_selectedTopLeft
= updateTopLeft
;
5919 m_selectedBottomRight
= updateBottomRight
;
5921 // various Refresh() calls
5922 for (i
= 0; i
< 4; i
++ )
5923 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5924 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5927 // only generate an event if the block is not being selected by
5928 // dragging the mouse (in which case the event will be generated in
5929 // the mouse event handler)
5930 if ( !m_isDragging
)
5932 wxGridRangeSelectEvent
gridEvt( GetId(),
5933 EVT_GRID_RANGE_SELECT
,
5936 m_selectedBottomRight
);
5938 GetEventHandler()->ProcessEvent(gridEvt
);
5942 void wxGrid::SelectAll()
5944 m_selectedTopLeft
.Set( 0, 0 );
5945 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5947 m_gridWin
->Refresh();
5951 void wxGrid::ClearSelection()
5953 m_selectedTopLeft
= wxGridNoCellCoords
;
5954 m_selectedBottomRight
= wxGridNoCellCoords
;
5958 // This function returns the rectangle that encloses the given block
5959 // in device coords clipped to the client size of the grid window.
5961 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5962 const wxGridCellCoords
&bottomRight
)
5964 wxRect
rect( wxGridNoCellRect
);
5967 cellRect
= CellToRect( topLeft
);
5968 if ( cellRect
!= wxGridNoCellRect
)
5974 rect
= wxRect( 0, 0, 0, 0 );
5977 cellRect
= CellToRect( bottomRight
);
5978 if ( cellRect
!= wxGridNoCellRect
)
5984 return wxGridNoCellRect
;
5987 // convert to scrolled coords
5989 int left
, top
, right
, bottom
;
5990 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5991 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5994 m_gridWin
->GetClientSize( &cw
, &ch
);
5996 rect
.SetLeft( wxMax(0, left
) );
5997 rect
.SetTop( wxMax(0, top
) );
5998 rect
.SetRight( wxMin(cw
, right
) );
5999 rect
.SetBottom( wxMin(ch
, bottom
) );
6007 // ------ Grid event classes
6010 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
6012 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
6013 int row
, int col
, int x
, int y
,
6014 bool control
, bool shift
, bool alt
, bool meta
)
6015 : wxNotifyEvent( type
, id
)
6021 m_control
= control
;
6026 SetEventObject(obj
);
6030 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
6032 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
6033 int rowOrCol
, int x
, int y
,
6034 bool control
, bool shift
, bool alt
, bool meta
)
6035 : wxNotifyEvent( type
, id
)
6037 m_rowOrCol
= rowOrCol
;
6040 m_control
= control
;
6045 SetEventObject(obj
);
6049 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
6051 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
6052 const wxGridCellCoords
& topLeft
,
6053 const wxGridCellCoords
& bottomRight
,
6054 bool control
, bool shift
, bool alt
, bool meta
)
6055 : wxNotifyEvent( type
, id
)
6057 m_topLeft
= topLeft
;
6058 m_bottomRight
= bottomRight
;
6059 m_control
= control
;
6064 SetEventObject(obj
);
6068 #endif // ifndef wxUSE_NEW_GRID