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"
37 #include "wx/generic/grid.h"
39 //////////////////////////////////////////////////////////////////////
41 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
42 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
44 // this is a magic incantation which must be done!
45 #include "wx/arrimpl.cpp"
47 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
50 // TODO: fixed so far - make configurable later (and also different for x/y)
51 static const size_t GRID_SCROLL_LINE
= 10;
53 //////////////////////////////////////////////////////////////////////
55 // Abstract base class for grid data (the model)
57 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
60 wxGridTableBase::wxGridTableBase()
63 m_view
= (wxGrid
*) NULL
;
66 wxGridTableBase::~wxGridTableBase()
71 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
73 wxLogWarning( wxT("Called grid table class function InsertRows(pos=%d, N=%d)\n"
74 "but your derived table class does not override this function"),
80 bool wxGridTableBase::AppendRows( size_t numRows
)
82 wxLogWarning( wxT("Called grid table class function AppendRows(N=%d)\n"
83 "but your derived table class does not override this function"),
89 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
91 wxLogWarning( wxT("Called grid table class function DeleteRows(pos=%d, N=%d)\n"
92 "but your derived table class does not override this function"),
98 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
100 wxLogWarning( wxT("Called grid table class function InsertCols(pos=%d, N=%d)\n"
101 "but your derived table class does not override this function"),
107 bool wxGridTableBase::AppendCols( size_t numCols
)
109 wxLogWarning( wxT("Called grid table class function AppendCols(N=%d)\n"
110 "but your derived table class does not override this function"),
116 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
118 wxLogWarning( wxT("Called grid table class function DeleteCols(pos=%d, N=%d)\n"
119 "but your derived table class does not override this function"),
126 wxString
wxGridTableBase::GetRowLabelValue( int row
)
133 wxString
wxGridTableBase::GetColLabelValue( int col
)
135 // default col labels are:
136 // cols 0 to 25 : A-Z
137 // cols 26 to 675 : AA-ZZ
144 s
+= (_T('A') + (wxChar
)( col%26
));
146 if ( col
< 0 ) break;
149 // reverse the string...
151 for ( i
= 0; i
< n
; i
++ )
161 //////////////////////////////////////////////////////////////////////
163 // Message class for the grid table to send requests and notifications
167 wxGridTableMessage::wxGridTableMessage()
169 m_table
= (wxGridTableBase
*) NULL
;
175 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
176 int commandInt1
, int commandInt2
)
180 m_comInt1
= commandInt1
;
181 m_comInt2
= commandInt2
;
186 //////////////////////////////////////////////////////////////////////
188 // A basic grid table for string data. An object of this class will
189 // created by wxGrid if you don't specify an alternative table class.
192 WX_DEFINE_OBJARRAY(wxGridStringArray
)
194 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
196 wxGridStringTable::wxGridStringTable()
201 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
206 m_data
.Alloc( numRows
);
210 for ( col
= 0; col
< numCols
; col
++ )
212 sa
.Add( wxEmptyString
);
215 for ( row
= 0; row
< numRows
; row
++ )
221 wxGridStringTable::~wxGridStringTable()
225 long wxGridStringTable::GetNumberRows()
227 return m_data
.GetCount();
230 long wxGridStringTable::GetNumberCols()
232 if ( m_data
.GetCount() > 0 )
233 return m_data
[0].GetCount();
238 wxString
wxGridStringTable::GetValue( int row
, int col
)
240 // TODO: bounds checking
242 return m_data
[row
][col
];
245 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
247 // TODO: bounds checking
249 m_data
[row
][col
] = s
;
252 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
254 // TODO: bounds checking
256 return (m_data
[row
][col
] == wxEmptyString
);
260 void wxGridStringTable::Clear()
263 int numRows
, numCols
;
265 numRows
= m_data
.GetCount();
268 numCols
= m_data
[0].GetCount();
270 for ( row
= 0; row
< numRows
; row
++ )
272 for ( col
= 0; col
< numCols
; col
++ )
274 m_data
[row
][col
] = wxEmptyString
;
281 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
285 size_t curNumRows
= m_data
.GetCount();
286 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
288 if ( pos
>= curNumRows
)
290 return AppendRows( numRows
);
294 sa
.Alloc( curNumCols
);
295 for ( col
= 0; col
< curNumCols
; col
++ )
297 sa
.Add( wxEmptyString
);
300 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
302 m_data
.Insert( sa
, row
);
307 wxGridTableMessage
msg( this,
308 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
312 GetView()->ProcessTableMessage( msg
);
318 bool wxGridStringTable::AppendRows( size_t numRows
)
322 size_t curNumRows
= m_data
.GetCount();
323 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
326 if ( curNumCols
> 0 )
328 sa
.Alloc( curNumCols
);
329 for ( col
= 0; col
< curNumCols
; col
++ )
331 sa
.Add( wxEmptyString
);
335 for ( row
= 0; row
< numRows
; row
++ )
342 wxGridTableMessage
msg( this,
343 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
346 GetView()->ProcessTableMessage( msg
);
352 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
356 size_t curNumRows
= m_data
.GetCount();
358 if ( pos
>= curNumRows
)
360 wxLogError( wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)...\n"
361 "Pos value is invalid for present table with %d rows"),
362 pos
, numRows
, curNumRows
);
366 if ( numRows
> curNumRows
- pos
)
368 numRows
= curNumRows
- pos
;
371 if ( numRows
>= curNumRows
)
373 m_data
.Empty(); // don't release memory just yet
377 for ( n
= 0; n
< numRows
; n
++ )
379 m_data
.Remove( pos
);
385 wxGridTableMessage
msg( this,
386 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
390 GetView()->ProcessTableMessage( msg
);
396 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
400 size_t curNumRows
= m_data
.GetCount();
401 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
403 if ( pos
>= curNumCols
)
405 return AppendCols( numCols
);
408 for ( row
= 0; row
< curNumRows
; row
++ )
410 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
412 m_data
[row
].Insert( wxEmptyString
, col
);
418 wxGridTableMessage
msg( this,
419 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
423 GetView()->ProcessTableMessage( msg
);
429 bool wxGridStringTable::AppendCols( size_t numCols
)
433 size_t curNumRows
= m_data
.GetCount();
436 // TODO: something better than this ?
438 wxLogError( wxT("Unable to append cols to a grid table with no rows.\n"
439 "Call AppendRows() first") );
443 for ( row
= 0; row
< curNumRows
; row
++ )
445 for ( n
= 0; n
< numCols
; n
++ )
447 m_data
[row
].Add( wxEmptyString
);
453 wxGridTableMessage
msg( this,
454 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
457 GetView()->ProcessTableMessage( msg
);
463 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
467 size_t curNumRows
= m_data
.GetCount();
468 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
470 if ( pos
>= curNumCols
)
472 wxLogError( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
473 "Pos value is invalid for present table with %d cols"),
474 pos
, numCols
, curNumCols
);
478 if ( numCols
> curNumCols
- pos
)
480 numCols
= curNumCols
- pos
;
483 for ( row
= 0; row
< curNumRows
; row
++ )
485 if ( numCols
>= curNumCols
)
491 for ( n
= 0; n
< numCols
; n
++ )
493 m_data
[row
].Remove( pos
);
500 wxGridTableMessage
msg( this,
501 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
505 GetView()->ProcessTableMessage( msg
);
511 wxString
wxGridStringTable::GetRowLabelValue( int row
)
513 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
515 // using default label
517 return wxGridTableBase::GetRowLabelValue( row
);
521 return m_rowLabels
[ row
];
525 wxString
wxGridStringTable::GetColLabelValue( int col
)
527 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
529 // using default label
531 return wxGridTableBase::GetColLabelValue( col
);
535 return m_colLabels
[ col
];
539 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
541 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
543 int n
= m_rowLabels
.GetCount();
545 for ( i
= n
; i
<= row
; i
++ )
547 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
551 m_rowLabels
[row
] = value
;
554 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
556 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
558 int n
= m_colLabels
.GetCount();
560 for ( i
= n
; i
<= col
; i
++ )
562 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
566 m_colLabels
[col
] = value
;
572 //////////////////////////////////////////////////////////////////////
574 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
576 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
577 EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown
)
581 wxGridTextCtrl::wxGridTextCtrl( wxWindow
*par
,
585 const wxString
& value
,
589 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
592 m_isCellControl
= isCellControl
;
596 void wxGridTextCtrl::OnKeyDown( wxKeyEvent
& event
)
598 switch ( event
.KeyCode() )
601 m_grid
->SetEditControlValue( startValue
);
602 SetInsertionPointEnd();
611 if ( m_isCellControl
)
613 // send the event to the parent grid, skipping the
614 // event if nothing happens
616 event
.Skip( m_grid
->ProcessEvent( event
) );
620 // default text control response within the top edit
628 if ( m_isCellControl
)
630 if ( !m_grid
->ProcessEvent( event
) )
633 // wxMotif needs a little extra help...
635 int pos
= GetInsertionPoint();
636 wxString
s( GetValue() );
637 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
639 SetInsertionPoint( pos
);
641 // the other ports can handle a Return key press
651 if ( m_isCellControl
)
653 // send the event to the parent grid, skipping the
654 // event if nothing happens
656 event
.Skip( m_grid
->ProcessEvent( event
) );
660 // default text control response within the top edit
672 void wxGridTextCtrl::SetStartValue( const wxString
& s
)
675 wxTextCtrl::SetValue(s
);
680 //////////////////////////////////////////////////////////////////////
682 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
684 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
685 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
686 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
687 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
690 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
692 const wxPoint
&pos
, const wxSize
&size
)
693 : wxWindow( parent
, id
, pos
, size
)
698 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
702 // NO - don't do this because it will set both the x and y origin
703 // coords to match the parent scrolled window and we just want to
704 // set the y coord - MB
706 // m_owner->PrepareDC( dc );
709 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
710 dc
.SetDeviceOrigin( 0, -y
);
712 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
713 m_owner
->DrawRowLabels( dc
);
717 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
719 m_owner
->ProcessRowLabelMouseEvent( event
);
723 // This seems to be required for wxMotif otherwise the mouse
724 // cursor must be in the cell edit control to get key events
726 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
728 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
733 //////////////////////////////////////////////////////////////////////
735 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
737 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
738 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
739 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
740 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
743 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
745 const wxPoint
&pos
, const wxSize
&size
)
746 : wxWindow( parent
, id
, pos
, size
)
751 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
755 // NO - don't do this because it will set both the x and y origin
756 // coords to match the parent scrolled window and we just want to
757 // set the x coord - MB
759 // m_owner->PrepareDC( dc );
762 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
763 dc
.SetDeviceOrigin( -x
, 0 );
765 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
766 m_owner
->DrawColLabels( dc
);
770 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
772 m_owner
->ProcessColLabelMouseEvent( event
);
776 // This seems to be required for wxMotif otherwise the mouse
777 // cursor must be in the cell edit control to get key events
779 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
781 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
786 //////////////////////////////////////////////////////////////////////
788 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
790 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
791 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
792 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
795 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
797 const wxPoint
&pos
, const wxSize
&size
)
798 : wxWindow( parent
, id
, pos
, size
, wxRAISED_BORDER
)
804 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
806 m_owner
->ProcessCornerLabelMouseEvent( event
);
810 // This seems to be required for wxMotif otherwise the mouse
811 // cursor must be in the cell edit control to get key events
813 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
815 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
820 //////////////////////////////////////////////////////////////////////
822 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
824 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
825 EVT_PAINT( wxGridWindow::OnPaint
)
826 EVT_SCROLLWIN( wxGridWindow::ScrollWindow
)
827 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
828 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
831 wxGridWindow::wxGridWindow( wxGrid
*parent
,
832 wxGridRowLabelWindow
*rowLblWin
,
833 wxGridColLabelWindow
*colLblWin
,
834 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
835 : wxPanel( parent
, id
, pos
, size
, wxSUNKEN_BORDER
, "grid window" )
838 m_rowLabelWin
= rowLblWin
;
839 m_colLabelWin
= colLblWin
;
841 SetBackgroundColour( "WHITE" );
845 wxGridWindow::~wxGridWindow()
850 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
852 wxPaintDC
dc( this );
853 m_owner
->PrepareDC( dc
);
855 m_owner
->CalcCellsExposed( GetUpdateRegion() );
856 m_owner
->DrawGridCellArea( dc
);
860 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
862 wxPanel::ScrollWindow( dx
, dy
, rect
);
863 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
864 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
868 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
870 m_owner
->ProcessGridCellMouseEvent( event
);
874 // This seems to be required for wxMotif otherwise the mouse
875 // cursor must be in the cell edit control to get key events
877 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
879 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
884 //////////////////////////////////////////////////////////////////////
886 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
888 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
889 EVT_PAINT( wxGrid::OnPaint
)
890 EVT_SIZE( wxGrid::OnSize
)
891 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
894 wxGrid::wxGrid( wxWindow
*parent
,
899 const wxString
& name
)
900 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
913 // ----- internal init and update functions
916 void wxGrid::Create()
918 int colLblH
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
919 int rowLblW
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
921 m_rowLabelWin
= new wxGridRowLabelWindow( this,
924 wxSize(rowLblW
,-1) );
926 m_colLabelWin
= new wxGridColLabelWindow( this,
929 wxSize(-1, colLblH
) );
931 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
934 wxSize(rowLblW
, colLblH
) );
936 m_gridWin
= new wxGridWindow( this,
943 SetTargetWindow( m_gridWin
);
945 m_mainSizer
= new wxBoxSizer( wxVERTICAL
);
947 m_topSizer
= new wxBoxSizer( wxHORIZONTAL
);
948 m_topSizer
->Add( m_cornerLabelWin
, 0 );
949 m_topSizer
->Add( m_colLabelWin
, 1 );
951 m_mainSizer
->Add( m_topSizer
, 0, wxEXPAND
);
953 m_middleSizer
= new wxBoxSizer( wxHORIZONTAL
);
954 m_middleSizer
->Add( m_rowLabelWin
, 0, wxEXPAND
);
955 m_middleSizer
->Add( m_gridWin
, 1, wxEXPAND
);
957 m_mainSizer
->Add( m_middleSizer
, 1, wxEXPAND
);
959 SetAutoLayout( TRUE
);
960 SetSizer( m_mainSizer
);
964 bool wxGrid::CreateGrid( int numRows
, int numCols
)
968 wxLogError( wxT("wxGrid::CreateGrid(numRows, numCols) called more than once") );
976 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
977 m_table
->SetView( this );
990 if ( m_numRows
<= 0 )
991 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
993 if ( m_numCols
<= 0 )
994 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
996 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
997 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1001 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1005 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1008 m_labelTextColour
= wxColour( _T("BLACK") );
1010 // TODO: something better than this ?
1012 m_labelFont
= this->GetFont();
1013 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1015 m_rowLabelHorizAlign
= wxLEFT
;
1016 m_rowLabelVertAlign
= wxCENTRE
;
1018 m_colLabelHorizAlign
= wxCENTRE
;
1019 m_colLabelVertAlign
= wxTOP
;
1021 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1022 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1024 #if defined (__WXMOTIF__) // see also text ctrl sizing in ShowCellEditControl()
1025 m_defaultRowHeight
+= 8;
1027 m_defaultRowHeight
+= 4;
1030 m_rowHeights
.Alloc( m_numRows
);
1031 m_rowBottoms
.Alloc( m_numRows
);
1033 for ( i
= 0; i
< m_numRows
; i
++ )
1035 m_rowHeights
.Add( m_defaultRowHeight
);
1036 rowBottom
+= m_defaultRowHeight
;
1037 m_rowBottoms
.Add( rowBottom
);
1040 m_colWidths
.Alloc( m_numCols
);
1041 m_colRights
.Alloc( m_numCols
);
1043 for ( i
= 0; i
< m_numCols
; i
++ )
1045 m_colWidths
.Add( m_defaultColWidth
);
1046 colRight
+= m_defaultColWidth
;
1047 m_colRights
.Add( colRight
);
1050 // TODO: improve this ?
1052 m_defaultCellFont
= this->GetFont();
1054 m_gridLineColour
= wxColour( 128, 128, 255 );
1055 m_gridLinesEnabled
= TRUE
;
1057 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1059 m_dragRowOrCol
= -1;
1060 m_isDragging
= FALSE
;
1062 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1063 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1065 m_currentCellCoords
= wxGridNoCellCoords
;
1067 m_selectedTopLeft
= wxGridNoCellCoords
;
1068 m_selectedBottomRight
= wxGridNoCellCoords
;
1070 m_editable
= TRUE
; // default for whole grid
1072 m_inOnKeyDown
= FALSE
;
1075 // TODO: extend this to other types of controls
1077 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1085 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1089 m_cellEditCtrl
->Show( FALSE
);
1090 m_cellEditCtrlEnabled
= TRUE
;
1091 m_editCtrlType
= wxGRID_TEXTCTRL
;
1095 void wxGrid::CalcDimensions()
1098 GetClientSize( &cw
, &ch
);
1100 if ( m_numRows
> 0 && m_numCols
> 0 )
1102 int right
= m_colRights
[ m_numCols
-1 ] + 20;
1103 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 20;
1105 // TODO: restore the scroll position that we had before sizing
1108 GetViewStart( &x
, &y
);
1109 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1110 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1116 // this is called when the grid table sends a message to say that it
1117 // has been redimensioned
1119 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1123 switch ( msg
.GetId() )
1125 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1127 size_t pos
= msg
.GetCommandInt();
1128 int numRows
= msg
.GetCommandInt2();
1129 for ( i
= 0; i
< numRows
; i
++ )
1131 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1132 m_rowBottoms
.Insert( 0, pos
);
1134 m_numRows
+= numRows
;
1137 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1139 for ( i
= pos
; i
< m_numRows
; i
++ )
1141 bottom
+= m_rowHeights
[i
];
1142 m_rowBottoms
[i
] = bottom
;
1148 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1150 int numRows
= msg
.GetCommandInt();
1151 for ( i
= 0; i
< numRows
; i
++ )
1153 m_rowHeights
.Add( m_defaultRowHeight
);
1154 m_rowBottoms
.Add( 0 );
1157 int oldNumRows
= m_numRows
;
1158 m_numRows
+= numRows
;
1161 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1163 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1165 bottom
+= m_rowHeights
[i
];
1166 m_rowBottoms
[i
] = bottom
;
1172 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1174 size_t pos
= msg
.GetCommandInt();
1175 int numRows
= msg
.GetCommandInt2();
1176 for ( i
= 0; i
< numRows
; i
++ )
1178 m_rowHeights
.Remove( pos
);
1179 m_rowBottoms
.Remove( pos
);
1181 m_numRows
-= numRows
;
1186 m_colWidths
.Clear();
1187 m_colRights
.Clear();
1188 m_currentCellCoords
= wxGridNoCellCoords
;
1192 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1193 m_currentCellCoords
.Set( 0, 0 );
1196 for ( i
= 0; i
< m_numRows
; i
++ )
1198 h
+= m_rowHeights
[i
];
1199 m_rowBottoms
[i
] = h
;
1207 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1209 size_t pos
= msg
.GetCommandInt();
1210 int numCols
= msg
.GetCommandInt2();
1211 for ( i
= 0; i
< numCols
; i
++ )
1213 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1214 m_colRights
.Insert( 0, pos
);
1216 m_numCols
+= numCols
;
1219 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1221 for ( i
= pos
; i
< m_numCols
; i
++ )
1223 right
+= m_colWidths
[i
];
1224 m_colRights
[i
] = right
;
1230 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1232 int numCols
= msg
.GetCommandInt();
1233 for ( i
= 0; i
< numCols
; i
++ )
1235 m_colWidths
.Add( m_defaultColWidth
);
1236 m_colRights
.Add( 0 );
1239 int oldNumCols
= m_numCols
;
1240 m_numCols
+= numCols
;
1243 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1245 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1247 right
+= m_colWidths
[i
];
1248 m_colRights
[i
] = right
;
1254 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1256 size_t pos
= msg
.GetCommandInt();
1257 int numCols
= msg
.GetCommandInt2();
1258 for ( i
= 0; i
< numCols
; i
++ )
1260 m_colWidths
.Remove( pos
);
1261 m_colRights
.Remove( pos
);
1263 m_numCols
-= numCols
;
1267 #if 0 // leave the row alone here so that AppendCols will work subsequently
1269 m_rowHeights
.Clear();
1270 m_rowBottoms
.Clear();
1272 m_currentCellCoords
= wxGridNoCellCoords
;
1276 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1277 m_currentCellCoords
.Set( 0, 0 );
1280 for ( i
= 0; i
< m_numCols
; i
++ )
1282 w
+= m_colWidths
[i
];
1295 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
1297 wxRegionIterator
iter( reg
);
1300 m_rowLabelsExposed
.Empty();
1307 // TODO: remove this when we can...
1308 // There is a bug in wxMotif that gives garbage update
1309 // rectangles if you jump-scroll a long way by clicking the
1310 // scrollbar with middle button. This is a work-around
1312 #if defined(__WXMOTIF__)
1314 m_gridWin
->GetClientSize( &cw
, &ch
);
1315 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1316 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1319 // logical bounds of update region
1322 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1323 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1325 // find the row labels within these bounds
1329 for ( row
= 0; row
< m_numRows
; row
++ )
1331 if ( m_rowBottoms
[row
] < top
) continue;
1333 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1334 if ( rowTop
> bottom
) break;
1336 m_rowLabelsExposed
.Add( row
);
1344 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
1346 wxRegionIterator
iter( reg
);
1349 m_colLabelsExposed
.Empty();
1356 // TODO: remove this when we can...
1357 // There is a bug in wxMotif that gives garbage update
1358 // rectangles if you jump-scroll a long way by clicking the
1359 // scrollbar with middle button. This is a work-around
1361 #if defined(__WXMOTIF__)
1363 m_gridWin
->GetClientSize( &cw
, &ch
);
1364 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1365 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1368 // logical bounds of update region
1371 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1372 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1374 // find the cells within these bounds
1378 for ( col
= 0; col
< m_numCols
; col
++ )
1380 if ( m_colRights
[col
] < left
) continue;
1382 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1383 if ( colLeft
> right
) break;
1385 m_colLabelsExposed
.Add( col
);
1393 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
1395 wxRegionIterator
iter( reg
);
1398 m_cellsExposed
.Empty();
1399 m_rowsExposed
.Empty();
1400 m_colsExposed
.Empty();
1402 int left
, top
, right
, bottom
;
1407 // TODO: remove this when we can...
1408 // There is a bug in wxMotif that gives garbage update
1409 // rectangles if you jump-scroll a long way by clicking the
1410 // scrollbar with middle button. This is a work-around
1412 #if defined(__WXMOTIF__)
1414 m_gridWin
->GetClientSize( &cw
, &ch
);
1415 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1416 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1417 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1418 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1421 // logical bounds of update region
1423 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1424 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1426 // find the cells within these bounds
1429 int colLeft
, rowTop
;
1430 for ( row
= 0; row
< m_numRows
; row
++ )
1432 if ( m_rowBottoms
[row
] < top
) continue;
1434 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1435 if ( rowTop
> bottom
) break;
1437 m_rowsExposed
.Add( row
);
1439 for ( col
= 0; col
< m_numCols
; col
++ )
1441 if ( m_colRights
[col
] < left
) continue;
1443 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1444 if ( colLeft
> right
) break;
1446 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1447 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1456 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1459 wxPoint
pos( event
.GetPosition() );
1460 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1462 if ( event
.Dragging() )
1464 m_isDragging
= TRUE
;
1466 if ( event
.LeftIsDown() )
1468 switch( m_cursorMode
)
1470 case WXGRID_CURSOR_RESIZE_ROW
:
1472 int cw
, ch
, left
, dummy
;
1473 m_gridWin
->GetClientSize( &cw
, &ch
);
1474 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1476 wxClientDC
dc( m_gridWin
);
1478 dc
.SetLogicalFunction(wxXOR
);
1479 if ( m_dragLastPos
>= 0 )
1481 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1483 dc
.DrawLine( left
, y
, left
+cw
, y
);
1488 case WXGRID_CURSOR_SELECT_ROW
:
1490 if ( (row
= YToRow( y
)) >= 0 &&
1491 !IsInSelection( row
, 0 ) )
1493 SelectRow( row
, TRUE
);
1502 m_isDragging
= FALSE
;
1505 // ------------ Left button pressed
1507 if ( event
.LeftDown() )
1509 // don't send a label click event for a hit on the
1510 // edge of the row label - this is probably the user
1511 // wanting to resize the row
1513 if ( YToEdgeOfRow(y
) < 0 )
1517 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
1519 SelectRow( row
, event
.ShiftDown() );
1520 m_cursorMode
= WXGRID_CURSOR_SELECT_ROW
;
1525 // starting to drag-resize a row
1527 m_rowLabelWin
->CaptureMouse();
1532 // ------------ Left double click
1534 else if (event
.LeftDClick() )
1536 if ( YToEdgeOfRow(y
) < 0 )
1539 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
1544 // ------------ Left button released
1546 else if ( event
.LeftUp() )
1548 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
1550 m_rowLabelWin
->ReleaseMouse();
1552 if ( m_dragLastPos
>= 0 )
1554 // erase the last line and resize the row
1556 int cw
, ch
, left
, dummy
;
1557 m_gridWin
->GetClientSize( &cw
, &ch
);
1558 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1560 wxClientDC
dc( m_gridWin
);
1562 dc
.SetLogicalFunction( wxINVERT
);
1563 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1564 HideCellEditControl();
1566 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
1567 SetRowSize( m_dragRowOrCol
, wxMax( y
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
1568 if ( !GetBatchCount() )
1570 // TODO: optimize this
1571 m_rowLabelWin
->Refresh();
1572 m_gridWin
->Refresh();
1575 ShowCellEditControl();
1577 // Note: we are ending the event *after* doing
1578 // default processing in this case
1580 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1588 // ------------ Right button down
1590 else if ( event
.RightDown() )
1593 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1595 // no default action at the moment
1600 // ------------ Right double click
1602 else if ( event
.RightDClick() )
1605 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1607 // no default action at the moment
1612 // ------------ No buttons down and mouse moving
1614 else if ( event
.Moving() )
1616 m_dragRowOrCol
= YToEdgeOfRow( y
);
1617 if ( m_dragRowOrCol
>= 0 )
1619 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1621 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1622 m_rowLabelWin
->SetCursor( m_rowResizeCursor
);
1627 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1629 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1630 m_rowLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1637 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
1640 wxPoint
pos( event
.GetPosition() );
1641 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1643 if ( event
.Dragging() )
1645 m_isDragging
= TRUE
;
1647 if ( event
.LeftIsDown() )
1649 switch( m_cursorMode
)
1651 case WXGRID_CURSOR_RESIZE_COL
:
1653 int cw
, ch
, dummy
, top
;
1654 m_gridWin
->GetClientSize( &cw
, &ch
);
1655 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1657 wxClientDC
dc( m_gridWin
);
1659 dc
.SetLogicalFunction(wxXOR
);
1660 if ( m_dragLastPos
>= 0 )
1662 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1664 dc
.DrawLine( x
, top
, x
, top
+ch
);
1669 case WXGRID_CURSOR_SELECT_COL
:
1671 if ( (col
= XToCol( x
)) >= 0 &&
1672 !IsInSelection( 0, col
) )
1674 SelectCol( col
, TRUE
);
1683 m_isDragging
= FALSE
;
1686 // ------------ Left button pressed
1688 if ( event
.LeftDown() )
1690 // don't send a label click event for a hit on the
1691 // edge of the col label - this is probably the user
1692 // wanting to resize the col
1694 if ( XToEdgeOfCol(x
) < 0 )
1698 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
1700 SelectCol( col
, event
.ShiftDown() );
1701 m_cursorMode
= WXGRID_CURSOR_SELECT_COL
;
1706 // starting to drag-resize a col
1708 m_colLabelWin
->CaptureMouse();
1713 // ------------ Left double click
1715 if ( event
.LeftDClick() )
1717 if ( XToEdgeOfCol(x
) < 0 )
1720 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
1725 // ------------ Left button released
1727 else if ( event
.LeftUp() )
1729 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
1731 m_colLabelWin
->ReleaseMouse();
1733 if ( m_dragLastPos
>= 0 )
1735 // erase the last line and resize the col
1737 int cw
, ch
, dummy
, top
;
1738 m_gridWin
->GetClientSize( &cw
, &ch
);
1739 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1741 wxClientDC
dc( m_gridWin
);
1743 dc
.SetLogicalFunction( wxINVERT
);
1744 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1745 HideCellEditControl();
1747 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
1748 SetColSize( m_dragRowOrCol
, wxMax( x
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
1750 if ( !GetBatchCount() )
1752 // TODO: optimize this
1753 m_colLabelWin
->Refresh();
1754 m_gridWin
->Refresh();
1757 ShowCellEditControl();
1759 // Note: we are ending the event *after* doing
1760 // default processing in this case
1762 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
1770 // ------------ Right button down
1772 else if ( event
.RightDown() )
1775 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
1777 // no default action at the moment
1782 // ------------ Right double click
1784 else if ( event
.RightDClick() )
1787 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
1789 // no default action at the moment
1794 // ------------ No buttons down and mouse moving
1796 else if ( event
.Moving() )
1798 m_dragRowOrCol
= XToEdgeOfCol( x
);
1799 if ( m_dragRowOrCol
>= 0 )
1801 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1803 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
1804 m_colLabelWin
->SetCursor( m_colResizeCursor
);
1809 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1811 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1812 m_colLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1819 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
1821 if ( event
.LeftDown() )
1823 // indicate corner label by having both row and
1826 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
1832 else if ( event
.LeftDClick() )
1834 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
1837 else if ( event
.RightDown() )
1839 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
1841 // no default action at the moment
1845 else if ( event
.RightDClick() )
1847 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
1849 // no default action at the moment
1855 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
1858 wxPoint
pos( event
.GetPosition() );
1859 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1861 wxGridCellCoords coords
;
1862 XYToCell( x
, y
, coords
);
1864 if ( event
.Dragging() )
1866 m_isDragging
= TRUE
;
1867 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1869 // Hide the edit control, so it
1870 // won't interfer with drag-shrinking.
1871 if ( IsCellEditControlEnabled() )
1872 HideCellEditControl();
1873 if ( coords
!= wxGridNoCellCoords
)
1875 if ( !IsSelection() )
1877 SelectBlock( coords
, coords
);
1881 SelectBlock( m_currentCellCoords
, coords
);
1889 m_isDragging
= FALSE
;
1891 if ( coords
!= wxGridNoCellCoords
)
1893 if ( event
.LeftDown() )
1895 if ( event
.ShiftDown() )
1897 SelectBlock( m_currentCellCoords
, coords
);
1901 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
1906 MakeCellVisible( coords
);
1907 SetCurrentCell( coords
);
1913 // ------------ Left double click
1915 else if ( event
.LeftDClick() )
1917 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
1924 // ------------ Left button released
1926 else if ( event
.LeftUp() )
1928 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1930 if ( IsSelection() )
1932 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
1936 // Show the edit control, if it has
1937 // been hidden for drag-shrinking.
1938 if ( IsCellEditControlEnabled() )
1939 ShowCellEditControl();
1945 // ------------ Right button down
1947 else if ( event
.RightDown() )
1949 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
1954 // no default action at the moment
1959 // ------------ Right double click
1961 else if ( event
.RightDClick() )
1963 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
1968 // no default action at the moment
1976 // ------ interaction with data model
1978 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
1980 switch ( msg
.GetId() )
1982 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
1983 return GetModelValues();
1985 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
1986 return SetModelValues();
1988 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1989 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1990 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1991 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1992 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1993 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1994 return Redimension( msg
);
2003 // The behaviour of this function depends on the grid table class
2004 // Clear() function. For the default wxGridStringTable class the
2005 // behavious is to replace all cell contents with wxEmptyString but
2006 // not to change the number of rows or cols.
2008 void wxGrid::ClearGrid()
2013 SetEditControlValue();
2014 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2019 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2021 // TODO: something with updateLabels flag
2025 wxLogError( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2031 bool ok
= m_table
->InsertRows( pos
, numRows
);
2033 // the table will have sent the results of the insert row
2034 // operation to this view object as a grid table message
2038 if ( m_numCols
== 0 )
2040 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2042 // TODO: perhaps instead of appending the default number of cols
2043 // we should remember what the last non-zero number of cols was ?
2047 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2049 // if we have just inserted cols into an empty grid the current
2050 // cell will be undefined...
2052 SetCurrentCell( 0, 0 );
2056 if ( !GetBatchCount() ) Refresh();
2059 SetEditControlValue();
2069 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2071 // TODO: something with updateLabels flag
2075 wxLogError( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2079 if ( m_table
&& m_table
->AppendRows( numRows
) )
2081 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2083 // if we have just inserted cols into an empty grid the current
2084 // cell will be undefined...
2086 SetCurrentCell( 0, 0 );
2089 // the table will have sent the results of the append row
2090 // operation to this view object as a grid table message
2093 if ( !GetBatchCount() ) Refresh();
2103 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2105 // TODO: something with updateLabels flag
2109 wxLogError( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2113 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2115 // the table will have sent the results of the delete row
2116 // operation to this view object as a grid table message
2118 if ( m_numRows
> 0 )
2119 SetEditControlValue();
2121 HideCellEditControl();
2124 if ( !GetBatchCount() ) Refresh();
2134 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2136 // TODO: something with updateLabels flag
2140 wxLogError( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2146 HideCellEditControl();
2147 bool ok
= m_table
->InsertCols( pos
, numCols
);
2149 // the table will have sent the results of the insert col
2150 // operation to this view object as a grid table message
2154 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2156 // if we have just inserted cols into an empty grid the current
2157 // cell will be undefined...
2159 SetCurrentCell( 0, 0 );
2163 if ( !GetBatchCount() ) Refresh();
2166 SetEditControlValue();
2176 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2178 // TODO: something with updateLabels flag
2182 wxLogError( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2186 if ( m_table
&& m_table
->AppendCols( numCols
) )
2188 // the table will have sent the results of the append col
2189 // operation to this view object as a grid table message
2191 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2193 // if we have just inserted cols into an empty grid the current
2194 // cell will be undefined...
2196 SetCurrentCell( 0, 0 );
2200 if ( !GetBatchCount() ) Refresh();
2210 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2212 // TODO: something with updateLabels flag
2216 wxLogError( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2220 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2222 // the table will have sent the results of the delete col
2223 // operation to this view object as a grid table message
2225 if ( m_numCols
> 0 )
2226 SetEditControlValue();
2228 HideCellEditControl();
2231 if ( !GetBatchCount() ) Refresh();
2243 // ----- event handlers
2246 // Generate a grid event based on a mouse event and
2247 // return the result of ProcessEvent()
2249 bool wxGrid::SendEvent( const wxEventType type
,
2251 wxMouseEvent
& mouseEv
)
2253 if ( type
== EVT_GRID_ROW_SIZE
||
2254 type
== EVT_GRID_COL_SIZE
)
2256 int rowOrCol
= (row
== -1 ? col
: row
);
2258 wxGridSizeEvent
gridEvt( GetId(),
2262 mouseEv
.GetX(), mouseEv
.GetY(),
2263 mouseEv
.ControlDown(),
2264 mouseEv
.ShiftDown(),
2266 mouseEv
.MetaDown() );
2268 return GetEventHandler()->ProcessEvent(gridEvt
);
2270 else if ( type
== EVT_GRID_RANGE_SELECT
)
2272 wxGridRangeSelectEvent
gridEvt( GetId(),
2276 m_selectedBottomRight
,
2277 mouseEv
.ControlDown(),
2278 mouseEv
.ShiftDown(),
2280 mouseEv
.MetaDown() );
2282 return GetEventHandler()->ProcessEvent(gridEvt
);
2286 wxGridEvent
gridEvt( GetId(),
2290 mouseEv
.GetX(), mouseEv
.GetY(),
2291 mouseEv
.ControlDown(),
2292 mouseEv
.ShiftDown(),
2294 mouseEv
.MetaDown() );
2296 return GetEventHandler()->ProcessEvent(gridEvt
);
2301 // Generate a grid event of specified type and return the result
2302 // of ProcessEvent().
2304 bool wxGrid::SendEvent( const wxEventType type
,
2307 if ( type
== EVT_GRID_ROW_SIZE
||
2308 type
== EVT_GRID_COL_SIZE
)
2310 int rowOrCol
= (row
== -1 ? col
: row
);
2312 wxGridSizeEvent
gridEvt( GetId(),
2317 return GetEventHandler()->ProcessEvent(gridEvt
);
2321 wxGridEvent
gridEvt( GetId(),
2326 return GetEventHandler()->ProcessEvent(gridEvt
);
2331 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2333 wxPaintDC
dc( this );
2335 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2336 m_numRows
&& m_numCols
)
2338 m_currentCellCoords
.Set(0, 0);
2339 SetEditControlValue();
2340 ShowCellEditControl();
2345 // This is just here to make sure that CalcDimensions gets called when
2346 // the grid view is resized... then the size event is skipped to allow
2347 // the box sizers to handle everything
2349 void wxGrid::OnSize( wxSizeEvent
& event
)
2356 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2358 if ( m_inOnKeyDown
)
2360 // shouldn't be here - we are going round in circles...
2362 wxLogFatalError( wxT("wxGrid::OnKeyDown called while alread active") );
2365 m_inOnKeyDown
= TRUE
;
2367 // propagate the event up and see if it gets processed
2369 wxWindow
*parent
= GetParent();
2370 wxKeyEvent
keyEvt( event
);
2371 keyEvt
.SetEventObject( parent
);
2373 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2375 // try local handlers
2377 switch ( event
.KeyCode() )
2380 if ( event
.ControlDown() )
2382 MoveCursorUpBlock();
2391 if ( event
.ControlDown() )
2393 MoveCursorDownBlock();
2402 if ( event
.ControlDown() )
2404 MoveCursorLeftBlock();
2413 if ( event
.ControlDown() )
2415 MoveCursorRightBlock();
2424 if ( event
.ControlDown() )
2426 event
.Skip(); // to let the edit control have the return
2435 if ( event
.ControlDown() )
2437 MakeCellVisible( 0, 0 );
2438 SetCurrentCell( 0, 0 );
2447 if ( event
.ControlDown() )
2449 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2450 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2467 // now try the cell edit control
2469 if ( IsCellEditControlEnabled() )
2471 event
.SetEventObject( m_cellEditCtrl
);
2472 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2478 m_inOnKeyDown
= FALSE
;
2482 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
2484 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
2486 // the event has been intercepted - do nothing
2490 wxClientDC
dc( m_gridWin
);
2493 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
2495 HideCellEditControl();
2496 SaveEditControlValue();
2499 m_currentCellCoords
= coords
;
2501 SetEditControlValue();
2502 ShowCellEditControl();
2504 if ( IsSelection() )
2506 wxRect
r( SelectionToDeviceRect() );
2508 if ( !GetBatchCount() ) m_gridWin
->Refresh( TRUE
, &r
);
2514 // ------ functions to get/send data (see also public functions)
2517 bool wxGrid::GetModelValues()
2521 // all we need to do is repaint the grid
2523 m_gridWin
->Refresh();
2531 bool wxGrid::SetModelValues()
2537 for ( row
= 0; row
< m_numRows
; row
++ )
2539 for ( col
= 0; col
< m_numCols
; col
++ )
2541 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
2553 // Note - this function only draws cells that are in the list of
2554 // exposed cells (usually set from the update region by
2555 // CalcExposedCells)
2557 void wxGrid::DrawGridCellArea( wxDC
& dc
)
2559 if ( !m_numRows
|| !m_numCols
) return;
2562 size_t numCells
= m_cellsExposed
.GetCount();
2564 for ( i
= 0; i
< numCells
; i
++ )
2566 DrawCell( dc
, m_cellsExposed
[i
] );
2571 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
2573 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2574 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2576 if ( m_gridLinesEnabled
)
2577 DrawCellBorder( dc
, coords
);
2579 DrawCellBackground( dc
, coords
);
2581 // TODO: separate functions here for different kinds of cells ?
2584 DrawCellValue( dc
, coords
);
2588 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
2590 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2591 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2593 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2594 int row
= coords
.GetRow();
2595 int col
= coords
.GetCol();
2597 // right hand border
2599 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
2600 m_colRights
[col
], m_rowBottoms
[row
] );
2604 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
2605 m_colRights
[col
], m_rowBottoms
[row
] );
2609 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
2611 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2612 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2614 int row
= coords
.GetRow();
2615 int col
= coords
.GetCol();
2617 dc
.SetBackgroundMode( wxSOLID
);
2619 if ( IsInSelection( coords
) )
2621 // TODO: improve this
2623 dc
.SetBrush( *wxBLACK_BRUSH
);
2627 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
2630 dc
.SetPen( *wxTRANSPARENT_PEN
);
2632 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
2633 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
2635 m_rowHeights
[row
]-1 );
2639 void wxGrid::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
2641 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2642 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2644 int row
= coords
.GetRow();
2645 int col
= coords
.GetCol();
2647 dc
.SetBackgroundMode( wxTRANSPARENT
);
2649 if ( IsInSelection( row
, col
) )
2651 // TODO: improve this
2653 dc
.SetTextBackground( wxColour(0, 0, 0) );
2654 dc
.SetTextForeground( wxColour(255, 255, 255) );
2658 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
2659 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
2661 dc
.SetFont( GetCellFont(row
, col
) );
2664 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
2667 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2668 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2669 rect
.SetWidth( m_colWidths
[col
] - 4 );
2670 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2672 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
2677 // TODO: remove this ???
2678 // This is used to redraw all grid lines e.g. when the grid line colour
2681 void wxGrid::DrawAllGridLines( wxDC
& dc
)
2683 if ( !m_gridLinesEnabled
||
2685 !m_numCols
) return;
2688 m_gridWin
->GetClientSize(&cw
, &ch
);
2690 // virtual coords of visible area
2692 int top
, bottom
, left
, right
;
2693 CalcUnscrolledPosition( 0, 0, &left
, &top
);
2694 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
2696 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2698 // horizontal grid lines
2701 for ( i
= 0; i
<= m_numRows
; i
++ )
2703 if ( m_rowBottoms
[i
] > bottom
)
2707 else if ( m_rowBottoms
[i
] >= top
)
2709 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
2714 // vertical grid lines
2716 for ( i
= 0; i
<= m_numCols
; i
++ )
2718 if ( m_colRights
[i
] > right
)
2722 else if ( m_colRights
[i
] >= left
)
2724 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
2730 void wxGrid::DrawRowLabels( wxDC
& dc
)
2732 if ( !m_numRows
|| !m_numCols
) return;
2735 size_t numLabels
= m_rowLabelsExposed
.GetCount();
2737 for ( i
= 0; i
< numLabels
; i
++ )
2739 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
2744 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
2746 if ( m_rowHeights
[row
] <= 0 ) return;
2748 // draw the label's horizontal border (the vertical border is
2749 // provided by the cell area window margin)
2751 dc
.SetPen( *wxBLACK_PEN
);
2753 dc
.DrawLine( 0, m_rowBottoms
[row
]+1,
2754 m_rowLabelWidth
, m_rowBottoms
[row
]+1 );
2756 dc
.SetPen( *wxWHITE_PEN
);
2758 dc
.DrawLine( 0, m_rowBottoms
[row
]+2,
2759 m_rowLabelWidth
, m_rowBottoms
[row
]+2 );
2761 dc
.SetBackgroundMode( wxTRANSPARENT
);
2762 dc
.SetTextForeground( GetLabelTextColour() );
2763 dc
.SetFont( GetLabelFont() );
2766 GetRowLabelAlignment( &hAlign
, &vAlign
);
2770 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2771 rect
.SetWidth( m_rowLabelWidth
- 4 );
2772 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2773 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
2777 void wxGrid::DrawColLabels( wxDC
& dc
)
2779 if ( !m_numRows
|| !m_numCols
) return;
2782 size_t numLabels
= m_colLabelsExposed
.GetCount();
2784 for ( i
= 0; i
< numLabels
; i
++ )
2786 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
2791 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
2793 if ( m_colWidths
[col
] <= 0 ) return;
2795 // draw the label's vertical border (the horizontal border is
2796 // provided by the cell area window margin)
2798 dc
.SetPen( *wxBLACK_PEN
);
2800 dc
.DrawLine( m_colRights
[col
]+1, 0,
2801 m_colRights
[col
]+1, m_colLabelHeight
);
2803 dc
.SetPen( *wxWHITE_PEN
);
2805 dc
.DrawLine( m_colRights
[col
]+2, 0,
2806 m_colRights
[col
]+2, m_colLabelHeight
);
2808 dc
.SetBackgroundMode( wxTRANSPARENT
);
2809 dc
.SetTextForeground( GetLabelTextColour() );
2810 dc
.SetFont( GetLabelFont() );
2813 GetColLabelAlignment( &hAlign
, &vAlign
);
2816 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2818 rect
.SetWidth( m_colWidths
[col
] - 4 );
2819 rect
.SetHeight( m_colLabelHeight
- 4 );
2820 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
2824 void wxGrid::DrawTextRectangle( wxDC
& dc
,
2825 const wxString
& value
,
2830 long textWidth
, textHeight
;
2831 long lineWidth
, lineHeight
;
2832 wxArrayString lines
;
2834 dc
.SetClippingRegion( rect
);
2835 StringToLines( value
, lines
);
2836 if ( lines
.GetCount() )
2838 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
2839 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
2842 switch ( horizAlign
)
2845 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
2849 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
2858 switch ( vertAlign
)
2861 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
2865 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
2874 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
2876 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
2881 dc
.DestroyClippingRegion();
2885 // Split multi line text up into an array of strings. Any existing
2886 // contents of the string array are preserved.
2888 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
2890 // TODO: this won't work for WXMAC ? (lines end with '\r')
2891 // => use wxTextFile functions then (VZ)
2894 while ( startPos
< (int)value
.Length() )
2896 pos
= value
.Mid(startPos
).Find( '\n' );
2901 else if ( pos
== 0 )
2903 lines
.Add( wxEmptyString
);
2907 if ( value
[startPos
+pos
-1] == '\r' )
2909 lines
.Add( value
.Mid(startPos
, pos
-1) );
2913 lines
.Add( value
.Mid(startPos
, pos
) );
2918 if ( startPos
< (int)value
.Length() )
2920 lines
.Add( value
.Mid( startPos
) );
2925 void wxGrid::GetTextBoxSize( wxDC
& dc
,
2926 wxArrayString
& lines
,
2927 long *width
, long *height
)
2934 for ( i
= 0; i
< lines
.GetCount(); i
++ )
2936 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
2937 w
= wxMax( w
, lineW
);
2947 // ------ Edit control functions
2951 void wxGrid::EnableEditing( bool edit
)
2953 // TODO: improve this ?
2955 if ( edit
!= m_editable
)
2959 // TODO: extend this for other edit control types
2961 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
2963 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
2969 #if 0 // disabled for the moment - the cell control is always active
2970 void wxGrid::EnableCellEditControl( bool enable
)
2972 if ( m_cellEditCtrl
&&
2973 enable
!= m_cellEditCtrlEnabled
)
2975 m_cellEditCtrlEnabled
= enable
;
2977 if ( m_cellEditCtrlEnabled
)
2979 SetEditControlValue();
2980 ShowCellEditControl();
2984 HideCellEditControl();
2985 SaveEditControlValue();
2992 void wxGrid::ShowCellEditControl()
2996 if ( IsCellEditControlEnabled() )
2998 if ( !IsVisible( m_currentCellCoords
) )
3004 rect
= CellToRect( m_currentCellCoords
);
3006 // convert to scrolled coords
3008 int left
, top
, right
, bottom
;
3009 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3010 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3013 m_gridWin
->GetClientSize( &cw
, &ch
);
3015 // Make the edit control large enough to allow for internal margins
3016 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3019 #if defined (__WXMOTIF__)
3020 if ( m_currentCellCoords
.GetRow() == 0 ||
3021 m_currentCellCoords
.GetCol() == 0 )
3030 if ( m_currentCellCoords
.GetRow() == 0 ||
3031 m_currentCellCoords
.GetCol() == 0 )
3040 rect
.SetLeft( wxMax(0, left
- extra
) );
3041 rect
.SetTop( wxMax(0, top
- extra
) );
3042 rect
.SetRight( rect
.GetRight() + 2*extra
);
3043 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3046 m_cellEditCtrl
->SetSize( rect
);
3047 m_cellEditCtrl
->Show( TRUE
);
3049 switch ( m_editCtrlType
)
3051 case wxGRID_TEXTCTRL
:
3052 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3055 case wxGRID_CHECKBOX
:
3056 // TODO: anything ???
3061 // TODO: anything ???
3065 case wxGRID_COMBOBOX
:
3066 // TODO: anything ???
3071 m_cellEditCtrl
->SetFocus();
3077 void wxGrid::HideCellEditControl()
3079 if ( IsCellEditControlEnabled() )
3081 m_cellEditCtrl
->Show( FALSE
);
3086 void wxGrid::SetEditControlValue( const wxString
& value
)
3092 s
= GetCellValue(m_currentCellCoords
);
3096 if ( IsCellEditControlEnabled() )
3098 switch ( m_editCtrlType
)
3100 case wxGRID_TEXTCTRL
:
3101 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3104 case wxGRID_CHECKBOX
:
3105 // TODO: implement this
3110 // TODO: implement this
3114 case wxGRID_COMBOBOX
:
3115 // TODO: implement this
3124 void wxGrid::SaveEditControlValue()
3128 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3130 if ( IsCellEditControlEnabled() )
3132 ctrl
= m_cellEditCtrl
;
3139 bool valueChanged
= FALSE
;
3141 switch ( m_editCtrlType
)
3143 case wxGRID_TEXTCTRL
:
3144 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3145 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3146 SetCellValue( m_currentCellCoords
,
3147 ((wxTextCtrl
*) ctrl
)->GetValue() );
3150 case wxGRID_CHECKBOX
:
3151 // TODO: implement this
3156 // TODO: implement this
3160 case wxGRID_COMBOBOX
:
3161 // TODO: implement this
3168 SendEvent( EVT_GRID_CELL_CHANGE
,
3169 m_currentCellCoords
.GetRow(),
3170 m_currentCellCoords
.GetCol() );
3177 // ------ Grid location functions
3178 // Note that all of these functions work with the logical coordinates of
3179 // grid cells and labels so you will need to convert from device
3180 // coordinates for mouse events etc.
3183 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3185 int row
= YToRow(y
);
3186 int col
= XToCol(x
);
3188 if ( row
== -1 || col
== -1 )
3190 coords
= wxGridNoCellCoords
;
3194 coords
.Set( row
, col
);
3199 int wxGrid::YToRow( int y
)
3203 for ( i
= 0; i
< m_numRows
; i
++ )
3205 if ( y
< m_rowBottoms
[i
] ) return i
;
3212 int wxGrid::XToCol( int x
)
3216 for ( i
= 0; i
< m_numCols
; i
++ )
3218 if ( x
< m_colRights
[i
] ) return i
;
3225 // return the row number that that the y coord is near the edge of, or
3226 // -1 if not near an edge
3228 int wxGrid::YToEdgeOfRow( int y
)
3232 for ( i
= 0; i
< m_numRows
; i
++ )
3234 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3236 d
= abs( y
- m_rowBottoms
[i
] );
3238 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3247 // return the col number that that the x coord is near the edge of, or
3248 // -1 if not near an edge
3250 int wxGrid::XToEdgeOfCol( int x
)
3254 for ( i
= 0; i
< m_numCols
; i
++ )
3256 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3258 d
= abs( x
- m_colRights
[i
] );
3260 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3269 wxRect
wxGrid::CellToRect( int row
, int col
)
3271 wxRect
rect( -1, -1, -1, -1 );
3273 if ( row
>= 0 && row
< m_numRows
&&
3274 col
>= 0 && col
< m_numCols
)
3276 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3277 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3278 rect
.width
= m_colWidths
[col
];
3279 rect
.height
= m_rowHeights
[ row
];
3286 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3288 // get the cell rectangle in logical coords
3290 wxRect
r( CellToRect( row
, col
) );
3292 // convert to device coords
3294 int left
, top
, right
, bottom
;
3295 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3296 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3298 // check against the client area of the grid window
3301 m_gridWin
->GetClientSize( &cw
, &ch
);
3303 if ( wholeCellVisible
)
3305 // is the cell wholly visible ?
3307 return ( left
>= 0 && right
<= cw
&&
3308 top
>= 0 && bottom
<= ch
);
3312 // is the cell partly visible ?
3314 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3315 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3320 // make the specified cell location visible by doing a minimal amount
3323 void wxGrid::MakeCellVisible( int row
, int col
)
3326 int xpos
= -1, ypos
= -1;
3328 if ( row
>= 0 && row
< m_numRows
&&
3329 col
>= 0 && col
< m_numCols
)
3331 // get the cell rectangle in logical coords
3333 wxRect
r( CellToRect( row
, col
) );
3335 // convert to device coords
3337 int left
, top
, right
, bottom
;
3338 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3339 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3342 m_gridWin
->GetClientSize( &cw
, &ch
);
3348 else if ( bottom
> ch
)
3350 int h
= r
.GetHeight();
3352 for ( i
= row
-1; i
>= 0; i
-- )
3354 if ( h
+ m_rowHeights
[i
] > ch
) break;
3356 h
+= m_rowHeights
[i
];
3357 ypos
-= m_rowHeights
[i
];
3360 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
3361 // have rounding errors (this is important, because if we do, we
3362 // might not scroll at all and some cells won't be redrawn)
3363 ypos
+= GRID_SCROLL_LINE
/ 2;
3370 else if ( right
> cw
)
3372 int w
= r
.GetWidth();
3374 for ( i
= col
-1; i
>= 0; i
-- )
3376 if ( w
+ m_colWidths
[i
] > cw
) break;
3378 w
+= m_colWidths
[i
];
3379 xpos
-= m_colWidths
[i
];
3382 // see comment for ypos above
3383 xpos
+= GRID_SCROLL_LINE
/ 2;
3386 if ( xpos
!= -1 || ypos
!= -1 )
3388 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
3389 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
3390 Scroll( xpos
, ypos
);
3398 // ------ Grid cursor movement functions
3401 bool wxGrid::MoveCursorUp()
3403 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3404 m_currentCellCoords
.GetRow() > 0 )
3406 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3407 m_currentCellCoords
.GetCol() );
3409 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3410 m_currentCellCoords
.GetCol() );
3419 bool wxGrid::MoveCursorDown()
3421 // TODO: allow for scrolling
3423 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3424 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3426 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3427 m_currentCellCoords
.GetCol() );
3429 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3430 m_currentCellCoords
.GetCol() );
3439 bool wxGrid::MoveCursorLeft()
3441 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3442 m_currentCellCoords
.GetCol() > 0 )
3444 MakeCellVisible( m_currentCellCoords
.GetRow(),
3445 m_currentCellCoords
.GetCol() - 1 );
3447 SetCurrentCell( m_currentCellCoords
.GetRow(),
3448 m_currentCellCoords
.GetCol() - 1 );
3457 bool wxGrid::MoveCursorRight()
3459 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3460 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
3462 MakeCellVisible( m_currentCellCoords
.GetRow(),
3463 m_currentCellCoords
.GetCol() + 1 );
3465 SetCurrentCell( m_currentCellCoords
.GetRow(),
3466 m_currentCellCoords
.GetCol() + 1 );
3475 bool wxGrid::MovePageUp()
3477 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3479 int row
= m_currentCellCoords
.GetRow();
3483 m_gridWin
->GetClientSize( &cw
, &ch
);
3485 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3486 int newRow
= YToRow( y
- ch
+ 1 );
3491 else if ( newRow
== row
)
3496 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3497 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3505 bool wxGrid::MovePageDown()
3507 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3509 int row
= m_currentCellCoords
.GetRow();
3510 if ( row
< m_numRows
)
3513 m_gridWin
->GetClientSize( &cw
, &ch
);
3515 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3516 int newRow
= YToRow( y
+ ch
);
3519 newRow
= m_numRows
- 1;
3521 else if ( newRow
== row
)
3526 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3527 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3535 bool wxGrid::MoveCursorUpBlock()
3538 m_currentCellCoords
!= wxGridNoCellCoords
&&
3539 m_currentCellCoords
.GetRow() > 0 )
3541 int row
= m_currentCellCoords
.GetRow();
3542 int col
= m_currentCellCoords
.GetCol();
3544 if ( m_table
->IsEmptyCell(row
, col
) )
3546 // starting in an empty cell: find the next block of
3552 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3555 else if ( m_table
->IsEmptyCell(row
-1, col
) )
3557 // starting at the top of a block: find the next block
3563 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3568 // starting within a block: find the top of the block
3573 if ( m_table
->IsEmptyCell(row
, col
) )
3581 MakeCellVisible( row
, col
);
3582 SetCurrentCell( row
, col
);
3590 bool wxGrid::MoveCursorDownBlock()
3593 m_currentCellCoords
!= wxGridNoCellCoords
&&
3594 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3596 int row
= m_currentCellCoords
.GetRow();
3597 int col
= m_currentCellCoords
.GetCol();
3599 if ( m_table
->IsEmptyCell(row
, col
) )
3601 // starting in an empty cell: find the next block of
3604 while ( row
< m_numRows
-1 )
3607 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3610 else if ( m_table
->IsEmptyCell(row
+1, col
) )
3612 // starting at the bottom of a block: find the next block
3615 while ( row
< m_numRows
-1 )
3618 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3623 // starting within a block: find the bottom of the block
3625 while ( row
< m_numRows
-1 )
3628 if ( m_table
->IsEmptyCell(row
, col
) )
3636 MakeCellVisible( row
, col
);
3637 SetCurrentCell( row
, col
);
3645 bool wxGrid::MoveCursorLeftBlock()
3648 m_currentCellCoords
!= wxGridNoCellCoords
&&
3649 m_currentCellCoords
.GetCol() > 0 )
3651 int row
= m_currentCellCoords
.GetRow();
3652 int col
= m_currentCellCoords
.GetCol();
3654 if ( m_table
->IsEmptyCell(row
, col
) )
3656 // starting in an empty cell: find the next block of
3662 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3665 else if ( m_table
->IsEmptyCell(row
, col
-1) )
3667 // starting at the left of a block: find the next block
3673 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3678 // starting within a block: find the left of the block
3683 if ( m_table
->IsEmptyCell(row
, col
) )
3691 MakeCellVisible( row
, col
);
3692 SetCurrentCell( row
, col
);
3700 bool wxGrid::MoveCursorRightBlock()
3703 m_currentCellCoords
!= wxGridNoCellCoords
&&
3704 m_currentCellCoords
.GetCol() < m_numCols
-1 )
3706 int row
= m_currentCellCoords
.GetRow();
3707 int col
= m_currentCellCoords
.GetCol();
3709 if ( m_table
->IsEmptyCell(row
, col
) )
3711 // starting in an empty cell: find the next block of
3714 while ( col
< m_numCols
-1 )
3717 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3720 else if ( m_table
->IsEmptyCell(row
, col
+1) )
3722 // starting at the right of a block: find the next block
3725 while ( col
< m_numCols
-1 )
3728 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3733 // starting within a block: find the right of the block
3735 while ( col
< m_numCols
-1 )
3738 if ( m_table
->IsEmptyCell(row
, col
) )
3746 MakeCellVisible( row
, col
);
3747 SetCurrentCell( row
, col
);
3758 // ------ Label values and formatting
3761 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
3763 *horiz
= m_rowLabelHorizAlign
;
3764 *vert
= m_rowLabelVertAlign
;
3767 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
3769 *horiz
= m_colLabelHorizAlign
;
3770 *vert
= m_colLabelVertAlign
;
3773 wxString
wxGrid::GetRowLabelValue( int row
)
3777 return m_table
->GetRowLabelValue( row
);
3787 wxString
wxGrid::GetColLabelValue( int col
)
3791 return m_table
->GetColLabelValue( col
);
3801 void wxGrid::SetRowLabelSize( int width
)
3803 // TODO: how to do this with the box sizers ?
3806 void wxGrid::SetColLabelSize( int height
)
3808 // TODO: how to do this with the box sizers ?
3811 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
3813 if ( m_labelBackgroundColour
!= colour
)
3815 m_labelBackgroundColour
= colour
;
3816 m_rowLabelWin
->SetBackgroundColour( colour
);
3817 m_colLabelWin
->SetBackgroundColour( colour
);
3818 m_cornerLabelWin
->SetBackgroundColour( colour
);
3820 if ( !GetBatchCount() )
3822 m_rowLabelWin
->Refresh();
3823 m_colLabelWin
->Refresh();
3824 m_cornerLabelWin
->Refresh();
3829 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
3831 if ( m_labelTextColour
!= colour
)
3833 m_labelTextColour
= colour
;
3834 if ( !GetBatchCount() )
3836 m_rowLabelWin
->Refresh();
3837 m_colLabelWin
->Refresh();
3842 void wxGrid::SetLabelFont( const wxFont
& font
)
3845 if ( !GetBatchCount() )
3847 m_rowLabelWin
->Refresh();
3848 m_colLabelWin
->Refresh();
3852 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
3854 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3856 m_rowLabelHorizAlign
= horiz
;
3859 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3861 m_rowLabelVertAlign
= vert
;
3864 if ( !GetBatchCount() )
3866 m_rowLabelWin
->Refresh();
3867 m_colLabelWin
->Refresh();
3871 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
3873 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3875 m_colLabelHorizAlign
= horiz
;
3878 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3880 m_colLabelVertAlign
= vert
;
3883 if ( !GetBatchCount() )
3885 m_rowLabelWin
->Refresh();
3886 m_colLabelWin
->Refresh();
3890 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
3894 m_table
->SetRowLabelValue( row
, s
);
3895 if ( !GetBatchCount() )
3897 // TODO: Optimize this
3899 m_rowLabelWin
->Refresh();
3904 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
3908 m_table
->SetColLabelValue( col
, s
);
3909 if ( !GetBatchCount() )
3911 // TODO: optimize this
3913 m_colLabelWin
->Refresh();
3918 void wxGrid::SetGridLineColour( const wxColour
& colour
)
3920 if ( m_gridLineColour
!= colour
)
3922 m_gridLineColour
= colour
;
3924 wxClientDC
dc( m_gridWin
);
3926 DrawAllGridLines( dc
);
3930 void wxGrid::EnableGridLines( bool enable
)
3932 if ( enable
!= m_gridLinesEnabled
)
3934 m_gridLinesEnabled
= enable
;
3936 if ( !GetBatchCount() )
3940 wxClientDC
dc( m_gridWin
);
3942 DrawAllGridLines( dc
);
3946 m_gridWin
->Refresh();
3953 int wxGrid::GetDefaultRowSize()
3955 return m_defaultRowHeight
;
3958 int wxGrid::GetRowSize( int row
)
3960 if ( row
>= 0 && row
< m_numRows
)
3961 return m_rowHeights
[row
];
3963 return 0; // TODO: log an error here
3966 int wxGrid::GetDefaultColSize()
3968 return m_defaultColWidth
;
3971 int wxGrid::GetColSize( int col
)
3973 if ( col
>= 0 && col
< m_numCols
)
3974 return m_colWidths
[col
];
3976 return 0; // TODO: log an error here
3979 wxColour
wxGrid::GetDefaultCellBackgroundColour()
3981 // TODO: replace this temp test code
3983 return wxColour( 255, 255, 255 );
3986 wxColour
wxGrid::GetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
) )
3988 // TODO: replace this temp test code
3990 return wxColour( 255, 255, 255 );
3993 wxColour
wxGrid::GetDefaultCellTextColour()
3995 // TODO: replace this temp test code
3997 return wxColour( 0, 0, 0 );
4000 wxColour
wxGrid::GetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4002 // TODO: replace this temp test code
4004 return wxColour( 0, 0, 0 );
4008 wxFont
wxGrid::GetDefaultCellFont()
4010 return m_defaultCellFont
;
4013 wxFont
wxGrid::GetCellFont( int WXUNUSED(row
), int WXUNUSED(col
) )
4015 // TODO: replace this temp test code
4017 return m_defaultCellFont
;
4020 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
4022 // TODO: replace this temp test code
4028 void wxGrid::GetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int *horiz
, int *vert
)
4030 // TODO: replace this temp test code
4036 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
4038 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
4040 if ( resizeExistingRows
)
4044 for ( row
= 0; row
< m_numRows
; row
++ )
4046 m_rowHeights
[row
] = m_defaultRowHeight
;
4047 bottom
+= m_defaultRowHeight
;
4048 m_rowBottoms
[row
] = bottom
;
4054 void wxGrid::SetRowSize( int row
, int height
)
4058 if ( row
>= 0 && row
< m_numRows
)
4060 int h
= wxMax( 0, height
);
4061 int diff
= h
- m_rowHeights
[row
];
4063 m_rowHeights
[row
] = h
;
4064 for ( i
= row
; i
< m_numRows
; i
++ )
4066 m_rowBottoms
[i
] += diff
;
4070 // Note: we are ending the event *after* doing
4071 // default processing in this case
4073 SendEvent( EVT_GRID_ROW_SIZE
,
4078 // TODO: log an error here
4082 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4084 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4086 if ( resizeExistingCols
)
4090 for ( col
= 0; col
< m_numCols
; col
++ )
4092 m_colWidths
[col
] = m_defaultColWidth
;
4093 right
+= m_defaultColWidth
;
4094 m_colRights
[col
] = right
;
4100 void wxGrid::SetColSize( int col
, int width
)
4104 if ( col
>= 0 && col
< m_numCols
)
4106 int w
= wxMax( 0, width
);
4107 int diff
= w
- m_colWidths
[col
];
4108 m_colWidths
[col
] = w
;
4110 for ( i
= col
; i
< m_numCols
; i
++ )
4112 m_colRights
[i
] += diff
;
4116 // Note: we are ending the event *after* doing
4117 // default processing in this case
4119 SendEvent( EVT_GRID_COL_SIZE
,
4124 // TODO: log an error here
4128 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& )
4130 // TODO: everything !!!
4134 void wxGrid::SetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4136 // TODO: everything !!!
4140 void wxGrid::SetDefaultCellTextColour( const wxColour
& )
4142 // TODO: everything !!!
4146 void wxGrid::SetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4148 // TODO: everything !!!
4152 void wxGrid::SetDefaultCellFont( const wxFont
& )
4154 // TODO: everything !!!
4158 void wxGrid::SetCellFont( int WXUNUSED(row
), int WXUNUSED(col
), const wxFont
& )
4160 // TODO: everything !!!
4164 void wxGrid::SetDefaultCellAlignment( int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4166 // TODO: everything !!!
4170 void wxGrid::SetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4172 // TODO: everything !!!
4179 // ------ cell value accessor functions
4182 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4186 m_table
->SetValue( row
, col
, s
.c_str() );
4187 if ( !GetBatchCount() )
4189 wxClientDC
dc( m_gridWin
);
4191 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4194 #if 0 // TODO: edit in place
4196 if ( m_currentCellCoords
.GetRow() == row
&&
4197 m_currentCellCoords
.GetCol() == col
)
4199 SetEditControlValue( s
);
4208 // ------ Block, row and col selection
4211 void wxGrid::SelectRow( int row
, bool addToSelected
)
4215 if ( IsSelection() && addToSelected
)
4217 if ( m_selectedTopLeft
.GetRow() > row
)
4218 m_selectedTopLeft
.SetRow( row
);
4220 m_selectedTopLeft
.SetCol( 0 );
4222 if ( m_selectedBottomRight
.GetRow() < row
)
4223 m_selectedBottomRight
.SetRow( row
);
4225 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4227 // TODO: optimize this so that we only refresh the newly
4230 r
= SelectionToDeviceRect();
4231 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4235 r
= SelectionToDeviceRect();
4237 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4239 m_selectedTopLeft
.Set( row
, 0 );
4240 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4241 r
= SelectionToDeviceRect();
4242 m_gridWin
->Refresh( TRUE
, &r
);
4245 wxGridRangeSelectEvent
gridEvt( GetId(),
4246 EVT_GRID_RANGE_SELECT
,
4249 m_selectedBottomRight
);
4251 GetEventHandler()->ProcessEvent(gridEvt
);
4255 void wxGrid::SelectCol( int col
, bool addToSelected
)
4259 if ( IsSelection() && addToSelected
)
4261 if ( m_selectedTopLeft
.GetCol() > col
)
4262 m_selectedTopLeft
.SetCol( col
);
4264 m_selectedTopLeft
.SetRow( 0 );
4266 if ( m_selectedBottomRight
.GetCol() < col
)
4267 m_selectedBottomRight
.SetCol( col
);
4269 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4271 // TODO: optimize this so that we only refresh the newly
4274 r
= SelectionToDeviceRect();
4275 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4279 r
= SelectionToDeviceRect();
4281 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4283 m_selectedTopLeft
.Set( 0, col
);
4284 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4285 r
= SelectionToDeviceRect();
4286 m_gridWin
->Refresh( TRUE
, &r
);
4289 wxGridRangeSelectEvent
gridEvt( GetId(),
4290 EVT_GRID_RANGE_SELECT
,
4293 m_selectedBottomRight
);
4295 GetEventHandler()->ProcessEvent(gridEvt
);
4299 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4302 bool changed
= false;
4303 wxGridCellCoords updateTopLeft
, updateBottomRight
;
4305 if ( topRow
> bottomRow
)
4312 if ( leftCol
> rightCol
)
4319 updateTopLeft
= m_selectedTopLeft
;
4320 if (m_selectedTopLeft
!= wxGridCellCoords( topRow
, leftCol
) )
4322 m_selectedTopLeft
= wxGridCellCoords( topRow
, leftCol
);
4323 if (updateTopLeft
== wxGridNoCellCoords
)
4325 updateTopLeft
= m_selectedTopLeft
;
4329 if(updateTopLeft
.GetRow() > topRow
)
4330 updateTopLeft
.SetRow(topRow
);
4331 if (updateTopLeft
.GetCol() > leftCol
)
4332 updateTopLeft
.SetCol(leftCol
);
4337 updateBottomRight
= m_selectedBottomRight
;
4338 if (m_selectedBottomRight
!= wxGridCellCoords( bottomRow
, rightCol
) )
4340 m_selectedBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
4341 if (updateBottomRight
== wxGridNoCellCoords
)
4343 updateBottomRight
= m_selectedBottomRight
;
4347 if (updateBottomRight
.GetRow() < bottomRow
)
4348 updateBottomRight
.SetRow(bottomRow
);
4349 if (updateBottomRight
.GetCol() < rightCol
)
4350 updateBottomRight
.SetCol(rightCol
);
4357 wxRect
r( BlockToDeviceRect( updateTopLeft
, updateBottomRight
) );
4358 m_gridWin
->Refresh( TRUE
, &r
);
4361 // only generate an event if the block is not being selected by
4362 // dragging the mouse (in which case the event will be generated in
4363 // the mouse event handler)
4364 if ( !m_isDragging
)
4366 wxGridRangeSelectEvent
gridEvt( GetId(),
4367 EVT_GRID_RANGE_SELECT
,
4370 m_selectedBottomRight
);
4372 GetEventHandler()->ProcessEvent(gridEvt
);
4376 void wxGrid::SelectAll()
4378 m_selectedTopLeft
.Set( 0, 0 );
4379 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
4381 m_gridWin
->Refresh();
4385 void wxGrid::ClearSelection()
4387 m_selectedTopLeft
= wxGridNoCellCoords
;
4388 m_selectedBottomRight
= wxGridNoCellCoords
;
4392 // This function returns the rectangle that encloses the given block
4393 // in device coords clipped to the client size of the grid window.
4395 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
4396 const wxGridCellCoords
&bottomRight
)
4398 wxRect
rect( wxGridNoCellRect
);
4401 cellRect
= CellToRect( topLeft
);
4402 if ( cellRect
!= wxGridNoCellRect
)
4408 rect
= wxRect( 0, 0, 0, 0 );
4411 cellRect
= CellToRect( bottomRight
);
4412 if ( cellRect
!= wxGridNoCellRect
)
4418 return wxGridNoCellRect
;
4421 // convert to scrolled coords
4423 int left
, top
, right
, bottom
;
4424 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4425 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4428 m_gridWin
->GetClientSize( &cw
, &ch
);
4430 rect
.SetLeft( wxMax(0, left
) );
4431 rect
.SetTop( wxMax(0, top
) );
4432 rect
.SetRight( wxMin(cw
, right
) );
4433 rect
.SetBottom( wxMin(ch
, bottom
) );
4441 // ------ Grid event classes
4444 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
4446 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
4447 int row
, int col
, int x
, int y
,
4448 bool control
, bool shift
, bool alt
, bool meta
)
4449 : wxNotifyEvent( type
, id
)
4455 m_control
= control
;
4460 SetEventObject(obj
);
4464 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
4466 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
4467 int rowOrCol
, int x
, int y
,
4468 bool control
, bool shift
, bool alt
, bool meta
)
4469 : wxNotifyEvent( type
, id
)
4471 m_rowOrCol
= rowOrCol
;
4474 m_control
= control
;
4479 SetEventObject(obj
);
4483 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
4485 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
4486 const wxGridCellCoords
& topLeft
,
4487 const wxGridCellCoords
& bottomRight
,
4488 bool control
, bool shift
, bool alt
, bool meta
)
4489 : wxNotifyEvent( type
, id
)
4491 m_topLeft
= topLeft
;
4492 m_bottomRight
= bottomRight
;
4493 m_control
= control
;
4498 SetEventObject(obj
);
4502 #endif // ifndef wxUSE_NEW_GRID