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
;
1324 //////////////////////////////////////////////////////////////////////
1326 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
1328 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
1329 EVT_KEY_DOWN( wxGridTextCtrl
::OnKeyDown
)
1333 wxGridTextCtrl
::wxGridTextCtrl( wxWindow
*par
,
1337 const wxString
& value
,
1341 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
1344 m_isCellControl
= isCellControl
;
1348 void wxGridTextCtrl
::OnKeyDown( wxKeyEvent
& event
)
1350 switch ( event
.KeyCode() )
1353 m_grid
->SetEditControlValue( startValue
);
1354 SetInsertionPointEnd();
1364 if ( m_isCellControl
)
1366 // send the event to the parent grid, skipping the
1367 // event if nothing happens
1369 event
.Skip( m_grid
->ProcessEvent( event
) );
1373 // default text control response within the top edit
1381 if ( m_isCellControl
)
1383 if ( !m_grid
->ProcessEvent( event
) )
1385 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1386 // wxMotif needs a little extra help...
1388 int pos
= GetInsertionPoint();
1389 wxString
s( GetValue() );
1390 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
1392 SetInsertionPoint( pos
);
1394 // the other ports can handle a Return key press
1404 if ( m_isCellControl
)
1406 // send the event to the parent grid, skipping the
1407 // event if nothing happens
1409 event
.Skip( m_grid
->ProcessEvent( event
) );
1413 // default text control response within the top edit
1425 void wxGridTextCtrl
::SetStartValue( const wxString
& s
)
1428 wxTextCtrl
::SetValue(s
);
1433 //////////////////////////////////////////////////////////////////////
1435 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1437 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1438 EVT_PAINT( wxGridRowLabelWindow
::OnPaint
)
1439 EVT_MOUSE_EVENTS( wxGridRowLabelWindow
::OnMouseEvent
)
1440 EVT_KEY_DOWN( wxGridRowLabelWindow
::OnKeyDown
)
1443 wxGridRowLabelWindow
::wxGridRowLabelWindow( wxGrid
*parent
,
1445 const wxPoint
&pos
, const wxSize
&size
)
1446 : wxWindow( parent
, id
, pos
, size
)
1451 void wxGridRowLabelWindow
::OnPaint( wxPaintEvent
&event
)
1455 // NO - don't do this because it will set both the x and y origin
1456 // coords to match the parent scrolled window and we just want to
1457 // set the y coord - MB
1459 // m_owner->PrepareDC( dc );
1462 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1463 dc
.SetDeviceOrigin( 0, -y
);
1465 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1466 m_owner
->DrawRowLabels( dc
);
1470 void wxGridRowLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
1472 m_owner
->ProcessRowLabelMouseEvent( event
);
1476 // This seems to be required for wxMotif otherwise the mouse
1477 // cursor must be in the cell edit control to get key events
1479 void wxGridRowLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
1481 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1486 //////////////////////////////////////////////////////////////////////
1488 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1490 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1491 EVT_PAINT( wxGridColLabelWindow
::OnPaint
)
1492 EVT_MOUSE_EVENTS( wxGridColLabelWindow
::OnMouseEvent
)
1493 EVT_KEY_DOWN( wxGridColLabelWindow
::OnKeyDown
)
1496 wxGridColLabelWindow
::wxGridColLabelWindow( wxGrid
*parent
,
1498 const wxPoint
&pos
, const wxSize
&size
)
1499 : wxWindow( parent
, id
, pos
, size
)
1504 void wxGridColLabelWindow
::OnPaint( wxPaintEvent
&event
)
1508 // NO - don't do this because it will set both the x and y origin
1509 // coords to match the parent scrolled window and we just want to
1510 // set the x coord - MB
1512 // m_owner->PrepareDC( dc );
1515 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1516 dc
.SetDeviceOrigin( -x
, 0 );
1518 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1519 m_owner
->DrawColLabels( dc
);
1523 void wxGridColLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
1525 m_owner
->ProcessColLabelMouseEvent( event
);
1529 // This seems to be required for wxMotif otherwise the mouse
1530 // cursor must be in the cell edit control to get key events
1532 void wxGridColLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
1534 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1539 //////////////////////////////////////////////////////////////////////
1541 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1543 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1544 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow
::OnMouseEvent
)
1545 EVT_PAINT( wxGridCornerLabelWindow
::OnPaint
)
1546 EVT_KEY_DOWN( wxGridCornerLabelWindow
::OnKeyDown
)
1549 wxGridCornerLabelWindow
::wxGridCornerLabelWindow( wxGrid
*parent
,
1551 const wxPoint
&pos
, const wxSize
&size
)
1552 : wxWindow( parent
, id
, pos
, size
)
1557 void wxGridCornerLabelWindow
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1561 int client_height
= 0;
1562 int client_width
= 0;
1563 GetClientSize( &client_width
, &client_height
);
1565 dc
.SetPen( *wxBLACK_PEN
);
1566 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1567 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1569 dc
.SetPen( *wxWHITE_PEN
);
1570 dc
.DrawLine( 0, 0, client_width
, 0 );
1571 dc
.DrawLine( 0, 0, 0, client_height
);
1575 void wxGridCornerLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
1577 m_owner
->ProcessCornerLabelMouseEvent( event
);
1581 // This seems to be required for wxMotif otherwise the mouse
1582 // cursor must be in the cell edit control to get key events
1584 void wxGridCornerLabelWindow
::OnKeyDown( wxKeyEvent
& event
)
1586 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1591 //////////////////////////////////////////////////////////////////////
1593 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1595 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1596 EVT_PAINT( wxGridWindow
::OnPaint
)
1597 EVT_MOUSE_EVENTS( wxGridWindow
::OnMouseEvent
)
1598 EVT_KEY_DOWN( wxGridWindow
::OnKeyDown
)
1599 EVT_ERASE_BACKGROUND( wxGridWindow
::OnEraseBackground
)
1602 wxGridWindow
::wxGridWindow( wxGrid
*parent
,
1603 wxGridRowLabelWindow
*rowLblWin
,
1604 wxGridColLabelWindow
*colLblWin
,
1605 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1606 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1609 m_rowLabelWin
= rowLblWin
;
1610 m_colLabelWin
= colLblWin
;
1611 SetBackgroundColour( "WHITE" );
1615 wxGridWindow
::~wxGridWindow()
1620 void wxGridWindow
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1622 wxPaintDC
dc( this );
1623 m_owner
->PrepareDC( dc
);
1624 wxRegion reg
= GetUpdateRegion();
1625 m_owner
->CalcCellsExposed( reg
);
1626 m_owner
->DrawGridCellArea( dc
);
1627 #if WXGRID_DRAW_LINES
1628 m_owner
->DrawAllGridLines( dc
, reg
);
1633 void wxGridWindow
::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1635 wxPanel
::ScrollWindow( dx
, dy
, rect
);
1636 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1637 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1641 void wxGridWindow
::OnMouseEvent( wxMouseEvent
& event
)
1643 m_owner
->ProcessGridCellMouseEvent( event
);
1647 // This seems to be required for wxMotif otherwise the mouse
1648 // cursor must be in the cell edit control to get key events
1650 void wxGridWindow
::OnKeyDown( wxKeyEvent
& event
)
1652 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1655 void wxGridWindow
::OnEraseBackground(wxEraseEvent
&)
1660 //////////////////////////////////////////////////////////////////////
1662 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1664 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1665 EVT_PAINT( wxGrid
::OnPaint
)
1666 EVT_SIZE( wxGrid
::OnSize
)
1667 EVT_KEY_DOWN( wxGrid
::OnKeyDown
)
1668 EVT_ERASE_BACKGROUND( wxGrid
::OnEraseBackground
)
1671 wxGrid
::wxGrid( wxWindow
*parent
,
1676 const wxString
& name
)
1677 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1686 m_defaultCellAttr
->SafeDecRef();
1688 #ifdef DEBUG_ATTR_CACHE
1689 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1690 wxPrintf(_T("wxGrid attribute cache statistics: "
1691 "total: %u, hits: %u (%u%%)\n"),
1692 total
, gs_nAttrCacheHits
,
1693 total ?
(gs_nAttrCacheHits
*100) / total
: 0);
1702 // ----- internal init and update functions
1705 void wxGrid
::Create()
1707 m_created
= FALSE
; // set to TRUE by CreateGrid
1708 m_displayed
= FALSE
; // set to TRUE by OnPaint
1710 m_table
= (wxGridTableBase
*) NULL
;
1712 m_cellEditCtrl
= (wxWindow
*) NULL
;
1714 m_defaultCellAttr
= new wxGridCellAttr
;
1715 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1716 // RD: Should we fill the default attrs now or is waiting until Init() okay?
1721 m_currentCellCoords
= wxGridNoCellCoords
;
1723 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1724 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1726 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1731 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1736 m_colLabelWin
= new wxGridColLabelWindow( this,
1741 m_gridWin
= new wxGridWindow( this,
1748 SetTargetWindow( m_gridWin
);
1752 bool wxGrid
::CreateGrid( int numRows
, int numCols
)
1756 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1761 m_numRows
= numRows
;
1762 m_numCols
= numCols
;
1764 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1765 m_table
->SetView( this );
1774 bool wxGrid
::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
1778 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1783 m_numRows
= table
->GetNumberRows();
1784 m_numCols
= table
->GetNumberCols();
1787 m_table
->SetView( this );
1802 if ( m_numRows
<= 0 )
1803 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1805 if ( m_numCols
<= 0 )
1806 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1808 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1809 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1811 if ( m_rowLabelWin
)
1813 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1817 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1820 m_labelTextColour
= wxColour( _T("BLACK") );
1823 m_attrCache
.row
= -1;
1825 // TODO: something better than this ?
1827 m_labelFont
= this->GetFont();
1828 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1830 m_rowLabelHorizAlign
= wxLEFT
;
1831 m_rowLabelVertAlign
= wxCENTRE
;
1833 m_colLabelHorizAlign
= wxCENTRE
;
1834 m_colLabelVertAlign
= wxTOP
;
1836 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1837 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1839 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1840 m_defaultRowHeight
+= 8;
1842 m_defaultRowHeight
+= 4;
1845 m_rowHeights
.Alloc( m_numRows
);
1846 m_rowBottoms
.Alloc( m_numRows
);
1848 for ( i
= 0; i
< m_numRows
; i
++ )
1850 m_rowHeights
.Add( m_defaultRowHeight
);
1851 rowBottom
+= m_defaultRowHeight
;
1852 m_rowBottoms
.Add( rowBottom
);
1855 m_colWidths
.Alloc( m_numCols
);
1856 m_colRights
.Alloc( m_numCols
);
1858 for ( i
= 0; i
< m_numCols
; i
++ )
1860 m_colWidths
.Add( m_defaultColWidth
);
1861 colRight
+= m_defaultColWidth
;
1862 m_colRights
.Add( colRight
);
1865 m_defaultCellAttr
->SetFont(GetFont());
1866 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
1867 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1868 m_defaultCellAttr
->SetTextColour(
1869 wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
1870 m_defaultCellAttr
->SetBackgroundColour(
1871 wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_WINDOW
));
1874 m_gridLineColour
= wxColour( 128, 128, 255 );
1875 m_gridLinesEnabled
= TRUE
;
1877 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1878 m_winCapture
= (wxWindow
*)NULL
;
1880 m_dragRowOrCol
= -1;
1881 m_isDragging
= FALSE
;
1883 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1884 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1886 m_currentCellCoords
= wxGridNoCellCoords
;
1888 m_selectedTopLeft
= wxGridNoCellCoords
;
1889 m_selectedBottomRight
= wxGridNoCellCoords
;
1890 m_selectionBackground
= wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
1891 m_selectionForeground
= wxSystemSettings
::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1893 m_editable
= TRUE
; // default for whole grid
1895 m_inOnKeyDown
= FALSE
;
1898 // TODO: extend this to other types of controls
1900 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1907 #if defined(__WXMSW__)
1908 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1912 m_cellEditCtrl
->Show( FALSE
);
1913 m_cellEditCtrlEnabled
= FALSE
;
1914 m_editCtrlType
= wxGRID_TEXTCTRL
;
1918 void wxGrid
::CalcDimensions()
1921 GetClientSize( &cw
, &ch
);
1923 if ( m_numRows
> 0 && m_numCols
> 0 )
1925 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1926 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1928 // TODO: restore the scroll position that we had before sizing
1931 GetViewStart( &x
, &y
);
1932 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1933 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1939 void wxGrid
::CalcWindowSizes()
1942 GetClientSize( &cw
, &ch
);
1944 if ( m_cornerLabelWin
->IsShown() )
1945 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
1947 if ( m_colLabelWin
->IsShown() )
1948 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
1950 if ( m_rowLabelWin
->IsShown() )
1951 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
1953 if ( m_gridWin
->IsShown() )
1954 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
1958 // this is called when the grid table sends a message to say that it
1959 // has been redimensioned
1961 bool wxGrid
::Redimension( wxGridTableMessage
& msg
)
1965 switch ( msg
.GetId() )
1967 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1969 size_t pos
= msg
.GetCommandInt();
1970 int numRows
= msg
.GetCommandInt2();
1971 for ( i
= 0; i
< numRows
; i
++ )
1973 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1974 m_rowBottoms
.Insert( 0, pos
);
1976 m_numRows
+= numRows
;
1979 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1981 for ( i
= pos
; i
< m_numRows
; i
++ )
1983 bottom
+= m_rowHeights
[i
];
1984 m_rowBottoms
[i
] = bottom
;
1990 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1992 int numRows
= msg
.GetCommandInt();
1993 for ( i
= 0; i
< numRows
; i
++ )
1995 m_rowHeights
.Add( m_defaultRowHeight
);
1996 m_rowBottoms
.Add( 0 );
1999 int oldNumRows
= m_numRows
;
2000 m_numRows
+= numRows
;
2003 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
2005 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2007 bottom
+= m_rowHeights
[i
];
2008 m_rowBottoms
[i
] = bottom
;
2014 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2016 size_t pos
= msg
.GetCommandInt();
2017 int numRows
= msg
.GetCommandInt2();
2018 for ( i
= 0; i
< numRows
; i
++ )
2020 m_rowHeights
.Remove( pos
);
2021 m_rowBottoms
.Remove( pos
);
2023 m_numRows
-= numRows
;
2028 m_colWidths
.Clear();
2029 m_colRights
.Clear();
2030 m_currentCellCoords
= wxGridNoCellCoords
;
2034 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2035 m_currentCellCoords
.Set( 0, 0 );
2038 for ( i
= 0; i
< m_numRows
; i
++ )
2040 h
+= m_rowHeights
[i
];
2041 m_rowBottoms
[i
] = h
;
2049 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2051 size_t pos
= msg
.GetCommandInt();
2052 int numCols
= msg
.GetCommandInt2();
2053 for ( i
= 0; i
< numCols
; i
++ )
2055 m_colWidths
.Insert( m_defaultColWidth
, pos
);
2056 m_colRights
.Insert( 0, pos
);
2058 m_numCols
+= numCols
;
2061 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
2063 for ( i
= pos
; i
< m_numCols
; i
++ )
2065 right
+= m_colWidths
[i
];
2066 m_colRights
[i
] = right
;
2072 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2074 int numCols
= msg
.GetCommandInt();
2075 for ( i
= 0; i
< numCols
; i
++ )
2077 m_colWidths
.Add( m_defaultColWidth
);
2078 m_colRights
.Add( 0 );
2081 int oldNumCols
= m_numCols
;
2082 m_numCols
+= numCols
;
2085 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
2087 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2089 right
+= m_colWidths
[i
];
2090 m_colRights
[i
] = right
;
2096 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2098 size_t pos
= msg
.GetCommandInt();
2099 int numCols
= msg
.GetCommandInt2();
2100 for ( i
= 0; i
< numCols
; i
++ )
2102 m_colWidths
.Remove( pos
);
2103 m_colRights
.Remove( pos
);
2105 m_numCols
-= numCols
;
2109 #if 0 // leave the row alone here so that AppendCols will work subsequently
2111 m_rowHeights
.Clear();
2112 m_rowBottoms
.Clear();
2114 m_currentCellCoords
= wxGridNoCellCoords
;
2118 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2119 m_currentCellCoords
.Set( 0, 0 );
2122 for ( i
= 0; i
< m_numCols
; i
++ )
2124 w
+= m_colWidths
[i
];
2137 void wxGrid
::CalcRowLabelsExposed( wxRegion
& reg
)
2139 wxRegionIterator
iter( reg
);
2142 m_rowLabelsExposed
.Empty();
2149 // TODO: remove this when we can...
2150 // There is a bug in wxMotif that gives garbage update
2151 // rectangles if you jump-scroll a long way by clicking the
2152 // scrollbar with middle button. This is a work-around
2154 #if defined(__WXMOTIF__)
2156 m_gridWin
->GetClientSize( &cw
, &ch
);
2157 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2158 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2161 // logical bounds of update region
2164 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2165 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2167 // find the row labels within these bounds
2171 for ( row
= 0; row
< m_numRows
; row
++ )
2173 if ( m_rowBottoms
[row
] < top
) continue;
2175 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2176 if ( rowTop
> bottom
) break;
2178 m_rowLabelsExposed
.Add( row
);
2186 void wxGrid
::CalcColLabelsExposed( wxRegion
& reg
)
2188 wxRegionIterator
iter( reg
);
2191 m_colLabelsExposed
.Empty();
2198 // TODO: remove this when we can...
2199 // There is a bug in wxMotif that gives garbage update
2200 // rectangles if you jump-scroll a long way by clicking the
2201 // scrollbar with middle button. This is a work-around
2203 #if defined(__WXMOTIF__)
2205 m_gridWin
->GetClientSize( &cw
, &ch
);
2206 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2207 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2210 // logical bounds of update region
2213 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2214 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2216 // find the cells within these bounds
2220 for ( col
= 0; col
< m_numCols
; col
++ )
2222 if ( m_colRights
[col
] < left
) continue;
2224 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2225 if ( colLeft
> right
) break;
2227 m_colLabelsExposed
.Add( col
);
2235 void wxGrid
::CalcCellsExposed( wxRegion
& reg
)
2237 wxRegionIterator
iter( reg
);
2240 m_cellsExposed
.Empty();
2241 m_rowsExposed
.Empty();
2242 m_colsExposed
.Empty();
2244 int left
, top
, right
, bottom
;
2249 // TODO: remove this when we can...
2250 // There is a bug in wxMotif that gives garbage update
2251 // rectangles if you jump-scroll a long way by clicking the
2252 // scrollbar with middle button. This is a work-around
2254 #if defined(__WXMOTIF__)
2256 m_gridWin
->GetClientSize( &cw
, &ch
);
2257 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2258 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2259 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2260 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2263 // logical bounds of update region
2265 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2266 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2268 // find the cells within these bounds
2271 int colLeft
, rowTop
;
2272 for ( row
= 0; row
< m_numRows
; row
++ )
2274 if ( m_rowBottoms
[row
] < top
) continue;
2276 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
2277 if ( rowTop
> bottom
) break;
2279 m_rowsExposed
.Add( row
);
2281 for ( col
= 0; col
< m_numCols
; col
++ )
2283 if ( m_colRights
[col
] < left
) continue;
2285 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
2286 if ( colLeft
> right
) break;
2288 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
2289 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
2298 void wxGrid
::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2301 wxPoint
pos( event
.GetPosition() );
2302 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2304 if ( event
.Dragging() )
2306 m_isDragging
= TRUE
;
2308 if ( event
.LeftIsDown() )
2310 switch( m_cursorMode
)
2312 case WXGRID_CURSOR_RESIZE_ROW
:
2314 int cw
, ch
, left
, dummy
;
2315 m_gridWin
->GetClientSize( &cw
, &ch
);
2316 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2318 wxClientDC
dc( m_gridWin
);
2320 dc
.SetLogicalFunction(wxINVERT
);
2321 if ( m_dragLastPos
>= 0 )
2323 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2325 dc
.DrawLine( left
, y
, left
+cw
, y
);
2330 case WXGRID_CURSOR_SELECT_ROW
:
2331 if ( (row
= YToRow( y
)) >= 0 &&
2332 !IsInSelection( row
, 0 ) )
2334 SelectRow( row
, TRUE
);
2337 // default label to suppress warnings about "enumeration value
2338 // 'xxx' not handled in switch
2346 m_isDragging
= FALSE
;
2349 // ------------ Entering or leaving the window
2351 if ( event
.Entering() || event
.Leaving() )
2353 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2357 // ------------ Left button pressed
2359 else if ( event
.LeftDown() )
2361 // don't send a label click event for a hit on the
2362 // edge of the row label - this is probably the user
2363 // wanting to resize the row
2365 if ( YToEdgeOfRow(y
) < 0 )
2369 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2371 SelectRow( row
, event
.ShiftDown() );
2372 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2377 // starting to drag-resize a row
2379 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2384 // ------------ Left double click
2386 else if (event
.LeftDClick() )
2388 if ( YToEdgeOfRow(y
) < 0 )
2391 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2396 // ------------ Left button released
2398 else if ( event
.LeftUp() )
2400 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2402 DoEndDragResizeRow();
2404 // Note: we are ending the event *after* doing
2405 // default processing in this case
2407 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2410 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2415 // ------------ Right button down
2417 else if ( event
.RightDown() )
2420 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2422 // no default action at the moment
2427 // ------------ Right double click
2429 else if ( event
.RightDClick() )
2432 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2434 // no default action at the moment
2439 // ------------ No buttons down and mouse moving
2441 else if ( event
.Moving() )
2443 m_dragRowOrCol
= YToEdgeOfRow( y
);
2444 if ( m_dragRowOrCol
>= 0 )
2446 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2448 // don't capture the mouse yet
2449 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2452 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2454 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2460 void wxGrid
::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2463 wxPoint
pos( event
.GetPosition() );
2464 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2466 if ( event
.Dragging() )
2468 m_isDragging
= TRUE
;
2470 if ( event
.LeftIsDown() )
2472 switch( m_cursorMode
)
2474 case WXGRID_CURSOR_RESIZE_COL
:
2476 int cw
, ch
, dummy
, top
;
2477 m_gridWin
->GetClientSize( &cw
, &ch
);
2478 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2480 wxClientDC
dc( m_gridWin
);
2482 dc
.SetLogicalFunction(wxINVERT
);
2483 if ( m_dragLastPos
>= 0 )
2485 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2487 dc
.DrawLine( x
, top
, x
, top
+ch
);
2492 case WXGRID_CURSOR_SELECT_COL
:
2493 if ( (col
= XToCol( x
)) >= 0 &&
2494 !IsInSelection( 0, col
) )
2496 SelectCol( col
, TRUE
);
2499 // default label to suppress warnings about "enumeration value
2500 // 'xxx' not handled in switch
2508 m_isDragging
= FALSE
;
2511 // ------------ Entering or leaving the window
2513 if ( event
.Entering() || event
.Leaving() )
2515 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2519 // ------------ Left button pressed
2521 else if ( event
.LeftDown() )
2523 // don't send a label click event for a hit on the
2524 // edge of the col label - this is probably the user
2525 // wanting to resize the col
2527 if ( XToEdgeOfCol(x
) < 0 )
2531 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2533 SelectCol( col
, event
.ShiftDown() );
2534 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2539 // starting to drag-resize a col
2541 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2546 // ------------ Left double click
2548 if ( event
.LeftDClick() )
2550 if ( XToEdgeOfCol(x
) < 0 )
2553 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2558 // ------------ Left button released
2560 else if ( event
.LeftUp() )
2562 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2564 DoEndDragResizeCol();
2566 // Note: we are ending the event *after* doing
2567 // default processing in this case
2569 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2572 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2577 // ------------ Right button down
2579 else if ( event
.RightDown() )
2582 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2584 // no default action at the moment
2589 // ------------ Right double click
2591 else if ( event
.RightDClick() )
2594 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2596 // no default action at the moment
2601 // ------------ No buttons down and mouse moving
2603 else if ( event
.Moving() )
2605 m_dragRowOrCol
= XToEdgeOfCol( x
);
2606 if ( m_dragRowOrCol
>= 0 )
2608 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2610 // don't capture the cursor yet
2611 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2614 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2616 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2622 void wxGrid
::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2624 if ( event
.LeftDown() )
2626 // indicate corner label by having both row and
2629 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2635 else if ( event
.LeftDClick() )
2637 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2640 else if ( event
.RightDown() )
2642 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2644 // no default action at the moment
2648 else if ( event
.RightDClick() )
2650 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2652 // no default action at the moment
2657 void wxGrid
::ChangeCursorMode(CursorMode mode
,
2662 static const wxChar
*cursorModes
[] =
2671 wxLogTrace(_T("grid"),
2672 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2673 win
== m_colLabelWin ?
_T("colLabelWin")
2674 : win ?
_T("rowLabelWin")
2676 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2677 #endif // __WXDEBUG__
2679 if ( mode
== m_cursorMode
)
2684 // by default use the grid itself
2690 m_winCapture
->ReleaseMouse();
2691 m_winCapture
= (wxWindow
*)NULL
;
2694 m_cursorMode
= mode
;
2696 switch ( m_cursorMode
)
2698 case WXGRID_CURSOR_RESIZE_ROW
:
2699 win
->SetCursor( m_rowResizeCursor
);
2702 case WXGRID_CURSOR_RESIZE_COL
:
2703 win
->SetCursor( m_colResizeCursor
);
2707 win
->SetCursor( *wxSTANDARD_CURSOR
);
2710 // we need to capture mouse when resizing
2711 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2712 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2714 if ( captureMouse
&& resize
)
2716 win
->CaptureMouse();
2721 void wxGrid
::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2724 wxPoint
pos( event
.GetPosition() );
2725 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2727 wxGridCellCoords coords
;
2728 XYToCell( x
, y
, coords
);
2730 if ( event
.Dragging() )
2732 m_isDragging
= TRUE
;
2733 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2735 // Hide the edit control, so it
2736 // won't interfer with drag-shrinking.
2737 if ( IsCellEditControlEnabled() )
2738 HideCellEditControl();
2739 if ( coords
!= wxGridNoCellCoords
)
2741 if ( !IsSelection() )
2743 SelectBlock( coords
, coords
);
2747 SelectBlock( m_currentCellCoords
, coords
);
2751 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2753 int cw
, ch
, left
, dummy
;
2754 m_gridWin
->GetClientSize( &cw
, &ch
);
2755 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2757 wxClientDC
dc( m_gridWin
);
2759 dc
.SetLogicalFunction(wxINVERT
);
2760 if ( m_dragLastPos
>= 0 )
2762 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2764 dc
.DrawLine( left
, y
, left
+cw
, y
);
2767 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2769 int cw
, ch
, dummy
, top
;
2770 m_gridWin
->GetClientSize( &cw
, &ch
);
2771 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2773 wxClientDC
dc( m_gridWin
);
2775 dc
.SetLogicalFunction(wxINVERT
);
2776 if ( m_dragLastPos
>= 0 )
2778 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2780 dc
.DrawLine( x
, top
, x
, top
+ch
);
2787 m_isDragging
= FALSE
;
2789 if ( coords
!= wxGridNoCellCoords
)
2791 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2792 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2795 if ( event
.Entering() || event
.Leaving() )
2797 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2798 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2803 // ------------ Left button pressed
2805 if ( event
.LeftDown() )
2807 if ( event
.AltDown() )
2809 MakeCellVisible( coords
);
2810 SetCurrentCell( coords
);
2811 EnableCellEditControl( TRUE
);
2815 EnableCellEditControl( FALSE
);
2817 if ( event
.ShiftDown() )
2819 SelectBlock( m_currentCellCoords
, coords
);
2821 else if ( XToEdgeOfCol(x
) < 0 &&
2822 YToEdgeOfRow(y
) < 0 )
2824 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2829 MakeCellVisible( coords
);
2830 SetCurrentCell( coords
);
2836 // ------------ Left double click
2838 else if ( event
.LeftDClick() )
2840 EnableCellEditControl( FALSE
);
2841 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2843 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2851 // ------------ Left button released
2853 else if ( event
.LeftUp() )
2855 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2857 if ( IsSelection() )
2859 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
2862 // Show the edit control, if it has
2863 // been hidden for drag-shrinking.
2864 if ( IsCellEditControlEnabled() )
2865 ShowCellEditControl();
2867 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2869 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2870 DoEndDragResizeRow();
2872 // Note: we are ending the event *after* doing
2873 // default processing in this case
2875 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2877 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2879 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2880 DoEndDragResizeCol();
2882 // Note: we are ending the event *after* doing
2883 // default processing in this case
2885 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2892 // ------------ Right button down
2894 else if ( event
.RightDown() )
2896 EnableCellEditControl( FALSE
);
2897 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2902 // no default action at the moment
2907 // ------------ Right double click
2909 else if ( event
.RightDClick() )
2911 EnableCellEditControl( FALSE
);
2912 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2917 // no default action at the moment
2921 // ------------ Moving and no button action
2923 else if ( event
.Moving() && !event
.IsButton() )
2925 int dragRow
= YToEdgeOfRow( y
);
2926 int dragCol
= XToEdgeOfCol( x
);
2928 // Dragging on the corner of a cell to resize in both
2929 // directions is not implemented yet...
2931 if ( dragRow
>= 0 && dragCol
>= 0 )
2933 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2939 m_dragRowOrCol
= dragRow
;
2941 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2943 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
2951 m_dragRowOrCol
= dragCol
;
2953 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2955 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
2961 // Neither on a row or col edge
2963 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2965 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2972 void wxGrid
::DoEndDragResizeRow()
2974 if ( m_dragLastPos
>= 0 )
2976 // erase the last line and resize the row
2978 int cw
, ch
, left
, dummy
;
2979 m_gridWin
->GetClientSize( &cw
, &ch
);
2980 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2982 wxClientDC
dc( m_gridWin
);
2984 dc
.SetLogicalFunction( wxINVERT
);
2985 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2986 HideCellEditControl();
2988 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
2989 SetRowSize( m_dragRowOrCol
,
2990 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
2992 if ( !GetBatchCount() )
2994 // Only needed to get the correct rect.y:
2995 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
2997 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
2998 rect
.width
= m_rowLabelWidth
;
2999 rect
.height
= ch
- rect
.y
;
3000 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3002 m_gridWin
->Refresh( FALSE
, &rect
);
3005 ShowCellEditControl();
3010 void wxGrid
::DoEndDragResizeCol()
3012 if ( m_dragLastPos
>= 0 )
3014 // erase the last line and resize the col
3016 int cw
, ch
, dummy
, top
;
3017 m_gridWin
->GetClientSize( &cw
, &ch
);
3018 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3020 wxClientDC
dc( m_gridWin
);
3022 dc
.SetLogicalFunction( wxINVERT
);
3023 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3024 HideCellEditControl();
3026 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
3027 SetColSize( m_dragRowOrCol
,
3028 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
3030 if ( !GetBatchCount() )
3032 // Only needed to get the correct rect.x:
3033 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
3035 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
3036 rect
.width
= cw
- rect
.x
;
3037 rect
.height
= m_colLabelHeight
;
3038 m_colLabelWin
->Refresh( TRUE
, &rect
);
3040 m_gridWin
->Refresh( FALSE
, &rect
);
3043 ShowCellEditControl();
3050 // ------ interaction with data model
3052 bool wxGrid
::ProcessTableMessage( wxGridTableMessage
& msg
)
3054 switch ( msg
.GetId() )
3056 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
3057 return GetModelValues();
3059 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
3060 return SetModelValues();
3062 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3063 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3064 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3065 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3066 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3067 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3068 return Redimension( msg
);
3077 // The behaviour of this function depends on the grid table class
3078 // Clear() function. For the default wxGridStringTable class the
3079 // behavious is to replace all cell contents with wxEmptyString but
3080 // not to change the number of rows or cols.
3082 void wxGrid
::ClearGrid()
3087 SetEditControlValue();
3088 if ( !GetBatchCount() ) m_gridWin
->Refresh();
3093 bool wxGrid
::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3095 // TODO: something with updateLabels flag
3099 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
3105 bool ok
= m_table
->InsertRows( pos
, numRows
);
3107 // the table will have sent the results of the insert row
3108 // operation to this view object as a grid table message
3112 if ( m_numCols
== 0 )
3114 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
3116 // TODO: perhaps instead of appending the default number of cols
3117 // we should remember what the last non-zero number of cols was ?
3121 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3123 // if we have just inserted cols into an empty grid the current
3124 // cell will be undefined...
3126 SetCurrentCell( 0, 0 );
3130 if ( !GetBatchCount() ) Refresh();
3133 SetEditControlValue();
3143 bool wxGrid
::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
3145 // TODO: something with updateLabels flag
3149 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
3153 if ( m_table
&& m_table
->AppendRows( numRows
) )
3155 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3157 // if we have just inserted cols into an empty grid the current
3158 // cell will be undefined...
3160 SetCurrentCell( 0, 0 );
3163 // the table will have sent the results of the append row
3164 // operation to this view object as a grid table message
3167 if ( !GetBatchCount() ) Refresh();
3177 bool wxGrid
::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
3179 // TODO: something with updateLabels flag
3183 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
3187 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
3189 // the table will have sent the results of the delete row
3190 // operation to this view object as a grid table message
3192 if ( m_numRows
> 0 )
3193 SetEditControlValue();
3195 HideCellEditControl();
3198 if ( !GetBatchCount() ) Refresh();
3208 bool wxGrid
::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3210 // TODO: something with updateLabels flag
3214 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
3220 HideCellEditControl();
3221 bool ok
= m_table
->InsertCols( pos
, numCols
);
3223 // the table will have sent the results of the insert col
3224 // operation to this view object as a grid table message
3228 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3230 // if we have just inserted cols into an empty grid the current
3231 // cell will be undefined...
3233 SetCurrentCell( 0, 0 );
3237 if ( !GetBatchCount() ) Refresh();
3240 SetEditControlValue();
3250 bool wxGrid
::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
3252 // TODO: something with updateLabels flag
3256 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
3260 if ( m_table
&& m_table
->AppendCols( numCols
) )
3262 // the table will have sent the results of the append col
3263 // operation to this view object as a grid table message
3265 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3267 // if we have just inserted cols into an empty grid the current
3268 // cell will be undefined...
3270 SetCurrentCell( 0, 0 );
3274 if ( !GetBatchCount() ) Refresh();
3284 bool wxGrid
::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
3286 // TODO: something with updateLabels flag
3290 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
3294 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
3296 // the table will have sent the results of the delete col
3297 // operation to this view object as a grid table message
3299 if ( m_numCols
> 0 )
3300 SetEditControlValue();
3302 HideCellEditControl();
3305 if ( !GetBatchCount() ) Refresh();
3317 // ----- event handlers
3320 // Generate a grid event based on a mouse event and
3321 // return the result of ProcessEvent()
3323 bool wxGrid
::SendEvent( const wxEventType type
,
3325 wxMouseEvent
& mouseEv
)
3327 if ( type
== EVT_GRID_ROW_SIZE
||
3328 type
== EVT_GRID_COL_SIZE
)
3330 int rowOrCol
= (row
== -1 ? col
: row
);
3332 wxGridSizeEvent
gridEvt( GetId(),
3336 mouseEv
.GetX(), mouseEv
.GetY(),
3337 mouseEv
.ControlDown(),
3338 mouseEv
.ShiftDown(),
3340 mouseEv
.MetaDown() );
3342 return GetEventHandler()->ProcessEvent(gridEvt
);
3344 else if ( type
== EVT_GRID_RANGE_SELECT
)
3346 wxGridRangeSelectEvent
gridEvt( GetId(),
3350 m_selectedBottomRight
,
3351 mouseEv
.ControlDown(),
3352 mouseEv
.ShiftDown(),
3354 mouseEv
.MetaDown() );
3356 return GetEventHandler()->ProcessEvent(gridEvt
);
3360 wxGridEvent
gridEvt( GetId(),
3364 mouseEv
.GetX(), mouseEv
.GetY(),
3365 mouseEv
.ControlDown(),
3366 mouseEv
.ShiftDown(),
3368 mouseEv
.MetaDown() );
3370 return GetEventHandler()->ProcessEvent(gridEvt
);
3375 // Generate a grid event of specified type and return the result
3376 // of ProcessEvent().
3378 bool wxGrid
::SendEvent( const wxEventType type
,
3381 if ( type
== EVT_GRID_ROW_SIZE
||
3382 type
== EVT_GRID_COL_SIZE
)
3384 int rowOrCol
= (row
== -1 ? col
: row
);
3386 wxGridSizeEvent
gridEvt( GetId(),
3391 return GetEventHandler()->ProcessEvent(gridEvt
);
3395 wxGridEvent
gridEvt( GetId(),
3400 return GetEventHandler()->ProcessEvent(gridEvt
);
3405 void wxGrid
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3407 wxPaintDC
dc( this );
3409 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3410 m_numRows
&& m_numCols
)
3412 m_currentCellCoords
.Set(0, 0);
3413 SetEditControlValue();
3414 ShowCellEditControl();
3421 // This is just here to make sure that CalcDimensions gets called when
3422 // the grid view is resized... then the size event is skipped to allow
3423 // the box sizers to handle everything
3425 void wxGrid
::OnSize( wxSizeEvent
& event
)
3432 void wxGrid
::OnKeyDown( wxKeyEvent
& event
)
3434 if ( m_inOnKeyDown
)
3436 // shouldn't be here - we are going round in circles...
3438 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while alread active") );
3441 m_inOnKeyDown
= TRUE
;
3443 // propagate the event up and see if it gets processed
3445 wxWindow
*parent
= GetParent();
3446 wxKeyEvent
keyEvt( event
);
3447 keyEvt
.SetEventObject( parent
);
3449 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3451 // try local handlers
3453 switch ( event
.KeyCode() )
3456 if ( event
.ControlDown() )
3458 MoveCursorUpBlock();
3467 if ( event
.ControlDown() )
3469 MoveCursorDownBlock();
3478 if ( event
.ControlDown() )
3480 MoveCursorLeftBlock();
3489 if ( event
.ControlDown() )
3491 MoveCursorRightBlock();
3500 if ( !IsEditable() )
3511 if ( event
.ControlDown() )
3513 event
.Skip(); // to let the edit control have the return
3522 if ( event
.ControlDown() )
3524 MakeCellVisible( 0, 0 );
3525 SetCurrentCell( 0, 0 );
3534 if ( event
.ControlDown() )
3536 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3537 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3554 // now try the cell edit control
3556 if ( !IsCellEditControlEnabled() )
3557 EnableCellEditControl( TRUE
);
3558 if ( IsCellEditControlEnabled() )
3560 event
.SetEventObject( m_cellEditCtrl
);
3561 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
3567 m_inOnKeyDown
= FALSE
;
3570 void wxGrid
::OnEraseBackground(wxEraseEvent
&)
3573 void wxGrid
::SetCurrentCell( const wxGridCellCoords
& coords
)
3575 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3577 // the event has been intercepted - do nothing
3582 m_currentCellCoords
!= wxGridNoCellCoords
)
3584 HideCellEditControl();
3585 SaveEditControlValue();
3588 m_currentCellCoords
= coords
;
3590 SetEditControlValue();
3594 ShowCellEditControl();
3596 if ( IsSelection() )
3598 wxRect
r( SelectionToDeviceRect() );
3600 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3604 SelectBlock ( coords
, coords
);
3610 // ------ functions to get/send data (see also public functions)
3613 bool wxGrid
::GetModelValues()
3617 // all we need to do is repaint the grid
3619 m_gridWin
->Refresh();
3627 bool wxGrid
::SetModelValues()
3633 for ( row
= 0; row
< m_numRows
; row
++ )
3635 for ( col
= 0; col
< m_numCols
; col
++ )
3637 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3649 // Note - this function only draws cells that are in the list of
3650 // exposed cells (usually set from the update region by
3651 // CalcExposedCells)
3653 void wxGrid
::DrawGridCellArea( wxDC
& dc
)
3655 if ( !m_numRows
|| !m_numCols
) return;
3658 size_t numCells
= m_cellsExposed
.GetCount();
3660 for ( i
= 0; i
< numCells
; i
++ )
3662 DrawCell( dc
, m_cellsExposed
[i
] );
3667 void wxGrid
::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3669 int row
= coords
.GetRow();
3670 int col
= coords
.GetCol();
3672 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3675 // we draw the cell border ourselves
3676 #if !WXGRID_DRAW_LINES
3677 if ( m_gridLinesEnabled
)
3678 DrawCellBorder( dc
, coords
);
3681 // but all the rest is drawn by the cell renderer and hence may be
3684 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3685 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3686 rect
.width
= m_colWidths
[col
] - 1;
3687 rect
.height
= m_rowHeights
[row
] - 1;
3689 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
3690 attr
->GetRenderer()->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
3694 void wxGrid
::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3696 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3697 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3699 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3700 int row
= coords
.GetRow();
3701 int col
= coords
.GetCol();
3703 // right hand border
3705 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3706 m_colRights
[col
], m_rowBottoms
[row
] );
3710 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3711 m_colRights
[col
], m_rowBottoms
[row
] );
3715 // TODO: remove this ???
3716 // This is used to redraw all grid lines e.g. when the grid line colour
3719 void wxGrid
::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3721 if ( !m_gridLinesEnabled
||
3723 !m_numCols
) return;
3725 int top
, bottom
, left
, right
;
3729 m_gridWin
->GetClientSize(&cw
, &ch
);
3731 // virtual coords of visible area
3733 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3734 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3738 reg
.GetBox(x
, y
, w
, h
);
3739 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3740 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3743 // avoid drawing grid lines past the last row and col
3745 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3746 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3748 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3750 // horizontal grid lines
3753 for ( i
= 0; i
< m_numRows
; i
++ )
3755 if ( m_rowBottoms
[i
]-1 > bottom
)
3759 else if ( m_rowBottoms
[i
]-1 >= top
)
3761 dc
.DrawLine( left
, m_rowBottoms
[i
]-1, right
, m_rowBottoms
[i
]-1 );
3766 // vertical grid lines
3768 for ( i
= 0; i
< m_numCols
; i
++ )
3770 if ( m_colRights
[i
]-1 > right
)
3774 else if ( m_colRights
[i
]-1 >= left
)
3776 dc
.DrawLine( m_colRights
[i
]-1, top
, m_colRights
[i
]-1, bottom
);
3782 void wxGrid
::DrawRowLabels( wxDC
& dc
)
3784 if ( !m_numRows
|| !m_numCols
) return;
3787 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3789 for ( i
= 0; i
< numLabels
; i
++ )
3791 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3796 void wxGrid
::DrawRowLabel( wxDC
& dc
, int row
)
3798 if ( m_rowHeights
[row
] <= 0 ) return;
3800 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3802 dc
.SetPen( *wxBLACK_PEN
);
3803 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3804 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3806 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3807 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3809 dc
.SetPen( *wxWHITE_PEN
);
3810 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
3811 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
3813 dc
.SetBackgroundMode( wxTRANSPARENT
);
3814 dc
.SetTextForeground( GetLabelTextColour() );
3815 dc
.SetFont( GetLabelFont() );
3818 GetRowLabelAlignment( &hAlign
, &vAlign
);
3822 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3823 rect
.SetWidth( m_rowLabelWidth
- 4 );
3824 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3825 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
3829 void wxGrid
::DrawColLabels( wxDC
& dc
)
3831 if ( !m_numRows
|| !m_numCols
) return;
3834 size_t numLabels
= m_colLabelsExposed
.GetCount();
3836 for ( i
= 0; i
< numLabels
; i
++ )
3838 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
3843 void wxGrid
::DrawColLabel( wxDC
& dc
, int col
)
3845 if ( m_colWidths
[col
] <= 0 ) return;
3847 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
3849 dc
.SetPen( *wxBLACK_PEN
);
3850 dc
.DrawLine( m_colRights
[col
]-1, 0,
3851 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3853 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
3854 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3856 dc
.SetPen( *wxWHITE_PEN
);
3857 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
3858 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
3860 dc
.SetBackgroundMode( wxTRANSPARENT
);
3861 dc
.SetTextForeground( GetLabelTextColour() );
3862 dc
.SetFont( GetLabelFont() );
3864 dc
.SetBackgroundMode( wxTRANSPARENT
);
3865 dc
.SetTextForeground( GetLabelTextColour() );
3866 dc
.SetFont( GetLabelFont() );
3869 GetColLabelAlignment( &hAlign
, &vAlign
);
3872 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3874 rect
.SetWidth( m_colWidths
[col
] - 4 );
3875 rect
.SetHeight( m_colLabelHeight
- 4 );
3876 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
3880 void wxGrid
::DrawTextRectangle( wxDC
& dc
,
3881 const wxString
& value
,
3886 long textWidth
, textHeight
;
3887 long lineWidth
, lineHeight
;
3888 wxArrayString lines
;
3890 dc
.SetClippingRegion( rect
);
3891 StringToLines( value
, lines
);
3892 if ( lines
.GetCount() )
3894 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
3895 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
3898 switch ( horizAlign
)
3901 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
3905 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
3914 switch ( vertAlign
)
3917 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
3921 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
3930 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
3932 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
3937 dc
.DestroyClippingRegion();
3941 // Split multi line text up into an array of strings. Any existing
3942 // contents of the string array are preserved.
3944 void wxGrid
::StringToLines( const wxString
& value
, wxArrayString
& lines
)
3948 wxString eol
= wxTextFile
::GetEOL( wxTextFileType_Unix
);
3949 wxString tVal
= wxTextFile
::Translate( value
, wxTextFileType_Unix
);
3951 while ( startPos
< (int)tVal
.Length() )
3953 pos
= tVal
.Mid(startPos
).Find( eol
);
3958 else if ( pos
== 0 )
3960 lines
.Add( wxEmptyString
);
3964 lines
.Add( value
.Mid(startPos
, pos
) );
3968 if ( startPos
< (int)value
.Length() )
3970 lines
.Add( value
.Mid( startPos
) );
3975 void wxGrid
::GetTextBoxSize( wxDC
& dc
,
3976 wxArrayString
& lines
,
3977 long *width
, long *height
)
3984 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3986 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3987 w
= wxMax( w
, lineW
);
3997 // ------ Edit control functions
4001 void wxGrid
::EnableEditing( bool edit
)
4003 // TODO: improve this ?
4005 if ( edit
!= m_editable
)
4009 // TODO: extend this for other edit control types
4011 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
4013 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
4019 void wxGrid
::EnableCellEditControl( bool enable
)
4021 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4022 SetCurrentCell( 0, 0 );
4023 if ( m_cellEditCtrl
&&
4024 enable
!= m_cellEditCtrlEnabled
)
4029 m_cellEditCtrlEnabled
= enable
;
4030 SetEditControlValue();
4031 // requires m_cellEditCtrlEnabled to be already true
4032 ShowCellEditControl();
4036 HideCellEditControl();
4037 // requires m_cellEditCtrlEnabled to be still true
4038 SaveEditControlValue();
4039 m_cellEditCtrlEnabled
= enable
;
4045 void wxGrid
::ShowCellEditControl()
4049 if ( IsCellEditControlEnabled() )
4051 if ( !IsVisible( m_currentCellCoords
) )
4057 rect
= CellToRect( m_currentCellCoords
);
4059 // convert to scrolled coords
4061 int left
, top
, right
, bottom
;
4062 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4063 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4066 m_gridWin
->GetClientSize( &cw
, &ch
);
4068 // Make the edit control large enough to allow for internal margins
4069 // TODO: remove this if the text ctrl sizing is improved esp. for unix
4072 #if defined(__WXMOTIF__)
4073 if ( m_currentCellCoords
.GetRow() == 0 ||
4074 m_currentCellCoords
.GetCol() == 0 )
4083 if ( m_currentCellCoords
.GetRow() == 0 ||
4084 m_currentCellCoords
.GetCol() == 0 )
4094 #if defined(__WXGTK__)
4097 if (left
!= 0) left_diff
++;
4098 if (top
!= 0) top_diff
++;
4099 rect
.SetLeft( left
+ left_diff
);
4100 rect
.SetTop( top
+ top_diff
);
4101 rect
.SetRight( rect
.GetRight() - left_diff
);
4102 rect
.SetBottom( rect
.GetBottom() - top_diff
);
4104 rect
.SetLeft( wxMax(0, left
- extra
) );
4105 rect
.SetTop( wxMax(0, top
- extra
) );
4106 rect
.SetRight( rect
.GetRight() + 2*extra
);
4107 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
4110 m_cellEditCtrl
->SetSize( rect
);
4111 m_cellEditCtrl
->Show( TRUE
);
4113 switch ( m_editCtrlType
)
4115 case wxGRID_TEXTCTRL
:
4116 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
4119 case wxGRID_CHECKBOX
:
4120 // TODO: anything ???
4125 // TODO: anything ???
4129 case wxGRID_COMBOBOX
:
4130 // TODO: anything ???
4135 m_cellEditCtrl
->SetFocus();
4141 void wxGrid
::HideCellEditControl()
4143 if ( IsCellEditControlEnabled() )
4145 m_cellEditCtrl
->Show( FALSE
);
4151 void wxGrid
::SetEditControlValue( const wxString
& value
)
4157 s
= GetCellValue(m_currentCellCoords
);
4161 if ( IsCellEditControlEnabled() )
4163 switch ( m_editCtrlType
)
4165 case wxGRID_TEXTCTRL
:
4166 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
4169 case wxGRID_CHECKBOX
:
4170 // TODO: implement this
4175 // TODO: implement this
4179 case wxGRID_COMBOBOX
:
4180 // TODO: implement this
4189 void wxGrid
::SaveEditControlValue()
4193 wxWindow
*ctrl
= (wxWindow
*)NULL
;
4195 if ( IsCellEditControlEnabled() )
4197 ctrl
= m_cellEditCtrl
;
4204 bool valueChanged
= FALSE
;
4206 switch ( m_editCtrlType
)
4208 case wxGRID_TEXTCTRL
:
4209 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
4210 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
4211 SetCellValue( m_currentCellCoords
,
4212 ((wxTextCtrl
*) ctrl
)->GetValue() );
4215 case wxGRID_CHECKBOX
:
4216 // TODO: implement this
4221 // TODO: implement this
4225 case wxGRID_COMBOBOX
:
4226 // TODO: implement this
4233 SendEvent( EVT_GRID_CELL_CHANGE
,
4234 m_currentCellCoords
.GetRow(),
4235 m_currentCellCoords
.GetCol() );
4242 // ------ Grid location functions
4243 // Note that all of these functions work with the logical coordinates of
4244 // grid cells and labels so you will need to convert from device
4245 // coordinates for mouse events etc.
4248 void wxGrid
::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
4250 int row
= YToRow(y
);
4251 int col
= XToCol(x
);
4253 if ( row
== -1 || col
== -1 )
4255 coords
= wxGridNoCellCoords
;
4259 coords
.Set( row
, col
);
4264 int wxGrid
::YToRow( int y
)
4268 for ( i
= 0; i
< m_numRows
; i
++ )
4270 if ( y
< m_rowBottoms
[i
] ) return i
;
4277 int wxGrid
::XToCol( int x
)
4281 for ( i
= 0; i
< m_numCols
; i
++ )
4283 if ( x
< m_colRights
[i
] ) return i
;
4290 // return the row number that that the y coord is near the edge of, or
4291 // -1 if not near an edge
4293 int wxGrid
::YToEdgeOfRow( int y
)
4297 for ( i
= 0; i
< m_numRows
; i
++ )
4299 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4301 d
= abs( y
- m_rowBottoms
[i
] );
4303 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4312 // return the col number that that the x coord is near the edge of, or
4313 // -1 if not near an edge
4315 int wxGrid
::XToEdgeOfCol( int x
)
4319 for ( i
= 0; i
< m_numCols
; i
++ )
4321 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
4323 d
= abs( x
- m_colRights
[i
] );
4325 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4334 wxRect wxGrid
::CellToRect( int row
, int col
)
4336 wxRect
rect( -1, -1, -1, -1 );
4338 if ( row
>= 0 && row
< m_numRows
&&
4339 col
>= 0 && col
< m_numCols
)
4341 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4342 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4343 rect
.width
= m_colWidths
[col
];
4344 rect
.height
= m_rowHeights
[ row
];
4351 bool wxGrid
::IsVisible( int row
, int col
, bool wholeCellVisible
)
4353 // get the cell rectangle in logical coords
4355 wxRect
r( CellToRect( row
, col
) );
4357 // convert to device coords
4359 int left
, top
, right
, bottom
;
4360 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4361 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4363 // check against the client area of the grid window
4366 m_gridWin
->GetClientSize( &cw
, &ch
);
4368 if ( wholeCellVisible
)
4370 // is the cell wholly visible ?
4372 return ( left
>= 0 && right
<= cw
&&
4373 top
>= 0 && bottom
<= ch
);
4377 // is the cell partly visible ?
4379 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4380 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4385 // make the specified cell location visible by doing a minimal amount
4388 void wxGrid
::MakeCellVisible( int row
, int col
)
4391 int xpos
= -1, ypos
= -1;
4393 if ( row
>= 0 && row
< m_numRows
&&
4394 col
>= 0 && col
< m_numCols
)
4396 // get the cell rectangle in logical coords
4398 wxRect
r( CellToRect( row
, col
) );
4400 // convert to device coords
4402 int left
, top
, right
, bottom
;
4403 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4404 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4407 m_gridWin
->GetClientSize( &cw
, &ch
);
4413 else if ( bottom
> ch
)
4415 int h
= r
.GetHeight();
4417 for ( i
= row
-1; i
>= 0; i
-- )
4419 if ( h
+ m_rowHeights
[i
] > ch
) break;
4421 h
+= m_rowHeights
[i
];
4422 ypos
-= m_rowHeights
[i
];
4425 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4426 // have rounding errors (this is important, because if we do, we
4427 // might not scroll at all and some cells won't be redrawn)
4428 ypos
+= GRID_SCROLL_LINE
/ 2;
4435 else if ( right
> cw
)
4437 int w
= r
.GetWidth();
4439 for ( i
= col
-1; i
>= 0; i
-- )
4441 if ( w
+ m_colWidths
[i
] > cw
) break;
4443 w
+= m_colWidths
[i
];
4444 xpos
-= m_colWidths
[i
];
4447 // see comment for ypos above
4448 xpos
+= GRID_SCROLL_LINE
/ 2;
4451 if ( xpos
!= -1 || ypos
!= -1 )
4453 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4454 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4455 Scroll( xpos
, ypos
);
4463 // ------ Grid cursor movement functions
4466 bool wxGrid
::MoveCursorUp()
4468 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4469 m_currentCellCoords
.GetRow() > 0 )
4471 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4472 m_currentCellCoords
.GetCol() );
4474 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4475 m_currentCellCoords
.GetCol() );
4484 bool wxGrid
::MoveCursorDown()
4486 // TODO: allow for scrolling
4488 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4489 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4491 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4492 m_currentCellCoords
.GetCol() );
4494 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4495 m_currentCellCoords
.GetCol() );
4504 bool wxGrid
::MoveCursorLeft()
4506 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4507 m_currentCellCoords
.GetCol() > 0 )
4509 MakeCellVisible( m_currentCellCoords
.GetRow(),
4510 m_currentCellCoords
.GetCol() - 1 );
4512 SetCurrentCell( m_currentCellCoords
.GetRow(),
4513 m_currentCellCoords
.GetCol() - 1 );
4522 bool wxGrid
::MoveCursorRight()
4524 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4525 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4527 MakeCellVisible( m_currentCellCoords
.GetRow(),
4528 m_currentCellCoords
.GetCol() + 1 );
4530 SetCurrentCell( m_currentCellCoords
.GetRow(),
4531 m_currentCellCoords
.GetCol() + 1 );
4540 bool wxGrid
::MovePageUp()
4542 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4544 int row
= m_currentCellCoords
.GetRow();
4548 m_gridWin
->GetClientSize( &cw
, &ch
);
4550 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4551 int newRow
= YToRow( y
- ch
+ 1 );
4556 else if ( newRow
== row
)
4561 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4562 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4570 bool wxGrid
::MovePageDown()
4572 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4574 int row
= m_currentCellCoords
.GetRow();
4575 if ( row
< m_numRows
)
4578 m_gridWin
->GetClientSize( &cw
, &ch
);
4580 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4581 int newRow
= YToRow( y
+ ch
);
4584 newRow
= m_numRows
- 1;
4586 else if ( newRow
== row
)
4591 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4592 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4600 bool wxGrid
::MoveCursorUpBlock()
4603 m_currentCellCoords
!= wxGridNoCellCoords
&&
4604 m_currentCellCoords
.GetRow() > 0 )
4606 int row
= m_currentCellCoords
.GetRow();
4607 int col
= m_currentCellCoords
.GetCol();
4609 if ( m_table
->IsEmptyCell(row
, col
) )
4611 // starting in an empty cell: find the next block of
4617 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4620 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4622 // starting at the top of a block: find the next block
4628 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4633 // starting within a block: find the top of the block
4638 if ( m_table
->IsEmptyCell(row
, col
) )
4646 MakeCellVisible( row
, col
);
4647 SetCurrentCell( row
, col
);
4655 bool wxGrid
::MoveCursorDownBlock()
4658 m_currentCellCoords
!= wxGridNoCellCoords
&&
4659 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4661 int row
= m_currentCellCoords
.GetRow();
4662 int col
= m_currentCellCoords
.GetCol();
4664 if ( m_table
->IsEmptyCell(row
, col
) )
4666 // starting in an empty cell: find the next block of
4669 while ( row
< m_numRows
-1 )
4672 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4675 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4677 // starting at the bottom of a block: find the next block
4680 while ( row
< m_numRows
-1 )
4683 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4688 // starting within a block: find the bottom of the block
4690 while ( row
< m_numRows
-1 )
4693 if ( m_table
->IsEmptyCell(row
, col
) )
4701 MakeCellVisible( row
, col
);
4702 SetCurrentCell( row
, col
);
4710 bool wxGrid
::MoveCursorLeftBlock()
4713 m_currentCellCoords
!= wxGridNoCellCoords
&&
4714 m_currentCellCoords
.GetCol() > 0 )
4716 int row
= m_currentCellCoords
.GetRow();
4717 int col
= m_currentCellCoords
.GetCol();
4719 if ( m_table
->IsEmptyCell(row
, col
) )
4721 // starting in an empty cell: find the next block of
4727 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4730 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4732 // starting at the left of a block: find the next block
4738 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4743 // starting within a block: find the left of the block
4748 if ( m_table
->IsEmptyCell(row
, col
) )
4756 MakeCellVisible( row
, col
);
4757 SetCurrentCell( row
, col
);
4765 bool wxGrid
::MoveCursorRightBlock()
4768 m_currentCellCoords
!= wxGridNoCellCoords
&&
4769 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4771 int row
= m_currentCellCoords
.GetRow();
4772 int col
= m_currentCellCoords
.GetCol();
4774 if ( m_table
->IsEmptyCell(row
, col
) )
4776 // starting in an empty cell: find the next block of
4779 while ( col
< m_numCols
-1 )
4782 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4785 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4787 // starting at the right of a block: find the next block
4790 while ( col
< m_numCols
-1 )
4793 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4798 // starting within a block: find the right of the block
4800 while ( col
< m_numCols
-1 )
4803 if ( m_table
->IsEmptyCell(row
, col
) )
4811 MakeCellVisible( row
, col
);
4812 SetCurrentCell( row
, col
);
4823 // ------ Label values and formatting
4826 void wxGrid
::GetRowLabelAlignment( int *horiz
, int *vert
)
4828 *horiz
= m_rowLabelHorizAlign
;
4829 *vert
= m_rowLabelVertAlign
;
4832 void wxGrid
::GetColLabelAlignment( int *horiz
, int *vert
)
4834 *horiz
= m_colLabelHorizAlign
;
4835 *vert
= m_colLabelVertAlign
;
4838 wxString wxGrid
::GetRowLabelValue( int row
)
4842 return m_table
->GetRowLabelValue( row
);
4852 wxString wxGrid
::GetColLabelValue( int col
)
4856 return m_table
->GetColLabelValue( col
);
4867 void wxGrid
::SetRowLabelSize( int width
)
4869 width
= wxMax( width
, 0 );
4870 if ( width
!= m_rowLabelWidth
)
4874 m_rowLabelWin
->Show( FALSE
);
4875 m_cornerLabelWin
->Show( FALSE
);
4877 else if ( m_rowLabelWidth
== 0 )
4879 m_rowLabelWin
->Show( TRUE
);
4880 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4883 m_rowLabelWidth
= width
;
4890 void wxGrid
::SetColLabelSize( int height
)
4892 height
= wxMax( height
, 0 );
4893 if ( height
!= m_colLabelHeight
)
4897 m_colLabelWin
->Show( FALSE
);
4898 m_cornerLabelWin
->Show( FALSE
);
4900 else if ( m_colLabelHeight
== 0 )
4902 m_colLabelWin
->Show( TRUE
);
4903 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4906 m_colLabelHeight
= height
;
4913 void wxGrid
::SetLabelBackgroundColour( const wxColour
& colour
)
4915 if ( m_labelBackgroundColour
!= colour
)
4917 m_labelBackgroundColour
= colour
;
4918 m_rowLabelWin
->SetBackgroundColour( colour
);
4919 m_colLabelWin
->SetBackgroundColour( colour
);
4920 m_cornerLabelWin
->SetBackgroundColour( colour
);
4922 if ( !GetBatchCount() )
4924 m_rowLabelWin
->Refresh();
4925 m_colLabelWin
->Refresh();
4926 m_cornerLabelWin
->Refresh();
4931 void wxGrid
::SetLabelTextColour( const wxColour
& colour
)
4933 if ( m_labelTextColour
!= colour
)
4935 m_labelTextColour
= colour
;
4936 if ( !GetBatchCount() )
4938 m_rowLabelWin
->Refresh();
4939 m_colLabelWin
->Refresh();
4944 void wxGrid
::SetLabelFont( const wxFont
& font
)
4947 if ( !GetBatchCount() )
4949 m_rowLabelWin
->Refresh();
4950 m_colLabelWin
->Refresh();
4954 void wxGrid
::SetRowLabelAlignment( int horiz
, int vert
)
4956 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4958 m_rowLabelHorizAlign
= horiz
;
4961 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4963 m_rowLabelVertAlign
= vert
;
4966 if ( !GetBatchCount() )
4968 m_rowLabelWin
->Refresh();
4972 void wxGrid
::SetColLabelAlignment( int horiz
, int vert
)
4974 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4976 m_colLabelHorizAlign
= horiz
;
4979 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4981 m_colLabelVertAlign
= vert
;
4984 if ( !GetBatchCount() )
4986 m_colLabelWin
->Refresh();
4990 void wxGrid
::SetRowLabelValue( int row
, const wxString
& s
)
4994 m_table
->SetRowLabelValue( row
, s
);
4995 if ( !GetBatchCount() )
4997 wxRect rect
= CellToRect( row
, 0);
4998 if ( rect
.height
> 0 )
5000 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
5002 rect
.width
= m_rowLabelWidth
;
5003 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5009 void wxGrid
::SetColLabelValue( int col
, const wxString
& s
)
5013 m_table
->SetColLabelValue( col
, s
);
5014 if ( !GetBatchCount() )
5016 wxRect rect
= CellToRect( 0, col
);
5017 if ( rect
.width
> 0 )
5019 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
5021 rect
.height
= m_colLabelHeight
;
5022 m_colLabelWin
->Refresh( TRUE
, &rect
);
5028 void wxGrid
::SetGridLineColour( const wxColour
& colour
)
5030 if ( m_gridLineColour
!= colour
)
5032 m_gridLineColour
= colour
;
5034 wxClientDC
dc( m_gridWin
);
5036 DrawAllGridLines( dc
, wxRegion() );
5040 void wxGrid
::EnableGridLines( bool enable
)
5042 if ( enable
!= m_gridLinesEnabled
)
5044 m_gridLinesEnabled
= enable
;
5046 if ( !GetBatchCount() )
5050 wxClientDC
dc( m_gridWin
);
5052 DrawAllGridLines( dc
, wxRegion() );
5056 m_gridWin
->Refresh();
5063 int wxGrid
::GetDefaultRowSize()
5065 return m_defaultRowHeight
;
5068 int wxGrid
::GetRowSize( int row
)
5070 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
5072 return m_rowHeights
[row
];
5075 int wxGrid
::GetDefaultColSize()
5077 return m_defaultColWidth
;
5080 int wxGrid
::GetColSize( int col
)
5082 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
5084 return m_colWidths
[col
];
5087 // ============================================================================
5088 // access to the grid attributes: each of them has a default value in the grid
5089 // itself and may be overidden on a per-cell basis
5090 // ============================================================================
5092 // ----------------------------------------------------------------------------
5093 // setting default attributes
5094 // ----------------------------------------------------------------------------
5096 void wxGrid
::SetDefaultCellBackgroundColour( const wxColour
& col
)
5098 m_defaultCellAttr
->SetBackgroundColour(col
);
5101 void wxGrid
::SetDefaultCellTextColour( const wxColour
& col
)
5103 m_defaultCellAttr
->SetTextColour(col
);
5106 void wxGrid
::SetDefaultCellAlignment( int horiz
, int vert
)
5108 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
5111 void wxGrid
::SetDefaultCellFont( const wxFont
& font
)
5113 m_defaultCellAttr
->SetFont(font
);
5116 void wxGrid
::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
5118 m_defaultCellAttr
->SetRenderer(renderer
);
5121 // ----------------------------------------------------------------------------
5122 // access to the default attrbiutes
5123 // ----------------------------------------------------------------------------
5125 wxColour wxGrid
::GetDefaultCellBackgroundColour()
5127 return m_defaultCellAttr
->GetBackgroundColour();
5130 wxColour wxGrid
::GetDefaultCellTextColour()
5132 return m_defaultCellAttr
->GetTextColour();
5135 wxFont wxGrid
::GetDefaultCellFont()
5137 return m_defaultCellAttr
->GetFont();
5140 void wxGrid
::GetDefaultCellAlignment( int *horiz
, int *vert
)
5142 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
5145 wxGridCellRenderer
*wxGrid
::GetDefaultRenderer() const
5147 return m_defaultCellAttr
->GetRenderer();
5150 // ----------------------------------------------------------------------------
5151 // access to cell attributes
5152 // ----------------------------------------------------------------------------
5154 wxColour wxGrid
::GetCellBackgroundColour(int row
, int col
)
5156 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5157 wxColour colour
= attr
->GetBackgroundColour();
5162 wxColour wxGrid
::GetCellTextColour( int row
, int col
)
5164 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5165 wxColour colour
= attr
->GetTextColour();
5170 wxFont wxGrid
::GetCellFont( int row
, int col
)
5172 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5173 wxFont font
= attr
->GetFont();
5178 void wxGrid
::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
5180 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5181 attr
->GetAlignment(horiz
, vert
);
5185 wxGridCellRenderer
* wxGrid
::GetCellRenderer(int row
, int col
)
5187 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5188 wxGridCellRenderer
* renderer
= attr
->GetRenderer();
5193 // ----------------------------------------------------------------------------
5194 // attribute support: cache, automatic provider creation, ...
5195 // ----------------------------------------------------------------------------
5197 bool wxGrid
::CanHaveAttributes()
5204 // RD: Maybe m_table->CanHaveAttributes() would be better in case the
5205 // table is providing the attributes itself??? In which case
5206 // I don't think the grid should create a Provider object for the
5207 // table but the table should be smart enough to do that on its own.
5208 if ( !m_table
->GetAttrProvider() )
5210 // use the default attr provider by default
5211 // (another choice would be to just return FALSE thus forcing the user
5213 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
5219 void wxGrid
::ClearAttrCache()
5221 if ( m_attrCache
.row
!= -1 )
5223 m_attrCache
.attr
->SafeDecRef();
5224 m_attrCache
.row
= -1;
5228 void wxGrid
::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
5230 wxGrid
*self
= (wxGrid
*)this; // const_cast
5232 self
->ClearAttrCache();
5233 self
->m_attrCache
.row
= row
;
5234 self
->m_attrCache
.col
= col
;
5235 self
->m_attrCache
.attr
= attr
;
5239 bool wxGrid
::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
5241 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
5243 *attr
= m_attrCache
.attr
;
5244 (*attr
)->SafeIncRef();
5246 #ifdef DEBUG_ATTR_CACHE
5247 gs_nAttrCacheHits
++;
5254 #ifdef DEBUG_ATTR_CACHE
5255 gs_nAttrCacheMisses
++;
5261 wxGridCellAttr
*wxGrid
::GetCellAttr(int row
, int col
) const
5263 wxGridCellAttr
*attr
;
5264 if ( !LookupAttr(row
, col
, &attr
) )
5266 attr
= m_table ? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
5267 CacheAttr(row
, col
, attr
);
5270 attr
->SetDefAttr(m_defaultCellAttr
);
5272 attr
= m_defaultCellAttr
;
5279 wxGridCellAttr
*wxGrid
::GetOrCreateCellAttr(int row
, int col
) const
5281 wxGridCellAttr
*attr
;
5282 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
5284 wxASSERT_MSG( m_table
,
5285 _T("we may only be called if CanHaveAttributes() "
5286 "returned TRUE and then m_table should be !NULL") );
5288 attr
= m_table
->GetAttr(row
, col
);
5291 attr
= new wxGridCellAttr
;
5293 // artificially inc the ref count to match DecRef() in caller
5296 m_table
->SetAttr(attr
, row
, col
);
5299 CacheAttr(row
, col
, attr
);
5301 attr
->SetDefAttr(m_defaultCellAttr
);
5305 // ----------------------------------------------------------------------------
5306 // setting cell attributes: this is forwarded to the table
5307 // ----------------------------------------------------------------------------
5309 void wxGrid
::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5311 if ( CanHaveAttributes() )
5313 m_table
->SetRowAttr(attr
, row
);
5321 void wxGrid
::SetColAttr(int col
, wxGridCellAttr
*attr
)
5323 if ( CanHaveAttributes() )
5325 m_table
->SetColAttr(attr
, col
);
5333 void wxGrid
::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5335 if ( CanHaveAttributes() )
5337 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5338 attr
->SetBackgroundColour(colour
);
5343 void wxGrid
::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5345 if ( CanHaveAttributes() )
5347 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5348 attr
->SetTextColour(colour
);
5353 void wxGrid
::SetCellFont( int row
, int col
, const wxFont
& font
)
5355 if ( CanHaveAttributes() )
5357 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5358 attr
->SetFont(font
);
5363 void wxGrid
::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5365 if ( CanHaveAttributes() )
5367 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5368 attr
->SetAlignment(horiz
, vert
);
5373 void wxGrid
::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5375 if ( CanHaveAttributes() )
5377 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5378 attr
->SetRenderer(renderer
);
5383 // ----------------------------------------------------------------------------
5385 // ----------------------------------------------------------------------------
5387 void wxGrid
::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5389 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5391 if ( resizeExistingRows
)
5395 for ( row
= 0; row
< m_numRows
; row
++ )
5397 m_rowHeights
[row
] = m_defaultRowHeight
;
5398 bottom
+= m_defaultRowHeight
;
5399 m_rowBottoms
[row
] = bottom
;
5405 void wxGrid
::SetRowSize( int row
, int height
)
5407 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5411 int h
= wxMax( 0, height
);
5412 int diff
= h
- m_rowHeights
[row
];
5414 m_rowHeights
[row
] = h
;
5415 for ( i
= row
; i
< m_numRows
; i
++ )
5417 m_rowBottoms
[i
] += diff
;
5422 void wxGrid
::SetDefaultColSize( int width
, bool resizeExistingCols
)
5424 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5426 if ( resizeExistingCols
)
5430 for ( col
= 0; col
< m_numCols
; col
++ )
5432 m_colWidths
[col
] = m_defaultColWidth
;
5433 right
+= m_defaultColWidth
;
5434 m_colRights
[col
] = right
;
5440 void wxGrid
::SetColSize( int col
, int width
)
5442 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5446 int w
= wxMax( 0, width
);
5447 int diff
= w
- m_colWidths
[col
];
5448 m_colWidths
[col
] = w
;
5450 for ( i
= col
; i
< m_numCols
; i
++ )
5452 m_colRights
[i
] += diff
;
5459 // ------ cell value accessor functions
5462 void wxGrid
::SetCellValue( int row
, int col
, const wxString
& s
)
5466 m_table
->SetValue( row
, col
, s
.c_str() );
5467 if ( !GetBatchCount() )
5469 wxClientDC
dc( m_gridWin
);
5471 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5474 #if 0 // TODO: edit in place
5476 if ( m_currentCellCoords
.GetRow() == row
&&
5477 m_currentCellCoords
.GetCol() == col
)
5479 SetEditControlValue( s
);
5488 // ------ Block, row and col selection
5491 void wxGrid
::SelectRow( int row
, bool addToSelected
)
5495 if ( IsSelection() && addToSelected
)
5498 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5501 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5502 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5503 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5504 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5508 need_refresh
[0] = TRUE
;
5509 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5510 wxGridCellCoords ( oldTop
- 1,
5512 m_selectedTopLeft
.SetRow( row
);
5517 need_refresh
[1] = TRUE
;
5518 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5519 wxGridCellCoords ( oldBottom
,
5522 m_selectedTopLeft
.SetCol( 0 );
5525 if ( oldBottom
< row
)
5527 need_refresh
[2] = TRUE
;
5528 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5529 wxGridCellCoords ( row
,
5531 m_selectedBottomRight
.SetRow( row
);
5534 if ( oldRight
< m_numCols
- 1 )
5536 need_refresh
[3] = TRUE
;
5537 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5539 wxGridCellCoords ( oldBottom
,
5541 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5544 for (i
= 0; i
< 4; i
++ )
5545 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5546 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5550 r
= SelectionToDeviceRect();
5552 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5554 m_selectedTopLeft
.Set( row
, 0 );
5555 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5556 r
= SelectionToDeviceRect();
5557 m_gridWin
->Refresh( FALSE
, &r
);
5560 wxGridRangeSelectEvent
gridEvt( GetId(),
5561 EVT_GRID_RANGE_SELECT
,
5564 m_selectedBottomRight
);
5566 GetEventHandler()->ProcessEvent(gridEvt
);
5570 void wxGrid
::SelectCol( int col
, bool addToSelected
)
5572 if ( IsSelection() && addToSelected
)
5575 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5578 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5579 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5580 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5581 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5583 if ( oldLeft
> col
)
5585 need_refresh
[0] = TRUE
;
5586 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5587 wxGridCellCoords ( m_numRows
- 1,
5589 m_selectedTopLeft
.SetCol( col
);
5594 need_refresh
[1] = TRUE
;
5595 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5596 wxGridCellCoords ( oldTop
- 1,
5598 m_selectedTopLeft
.SetRow( 0 );
5601 if ( oldRight
< col
)
5603 need_refresh
[2] = TRUE
;
5604 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5605 wxGridCellCoords ( m_numRows
- 1,
5607 m_selectedBottomRight
.SetCol( col
);
5610 if ( oldBottom
< m_numRows
- 1 )
5612 need_refresh
[3] = TRUE
;
5613 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5615 wxGridCellCoords ( m_numRows
- 1,
5617 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5620 for (i
= 0; i
< 4; i
++ )
5621 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5622 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5628 r
= SelectionToDeviceRect();
5630 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5632 m_selectedTopLeft
.Set( 0, col
);
5633 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5634 r
= SelectionToDeviceRect();
5635 m_gridWin
->Refresh( FALSE
, &r
);
5638 wxGridRangeSelectEvent
gridEvt( GetId(),
5639 EVT_GRID_RANGE_SELECT
,
5642 m_selectedBottomRight
);
5644 GetEventHandler()->ProcessEvent(gridEvt
);
5648 void wxGrid
::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5651 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5653 if ( topRow
> bottomRow
)
5660 if ( leftCol
> rightCol
)
5667 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5668 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5670 if ( m_selectedTopLeft
!= updateTopLeft
||
5671 m_selectedBottomRight
!= updateBottomRight
)
5673 // Compute two optimal update rectangles:
5674 // Either one rectangle is a real subset of the
5675 // other, or they are (almost) disjoint!
5677 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5680 // Store intermediate values
5681 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5682 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5683 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5684 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5686 // Determine the outer/inner coordinates.
5687 if (oldLeft
> leftCol
)
5693 if (oldTop
> topRow
)
5699 if (oldRight
< rightCol
)
5702 oldRight
= rightCol
;
5705 if (oldBottom
< bottomRow
)
5708 oldBottom
= bottomRow
;
5712 // Now, either the stuff marked old is the outer
5713 // rectangle or we don't have a situation where one
5714 // is contained in the other.
5716 if ( oldLeft
< leftCol
)
5718 need_refresh
[0] = TRUE
;
5719 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5721 wxGridCellCoords ( oldBottom
,
5725 if ( oldTop
< topRow
)
5727 need_refresh
[1] = TRUE
;
5728 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5730 wxGridCellCoords ( topRow
- 1,
5734 if ( oldRight
> rightCol
)
5736 need_refresh
[2] = TRUE
;
5737 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5739 wxGridCellCoords ( oldBottom
,
5743 if ( oldBottom
> bottomRow
)
5745 need_refresh
[3] = TRUE
;
5746 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5748 wxGridCellCoords ( oldBottom
,
5754 m_selectedTopLeft
= updateTopLeft
;
5755 m_selectedBottomRight
= updateBottomRight
;
5757 // various Refresh() calls
5758 for (i
= 0; i
< 4; i
++ )
5759 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5760 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5763 // only generate an event if the block is not being selected by
5764 // dragging the mouse (in which case the event will be generated in
5765 // the mouse event handler)
5766 if ( !m_isDragging
)
5768 wxGridRangeSelectEvent
gridEvt( GetId(),
5769 EVT_GRID_RANGE_SELECT
,
5772 m_selectedBottomRight
);
5774 GetEventHandler()->ProcessEvent(gridEvt
);
5778 void wxGrid
::SelectAll()
5780 m_selectedTopLeft
.Set( 0, 0 );
5781 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5783 m_gridWin
->Refresh();
5787 void wxGrid
::ClearSelection()
5789 m_selectedTopLeft
= wxGridNoCellCoords
;
5790 m_selectedBottomRight
= wxGridNoCellCoords
;
5794 // This function returns the rectangle that encloses the given block
5795 // in device coords clipped to the client size of the grid window.
5797 wxRect wxGrid
::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5798 const wxGridCellCoords
&bottomRight
)
5800 wxRect
rect( wxGridNoCellRect
);
5803 cellRect
= CellToRect( topLeft
);
5804 if ( cellRect
!= wxGridNoCellRect
)
5810 rect
= wxRect( 0, 0, 0, 0 );
5813 cellRect
= CellToRect( bottomRight
);
5814 if ( cellRect
!= wxGridNoCellRect
)
5820 return wxGridNoCellRect
;
5823 // convert to scrolled coords
5825 int left
, top
, right
, bottom
;
5826 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5827 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5830 m_gridWin
->GetClientSize( &cw
, &ch
);
5832 rect
.SetLeft( wxMax(0, left
) );
5833 rect
.SetTop( wxMax(0, top
) );
5834 rect
.SetRight( wxMin(cw
, right
) );
5835 rect
.SetBottom( wxMin(ch
, bottom
) );
5843 // ------ Grid event classes
5846 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5848 wxGridEvent
::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5849 int row
, int col
, int x
, int y
,
5850 bool control
, bool shift
, bool alt
, bool meta
)
5851 : wxNotifyEvent( type
, id
)
5857 m_control
= control
;
5862 SetEventObject(obj
);
5866 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
5868 wxGridSizeEvent
::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
5869 int rowOrCol
, int x
, int y
,
5870 bool control
, bool shift
, bool alt
, bool meta
)
5871 : wxNotifyEvent( type
, id
)
5873 m_rowOrCol
= rowOrCol
;
5876 m_control
= control
;
5881 SetEventObject(obj
);
5885 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
5887 wxGridRangeSelectEvent
::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
5888 const wxGridCellCoords
& topLeft
,
5889 const wxGridCellCoords
& bottomRight
,
5890 bool control
, bool shift
, bool alt
, bool meta
)
5891 : wxNotifyEvent( type
, id
)
5893 m_topLeft
= topLeft
;
5894 m_bottomRight
= bottomRight
;
5895 m_control
= control
;
5900 SetEventObject(obj
);
5904 #endif // ifndef wxUSE_NEW_GRID