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
);
460 dc
.DrawRectangle(rect
);
463 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
464 wxGridCellAttr
& attr
,
466 const wxRect
& rectCell
,
470 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
472 // now we only have to draw the text
473 dc
.SetBackgroundMode( wxTRANSPARENT
);
477 dc
.SetTextBackground( grid
.GetSelectionBackground() );
478 dc
.SetTextForeground( grid
.GetSelectionForeground() );
482 dc
.SetTextBackground( attr
.GetBackgroundColour() );
483 dc
.SetTextForeground( attr
.GetTextColour() );
485 dc
.SetFont( attr
.GetFont() );
488 attr
.GetAlignment(&hAlign
, &vAlign
);
490 wxRect rect
= rectCell
;
496 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
497 rect
, hAlign
, vAlign
);
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
504 const wxColour
& wxGridCellAttr::GetTextColour() const
508 else if (m_defGridAttr
!= this)
509 return m_defGridAttr
->GetTextColour();
511 wxFAIL_MSG(wxT("Missing default cell attribute"));
517 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
519 if (HasBackgroundColour())
521 else if (m_defGridAttr
!= this)
522 return m_defGridAttr
->GetBackgroundColour();
524 wxFAIL_MSG(wxT("Missing default cell attribute"));
530 const wxFont
& wxGridCellAttr::GetFont() const
534 else if (m_defGridAttr
!= this)
535 return m_defGridAttr
->GetFont();
537 wxFAIL_MSG(wxT("Missing default cell attribute"));
543 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
545 if (HasAlignment()) {
546 if ( hAlign
) *hAlign
= m_hAlign
;
547 if ( vAlign
) *vAlign
= m_vAlign
;
549 else if (m_defGridAttr
!= this)
550 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
552 wxFAIL_MSG(wxT("Missing default cell attribute"));
557 wxGridCellRenderer
* wxGridCellAttr::GetRenderer() const
561 else if (m_defGridAttr
!= this)
562 return m_defGridAttr
->GetRenderer();
564 wxFAIL_MSG(wxT("Missing default cell attribute"));
569 // ----------------------------------------------------------------------------
570 // wxGridCellAttrData
571 // ----------------------------------------------------------------------------
573 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
575 int n
= FindIndex(row
, col
);
576 if ( n
== wxNOT_FOUND
)
579 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
585 // change the attribute
586 m_attrs
[(size_t)n
].attr
= attr
;
590 // remove this attribute
591 m_attrs
.RemoveAt((size_t)n
);
596 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
598 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
600 int n
= FindIndex(row
, col
);
601 if ( n
!= wxNOT_FOUND
)
603 attr
= m_attrs
[(size_t)n
].attr
;
610 int wxGridCellAttrData::FindIndex(int row
, int col
) const
612 size_t count
= m_attrs
.GetCount();
613 for ( size_t n
= 0; n
< count
; n
++ )
615 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
616 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
625 // ----------------------------------------------------------------------------
626 // wxGridRowOrColAttrData
627 // ----------------------------------------------------------------------------
629 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
631 size_t count
= m_attrs
.Count();
632 for ( size_t n
= 0; n
< count
; n
++ )
634 m_attrs
[n
]->DecRef();
638 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
640 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
642 int n
= m_rowsOrCols
.Index(rowOrCol
);
643 if ( n
!= wxNOT_FOUND
)
645 attr
= m_attrs
[(size_t)n
];
652 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
654 int n
= m_rowsOrCols
.Index(rowOrCol
);
655 if ( n
== wxNOT_FOUND
)
658 m_rowsOrCols
.Add(rowOrCol
);
665 // change the attribute
666 m_attrs
[(size_t)n
] = attr
;
670 // remove this attribute
671 m_attrs
[(size_t)n
]->DecRef();
672 m_rowsOrCols
.RemoveAt((size_t)n
);
673 m_attrs
.RemoveAt((size_t)n
);
678 // ----------------------------------------------------------------------------
679 // wxGridCellAttrProvider
680 // ----------------------------------------------------------------------------
682 wxGridCellAttrProvider::wxGridCellAttrProvider()
684 m_data
= (wxGridCellAttrProviderData
*)NULL
;
687 wxGridCellAttrProvider::~wxGridCellAttrProvider()
692 void wxGridCellAttrProvider::InitData()
694 m_data
= new wxGridCellAttrProviderData
;
697 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
699 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
702 // first look for the attribute of this specific cell
703 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
707 // then look for the col attr (col attributes are more common than
708 // the row ones, hence they have priority)
709 attr
= m_data
->m_colAttrs
.GetAttr(col
);
714 // finally try the row attributes
715 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
722 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
728 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
731 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
736 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
739 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
744 m_data
->m_colAttrs
.SetAttr(attr
, col
);
747 // ----------------------------------------------------------------------------
749 // ----------------------------------------------------------------------------
751 //////////////////////////////////////////////////////////////////////
753 // Abstract base class for grid data (the model)
755 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
758 wxGridTableBase::wxGridTableBase()
760 m_view
= (wxGrid
*) NULL
;
761 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
764 wxGridTableBase::~wxGridTableBase()
766 delete m_attrProvider
;
769 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
771 delete m_attrProvider
;
772 m_attrProvider
= attrProvider
;
775 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
777 if ( m_attrProvider
)
778 return m_attrProvider
->GetAttr(row
, col
);
780 return (wxGridCellAttr
*)NULL
;
783 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
785 if ( m_attrProvider
)
787 m_attrProvider
->SetAttr(attr
, row
, col
);
791 // as we take ownership of the pointer and don't store it, we must
797 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
799 if ( m_attrProvider
)
801 m_attrProvider
->SetRowAttr(attr
, row
);
805 // as we take ownership of the pointer and don't store it, we must
811 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
813 if ( m_attrProvider
)
815 m_attrProvider
->SetColAttr(attr
, col
);
819 // as we take ownership of the pointer and don't store it, we must
825 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
827 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
828 "but your derived table class does not override this function") );
833 bool wxGridTableBase::AppendRows( size_t numRows
)
835 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
836 "but your derived table class does not override this function"));
841 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
843 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
844 "but your derived table class does not override this function"));
849 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
851 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
852 "but your derived table class does not override this function"));
857 bool wxGridTableBase::AppendCols( size_t numCols
)
859 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
860 "but your derived table class does not override this function"));
865 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
867 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
868 "but your derived table class does not override this function"));
874 wxString
wxGridTableBase::GetRowLabelValue( int row
)
881 wxString
wxGridTableBase::GetColLabelValue( int col
)
883 // default col labels are:
884 // cols 0 to 25 : A-Z
885 // cols 26 to 675 : AA-ZZ
892 s
+= (_T('A') + (wxChar
)( col%26
));
894 if ( col
< 0 ) break;
897 // reverse the string...
899 for ( i
= 0; i
< n
; i
++ )
909 //////////////////////////////////////////////////////////////////////
911 // Message class for the grid table to send requests and notifications
915 wxGridTableMessage::wxGridTableMessage()
917 m_table
= (wxGridTableBase
*) NULL
;
923 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
924 int commandInt1
, int commandInt2
)
928 m_comInt1
= commandInt1
;
929 m_comInt2
= commandInt2
;
934 //////////////////////////////////////////////////////////////////////
936 // A basic grid table for string data. An object of this class will
937 // created by wxGrid if you don't specify an alternative table class.
940 WX_DEFINE_OBJARRAY(wxGridStringArray
)
942 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
944 wxGridStringTable::wxGridStringTable()
949 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
954 m_data
.Alloc( numRows
);
958 for ( col
= 0; col
< numCols
; col
++ )
960 sa
.Add( wxEmptyString
);
963 for ( row
= 0; row
< numRows
; row
++ )
969 wxGridStringTable::~wxGridStringTable()
973 long wxGridStringTable::GetNumberRows()
975 return m_data
.GetCount();
978 long wxGridStringTable::GetNumberCols()
980 if ( m_data
.GetCount() > 0 )
981 return m_data
[0].GetCount();
986 wxString
wxGridStringTable::GetValue( int row
, int col
)
988 // TODO: bounds checking
990 return m_data
[row
][col
];
993 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
995 // TODO: bounds checking
997 m_data
[row
][col
] = s
;
1000 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
1002 // TODO: bounds checking
1004 return (m_data
[row
][col
] == wxEmptyString
);
1008 void wxGridStringTable::Clear()
1011 int numRows
, numCols
;
1013 numRows
= m_data
.GetCount();
1016 numCols
= m_data
[0].GetCount();
1018 for ( row
= 0; row
< numRows
; row
++ )
1020 for ( col
= 0; col
< numCols
; col
++ )
1022 m_data
[row
][col
] = wxEmptyString
;
1029 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1033 size_t curNumRows
= m_data
.GetCount();
1034 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1036 if ( pos
>= curNumRows
)
1038 return AppendRows( numRows
);
1042 sa
.Alloc( curNumCols
);
1043 for ( col
= 0; col
< curNumCols
; col
++ )
1045 sa
.Add( wxEmptyString
);
1048 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
1050 m_data
.Insert( sa
, row
);
1055 wxGridTableMessage
msg( this,
1056 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1060 GetView()->ProcessTableMessage( msg
);
1066 bool wxGridStringTable::AppendRows( size_t numRows
)
1070 size_t curNumRows
= m_data
.GetCount();
1071 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1074 if ( curNumCols
> 0 )
1076 sa
.Alloc( curNumCols
);
1077 for ( col
= 0; col
< curNumCols
; col
++ )
1079 sa
.Add( wxEmptyString
);
1083 for ( row
= 0; row
< numRows
; row
++ )
1090 wxGridTableMessage
msg( this,
1091 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1094 GetView()->ProcessTableMessage( msg
);
1100 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1104 size_t curNumRows
= m_data
.GetCount();
1106 if ( pos
>= curNumRows
)
1109 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
1110 "Pos value is invalid for present table with %d rows",
1111 pos
, numRows
, curNumRows
);
1112 wxFAIL_MSG( wxT(errmsg
) );
1116 if ( numRows
> curNumRows
- pos
)
1118 numRows
= curNumRows
- pos
;
1121 if ( numRows
>= curNumRows
)
1123 m_data
.Empty(); // don't release memory just yet
1127 for ( n
= 0; n
< numRows
; n
++ )
1129 m_data
.Remove( pos
);
1135 wxGridTableMessage
msg( this,
1136 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1140 GetView()->ProcessTableMessage( msg
);
1146 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1150 size_t curNumRows
= m_data
.GetCount();
1151 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1153 if ( pos
>= curNumCols
)
1155 return AppendCols( numCols
);
1158 for ( row
= 0; row
< curNumRows
; row
++ )
1160 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1162 m_data
[row
].Insert( wxEmptyString
, col
);
1168 wxGridTableMessage
msg( this,
1169 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1173 GetView()->ProcessTableMessage( msg
);
1179 bool wxGridStringTable::AppendCols( size_t numCols
)
1183 size_t curNumRows
= m_data
.GetCount();
1186 // TODO: something better than this ?
1188 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
1189 "Call AppendRows() first") );
1193 for ( row
= 0; row
< curNumRows
; row
++ )
1195 for ( n
= 0; n
< numCols
; n
++ )
1197 m_data
[row
].Add( wxEmptyString
);
1203 wxGridTableMessage
msg( this,
1204 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1207 GetView()->ProcessTableMessage( msg
);
1213 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1217 size_t curNumRows
= m_data
.GetCount();
1218 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
1220 if ( pos
>= curNumCols
)
1223 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
1224 "Pos value is invalid for present table with %d cols",
1225 pos
, numCols
, curNumCols
);
1226 wxFAIL_MSG( wxT( errmsg
) );
1230 if ( numCols
> curNumCols
- pos
)
1232 numCols
= curNumCols
- pos
;
1235 for ( row
= 0; row
< curNumRows
; row
++ )
1237 if ( numCols
>= curNumCols
)
1239 m_data
[row
].Clear();
1243 for ( n
= 0; n
< numCols
; n
++ )
1245 m_data
[row
].Remove( pos
);
1252 wxGridTableMessage
msg( this,
1253 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1257 GetView()->ProcessTableMessage( msg
);
1263 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1265 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1267 // using default label
1269 return wxGridTableBase::GetRowLabelValue( row
);
1273 return m_rowLabels
[ row
];
1277 wxString
wxGridStringTable::GetColLabelValue( int col
)
1279 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1281 // using default label
1283 return wxGridTableBase::GetColLabelValue( col
);
1287 return m_colLabels
[ col
];
1291 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1293 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1295 int n
= m_rowLabels
.GetCount();
1297 for ( i
= n
; i
<= row
; i
++ )
1299 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1303 m_rowLabels
[row
] = value
;
1306 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1308 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1310 int n
= m_colLabels
.GetCount();
1312 for ( i
= n
; i
<= col
; i
++ )
1314 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1318 m_colLabels
[col
] = value
;
1323 //////////////////////////////////////////////////////////////////////
1325 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
1327 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
1328 EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown
)
1332 wxGridTextCtrl::wxGridTextCtrl( wxWindow
*par
,
1336 const wxString
& value
,
1340 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
1343 m_isCellControl
= isCellControl
;
1347 void wxGridTextCtrl::OnKeyDown( wxKeyEvent
& event
)
1349 switch ( event
.KeyCode() )
1353 m_grid
->SetEditControlValue( startValue
);
1354 SetInsertionPointEnd();
1358 m_grid
->EnableCellEditControl( FALSE
);
1367 if ( m_isCellControl
)
1369 // send the event to the parent grid, skipping the
1370 // event if nothing happens
1372 event
.Skip( m_grid
->ProcessEvent( event
) );
1376 // default text control response within the top edit
1384 if ( m_isCellControl
)
1386 if ( !m_grid
->ProcessEvent( event
) )
1388 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1389 // wxMotif needs a little extra help...
1391 int pos
= GetInsertionPoint();
1392 wxString
s( GetValue() );
1393 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
1395 SetInsertionPoint( pos
);
1397 // the other ports can handle a Return key press
1406 if ( m_isCellControl
)
1408 // send the event to the parent grid, skipping the
1409 // event if nothing happens
1411 event
.Skip( m_grid
->ProcessEvent( event
) );
1415 // default text control response within the top edit
1427 void wxGridTextCtrl::SetStartValue( const wxString
& s
)
1430 wxTextCtrl::SetValue(s
);
1435 //////////////////////////////////////////////////////////////////////
1437 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1439 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1440 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1441 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1442 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1445 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1447 const wxPoint
&pos
, const wxSize
&size
)
1448 : wxWindow( parent
, id
, pos
, size
)
1453 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1457 // NO - don't do this because it will set both the x and y origin
1458 // coords to match the parent scrolled window and we just want to
1459 // set the y coord - MB
1461 // m_owner->PrepareDC( dc );
1464 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1465 dc
.SetDeviceOrigin( 0, -y
);
1467 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1468 m_owner
->DrawRowLabels( dc
);
1472 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1474 m_owner
->ProcessRowLabelMouseEvent( event
);
1478 // This seems to be required for wxMotif otherwise the mouse
1479 // cursor must be in the cell edit control to get key events
1481 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1483 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1488 //////////////////////////////////////////////////////////////////////
1490 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1492 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1493 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1494 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1495 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1498 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1500 const wxPoint
&pos
, const wxSize
&size
)
1501 : wxWindow( parent
, id
, pos
, size
)
1506 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1510 // NO - don't do this because it will set both the x and y origin
1511 // coords to match the parent scrolled window and we just want to
1512 // set the x coord - MB
1514 // m_owner->PrepareDC( dc );
1517 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1518 dc
.SetDeviceOrigin( -x
, 0 );
1520 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1521 m_owner
->DrawColLabels( dc
);
1525 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1527 m_owner
->ProcessColLabelMouseEvent( event
);
1531 // This seems to be required for wxMotif otherwise the mouse
1532 // cursor must be in the cell edit control to get key events
1534 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1536 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1541 //////////////////////////////////////////////////////////////////////
1543 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1545 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1546 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1547 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1548 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1551 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1553 const wxPoint
&pos
, const wxSize
&size
)
1554 : wxWindow( parent
, id
, pos
, size
)
1559 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1563 int client_height
= 0;
1564 int client_width
= 0;
1565 GetClientSize( &client_width
, &client_height
);
1567 dc
.SetPen( *wxBLACK_PEN
);
1568 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1569 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1571 dc
.SetPen( *wxWHITE_PEN
);
1572 dc
.DrawLine( 0, 0, client_width
, 0 );
1573 dc
.DrawLine( 0, 0, 0, client_height
);
1577 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1579 m_owner
->ProcessCornerLabelMouseEvent( event
);
1583 // This seems to be required for wxMotif otherwise the mouse
1584 // cursor must be in the cell edit control to get key events
1586 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1588 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1593 //////////////////////////////////////////////////////////////////////
1595 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1597 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1598 EVT_PAINT( wxGridWindow::OnPaint
)
1599 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1600 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1601 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1604 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1605 wxGridRowLabelWindow
*rowLblWin
,
1606 wxGridColLabelWindow
*colLblWin
,
1607 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1608 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1611 m_rowLabelWin
= rowLblWin
;
1612 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
&)
1661 //-----------------------------------------------------------------------------
1662 // wxGridEditTimer (internal)
1663 //-----------------------------------------------------------------------------
1665 wxGridEditTimer::wxGridEditTimer( wxGrid
*owner
)
1670 void wxGridEditTimer::Notify()
1672 m_owner
->EnableCellEditControl( TRUE
);
1676 //////////////////////////////////////////////////////////////////////
1678 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1680 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1681 EVT_PAINT( wxGrid::OnPaint
)
1682 EVT_SIZE( wxGrid::OnSize
)
1683 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1684 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1687 wxGrid::wxGrid( wxWindow
*parent
,
1692 const wxString
& name
)
1693 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1702 m_defaultCellAttr
->SafeDecRef();
1704 #ifdef DEBUG_ATTR_CACHE
1705 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1706 wxPrintf(_T("wxGrid attribute cache statistics: "
1707 "total: %u, hits: %u (%u%%)\n"),
1708 total
, gs_nAttrCacheHits
,
1709 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1719 // ----- internal init and update functions
1722 void wxGrid::Create()
1724 m_created
= FALSE
; // set to TRUE by CreateGrid
1725 m_displayed
= FALSE
; // set to TRUE by OnPaint
1727 m_table
= (wxGridTableBase
*) NULL
;
1729 m_cellEditCtrl
= (wxWindow
*) NULL
;
1731 m_defaultCellAttr
= new wxGridCellAttr
;
1732 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1733 // RD: Should we fill the default attrs now or is waiting until Init() okay?
1738 m_currentCellCoords
= wxGridNoCellCoords
;
1740 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1741 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1743 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1748 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1753 m_colLabelWin
= new wxGridColLabelWindow( this,
1758 m_gridWin
= new wxGridWindow( this,
1765 SetTargetWindow( m_gridWin
);
1769 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1773 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1778 m_numRows
= numRows
;
1779 m_numCols
= numCols
;
1781 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1782 m_table
->SetView( this );
1791 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
1795 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1800 m_numRows
= table
->GetNumberRows();
1801 m_numCols
= table
->GetNumberCols();
1804 m_table
->SetView( this );
1819 if ( m_numRows
<= 0 )
1820 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1822 if ( m_numCols
<= 0 )
1823 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1825 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1826 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1828 if ( m_rowLabelWin
)
1830 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1834 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1837 m_labelTextColour
= wxColour( _T("BLACK") );
1840 m_attrCache
.row
= -1;
1842 // TODO: something better than this ?
1844 m_labelFont
= this->GetFont();
1845 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1847 m_rowLabelHorizAlign
= wxLEFT
;
1848 m_rowLabelVertAlign
= wxCENTRE
;
1850 m_colLabelHorizAlign
= wxCENTRE
;
1851 m_colLabelVertAlign
= wxTOP
;
1853 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1854 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1856 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1857 m_defaultRowHeight
+= 8;
1859 m_defaultRowHeight
+= 4;
1862 m_rowHeights
.Alloc( m_numRows
);
1863 m_rowBottoms
.Alloc( m_numRows
);
1865 for ( i
= 0; i
< m_numRows
; i
++ )
1867 m_rowHeights
.Add( m_defaultRowHeight
);
1868 rowBottom
+= m_defaultRowHeight
;
1869 m_rowBottoms
.Add( rowBottom
);
1872 m_colWidths
.Alloc( m_numCols
);
1873 m_colRights
.Alloc( m_numCols
);
1875 for ( i
= 0; i
< m_numCols
; i
++ )
1877 m_colWidths
.Add( m_defaultColWidth
);
1878 colRight
+= m_defaultColWidth
;
1879 m_colRights
.Add( colRight
);
1882 m_defaultCellAttr
->SetFont(GetFont());
1883 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
1884 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1885 m_defaultCellAttr
->SetTextColour(
1886 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
1887 m_defaultCellAttr
->SetBackgroundColour(
1888 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
1891 m_gridLineColour
= wxColour( 128, 128, 255 );
1892 m_gridLinesEnabled
= TRUE
;
1894 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1895 m_winCapture
= (wxWindow
*)NULL
;
1897 m_dragRowOrCol
= -1;
1898 m_isDragging
= FALSE
;
1900 m_editTimer
= new wxGridEditTimer ( this );
1902 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1903 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1905 m_currentCellCoords
= wxGridNoCellCoords
;
1907 m_selectedTopLeft
= wxGridNoCellCoords
;
1908 m_selectedBottomRight
= wxGridNoCellCoords
;
1909 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
1910 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1912 m_editable
= TRUE
; // default for whole grid
1914 m_inOnKeyDown
= FALSE
;
1917 // TODO: extend this to other types of controls
1919 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1926 #if defined(__WXMSW__)
1927 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1931 m_cellEditCtrl
->Show( FALSE
);
1932 m_cellEditCtrlEnabled
= FALSE
;
1933 m_editCtrlType
= wxGRID_TEXTCTRL
;
1937 void wxGrid::CalcDimensions()
1940 GetClientSize( &cw
, &ch
);
1942 if ( m_numRows
> 0 && m_numCols
> 0 )
1944 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1945 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1947 // TODO: restore the scroll position that we had before sizing
1950 GetViewStart( &x
, &y
);
1951 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1952 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1958 void wxGrid::CalcWindowSizes()
1961 GetClientSize( &cw
, &ch
);
1963 if ( m_cornerLabelWin
->IsShown() )
1964 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
1966 if ( m_colLabelWin
->IsShown() )
1967 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
1969 if ( m_rowLabelWin
->IsShown() )
1970 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
1972 if ( m_gridWin
->IsShown() )
1973 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
1977 // this is called when the grid table sends a message to say that it
1978 // has been redimensioned
1980 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1984 switch ( msg
.GetId() )
1986 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1988 size_t pos
= msg
.GetCommandInt();
1989 int numRows
= msg
.GetCommandInt2();
1990 for ( i
= 0; i
< numRows
; i
++ )
1992 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1993 m_rowBottoms
.Insert( 0, pos
);
1995 m_numRows
+= numRows
;
1998 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
2000 for ( i
= pos
; i
< m_numRows
; i
++ )
2002 bottom
+= m_rowHeights
[i
];
2003 m_rowBottoms
[i
] = bottom
;
2009 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2011 int numRows
= msg
.GetCommandInt();
2012 for ( i
= 0; i
< numRows
; i
++ )
2014 m_rowHeights
.Add( m_defaultRowHeight
);
2015 m_rowBottoms
.Add( 0 );
2018 int oldNumRows
= m_numRows
;
2019 m_numRows
+= numRows
;
2022 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2024 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2026 bottom
+= m_rowHeights
[i
];
2027 m_rowBottoms
[i
] = bottom
;
2033 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2035 size_t pos
= msg
.GetCommandInt();
2036 int numRows
= msg
.GetCommandInt2();
2037 for ( i
= 0; i
< numRows
; i
++ )
2039 m_rowHeights
.Remove( pos
);
2040 m_rowBottoms
.Remove( pos
);
2042 m_numRows
-= numRows
;
2047 m_colWidths
.Clear();
2048 m_colRights
.Clear();
2049 m_currentCellCoords
= wxGridNoCellCoords
;
2053 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2054 m_currentCellCoords
.Set( 0, 0 );
2057 for ( i
= 0; i
< m_numRows
; i
++ )
2059 h
+= m_rowHeights
[i
];
2060 m_rowBottoms
[i
] = h
;
2068 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2070 size_t pos
= msg
.GetCommandInt();
2071 int numCols
= msg
.GetCommandInt2();
2072 for ( i
= 0; i
< numCols
; i
++ )
2074 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2075 m_colRights
.Insert( 0, pos
);
2077 m_numCols
+= numCols
;
2080 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2082 for ( i
= pos
; i
< m_numCols
; i
++ )
2084 right
+= m_colWidths
[i
];
2085 m_colRights
[i
] = right
;
2091 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2093 int numCols
= msg
.GetCommandInt();
2094 for ( i
= 0; i
< numCols
; i
++ )
2096 m_colWidths
.Add( m_defaultColWidth
);
2097 m_colRights
.Add( 0 );
2100 int oldNumCols
= m_numCols
;
2101 m_numCols
+= numCols
;
2104 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2106 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2108 right
+= m_colWidths
[i
];
2109 m_colRights
[i
] = right
;
2115 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2117 size_t pos
= msg
.GetCommandInt();
2118 int numCols
= msg
.GetCommandInt2();
2119 for ( i
= 0; i
< numCols
; i
++ )
2121 m_colWidths
.Remove( pos
);
2122 m_colRights
.Remove( pos
);
2124 m_numCols
-= numCols
;
2128 #if 0 // leave the row alone here so that AppendCols will work subsequently
2130 m_rowHeights
.Clear();
2131 m_rowBottoms
.Clear();
2133 m_currentCellCoords
= wxGridNoCellCoords
;
2137 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2138 m_currentCellCoords
.Set( 0, 0 );
2141 for ( i
= 0; i
< m_numCols
; i
++ )
2143 w
+= m_colWidths
[i
];
2156 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
2158 wxRegionIterator
iter( reg
);
2161 m_rowLabelsExposed
.Empty();
2168 // TODO: remove this when we can...
2169 // There is a bug in wxMotif that gives garbage update
2170 // rectangles if you jump-scroll a long way by clicking the
2171 // scrollbar with middle button. This is a work-around
2173 #if defined(__WXMOTIF__)
2175 m_gridWin
->GetClientSize( &cw
, &ch
);
2176 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2177 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2180 // logical bounds of update region
2183 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2184 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2186 // find the row labels within these bounds
2190 for ( row
= 0; row
< m_numRows
; row
++ )
2192 if ( m_rowBottoms
[row
] < top
) continue;
2194 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2195 if ( rowTop
> bottom
) break;
2197 m_rowLabelsExposed
.Add( row
);
2205 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
2207 wxRegionIterator
iter( reg
);
2210 m_colLabelsExposed
.Empty();
2217 // TODO: remove this when we can...
2218 // There is a bug in wxMotif that gives garbage update
2219 // rectangles if you jump-scroll a long way by clicking the
2220 // scrollbar with middle button. This is a work-around
2222 #if defined(__WXMOTIF__)
2224 m_gridWin
->GetClientSize( &cw
, &ch
);
2225 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2226 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2229 // logical bounds of update region
2232 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2233 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2235 // find the cells within these bounds
2239 for ( col
= 0; col
< m_numCols
; col
++ )
2241 if ( m_colRights
[col
] < left
) continue;
2243 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2244 if ( colLeft
> right
) break;
2246 m_colLabelsExposed
.Add( col
);
2254 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
2256 wxRegionIterator
iter( reg
);
2259 m_cellsExposed
.Empty();
2260 m_rowsExposed
.Empty();
2261 m_colsExposed
.Empty();
2263 int left
, top
, right
, bottom
;
2268 // TODO: remove this when we can...
2269 // There is a bug in wxMotif that gives garbage update
2270 // rectangles if you jump-scroll a long way by clicking the
2271 // scrollbar with middle button. This is a work-around
2273 #if defined(__WXMOTIF__)
2275 m_gridWin
->GetClientSize( &cw
, &ch
);
2276 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2277 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2278 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2279 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2282 // logical bounds of update region
2284 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2285 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2287 // find the cells within these bounds
2290 int colLeft
, rowTop
;
2291 for ( row
= 0; row
< m_numRows
; row
++ )
2293 if ( m_rowBottoms
[row
] < top
) continue;
2295 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2296 if ( rowTop
> bottom
) break;
2298 m_rowsExposed
.Add( row
);
2300 for ( col
= 0; col
< m_numCols
; col
++ )
2302 if ( m_colRights
[col
] < left
) continue;
2304 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2305 if ( colLeft
> right
) break;
2307 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2308 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2317 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2320 wxPoint
pos( event
.GetPosition() );
2321 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2323 if ( event
.Dragging() )
2325 m_isDragging
= TRUE
;
2327 if ( event
.LeftIsDown() )
2329 switch( m_cursorMode
)
2331 case WXGRID_CURSOR_RESIZE_ROW
:
2333 int cw
, ch
, left
, dummy
;
2334 m_gridWin
->GetClientSize( &cw
, &ch
);
2335 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2337 wxClientDC
dc( m_gridWin
);
2340 m_rowBottoms
[m_dragRowOrCol
] -
2341 m_rowHeights
[m_dragRowOrCol
] +
2342 WXGRID_MIN_ROW_HEIGHT
);
2343 dc
.SetLogicalFunction(wxINVERT
);
2344 if ( m_dragLastPos
>= 0 )
2346 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2348 dc
.DrawLine( left
, y
, left
+cw
, y
);
2353 case WXGRID_CURSOR_SELECT_ROW
:
2354 if ( (row
= YToRow( y
)) >= 0 &&
2355 !IsInSelection( row
, 0 ) )
2357 SelectRow( row
, TRUE
);
2360 // default label to suppress warnings about "enumeration value
2361 // 'xxx' not handled in switch
2369 m_isDragging
= FALSE
;
2372 // ------------ Entering or leaving the window
2374 if ( event
.Entering() || event
.Leaving() )
2376 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2380 // ------------ Left button pressed
2382 else if ( event
.LeftDown() )
2384 // don't send a label click event for a hit on the
2385 // edge of the row label - this is probably the user
2386 // wanting to resize the row
2388 if ( YToEdgeOfRow(y
) < 0 )
2392 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2394 SelectRow( row
, event
.ShiftDown() );
2395 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2400 // starting to drag-resize a row
2402 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2407 // ------------ Left double click
2409 else if (event
.LeftDClick() )
2411 if ( YToEdgeOfRow(y
) < 0 )
2414 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2419 // ------------ Left button released
2421 else if ( event
.LeftUp() )
2423 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2425 DoEndDragResizeRow();
2427 // Note: we are ending the event *after* doing
2428 // default processing in this case
2430 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2433 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2438 // ------------ Right button down
2440 else if ( event
.RightDown() )
2443 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2445 // no default action at the moment
2450 // ------------ Right double click
2452 else if ( event
.RightDClick() )
2455 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2457 // no default action at the moment
2462 // ------------ No buttons down and mouse moving
2464 else if ( event
.Moving() )
2466 m_dragRowOrCol
= YToEdgeOfRow( y
);
2467 if ( m_dragRowOrCol
>= 0 )
2469 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2471 // don't capture the mouse yet
2472 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2475 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2477 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2483 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2486 wxPoint
pos( event
.GetPosition() );
2487 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2489 if ( event
.Dragging() )
2491 m_isDragging
= TRUE
;
2493 if ( event
.LeftIsDown() )
2495 switch( m_cursorMode
)
2497 case WXGRID_CURSOR_RESIZE_COL
:
2499 int cw
, ch
, dummy
, top
;
2500 m_gridWin
->GetClientSize( &cw
, &ch
);
2501 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2503 wxClientDC
dc( m_gridWin
);
2506 m_colRights
[m_dragRowOrCol
] -
2507 m_colWidths
[m_dragRowOrCol
] +
2508 WXGRID_MIN_COL_WIDTH
);
2509 dc
.SetLogicalFunction(wxINVERT
);
2510 if ( m_dragLastPos
>= 0 )
2512 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2514 dc
.DrawLine( x
, top
, x
, top
+ch
);
2519 case WXGRID_CURSOR_SELECT_COL
:
2520 if ( (col
= XToCol( x
)) >= 0 &&
2521 !IsInSelection( 0, col
) )
2523 SelectCol( col
, TRUE
);
2526 // default label to suppress warnings about "enumeration value
2527 // 'xxx' not handled in switch
2535 m_isDragging
= FALSE
;
2538 // ------------ Entering or leaving the window
2540 if ( event
.Entering() || event
.Leaving() )
2542 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2546 // ------------ Left button pressed
2548 else if ( event
.LeftDown() )
2550 // don't send a label click event for a hit on the
2551 // edge of the col label - this is probably the user
2552 // wanting to resize the col
2554 if ( XToEdgeOfCol(x
) < 0 )
2558 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2560 SelectCol( col
, event
.ShiftDown() );
2561 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2566 // starting to drag-resize a col
2568 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2573 // ------------ Left double click
2575 if ( event
.LeftDClick() )
2577 if ( XToEdgeOfCol(x
) < 0 )
2580 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2585 // ------------ Left button released
2587 else if ( event
.LeftUp() )
2589 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2591 DoEndDragResizeCol();
2593 // Note: we are ending the event *after* doing
2594 // default processing in this case
2596 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2599 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2604 // ------------ Right button down
2606 else if ( event
.RightDown() )
2609 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2611 // no default action at the moment
2616 // ------------ Right double click
2618 else if ( event
.RightDClick() )
2621 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2623 // no default action at the moment
2628 // ------------ No buttons down and mouse moving
2630 else if ( event
.Moving() )
2632 m_dragRowOrCol
= XToEdgeOfCol( x
);
2633 if ( m_dragRowOrCol
>= 0 )
2635 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2637 // don't capture the cursor yet
2638 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2641 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2643 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2649 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2651 if ( event
.LeftDown() )
2653 // indicate corner label by having both row and
2656 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2662 else if ( event
.LeftDClick() )
2664 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2667 else if ( event
.RightDown() )
2669 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2671 // no default action at the moment
2675 else if ( event
.RightDClick() )
2677 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2679 // no default action at the moment
2684 void wxGrid::ChangeCursorMode(CursorMode mode
,
2689 static const wxChar
*cursorModes
[] =
2698 wxLogTrace(_T("grid"),
2699 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2700 win
== m_colLabelWin
? _T("colLabelWin")
2701 : win
? _T("rowLabelWin")
2703 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2704 #endif // __WXDEBUG__
2706 if ( mode
== m_cursorMode
)
2711 // by default use the grid itself
2717 m_winCapture
->ReleaseMouse();
2718 m_winCapture
= (wxWindow
*)NULL
;
2721 m_cursorMode
= mode
;
2723 switch ( m_cursorMode
)
2725 case WXGRID_CURSOR_RESIZE_ROW
:
2726 win
->SetCursor( m_rowResizeCursor
);
2729 case WXGRID_CURSOR_RESIZE_COL
:
2730 win
->SetCursor( m_colResizeCursor
);
2734 win
->SetCursor( *wxSTANDARD_CURSOR
);
2737 // we need to capture mouse when resizing
2738 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2739 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2741 if ( captureMouse
&& resize
)
2743 win
->CaptureMouse();
2748 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2751 wxPoint
pos( event
.GetPosition() );
2752 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2754 wxGridCellCoords coords
;
2755 XYToCell( x
, y
, coords
);
2757 if ( event
.Dragging() )
2759 m_isDragging
= TRUE
;
2760 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2762 // Hide the edit control, so it
2763 // won't interfer with drag-shrinking.
2764 if ( IsCellEditControlEnabled() )
2765 HideCellEditControl();
2766 if ( coords
!= wxGridNoCellCoords
)
2768 if ( !IsSelection() )
2770 SelectBlock( coords
, coords
);
2774 SelectBlock( m_currentCellCoords
, coords
);
2778 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2780 int cw
, ch
, left
, dummy
;
2781 m_gridWin
->GetClientSize( &cw
, &ch
);
2782 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2784 wxClientDC
dc( m_gridWin
);
2787 m_rowBottoms
[m_dragRowOrCol
] -
2788 m_rowHeights
[m_dragRowOrCol
] +
2789 WXGRID_MIN_ROW_HEIGHT
);
2790 dc
.SetLogicalFunction(wxINVERT
);
2791 if ( m_dragLastPos
>= 0 )
2793 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2795 dc
.DrawLine( left
, y
, left
+cw
, y
);
2798 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2800 int cw
, ch
, dummy
, top
;
2801 m_gridWin
->GetClientSize( &cw
, &ch
);
2802 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2804 wxClientDC
dc( m_gridWin
);
2807 m_colRights
[m_dragRowOrCol
] -
2808 m_colWidths
[m_dragRowOrCol
] + WXGRID_MIN_COL_WIDTH
);
2809 dc
.SetLogicalFunction(wxINVERT
);
2810 if ( m_dragLastPos
>= 0 )
2812 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2814 dc
.DrawLine( x
, top
, x
, top
+ch
);
2821 m_isDragging
= FALSE
;
2823 if ( coords
!= wxGridNoCellCoords
)
2825 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2826 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2829 if ( event
.Entering() || event
.Leaving() )
2831 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2832 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2837 // ------------ Left button pressed
2839 if ( event
.LeftDown() )
2841 EnableCellEditControl( FALSE
);
2842 if ( event
.ShiftDown() )
2844 SelectBlock( m_currentCellCoords
, coords
);
2846 else if ( XToEdgeOfCol(x
) < 0 &&
2847 YToEdgeOfRow(y
) < 0 )
2849 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2854 MakeCellVisible( coords
);
2855 SetCurrentCell( coords
);
2861 // ------------ Left double click
2863 else if ( event
.LeftDClick() )
2865 EnableCellEditControl( FALSE
);
2866 m_editTimer
->Stop();
2867 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2869 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2877 // ------------ Left button released
2879 else if ( event
.LeftUp() )
2881 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2883 if ( IsSelection() )
2885 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
2888 // Show the edit control, if it has
2889 // been hidden for drag-shrinking.
2890 if ( IsCellEditControlEnabled() )
2891 ShowCellEditControl();
2892 if( IsEditable() && coords
== m_currentCellCoords
)
2893 m_editTimer
->Start( 100, TRUE
);
2895 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2897 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2898 DoEndDragResizeRow();
2900 // Note: we are ending the event *after* doing
2901 // default processing in this case
2903 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2905 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2907 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2908 DoEndDragResizeCol();
2910 // Note: we are ending the event *after* doing
2911 // default processing in this case
2913 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2920 // ------------ Right button down
2922 else if ( event
.RightDown() )
2924 EnableCellEditControl( FALSE
);
2925 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2930 // no default action at the moment
2935 // ------------ Right double click
2937 else if ( event
.RightDClick() )
2939 EnableCellEditControl( FALSE
);
2940 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2945 // no default action at the moment
2949 // ------------ Moving and no button action
2951 else if ( event
.Moving() && !event
.IsButton() )
2953 int dragRow
= YToEdgeOfRow( y
);
2954 int dragCol
= XToEdgeOfCol( x
);
2956 // Dragging on the corner of a cell to resize in both
2957 // directions is not implemented yet...
2959 if ( dragRow
>= 0 && dragCol
>= 0 )
2961 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2967 m_dragRowOrCol
= dragRow
;
2969 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2971 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
2979 m_dragRowOrCol
= dragCol
;
2981 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2983 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
2989 // Neither on a row or col edge
2991 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2993 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3000 void wxGrid::DoEndDragResizeRow()
3002 if ( m_dragLastPos
>= 0 )
3004 // erase the last line and resize the row
3006 int cw
, ch
, left
, dummy
;
3007 m_gridWin
->GetClientSize( &cw
, &ch
);
3008 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3010 wxClientDC
dc( m_gridWin
);
3012 dc
.SetLogicalFunction( wxINVERT
);
3013 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3014 HideCellEditControl();
3016 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
3017 SetRowSize( m_dragRowOrCol
,
3018 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
3020 if ( !GetBatchCount() )
3022 // Only needed to get the correct rect.y:
3023 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
3025 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
3026 rect
.width
= m_rowLabelWidth
;
3027 rect
.height
= ch
- rect
.y
;
3028 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3030 m_gridWin
->Refresh( FALSE
, &rect
);
3033 ShowCellEditControl();
3038 void wxGrid::DoEndDragResizeCol()
3040 if ( m_dragLastPos
>= 0 )
3042 // erase the last line and resize the col
3044 int cw
, ch
, dummy
, top
;
3045 m_gridWin
->GetClientSize( &cw
, &ch
);
3046 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3048 wxClientDC
dc( m_gridWin
);
3050 dc
.SetLogicalFunction( wxINVERT
);
3051 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3052 HideCellEditControl();
3054 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3055 SetColSize( m_dragRowOrCol
,
3056 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3058 if ( !GetBatchCount() )
3060 // Only needed to get the correct rect.x:
3061 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3063 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3064 rect
.width
= cw
- rect
.x
;
3065 rect
.height
= m_colLabelHeight
;
3066 m_colLabelWin
->Refresh( TRUE
, &rect
);
3068 m_gridWin
->Refresh( FALSE
, &rect
);
3071 ShowCellEditControl();
3078 // ------ interaction with data model
3080 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
3082 switch ( msg
.GetId() )
3084 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3085 return GetModelValues();
3087 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3088 return SetModelValues();
3090 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3091 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3092 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3093 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3094 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3095 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3096 return Redimension( msg
);
3105 // The behaviour of this function depends on the grid table class
3106 // Clear() function. For the default wxGridStringTable class the
3107 // behavious is to replace all cell contents with wxEmptyString but
3108 // not to change the number of rows or cols.
3110 void wxGrid::ClearGrid()
3115 SetEditControlValue();
3116 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3121 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3123 // TODO: something with updateLabels flag
3127 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3133 bool ok
= m_table
->InsertRows( pos
, numRows
);
3135 // the table will have sent the results of the insert row
3136 // operation to this view object as a grid table message
3140 if ( m_numCols
== 0 )
3142 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3144 // TODO: perhaps instead of appending the default number of cols
3145 // we should remember what the last non-zero number of cols was ?
3149 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3151 // if we have just inserted cols into an empty grid the current
3152 // cell will be undefined...
3154 SetCurrentCell( 0, 0 );
3158 if ( !GetBatchCount() ) Refresh();
3161 SetEditControlValue();
3171 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3173 // TODO: something with updateLabels flag
3177 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3181 if ( m_table
&& m_table
->AppendRows( numRows
) )
3183 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3185 // if we have just inserted cols into an empty grid the current
3186 // cell will be undefined...
3188 SetCurrentCell( 0, 0 );
3191 // the table will have sent the results of the append row
3192 // operation to this view object as a grid table message
3195 if ( !GetBatchCount() ) Refresh();
3205 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3207 // TODO: something with updateLabels flag
3211 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3215 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
3217 // the table will have sent the results of the delete row
3218 // operation to this view object as a grid table message
3220 if ( m_numRows
> 0 )
3221 SetEditControlValue();
3223 HideCellEditControl();
3226 if ( !GetBatchCount() ) Refresh();
3236 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3238 // TODO: something with updateLabels flag
3242 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3248 HideCellEditControl();
3249 bool ok
= m_table
->InsertCols( pos
, numCols
);
3251 // the table will have sent the results of the insert col
3252 // operation to this view object as a grid table message
3256 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3258 // if we have just inserted cols into an empty grid the current
3259 // cell will be undefined...
3261 SetCurrentCell( 0, 0 );
3265 if ( !GetBatchCount() ) Refresh();
3268 SetEditControlValue();
3278 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3280 // TODO: something with updateLabels flag
3284 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3288 if ( m_table
&& m_table
->AppendCols( numCols
) )
3290 // the table will have sent the results of the append col
3291 // operation to this view object as a grid table message
3293 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3295 // if we have just inserted cols into an empty grid the current
3296 // cell will be undefined...
3298 SetCurrentCell( 0, 0 );
3302 if ( !GetBatchCount() ) Refresh();
3312 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3314 // TODO: something with updateLabels flag
3318 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3322 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
3324 // the table will have sent the results of the delete col
3325 // operation to this view object as a grid table message
3327 if ( m_numCols
> 0 )
3328 SetEditControlValue();
3330 HideCellEditControl();
3333 if ( !GetBatchCount() ) Refresh();
3345 // ----- event handlers
3348 // Generate a grid event based on a mouse event and
3349 // return the result of ProcessEvent()
3351 bool wxGrid::SendEvent( const wxEventType type
,
3353 wxMouseEvent
& mouseEv
)
3355 if ( type
== EVT_GRID_ROW_SIZE
||
3356 type
== EVT_GRID_COL_SIZE
)
3358 int rowOrCol
= (row
== -1 ? col
: row
);
3360 wxGridSizeEvent
gridEvt( GetId(),
3364 mouseEv
.GetX(), mouseEv
.GetY(),
3365 mouseEv
.ControlDown(),
3366 mouseEv
.ShiftDown(),
3368 mouseEv
.MetaDown() );
3370 return GetEventHandler()->ProcessEvent(gridEvt
);
3372 else if ( type
== EVT_GRID_RANGE_SELECT
)
3374 wxGridRangeSelectEvent
gridEvt( GetId(),
3378 m_selectedBottomRight
,
3379 mouseEv
.ControlDown(),
3380 mouseEv
.ShiftDown(),
3382 mouseEv
.MetaDown() );
3384 return GetEventHandler()->ProcessEvent(gridEvt
);
3388 wxGridEvent
gridEvt( GetId(),
3392 mouseEv
.GetX(), mouseEv
.GetY(),
3393 mouseEv
.ControlDown(),
3394 mouseEv
.ShiftDown(),
3396 mouseEv
.MetaDown() );
3398 return GetEventHandler()->ProcessEvent(gridEvt
);
3403 // Generate a grid event of specified type and return the result
3404 // of ProcessEvent().
3406 bool wxGrid::SendEvent( const wxEventType type
,
3409 if ( type
== EVT_GRID_ROW_SIZE
||
3410 type
== EVT_GRID_COL_SIZE
)
3412 int rowOrCol
= (row
== -1 ? col
: row
);
3414 wxGridSizeEvent
gridEvt( GetId(),
3419 return GetEventHandler()->ProcessEvent(gridEvt
);
3423 wxGridEvent
gridEvt( GetId(),
3428 return GetEventHandler()->ProcessEvent(gridEvt
);
3433 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3435 wxPaintDC
dc( this );
3437 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3438 m_numRows
&& m_numCols
)
3440 m_currentCellCoords
.Set(0, 0);
3441 SetEditControlValue();
3442 ShowCellEditControl();
3449 // This is just here to make sure that CalcDimensions gets called when
3450 // the grid view is resized... then the size event is skipped to allow
3451 // the box sizers to handle everything
3453 void wxGrid::OnSize( wxSizeEvent
& event
)
3460 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3462 if ( m_inOnKeyDown
)
3464 // shouldn't be here - we are going round in circles...
3466 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while alread active") );
3469 m_inOnKeyDown
= TRUE
;
3471 // propagate the event up and see if it gets processed
3473 wxWindow
*parent
= GetParent();
3474 wxKeyEvent
keyEvt( event
);
3475 keyEvt
.SetEventObject( parent
);
3477 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3479 // try local handlers
3481 switch ( event
.KeyCode() )
3484 if ( event
.ControlDown() )
3486 MoveCursorUpBlock();
3495 if ( event
.ControlDown() )
3497 MoveCursorDownBlock();
3506 if ( event
.ControlDown() )
3508 MoveCursorLeftBlock();
3517 if ( event
.ControlDown() )
3519 MoveCursorRightBlock();
3528 if ( !IsEditable() )
3539 if ( event
.ControlDown() )
3541 event
.Skip(); // to let the edit control have the return
3550 if ( event
.ControlDown() )
3552 MakeCellVisible( 0, 0 );
3553 SetCurrentCell( 0, 0 );
3562 if ( event
.ControlDown() )
3564 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3565 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3582 // now try the cell edit control
3584 if ( !IsCellEditControlEnabled() )
3585 EnableCellEditControl( TRUE
);
3586 if ( IsCellEditControlEnabled() )
3588 event
.SetEventObject( m_cellEditCtrl
);
3589 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
3595 m_inOnKeyDown
= FALSE
;
3598 void wxGrid::OnEraseBackground(wxEraseEvent
&)
3601 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3603 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3605 // the event has been intercepted - do nothing
3610 m_currentCellCoords
!= wxGridNoCellCoords
)
3612 HideCellEditControl();
3613 SaveEditControlValue();
3616 m_currentCellCoords
= coords
;
3618 SetEditControlValue();
3622 ShowCellEditControl();
3624 if ( IsSelection() )
3626 wxRect
r( SelectionToDeviceRect() );
3628 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3632 SelectBlock ( coords
, coords
);
3638 // ------ functions to get/send data (see also public functions)
3641 bool wxGrid::GetModelValues()
3645 // all we need to do is repaint the grid
3647 m_gridWin
->Refresh();
3655 bool wxGrid::SetModelValues()
3661 for ( row
= 0; row
< m_numRows
; row
++ )
3663 for ( col
= 0; col
< m_numCols
; col
++ )
3665 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3677 // Note - this function only draws cells that are in the list of
3678 // exposed cells (usually set from the update region by
3679 // CalcExposedCells)
3681 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3683 if ( !m_numRows
|| !m_numCols
) return;
3686 size_t numCells
= m_cellsExposed
.GetCount();
3688 for ( i
= 0; i
< numCells
; i
++ )
3690 DrawCell( dc
, m_cellsExposed
[i
] );
3695 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3697 int row
= coords
.GetRow();
3698 int col
= coords
.GetCol();
3700 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3703 // we draw the cell border ourselves
3704 #if !WXGRID_DRAW_LINES
3705 if ( m_gridLinesEnabled
)
3706 DrawCellBorder( dc
, coords
);
3709 // but all the rest is drawn by the cell renderer and hence may be
3712 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3713 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3714 rect
.width
= m_colWidths
[col
] - 1;
3715 rect
.height
= m_rowHeights
[row
] - 1;
3717 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
3718 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
3722 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3724 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3725 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3727 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3728 int row
= coords
.GetRow();
3729 int col
= coords
.GetCol();
3731 // right hand border
3733 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3734 m_colRights
[col
], m_rowBottoms
[row
] );
3738 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3739 m_colRights
[col
], m_rowBottoms
[row
] );
3743 // TODO: remove this ???
3744 // This is used to redraw all grid lines e.g. when the grid line colour
3747 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3749 if ( !m_gridLinesEnabled
||
3751 !m_numCols
) return;
3753 int top
, bottom
, left
, right
;
3757 m_gridWin
->GetClientSize(&cw
, &ch
);
3759 // virtual coords of visible area
3761 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3762 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3766 reg
.GetBox(x
, y
, w
, h
);
3767 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3768 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3771 // avoid drawing grid lines past the last row and col
3773 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3774 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3776 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3778 // horizontal grid lines
3781 for ( i
= 0; i
< m_numRows
; i
++ )
3783 if ( m_rowBottoms
[i
]-1 > bottom
)
3787 else if ( m_rowBottoms
[i
]-1 >= top
)
3789 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
3794 // vertical grid lines
3796 for ( i
= 0; i
< m_numCols
; i
++ )
3798 if ( m_colRights
[i
]-1 > right
)
3802 else if ( m_colRights
[i
]-1 >= left
)
3804 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
3810 void wxGrid::DrawRowLabels( wxDC
& dc
)
3812 if ( !m_numRows
|| !m_numCols
) return;
3815 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3817 for ( i
= 0; i
< numLabels
; i
++ )
3819 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3824 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
3826 if ( m_rowHeights
[row
] <= 0 ) return;
3828 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3830 dc
.SetPen( *wxBLACK_PEN
);
3831 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3832 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3834 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3835 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3837 dc
.SetPen( *wxWHITE_PEN
);
3838 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
3839 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
3841 dc
.SetBackgroundMode( wxTRANSPARENT
);
3842 dc
.SetTextForeground( GetLabelTextColour() );
3843 dc
.SetFont( GetLabelFont() );
3846 GetRowLabelAlignment( &hAlign
, &vAlign
);
3850 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3851 rect
.SetWidth( m_rowLabelWidth
- 4 );
3852 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3853 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
3857 void wxGrid::DrawColLabels( wxDC
& dc
)
3859 if ( !m_numRows
|| !m_numCols
) return;
3862 size_t numLabels
= m_colLabelsExposed
.GetCount();
3864 for ( i
= 0; i
< numLabels
; i
++ )
3866 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
3871 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
3873 if ( m_colWidths
[col
] <= 0 ) return;
3875 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
3877 dc
.SetPen( *wxBLACK_PEN
);
3878 dc
.DrawLine( m_colRights
[col
]-1, 0,
3879 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3881 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
3882 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3884 dc
.SetPen( *wxWHITE_PEN
);
3885 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
3886 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
3888 dc
.SetBackgroundMode( wxTRANSPARENT
);
3889 dc
.SetTextForeground( GetLabelTextColour() );
3890 dc
.SetFont( GetLabelFont() );
3892 dc
.SetBackgroundMode( wxTRANSPARENT
);
3893 dc
.SetTextForeground( GetLabelTextColour() );
3894 dc
.SetFont( GetLabelFont() );
3897 GetColLabelAlignment( &hAlign
, &vAlign
);
3900 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3902 rect
.SetWidth( m_colWidths
[col
] - 4 );
3903 rect
.SetHeight( m_colLabelHeight
- 4 );
3904 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
3908 void wxGrid::DrawTextRectangle( wxDC
& dc
,
3909 const wxString
& value
,
3914 long textWidth
, textHeight
;
3915 long lineWidth
, lineHeight
;
3916 wxArrayString lines
;
3918 dc
.SetClippingRegion( rect
);
3919 StringToLines( value
, lines
);
3920 if ( lines
.GetCount() )
3922 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
3923 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
3926 switch ( horizAlign
)
3929 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
3933 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
3942 switch ( vertAlign
)
3945 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
3949 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
3958 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
3960 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
3965 dc
.DestroyClippingRegion();
3969 // Split multi line text up into an array of strings. Any existing
3970 // contents of the string array are preserved.
3972 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
3976 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
3977 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
3979 while ( startPos
< (int)tVal
.Length() )
3981 pos
= tVal
.Mid(startPos
).Find( eol
);
3986 else if ( pos
== 0 )
3988 lines
.Add( wxEmptyString
);
3992 lines
.Add( value
.Mid(startPos
, pos
) );
3996 if ( startPos
< (int)value
.Length() )
3998 lines
.Add( value
.Mid( startPos
) );
4003 void wxGrid::GetTextBoxSize( wxDC
& dc
,
4004 wxArrayString
& lines
,
4005 long *width
, long *height
)
4012 for ( i
= 0; i
< lines
.GetCount(); i
++ )
4014 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
4015 w
= wxMax( w
, lineW
);
4025 // ------ Edit control functions
4029 void wxGrid::EnableEditing( bool edit
)
4031 // TODO: improve this ?
4033 if ( edit
!= m_editable
)
4037 // TODO: extend this for other edit control types
4039 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
4041 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
4047 void wxGrid::EnableCellEditControl( bool enable
)
4049 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4050 SetCurrentCell( 0, 0 );
4051 if ( m_cellEditCtrl
&&
4052 enable
!= m_cellEditCtrlEnabled
)
4057 m_cellEditCtrlEnabled
= enable
;
4058 SetEditControlValue();
4059 // requires m_cellEditCtrlEnabled to be already true
4060 ShowCellEditControl();
4064 HideCellEditControl();
4065 // requires m_cellEditCtrlEnabled to be still true
4066 SaveEditControlValue();
4067 m_cellEditCtrlEnabled
= enable
;
4073 void wxGrid::ShowCellEditControl()
4077 if ( IsCellEditControlEnabled() )
4079 if ( !IsVisible( m_currentCellCoords
) )
4085 rect
= CellToRect( m_currentCellCoords
);
4087 // convert to scrolled coords
4089 int left
, top
, right
, bottom
;
4090 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4091 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4092 left
--; top
--; right
--; bottom
--; // cell is shifted by one pixel
4094 m_gridWin
->GetClientSize( &cw
, &ch
);
4096 // Make the edit control large enough to allow for internal margins
4097 // TODO: remove this if the text ctrl sizing is improved esp. for unix
4100 #if defined(__WXMOTIF__)
4101 if ( m_currentCellCoords
.GetRow() == 0 ||
4102 m_currentCellCoords
.GetCol() == 0 )
4111 if ( m_currentCellCoords
.GetRow() == 0 ||
4112 m_currentCellCoords
.GetCol() == 0 )
4122 #if defined(__WXGTK__)
4125 if (left
!= 0) left_diff
++;
4126 if (top
!= 0) top_diff
++;
4127 rect
.SetLeft( left
+ left_diff
);
4128 rect
.SetTop( top
+ top_diff
);
4129 rect
.SetRight( rect
.GetRight() - left_diff
);
4130 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4132 rect
.SetLeft( wxMax(0, left
- extra
) );
4133 rect
.SetTop( wxMax(0, top
- extra
) );
4134 rect
.SetRight( rect
.GetRight() + 2*extra
);
4135 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4138 m_cellEditCtrl
->SetSize( rect
);
4139 m_cellEditCtrl
->Show( TRUE
);
4141 switch ( m_editCtrlType
)
4143 case wxGRID_TEXTCTRL
:
4144 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
4147 case wxGRID_CHECKBOX
:
4148 // TODO: anything ???
4153 // TODO: anything ???
4157 case wxGRID_COMBOBOX
:
4158 // TODO: anything ???
4163 m_cellEditCtrl
->SetFocus();
4169 void wxGrid::HideCellEditControl()
4171 if ( IsCellEditControlEnabled() )
4173 m_cellEditCtrl
->Show( FALSE
);
4179 void wxGrid::SetEditControlValue( const wxString
& value
)
4185 s
= GetCellValue(m_currentCellCoords
);
4189 if ( IsCellEditControlEnabled() )
4191 switch ( m_editCtrlType
)
4193 case wxGRID_TEXTCTRL
:
4194 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
4197 case wxGRID_CHECKBOX
:
4198 // TODO: implement this
4203 // TODO: implement this
4207 case wxGRID_COMBOBOX
:
4208 // TODO: implement this
4217 void wxGrid::SaveEditControlValue()
4221 wxWindow
*ctrl
= (wxWindow
*)NULL
;
4223 if ( IsCellEditControlEnabled() )
4225 ctrl
= m_cellEditCtrl
;
4232 bool valueChanged
= FALSE
;
4234 switch ( m_editCtrlType
)
4236 case wxGRID_TEXTCTRL
:
4237 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
4238 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
4239 SetCellValue( m_currentCellCoords
,
4240 ((wxTextCtrl
*) ctrl
)->GetValue() );
4243 case wxGRID_CHECKBOX
:
4244 // TODO: implement this
4249 // TODO: implement this
4253 case wxGRID_COMBOBOX
:
4254 // TODO: implement this
4261 SendEvent( EVT_GRID_CELL_CHANGE
,
4262 m_currentCellCoords
.GetRow(),
4263 m_currentCellCoords
.GetCol() );
4270 // ------ Grid location functions
4271 // Note that all of these functions work with the logical coordinates of
4272 // grid cells and labels so you will need to convert from device
4273 // coordinates for mouse events etc.
4276 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4278 int row
= YToRow(y
);
4279 int col
= XToCol(x
);
4281 if ( row
== -1 || col
== -1 )
4283 coords
= wxGridNoCellCoords
;
4287 coords
.Set( row
, col
);
4292 int wxGrid::YToRow( int y
)
4296 for ( i
= 0; i
< m_numRows
; i
++ )
4298 if ( y
< m_rowBottoms
[i
] ) return i
;
4305 int wxGrid::XToCol( int x
)
4309 for ( i
= 0; i
< m_numCols
; i
++ )
4311 if ( x
< m_colRights
[i
] ) return i
;
4318 // return the row number that that the y coord is near the edge of, or
4319 // -1 if not near an edge
4321 int wxGrid::YToEdgeOfRow( int y
)
4325 for ( i
= 0; i
< m_numRows
; i
++ )
4327 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4329 d
= abs( y
- m_rowBottoms
[i
] );
4331 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4340 // return the col number that that the x coord is near the edge of, or
4341 // -1 if not near an edge
4343 int wxGrid::XToEdgeOfCol( int x
)
4347 for ( i
= 0; i
< m_numCols
; i
++ )
4349 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4351 d
= abs( x
- m_colRights
[i
] );
4353 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4362 wxRect
wxGrid::CellToRect( int row
, int col
)
4364 wxRect
rect( -1, -1, -1, -1 );
4366 if ( row
>= 0 && row
< m_numRows
&&
4367 col
>= 0 && col
< m_numCols
)
4369 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4370 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4371 rect
.width
= m_colWidths
[col
];
4372 rect
.height
= m_rowHeights
[ row
];
4379 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4381 // get the cell rectangle in logical coords
4383 wxRect
r( CellToRect( row
, col
) );
4385 // convert to device coords
4387 int left
, top
, right
, bottom
;
4388 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4389 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4391 // check against the client area of the grid window
4394 m_gridWin
->GetClientSize( &cw
, &ch
);
4396 if ( wholeCellVisible
)
4398 // is the cell wholly visible ?
4400 return ( left
>= 0 && right
<= cw
&&
4401 top
>= 0 && bottom
<= ch
);
4405 // is the cell partly visible ?
4407 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4408 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4413 // make the specified cell location visible by doing a minimal amount
4416 void wxGrid::MakeCellVisible( int row
, int col
)
4419 int xpos
= -1, ypos
= -1;
4421 if ( row
>= 0 && row
< m_numRows
&&
4422 col
>= 0 && col
< m_numCols
)
4424 // get the cell rectangle in logical coords
4426 wxRect
r( CellToRect( row
, col
) );
4428 // convert to device coords
4430 int left
, top
, right
, bottom
;
4431 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4432 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4435 m_gridWin
->GetClientSize( &cw
, &ch
);
4441 else if ( bottom
> ch
)
4443 int h
= r
.GetHeight();
4445 for ( i
= row
-1; i
>= 0; i
-- )
4447 if ( h
+ m_rowHeights
[i
] > ch
) break;
4449 h
+= m_rowHeights
[i
];
4450 ypos
-= m_rowHeights
[i
];
4453 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4454 // have rounding errors (this is important, because if we do, we
4455 // might not scroll at all and some cells won't be redrawn)
4456 ypos
+= GRID_SCROLL_LINE
/ 2;
4463 else if ( right
> cw
)
4465 int w
= r
.GetWidth();
4467 for ( i
= col
-1; i
>= 0; i
-- )
4469 if ( w
+ m_colWidths
[i
] > cw
) break;
4471 w
+= m_colWidths
[i
];
4472 xpos
-= m_colWidths
[i
];
4475 // see comment for ypos above
4476 xpos
+= GRID_SCROLL_LINE
/ 2;
4479 if ( xpos
!= -1 || ypos
!= -1 )
4481 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4482 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4483 Scroll( xpos
, ypos
);
4491 // ------ Grid cursor movement functions
4494 bool wxGrid::MoveCursorUp()
4496 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4497 m_currentCellCoords
.GetRow() > 0 )
4499 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4500 m_currentCellCoords
.GetCol() );
4502 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4503 m_currentCellCoords
.GetCol() );
4512 bool wxGrid::MoveCursorDown()
4514 // TODO: allow for scrolling
4516 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4517 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4519 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4520 m_currentCellCoords
.GetCol() );
4522 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4523 m_currentCellCoords
.GetCol() );
4532 bool wxGrid::MoveCursorLeft()
4534 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4535 m_currentCellCoords
.GetCol() > 0 )
4537 MakeCellVisible( m_currentCellCoords
.GetRow(),
4538 m_currentCellCoords
.GetCol() - 1 );
4540 SetCurrentCell( m_currentCellCoords
.GetRow(),
4541 m_currentCellCoords
.GetCol() - 1 );
4550 bool wxGrid::MoveCursorRight()
4552 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4553 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4555 MakeCellVisible( m_currentCellCoords
.GetRow(),
4556 m_currentCellCoords
.GetCol() + 1 );
4558 SetCurrentCell( m_currentCellCoords
.GetRow(),
4559 m_currentCellCoords
.GetCol() + 1 );
4568 bool wxGrid::MovePageUp()
4570 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4572 int row
= m_currentCellCoords
.GetRow();
4576 m_gridWin
->GetClientSize( &cw
, &ch
);
4578 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4579 int newRow
= YToRow( y
- ch
+ 1 );
4584 else if ( newRow
== row
)
4589 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4590 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4598 bool wxGrid::MovePageDown()
4600 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4602 int row
= m_currentCellCoords
.GetRow();
4603 if ( row
< m_numRows
)
4606 m_gridWin
->GetClientSize( &cw
, &ch
);
4608 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4609 int newRow
= YToRow( y
+ ch
);
4612 newRow
= m_numRows
- 1;
4614 else if ( newRow
== row
)
4619 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4620 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4628 bool wxGrid::MoveCursorUpBlock()
4631 m_currentCellCoords
!= wxGridNoCellCoords
&&
4632 m_currentCellCoords
.GetRow() > 0 )
4634 int row
= m_currentCellCoords
.GetRow();
4635 int col
= m_currentCellCoords
.GetCol();
4637 if ( m_table
->IsEmptyCell(row
, col
) )
4639 // starting in an empty cell: find the next block of
4645 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4648 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4650 // starting at the top of a block: find the next block
4656 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4661 // starting within a block: find the top of the block
4666 if ( m_table
->IsEmptyCell(row
, col
) )
4674 MakeCellVisible( row
, col
);
4675 SetCurrentCell( row
, col
);
4683 bool wxGrid::MoveCursorDownBlock()
4686 m_currentCellCoords
!= wxGridNoCellCoords
&&
4687 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4689 int row
= m_currentCellCoords
.GetRow();
4690 int col
= m_currentCellCoords
.GetCol();
4692 if ( m_table
->IsEmptyCell(row
, col
) )
4694 // starting in an empty cell: find the next block of
4697 while ( row
< m_numRows
-1 )
4700 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4703 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4705 // starting at the bottom of a block: find the next block
4708 while ( row
< m_numRows
-1 )
4711 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4716 // starting within a block: find the bottom of the block
4718 while ( row
< m_numRows
-1 )
4721 if ( m_table
->IsEmptyCell(row
, col
) )
4729 MakeCellVisible( row
, col
);
4730 SetCurrentCell( row
, col
);
4738 bool wxGrid::MoveCursorLeftBlock()
4741 m_currentCellCoords
!= wxGridNoCellCoords
&&
4742 m_currentCellCoords
.GetCol() > 0 )
4744 int row
= m_currentCellCoords
.GetRow();
4745 int col
= m_currentCellCoords
.GetCol();
4747 if ( m_table
->IsEmptyCell(row
, col
) )
4749 // starting in an empty cell: find the next block of
4755 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4758 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4760 // starting at the left of a block: find the next block
4766 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4771 // starting within a block: find the left of the block
4776 if ( m_table
->IsEmptyCell(row
, col
) )
4784 MakeCellVisible( row
, col
);
4785 SetCurrentCell( row
, col
);
4793 bool wxGrid::MoveCursorRightBlock()
4796 m_currentCellCoords
!= wxGridNoCellCoords
&&
4797 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4799 int row
= m_currentCellCoords
.GetRow();
4800 int col
= m_currentCellCoords
.GetCol();
4802 if ( m_table
->IsEmptyCell(row
, col
) )
4804 // starting in an empty cell: find the next block of
4807 while ( col
< m_numCols
-1 )
4810 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4813 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4815 // starting at the right of a block: find the next block
4818 while ( col
< m_numCols
-1 )
4821 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4826 // starting within a block: find the right of the block
4828 while ( col
< m_numCols
-1 )
4831 if ( m_table
->IsEmptyCell(row
, col
) )
4839 MakeCellVisible( row
, col
);
4840 SetCurrentCell( row
, col
);
4851 // ------ Label values and formatting
4854 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4856 *horiz
= m_rowLabelHorizAlign
;
4857 *vert
= m_rowLabelVertAlign
;
4860 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4862 *horiz
= m_colLabelHorizAlign
;
4863 *vert
= m_colLabelVertAlign
;
4866 wxString
wxGrid::GetRowLabelValue( int row
)
4870 return m_table
->GetRowLabelValue( row
);
4880 wxString
wxGrid::GetColLabelValue( int col
)
4884 return m_table
->GetColLabelValue( col
);
4895 void wxGrid::SetRowLabelSize( int width
)
4897 width
= wxMax( width
, 0 );
4898 if ( width
!= m_rowLabelWidth
)
4902 m_rowLabelWin
->Show( FALSE
);
4903 m_cornerLabelWin
->Show( FALSE
);
4905 else if ( m_rowLabelWidth
== 0 )
4907 m_rowLabelWin
->Show( TRUE
);
4908 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4911 m_rowLabelWidth
= width
;
4918 void wxGrid::SetColLabelSize( int height
)
4920 height
= wxMax( height
, 0 );
4921 if ( height
!= m_colLabelHeight
)
4925 m_colLabelWin
->Show( FALSE
);
4926 m_cornerLabelWin
->Show( FALSE
);
4928 else if ( m_colLabelHeight
== 0 )
4930 m_colLabelWin
->Show( TRUE
);
4931 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4934 m_colLabelHeight
= height
;
4941 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
4943 if ( m_labelBackgroundColour
!= colour
)
4945 m_labelBackgroundColour
= colour
;
4946 m_rowLabelWin
->SetBackgroundColour( colour
);
4947 m_colLabelWin
->SetBackgroundColour( colour
);
4948 m_cornerLabelWin
->SetBackgroundColour( colour
);
4950 if ( !GetBatchCount() )
4952 m_rowLabelWin
->Refresh();
4953 m_colLabelWin
->Refresh();
4954 m_cornerLabelWin
->Refresh();
4959 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
4961 if ( m_labelTextColour
!= colour
)
4963 m_labelTextColour
= colour
;
4964 if ( !GetBatchCount() )
4966 m_rowLabelWin
->Refresh();
4967 m_colLabelWin
->Refresh();
4972 void wxGrid::SetLabelFont( const wxFont
& font
)
4975 if ( !GetBatchCount() )
4977 m_rowLabelWin
->Refresh();
4978 m_colLabelWin
->Refresh();
4982 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
4984 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4986 m_rowLabelHorizAlign
= horiz
;
4989 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4991 m_rowLabelVertAlign
= vert
;
4994 if ( !GetBatchCount() )
4996 m_rowLabelWin
->Refresh();
5000 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
5002 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
5004 m_colLabelHorizAlign
= horiz
;
5007 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
5009 m_colLabelVertAlign
= vert
;
5012 if ( !GetBatchCount() )
5014 m_colLabelWin
->Refresh();
5018 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
5022 m_table
->SetRowLabelValue( row
, s
);
5023 if ( !GetBatchCount() )
5025 wxRect rect
= CellToRect( row
, 0);
5026 if ( rect
.height
> 0 )
5028 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5030 rect
.width
= m_rowLabelWidth
;
5031 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5037 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
5041 m_table
->SetColLabelValue( col
, s
);
5042 if ( !GetBatchCount() )
5044 wxRect rect
= CellToRect( 0, col
);
5045 if ( rect
.width
> 0 )
5047 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5049 rect
.height
= m_colLabelHeight
;
5050 m_colLabelWin
->Refresh( TRUE
, &rect
);
5056 void wxGrid::SetGridLineColour( const wxColour
& colour
)
5058 if ( m_gridLineColour
!= colour
)
5060 m_gridLineColour
= colour
;
5062 wxClientDC
dc( m_gridWin
);
5064 DrawAllGridLines( dc
, wxRegion() );
5068 void wxGrid::EnableGridLines( bool enable
)
5070 if ( enable
!= m_gridLinesEnabled
)
5072 m_gridLinesEnabled
= enable
;
5074 if ( !GetBatchCount() )
5078 wxClientDC
dc( m_gridWin
);
5080 DrawAllGridLines( dc
, wxRegion() );
5084 m_gridWin
->Refresh();
5091 int wxGrid::GetDefaultRowSize()
5093 return m_defaultRowHeight
;
5096 int wxGrid::GetRowSize( int row
)
5098 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5100 return m_rowHeights
[row
];
5103 int wxGrid::GetDefaultColSize()
5105 return m_defaultColWidth
;
5108 int wxGrid::GetColSize( int col
)
5110 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5112 return m_colWidths
[col
];
5115 // ============================================================================
5116 // access to the grid attributes: each of them has a default value in the grid
5117 // itself and may be overidden on a per-cell basis
5118 // ============================================================================
5120 // ----------------------------------------------------------------------------
5121 // setting default attributes
5122 // ----------------------------------------------------------------------------
5124 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
5126 m_defaultCellAttr
->SetBackgroundColour(col
);
5129 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
5131 m_defaultCellAttr
->SetTextColour(col
);
5134 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
5136 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5139 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
5141 m_defaultCellAttr
->SetFont(font
);
5144 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5146 m_defaultCellAttr
->SetRenderer(renderer
);
5149 // ----------------------------------------------------------------------------
5150 // access to the default attrbiutes
5151 // ----------------------------------------------------------------------------
5153 wxColour
wxGrid::GetDefaultCellBackgroundColour()
5155 return m_defaultCellAttr
->GetBackgroundColour();
5158 wxColour
wxGrid::GetDefaultCellTextColour()
5160 return m_defaultCellAttr
->GetTextColour();
5163 wxFont
wxGrid::GetDefaultCellFont()
5165 return m_defaultCellAttr
->GetFont();
5168 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
5170 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5173 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
5175 return m_defaultCellAttr
->GetRenderer();
5178 // ----------------------------------------------------------------------------
5179 // access to cell attributes
5180 // ----------------------------------------------------------------------------
5182 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
5184 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5185 wxColour colour
= attr
->GetBackgroundColour();
5190 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
5192 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5193 wxColour colour
= attr
->GetTextColour();
5198 wxFont
wxGrid::GetCellFont( int row
, int col
)
5200 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5201 wxFont font
= attr
->GetFont();
5206 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5208 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5209 attr
->GetAlignment(horiz
, vert
);
5213 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
5215 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5216 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5221 // ----------------------------------------------------------------------------
5222 // attribute support: cache, automatic provider creation, ...
5223 // ----------------------------------------------------------------------------
5225 bool wxGrid::CanHaveAttributes()
5232 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5233 // table is providing the attributes itself??? In which case
5234 // I don't think the grid should create a Provider object for the
5235 // table but the table should be smart enough to do that on its own.
5236 if ( !m_table
->GetAttrProvider() )
5238 // use the default attr provider by default
5239 // (another choice would be to just return FALSE thus forcing the user
5241 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5247 void wxGrid::ClearAttrCache()
5249 if ( m_attrCache
.row
!= -1 )
5251 m_attrCache
.attr
->SafeDecRef();
5252 m_attrCache
.row
= -1;
5256 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5258 wxGrid
*self
= (wxGrid
*)this; // const_cast
5260 self
->ClearAttrCache();
5261 self
->m_attrCache
.row
= row
;
5262 self
->m_attrCache
.col
= col
;
5263 self
->m_attrCache
.attr
= attr
;
5267 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5269 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5271 *attr
= m_attrCache
.attr
;
5272 (*attr
)->SafeIncRef();
5274 #ifdef DEBUG_ATTR_CACHE
5275 gs_nAttrCacheHits
++;
5282 #ifdef DEBUG_ATTR_CACHE
5283 gs_nAttrCacheMisses
++;
5289 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
5291 wxGridCellAttr
*attr
;
5292 if ( !LookupAttr(row
, col
, &attr
) )
5294 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5295 CacheAttr(row
, col
, attr
);
5298 attr
->SetDefAttr(m_defaultCellAttr
);
5300 attr
= m_defaultCellAttr
;
5307 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
5309 wxGridCellAttr
*attr
;
5310 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5312 wxASSERT_MSG( m_table
,
5313 _T("we may only be called if CanHaveAttributes() "
5314 "returned TRUE and then m_table should be !NULL") );
5316 attr
= m_table
->GetAttr(row
, col
);
5319 attr
= new wxGridCellAttr
;
5321 // artificially inc the ref count to match DecRef() in caller
5324 m_table
->SetAttr(attr
, row
, col
);
5327 CacheAttr(row
, col
, attr
);
5329 attr
->SetDefAttr(m_defaultCellAttr
);
5333 // ----------------------------------------------------------------------------
5334 // setting cell attributes: this is forwarded to the table
5335 // ----------------------------------------------------------------------------
5337 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5339 if ( CanHaveAttributes() )
5341 m_table
->SetRowAttr(attr
, row
);
5349 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5351 if ( CanHaveAttributes() )
5353 m_table
->SetColAttr(attr
, col
);
5361 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5363 if ( CanHaveAttributes() )
5365 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5366 attr
->SetBackgroundColour(colour
);
5371 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5373 if ( CanHaveAttributes() )
5375 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5376 attr
->SetTextColour(colour
);
5381 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5383 if ( CanHaveAttributes() )
5385 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5386 attr
->SetFont(font
);
5391 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5393 if ( CanHaveAttributes() )
5395 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5396 attr
->SetAlignment(horiz
, vert
);
5401 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5403 if ( CanHaveAttributes() )
5405 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5406 attr
->SetRenderer(renderer
);
5411 // ----------------------------------------------------------------------------
5413 // ----------------------------------------------------------------------------
5415 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5417 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5419 if ( resizeExistingRows
)
5423 for ( row
= 0; row
< m_numRows
; row
++ )
5425 m_rowHeights
[row
] = m_defaultRowHeight
;
5426 bottom
+= m_defaultRowHeight
;
5427 m_rowBottoms
[row
] = bottom
;
5433 void wxGrid::SetRowSize( int row
, int height
)
5435 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5439 int h
= wxMax( 0, height
);
5440 int diff
= h
- m_rowHeights
[row
];
5442 m_rowHeights
[row
] = h
;
5443 for ( i
= row
; i
< m_numRows
; i
++ )
5445 m_rowBottoms
[i
] += diff
;
5450 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5452 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5454 if ( resizeExistingCols
)
5458 for ( col
= 0; col
< m_numCols
; col
++ )
5460 m_colWidths
[col
] = m_defaultColWidth
;
5461 right
+= m_defaultColWidth
;
5462 m_colRights
[col
] = right
;
5468 void wxGrid::SetColSize( int col
, int width
)
5470 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5474 int w
= wxMax( 0, width
);
5475 int diff
= w
- m_colWidths
[col
];
5476 m_colWidths
[col
] = w
;
5478 for ( i
= col
; i
< m_numCols
; i
++ )
5480 m_colRights
[i
] += diff
;
5487 // ------ cell value accessor functions
5490 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5494 m_table
->SetValue( row
, col
, s
.c_str() );
5495 if ( !GetBatchCount() )
5497 wxClientDC
dc( m_gridWin
);
5499 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5502 #if 0 // TODO: edit in place
5504 if ( m_currentCellCoords
.GetRow() == row
&&
5505 m_currentCellCoords
.GetCol() == col
)
5507 SetEditControlValue( s
);
5516 // ------ Block, row and col selection
5519 void wxGrid::SelectRow( int row
, bool addToSelected
)
5523 if ( IsSelection() && addToSelected
)
5526 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5529 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5530 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5531 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5532 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5536 need_refresh
[0] = TRUE
;
5537 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5538 wxGridCellCoords ( oldTop
- 1,
5540 m_selectedTopLeft
.SetRow( row
);
5545 need_refresh
[1] = TRUE
;
5546 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5547 wxGridCellCoords ( oldBottom
,
5550 m_selectedTopLeft
.SetCol( 0 );
5553 if ( oldBottom
< row
)
5555 need_refresh
[2] = TRUE
;
5556 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5557 wxGridCellCoords ( row
,
5559 m_selectedBottomRight
.SetRow( row
);
5562 if ( oldRight
< m_numCols
- 1 )
5564 need_refresh
[3] = TRUE
;
5565 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5567 wxGridCellCoords ( oldBottom
,
5569 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5572 for (i
= 0; i
< 4; i
++ )
5573 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5574 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5578 r
= SelectionToDeviceRect();
5580 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5582 m_selectedTopLeft
.Set( row
, 0 );
5583 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5584 r
= SelectionToDeviceRect();
5585 m_gridWin
->Refresh( FALSE
, &r
);
5588 wxGridRangeSelectEvent
gridEvt( GetId(),
5589 EVT_GRID_RANGE_SELECT
,
5592 m_selectedBottomRight
);
5594 GetEventHandler()->ProcessEvent(gridEvt
);
5598 void wxGrid::SelectCol( int col
, bool addToSelected
)
5600 if ( IsSelection() && addToSelected
)
5603 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5606 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5607 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5608 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5609 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5611 if ( oldLeft
> col
)
5613 need_refresh
[0] = TRUE
;
5614 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5615 wxGridCellCoords ( m_numRows
- 1,
5617 m_selectedTopLeft
.SetCol( col
);
5622 need_refresh
[1] = TRUE
;
5623 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5624 wxGridCellCoords ( oldTop
- 1,
5626 m_selectedTopLeft
.SetRow( 0 );
5629 if ( oldRight
< col
)
5631 need_refresh
[2] = TRUE
;
5632 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5633 wxGridCellCoords ( m_numRows
- 1,
5635 m_selectedBottomRight
.SetCol( col
);
5638 if ( oldBottom
< m_numRows
- 1 )
5640 need_refresh
[3] = TRUE
;
5641 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5643 wxGridCellCoords ( m_numRows
- 1,
5645 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5648 for (i
= 0; i
< 4; i
++ )
5649 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5650 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5656 r
= SelectionToDeviceRect();
5658 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5660 m_selectedTopLeft
.Set( 0, col
);
5661 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5662 r
= SelectionToDeviceRect();
5663 m_gridWin
->Refresh( FALSE
, &r
);
5666 wxGridRangeSelectEvent
gridEvt( GetId(),
5667 EVT_GRID_RANGE_SELECT
,
5670 m_selectedBottomRight
);
5672 GetEventHandler()->ProcessEvent(gridEvt
);
5676 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5679 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5681 if ( topRow
> bottomRow
)
5688 if ( leftCol
> rightCol
)
5695 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5696 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5698 if ( m_selectedTopLeft
!= updateTopLeft
||
5699 m_selectedBottomRight
!= updateBottomRight
)
5701 // Compute two optimal update rectangles:
5702 // Either one rectangle is a real subset of the
5703 // other, or they are (almost) disjoint!
5705 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5708 // Store intermediate values
5709 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5710 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5711 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5712 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5714 // Determine the outer/inner coordinates.
5715 if (oldLeft
> leftCol
)
5721 if (oldTop
> topRow
)
5727 if (oldRight
< rightCol
)
5730 oldRight
= rightCol
;
5733 if (oldBottom
< bottomRow
)
5736 oldBottom
= bottomRow
;
5740 // Now, either the stuff marked old is the outer
5741 // rectangle or we don't have a situation where one
5742 // is contained in the other.
5744 if ( oldLeft
< leftCol
)
5746 need_refresh
[0] = TRUE
;
5747 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5749 wxGridCellCoords ( oldBottom
,
5753 if ( oldTop
< topRow
)
5755 need_refresh
[1] = TRUE
;
5756 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5758 wxGridCellCoords ( topRow
- 1,
5762 if ( oldRight
> rightCol
)
5764 need_refresh
[2] = TRUE
;
5765 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5767 wxGridCellCoords ( oldBottom
,
5771 if ( oldBottom
> bottomRow
)
5773 need_refresh
[3] = TRUE
;
5774 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5776 wxGridCellCoords ( oldBottom
,
5782 m_selectedTopLeft
= updateTopLeft
;
5783 m_selectedBottomRight
= updateBottomRight
;
5785 // various Refresh() calls
5786 for (i
= 0; i
< 4; i
++ )
5787 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5788 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5791 // only generate an event if the block is not being selected by
5792 // dragging the mouse (in which case the event will be generated in
5793 // the mouse event handler)
5794 if ( !m_isDragging
)
5796 wxGridRangeSelectEvent
gridEvt( GetId(),
5797 EVT_GRID_RANGE_SELECT
,
5800 m_selectedBottomRight
);
5802 GetEventHandler()->ProcessEvent(gridEvt
);
5806 void wxGrid::SelectAll()
5808 m_selectedTopLeft
.Set( 0, 0 );
5809 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5811 m_gridWin
->Refresh();
5815 void wxGrid::ClearSelection()
5817 m_selectedTopLeft
= wxGridNoCellCoords
;
5818 m_selectedBottomRight
= wxGridNoCellCoords
;
5822 // This function returns the rectangle that encloses the given block
5823 // in device coords clipped to the client size of the grid window.
5825 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5826 const wxGridCellCoords
&bottomRight
)
5828 wxRect
rect( wxGridNoCellRect
);
5831 cellRect
= CellToRect( topLeft
);
5832 if ( cellRect
!= wxGridNoCellRect
)
5838 rect
= wxRect( 0, 0, 0, 0 );
5841 cellRect
= CellToRect( bottomRight
);
5842 if ( cellRect
!= wxGridNoCellRect
)
5848 return wxGridNoCellRect
;
5851 // convert to scrolled coords
5853 int left
, top
, right
, bottom
;
5854 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5855 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5858 m_gridWin
->GetClientSize( &cw
, &ch
);
5860 rect
.SetLeft( wxMax(0, left
) );
5861 rect
.SetTop( wxMax(0, top
) );
5862 rect
.SetRight( wxMin(cw
, right
) );
5863 rect
.SetBottom( wxMin(ch
, bottom
) );
5871 // ------ Grid event classes
5874 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5876 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5877 int row
, int col
, int x
, int y
,
5878 bool control
, bool shift
, bool alt
, bool meta
)
5879 : wxNotifyEvent( type
, id
)
5885 m_control
= control
;
5890 SetEventObject(obj
);
5894 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
5896 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
5897 int rowOrCol
, int x
, int y
,
5898 bool control
, bool shift
, bool alt
, bool meta
)
5899 : wxNotifyEvent( type
, id
)
5901 m_rowOrCol
= rowOrCol
;
5904 m_control
= control
;
5909 SetEventObject(obj
);
5913 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
5915 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
5916 const wxGridCellCoords
& topLeft
,
5917 const wxGridCellCoords
& bottomRight
,
5918 bool control
, bool shift
, bool alt
, bool meta
)
5919 : wxNotifyEvent( type
, id
)
5921 m_topLeft
= topLeft
;
5922 m_bottomRight
= bottomRight
;
5923 m_control
= control
;
5928 SetEventObject(obj
);
5932 #endif // ifndef wxUSE_NEW_GRID