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"
47 #include "wx/generic/grid.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
55 struct wxGridCellWithAttr
57 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
58 : coords(row
, col
), attr(attr_
)
67 wxGridCellCoords coords
;
71 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
73 #include "wx/arrimpl.cpp"
75 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
76 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
85 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
86 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
87 const wxPoint
&pos
, const wxSize
&size
);
92 void OnPaint( wxPaintEvent
& event
);
93 void OnMouseEvent( wxMouseEvent
& event
);
94 void OnKeyDown( wxKeyEvent
& event
);
96 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
101 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
104 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
105 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
106 const wxPoint
&pos
, const wxSize
&size
);
111 void OnPaint( wxPaintEvent
&event
);
112 void OnMouseEvent( wxMouseEvent
& event
);
113 void OnKeyDown( wxKeyEvent
& event
);
115 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
116 DECLARE_EVENT_TABLE()
120 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
123 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
124 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
125 const wxPoint
&pos
, const wxSize
&size
);
130 void OnMouseEvent( wxMouseEvent
& event
);
131 void OnKeyDown( wxKeyEvent
& event
);
132 void OnPaint( wxPaintEvent
& event
);
134 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
135 DECLARE_EVENT_TABLE()
138 class WXDLLEXPORT wxGridWindow
: public wxPanel
143 m_owner
= (wxGrid
*)NULL
;
144 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
145 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
148 wxGridWindow( wxGrid
*parent
,
149 wxGridRowLabelWindow
*rowLblWin
,
150 wxGridColLabelWindow
*colLblWin
,
151 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
154 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
158 wxGridRowLabelWindow
*m_rowLabelWin
;
159 wxGridColLabelWindow
*m_colLabelWin
;
161 void OnPaint( wxPaintEvent
&event
);
162 void OnMouseEvent( wxMouseEvent
& event
);
163 void OnKeyDown( wxKeyEvent
& );
164 void OnEraseBackground( wxEraseEvent
& );
167 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
168 DECLARE_EVENT_TABLE()
173 class wxGridCellEditorEvtHandler
: public wxEvtHandler
176 wxGridCellEditorEvtHandler()
177 : m_grid(0), m_editor(0)
179 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
180 : m_grid(grid
), m_editor(editor
)
183 void OnKeyDown(wxKeyEvent
& event
);
187 wxGridCellEditor
* m_editor
;
188 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
189 DECLARE_EVENT_TABLE()
193 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
194 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
195 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
200 // ----------------------------------------------------------------------------
201 // the internal data representation used by wxGridCellAttrProvider
202 // ----------------------------------------------------------------------------
204 // this class stores attributes set for cells
205 class WXDLLEXPORT wxGridCellAttrData
208 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
209 wxGridCellAttr
*GetAttr(int row
, int col
) const;
212 // searches for the attr for given cell, returns wxNOT_FOUND if not found
213 int FindIndex(int row
, int col
) const;
215 wxGridCellWithAttrArray m_attrs
;
218 // this class stores attributes set for rows or columns
219 class WXDLLEXPORT wxGridRowOrColAttrData
222 ~wxGridRowOrColAttrData();
224 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
225 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
228 wxArrayInt m_rowsOrCols
;
229 wxArrayAttrs m_attrs
;
232 // NB: this is just a wrapper around 3 objects: one which stores cell
233 // attributes, and 2 others for row/col ones
234 class WXDLLEXPORT wxGridCellAttrProviderData
237 wxGridCellAttrData m_cellAttrs
;
238 wxGridRowOrColAttrData m_rowAttrs
,
242 // ----------------------------------------------------------------------------
243 // conditional compilation
244 // ----------------------------------------------------------------------------
246 #ifndef WXGRID_DRAW_LINES
247 #define WXGRID_DRAW_LINES 1
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 //#define DEBUG_ATTR_CACHE
255 #ifdef DEBUG_ATTR_CACHE
256 static size_t gs_nAttrCacheHits
= 0;
257 static size_t gs_nAttrCacheMisses
= 0;
258 #endif // DEBUG_ATTR_CACHE
260 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
261 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
264 // TODO: fixed so far - make configurable later (and also different for x/y)
265 static const size_t GRID_SCROLL_LINE
= 10;
267 // ============================================================================
269 // ============================================================================
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 wxGridCellEditor::wxGridCellEditor()
282 wxGridCellEditor::~wxGridCellEditor()
288 void wxGridCellEditor::Destroy()
291 m_control
->Destroy();
296 void wxGridCellEditor::Show(bool show
)
298 wxASSERT_MSG(m_control
,
299 wxT("The wxGridCellEditor must be Created first!"));
300 m_control
->Show(show
);
303 void wxGridCellEditor::SetSize(const wxRect
& rect
)
305 wxASSERT_MSG(m_control
,
306 wxT("The wxGridCellEditor must be Created first!"));
307 m_control
->SetSize(rect
);
310 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
316 wxGridCellTextEditor::wxGridCellTextEditor()
320 void wxGridCellTextEditor::Create(wxWindow
* parent
,
324 wxEvtHandler
* evtHandler
)
326 m_control
= new wxTextCtrl(parent
, -1, "", pos
, size
327 #if defined(__WXMSW__)
328 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
333 m_control
->PushEventHandler(evtHandler
);
337 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
,
338 wxGridCellAttr
* attr
)
340 wxASSERT_MSG(m_control
,
341 wxT("The wxGridCellEditor must be Created first!"));
343 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
344 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
345 ((wxTextCtrl
*)m_control
)->SetInsertionPointEnd();
346 ((wxTextCtrl
*)m_control
)->SetFocus();
348 // ??? Should we use attr and try to set colours and font?
353 bool wxGridCellTextEditor::EndEdit(int row
, int col
, bool saveValue
,
354 wxGrid
* grid
, wxGridCellAttr
* attr
)
356 wxASSERT_MSG(m_control
,
357 wxT("The wxGridCellEditor must be Created first!"));
359 bool changed
= FALSE
;
360 wxString value
= ((wxTextCtrl
*)m_control
)->GetValue();
361 if (value
!= m_startValue
)
365 grid
->GetTable()->SetValue(row
, col
, value
);
372 void wxGridCellTextEditor::Reset()
374 wxASSERT_MSG(m_control
,
375 wxT("The wxGridCellEditor must be Created first!"));
377 ((wxTextCtrl
*)m_control
)->SetValue(m_startValue
);
378 ((wxTextCtrl
*)m_control
)->SetInsertionPointEnd();
381 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
383 #if defined(__WXMOTIF__) || defined(__WXGTK__)
384 // wxMotif needs a little extra help...
385 int pos
= ((wxTextCtrl
*)m_control
)->GetInsertionPoint();
386 wxString
s( ((wxTextCtrl
*)m_control
)->GetValue() );
387 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
388 ((wxTextCtrl
*)m_control
)->SetValue(s
);
389 ((wxTextCtrl
*)m_control
)->SetInsertionPoint( pos
);
391 // the other ports can handle a Return key press
398 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
400 switch ( event
.KeyCode() )
413 // send the event to the parent grid, skipping the
414 // event if nothing happens
416 event
.Skip( m_grid
->ProcessEvent( event
) );
420 if (!m_grid
->ProcessEvent(event
))
421 m_editor
->HandleReturn(event
);
426 // send the event to the parent grid, skipping the
427 // event if nothing happens
429 event
.Skip( m_grid
->ProcessEvent( event
) );
437 // ----------------------------------------------------------------------------
438 // wxGridCellRenderer
439 // ----------------------------------------------------------------------------
441 void wxGridCellRenderer::Draw(wxGrid
& grid
,
442 wxGridCellAttr
& attr
,
448 dc
.SetBackgroundMode( wxSOLID
);
452 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
456 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
459 dc
.SetPen( *wxTRANSPARENT_PEN
);
461 dc
.DrawRectangle(rect
);
464 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
465 wxGridCellAttr
& attr
,
467 const wxRect
& rectCell
,
471 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
473 // now we only have to draw the text
474 dc
.SetBackgroundMode( wxTRANSPARENT
);
478 dc
.SetTextBackground( grid
.GetSelectionBackground() );
479 dc
.SetTextForeground( grid
.GetSelectionForeground() );
483 dc
.SetTextBackground( attr
.GetBackgroundColour() );
484 dc
.SetTextForeground( attr
.GetTextColour() );
486 dc
.SetFont( attr
.GetFont() );
489 attr
.GetAlignment(&hAlign
, &vAlign
);
491 wxRect rect
= rectCell
;
497 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
498 rect
, hAlign
, vAlign
);
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
505 const wxColour
& wxGridCellAttr::GetTextColour() const
509 else if (m_defGridAttr
!= this)
510 return m_defGridAttr
->GetTextColour();
512 wxFAIL_MSG(wxT("Missing default cell attribute"));
518 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
520 if (HasBackgroundColour())
522 else if (m_defGridAttr
!= this)
523 return m_defGridAttr
->GetBackgroundColour();
525 wxFAIL_MSG(wxT("Missing default cell attribute"));
531 const wxFont
& wxGridCellAttr::GetFont() const
535 else if (m_defGridAttr
!= this)
536 return m_defGridAttr
->GetFont();
538 wxFAIL_MSG(wxT("Missing default cell attribute"));
544 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
546 if (HasAlignment()) {
547 if ( hAlign
) *hAlign
= m_hAlign
;
548 if ( vAlign
) *vAlign
= m_vAlign
;
550 else if (m_defGridAttr
!= this)
551 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
553 wxFAIL_MSG(wxT("Missing default cell attribute"));
558 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
562 else if (m_defGridAttr
!= this)
563 return m_defGridAttr
->GetRenderer();
565 wxFAIL_MSG(wxT("Missing default cell attribute"));
570 // ----------------------------------------------------------------------------
571 // wxGridCellAttrData
572 // ----------------------------------------------------------------------------
574 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
576 int n
= FindIndex(row
, col
);
577 if ( n
== wxNOT_FOUND
)
580 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
586 // change the attribute
587 m_attrs
[(size_t)n
].attr
= attr
;
591 // remove this attribute
592 m_attrs
.RemoveAt((size_t)n
);
597 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
599 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
601 int n
= FindIndex(row
, col
);
602 if ( n
!= wxNOT_FOUND
)
604 attr
= m_attrs
[(size_t)n
].attr
;
611 int wxGridCellAttrData::FindIndex(int row
, int col
) const
613 size_t count
= m_attrs
.GetCount();
614 for ( size_t n
= 0; n
< count
; n
++ )
616 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
617 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
626 // ----------------------------------------------------------------------------
627 // wxGridRowOrColAttrData
628 // ----------------------------------------------------------------------------
630 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
632 size_t count
= m_attrs
.Count();
633 for ( size_t n
= 0; n
< count
; n
++ )
635 m_attrs
[n
]->DecRef();
639 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
641 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
643 int n
= m_rowsOrCols
.Index(rowOrCol
);
644 if ( n
!= wxNOT_FOUND
)
646 attr
= m_attrs
[(size_t)n
];
653 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
655 int n
= m_rowsOrCols
.Index(rowOrCol
);
656 if ( n
== wxNOT_FOUND
)
659 m_rowsOrCols
.Add(rowOrCol
);
666 // change the attribute
667 m_attrs
[(size_t)n
] = attr
;
671 // remove this attribute
672 m_attrs
[(size_t)n
]->DecRef();
673 m_rowsOrCols
.RemoveAt((size_t)n
);
674 m_attrs
.RemoveAt((size_t)n
);
679 // ----------------------------------------------------------------------------
680 // wxGridCellAttrProvider
681 // ----------------------------------------------------------------------------
683 wxGridCellAttrProvider::wxGridCellAttrProvider()
685 m_data
= (wxGridCellAttrProviderData
*)NULL
;
688 wxGridCellAttrProvider::~wxGridCellAttrProvider()
693 void wxGridCellAttrProvider::InitData()
695 m_data
= new wxGridCellAttrProviderData
;
698 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
700 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
703 // first look for the attribute of this specific cell
704 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
708 // then look for the col attr (col attributes are more common than
709 // the row ones, hence they have priority)
710 attr
= m_data
->m_colAttrs
.GetAttr(col
);
715 // finally try the row attributes
716 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
723 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
729 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
732 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
737 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
740 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
745 m_data
->m_colAttrs
.SetAttr(attr
, col
);
748 // ----------------------------------------------------------------------------
750 // ----------------------------------------------------------------------------
752 //////////////////////////////////////////////////////////////////////
754 // Abstract base class for grid data (the model)
756 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
759 wxGridTableBase::wxGridTableBase()
761 m_view
= (wxGrid
*) NULL
;
762 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
765 wxGridTableBase::~wxGridTableBase()
767 delete m_attrProvider
;
770 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
772 delete m_attrProvider
;
773 m_attrProvider
= attrProvider
;
776 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
778 if ( m_attrProvider
)
779 return m_attrProvider
->GetAttr(row
, col
);
781 return (wxGridCellAttr
*)NULL
;
784 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
786 if ( m_attrProvider
)
788 m_attrProvider
->SetAttr(attr
, row
, col
);
792 // as we take ownership of the pointer and don't store it, we must
798 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
800 if ( m_attrProvider
)
802 m_attrProvider
->SetRowAttr(attr
, row
);
806 // as we take ownership of the pointer and don't store it, we must
812 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
814 if ( m_attrProvider
)
816 m_attrProvider
->SetColAttr(attr
, col
);
820 // as we take ownership of the pointer and don't store it, we must
826 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
828 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
829 "but your derived table class does not override this function") );
834 bool wxGridTableBase::AppendRows( size_t numRows
)
836 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
837 "but your derived table class does not override this function"));
842 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
844 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
845 "but your derived table class does not override this function"));
850 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
852 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
853 "but your derived table class does not override this function"));
858 bool wxGridTableBase::AppendCols( size_t numCols
)
860 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
861 "but your derived table class does not override this function"));
866 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
868 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
869 "but your derived table class does not override this function"));
875 wxString
wxGridTableBase::GetRowLabelValue( int row
)
882 wxString
wxGridTableBase::GetColLabelValue( int col
)
884 // default col labels are:
885 // cols 0 to 25 : A-Z
886 // cols 26 to 675 : AA-ZZ
893 s
+= (_T('A') + (wxChar
)( col%26
));
895 if ( col
< 0 ) break;
898 // reverse the string...
900 for ( i
= 0; i
< n
; i
++ )
910 //////////////////////////////////////////////////////////////////////
912 // Message class for the grid table to send requests and notifications
916 wxGridTableMessage::wxGridTableMessage()
918 m_table
= (wxGridTableBase
*) NULL
;
924 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
925 int commandInt1
, int commandInt2
)
929 m_comInt1
= commandInt1
;
930 m_comInt2
= commandInt2
;
935 //////////////////////////////////////////////////////////////////////
937 // A basic grid table for string data. An object of this class will
938 // created by wxGrid if you don't specify an alternative table class.
941 WX_DEFINE_OBJARRAY(wxGridStringArray
)
943 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
945 wxGridStringTable::wxGridStringTable()
950 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
955 m_data
.Alloc( numRows
);
959 for ( col
= 0; col
< numCols
; col
++ )
961 sa
.Add( wxEmptyString
);
964 for ( row
= 0; row
< numRows
; row
++ )
970 wxGridStringTable::~wxGridStringTable()
974 long wxGridStringTable::GetNumberRows()
976 return m_data
.GetCount();
979 long wxGridStringTable::GetNumberCols()
981 if ( m_data
.GetCount() > 0 )
982 return m_data
[0].GetCount();
987 wxString
wxGridStringTable::GetValue( int row
, int col
)
989 // TODO: bounds checking
991 return m_data
[row
][col
];
994 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
996 // TODO: bounds checking
998 m_data
[row
][col
] = s
;
1001 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1003 // TODO: bounds checking
1005 return (m_data
[row
][col
] == wxEmptyString
);
1009 void wxGridStringTable::Clear()
1012 int numRows
, numCols
;
1014 numRows
= m_data
.GetCount();
1017 numCols
= m_data
[0].GetCount();
1019 for ( row
= 0; row
< numRows
; row
++ )
1021 for ( col
= 0; col
< numCols
; col
++ )
1023 m_data
[row
][col
] = wxEmptyString
;
1030 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1034 size_t curNumRows
= m_data
.GetCount();
1035 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1037 if ( pos
>= curNumRows
)
1039 return AppendRows( numRows
);
1043 sa
.Alloc( curNumCols
);
1044 for ( col
= 0; col
< curNumCols
; col
++ )
1046 sa
.Add( wxEmptyString
);
1049 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1051 m_data
.Insert( sa
, row
);
1056 wxGridTableMessage
msg( this,
1057 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1061 GetView()->ProcessTableMessage( msg
);
1067 bool wxGridStringTable::AppendRows( size_t numRows
)
1071 size_t curNumRows
= m_data
.GetCount();
1072 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1075 if ( curNumCols
> 0 )
1077 sa
.Alloc( curNumCols
);
1078 for ( col
= 0; col
< curNumCols
; col
++ )
1080 sa
.Add( wxEmptyString
);
1084 for ( row
= 0; row
< numRows
; row
++ )
1091 wxGridTableMessage
msg( this,
1092 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1095 GetView()->ProcessTableMessage( msg
);
1101 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1105 size_t curNumRows
= m_data
.GetCount();
1107 if ( pos
>= curNumRows
)
1110 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1111 "Pos value is invalid for present table with %d rows",
1112 pos
, numRows
, curNumRows
);
1113 wxFAIL_MSG( wxT(errmsg
) );
1117 if ( numRows
> curNumRows
- pos
)
1119 numRows
= curNumRows
- pos
;
1122 if ( numRows
>= curNumRows
)
1124 m_data
.Empty(); // don't release memory just yet
1128 for ( n
= 0; n
< numRows
; n
++ )
1130 m_data
.Remove( pos
);
1136 wxGridTableMessage
msg( this,
1137 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1141 GetView()->ProcessTableMessage( msg
);
1147 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1151 size_t curNumRows
= m_data
.GetCount();
1152 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1154 if ( pos
>= curNumCols
)
1156 return AppendCols( numCols
);
1159 for ( row
= 0; row
< curNumRows
; row
++ )
1161 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1163 m_data
[row
].Insert( wxEmptyString
, col
);
1169 wxGridTableMessage
msg( this,
1170 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1174 GetView()->ProcessTableMessage( msg
);
1180 bool wxGridStringTable::AppendCols( size_t numCols
)
1184 size_t curNumRows
= m_data
.GetCount();
1187 // TODO: something better than this ?
1189 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1190 "Call AppendRows() first") );
1194 for ( row
= 0; row
< curNumRows
; row
++ )
1196 for ( n
= 0; n
< numCols
; n
++ )
1198 m_data
[row
].Add( wxEmptyString
);
1204 wxGridTableMessage
msg( this,
1205 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1208 GetView()->ProcessTableMessage( msg
);
1214 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1218 size_t curNumRows
= m_data
.GetCount();
1219 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1221 if ( pos
>= curNumCols
)
1224 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1225 "Pos value is invalid for present table with %d cols",
1226 pos
, numCols
, curNumCols
);
1227 wxFAIL_MSG( wxT( errmsg
) );
1231 if ( numCols
> curNumCols
- pos
)
1233 numCols
= curNumCols
- pos
;
1236 for ( row
= 0; row
< curNumRows
; row
++ )
1238 if ( numCols
>= curNumCols
)
1240 m_data
[row
].Clear();
1244 for ( n
= 0; n
< numCols
; n
++ )
1246 m_data
[row
].Remove( pos
);
1253 wxGridTableMessage
msg( this,
1254 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1258 GetView()->ProcessTableMessage( msg
);
1264 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1266 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1268 // using default label
1270 return wxGridTableBase::GetRowLabelValue( row
);
1274 return m_rowLabels
[ row
];
1278 wxString
wxGridStringTable::GetColLabelValue( int col
)
1280 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1282 // using default label
1284 return wxGridTableBase::GetColLabelValue( col
);
1288 return m_colLabels
[ col
];
1292 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1294 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1296 int n
= m_rowLabels
.GetCount();
1298 for ( i
= n
; i
<= row
; i
++ )
1300 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1304 m_rowLabels
[row
] = value
;
1307 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1309 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1311 int n
= m_colLabels
.GetCount();
1313 for ( i
= n
; i
<= col
; i
++ )
1315 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1319 m_colLabels
[col
] = value
;
1325 //////////////////////////////////////////////////////////////////////
1327 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
1329 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
1330 EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown
)
1334 wxGridTextCtrl::wxGridTextCtrl( wxWindow
*par
,
1338 const wxString
& value
,
1342 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
1345 m_isCellControl
= isCellControl
;
1349 void wxGridTextCtrl::OnKeyDown( wxKeyEvent
& event
)
1351 switch ( event
.KeyCode() )
1354 m_grid
->SetEditControlValue( startValue
);
1355 SetInsertionPointEnd();
1365 if ( m_isCellControl
)
1367 // send the event to the parent grid, skipping the
1368 // event if nothing happens
1370 event
.Skip( m_grid
->ProcessEvent( event
) );
1374 // default text control response within the top edit
1382 if ( m_isCellControl
)
1384 if ( !m_grid
->ProcessEvent( event
) )
1386 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1387 // wxMotif needs a little extra help...
1389 int pos
= GetInsertionPoint();
1390 wxString
s( GetValue() );
1391 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
1393 SetInsertionPoint( pos
);
1395 // the other ports can handle a Return key press
1405 if ( m_isCellControl
)
1407 // send the event to the parent grid, skipping the
1408 // event if nothing happens
1410 event
.Skip( m_grid
->ProcessEvent( event
) );
1414 // default text control response within the top edit
1426 void wxGridTextCtrl::SetStartValue( const wxString
& s
)
1429 wxTextCtrl::SetValue(s
);
1434 //////////////////////////////////////////////////////////////////////
1436 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1438 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1439 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1440 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1441 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1444 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1446 const wxPoint
&pos
, const wxSize
&size
)
1447 : wxWindow( parent
, id
, pos
, size
)
1452 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1456 // NO - don't do this because it will set both the x and y origin
1457 // coords to match the parent scrolled window and we just want to
1458 // set the y coord - MB
1460 // m_owner->PrepareDC( dc );
1463 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1464 dc
.SetDeviceOrigin( 0, -y
);
1466 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1467 m_owner
->DrawRowLabels( dc
);
1471 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1473 m_owner
->ProcessRowLabelMouseEvent( event
);
1477 // This seems to be required for wxMotif otherwise the mouse
1478 // cursor must be in the cell edit control to get key events
1480 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1482 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1487 //////////////////////////////////////////////////////////////////////
1489 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1491 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1492 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1493 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1494 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1497 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1499 const wxPoint
&pos
, const wxSize
&size
)
1500 : wxWindow( parent
, id
, pos
, size
)
1505 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1509 // NO - don't do this because it will set both the x and y origin
1510 // coords to match the parent scrolled window and we just want to
1511 // set the x coord - MB
1513 // m_owner->PrepareDC( dc );
1516 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1517 dc
.SetDeviceOrigin( -x
, 0 );
1519 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1520 m_owner
->DrawColLabels( dc
);
1524 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1526 m_owner
->ProcessColLabelMouseEvent( event
);
1530 // This seems to be required for wxMotif otherwise the mouse
1531 // cursor must be in the cell edit control to get key events
1533 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1535 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1540 //////////////////////////////////////////////////////////////////////
1542 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1544 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1545 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1546 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1547 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1550 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1552 const wxPoint
&pos
, const wxSize
&size
)
1553 : wxWindow( parent
, id
, pos
, size
)
1558 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1562 int client_height
= 0;
1563 int client_width
= 0;
1564 GetClientSize( &client_width
, &client_height
);
1566 dc
.SetPen( *wxBLACK_PEN
);
1567 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1568 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1570 dc
.SetPen( *wxWHITE_PEN
);
1571 dc
.DrawLine( 0, 0, client_width
, 0 );
1572 dc
.DrawLine( 0, 0, 0, client_height
);
1576 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1578 m_owner
->ProcessCornerLabelMouseEvent( event
);
1582 // This seems to be required for wxMotif otherwise the mouse
1583 // cursor must be in the cell edit control to get key events
1585 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1587 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1592 //////////////////////////////////////////////////////////////////////
1594 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1596 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1597 EVT_PAINT( wxGridWindow::OnPaint
)
1598 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1599 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1600 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1603 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1604 wxGridRowLabelWindow
*rowLblWin
,
1605 wxGridColLabelWindow
*colLblWin
,
1606 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1607 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1610 m_rowLabelWin
= rowLblWin
;
1611 m_colLabelWin
= colLblWin
;
1613 SetBackgroundColour( "WHITE" );
1617 wxGridWindow::~wxGridWindow()
1622 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1624 wxPaintDC
dc( this );
1625 m_owner
->PrepareDC( dc
);
1626 wxRegion reg
= GetUpdateRegion();
1627 m_owner
->CalcCellsExposed( reg
);
1628 m_owner
->DrawGridCellArea( dc
);
1629 #if WXGRID_DRAW_LINES
1630 m_owner
->DrawAllGridLines( dc
, reg
);
1635 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1637 wxPanel::ScrollWindow( dx
, dy
, rect
);
1638 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1639 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1643 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1645 m_owner
->ProcessGridCellMouseEvent( event
);
1649 // This seems to be required for wxMotif otherwise the mouse
1650 // cursor must be in the cell edit control to get key events
1652 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1654 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1657 void wxGridWindow::OnEraseBackground(wxEraseEvent
&)
1662 //////////////////////////////////////////////////////////////////////
1664 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1666 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1667 EVT_PAINT( wxGrid::OnPaint
)
1668 EVT_SIZE( wxGrid::OnSize
)
1669 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1670 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1673 wxGrid::wxGrid( wxWindow
*parent
,
1678 const wxString
& name
)
1679 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1688 m_defaultCellAttr
->SafeDecRef();
1690 #ifdef DEBUG_ATTR_CACHE
1691 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1692 wxPrintf(_T("wxGrid attribute cache statistics: "
1693 "total: %u, hits: %u (%u%%)\n"),
1694 total
, gs_nAttrCacheHits
,
1695 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1704 // ----- internal init and update functions
1707 void wxGrid::Create()
1709 m_created
= FALSE
; // set to TRUE by CreateGrid
1710 m_displayed
= FALSE
; // set to TRUE by OnPaint
1712 m_table
= (wxGridTableBase
*) NULL
;
1714 m_cellEditCtrl
= (wxWindow
*) NULL
;
1716 m_defaultCellAttr
= new wxGridCellAttr
;
1717 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1718 // RD: Should we fill the default attrs now or is waiting until Init() okay?
1723 m_currentCellCoords
= wxGridNoCellCoords
;
1725 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1726 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1728 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1733 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1738 m_colLabelWin
= new wxGridColLabelWindow( this,
1743 m_gridWin
= new wxGridWindow( this,
1750 SetTargetWindow( m_gridWin
);
1754 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1758 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1763 m_numRows
= numRows
;
1764 m_numCols
= numCols
;
1766 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1767 m_table
->SetView( this );
1776 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
1780 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1785 m_numRows
= table
->GetNumberRows();
1786 m_numCols
= table
->GetNumberCols();
1789 m_table
->SetView( this );
1804 if ( m_numRows
<= 0 )
1805 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1807 if ( m_numCols
<= 0 )
1808 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1810 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1811 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1813 if ( m_rowLabelWin
)
1815 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1819 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1822 m_labelTextColour
= wxColour( _T("BLACK") );
1825 m_attrCache
.row
= -1;
1827 // TODO: something better than this ?
1829 m_labelFont
= this->GetFont();
1830 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1832 m_rowLabelHorizAlign
= wxLEFT
;
1833 m_rowLabelVertAlign
= wxCENTRE
;
1835 m_colLabelHorizAlign
= wxCENTRE
;
1836 m_colLabelVertAlign
= wxTOP
;
1838 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1839 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1841 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1842 m_defaultRowHeight
+= 8;
1844 m_defaultRowHeight
+= 4;
1847 m_rowHeights
.Alloc( m_numRows
);
1848 m_rowBottoms
.Alloc( m_numRows
);
1850 for ( i
= 0; i
< m_numRows
; i
++ )
1852 m_rowHeights
.Add( m_defaultRowHeight
);
1853 rowBottom
+= m_defaultRowHeight
;
1854 m_rowBottoms
.Add( rowBottom
);
1857 m_colWidths
.Alloc( m_numCols
);
1858 m_colRights
.Alloc( m_numCols
);
1860 for ( i
= 0; i
< m_numCols
; i
++ )
1862 m_colWidths
.Add( m_defaultColWidth
);
1863 colRight
+= m_defaultColWidth
;
1864 m_colRights
.Add( colRight
);
1867 m_defaultCellAttr
->SetFont(GetFont());
1868 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
1869 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1870 m_defaultCellAttr
->SetTextColour(
1871 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
1872 m_defaultCellAttr
->SetBackgroundColour(
1873 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
1876 m_gridLineColour
= wxColour( 128, 128, 255 );
1877 m_gridLinesEnabled
= TRUE
;
1879 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1880 m_winCapture
= (wxWindow
*)NULL
;
1882 m_dragRowOrCol
= -1;
1883 m_isDragging
= FALSE
;
1885 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1886 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1888 m_currentCellCoords
= wxGridNoCellCoords
;
1890 m_selectedTopLeft
= wxGridNoCellCoords
;
1891 m_selectedBottomRight
= wxGridNoCellCoords
;
1892 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
1893 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1895 m_editable
= TRUE
; // default for whole grid
1897 m_inOnKeyDown
= FALSE
;
1900 // TODO: extend this to other types of controls
1902 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1909 #if defined(__WXMSW__)
1910 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1914 m_cellEditCtrl
->Show( FALSE
);
1915 m_cellEditCtrlEnabled
= TRUE
;
1916 m_editCtrlType
= wxGRID_TEXTCTRL
;
1920 void wxGrid::CalcDimensions()
1923 GetClientSize( &cw
, &ch
);
1925 if ( m_numRows
> 0 && m_numCols
> 0 )
1927 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1928 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1930 // TODO: restore the scroll position that we had before sizing
1933 GetViewStart( &x
, &y
);
1934 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1935 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1941 void wxGrid::CalcWindowSizes()
1944 GetClientSize( &cw
, &ch
);
1946 if ( m_cornerLabelWin
->IsShown() )
1947 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
1949 if ( m_colLabelWin
->IsShown() )
1950 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
1952 if ( m_rowLabelWin
->IsShown() )
1953 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
1955 if ( m_gridWin
->IsShown() )
1956 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
1960 // this is called when the grid table sends a message to say that it
1961 // has been redimensioned
1963 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1967 switch ( msg
.GetId() )
1969 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1971 size_t pos
= msg
.GetCommandInt();
1972 int numRows
= msg
.GetCommandInt2();
1973 for ( i
= 0; i
< numRows
; i
++ )
1975 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1976 m_rowBottoms
.Insert( 0, pos
);
1978 m_numRows
+= numRows
;
1981 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1983 for ( i
= pos
; i
< m_numRows
; i
++ )
1985 bottom
+= m_rowHeights
[i
];
1986 m_rowBottoms
[i
] = bottom
;
1992 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1994 int numRows
= msg
.GetCommandInt();
1995 for ( i
= 0; i
< numRows
; i
++ )
1997 m_rowHeights
.Add( m_defaultRowHeight
);
1998 m_rowBottoms
.Add( 0 );
2001 int oldNumRows
= m_numRows
;
2002 m_numRows
+= numRows
;
2005 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2007 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2009 bottom
+= m_rowHeights
[i
];
2010 m_rowBottoms
[i
] = bottom
;
2016 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2018 size_t pos
= msg
.GetCommandInt();
2019 int numRows
= msg
.GetCommandInt2();
2020 for ( i
= 0; i
< numRows
; i
++ )
2022 m_rowHeights
.Remove( pos
);
2023 m_rowBottoms
.Remove( pos
);
2025 m_numRows
-= numRows
;
2030 m_colWidths
.Clear();
2031 m_colRights
.Clear();
2032 m_currentCellCoords
= wxGridNoCellCoords
;
2036 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2037 m_currentCellCoords
.Set( 0, 0 );
2040 for ( i
= 0; i
< m_numRows
; i
++ )
2042 h
+= m_rowHeights
[i
];
2043 m_rowBottoms
[i
] = h
;
2051 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2053 size_t pos
= msg
.GetCommandInt();
2054 int numCols
= msg
.GetCommandInt2();
2055 for ( i
= 0; i
< numCols
; i
++ )
2057 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2058 m_colRights
.Insert( 0, pos
);
2060 m_numCols
+= numCols
;
2063 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2065 for ( i
= pos
; i
< m_numCols
; i
++ )
2067 right
+= m_colWidths
[i
];
2068 m_colRights
[i
] = right
;
2074 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2076 int numCols
= msg
.GetCommandInt();
2077 for ( i
= 0; i
< numCols
; i
++ )
2079 m_colWidths
.Add( m_defaultColWidth
);
2080 m_colRights
.Add( 0 );
2083 int oldNumCols
= m_numCols
;
2084 m_numCols
+= numCols
;
2087 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2089 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2091 right
+= m_colWidths
[i
];
2092 m_colRights
[i
] = right
;
2098 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2100 size_t pos
= msg
.GetCommandInt();
2101 int numCols
= msg
.GetCommandInt2();
2102 for ( i
= 0; i
< numCols
; i
++ )
2104 m_colWidths
.Remove( pos
);
2105 m_colRights
.Remove( pos
);
2107 m_numCols
-= numCols
;
2111 #if 0 // leave the row alone here so that AppendCols will work subsequently
2113 m_rowHeights
.Clear();
2114 m_rowBottoms
.Clear();
2116 m_currentCellCoords
= wxGridNoCellCoords
;
2120 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2121 m_currentCellCoords
.Set( 0, 0 );
2124 for ( i
= 0; i
< m_numCols
; i
++ )
2126 w
+= m_colWidths
[i
];
2139 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2141 wxRegionIterator
iter( reg
);
2144 m_rowLabelsExposed
.Empty();
2151 // TODO: remove this when we can...
2152 // There is a bug in wxMotif that gives garbage update
2153 // rectangles if you jump-scroll a long way by clicking the
2154 // scrollbar with middle button. This is a work-around
2156 #if defined(__WXMOTIF__)
2158 m_gridWin
->GetClientSize( &cw
, &ch
);
2159 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2160 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2163 // logical bounds of update region
2166 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2167 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2169 // find the row labels within these bounds
2173 for ( row
= 0; row
< m_numRows
; row
++ )
2175 if ( m_rowBottoms
[row
] < top
) continue;
2177 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2178 if ( rowTop
> bottom
) break;
2180 m_rowLabelsExposed
.Add( row
);
2188 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2190 wxRegionIterator
iter( reg
);
2193 m_colLabelsExposed
.Empty();
2200 // TODO: remove this when we can...
2201 // There is a bug in wxMotif that gives garbage update
2202 // rectangles if you jump-scroll a long way by clicking the
2203 // scrollbar with middle button. This is a work-around
2205 #if defined(__WXMOTIF__)
2207 m_gridWin
->GetClientSize( &cw
, &ch
);
2208 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2209 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2212 // logical bounds of update region
2215 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2216 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2218 // find the cells within these bounds
2222 for ( col
= 0; col
< m_numCols
; col
++ )
2224 if ( m_colRights
[col
] < left
) continue;
2226 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2227 if ( colLeft
> right
) break;
2229 m_colLabelsExposed
.Add( col
);
2237 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2239 wxRegionIterator
iter( reg
);
2242 m_cellsExposed
.Empty();
2243 m_rowsExposed
.Empty();
2244 m_colsExposed
.Empty();
2246 int left
, top
, right
, bottom
;
2251 // TODO: remove this when we can...
2252 // There is a bug in wxMotif that gives garbage update
2253 // rectangles if you jump-scroll a long way by clicking the
2254 // scrollbar with middle button. This is a work-around
2256 #if defined(__WXMOTIF__)
2258 m_gridWin
->GetClientSize( &cw
, &ch
);
2259 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2260 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2261 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2262 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2265 // logical bounds of update region
2267 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2268 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2270 // find the cells within these bounds
2273 int colLeft
, rowTop
;
2274 for ( row
= 0; row
< m_numRows
; row
++ )
2276 if ( m_rowBottoms
[row
] < top
) continue;
2278 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2279 if ( rowTop
> bottom
) break;
2281 m_rowsExposed
.Add( row
);
2283 for ( col
= 0; col
< m_numCols
; col
++ )
2285 if ( m_colRights
[col
] < left
) continue;
2287 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2288 if ( colLeft
> right
) break;
2290 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2291 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2300 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2303 wxPoint
pos( event
.GetPosition() );
2304 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2306 if ( event
.Dragging() )
2308 m_isDragging
= TRUE
;
2310 if ( event
.LeftIsDown() )
2312 switch( m_cursorMode
)
2314 case WXGRID_CURSOR_RESIZE_ROW
:
2316 int cw
, ch
, left
, dummy
;
2317 m_gridWin
->GetClientSize( &cw
, &ch
);
2318 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2320 wxClientDC
dc( m_gridWin
);
2322 dc
.SetLogicalFunction(wxINVERT
);
2323 if ( m_dragLastPos
>= 0 )
2325 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2327 dc
.DrawLine( left
, y
, left
+cw
, y
);
2332 case WXGRID_CURSOR_SELECT_ROW
:
2333 if ( (row
= YToRow( y
)) >= 0 &&
2334 !IsInSelection( row
, 0 ) )
2336 SelectRow( row
, TRUE
);
2339 // default label to suppress warnings about "enumeration value
2340 // 'xxx' not handled in switch
2348 m_isDragging
= FALSE
;
2351 // ------------ Entering or leaving the window
2353 if ( event
.Entering() || event
.Leaving() )
2355 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2359 // ------------ Left button pressed
2361 else if ( event
.LeftDown() )
2363 // don't send a label click event for a hit on the
2364 // edge of the row label - this is probably the user
2365 // wanting to resize the row
2367 if ( YToEdgeOfRow(y
) < 0 )
2371 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2373 SelectRow( row
, event
.ShiftDown() );
2374 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2379 // starting to drag-resize a row
2381 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2386 // ------------ Left double click
2388 else if (event
.LeftDClick() )
2390 if ( YToEdgeOfRow(y
) < 0 )
2393 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2398 // ------------ Left button released
2400 else if ( event
.LeftUp() )
2402 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2404 DoEndDragResizeRow();
2406 // Note: we are ending the event *after* doing
2407 // default processing in this case
2409 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2412 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2417 // ------------ Right button down
2419 else if ( event
.RightDown() )
2422 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2424 // no default action at the moment
2429 // ------------ Right double click
2431 else if ( event
.RightDClick() )
2434 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2436 // no default action at the moment
2441 // ------------ No buttons down and mouse moving
2443 else if ( event
.Moving() )
2445 m_dragRowOrCol
= YToEdgeOfRow( y
);
2446 if ( m_dragRowOrCol
>= 0 )
2448 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2450 // don't capture the mouse yet
2451 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2454 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2456 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2462 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2465 wxPoint
pos( event
.GetPosition() );
2466 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2468 if ( event
.Dragging() )
2470 m_isDragging
= TRUE
;
2472 if ( event
.LeftIsDown() )
2474 switch( m_cursorMode
)
2476 case WXGRID_CURSOR_RESIZE_COL
:
2478 int cw
, ch
, dummy
, top
;
2479 m_gridWin
->GetClientSize( &cw
, &ch
);
2480 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2482 wxClientDC
dc( m_gridWin
);
2484 dc
.SetLogicalFunction(wxINVERT
);
2485 if ( m_dragLastPos
>= 0 )
2487 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2489 dc
.DrawLine( x
, top
, x
, top
+ch
);
2494 case WXGRID_CURSOR_SELECT_COL
:
2495 if ( (col
= XToCol( x
)) >= 0 &&
2496 !IsInSelection( 0, col
) )
2498 SelectCol( col
, TRUE
);
2501 // default label to suppress warnings about "enumeration value
2502 // 'xxx' not handled in switch
2510 m_isDragging
= FALSE
;
2513 // ------------ Entering or leaving the window
2515 if ( event
.Entering() || event
.Leaving() )
2517 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2521 // ------------ Left button pressed
2523 else if ( event
.LeftDown() )
2525 // don't send a label click event for a hit on the
2526 // edge of the col label - this is probably the user
2527 // wanting to resize the col
2529 if ( XToEdgeOfCol(x
) < 0 )
2533 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2535 SelectCol( col
, event
.ShiftDown() );
2536 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2541 // starting to drag-resize a col
2543 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2548 // ------------ Left double click
2550 if ( event
.LeftDClick() )
2552 if ( XToEdgeOfCol(x
) < 0 )
2555 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2560 // ------------ Left button released
2562 else if ( event
.LeftUp() )
2564 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2566 DoEndDragResizeCol();
2568 // Note: we are ending the event *after* doing
2569 // default processing in this case
2571 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2574 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2579 // ------------ Right button down
2581 else if ( event
.RightDown() )
2584 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2586 // no default action at the moment
2591 // ------------ Right double click
2593 else if ( event
.RightDClick() )
2596 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2598 // no default action at the moment
2603 // ------------ No buttons down and mouse moving
2605 else if ( event
.Moving() )
2607 m_dragRowOrCol
= XToEdgeOfCol( x
);
2608 if ( m_dragRowOrCol
>= 0 )
2610 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2612 // don't capture the cursor yet
2613 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2616 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2618 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2624 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2626 if ( event
.LeftDown() )
2628 // indicate corner label by having both row and
2631 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2637 else if ( event
.LeftDClick() )
2639 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2642 else if ( event
.RightDown() )
2644 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2646 // no default action at the moment
2650 else if ( event
.RightDClick() )
2652 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2654 // no default action at the moment
2659 void wxGrid::ChangeCursorMode(CursorMode mode
,
2664 static const wxChar
*cursorModes
[] =
2673 wxLogTrace(_T("grid"),
2674 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2675 win
== m_colLabelWin
? _T("colLabelWin")
2676 : win
? _T("rowLabelWin")
2678 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2679 #endif // __WXDEBUG__
2681 if ( mode
== m_cursorMode
)
2686 // by default use the grid itself
2692 m_winCapture
->ReleaseMouse();
2693 m_winCapture
= (wxWindow
*)NULL
;
2696 m_cursorMode
= mode
;
2698 switch ( m_cursorMode
)
2700 case WXGRID_CURSOR_RESIZE_ROW
:
2701 win
->SetCursor( m_rowResizeCursor
);
2704 case WXGRID_CURSOR_RESIZE_COL
:
2705 win
->SetCursor( m_colResizeCursor
);
2709 win
->SetCursor( *wxSTANDARD_CURSOR
);
2712 // we need to capture mouse when resizing
2713 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2714 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2716 if ( captureMouse
&& resize
)
2718 win
->CaptureMouse();
2723 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2726 wxPoint
pos( event
.GetPosition() );
2727 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2729 wxGridCellCoords coords
;
2730 XYToCell( x
, y
, coords
);
2732 if ( event
.Dragging() )
2734 m_isDragging
= TRUE
;
2735 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2737 // Hide the edit control, so it
2738 // won't interfer with drag-shrinking.
2739 if ( IsCellEditControlEnabled() )
2740 HideCellEditControl();
2741 if ( coords
!= wxGridNoCellCoords
)
2743 if ( !IsSelection() )
2745 SelectBlock( coords
, coords
);
2749 SelectBlock( m_currentCellCoords
, coords
);
2753 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2755 int cw
, ch
, left
, dummy
;
2756 m_gridWin
->GetClientSize( &cw
, &ch
);
2757 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2759 wxClientDC
dc( m_gridWin
);
2761 dc
.SetLogicalFunction(wxINVERT
);
2762 if ( m_dragLastPos
>= 0 )
2764 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2766 dc
.DrawLine( left
, y
, left
+cw
, y
);
2769 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2771 int cw
, ch
, dummy
, top
;
2772 m_gridWin
->GetClientSize( &cw
, &ch
);
2773 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2775 wxClientDC
dc( m_gridWin
);
2777 dc
.SetLogicalFunction(wxINVERT
);
2778 if ( m_dragLastPos
>= 0 )
2780 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2782 dc
.DrawLine( x
, top
, x
, top
+ch
);
2789 m_isDragging
= FALSE
;
2791 if ( coords
!= wxGridNoCellCoords
)
2793 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2794 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2797 if ( event
.Entering() || event
.Leaving() )
2799 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2800 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2805 // ------------ Left button pressed
2807 if ( event
.LeftDown() )
2809 if ( event
.ShiftDown() )
2811 SelectBlock( m_currentCellCoords
, coords
);
2813 else if ( XToEdgeOfCol(x
) < 0 &&
2814 YToEdgeOfRow(y
) < 0 )
2816 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2821 MakeCellVisible( coords
);
2822 SetCurrentCell( coords
);
2828 // ------------ Left double click
2830 else if ( event
.LeftDClick() )
2832 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2834 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2842 // ------------ Left button released
2844 else if ( event
.LeftUp() )
2846 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2848 if ( IsSelection() )
2850 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
2853 // Show the edit control, if it has
2854 // been hidden for drag-shrinking.
2855 if ( IsCellEditControlEnabled() )
2856 ShowCellEditControl();
2858 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2860 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2861 DoEndDragResizeRow();
2863 // Note: we are ending the event *after* doing
2864 // default processing in this case
2866 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2868 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2870 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2871 DoEndDragResizeCol();
2873 // Note: we are ending the event *after* doing
2874 // default processing in this case
2876 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2883 // ------------ Right button down
2885 else if ( event
.RightDown() )
2887 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2892 // no default action at the moment
2897 // ------------ Right double click
2899 else if ( event
.RightDClick() )
2901 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2906 // no default action at the moment
2910 // ------------ Moving and no button action
2912 else if ( event
.Moving() && !event
.IsButton() )
2914 int dragRow
= YToEdgeOfRow( y
);
2915 int dragCol
= XToEdgeOfCol( x
);
2917 // Dragging on the corner of a cell to resize in both
2918 // directions is not implemented yet...
2920 if ( dragRow
>= 0 && dragCol
>= 0 )
2922 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2928 m_dragRowOrCol
= dragRow
;
2930 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2932 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
2940 m_dragRowOrCol
= dragCol
;
2942 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2944 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
2950 // Neither on a row or col edge
2952 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2954 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2961 void wxGrid::DoEndDragResizeRow()
2963 if ( m_dragLastPos
>= 0 )
2965 // erase the last line and resize the row
2967 int cw
, ch
, left
, dummy
;
2968 m_gridWin
->GetClientSize( &cw
, &ch
);
2969 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2971 wxClientDC
dc( m_gridWin
);
2973 dc
.SetLogicalFunction( wxINVERT
);
2974 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2975 HideCellEditControl();
2977 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
2978 SetRowSize( m_dragRowOrCol
,
2979 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
2981 if ( !GetBatchCount() )
2983 // Only needed to get the correct rect.y:
2984 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
2986 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
2987 rect
.width
= m_rowLabelWidth
;
2988 rect
.height
= ch
- rect
.y
;
2989 m_rowLabelWin
->Refresh( TRUE
, &rect
);
2991 m_gridWin
->Refresh( FALSE
, &rect
);
2994 ShowCellEditControl();
2999 void wxGrid::DoEndDragResizeCol()
3001 if ( m_dragLastPos
>= 0 )
3003 // erase the last line and resize the col
3005 int cw
, ch
, dummy
, top
;
3006 m_gridWin
->GetClientSize( &cw
, &ch
);
3007 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3009 wxClientDC
dc( m_gridWin
);
3011 dc
.SetLogicalFunction( wxINVERT
);
3012 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3013 HideCellEditControl();
3015 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3016 SetColSize( m_dragRowOrCol
,
3017 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3019 if ( !GetBatchCount() )
3021 // Only needed to get the correct rect.x:
3022 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3024 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3025 rect
.width
= cw
- rect
.x
;
3026 rect
.height
= m_colLabelHeight
;
3027 m_colLabelWin
->Refresh( TRUE
, &rect
);
3029 m_gridWin
->Refresh( FALSE
, &rect
);
3032 ShowCellEditControl();
3039 // ------ interaction with data model
3041 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3043 switch ( msg
.GetId() )
3045 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3046 return GetModelValues();
3048 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3049 return SetModelValues();
3051 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3052 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3053 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3054 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3055 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3056 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3057 return Redimension( msg
);
3066 // The behaviour of this function depends on the grid table class
3067 // Clear() function. For the default wxGridStringTable class the
3068 // behavious is to replace all cell contents with wxEmptyString but
3069 // not to change the number of rows or cols.
3071 void wxGrid::ClearGrid()
3076 SetEditControlValue();
3077 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3082 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3084 // TODO: something with updateLabels flag
3088 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3094 bool ok
= m_table
->InsertRows( pos
, numRows
);
3096 // the table will have sent the results of the insert row
3097 // operation to this view object as a grid table message
3101 if ( m_numCols
== 0 )
3103 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3105 // TODO: perhaps instead of appending the default number of cols
3106 // we should remember what the last non-zero number of cols was ?
3110 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3112 // if we have just inserted cols into an empty grid the current
3113 // cell will be undefined...
3115 SetCurrentCell( 0, 0 );
3119 if ( !GetBatchCount() ) Refresh();
3122 SetEditControlValue();
3132 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3134 // TODO: something with updateLabels flag
3138 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3142 if ( m_table
&& m_table
->AppendRows( numRows
) )
3144 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3146 // if we have just inserted cols into an empty grid the current
3147 // cell will be undefined...
3149 SetCurrentCell( 0, 0 );
3152 // the table will have sent the results of the append row
3153 // operation to this view object as a grid table message
3156 if ( !GetBatchCount() ) Refresh();
3166 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3168 // TODO: something with updateLabels flag
3172 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3176 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
3178 // the table will have sent the results of the delete row
3179 // operation to this view object as a grid table message
3181 if ( m_numRows
> 0 )
3182 SetEditControlValue();
3184 HideCellEditControl();
3187 if ( !GetBatchCount() ) Refresh();
3197 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3199 // TODO: something with updateLabels flag
3203 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3209 HideCellEditControl();
3210 bool ok
= m_table
->InsertCols( pos
, numCols
);
3212 // the table will have sent the results of the insert col
3213 // operation to this view object as a grid table message
3217 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3219 // if we have just inserted cols into an empty grid the current
3220 // cell will be undefined...
3222 SetCurrentCell( 0, 0 );
3226 if ( !GetBatchCount() ) Refresh();
3229 SetEditControlValue();
3239 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3241 // TODO: something with updateLabels flag
3245 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3249 if ( m_table
&& m_table
->AppendCols( numCols
) )
3251 // the table will have sent the results of the append col
3252 // operation to this view object as a grid table message
3254 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3256 // if we have just inserted cols into an empty grid the current
3257 // cell will be undefined...
3259 SetCurrentCell( 0, 0 );
3263 if ( !GetBatchCount() ) Refresh();
3273 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3275 // TODO: something with updateLabels flag
3279 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3283 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
3285 // the table will have sent the results of the delete col
3286 // operation to this view object as a grid table message
3288 if ( m_numCols
> 0 )
3289 SetEditControlValue();
3291 HideCellEditControl();
3294 if ( !GetBatchCount() ) Refresh();
3306 // ----- event handlers
3309 // Generate a grid event based on a mouse event and
3310 // return the result of ProcessEvent()
3312 bool wxGrid::SendEvent( const wxEventType type
,
3314 wxMouseEvent
& mouseEv
)
3316 if ( type
== EVT_GRID_ROW_SIZE
||
3317 type
== EVT_GRID_COL_SIZE
)
3319 int rowOrCol
= (row
== -1 ? col
: row
);
3321 wxGridSizeEvent
gridEvt( GetId(),
3325 mouseEv
.GetX(), mouseEv
.GetY(),
3326 mouseEv
.ControlDown(),
3327 mouseEv
.ShiftDown(),
3329 mouseEv
.MetaDown() );
3331 return GetEventHandler()->ProcessEvent(gridEvt
);
3333 else if ( type
== EVT_GRID_RANGE_SELECT
)
3335 wxGridRangeSelectEvent
gridEvt( GetId(),
3339 m_selectedBottomRight
,
3340 mouseEv
.ControlDown(),
3341 mouseEv
.ShiftDown(),
3343 mouseEv
.MetaDown() );
3345 return GetEventHandler()->ProcessEvent(gridEvt
);
3349 wxGridEvent
gridEvt( GetId(),
3353 mouseEv
.GetX(), mouseEv
.GetY(),
3354 mouseEv
.ControlDown(),
3355 mouseEv
.ShiftDown(),
3357 mouseEv
.MetaDown() );
3359 return GetEventHandler()->ProcessEvent(gridEvt
);
3364 // Generate a grid event of specified type and return the result
3365 // of ProcessEvent().
3367 bool wxGrid::SendEvent( const wxEventType type
,
3370 if ( type
== EVT_GRID_ROW_SIZE
||
3371 type
== EVT_GRID_COL_SIZE
)
3373 int rowOrCol
= (row
== -1 ? col
: row
);
3375 wxGridSizeEvent
gridEvt( GetId(),
3380 return GetEventHandler()->ProcessEvent(gridEvt
);
3384 wxGridEvent
gridEvt( GetId(),
3389 return GetEventHandler()->ProcessEvent(gridEvt
);
3394 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3396 wxPaintDC
dc( this );
3398 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3399 m_numRows
&& m_numCols
)
3401 m_currentCellCoords
.Set(0, 0);
3402 SetEditControlValue();
3403 ShowCellEditControl();
3410 // This is just here to make sure that CalcDimensions gets called when
3411 // the grid view is resized... then the size event is skipped to allow
3412 // the box sizers to handle everything
3414 void wxGrid::OnSize( wxSizeEvent
& event
)
3421 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3423 if ( m_inOnKeyDown
)
3425 // shouldn't be here - we are going round in circles...
3427 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while alread active") );
3430 m_inOnKeyDown
= TRUE
;
3432 // propagate the event up and see if it gets processed
3434 wxWindow
*parent
= GetParent();
3435 wxKeyEvent
keyEvt( event
);
3436 keyEvt
.SetEventObject( parent
);
3438 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3440 // try local handlers
3442 switch ( event
.KeyCode() )
3445 if ( event
.ControlDown() )
3447 MoveCursorUpBlock();
3456 if ( event
.ControlDown() )
3458 MoveCursorDownBlock();
3467 if ( event
.ControlDown() )
3469 MoveCursorLeftBlock();
3478 if ( event
.ControlDown() )
3480 MoveCursorRightBlock();
3489 if ( !IsEditable() )
3500 if ( event
.ControlDown() )
3502 event
.Skip(); // to let the edit control have the return
3511 if ( event
.ControlDown() )
3513 MakeCellVisible( 0, 0 );
3514 SetCurrentCell( 0, 0 );
3523 if ( event
.ControlDown() )
3525 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3526 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3543 // now try the cell edit control
3545 if ( IsCellEditControlEnabled() )
3547 event
.SetEventObject( m_cellEditCtrl
);
3548 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
3554 m_inOnKeyDown
= FALSE
;
3557 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3560 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3562 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3564 // the event has been intercepted - do nothing
3569 m_currentCellCoords
!= wxGridNoCellCoords
)
3571 HideCellEditControl();
3572 SaveEditControlValue();
3575 m_currentCellCoords
= coords
;
3577 SetEditControlValue();
3581 ShowCellEditControl();
3583 if ( IsSelection() )
3585 wxRect
r( SelectionToDeviceRect() );
3587 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3594 // ------ functions to get/send data (see also public functions)
3597 bool wxGrid::GetModelValues()
3601 // all we need to do is repaint the grid
3603 m_gridWin
->Refresh();
3611 bool wxGrid::SetModelValues()
3617 for ( row
= 0; row
< m_numRows
; row
++ )
3619 for ( col
= 0; col
< m_numCols
; col
++ )
3621 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3633 // Note - this function only draws cells that are in the list of
3634 // exposed cells (usually set from the update region by
3635 // CalcExposedCells)
3637 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3639 if ( !m_numRows
|| !m_numCols
) return;
3642 size_t numCells
= m_cellsExposed
.GetCount();
3644 for ( i
= 0; i
< numCells
; i
++ )
3646 DrawCell( dc
, m_cellsExposed
[i
] );
3651 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3653 int row
= coords
.GetRow();
3654 int col
= coords
.GetCol();
3656 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3659 // we draw the cell border ourselves
3660 #if !WXGRID_DRAW_LINES
3661 if ( m_gridLinesEnabled
)
3662 DrawCellBorder( dc
, coords
);
3665 // but all the rest is drawn by the cell renderer and hence may be
3668 rect
.x
= m_colRights
[col
] - m_colWidths
[col
] + 1;
3669 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
] + 1;
3670 rect
.width
= m_colWidths
[col
] - 1;
3671 rect
.height
= m_rowHeights
[row
] - 1;
3673 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
3674 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
3678 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3680 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3681 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3683 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3684 int row
= coords
.GetRow();
3685 int col
= coords
.GetCol();
3687 // right hand border
3689 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3690 m_colRights
[col
], m_rowBottoms
[row
] );
3694 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3695 m_colRights
[col
], m_rowBottoms
[row
] );
3699 // TODO: remove this ???
3700 // This is used to redraw all grid lines e.g. when the grid line colour
3703 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3705 if ( !m_gridLinesEnabled
||
3707 !m_numCols
) return;
3709 int top
, bottom
, left
, right
;
3713 m_gridWin
->GetClientSize(&cw
, &ch
);
3715 // virtual coords of visible area
3717 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3718 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3722 reg
.GetBox(x
, y
, w
, h
);
3723 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3724 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3727 // avoid drawing grid lines past the last row and col
3729 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3730 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3732 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3734 // horizontal grid lines
3737 for ( i
= 0; i
< m_numRows
; i
++ )
3739 if ( m_rowBottoms
[i
] > bottom
)
3743 else if ( m_rowBottoms
[i
] >= top
)
3745 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
3750 // vertical grid lines
3752 for ( i
= 0; i
< m_numCols
; i
++ )
3754 if ( m_colRights
[i
] > right
)
3758 else if ( m_colRights
[i
] >= left
)
3760 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
3766 void wxGrid::DrawRowLabels( wxDC
& dc
)
3768 if ( !m_numRows
|| !m_numCols
) return;
3771 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3773 for ( i
= 0; i
< numLabels
; i
++ )
3775 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3780 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
3782 if ( m_rowHeights
[row
] <= 0 ) return;
3784 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3786 dc
.SetPen( *wxBLACK_PEN
);
3787 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3788 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3790 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3791 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3793 dc
.SetPen( *wxWHITE_PEN
);
3794 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
3795 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
3797 dc
.SetBackgroundMode( wxTRANSPARENT
);
3798 dc
.SetTextForeground( GetLabelTextColour() );
3799 dc
.SetFont( GetLabelFont() );
3802 GetRowLabelAlignment( &hAlign
, &vAlign
);
3806 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3807 rect
.SetWidth( m_rowLabelWidth
- 4 );
3808 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3809 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
3813 void wxGrid::DrawColLabels( wxDC
& dc
)
3815 if ( !m_numRows
|| !m_numCols
) return;
3818 size_t numLabels
= m_colLabelsExposed
.GetCount();
3820 for ( i
= 0; i
< numLabels
; i
++ )
3822 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
3827 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
3829 if ( m_colWidths
[col
] <= 0 ) return;
3831 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
3833 dc
.SetPen( *wxBLACK_PEN
);
3834 dc
.DrawLine( m_colRights
[col
]-1, 0,
3835 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3837 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
3838 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3840 dc
.SetPen( *wxWHITE_PEN
);
3841 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
3842 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
3844 dc
.SetBackgroundMode( wxTRANSPARENT
);
3845 dc
.SetTextForeground( GetLabelTextColour() );
3846 dc
.SetFont( GetLabelFont() );
3848 dc
.SetBackgroundMode( wxTRANSPARENT
);
3849 dc
.SetTextForeground( GetLabelTextColour() );
3850 dc
.SetFont( GetLabelFont() );
3853 GetColLabelAlignment( &hAlign
, &vAlign
);
3856 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3858 rect
.SetWidth( m_colWidths
[col
] - 4 );
3859 rect
.SetHeight( m_colLabelHeight
- 4 );
3860 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
3864 void wxGrid::DrawTextRectangle( wxDC
& dc
,
3865 const wxString
& value
,
3870 long textWidth
, textHeight
;
3871 long lineWidth
, lineHeight
;
3872 wxArrayString lines
;
3874 dc
.SetClippingRegion( rect
);
3875 StringToLines( value
, lines
);
3876 if ( lines
.GetCount() )
3878 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
3879 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
3882 switch ( horizAlign
)
3885 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
3889 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
3898 switch ( vertAlign
)
3901 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
3905 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
3914 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
3916 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
3921 dc
.DestroyClippingRegion();
3925 // Split multi line text up into an array of strings. Any existing
3926 // contents of the string array are preserved.
3928 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
3932 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
3933 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
3935 while ( startPos
< (int)tVal
.Length() )
3937 pos
= tVal
.Mid(startPos
).Find( eol
);
3942 else if ( pos
== 0 )
3944 lines
.Add( wxEmptyString
);
3948 lines
.Add( value
.Mid(startPos
, pos
) );
3952 if ( startPos
< (int)value
.Length() )
3954 lines
.Add( value
.Mid( startPos
) );
3959 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3960 wxArrayString
& lines
,
3961 long *width
, long *height
)
3968 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3970 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3971 w
= wxMax( w
, lineW
);
3981 // ------ Edit control functions
3985 void wxGrid::EnableEditing( bool edit
)
3987 // TODO: improve this ?
3989 if ( edit
!= m_editable
)
3993 // TODO: extend this for other edit control types
3995 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3997 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
4003 #if 0 // disabled for the moment - the cell control is always active
4004 void wxGrid::EnableCellEditControl( bool enable
)
4006 if ( m_cellEditCtrl
&&
4007 enable
!= m_cellEditCtrlEnabled
)
4009 m_cellEditCtrlEnabled
= enable
;
4011 if ( m_cellEditCtrlEnabled
)
4013 SetEditControlValue();
4014 ShowCellEditControl();
4018 HideCellEditControl();
4019 SaveEditControlValue();
4026 void wxGrid::ShowCellEditControl()
4030 if ( IsCellEditControlEnabled() )
4032 if ( !IsVisible( m_currentCellCoords
) )
4038 rect
= CellToRect( m_currentCellCoords
);
4040 // convert to scrolled coords
4042 int left
, top
, right
, bottom
;
4043 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4044 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4047 m_gridWin
->GetClientSize( &cw
, &ch
);
4049 // Make the edit control large enough to allow for internal margins
4050 // TODO: remove this if the text ctrl sizing is improved esp. for unix
4053 #if defined(__WXMOTIF__)
4054 if ( m_currentCellCoords
.GetRow() == 0 ||
4055 m_currentCellCoords
.GetCol() == 0 )
4064 if ( m_currentCellCoords
.GetRow() == 0 ||
4065 m_currentCellCoords
.GetCol() == 0 )
4075 #if defined(__WXGTK__)
4078 if (left
!= 0) left_diff
++;
4079 if (top
!= 0) top_diff
++;
4080 rect
.SetLeft( left
+ left_diff
);
4081 rect
.SetTop( top
+ top_diff
);
4082 rect
.SetRight( rect
.GetRight() - left_diff
);
4083 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4085 rect
.SetLeft( wxMax(0, left
- extra
) );
4086 rect
.SetTop( wxMax(0, top
- extra
) );
4087 rect
.SetRight( rect
.GetRight() + 2*extra
);
4088 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4091 m_cellEditCtrl
->SetSize( rect
);
4092 m_cellEditCtrl
->Show( TRUE
);
4094 switch ( m_editCtrlType
)
4096 case wxGRID_TEXTCTRL
:
4097 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
4100 case wxGRID_CHECKBOX
:
4101 // TODO: anything ???
4106 // TODO: anything ???
4110 case wxGRID_COMBOBOX
:
4111 // TODO: anything ???
4116 m_cellEditCtrl
->SetFocus();
4122 void wxGrid::HideCellEditControl()
4124 if ( IsCellEditControlEnabled() )
4126 m_cellEditCtrl
->Show( FALSE
);
4131 void wxGrid::SetEditControlValue( const wxString
& value
)
4137 s
= GetCellValue(m_currentCellCoords
);
4141 if ( IsCellEditControlEnabled() )
4143 switch ( m_editCtrlType
)
4145 case wxGRID_TEXTCTRL
:
4146 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
4149 case wxGRID_CHECKBOX
:
4150 // TODO: implement this
4155 // TODO: implement this
4159 case wxGRID_COMBOBOX
:
4160 // TODO: implement this
4169 void wxGrid::SaveEditControlValue()
4173 wxWindow
*ctrl
= (wxWindow
*)NULL
;
4175 if ( IsCellEditControlEnabled() )
4177 ctrl
= m_cellEditCtrl
;
4184 bool valueChanged
= FALSE
;
4186 switch ( m_editCtrlType
)
4188 case wxGRID_TEXTCTRL
:
4189 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
4190 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
4191 SetCellValue( m_currentCellCoords
,
4192 ((wxTextCtrl
*) ctrl
)->GetValue() );
4195 case wxGRID_CHECKBOX
:
4196 // TODO: implement this
4201 // TODO: implement this
4205 case wxGRID_COMBOBOX
:
4206 // TODO: implement this
4213 SendEvent( EVT_GRID_CELL_CHANGE
,
4214 m_currentCellCoords
.GetRow(),
4215 m_currentCellCoords
.GetCol() );
4222 // ------ Grid location functions
4223 // Note that all of these functions work with the logical coordinates of
4224 // grid cells and labels so you will need to convert from device
4225 // coordinates for mouse events etc.
4228 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4230 int row
= YToRow(y
);
4231 int col
= XToCol(x
);
4233 if ( row
== -1 || col
== -1 )
4235 coords
= wxGridNoCellCoords
;
4239 coords
.Set( row
, col
);
4244 int wxGrid::YToRow( int y
)
4248 for ( i
= 0; i
< m_numRows
; i
++ )
4250 if ( y
< m_rowBottoms
[i
] ) return i
;
4257 int wxGrid::XToCol( int x
)
4261 for ( i
= 0; i
< m_numCols
; i
++ )
4263 if ( x
< m_colRights
[i
] ) return i
;
4270 // return the row number that that the y coord is near the edge of, or
4271 // -1 if not near an edge
4273 int wxGrid::YToEdgeOfRow( int y
)
4277 for ( i
= 0; i
< m_numRows
; i
++ )
4279 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4281 d
= abs( y
- m_rowBottoms
[i
] );
4283 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4292 // return the col number that that the x coord is near the edge of, or
4293 // -1 if not near an edge
4295 int wxGrid::XToEdgeOfCol( int x
)
4299 for ( i
= 0; i
< m_numCols
; i
++ )
4301 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4303 d
= abs( x
- m_colRights
[i
] );
4305 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4314 wxRect
wxGrid::CellToRect( int row
, int col
)
4316 wxRect
rect( -1, -1, -1, -1 );
4318 if ( row
>= 0 && row
< m_numRows
&&
4319 col
>= 0 && col
< m_numCols
)
4321 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4322 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4323 rect
.width
= m_colWidths
[col
];
4324 rect
.height
= m_rowHeights
[ row
];
4331 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4333 // get the cell rectangle in logical coords
4335 wxRect
r( CellToRect( row
, col
) );
4337 // convert to device coords
4339 int left
, top
, right
, bottom
;
4340 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4341 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4343 // check against the client area of the grid window
4346 m_gridWin
->GetClientSize( &cw
, &ch
);
4348 if ( wholeCellVisible
)
4350 // is the cell wholly visible ?
4352 return ( left
>= 0 && right
<= cw
&&
4353 top
>= 0 && bottom
<= ch
);
4357 // is the cell partly visible ?
4359 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4360 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4365 // make the specified cell location visible by doing a minimal amount
4368 void wxGrid::MakeCellVisible( int row
, int col
)
4371 int xpos
= -1, ypos
= -1;
4373 if ( row
>= 0 && row
< m_numRows
&&
4374 col
>= 0 && col
< m_numCols
)
4376 // get the cell rectangle in logical coords
4378 wxRect
r( CellToRect( row
, col
) );
4380 // convert to device coords
4382 int left
, top
, right
, bottom
;
4383 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4384 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4387 m_gridWin
->GetClientSize( &cw
, &ch
);
4393 else if ( bottom
> ch
)
4395 int h
= r
.GetHeight();
4397 for ( i
= row
-1; i
>= 0; i
-- )
4399 if ( h
+ m_rowHeights
[i
] > ch
) break;
4401 h
+= m_rowHeights
[i
];
4402 ypos
-= m_rowHeights
[i
];
4405 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4406 // have rounding errors (this is important, because if we do, we
4407 // might not scroll at all and some cells won't be redrawn)
4408 ypos
+= GRID_SCROLL_LINE
/ 2;
4415 else if ( right
> cw
)
4417 int w
= r
.GetWidth();
4419 for ( i
= col
-1; i
>= 0; i
-- )
4421 if ( w
+ m_colWidths
[i
] > cw
) break;
4423 w
+= m_colWidths
[i
];
4424 xpos
-= m_colWidths
[i
];
4427 // see comment for ypos above
4428 xpos
+= GRID_SCROLL_LINE
/ 2;
4431 if ( xpos
!= -1 || ypos
!= -1 )
4433 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4434 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4435 Scroll( xpos
, ypos
);
4443 // ------ Grid cursor movement functions
4446 bool wxGrid::MoveCursorUp()
4448 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4449 m_currentCellCoords
.GetRow() > 0 )
4451 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4452 m_currentCellCoords
.GetCol() );
4454 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4455 m_currentCellCoords
.GetCol() );
4464 bool wxGrid::MoveCursorDown()
4466 // TODO: allow for scrolling
4468 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4469 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4471 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4472 m_currentCellCoords
.GetCol() );
4474 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4475 m_currentCellCoords
.GetCol() );
4484 bool wxGrid::MoveCursorLeft()
4486 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4487 m_currentCellCoords
.GetCol() > 0 )
4489 MakeCellVisible( m_currentCellCoords
.GetRow(),
4490 m_currentCellCoords
.GetCol() - 1 );
4492 SetCurrentCell( m_currentCellCoords
.GetRow(),
4493 m_currentCellCoords
.GetCol() - 1 );
4502 bool wxGrid::MoveCursorRight()
4504 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4505 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4507 MakeCellVisible( m_currentCellCoords
.GetRow(),
4508 m_currentCellCoords
.GetCol() + 1 );
4510 SetCurrentCell( m_currentCellCoords
.GetRow(),
4511 m_currentCellCoords
.GetCol() + 1 );
4520 bool wxGrid::MovePageUp()
4522 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4524 int row
= m_currentCellCoords
.GetRow();
4528 m_gridWin
->GetClientSize( &cw
, &ch
);
4530 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4531 int newRow
= YToRow( y
- ch
+ 1 );
4536 else if ( newRow
== row
)
4541 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4542 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4550 bool wxGrid::MovePageDown()
4552 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4554 int row
= m_currentCellCoords
.GetRow();
4555 if ( row
< m_numRows
)
4558 m_gridWin
->GetClientSize( &cw
, &ch
);
4560 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4561 int newRow
= YToRow( y
+ ch
);
4564 newRow
= m_numRows
- 1;
4566 else if ( newRow
== row
)
4571 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4572 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4580 bool wxGrid::MoveCursorUpBlock()
4583 m_currentCellCoords
!= wxGridNoCellCoords
&&
4584 m_currentCellCoords
.GetRow() > 0 )
4586 int row
= m_currentCellCoords
.GetRow();
4587 int col
= m_currentCellCoords
.GetCol();
4589 if ( m_table
->IsEmptyCell(row
, col
) )
4591 // starting in an empty cell: find the next block of
4597 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4600 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4602 // starting at the top of a block: find the next block
4608 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4613 // starting within a block: find the top of the block
4618 if ( m_table
->IsEmptyCell(row
, col
) )
4626 MakeCellVisible( row
, col
);
4627 SetCurrentCell( row
, col
);
4635 bool wxGrid::MoveCursorDownBlock()
4638 m_currentCellCoords
!= wxGridNoCellCoords
&&
4639 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4641 int row
= m_currentCellCoords
.GetRow();
4642 int col
= m_currentCellCoords
.GetCol();
4644 if ( m_table
->IsEmptyCell(row
, col
) )
4646 // starting in an empty cell: find the next block of
4649 while ( row
< m_numRows
-1 )
4652 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4655 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4657 // starting at the bottom of a block: find the next block
4660 while ( row
< m_numRows
-1 )
4663 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4668 // starting within a block: find the bottom of the block
4670 while ( row
< m_numRows
-1 )
4673 if ( m_table
->IsEmptyCell(row
, col
) )
4681 MakeCellVisible( row
, col
);
4682 SetCurrentCell( row
, col
);
4690 bool wxGrid::MoveCursorLeftBlock()
4693 m_currentCellCoords
!= wxGridNoCellCoords
&&
4694 m_currentCellCoords
.GetCol() > 0 )
4696 int row
= m_currentCellCoords
.GetRow();
4697 int col
= m_currentCellCoords
.GetCol();
4699 if ( m_table
->IsEmptyCell(row
, col
) )
4701 // starting in an empty cell: find the next block of
4707 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4710 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4712 // starting at the left of a block: find the next block
4718 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4723 // starting within a block: find the left of the block
4728 if ( m_table
->IsEmptyCell(row
, col
) )
4736 MakeCellVisible( row
, col
);
4737 SetCurrentCell( row
, col
);
4745 bool wxGrid::MoveCursorRightBlock()
4748 m_currentCellCoords
!= wxGridNoCellCoords
&&
4749 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4751 int row
= m_currentCellCoords
.GetRow();
4752 int col
= m_currentCellCoords
.GetCol();
4754 if ( m_table
->IsEmptyCell(row
, col
) )
4756 // starting in an empty cell: find the next block of
4759 while ( col
< m_numCols
-1 )
4762 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4765 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4767 // starting at the right of a block: find the next block
4770 while ( col
< m_numCols
-1 )
4773 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4778 // starting within a block: find the right of the block
4780 while ( col
< m_numCols
-1 )
4783 if ( m_table
->IsEmptyCell(row
, col
) )
4791 MakeCellVisible( row
, col
);
4792 SetCurrentCell( row
, col
);
4803 // ------ Label values and formatting
4806 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4808 *horiz
= m_rowLabelHorizAlign
;
4809 *vert
= m_rowLabelVertAlign
;
4812 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4814 *horiz
= m_colLabelHorizAlign
;
4815 *vert
= m_colLabelVertAlign
;
4818 wxString
wxGrid::GetRowLabelValue( int row
)
4822 return m_table
->GetRowLabelValue( row
);
4832 wxString
wxGrid::GetColLabelValue( int col
)
4836 return m_table
->GetColLabelValue( col
);
4847 void wxGrid::SetRowLabelSize( int width
)
4849 width
= wxMax( width
, 0 );
4850 if ( width
!= m_rowLabelWidth
)
4854 m_rowLabelWin
->Show( FALSE
);
4855 m_cornerLabelWin
->Show( FALSE
);
4857 else if ( m_rowLabelWidth
== 0 )
4859 m_rowLabelWin
->Show( TRUE
);
4860 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4863 m_rowLabelWidth
= width
;
4870 void wxGrid::SetColLabelSize( int height
)
4872 height
= wxMax( height
, 0 );
4873 if ( height
!= m_colLabelHeight
)
4877 m_colLabelWin
->Show( FALSE
);
4878 m_cornerLabelWin
->Show( FALSE
);
4880 else if ( m_colLabelHeight
== 0 )
4882 m_colLabelWin
->Show( TRUE
);
4883 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4886 m_colLabelHeight
= height
;
4893 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
4895 if ( m_labelBackgroundColour
!= colour
)
4897 m_labelBackgroundColour
= colour
;
4898 m_rowLabelWin
->SetBackgroundColour( colour
);
4899 m_colLabelWin
->SetBackgroundColour( colour
);
4900 m_cornerLabelWin
->SetBackgroundColour( colour
);
4902 if ( !GetBatchCount() )
4904 m_rowLabelWin
->Refresh();
4905 m_colLabelWin
->Refresh();
4906 m_cornerLabelWin
->Refresh();
4911 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
4913 if ( m_labelTextColour
!= colour
)
4915 m_labelTextColour
= colour
;
4916 if ( !GetBatchCount() )
4918 m_rowLabelWin
->Refresh();
4919 m_colLabelWin
->Refresh();
4924 void wxGrid::SetLabelFont( const wxFont
& font
)
4927 if ( !GetBatchCount() )
4929 m_rowLabelWin
->Refresh();
4930 m_colLabelWin
->Refresh();
4934 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
4936 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4938 m_rowLabelHorizAlign
= horiz
;
4941 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4943 m_rowLabelVertAlign
= vert
;
4946 if ( !GetBatchCount() )
4948 m_rowLabelWin
->Refresh();
4952 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
4954 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4956 m_colLabelHorizAlign
= horiz
;
4959 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4961 m_colLabelVertAlign
= vert
;
4964 if ( !GetBatchCount() )
4966 m_colLabelWin
->Refresh();
4970 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
4974 m_table
->SetRowLabelValue( row
, s
);
4975 if ( !GetBatchCount() )
4977 wxRect rect
= CellToRect( row
, 0);
4978 if ( rect
.height
> 0 )
4980 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
4982 rect
.width
= m_rowLabelWidth
;
4983 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4989 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
4993 m_table
->SetColLabelValue( col
, s
);
4994 if ( !GetBatchCount() )
4996 wxRect rect
= CellToRect( 0, col
);
4997 if ( rect
.width
> 0 )
4999 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5001 rect
.height
= m_colLabelHeight
;
5002 m_colLabelWin
->Refresh( TRUE
, &rect
);
5008 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5010 if ( m_gridLineColour
!= colour
)
5012 m_gridLineColour
= colour
;
5014 wxClientDC
dc( m_gridWin
);
5016 DrawAllGridLines( dc
, wxRegion() );
5020 void wxGrid::EnableGridLines( bool enable
)
5022 if ( enable
!= m_gridLinesEnabled
)
5024 m_gridLinesEnabled
= enable
;
5026 if ( !GetBatchCount() )
5030 wxClientDC
dc( m_gridWin
);
5032 DrawAllGridLines( dc
, wxRegion() );
5036 m_gridWin
->Refresh();
5043 int wxGrid::GetDefaultRowSize()
5045 return m_defaultRowHeight
;
5048 int wxGrid::GetRowSize( int row
)
5050 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5052 return m_rowHeights
[row
];
5055 int wxGrid::GetDefaultColSize()
5057 return m_defaultColWidth
;
5060 int wxGrid::GetColSize( int col
)
5062 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5064 return m_colWidths
[col
];
5067 // ============================================================================
5068 // access to the grid attributes: each of them has a default value in the grid
5069 // itself and may be overidden on a per-cell basis
5070 // ============================================================================
5072 // ----------------------------------------------------------------------------
5073 // setting default attributes
5074 // ----------------------------------------------------------------------------
5076 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5078 m_defaultCellAttr
->SetBackgroundColour(col
);
5081 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5083 m_defaultCellAttr
->SetTextColour(col
);
5086 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5088 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5091 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5093 m_defaultCellAttr
->SetFont(font
);
5096 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5098 m_defaultCellAttr
->SetRenderer(renderer
);
5101 // ----------------------------------------------------------------------------
5102 // access to the default attrbiutes
5103 // ----------------------------------------------------------------------------
5105 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5107 return m_defaultCellAttr
->GetBackgroundColour();
5110 wxColour
wxGrid::GetDefaultCellTextColour()
5112 return m_defaultCellAttr
->GetTextColour();
5115 wxFont
wxGrid::GetDefaultCellFont()
5117 return m_defaultCellAttr
->GetFont();
5120 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5122 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5125 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5127 return m_defaultCellAttr
->GetRenderer();
5130 // ----------------------------------------------------------------------------
5131 // access to cell attributes
5132 // ----------------------------------------------------------------------------
5134 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5136 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5137 wxColour colour
= attr
->GetBackgroundColour();
5142 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5144 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5145 wxColour colour
= attr
->GetTextColour();
5150 wxFont
wxGrid::GetCellFont( int row
, int col
)
5152 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5153 wxFont font
= attr
->GetFont();
5158 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5160 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5161 attr
->GetAlignment(horiz
, vert
);
5165 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5167 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5168 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5173 // ----------------------------------------------------------------------------
5174 // attribute support: cache, automatic provider creation, ...
5175 // ----------------------------------------------------------------------------
5177 bool wxGrid::CanHaveAttributes()
5184 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5185 // table is providing the attributes itself??? In which case
5186 // I don't think the grid should create a Provider object for the
5187 // table but the table should be smart enough to do that on its own.
5188 if ( !m_table
->GetAttrProvider() )
5190 // use the default attr provider by default
5191 // (another choice would be to just return FALSE thus forcing the user
5193 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5199 void wxGrid::ClearAttrCache()
5201 if ( m_attrCache
.row
!= -1 )
5203 m_attrCache
.attr
->SafeDecRef();
5204 m_attrCache
.row
= -1;
5208 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5210 wxGrid
*self
= (wxGrid
*)this; // const_cast
5212 self
->ClearAttrCache();
5213 self
->m_attrCache
.row
= row
;
5214 self
->m_attrCache
.col
= col
;
5215 self
->m_attrCache
.attr
= attr
;
5219 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5221 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5223 *attr
= m_attrCache
.attr
;
5224 (*attr
)->SafeIncRef();
5226 #ifdef DEBUG_ATTR_CACHE
5227 gs_nAttrCacheHits
++;
5234 #ifdef DEBUG_ATTR_CACHE
5235 gs_nAttrCacheMisses
++;
5241 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5243 wxGridCellAttr
*attr
;
5244 if ( !LookupAttr(row
, col
, &attr
) )
5246 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5247 CacheAttr(row
, col
, attr
);
5250 attr
->SetDefAttr(m_defaultCellAttr
);
5252 attr
= m_defaultCellAttr
;
5259 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5261 wxGridCellAttr
*attr
;
5262 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5264 wxASSERT_MSG( m_table
,
5265 _T("we may only be called if CanHaveAttributes() "
5266 "returned TRUE and then m_table should be !NULL") );
5268 attr
= m_table
->GetAttr(row
, col
);
5271 attr
= new wxGridCellAttr
;
5273 // artificially inc the ref count to match DecRef() in caller
5276 m_table
->SetAttr(attr
, row
, col
);
5279 CacheAttr(row
, col
, attr
);
5281 attr
->SetDefAttr(m_defaultCellAttr
);
5285 // ----------------------------------------------------------------------------
5286 // setting cell attributes: this is forwarded to the table
5287 // ----------------------------------------------------------------------------
5289 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5291 if ( CanHaveAttributes() )
5293 m_table
->SetRowAttr(attr
, row
);
5301 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5303 if ( CanHaveAttributes() )
5305 m_table
->SetColAttr(attr
, col
);
5313 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5315 if ( CanHaveAttributes() )
5317 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5318 attr
->SetBackgroundColour(colour
);
5323 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5325 if ( CanHaveAttributes() )
5327 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5328 attr
->SetTextColour(colour
);
5333 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5335 if ( CanHaveAttributes() )
5337 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5338 attr
->SetFont(font
);
5343 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5345 if ( CanHaveAttributes() )
5347 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5348 attr
->SetAlignment(horiz
, vert
);
5353 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5355 if ( CanHaveAttributes() )
5357 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5358 attr
->SetRenderer(renderer
);
5363 // ----------------------------------------------------------------------------
5365 // ----------------------------------------------------------------------------
5367 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5369 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5371 if ( resizeExistingRows
)
5375 for ( row
= 0; row
< m_numRows
; row
++ )
5377 m_rowHeights
[row
] = m_defaultRowHeight
;
5378 bottom
+= m_defaultRowHeight
;
5379 m_rowBottoms
[row
] = bottom
;
5385 void wxGrid::SetRowSize( int row
, int height
)
5387 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5391 int h
= wxMax( 0, height
);
5392 int diff
= h
- m_rowHeights
[row
];
5394 m_rowHeights
[row
] = h
;
5395 for ( i
= row
; i
< m_numRows
; i
++ )
5397 m_rowBottoms
[i
] += diff
;
5402 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5404 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5406 if ( resizeExistingCols
)
5410 for ( col
= 0; col
< m_numCols
; col
++ )
5412 m_colWidths
[col
] = m_defaultColWidth
;
5413 right
+= m_defaultColWidth
;
5414 m_colRights
[col
] = right
;
5420 void wxGrid::SetColSize( int col
, int width
)
5422 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5426 int w
= wxMax( 0, width
);
5427 int diff
= w
- m_colWidths
[col
];
5428 m_colWidths
[col
] = w
;
5430 for ( i
= col
; i
< m_numCols
; i
++ )
5432 m_colRights
[i
] += diff
;
5439 // ------ cell value accessor functions
5442 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5446 m_table
->SetValue( row
, col
, s
.c_str() );
5447 if ( !GetBatchCount() )
5449 wxClientDC
dc( m_gridWin
);
5451 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5454 #if 0 // TODO: edit in place
5456 if ( m_currentCellCoords
.GetRow() == row
&&
5457 m_currentCellCoords
.GetCol() == col
)
5459 SetEditControlValue( s
);
5468 // ------ Block, row and col selection
5471 void wxGrid::SelectRow( int row
, bool addToSelected
)
5475 if ( IsSelection() && addToSelected
)
5478 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5481 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5482 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5483 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5484 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5488 need_refresh
[0] = TRUE
;
5489 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5490 wxGridCellCoords ( oldTop
- 1,
5492 m_selectedTopLeft
.SetRow( row
);
5497 need_refresh
[1] = TRUE
;
5498 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5499 wxGridCellCoords ( oldBottom
,
5502 m_selectedTopLeft
.SetCol( 0 );
5505 if ( oldBottom
< row
)
5507 need_refresh
[2] = TRUE
;
5508 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5509 wxGridCellCoords ( row
,
5511 m_selectedBottomRight
.SetRow( row
);
5514 if ( oldRight
< m_numCols
- 1 )
5516 need_refresh
[3] = TRUE
;
5517 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5519 wxGridCellCoords ( oldBottom
,
5521 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5524 for (i
= 0; i
< 4; i
++ )
5525 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5526 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5530 r
= SelectionToDeviceRect();
5532 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5534 m_selectedTopLeft
.Set( row
, 0 );
5535 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5536 r
= SelectionToDeviceRect();
5537 m_gridWin
->Refresh( FALSE
, &r
);
5540 wxGridRangeSelectEvent
gridEvt( GetId(),
5541 EVT_GRID_RANGE_SELECT
,
5544 m_selectedBottomRight
);
5546 GetEventHandler()->ProcessEvent(gridEvt
);
5550 void wxGrid::SelectCol( int col
, bool addToSelected
)
5552 if ( IsSelection() && addToSelected
)
5555 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5558 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5559 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5560 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5561 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5563 if ( oldLeft
> col
)
5565 need_refresh
[0] = TRUE
;
5566 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5567 wxGridCellCoords ( m_numRows
- 1,
5569 m_selectedTopLeft
.SetCol( col
);
5574 need_refresh
[1] = TRUE
;
5575 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5576 wxGridCellCoords ( oldTop
- 1,
5578 m_selectedTopLeft
.SetRow( 0 );
5581 if ( oldRight
< col
)
5583 need_refresh
[2] = TRUE
;
5584 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5585 wxGridCellCoords ( m_numRows
- 1,
5587 m_selectedBottomRight
.SetCol( col
);
5590 if ( oldBottom
< m_numRows
- 1 )
5592 need_refresh
[3] = TRUE
;
5593 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5595 wxGridCellCoords ( m_numRows
- 1,
5597 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5600 for (i
= 0; i
< 4; i
++ )
5601 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5602 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5608 r
= SelectionToDeviceRect();
5610 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5612 m_selectedTopLeft
.Set( 0, col
);
5613 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5614 r
= SelectionToDeviceRect();
5615 m_gridWin
->Refresh( FALSE
, &r
);
5618 wxGridRangeSelectEvent
gridEvt( GetId(),
5619 EVT_GRID_RANGE_SELECT
,
5622 m_selectedBottomRight
);
5624 GetEventHandler()->ProcessEvent(gridEvt
);
5628 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5631 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5633 if ( topRow
> bottomRow
)
5640 if ( leftCol
> rightCol
)
5647 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5648 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5650 if ( m_selectedTopLeft
!= updateTopLeft
||
5651 m_selectedBottomRight
!= updateBottomRight
)
5653 // Compute two optimal update rectangles:
5654 // Either one rectangle is a real subset of the
5655 // other, or they are (almost) disjoint!
5657 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5660 // Store intermediate values
5661 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5662 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5663 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5664 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5666 // Determine the outer/inner coordinates.
5667 if (oldLeft
> leftCol
)
5673 if (oldTop
> topRow
)
5679 if (oldRight
< rightCol
)
5682 oldRight
= rightCol
;
5685 if (oldBottom
< bottomRow
)
5688 oldBottom
= bottomRow
;
5692 // Now, either the stuff marked old is the outer
5693 // rectangle or we don't have a situation where one
5694 // is contained in the other.
5696 if ( oldLeft
< leftCol
)
5698 need_refresh
[0] = TRUE
;
5699 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5701 wxGridCellCoords ( oldBottom
,
5705 if ( oldTop
< topRow
)
5707 need_refresh
[1] = TRUE
;
5708 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5710 wxGridCellCoords ( topRow
- 1,
5714 if ( oldRight
> rightCol
)
5716 need_refresh
[2] = TRUE
;
5717 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5719 wxGridCellCoords ( oldBottom
,
5723 if ( oldBottom
> bottomRow
)
5725 need_refresh
[3] = TRUE
;
5726 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5728 wxGridCellCoords ( oldBottom
,
5734 m_selectedTopLeft
= updateTopLeft
;
5735 m_selectedBottomRight
= updateBottomRight
;
5737 // various Refresh() calls
5738 for (i
= 0; i
< 4; i
++ )
5739 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5740 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5743 // only generate an event if the block is not being selected by
5744 // dragging the mouse (in which case the event will be generated in
5745 // the mouse event handler)
5746 if ( !m_isDragging
)
5748 wxGridRangeSelectEvent
gridEvt( GetId(),
5749 EVT_GRID_RANGE_SELECT
,
5752 m_selectedBottomRight
);
5754 GetEventHandler()->ProcessEvent(gridEvt
);
5758 void wxGrid::SelectAll()
5760 m_selectedTopLeft
.Set( 0, 0 );
5761 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5763 m_gridWin
->Refresh();
5767 void wxGrid::ClearSelection()
5769 m_selectedTopLeft
= wxGridNoCellCoords
;
5770 m_selectedBottomRight
= wxGridNoCellCoords
;
5774 // This function returns the rectangle that encloses the given block
5775 // in device coords clipped to the client size of the grid window.
5777 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5778 const wxGridCellCoords
&bottomRight
)
5780 wxRect
rect( wxGridNoCellRect
);
5783 cellRect
= CellToRect( topLeft
);
5784 if ( cellRect
!= wxGridNoCellRect
)
5790 rect
= wxRect( 0, 0, 0, 0 );
5793 cellRect
= CellToRect( bottomRight
);
5794 if ( cellRect
!= wxGridNoCellRect
)
5800 return wxGridNoCellRect
;
5803 // convert to scrolled coords
5805 int left
, top
, right
, bottom
;
5806 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5807 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5810 m_gridWin
->GetClientSize( &cw
, &ch
);
5812 rect
.SetLeft( wxMax(0, left
) );
5813 rect
.SetTop( wxMax(0, top
) );
5814 rect
.SetRight( wxMin(cw
, right
) );
5815 rect
.SetBottom( wxMin(ch
, bottom
) );
5823 // ------ Grid event classes
5826 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5828 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5829 int row
, int col
, int x
, int y
,
5830 bool control
, bool shift
, bool alt
, bool meta
)
5831 : wxNotifyEvent( type
, id
)
5837 m_control
= control
;
5842 SetEventObject(obj
);
5846 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
5848 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
5849 int rowOrCol
, int x
, int y
,
5850 bool control
, bool shift
, bool alt
, bool meta
)
5851 : wxNotifyEvent( type
, id
)
5853 m_rowOrCol
= rowOrCol
;
5856 m_control
= control
;
5861 SetEventObject(obj
);
5865 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
5867 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
5868 const wxGridCellCoords
& topLeft
,
5869 const wxGridCellCoords
& bottomRight
,
5870 bool control
, bool shift
, bool alt
, bool meta
)
5871 : wxNotifyEvent( type
, id
)
5873 m_topLeft
= topLeft
;
5874 m_bottomRight
= bottomRight
;
5875 m_control
= control
;
5880 SetEventObject(obj
);
5884 #endif // ifndef wxUSE_NEW_GRID