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
, wxGridCellAttr
*attr_
)
48 : coords(row
, col
), attr(attr_
)
57 wxGridCellCoords coords
;
61 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
63 #include "wx/arrimpl.cpp"
65 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
66 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
75 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
76 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
77 const wxPoint
&pos
, const wxSize
&size
);
82 void OnPaint( wxPaintEvent
& event
);
83 void OnMouseEvent( wxMouseEvent
& event
);
84 void OnKeyDown( wxKeyEvent
& event
);
86 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
91 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
94 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
95 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
96 const wxPoint
&pos
, const wxSize
&size
);
101 void OnPaint( wxPaintEvent
&event
);
102 void OnMouseEvent( wxMouseEvent
& event
);
103 void OnKeyDown( wxKeyEvent
& event
);
105 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
106 DECLARE_EVENT_TABLE()
110 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
113 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
114 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
115 const wxPoint
&pos
, const wxSize
&size
);
120 void OnMouseEvent( wxMouseEvent
& event
);
121 void OnKeyDown( wxKeyEvent
& event
);
122 void OnPaint( wxPaintEvent
& event
);
124 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
125 DECLARE_EVENT_TABLE()
128 class WXDLLEXPORT wxGridWindow
: public wxPanel
133 m_owner
= (wxGrid
*)NULL
;
134 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
135 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
138 wxGridWindow( wxGrid
*parent
,
139 wxGridRowLabelWindow
*rowLblWin
,
140 wxGridColLabelWindow
*colLblWin
,
141 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
144 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
148 wxGridRowLabelWindow
*m_rowLabelWin
;
149 wxGridColLabelWindow
*m_colLabelWin
;
151 void OnPaint( wxPaintEvent
&event
);
152 void OnMouseEvent( wxMouseEvent
& event
);
153 void OnKeyDown( wxKeyEvent
& );
155 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
156 DECLARE_EVENT_TABLE()
159 // the internal data representation used by wxGridCellAttrProvider
161 // TODO make it more efficient
162 class WXDLLEXPORT wxGridCellAttrProviderData
165 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
166 wxGridCellAttr
*GetAttr(int row
, int col
) const;
169 // searches for the attr for given cell, returns wxNOT_FOUND if not found
170 int FindIndex(int row
, int col
) const;
172 wxGridCellWithAttrArray m_attrs
;
175 // ----------------------------------------------------------------------------
176 // conditional compilation
177 // ----------------------------------------------------------------------------
179 #ifndef WXGRID_DRAW_LINES
180 #define WXGRID_DRAW_LINES 1
183 //////////////////////////////////////////////////////////////////////
185 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
186 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
189 // TODO: fixed so far - make configurable later (and also different for x/y)
190 static const size_t GRID_SCROLL_LINE
= 10;
192 // ============================================================================
194 // ============================================================================
196 // ----------------------------------------------------------------------------
197 // wxGridCellRenderer
198 // ----------------------------------------------------------------------------
200 void wxGridCellRenderer::Draw(wxGrid
& grid
,
206 dc
.SetBackgroundMode( wxSOLID
);
211 dc
.SetBrush( *wxBLACK_BRUSH
);
215 dc
.SetBrush( wxBrush(grid
.GetCellBackgroundColour(row
, col
), wxSOLID
) );
218 dc
.SetPen( *wxTRANSPARENT_PEN
);
220 dc
.DrawRectangle(rect
);
223 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
225 const wxRect
& rectCell
,
229 wxGridCellRenderer::Draw(grid
, dc
, rectCell
, row
, col
, isSelected
);
231 // now we only have to draw the text
232 dc
.SetBackgroundMode( wxTRANSPARENT
);
237 dc
.SetTextBackground( wxColour(0, 0, 0) );
238 dc
.SetTextForeground( wxColour(255, 255, 255) );
242 dc
.SetTextBackground( grid
.GetCellBackgroundColour(row
, col
) );
243 dc
.SetTextForeground( grid
.GetCellTextColour(row
, col
) );
245 dc
.SetFont( grid
.GetCellFont(row
, col
) );
248 grid
.GetCellAlignment(row
, col
, &hAlign
, &vAlign
);
250 wxRect rect
= rectCell
;
256 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
257 rect
, hAlign
, vAlign
);
260 // ----------------------------------------------------------------------------
261 // wxGridCellAttrProviderData
262 // ----------------------------------------------------------------------------
264 void wxGridCellAttrProviderData::SetAttr(wxGridCellAttr
*attr
,
267 int n
= FindIndex(row
, col
);
268 if ( n
== wxNOT_FOUND
)
271 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
277 // change the attribute
278 m_attrs
[(size_t)n
].attr
= attr
;
282 // remove this attribute
283 m_attrs
.RemoveAt((size_t)n
);
288 wxGridCellAttr
*wxGridCellAttrProviderData::GetAttr(int row
, int col
) const
290 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
292 int n
= FindIndex(row
, col
);
293 if ( n
!= wxNOT_FOUND
)
295 attr
= m_attrs
[(size_t)n
].attr
;
302 int wxGridCellAttrProviderData::FindIndex(int row
, int col
) const
304 size_t count
= m_attrs
.GetCount();
305 for ( size_t n
= 0; n
< count
; n
++ )
307 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
308 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
317 // ----------------------------------------------------------------------------
318 // wxGridCellAttrProvider
319 // ----------------------------------------------------------------------------
321 wxGridCellAttrProvider::wxGridCellAttrProvider()
323 m_data
= (wxGridCellAttrProviderData
*)NULL
;
326 wxGridCellAttrProvider::~wxGridCellAttrProvider()
331 void wxGridCellAttrProvider::InitData()
333 m_data
= new wxGridCellAttrProviderData
;
336 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
338 return m_data
? m_data
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
341 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
347 m_data
->SetAttr(attr
, row
, col
);
350 //////////////////////////////////////////////////////////////////////
352 // Abstract base class for grid data (the model)
354 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
357 wxGridTableBase::wxGridTableBase()
359 m_view
= (wxGrid
*) NULL
;
360 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
363 wxGridTableBase::~wxGridTableBase()
365 delete m_attrProvider
;
368 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
370 delete m_attrProvider
;
371 m_attrProvider
= attrProvider
;
374 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
376 if ( m_attrProvider
)
377 return m_attrProvider
->GetAttr(row
, col
);
379 return (wxGridCellAttr
*)NULL
;
382 void wxGridTableBase::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
384 if ( m_attrProvider
)
386 m_attrProvider
->SetAttr(attr
, row
, col
);
390 // as we take ownership of the pointer and don't store it, we must
396 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
398 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
399 "but your derived table class does not override this function") );
404 bool wxGridTableBase::AppendRows( size_t numRows
)
406 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
407 "but your derived table class does not override this function"));
412 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
414 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
415 "but your derived table class does not override this function"));
420 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
422 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
423 "but your derived table class does not override this function"));
428 bool wxGridTableBase::AppendCols( size_t numCols
)
430 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
431 "but your derived table class does not override this function"));
436 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
438 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
439 "but your derived table class does not override this function"));
445 wxString
wxGridTableBase::GetRowLabelValue( int row
)
452 wxString
wxGridTableBase::GetColLabelValue( int col
)
454 // default col labels are:
455 // cols 0 to 25 : A-Z
456 // cols 26 to 675 : AA-ZZ
463 s
+= (_T('A') + (wxChar
)( col%26
));
465 if ( col
< 0 ) break;
468 // reverse the string...
470 for ( i
= 0; i
< n
; i
++ )
480 //////////////////////////////////////////////////////////////////////
482 // Message class for the grid table to send requests and notifications
486 wxGridTableMessage::wxGridTableMessage()
488 m_table
= (wxGridTableBase
*) NULL
;
494 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
495 int commandInt1
, int commandInt2
)
499 m_comInt1
= commandInt1
;
500 m_comInt2
= commandInt2
;
505 //////////////////////////////////////////////////////////////////////
507 // A basic grid table for string data. An object of this class will
508 // created by wxGrid if you don't specify an alternative table class.
511 WX_DEFINE_OBJARRAY(wxGridStringArray
)
513 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
515 wxGridStringTable::wxGridStringTable()
520 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
525 m_data
.Alloc( numRows
);
529 for ( col
= 0; col
< numCols
; col
++ )
531 sa
.Add( wxEmptyString
);
534 for ( row
= 0; row
< numRows
; row
++ )
540 wxGridStringTable::~wxGridStringTable()
544 long wxGridStringTable::GetNumberRows()
546 return m_data
.GetCount();
549 long wxGridStringTable::GetNumberCols()
551 if ( m_data
.GetCount() > 0 )
552 return m_data
[0].GetCount();
557 wxString
wxGridStringTable::GetValue( int row
, int col
)
559 // TODO: bounds checking
561 return m_data
[row
][col
];
564 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
566 // TODO: bounds checking
568 m_data
[row
][col
] = s
;
571 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
573 // TODO: bounds checking
575 return (m_data
[row
][col
] == wxEmptyString
);
579 void wxGridStringTable::Clear()
582 int numRows
, numCols
;
584 numRows
= m_data
.GetCount();
587 numCols
= m_data
[0].GetCount();
589 for ( row
= 0; row
< numRows
; row
++ )
591 for ( col
= 0; col
< numCols
; col
++ )
593 m_data
[row
][col
] = wxEmptyString
;
600 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
604 size_t curNumRows
= m_data
.GetCount();
605 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
607 if ( pos
>= curNumRows
)
609 return AppendRows( numRows
);
613 sa
.Alloc( curNumCols
);
614 for ( col
= 0; col
< curNumCols
; col
++ )
616 sa
.Add( wxEmptyString
);
619 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
621 m_data
.Insert( sa
, row
);
626 wxGridTableMessage
msg( this,
627 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
631 GetView()->ProcessTableMessage( msg
);
637 bool wxGridStringTable::AppendRows( size_t numRows
)
641 size_t curNumRows
= m_data
.GetCount();
642 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
645 if ( curNumCols
> 0 )
647 sa
.Alloc( curNumCols
);
648 for ( col
= 0; col
< curNumCols
; col
++ )
650 sa
.Add( wxEmptyString
);
654 for ( row
= 0; row
< numRows
; row
++ )
661 wxGridTableMessage
msg( this,
662 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
665 GetView()->ProcessTableMessage( msg
);
671 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
675 size_t curNumRows
= m_data
.GetCount();
677 if ( pos
>= curNumRows
)
680 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
681 "Pos value is invalid for present table with %d rows",
682 pos
, numRows
, curNumRows
);
683 wxFAIL_MSG( wxT(errmsg
) );
687 if ( numRows
> curNumRows
- pos
)
689 numRows
= curNumRows
- pos
;
692 if ( numRows
>= curNumRows
)
694 m_data
.Empty(); // don't release memory just yet
698 for ( n
= 0; n
< numRows
; n
++ )
700 m_data
.Remove( pos
);
706 wxGridTableMessage
msg( this,
707 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
711 GetView()->ProcessTableMessage( msg
);
717 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
721 size_t curNumRows
= m_data
.GetCount();
722 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
724 if ( pos
>= curNumCols
)
726 return AppendCols( numCols
);
729 for ( row
= 0; row
< curNumRows
; row
++ )
731 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
733 m_data
[row
].Insert( wxEmptyString
, col
);
739 wxGridTableMessage
msg( this,
740 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
744 GetView()->ProcessTableMessage( msg
);
750 bool wxGridStringTable::AppendCols( size_t numCols
)
754 size_t curNumRows
= m_data
.GetCount();
757 // TODO: something better than this ?
759 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
760 "Call AppendRows() first") );
764 for ( row
= 0; row
< curNumRows
; row
++ )
766 for ( n
= 0; n
< numCols
; n
++ )
768 m_data
[row
].Add( wxEmptyString
);
774 wxGridTableMessage
msg( this,
775 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
778 GetView()->ProcessTableMessage( msg
);
784 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
788 size_t curNumRows
= m_data
.GetCount();
789 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
791 if ( pos
>= curNumCols
)
794 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
795 "Pos value is invalid for present table with %d cols",
796 pos
, numCols
, curNumCols
);
797 wxFAIL_MSG( wxT( errmsg
) );
801 if ( numCols
> curNumCols
- pos
)
803 numCols
= curNumCols
- pos
;
806 for ( row
= 0; row
< curNumRows
; row
++ )
808 if ( numCols
>= curNumCols
)
814 for ( n
= 0; n
< numCols
; n
++ )
816 m_data
[row
].Remove( pos
);
823 wxGridTableMessage
msg( this,
824 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
828 GetView()->ProcessTableMessage( msg
);
834 wxString
wxGridStringTable::GetRowLabelValue( int row
)
836 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
838 // using default label
840 return wxGridTableBase::GetRowLabelValue( row
);
844 return m_rowLabels
[ row
];
848 wxString
wxGridStringTable::GetColLabelValue( int col
)
850 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
852 // using default label
854 return wxGridTableBase::GetColLabelValue( col
);
858 return m_colLabels
[ col
];
862 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
864 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
866 int n
= m_rowLabels
.GetCount();
868 for ( i
= n
; i
<= row
; i
++ )
870 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
874 m_rowLabels
[row
] = value
;
877 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
879 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
881 int n
= m_colLabels
.GetCount();
883 for ( i
= n
; i
<= col
; i
++ )
885 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
889 m_colLabels
[col
] = value
;
895 //////////////////////////////////////////////////////////////////////
897 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
899 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
900 EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown
)
904 wxGridTextCtrl::wxGridTextCtrl( wxWindow
*par
,
908 const wxString
& value
,
912 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
915 m_isCellControl
= isCellControl
;
919 void wxGridTextCtrl::OnKeyDown( wxKeyEvent
& event
)
921 switch ( event
.KeyCode() )
924 m_grid
->SetEditControlValue( startValue
);
925 SetInsertionPointEnd();
935 if ( m_isCellControl
)
937 // send the event to the parent grid, skipping the
938 // event if nothing happens
940 event
.Skip( m_grid
->ProcessEvent( event
) );
944 // default text control response within the top edit
952 if ( m_isCellControl
)
954 if ( !m_grid
->ProcessEvent( event
) )
956 #if defined(__WXMOTIF__) || defined(__WXGTK__)
957 // wxMotif needs a little extra help...
959 int pos
= GetInsertionPoint();
960 wxString
s( GetValue() );
961 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
963 SetInsertionPoint( pos
);
965 // the other ports can handle a Return key press
975 if ( m_isCellControl
)
977 // send the event to the parent grid, skipping the
978 // event if nothing happens
980 event
.Skip( m_grid
->ProcessEvent( event
) );
984 // default text control response within the top edit
996 void wxGridTextCtrl::SetStartValue( const wxString
& s
)
999 wxTextCtrl::SetValue(s
);
1004 //////////////////////////////////////////////////////////////////////
1006 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
1008 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
1009 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1010 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1011 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
1014 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
1016 const wxPoint
&pos
, const wxSize
&size
)
1017 : wxWindow( parent
, id
, pos
, size
)
1022 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
1026 // NO - don't do this because it will set both the x and y origin
1027 // coords to match the parent scrolled window and we just want to
1028 // set the y coord - MB
1030 // m_owner->PrepareDC( dc );
1033 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1034 dc
.SetDeviceOrigin( 0, -y
);
1036 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1037 m_owner
->DrawRowLabels( dc
);
1041 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1043 m_owner
->ProcessRowLabelMouseEvent( event
);
1047 // This seems to be required for wxMotif otherwise the mouse
1048 // cursor must be in the cell edit control to get key events
1050 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1052 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1057 //////////////////////////////////////////////////////////////////////
1059 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
1061 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
1062 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1063 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1064 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
1067 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
1069 const wxPoint
&pos
, const wxSize
&size
)
1070 : wxWindow( parent
, id
, pos
, size
)
1075 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
1079 // NO - don't do this because it will set both the x and y origin
1080 // coords to match the parent scrolled window and we just want to
1081 // set the x coord - MB
1083 // m_owner->PrepareDC( dc );
1086 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1087 dc
.SetDeviceOrigin( -x
, 0 );
1089 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1090 m_owner
->DrawColLabels( dc
);
1094 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1096 m_owner
->ProcessColLabelMouseEvent( event
);
1100 // This seems to be required for wxMotif otherwise the mouse
1101 // cursor must be in the cell edit control to get key events
1103 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1105 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1110 //////////////////////////////////////////////////////////////////////
1112 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
1114 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
1115 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1116 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1117 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
1120 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
1122 const wxPoint
&pos
, const wxSize
&size
)
1123 : wxWindow( parent
, id
, pos
, size
)
1128 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1132 int client_height
= 0;
1133 int client_width
= 0;
1134 GetClientSize( &client_width
, &client_height
);
1136 dc
.SetPen( *wxBLACK_PEN
);
1137 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
1138 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
1140 dc
.SetPen( *wxWHITE_PEN
);
1141 dc
.DrawLine( 0, 0, client_width
, 0 );
1142 dc
.DrawLine( 0, 0, 0, client_height
);
1146 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1148 m_owner
->ProcessCornerLabelMouseEvent( event
);
1152 // This seems to be required for wxMotif otherwise the mouse
1153 // cursor must be in the cell edit control to get key events
1155 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
1157 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1162 //////////////////////////////////////////////////////////////////////
1164 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
1166 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
1167 EVT_PAINT( wxGridWindow::OnPaint
)
1168 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1169 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1172 wxGridWindow::wxGridWindow( wxGrid
*parent
,
1173 wxGridRowLabelWindow
*rowLblWin
,
1174 wxGridColLabelWindow
*colLblWin
,
1175 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
1176 : wxPanel( parent
, id
, pos
, size
, 0, "grid window" )
1179 m_rowLabelWin
= rowLblWin
;
1180 m_colLabelWin
= colLblWin
;
1182 SetBackgroundColour( "WHITE" );
1186 wxGridWindow::~wxGridWindow()
1191 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1193 wxPaintDC
dc( this );
1194 m_owner
->PrepareDC( dc
);
1195 wxRegion reg
= GetUpdateRegion();
1196 m_owner
->CalcCellsExposed( reg
);
1197 m_owner
->DrawGridCellArea( dc
);
1198 #if WXGRID_DRAW_LINES
1199 m_owner
->DrawAllGridLines( dc
, reg
);
1204 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1206 wxPanel::ScrollWindow( dx
, dy
, rect
);
1207 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
1208 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
1212 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1214 m_owner
->ProcessGridCellMouseEvent( event
);
1218 // This seems to be required for wxMotif otherwise the mouse
1219 // cursor must be in the cell edit control to get key events
1221 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1223 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
1228 //////////////////////////////////////////////////////////////////////
1230 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1232 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1233 EVT_PAINT( wxGrid::OnPaint
)
1234 EVT_SIZE( wxGrid::OnSize
)
1235 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1238 wxGrid::wxGrid( wxWindow
*parent
,
1243 const wxString
& name
)
1244 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
1252 delete m_defaultRenderer
;
1258 // ----- internal init and update functions
1261 void wxGrid::Create()
1263 m_created
= FALSE
; // set to TRUE by CreateGrid
1264 m_displayed
= FALSE
; // set to TRUE by OnPaint
1266 m_table
= (wxGridTableBase
*) NULL
;
1267 m_cellEditCtrl
= (wxWindow
*) NULL
;
1271 m_currentCellCoords
= wxGridNoCellCoords
;
1273 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1274 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1276 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
1281 m_rowLabelWin
= new wxGridRowLabelWindow( this,
1286 m_colLabelWin
= new wxGridColLabelWindow( this,
1291 m_gridWin
= new wxGridWindow( this,
1298 SetTargetWindow( m_gridWin
);
1302 bool wxGrid::CreateGrid( int numRows
, int numCols
)
1306 wxFAIL_MSG( wxT("wxGrid::CreateGrid called more than once") );
1311 m_numRows
= numRows
;
1312 m_numCols
= numCols
;
1314 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1315 m_table
->SetView( this );
1328 if ( m_numRows
<= 0 )
1329 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1331 if ( m_numCols
<= 0 )
1332 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1334 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1335 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1337 if ( m_rowLabelWin
)
1339 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1343 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1346 m_labelTextColour
= wxColour( _T("BLACK") );
1348 // TODO: something better than this ?
1350 m_labelFont
= this->GetFont();
1351 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1353 m_rowLabelHorizAlign
= wxLEFT
;
1354 m_rowLabelVertAlign
= wxCENTRE
;
1356 m_colLabelHorizAlign
= wxCENTRE
;
1357 m_colLabelVertAlign
= wxTOP
;
1359 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1360 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1362 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1363 m_defaultRowHeight
+= 8;
1365 m_defaultRowHeight
+= 4;
1368 m_rowHeights
.Alloc( m_numRows
);
1369 m_rowBottoms
.Alloc( m_numRows
);
1371 for ( i
= 0; i
< m_numRows
; i
++ )
1373 m_rowHeights
.Add( m_defaultRowHeight
);
1374 rowBottom
+= m_defaultRowHeight
;
1375 m_rowBottoms
.Add( rowBottom
);
1378 m_colWidths
.Alloc( m_numCols
);
1379 m_colRights
.Alloc( m_numCols
);
1381 for ( i
= 0; i
< m_numCols
; i
++ )
1383 m_colWidths
.Add( m_defaultColWidth
);
1384 colRight
+= m_defaultColWidth
;
1385 m_colRights
.Add( colRight
);
1388 // TODO: improve this by using wxSystemSettings?
1390 m_defaultCellFont
= GetFont();
1392 m_defaultCellHAlign
= wxLEFT
;
1393 m_defaultCellVAlign
= wxTOP
;
1395 m_defaultRenderer
= (wxGridCellRenderer
*)NULL
;
1397 m_gridLineColour
= wxColour( 128, 128, 255 );
1398 m_gridLinesEnabled
= TRUE
;
1400 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1401 m_winCapture
= (wxWindow
*)NULL
;
1403 m_dragRowOrCol
= -1;
1404 m_isDragging
= FALSE
;
1406 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1407 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1409 m_currentCellCoords
= wxGridNoCellCoords
;
1411 m_selectedTopLeft
= wxGridNoCellCoords
;
1412 m_selectedBottomRight
= wxGridNoCellCoords
;
1414 m_editable
= TRUE
; // default for whole grid
1416 m_inOnKeyDown
= FALSE
;
1419 // TODO: extend this to other types of controls
1421 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1428 #if defined(__WXMSW__)
1429 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1433 m_cellEditCtrl
->Show( FALSE
);
1434 m_cellEditCtrlEnabled
= TRUE
;
1435 m_editCtrlType
= wxGRID_TEXTCTRL
;
1439 void wxGrid::CalcDimensions()
1442 GetClientSize( &cw
, &ch
);
1444 if ( m_numRows
> 0 && m_numCols
> 0 )
1446 int right
= m_colRights
[ m_numCols
-1 ] + 50;
1447 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 50;
1449 // TODO: restore the scroll position that we had before sizing
1452 GetViewStart( &x
, &y
);
1453 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1454 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1460 void wxGrid::CalcWindowSizes()
1463 GetClientSize( &cw
, &ch
);
1465 if ( m_cornerLabelWin
->IsShown() )
1466 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
1468 if ( m_colLabelWin
->IsShown() )
1469 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
1471 if ( m_rowLabelWin
->IsShown() )
1472 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
1474 if ( m_gridWin
->IsShown() )
1475 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
1479 // this is called when the grid table sends a message to say that it
1480 // has been redimensioned
1482 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1486 switch ( msg
.GetId() )
1488 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1490 size_t pos
= msg
.GetCommandInt();
1491 int numRows
= msg
.GetCommandInt2();
1492 for ( i
= 0; i
< numRows
; i
++ )
1494 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1495 m_rowBottoms
.Insert( 0, pos
);
1497 m_numRows
+= numRows
;
1500 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1502 for ( i
= pos
; i
< m_numRows
; i
++ )
1504 bottom
+= m_rowHeights
[i
];
1505 m_rowBottoms
[i
] = bottom
;
1511 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1513 int numRows
= msg
.GetCommandInt();
1514 for ( i
= 0; i
< numRows
; i
++ )
1516 m_rowHeights
.Add( m_defaultRowHeight
);
1517 m_rowBottoms
.Add( 0 );
1520 int oldNumRows
= m_numRows
;
1521 m_numRows
+= numRows
;
1524 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1526 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1528 bottom
+= m_rowHeights
[i
];
1529 m_rowBottoms
[i
] = bottom
;
1535 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1537 size_t pos
= msg
.GetCommandInt();
1538 int numRows
= msg
.GetCommandInt2();
1539 for ( i
= 0; i
< numRows
; i
++ )
1541 m_rowHeights
.Remove( pos
);
1542 m_rowBottoms
.Remove( pos
);
1544 m_numRows
-= numRows
;
1549 m_colWidths
.Clear();
1550 m_colRights
.Clear();
1551 m_currentCellCoords
= wxGridNoCellCoords
;
1555 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1556 m_currentCellCoords
.Set( 0, 0 );
1559 for ( i
= 0; i
< m_numRows
; i
++ )
1561 h
+= m_rowHeights
[i
];
1562 m_rowBottoms
[i
] = h
;
1570 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1572 size_t pos
= msg
.GetCommandInt();
1573 int numCols
= msg
.GetCommandInt2();
1574 for ( i
= 0; i
< numCols
; i
++ )
1576 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1577 m_colRights
.Insert( 0, pos
);
1579 m_numCols
+= numCols
;
1582 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1584 for ( i
= pos
; i
< m_numCols
; i
++ )
1586 right
+= m_colWidths
[i
];
1587 m_colRights
[i
] = right
;
1593 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1595 int numCols
= msg
.GetCommandInt();
1596 for ( i
= 0; i
< numCols
; i
++ )
1598 m_colWidths
.Add( m_defaultColWidth
);
1599 m_colRights
.Add( 0 );
1602 int oldNumCols
= m_numCols
;
1603 m_numCols
+= numCols
;
1606 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1608 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1610 right
+= m_colWidths
[i
];
1611 m_colRights
[i
] = right
;
1617 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1619 size_t pos
= msg
.GetCommandInt();
1620 int numCols
= msg
.GetCommandInt2();
1621 for ( i
= 0; i
< numCols
; i
++ )
1623 m_colWidths
.Remove( pos
);
1624 m_colRights
.Remove( pos
);
1626 m_numCols
-= numCols
;
1630 #if 0 // leave the row alone here so that AppendCols will work subsequently
1632 m_rowHeights
.Clear();
1633 m_rowBottoms
.Clear();
1635 m_currentCellCoords
= wxGridNoCellCoords
;
1639 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1640 m_currentCellCoords
.Set( 0, 0 );
1643 for ( i
= 0; i
< m_numCols
; i
++ )
1645 w
+= m_colWidths
[i
];
1658 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
1660 wxRegionIterator
iter( reg
);
1663 m_rowLabelsExposed
.Empty();
1670 // TODO: remove this when we can...
1671 // There is a bug in wxMotif that gives garbage update
1672 // rectangles if you jump-scroll a long way by clicking the
1673 // scrollbar with middle button. This is a work-around
1675 #if defined(__WXMOTIF__)
1677 m_gridWin
->GetClientSize( &cw
, &ch
);
1678 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1679 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1682 // logical bounds of update region
1685 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1686 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1688 // find the row labels within these bounds
1692 for ( row
= 0; row
< m_numRows
; row
++ )
1694 if ( m_rowBottoms
[row
] < top
) continue;
1696 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1697 if ( rowTop
> bottom
) break;
1699 m_rowLabelsExposed
.Add( row
);
1707 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
1709 wxRegionIterator
iter( reg
);
1712 m_colLabelsExposed
.Empty();
1719 // TODO: remove this when we can...
1720 // There is a bug in wxMotif that gives garbage update
1721 // rectangles if you jump-scroll a long way by clicking the
1722 // scrollbar with middle button. This is a work-around
1724 #if defined(__WXMOTIF__)
1726 m_gridWin
->GetClientSize( &cw
, &ch
);
1727 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1728 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1731 // logical bounds of update region
1734 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1735 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1737 // find the cells within these bounds
1741 for ( col
= 0; col
< m_numCols
; col
++ )
1743 if ( m_colRights
[col
] < left
) continue;
1745 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1746 if ( colLeft
> right
) break;
1748 m_colLabelsExposed
.Add( col
);
1756 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
1758 wxRegionIterator
iter( reg
);
1761 m_cellsExposed
.Empty();
1762 m_rowsExposed
.Empty();
1763 m_colsExposed
.Empty();
1765 int left
, top
, right
, bottom
;
1770 // TODO: remove this when we can...
1771 // There is a bug in wxMotif that gives garbage update
1772 // rectangles if you jump-scroll a long way by clicking the
1773 // scrollbar with middle button. This is a work-around
1775 #if defined(__WXMOTIF__)
1777 m_gridWin
->GetClientSize( &cw
, &ch
);
1778 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1779 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1780 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1781 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1784 // logical bounds of update region
1786 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1787 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1789 // find the cells within these bounds
1792 int colLeft
, rowTop
;
1793 for ( row
= 0; row
< m_numRows
; row
++ )
1795 if ( m_rowBottoms
[row
] < top
) continue;
1797 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1798 if ( rowTop
> bottom
) break;
1800 m_rowsExposed
.Add( row
);
1802 for ( col
= 0; col
< m_numCols
; col
++ )
1804 if ( m_colRights
[col
] < left
) continue;
1806 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1807 if ( colLeft
> right
) break;
1809 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1810 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1819 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1822 wxPoint
pos( event
.GetPosition() );
1823 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1825 if ( event
.Dragging() )
1827 m_isDragging
= TRUE
;
1829 if ( event
.LeftIsDown() )
1831 switch( m_cursorMode
)
1833 case WXGRID_CURSOR_RESIZE_ROW
:
1835 int cw
, ch
, left
, dummy
;
1836 m_gridWin
->GetClientSize( &cw
, &ch
);
1837 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1839 wxClientDC
dc( m_gridWin
);
1841 dc
.SetLogicalFunction(wxINVERT
);
1842 if ( m_dragLastPos
>= 0 )
1844 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1846 dc
.DrawLine( left
, y
, left
+cw
, y
);
1851 case WXGRID_CURSOR_SELECT_ROW
:
1852 if ( (row
= YToRow( y
)) >= 0 &&
1853 !IsInSelection( row
, 0 ) )
1855 SelectRow( row
, TRUE
);
1858 // default label to suppress warnings about "enumeration value
1859 // 'xxx' not handled in switch
1867 m_isDragging
= FALSE
;
1870 // ------------ Entering or leaving the window
1872 if ( event
.Entering() || event
.Leaving() )
1874 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
1878 // ------------ Left button pressed
1880 else if ( event
.LeftDown() )
1882 // don't send a label click event for a hit on the
1883 // edge of the row label - this is probably the user
1884 // wanting to resize the row
1886 if ( YToEdgeOfRow(y
) < 0 )
1890 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
1892 SelectRow( row
, event
.ShiftDown() );
1893 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
1898 // starting to drag-resize a row
1900 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
1905 // ------------ Left double click
1907 else if (event
.LeftDClick() )
1909 if ( YToEdgeOfRow(y
) < 0 )
1912 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
1917 // ------------ Left button released
1919 else if ( event
.LeftUp() )
1921 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
1923 DoEndDragResizeRow();
1925 // Note: we are ending the event *after* doing
1926 // default processing in this case
1928 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1931 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
1936 // ------------ Right button down
1938 else if ( event
.RightDown() )
1941 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1943 // no default action at the moment
1948 // ------------ Right double click
1950 else if ( event
.RightDClick() )
1953 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1955 // no default action at the moment
1960 // ------------ No buttons down and mouse moving
1962 else if ( event
.Moving() )
1964 m_dragRowOrCol
= YToEdgeOfRow( y
);
1965 if ( m_dragRowOrCol
>= 0 )
1967 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1969 // don't capture the mouse yet
1970 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
1973 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1975 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
1981 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
1984 wxPoint
pos( event
.GetPosition() );
1985 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1987 if ( event
.Dragging() )
1989 m_isDragging
= TRUE
;
1991 if ( event
.LeftIsDown() )
1993 switch( m_cursorMode
)
1995 case WXGRID_CURSOR_RESIZE_COL
:
1997 int cw
, ch
, dummy
, top
;
1998 m_gridWin
->GetClientSize( &cw
, &ch
);
1999 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2001 wxClientDC
dc( m_gridWin
);
2003 dc
.SetLogicalFunction(wxINVERT
);
2004 if ( m_dragLastPos
>= 0 )
2006 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2008 dc
.DrawLine( x
, top
, x
, top
+ch
);
2013 case WXGRID_CURSOR_SELECT_COL
:
2014 if ( (col
= XToCol( x
)) >= 0 &&
2015 !IsInSelection( 0, col
) )
2017 SelectCol( col
, TRUE
);
2020 // default label to suppress warnings about "enumeration value
2021 // 'xxx' not handled in switch
2029 m_isDragging
= FALSE
;
2032 // ------------ Entering or leaving the window
2034 if ( event
.Entering() || event
.Leaving() )
2036 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2040 // ------------ Left button pressed
2042 else if ( event
.LeftDown() )
2044 // don't send a label click event for a hit on the
2045 // edge of the col label - this is probably the user
2046 // wanting to resize the col
2048 if ( XToEdgeOfCol(x
) < 0 )
2052 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
2054 SelectCol( col
, event
.ShiftDown() );
2055 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
2060 // starting to drag-resize a col
2062 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
2067 // ------------ Left double click
2069 if ( event
.LeftDClick() )
2071 if ( XToEdgeOfCol(x
) < 0 )
2074 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
2079 // ------------ Left button released
2081 else if ( event
.LeftUp() )
2083 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2085 DoEndDragResizeCol();
2087 // Note: we are ending the event *after* doing
2088 // default processing in this case
2090 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2093 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
2098 // ------------ Right button down
2100 else if ( event
.RightDown() )
2103 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
2105 // no default action at the moment
2110 // ------------ Right double click
2112 else if ( event
.RightDClick() )
2115 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
2117 // no default action at the moment
2122 // ------------ No buttons down and mouse moving
2124 else if ( event
.Moving() )
2126 m_dragRowOrCol
= XToEdgeOfCol( x
);
2127 if ( m_dragRowOrCol
>= 0 )
2129 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2131 // don't capture the cursor yet
2132 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
2135 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2137 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
2143 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
2145 if ( event
.LeftDown() )
2147 // indicate corner label by having both row and
2150 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
2156 else if ( event
.LeftDClick() )
2158 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
2161 else if ( event
.RightDown() )
2163 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
2165 // no default action at the moment
2169 else if ( event
.RightDClick() )
2171 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
2173 // no default action at the moment
2178 void wxGrid::ChangeCursorMode(CursorMode mode
,
2183 static const wxChar
*cursorModes
[] =
2192 wxLogTrace(_T("grid"),
2193 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
2194 win
== m_colLabelWin
? _T("colLabelWin")
2195 : win
? _T("rowLabelWin")
2197 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
2198 #endif // __WXDEBUG__
2200 if ( mode
== m_cursorMode
)
2205 // by default use the grid itself
2211 m_winCapture
->ReleaseMouse();
2212 m_winCapture
= (wxWindow
*)NULL
;
2215 m_cursorMode
= mode
;
2217 switch ( m_cursorMode
)
2219 case WXGRID_CURSOR_RESIZE_ROW
:
2220 win
->SetCursor( m_rowResizeCursor
);
2223 case WXGRID_CURSOR_RESIZE_COL
:
2224 win
->SetCursor( m_colResizeCursor
);
2228 win
->SetCursor( *wxSTANDARD_CURSOR
);
2231 // we need to capture mouse when resizing
2232 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
2233 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
2235 if ( captureMouse
&& resize
)
2237 win
->CaptureMouse();
2242 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
2245 wxPoint
pos( event
.GetPosition() );
2246 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2248 wxGridCellCoords coords
;
2249 XYToCell( x
, y
, coords
);
2251 if ( event
.Dragging() )
2253 m_isDragging
= TRUE
;
2254 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2256 // Hide the edit control, so it
2257 // won't interfer with drag-shrinking.
2258 if ( IsCellEditControlEnabled() )
2259 HideCellEditControl();
2260 if ( coords
!= wxGridNoCellCoords
)
2262 if ( !IsSelection() )
2264 SelectBlock( coords
, coords
);
2268 SelectBlock( m_currentCellCoords
, coords
);
2272 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2274 int cw
, ch
, left
, dummy
;
2275 m_gridWin
->GetClientSize( &cw
, &ch
);
2276 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2278 wxClientDC
dc( m_gridWin
);
2280 dc
.SetLogicalFunction(wxINVERT
);
2281 if ( m_dragLastPos
>= 0 )
2283 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2285 dc
.DrawLine( left
, y
, left
+cw
, y
);
2288 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2290 int cw
, ch
, dummy
, top
;
2291 m_gridWin
->GetClientSize( &cw
, &ch
);
2292 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2294 wxClientDC
dc( m_gridWin
);
2296 dc
.SetLogicalFunction(wxINVERT
);
2297 if ( m_dragLastPos
>= 0 )
2299 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2301 dc
.DrawLine( x
, top
, x
, top
+ch
);
2308 m_isDragging
= FALSE
;
2310 if ( coords
!= wxGridNoCellCoords
)
2312 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
2313 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
2316 if ( event
.Entering() || event
.Leaving() )
2318 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2319 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
2324 // ------------ Left button pressed
2326 if ( event
.LeftDown() )
2328 if ( event
.ShiftDown() )
2330 SelectBlock( m_currentCellCoords
, coords
);
2332 else if ( XToEdgeOfCol(x
) < 0 &&
2333 YToEdgeOfRow(y
) < 0 )
2335 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
2340 MakeCellVisible( coords
);
2341 SetCurrentCell( coords
);
2347 // ------------ Left double click
2349 else if ( event
.LeftDClick() )
2351 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
2353 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
2361 // ------------ Left button released
2363 else if ( event
.LeftUp() )
2365 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2367 if ( IsSelection() )
2369 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
2372 // Show the edit control, if it has
2373 // been hidden for drag-shrinking.
2374 if ( IsCellEditControlEnabled() )
2375 ShowCellEditControl();
2377 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2379 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2380 DoEndDragResizeRow();
2382 // Note: we are ending the event *after* doing
2383 // default processing in this case
2385 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
2387 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
2389 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2390 DoEndDragResizeCol();
2392 // Note: we are ending the event *after* doing
2393 // default processing in this case
2395 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
2402 // ------------ Right button down
2404 else if ( event
.RightDown() )
2406 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
2411 // no default action at the moment
2416 // ------------ Right double click
2418 else if ( event
.RightDClick() )
2420 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2425 // no default action at the moment
2429 // ------------ Moving and no button action
2431 else if ( event
.Moving() && !event
.IsButton() )
2433 int dragRow
= YToEdgeOfRow( y
);
2434 int dragCol
= XToEdgeOfCol( x
);
2436 // Dragging on the corner of a cell to resize in both
2437 // directions is not implemented yet...
2439 if ( dragRow
>= 0 && dragCol
>= 0 )
2441 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2447 m_dragRowOrCol
= dragRow
;
2449 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2451 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
2459 m_dragRowOrCol
= dragCol
;
2461 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
2463 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
2469 // Neither on a row or col edge
2471 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
2473 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
2480 void wxGrid::DoEndDragResizeRow()
2482 if ( m_dragLastPos
>= 0 )
2484 // erase the last line and resize the row
2486 int cw
, ch
, left
, dummy
;
2487 m_gridWin
->GetClientSize( &cw
, &ch
);
2488 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2490 wxClientDC
dc( m_gridWin
);
2492 dc
.SetLogicalFunction( wxINVERT
);
2493 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2494 HideCellEditControl();
2496 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
2497 SetRowSize( m_dragRowOrCol
,
2498 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
2500 if ( !GetBatchCount() )
2502 // Only needed to get the correct rect.y:
2503 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
2505 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
2506 rect
.width
= m_rowLabelWidth
;
2507 rect
.height
= ch
- rect
.y
;
2508 m_rowLabelWin
->Refresh( TRUE
, &rect
);
2510 m_gridWin
->Refresh( FALSE
, &rect
);
2513 ShowCellEditControl();
2518 void wxGrid::DoEndDragResizeCol()
2520 if ( m_dragLastPos
>= 0 )
2522 // erase the last line and resize the col
2524 int cw
, ch
, dummy
, top
;
2525 m_gridWin
->GetClientSize( &cw
, &ch
);
2526 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
2528 wxClientDC
dc( m_gridWin
);
2530 dc
.SetLogicalFunction( wxINVERT
);
2531 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
2532 HideCellEditControl();
2534 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
2535 SetColSize( m_dragRowOrCol
,
2536 wxMax( m_dragLastPos
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
2538 if ( !GetBatchCount() )
2540 // Only needed to get the correct rect.x:
2541 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
2543 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
2544 rect
.width
= cw
- rect
.x
;
2545 rect
.height
= m_colLabelHeight
;
2546 m_colLabelWin
->Refresh( TRUE
, &rect
);
2548 m_gridWin
->Refresh( FALSE
, &rect
);
2551 ShowCellEditControl();
2558 // ------ interaction with data model
2560 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
2562 switch ( msg
.GetId() )
2564 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
2565 return GetModelValues();
2567 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
2568 return SetModelValues();
2570 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2571 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2572 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2573 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2574 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2575 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2576 return Redimension( msg
);
2585 // The behaviour of this function depends on the grid table class
2586 // Clear() function. For the default wxGridStringTable class the
2587 // behavious is to replace all cell contents with wxEmptyString but
2588 // not to change the number of rows or cols.
2590 void wxGrid::ClearGrid()
2595 SetEditControlValue();
2596 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2601 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2603 // TODO: something with updateLabels flag
2607 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2613 bool ok
= m_table
->InsertRows( pos
, numRows
);
2615 // the table will have sent the results of the insert row
2616 // operation to this view object as a grid table message
2620 if ( m_numCols
== 0 )
2622 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2624 // TODO: perhaps instead of appending the default number of cols
2625 // we should remember what the last non-zero number of cols was ?
2629 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2631 // if we have just inserted cols into an empty grid the current
2632 // cell will be undefined...
2634 SetCurrentCell( 0, 0 );
2638 if ( !GetBatchCount() ) Refresh();
2641 SetEditControlValue();
2651 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2653 // TODO: something with updateLabels flag
2657 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2661 if ( m_table
&& m_table
->AppendRows( numRows
) )
2663 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2665 // if we have just inserted cols into an empty grid the current
2666 // cell will be undefined...
2668 SetCurrentCell( 0, 0 );
2671 // the table will have sent the results of the append row
2672 // operation to this view object as a grid table message
2675 if ( !GetBatchCount() ) Refresh();
2685 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2687 // TODO: something with updateLabels flag
2691 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2695 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2697 // the table will have sent the results of the delete row
2698 // operation to this view object as a grid table message
2700 if ( m_numRows
> 0 )
2701 SetEditControlValue();
2703 HideCellEditControl();
2706 if ( !GetBatchCount() ) Refresh();
2716 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2718 // TODO: something with updateLabels flag
2722 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2728 HideCellEditControl();
2729 bool ok
= m_table
->InsertCols( pos
, numCols
);
2731 // the table will have sent the results of the insert col
2732 // operation to this view object as a grid table message
2736 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2738 // if we have just inserted cols into an empty grid the current
2739 // cell will be undefined...
2741 SetCurrentCell( 0, 0 );
2745 if ( !GetBatchCount() ) Refresh();
2748 SetEditControlValue();
2758 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2760 // TODO: something with updateLabels flag
2764 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2768 if ( m_table
&& m_table
->AppendCols( numCols
) )
2770 // the table will have sent the results of the append col
2771 // operation to this view object as a grid table message
2773 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2775 // if we have just inserted cols into an empty grid the current
2776 // cell will be undefined...
2778 SetCurrentCell( 0, 0 );
2782 if ( !GetBatchCount() ) Refresh();
2792 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2794 // TODO: something with updateLabels flag
2798 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2802 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2804 // the table will have sent the results of the delete col
2805 // operation to this view object as a grid table message
2807 if ( m_numCols
> 0 )
2808 SetEditControlValue();
2810 HideCellEditControl();
2813 if ( !GetBatchCount() ) Refresh();
2825 // ----- event handlers
2828 // Generate a grid event based on a mouse event and
2829 // return the result of ProcessEvent()
2831 bool wxGrid::SendEvent( const wxEventType type
,
2833 wxMouseEvent
& mouseEv
)
2835 if ( type
== EVT_GRID_ROW_SIZE
||
2836 type
== EVT_GRID_COL_SIZE
)
2838 int rowOrCol
= (row
== -1 ? col
: row
);
2840 wxGridSizeEvent
gridEvt( GetId(),
2844 mouseEv
.GetX(), mouseEv
.GetY(),
2845 mouseEv
.ControlDown(),
2846 mouseEv
.ShiftDown(),
2848 mouseEv
.MetaDown() );
2850 return GetEventHandler()->ProcessEvent(gridEvt
);
2852 else if ( type
== EVT_GRID_RANGE_SELECT
)
2854 wxGridRangeSelectEvent
gridEvt( GetId(),
2858 m_selectedBottomRight
,
2859 mouseEv
.ControlDown(),
2860 mouseEv
.ShiftDown(),
2862 mouseEv
.MetaDown() );
2864 return GetEventHandler()->ProcessEvent(gridEvt
);
2868 wxGridEvent
gridEvt( GetId(),
2872 mouseEv
.GetX(), mouseEv
.GetY(),
2873 mouseEv
.ControlDown(),
2874 mouseEv
.ShiftDown(),
2876 mouseEv
.MetaDown() );
2878 return GetEventHandler()->ProcessEvent(gridEvt
);
2883 // Generate a grid event of specified type and return the result
2884 // of ProcessEvent().
2886 bool wxGrid::SendEvent( const wxEventType type
,
2889 if ( type
== EVT_GRID_ROW_SIZE
||
2890 type
== EVT_GRID_COL_SIZE
)
2892 int rowOrCol
= (row
== -1 ? col
: row
);
2894 wxGridSizeEvent
gridEvt( GetId(),
2899 return GetEventHandler()->ProcessEvent(gridEvt
);
2903 wxGridEvent
gridEvt( GetId(),
2908 return GetEventHandler()->ProcessEvent(gridEvt
);
2913 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2915 wxPaintDC
dc( this );
2917 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2918 m_numRows
&& m_numCols
)
2920 m_currentCellCoords
.Set(0, 0);
2921 SetEditControlValue();
2922 ShowCellEditControl();
2929 // This is just here to make sure that CalcDimensions gets called when
2930 // the grid view is resized... then the size event is skipped to allow
2931 // the box sizers to handle everything
2933 void wxGrid::OnSize( wxSizeEvent
& event
)
2940 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2942 if ( m_inOnKeyDown
)
2944 // shouldn't be here - we are going round in circles...
2946 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while alread active") );
2949 m_inOnKeyDown
= TRUE
;
2951 // propagate the event up and see if it gets processed
2953 wxWindow
*parent
= GetParent();
2954 wxKeyEvent
keyEvt( event
);
2955 keyEvt
.SetEventObject( parent
);
2957 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2959 // try local handlers
2961 switch ( event
.KeyCode() )
2964 if ( event
.ControlDown() )
2966 MoveCursorUpBlock();
2975 if ( event
.ControlDown() )
2977 MoveCursorDownBlock();
2986 if ( event
.ControlDown() )
2988 MoveCursorLeftBlock();
2997 if ( event
.ControlDown() )
2999 MoveCursorRightBlock();
3008 if ( !IsEditable() )
3019 if ( event
.ControlDown() )
3021 event
.Skip(); // to let the edit control have the return
3030 if ( event
.ControlDown() )
3032 MakeCellVisible( 0, 0 );
3033 SetCurrentCell( 0, 0 );
3042 if ( event
.ControlDown() )
3044 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
3045 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
3062 // now try the cell edit control
3064 if ( IsCellEditControlEnabled() )
3066 event
.SetEventObject( m_cellEditCtrl
);
3067 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
3073 m_inOnKeyDown
= FALSE
;
3077 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
3079 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
3081 // the event has been intercepted - do nothing
3086 m_currentCellCoords
!= wxGridNoCellCoords
)
3088 HideCellEditControl();
3089 SaveEditControlValue();
3092 m_currentCellCoords
= coords
;
3094 SetEditControlValue();
3098 ShowCellEditControl();
3100 if ( IsSelection() )
3102 wxRect
r( SelectionToDeviceRect() );
3104 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
3111 // ------ functions to get/send data (see also public functions)
3114 bool wxGrid::GetModelValues()
3118 // all we need to do is repaint the grid
3120 m_gridWin
->Refresh();
3128 bool wxGrid::SetModelValues()
3134 for ( row
= 0; row
< m_numRows
; row
++ )
3136 for ( col
= 0; col
< m_numCols
; col
++ )
3138 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
3150 // Note - this function only draws cells that are in the list of
3151 // exposed cells (usually set from the update region by
3152 // CalcExposedCells)
3154 void wxGrid::DrawGridCellArea( wxDC
& dc
)
3156 if ( !m_numRows
|| !m_numCols
) return;
3159 size_t numCells
= m_cellsExposed
.GetCount();
3161 for ( i
= 0; i
< numCells
; i
++ )
3163 DrawCell( dc
, m_cellsExposed
[i
] );
3168 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
3170 int row
= coords
.GetRow();
3171 int col
= coords
.GetCol();
3173 if ( m_colWidths
[col
] <= 0 || m_rowHeights
[row
] <= 0 )
3176 // we draw the cell border ourselves
3177 #if !WXGRID_DRAW_LINES
3178 if ( m_gridLinesEnabled
)
3179 DrawCellBorder( dc
, coords
);
3182 // but all the rest is drawn by the cell renderer and hence may be
3184 wxGridCellRenderer
*renderer
= GetCellRenderer(row
, col
);
3186 rect
.x
= m_colRights
[col
] - m_colWidths
[col
] + 1;
3187 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
] + 1;
3188 rect
.width
= m_colWidths
[col
] - 1;
3189 rect
.height
= m_rowHeights
[row
] - 1;
3191 renderer
->Draw(*this, dc
, rect
, row
, col
, IsInSelection(coords
));
3194 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
3196 if ( m_colWidths
[coords
.GetCol()] <=0 ||
3197 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
3199 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3200 int row
= coords
.GetRow();
3201 int col
= coords
.GetCol();
3203 // right hand border
3205 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
3206 m_colRights
[col
], m_rowBottoms
[row
] );
3210 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
3211 m_colRights
[col
], m_rowBottoms
[row
] );
3215 // TODO: remove this ???
3216 // This is used to redraw all grid lines e.g. when the grid line colour
3219 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
3221 if ( !m_gridLinesEnabled
||
3223 !m_numCols
) return;
3225 int top
, bottom
, left
, right
;
3229 m_gridWin
->GetClientSize(&cw
, &ch
);
3231 // virtual coords of visible area
3233 CalcUnscrolledPosition( 0, 0, &left
, &top
);
3234 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
3238 reg
.GetBox(x
, y
, w
, h
);
3239 CalcUnscrolledPosition( x
, y
, &left
, &top
);
3240 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
3243 // avoid drawing grid lines past the last row and col
3245 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
3246 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
3248 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
3250 // horizontal grid lines
3253 for ( i
= 0; i
< m_numRows
; i
++ )
3255 if ( m_rowBottoms
[i
] > bottom
)
3259 else if ( m_rowBottoms
[i
] >= top
)
3261 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
3266 // vertical grid lines
3268 for ( i
= 0; i
< m_numCols
; i
++ )
3270 if ( m_colRights
[i
] > right
)
3274 else if ( m_colRights
[i
] >= left
)
3276 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
3282 void wxGrid::DrawRowLabels( wxDC
& dc
)
3284 if ( !m_numRows
|| !m_numCols
) return;
3287 size_t numLabels
= m_rowLabelsExposed
.GetCount();
3289 for ( i
= 0; i
< numLabels
; i
++ )
3291 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
3296 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
3298 if ( m_rowHeights
[row
] <= 0 ) return;
3300 int rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3302 dc
.SetPen( *wxBLACK_PEN
);
3303 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
3304 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3306 dc
.DrawLine( 0, m_rowBottoms
[row
]-1,
3307 m_rowLabelWidth
-1, m_rowBottoms
[row
]-1 );
3309 dc
.SetPen( *wxWHITE_PEN
);
3310 dc
.DrawLine( 0, rowTop
, 0, m_rowBottoms
[row
]-1 );
3311 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
3313 dc
.SetBackgroundMode( wxTRANSPARENT
);
3314 dc
.SetTextForeground( GetLabelTextColour() );
3315 dc
.SetFont( GetLabelFont() );
3318 GetRowLabelAlignment( &hAlign
, &vAlign
);
3322 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
3323 rect
.SetWidth( m_rowLabelWidth
- 4 );
3324 rect
.SetHeight( m_rowHeights
[row
] - 4 );
3325 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
3329 void wxGrid::DrawColLabels( wxDC
& dc
)
3331 if ( !m_numRows
|| !m_numCols
) return;
3334 size_t numLabels
= m_colLabelsExposed
.GetCount();
3336 for ( i
= 0; i
< numLabels
; i
++ )
3338 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
3343 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
3345 if ( m_colWidths
[col
] <= 0 ) return;
3347 int colLeft
= m_colRights
[col
] - m_colWidths
[col
];
3349 dc
.SetPen( *wxBLACK_PEN
);
3350 dc
.DrawLine( m_colRights
[col
]-1, 0,
3351 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3353 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
3354 m_colRights
[col
]-1, m_colLabelHeight
-1 );
3356 dc
.SetPen( *wxWHITE_PEN
);
3357 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
3358 dc
.DrawLine( colLeft
, 0, m_colRights
[col
]-1, 0 );
3360 dc
.SetBackgroundMode( wxTRANSPARENT
);
3361 dc
.SetTextForeground( GetLabelTextColour() );
3362 dc
.SetFont( GetLabelFont() );
3364 dc
.SetBackgroundMode( wxTRANSPARENT
);
3365 dc
.SetTextForeground( GetLabelTextColour() );
3366 dc
.SetFont( GetLabelFont() );
3369 GetColLabelAlignment( &hAlign
, &vAlign
);
3372 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
3374 rect
.SetWidth( m_colWidths
[col
] - 4 );
3375 rect
.SetHeight( m_colLabelHeight
- 4 );
3376 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
3380 void wxGrid::DrawTextRectangle( wxDC
& dc
,
3381 const wxString
& value
,
3386 long textWidth
, textHeight
;
3387 long lineWidth
, lineHeight
;
3388 wxArrayString lines
;
3390 dc
.SetClippingRegion( rect
);
3391 StringToLines( value
, lines
);
3392 if ( lines
.GetCount() )
3394 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
3395 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
3398 switch ( horizAlign
)
3401 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
3405 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
3414 switch ( vertAlign
)
3417 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
3421 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
3430 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
3432 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
3437 dc
.DestroyClippingRegion();
3441 // Split multi line text up into an array of strings. Any existing
3442 // contents of the string array are preserved.
3444 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
3448 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
3449 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
3451 while ( startPos
< (int)tVal
.Length() )
3453 pos
= tVal
.Mid(startPos
).Find( eol
);
3458 else if ( pos
== 0 )
3460 lines
.Add( wxEmptyString
);
3464 lines
.Add( value
.Mid(startPos
, pos
) );
3468 if ( startPos
< (int)value
.Length() )
3470 lines
.Add( value
.Mid( startPos
) );
3475 void wxGrid::GetTextBoxSize( wxDC
& dc
,
3476 wxArrayString
& lines
,
3477 long *width
, long *height
)
3484 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3486 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3487 w
= wxMax( w
, lineW
);
3497 // ------ Edit control functions
3501 void wxGrid::EnableEditing( bool edit
)
3503 // TODO: improve this ?
3505 if ( edit
!= m_editable
)
3509 // TODO: extend this for other edit control types
3511 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3513 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
3519 #if 0 // disabled for the moment - the cell control is always active
3520 void wxGrid::EnableCellEditControl( bool enable
)
3522 if ( m_cellEditCtrl
&&
3523 enable
!= m_cellEditCtrlEnabled
)
3525 m_cellEditCtrlEnabled
= enable
;
3527 if ( m_cellEditCtrlEnabled
)
3529 SetEditControlValue();
3530 ShowCellEditControl();
3534 HideCellEditControl();
3535 SaveEditControlValue();
3542 void wxGrid::ShowCellEditControl()
3546 if ( IsCellEditControlEnabled() )
3548 if ( !IsVisible( m_currentCellCoords
) )
3554 rect
= CellToRect( m_currentCellCoords
);
3556 // convert to scrolled coords
3558 int left
, top
, right
, bottom
;
3559 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3560 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3563 m_gridWin
->GetClientSize( &cw
, &ch
);
3565 // Make the edit control large enough to allow for internal margins
3566 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3569 #if defined(__WXMOTIF__)
3570 if ( m_currentCellCoords
.GetRow() == 0 ||
3571 m_currentCellCoords
.GetCol() == 0 )
3580 if ( m_currentCellCoords
.GetRow() == 0 ||
3581 m_currentCellCoords
.GetCol() == 0 )
3591 #if defined(__WXGTK__)
3594 if (left
!= 0) left_diff
++;
3595 if (top
!= 0) top_diff
++;
3596 rect
.SetLeft( left
+ left_diff
);
3597 rect
.SetTop( top
+ top_diff
);
3598 rect
.SetRight( rect
.GetRight() - left_diff
);
3599 rect
.SetBottom( rect
.GetBottom() - top_diff
);
3601 rect
.SetLeft( wxMax(0, left
- extra
) );
3602 rect
.SetTop( wxMax(0, top
- extra
) );
3603 rect
.SetRight( rect
.GetRight() + 2*extra
);
3604 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3607 m_cellEditCtrl
->SetSize( rect
);
3608 m_cellEditCtrl
->Show( TRUE
);
3610 switch ( m_editCtrlType
)
3612 case wxGRID_TEXTCTRL
:
3613 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3616 case wxGRID_CHECKBOX
:
3617 // TODO: anything ???
3622 // TODO: anything ???
3626 case wxGRID_COMBOBOX
:
3627 // TODO: anything ???
3632 m_cellEditCtrl
->SetFocus();
3638 void wxGrid::HideCellEditControl()
3640 if ( IsCellEditControlEnabled() )
3642 m_cellEditCtrl
->Show( FALSE
);
3647 void wxGrid::SetEditControlValue( const wxString
& value
)
3653 s
= GetCellValue(m_currentCellCoords
);
3657 if ( IsCellEditControlEnabled() )
3659 switch ( m_editCtrlType
)
3661 case wxGRID_TEXTCTRL
:
3662 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3665 case wxGRID_CHECKBOX
:
3666 // TODO: implement this
3671 // TODO: implement this
3675 case wxGRID_COMBOBOX
:
3676 // TODO: implement this
3685 void wxGrid::SaveEditControlValue()
3689 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3691 if ( IsCellEditControlEnabled() )
3693 ctrl
= m_cellEditCtrl
;
3700 bool valueChanged
= FALSE
;
3702 switch ( m_editCtrlType
)
3704 case wxGRID_TEXTCTRL
:
3705 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3706 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3707 SetCellValue( m_currentCellCoords
,
3708 ((wxTextCtrl
*) ctrl
)->GetValue() );
3711 case wxGRID_CHECKBOX
:
3712 // TODO: implement this
3717 // TODO: implement this
3721 case wxGRID_COMBOBOX
:
3722 // TODO: implement this
3729 SendEvent( EVT_GRID_CELL_CHANGE
,
3730 m_currentCellCoords
.GetRow(),
3731 m_currentCellCoords
.GetCol() );
3738 // ------ Grid location functions
3739 // Note that all of these functions work with the logical coordinates of
3740 // grid cells and labels so you will need to convert from device
3741 // coordinates for mouse events etc.
3744 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3746 int row
= YToRow(y
);
3747 int col
= XToCol(x
);
3749 if ( row
== -1 || col
== -1 )
3751 coords
= wxGridNoCellCoords
;
3755 coords
.Set( row
, col
);
3760 int wxGrid::YToRow( int y
)
3764 for ( i
= 0; i
< m_numRows
; i
++ )
3766 if ( y
< m_rowBottoms
[i
] ) return i
;
3773 int wxGrid::XToCol( int x
)
3777 for ( i
= 0; i
< m_numCols
; i
++ )
3779 if ( x
< m_colRights
[i
] ) return i
;
3786 // return the row number that that the y coord is near the edge of, or
3787 // -1 if not near an edge
3789 int wxGrid::YToEdgeOfRow( int y
)
3793 for ( i
= 0; i
< m_numRows
; i
++ )
3795 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3797 d
= abs( y
- m_rowBottoms
[i
] );
3799 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3808 // return the col number that that the x coord is near the edge of, or
3809 // -1 if not near an edge
3811 int wxGrid::XToEdgeOfCol( int x
)
3815 for ( i
= 0; i
< m_numCols
; i
++ )
3817 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3819 d
= abs( x
- m_colRights
[i
] );
3821 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3830 wxRect
wxGrid::CellToRect( int row
, int col
)
3832 wxRect
rect( -1, -1, -1, -1 );
3834 if ( row
>= 0 && row
< m_numRows
&&
3835 col
>= 0 && col
< m_numCols
)
3837 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3838 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3839 rect
.width
= m_colWidths
[col
];
3840 rect
.height
= m_rowHeights
[ row
];
3847 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3849 // get the cell rectangle in logical coords
3851 wxRect
r( CellToRect( row
, col
) );
3853 // convert to device coords
3855 int left
, top
, right
, bottom
;
3856 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3857 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3859 // check against the client area of the grid window
3862 m_gridWin
->GetClientSize( &cw
, &ch
);
3864 if ( wholeCellVisible
)
3866 // is the cell wholly visible ?
3868 return ( left
>= 0 && right
<= cw
&&
3869 top
>= 0 && bottom
<= ch
);
3873 // is the cell partly visible ?
3875 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3876 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3881 // make the specified cell location visible by doing a minimal amount
3884 void wxGrid::MakeCellVisible( int row
, int col
)
3887 int xpos
= -1, ypos
= -1;
3889 if ( row
>= 0 && row
< m_numRows
&&
3890 col
>= 0 && col
< m_numCols
)
3892 // get the cell rectangle in logical coords
3894 wxRect
r( CellToRect( row
, col
) );
3896 // convert to device coords
3898 int left
, top
, right
, bottom
;
3899 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3900 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3903 m_gridWin
->GetClientSize( &cw
, &ch
);
3909 else if ( bottom
> ch
)
3911 int h
= r
.GetHeight();
3913 for ( i
= row
-1; i
>= 0; i
-- )
3915 if ( h
+ m_rowHeights
[i
] > ch
) break;
3917 h
+= m_rowHeights
[i
];
3918 ypos
-= m_rowHeights
[i
];
3921 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
3922 // have rounding errors (this is important, because if we do, we
3923 // might not scroll at all and some cells won't be redrawn)
3924 ypos
+= GRID_SCROLL_LINE
/ 2;
3931 else if ( right
> cw
)
3933 int w
= r
.GetWidth();
3935 for ( i
= col
-1; i
>= 0; i
-- )
3937 if ( w
+ m_colWidths
[i
] > cw
) break;
3939 w
+= m_colWidths
[i
];
3940 xpos
-= m_colWidths
[i
];
3943 // see comment for ypos above
3944 xpos
+= GRID_SCROLL_LINE
/ 2;
3947 if ( xpos
!= -1 || ypos
!= -1 )
3949 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
3950 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
3951 Scroll( xpos
, ypos
);
3959 // ------ Grid cursor movement functions
3962 bool wxGrid::MoveCursorUp()
3964 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3965 m_currentCellCoords
.GetRow() > 0 )
3967 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3968 m_currentCellCoords
.GetCol() );
3970 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3971 m_currentCellCoords
.GetCol() );
3980 bool wxGrid::MoveCursorDown()
3982 // TODO: allow for scrolling
3984 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3985 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3987 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3988 m_currentCellCoords
.GetCol() );
3990 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3991 m_currentCellCoords
.GetCol() );
4000 bool wxGrid::MoveCursorLeft()
4002 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4003 m_currentCellCoords
.GetCol() > 0 )
4005 MakeCellVisible( m_currentCellCoords
.GetRow(),
4006 m_currentCellCoords
.GetCol() - 1 );
4008 SetCurrentCell( m_currentCellCoords
.GetRow(),
4009 m_currentCellCoords
.GetCol() - 1 );
4018 bool wxGrid::MoveCursorRight()
4020 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
4021 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
4023 MakeCellVisible( m_currentCellCoords
.GetRow(),
4024 m_currentCellCoords
.GetCol() + 1 );
4026 SetCurrentCell( m_currentCellCoords
.GetRow(),
4027 m_currentCellCoords
.GetCol() + 1 );
4036 bool wxGrid::MovePageUp()
4038 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4040 int row
= m_currentCellCoords
.GetRow();
4044 m_gridWin
->GetClientSize( &cw
, &ch
);
4046 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4047 int newRow
= YToRow( y
- ch
+ 1 );
4052 else if ( newRow
== row
)
4057 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4058 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4066 bool wxGrid::MovePageDown()
4068 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
4070 int row
= m_currentCellCoords
.GetRow();
4071 if ( row
< m_numRows
)
4074 m_gridWin
->GetClientSize( &cw
, &ch
);
4076 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
4077 int newRow
= YToRow( y
+ ch
);
4080 newRow
= m_numRows
- 1;
4082 else if ( newRow
== row
)
4087 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
4088 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
4096 bool wxGrid::MoveCursorUpBlock()
4099 m_currentCellCoords
!= wxGridNoCellCoords
&&
4100 m_currentCellCoords
.GetRow() > 0 )
4102 int row
= m_currentCellCoords
.GetRow();
4103 int col
= m_currentCellCoords
.GetCol();
4105 if ( m_table
->IsEmptyCell(row
, col
) )
4107 // starting in an empty cell: find the next block of
4113 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4116 else if ( m_table
->IsEmptyCell(row
-1, col
) )
4118 // starting at the top of a block: find the next block
4124 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4129 // starting within a block: find the top of the block
4134 if ( m_table
->IsEmptyCell(row
, col
) )
4142 MakeCellVisible( row
, col
);
4143 SetCurrentCell( row
, col
);
4151 bool wxGrid::MoveCursorDownBlock()
4154 m_currentCellCoords
!= wxGridNoCellCoords
&&
4155 m_currentCellCoords
.GetRow() < m_numRows
-1 )
4157 int row
= m_currentCellCoords
.GetRow();
4158 int col
= m_currentCellCoords
.GetCol();
4160 if ( m_table
->IsEmptyCell(row
, col
) )
4162 // starting in an empty cell: find the next block of
4165 while ( row
< m_numRows
-1 )
4168 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4171 else if ( m_table
->IsEmptyCell(row
+1, col
) )
4173 // starting at the bottom of a block: find the next block
4176 while ( row
< m_numRows
-1 )
4179 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4184 // starting within a block: find the bottom of the block
4186 while ( row
< m_numRows
-1 )
4189 if ( m_table
->IsEmptyCell(row
, col
) )
4197 MakeCellVisible( row
, col
);
4198 SetCurrentCell( row
, col
);
4206 bool wxGrid::MoveCursorLeftBlock()
4209 m_currentCellCoords
!= wxGridNoCellCoords
&&
4210 m_currentCellCoords
.GetCol() > 0 )
4212 int row
= m_currentCellCoords
.GetRow();
4213 int col
= m_currentCellCoords
.GetCol();
4215 if ( m_table
->IsEmptyCell(row
, col
) )
4217 // starting in an empty cell: find the next block of
4223 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4226 else if ( m_table
->IsEmptyCell(row
, col
-1) )
4228 // starting at the left of a block: find the next block
4234 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4239 // starting within a block: find the left of the block
4244 if ( m_table
->IsEmptyCell(row
, col
) )
4252 MakeCellVisible( row
, col
);
4253 SetCurrentCell( row
, col
);
4261 bool wxGrid::MoveCursorRightBlock()
4264 m_currentCellCoords
!= wxGridNoCellCoords
&&
4265 m_currentCellCoords
.GetCol() < m_numCols
-1 )
4267 int row
= m_currentCellCoords
.GetRow();
4268 int col
= m_currentCellCoords
.GetCol();
4270 if ( m_table
->IsEmptyCell(row
, col
) )
4272 // starting in an empty cell: find the next block of
4275 while ( col
< m_numCols
-1 )
4278 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4281 else if ( m_table
->IsEmptyCell(row
, col
+1) )
4283 // starting at the right of a block: find the next block
4286 while ( col
< m_numCols
-1 )
4289 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
4294 // starting within a block: find the right of the block
4296 while ( col
< m_numCols
-1 )
4299 if ( m_table
->IsEmptyCell(row
, col
) )
4307 MakeCellVisible( row
, col
);
4308 SetCurrentCell( row
, col
);
4319 // ------ Label values and formatting
4322 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
4324 *horiz
= m_rowLabelHorizAlign
;
4325 *vert
= m_rowLabelVertAlign
;
4328 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
4330 *horiz
= m_colLabelHorizAlign
;
4331 *vert
= m_colLabelVertAlign
;
4334 wxString
wxGrid::GetRowLabelValue( int row
)
4338 return m_table
->GetRowLabelValue( row
);
4348 wxString
wxGrid::GetColLabelValue( int col
)
4352 return m_table
->GetColLabelValue( col
);
4363 void wxGrid::SetRowLabelSize( int width
)
4365 width
= wxMax( width
, 0 );
4366 if ( width
!= m_rowLabelWidth
)
4370 m_rowLabelWin
->Show( FALSE
);
4371 m_cornerLabelWin
->Show( FALSE
);
4373 else if ( m_rowLabelWidth
== 0 )
4375 m_rowLabelWin
->Show( TRUE
);
4376 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4379 m_rowLabelWidth
= width
;
4386 void wxGrid::SetColLabelSize( int height
)
4388 height
= wxMax( height
, 0 );
4389 if ( height
!= m_colLabelHeight
)
4393 m_colLabelWin
->Show( FALSE
);
4394 m_cornerLabelWin
->Show( FALSE
);
4396 else if ( m_colLabelHeight
== 0 )
4398 m_colLabelWin
->Show( TRUE
);
4399 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
4402 m_colLabelHeight
= height
;
4409 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
4411 if ( m_labelBackgroundColour
!= colour
)
4413 m_labelBackgroundColour
= colour
;
4414 m_rowLabelWin
->SetBackgroundColour( colour
);
4415 m_colLabelWin
->SetBackgroundColour( colour
);
4416 m_cornerLabelWin
->SetBackgroundColour( colour
);
4418 if ( !GetBatchCount() )
4420 m_rowLabelWin
->Refresh();
4421 m_colLabelWin
->Refresh();
4422 m_cornerLabelWin
->Refresh();
4427 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
4429 if ( m_labelTextColour
!= colour
)
4431 m_labelTextColour
= colour
;
4432 if ( !GetBatchCount() )
4434 m_rowLabelWin
->Refresh();
4435 m_colLabelWin
->Refresh();
4440 void wxGrid::SetLabelFont( const wxFont
& font
)
4443 if ( !GetBatchCount() )
4445 m_rowLabelWin
->Refresh();
4446 m_colLabelWin
->Refresh();
4450 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
4452 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4454 m_rowLabelHorizAlign
= horiz
;
4457 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4459 m_rowLabelVertAlign
= vert
;
4462 if ( !GetBatchCount() )
4464 m_rowLabelWin
->Refresh();
4468 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
4470 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
4472 m_colLabelHorizAlign
= horiz
;
4475 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
4477 m_colLabelVertAlign
= vert
;
4480 if ( !GetBatchCount() )
4482 m_colLabelWin
->Refresh();
4486 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
4490 m_table
->SetRowLabelValue( row
, s
);
4491 if ( !GetBatchCount() )
4493 wxRect rect
= CellToRect( row
, 0);
4494 if ( rect
.height
> 0 )
4496 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
4498 rect
.width
= m_rowLabelWidth
;
4499 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4505 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
4509 m_table
->SetColLabelValue( col
, s
);
4510 if ( !GetBatchCount() )
4512 wxRect rect
= CellToRect( 0, col
);
4513 if ( rect
.width
> 0 )
4515 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
4517 rect
.height
= m_colLabelHeight
;
4518 m_colLabelWin
->Refresh( TRUE
, &rect
);
4524 void wxGrid::SetGridLineColour( const wxColour
& colour
)
4526 if ( m_gridLineColour
!= colour
)
4528 m_gridLineColour
= colour
;
4530 wxClientDC
dc( m_gridWin
);
4532 DrawAllGridLines( dc
, wxRegion() );
4536 void wxGrid::EnableGridLines( bool enable
)
4538 if ( enable
!= m_gridLinesEnabled
)
4540 m_gridLinesEnabled
= enable
;
4542 if ( !GetBatchCount() )
4546 wxClientDC
dc( m_gridWin
);
4548 DrawAllGridLines( dc
, wxRegion() );
4552 m_gridWin
->Refresh();
4559 int wxGrid::GetDefaultRowSize()
4561 return m_defaultRowHeight
;
4564 int wxGrid::GetRowSize( int row
)
4566 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
4568 return m_rowHeights
[row
];
4571 int wxGrid::GetDefaultColSize()
4573 return m_defaultColWidth
;
4576 int wxGrid::GetColSize( int col
)
4578 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
4580 return m_colWidths
[col
];
4583 // ============================================================================
4584 // access to the grid attributes: each of them has a default value in the grid
4585 // itself and may be overidden on a per-cell basis
4586 // ============================================================================
4588 // ----------------------------------------------------------------------------
4589 // setting default attributes
4590 // ----------------------------------------------------------------------------
4592 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
4594 m_gridWin
->SetBackgroundColour(col
);
4597 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
4599 m_gridWin
->SetForegroundColour(col
);
4602 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
4604 m_defaultCellHAlign
= horiz
;
4605 m_defaultCellVAlign
= vert
;
4608 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
4610 m_defaultCellFont
= font
;
4613 // ----------------------------------------------------------------------------
4614 // access to the default attrbiutes
4615 // ----------------------------------------------------------------------------
4617 wxColour
wxGrid::GetDefaultCellBackgroundColour()
4619 return m_gridWin
->GetBackgroundColour();
4622 wxColour
wxGrid::GetDefaultCellTextColour()
4624 return m_gridWin
->GetForegroundColour();
4627 wxFont
wxGrid::GetDefaultCellFont()
4629 return m_defaultCellFont
;
4632 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
4635 *horiz
= m_defaultCellHAlign
;
4637 *vert
= m_defaultCellVAlign
;
4640 wxGridCellRenderer
*wxGrid::GetCellRenderer(int row
, int col
)
4642 wxGridCellRenderer
*renderer
= (wxGridCellRenderer
*)NULL
;
4643 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4646 renderer
= attr
->GetRenderer();
4653 if ( !m_defaultRenderer
)
4655 m_defaultRenderer
= new wxGridCellStringRenderer
;
4658 renderer
= m_defaultRenderer
;
4664 // ----------------------------------------------------------------------------
4665 // access to cell attributes
4666 // ----------------------------------------------------------------------------
4668 // TODO VZ: we must cache the attr to allow only retrieveing it once!
4670 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
4672 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4675 if ( attr
&& attr
->HasBackgroundColour() )
4676 colour
= attr
->GetBackgroundColour();
4678 colour
= GetDefaultCellBackgroundColour();
4685 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
4687 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4690 if ( attr
&& attr
->HasTextColour() )
4691 colour
= attr
->GetTextColour();
4693 colour
= GetDefaultCellTextColour();
4700 wxFont
wxGrid::GetCellFont( int row
, int col
)
4702 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4705 if ( attr
&& attr
->HasFont() )
4706 font
= attr
->GetFont();
4708 font
= GetDefaultCellFont();
4715 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
4717 wxGridCellAttr
*attr
= m_table
? m_table
->GetAttr(row
, col
) : NULL
;
4719 if ( attr
&& attr
->HasAlignment() )
4720 attr
->GetAlignment(horiz
, vert
);
4722 GetDefaultCellAlignment(horiz
, vert
);
4727 // ----------------------------------------------------------------------------
4728 // setting cell attributes: this is forwarded to the table
4729 // ----------------------------------------------------------------------------
4731 bool wxGrid::CanHaveAttributes()
4738 if ( !m_table
->GetAttrProvider() )
4740 // use the default attr provider by default
4741 // (another choice would be to just return FALSE thus forcing the user
4743 m_table
->SetAttrProvider(new wxGridCellAttrProvider
);
4749 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
4751 wxGridCellAttr
*attr
= m_table
->GetAttr(row
, col
);
4754 attr
= new wxGridCellAttr
;
4756 // artificially inc the ref count to match DecRef() in caller
4759 m_table
->SetAttr(attr
, row
, col
);
4765 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
4767 if ( CanHaveAttributes() )
4769 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4770 attr
->SetBackgroundColour(colour
);
4775 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
4777 if ( CanHaveAttributes() )
4779 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4780 attr
->SetTextColour(colour
);
4785 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
4787 if ( CanHaveAttributes() )
4789 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4790 attr
->SetFont(font
);
4795 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
4797 if ( CanHaveAttributes() )
4799 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4800 attr
->SetAlignment(horiz
, vert
);
4805 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
4807 if ( CanHaveAttributes() )
4809 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4810 attr
->SetRenderer(renderer
);
4815 // ----------------------------------------------------------------------------
4817 // ----------------------------------------------------------------------------
4819 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
4821 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
4823 if ( resizeExistingRows
)
4827 for ( row
= 0; row
< m_numRows
; row
++ )
4829 m_rowHeights
[row
] = m_defaultRowHeight
;
4830 bottom
+= m_defaultRowHeight
;
4831 m_rowBottoms
[row
] = bottom
;
4837 void wxGrid::SetRowSize( int row
, int height
)
4839 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
4843 int h
= wxMax( 0, height
);
4844 int diff
= h
- m_rowHeights
[row
];
4846 m_rowHeights
[row
] = h
;
4847 for ( i
= row
; i
< m_numRows
; i
++ )
4849 m_rowBottoms
[i
] += diff
;
4854 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4856 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4858 if ( resizeExistingCols
)
4862 for ( col
= 0; col
< m_numCols
; col
++ )
4864 m_colWidths
[col
] = m_defaultColWidth
;
4865 right
+= m_defaultColWidth
;
4866 m_colRights
[col
] = right
;
4872 void wxGrid::SetColSize( int col
, int width
)
4874 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
4878 int w
= wxMax( 0, width
);
4879 int diff
= w
- m_colWidths
[col
];
4880 m_colWidths
[col
] = w
;
4882 for ( i
= col
; i
< m_numCols
; i
++ )
4884 m_colRights
[i
] += diff
;
4891 // ------ cell value accessor functions
4894 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4898 m_table
->SetValue( row
, col
, s
.c_str() );
4899 if ( !GetBatchCount() )
4901 wxClientDC
dc( m_gridWin
);
4903 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4906 #if 0 // TODO: edit in place
4908 if ( m_currentCellCoords
.GetRow() == row
&&
4909 m_currentCellCoords
.GetCol() == col
)
4911 SetEditControlValue( s
);
4920 // ------ Block, row and col selection
4923 void wxGrid::SelectRow( int row
, bool addToSelected
)
4927 if ( IsSelection() && addToSelected
)
4930 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4933 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4934 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4935 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4936 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4940 need_refresh
[0] = TRUE
;
4941 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
4942 wxGridCellCoords ( oldTop
- 1,
4944 m_selectedTopLeft
.SetRow( row
);
4949 need_refresh
[1] = TRUE
;
4950 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
4951 wxGridCellCoords ( oldBottom
,
4954 m_selectedTopLeft
.SetCol( 0 );
4957 if ( oldBottom
< row
)
4959 need_refresh
[2] = TRUE
;
4960 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
4961 wxGridCellCoords ( row
,
4963 m_selectedBottomRight
.SetRow( row
);
4966 if ( oldRight
< m_numCols
- 1 )
4968 need_refresh
[3] = TRUE
;
4969 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4971 wxGridCellCoords ( oldBottom
,
4973 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4976 for (i
= 0; i
< 4; i
++ )
4977 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4978 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
4982 r
= SelectionToDeviceRect();
4984 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
4986 m_selectedTopLeft
.Set( row
, 0 );
4987 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4988 r
= SelectionToDeviceRect();
4989 m_gridWin
->Refresh( FALSE
, &r
);
4992 wxGridRangeSelectEvent
gridEvt( GetId(),
4993 EVT_GRID_RANGE_SELECT
,
4996 m_selectedBottomRight
);
4998 GetEventHandler()->ProcessEvent(gridEvt
);
5002 void wxGrid::SelectCol( int col
, bool addToSelected
)
5004 if ( IsSelection() && addToSelected
)
5007 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5010 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5011 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5012 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5013 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5015 if ( oldLeft
> col
)
5017 need_refresh
[0] = TRUE
;
5018 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
5019 wxGridCellCoords ( m_numRows
- 1,
5021 m_selectedTopLeft
.SetCol( col
);
5026 need_refresh
[1] = TRUE
;
5027 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
5028 wxGridCellCoords ( oldTop
- 1,
5030 m_selectedTopLeft
.SetRow( 0 );
5033 if ( oldRight
< col
)
5035 need_refresh
[2] = TRUE
;
5036 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
5037 wxGridCellCoords ( m_numRows
- 1,
5039 m_selectedBottomRight
.SetCol( col
);
5042 if ( oldBottom
< m_numRows
- 1 )
5044 need_refresh
[3] = TRUE
;
5045 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
5047 wxGridCellCoords ( m_numRows
- 1,
5049 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
5052 for (i
= 0; i
< 4; i
++ )
5053 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5054 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5060 r
= SelectionToDeviceRect();
5062 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
5064 m_selectedTopLeft
.Set( 0, col
);
5065 m_selectedBottomRight
.Set( m_numRows
-1, col
);
5066 r
= SelectionToDeviceRect();
5067 m_gridWin
->Refresh( FALSE
, &r
);
5070 wxGridRangeSelectEvent
gridEvt( GetId(),
5071 EVT_GRID_RANGE_SELECT
,
5074 m_selectedBottomRight
);
5076 GetEventHandler()->ProcessEvent(gridEvt
);
5080 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5083 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5085 if ( topRow
> bottomRow
)
5092 if ( leftCol
> rightCol
)
5099 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5100 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5102 if ( m_selectedTopLeft
!= updateTopLeft
||
5103 m_selectedBottomRight
!= updateBottomRight
)
5105 // Compute two optimal update rectangles:
5106 // Either one rectangle is a real subset of the
5107 // other, or they are (almost) disjoint!
5109 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
5112 // Store intermediate values
5113 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
5114 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
5115 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
5116 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
5118 // Determine the outer/inner coordinates.
5119 if (oldLeft
> leftCol
)
5125 if (oldTop
> topRow
)
5131 if (oldRight
< rightCol
)
5134 oldRight
= rightCol
;
5137 if (oldBottom
< bottomRow
)
5140 oldBottom
= bottomRow
;
5144 // Now, either the stuff marked old is the outer
5145 // rectangle or we don't have a situation where one
5146 // is contained in the other.
5148 if ( oldLeft
< leftCol
)
5150 need_refresh
[0] = TRUE
;
5151 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5153 wxGridCellCoords ( oldBottom
,
5157 if ( oldTop
< topRow
)
5159 need_refresh
[1] = TRUE
;
5160 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5162 wxGridCellCoords ( topRow
- 1,
5166 if ( oldRight
> rightCol
)
5168 need_refresh
[2] = TRUE
;
5169 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5171 wxGridCellCoords ( oldBottom
,
5175 if ( oldBottom
> bottomRow
)
5177 need_refresh
[3] = TRUE
;
5178 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5180 wxGridCellCoords ( oldBottom
,
5186 m_selectedTopLeft
= updateTopLeft
;
5187 m_selectedBottomRight
= updateBottomRight
;
5189 // various Refresh() calls
5190 for (i
= 0; i
< 4; i
++ )
5191 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5192 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5195 // only generate an event if the block is not being selected by
5196 // dragging the mouse (in which case the event will be generated in
5197 // the mouse event handler)
5198 if ( !m_isDragging
)
5200 wxGridRangeSelectEvent
gridEvt( GetId(),
5201 EVT_GRID_RANGE_SELECT
,
5204 m_selectedBottomRight
);
5206 GetEventHandler()->ProcessEvent(gridEvt
);
5210 void wxGrid::SelectAll()
5212 m_selectedTopLeft
.Set( 0, 0 );
5213 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
5215 m_gridWin
->Refresh();
5219 void wxGrid::ClearSelection()
5221 m_selectedTopLeft
= wxGridNoCellCoords
;
5222 m_selectedBottomRight
= wxGridNoCellCoords
;
5226 // This function returns the rectangle that encloses the given block
5227 // in device coords clipped to the client size of the grid window.
5229 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
5230 const wxGridCellCoords
&bottomRight
)
5232 wxRect
rect( wxGridNoCellRect
);
5235 cellRect
= CellToRect( topLeft
);
5236 if ( cellRect
!= wxGridNoCellRect
)
5242 rect
= wxRect( 0, 0, 0, 0 );
5245 cellRect
= CellToRect( bottomRight
);
5246 if ( cellRect
!= wxGridNoCellRect
)
5252 return wxGridNoCellRect
;
5255 // convert to scrolled coords
5257 int left
, top
, right
, bottom
;
5258 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
5259 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
5262 m_gridWin
->GetClientSize( &cw
, &ch
);
5264 rect
.SetLeft( wxMax(0, left
) );
5265 rect
.SetTop( wxMax(0, top
) );
5266 rect
.SetRight( wxMin(cw
, right
) );
5267 rect
.SetBottom( wxMin(ch
, bottom
) );
5275 // ------ Grid event classes
5278 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
5280 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
5281 int row
, int col
, int x
, int y
,
5282 bool control
, bool shift
, bool alt
, bool meta
)
5283 : wxNotifyEvent( type
, id
)
5289 m_control
= control
;
5294 SetEventObject(obj
);
5298 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
5300 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
5301 int rowOrCol
, int x
, int y
,
5302 bool control
, bool shift
, bool alt
, bool meta
)
5303 : wxNotifyEvent( type
, id
)
5305 m_rowOrCol
= rowOrCol
;
5308 m_control
= control
;
5313 SetEventObject(obj
);
5317 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
5319 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
5320 const wxGridCellCoords
& topLeft
,
5321 const wxGridCellCoords
& bottomRight
,
5322 bool control
, bool shift
, bool alt
, bool meta
)
5323 : wxNotifyEvent( type
, id
)
5325 m_topLeft
= topLeft
;
5326 m_bottomRight
= bottomRight
;
5327 m_control
= control
;
5332 SetEventObject(obj
);
5336 #endif // ifndef wxUSE_NEW_GRID