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
& );
165 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
166 DECLARE_EVENT_TABLE()
169 // ----------------------------------------------------------------------------
170 // the internal data representation used by wxGridCellAttrProvider
171 // ----------------------------------------------------------------------------
173 // this class stores attributes set for cells
174 class WXDLLEXPORT wxGridCellAttrData
177 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
178 wxGridCellAttr
*GetAttr(int row
, int col
) const;
181 // searches for the attr for given cell, returns wxNOT_FOUND if not found
182 int FindIndex(int row
, int col
) const;
184 wxGridCellWithAttrArray m_attrs
;
187 // this class stores attributes set for rows or columns
188 class WXDLLEXPORT wxGridRowOrColAttrData
191 ~wxGridRowOrColAttrData();
193 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
194 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
197 wxArrayInt m_rowsOrCols
;
198 wxArrayAttrs m_attrs
;
201 // NB: this is just a wrapper around 3 objects: one which stores cell
202 // attributes, and 2 others for row/col ones
203 class WXDLLEXPORT wxGridCellAttrProviderData
206 wxGridCellAttrData m_cellAttrs
;
207 wxGridRowOrColAttrData m_rowAttrs
,
211 // ----------------------------------------------------------------------------
212 // conditional compilation
213 // ----------------------------------------------------------------------------
215 #ifndef WXGRID_DRAW_LINES
216 #define WXGRID_DRAW_LINES 1
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 //#define DEBUG_ATTR_CACHE
224 #ifdef DEBUG_ATTR_CACHE
225 static size_t gs_nAttrCacheHits
= 0;
226 static size_t gs_nAttrCacheMisses
= 0;
227 #endif // DEBUG_ATTR_CACHE
229 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
230 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
233 // TODO: fixed so far - make configurable later (and also different for x/y)
234 static const size_t GRID_SCROLL_LINE
= 10;
236 // ============================================================================
238 // ============================================================================
240 // ----------------------------------------------------------------------------
241 // wxGridCellRenderer
242 // ----------------------------------------------------------------------------
244 void wxGridCellRenderer::Draw(wxGrid
& grid
,
250 dc
.SetBackgroundMode( wxSOLID
);
255 dc
.SetBrush( *wxBLACK_BRUSH
);
259 dc
.SetBrush( wxBrush(grid
.GetCellBackgroundColour(row
, col
), wxSOLID
) );
262 dc
.SetPen( *wxTRANSPARENT_PEN
);
264 dc
.DrawRectangle(rect
);
267 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
269 const wxRect
& rectCell
,
273 wxGridCellRenderer::Draw(grid
, dc
, rectCell
, row
, col
, isSelected
);
275 // now we only have to draw the text
276 dc
.SetBackgroundMode( wxTRANSPARENT
);
281 dc
.SetTextBackground( wxColour(0, 0, 0) );
282 dc
.SetTextForeground( wxColour(255, 255, 255) );
286 dc
.SetTextBackground( grid
.GetCellBackgroundColour(row
, col
) );
287 dc
.SetTextForeground( grid
.GetCellTextColour(row
, col
) );
289 dc
.SetFont( grid
.GetCellFont(row
, col
) );
292 grid
.GetCellAlignment(row
, col
, &hAlign
, &vAlign
);
294 wxRect rect
= rectCell
;
300 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
301 rect
, hAlign
, vAlign
);
304 // ----------------------------------------------------------------------------
305 // wxGridCellAttrData
306 // ----------------------------------------------------------------------------
308 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
310 int n
= FindIndex(row
, col
);
311 if ( n
== wxNOT_FOUND
)
314 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
320 // change the attribute
321 m_attrs
[(size_t)n
].attr
= attr
;
325 // remove this attribute
326 m_attrs
.RemoveAt((size_t)n
);
331 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
333 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
335 int n
= FindIndex(row
, col
);
336 if ( n
!= wxNOT_FOUND
)
338 attr
= m_attrs
[(size_t)n
].attr
;
345 int wxGridCellAttrData::FindIndex(int row
, int col
) const
347 size_t count
= m_attrs
.GetCount();
348 for ( size_t n
= 0; n
< count
; n
++ )
350 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
351 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
360 // ----------------------------------------------------------------------------
361 // wxGridRowOrColAttrData
362 // ----------------------------------------------------------------------------
364 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
366 size_t count
= m_attrs
.Count();
367 for ( size_t n
= 0; n
< count
; n
++ )
369 m_attrs
[n
]->DecRef();
373 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
375 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
377 int n
= m_rowsOrCols
.Index(rowOrCol
);
378 if ( n
!= wxNOT_FOUND
)
380 attr
= m_attrs
[(size_t)n
];
387 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
389 int n
= m_rowsOrCols
.Index(rowOrCol
);
390 if ( n
== wxNOT_FOUND
)
393 m_rowsOrCols
.Add(rowOrCol
);
400 // change the attribute
401 m_attrs
[(size_t)n
] = attr
;
405 // remove this attribute
406 m_attrs
[(size_t)n
]->DecRef();
407 m_rowsOrCols
.RemoveAt((size_t)n
);
408 m_attrs
.RemoveAt((size_t)n
);
413 // ----------------------------------------------------------------------------
414 // wxGridCellAttrProvider
415 // ----------------------------------------------------------------------------
417 wxGridCellAttrProvider::wxGridCellAttrProvider()
419 m_data
= (wxGridCellAttrProviderData
*)NULL
;
422 wxGridCellAttrProvider::~wxGridCellAttrProvider()
427 void wxGridCellAttrProvider::InitData()
429 m_data
= new wxGridCellAttrProviderData
;
432 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
434 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
437 // first look for the attribute of this specific cell
438 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
442 // then look for the col attr (col attributes are more common than
443 // the row ones, hence they have priority)
444 attr
= m_data
->m_colAttrs
.GetAttr(col
);
449 // finally try the row attributes
450 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
457 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
463 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
466 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
471 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
474 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
479 m_data
->m_colAttrs
.SetAttr(attr
, col
);
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 //////////////////////////////////////////////////////////////////////
488 // Abstract base class for grid data (the model)
490 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
493 wxGridTableBase::wxGridTableBase()
495 m_view
= (wxGrid
*) NULL
;
496 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
499 wxGridTableBase::~wxGridTableBase()
501 delete m_attrProvider
;
504 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
506 delete m_attrProvider
;
507 m_attrProvider
= attrProvider
;
510 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
512 if ( m_attrProvider
)
513 return m_attrProvider
->GetAttr(row
, col
);
515 return (wxGridCellAttr
*)NULL
;
518 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
520 if ( m_attrProvider
)
522 m_attrProvider
->SetAttr(attr
, row
, col
);
526 // as we take ownership of the pointer and don't store it, we must
532 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
534 if ( m_attrProvider
)
536 m_attrProvider
->SetRowAttr(attr
, row
);
540 // as we take ownership of the pointer and don't store it, we must
546 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
548 if ( m_attrProvider
)
550 m_attrProvider
->SetColAttr(attr
, col
);
554 // as we take ownership of the pointer and don't store it, we must
560 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
562 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
563 "but your derived table class does not override this function") );
568 bool wxGridTableBase::AppendRows( size_t numRows
)
570 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
571 "but your derived table class does not override this function"));
576 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
578 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
579 "but your derived table class does not override this function"));
584 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
586 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
587 "but your derived table class does not override this function"));
592 bool wxGridTableBase::AppendCols( size_t numCols
)
594 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
595 "but your derived table class does not override this function"));
600 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
602 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
603 "but your derived table class does not override this function"));
609 wxString
wxGridTableBase::GetRowLabelValue( int row
)
616 wxString
wxGridTableBase::GetColLabelValue( int col
)
618 // default col labels are:
619 // cols 0 to 25 : A-Z
620 // cols 26 to 675 : AA-ZZ
627 s
+= (_T('A') + (wxChar
)( col%26
));
629 if ( col
< 0 ) break;
632 // reverse the string...
634 for ( i
= 0; i
< n
; i
++ )
644 //////////////////////////////////////////////////////////////////////
646 // Message class for the grid table to send requests and notifications
650 wxGridTableMessage::wxGridTableMessage()
652 m_table
= (wxGridTableBase
*) NULL
;
658 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
659 int commandInt1
, int commandInt2
)
663 m_comInt1
= commandInt1
;
664 m_comInt2
= commandInt2
;
669 //////////////////////////////////////////////////////////////////////
671 // A basic grid table for string data. An object of this class will
672 // created by wxGrid if you don't specify an alternative table class.
675 WX_DEFINE_OBJARRAY(wxGridStringArray
)
677 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
679 wxGridStringTable::wxGridStringTable()
684 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
689 m_data
.Alloc( numRows
);
693 for ( col
= 0; col
< numCols
; col
++ )
695 sa
.Add( wxEmptyString
);
698 for ( row
= 0; row
< numRows
; row
++ )
704 wxGridStringTable::~wxGridStringTable()
708 long wxGridStringTable::GetNumberRows()
710 return m_data
.GetCount();
713 long wxGridStringTable::GetNumberCols()
715 if ( m_data
.GetCount() > 0 )
716 return m_data
[0].GetCount();
721 wxString
wxGridStringTable::GetValue( int row
, int col
)
723 // TODO: bounds checking
725 return m_data
[row
][col
];
728 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
730 // TODO: bounds checking
732 m_data
[row
][col
] = s
;
735 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
737 // TODO: bounds checking
739 return (m_data
[row
][col
] == wxEmptyString
);
743 void wxGridStringTable::Clear()
746 int numRows
, numCols
;
748 numRows
= m_data
.GetCount();
751 numCols
= m_data
[0].GetCount();
753 for ( row
= 0; row
< numRows
; row
++ )
755 for ( col
= 0; col
< numCols
; col
++ )
757 m_data
[row
][col
] = wxEmptyString
;
764 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
768 size_t curNumRows
= m_data
.GetCount();
769 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
771 if ( pos
>= curNumRows
)
773 return AppendRows( numRows
);
777 sa
.Alloc( curNumCols
);
778 for ( col
= 0; col
< curNumCols
; col
++ )
780 sa
.Add( wxEmptyString
);
783 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
785 m_data
.Insert( sa
, row
);
790 wxGridTableMessage
msg( this,
791 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
795 GetView()->ProcessTableMessage( msg
);
801 bool wxGridStringTable::AppendRows( size_t numRows
)
805 size_t curNumRows
= m_data
.GetCount();
806 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
809 if ( curNumCols
> 0 )
811 sa
.Alloc( curNumCols
);
812 for ( col
= 0; col
< curNumCols
; col
++ )
814 sa
.Add( wxEmptyString
);
818 for ( row
= 0; row
< numRows
; row
++ )
825 wxGridTableMessage
msg( this,
826 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
829 GetView()->ProcessTableMessage( msg
);
835 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
839 size_t curNumRows
= m_data
.GetCount();
841 if ( pos
>= curNumRows
)
844 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
845 "Pos value is invalid for present table with %d rows",
846 pos
, numRows
, curNumRows
);
847 wxFAIL_MSG( wxT(errmsg
) );
851 if ( numRows
> curNumRows
- pos
)
853 numRows
= curNumRows
- pos
;
856 if ( numRows
>= curNumRows
)
858 m_data
.Empty(); // don't release memory just yet
862 for ( n
= 0; n
< numRows
; n
++ )
864 m_data
.Remove( pos
);
870 wxGridTableMessage
msg( this,
871 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
875 GetView()->ProcessTableMessage( msg
);
881 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
885 size_t curNumRows
= m_data
.GetCount();
886 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
888 if ( pos
>= curNumCols
)
890 return AppendCols( numCols
);
893 for ( row
= 0; row
< curNumRows
; row
++ )
895 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
897 m_data
[row
].Insert( wxEmptyString
, col
);
903 wxGridTableMessage
msg( this,
904 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
908 GetView()->ProcessTableMessage( msg
);
914 bool wxGridStringTable::AppendCols( size_t numCols
)
918 size_t curNumRows
= m_data
.GetCount();
921 // TODO: something better than this ?
923 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
924 "Call AppendRows() first") );
928 for ( row
= 0; row
< curNumRows
; row
++ )
930 for ( n
= 0; n
< numCols
; n
++ )
932 m_data
[row
].Add( wxEmptyString
);
938 wxGridTableMessage
msg( this,
939 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
942 GetView()->ProcessTableMessage( msg
);
948 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
952 size_t curNumRows
= m_data
.GetCount();
953 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
955 if ( pos
>= curNumCols
)
958 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
959 "Pos value is invalid for present table with %d cols",
960 pos
, numCols
, curNumCols
);
961 wxFAIL_MSG( wxT( errmsg
) );
965 if ( numCols
> curNumCols
- pos
)
967 numCols
= curNumCols
- pos
;
970 for ( row
= 0; row
< curNumRows
; row
++ )
972 if ( numCols
>= curNumCols
)
978 for ( n
= 0; n
< numCols
; n
++ )
980 m_data
[row
].Remove( pos
);
987 wxGridTableMessage
msg( this,
988 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
992 GetView()->ProcessTableMessage( msg
);
998 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1000 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1002 // using default label
1004 return wxGridTableBase::GetRowLabelValue( row
);
1008 return m_rowLabels
[ row
];
1012 wxString
wxGridStringTable::GetColLabelValue( int col
)
1014 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1016 // using default label
1018 return wxGridTableBase::GetColLabelValue( col
);
1022 return m_colLabels
[ col
];
1026 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1028 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1030 int n
= m_rowLabels
.GetCount();
1032 for ( i
= n
; i
<= row
; i
++ )
1034 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1038 m_rowLabels
[row
] = value
;
1041 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1043 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1045 int n
= m_colLabels
.GetCount();
1047 for ( i
= n
; i
<= col
; i
++ )
1049 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1053 m_colLabels
[col
] = value
;
1059 //////////////////////////////////////////////////////////////////////
1061 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
1063 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
1064 EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown
)
1068 wxGridTextCtrl::wxGridTextCtrl( wxWindow
*par
,
1072 const wxString
& value
,
1076 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
1079 m_isCellControl
= isCellControl
;
1083 void wxGridTextCtrl::OnKeyDown( wxKeyEvent
& event
)
1085 switch ( event
.KeyCode() )
1088 m_grid
->SetEditControlValue( startValue
);
1089 SetInsertionPointEnd();
1099 if ( m_isCellControl
)
1101 // send the event to the parent grid, skipping the
1102 // event if nothing happens
1104 event
.Skip( m_grid
->ProcessEvent( event
) );
1108 // default text control response within the top edit
1116 if ( m_isCellControl
)
1118 if ( !m_grid
->ProcessEvent( event
) )
1120 #if defined(__WXMOTIF__) || defined(__WXGTK__)
1121 // wxMotif needs a little extra help...
1123 int pos
= GetInsertionPoint();
1124 wxString
s( GetValue() );
1125 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
1127 SetInsertionPoint( pos
);
1129 // the other ports can handle a Return key press
1139 if ( m_isCellControl
)
1141 // send the event to the parent grid, skipping the
1142 // event if nothing happens
1144 event
.Skip( m_grid
->ProcessEvent( event
) );
1148 // default text control response within the top edit
1160 void wxGridTextCtrl::SetStartValue( const wxString
& s
)
1163 wxTextCtrl::SetValue(s
);
1168 //////////////////////////////////////////////////////////////////////
1170 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1172 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1173 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1174 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1175 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1178 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1180 const wxPoint
&pos
, const wxSize
&size
)
1181 : wxWindow( parent
, id
, pos
, size
)
1186 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1190 // NO - don't do this because it will set both the x and y origin
1191 // coords to match the parent scrolled window and we just want to
1192 // set the y coord - MB
1194 // m_owner->PrepareDC( dc );
1197 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1198 dc
.SetDeviceOrigin( 0, -y
);
1200 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1201 m_owner
->DrawRowLabels( dc
);
1205 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1207 m_owner
->ProcessRowLabelMouseEvent( event
);
1211 // This seems to be required for wxMotif otherwise the mouse
1212 // cursor must be in the cell edit control to get key events
1214 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1216 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1221 //////////////////////////////////////////////////////////////////////
1223 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1225 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1226 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1227 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1228 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1231 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1233 const wxPoint
&pos
, const wxSize
&size
)
1234 : wxWindow( parent
, id
, pos
, size
)
1239 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1243 // NO - don't do this because it will set both the x and y origin
1244 // coords to match the parent scrolled window and we just want to
1245 // set the x coord - MB
1247 // m_owner->PrepareDC( dc );
1250 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1251 dc
.SetDeviceOrigin( -x
, 0 );
1253 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1254 m_owner
->DrawColLabels( dc
);
1258 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1260 m_owner
->ProcessColLabelMouseEvent( event
);
1264 // This seems to be required for wxMotif otherwise the mouse
1265 // cursor must be in the cell edit control to get key events
1267 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1269 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1274 //////////////////////////////////////////////////////////////////////
1276 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1278 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1279 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1280 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1281 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1284 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1286 const wxPoint
&pos
, const wxSize
&size
)
1287 : wxWindow( parent
, id
, pos
, size
)
1292 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1296 int client_height
= 0;
1297 int client_width
= 0;
1298 GetClientSize( &client_width
, &client_height
);
1300 dc
.SetPen( *wxBLACK_PEN
);
1301 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1302 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1304 dc
.SetPen( *wxWHITE_PEN
);
1305 dc
.DrawLine( 0, 0, client_width
, 0 );
1306 dc
.DrawLine( 0, 0, 0, client_height
);
1310 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1312 m_owner
->ProcessCornerLabelMouseEvent( event
);
1316 // This seems to be required for wxMotif otherwise the mouse
1317 // cursor must be in the cell edit control to get key events
1319 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1321 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1326 //////////////////////////////////////////////////////////////////////
1328 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1330 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1331 EVT_PAINT( wxGridWindow::OnPaint
)
1332 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1333 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1336 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1337 wxGridRowLabelWindow
*rowLblWin
,
1338 wxGridColLabelWindow
*colLblWin
,
1339 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1340 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1343 m_rowLabelWin
= rowLblWin
;
1344 m_colLabelWin
= colLblWin
;
1346 SetBackgroundColour( "WHITE" );
1350 wxGridWindow::~wxGridWindow()
1355 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1357 wxPaintDC
dc( this );
1358 m_owner
->PrepareDC( dc
);
1359 wxRegion reg
= GetUpdateRegion();
1360 m_owner
->CalcCellsExposed( reg
);
1361 m_owner
->DrawGridCellArea( dc
);
1362 #if WXGRID_DRAW_LINES
1363 m_owner
->DrawAllGridLines( dc
, reg
);
1368 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1370 wxPanel::ScrollWindow( dx
, dy
, rect
);
1371 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1372 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1376 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1378 m_owner
->ProcessGridCellMouseEvent( event
);
1382 // This seems to be required for wxMotif otherwise the mouse
1383 // cursor must be in the cell edit control to get key events
1385 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1387 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1392 //////////////////////////////////////////////////////////////////////
1394 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1396 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1397 EVT_PAINT( wxGrid::OnPaint
)
1398 EVT_SIZE( wxGrid::OnSize
)
1399 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1402 wxGrid::wxGrid( wxWindow
*parent
,
1407 const wxString
& name
)
1408 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1418 #ifdef DEBUG_ATTR_CACHE
1419 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1420 wxPrintf(_T("wxGrid attribute cache statistics: "
1421 "total: %u, hits: %u (%u%%)\n"),
1422 total
, gs_nAttrCacheHits
,
1423 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1426 delete m_defaultRenderer
;
1432 // ----- internal init and update functions
1435 void wxGrid::Create()
1437 m_created
= FALSE
; // set to TRUE by CreateGrid
1438 m_displayed
= FALSE
; // set to TRUE by OnPaint
1440 m_table
= (wxGridTableBase
*) NULL
;
1441 m_cellEditCtrl
= (wxWindow
*) NULL
;
1445 m_currentCellCoords
= wxGridNoCellCoords
;
1447 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1448 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1450 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1455 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1460 m_colLabelWin
= new wxGridColLabelWindow( this,
1465 m_gridWin
= new wxGridWindow( this,
1472 SetTargetWindow( m_gridWin
);
1476 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1480 wxFAIL_MSG( wxT("wxGrid::CreateGrid called more than once") );
1485 m_numRows
= numRows
;
1486 m_numCols
= numCols
;
1488 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1489 m_table
->SetView( this );
1502 if ( m_numRows
<= 0 )
1503 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1505 if ( m_numCols
<= 0 )
1506 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1508 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1509 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1511 if ( m_rowLabelWin
)
1513 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1517 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1520 m_labelTextColour
= wxColour( _T("BLACK") );
1523 m_attrCache
.row
= -1;
1525 // TODO: something better than this ?
1527 m_labelFont
= this->GetFont();
1528 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1530 m_rowLabelHorizAlign
= wxLEFT
;
1531 m_rowLabelVertAlign
= wxCENTRE
;
1533 m_colLabelHorizAlign
= wxCENTRE
;
1534 m_colLabelVertAlign
= wxTOP
;
1536 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1537 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1539 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1540 m_defaultRowHeight
+= 8;
1542 m_defaultRowHeight
+= 4;
1545 m_rowHeights
.Alloc( m_numRows
);
1546 m_rowBottoms
.Alloc( m_numRows
);
1548 for ( i
= 0; i
< m_numRows
; i
++ )
1550 m_rowHeights
.Add( m_defaultRowHeight
);
1551 rowBottom
+= m_defaultRowHeight
;
1552 m_rowBottoms
.Add( rowBottom
);
1555 m_colWidths
.Alloc( m_numCols
);
1556 m_colRights
.Alloc( m_numCols
);
1558 for ( i
= 0; i
< m_numCols
; i
++ )
1560 m_colWidths
.Add( m_defaultColWidth
);
1561 colRight
+= m_defaultColWidth
;
1562 m_colRights
.Add( colRight
);
1565 // TODO: improve this by using wxSystemSettings?
1567 m_defaultCellFont
= GetFont();
1569 m_defaultCellHAlign
= wxLEFT
;
1570 m_defaultCellVAlign
= wxTOP
;
1572 m_defaultRenderer
= (wxGridCellRenderer
*)NULL
;
1574 m_gridLineColour
= wxColour( 128, 128, 255 );
1575 m_gridLinesEnabled
= TRUE
;
1577 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1578 m_winCapture
= (wxWindow
*)NULL
;
1580 m_dragRowOrCol
= -1;
1581 m_isDragging
= FALSE
;
1583 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1584 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1586 m_currentCellCoords
= wxGridNoCellCoords
;
1588 m_selectedTopLeft
= wxGridNoCellCoords
;
1589 m_selectedBottomRight
= wxGridNoCellCoords
;
1591 m_editable
= TRUE
; // default for whole grid
1593 m_inOnKeyDown
= FALSE
;
1596 // TODO: extend this to other types of controls
1598 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1605 #if defined(__WXMSW__)
1606 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1610 m_cellEditCtrl
->Show( FALSE
);
1611 m_cellEditCtrlEnabled
= TRUE
;
1612 m_editCtrlType
= wxGRID_TEXTCTRL
;
1616 void wxGrid::CalcDimensions()
1619 GetClientSize( &cw
, &ch
);
1621 if ( m_numRows
> 0 && m_numCols
> 0 )
1623 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1624 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1626 // TODO: restore the scroll position that we had before sizing
1629 GetViewStart( &x
, &y
);
1630 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1631 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1637 void wxGrid::CalcWindowSizes()
1640 GetClientSize( &cw
, &ch
);
1642 if ( m_cornerLabelWin
->IsShown() )
1643 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
1645 if ( m_colLabelWin
->IsShown() )
1646 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
1648 if ( m_rowLabelWin
->IsShown() )
1649 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
1651 if ( m_gridWin
->IsShown() )
1652 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
1656 // this is called when the grid table sends a message to say that it
1657 // has been redimensioned
1659 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1663 switch ( msg
.GetId() )
1665 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1667 size_t pos
= msg
.GetCommandInt();
1668 int numRows
= msg
.GetCommandInt2();
1669 for ( i
= 0; i
< numRows
; i
++ )
1671 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1672 m_rowBottoms
.Insert( 0, pos
);
1674 m_numRows
+= numRows
;
1677 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1679 for ( i
= pos
; i
< m_numRows
; i
++ )
1681 bottom
+= m_rowHeights
[i
];
1682 m_rowBottoms
[i
] = bottom
;
1688 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1690 int numRows
= msg
.GetCommandInt();
1691 for ( i
= 0; i
< numRows
; i
++ )
1693 m_rowHeights
.Add( m_defaultRowHeight
);
1694 m_rowBottoms
.Add( 0 );
1697 int oldNumRows
= m_numRows
;
1698 m_numRows
+= numRows
;
1701 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1703 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1705 bottom
+= m_rowHeights
[i
];
1706 m_rowBottoms
[i
] = bottom
;
1712 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1714 size_t pos
= msg
.GetCommandInt();
1715 int numRows
= msg
.GetCommandInt2();
1716 for ( i
= 0; i
< numRows
; i
++ )
1718 m_rowHeights
.Remove( pos
);
1719 m_rowBottoms
.Remove( pos
);
1721 m_numRows
-= numRows
;
1726 m_colWidths
.Clear();
1727 m_colRights
.Clear();
1728 m_currentCellCoords
= wxGridNoCellCoords
;
1732 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1733 m_currentCellCoords
.Set( 0, 0 );
1736 for ( i
= 0; i
< m_numRows
; i
++ )
1738 h
+= m_rowHeights
[i
];
1739 m_rowBottoms
[i
] = h
;
1747 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1749 size_t pos
= msg
.GetCommandInt();
1750 int numCols
= msg
.GetCommandInt2();
1751 for ( i
= 0; i
< numCols
; i
++ )
1753 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1754 m_colRights
.Insert( 0, pos
);
1756 m_numCols
+= numCols
;
1759 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1761 for ( i
= pos
; i
< m_numCols
; i
++ )
1763 right
+= m_colWidths
[i
];
1764 m_colRights
[i
] = right
;
1770 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1772 int numCols
= msg
.GetCommandInt();
1773 for ( i
= 0; i
< numCols
; i
++ )
1775 m_colWidths
.Add( m_defaultColWidth
);
1776 m_colRights
.Add( 0 );
1779 int oldNumCols
= m_numCols
;
1780 m_numCols
+= numCols
;
1783 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1785 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1787 right
+= m_colWidths
[i
];
1788 m_colRights
[i
] = right
;
1794 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1796 size_t pos
= msg
.GetCommandInt();
1797 int numCols
= msg
.GetCommandInt2();
1798 for ( i
= 0; i
< numCols
; i
++ )
1800 m_colWidths
.Remove( pos
);
1801 m_colRights
.Remove( pos
);
1803 m_numCols
-= numCols
;
1807 #if 0 // leave the row alone here so that AppendCols will work subsequently
1809 m_rowHeights
.Clear();
1810 m_rowBottoms
.Clear();
1812 m_currentCellCoords
= wxGridNoCellCoords
;
1816 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1817 m_currentCellCoords
.Set( 0, 0 );
1820 for ( i
= 0; i
< m_numCols
; i
++ )
1822 w
+= m_colWidths
[i
];
1835 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
1837 wxRegionIterator
iter( reg
);
1840 m_rowLabelsExposed
.Empty();
1847 // TODO: remove this when we can...
1848 // There is a bug in wxMotif that gives garbage update
1849 // rectangles if you jump-scroll a long way by clicking the
1850 // scrollbar with middle button. This is a work-around
1852 #if defined(__WXMOTIF__)
1854 m_gridWin
->GetClientSize( &cw
, &ch
);
1855 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1856 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1859 // logical bounds of update region
1862 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1863 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1865 // find the row labels within these bounds
1869 for ( row
= 0; row
< m_numRows
; row
++ )
1871 if ( m_rowBottoms
[row
] < top
) continue;
1873 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1874 if ( rowTop
> bottom
) break;
1876 m_rowLabelsExposed
.Add( row
);
1884 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
1886 wxRegionIterator
iter( reg
);
1889 m_colLabelsExposed
.Empty();
1896 // TODO: remove this when we can...
1897 // There is a bug in wxMotif that gives garbage update
1898 // rectangles if you jump-scroll a long way by clicking the
1899 // scrollbar with middle button. This is a work-around
1901 #if defined(__WXMOTIF__)
1903 m_gridWin
->GetClientSize( &cw
, &ch
);
1904 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1905 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1908 // logical bounds of update region
1911 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1912 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1914 // find the cells within these bounds
1918 for ( col
= 0; col
< m_numCols
; col
++ )
1920 if ( m_colRights
[col
] < left
) continue;
1922 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1923 if ( colLeft
> right
) break;
1925 m_colLabelsExposed
.Add( col
);
1933 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
1935 wxRegionIterator
iter( reg
);
1938 m_cellsExposed
.Empty();
1939 m_rowsExposed
.Empty();
1940 m_colsExposed
.Empty();
1942 int left
, top
, right
, bottom
;
1947 // TODO: remove this when we can...
1948 // There is a bug in wxMotif that gives garbage update
1949 // rectangles if you jump-scroll a long way by clicking the
1950 // scrollbar with middle button. This is a work-around
1952 #if defined(__WXMOTIF__)
1954 m_gridWin
->GetClientSize( &cw
, &ch
);
1955 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1956 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1957 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1958 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1961 // logical bounds of update region
1963 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1964 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1966 // find the cells within these bounds
1969 int colLeft
, rowTop
;
1970 for ( row
= 0; row
< m_numRows
; row
++ )
1972 if ( m_rowBottoms
[row
] < top
) continue;
1974 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1975 if ( rowTop
> bottom
) break;
1977 m_rowsExposed
.Add( row
);
1979 for ( col
= 0; col
< m_numCols
; col
++ )
1981 if ( m_colRights
[col
] < left
) continue;
1983 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1984 if ( colLeft
> right
) break;
1986 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1987 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1996 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1999 wxPoint
pos( event
.GetPosition() );
2000 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2002 if ( event
.Dragging() )
2004 m_isDragging
= TRUE
;
2006 if ( event
.LeftIsDown() )
2008 switch( m_cursorMode
)
2010 case WXGRID_CURSOR_RESIZE_ROW
:
2012 int cw
, ch
, left
, dummy
;
2013 m_gridWin
->GetClientSize( &cw
, &ch
);
2014 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2016 wxClientDC
dc( m_gridWin
);
2018 dc
.SetLogicalFunction(wxINVERT
);
2019 if ( m_dragLastPos
>= 0 )
2021 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2023 dc
.DrawLine( left
, y
, left
+cw
, y
);
2028 case WXGRID_CURSOR_SELECT_ROW
:
2029 if ( (row
= YToRow( y
)) >= 0 &&
2030 !IsInSelection( row
, 0 ) )
2032 SelectRow( row
, TRUE
);
2035 // default label to suppress warnings about "enumeration value
2036 // 'xxx' not handled in switch
2044 m_isDragging
= FALSE
;
2047 // ------------ Entering or leaving the window
2049 if ( event
.Entering() || event
.Leaving() )
2051 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2055 // ------------ Left button pressed
2057 else if ( event
.LeftDown() )
2059 // don't send a label click event for a hit on the
2060 // edge of the row label - this is probably the user
2061 // wanting to resize the row
2063 if ( YToEdgeOfRow(y
) < 0 )
2067 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2069 SelectRow( row
, event
.ShiftDown() );
2070 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2075 // starting to drag-resize a row
2077 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2082 // ------------ Left double click
2084 else if (event
.LeftDClick() )
2086 if ( YToEdgeOfRow(y
) < 0 )
2089 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
2094 // ------------ Left button released
2096 else if ( event
.LeftUp() )
2098 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2100 DoEndDragResizeRow();
2102 // Note: we are ending the event *after* doing
2103 // default processing in this case
2105 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2108 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2113 // ------------ Right button down
2115 else if ( event
.RightDown() )
2118 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
2120 // no default action at the moment
2125 // ------------ Right double click
2127 else if ( event
.RightDClick() )
2130 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
2132 // no default action at the moment
2137 // ------------ No buttons down and mouse moving
2139 else if ( event
.Moving() )
2141 m_dragRowOrCol
= YToEdgeOfRow( y
);
2142 if ( m_dragRowOrCol
>= 0 )
2144 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2146 // don't capture the mouse yet
2147 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
2150 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2152 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
2158 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
2161 wxPoint
pos( event
.GetPosition() );
2162 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2164 if ( event
.Dragging() )
2166 m_isDragging
= TRUE
;
2168 if ( event
.LeftIsDown() )
2170 switch( m_cursorMode
)
2172 case WXGRID_CURSOR_RESIZE_COL
:
2174 int cw
, ch
, dummy
, top
;
2175 m_gridWin
->GetClientSize( &cw
, &ch
);
2176 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2178 wxClientDC
dc( m_gridWin
);
2180 dc
.SetLogicalFunction(wxINVERT
);
2181 if ( m_dragLastPos
>= 0 )
2183 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2185 dc
.DrawLine( x
, top
, x
, top
+ch
);
2190 case WXGRID_CURSOR_SELECT_COL
:
2191 if ( (col
= XToCol( x
)) >= 0 &&
2192 !IsInSelection( 0, col
) )
2194 SelectCol( col
, TRUE
);
2197 // default label to suppress warnings about "enumeration value
2198 // 'xxx' not handled in switch
2206 m_isDragging
= FALSE
;
2209 // ------------ Entering or leaving the window
2211 if ( event
.Entering() || event
.Leaving() )
2213 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2217 // ------------ Left button pressed
2219 else if ( event
.LeftDown() )
2221 // don't send a label click event for a hit on the
2222 // edge of the col label - this is probably the user
2223 // wanting to resize the col
2225 if ( XToEdgeOfCol(x
) < 0 )
2229 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2231 SelectCol( col
, event
.ShiftDown() );
2232 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2237 // starting to drag-resize a col
2239 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2244 // ------------ Left double click
2246 if ( event
.LeftDClick() )
2248 if ( XToEdgeOfCol(x
) < 0 )
2251 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2256 // ------------ Left button released
2258 else if ( event
.LeftUp() )
2260 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2262 DoEndDragResizeCol();
2264 // Note: we are ending the event *after* doing
2265 // default processing in this case
2267 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2270 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2275 // ------------ Right button down
2277 else if ( event
.RightDown() )
2280 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2282 // no default action at the moment
2287 // ------------ Right double click
2289 else if ( event
.RightDClick() )
2292 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2294 // no default action at the moment
2299 // ------------ No buttons down and mouse moving
2301 else if ( event
.Moving() )
2303 m_dragRowOrCol
= XToEdgeOfCol( x
);
2304 if ( m_dragRowOrCol
>= 0 )
2306 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2308 // don't capture the cursor yet
2309 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2312 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2314 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2320 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2322 if ( event
.LeftDown() )
2324 // indicate corner label by having both row and
2327 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2333 else if ( event
.LeftDClick() )
2335 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2338 else if ( event
.RightDown() )
2340 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2342 // no default action at the moment
2346 else if ( event
.RightDClick() )
2348 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2350 // no default action at the moment
2355 void wxGrid::ChangeCursorMode(CursorMode mode
,
2360 static const wxChar
*cursorModes
[] =
2369 wxLogTrace(_T("grid"),
2370 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2371 win
== m_colLabelWin
? _T("colLabelWin")
2372 : win
? _T("rowLabelWin")
2374 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2375 #endif // __WXDEBUG__
2377 if ( mode
== m_cursorMode
)
2382 // by default use the grid itself
2388 m_winCapture
->ReleaseMouse();
2389 m_winCapture
= (wxWindow
*)NULL
;
2392 m_cursorMode
= mode
;
2394 switch ( m_cursorMode
)
2396 case WXGRID_CURSOR_RESIZE_ROW
:
2397 win
->SetCursor( m_rowResizeCursor
);
2400 case WXGRID_CURSOR_RESIZE_COL
:
2401 win
->SetCursor( m_colResizeCursor
);
2405 win
->SetCursor( *wxSTANDARD_CURSOR
);
2408 // we need to capture mouse when resizing
2409 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2410 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2412 if ( captureMouse
&& resize
)
2414 win
->CaptureMouse();
2419 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2422 wxPoint
pos( event
.GetPosition() );
2423 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2425 wxGridCellCoords coords
;
2426 XYToCell( x
, y
, coords
);
2428 if ( event
.Dragging() )
2430 m_isDragging
= TRUE
;
2431 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2433 // Hide the edit control, so it
2434 // won't interfer with drag-shrinking.
2435 if ( IsCellEditControlEnabled() )
2436 HideCellEditControl();
2437 if ( coords
!= wxGridNoCellCoords
)
2439 if ( !IsSelection() )
2441 SelectBlock( coords
, coords
);
2445 SelectBlock( m_currentCellCoords
, coords
);
2449 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2451 int cw
, ch
, left
, dummy
;
2452 m_gridWin
->GetClientSize( &cw
, &ch
);
2453 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2455 wxClientDC
dc( m_gridWin
);
2457 dc
.SetLogicalFunction(wxINVERT
);
2458 if ( m_dragLastPos
>= 0 )
2460 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2462 dc
.DrawLine( left
, y
, left
+cw
, y
);
2465 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2467 int cw
, ch
, dummy
, top
;
2468 m_gridWin
->GetClientSize( &cw
, &ch
);
2469 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2471 wxClientDC
dc( m_gridWin
);
2473 dc
.SetLogicalFunction(wxINVERT
);
2474 if ( m_dragLastPos
>= 0 )
2476 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2478 dc
.DrawLine( x
, top
, x
, top
+ch
);
2485 m_isDragging
= FALSE
;
2487 if ( coords
!= wxGridNoCellCoords
)
2489 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2490 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2493 if ( event
.Entering() || event
.Leaving() )
2495 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2496 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2501 // ------------ Left button pressed
2503 if ( event
.LeftDown() )
2505 if ( event
.ShiftDown() )
2507 SelectBlock( m_currentCellCoords
, coords
);
2509 else if ( XToEdgeOfCol(x
) < 0 &&
2510 YToEdgeOfRow(y
) < 0 )
2512 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2517 MakeCellVisible( coords
);
2518 SetCurrentCell( coords
);
2524 // ------------ Left double click
2526 else if ( event
.LeftDClick() )
2528 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2530 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2538 // ------------ Left button released
2540 else if ( event
.LeftUp() )
2542 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2544 if ( IsSelection() )
2546 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
2549 // Show the edit control, if it has
2550 // been hidden for drag-shrinking.
2551 if ( IsCellEditControlEnabled() )
2552 ShowCellEditControl();
2554 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2556 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2557 DoEndDragResizeRow();
2559 // Note: we are ending the event *after* doing
2560 // default processing in this case
2562 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2564 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2566 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2567 DoEndDragResizeCol();
2569 // Note: we are ending the event *after* doing
2570 // default processing in this case
2572 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2579 // ------------ Right button down
2581 else if ( event
.RightDown() )
2583 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2588 // no default action at the moment
2593 // ------------ Right double click
2595 else if ( event
.RightDClick() )
2597 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2602 // no default action at the moment
2606 // ------------ Moving and no button action
2608 else if ( event
.Moving() && !event
.IsButton() )
2610 int dragRow
= YToEdgeOfRow( y
);
2611 int dragCol
= XToEdgeOfCol( x
);
2613 // Dragging on the corner of a cell to resize in both
2614 // directions is not implemented yet...
2616 if ( dragRow
>= 0 && dragCol
>= 0 )
2618 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2624 m_dragRowOrCol
= dragRow
;
2626 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2628 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
2636 m_dragRowOrCol
= dragCol
;
2638 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2640 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
2646 // Neither on a row or col edge
2648 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2650 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2657 void wxGrid::DoEndDragResizeRow()
2659 if ( m_dragLastPos
>= 0 )
2661 // erase the last line and resize the row
2663 int cw
, ch
, left
, dummy
;
2664 m_gridWin
->GetClientSize( &cw
, &ch
);
2665 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2667 wxClientDC
dc( m_gridWin
);
2669 dc
.SetLogicalFunction( wxINVERT
);
2670 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2671 HideCellEditControl();
2673 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
2674 SetRowSize( m_dragRowOrCol
,
2675 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
2677 if ( !GetBatchCount() )
2679 // Only needed to get the correct rect.y:
2680 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
2682 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
2683 rect
.width
= m_rowLabelWidth
;
2684 rect
.height
= ch
- rect
.y
;
2685 m_rowLabelWin
->Refresh( TRUE
, &rect
);
2687 m_gridWin
->Refresh( FALSE
, &rect
);
2690 ShowCellEditControl();
2695 void wxGrid::DoEndDragResizeCol()
2697 if ( m_dragLastPos
>= 0 )
2699 // erase the last line and resize the col
2701 int cw
, ch
, dummy
, top
;
2702 m_gridWin
->GetClientSize( &cw
, &ch
);
2703 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2705 wxClientDC
dc( m_gridWin
);
2707 dc
.SetLogicalFunction( wxINVERT
);
2708 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2709 HideCellEditControl();
2711 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
2712 SetColSize( m_dragRowOrCol
,
2713 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
2715 if ( !GetBatchCount() )
2717 // Only needed to get the correct rect.x:
2718 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
2720 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
2721 rect
.width
= cw
- rect
.x
;
2722 rect
.height
= m_colLabelHeight
;
2723 m_colLabelWin
->Refresh( TRUE
, &rect
);
2725 m_gridWin
->Refresh( FALSE
, &rect
);
2728 ShowCellEditControl();
2735 // ------ interaction with data model
2737 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
2739 switch ( msg
.GetId() )
2741 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
2742 return GetModelValues();
2744 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
2745 return SetModelValues();
2747 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2748 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2749 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2750 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2751 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2752 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2753 return Redimension( msg
);
2762 // The behaviour of this function depends on the grid table class
2763 // Clear() function. For the default wxGridStringTable class the
2764 // behavious is to replace all cell contents with wxEmptyString but
2765 // not to change the number of rows or cols.
2767 void wxGrid::ClearGrid()
2772 SetEditControlValue();
2773 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2778 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2780 // TODO: something with updateLabels flag
2784 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2790 bool ok
= m_table
->InsertRows( pos
, numRows
);
2792 // the table will have sent the results of the insert row
2793 // operation to this view object as a grid table message
2797 if ( m_numCols
== 0 )
2799 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2801 // TODO: perhaps instead of appending the default number of cols
2802 // we should remember what the last non-zero number of cols was ?
2806 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2808 // if we have just inserted cols into an empty grid the current
2809 // cell will be undefined...
2811 SetCurrentCell( 0, 0 );
2815 if ( !GetBatchCount() ) Refresh();
2818 SetEditControlValue();
2828 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2830 // TODO: something with updateLabels flag
2834 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2838 if ( m_table
&& m_table
->AppendRows( numRows
) )
2840 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2842 // if we have just inserted cols into an empty grid the current
2843 // cell will be undefined...
2845 SetCurrentCell( 0, 0 );
2848 // the table will have sent the results of the append row
2849 // operation to this view object as a grid table message
2852 if ( !GetBatchCount() ) Refresh();
2862 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2864 // TODO: something with updateLabels flag
2868 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2872 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2874 // the table will have sent the results of the delete row
2875 // operation to this view object as a grid table message
2877 if ( m_numRows
> 0 )
2878 SetEditControlValue();
2880 HideCellEditControl();
2883 if ( !GetBatchCount() ) Refresh();
2893 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2895 // TODO: something with updateLabels flag
2899 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2905 HideCellEditControl();
2906 bool ok
= m_table
->InsertCols( pos
, numCols
);
2908 // the table will have sent the results of the insert col
2909 // operation to this view object as a grid table message
2913 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2915 // if we have just inserted cols into an empty grid the current
2916 // cell will be undefined...
2918 SetCurrentCell( 0, 0 );
2922 if ( !GetBatchCount() ) Refresh();
2925 SetEditControlValue();
2935 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2937 // TODO: something with updateLabels flag
2941 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2945 if ( m_table
&& m_table
->AppendCols( numCols
) )
2947 // the table will have sent the results of the append col
2948 // operation to this view object as a grid table message
2950 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2952 // if we have just inserted cols into an empty grid the current
2953 // cell will be undefined...
2955 SetCurrentCell( 0, 0 );
2959 if ( !GetBatchCount() ) Refresh();
2969 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2971 // TODO: something with updateLabels flag
2975 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2979 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2981 // the table will have sent the results of the delete col
2982 // operation to this view object as a grid table message
2984 if ( m_numCols
> 0 )
2985 SetEditControlValue();
2987 HideCellEditControl();
2990 if ( !GetBatchCount() ) Refresh();
3002 // ----- event handlers
3005 // Generate a grid event based on a mouse event and
3006 // return the result of ProcessEvent()
3008 bool wxGrid::SendEvent( const wxEventType type
,
3010 wxMouseEvent
& mouseEv
)
3012 if ( type
== EVT_GRID_ROW_SIZE
||
3013 type
== EVT_GRID_COL_SIZE
)
3015 int rowOrCol
= (row
== -1 ? col
: row
);
3017 wxGridSizeEvent
gridEvt( GetId(),
3021 mouseEv
.GetX(), mouseEv
.GetY(),
3022 mouseEv
.ControlDown(),
3023 mouseEv
.ShiftDown(),
3025 mouseEv
.MetaDown() );
3027 return GetEventHandler()->ProcessEvent(gridEvt
);
3029 else if ( type
== EVT_GRID_RANGE_SELECT
)
3031 wxGridRangeSelectEvent
gridEvt( GetId(),
3035 m_selectedBottomRight
,
3036 mouseEv
.ControlDown(),
3037 mouseEv
.ShiftDown(),
3039 mouseEv
.MetaDown() );
3041 return GetEventHandler()->ProcessEvent(gridEvt
);
3045 wxGridEvent
gridEvt( GetId(),
3049 mouseEv
.GetX(), mouseEv
.GetY(),
3050 mouseEv
.ControlDown(),
3051 mouseEv
.ShiftDown(),
3053 mouseEv
.MetaDown() );
3055 return GetEventHandler()->ProcessEvent(gridEvt
);
3060 // Generate a grid event of specified type and return the result
3061 // of ProcessEvent().
3063 bool wxGrid::SendEvent( const wxEventType type
,
3066 if ( type
== EVT_GRID_ROW_SIZE
||
3067 type
== EVT_GRID_COL_SIZE
)
3069 int rowOrCol
= (row
== -1 ? col
: row
);
3071 wxGridSizeEvent
gridEvt( GetId(),
3076 return GetEventHandler()->ProcessEvent(gridEvt
);
3080 wxGridEvent
gridEvt( GetId(),
3085 return GetEventHandler()->ProcessEvent(gridEvt
);
3090 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3092 wxPaintDC
dc( this );
3094 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
3095 m_numRows
&& m_numCols
)
3097 m_currentCellCoords
.Set(0, 0);
3098 SetEditControlValue();
3099 ShowCellEditControl();
3106 // This is just here to make sure that CalcDimensions gets called when
3107 // the grid view is resized... then the size event is skipped to allow
3108 // the box sizers to handle everything
3110 void wxGrid::OnSize( wxSizeEvent
& event
)
3117 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
3119 if ( m_inOnKeyDown
)
3121 // shouldn't be here - we are going round in circles...
3123 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while alread active") );
3126 m_inOnKeyDown
= TRUE
;
3128 // propagate the event up and see if it gets processed
3130 wxWindow
*parent
= GetParent();
3131 wxKeyEvent
keyEvt( event
);
3132 keyEvt
.SetEventObject( parent
);
3134 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
3136 // try local handlers
3138 switch ( event
.KeyCode() )
3141 if ( event
.ControlDown() )
3143 MoveCursorUpBlock();
3152 if ( event
.ControlDown() )
3154 MoveCursorDownBlock();
3163 if ( event
.ControlDown() )
3165 MoveCursorLeftBlock();
3174 if ( event
.ControlDown() )
3176 MoveCursorRightBlock();
3185 if ( !IsEditable() )
3196 if ( event
.ControlDown() )
3198 event
.Skip(); // to let the edit control have the return
3207 if ( event
.ControlDown() )
3209 MakeCellVisible( 0, 0 );
3210 SetCurrentCell( 0, 0 );
3219 if ( event
.ControlDown() )
3221 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3222 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3239 // now try the cell edit control
3241 if ( IsCellEditControlEnabled() )
3243 event
.SetEventObject( m_cellEditCtrl
);
3244 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
3250 m_inOnKeyDown
= FALSE
;
3254 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3256 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3258 // the event has been intercepted - do nothing
3263 m_currentCellCoords
!= wxGridNoCellCoords
)
3265 HideCellEditControl();
3266 SaveEditControlValue();
3269 m_currentCellCoords
= coords
;
3271 SetEditControlValue();
3275 ShowCellEditControl();
3277 if ( IsSelection() )
3279 wxRect
r( SelectionToDeviceRect() );
3281 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3288 // ------ functions to get/send data (see also public functions)
3291 bool wxGrid::GetModelValues()
3295 // all we need to do is repaint the grid
3297 m_gridWin
->Refresh();
3305 bool wxGrid::SetModelValues()
3311 for ( row
= 0; row
< m_numRows
; row
++ )
3313 for ( col
= 0; col
< m_numCols
; col
++ )
3315 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3327 // Note - this function only draws cells that are in the list of
3328 // exposed cells (usually set from the update region by
3329 // CalcExposedCells)
3331 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3333 if ( !m_numRows
|| !m_numCols
) return;
3336 size_t numCells
= m_cellsExposed
.GetCount();
3338 for ( i
= 0; i
< numCells
; i
++ )
3340 DrawCell( dc
, m_cellsExposed
[i
] );
3345 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3347 int row
= coords
.GetRow();
3348 int col
= coords
.GetCol();
3350 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3353 // we draw the cell border ourselves
3354 #if !WXGRID_DRAW_LINES
3355 if ( m_gridLinesEnabled
)
3356 DrawCellBorder( dc
, coords
);
3359 // but all the rest is drawn by the cell renderer and hence may be
3361 wxGridCellRenderer
*renderer
= GetCellRenderer(row
, col
);
3363 rect
.x
= m_colRights
[col
] - m_colWidths
[col
] + 1;
3364 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
] + 1;
3365 rect
.width
= m_colWidths
[col
] - 1;
3366 rect
.height
= m_rowHeights
[row
] - 1;
3368 renderer
->Draw(*this, dc
, rect
, row
, col
, IsInSelection(coords
));
3371 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3373 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3374 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3376 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3377 int row
= coords
.GetRow();
3378 int col
= coords
.GetCol();
3380 // right hand border
3382 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3383 m_colRights
[col
], m_rowBottoms
[row
] );
3387 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3388 m_colRights
[col
], m_rowBottoms
[row
] );
3392 // TODO: remove this ???
3393 // This is used to redraw all grid lines e.g. when the grid line colour
3396 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3398 if ( !m_gridLinesEnabled
||
3400 !m_numCols
) return;
3402 int top
, bottom
, left
, right
;
3406 m_gridWin
->GetClientSize(&cw
, &ch
);
3408 // virtual coords of visible area
3410 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3411 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3415 reg
.GetBox(x
, y
, w
, h
);
3416 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3417 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3420 // avoid drawing grid lines past the last row and col
3422 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3423 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3425 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3427 // horizontal grid lines
3430 for ( i
= 0; i
< m_numRows
; i
++ )
3432 if ( m_rowBottoms
[i
] > bottom
)
3436 else if ( m_rowBottoms
[i
] >= top
)
3438 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
3443 // vertical grid lines
3445 for ( i
= 0; i
< m_numCols
; i
++ )
3447 if ( m_colRights
[i
] > right
)
3451 else if ( m_colRights
[i
] >= left
)
3453 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
3459 void wxGrid::DrawRowLabels( wxDC
& dc
)
3461 if ( !m_numRows
|| !m_numCols
) return;
3464 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3466 for ( i
= 0; i
< numLabels
; i
++ )
3468 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3473 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
3475 if ( m_rowHeights
[row
] <= 0 ) return;
3477 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3479 dc
.SetPen( *wxBLACK_PEN
);
3480 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3481 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3483 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3484 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3486 dc
.SetPen( *wxWHITE_PEN
);
3487 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
3488 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
3490 dc
.SetBackgroundMode( wxTRANSPARENT
);
3491 dc
.SetTextForeground( GetLabelTextColour() );
3492 dc
.SetFont( GetLabelFont() );
3495 GetRowLabelAlignment( &hAlign
, &vAlign
);
3499 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3500 rect
.SetWidth( m_rowLabelWidth
- 4 );
3501 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3502 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
3506 void wxGrid::DrawColLabels( wxDC
& dc
)
3508 if ( !m_numRows
|| !m_numCols
) return;
3511 size_t numLabels
= m_colLabelsExposed
.GetCount();
3513 for ( i
= 0; i
< numLabels
; i
++ )
3515 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
3520 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
3522 if ( m_colWidths
[col
] <= 0 ) return;
3524 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
3526 dc
.SetPen( *wxBLACK_PEN
);
3527 dc
.DrawLine( m_colRights
[col
]-1, 0,
3528 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3530 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
3531 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3533 dc
.SetPen( *wxWHITE_PEN
);
3534 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
3535 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
3537 dc
.SetBackgroundMode( wxTRANSPARENT
);
3538 dc
.SetTextForeground( GetLabelTextColour() );
3539 dc
.SetFont( GetLabelFont() );
3541 dc
.SetBackgroundMode( wxTRANSPARENT
);
3542 dc
.SetTextForeground( GetLabelTextColour() );
3543 dc
.SetFont( GetLabelFont() );
3546 GetColLabelAlignment( &hAlign
, &vAlign
);
3549 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3551 rect
.SetWidth( m_colWidths
[col
] - 4 );
3552 rect
.SetHeight( m_colLabelHeight
- 4 );
3553 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
3557 void wxGrid::DrawTextRectangle( wxDC
& dc
,
3558 const wxString
& value
,
3563 long textWidth
, textHeight
;
3564 long lineWidth
, lineHeight
;
3565 wxArrayString lines
;
3567 dc
.SetClippingRegion( rect
);
3568 StringToLines( value
, lines
);
3569 if ( lines
.GetCount() )
3571 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
3572 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
3575 switch ( horizAlign
)
3578 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
3582 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
3591 switch ( vertAlign
)
3594 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
3598 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
3607 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
3609 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
3614 dc
.DestroyClippingRegion();
3618 // Split multi line text up into an array of strings. Any existing
3619 // contents of the string array are preserved.
3621 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
3625 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
3626 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
3628 while ( startPos
< (int)tVal
.Length() )
3630 pos
= tVal
.Mid(startPos
).Find( eol
);
3635 else if ( pos
== 0 )
3637 lines
.Add( wxEmptyString
);
3641 lines
.Add( value
.Mid(startPos
, pos
) );
3645 if ( startPos
< (int)value
.Length() )
3647 lines
.Add( value
.Mid( startPos
) );
3652 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3653 wxArrayString
& lines
,
3654 long *width
, long *height
)
3661 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3663 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3664 w
= wxMax( w
, lineW
);
3674 // ------ Edit control functions
3678 void wxGrid::EnableEditing( bool edit
)
3680 // TODO: improve this ?
3682 if ( edit
!= m_editable
)
3686 // TODO: extend this for other edit control types
3688 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3690 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
3696 #if 0 // disabled for the moment - the cell control is always active
3697 void wxGrid::EnableCellEditControl( bool enable
)
3699 if ( m_cellEditCtrl
&&
3700 enable
!= m_cellEditCtrlEnabled
)
3702 m_cellEditCtrlEnabled
= enable
;
3704 if ( m_cellEditCtrlEnabled
)
3706 SetEditControlValue();
3707 ShowCellEditControl();
3711 HideCellEditControl();
3712 SaveEditControlValue();
3719 void wxGrid::ShowCellEditControl()
3723 if ( IsCellEditControlEnabled() )
3725 if ( !IsVisible( m_currentCellCoords
) )
3731 rect
= CellToRect( m_currentCellCoords
);
3733 // convert to scrolled coords
3735 int left
, top
, right
, bottom
;
3736 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3737 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3740 m_gridWin
->GetClientSize( &cw
, &ch
);
3742 // Make the edit control large enough to allow for internal margins
3743 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3746 #if defined(__WXMOTIF__)
3747 if ( m_currentCellCoords
.GetRow() == 0 ||
3748 m_currentCellCoords
.GetCol() == 0 )
3757 if ( m_currentCellCoords
.GetRow() == 0 ||
3758 m_currentCellCoords
.GetCol() == 0 )
3768 #if defined(__WXGTK__)
3771 if (left
!= 0) left_diff
++;
3772 if (top
!= 0) top_diff
++;
3773 rect
.SetLeft( left
+ left_diff
);
3774 rect
.SetTop( top
+ top_diff
);
3775 rect
.SetRight( rect
.GetRight() - left_diff
);
3776 rect
.SetBottom( rect
.GetBottom() - top_diff
);
3778 rect
.SetLeft( wxMax(0, left
- extra
) );
3779 rect
.SetTop( wxMax(0, top
- extra
) );
3780 rect
.SetRight( rect
.GetRight() + 2*extra
);
3781 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3784 m_cellEditCtrl
->SetSize( rect
);
3785 m_cellEditCtrl
->Show( TRUE
);
3787 switch ( m_editCtrlType
)
3789 case wxGRID_TEXTCTRL
:
3790 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3793 case wxGRID_CHECKBOX
:
3794 // TODO: anything ???
3799 // TODO: anything ???
3803 case wxGRID_COMBOBOX
:
3804 // TODO: anything ???
3809 m_cellEditCtrl
->SetFocus();
3815 void wxGrid::HideCellEditControl()
3817 if ( IsCellEditControlEnabled() )
3819 m_cellEditCtrl
->Show( FALSE
);
3824 void wxGrid::SetEditControlValue( const wxString
& value
)
3830 s
= GetCellValue(m_currentCellCoords
);
3834 if ( IsCellEditControlEnabled() )
3836 switch ( m_editCtrlType
)
3838 case wxGRID_TEXTCTRL
:
3839 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3842 case wxGRID_CHECKBOX
:
3843 // TODO: implement this
3848 // TODO: implement this
3852 case wxGRID_COMBOBOX
:
3853 // TODO: implement this
3862 void wxGrid::SaveEditControlValue()
3866 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3868 if ( IsCellEditControlEnabled() )
3870 ctrl
= m_cellEditCtrl
;
3877 bool valueChanged
= FALSE
;
3879 switch ( m_editCtrlType
)
3881 case wxGRID_TEXTCTRL
:
3882 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3883 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3884 SetCellValue( m_currentCellCoords
,
3885 ((wxTextCtrl
*) ctrl
)->GetValue() );
3888 case wxGRID_CHECKBOX
:
3889 // TODO: implement this
3894 // TODO: implement this
3898 case wxGRID_COMBOBOX
:
3899 // TODO: implement this
3906 SendEvent( EVT_GRID_CELL_CHANGE
,
3907 m_currentCellCoords
.GetRow(),
3908 m_currentCellCoords
.GetCol() );
3915 // ------ Grid location functions
3916 // Note that all of these functions work with the logical coordinates of
3917 // grid cells and labels so you will need to convert from device
3918 // coordinates for mouse events etc.
3921 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3923 int row
= YToRow(y
);
3924 int col
= XToCol(x
);
3926 if ( row
== -1 || col
== -1 )
3928 coords
= wxGridNoCellCoords
;
3932 coords
.Set( row
, col
);
3937 int wxGrid::YToRow( int y
)
3941 for ( i
= 0; i
< m_numRows
; i
++ )
3943 if ( y
< m_rowBottoms
[i
] ) return i
;
3950 int wxGrid::XToCol( int x
)
3954 for ( i
= 0; i
< m_numCols
; i
++ )
3956 if ( x
< m_colRights
[i
] ) return i
;
3963 // return the row number that that the y coord is near the edge of, or
3964 // -1 if not near an edge
3966 int wxGrid::YToEdgeOfRow( int y
)
3970 for ( i
= 0; i
< m_numRows
; i
++ )
3972 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3974 d
= abs( y
- m_rowBottoms
[i
] );
3976 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3985 // return the col number that that the x coord is near the edge of, or
3986 // -1 if not near an edge
3988 int wxGrid::XToEdgeOfCol( int x
)
3992 for ( i
= 0; i
< m_numCols
; i
++ )
3994 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3996 d
= abs( x
- m_colRights
[i
] );
3998 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
4007 wxRect
wxGrid::CellToRect( int row
, int col
)
4009 wxRect
rect( -1, -1, -1, -1 );
4011 if ( row
>= 0 && row
< m_numRows
&&
4012 col
>= 0 && col
< m_numCols
)
4014 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
4015 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
4016 rect
.width
= m_colWidths
[col
];
4017 rect
.height
= m_rowHeights
[ row
];
4024 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
4026 // get the cell rectangle in logical coords
4028 wxRect
r( CellToRect( row
, col
) );
4030 // convert to device coords
4032 int left
, top
, right
, bottom
;
4033 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4034 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4036 // check against the client area of the grid window
4039 m_gridWin
->GetClientSize( &cw
, &ch
);
4041 if ( wholeCellVisible
)
4043 // is the cell wholly visible ?
4045 return ( left
>= 0 && right
<= cw
&&
4046 top
>= 0 && bottom
<= ch
);
4050 // is the cell partly visible ?
4052 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
4053 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
4058 // make the specified cell location visible by doing a minimal amount
4061 void wxGrid::MakeCellVisible( int row
, int col
)
4064 int xpos
= -1, ypos
= -1;
4066 if ( row
>= 0 && row
< m_numRows
&&
4067 col
>= 0 && col
< m_numCols
)
4069 // get the cell rectangle in logical coords
4071 wxRect
r( CellToRect( row
, col
) );
4073 // convert to device coords
4075 int left
, top
, right
, bottom
;
4076 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4077 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4080 m_gridWin
->GetClientSize( &cw
, &ch
);
4086 else if ( bottom
> ch
)
4088 int h
= r
.GetHeight();
4090 for ( i
= row
-1; i
>= 0; i
-- )
4092 if ( h
+ m_rowHeights
[i
] > ch
) break;
4094 h
+= m_rowHeights
[i
];
4095 ypos
-= m_rowHeights
[i
];
4098 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
4099 // have rounding errors (this is important, because if we do, we
4100 // might not scroll at all and some cells won't be redrawn)
4101 ypos
+= GRID_SCROLL_LINE
/ 2;
4108 else if ( right
> cw
)
4110 int w
= r
.GetWidth();
4112 for ( i
= col
-1; i
>= 0; i
-- )
4114 if ( w
+ m_colWidths
[i
] > cw
) break;
4116 w
+= m_colWidths
[i
];
4117 xpos
-= m_colWidths
[i
];
4120 // see comment for ypos above
4121 xpos
+= GRID_SCROLL_LINE
/ 2;
4124 if ( xpos
!= -1 || ypos
!= -1 )
4126 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
4127 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
4128 Scroll( xpos
, ypos
);
4136 // ------ Grid cursor movement functions
4139 bool wxGrid::MoveCursorUp()
4141 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4142 m_currentCellCoords
.GetRow() > 0 )
4144 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
4145 m_currentCellCoords
.GetCol() );
4147 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
4148 m_currentCellCoords
.GetCol() );
4157 bool wxGrid::MoveCursorDown()
4159 // TODO: allow for scrolling
4161 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4162 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4164 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
4165 m_currentCellCoords
.GetCol() );
4167 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
4168 m_currentCellCoords
.GetCol() );
4177 bool wxGrid::MoveCursorLeft()
4179 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4180 m_currentCellCoords
.GetCol() > 0 )
4182 MakeCellVisible( m_currentCellCoords
.GetRow(),
4183 m_currentCellCoords
.GetCol() - 1 );
4185 SetCurrentCell( m_currentCellCoords
.GetRow(),
4186 m_currentCellCoords
.GetCol() - 1 );
4195 bool wxGrid::MoveCursorRight()
4197 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4198 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4200 MakeCellVisible( m_currentCellCoords
.GetRow(),
4201 m_currentCellCoords
.GetCol() + 1 );
4203 SetCurrentCell( m_currentCellCoords
.GetRow(),
4204 m_currentCellCoords
.GetCol() + 1 );
4213 bool wxGrid::MovePageUp()
4215 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4217 int row
= m_currentCellCoords
.GetRow();
4221 m_gridWin
->GetClientSize( &cw
, &ch
);
4223 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4224 int newRow
= YToRow( y
- ch
+ 1 );
4229 else if ( newRow
== row
)
4234 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4235 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4243 bool wxGrid::MovePageDown()
4245 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4247 int row
= m_currentCellCoords
.GetRow();
4248 if ( row
< m_numRows
)
4251 m_gridWin
->GetClientSize( &cw
, &ch
);
4253 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4254 int newRow
= YToRow( y
+ ch
);
4257 newRow
= m_numRows
- 1;
4259 else if ( newRow
== row
)
4264 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4265 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4273 bool wxGrid::MoveCursorUpBlock()
4276 m_currentCellCoords
!= wxGridNoCellCoords
&&
4277 m_currentCellCoords
.GetRow() > 0 )
4279 int row
= m_currentCellCoords
.GetRow();
4280 int col
= m_currentCellCoords
.GetCol();
4282 if ( m_table
->IsEmptyCell(row
, col
) )
4284 // starting in an empty cell: find the next block of
4290 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4293 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4295 // starting at the top of a block: find the next block
4301 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4306 // starting within a block: find the top of the block
4311 if ( m_table
->IsEmptyCell(row
, col
) )
4319 MakeCellVisible( row
, col
);
4320 SetCurrentCell( row
, col
);
4328 bool wxGrid::MoveCursorDownBlock()
4331 m_currentCellCoords
!= wxGridNoCellCoords
&&
4332 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4334 int row
= m_currentCellCoords
.GetRow();
4335 int col
= m_currentCellCoords
.GetCol();
4337 if ( m_table
->IsEmptyCell(row
, col
) )
4339 // starting in an empty cell: find the next block of
4342 while ( row
< m_numRows
-1 )
4345 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4348 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4350 // starting at the bottom of a block: find the next block
4353 while ( row
< m_numRows
-1 )
4356 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4361 // starting within a block: find the bottom of the block
4363 while ( row
< m_numRows
-1 )
4366 if ( m_table
->IsEmptyCell(row
, col
) )
4374 MakeCellVisible( row
, col
);
4375 SetCurrentCell( row
, col
);
4383 bool wxGrid::MoveCursorLeftBlock()
4386 m_currentCellCoords
!= wxGridNoCellCoords
&&
4387 m_currentCellCoords
.GetCol() > 0 )
4389 int row
= m_currentCellCoords
.GetRow();
4390 int col
= m_currentCellCoords
.GetCol();
4392 if ( m_table
->IsEmptyCell(row
, col
) )
4394 // starting in an empty cell: find the next block of
4400 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4403 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4405 // starting at the left of a block: find the next block
4411 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4416 // starting within a block: find the left of the block
4421 if ( m_table
->IsEmptyCell(row
, col
) )
4429 MakeCellVisible( row
, col
);
4430 SetCurrentCell( row
, col
);
4438 bool wxGrid::MoveCursorRightBlock()
4441 m_currentCellCoords
!= wxGridNoCellCoords
&&
4442 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4444 int row
= m_currentCellCoords
.GetRow();
4445 int col
= m_currentCellCoords
.GetCol();
4447 if ( m_table
->IsEmptyCell(row
, col
) )
4449 // starting in an empty cell: find the next block of
4452 while ( col
< m_numCols
-1 )
4455 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4458 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4460 // starting at the right of a block: find the next block
4463 while ( col
< m_numCols
-1 )
4466 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4471 // starting within a block: find the right of the block
4473 while ( col
< m_numCols
-1 )
4476 if ( m_table
->IsEmptyCell(row
, col
) )
4484 MakeCellVisible( row
, col
);
4485 SetCurrentCell( row
, col
);
4496 // ------ Label values and formatting
4499 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4501 *horiz
= m_rowLabelHorizAlign
;
4502 *vert
= m_rowLabelVertAlign
;
4505 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4507 *horiz
= m_colLabelHorizAlign
;
4508 *vert
= m_colLabelVertAlign
;
4511 wxString
wxGrid::GetRowLabelValue( int row
)
4515 return m_table
->GetRowLabelValue( row
);
4525 wxString
wxGrid::GetColLabelValue( int col
)
4529 return m_table
->GetColLabelValue( col
);
4540 void wxGrid::SetRowLabelSize( int width
)
4542 width
= wxMax( width
, 0 );
4543 if ( width
!= m_rowLabelWidth
)
4547 m_rowLabelWin
->Show( FALSE
);
4548 m_cornerLabelWin
->Show( FALSE
);
4550 else if ( m_rowLabelWidth
== 0 )
4552 m_rowLabelWin
->Show( TRUE
);
4553 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4556 m_rowLabelWidth
= width
;
4563 void wxGrid::SetColLabelSize( int height
)
4565 height
= wxMax( height
, 0 );
4566 if ( height
!= m_colLabelHeight
)
4570 m_colLabelWin
->Show( FALSE
);
4571 m_cornerLabelWin
->Show( FALSE
);
4573 else if ( m_colLabelHeight
== 0 )
4575 m_colLabelWin
->Show( TRUE
);
4576 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4579 m_colLabelHeight
= height
;
4586 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
4588 if ( m_labelBackgroundColour
!= colour
)
4590 m_labelBackgroundColour
= colour
;
4591 m_rowLabelWin
->SetBackgroundColour( colour
);
4592 m_colLabelWin
->SetBackgroundColour( colour
);
4593 m_cornerLabelWin
->SetBackgroundColour( colour
);
4595 if ( !GetBatchCount() )
4597 m_rowLabelWin
->Refresh();
4598 m_colLabelWin
->Refresh();
4599 m_cornerLabelWin
->Refresh();
4604 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
4606 if ( m_labelTextColour
!= colour
)
4608 m_labelTextColour
= colour
;
4609 if ( !GetBatchCount() )
4611 m_rowLabelWin
->Refresh();
4612 m_colLabelWin
->Refresh();
4617 void wxGrid::SetLabelFont( const wxFont
& font
)
4620 if ( !GetBatchCount() )
4622 m_rowLabelWin
->Refresh();
4623 m_colLabelWin
->Refresh();
4627 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
4629 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4631 m_rowLabelHorizAlign
= horiz
;
4634 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4636 m_rowLabelVertAlign
= vert
;
4639 if ( !GetBatchCount() )
4641 m_rowLabelWin
->Refresh();
4645 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
4647 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4649 m_colLabelHorizAlign
= horiz
;
4652 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4654 m_colLabelVertAlign
= vert
;
4657 if ( !GetBatchCount() )
4659 m_colLabelWin
->Refresh();
4663 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
4667 m_table
->SetRowLabelValue( row
, s
);
4668 if ( !GetBatchCount() )
4670 wxRect rect
= CellToRect( row
, 0);
4671 if ( rect
.height
> 0 )
4673 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
4675 rect
.width
= m_rowLabelWidth
;
4676 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4682 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
4686 m_table
->SetColLabelValue( col
, s
);
4687 if ( !GetBatchCount() )
4689 wxRect rect
= CellToRect( 0, col
);
4690 if ( rect
.width
> 0 )
4692 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
4694 rect
.height
= m_colLabelHeight
;
4695 m_colLabelWin
->Refresh( TRUE
, &rect
);
4701 void wxGrid::SetGridLineColour( const wxColour
& colour
)
4703 if ( m_gridLineColour
!= colour
)
4705 m_gridLineColour
= colour
;
4707 wxClientDC
dc( m_gridWin
);
4709 DrawAllGridLines( dc
, wxRegion() );
4713 void wxGrid::EnableGridLines( bool enable
)
4715 if ( enable
!= m_gridLinesEnabled
)
4717 m_gridLinesEnabled
= enable
;
4719 if ( !GetBatchCount() )
4723 wxClientDC
dc( m_gridWin
);
4725 DrawAllGridLines( dc
, wxRegion() );
4729 m_gridWin
->Refresh();
4736 int wxGrid::GetDefaultRowSize()
4738 return m_defaultRowHeight
;
4741 int wxGrid::GetRowSize( int row
)
4743 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
4745 return m_rowHeights
[row
];
4748 int wxGrid::GetDefaultColSize()
4750 return m_defaultColWidth
;
4753 int wxGrid::GetColSize( int col
)
4755 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
4757 return m_colWidths
[col
];
4760 // ============================================================================
4761 // access to the grid attributes: each of them has a default value in the grid
4762 // itself and may be overidden on a per-cell basis
4763 // ============================================================================
4765 // ----------------------------------------------------------------------------
4766 // setting default attributes
4767 // ----------------------------------------------------------------------------
4769 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
4771 m_gridWin
->SetBackgroundColour(col
);
4774 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
4776 m_gridWin
->SetForegroundColour(col
);
4779 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
4781 m_defaultCellHAlign
= horiz
;
4782 m_defaultCellVAlign
= vert
;
4785 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
4787 m_defaultCellFont
= font
;
4790 // ----------------------------------------------------------------------------
4791 // access to the default attrbiutes
4792 // ----------------------------------------------------------------------------
4794 wxColour
wxGrid::GetDefaultCellBackgroundColour()
4796 return m_gridWin
->GetBackgroundColour();
4799 wxColour
wxGrid::GetDefaultCellTextColour()
4801 return m_gridWin
->GetForegroundColour();
4804 wxFont
wxGrid::GetDefaultCellFont()
4806 return m_defaultCellFont
;
4809 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
4812 *horiz
= m_defaultCellHAlign
;
4814 *vert
= m_defaultCellVAlign
;
4817 wxGridCellRenderer
*wxGrid::GetCellRenderer(int row
, int col
)
4819 wxGridCellRenderer
*renderer
= (wxGridCellRenderer
*)NULL
;
4820 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4823 renderer
= attr
->GetRenderer();
4830 if ( !m_defaultRenderer
)
4832 m_defaultRenderer
= new wxGridCellStringRenderer
;
4835 renderer
= m_defaultRenderer
;
4841 // ----------------------------------------------------------------------------
4842 // access to cell attributes
4843 // ----------------------------------------------------------------------------
4845 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
4847 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4850 if ( attr
&& attr
->HasBackgroundColour() )
4851 colour
= attr
->GetBackgroundColour();
4853 colour
= GetDefaultCellBackgroundColour();
4860 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
4862 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4865 if ( attr
&& attr
->HasTextColour() )
4866 colour
= attr
->GetTextColour();
4868 colour
= GetDefaultCellTextColour();
4875 wxFont
wxGrid::GetCellFont( int row
, int col
)
4877 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4880 if ( attr
&& attr
->HasFont() )
4881 font
= attr
->GetFont();
4883 font
= GetDefaultCellFont();
4890 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
4892 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4894 if ( attr
&& attr
->HasAlignment() )
4895 attr
->GetAlignment(horiz
, vert
);
4897 GetDefaultCellAlignment(horiz
, vert
);
4902 // ----------------------------------------------------------------------------
4903 // attribute support: cache, automatic provider creation, ...
4904 // ----------------------------------------------------------------------------
4906 bool wxGrid::CanHaveAttributes()
4913 if ( !m_table
->GetAttrProvider() )
4915 // use the default attr provider by default
4916 // (another choice would be to just return FALSE thus forcing the user
4918 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
4924 void wxGrid::ClearAttrCache()
4926 if ( m_attrCache
.row
!= -1 )
4928 m_attrCache
.attr
->SafeDecRef();
4929 m_attrCache
.row
= -1;
4933 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
4935 wxGrid
*self
= (wxGrid
*)this; // const_cast
4937 self
->ClearAttrCache();
4938 self
->m_attrCache
.row
= row
;
4939 self
->m_attrCache
.col
= col
;
4940 self
->m_attrCache
.attr
= attr
;
4944 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
4946 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
4948 *attr
= m_attrCache
.attr
;
4949 (*attr
)->SafeIncRef();
4951 #ifdef DEBUG_ATTR_CACHE
4952 gs_nAttrCacheHits
++;
4959 #ifdef DEBUG_ATTR_CACHE
4960 gs_nAttrCacheMisses
++;
4966 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
4968 wxGridCellAttr
*attr
;
4969 if ( !LookupAttr(row
, col
, &attr
) )
4971 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
4972 CacheAttr(row
, col
, attr
);
4978 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
4980 wxGridCellAttr
*attr
;
4981 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
4983 wxASSERT_MSG( m_table
,
4984 _T("we may only be called if CanHaveAttributes() "
4985 "returned TRUE and then m_table should be !NULL") );
4987 attr
= m_table
->GetAttr(row
, col
);
4990 attr
= new wxGridCellAttr
;
4992 // artificially inc the ref count to match DecRef() in caller
4995 m_table
->SetAttr(attr
, row
, col
);
4998 CacheAttr(row
, col
, attr
);
5004 // ----------------------------------------------------------------------------
5005 // setting cell attributes: this is forwarded to the table
5006 // ----------------------------------------------------------------------------
5008 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
5010 if ( CanHaveAttributes() )
5012 m_table
->SetRowAttr(attr
, row
);
5020 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
5022 if ( CanHaveAttributes() )
5024 m_table
->SetColAttr(attr
, col
);
5032 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
5034 if ( CanHaveAttributes() )
5036 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5037 attr
->SetBackgroundColour(colour
);
5042 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
5044 if ( CanHaveAttributes() )
5046 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5047 attr
->SetTextColour(colour
);
5052 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
5054 if ( CanHaveAttributes() )
5056 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5057 attr
->SetFont(font
);
5062 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
5064 if ( CanHaveAttributes() )
5066 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5067 attr
->SetAlignment(horiz
, vert
);
5072 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
5074 if ( CanHaveAttributes() )
5076 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
5077 attr
->SetRenderer(renderer
);
5082 // ----------------------------------------------------------------------------
5084 // ----------------------------------------------------------------------------
5086 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
5088 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
5090 if ( resizeExistingRows
)
5094 for ( row
= 0; row
< m_numRows
; row
++ )
5096 m_rowHeights
[row
] = m_defaultRowHeight
;
5097 bottom
+= m_defaultRowHeight
;
5098 m_rowBottoms
[row
] = bottom
;
5104 void wxGrid::SetRowSize( int row
, int height
)
5106 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
5110 int h
= wxMax( 0, height
);
5111 int diff
= h
- m_rowHeights
[row
];
5113 m_rowHeights
[row
] = h
;
5114 for ( i
= row
; i
< m_numRows
; i
++ )
5116 m_rowBottoms
[i
] += diff
;
5121 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
5123 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
5125 if ( resizeExistingCols
)
5129 for ( col
= 0; col
< m_numCols
; col
++ )
5131 m_colWidths
[col
] = m_defaultColWidth
;
5132 right
+= m_defaultColWidth
;
5133 m_colRights
[col
] = right
;
5139 void wxGrid::SetColSize( int col
, int width
)
5141 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
5145 int w
= wxMax( 0, width
);
5146 int diff
= w
- m_colWidths
[col
];
5147 m_colWidths
[col
] = w
;
5149 for ( i
= col
; i
< m_numCols
; i
++ )
5151 m_colRights
[i
] += diff
;
5158 // ------ cell value accessor functions
5161 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
5165 m_table
->SetValue( row
, col
, s
.c_str() );
5166 if ( !GetBatchCount() )
5168 wxClientDC
dc( m_gridWin
);
5170 DrawCell( dc
, wxGridCellCoords(row
, col
) );
5173 #if 0 // TODO: edit in place
5175 if ( m_currentCellCoords
.GetRow() == row
&&
5176 m_currentCellCoords
.GetCol() == col
)
5178 SetEditControlValue( s
);
5187 // ------ Block, row and col selection
5190 void wxGrid::SelectRow( int row
, bool addToSelected
)
5194 if ( IsSelection() && addToSelected
)
5197 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5200 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5201 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5202 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5203 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5207 need_refresh
[0] = TRUE
;
5208 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
5209 wxGridCellCoords ( oldTop
- 1,
5211 m_selectedTopLeft
.SetRow( row
);
5216 need_refresh
[1] = TRUE
;
5217 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
5218 wxGridCellCoords ( oldBottom
,
5221 m_selectedTopLeft
.SetCol( 0 );
5224 if ( oldBottom
< row
)
5226 need_refresh
[2] = TRUE
;
5227 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
5228 wxGridCellCoords ( row
,
5230 m_selectedBottomRight
.SetRow( row
);
5233 if ( oldRight
< m_numCols
- 1 )
5235 need_refresh
[3] = TRUE
;
5236 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5238 wxGridCellCoords ( oldBottom
,
5240 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
5243 for (i
= 0; i
< 4; i
++ )
5244 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5245 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5249 r
= SelectionToDeviceRect();
5251 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5253 m_selectedTopLeft
.Set( row
, 0 );
5254 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
5255 r
= SelectionToDeviceRect();
5256 m_gridWin
->Refresh( FALSE
, &r
);
5259 wxGridRangeSelectEvent
gridEvt( GetId(),
5260 EVT_GRID_RANGE_SELECT
,
5263 m_selectedBottomRight
);
5265 GetEventHandler()->ProcessEvent(gridEvt
);
5269 void wxGrid::SelectCol( int col
, bool addToSelected
)
5271 if ( IsSelection() && addToSelected
)
5274 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5277 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5278 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5279 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5280 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5282 if ( oldLeft
> col
)
5284 need_refresh
[0] = TRUE
;
5285 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5286 wxGridCellCoords ( m_numRows
- 1,
5288 m_selectedTopLeft
.SetCol( col
);
5293 need_refresh
[1] = TRUE
;
5294 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5295 wxGridCellCoords ( oldTop
- 1,
5297 m_selectedTopLeft
.SetRow( 0 );
5300 if ( oldRight
< col
)
5302 need_refresh
[2] = TRUE
;
5303 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5304 wxGridCellCoords ( m_numRows
- 1,
5306 m_selectedBottomRight
.SetCol( col
);
5309 if ( oldBottom
< m_numRows
- 1 )
5311 need_refresh
[3] = TRUE
;
5312 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5314 wxGridCellCoords ( m_numRows
- 1,
5316 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5319 for (i
= 0; i
< 4; i
++ )
5320 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5321 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5327 r
= SelectionToDeviceRect();
5329 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5331 m_selectedTopLeft
.Set( 0, col
);
5332 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5333 r
= SelectionToDeviceRect();
5334 m_gridWin
->Refresh( FALSE
, &r
);
5337 wxGridRangeSelectEvent
gridEvt( GetId(),
5338 EVT_GRID_RANGE_SELECT
,
5341 m_selectedBottomRight
);
5343 GetEventHandler()->ProcessEvent(gridEvt
);
5347 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5350 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5352 if ( topRow
> bottomRow
)
5359 if ( leftCol
> rightCol
)
5366 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5367 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5369 if ( m_selectedTopLeft
!= updateTopLeft
||
5370 m_selectedBottomRight
!= updateBottomRight
)
5372 // Compute two optimal update rectangles:
5373 // Either one rectangle is a real subset of the
5374 // other, or they are (almost) disjoint!
5376 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5379 // Store intermediate values
5380 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5381 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5382 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5383 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5385 // Determine the outer/inner coordinates.
5386 if (oldLeft
> leftCol
)
5392 if (oldTop
> topRow
)
5398 if (oldRight
< rightCol
)
5401 oldRight
= rightCol
;
5404 if (oldBottom
< bottomRow
)
5407 oldBottom
= bottomRow
;
5411 // Now, either the stuff marked old is the outer
5412 // rectangle or we don't have a situation where one
5413 // is contained in the other.
5415 if ( oldLeft
< leftCol
)
5417 need_refresh
[0] = TRUE
;
5418 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5420 wxGridCellCoords ( oldBottom
,
5424 if ( oldTop
< topRow
)
5426 need_refresh
[1] = TRUE
;
5427 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5429 wxGridCellCoords ( topRow
- 1,
5433 if ( oldRight
> rightCol
)
5435 need_refresh
[2] = TRUE
;
5436 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5438 wxGridCellCoords ( oldBottom
,
5442 if ( oldBottom
> bottomRow
)
5444 need_refresh
[3] = TRUE
;
5445 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5447 wxGridCellCoords ( oldBottom
,
5453 m_selectedTopLeft
= updateTopLeft
;
5454 m_selectedBottomRight
= updateBottomRight
;
5456 // various Refresh() calls
5457 for (i
= 0; i
< 4; i
++ )
5458 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5459 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5462 // only generate an event if the block is not being selected by
5463 // dragging the mouse (in which case the event will be generated in
5464 // the mouse event handler)
5465 if ( !m_isDragging
)
5467 wxGridRangeSelectEvent
gridEvt( GetId(),
5468 EVT_GRID_RANGE_SELECT
,
5471 m_selectedBottomRight
);
5473 GetEventHandler()->ProcessEvent(gridEvt
);
5477 void wxGrid::SelectAll()
5479 m_selectedTopLeft
.Set( 0, 0 );
5480 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5482 m_gridWin
->Refresh();
5486 void wxGrid::ClearSelection()
5488 m_selectedTopLeft
= wxGridNoCellCoords
;
5489 m_selectedBottomRight
= wxGridNoCellCoords
;
5493 // This function returns the rectangle that encloses the given block
5494 // in device coords clipped to the client size of the grid window.
5496 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5497 const wxGridCellCoords
&bottomRight
)
5499 wxRect
rect( wxGridNoCellRect
);
5502 cellRect
= CellToRect( topLeft
);
5503 if ( cellRect
!= wxGridNoCellRect
)
5509 rect
= wxRect( 0, 0, 0, 0 );
5512 cellRect
= CellToRect( bottomRight
);
5513 if ( cellRect
!= wxGridNoCellRect
)
5519 return wxGridNoCellRect
;
5522 // convert to scrolled coords
5524 int left
, top
, right
, bottom
;
5525 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5526 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5529 m_gridWin
->GetClientSize( &cw
, &ch
);
5531 rect
.SetLeft( wxMax(0, left
) );
5532 rect
.SetTop( wxMax(0, top
) );
5533 rect
.SetRight( wxMin(cw
, right
) );
5534 rect
.SetBottom( wxMin(ch
, bottom
) );
5542 // ------ Grid event classes
5545 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5547 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5548 int row
, int col
, int x
, int y
,
5549 bool control
, bool shift
, bool alt
, bool meta
)
5550 : wxNotifyEvent( type
, id
)
5556 m_control
= control
;
5561 SetEventObject(obj
);
5565 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
5567 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
5568 int rowOrCol
, int x
, int y
,
5569 bool control
, bool shift
, bool alt
, bool meta
)
5570 : wxNotifyEvent( type
, id
)
5572 m_rowOrCol
= rowOrCol
;
5575 m_control
= control
;
5580 SetEventObject(obj
);
5584 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
5586 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
5587 const wxGridCellCoords
& topLeft
,
5588 const wxGridCellCoords
& bottomRight
,
5589 bool control
, bool shift
, bool alt
, bool meta
)
5590 : wxNotifyEvent( type
, id
)
5592 m_topLeft
= topLeft
;
5593 m_bottomRight
= bottomRight
;
5594 m_control
= control
;
5599 SetEventObject(obj
);
5603 #endif // ifndef wxUSE_NEW_GRID