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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "grid.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
25 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
36 // this include needs to be outside precomp for BCC
37 #include "wx/textfile.h"
39 #include "wx/generic/grid.h"
41 // ----------------------------------------------------------------------------
42 // array classes instantiation
43 // ----------------------------------------------------------------------------
45 struct wxGridCellWithAttr
47 wxGridCellWithAttr(int row
, int col
, const wxGridCellAttr
*pAttr
)
48 : coords(row
, col
), attr(*pAttr
)
52 wxGridCellCoords coords
;
56 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
58 #include "wx/arrimpl.cpp"
60 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
61 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
70 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
71 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
72 const wxPoint
&pos
, const wxSize
&size
);
77 void OnPaint( wxPaintEvent
& event
);
78 void OnMouseEvent( wxMouseEvent
& event
);
79 void OnKeyDown( wxKeyEvent
& event
);
81 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
86 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
89 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
90 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
91 const wxPoint
&pos
, const wxSize
&size
);
96 void OnPaint( wxPaintEvent
&event
);
97 void OnMouseEvent( wxMouseEvent
& event
);
98 void OnKeyDown( wxKeyEvent
& event
);
100 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
101 DECLARE_EVENT_TABLE()
105 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
108 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
109 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
110 const wxPoint
&pos
, const wxSize
&size
);
115 void OnMouseEvent( wxMouseEvent
& event
);
116 void OnKeyDown( wxKeyEvent
& event
);
117 void OnPaint( wxPaintEvent
& event
);
119 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
120 DECLARE_EVENT_TABLE()
123 class WXDLLEXPORT wxGridWindow
: public wxPanel
128 m_owner
= (wxGrid
*)NULL
;
129 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
130 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
133 wxGridWindow( wxGrid
*parent
,
134 wxGridRowLabelWindow
*rowLblWin
,
135 wxGridColLabelWindow
*colLblWin
,
136 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
139 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
143 wxGridRowLabelWindow
*m_rowLabelWin
;
144 wxGridColLabelWindow
*m_colLabelWin
;
146 void OnPaint( wxPaintEvent
&event
);
147 void OnMouseEvent( wxMouseEvent
& event
);
148 void OnKeyDown( wxKeyEvent
& );
150 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
151 DECLARE_EVENT_TABLE()
154 // the internal data representation used by wxGridCellAttrProvider
156 // TODO make it more efficient
157 class WXDLLEXPORT wxGridCellAttrProviderData
160 void SetAttr(const wxGridCellAttr
*attr
, int row
, int col
);
161 wxGridCellAttr
*GetAttr(int row
, int col
) const;
164 // searches for the attr for given cell, returns wxNOT_FOUND if not found
165 int FindIndex(int row
, int col
) const;
167 wxGridCellWithAttrArray m_attrs
;
170 // ----------------------------------------------------------------------------
171 // conditional compilation
172 // ----------------------------------------------------------------------------
174 #ifndef WXGRID_DRAW_LINES
175 #define WXGRID_DRAW_LINES 1
178 //////////////////////////////////////////////////////////////////////
180 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
181 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
184 // TODO: fixed so far - make configurable later (and also different for x/y)
185 static const size_t GRID_SCROLL_LINE
= 10;
187 // ----------------------------------------------------------------------------
188 // wxGridCellAttrProviderData
189 // ----------------------------------------------------------------------------
191 void wxGridCellAttrProviderData::SetAttr(const wxGridCellAttr
*attr
,
194 int n
= FindIndex(row
, col
);
195 if ( n
== wxNOT_FOUND
)
198 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
204 // change the attribute
205 m_attrs
[(size_t)n
].attr
= *attr
;
209 // remove this attribute
210 m_attrs
.RemoveAt((size_t)n
);
217 wxGridCellAttr
*wxGridCellAttrProviderData::GetAttr(int row
, int col
) const
219 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
221 int n
= FindIndex(row
, col
);
222 if ( n
!= wxNOT_FOUND
)
224 attr
= new wxGridCellAttr(m_attrs
[(size_t)n
].attr
);
230 int wxGridCellAttrProviderData::FindIndex(int row
, int col
) const
232 size_t count
= m_attrs
.GetCount();
233 for ( size_t n
= 0; n
< count
; n
++ )
235 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
236 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
245 // ----------------------------------------------------------------------------
246 // wxGridCellAttrProvider
247 // ----------------------------------------------------------------------------
249 wxGridCellAttrProvider::wxGridCellAttrProvider()
251 m_data
= (wxGridCellAttrProviderData
*)NULL
;
254 wxGridCellAttrProvider::~wxGridCellAttrProvider()
259 void wxGridCellAttrProvider::InitData()
261 m_data
= new wxGridCellAttrProviderData
;
264 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
266 return m_data
? m_data
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
269 void wxGridCellAttrProvider::SetAttr(const wxGridCellAttr
*attr
,
275 m_data
->SetAttr(attr
, row
, col
);
278 //////////////////////////////////////////////////////////////////////
280 // Abstract base class for grid data (the model)
282 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
285 wxGridTableBase::wxGridTableBase()
287 m_view
= (wxGrid
*) NULL
;
288 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
291 wxGridTableBase::~wxGridTableBase()
293 delete m_attrProvider
;
296 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
298 delete m_attrProvider
;
299 m_attrProvider
= attrProvider
;
302 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
304 if ( m_attrProvider
)
305 return m_attrProvider
->GetAttr(row
, col
);
307 return (wxGridCellAttr
*)NULL
;
310 void wxGridTableBase::SetAttr(const wxGridCellAttr
*attr
, int row
, int col
)
312 if ( m_attrProvider
)
314 m_attrProvider
->SetAttr(attr
, row
, col
);
318 // as we take ownership of the pointer and don't store it, we must
324 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
326 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
327 "but your derived table class does not override this function") );
332 bool wxGridTableBase::AppendRows( size_t numRows
)
334 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
335 "but your derived table class does not override this function"));
340 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
342 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
343 "but your derived table class does not override this function"));
348 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
350 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
351 "but your derived table class does not override this function"));
356 bool wxGridTableBase::AppendCols( size_t numCols
)
358 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
359 "but your derived table class does not override this function"));
364 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
366 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
367 "but your derived table class does not override this function"));
373 wxString
wxGridTableBase::GetRowLabelValue( int row
)
380 wxString
wxGridTableBase::GetColLabelValue( int col
)
382 // default col labels are:
383 // cols 0 to 25 : A-Z
384 // cols 26 to 675 : AA-ZZ
391 s
+= (_T('A') + (wxChar
)( col%26
));
393 if ( col
< 0 ) break;
396 // reverse the string...
398 for ( i
= 0; i
< n
; i
++ )
408 //////////////////////////////////////////////////////////////////////
410 // Message class for the grid table to send requests and notifications
414 wxGridTableMessage::wxGridTableMessage()
416 m_table
= (wxGridTableBase
*) NULL
;
422 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
423 int commandInt1
, int commandInt2
)
427 m_comInt1
= commandInt1
;
428 m_comInt2
= commandInt2
;
433 //////////////////////////////////////////////////////////////////////
435 // A basic grid table for string data. An object of this class will
436 // created by wxGrid if you don't specify an alternative table class.
439 WX_DEFINE_OBJARRAY(wxGridStringArray
)
441 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
443 wxGridStringTable::wxGridStringTable()
448 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
453 m_data
.Alloc( numRows
);
457 for ( col
= 0; col
< numCols
; col
++ )
459 sa
.Add( wxEmptyString
);
462 for ( row
= 0; row
< numRows
; row
++ )
468 wxGridStringTable::~wxGridStringTable()
472 long wxGridStringTable::GetNumberRows()
474 return m_data
.GetCount();
477 long wxGridStringTable::GetNumberCols()
479 if ( m_data
.GetCount() > 0 )
480 return m_data
[0].GetCount();
485 wxString
wxGridStringTable::GetValue( int row
, int col
)
487 // TODO: bounds checking
489 return m_data
[row
][col
];
492 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
494 // TODO: bounds checking
496 m_data
[row
][col
] = s
;
499 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
501 // TODO: bounds checking
503 return (m_data
[row
][col
] == wxEmptyString
);
507 void wxGridStringTable::Clear()
510 int numRows
, numCols
;
512 numRows
= m_data
.GetCount();
515 numCols
= m_data
[0].GetCount();
517 for ( row
= 0; row
< numRows
; row
++ )
519 for ( col
= 0; col
< numCols
; col
++ )
521 m_data
[row
][col
] = wxEmptyString
;
528 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
532 size_t curNumRows
= m_data
.GetCount();
533 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
535 if ( pos
>= curNumRows
)
537 return AppendRows( numRows
);
541 sa
.Alloc( curNumCols
);
542 for ( col
= 0; col
< curNumCols
; col
++ )
544 sa
.Add( wxEmptyString
);
547 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
549 m_data
.Insert( sa
, row
);
554 wxGridTableMessage
msg( this,
555 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
559 GetView()->ProcessTableMessage( msg
);
565 bool wxGridStringTable::AppendRows( size_t numRows
)
569 size_t curNumRows
= m_data
.GetCount();
570 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
573 if ( curNumCols
> 0 )
575 sa
.Alloc( curNumCols
);
576 for ( col
= 0; col
< curNumCols
; col
++ )
578 sa
.Add( wxEmptyString
);
582 for ( row
= 0; row
< numRows
; row
++ )
589 wxGridTableMessage
msg( this,
590 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
593 GetView()->ProcessTableMessage( msg
);
599 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
603 size_t curNumRows
= m_data
.GetCount();
605 if ( pos
>= curNumRows
)
608 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
609 "Pos value is invalid for present table with %d rows",
610 pos
, numRows
, curNumRows
);
611 wxFAIL_MSG( wxT(errmsg
) );
615 if ( numRows
> curNumRows
- pos
)
617 numRows
= curNumRows
- pos
;
620 if ( numRows
>= curNumRows
)
622 m_data
.Empty(); // don't release memory just yet
626 for ( n
= 0; n
< numRows
; n
++ )
628 m_data
.Remove( pos
);
634 wxGridTableMessage
msg( this,
635 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
639 GetView()->ProcessTableMessage( msg
);
645 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
649 size_t curNumRows
= m_data
.GetCount();
650 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
652 if ( pos
>= curNumCols
)
654 return AppendCols( numCols
);
657 for ( row
= 0; row
< curNumRows
; row
++ )
659 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
661 m_data
[row
].Insert( wxEmptyString
, col
);
667 wxGridTableMessage
msg( this,
668 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
672 GetView()->ProcessTableMessage( msg
);
678 bool wxGridStringTable::AppendCols( size_t numCols
)
682 size_t curNumRows
= m_data
.GetCount();
685 // TODO: something better than this ?
687 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
688 "Call AppendRows() first") );
692 for ( row
= 0; row
< curNumRows
; row
++ )
694 for ( n
= 0; n
< numCols
; n
++ )
696 m_data
[row
].Add( wxEmptyString
);
702 wxGridTableMessage
msg( this,
703 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
706 GetView()->ProcessTableMessage( msg
);
712 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
716 size_t curNumRows
= m_data
.GetCount();
717 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
719 if ( pos
>= curNumCols
)
722 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
723 "Pos value is invalid for present table with %d cols",
724 pos
, numCols
, curNumCols
);
725 wxFAIL_MSG( wxT( errmsg
) );
729 if ( numCols
> curNumCols
- pos
)
731 numCols
= curNumCols
- pos
;
734 for ( row
= 0; row
< curNumRows
; row
++ )
736 if ( numCols
>= curNumCols
)
742 for ( n
= 0; n
< numCols
; n
++ )
744 m_data
[row
].Remove( pos
);
751 wxGridTableMessage
msg( this,
752 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
756 GetView()->ProcessTableMessage( msg
);
762 wxString
wxGridStringTable::GetRowLabelValue( int row
)
764 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
766 // using default label
768 return wxGridTableBase::GetRowLabelValue( row
);
772 return m_rowLabels
[ row
];
776 wxString
wxGridStringTable::GetColLabelValue( int col
)
778 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
780 // using default label
782 return wxGridTableBase::GetColLabelValue( col
);
786 return m_colLabels
[ col
];
790 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
792 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
794 int n
= m_rowLabels
.GetCount();
796 for ( i
= n
; i
<= row
; i
++ )
798 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
802 m_rowLabels
[row
] = value
;
805 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
807 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
809 int n
= m_colLabels
.GetCount();
811 for ( i
= n
; i
<= col
; i
++ )
813 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
817 m_colLabels
[col
] = value
;
823 //////////////////////////////////////////////////////////////////////
825 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
827 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
828 EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown
)
832 wxGridTextCtrl::wxGridTextCtrl( wxWindow
*par
,
836 const wxString
& value
,
840 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
843 m_isCellControl
= isCellControl
;
847 void wxGridTextCtrl::OnKeyDown( wxKeyEvent
& event
)
849 switch ( event
.KeyCode() )
852 m_grid
->SetEditControlValue( startValue
);
853 SetInsertionPointEnd();
863 if ( m_isCellControl
)
865 // send the event to the parent grid, skipping the
866 // event if nothing happens
868 event
.Skip( m_grid
->ProcessEvent( event
) );
872 // default text control response within the top edit
880 if ( m_isCellControl
)
882 if ( !m_grid
->ProcessEvent( event
) )
884 #if defined(__WXMOTIF__) || defined(__WXGTK__)
885 // wxMotif needs a little extra help...
887 int pos
= GetInsertionPoint();
888 wxString
s( GetValue() );
889 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
891 SetInsertionPoint( pos
);
893 // the other ports can handle a Return key press
903 if ( m_isCellControl
)
905 // send the event to the parent grid, skipping the
906 // event if nothing happens
908 event
.Skip( m_grid
->ProcessEvent( event
) );
912 // default text control response within the top edit
924 void wxGridTextCtrl::SetStartValue( const wxString
& s
)
927 wxTextCtrl::SetValue(s
);
932 //////////////////////////////////////////////////////////////////////
934 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
936 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
937 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
938 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
939 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
942 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
944 const wxPoint
&pos
, const wxSize
&size
)
945 : wxWindow( parent
, id
, pos
, size
)
950 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
954 // NO - don't do this because it will set both the x and y origin
955 // coords to match the parent scrolled window and we just want to
956 // set the y coord - MB
958 // m_owner->PrepareDC( dc );
961 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
962 dc
.SetDeviceOrigin( 0, -y
);
964 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
965 m_owner
->DrawRowLabels( dc
);
969 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
971 m_owner
->ProcessRowLabelMouseEvent( event
);
975 // This seems to be required for wxMotif otherwise the mouse
976 // cursor must be in the cell edit control to get key events
978 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
980 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
985 //////////////////////////////////////////////////////////////////////
987 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
989 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
990 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
991 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
992 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
995 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
997 const wxPoint
&pos
, const wxSize
&size
)
998 : wxWindow( parent
, id
, pos
, size
)
1003 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1007 // NO - don't do this because it will set both the x and y origin
1008 // coords to match the parent scrolled window and we just want to
1009 // set the x coord - MB
1011 // m_owner->PrepareDC( dc );
1014 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1015 dc
.SetDeviceOrigin( -x
, 0 );
1017 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1018 m_owner
->DrawColLabels( dc
);
1022 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1024 m_owner
->ProcessColLabelMouseEvent( event
);
1028 // This seems to be required for wxMotif otherwise the mouse
1029 // cursor must be in the cell edit control to get key events
1031 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1033 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1038 //////////////////////////////////////////////////////////////////////
1040 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1042 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1043 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1044 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1045 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1048 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1050 const wxPoint
&pos
, const wxSize
&size
)
1051 : wxWindow( parent
, id
, pos
, size
)
1056 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1060 int client_height
= 0;
1061 int client_width
= 0;
1062 GetClientSize( &client_width
, &client_height
);
1064 dc
.SetPen( *wxBLACK_PEN
);
1065 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1066 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1068 dc
.SetPen( *wxWHITE_PEN
);
1069 dc
.DrawLine( 0, 0, client_width
, 0 );
1070 dc
.DrawLine( 0, 0, 0, client_height
);
1074 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1076 m_owner
->ProcessCornerLabelMouseEvent( event
);
1080 // This seems to be required for wxMotif otherwise the mouse
1081 // cursor must be in the cell edit control to get key events
1083 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1085 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1090 //////////////////////////////////////////////////////////////////////
1092 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1094 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1095 EVT_PAINT( wxGridWindow::OnPaint
)
1096 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1097 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1100 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1101 wxGridRowLabelWindow
*rowLblWin
,
1102 wxGridColLabelWindow
*colLblWin
,
1103 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1104 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1107 m_rowLabelWin
= rowLblWin
;
1108 m_colLabelWin
= colLblWin
;
1110 SetBackgroundColour( "WHITE" );
1114 wxGridWindow::~wxGridWindow()
1119 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1121 wxPaintDC
dc( this );
1122 m_owner
->PrepareDC( dc
);
1123 wxRegion reg
= GetUpdateRegion();
1124 m_owner
->CalcCellsExposed( reg
);
1125 m_owner
->DrawGridCellArea( dc
);
1126 #if WXGRID_DRAW_LINES
1127 m_owner
->DrawAllGridLines( dc
, reg
);
1132 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1134 wxPanel::ScrollWindow( dx
, dy
, rect
);
1135 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1136 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1140 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1142 m_owner
->ProcessGridCellMouseEvent( event
);
1146 // This seems to be required for wxMotif otherwise the mouse
1147 // cursor must be in the cell edit control to get key events
1149 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1151 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1156 //////////////////////////////////////////////////////////////////////
1158 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1160 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1161 EVT_PAINT( wxGrid::OnPaint
)
1162 EVT_SIZE( wxGrid::OnSize
)
1163 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1166 wxGrid::wxGrid( wxWindow
*parent
,
1171 const wxString
& name
)
1172 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1185 // ----- internal init and update functions
1188 void wxGrid::Create()
1190 m_created
= FALSE
; // set to TRUE by CreateGrid
1191 m_displayed
= FALSE
; // set to TRUE by OnPaint
1193 m_table
= (wxGridTableBase
*) NULL
;
1194 m_cellEditCtrl
= (wxWindow
*) NULL
;
1198 m_currentCellCoords
= wxGridNoCellCoords
;
1200 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1201 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1203 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1208 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1213 m_colLabelWin
= new wxGridColLabelWindow( this,
1218 m_gridWin
= new wxGridWindow( this,
1225 SetTargetWindow( m_gridWin
);
1229 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1233 wxFAIL_MSG( wxT("wxGrid::CreateGrid called more than once") );
1238 m_numRows
= numRows
;
1239 m_numCols
= numCols
;
1241 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1242 m_table
->SetView( this );
1255 if ( m_numRows
<= 0 )
1256 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1258 if ( m_numCols
<= 0 )
1259 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1261 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1262 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1264 if ( m_rowLabelWin
)
1266 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1270 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1273 m_labelTextColour
= wxColour( _T("BLACK") );
1275 // TODO: something better than this ?
1277 m_labelFont
= this->GetFont();
1278 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1280 m_rowLabelHorizAlign
= wxLEFT
;
1281 m_rowLabelVertAlign
= wxCENTRE
;
1283 m_colLabelHorizAlign
= wxCENTRE
;
1284 m_colLabelVertAlign
= wxTOP
;
1286 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1287 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1289 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1290 m_defaultRowHeight
+= 8;
1292 m_defaultRowHeight
+= 4;
1295 m_rowHeights
.Alloc( m_numRows
);
1296 m_rowBottoms
.Alloc( m_numRows
);
1298 for ( i
= 0; i
< m_numRows
; i
++ )
1300 m_rowHeights
.Add( m_defaultRowHeight
);
1301 rowBottom
+= m_defaultRowHeight
;
1302 m_rowBottoms
.Add( rowBottom
);
1305 m_colWidths
.Alloc( m_numCols
);
1306 m_colRights
.Alloc( m_numCols
);
1308 for ( i
= 0; i
< m_numCols
; i
++ )
1310 m_colWidths
.Add( m_defaultColWidth
);
1311 colRight
+= m_defaultColWidth
;
1312 m_colRights
.Add( colRight
);
1315 // TODO: improve this by using wxSystemSettings?
1317 m_defaultCellFont
= GetFont();
1319 m_defaultCellHAlign
= wxLEFT
;
1320 m_defaultCellVAlign
= wxTOP
;
1322 m_gridLineColour
= wxColour( 128, 128, 255 );
1323 m_gridLinesEnabled
= TRUE
;
1325 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1327 m_dragRowOrCol
= -1;
1328 m_isDragging
= FALSE
;
1330 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1331 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1333 m_currentCellCoords
= wxGridNoCellCoords
;
1335 m_selectedTopLeft
= wxGridNoCellCoords
;
1336 m_selectedBottomRight
= wxGridNoCellCoords
;
1338 m_editable
= TRUE
; // default for whole grid
1340 m_inOnKeyDown
= FALSE
;
1343 // TODO: extend this to other types of controls
1345 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1352 #if defined(__WXMSW__)
1353 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1357 m_cellEditCtrl
->Show( FALSE
);
1358 m_cellEditCtrlEnabled
= TRUE
;
1359 m_editCtrlType
= wxGRID_TEXTCTRL
;
1363 void wxGrid::CalcDimensions()
1366 GetClientSize( &cw
, &ch
);
1368 if ( m_numRows
> 0 && m_numCols
> 0 )
1370 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1371 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1373 // TODO: restore the scroll position that we had before sizing
1376 GetViewStart( &x
, &y
);
1377 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1378 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1384 void wxGrid::CalcWindowSizes()
1387 GetClientSize( &cw
, &ch
);
1389 if ( m_cornerLabelWin
->IsShown() )
1390 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
1392 if ( m_colLabelWin
->IsShown() )
1393 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
1395 if ( m_rowLabelWin
->IsShown() )
1396 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
1398 if ( m_gridWin
->IsShown() )
1399 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
1403 // this is called when the grid table sends a message to say that it
1404 // has been redimensioned
1406 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1410 switch ( msg
.GetId() )
1412 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1414 size_t pos
= msg
.GetCommandInt();
1415 int numRows
= msg
.GetCommandInt2();
1416 for ( i
= 0; i
< numRows
; i
++ )
1418 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1419 m_rowBottoms
.Insert( 0, pos
);
1421 m_numRows
+= numRows
;
1424 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1426 for ( i
= pos
; i
< m_numRows
; i
++ )
1428 bottom
+= m_rowHeights
[i
];
1429 m_rowBottoms
[i
] = bottom
;
1435 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1437 int numRows
= msg
.GetCommandInt();
1438 for ( i
= 0; i
< numRows
; i
++ )
1440 m_rowHeights
.Add( m_defaultRowHeight
);
1441 m_rowBottoms
.Add( 0 );
1444 int oldNumRows
= m_numRows
;
1445 m_numRows
+= numRows
;
1448 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1450 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1452 bottom
+= m_rowHeights
[i
];
1453 m_rowBottoms
[i
] = bottom
;
1459 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1461 size_t pos
= msg
.GetCommandInt();
1462 int numRows
= msg
.GetCommandInt2();
1463 for ( i
= 0; i
< numRows
; i
++ )
1465 m_rowHeights
.Remove( pos
);
1466 m_rowBottoms
.Remove( pos
);
1468 m_numRows
-= numRows
;
1473 m_colWidths
.Clear();
1474 m_colRights
.Clear();
1475 m_currentCellCoords
= wxGridNoCellCoords
;
1479 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1480 m_currentCellCoords
.Set( 0, 0 );
1483 for ( i
= 0; i
< m_numRows
; i
++ )
1485 h
+= m_rowHeights
[i
];
1486 m_rowBottoms
[i
] = h
;
1494 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1496 size_t pos
= msg
.GetCommandInt();
1497 int numCols
= msg
.GetCommandInt2();
1498 for ( i
= 0; i
< numCols
; i
++ )
1500 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1501 m_colRights
.Insert( 0, pos
);
1503 m_numCols
+= numCols
;
1506 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1508 for ( i
= pos
; i
< m_numCols
; i
++ )
1510 right
+= m_colWidths
[i
];
1511 m_colRights
[i
] = right
;
1517 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1519 int numCols
= msg
.GetCommandInt();
1520 for ( i
= 0; i
< numCols
; i
++ )
1522 m_colWidths
.Add( m_defaultColWidth
);
1523 m_colRights
.Add( 0 );
1526 int oldNumCols
= m_numCols
;
1527 m_numCols
+= numCols
;
1530 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1532 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1534 right
+= m_colWidths
[i
];
1535 m_colRights
[i
] = right
;
1541 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1543 size_t pos
= msg
.GetCommandInt();
1544 int numCols
= msg
.GetCommandInt2();
1545 for ( i
= 0; i
< numCols
; i
++ )
1547 m_colWidths
.Remove( pos
);
1548 m_colRights
.Remove( pos
);
1550 m_numCols
-= numCols
;
1554 #if 0 // leave the row alone here so that AppendCols will work subsequently
1556 m_rowHeights
.Clear();
1557 m_rowBottoms
.Clear();
1559 m_currentCellCoords
= wxGridNoCellCoords
;
1563 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1564 m_currentCellCoords
.Set( 0, 0 );
1567 for ( i
= 0; i
< m_numCols
; i
++ )
1569 w
+= m_colWidths
[i
];
1582 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
1584 wxRegionIterator
iter( reg
);
1587 m_rowLabelsExposed
.Empty();
1594 // TODO: remove this when we can...
1595 // There is a bug in wxMotif that gives garbage update
1596 // rectangles if you jump-scroll a long way by clicking the
1597 // scrollbar with middle button. This is a work-around
1599 #if defined(__WXMOTIF__)
1601 m_gridWin
->GetClientSize( &cw
, &ch
);
1602 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1603 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1606 // logical bounds of update region
1609 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1610 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1612 // find the row labels within these bounds
1616 for ( row
= 0; row
< m_numRows
; row
++ )
1618 if ( m_rowBottoms
[row
] < top
) continue;
1620 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1621 if ( rowTop
> bottom
) break;
1623 m_rowLabelsExposed
.Add( row
);
1631 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
1633 wxRegionIterator
iter( reg
);
1636 m_colLabelsExposed
.Empty();
1643 // TODO: remove this when we can...
1644 // There is a bug in wxMotif that gives garbage update
1645 // rectangles if you jump-scroll a long way by clicking the
1646 // scrollbar with middle button. This is a work-around
1648 #if defined(__WXMOTIF__)
1650 m_gridWin
->GetClientSize( &cw
, &ch
);
1651 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1652 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1655 // logical bounds of update region
1658 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1659 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1661 // find the cells within these bounds
1665 for ( col
= 0; col
< m_numCols
; col
++ )
1667 if ( m_colRights
[col
] < left
) continue;
1669 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1670 if ( colLeft
> right
) break;
1672 m_colLabelsExposed
.Add( col
);
1680 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
1682 wxRegionIterator
iter( reg
);
1685 m_cellsExposed
.Empty();
1686 m_rowsExposed
.Empty();
1687 m_colsExposed
.Empty();
1689 int left
, top
, right
, bottom
;
1694 // TODO: remove this when we can...
1695 // There is a bug in wxMotif that gives garbage update
1696 // rectangles if you jump-scroll a long way by clicking the
1697 // scrollbar with middle button. This is a work-around
1699 #if defined(__WXMOTIF__)
1701 m_gridWin
->GetClientSize( &cw
, &ch
);
1702 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1703 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1704 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1705 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1708 // logical bounds of update region
1710 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1711 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1713 // find the cells within these bounds
1716 int colLeft
, rowTop
;
1717 for ( row
= 0; row
< m_numRows
; row
++ )
1719 if ( m_rowBottoms
[row
] < top
) continue;
1721 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1722 if ( rowTop
> bottom
) break;
1724 m_rowsExposed
.Add( row
);
1726 for ( col
= 0; col
< m_numCols
; col
++ )
1728 if ( m_colRights
[col
] < left
) continue;
1730 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1731 if ( colLeft
> right
) break;
1733 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1734 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1743 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1746 wxPoint
pos( event
.GetPosition() );
1747 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1749 if ( event
.Dragging() )
1751 m_isDragging
= TRUE
;
1753 if ( event
.LeftIsDown() )
1755 switch( m_cursorMode
)
1757 case WXGRID_CURSOR_RESIZE_ROW
:
1759 int cw
, ch
, left
, dummy
;
1760 m_gridWin
->GetClientSize( &cw
, &ch
);
1761 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1763 wxClientDC
dc( m_gridWin
);
1765 dc
.SetLogicalFunction(wxINVERT
);
1766 if ( m_dragLastPos
>= 0 )
1768 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1770 dc
.DrawLine( left
, y
, left
+cw
, y
);
1775 case WXGRID_CURSOR_SELECT_ROW
:
1777 if ( (row
= YToRow( y
)) >= 0 &&
1778 !IsInSelection( row
, 0 ) )
1780 SelectRow( row
, TRUE
);
1789 m_isDragging
= FALSE
;
1792 // ------------ Entering or leaving the window
1794 if ( event
.Entering() || event
.Leaving() )
1796 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1797 m_rowLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1801 // ------------ Left button pressed
1803 else if ( event
.LeftDown() )
1805 // don't send a label click event for a hit on the
1806 // edge of the row label - this is probably the user
1807 // wanting to resize the row
1809 if ( YToEdgeOfRow(y
) < 0 )
1813 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
1815 SelectRow( row
, event
.ShiftDown() );
1816 m_cursorMode
= WXGRID_CURSOR_SELECT_ROW
;
1821 // starting to drag-resize a row
1823 m_rowLabelWin
->CaptureMouse();
1828 // ------------ Left double click
1830 else if (event
.LeftDClick() )
1832 if ( YToEdgeOfRow(y
) < 0 )
1835 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
1840 // ------------ Left button released
1842 else if ( event
.LeftUp() )
1844 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
1846 m_rowLabelWin
->ReleaseMouse();
1847 DoEndDragResizeRow();
1849 // Note: we are ending the event *after* doing
1850 // default processing in this case
1852 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1855 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1860 // ------------ Right button down
1862 else if ( event
.RightDown() )
1865 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1867 // no default action at the moment
1872 // ------------ Right double click
1874 else if ( event
.RightDClick() )
1877 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1879 // no default action at the moment
1884 // ------------ No buttons down and mouse moving
1886 else if ( event
.Moving() )
1888 m_dragRowOrCol
= YToEdgeOfRow( y
);
1889 if ( m_dragRowOrCol
>= 0 )
1891 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1893 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1894 m_rowLabelWin
->SetCursor( m_rowResizeCursor
);
1897 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1899 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1900 m_rowLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1906 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
1909 wxPoint
pos( event
.GetPosition() );
1910 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1912 if ( event
.Dragging() )
1914 m_isDragging
= TRUE
;
1916 if ( event
.LeftIsDown() )
1918 switch( m_cursorMode
)
1920 case WXGRID_CURSOR_RESIZE_COL
:
1922 int cw
, ch
, dummy
, top
;
1923 m_gridWin
->GetClientSize( &cw
, &ch
);
1924 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1926 wxClientDC
dc( m_gridWin
);
1928 dc
.SetLogicalFunction(wxINVERT
);
1929 if ( m_dragLastPos
>= 0 )
1931 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1933 dc
.DrawLine( x
, top
, x
, top
+ch
);
1938 case WXGRID_CURSOR_SELECT_COL
:
1940 if ( (col
= XToCol( x
)) >= 0 &&
1941 !IsInSelection( 0, col
) )
1943 SelectCol( col
, TRUE
);
1952 m_isDragging
= FALSE
;
1955 // ------------ Entering or leaving the window
1957 if ( event
.Entering() || event
.Leaving() )
1959 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1960 m_colLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1964 // ------------ Left button pressed
1966 else if ( event
.LeftDown() )
1968 // don't send a label click event for a hit on the
1969 // edge of the col label - this is probably the user
1970 // wanting to resize the col
1972 if ( XToEdgeOfCol(x
) < 0 )
1976 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
1978 SelectCol( col
, event
.ShiftDown() );
1979 m_cursorMode
= WXGRID_CURSOR_SELECT_COL
;
1984 // starting to drag-resize a col
1986 m_colLabelWin
->CaptureMouse();
1991 // ------------ Left double click
1993 if ( event
.LeftDClick() )
1995 if ( XToEdgeOfCol(x
) < 0 )
1998 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2003 // ------------ Left button released
2005 else if ( event
.LeftUp() )
2007 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2009 m_colLabelWin
->ReleaseMouse();
2010 DoEndDragResizeCol();
2012 // Note: we are ending the event *after* doing
2013 // default processing in this case
2015 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2018 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2023 // ------------ Right button down
2025 else if ( event
.RightDown() )
2028 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2030 // no default action at the moment
2035 // ------------ Right double click
2037 else if ( event
.RightDClick() )
2040 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2042 // no default action at the moment
2047 // ------------ No buttons down and mouse moving
2049 else if ( event
.Moving() )
2051 m_dragRowOrCol
= XToEdgeOfCol( x
);
2052 if ( m_dragRowOrCol
>= 0 )
2054 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2056 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
2057 m_colLabelWin
->SetCursor( m_colResizeCursor
);
2060 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2062 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2063 m_colLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
2069 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2071 if ( event
.LeftDown() )
2073 // indicate corner label by having both row and
2076 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2082 else if ( event
.LeftDClick() )
2084 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2087 else if ( event
.RightDown() )
2089 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2091 // no default action at the moment
2095 else if ( event
.RightDClick() )
2097 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2099 // no default action at the moment
2105 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2108 wxPoint
pos( event
.GetPosition() );
2109 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2111 wxGridCellCoords coords
;
2112 XYToCell( x
, y
, coords
);
2114 if ( event
.Dragging() )
2116 m_isDragging
= TRUE
;
2117 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2119 // Hide the edit control, so it
2120 // won't interfer with drag-shrinking.
2121 if ( IsCellEditControlEnabled() )
2122 HideCellEditControl();
2123 if ( coords
!= wxGridNoCellCoords
)
2125 if ( !IsSelection() )
2127 SelectBlock( coords
, coords
);
2131 SelectBlock( m_currentCellCoords
, coords
);
2135 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2137 int cw
, ch
, left
, dummy
;
2138 m_gridWin
->GetClientSize( &cw
, &ch
);
2139 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2141 wxClientDC
dc( m_gridWin
);
2143 dc
.SetLogicalFunction(wxINVERT
);
2144 if ( m_dragLastPos
>= 0 )
2146 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2148 dc
.DrawLine( left
, y
, left
+cw
, y
);
2151 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2153 int cw
, ch
, dummy
, top
;
2154 m_gridWin
->GetClientSize( &cw
, &ch
);
2155 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2157 wxClientDC
dc( m_gridWin
);
2159 dc
.SetLogicalFunction(wxINVERT
);
2160 if ( m_dragLastPos
>= 0 )
2162 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2164 dc
.DrawLine( x
, top
, x
, top
+ch
);
2171 m_isDragging
= FALSE
;
2173 if ( coords
!= wxGridNoCellCoords
)
2175 if ( event
.Entering() || event
.Leaving() )
2177 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2178 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2181 // ------------ Left button pressed
2183 else if ( event
.LeftDown() )
2185 if ( event
.ShiftDown() )
2187 SelectBlock( m_currentCellCoords
, coords
);
2189 else if ( XToEdgeOfCol(x
) < 0 &&
2190 YToEdgeOfRow(y
) < 0 )
2192 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2197 MakeCellVisible( coords
);
2198 SetCurrentCell( coords
);
2204 // ------------ Left double click
2206 else if ( event
.LeftDClick() )
2208 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2210 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2218 // ------------ Left button released
2220 else if ( event
.LeftUp() )
2222 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2224 if ( IsSelection() )
2226 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
2229 // Show the edit control, if it has
2230 // been hidden for drag-shrinking.
2231 if ( IsCellEditControlEnabled() )
2232 ShowCellEditControl();
2234 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2236 m_gridWin
->ReleaseMouse();
2237 DoEndDragResizeRow();
2239 // Note: we are ending the event *after* doing
2240 // default processing in this case
2242 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2244 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2246 m_gridWin
->ReleaseMouse();
2247 DoEndDragResizeCol();
2249 // Note: we are ending the event *after* doing
2250 // default processing in this case
2252 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2259 // ------------ Right button down
2261 else if ( event
.RightDown() )
2263 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2268 // no default action at the moment
2273 // ------------ Right double click
2275 else if ( event
.RightDClick() )
2277 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2282 // no default action at the moment
2286 // ------------ Moving and no button action
2288 else if ( event
.Moving() && !event
.IsButton() )
2290 int dragRow
= YToEdgeOfRow( y
);
2291 int dragCol
= XToEdgeOfCol( x
);
2293 // Dragging on the corner of a cell to resize in both
2294 // directions is not implemented yet...
2296 if ( dragRow
>= 0 && dragCol
>= 0 )
2298 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2299 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2305 m_dragRowOrCol
= dragRow
;
2307 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2309 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
2310 m_gridWin
->SetCursor( m_rowResizeCursor
);
2318 m_dragRowOrCol
= dragCol
;
2320 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2322 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
2323 m_gridWin
->SetCursor( m_colResizeCursor
);
2329 // Neither on a row or col edge
2331 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2333 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2334 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2341 void wxGrid::DoEndDragResizeRow()
2343 if ( m_dragLastPos
>= 0 )
2345 // erase the last line and resize the row
2347 int cw
, ch
, left
, dummy
;
2348 m_gridWin
->GetClientSize( &cw
, &ch
);
2349 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2351 wxClientDC
dc( m_gridWin
);
2353 dc
.SetLogicalFunction( wxINVERT
);
2354 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2355 HideCellEditControl();
2357 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
2358 SetRowSize( m_dragRowOrCol
,
2359 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
2361 if ( !GetBatchCount() )
2363 // Only needed to get the correct rect.y:
2364 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
2366 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
2367 rect
.width
= m_rowLabelWidth
;
2368 rect
.height
= ch
- rect
.y
;
2369 m_rowLabelWin
->Refresh( TRUE
, &rect
);
2371 m_gridWin
->Refresh( FALSE
, &rect
);
2374 ShowCellEditControl();
2379 void wxGrid::DoEndDragResizeCol()
2381 if ( m_dragLastPos
>= 0 )
2383 // erase the last line and resize the col
2385 int cw
, ch
, dummy
, top
;
2386 m_gridWin
->GetClientSize( &cw
, &ch
);
2387 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2389 wxClientDC
dc( m_gridWin
);
2391 dc
.SetLogicalFunction( wxINVERT
);
2392 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2393 HideCellEditControl();
2395 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
2396 SetColSize( m_dragRowOrCol
,
2397 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
2399 if ( !GetBatchCount() )
2401 // Only needed to get the correct rect.x:
2402 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
2404 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
2405 rect
.width
= cw
- rect
.x
;
2406 rect
.height
= m_colLabelHeight
;
2407 m_colLabelWin
->Refresh( TRUE
, &rect
);
2409 m_gridWin
->Refresh( FALSE
, &rect
);
2412 ShowCellEditControl();
2419 // ------ interaction with data model
2421 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
2423 switch ( msg
.GetId() )
2425 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
2426 return GetModelValues();
2428 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
2429 return SetModelValues();
2431 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2432 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2433 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2434 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2435 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2436 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2437 return Redimension( msg
);
2446 // The behaviour of this function depends on the grid table class
2447 // Clear() function. For the default wxGridStringTable class the
2448 // behavious is to replace all cell contents with wxEmptyString but
2449 // not to change the number of rows or cols.
2451 void wxGrid::ClearGrid()
2456 SetEditControlValue();
2457 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2462 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2464 // TODO: something with updateLabels flag
2468 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2474 bool ok
= m_table
->InsertRows( pos
, numRows
);
2476 // the table will have sent the results of the insert row
2477 // operation to this view object as a grid table message
2481 if ( m_numCols
== 0 )
2483 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2485 // TODO: perhaps instead of appending the default number of cols
2486 // we should remember what the last non-zero number of cols was ?
2490 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2492 // if we have just inserted cols into an empty grid the current
2493 // cell will be undefined...
2495 SetCurrentCell( 0, 0 );
2499 if ( !GetBatchCount() ) Refresh();
2502 SetEditControlValue();
2512 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2514 // TODO: something with updateLabels flag
2518 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2522 if ( m_table
&& m_table
->AppendRows( numRows
) )
2524 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2526 // if we have just inserted cols into an empty grid the current
2527 // cell will be undefined...
2529 SetCurrentCell( 0, 0 );
2532 // the table will have sent the results of the append row
2533 // operation to this view object as a grid table message
2536 if ( !GetBatchCount() ) Refresh();
2546 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2548 // TODO: something with updateLabels flag
2552 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2556 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2558 // the table will have sent the results of the delete row
2559 // operation to this view object as a grid table message
2561 if ( m_numRows
> 0 )
2562 SetEditControlValue();
2564 HideCellEditControl();
2567 if ( !GetBatchCount() ) Refresh();
2577 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2579 // TODO: something with updateLabels flag
2583 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2589 HideCellEditControl();
2590 bool ok
= m_table
->InsertCols( pos
, numCols
);
2592 // the table will have sent the results of the insert col
2593 // operation to this view object as a grid table message
2597 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2599 // if we have just inserted cols into an empty grid the current
2600 // cell will be undefined...
2602 SetCurrentCell( 0, 0 );
2606 if ( !GetBatchCount() ) Refresh();
2609 SetEditControlValue();
2619 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2621 // TODO: something with updateLabels flag
2625 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2629 if ( m_table
&& m_table
->AppendCols( numCols
) )
2631 // the table will have sent the results of the append col
2632 // operation to this view object as a grid table message
2634 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2636 // if we have just inserted cols into an empty grid the current
2637 // cell will be undefined...
2639 SetCurrentCell( 0, 0 );
2643 if ( !GetBatchCount() ) Refresh();
2653 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2655 // TODO: something with updateLabels flag
2659 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2663 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2665 // the table will have sent the results of the delete col
2666 // operation to this view object as a grid table message
2668 if ( m_numCols
> 0 )
2669 SetEditControlValue();
2671 HideCellEditControl();
2674 if ( !GetBatchCount() ) Refresh();
2686 // ----- event handlers
2689 // Generate a grid event based on a mouse event and
2690 // return the result of ProcessEvent()
2692 bool wxGrid::SendEvent( const wxEventType type
,
2694 wxMouseEvent
& mouseEv
)
2696 if ( type
== EVT_GRID_ROW_SIZE
||
2697 type
== EVT_GRID_COL_SIZE
)
2699 int rowOrCol
= (row
== -1 ? col
: row
);
2701 wxGridSizeEvent
gridEvt( GetId(),
2705 mouseEv
.GetX(), mouseEv
.GetY(),
2706 mouseEv
.ControlDown(),
2707 mouseEv
.ShiftDown(),
2709 mouseEv
.MetaDown() );
2711 return GetEventHandler()->ProcessEvent(gridEvt
);
2713 else if ( type
== EVT_GRID_RANGE_SELECT
)
2715 wxGridRangeSelectEvent
gridEvt( GetId(),
2719 m_selectedBottomRight
,
2720 mouseEv
.ControlDown(),
2721 mouseEv
.ShiftDown(),
2723 mouseEv
.MetaDown() );
2725 return GetEventHandler()->ProcessEvent(gridEvt
);
2729 wxGridEvent
gridEvt( GetId(),
2733 mouseEv
.GetX(), mouseEv
.GetY(),
2734 mouseEv
.ControlDown(),
2735 mouseEv
.ShiftDown(),
2737 mouseEv
.MetaDown() );
2739 return GetEventHandler()->ProcessEvent(gridEvt
);
2744 // Generate a grid event of specified type and return the result
2745 // of ProcessEvent().
2747 bool wxGrid::SendEvent( const wxEventType type
,
2750 if ( type
== EVT_GRID_ROW_SIZE
||
2751 type
== EVT_GRID_COL_SIZE
)
2753 int rowOrCol
= (row
== -1 ? col
: row
);
2755 wxGridSizeEvent
gridEvt( GetId(),
2760 return GetEventHandler()->ProcessEvent(gridEvt
);
2764 wxGridEvent
gridEvt( GetId(),
2769 return GetEventHandler()->ProcessEvent(gridEvt
);
2774 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2776 wxPaintDC
dc( this );
2778 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2779 m_numRows
&& m_numCols
)
2781 m_currentCellCoords
.Set(0, 0);
2782 SetEditControlValue();
2783 ShowCellEditControl();
2790 // This is just here to make sure that CalcDimensions gets called when
2791 // the grid view is resized... then the size event is skipped to allow
2792 // the box sizers to handle everything
2794 void wxGrid::OnSize( wxSizeEvent
& event
)
2801 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2803 if ( m_inOnKeyDown
)
2805 // shouldn't be here - we are going round in circles...
2807 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while alread active") );
2810 m_inOnKeyDown
= TRUE
;
2812 // propagate the event up and see if it gets processed
2814 wxWindow
*parent
= GetParent();
2815 wxKeyEvent
keyEvt( event
);
2816 keyEvt
.SetEventObject( parent
);
2818 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2820 // try local handlers
2822 switch ( event
.KeyCode() )
2825 if ( event
.ControlDown() )
2827 MoveCursorUpBlock();
2836 if ( event
.ControlDown() )
2838 MoveCursorDownBlock();
2847 if ( event
.ControlDown() )
2849 MoveCursorLeftBlock();
2858 if ( event
.ControlDown() )
2860 MoveCursorRightBlock();
2869 if ( !IsEditable() )
2880 if ( event
.ControlDown() )
2882 event
.Skip(); // to let the edit control have the return
2891 if ( event
.ControlDown() )
2893 MakeCellVisible( 0, 0 );
2894 SetCurrentCell( 0, 0 );
2903 if ( event
.ControlDown() )
2905 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2906 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2923 // now try the cell edit control
2925 if ( IsCellEditControlEnabled() )
2927 event
.SetEventObject( m_cellEditCtrl
);
2928 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2934 m_inOnKeyDown
= FALSE
;
2938 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
2940 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
2942 // the event has been intercepted - do nothing
2947 m_currentCellCoords
!= wxGridNoCellCoords
)
2949 HideCellEditControl();
2950 SaveEditControlValue();
2953 m_currentCellCoords
= coords
;
2955 SetEditControlValue();
2959 ShowCellEditControl();
2961 if ( IsSelection() )
2963 wxRect
r( SelectionToDeviceRect() );
2965 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
2972 // ------ functions to get/send data (see also public functions)
2975 bool wxGrid::GetModelValues()
2979 // all we need to do is repaint the grid
2981 m_gridWin
->Refresh();
2989 bool wxGrid::SetModelValues()
2995 for ( row
= 0; row
< m_numRows
; row
++ )
2997 for ( col
= 0; col
< m_numCols
; col
++ )
2999 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3011 // Note - this function only draws cells that are in the list of
3012 // exposed cells (usually set from the update region by
3013 // CalcExposedCells)
3015 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3017 if ( !m_numRows
|| !m_numCols
) return;
3020 size_t numCells
= m_cellsExposed
.GetCount();
3022 for ( i
= 0; i
< numCells
; i
++ )
3024 DrawCell( dc
, m_cellsExposed
[i
] );
3029 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3031 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3032 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3034 #if !WXGRID_DRAW_LINES
3035 if ( m_gridLinesEnabled
)
3036 DrawCellBorder( dc
, coords
);
3039 DrawCellBackground( dc
, coords
);
3041 // TODO: separate functions here for different kinds of cells ?
3044 DrawCellValue( dc
, coords
);
3048 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3050 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3051 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3053 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3054 int row
= coords
.GetRow();
3055 int col
= coords
.GetCol();
3057 // right hand border
3059 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3060 m_colRights
[col
], m_rowBottoms
[row
] );
3064 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3065 m_colRights
[col
], m_rowBottoms
[row
] );
3069 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
3071 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3072 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3074 int row
= coords
.GetRow();
3075 int col
= coords
.GetCol();
3077 dc
.SetBackgroundMode( wxSOLID
);
3079 if ( IsInSelection( coords
) )
3081 // TODO: improve this
3083 dc
.SetBrush( *wxBLACK_BRUSH
);
3087 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
3090 dc
.SetPen( *wxTRANSPARENT_PEN
);
3092 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
3093 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
3095 m_rowHeights
[row
]-1 );
3099 void wxGrid::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
3101 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3102 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3104 int row
= coords
.GetRow();
3105 int col
= coords
.GetCol();
3107 dc
.SetBackgroundMode( wxTRANSPARENT
);
3109 if ( IsInSelection( row
, col
) )
3111 // TODO: improve this
3113 dc
.SetTextBackground( wxColour(0, 0, 0) );
3114 dc
.SetTextForeground( wxColour(255, 255, 255) );
3118 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
3119 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
3121 dc
.SetFont( GetCellFont(row
, col
) );
3124 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
3127 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3128 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3129 rect
.SetWidth( m_colWidths
[col
] - 4 );
3130 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3132 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
3137 // TODO: remove this ???
3138 // This is used to redraw all grid lines e.g. when the grid line colour
3141 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3143 if ( !m_gridLinesEnabled
||
3145 !m_numCols
) return;
3147 int top
, bottom
, left
, right
;
3151 m_gridWin
->GetClientSize(&cw
, &ch
);
3153 // virtual coords of visible area
3155 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3156 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3160 reg
.GetBox(x
, y
, w
, h
);
3161 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3162 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3165 // avoid drawing grid lines past the last row and col
3167 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3168 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3170 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3172 // horizontal grid lines
3175 for ( i
= 0; i
< m_numRows
; i
++ )
3177 if ( m_rowBottoms
[i
] > bottom
)
3181 else if ( m_rowBottoms
[i
] >= top
)
3183 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
3188 // vertical grid lines
3190 for ( i
= 0; i
< m_numCols
; i
++ )
3192 if ( m_colRights
[i
] > right
)
3196 else if ( m_colRights
[i
] >= left
)
3198 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
3204 void wxGrid::DrawRowLabels( wxDC
& dc
)
3206 if ( !m_numRows
|| !m_numCols
) return;
3209 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3211 for ( i
= 0; i
< numLabels
; i
++ )
3213 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3218 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
3220 if ( m_rowHeights
[row
] <= 0 ) return;
3222 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3224 dc
.SetPen( *wxBLACK_PEN
);
3225 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3226 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3228 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3229 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3231 dc
.SetPen( *wxWHITE_PEN
);
3232 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
3233 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
3235 dc
.SetBackgroundMode( wxTRANSPARENT
);
3236 dc
.SetTextForeground( GetLabelTextColour() );
3237 dc
.SetFont( GetLabelFont() );
3240 GetRowLabelAlignment( &hAlign
, &vAlign
);
3244 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3245 rect
.SetWidth( m_rowLabelWidth
- 4 );
3246 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3247 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
3251 void wxGrid::DrawColLabels( wxDC
& dc
)
3253 if ( !m_numRows
|| !m_numCols
) return;
3256 size_t numLabels
= m_colLabelsExposed
.GetCount();
3258 for ( i
= 0; i
< numLabels
; i
++ )
3260 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
3265 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
3267 if ( m_colWidths
[col
] <= 0 ) return;
3269 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
3271 dc
.SetPen( *wxBLACK_PEN
);
3272 dc
.DrawLine( m_colRights
[col
]-1, 0,
3273 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3275 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
3276 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3278 dc
.SetPen( *wxWHITE_PEN
);
3279 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
3280 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
3282 dc
.SetBackgroundMode( wxTRANSPARENT
);
3283 dc
.SetTextForeground( GetLabelTextColour() );
3284 dc
.SetFont( GetLabelFont() );
3286 dc
.SetBackgroundMode( wxTRANSPARENT
);
3287 dc
.SetTextForeground( GetLabelTextColour() );
3288 dc
.SetFont( GetLabelFont() );
3291 GetColLabelAlignment( &hAlign
, &vAlign
);
3294 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3296 rect
.SetWidth( m_colWidths
[col
] - 4 );
3297 rect
.SetHeight( m_colLabelHeight
- 4 );
3298 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
3302 void wxGrid::DrawTextRectangle( wxDC
& dc
,
3303 const wxString
& value
,
3308 long textWidth
, textHeight
;
3309 long lineWidth
, lineHeight
;
3310 wxArrayString lines
;
3312 dc
.SetClippingRegion( rect
);
3313 StringToLines( value
, lines
);
3314 if ( lines
.GetCount() )
3316 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
3317 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
3320 switch ( horizAlign
)
3323 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
3327 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
3336 switch ( vertAlign
)
3339 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
3343 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
3352 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
3354 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
3359 dc
.DestroyClippingRegion();
3363 // Split multi line text up into an array of strings. Any existing
3364 // contents of the string array are preserved.
3366 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
3370 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
3371 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
3373 while ( startPos
< (int)tVal
.Length() )
3375 pos
= tVal
.Mid(startPos
).Find( eol
);
3380 else if ( pos
== 0 )
3382 lines
.Add( wxEmptyString
);
3386 lines
.Add( value
.Mid(startPos
, pos
) );
3390 if ( startPos
< (int)value
.Length() )
3392 lines
.Add( value
.Mid( startPos
) );
3397 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3398 wxArrayString
& lines
,
3399 long *width
, long *height
)
3406 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3408 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3409 w
= wxMax( w
, lineW
);
3419 // ------ Edit control functions
3423 void wxGrid::EnableEditing( bool edit
)
3425 // TODO: improve this ?
3427 if ( edit
!= m_editable
)
3431 // TODO: extend this for other edit control types
3433 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3435 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
3441 #if 0 // disabled for the moment - the cell control is always active
3442 void wxGrid::EnableCellEditControl( bool enable
)
3444 if ( m_cellEditCtrl
&&
3445 enable
!= m_cellEditCtrlEnabled
)
3447 m_cellEditCtrlEnabled
= enable
;
3449 if ( m_cellEditCtrlEnabled
)
3451 SetEditControlValue();
3452 ShowCellEditControl();
3456 HideCellEditControl();
3457 SaveEditControlValue();
3464 void wxGrid::ShowCellEditControl()
3468 if ( IsCellEditControlEnabled() )
3470 if ( !IsVisible( m_currentCellCoords
) )
3476 rect
= CellToRect( m_currentCellCoords
);
3478 // convert to scrolled coords
3480 int left
, top
, right
, bottom
;
3481 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3482 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3485 m_gridWin
->GetClientSize( &cw
, &ch
);
3487 // Make the edit control large enough to allow for internal margins
3488 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3491 #if defined(__WXMOTIF__)
3492 if ( m_currentCellCoords
.GetRow() == 0 ||
3493 m_currentCellCoords
.GetCol() == 0 )
3502 if ( m_currentCellCoords
.GetRow() == 0 ||
3503 m_currentCellCoords
.GetCol() == 0 )
3513 #if defined(__WXGTK__)
3516 if (left
!= 0) left_diff
++;
3517 if (top
!= 0) top_diff
++;
3518 rect
.SetLeft( left
+ left_diff
);
3519 rect
.SetTop( top
+ top_diff
);
3520 rect
.SetRight( rect
.GetRight() - left_diff
);
3521 rect
.SetBottom( rect
.GetBottom() - top_diff
);
3523 rect
.SetLeft( wxMax(0, left
- extra
) );
3524 rect
.SetTop( wxMax(0, top
- extra
) );
3525 rect
.SetRight( rect
.GetRight() + 2*extra
);
3526 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3529 m_cellEditCtrl
->SetSize( rect
);
3530 m_cellEditCtrl
->Show( TRUE
);
3532 switch ( m_editCtrlType
)
3534 case wxGRID_TEXTCTRL
:
3535 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3538 case wxGRID_CHECKBOX
:
3539 // TODO: anything ???
3544 // TODO: anything ???
3548 case wxGRID_COMBOBOX
:
3549 // TODO: anything ???
3554 m_cellEditCtrl
->SetFocus();
3560 void wxGrid::HideCellEditControl()
3562 if ( IsCellEditControlEnabled() )
3564 m_cellEditCtrl
->Show( FALSE
);
3569 void wxGrid::SetEditControlValue( const wxString
& value
)
3575 s
= GetCellValue(m_currentCellCoords
);
3579 if ( IsCellEditControlEnabled() )
3581 switch ( m_editCtrlType
)
3583 case wxGRID_TEXTCTRL
:
3584 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3587 case wxGRID_CHECKBOX
:
3588 // TODO: implement this
3593 // TODO: implement this
3597 case wxGRID_COMBOBOX
:
3598 // TODO: implement this
3607 void wxGrid::SaveEditControlValue()
3611 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3613 if ( IsCellEditControlEnabled() )
3615 ctrl
= m_cellEditCtrl
;
3622 bool valueChanged
= FALSE
;
3624 switch ( m_editCtrlType
)
3626 case wxGRID_TEXTCTRL
:
3627 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3628 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3629 SetCellValue( m_currentCellCoords
,
3630 ((wxTextCtrl
*) ctrl
)->GetValue() );
3633 case wxGRID_CHECKBOX
:
3634 // TODO: implement this
3639 // TODO: implement this
3643 case wxGRID_COMBOBOX
:
3644 // TODO: implement this
3651 SendEvent( EVT_GRID_CELL_CHANGE
,
3652 m_currentCellCoords
.GetRow(),
3653 m_currentCellCoords
.GetCol() );
3660 // ------ Grid location functions
3661 // Note that all of these functions work with the logical coordinates of
3662 // grid cells and labels so you will need to convert from device
3663 // coordinates for mouse events etc.
3666 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3668 int row
= YToRow(y
);
3669 int col
= XToCol(x
);
3671 if ( row
== -1 || col
== -1 )
3673 coords
= wxGridNoCellCoords
;
3677 coords
.Set( row
, col
);
3682 int wxGrid::YToRow( int y
)
3686 for ( i
= 0; i
< m_numRows
; i
++ )
3688 if ( y
< m_rowBottoms
[i
] ) return i
;
3695 int wxGrid::XToCol( int x
)
3699 for ( i
= 0; i
< m_numCols
; i
++ )
3701 if ( x
< m_colRights
[i
] ) return i
;
3708 // return the row number that that the y coord is near the edge of, or
3709 // -1 if not near an edge
3711 int wxGrid::YToEdgeOfRow( int y
)
3715 for ( i
= 0; i
< m_numRows
; i
++ )
3717 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3719 d
= abs( y
- m_rowBottoms
[i
] );
3721 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3730 // return the col number that that the x coord is near the edge of, or
3731 // -1 if not near an edge
3733 int wxGrid::XToEdgeOfCol( int x
)
3737 for ( i
= 0; i
< m_numCols
; i
++ )
3739 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3741 d
= abs( x
- m_colRights
[i
] );
3743 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3752 wxRect
wxGrid::CellToRect( int row
, int col
)
3754 wxRect
rect( -1, -1, -1, -1 );
3756 if ( row
>= 0 && row
< m_numRows
&&
3757 col
>= 0 && col
< m_numCols
)
3759 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3760 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3761 rect
.width
= m_colWidths
[col
];
3762 rect
.height
= m_rowHeights
[ row
];
3769 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3771 // get the cell rectangle in logical coords
3773 wxRect
r( CellToRect( row
, col
) );
3775 // convert to device coords
3777 int left
, top
, right
, bottom
;
3778 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3779 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3781 // check against the client area of the grid window
3784 m_gridWin
->GetClientSize( &cw
, &ch
);
3786 if ( wholeCellVisible
)
3788 // is the cell wholly visible ?
3790 return ( left
>= 0 && right
<= cw
&&
3791 top
>= 0 && bottom
<= ch
);
3795 // is the cell partly visible ?
3797 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3798 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3803 // make the specified cell location visible by doing a minimal amount
3806 void wxGrid::MakeCellVisible( int row
, int col
)
3809 int xpos
= -1, ypos
= -1;
3811 if ( row
>= 0 && row
< m_numRows
&&
3812 col
>= 0 && col
< m_numCols
)
3814 // get the cell rectangle in logical coords
3816 wxRect
r( CellToRect( row
, col
) );
3818 // convert to device coords
3820 int left
, top
, right
, bottom
;
3821 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3822 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3825 m_gridWin
->GetClientSize( &cw
, &ch
);
3831 else if ( bottom
> ch
)
3833 int h
= r
.GetHeight();
3835 for ( i
= row
-1; i
>= 0; i
-- )
3837 if ( h
+ m_rowHeights
[i
] > ch
) break;
3839 h
+= m_rowHeights
[i
];
3840 ypos
-= m_rowHeights
[i
];
3843 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
3844 // have rounding errors (this is important, because if we do, we
3845 // might not scroll at all and some cells won't be redrawn)
3846 ypos
+= GRID_SCROLL_LINE
/ 2;
3853 else if ( right
> cw
)
3855 int w
= r
.GetWidth();
3857 for ( i
= col
-1; i
>= 0; i
-- )
3859 if ( w
+ m_colWidths
[i
] > cw
) break;
3861 w
+= m_colWidths
[i
];
3862 xpos
-= m_colWidths
[i
];
3865 // see comment for ypos above
3866 xpos
+= GRID_SCROLL_LINE
/ 2;
3869 if ( xpos
!= -1 || ypos
!= -1 )
3871 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
3872 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
3873 Scroll( xpos
, ypos
);
3881 // ------ Grid cursor movement functions
3884 bool wxGrid::MoveCursorUp()
3886 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3887 m_currentCellCoords
.GetRow() > 0 )
3889 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3890 m_currentCellCoords
.GetCol() );
3892 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3893 m_currentCellCoords
.GetCol() );
3902 bool wxGrid::MoveCursorDown()
3904 // TODO: allow for scrolling
3906 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3907 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3909 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3910 m_currentCellCoords
.GetCol() );
3912 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3913 m_currentCellCoords
.GetCol() );
3922 bool wxGrid::MoveCursorLeft()
3924 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3925 m_currentCellCoords
.GetCol() > 0 )
3927 MakeCellVisible( m_currentCellCoords
.GetRow(),
3928 m_currentCellCoords
.GetCol() - 1 );
3930 SetCurrentCell( m_currentCellCoords
.GetRow(),
3931 m_currentCellCoords
.GetCol() - 1 );
3940 bool wxGrid::MoveCursorRight()
3942 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3943 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
3945 MakeCellVisible( m_currentCellCoords
.GetRow(),
3946 m_currentCellCoords
.GetCol() + 1 );
3948 SetCurrentCell( m_currentCellCoords
.GetRow(),
3949 m_currentCellCoords
.GetCol() + 1 );
3958 bool wxGrid::MovePageUp()
3960 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3962 int row
= m_currentCellCoords
.GetRow();
3966 m_gridWin
->GetClientSize( &cw
, &ch
);
3968 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3969 int newRow
= YToRow( y
- ch
+ 1 );
3974 else if ( newRow
== row
)
3979 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3980 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3988 bool wxGrid::MovePageDown()
3990 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3992 int row
= m_currentCellCoords
.GetRow();
3993 if ( row
< m_numRows
)
3996 m_gridWin
->GetClientSize( &cw
, &ch
);
3998 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3999 int newRow
= YToRow( y
+ ch
);
4002 newRow
= m_numRows
- 1;
4004 else if ( newRow
== row
)
4009 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4010 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4018 bool wxGrid::MoveCursorUpBlock()
4021 m_currentCellCoords
!= wxGridNoCellCoords
&&
4022 m_currentCellCoords
.GetRow() > 0 )
4024 int row
= m_currentCellCoords
.GetRow();
4025 int col
= m_currentCellCoords
.GetCol();
4027 if ( m_table
->IsEmptyCell(row
, col
) )
4029 // starting in an empty cell: find the next block of
4035 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4038 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4040 // starting at the top of a block: find the next block
4046 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4051 // starting within a block: find the top of the block
4056 if ( m_table
->IsEmptyCell(row
, col
) )
4064 MakeCellVisible( row
, col
);
4065 SetCurrentCell( row
, col
);
4073 bool wxGrid::MoveCursorDownBlock()
4076 m_currentCellCoords
!= wxGridNoCellCoords
&&
4077 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4079 int row
= m_currentCellCoords
.GetRow();
4080 int col
= m_currentCellCoords
.GetCol();
4082 if ( m_table
->IsEmptyCell(row
, col
) )
4084 // starting in an empty cell: find the next block of
4087 while ( row
< m_numRows
-1 )
4090 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4093 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4095 // starting at the bottom of a block: find the next block
4098 while ( row
< m_numRows
-1 )
4101 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4106 // starting within a block: find the bottom of the block
4108 while ( row
< m_numRows
-1 )
4111 if ( m_table
->IsEmptyCell(row
, col
) )
4119 MakeCellVisible( row
, col
);
4120 SetCurrentCell( row
, col
);
4128 bool wxGrid::MoveCursorLeftBlock()
4131 m_currentCellCoords
!= wxGridNoCellCoords
&&
4132 m_currentCellCoords
.GetCol() > 0 )
4134 int row
= m_currentCellCoords
.GetRow();
4135 int col
= m_currentCellCoords
.GetCol();
4137 if ( m_table
->IsEmptyCell(row
, col
) )
4139 // starting in an empty cell: find the next block of
4145 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4148 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4150 // starting at the left of a block: find the next block
4156 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4161 // starting within a block: find the left of the block
4166 if ( m_table
->IsEmptyCell(row
, col
) )
4174 MakeCellVisible( row
, col
);
4175 SetCurrentCell( row
, col
);
4183 bool wxGrid::MoveCursorRightBlock()
4186 m_currentCellCoords
!= wxGridNoCellCoords
&&
4187 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4189 int row
= m_currentCellCoords
.GetRow();
4190 int col
= m_currentCellCoords
.GetCol();
4192 if ( m_table
->IsEmptyCell(row
, col
) )
4194 // starting in an empty cell: find the next block of
4197 while ( col
< m_numCols
-1 )
4200 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4203 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4205 // starting at the right of a block: find the next block
4208 while ( col
< m_numCols
-1 )
4211 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4216 // starting within a block: find the right of the block
4218 while ( col
< m_numCols
-1 )
4221 if ( m_table
->IsEmptyCell(row
, col
) )
4229 MakeCellVisible( row
, col
);
4230 SetCurrentCell( row
, col
);
4241 // ------ Label values and formatting
4244 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4246 *horiz
= m_rowLabelHorizAlign
;
4247 *vert
= m_rowLabelVertAlign
;
4250 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4252 *horiz
= m_colLabelHorizAlign
;
4253 *vert
= m_colLabelVertAlign
;
4256 wxString
wxGrid::GetRowLabelValue( int row
)
4260 return m_table
->GetRowLabelValue( row
);
4270 wxString
wxGrid::GetColLabelValue( int col
)
4274 return m_table
->GetColLabelValue( col
);
4285 void wxGrid::SetRowLabelSize( int width
)
4287 width
= wxMax( width
, 0 );
4288 if ( width
!= m_rowLabelWidth
)
4292 m_rowLabelWin
->Show( FALSE
);
4293 m_cornerLabelWin
->Show( FALSE
);
4295 else if ( m_rowLabelWidth
== 0 )
4297 m_rowLabelWin
->Show( TRUE
);
4298 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4301 m_rowLabelWidth
= width
;
4308 void wxGrid::SetColLabelSize( int height
)
4310 height
= wxMax( height
, 0 );
4311 if ( height
!= m_colLabelHeight
)
4315 m_colLabelWin
->Show( FALSE
);
4316 m_cornerLabelWin
->Show( FALSE
);
4318 else if ( m_colLabelHeight
== 0 )
4320 m_colLabelWin
->Show( TRUE
);
4321 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4324 m_colLabelHeight
= height
;
4331 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
4333 if ( m_labelBackgroundColour
!= colour
)
4335 m_labelBackgroundColour
= colour
;
4336 m_rowLabelWin
->SetBackgroundColour( colour
);
4337 m_colLabelWin
->SetBackgroundColour( colour
);
4338 m_cornerLabelWin
->SetBackgroundColour( colour
);
4340 if ( !GetBatchCount() )
4342 m_rowLabelWin
->Refresh();
4343 m_colLabelWin
->Refresh();
4344 m_cornerLabelWin
->Refresh();
4349 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
4351 if ( m_labelTextColour
!= colour
)
4353 m_labelTextColour
= colour
;
4354 if ( !GetBatchCount() )
4356 m_rowLabelWin
->Refresh();
4357 m_colLabelWin
->Refresh();
4362 void wxGrid::SetLabelFont( const wxFont
& font
)
4365 if ( !GetBatchCount() )
4367 m_rowLabelWin
->Refresh();
4368 m_colLabelWin
->Refresh();
4372 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
4374 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4376 m_rowLabelHorizAlign
= horiz
;
4379 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4381 m_rowLabelVertAlign
= vert
;
4384 if ( !GetBatchCount() )
4386 m_rowLabelWin
->Refresh();
4390 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
4392 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4394 m_colLabelHorizAlign
= horiz
;
4397 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4399 m_colLabelVertAlign
= vert
;
4402 if ( !GetBatchCount() )
4404 m_colLabelWin
->Refresh();
4408 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
4412 m_table
->SetRowLabelValue( row
, s
);
4413 if ( !GetBatchCount() )
4415 wxRect rect
= CellToRect( row
, 0);
4416 if ( rect
.height
> 0 )
4418 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
4420 rect
.width
= m_rowLabelWidth
;
4421 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4427 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
4431 m_table
->SetColLabelValue( col
, s
);
4432 if ( !GetBatchCount() )
4434 wxRect rect
= CellToRect( 0, col
);
4435 if ( rect
.width
> 0 )
4437 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
4439 rect
.height
= m_colLabelHeight
;
4440 m_colLabelWin
->Refresh( TRUE
, &rect
);
4446 void wxGrid::SetGridLineColour( const wxColour
& colour
)
4448 if ( m_gridLineColour
!= colour
)
4450 m_gridLineColour
= colour
;
4452 wxClientDC
dc( m_gridWin
);
4454 DrawAllGridLines( dc
, wxRegion() );
4458 void wxGrid::EnableGridLines( bool enable
)
4460 if ( enable
!= m_gridLinesEnabled
)
4462 m_gridLinesEnabled
= enable
;
4464 if ( !GetBatchCount() )
4468 wxClientDC
dc( m_gridWin
);
4470 DrawAllGridLines( dc
, wxRegion() );
4474 m_gridWin
->Refresh();
4481 int wxGrid::GetDefaultRowSize()
4483 return m_defaultRowHeight
;
4486 int wxGrid::GetRowSize( int row
)
4488 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
4490 return m_rowHeights
[row
];
4493 int wxGrid::GetDefaultColSize()
4495 return m_defaultColWidth
;
4498 int wxGrid::GetColSize( int col
)
4500 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
4502 return m_colWidths
[col
];
4505 wxColour
wxGrid::GetDefaultCellBackgroundColour()
4507 return m_gridWin
->GetBackgroundColour();
4510 // TODO VZ: this must be optimized to allow only retrieveing attr once!
4512 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
4514 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4517 if ( attr
&& attr
->HasBackgroundColour() )
4518 colour
= attr
->GetBackgroundColour();
4520 colour
= GetDefaultCellBackgroundColour();
4527 wxColour
wxGrid::GetDefaultCellTextColour()
4529 return m_gridWin
->GetForegroundColour();
4532 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
4534 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4537 if ( attr
&& attr
->HasTextColour() )
4538 colour
= attr
->GetTextColour();
4540 colour
= GetDefaultCellTextColour();
4548 wxFont
wxGrid::GetDefaultCellFont()
4550 return m_defaultCellFont
;
4553 wxFont
wxGrid::GetCellFont( int row
, int col
)
4555 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4558 if ( attr
&& attr
->HasFont() )
4559 font
= attr
->GetFont();
4561 font
= GetDefaultCellFont();
4568 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
4571 *horiz
= m_defaultCellHAlign
;
4573 *vert
= m_defaultCellVAlign
;
4576 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
4578 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4580 if ( attr
&& attr
->HasAlignment() )
4581 attr
->GetAlignment(horiz
, vert
);
4583 GetDefaultCellAlignment(horiz
, vert
);
4588 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
4590 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
4592 if ( resizeExistingRows
)
4596 for ( row
= 0; row
< m_numRows
; row
++ )
4598 m_rowHeights
[row
] = m_defaultRowHeight
;
4599 bottom
+= m_defaultRowHeight
;
4600 m_rowBottoms
[row
] = bottom
;
4606 void wxGrid::SetRowSize( int row
, int height
)
4608 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
4612 int h
= wxMax( 0, height
);
4613 int diff
= h
- m_rowHeights
[row
];
4615 m_rowHeights
[row
] = h
;
4616 for ( i
= row
; i
< m_numRows
; i
++ )
4618 m_rowBottoms
[i
] += diff
;
4623 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4625 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4627 if ( resizeExistingCols
)
4631 for ( col
= 0; col
< m_numCols
; col
++ )
4633 m_colWidths
[col
] = m_defaultColWidth
;
4634 right
+= m_defaultColWidth
;
4635 m_colRights
[col
] = right
;
4641 void wxGrid::SetColSize( int col
, int width
)
4643 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
4647 int w
= wxMax( 0, width
);
4648 int diff
= w
- m_colWidths
[col
];
4649 m_colWidths
[col
] = w
;
4651 for ( i
= col
; i
< m_numCols
; i
++ )
4653 m_colRights
[i
] += diff
;
4658 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
4660 m_gridWin
->SetBackgroundColour(col
);
4663 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
4665 m_gridWin
->SetForegroundColour(col
);
4668 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
4670 m_defaultCellHAlign
= horiz
;
4671 m_defaultCellVAlign
= vert
;
4674 bool wxGrid::CanHaveAttributes()
4681 if ( !m_table
->GetAttrProvider() )
4683 // use the default attr provider by default
4684 // (another choice would be to just return FALSE thus forcing the user
4686 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
4692 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
4694 if ( CanHaveAttributes() )
4696 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4697 attr
->SetBackgroundColour(colour
);
4699 m_table
->SetAttr(attr
, row
, col
);
4703 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
4705 if ( CanHaveAttributes() )
4707 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4708 attr
->SetTextColour(colour
);
4710 m_table
->SetAttr(attr
, row
, col
);
4714 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
4716 m_defaultCellFont
= font
;
4719 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
4721 if ( CanHaveAttributes() )
4723 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4724 attr
->SetFont(font
);
4726 m_table
->SetAttr(attr
, row
, col
);
4730 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
4732 if ( CanHaveAttributes() )
4734 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4735 attr
->SetAlignment(horiz
, vert
);
4737 m_table
->SetAttr(attr
, row
, col
);
4744 // ------ cell value accessor functions
4747 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4751 m_table
->SetValue( row
, col
, s
.c_str() );
4752 if ( !GetBatchCount() )
4754 wxClientDC
dc( m_gridWin
);
4756 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4759 #if 0 // TODO: edit in place
4761 if ( m_currentCellCoords
.GetRow() == row
&&
4762 m_currentCellCoords
.GetCol() == col
)
4764 SetEditControlValue( s
);
4773 // ------ Block, row and col selection
4776 void wxGrid::SelectRow( int row
, bool addToSelected
)
4780 if ( IsSelection() && addToSelected
)
4783 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4786 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4787 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4788 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4789 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4793 need_refresh
[0] = TRUE
;
4794 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
4795 wxGridCellCoords ( oldTop
- 1,
4797 m_selectedTopLeft
.SetRow( row
);
4802 need_refresh
[1] = TRUE
;
4803 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
4804 wxGridCellCoords ( oldBottom
,
4807 m_selectedTopLeft
.SetCol( 0 );
4810 if ( oldBottom
< row
)
4812 need_refresh
[2] = TRUE
;
4813 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
4814 wxGridCellCoords ( row
,
4816 m_selectedBottomRight
.SetRow( row
);
4819 if ( oldRight
< m_numCols
- 1 )
4821 need_refresh
[3] = TRUE
;
4822 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4824 wxGridCellCoords ( oldBottom
,
4826 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4829 for (i
= 0; i
< 4; i
++ )
4830 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4831 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4835 r
= SelectionToDeviceRect();
4837 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4839 m_selectedTopLeft
.Set( row
, 0 );
4840 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4841 r
= SelectionToDeviceRect();
4842 m_gridWin
->Refresh( FALSE
, &r
);
4845 wxGridRangeSelectEvent
gridEvt( GetId(),
4846 EVT_GRID_RANGE_SELECT
,
4849 m_selectedBottomRight
);
4851 GetEventHandler()->ProcessEvent(gridEvt
);
4855 void wxGrid::SelectCol( int col
, bool addToSelected
)
4857 if ( IsSelection() && addToSelected
)
4860 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4863 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4864 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4865 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4866 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4868 if ( oldLeft
> col
)
4870 need_refresh
[0] = TRUE
;
4871 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
4872 wxGridCellCoords ( m_numRows
- 1,
4874 m_selectedTopLeft
.SetCol( col
);
4879 need_refresh
[1] = TRUE
;
4880 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
4881 wxGridCellCoords ( oldTop
- 1,
4883 m_selectedTopLeft
.SetRow( 0 );
4886 if ( oldRight
< col
)
4888 need_refresh
[2] = TRUE
;
4889 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
4890 wxGridCellCoords ( m_numRows
- 1,
4892 m_selectedBottomRight
.SetCol( col
);
4895 if ( oldBottom
< m_numRows
- 1 )
4897 need_refresh
[3] = TRUE
;
4898 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
4900 wxGridCellCoords ( m_numRows
- 1,
4902 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4905 for (i
= 0; i
< 4; i
++ )
4906 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4907 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4913 r
= SelectionToDeviceRect();
4915 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4917 m_selectedTopLeft
.Set( 0, col
);
4918 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4919 r
= SelectionToDeviceRect();
4920 m_gridWin
->Refresh( FALSE
, &r
);
4923 wxGridRangeSelectEvent
gridEvt( GetId(),
4924 EVT_GRID_RANGE_SELECT
,
4927 m_selectedBottomRight
);
4929 GetEventHandler()->ProcessEvent(gridEvt
);
4933 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4936 wxGridCellCoords updateTopLeft
, updateBottomRight
;
4938 if ( topRow
> bottomRow
)
4945 if ( leftCol
> rightCol
)
4952 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
4953 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
4955 if ( m_selectedTopLeft
!= updateTopLeft
||
4956 m_selectedBottomRight
!= updateBottomRight
)
4958 // Compute two optimal update rectangles:
4959 // Either one rectangle is a real subset of the
4960 // other, or they are (almost) disjoint!
4962 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4965 // Store intermediate values
4966 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4967 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4968 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4969 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4971 // Determine the outer/inner coordinates.
4972 if (oldLeft
> leftCol
)
4978 if (oldTop
> topRow
)
4984 if (oldRight
< rightCol
)
4987 oldRight
= rightCol
;
4990 if (oldBottom
< bottomRow
)
4993 oldBottom
= bottomRow
;
4997 // Now, either the stuff marked old is the outer
4998 // rectangle or we don't have a situation where one
4999 // is contained in the other.
5001 if ( oldLeft
< leftCol
)
5003 need_refresh
[0] = TRUE
;
5004 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5006 wxGridCellCoords ( oldBottom
,
5010 if ( oldTop
< topRow
)
5012 need_refresh
[1] = TRUE
;
5013 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5015 wxGridCellCoords ( topRow
- 1,
5019 if ( oldRight
> rightCol
)
5021 need_refresh
[2] = TRUE
;
5022 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5024 wxGridCellCoords ( oldBottom
,
5028 if ( oldBottom
> bottomRow
)
5030 need_refresh
[3] = TRUE
;
5031 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5033 wxGridCellCoords ( oldBottom
,
5039 m_selectedTopLeft
= updateTopLeft
;
5040 m_selectedBottomRight
= updateBottomRight
;
5042 // various Refresh() calls
5043 for (i
= 0; i
< 4; i
++ )
5044 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5045 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5048 // only generate an event if the block is not being selected by
5049 // dragging the mouse (in which case the event will be generated in
5050 // the mouse event handler)
5051 if ( !m_isDragging
)
5053 wxGridRangeSelectEvent
gridEvt( GetId(),
5054 EVT_GRID_RANGE_SELECT
,
5057 m_selectedBottomRight
);
5059 GetEventHandler()->ProcessEvent(gridEvt
);
5063 void wxGrid::SelectAll()
5065 m_selectedTopLeft
.Set( 0, 0 );
5066 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5068 m_gridWin
->Refresh();
5072 void wxGrid::ClearSelection()
5074 m_selectedTopLeft
= wxGridNoCellCoords
;
5075 m_selectedBottomRight
= wxGridNoCellCoords
;
5079 // This function returns the rectangle that encloses the given block
5080 // in device coords clipped to the client size of the grid window.
5082 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5083 const wxGridCellCoords
&bottomRight
)
5085 wxRect
rect( wxGridNoCellRect
);
5088 cellRect
= CellToRect( topLeft
);
5089 if ( cellRect
!= wxGridNoCellRect
)
5095 rect
= wxRect( 0, 0, 0, 0 );
5098 cellRect
= CellToRect( bottomRight
);
5099 if ( cellRect
!= wxGridNoCellRect
)
5105 return wxGridNoCellRect
;
5108 // convert to scrolled coords
5110 int left
, top
, right
, bottom
;
5111 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5112 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5115 m_gridWin
->GetClientSize( &cw
, &ch
);
5117 rect
.SetLeft( wxMax(0, left
) );
5118 rect
.SetTop( wxMax(0, top
) );
5119 rect
.SetRight( wxMin(cw
, right
) );
5120 rect
.SetBottom( wxMin(ch
, bottom
) );
5128 // ------ Grid event classes
5131 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5133 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5134 int row
, int col
, int x
, int y
,
5135 bool control
, bool shift
, bool alt
, bool meta
)
5136 : wxNotifyEvent( type
, id
)
5142 m_control
= control
;
5147 SetEventObject(obj
);
5151 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
5153 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
5154 int rowOrCol
, int x
, int y
,
5155 bool control
, bool shift
, bool alt
, bool meta
)
5156 : wxNotifyEvent( type
, id
)
5158 m_rowOrCol
= rowOrCol
;
5161 m_control
= control
;
5166 SetEventObject(obj
);
5170 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
5172 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
5173 const wxGridCellCoords
& topLeft
,
5174 const wxGridCellCoords
& bottomRight
,
5175 bool control
, bool shift
, bool alt
, bool meta
)
5176 : wxNotifyEvent( type
, id
)
5178 m_topLeft
= topLeft
;
5179 m_bottomRight
= bottomRight
;
5180 m_control
= control
;
5185 SetEventObject(obj
);
5189 #endif // ifndef wxUSE_NEW_GRID