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
;
1326 m_winCapture
= (wxWindow
*)NULL
;
1328 m_dragRowOrCol
= -1;
1329 m_isDragging
= FALSE
;
1331 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1332 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1334 m_currentCellCoords
= wxGridNoCellCoords
;
1336 m_selectedTopLeft
= wxGridNoCellCoords
;
1337 m_selectedBottomRight
= wxGridNoCellCoords
;
1339 m_editable
= TRUE
; // default for whole grid
1341 m_inOnKeyDown
= FALSE
;
1344 // TODO: extend this to other types of controls
1346 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1353 #if defined(__WXMSW__)
1354 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1358 m_cellEditCtrl
->Show( FALSE
);
1359 m_cellEditCtrlEnabled
= TRUE
;
1360 m_editCtrlType
= wxGRID_TEXTCTRL
;
1364 void wxGrid
::CalcDimensions()
1367 GetClientSize( &cw
, &ch
);
1369 if ( m_numRows
> 0 && m_numCols
> 0 )
1371 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1372 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1374 // TODO: restore the scroll position that we had before sizing
1377 GetViewStart( &x
, &y
);
1378 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1379 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1385 void wxGrid
::CalcWindowSizes()
1388 GetClientSize( &cw
, &ch
);
1390 if ( m_cornerLabelWin
->IsShown() )
1391 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
1393 if ( m_colLabelWin
->IsShown() )
1394 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
1396 if ( m_rowLabelWin
->IsShown() )
1397 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
1399 if ( m_gridWin
->IsShown() )
1400 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
1404 // this is called when the grid table sends a message to say that it
1405 // has been redimensioned
1407 bool wxGrid
::Redimension( wxGridTableMessage
& msg
)
1411 switch ( msg
.GetId() )
1413 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1415 size_t pos
= msg
.GetCommandInt();
1416 int numRows
= msg
.GetCommandInt2();
1417 for ( i
= 0; i
< numRows
; i
++ )
1419 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1420 m_rowBottoms
.Insert( 0, pos
);
1422 m_numRows
+= numRows
;
1425 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1427 for ( i
= pos
; i
< m_numRows
; i
++ )
1429 bottom
+= m_rowHeights
[i
];
1430 m_rowBottoms
[i
] = bottom
;
1436 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1438 int numRows
= msg
.GetCommandInt();
1439 for ( i
= 0; i
< numRows
; i
++ )
1441 m_rowHeights
.Add( m_defaultRowHeight
);
1442 m_rowBottoms
.Add( 0 );
1445 int oldNumRows
= m_numRows
;
1446 m_numRows
+= numRows
;
1449 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1451 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1453 bottom
+= m_rowHeights
[i
];
1454 m_rowBottoms
[i
] = bottom
;
1460 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1462 size_t pos
= msg
.GetCommandInt();
1463 int numRows
= msg
.GetCommandInt2();
1464 for ( i
= 0; i
< numRows
; i
++ )
1466 m_rowHeights
.Remove( pos
);
1467 m_rowBottoms
.Remove( pos
);
1469 m_numRows
-= numRows
;
1474 m_colWidths
.Clear();
1475 m_colRights
.Clear();
1476 m_currentCellCoords
= wxGridNoCellCoords
;
1480 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1481 m_currentCellCoords
.Set( 0, 0 );
1484 for ( i
= 0; i
< m_numRows
; i
++ )
1486 h
+= m_rowHeights
[i
];
1487 m_rowBottoms
[i
] = h
;
1495 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1497 size_t pos
= msg
.GetCommandInt();
1498 int numCols
= msg
.GetCommandInt2();
1499 for ( i
= 0; i
< numCols
; i
++ )
1501 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1502 m_colRights
.Insert( 0, pos
);
1504 m_numCols
+= numCols
;
1507 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1509 for ( i
= pos
; i
< m_numCols
; i
++ )
1511 right
+= m_colWidths
[i
];
1512 m_colRights
[i
] = right
;
1518 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1520 int numCols
= msg
.GetCommandInt();
1521 for ( i
= 0; i
< numCols
; i
++ )
1523 m_colWidths
.Add( m_defaultColWidth
);
1524 m_colRights
.Add( 0 );
1527 int oldNumCols
= m_numCols
;
1528 m_numCols
+= numCols
;
1531 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1533 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1535 right
+= m_colWidths
[i
];
1536 m_colRights
[i
] = right
;
1542 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1544 size_t pos
= msg
.GetCommandInt();
1545 int numCols
= msg
.GetCommandInt2();
1546 for ( i
= 0; i
< numCols
; i
++ )
1548 m_colWidths
.Remove( pos
);
1549 m_colRights
.Remove( pos
);
1551 m_numCols
-= numCols
;
1555 #if 0 // leave the row alone here so that AppendCols will work subsequently
1557 m_rowHeights
.Clear();
1558 m_rowBottoms
.Clear();
1560 m_currentCellCoords
= wxGridNoCellCoords
;
1564 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1565 m_currentCellCoords
.Set( 0, 0 );
1568 for ( i
= 0; i
< m_numCols
; i
++ )
1570 w
+= m_colWidths
[i
];
1583 void wxGrid
::CalcRowLabelsExposed( wxRegion
& reg
)
1585 wxRegionIterator
iter( reg
);
1588 m_rowLabelsExposed
.Empty();
1595 // TODO: remove this when we can...
1596 // There is a bug in wxMotif that gives garbage update
1597 // rectangles if you jump-scroll a long way by clicking the
1598 // scrollbar with middle button. This is a work-around
1600 #if defined(__WXMOTIF__)
1602 m_gridWin
->GetClientSize( &cw
, &ch
);
1603 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1604 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1607 // logical bounds of update region
1610 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1611 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1613 // find the row labels within these bounds
1617 for ( row
= 0; row
< m_numRows
; row
++ )
1619 if ( m_rowBottoms
[row
] < top
) continue;
1621 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1622 if ( rowTop
> bottom
) break;
1624 m_rowLabelsExposed
.Add( row
);
1632 void wxGrid
::CalcColLabelsExposed( wxRegion
& reg
)
1634 wxRegionIterator
iter( reg
);
1637 m_colLabelsExposed
.Empty();
1644 // TODO: remove this when we can...
1645 // There is a bug in wxMotif that gives garbage update
1646 // rectangles if you jump-scroll a long way by clicking the
1647 // scrollbar with middle button. This is a work-around
1649 #if defined(__WXMOTIF__)
1651 m_gridWin
->GetClientSize( &cw
, &ch
);
1652 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1653 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1656 // logical bounds of update region
1659 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1660 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1662 // find the cells within these bounds
1666 for ( col
= 0; col
< m_numCols
; col
++ )
1668 if ( m_colRights
[col
] < left
) continue;
1670 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1671 if ( colLeft
> right
) break;
1673 m_colLabelsExposed
.Add( col
);
1681 void wxGrid
::CalcCellsExposed( wxRegion
& reg
)
1683 wxRegionIterator
iter( reg
);
1686 m_cellsExposed
.Empty();
1687 m_rowsExposed
.Empty();
1688 m_colsExposed
.Empty();
1690 int left
, top
, right
, bottom
;
1695 // TODO: remove this when we can...
1696 // There is a bug in wxMotif that gives garbage update
1697 // rectangles if you jump-scroll a long way by clicking the
1698 // scrollbar with middle button. This is a work-around
1700 #if defined(__WXMOTIF__)
1702 m_gridWin
->GetClientSize( &cw
, &ch
);
1703 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1704 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1705 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1706 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1709 // logical bounds of update region
1711 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1712 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1714 // find the cells within these bounds
1717 int colLeft
, rowTop
;
1718 for ( row
= 0; row
< m_numRows
; row
++ )
1720 if ( m_rowBottoms
[row
] < top
) continue;
1722 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1723 if ( rowTop
> bottom
) break;
1725 m_rowsExposed
.Add( row
);
1727 for ( col
= 0; col
< m_numCols
; col
++ )
1729 if ( m_colRights
[col
] < left
) continue;
1731 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1732 if ( colLeft
> right
) break;
1734 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1735 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1744 void wxGrid
::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1747 wxPoint
pos( event
.GetPosition() );
1748 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1750 if ( event
.Dragging() )
1752 m_isDragging
= TRUE
;
1754 if ( event
.LeftIsDown() )
1756 switch( m_cursorMode
)
1758 case WXGRID_CURSOR_RESIZE_ROW
:
1760 int cw
, ch
, left
, dummy
;
1761 m_gridWin
->GetClientSize( &cw
, &ch
);
1762 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1764 wxClientDC
dc( m_gridWin
);
1766 dc
.SetLogicalFunction(wxINVERT
);
1767 if ( m_dragLastPos
>= 0 )
1769 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1771 dc
.DrawLine( left
, y
, left
+cw
, y
);
1776 case WXGRID_CURSOR_SELECT_ROW
:
1777 if ( (row
= YToRow( y
)) >= 0 &&
1778 !IsInSelection( row
, 0 ) )
1780 SelectRow( row
, TRUE
);
1783 // default label to suppress warnings about "enumeration value
1784 // 'xxx' not handled in switch
1792 m_isDragging
= FALSE
;
1795 // ------------ Entering or leaving the window
1797 if ( event
.Entering() || event
.Leaving() )
1799 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
1803 // ------------ Left button pressed
1805 else if ( event
.LeftDown() )
1807 // don't send a label click event for a hit on the
1808 // edge of the row label - this is probably the user
1809 // wanting to resize the row
1811 if ( YToEdgeOfRow(y
) < 0 )
1815 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
1817 SelectRow( row
, event
.ShiftDown() );
1818 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
1823 // starting to drag-resize a row
1825 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
1830 // ------------ Left double click
1832 else if (event
.LeftDClick() )
1834 if ( YToEdgeOfRow(y
) < 0 )
1837 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
1842 // ------------ Left button released
1844 else if ( event
.LeftUp() )
1846 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
1848 DoEndDragResizeRow();
1850 // Note: we are ending the event *after* doing
1851 // default processing in this case
1853 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1856 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
1861 // ------------ Right button down
1863 else if ( event
.RightDown() )
1866 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1868 // no default action at the moment
1873 // ------------ Right double click
1875 else if ( event
.RightDClick() )
1878 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1880 // no default action at the moment
1885 // ------------ No buttons down and mouse moving
1887 else if ( event
.Moving() )
1889 m_dragRowOrCol
= YToEdgeOfRow( y
);
1890 if ( m_dragRowOrCol
>= 0 )
1892 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1894 // don't capture the mouse yet
1895 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
1898 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1900 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
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
:
1939 if ( (col
= XToCol( x
)) >= 0 &&
1940 !IsInSelection( 0, col
) )
1942 SelectCol( col
, TRUE
);
1945 // default label to suppress warnings about "enumeration value
1946 // 'xxx' not handled in switch
1954 m_isDragging
= FALSE
;
1957 // ------------ Entering or leaving the window
1959 if ( event
.Entering() || event
.Leaving() )
1961 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
1965 // ------------ Left button pressed
1967 else if ( event
.LeftDown() )
1969 // don't send a label click event for a hit on the
1970 // edge of the col label - this is probably the user
1971 // wanting to resize the col
1973 if ( XToEdgeOfCol(x
) < 0 )
1977 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
1979 SelectCol( col
, event
.ShiftDown() );
1980 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
1985 // starting to drag-resize a col
1987 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
1992 // ------------ Left double click
1994 if ( event
.LeftDClick() )
1996 if ( XToEdgeOfCol(x
) < 0 )
1999 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2004 // ------------ Left button released
2006 else if ( event
.LeftUp() )
2008 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
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 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
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 // don't capture the cursor yet
2057 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2060 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2062 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2068 void wxGrid
::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2070 if ( event
.LeftDown() )
2072 // indicate corner label by having both row and
2075 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2081 else if ( event
.LeftDClick() )
2083 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2086 else if ( event
.RightDown() )
2088 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2090 // no default action at the moment
2094 else if ( event
.RightDClick() )
2096 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2098 // no default action at the moment
2103 void wxGrid
::ChangeCursorMode(CursorMode mode
,
2108 static const wxChar
*cursorModes
[] =
2117 wxLogTrace(_T("grid"),
2118 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2119 win
== m_colLabelWin ?
_T("colLabelWin")
2120 : win ?
_T("rowLabelWin")
2122 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2123 #endif // __WXDEBUG__
2125 if ( mode
== m_cursorMode
)
2130 // by default use the grid itself
2136 m_winCapture
->ReleaseMouse();
2137 m_winCapture
= (wxWindow
*)NULL
;
2140 m_cursorMode
= mode
;
2142 switch ( m_cursorMode
)
2144 case WXGRID_CURSOR_RESIZE_ROW
:
2145 win
->SetCursor( m_rowResizeCursor
);
2148 case WXGRID_CURSOR_RESIZE_COL
:
2149 win
->SetCursor( m_colResizeCursor
);
2153 win
->SetCursor( *wxSTANDARD_CURSOR
);
2156 // we need to capture mouse when resizing
2157 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2158 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2160 if ( captureMouse
&& resize
)
2162 win
->CaptureMouse();
2167 void wxGrid
::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2170 wxPoint
pos( event
.GetPosition() );
2171 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2173 wxGridCellCoords coords
;
2174 XYToCell( x
, y
, coords
);
2176 if ( event
.Dragging() )
2178 m_isDragging
= TRUE
;
2179 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2181 // Hide the edit control, so it
2182 // won't interfer with drag-shrinking.
2183 if ( IsCellEditControlEnabled() )
2184 HideCellEditControl();
2185 if ( coords
!= wxGridNoCellCoords
)
2187 if ( !IsSelection() )
2189 SelectBlock( coords
, coords
);
2193 SelectBlock( m_currentCellCoords
, coords
);
2197 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2199 int cw
, ch
, left
, dummy
;
2200 m_gridWin
->GetClientSize( &cw
, &ch
);
2201 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2203 wxClientDC
dc( m_gridWin
);
2205 dc
.SetLogicalFunction(wxINVERT
);
2206 if ( m_dragLastPos
>= 0 )
2208 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2210 dc
.DrawLine( left
, y
, left
+cw
, y
);
2213 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2215 int cw
, ch
, dummy
, top
;
2216 m_gridWin
->GetClientSize( &cw
, &ch
);
2217 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2219 wxClientDC
dc( m_gridWin
);
2221 dc
.SetLogicalFunction(wxINVERT
);
2222 if ( m_dragLastPos
>= 0 )
2224 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2226 dc
.DrawLine( x
, top
, x
, top
+ch
);
2233 m_isDragging
= FALSE
;
2235 if ( coords
!= wxGridNoCellCoords
)
2237 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2238 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2241 if ( event
.Entering() || event
.Leaving() )
2243 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2244 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2249 // ------------ Left button pressed
2251 if ( event
.LeftDown() )
2253 if ( event
.ShiftDown() )
2255 SelectBlock( m_currentCellCoords
, coords
);
2257 else if ( XToEdgeOfCol(x
) < 0 &&
2258 YToEdgeOfRow(y
) < 0 )
2260 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2265 MakeCellVisible( coords
);
2266 SetCurrentCell( coords
);
2272 // ------------ Left double click
2274 else if ( event
.LeftDClick() )
2276 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2278 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2286 // ------------ Left button released
2288 else if ( event
.LeftUp() )
2290 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2292 if ( IsSelection() )
2294 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
2297 // Show the edit control, if it has
2298 // been hidden for drag-shrinking.
2299 if ( IsCellEditControlEnabled() )
2300 ShowCellEditControl();
2302 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2304 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2305 DoEndDragResizeRow();
2307 // Note: we are ending the event *after* doing
2308 // default processing in this case
2310 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2312 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2314 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2315 DoEndDragResizeCol();
2317 // Note: we are ending the event *after* doing
2318 // default processing in this case
2320 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2327 // ------------ Right button down
2329 else if ( event
.RightDown() )
2331 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2336 // no default action at the moment
2341 // ------------ Right double click
2343 else if ( event
.RightDClick() )
2345 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2350 // no default action at the moment
2354 // ------------ Moving and no button action
2356 else if ( event
.Moving() && !event
.IsButton() )
2358 int dragRow
= YToEdgeOfRow( y
);
2359 int dragCol
= XToEdgeOfCol( x
);
2361 // Dragging on the corner of a cell to resize in both
2362 // directions is not implemented yet...
2364 if ( dragRow
>= 0 && dragCol
>= 0 )
2366 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2372 m_dragRowOrCol
= dragRow
;
2374 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2376 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
2384 m_dragRowOrCol
= dragCol
;
2386 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2388 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
2394 // Neither on a row or col edge
2396 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2398 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2405 void wxGrid
::DoEndDragResizeRow()
2407 if ( m_dragLastPos
>= 0 )
2409 // erase the last line and resize the row
2411 int cw
, ch
, left
, dummy
;
2412 m_gridWin
->GetClientSize( &cw
, &ch
);
2413 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2415 wxClientDC
dc( m_gridWin
);
2417 dc
.SetLogicalFunction( wxINVERT
);
2418 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2419 HideCellEditControl();
2421 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
2422 SetRowSize( m_dragRowOrCol
,
2423 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
2425 if ( !GetBatchCount() )
2427 // Only needed to get the correct rect.y:
2428 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
2430 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
2431 rect
.width
= m_rowLabelWidth
;
2432 rect
.height
= ch
- rect
.y
;
2433 m_rowLabelWin
->Refresh( TRUE
, &rect
);
2435 m_gridWin
->Refresh( FALSE
, &rect
);
2438 ShowCellEditControl();
2443 void wxGrid
::DoEndDragResizeCol()
2445 if ( m_dragLastPos
>= 0 )
2447 // erase the last line and resize the col
2449 int cw
, ch
, dummy
, top
;
2450 m_gridWin
->GetClientSize( &cw
, &ch
);
2451 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2453 wxClientDC
dc( m_gridWin
);
2455 dc
.SetLogicalFunction( wxINVERT
);
2456 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2457 HideCellEditControl();
2459 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
2460 SetColSize( m_dragRowOrCol
,
2461 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
2463 if ( !GetBatchCount() )
2465 // Only needed to get the correct rect.x:
2466 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
2468 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
2469 rect
.width
= cw
- rect
.x
;
2470 rect
.height
= m_colLabelHeight
;
2471 m_colLabelWin
->Refresh( TRUE
, &rect
);
2473 m_gridWin
->Refresh( FALSE
, &rect
);
2476 ShowCellEditControl();
2483 // ------ interaction with data model
2485 bool wxGrid
::ProcessTableMessage( wxGridTableMessage
& msg
)
2487 switch ( msg
.GetId() )
2489 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
2490 return GetModelValues();
2492 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
2493 return SetModelValues();
2495 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2496 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2497 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2498 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2499 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2500 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2501 return Redimension( msg
);
2510 // The behaviour of this function depends on the grid table class
2511 // Clear() function. For the default wxGridStringTable class the
2512 // behavious is to replace all cell contents with wxEmptyString but
2513 // not to change the number of rows or cols.
2515 void wxGrid
::ClearGrid()
2520 SetEditControlValue();
2521 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2526 bool wxGrid
::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2528 // TODO: something with updateLabels flag
2532 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2538 bool ok
= m_table
->InsertRows( pos
, numRows
);
2540 // the table will have sent the results of the insert row
2541 // operation to this view object as a grid table message
2545 if ( m_numCols
== 0 )
2547 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2549 // TODO: perhaps instead of appending the default number of cols
2550 // we should remember what the last non-zero number of cols was ?
2554 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2556 // if we have just inserted cols into an empty grid the current
2557 // cell will be undefined...
2559 SetCurrentCell( 0, 0 );
2563 if ( !GetBatchCount() ) Refresh();
2566 SetEditControlValue();
2576 bool wxGrid
::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2578 // TODO: something with updateLabels flag
2582 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2586 if ( m_table
&& m_table
->AppendRows( numRows
) )
2588 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2590 // if we have just inserted cols into an empty grid the current
2591 // cell will be undefined...
2593 SetCurrentCell( 0, 0 );
2596 // the table will have sent the results of the append row
2597 // operation to this view object as a grid table message
2600 if ( !GetBatchCount() ) Refresh();
2610 bool wxGrid
::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2612 // TODO: something with updateLabels flag
2616 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2620 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2622 // the table will have sent the results of the delete row
2623 // operation to this view object as a grid table message
2625 if ( m_numRows
> 0 )
2626 SetEditControlValue();
2628 HideCellEditControl();
2631 if ( !GetBatchCount() ) Refresh();
2641 bool wxGrid
::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2643 // TODO: something with updateLabels flag
2647 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2653 HideCellEditControl();
2654 bool ok
= m_table
->InsertCols( pos
, numCols
);
2656 // the table will have sent the results of the insert col
2657 // operation to this view object as a grid table message
2661 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2663 // if we have just inserted cols into an empty grid the current
2664 // cell will be undefined...
2666 SetCurrentCell( 0, 0 );
2670 if ( !GetBatchCount() ) Refresh();
2673 SetEditControlValue();
2683 bool wxGrid
::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2685 // TODO: something with updateLabels flag
2689 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2693 if ( m_table
&& m_table
->AppendCols( numCols
) )
2695 // the table will have sent the results of the append col
2696 // operation to this view object as a grid table message
2698 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2700 // if we have just inserted cols into an empty grid the current
2701 // cell will be undefined...
2703 SetCurrentCell( 0, 0 );
2707 if ( !GetBatchCount() ) Refresh();
2717 bool wxGrid
::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2719 // TODO: something with updateLabels flag
2723 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2727 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2729 // the table will have sent the results of the delete col
2730 // operation to this view object as a grid table message
2732 if ( m_numCols
> 0 )
2733 SetEditControlValue();
2735 HideCellEditControl();
2738 if ( !GetBatchCount() ) Refresh();
2750 // ----- event handlers
2753 // Generate a grid event based on a mouse event and
2754 // return the result of ProcessEvent()
2756 bool wxGrid
::SendEvent( const wxEventType type
,
2758 wxMouseEvent
& mouseEv
)
2760 if ( type
== EVT_GRID_ROW_SIZE
||
2761 type
== EVT_GRID_COL_SIZE
)
2763 int rowOrCol
= (row
== -1 ? col
: row
);
2765 wxGridSizeEvent
gridEvt( GetId(),
2769 mouseEv
.GetX(), mouseEv
.GetY(),
2770 mouseEv
.ControlDown(),
2771 mouseEv
.ShiftDown(),
2773 mouseEv
.MetaDown() );
2775 return GetEventHandler()->ProcessEvent(gridEvt
);
2777 else if ( type
== EVT_GRID_RANGE_SELECT
)
2779 wxGridRangeSelectEvent
gridEvt( GetId(),
2783 m_selectedBottomRight
,
2784 mouseEv
.ControlDown(),
2785 mouseEv
.ShiftDown(),
2787 mouseEv
.MetaDown() );
2789 return GetEventHandler()->ProcessEvent(gridEvt
);
2793 wxGridEvent
gridEvt( GetId(),
2797 mouseEv
.GetX(), mouseEv
.GetY(),
2798 mouseEv
.ControlDown(),
2799 mouseEv
.ShiftDown(),
2801 mouseEv
.MetaDown() );
2803 return GetEventHandler()->ProcessEvent(gridEvt
);
2808 // Generate a grid event of specified type and return the result
2809 // of ProcessEvent().
2811 bool wxGrid
::SendEvent( const wxEventType type
,
2814 if ( type
== EVT_GRID_ROW_SIZE
||
2815 type
== EVT_GRID_COL_SIZE
)
2817 int rowOrCol
= (row
== -1 ? col
: row
);
2819 wxGridSizeEvent
gridEvt( GetId(),
2824 return GetEventHandler()->ProcessEvent(gridEvt
);
2828 wxGridEvent
gridEvt( GetId(),
2833 return GetEventHandler()->ProcessEvent(gridEvt
);
2838 void wxGrid
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2840 wxPaintDC
dc( this );
2842 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2843 m_numRows
&& m_numCols
)
2845 m_currentCellCoords
.Set(0, 0);
2846 SetEditControlValue();
2847 ShowCellEditControl();
2854 // This is just here to make sure that CalcDimensions gets called when
2855 // the grid view is resized... then the size event is skipped to allow
2856 // the box sizers to handle everything
2858 void wxGrid
::OnSize( wxSizeEvent
& event
)
2865 void wxGrid
::OnKeyDown( wxKeyEvent
& event
)
2867 if ( m_inOnKeyDown
)
2869 // shouldn't be here - we are going round in circles...
2871 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while alread active") );
2874 m_inOnKeyDown
= TRUE
;
2876 // propagate the event up and see if it gets processed
2878 wxWindow
*parent
= GetParent();
2879 wxKeyEvent
keyEvt( event
);
2880 keyEvt
.SetEventObject( parent
);
2882 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2884 // try local handlers
2886 switch ( event
.KeyCode() )
2889 if ( event
.ControlDown() )
2891 MoveCursorUpBlock();
2900 if ( event
.ControlDown() )
2902 MoveCursorDownBlock();
2911 if ( event
.ControlDown() )
2913 MoveCursorLeftBlock();
2922 if ( event
.ControlDown() )
2924 MoveCursorRightBlock();
2933 if ( !IsEditable() )
2944 if ( event
.ControlDown() )
2946 event
.Skip(); // to let the edit control have the return
2955 if ( event
.ControlDown() )
2957 MakeCellVisible( 0, 0 );
2958 SetCurrentCell( 0, 0 );
2967 if ( event
.ControlDown() )
2969 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2970 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2987 // now try the cell edit control
2989 if ( IsCellEditControlEnabled() )
2991 event
.SetEventObject( m_cellEditCtrl
);
2992 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2998 m_inOnKeyDown
= FALSE
;
3002 void wxGrid
::SetCurrentCell( const wxGridCellCoords
& coords
)
3004 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3006 // the event has been intercepted - do nothing
3011 m_currentCellCoords
!= wxGridNoCellCoords
)
3013 HideCellEditControl();
3014 SaveEditControlValue();
3017 m_currentCellCoords
= coords
;
3019 SetEditControlValue();
3023 ShowCellEditControl();
3025 if ( IsSelection() )
3027 wxRect
r( SelectionToDeviceRect() );
3029 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3036 // ------ functions to get/send data (see also public functions)
3039 bool wxGrid
::GetModelValues()
3043 // all we need to do is repaint the grid
3045 m_gridWin
->Refresh();
3053 bool wxGrid
::SetModelValues()
3059 for ( row
= 0; row
< m_numRows
; row
++ )
3061 for ( col
= 0; col
< m_numCols
; col
++ )
3063 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3075 // Note - this function only draws cells that are in the list of
3076 // exposed cells (usually set from the update region by
3077 // CalcExposedCells)
3079 void wxGrid
::DrawGridCellArea( wxDC
& dc
)
3081 if ( !m_numRows
|| !m_numCols
) return;
3084 size_t numCells
= m_cellsExposed
.GetCount();
3086 for ( i
= 0; i
< numCells
; i
++ )
3088 DrawCell( dc
, m_cellsExposed
[i
] );
3093 void wxGrid
::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3095 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3096 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3098 #if !WXGRID_DRAW_LINES
3099 if ( m_gridLinesEnabled
)
3100 DrawCellBorder( dc
, coords
);
3103 DrawCellBackground( dc
, coords
);
3105 // TODO: separate functions here for different kinds of cells ?
3108 DrawCellValue( dc
, coords
);
3112 void wxGrid
::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3114 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3115 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3117 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3118 int row
= coords
.GetRow();
3119 int col
= coords
.GetCol();
3121 // right hand border
3123 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3124 m_colRights
[col
], m_rowBottoms
[row
] );
3128 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3129 m_colRights
[col
], m_rowBottoms
[row
] );
3133 void wxGrid
::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
3135 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3136 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3138 int row
= coords
.GetRow();
3139 int col
= coords
.GetCol();
3141 dc
.SetBackgroundMode( wxSOLID
);
3143 if ( IsInSelection( coords
) )
3145 // TODO: improve this
3147 dc
.SetBrush( *wxBLACK_BRUSH
);
3151 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
3154 dc
.SetPen( *wxTRANSPARENT_PEN
);
3156 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
3157 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
3159 m_rowHeights
[row
]-1 );
3163 void wxGrid
::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
3165 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3166 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3168 int row
= coords
.GetRow();
3169 int col
= coords
.GetCol();
3171 dc
.SetBackgroundMode( wxTRANSPARENT
);
3173 if ( IsInSelection( row
, col
) )
3175 // TODO: improve this
3177 dc
.SetTextBackground( wxColour(0, 0, 0) );
3178 dc
.SetTextForeground( wxColour(255, 255, 255) );
3182 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
3183 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
3185 dc
.SetFont( GetCellFont(row
, col
) );
3188 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
3191 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3192 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3193 rect
.SetWidth( m_colWidths
[col
] - 4 );
3194 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3196 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
3201 // TODO: remove this ???
3202 // This is used to redraw all grid lines e.g. when the grid line colour
3205 void wxGrid
::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3207 if ( !m_gridLinesEnabled
||
3209 !m_numCols
) return;
3211 int top
, bottom
, left
, right
;
3215 m_gridWin
->GetClientSize(&cw
, &ch
);
3217 // virtual coords of visible area
3219 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3220 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3224 reg
.GetBox(x
, y
, w
, h
);
3225 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3226 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3229 // avoid drawing grid lines past the last row and col
3231 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3232 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3234 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3236 // horizontal grid lines
3239 for ( i
= 0; i
< m_numRows
; i
++ )
3241 if ( m_rowBottoms
[i
] > bottom
)
3245 else if ( m_rowBottoms
[i
] >= top
)
3247 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
3252 // vertical grid lines
3254 for ( i
= 0; i
< m_numCols
; i
++ )
3256 if ( m_colRights
[i
] > right
)
3260 else if ( m_colRights
[i
] >= left
)
3262 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
3268 void wxGrid
::DrawRowLabels( wxDC
& dc
)
3270 if ( !m_numRows
|| !m_numCols
) return;
3273 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3275 for ( i
= 0; i
< numLabels
; i
++ )
3277 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3282 void wxGrid
::DrawRowLabel( wxDC
& dc
, int row
)
3284 if ( m_rowHeights
[row
] <= 0 ) return;
3286 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3288 dc
.SetPen( *wxBLACK_PEN
);
3289 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3290 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3292 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3293 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3295 dc
.SetPen( *wxWHITE_PEN
);
3296 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
3297 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
3299 dc
.SetBackgroundMode( wxTRANSPARENT
);
3300 dc
.SetTextForeground( GetLabelTextColour() );
3301 dc
.SetFont( GetLabelFont() );
3304 GetRowLabelAlignment( &hAlign
, &vAlign
);
3308 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3309 rect
.SetWidth( m_rowLabelWidth
- 4 );
3310 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3311 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
3315 void wxGrid
::DrawColLabels( wxDC
& dc
)
3317 if ( !m_numRows
|| !m_numCols
) return;
3320 size_t numLabels
= m_colLabelsExposed
.GetCount();
3322 for ( i
= 0; i
< numLabels
; i
++ )
3324 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
3329 void wxGrid
::DrawColLabel( wxDC
& dc
, int col
)
3331 if ( m_colWidths
[col
] <= 0 ) return;
3333 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
3335 dc
.SetPen( *wxBLACK_PEN
);
3336 dc
.DrawLine( m_colRights
[col
]-1, 0,
3337 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3339 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
3340 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3342 dc
.SetPen( *wxWHITE_PEN
);
3343 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
3344 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
3346 dc
.SetBackgroundMode( wxTRANSPARENT
);
3347 dc
.SetTextForeground( GetLabelTextColour() );
3348 dc
.SetFont( GetLabelFont() );
3350 dc
.SetBackgroundMode( wxTRANSPARENT
);
3351 dc
.SetTextForeground( GetLabelTextColour() );
3352 dc
.SetFont( GetLabelFont() );
3355 GetColLabelAlignment( &hAlign
, &vAlign
);
3358 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3360 rect
.SetWidth( m_colWidths
[col
] - 4 );
3361 rect
.SetHeight( m_colLabelHeight
- 4 );
3362 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
3366 void wxGrid
::DrawTextRectangle( wxDC
& dc
,
3367 const wxString
& value
,
3372 long textWidth
, textHeight
;
3373 long lineWidth
, lineHeight
;
3374 wxArrayString lines
;
3376 dc
.SetClippingRegion( rect
);
3377 StringToLines( value
, lines
);
3378 if ( lines
.GetCount() )
3380 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
3381 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
3384 switch ( horizAlign
)
3387 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
3391 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
3400 switch ( vertAlign
)
3403 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
3407 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
3416 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
3418 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
3423 dc
.DestroyClippingRegion();
3427 // Split multi line text up into an array of strings. Any existing
3428 // contents of the string array are preserved.
3430 void wxGrid
::StringToLines( const wxString
& value
, wxArrayString
& lines
)
3434 wxString eol
= wxTextFile
::GetEOL( wxTextFileType_Unix
);
3435 wxString tVal
= wxTextFile
::Translate( value
, wxTextFileType_Unix
);
3437 while ( startPos
< (int)tVal
.Length() )
3439 pos
= tVal
.Mid(startPos
).Find( eol
);
3444 else if ( pos
== 0 )
3446 lines
.Add( wxEmptyString
);
3450 lines
.Add( value
.Mid(startPos
, pos
) );
3454 if ( startPos
< (int)value
.Length() )
3456 lines
.Add( value
.Mid( startPos
) );
3461 void wxGrid
::GetTextBoxSize( wxDC
& dc
,
3462 wxArrayString
& lines
,
3463 long *width
, long *height
)
3470 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3472 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3473 w
= wxMax( w
, lineW
);
3483 // ------ Edit control functions
3487 void wxGrid
::EnableEditing( bool edit
)
3489 // TODO: improve this ?
3491 if ( edit
!= m_editable
)
3495 // TODO: extend this for other edit control types
3497 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3499 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
3505 #if 0 // disabled for the moment - the cell control is always active
3506 void wxGrid
::EnableCellEditControl( bool enable
)
3508 if ( m_cellEditCtrl
&&
3509 enable
!= m_cellEditCtrlEnabled
)
3511 m_cellEditCtrlEnabled
= enable
;
3513 if ( m_cellEditCtrlEnabled
)
3515 SetEditControlValue();
3516 ShowCellEditControl();
3520 HideCellEditControl();
3521 SaveEditControlValue();
3528 void wxGrid
::ShowCellEditControl()
3532 if ( IsCellEditControlEnabled() )
3534 if ( !IsVisible( m_currentCellCoords
) )
3540 rect
= CellToRect( m_currentCellCoords
);
3542 // convert to scrolled coords
3544 int left
, top
, right
, bottom
;
3545 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3546 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3549 m_gridWin
->GetClientSize( &cw
, &ch
);
3551 // Make the edit control large enough to allow for internal margins
3552 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3555 #if defined(__WXMOTIF__)
3556 if ( m_currentCellCoords
.GetRow() == 0 ||
3557 m_currentCellCoords
.GetCol() == 0 )
3566 if ( m_currentCellCoords
.GetRow() == 0 ||
3567 m_currentCellCoords
.GetCol() == 0 )
3577 #if defined(__WXGTK__)
3580 if (left
!= 0) left_diff
++;
3581 if (top
!= 0) top_diff
++;
3582 rect
.SetLeft( left
+ left_diff
);
3583 rect
.SetTop( top
+ top_diff
);
3584 rect
.SetRight( rect
.GetRight() - left_diff
);
3585 rect
.SetBottom( rect
.GetBottom() - top_diff
);
3587 rect
.SetLeft( wxMax(0, left
- extra
) );
3588 rect
.SetTop( wxMax(0, top
- extra
) );
3589 rect
.SetRight( rect
.GetRight() + 2*extra
);
3590 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3593 m_cellEditCtrl
->SetSize( rect
);
3594 m_cellEditCtrl
->Show( TRUE
);
3596 switch ( m_editCtrlType
)
3598 case wxGRID_TEXTCTRL
:
3599 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3602 case wxGRID_CHECKBOX
:
3603 // TODO: anything ???
3608 // TODO: anything ???
3612 case wxGRID_COMBOBOX
:
3613 // TODO: anything ???
3618 m_cellEditCtrl
->SetFocus();
3624 void wxGrid
::HideCellEditControl()
3626 if ( IsCellEditControlEnabled() )
3628 m_cellEditCtrl
->Show( FALSE
);
3633 void wxGrid
::SetEditControlValue( const wxString
& value
)
3639 s
= GetCellValue(m_currentCellCoords
);
3643 if ( IsCellEditControlEnabled() )
3645 switch ( m_editCtrlType
)
3647 case wxGRID_TEXTCTRL
:
3648 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3651 case wxGRID_CHECKBOX
:
3652 // TODO: implement this
3657 // TODO: implement this
3661 case wxGRID_COMBOBOX
:
3662 // TODO: implement this
3671 void wxGrid
::SaveEditControlValue()
3675 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3677 if ( IsCellEditControlEnabled() )
3679 ctrl
= m_cellEditCtrl
;
3686 bool valueChanged
= FALSE
;
3688 switch ( m_editCtrlType
)
3690 case wxGRID_TEXTCTRL
:
3691 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3692 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3693 SetCellValue( m_currentCellCoords
,
3694 ((wxTextCtrl
*) ctrl
)->GetValue() );
3697 case wxGRID_CHECKBOX
:
3698 // TODO: implement this
3703 // TODO: implement this
3707 case wxGRID_COMBOBOX
:
3708 // TODO: implement this
3715 SendEvent( EVT_GRID_CELL_CHANGE
,
3716 m_currentCellCoords
.GetRow(),
3717 m_currentCellCoords
.GetCol() );
3724 // ------ Grid location functions
3725 // Note that all of these functions work with the logical coordinates of
3726 // grid cells and labels so you will need to convert from device
3727 // coordinates for mouse events etc.
3730 void wxGrid
::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3732 int row
= YToRow(y
);
3733 int col
= XToCol(x
);
3735 if ( row
== -1 || col
== -1 )
3737 coords
= wxGridNoCellCoords
;
3741 coords
.Set( row
, col
);
3746 int wxGrid
::YToRow( int y
)
3750 for ( i
= 0; i
< m_numRows
; i
++ )
3752 if ( y
< m_rowBottoms
[i
] ) return i
;
3759 int wxGrid
::XToCol( int x
)
3763 for ( i
= 0; i
< m_numCols
; i
++ )
3765 if ( x
< m_colRights
[i
] ) return i
;
3772 // return the row number that that the y coord is near the edge of, or
3773 // -1 if not near an edge
3775 int wxGrid
::YToEdgeOfRow( int y
)
3779 for ( i
= 0; i
< m_numRows
; i
++ )
3781 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3783 d
= abs( y
- m_rowBottoms
[i
] );
3785 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3794 // return the col number that that the x coord is near the edge of, or
3795 // -1 if not near an edge
3797 int wxGrid
::XToEdgeOfCol( int x
)
3801 for ( i
= 0; i
< m_numCols
; i
++ )
3803 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3805 d
= abs( x
- m_colRights
[i
] );
3807 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3816 wxRect wxGrid
::CellToRect( int row
, int col
)
3818 wxRect
rect( -1, -1, -1, -1 );
3820 if ( row
>= 0 && row
< m_numRows
&&
3821 col
>= 0 && col
< m_numCols
)
3823 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3824 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3825 rect
.width
= m_colWidths
[col
];
3826 rect
.height
= m_rowHeights
[ row
];
3833 bool wxGrid
::IsVisible( int row
, int col
, bool wholeCellVisible
)
3835 // get the cell rectangle in logical coords
3837 wxRect
r( CellToRect( row
, col
) );
3839 // convert to device coords
3841 int left
, top
, right
, bottom
;
3842 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3843 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3845 // check against the client area of the grid window
3848 m_gridWin
->GetClientSize( &cw
, &ch
);
3850 if ( wholeCellVisible
)
3852 // is the cell wholly visible ?
3854 return ( left
>= 0 && right
<= cw
&&
3855 top
>= 0 && bottom
<= ch
);
3859 // is the cell partly visible ?
3861 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3862 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3867 // make the specified cell location visible by doing a minimal amount
3870 void wxGrid
::MakeCellVisible( int row
, int col
)
3873 int xpos
= -1, ypos
= -1;
3875 if ( row
>= 0 && row
< m_numRows
&&
3876 col
>= 0 && col
< m_numCols
)
3878 // get the cell rectangle in logical coords
3880 wxRect
r( CellToRect( row
, col
) );
3882 // convert to device coords
3884 int left
, top
, right
, bottom
;
3885 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3886 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3889 m_gridWin
->GetClientSize( &cw
, &ch
);
3895 else if ( bottom
> ch
)
3897 int h
= r
.GetHeight();
3899 for ( i
= row
-1; i
>= 0; i
-- )
3901 if ( h
+ m_rowHeights
[i
] > ch
) break;
3903 h
+= m_rowHeights
[i
];
3904 ypos
-= m_rowHeights
[i
];
3907 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
3908 // have rounding errors (this is important, because if we do, we
3909 // might not scroll at all and some cells won't be redrawn)
3910 ypos
+= GRID_SCROLL_LINE
/ 2;
3917 else if ( right
> cw
)
3919 int w
= r
.GetWidth();
3921 for ( i
= col
-1; i
>= 0; i
-- )
3923 if ( w
+ m_colWidths
[i
] > cw
) break;
3925 w
+= m_colWidths
[i
];
3926 xpos
-= m_colWidths
[i
];
3929 // see comment for ypos above
3930 xpos
+= GRID_SCROLL_LINE
/ 2;
3933 if ( xpos
!= -1 || ypos
!= -1 )
3935 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
3936 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
3937 Scroll( xpos
, ypos
);
3945 // ------ Grid cursor movement functions
3948 bool wxGrid
::MoveCursorUp()
3950 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3951 m_currentCellCoords
.GetRow() > 0 )
3953 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3954 m_currentCellCoords
.GetCol() );
3956 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3957 m_currentCellCoords
.GetCol() );
3966 bool wxGrid
::MoveCursorDown()
3968 // TODO: allow for scrolling
3970 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3971 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3973 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3974 m_currentCellCoords
.GetCol() );
3976 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3977 m_currentCellCoords
.GetCol() );
3986 bool wxGrid
::MoveCursorLeft()
3988 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3989 m_currentCellCoords
.GetCol() > 0 )
3991 MakeCellVisible( m_currentCellCoords
.GetRow(),
3992 m_currentCellCoords
.GetCol() - 1 );
3994 SetCurrentCell( m_currentCellCoords
.GetRow(),
3995 m_currentCellCoords
.GetCol() - 1 );
4004 bool wxGrid
::MoveCursorRight()
4006 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4007 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4009 MakeCellVisible( m_currentCellCoords
.GetRow(),
4010 m_currentCellCoords
.GetCol() + 1 );
4012 SetCurrentCell( m_currentCellCoords
.GetRow(),
4013 m_currentCellCoords
.GetCol() + 1 );
4022 bool wxGrid
::MovePageUp()
4024 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4026 int row
= m_currentCellCoords
.GetRow();
4030 m_gridWin
->GetClientSize( &cw
, &ch
);
4032 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4033 int newRow
= YToRow( y
- ch
+ 1 );
4038 else if ( newRow
== row
)
4043 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4044 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4052 bool wxGrid
::MovePageDown()
4054 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4056 int row
= m_currentCellCoords
.GetRow();
4057 if ( row
< m_numRows
)
4060 m_gridWin
->GetClientSize( &cw
, &ch
);
4062 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4063 int newRow
= YToRow( y
+ ch
);
4066 newRow
= m_numRows
- 1;
4068 else if ( newRow
== row
)
4073 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4074 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4082 bool wxGrid
::MoveCursorUpBlock()
4085 m_currentCellCoords
!= wxGridNoCellCoords
&&
4086 m_currentCellCoords
.GetRow() > 0 )
4088 int row
= m_currentCellCoords
.GetRow();
4089 int col
= m_currentCellCoords
.GetCol();
4091 if ( m_table
->IsEmptyCell(row
, col
) )
4093 // starting in an empty cell: find the next block of
4099 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4102 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4104 // starting at the top of a block: find the next block
4110 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4115 // starting within a block: find the top of the block
4120 if ( m_table
->IsEmptyCell(row
, col
) )
4128 MakeCellVisible( row
, col
);
4129 SetCurrentCell( row
, col
);
4137 bool wxGrid
::MoveCursorDownBlock()
4140 m_currentCellCoords
!= wxGridNoCellCoords
&&
4141 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4143 int row
= m_currentCellCoords
.GetRow();
4144 int col
= m_currentCellCoords
.GetCol();
4146 if ( m_table
->IsEmptyCell(row
, col
) )
4148 // starting in an empty cell: find the next block of
4151 while ( row
< m_numRows
-1 )
4154 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4157 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4159 // starting at the bottom of a block: find the next block
4162 while ( row
< m_numRows
-1 )
4165 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4170 // starting within a block: find the bottom of the block
4172 while ( row
< m_numRows
-1 )
4175 if ( m_table
->IsEmptyCell(row
, col
) )
4183 MakeCellVisible( row
, col
);
4184 SetCurrentCell( row
, col
);
4192 bool wxGrid
::MoveCursorLeftBlock()
4195 m_currentCellCoords
!= wxGridNoCellCoords
&&
4196 m_currentCellCoords
.GetCol() > 0 )
4198 int row
= m_currentCellCoords
.GetRow();
4199 int col
= m_currentCellCoords
.GetCol();
4201 if ( m_table
->IsEmptyCell(row
, col
) )
4203 // starting in an empty cell: find the next block of
4209 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4212 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4214 // starting at the left of a block: find the next block
4220 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4225 // starting within a block: find the left of the block
4230 if ( m_table
->IsEmptyCell(row
, col
) )
4238 MakeCellVisible( row
, col
);
4239 SetCurrentCell( row
, col
);
4247 bool wxGrid
::MoveCursorRightBlock()
4250 m_currentCellCoords
!= wxGridNoCellCoords
&&
4251 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4253 int row
= m_currentCellCoords
.GetRow();
4254 int col
= m_currentCellCoords
.GetCol();
4256 if ( m_table
->IsEmptyCell(row
, col
) )
4258 // starting in an empty cell: find the next block of
4261 while ( col
< m_numCols
-1 )
4264 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4267 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4269 // starting at the right of a block: find the next block
4272 while ( col
< m_numCols
-1 )
4275 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4280 // starting within a block: find the right of the block
4282 while ( col
< m_numCols
-1 )
4285 if ( m_table
->IsEmptyCell(row
, col
) )
4293 MakeCellVisible( row
, col
);
4294 SetCurrentCell( row
, col
);
4305 // ------ Label values and formatting
4308 void wxGrid
::GetRowLabelAlignment( int *horiz
, int *vert
)
4310 *horiz
= m_rowLabelHorizAlign
;
4311 *vert
= m_rowLabelVertAlign
;
4314 void wxGrid
::GetColLabelAlignment( int *horiz
, int *vert
)
4316 *horiz
= m_colLabelHorizAlign
;
4317 *vert
= m_colLabelVertAlign
;
4320 wxString wxGrid
::GetRowLabelValue( int row
)
4324 return m_table
->GetRowLabelValue( row
);
4334 wxString wxGrid
::GetColLabelValue( int col
)
4338 return m_table
->GetColLabelValue( col
);
4349 void wxGrid
::SetRowLabelSize( int width
)
4351 width
= wxMax( width
, 0 );
4352 if ( width
!= m_rowLabelWidth
)
4356 m_rowLabelWin
->Show( FALSE
);
4357 m_cornerLabelWin
->Show( FALSE
);
4359 else if ( m_rowLabelWidth
== 0 )
4361 m_rowLabelWin
->Show( TRUE
);
4362 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4365 m_rowLabelWidth
= width
;
4372 void wxGrid
::SetColLabelSize( int height
)
4374 height
= wxMax( height
, 0 );
4375 if ( height
!= m_colLabelHeight
)
4379 m_colLabelWin
->Show( FALSE
);
4380 m_cornerLabelWin
->Show( FALSE
);
4382 else if ( m_colLabelHeight
== 0 )
4384 m_colLabelWin
->Show( TRUE
);
4385 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4388 m_colLabelHeight
= height
;
4395 void wxGrid
::SetLabelBackgroundColour( const wxColour
& colour
)
4397 if ( m_labelBackgroundColour
!= colour
)
4399 m_labelBackgroundColour
= colour
;
4400 m_rowLabelWin
->SetBackgroundColour( colour
);
4401 m_colLabelWin
->SetBackgroundColour( colour
);
4402 m_cornerLabelWin
->SetBackgroundColour( colour
);
4404 if ( !GetBatchCount() )
4406 m_rowLabelWin
->Refresh();
4407 m_colLabelWin
->Refresh();
4408 m_cornerLabelWin
->Refresh();
4413 void wxGrid
::SetLabelTextColour( const wxColour
& colour
)
4415 if ( m_labelTextColour
!= colour
)
4417 m_labelTextColour
= colour
;
4418 if ( !GetBatchCount() )
4420 m_rowLabelWin
->Refresh();
4421 m_colLabelWin
->Refresh();
4426 void wxGrid
::SetLabelFont( const wxFont
& font
)
4429 if ( !GetBatchCount() )
4431 m_rowLabelWin
->Refresh();
4432 m_colLabelWin
->Refresh();
4436 void wxGrid
::SetRowLabelAlignment( int horiz
, int vert
)
4438 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4440 m_rowLabelHorizAlign
= horiz
;
4443 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4445 m_rowLabelVertAlign
= vert
;
4448 if ( !GetBatchCount() )
4450 m_rowLabelWin
->Refresh();
4454 void wxGrid
::SetColLabelAlignment( int horiz
, int vert
)
4456 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4458 m_colLabelHorizAlign
= horiz
;
4461 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4463 m_colLabelVertAlign
= vert
;
4466 if ( !GetBatchCount() )
4468 m_colLabelWin
->Refresh();
4472 void wxGrid
::SetRowLabelValue( int row
, const wxString
& s
)
4476 m_table
->SetRowLabelValue( row
, s
);
4477 if ( !GetBatchCount() )
4479 wxRect rect
= CellToRect( row
, 0);
4480 if ( rect
.height
> 0 )
4482 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
4484 rect
.width
= m_rowLabelWidth
;
4485 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4491 void wxGrid
::SetColLabelValue( int col
, const wxString
& s
)
4495 m_table
->SetColLabelValue( col
, s
);
4496 if ( !GetBatchCount() )
4498 wxRect rect
= CellToRect( 0, col
);
4499 if ( rect
.width
> 0 )
4501 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
4503 rect
.height
= m_colLabelHeight
;
4504 m_colLabelWin
->Refresh( TRUE
, &rect
);
4510 void wxGrid
::SetGridLineColour( const wxColour
& colour
)
4512 if ( m_gridLineColour
!= colour
)
4514 m_gridLineColour
= colour
;
4516 wxClientDC
dc( m_gridWin
);
4518 DrawAllGridLines( dc
, wxRegion() );
4522 void wxGrid
::EnableGridLines( bool enable
)
4524 if ( enable
!= m_gridLinesEnabled
)
4526 m_gridLinesEnabled
= enable
;
4528 if ( !GetBatchCount() )
4532 wxClientDC
dc( m_gridWin
);
4534 DrawAllGridLines( dc
, wxRegion() );
4538 m_gridWin
->Refresh();
4545 int wxGrid
::GetDefaultRowSize()
4547 return m_defaultRowHeight
;
4550 int wxGrid
::GetRowSize( int row
)
4552 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
4554 return m_rowHeights
[row
];
4557 int wxGrid
::GetDefaultColSize()
4559 return m_defaultColWidth
;
4562 int wxGrid
::GetColSize( int col
)
4564 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
4566 return m_colWidths
[col
];
4569 wxColour wxGrid
::GetDefaultCellBackgroundColour()
4571 return m_gridWin
->GetBackgroundColour();
4574 // TODO VZ: this must be optimized to allow only retrieveing attr once!
4576 wxColour wxGrid
::GetCellBackgroundColour(int row
, int col
)
4578 wxGridCellAttr
*attr
= m_table ? m_table
->GetAttr(row
, col
) : NULL
;
4581 if ( attr
&& attr
->HasBackgroundColour() )
4582 colour
= attr
->GetBackgroundColour();
4584 colour
= GetDefaultCellBackgroundColour();
4591 wxColour wxGrid
::GetDefaultCellTextColour()
4593 return m_gridWin
->GetForegroundColour();
4596 wxColour wxGrid
::GetCellTextColour( int row
, int col
)
4598 wxGridCellAttr
*attr
= m_table ? m_table
->GetAttr(row
, col
) : NULL
;
4601 if ( attr
&& attr
->HasTextColour() )
4602 colour
= attr
->GetTextColour();
4604 colour
= GetDefaultCellTextColour();
4612 wxFont wxGrid
::GetDefaultCellFont()
4614 return m_defaultCellFont
;
4617 wxFont wxGrid
::GetCellFont( int row
, int col
)
4619 wxGridCellAttr
*attr
= m_table ? m_table
->GetAttr(row
, col
) : NULL
;
4622 if ( attr
&& attr
->HasFont() )
4623 font
= attr
->GetFont();
4625 font
= GetDefaultCellFont();
4632 void wxGrid
::GetDefaultCellAlignment( int *horiz
, int *vert
)
4635 *horiz
= m_defaultCellHAlign
;
4637 *vert
= m_defaultCellVAlign
;
4640 void wxGrid
::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
4642 wxGridCellAttr
*attr
= m_table ? m_table
->GetAttr(row
, col
) : NULL
;
4644 if ( attr
&& attr
->HasAlignment() )
4645 attr
->GetAlignment(horiz
, vert
);
4647 GetDefaultCellAlignment(horiz
, vert
);
4652 void wxGrid
::SetDefaultRowSize( int height
, bool resizeExistingRows
)
4654 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
4656 if ( resizeExistingRows
)
4660 for ( row
= 0; row
< m_numRows
; row
++ )
4662 m_rowHeights
[row
] = m_defaultRowHeight
;
4663 bottom
+= m_defaultRowHeight
;
4664 m_rowBottoms
[row
] = bottom
;
4670 void wxGrid
::SetRowSize( int row
, int height
)
4672 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
4676 int h
= wxMax( 0, height
);
4677 int diff
= h
- m_rowHeights
[row
];
4679 m_rowHeights
[row
] = h
;
4680 for ( i
= row
; i
< m_numRows
; i
++ )
4682 m_rowBottoms
[i
] += diff
;
4687 void wxGrid
::SetDefaultColSize( int width
, bool resizeExistingCols
)
4689 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4691 if ( resizeExistingCols
)
4695 for ( col
= 0; col
< m_numCols
; col
++ )
4697 m_colWidths
[col
] = m_defaultColWidth
;
4698 right
+= m_defaultColWidth
;
4699 m_colRights
[col
] = right
;
4705 void wxGrid
::SetColSize( int col
, int width
)
4707 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
4711 int w
= wxMax( 0, width
);
4712 int diff
= w
- m_colWidths
[col
];
4713 m_colWidths
[col
] = w
;
4715 for ( i
= col
; i
< m_numCols
; i
++ )
4717 m_colRights
[i
] += diff
;
4722 void wxGrid
::SetDefaultCellBackgroundColour( const wxColour
& col
)
4724 m_gridWin
->SetBackgroundColour(col
);
4727 void wxGrid
::SetDefaultCellTextColour( const wxColour
& col
)
4729 m_gridWin
->SetForegroundColour(col
);
4732 void wxGrid
::SetDefaultCellAlignment( int horiz
, int vert
)
4734 m_defaultCellHAlign
= horiz
;
4735 m_defaultCellVAlign
= vert
;
4738 bool wxGrid
::CanHaveAttributes()
4745 if ( !m_table
->GetAttrProvider() )
4747 // use the default attr provider by default
4748 // (another choice would be to just return FALSE thus forcing the user
4750 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
4756 void wxGrid
::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
4758 if ( CanHaveAttributes() )
4760 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4761 attr
->SetBackgroundColour(colour
);
4763 m_table
->SetAttr(attr
, row
, col
);
4767 void wxGrid
::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
4769 if ( CanHaveAttributes() )
4771 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4772 attr
->SetTextColour(colour
);
4774 m_table
->SetAttr(attr
, row
, col
);
4778 void wxGrid
::SetDefaultCellFont( const wxFont
& font
)
4780 m_defaultCellFont
= font
;
4783 void wxGrid
::SetCellFont( int row
, int col
, const wxFont
& font
)
4785 if ( CanHaveAttributes() )
4787 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4788 attr
->SetFont(font
);
4790 m_table
->SetAttr(attr
, row
, col
);
4794 void wxGrid
::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
4796 if ( CanHaveAttributes() )
4798 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4799 attr
->SetAlignment(horiz
, vert
);
4801 m_table
->SetAttr(attr
, row
, col
);
4808 // ------ cell value accessor functions
4811 void wxGrid
::SetCellValue( int row
, int col
, const wxString
& s
)
4815 m_table
->SetValue( row
, col
, s
.c_str() );
4816 if ( !GetBatchCount() )
4818 wxClientDC
dc( m_gridWin
);
4820 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4823 #if 0 // TODO: edit in place
4825 if ( m_currentCellCoords
.GetRow() == row
&&
4826 m_currentCellCoords
.GetCol() == col
)
4828 SetEditControlValue( s
);
4837 // ------ Block, row and col selection
4840 void wxGrid
::SelectRow( int row
, bool addToSelected
)
4844 if ( IsSelection() && addToSelected
)
4847 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4850 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4851 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4852 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4853 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4857 need_refresh
[0] = TRUE
;
4858 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
4859 wxGridCellCoords ( oldTop
- 1,
4861 m_selectedTopLeft
.SetRow( row
);
4866 need_refresh
[1] = TRUE
;
4867 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
4868 wxGridCellCoords ( oldBottom
,
4871 m_selectedTopLeft
.SetCol( 0 );
4874 if ( oldBottom
< row
)
4876 need_refresh
[2] = TRUE
;
4877 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
4878 wxGridCellCoords ( row
,
4880 m_selectedBottomRight
.SetRow( row
);
4883 if ( oldRight
< m_numCols
- 1 )
4885 need_refresh
[3] = TRUE
;
4886 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4888 wxGridCellCoords ( oldBottom
,
4890 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4893 for (i
= 0; i
< 4; i
++ )
4894 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4895 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4899 r
= SelectionToDeviceRect();
4901 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4903 m_selectedTopLeft
.Set( row
, 0 );
4904 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4905 r
= SelectionToDeviceRect();
4906 m_gridWin
->Refresh( FALSE
, &r
);
4909 wxGridRangeSelectEvent
gridEvt( GetId(),
4910 EVT_GRID_RANGE_SELECT
,
4913 m_selectedBottomRight
);
4915 GetEventHandler()->ProcessEvent(gridEvt
);
4919 void wxGrid
::SelectCol( int col
, bool addToSelected
)
4921 if ( IsSelection() && addToSelected
)
4924 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4927 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4928 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4929 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4930 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4932 if ( oldLeft
> col
)
4934 need_refresh
[0] = TRUE
;
4935 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
4936 wxGridCellCoords ( m_numRows
- 1,
4938 m_selectedTopLeft
.SetCol( col
);
4943 need_refresh
[1] = TRUE
;
4944 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
4945 wxGridCellCoords ( oldTop
- 1,
4947 m_selectedTopLeft
.SetRow( 0 );
4950 if ( oldRight
< col
)
4952 need_refresh
[2] = TRUE
;
4953 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
4954 wxGridCellCoords ( m_numRows
- 1,
4956 m_selectedBottomRight
.SetCol( col
);
4959 if ( oldBottom
< m_numRows
- 1 )
4961 need_refresh
[3] = TRUE
;
4962 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
4964 wxGridCellCoords ( m_numRows
- 1,
4966 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4969 for (i
= 0; i
< 4; i
++ )
4970 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4971 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4977 r
= SelectionToDeviceRect();
4979 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4981 m_selectedTopLeft
.Set( 0, col
);
4982 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4983 r
= SelectionToDeviceRect();
4984 m_gridWin
->Refresh( FALSE
, &r
);
4987 wxGridRangeSelectEvent
gridEvt( GetId(),
4988 EVT_GRID_RANGE_SELECT
,
4991 m_selectedBottomRight
);
4993 GetEventHandler()->ProcessEvent(gridEvt
);
4997 void wxGrid
::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5000 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5002 if ( topRow
> bottomRow
)
5009 if ( leftCol
> rightCol
)
5016 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5017 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5019 if ( m_selectedTopLeft
!= updateTopLeft
||
5020 m_selectedBottomRight
!= updateBottomRight
)
5022 // Compute two optimal update rectangles:
5023 // Either one rectangle is a real subset of the
5024 // other, or they are (almost) disjoint!
5026 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5029 // Store intermediate values
5030 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5031 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5032 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5033 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5035 // Determine the outer/inner coordinates.
5036 if (oldLeft
> leftCol
)
5042 if (oldTop
> topRow
)
5048 if (oldRight
< rightCol
)
5051 oldRight
= rightCol
;
5054 if (oldBottom
< bottomRow
)
5057 oldBottom
= bottomRow
;
5061 // Now, either the stuff marked old is the outer
5062 // rectangle or we don't have a situation where one
5063 // is contained in the other.
5065 if ( oldLeft
< leftCol
)
5067 need_refresh
[0] = TRUE
;
5068 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5070 wxGridCellCoords ( oldBottom
,
5074 if ( oldTop
< topRow
)
5076 need_refresh
[1] = TRUE
;
5077 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5079 wxGridCellCoords ( topRow
- 1,
5083 if ( oldRight
> rightCol
)
5085 need_refresh
[2] = TRUE
;
5086 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5088 wxGridCellCoords ( oldBottom
,
5092 if ( oldBottom
> bottomRow
)
5094 need_refresh
[3] = TRUE
;
5095 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5097 wxGridCellCoords ( oldBottom
,
5103 m_selectedTopLeft
= updateTopLeft
;
5104 m_selectedBottomRight
= updateBottomRight
;
5106 // various Refresh() calls
5107 for (i
= 0; i
< 4; i
++ )
5108 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5109 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5112 // only generate an event if the block is not being selected by
5113 // dragging the mouse (in which case the event will be generated in
5114 // the mouse event handler)
5115 if ( !m_isDragging
)
5117 wxGridRangeSelectEvent
gridEvt( GetId(),
5118 EVT_GRID_RANGE_SELECT
,
5121 m_selectedBottomRight
);
5123 GetEventHandler()->ProcessEvent(gridEvt
);
5127 void wxGrid
::SelectAll()
5129 m_selectedTopLeft
.Set( 0, 0 );
5130 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5132 m_gridWin
->Refresh();
5136 void wxGrid
::ClearSelection()
5138 m_selectedTopLeft
= wxGridNoCellCoords
;
5139 m_selectedBottomRight
= wxGridNoCellCoords
;
5143 // This function returns the rectangle that encloses the given block
5144 // in device coords clipped to the client size of the grid window.
5146 wxRect wxGrid
::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5147 const wxGridCellCoords
&bottomRight
)
5149 wxRect
rect( wxGridNoCellRect
);
5152 cellRect
= CellToRect( topLeft
);
5153 if ( cellRect
!= wxGridNoCellRect
)
5159 rect
= wxRect( 0, 0, 0, 0 );
5162 cellRect
= CellToRect( bottomRight
);
5163 if ( cellRect
!= wxGridNoCellRect
)
5169 return wxGridNoCellRect
;
5172 // convert to scrolled coords
5174 int left
, top
, right
, bottom
;
5175 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5176 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5179 m_gridWin
->GetClientSize( &cw
, &ch
);
5181 rect
.SetLeft( wxMax(0, left
) );
5182 rect
.SetTop( wxMax(0, top
) );
5183 rect
.SetRight( wxMin(cw
, right
) );
5184 rect
.SetBottom( wxMin(ch
, bottom
) );
5192 // ------ Grid event classes
5195 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5197 wxGridEvent
::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5198 int row
, int col
, int x
, int y
,
5199 bool control
, bool shift
, bool alt
, bool meta
)
5200 : wxNotifyEvent( type
, id
)
5206 m_control
= control
;
5211 SetEventObject(obj
);
5215 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
5217 wxGridSizeEvent
::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
5218 int rowOrCol
, int x
, int y
,
5219 bool control
, bool shift
, bool alt
, bool meta
)
5220 : wxNotifyEvent( type
, id
)
5222 m_rowOrCol
= rowOrCol
;
5225 m_control
= control
;
5230 SetEventObject(obj
);
5234 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
5236 wxGridRangeSelectEvent
::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
5237 const wxGridCellCoords
& topLeft
,
5238 const wxGridCellCoords
& bottomRight
,
5239 bool control
, bool shift
, bool alt
, bool meta
)
5240 : wxNotifyEvent( type
, id
)
5242 m_topLeft
= topLeft
;
5243 m_bottomRight
= bottomRight
;
5244 m_control
= control
;
5249 SetEventObject(obj
);
5253 #endif // ifndef wxUSE_NEW_GRID