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 wxLogDebug(_T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2118 win
== m_colLabelWin
? _T("colLabelWin")
2119 : win
? _T("rowLabelWin")
2121 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2122 #endif // __WXDEBUG__
2124 if ( mode
== m_cursorMode
)
2129 // by default use the grid itself
2135 m_winCapture
->ReleaseMouse();
2136 m_winCapture
= (wxWindow
*)NULL
;
2139 m_cursorMode
= mode
;
2141 switch ( m_cursorMode
)
2143 case WXGRID_CURSOR_RESIZE_ROW
:
2144 win
->SetCursor( m_rowResizeCursor
);
2147 case WXGRID_CURSOR_RESIZE_COL
:
2148 win
->SetCursor( m_colResizeCursor
);
2152 win
->SetCursor( *wxSTANDARD_CURSOR
);
2155 // we need to capture mouse when resizing
2156 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2157 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2159 if ( captureMouse
&& resize
)
2161 win
->CaptureMouse();
2166 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2169 wxPoint
pos( event
.GetPosition() );
2170 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2172 wxGridCellCoords coords
;
2173 XYToCell( x
, y
, coords
);
2175 if ( event
.Dragging() )
2177 m_isDragging
= TRUE
;
2178 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2180 // Hide the edit control, so it
2181 // won't interfer with drag-shrinking.
2182 if ( IsCellEditControlEnabled() )
2183 HideCellEditControl();
2184 if ( coords
!= wxGridNoCellCoords
)
2186 if ( !IsSelection() )
2188 SelectBlock( coords
, coords
);
2192 SelectBlock( m_currentCellCoords
, coords
);
2196 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2198 int cw
, ch
, left
, dummy
;
2199 m_gridWin
->GetClientSize( &cw
, &ch
);
2200 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2202 wxClientDC
dc( m_gridWin
);
2204 dc
.SetLogicalFunction(wxINVERT
);
2205 if ( m_dragLastPos
>= 0 )
2207 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2209 dc
.DrawLine( left
, y
, left
+cw
, y
);
2212 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2214 int cw
, ch
, dummy
, top
;
2215 m_gridWin
->GetClientSize( &cw
, &ch
);
2216 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2218 wxClientDC
dc( m_gridWin
);
2220 dc
.SetLogicalFunction(wxINVERT
);
2221 if ( m_dragLastPos
>= 0 )
2223 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2225 dc
.DrawLine( x
, top
, x
, top
+ch
);
2232 m_isDragging
= FALSE
;
2234 if ( coords
!= wxGridNoCellCoords
)
2236 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2237 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2240 if ( event
.Entering() || event
.Leaving() )
2242 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2243 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2248 // ------------ Left button pressed
2250 if ( event
.LeftDown() )
2252 if ( event
.ShiftDown() )
2254 SelectBlock( m_currentCellCoords
, coords
);
2256 else if ( XToEdgeOfCol(x
) < 0 &&
2257 YToEdgeOfRow(y
) < 0 )
2259 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2264 MakeCellVisible( coords
);
2265 SetCurrentCell( coords
);
2271 // ------------ Left double click
2273 else if ( event
.LeftDClick() )
2275 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2277 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2285 // ------------ Left button released
2287 else if ( event
.LeftUp() )
2289 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2291 if ( IsSelection() )
2293 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
2296 // Show the edit control, if it has
2297 // been hidden for drag-shrinking.
2298 if ( IsCellEditControlEnabled() )
2299 ShowCellEditControl();
2301 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2303 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2304 DoEndDragResizeRow();
2306 // Note: we are ending the event *after* doing
2307 // default processing in this case
2309 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2311 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2313 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2314 DoEndDragResizeCol();
2316 // Note: we are ending the event *after* doing
2317 // default processing in this case
2319 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2326 // ------------ Right button down
2328 else if ( event
.RightDown() )
2330 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2335 // no default action at the moment
2340 // ------------ Right double click
2342 else if ( event
.RightDClick() )
2344 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2349 // no default action at the moment
2353 // ------------ Moving and no button action
2355 else if ( event
.Moving() && !event
.IsButton() )
2357 int dragRow
= YToEdgeOfRow( y
);
2358 int dragCol
= XToEdgeOfCol( x
);
2360 // Dragging on the corner of a cell to resize in both
2361 // directions is not implemented yet...
2363 if ( dragRow
>= 0 && dragCol
>= 0 )
2365 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2371 m_dragRowOrCol
= dragRow
;
2373 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2375 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
2383 m_dragRowOrCol
= dragCol
;
2385 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2387 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
2393 // Neither on a row or col edge
2395 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2397 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2404 void wxGrid::DoEndDragResizeRow()
2406 if ( m_dragLastPos
>= 0 )
2408 // erase the last line and resize the row
2410 int cw
, ch
, left
, dummy
;
2411 m_gridWin
->GetClientSize( &cw
, &ch
);
2412 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2414 wxClientDC
dc( m_gridWin
);
2416 dc
.SetLogicalFunction( wxINVERT
);
2417 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2418 HideCellEditControl();
2420 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
2421 SetRowSize( m_dragRowOrCol
,
2422 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
2424 if ( !GetBatchCount() )
2426 // Only needed to get the correct rect.y:
2427 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
2429 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
2430 rect
.width
= m_rowLabelWidth
;
2431 rect
.height
= ch
- rect
.y
;
2432 m_rowLabelWin
->Refresh( TRUE
, &rect
);
2434 m_gridWin
->Refresh( FALSE
, &rect
);
2437 ShowCellEditControl();
2442 void wxGrid::DoEndDragResizeCol()
2444 if ( m_dragLastPos
>= 0 )
2446 // erase the last line and resize the col
2448 int cw
, ch
, dummy
, top
;
2449 m_gridWin
->GetClientSize( &cw
, &ch
);
2450 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2452 wxClientDC
dc( m_gridWin
);
2454 dc
.SetLogicalFunction( wxINVERT
);
2455 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2456 HideCellEditControl();
2458 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
2459 SetColSize( m_dragRowOrCol
,
2460 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
2462 if ( !GetBatchCount() )
2464 // Only needed to get the correct rect.x:
2465 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
2467 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
2468 rect
.width
= cw
- rect
.x
;
2469 rect
.height
= m_colLabelHeight
;
2470 m_colLabelWin
->Refresh( TRUE
, &rect
);
2472 m_gridWin
->Refresh( FALSE
, &rect
);
2475 ShowCellEditControl();
2482 // ------ interaction with data model
2484 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
2486 switch ( msg
.GetId() )
2488 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
2489 return GetModelValues();
2491 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
2492 return SetModelValues();
2494 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2495 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2496 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2497 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2498 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2499 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2500 return Redimension( msg
);
2509 // The behaviour of this function depends on the grid table class
2510 // Clear() function. For the default wxGridStringTable class the
2511 // behavious is to replace all cell contents with wxEmptyString but
2512 // not to change the number of rows or cols.
2514 void wxGrid::ClearGrid()
2519 SetEditControlValue();
2520 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2525 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2527 // TODO: something with updateLabels flag
2531 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2537 bool ok
= m_table
->InsertRows( pos
, numRows
);
2539 // the table will have sent the results of the insert row
2540 // operation to this view object as a grid table message
2544 if ( m_numCols
== 0 )
2546 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2548 // TODO: perhaps instead of appending the default number of cols
2549 // we should remember what the last non-zero number of cols was ?
2553 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2555 // if we have just inserted cols into an empty grid the current
2556 // cell will be undefined...
2558 SetCurrentCell( 0, 0 );
2562 if ( !GetBatchCount() ) Refresh();
2565 SetEditControlValue();
2575 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2577 // TODO: something with updateLabels flag
2581 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2585 if ( m_table
&& m_table
->AppendRows( numRows
) )
2587 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2589 // if we have just inserted cols into an empty grid the current
2590 // cell will be undefined...
2592 SetCurrentCell( 0, 0 );
2595 // the table will have sent the results of the append row
2596 // operation to this view object as a grid table message
2599 if ( !GetBatchCount() ) Refresh();
2609 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2611 // TODO: something with updateLabels flag
2615 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2619 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2621 // the table will have sent the results of the delete row
2622 // operation to this view object as a grid table message
2624 if ( m_numRows
> 0 )
2625 SetEditControlValue();
2627 HideCellEditControl();
2630 if ( !GetBatchCount() ) Refresh();
2640 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2642 // TODO: something with updateLabels flag
2646 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2652 HideCellEditControl();
2653 bool ok
= m_table
->InsertCols( pos
, numCols
);
2655 // the table will have sent the results of the insert col
2656 // operation to this view object as a grid table message
2660 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2662 // if we have just inserted cols into an empty grid the current
2663 // cell will be undefined...
2665 SetCurrentCell( 0, 0 );
2669 if ( !GetBatchCount() ) Refresh();
2672 SetEditControlValue();
2682 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2684 // TODO: something with updateLabels flag
2688 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2692 if ( m_table
&& m_table
->AppendCols( numCols
) )
2694 // the table will have sent the results of the append col
2695 // operation to this view object as a grid table message
2697 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2699 // if we have just inserted cols into an empty grid the current
2700 // cell will be undefined...
2702 SetCurrentCell( 0, 0 );
2706 if ( !GetBatchCount() ) Refresh();
2716 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2718 // TODO: something with updateLabels flag
2722 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2726 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2728 // the table will have sent the results of the delete col
2729 // operation to this view object as a grid table message
2731 if ( m_numCols
> 0 )
2732 SetEditControlValue();
2734 HideCellEditControl();
2737 if ( !GetBatchCount() ) Refresh();
2749 // ----- event handlers
2752 // Generate a grid event based on a mouse event and
2753 // return the result of ProcessEvent()
2755 bool wxGrid::SendEvent( const wxEventType type
,
2757 wxMouseEvent
& mouseEv
)
2759 if ( type
== EVT_GRID_ROW_SIZE
||
2760 type
== EVT_GRID_COL_SIZE
)
2762 int rowOrCol
= (row
== -1 ? col
: row
);
2764 wxGridSizeEvent
gridEvt( GetId(),
2768 mouseEv
.GetX(), mouseEv
.GetY(),
2769 mouseEv
.ControlDown(),
2770 mouseEv
.ShiftDown(),
2772 mouseEv
.MetaDown() );
2774 return GetEventHandler()->ProcessEvent(gridEvt
);
2776 else if ( type
== EVT_GRID_RANGE_SELECT
)
2778 wxGridRangeSelectEvent
gridEvt( GetId(),
2782 m_selectedBottomRight
,
2783 mouseEv
.ControlDown(),
2784 mouseEv
.ShiftDown(),
2786 mouseEv
.MetaDown() );
2788 return GetEventHandler()->ProcessEvent(gridEvt
);
2792 wxGridEvent
gridEvt( GetId(),
2796 mouseEv
.GetX(), mouseEv
.GetY(),
2797 mouseEv
.ControlDown(),
2798 mouseEv
.ShiftDown(),
2800 mouseEv
.MetaDown() );
2802 return GetEventHandler()->ProcessEvent(gridEvt
);
2807 // Generate a grid event of specified type and return the result
2808 // of ProcessEvent().
2810 bool wxGrid::SendEvent( const wxEventType type
,
2813 if ( type
== EVT_GRID_ROW_SIZE
||
2814 type
== EVT_GRID_COL_SIZE
)
2816 int rowOrCol
= (row
== -1 ? col
: row
);
2818 wxGridSizeEvent
gridEvt( GetId(),
2823 return GetEventHandler()->ProcessEvent(gridEvt
);
2827 wxGridEvent
gridEvt( GetId(),
2832 return GetEventHandler()->ProcessEvent(gridEvt
);
2837 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2839 wxPaintDC
dc( this );
2841 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2842 m_numRows
&& m_numCols
)
2844 m_currentCellCoords
.Set(0, 0);
2845 SetEditControlValue();
2846 ShowCellEditControl();
2853 // This is just here to make sure that CalcDimensions gets called when
2854 // the grid view is resized... then the size event is skipped to allow
2855 // the box sizers to handle everything
2857 void wxGrid::OnSize( wxSizeEvent
& event
)
2864 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2866 if ( m_inOnKeyDown
)
2868 // shouldn't be here - we are going round in circles...
2870 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while alread active") );
2873 m_inOnKeyDown
= TRUE
;
2875 // propagate the event up and see if it gets processed
2877 wxWindow
*parent
= GetParent();
2878 wxKeyEvent
keyEvt( event
);
2879 keyEvt
.SetEventObject( parent
);
2881 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2883 // try local handlers
2885 switch ( event
.KeyCode() )
2888 if ( event
.ControlDown() )
2890 MoveCursorUpBlock();
2899 if ( event
.ControlDown() )
2901 MoveCursorDownBlock();
2910 if ( event
.ControlDown() )
2912 MoveCursorLeftBlock();
2921 if ( event
.ControlDown() )
2923 MoveCursorRightBlock();
2932 if ( !IsEditable() )
2943 if ( event
.ControlDown() )
2945 event
.Skip(); // to let the edit control have the return
2954 if ( event
.ControlDown() )
2956 MakeCellVisible( 0, 0 );
2957 SetCurrentCell( 0, 0 );
2966 if ( event
.ControlDown() )
2968 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2969 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2986 // now try the cell edit control
2988 if ( IsCellEditControlEnabled() )
2990 event
.SetEventObject( m_cellEditCtrl
);
2991 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2997 m_inOnKeyDown
= FALSE
;
3001 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3003 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3005 // the event has been intercepted - do nothing
3010 m_currentCellCoords
!= wxGridNoCellCoords
)
3012 HideCellEditControl();
3013 SaveEditControlValue();
3016 m_currentCellCoords
= coords
;
3018 SetEditControlValue();
3022 ShowCellEditControl();
3024 if ( IsSelection() )
3026 wxRect
r( SelectionToDeviceRect() );
3028 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3035 // ------ functions to get/send data (see also public functions)
3038 bool wxGrid::GetModelValues()
3042 // all we need to do is repaint the grid
3044 m_gridWin
->Refresh();
3052 bool wxGrid::SetModelValues()
3058 for ( row
= 0; row
< m_numRows
; row
++ )
3060 for ( col
= 0; col
< m_numCols
; col
++ )
3062 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3074 // Note - this function only draws cells that are in the list of
3075 // exposed cells (usually set from the update region by
3076 // CalcExposedCells)
3078 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3080 if ( !m_numRows
|| !m_numCols
) return;
3083 size_t numCells
= m_cellsExposed
.GetCount();
3085 for ( i
= 0; i
< numCells
; i
++ )
3087 DrawCell( dc
, m_cellsExposed
[i
] );
3092 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3094 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3095 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3097 #if !WXGRID_DRAW_LINES
3098 if ( m_gridLinesEnabled
)
3099 DrawCellBorder( dc
, coords
);
3102 DrawCellBackground( dc
, coords
);
3104 // TODO: separate functions here for different kinds of cells ?
3107 DrawCellValue( dc
, coords
);
3111 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3113 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3114 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3116 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3117 int row
= coords
.GetRow();
3118 int col
= coords
.GetCol();
3120 // right hand border
3122 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3123 m_colRights
[col
], m_rowBottoms
[row
] );
3127 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3128 m_colRights
[col
], m_rowBottoms
[row
] );
3132 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
3134 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3135 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3137 int row
= coords
.GetRow();
3138 int col
= coords
.GetCol();
3140 dc
.SetBackgroundMode( wxSOLID
);
3142 if ( IsInSelection( coords
) )
3144 // TODO: improve this
3146 dc
.SetBrush( *wxBLACK_BRUSH
);
3150 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
3153 dc
.SetPen( *wxTRANSPARENT_PEN
);
3155 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
3156 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
3158 m_rowHeights
[row
]-1 );
3162 void wxGrid::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
3164 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3165 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3167 int row
= coords
.GetRow();
3168 int col
= coords
.GetCol();
3170 dc
.SetBackgroundMode( wxTRANSPARENT
);
3172 if ( IsInSelection( row
, col
) )
3174 // TODO: improve this
3176 dc
.SetTextBackground( wxColour(0, 0, 0) );
3177 dc
.SetTextForeground( wxColour(255, 255, 255) );
3181 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
3182 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
3184 dc
.SetFont( GetCellFont(row
, col
) );
3187 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
3190 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3191 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3192 rect
.SetWidth( m_colWidths
[col
] - 4 );
3193 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3195 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
3200 // TODO: remove this ???
3201 // This is used to redraw all grid lines e.g. when the grid line colour
3204 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3206 if ( !m_gridLinesEnabled
||
3208 !m_numCols
) return;
3210 int top
, bottom
, left
, right
;
3214 m_gridWin
->GetClientSize(&cw
, &ch
);
3216 // virtual coords of visible area
3218 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3219 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3223 reg
.GetBox(x
, y
, w
, h
);
3224 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3225 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3228 // avoid drawing grid lines past the last row and col
3230 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3231 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3233 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3235 // horizontal grid lines
3238 for ( i
= 0; i
< m_numRows
; i
++ )
3240 if ( m_rowBottoms
[i
] > bottom
)
3244 else if ( m_rowBottoms
[i
] >= top
)
3246 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
3251 // vertical grid lines
3253 for ( i
= 0; i
< m_numCols
; i
++ )
3255 if ( m_colRights
[i
] > right
)
3259 else if ( m_colRights
[i
] >= left
)
3261 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
3267 void wxGrid::DrawRowLabels( wxDC
& dc
)
3269 if ( !m_numRows
|| !m_numCols
) return;
3272 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3274 for ( i
= 0; i
< numLabels
; i
++ )
3276 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3281 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
3283 if ( m_rowHeights
[row
] <= 0 ) return;
3285 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3287 dc
.SetPen( *wxBLACK_PEN
);
3288 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3289 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3291 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3292 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3294 dc
.SetPen( *wxWHITE_PEN
);
3295 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
3296 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
3298 dc
.SetBackgroundMode( wxTRANSPARENT
);
3299 dc
.SetTextForeground( GetLabelTextColour() );
3300 dc
.SetFont( GetLabelFont() );
3303 GetRowLabelAlignment( &hAlign
, &vAlign
);
3307 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3308 rect
.SetWidth( m_rowLabelWidth
- 4 );
3309 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3310 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
3314 void wxGrid::DrawColLabels( wxDC
& dc
)
3316 if ( !m_numRows
|| !m_numCols
) return;
3319 size_t numLabels
= m_colLabelsExposed
.GetCount();
3321 for ( i
= 0; i
< numLabels
; i
++ )
3323 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
3328 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
3330 if ( m_colWidths
[col
] <= 0 ) return;
3332 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
3334 dc
.SetPen( *wxBLACK_PEN
);
3335 dc
.DrawLine( m_colRights
[col
]-1, 0,
3336 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3338 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
3339 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3341 dc
.SetPen( *wxWHITE_PEN
);
3342 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
3343 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
3345 dc
.SetBackgroundMode( wxTRANSPARENT
);
3346 dc
.SetTextForeground( GetLabelTextColour() );
3347 dc
.SetFont( GetLabelFont() );
3349 dc
.SetBackgroundMode( wxTRANSPARENT
);
3350 dc
.SetTextForeground( GetLabelTextColour() );
3351 dc
.SetFont( GetLabelFont() );
3354 GetColLabelAlignment( &hAlign
, &vAlign
);
3357 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3359 rect
.SetWidth( m_colWidths
[col
] - 4 );
3360 rect
.SetHeight( m_colLabelHeight
- 4 );
3361 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
3365 void wxGrid::DrawTextRectangle( wxDC
& dc
,
3366 const wxString
& value
,
3371 long textWidth
, textHeight
;
3372 long lineWidth
, lineHeight
;
3373 wxArrayString lines
;
3375 dc
.SetClippingRegion( rect
);
3376 StringToLines( value
, lines
);
3377 if ( lines
.GetCount() )
3379 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
3380 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
3383 switch ( horizAlign
)
3386 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
3390 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
3399 switch ( vertAlign
)
3402 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
3406 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
3415 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
3417 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
3422 dc
.DestroyClippingRegion();
3426 // Split multi line text up into an array of strings. Any existing
3427 // contents of the string array are preserved.
3429 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
3433 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
3434 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
3436 while ( startPos
< (int)tVal
.Length() )
3438 pos
= tVal
.Mid(startPos
).Find( eol
);
3443 else if ( pos
== 0 )
3445 lines
.Add( wxEmptyString
);
3449 lines
.Add( value
.Mid(startPos
, pos
) );
3453 if ( startPos
< (int)value
.Length() )
3455 lines
.Add( value
.Mid( startPos
) );
3460 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3461 wxArrayString
& lines
,
3462 long *width
, long *height
)
3469 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3471 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3472 w
= wxMax( w
, lineW
);
3482 // ------ Edit control functions
3486 void wxGrid::EnableEditing( bool edit
)
3488 // TODO: improve this ?
3490 if ( edit
!= m_editable
)
3494 // TODO: extend this for other edit control types
3496 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3498 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
3504 #if 0 // disabled for the moment - the cell control is always active
3505 void wxGrid::EnableCellEditControl( bool enable
)
3507 if ( m_cellEditCtrl
&&
3508 enable
!= m_cellEditCtrlEnabled
)
3510 m_cellEditCtrlEnabled
= enable
;
3512 if ( m_cellEditCtrlEnabled
)
3514 SetEditControlValue();
3515 ShowCellEditControl();
3519 HideCellEditControl();
3520 SaveEditControlValue();
3527 void wxGrid::ShowCellEditControl()
3531 if ( IsCellEditControlEnabled() )
3533 if ( !IsVisible( m_currentCellCoords
) )
3539 rect
= CellToRect( m_currentCellCoords
);
3541 // convert to scrolled coords
3543 int left
, top
, right
, bottom
;
3544 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3545 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3548 m_gridWin
->GetClientSize( &cw
, &ch
);
3550 // Make the edit control large enough to allow for internal margins
3551 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3554 #if defined(__WXMOTIF__)
3555 if ( m_currentCellCoords
.GetRow() == 0 ||
3556 m_currentCellCoords
.GetCol() == 0 )
3565 if ( m_currentCellCoords
.GetRow() == 0 ||
3566 m_currentCellCoords
.GetCol() == 0 )
3576 #if defined(__WXGTK__)
3579 if (left
!= 0) left_diff
++;
3580 if (top
!= 0) top_diff
++;
3581 rect
.SetLeft( left
+ left_diff
);
3582 rect
.SetTop( top
+ top_diff
);
3583 rect
.SetRight( rect
.GetRight() - left_diff
);
3584 rect
.SetBottom( rect
.GetBottom() - top_diff
);
3586 rect
.SetLeft( wxMax(0, left
- extra
) );
3587 rect
.SetTop( wxMax(0, top
- extra
) );
3588 rect
.SetRight( rect
.GetRight() + 2*extra
);
3589 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3592 m_cellEditCtrl
->SetSize( rect
);
3593 m_cellEditCtrl
->Show( TRUE
);
3595 switch ( m_editCtrlType
)
3597 case wxGRID_TEXTCTRL
:
3598 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3601 case wxGRID_CHECKBOX
:
3602 // TODO: anything ???
3607 // TODO: anything ???
3611 case wxGRID_COMBOBOX
:
3612 // TODO: anything ???
3617 m_cellEditCtrl
->SetFocus();
3623 void wxGrid::HideCellEditControl()
3625 if ( IsCellEditControlEnabled() )
3627 m_cellEditCtrl
->Show( FALSE
);
3632 void wxGrid::SetEditControlValue( const wxString
& value
)
3638 s
= GetCellValue(m_currentCellCoords
);
3642 if ( IsCellEditControlEnabled() )
3644 switch ( m_editCtrlType
)
3646 case wxGRID_TEXTCTRL
:
3647 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3650 case wxGRID_CHECKBOX
:
3651 // TODO: implement this
3656 // TODO: implement this
3660 case wxGRID_COMBOBOX
:
3661 // TODO: implement this
3670 void wxGrid::SaveEditControlValue()
3674 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3676 if ( IsCellEditControlEnabled() )
3678 ctrl
= m_cellEditCtrl
;
3685 bool valueChanged
= FALSE
;
3687 switch ( m_editCtrlType
)
3689 case wxGRID_TEXTCTRL
:
3690 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3691 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3692 SetCellValue( m_currentCellCoords
,
3693 ((wxTextCtrl
*) ctrl
)->GetValue() );
3696 case wxGRID_CHECKBOX
:
3697 // TODO: implement this
3702 // TODO: implement this
3706 case wxGRID_COMBOBOX
:
3707 // TODO: implement this
3714 SendEvent( EVT_GRID_CELL_CHANGE
,
3715 m_currentCellCoords
.GetRow(),
3716 m_currentCellCoords
.GetCol() );
3723 // ------ Grid location functions
3724 // Note that all of these functions work with the logical coordinates of
3725 // grid cells and labels so you will need to convert from device
3726 // coordinates for mouse events etc.
3729 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3731 int row
= YToRow(y
);
3732 int col
= XToCol(x
);
3734 if ( row
== -1 || col
== -1 )
3736 coords
= wxGridNoCellCoords
;
3740 coords
.Set( row
, col
);
3745 int wxGrid::YToRow( int y
)
3749 for ( i
= 0; i
< m_numRows
; i
++ )
3751 if ( y
< m_rowBottoms
[i
] ) return i
;
3758 int wxGrid::XToCol( int x
)
3762 for ( i
= 0; i
< m_numCols
; i
++ )
3764 if ( x
< m_colRights
[i
] ) return i
;
3771 // return the row number that that the y coord is near the edge of, or
3772 // -1 if not near an edge
3774 int wxGrid::YToEdgeOfRow( int y
)
3778 for ( i
= 0; i
< m_numRows
; i
++ )
3780 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3782 d
= abs( y
- m_rowBottoms
[i
] );
3784 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3793 // return the col number that that the x coord is near the edge of, or
3794 // -1 if not near an edge
3796 int wxGrid::XToEdgeOfCol( int x
)
3800 for ( i
= 0; i
< m_numCols
; i
++ )
3802 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3804 d
= abs( x
- m_colRights
[i
] );
3806 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3815 wxRect
wxGrid::CellToRect( int row
, int col
)
3817 wxRect
rect( -1, -1, -1, -1 );
3819 if ( row
>= 0 && row
< m_numRows
&&
3820 col
>= 0 && col
< m_numCols
)
3822 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3823 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3824 rect
.width
= m_colWidths
[col
];
3825 rect
.height
= m_rowHeights
[ row
];
3832 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3834 // get the cell rectangle in logical coords
3836 wxRect
r( CellToRect( row
, col
) );
3838 // convert to device coords
3840 int left
, top
, right
, bottom
;
3841 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3842 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3844 // check against the client area of the grid window
3847 m_gridWin
->GetClientSize( &cw
, &ch
);
3849 if ( wholeCellVisible
)
3851 // is the cell wholly visible ?
3853 return ( left
>= 0 && right
<= cw
&&
3854 top
>= 0 && bottom
<= ch
);
3858 // is the cell partly visible ?
3860 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3861 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3866 // make the specified cell location visible by doing a minimal amount
3869 void wxGrid::MakeCellVisible( int row
, int col
)
3872 int xpos
= -1, ypos
= -1;
3874 if ( row
>= 0 && row
< m_numRows
&&
3875 col
>= 0 && col
< m_numCols
)
3877 // get the cell rectangle in logical coords
3879 wxRect
r( CellToRect( row
, col
) );
3881 // convert to device coords
3883 int left
, top
, right
, bottom
;
3884 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3885 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3888 m_gridWin
->GetClientSize( &cw
, &ch
);
3894 else if ( bottom
> ch
)
3896 int h
= r
.GetHeight();
3898 for ( i
= row
-1; i
>= 0; i
-- )
3900 if ( h
+ m_rowHeights
[i
] > ch
) break;
3902 h
+= m_rowHeights
[i
];
3903 ypos
-= m_rowHeights
[i
];
3906 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
3907 // have rounding errors (this is important, because if we do, we
3908 // might not scroll at all and some cells won't be redrawn)
3909 ypos
+= GRID_SCROLL_LINE
/ 2;
3916 else if ( right
> cw
)
3918 int w
= r
.GetWidth();
3920 for ( i
= col
-1; i
>= 0; i
-- )
3922 if ( w
+ m_colWidths
[i
] > cw
) break;
3924 w
+= m_colWidths
[i
];
3925 xpos
-= m_colWidths
[i
];
3928 // see comment for ypos above
3929 xpos
+= GRID_SCROLL_LINE
/ 2;
3932 if ( xpos
!= -1 || ypos
!= -1 )
3934 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
3935 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
3936 Scroll( xpos
, ypos
);
3944 // ------ Grid cursor movement functions
3947 bool wxGrid::MoveCursorUp()
3949 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3950 m_currentCellCoords
.GetRow() > 0 )
3952 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3953 m_currentCellCoords
.GetCol() );
3955 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3956 m_currentCellCoords
.GetCol() );
3965 bool wxGrid::MoveCursorDown()
3967 // TODO: allow for scrolling
3969 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3970 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3972 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3973 m_currentCellCoords
.GetCol() );
3975 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3976 m_currentCellCoords
.GetCol() );
3985 bool wxGrid::MoveCursorLeft()
3987 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3988 m_currentCellCoords
.GetCol() > 0 )
3990 MakeCellVisible( m_currentCellCoords
.GetRow(),
3991 m_currentCellCoords
.GetCol() - 1 );
3993 SetCurrentCell( m_currentCellCoords
.GetRow(),
3994 m_currentCellCoords
.GetCol() - 1 );
4003 bool wxGrid::MoveCursorRight()
4005 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4006 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4008 MakeCellVisible( m_currentCellCoords
.GetRow(),
4009 m_currentCellCoords
.GetCol() + 1 );
4011 SetCurrentCell( m_currentCellCoords
.GetRow(),
4012 m_currentCellCoords
.GetCol() + 1 );
4021 bool wxGrid::MovePageUp()
4023 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4025 int row
= m_currentCellCoords
.GetRow();
4029 m_gridWin
->GetClientSize( &cw
, &ch
);
4031 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4032 int newRow
= YToRow( y
- ch
+ 1 );
4037 else if ( newRow
== row
)
4042 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4043 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4051 bool wxGrid::MovePageDown()
4053 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4055 int row
= m_currentCellCoords
.GetRow();
4056 if ( row
< m_numRows
)
4059 m_gridWin
->GetClientSize( &cw
, &ch
);
4061 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4062 int newRow
= YToRow( y
+ ch
);
4065 newRow
= m_numRows
- 1;
4067 else if ( newRow
== row
)
4072 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4073 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4081 bool wxGrid::MoveCursorUpBlock()
4084 m_currentCellCoords
!= wxGridNoCellCoords
&&
4085 m_currentCellCoords
.GetRow() > 0 )
4087 int row
= m_currentCellCoords
.GetRow();
4088 int col
= m_currentCellCoords
.GetCol();
4090 if ( m_table
->IsEmptyCell(row
, col
) )
4092 // starting in an empty cell: find the next block of
4098 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4101 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4103 // starting at the top of a block: find the next block
4109 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4114 // starting within a block: find the top of the block
4119 if ( m_table
->IsEmptyCell(row
, col
) )
4127 MakeCellVisible( row
, col
);
4128 SetCurrentCell( row
, col
);
4136 bool wxGrid::MoveCursorDownBlock()
4139 m_currentCellCoords
!= wxGridNoCellCoords
&&
4140 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4142 int row
= m_currentCellCoords
.GetRow();
4143 int col
= m_currentCellCoords
.GetCol();
4145 if ( m_table
->IsEmptyCell(row
, col
) )
4147 // starting in an empty cell: find the next block of
4150 while ( row
< m_numRows
-1 )
4153 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4156 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4158 // starting at the bottom of a block: find the next block
4161 while ( row
< m_numRows
-1 )
4164 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4169 // starting within a block: find the bottom of the block
4171 while ( row
< m_numRows
-1 )
4174 if ( m_table
->IsEmptyCell(row
, col
) )
4182 MakeCellVisible( row
, col
);
4183 SetCurrentCell( row
, col
);
4191 bool wxGrid::MoveCursorLeftBlock()
4194 m_currentCellCoords
!= wxGridNoCellCoords
&&
4195 m_currentCellCoords
.GetCol() > 0 )
4197 int row
= m_currentCellCoords
.GetRow();
4198 int col
= m_currentCellCoords
.GetCol();
4200 if ( m_table
->IsEmptyCell(row
, col
) )
4202 // starting in an empty cell: find the next block of
4208 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4211 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4213 // starting at the left of a block: find the next block
4219 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4224 // starting within a block: find the left of the block
4229 if ( m_table
->IsEmptyCell(row
, col
) )
4237 MakeCellVisible( row
, col
);
4238 SetCurrentCell( row
, col
);
4246 bool wxGrid::MoveCursorRightBlock()
4249 m_currentCellCoords
!= wxGridNoCellCoords
&&
4250 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4252 int row
= m_currentCellCoords
.GetRow();
4253 int col
= m_currentCellCoords
.GetCol();
4255 if ( m_table
->IsEmptyCell(row
, col
) )
4257 // starting in an empty cell: find the next block of
4260 while ( col
< m_numCols
-1 )
4263 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4266 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4268 // starting at the right of a block: find the next block
4271 while ( col
< m_numCols
-1 )
4274 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4279 // starting within a block: find the right of the block
4281 while ( col
< m_numCols
-1 )
4284 if ( m_table
->IsEmptyCell(row
, col
) )
4292 MakeCellVisible( row
, col
);
4293 SetCurrentCell( row
, col
);
4304 // ------ Label values and formatting
4307 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4309 *horiz
= m_rowLabelHorizAlign
;
4310 *vert
= m_rowLabelVertAlign
;
4313 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4315 *horiz
= m_colLabelHorizAlign
;
4316 *vert
= m_colLabelVertAlign
;
4319 wxString
wxGrid::GetRowLabelValue( int row
)
4323 return m_table
->GetRowLabelValue( row
);
4333 wxString
wxGrid::GetColLabelValue( int col
)
4337 return m_table
->GetColLabelValue( col
);
4348 void wxGrid::SetRowLabelSize( int width
)
4350 width
= wxMax( width
, 0 );
4351 if ( width
!= m_rowLabelWidth
)
4355 m_rowLabelWin
->Show( FALSE
);
4356 m_cornerLabelWin
->Show( FALSE
);
4358 else if ( m_rowLabelWidth
== 0 )
4360 m_rowLabelWin
->Show( TRUE
);
4361 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4364 m_rowLabelWidth
= width
;
4371 void wxGrid::SetColLabelSize( int height
)
4373 height
= wxMax( height
, 0 );
4374 if ( height
!= m_colLabelHeight
)
4378 m_colLabelWin
->Show( FALSE
);
4379 m_cornerLabelWin
->Show( FALSE
);
4381 else if ( m_colLabelHeight
== 0 )
4383 m_colLabelWin
->Show( TRUE
);
4384 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4387 m_colLabelHeight
= height
;
4394 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
4396 if ( m_labelBackgroundColour
!= colour
)
4398 m_labelBackgroundColour
= colour
;
4399 m_rowLabelWin
->SetBackgroundColour( colour
);
4400 m_colLabelWin
->SetBackgroundColour( colour
);
4401 m_cornerLabelWin
->SetBackgroundColour( colour
);
4403 if ( !GetBatchCount() )
4405 m_rowLabelWin
->Refresh();
4406 m_colLabelWin
->Refresh();
4407 m_cornerLabelWin
->Refresh();
4412 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
4414 if ( m_labelTextColour
!= colour
)
4416 m_labelTextColour
= colour
;
4417 if ( !GetBatchCount() )
4419 m_rowLabelWin
->Refresh();
4420 m_colLabelWin
->Refresh();
4425 void wxGrid::SetLabelFont( const wxFont
& font
)
4428 if ( !GetBatchCount() )
4430 m_rowLabelWin
->Refresh();
4431 m_colLabelWin
->Refresh();
4435 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
4437 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4439 m_rowLabelHorizAlign
= horiz
;
4442 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4444 m_rowLabelVertAlign
= vert
;
4447 if ( !GetBatchCount() )
4449 m_rowLabelWin
->Refresh();
4453 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
4455 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4457 m_colLabelHorizAlign
= horiz
;
4460 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4462 m_colLabelVertAlign
= vert
;
4465 if ( !GetBatchCount() )
4467 m_colLabelWin
->Refresh();
4471 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
4475 m_table
->SetRowLabelValue( row
, s
);
4476 if ( !GetBatchCount() )
4478 wxRect rect
= CellToRect( row
, 0);
4479 if ( rect
.height
> 0 )
4481 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
4483 rect
.width
= m_rowLabelWidth
;
4484 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4490 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
4494 m_table
->SetColLabelValue( col
, s
);
4495 if ( !GetBatchCount() )
4497 wxRect rect
= CellToRect( 0, col
);
4498 if ( rect
.width
> 0 )
4500 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
4502 rect
.height
= m_colLabelHeight
;
4503 m_colLabelWin
->Refresh( TRUE
, &rect
);
4509 void wxGrid::SetGridLineColour( const wxColour
& colour
)
4511 if ( m_gridLineColour
!= colour
)
4513 m_gridLineColour
= colour
;
4515 wxClientDC
dc( m_gridWin
);
4517 DrawAllGridLines( dc
, wxRegion() );
4521 void wxGrid::EnableGridLines( bool enable
)
4523 if ( enable
!= m_gridLinesEnabled
)
4525 m_gridLinesEnabled
= enable
;
4527 if ( !GetBatchCount() )
4531 wxClientDC
dc( m_gridWin
);
4533 DrawAllGridLines( dc
, wxRegion() );
4537 m_gridWin
->Refresh();
4544 int wxGrid::GetDefaultRowSize()
4546 return m_defaultRowHeight
;
4549 int wxGrid::GetRowSize( int row
)
4551 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
4553 return m_rowHeights
[row
];
4556 int wxGrid::GetDefaultColSize()
4558 return m_defaultColWidth
;
4561 int wxGrid::GetColSize( int col
)
4563 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
4565 return m_colWidths
[col
];
4568 wxColour
wxGrid::GetDefaultCellBackgroundColour()
4570 return m_gridWin
->GetBackgroundColour();
4573 // TODO VZ: this must be optimized to allow only retrieveing attr once!
4575 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
4577 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4580 if ( attr
&& attr
->HasBackgroundColour() )
4581 colour
= attr
->GetBackgroundColour();
4583 colour
= GetDefaultCellBackgroundColour();
4590 wxColour
wxGrid::GetDefaultCellTextColour()
4592 return m_gridWin
->GetForegroundColour();
4595 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
4597 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4600 if ( attr
&& attr
->HasTextColour() )
4601 colour
= attr
->GetTextColour();
4603 colour
= GetDefaultCellTextColour();
4611 wxFont
wxGrid::GetDefaultCellFont()
4613 return m_defaultCellFont
;
4616 wxFont
wxGrid::GetCellFont( int row
, int col
)
4618 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4621 if ( attr
&& attr
->HasFont() )
4622 font
= attr
->GetFont();
4624 font
= GetDefaultCellFont();
4631 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
4634 *horiz
= m_defaultCellHAlign
;
4636 *vert
= m_defaultCellVAlign
;
4639 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
4641 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4643 if ( attr
&& attr
->HasAlignment() )
4644 attr
->GetAlignment(horiz
, vert
);
4646 GetDefaultCellAlignment(horiz
, vert
);
4651 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
4653 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
4655 if ( resizeExistingRows
)
4659 for ( row
= 0; row
< m_numRows
; row
++ )
4661 m_rowHeights
[row
] = m_defaultRowHeight
;
4662 bottom
+= m_defaultRowHeight
;
4663 m_rowBottoms
[row
] = bottom
;
4669 void wxGrid::SetRowSize( int row
, int height
)
4671 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
4675 int h
= wxMax( 0, height
);
4676 int diff
= h
- m_rowHeights
[row
];
4678 m_rowHeights
[row
] = h
;
4679 for ( i
= row
; i
< m_numRows
; i
++ )
4681 m_rowBottoms
[i
] += diff
;
4686 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4688 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4690 if ( resizeExistingCols
)
4694 for ( col
= 0; col
< m_numCols
; col
++ )
4696 m_colWidths
[col
] = m_defaultColWidth
;
4697 right
+= m_defaultColWidth
;
4698 m_colRights
[col
] = right
;
4704 void wxGrid::SetColSize( int col
, int width
)
4706 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
4710 int w
= wxMax( 0, width
);
4711 int diff
= w
- m_colWidths
[col
];
4712 m_colWidths
[col
] = w
;
4714 for ( i
= col
; i
< m_numCols
; i
++ )
4716 m_colRights
[i
] += diff
;
4721 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
4723 m_gridWin
->SetBackgroundColour(col
);
4726 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
4728 m_gridWin
->SetForegroundColour(col
);
4731 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
4733 m_defaultCellHAlign
= horiz
;
4734 m_defaultCellVAlign
= vert
;
4737 bool wxGrid::CanHaveAttributes()
4744 if ( !m_table
->GetAttrProvider() )
4746 // use the default attr provider by default
4747 // (another choice would be to just return FALSE thus forcing the user
4749 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
4755 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
4757 if ( CanHaveAttributes() )
4759 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4760 attr
->SetBackgroundColour(colour
);
4762 m_table
->SetAttr(attr
, row
, col
);
4766 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
4768 if ( CanHaveAttributes() )
4770 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4771 attr
->SetTextColour(colour
);
4773 m_table
->SetAttr(attr
, row
, col
);
4777 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
4779 m_defaultCellFont
= font
;
4782 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
4784 if ( CanHaveAttributes() )
4786 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4787 attr
->SetFont(font
);
4789 m_table
->SetAttr(attr
, row
, col
);
4793 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
4795 if ( CanHaveAttributes() )
4797 wxGridCellAttr
*attr
= new wxGridCellAttr
;
4798 attr
->SetAlignment(horiz
, vert
);
4800 m_table
->SetAttr(attr
, row
, col
);
4807 // ------ cell value accessor functions
4810 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4814 m_table
->SetValue( row
, col
, s
.c_str() );
4815 if ( !GetBatchCount() )
4817 wxClientDC
dc( m_gridWin
);
4819 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4822 #if 0 // TODO: edit in place
4824 if ( m_currentCellCoords
.GetRow() == row
&&
4825 m_currentCellCoords
.GetCol() == col
)
4827 SetEditControlValue( s
);
4836 // ------ Block, row and col selection
4839 void wxGrid::SelectRow( int row
, bool addToSelected
)
4843 if ( IsSelection() && addToSelected
)
4846 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4849 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4850 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4851 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4852 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4856 need_refresh
[0] = TRUE
;
4857 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
4858 wxGridCellCoords ( oldTop
- 1,
4860 m_selectedTopLeft
.SetRow( row
);
4865 need_refresh
[1] = TRUE
;
4866 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
4867 wxGridCellCoords ( oldBottom
,
4870 m_selectedTopLeft
.SetCol( 0 );
4873 if ( oldBottom
< row
)
4875 need_refresh
[2] = TRUE
;
4876 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
4877 wxGridCellCoords ( row
,
4879 m_selectedBottomRight
.SetRow( row
);
4882 if ( oldRight
< m_numCols
- 1 )
4884 need_refresh
[3] = TRUE
;
4885 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4887 wxGridCellCoords ( oldBottom
,
4889 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4892 for (i
= 0; i
< 4; i
++ )
4893 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4894 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4898 r
= SelectionToDeviceRect();
4900 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4902 m_selectedTopLeft
.Set( row
, 0 );
4903 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4904 r
= SelectionToDeviceRect();
4905 m_gridWin
->Refresh( FALSE
, &r
);
4908 wxGridRangeSelectEvent
gridEvt( GetId(),
4909 EVT_GRID_RANGE_SELECT
,
4912 m_selectedBottomRight
);
4914 GetEventHandler()->ProcessEvent(gridEvt
);
4918 void wxGrid::SelectCol( int col
, bool addToSelected
)
4920 if ( IsSelection() && addToSelected
)
4923 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4926 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4927 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4928 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4929 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4931 if ( oldLeft
> col
)
4933 need_refresh
[0] = TRUE
;
4934 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
4935 wxGridCellCoords ( m_numRows
- 1,
4937 m_selectedTopLeft
.SetCol( col
);
4942 need_refresh
[1] = TRUE
;
4943 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
4944 wxGridCellCoords ( oldTop
- 1,
4946 m_selectedTopLeft
.SetRow( 0 );
4949 if ( oldRight
< col
)
4951 need_refresh
[2] = TRUE
;
4952 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
4953 wxGridCellCoords ( m_numRows
- 1,
4955 m_selectedBottomRight
.SetCol( col
);
4958 if ( oldBottom
< m_numRows
- 1 )
4960 need_refresh
[3] = TRUE
;
4961 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
4963 wxGridCellCoords ( m_numRows
- 1,
4965 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4968 for (i
= 0; i
< 4; i
++ )
4969 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4970 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4976 r
= SelectionToDeviceRect();
4978 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4980 m_selectedTopLeft
.Set( 0, col
);
4981 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4982 r
= SelectionToDeviceRect();
4983 m_gridWin
->Refresh( FALSE
, &r
);
4986 wxGridRangeSelectEvent
gridEvt( GetId(),
4987 EVT_GRID_RANGE_SELECT
,
4990 m_selectedBottomRight
);
4992 GetEventHandler()->ProcessEvent(gridEvt
);
4996 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4999 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5001 if ( topRow
> bottomRow
)
5008 if ( leftCol
> rightCol
)
5015 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5016 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5018 if ( m_selectedTopLeft
!= updateTopLeft
||
5019 m_selectedBottomRight
!= updateBottomRight
)
5021 // Compute two optimal update rectangles:
5022 // Either one rectangle is a real subset of the
5023 // other, or they are (almost) disjoint!
5025 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5028 // Store intermediate values
5029 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5030 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5031 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5032 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5034 // Determine the outer/inner coordinates.
5035 if (oldLeft
> leftCol
)
5041 if (oldTop
> topRow
)
5047 if (oldRight
< rightCol
)
5050 oldRight
= rightCol
;
5053 if (oldBottom
< bottomRow
)
5056 oldBottom
= bottomRow
;
5060 // Now, either the stuff marked old is the outer
5061 // rectangle or we don't have a situation where one
5062 // is contained in the other.
5064 if ( oldLeft
< leftCol
)
5066 need_refresh
[0] = TRUE
;
5067 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5069 wxGridCellCoords ( oldBottom
,
5073 if ( oldTop
< topRow
)
5075 need_refresh
[1] = TRUE
;
5076 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5078 wxGridCellCoords ( topRow
- 1,
5082 if ( oldRight
> rightCol
)
5084 need_refresh
[2] = TRUE
;
5085 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5087 wxGridCellCoords ( oldBottom
,
5091 if ( oldBottom
> bottomRow
)
5093 need_refresh
[3] = TRUE
;
5094 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5096 wxGridCellCoords ( oldBottom
,
5102 m_selectedTopLeft
= updateTopLeft
;
5103 m_selectedBottomRight
= updateBottomRight
;
5105 // various Refresh() calls
5106 for (i
= 0; i
< 4; i
++ )
5107 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5108 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5111 // only generate an event if the block is not being selected by
5112 // dragging the mouse (in which case the event will be generated in
5113 // the mouse event handler)
5114 if ( !m_isDragging
)
5116 wxGridRangeSelectEvent
gridEvt( GetId(),
5117 EVT_GRID_RANGE_SELECT
,
5120 m_selectedBottomRight
);
5122 GetEventHandler()->ProcessEvent(gridEvt
);
5126 void wxGrid::SelectAll()
5128 m_selectedTopLeft
.Set( 0, 0 );
5129 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5131 m_gridWin
->Refresh();
5135 void wxGrid::ClearSelection()
5137 m_selectedTopLeft
= wxGridNoCellCoords
;
5138 m_selectedBottomRight
= wxGridNoCellCoords
;
5142 // This function returns the rectangle that encloses the given block
5143 // in device coords clipped to the client size of the grid window.
5145 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5146 const wxGridCellCoords
&bottomRight
)
5148 wxRect
rect( wxGridNoCellRect
);
5151 cellRect
= CellToRect( topLeft
);
5152 if ( cellRect
!= wxGridNoCellRect
)
5158 rect
= wxRect( 0, 0, 0, 0 );
5161 cellRect
= CellToRect( bottomRight
);
5162 if ( cellRect
!= wxGridNoCellRect
)
5168 return wxGridNoCellRect
;
5171 // convert to scrolled coords
5173 int left
, top
, right
, bottom
;
5174 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5175 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5178 m_gridWin
->GetClientSize( &cw
, &ch
);
5180 rect
.SetLeft( wxMax(0, left
) );
5181 rect
.SetTop( wxMax(0, top
) );
5182 rect
.SetRight( wxMin(cw
, right
) );
5183 rect
.SetBottom( wxMin(ch
, bottom
) );
5191 // ------ Grid event classes
5194 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5196 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5197 int row
, int col
, int x
, int y
,
5198 bool control
, bool shift
, bool alt
, bool meta
)
5199 : wxNotifyEvent( type
, id
)
5205 m_control
= control
;
5210 SetEventObject(obj
);
5214 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
5216 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
5217 int rowOrCol
, int x
, int y
,
5218 bool control
, bool shift
, bool alt
, bool meta
)
5219 : wxNotifyEvent( type
, id
)
5221 m_rowOrCol
= rowOrCol
;
5224 m_control
= control
;
5229 SetEventObject(obj
);
5233 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
5235 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
5236 const wxGridCellCoords
& topLeft
,
5237 const wxGridCellCoords
& bottomRight
,
5238 bool control
, bool shift
, bool alt
, bool meta
)
5239 : wxNotifyEvent( type
, id
)
5241 m_topLeft
= topLeft
;
5242 m_bottomRight
= bottomRight
;
5243 m_control
= control
;
5248 SetEventObject(obj
);
5252 #endif // ifndef wxUSE_NEW_GRID