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"
40 //////////////////////////////////////////////////////////////////////
42 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
43 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
45 // this is a magic incantation which must be done!
46 #include "wx/arrimpl.cpp"
48 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
51 //////////////////////////////////////////////////////////////////////
53 // Abstract base class for grid data (the model)
55 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
58 wxGridTableBase::wxGridTableBase()
61 m_view
= (wxGrid
*) NULL
;
64 wxGridTableBase::~wxGridTableBase()
69 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
71 wxLogWarning( wxT("Called grid table class function InsertRows(pos=%d, N=%d)\n"
72 "but your derived table class does not override this function"),
78 bool wxGridTableBase::AppendRows( size_t numRows
)
80 wxLogWarning( wxT("Called grid table class function AppendRows(N=%d)\n"
81 "but your derived table class does not override this function"),
87 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
89 wxLogWarning( wxT("Called grid table class function DeleteRows(pos=%d, N=%d)\n"
90 "but your derived table class does not override this function"),
96 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
98 wxLogWarning( wxT("Called grid table class function InsertCols(pos=%d, N=%d)\n"
99 "but your derived table class does not override this function"),
105 bool wxGridTableBase::AppendCols( size_t numCols
)
107 wxLogWarning( wxT("Called grid table class function AppendCols(N=%d)\n"
108 "but your derived table class does not override this function"),
114 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
116 wxLogWarning( wxT("Called grid table class function DeleteCols(pos=%d, N=%d)\n"
117 "but your derived table class does not override this function"),
124 wxString
wxGridTableBase::GetRowLabelValue( int row
)
131 wxString
wxGridTableBase::GetColLabelValue( int col
)
133 // default col labels are:
134 // cols 0 to 25 : A-Z
135 // cols 26 to 675 : AA-ZZ
142 s
+= ('A' + (char)( col%26
));
144 if ( col
< 0 ) break;
147 // reverse the string...
149 for ( i
= 0; i
< n
; i
++ )
159 //////////////////////////////////////////////////////////////////////
161 // Message class for the grid table to send requests and notifications
165 wxGridTableMessage::wxGridTableMessage()
167 m_table
= (wxGridTableBase
*) NULL
;
173 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
174 int commandInt1
, int commandInt2
)
178 m_comInt1
= commandInt1
;
179 m_comInt2
= commandInt2
;
184 //////////////////////////////////////////////////////////////////////
186 // A basic grid table for string data. An object of this class will
187 // created by wxGrid if you don't specify an alternative table class.
190 WX_DEFINE_OBJARRAY(wxGridStringArray
)
192 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
194 wxGridStringTable::wxGridStringTable()
199 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
204 m_data
.Alloc( numRows
);
208 for ( col
= 0; col
< numCols
; col
++ )
210 sa
.Add( wxEmptyString
);
213 for ( row
= 0; row
< numRows
; row
++ )
219 wxGridStringTable::~wxGridStringTable()
223 long wxGridStringTable::GetNumberRows()
225 return m_data
.GetCount();
228 long wxGridStringTable::GetNumberCols()
230 if ( m_data
.GetCount() > 0 )
231 return m_data
[0].GetCount();
236 wxString
wxGridStringTable::GetValue( int row
, int col
)
238 // TODO: bounds checking
240 return m_data
[row
][col
];
243 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
245 // TODO: bounds checking
247 m_data
[row
][col
] = s
;
250 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
252 // TODO: bounds checking
254 return (m_data
[row
][col
] == wxEmptyString
);
258 void wxGridStringTable::Clear()
261 int numRows
, numCols
;
263 numRows
= m_data
.GetCount();
266 numCols
= m_data
[0].GetCount();
268 for ( row
= 0; row
< numRows
; row
++ )
270 for ( col
= 0; col
< numCols
; col
++ )
272 m_data
[row
][col
] = wxEmptyString
;
279 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
283 size_t curNumRows
= m_data
.GetCount();
284 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
286 if ( pos
>= curNumRows
)
288 return AppendRows( numRows
);
292 sa
.Alloc( curNumCols
);
293 for ( col
= 0; col
< curNumCols
; col
++ )
295 sa
.Add( wxEmptyString
);
298 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
300 m_data
.Insert( sa
, row
);
305 wxGridTableMessage
msg( this,
306 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
310 GetView()->ProcessTableMessage( msg
);
316 bool wxGridStringTable::AppendRows( size_t numRows
)
320 size_t curNumRows
= m_data
.GetCount();
321 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
324 if ( curNumCols
> 0 )
326 sa
.Alloc( curNumCols
);
327 for ( col
= 0; col
< curNumCols
; col
++ )
329 sa
.Add( wxEmptyString
);
333 for ( row
= 0; row
< numRows
; row
++ )
340 wxGridTableMessage
msg( this,
341 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
344 GetView()->ProcessTableMessage( msg
);
350 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
354 size_t curNumRows
= m_data
.GetCount();
356 if ( pos
>= curNumRows
)
358 wxLogError( wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)...\n"
359 "Pos value is invalid for present table with %d rows"),
360 pos
, numRows
, curNumRows
);
364 if ( numRows
> curNumRows
- pos
)
366 numRows
= curNumRows
- pos
;
369 if ( numRows
>= curNumRows
)
371 m_data
.Empty(); // don't release memory just yet
375 for ( n
= 0; n
< numRows
; n
++ )
377 m_data
.Remove( pos
);
383 wxGridTableMessage
msg( this,
384 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
388 GetView()->ProcessTableMessage( msg
);
394 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
398 size_t curNumRows
= m_data
.GetCount();
399 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
401 if ( pos
>= curNumCols
)
403 return AppendCols( numCols
);
406 for ( row
= 0; row
< curNumRows
; row
++ )
408 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
410 m_data
[row
].Insert( wxEmptyString
, col
);
416 wxGridTableMessage
msg( this,
417 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
421 GetView()->ProcessTableMessage( msg
);
427 bool wxGridStringTable::AppendCols( size_t numCols
)
431 size_t curNumRows
= m_data
.GetCount();
434 // TODO: something better than this ?
436 wxLogError( wxT("Unable to append cols to a grid table with no rows.\n"
437 "Call AppendRows() first") );
441 for ( row
= 0; row
< curNumRows
; row
++ )
443 for ( n
= 0; n
< numCols
; n
++ )
445 m_data
[row
].Add( wxEmptyString
);
451 wxGridTableMessage
msg( this,
452 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
455 GetView()->ProcessTableMessage( msg
);
461 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
465 size_t curNumRows
= m_data
.GetCount();
466 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
468 if ( pos
>= curNumCols
)
470 wxLogError( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
471 "Pos value is invalid for present table with %d cols"),
472 pos
, numCols
, curNumCols
);
476 if ( numCols
> curNumCols
- pos
)
478 numCols
= curNumCols
- pos
;
481 for ( row
= 0; row
< curNumRows
; row
++ )
483 if ( numCols
>= curNumCols
)
489 for ( n
= 0; n
< numCols
; n
++ )
491 m_data
[row
].Remove( pos
);
498 wxGridTableMessage
msg( this,
499 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
503 GetView()->ProcessTableMessage( msg
);
509 wxString
wxGridStringTable::GetRowLabelValue( int row
)
511 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
513 // using default label
515 return wxGridTableBase::GetRowLabelValue( row
);
519 return m_rowLabels
[ row
];
523 wxString
wxGridStringTable::GetColLabelValue( int col
)
525 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
527 // using default label
529 return wxGridTableBase::GetColLabelValue( col
);
533 return m_colLabels
[ col
];
537 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
539 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
541 int n
= m_rowLabels
.GetCount();
543 for ( i
= n
; i
<= row
; i
++ )
545 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
549 m_rowLabels
[row
] = value
;
552 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
554 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
556 int n
= m_colLabels
.GetCount();
558 for ( i
= n
; i
<= col
; i
++ )
560 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
564 m_colLabels
[col
] = value
;
570 //////////////////////////////////////////////////////////////////////
572 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
574 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
575 EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown
)
579 wxGridTextCtrl::wxGridTextCtrl( wxWindow
*par
,
583 const wxString
& value
,
587 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
590 m_isCellControl
= isCellControl
;
594 void wxGridTextCtrl::OnKeyDown( wxKeyEvent
& event
)
596 switch ( event
.KeyCode() )
599 m_grid
->SetEditControlValue( startValue
);
600 SetInsertionPointEnd();
610 if ( m_isCellControl
)
612 // send the event to the parent grid, skipping the
613 // event if nothing happens
615 event
.Skip( m_grid
->ProcessEvent( event
) );
619 // default text control response within the top edit
628 if ( m_isCellControl
)
630 // send the event to the parent grid, skipping the
631 // event if nothing happens
633 event
.Skip( m_grid
->ProcessEvent( event
) );
637 // default text control response within the top edit
649 void wxGridTextCtrl::SetStartValue( const wxString
& s
)
652 wxTextCtrl::SetValue( s
.c_str() );
657 //////////////////////////////////////////////////////////////////////
659 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
661 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
662 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
663 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
664 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
667 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
669 const wxPoint
&pos
, const wxSize
&size
)
670 : wxWindow( parent
, id
, pos
, size
)
675 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
679 // NO - don't do this because it will set both the x and y origin
680 // coords to match the parent scrolled window and we just want to
681 // set the y coord - MB
683 // m_owner->PrepareDC( dc );
686 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
687 dc
.SetDeviceOrigin( 0, -y
);
689 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
690 m_owner
->DrawRowLabels( dc
);
694 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
696 m_owner
->ProcessRowLabelMouseEvent( event
);
700 // This seems to be required for wxMotif otherwise the mouse
701 // cursor must be in the cell edit control to get key events
703 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
705 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
710 //////////////////////////////////////////////////////////////////////
712 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
714 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
715 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
716 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
717 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
720 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
722 const wxPoint
&pos
, const wxSize
&size
)
723 : wxWindow( parent
, id
, pos
, size
)
728 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
732 // NO - don't do this because it will set both the x and y origin
733 // coords to match the parent scrolled window and we just want to
734 // set the x coord - MB
736 // m_owner->PrepareDC( dc );
739 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
740 dc
.SetDeviceOrigin( -x
, 0 );
742 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
743 m_owner
->DrawColLabels( dc
);
747 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
749 m_owner
->ProcessColLabelMouseEvent( event
);
753 // This seems to be required for wxMotif otherwise the mouse
754 // cursor must be in the cell edit control to get key events
756 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
758 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
763 //////////////////////////////////////////////////////////////////////
765 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
767 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
768 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
769 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
772 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
774 const wxPoint
&pos
, const wxSize
&size
)
775 : wxWindow( parent
, id
, pos
, size
, wxRAISED_BORDER
)
781 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
783 m_owner
->ProcessCornerLabelMouseEvent( event
);
787 // This seems to be required for wxMotif otherwise the mouse
788 // cursor must be in the cell edit control to get key events
790 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
792 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
797 //////////////////////////////////////////////////////////////////////
799 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
801 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
802 EVT_PAINT( wxGridWindow::OnPaint
)
803 EVT_SCROLLWIN( wxGridWindow::ScrollWindow
)
804 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
805 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
808 wxGridWindow::wxGridWindow( wxGrid
*parent
,
809 wxGridRowLabelWindow
*rowLblWin
,
810 wxGridColLabelWindow
*colLblWin
,
811 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
812 : wxPanel( parent
, id
, pos
, size
, wxSUNKEN_BORDER
, "grid window" )
815 m_rowLabelWin
= rowLblWin
;
816 m_colLabelWin
= colLblWin
;
818 SetBackgroundColour( "WHITE" );
822 wxGridWindow::~wxGridWindow()
827 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
829 wxPaintDC
dc( this );
830 m_owner
->PrepareDC( dc
);
832 m_owner
->CalcCellsExposed( GetUpdateRegion() );
833 m_owner
->DrawGridCellArea( dc
);
837 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
839 wxPanel::ScrollWindow( dx
, dy
, rect
);
840 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
841 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
845 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
847 m_owner
->ProcessGridCellMouseEvent( event
);
851 // This seems to be required for wxMotif otherwise the mouse
852 // cursor must be in the cell edit control to get key events
854 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
856 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
861 //////////////////////////////////////////////////////////////////////
863 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
865 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
866 EVT_PAINT( wxGrid::OnPaint
)
867 EVT_SIZE( wxGrid::OnSize
)
868 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
871 wxGrid::wxGrid( wxWindow
*parent
,
876 const wxString
& name
)
877 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
881 int colLblH
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
882 int rowLblW
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
884 m_rowLabelWin
= new wxGridRowLabelWindow( this,
887 wxSize(rowLblW
,-1) );
889 m_colLabelWin
= new wxGridColLabelWindow( this,
892 wxSize(-1, colLblH
) );
894 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
897 wxSize(rowLblW
, colLblH
) );
899 m_gridWin
= new wxGridWindow( this,
906 SetTargetWindow( m_gridWin
);
908 m_mainSizer
= new wxBoxSizer( wxVERTICAL
);
910 m_topSizer
= new wxBoxSizer( wxHORIZONTAL
);
911 m_topSizer
->Add( m_cornerLabelWin
, 0 );
912 m_topSizer
->Add( m_colLabelWin
, 1 );
914 m_mainSizer
->Add( m_topSizer
, 0, wxEXPAND
);
916 m_middleSizer
= new wxBoxSizer( wxHORIZONTAL
);
917 m_middleSizer
->Add( m_rowLabelWin
, 0, wxEXPAND
);
918 m_middleSizer
->Add( m_gridWin
, 1, wxEXPAND
);
920 m_mainSizer
->Add( m_middleSizer
, 1, wxEXPAND
);
922 SetAutoLayout( TRUE
);
923 SetSizer( m_mainSizer
);
933 // ----- internal init and update functions
936 void wxGrid::Create()
938 m_table
= (wxGridTableBase
*) NULL
;
939 m_gridWin
= (wxGridWindow
*) NULL
;
940 m_rowLabelWin
= (wxGridRowLabelWindow
*) NULL
;
941 m_colLabelWin
= (wxGridColLabelWindow
*) NULL
;
942 m_cornerLabelWin
= (wxGridCornerLabelWindow
*) NULL
;
943 m_cellEditCtrl
= (wxWindow
*) NULL
;
947 bool wxGrid::CreateGrid( int numRows
, int numCols
)
951 wxLogError( wxT("wxGrid::CreateGrid(numRows, numCols) called more than once") );
959 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
960 m_table
->SetView( this );
973 if ( m_numRows
<= 0 )
974 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
976 if ( m_numCols
<= 0 )
977 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
979 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
980 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
984 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
988 m_labelBackgroundColour
= wxColour( _T("WHITE") );
991 m_labelTextColour
= wxColour( _T("BLACK") );
993 // TODO: something better than this ?
995 m_labelFont
= this->GetFont();
996 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
998 m_rowLabelHorizAlign
= wxLEFT
;
999 m_rowLabelVertAlign
= wxCENTRE
;
1001 m_colLabelHorizAlign
= wxCENTRE
;
1002 m_colLabelVertAlign
= wxTOP
;
1004 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1005 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1007 #if defined (__WXMOTIF__) // see also text ctrl sizing in ShowCellEditControl()
1008 m_defaultRowHeight
+= 8;
1010 m_defaultRowHeight
+= 4;
1013 m_rowHeights
.Alloc( m_numRows
);
1014 m_rowBottoms
.Alloc( m_numRows
);
1016 for ( i
= 0; i
< m_numRows
; i
++ )
1018 m_rowHeights
.Add( m_defaultRowHeight
);
1019 rowBottom
+= m_defaultRowHeight
;
1020 m_rowBottoms
.Add( rowBottom
);
1023 m_colWidths
.Alloc( m_numCols
);
1024 m_colRights
.Alloc( m_numCols
);
1026 for ( i
= 0; i
< m_numCols
; i
++ )
1028 m_colWidths
.Add( m_defaultColWidth
);
1029 colRight
+= m_defaultColWidth
;
1030 m_colRights
.Add( colRight
);
1033 // TODO: improve this ?
1035 m_defaultCellFont
= this->GetFont();
1037 m_gridLineColour
= wxColour( 128, 128, 255 );
1038 m_gridLinesEnabled
= TRUE
;
1040 m_cursorMode
= WXGRID_CURSOR_DEFAULT
;
1042 m_dragRowOrCol
= -1;
1043 m_isDragging
= FALSE
;
1045 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1046 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1048 m_currentCellCoords
= wxGridNoCellCoords
;
1050 m_selectedTopLeft
= wxGridNoCellCoords
;
1051 m_selectedBottomRight
= wxGridNoCellCoords
;
1053 m_editable
= TRUE
; // default for whole grid
1055 m_inOnKeyDown
= FALSE
;
1058 // TODO: extend this to other types of controls
1060 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1068 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1072 m_cellEditCtrl
->Show( FALSE
);
1073 m_cellEditCtrlEnabled
= TRUE
;
1074 m_editCtrlType
= wxGRID_TEXTCTRL
;
1078 void wxGrid::CalcDimensions()
1081 GetClientSize( &cw
, &ch
);
1083 if ( m_numRows
> 0 && m_numCols
> 0 )
1085 int right
= m_colRights
[ m_numCols
-1 ] + 20;
1086 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 20;
1088 // TODO: restore the scroll position that we had before sizing
1091 GetViewStart( &x
, &y
);
1092 SetScrollbars( 10, 10,
1093 right
/10, bottom
/10,
1099 // this is called when the grid table sends a message to say that it
1100 // has been redimensioned
1102 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1106 switch ( msg
.GetId() )
1108 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1110 size_t pos
= msg
.GetCommandInt();
1111 int numRows
= msg
.GetCommandInt2();
1112 for ( i
= 0; i
< numRows
; i
++ )
1114 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1115 m_rowBottoms
.Insert( 0, pos
);
1117 m_numRows
+= numRows
;
1120 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1122 for ( i
= pos
; i
< m_numRows
; i
++ )
1124 bottom
+= m_rowHeights
[i
];
1125 m_rowBottoms
[i
] = bottom
;
1131 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1133 int numRows
= msg
.GetCommandInt();
1134 for ( i
= 0; i
< numRows
; i
++ )
1136 m_rowHeights
.Add( m_defaultRowHeight
);
1137 m_rowBottoms
.Add( 0 );
1140 int oldNumRows
= m_numRows
;
1141 m_numRows
+= numRows
;
1144 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1146 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1148 bottom
+= m_rowHeights
[i
];
1149 m_rowBottoms
[i
] = bottom
;
1155 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1157 size_t pos
= msg
.GetCommandInt();
1158 int numRows
= msg
.GetCommandInt2();
1159 for ( i
= 0; i
< numRows
; i
++ )
1161 m_rowHeights
.Remove( pos
);
1162 m_rowBottoms
.Remove( pos
);
1164 m_numRows
-= numRows
;
1169 m_colWidths
.Clear();
1170 m_colRights
.Clear();
1171 m_currentCellCoords
= wxGridNoCellCoords
;
1175 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1176 m_currentCellCoords
.Set( 0, 0 );
1179 for ( i
= 0; i
< m_numRows
; i
++ )
1181 h
+= m_rowHeights
[i
];
1182 m_rowBottoms
[i
] = h
;
1190 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1192 size_t pos
= msg
.GetCommandInt();
1193 int numCols
= msg
.GetCommandInt2();
1194 for ( i
= 0; i
< numCols
; i
++ )
1196 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1197 m_colRights
.Insert( 0, pos
);
1199 m_numCols
+= numCols
;
1202 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1204 for ( i
= pos
; i
< m_numCols
; i
++ )
1206 right
+= m_colWidths
[i
];
1207 m_colRights
[i
] = right
;
1213 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1215 int numCols
= msg
.GetCommandInt();
1216 for ( i
= 0; i
< numCols
; i
++ )
1218 m_colWidths
.Add( m_defaultColWidth
);
1219 m_colRights
.Add( 0 );
1222 int oldNumCols
= m_numCols
;
1223 m_numCols
+= numCols
;
1226 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1228 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1230 right
+= m_colWidths
[i
];
1231 m_colRights
[i
] = right
;
1237 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1239 size_t pos
= msg
.GetCommandInt();
1240 int numCols
= msg
.GetCommandInt2();
1241 for ( i
= 0; i
< numCols
; i
++ )
1243 m_colWidths
.Remove( pos
);
1244 m_colRights
.Remove( pos
);
1246 m_numCols
-= numCols
;
1250 #if 0 // leave the row alone here so that AppendCols will work subsequently
1252 m_rowHeights
.Clear();
1253 m_rowBottoms
.Clear();
1255 m_currentCellCoords
= wxGridNoCellCoords
;
1259 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1260 m_currentCellCoords
.Set( 0, 0 );
1263 for ( i
= 0; i
< m_numCols
; i
++ )
1265 w
+= m_colWidths
[i
];
1278 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
1280 wxRegionIterator
iter( reg
);
1283 m_rowLabelsExposed
.Empty();
1290 // TODO: remove this when we can...
1291 // There is a bug in wxMotif that gives garbage update
1292 // rectangles if you jump-scroll a long way by clicking the
1293 // scrollbar with middle button. This is a work-around
1295 #if defined(__WXMOTIF__)
1297 m_gridWin
->GetClientSize( &cw
, &ch
);
1298 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1299 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1302 // logical bounds of update region
1305 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1306 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1308 // find the row labels within these bounds
1312 for ( row
= 0; row
< m_numRows
; row
++ )
1314 if ( m_rowBottoms
[row
] < top
) continue;
1316 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1317 if ( rowTop
> bottom
) break;
1319 m_rowLabelsExposed
.Add( row
);
1327 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
1329 wxRegionIterator
iter( reg
);
1332 m_colLabelsExposed
.Empty();
1339 // TODO: remove this when we can...
1340 // There is a bug in wxMotif that gives garbage update
1341 // rectangles if you jump-scroll a long way by clicking the
1342 // scrollbar with middle button. This is a work-around
1344 #if defined(__WXMOTIF__)
1346 m_gridWin
->GetClientSize( &cw
, &ch
);
1347 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1348 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1351 // logical bounds of update region
1354 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1355 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1357 // find the cells within these bounds
1361 for ( col
= 0; col
< m_numCols
; col
++ )
1363 if ( m_colRights
[col
] < left
) continue;
1365 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1366 if ( colLeft
> right
) break;
1368 m_colLabelsExposed
.Add( col
);
1376 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
1378 wxRegionIterator
iter( reg
);
1381 m_cellsExposed
.Empty();
1382 m_rowsExposed
.Empty();
1383 m_colsExposed
.Empty();
1385 int left
, top
, right
, bottom
;
1390 // TODO: remove this when we can...
1391 // There is a bug in wxMotif that gives garbage update
1392 // rectangles if you jump-scroll a long way by clicking the
1393 // scrollbar with middle button. This is a work-around
1395 #if defined(__WXMOTIF__)
1397 m_gridWin
->GetClientSize( &cw
, &ch
);
1398 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1399 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1400 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1401 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1404 // logical bounds of update region
1406 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1407 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1409 // find the cells within these bounds
1412 int colLeft
, rowTop
;
1413 for ( row
= 0; row
< m_numRows
; row
++ )
1415 if ( m_rowBottoms
[row
] < top
) continue;
1417 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1418 if ( rowTop
> bottom
) break;
1420 m_rowsExposed
.Add( row
);
1422 for ( col
= 0; col
< m_numCols
; col
++ )
1424 if ( m_colRights
[col
] < left
) continue;
1426 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1427 if ( colLeft
> right
) break;
1429 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1430 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1439 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1442 wxPoint
pos( event
.GetPosition() );
1443 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1445 if ( event
.Dragging() )
1447 m_isDragging
= TRUE
;
1449 if ( event
.LeftIsDown() )
1451 switch( m_cursorMode
)
1453 case WXGRID_CURSOR_RESIZE_ROW
:
1455 int cw
, ch
, left
, dummy
;
1456 m_gridWin
->GetClientSize( &cw
, &ch
);
1457 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1459 wxClientDC
dc( m_gridWin
);
1461 dc
.SetLogicalFunction(wxXOR
);
1462 if ( m_dragLastPos
>= 0 )
1464 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1466 dc
.DrawLine( left
, y
, left
+cw
, y
);
1471 case WXGRID_CURSOR_SELECT_ROW
:
1473 if ( (row
= YToRow( y
)) >= 0 &&
1474 !IsInSelection( row
, 0 ) )
1476 SelectRow( row
, TRUE
);
1485 m_isDragging
= FALSE
;
1488 // ------------ Left button pressed
1490 if ( event
.LeftDown() )
1492 // don't send a label click event for a hit on the
1493 // edge of the row label - this is probably the user
1494 // wanting to resize the row
1496 if ( YToEdgeOfRow(y
) < 0 )
1499 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
1501 SelectRow( row
, event
.ShiftDown() );
1502 m_cursorMode
= WXGRID_CURSOR_SELECT_ROW
;
1507 // starting to drag-resize a row
1509 m_rowLabelWin
->CaptureMouse();
1514 // ------------ Left double click
1516 else if (event
.LeftDClick() )
1518 if ( YToEdgeOfRow(y
) < 0 )
1521 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
1526 // ------------ Left button released
1528 else if ( event
.LeftUp() )
1530 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
1532 m_rowLabelWin
->ReleaseMouse();
1534 if ( m_dragLastPos
>= 0 )
1536 // erase the last line and resize the row
1538 int cw
, ch
, left
, dummy
;
1539 m_gridWin
->GetClientSize( &cw
, &ch
);
1540 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1542 wxClientDC
dc( m_gridWin
);
1544 dc
.SetLogicalFunction( wxINVERT
);
1545 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1546 HideCellEditControl();
1548 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
1549 SetRowSize( m_dragRowOrCol
, wxMax( y
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
1550 if ( !GetBatchCount() )
1552 // TODO: optimize this
1553 m_rowLabelWin
->Refresh();
1554 m_gridWin
->Refresh();
1557 ShowCellEditControl();
1559 // Note: we are ending the event *after* doing
1560 // default processing in this case
1562 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1570 // ------------ Right button down
1572 else if ( event
.RightDown() )
1575 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1577 // no default action at the moment
1582 // ------------ Right double click
1584 else if ( event
.RightDClick() )
1587 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1589 // no default action at the moment
1594 // ------------ No buttons down and mouse moving
1596 else if ( event
.Moving() )
1598 m_dragRowOrCol
= YToEdgeOfRow( y
);
1599 if ( m_dragRowOrCol
>= 0 )
1601 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1603 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1604 m_rowLabelWin
->SetCursor( m_rowResizeCursor
);
1609 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1611 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1612 m_rowLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1619 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
1622 wxPoint
pos( event
.GetPosition() );
1623 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1625 if ( event
.Dragging() )
1627 m_isDragging
= TRUE
;
1629 if ( event
.LeftIsDown() )
1631 switch( m_cursorMode
)
1633 case WXGRID_CURSOR_RESIZE_COL
:
1635 int cw
, ch
, dummy
, top
;
1636 m_gridWin
->GetClientSize( &cw
, &ch
);
1637 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1639 wxClientDC
dc( m_gridWin
);
1641 dc
.SetLogicalFunction(wxXOR
);
1642 if ( m_dragLastPos
>= 0 )
1644 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1646 dc
.DrawLine( x
, top
, x
, top
+ch
);
1651 case WXGRID_CURSOR_SELECT_COL
:
1653 if ( (col
= XToCol( x
)) >= 0 &&
1654 !IsInSelection( 0, col
) )
1656 SelectCol( col
, TRUE
);
1665 m_isDragging
= FALSE
;
1668 // ------------ Left button pressed
1670 if ( event
.LeftDown() )
1672 // don't send a label click event for a hit on the
1673 // edge of the col label - this is probably the user
1674 // wanting to resize the col
1676 if ( XToEdgeOfCol(x
) < 0 )
1679 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
1681 SelectCol( col
, event
.ShiftDown() );
1682 m_cursorMode
= WXGRID_CURSOR_SELECT_COL
;
1687 // starting to drag-resize a col
1689 m_colLabelWin
->CaptureMouse();
1694 // ------------ Left double click
1696 if ( event
.LeftDClick() )
1698 if ( XToEdgeOfCol(x
) < 0 )
1701 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
1706 // ------------ Left button released
1708 else if ( event
.LeftUp() )
1710 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
1712 m_colLabelWin
->ReleaseMouse();
1714 if ( m_dragLastPos
>= 0 )
1716 // erase the last line and resize the col
1718 int cw
, ch
, dummy
, top
;
1719 m_gridWin
->GetClientSize( &cw
, &ch
);
1720 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1722 wxClientDC
dc( m_gridWin
);
1724 dc
.SetLogicalFunction( wxINVERT
);
1725 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1726 HideCellEditControl();
1728 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
1729 SetColSize( m_dragRowOrCol
, wxMax( x
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
1731 if ( !GetBatchCount() )
1733 // TODO: optimize this
1734 m_colLabelWin
->Refresh();
1735 m_gridWin
->Refresh();
1738 ShowCellEditControl();
1740 // Note: we are ending the event *after* doing
1741 // default processing in this case
1743 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
1751 // ------------ Right button down
1753 else if ( event
.RightDown() )
1756 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
1758 // no default action at the moment
1763 // ------------ Right double click
1765 else if ( event
.RightDClick() )
1768 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
1770 // no default action at the moment
1775 // ------------ No buttons down and mouse moving
1777 else if ( event
.Moving() )
1779 m_dragRowOrCol
= XToEdgeOfCol( x
);
1780 if ( m_dragRowOrCol
>= 0 )
1782 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1784 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
1785 m_colLabelWin
->SetCursor( m_colResizeCursor
);
1790 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1792 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1793 m_colLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1800 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
1802 if ( event
.LeftDown() )
1804 // indicate corner label by having both row and
1807 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
1813 else if ( event
.LeftDClick() )
1815 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
1818 else if ( event
.RightDown() )
1820 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
1822 // no default action at the moment
1826 else if ( event
.RightDClick() )
1828 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
1830 // no default action at the moment
1836 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
1839 wxPoint
pos( event
.GetPosition() );
1840 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1842 wxGridCellCoords coords
;
1843 XYToCell( x
, y
, coords
);
1845 if ( event
.Dragging() )
1847 m_isDragging
= TRUE
;
1849 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1851 if ( coords
!= wxGridNoCellCoords
)
1853 if ( !IsSelection() )
1855 SelectBlock( coords
, coords
);
1859 if ( !IsInSelection( coords
) )
1860 SelectBlock( m_currentCellCoords
, coords
);
1868 m_isDragging
= FALSE
;
1870 if ( event
.LeftDown() )
1872 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
1877 MakeCellVisible( coords
);
1878 SetCurrentCell( coords
);
1883 // ------------ Left double click
1885 else if ( event
.LeftDClick() )
1887 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
1894 // ------------ Left button released
1896 else if ( event
.LeftUp() )
1898 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1900 if ( IsSelection() )
1902 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
1910 // ------------ Right button down
1912 else if ( event
.RightDown() )
1914 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
1919 // no default action at the moment
1924 // ------------ Right double click
1926 else if ( event
.RightDClick() )
1928 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
1933 // no default action at the moment
1940 // ------ interaction with data model
1942 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
1944 switch ( msg
.GetId() )
1946 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
1947 return GetModelValues();
1949 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
1950 return SetModelValues();
1952 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1953 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1954 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1955 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1956 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1957 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1958 return Redimension( msg
);
1967 // The behaviour of this function depends on the grid table class
1968 // Clear() function. For the default wxGridStringTable class the
1969 // behavious is to replace all cell contents with wxEmptyString but
1970 // not to change the number of rows or cols.
1972 void wxGrid::ClearGrid()
1977 SetEditControlValue();
1978 if ( !GetBatchCount() ) m_gridWin
->Refresh();
1983 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
1985 // TODO: something with updateLabels flag
1989 wxLogError( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
1995 bool ok
= m_table
->InsertRows( pos
, numRows
);
1997 // the table will have sent the results of the insert row
1998 // operation to this view object as a grid table message
2002 if ( m_numCols
== 0 )
2004 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2006 // TODO: perhaps instead of appending the default number of cols
2007 // we should remember what the last non-zero number of cols was ?
2011 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2013 // if we have just inserted cols into an empty grid the current
2014 // cell will be undefined...
2016 SetCurrentCell( 0, 0 );
2020 if ( !GetBatchCount() ) Refresh();
2023 SetEditControlValue();
2033 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2035 // TODO: something with updateLabels flag
2039 wxLogError( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2043 if ( m_table
&& m_table
->AppendRows( numRows
) )
2045 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2047 // if we have just inserted cols into an empty grid the current
2048 // cell will be undefined...
2050 SetCurrentCell( 0, 0 );
2053 // the table will have sent the results of the append row
2054 // operation to this view object as a grid table message
2057 if ( !GetBatchCount() ) Refresh();
2067 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2069 // TODO: something with updateLabels flag
2073 wxLogError( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2077 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2079 // the table will have sent the results of the delete row
2080 // operation to this view object as a grid table message
2082 if ( m_numRows
> 0 )
2083 SetEditControlValue();
2085 HideCellEditControl();
2088 if ( !GetBatchCount() ) Refresh();
2098 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2100 // TODO: something with updateLabels flag
2104 wxLogError( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2110 HideCellEditControl();
2111 bool ok
= m_table
->InsertCols( pos
, numCols
);
2113 // the table will have sent the results of the insert col
2114 // operation to this view object as a grid table message
2118 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2120 // if we have just inserted cols into an empty grid the current
2121 // cell will be undefined...
2123 SetCurrentCell( 0, 0 );
2127 if ( !GetBatchCount() ) Refresh();
2130 SetEditControlValue();
2140 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2142 // TODO: something with updateLabels flag
2146 wxLogError( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2150 if ( m_table
&& m_table
->AppendCols( numCols
) )
2152 // the table will have sent the results of the append col
2153 // operation to this view object as a grid table message
2155 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2157 // if we have just inserted cols into an empty grid the current
2158 // cell will be undefined...
2160 SetCurrentCell( 0, 0 );
2164 if ( !GetBatchCount() ) Refresh();
2174 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2176 // TODO: something with updateLabels flag
2180 wxLogError( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2184 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2186 // the table will have sent the results of the delete col
2187 // operation to this view object as a grid table message
2189 if ( m_numCols
> 0 )
2190 SetEditControlValue();
2192 HideCellEditControl();
2195 if ( !GetBatchCount() ) Refresh();
2207 // ----- event handlers
2210 // Generate a grid event based on a mouse event and
2211 // return the result of ProcessEvent()
2213 bool wxGrid::SendEvent( const wxEventType type
,
2215 wxMouseEvent
& mouseEv
)
2217 if ( type
== EVT_GRID_ROW_SIZE
||
2218 type
== EVT_GRID_COL_SIZE
)
2220 int rowOrCol
= (row
== -1 ? col
: row
);
2222 wxGridSizeEvent
gridEvt( GetId(),
2226 mouseEv
.GetX(), mouseEv
.GetY(),
2227 mouseEv
.ControlDown(),
2228 mouseEv
.ShiftDown(),
2230 mouseEv
.MetaDown() );
2232 return GetEventHandler()->ProcessEvent(gridEvt
);
2234 else if ( type
== EVT_GRID_RANGE_SELECT
)
2236 wxGridRangeSelectEvent
gridEvt( GetId(),
2240 m_selectedBottomRight
,
2241 mouseEv
.ControlDown(),
2242 mouseEv
.ShiftDown(),
2244 mouseEv
.MetaDown() );
2246 return GetEventHandler()->ProcessEvent(gridEvt
);
2250 wxGridEvent
gridEvt( GetId(),
2254 mouseEv
.GetX(), mouseEv
.GetY(),
2255 mouseEv
.ControlDown(),
2256 mouseEv
.ShiftDown(),
2258 mouseEv
.MetaDown() );
2260 return GetEventHandler()->ProcessEvent(gridEvt
);
2265 // Generate a grid event of specified type and return the result
2266 // of ProcessEvent().
2268 bool wxGrid::SendEvent( const wxEventType type
,
2271 if ( type
== EVT_GRID_ROW_SIZE
||
2272 type
== EVT_GRID_COL_SIZE
)
2274 int rowOrCol
= (row
== -1 ? col
: row
);
2276 wxGridSizeEvent
gridEvt( GetId(),
2281 return GetEventHandler()->ProcessEvent(gridEvt
);
2285 wxGridEvent
gridEvt( GetId(),
2290 return GetEventHandler()->ProcessEvent(gridEvt
);
2295 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2297 wxPaintDC
dc( this );
2299 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2300 m_numRows
&& m_numCols
)
2302 m_currentCellCoords
.Set(0, 0);
2303 SetEditControlValue();
2304 ShowCellEditControl();
2309 // This is just here to make sure that CalcDimensions gets called when
2310 // the grid view is resized... then the size event is skipped to allow
2311 // the box sizers to handle everything
2313 void wxGrid::OnSize( wxSizeEvent
& event
)
2320 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2322 if ( m_inOnKeyDown
)
2324 // shouldn't be here - we are going round in circles...
2326 wxLogFatalError( wxT("wxGrid::OnKeyDown called while alread active") );
2329 m_inOnKeyDown
= TRUE
;
2331 // propagate the event up and see if it gets processed
2333 wxWindow
*parent
= GetParent();
2334 wxKeyEvent
keyEvt( event
);
2335 keyEvt
.SetEventObject( parent
);
2337 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2339 // try local handlers
2341 switch ( event
.KeyCode() )
2344 if ( event
.ControlDown() )
2346 MoveCursorUpBlock();
2355 if ( event
.ControlDown() )
2357 MoveCursorDownBlock();
2366 if ( event
.ControlDown() )
2368 MoveCursorLeftBlock();
2377 if ( event
.ControlDown() )
2379 MoveCursorRightBlock();
2392 if ( event
.ControlDown() )
2394 MakeCellVisible( 0, 0 );
2395 SetCurrentCell( 0, 0 );
2404 if ( event
.ControlDown() )
2406 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2407 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2424 // now try the cell edit control
2426 if ( IsCellEditControlEnabled() )
2428 event
.SetEventObject( m_cellEditCtrl
);
2429 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2435 m_inOnKeyDown
= FALSE
;
2439 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
2441 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
2443 // the event has been intercepted - do nothing
2447 wxClientDC
dc( m_gridWin
);
2450 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
2452 HideCellEditControl();
2453 SaveEditControlValue();
2456 m_currentCellCoords
= coords
;
2458 SetEditControlValue();
2459 ShowCellEditControl();
2461 if ( IsSelection() )
2463 wxRect
r( SelectionToDeviceRect() );
2465 if ( !GetBatchCount() ) m_gridWin
->Refresh( TRUE
, &r
);
2471 // ------ functions to get/send data (see also public functions)
2474 bool wxGrid::GetModelValues()
2478 // all we need to do is repaint the grid
2480 m_gridWin
->Refresh();
2488 bool wxGrid::SetModelValues()
2494 for ( row
= 0; row
< m_numRows
; row
++ )
2496 for ( col
= 0; col
< m_numCols
; col
++ )
2498 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
2510 // Note - this function only draws cells that are in the list of
2511 // exposed cells (usually set from the update region by
2512 // CalcExposedCells)
2514 void wxGrid::DrawGridCellArea( wxDC
& dc
)
2516 if ( !m_numRows
|| !m_numCols
) return;
2519 size_t numCells
= m_cellsExposed
.GetCount();
2521 for ( i
= 0; i
< numCells
; i
++ )
2523 DrawCell( dc
, m_cellsExposed
[i
] );
2528 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
2530 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2531 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2533 if ( m_gridLinesEnabled
)
2534 DrawCellBorder( dc
, coords
);
2536 DrawCellBackground( dc
, coords
);
2538 // TODO: separate functions here for different kinds of cells ?
2541 DrawCellValue( dc
, coords
);
2545 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
2547 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2548 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2550 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2551 int row
= coords
.GetRow();
2552 int col
= coords
.GetCol();
2554 // right hand border
2556 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
2557 m_colRights
[col
], m_rowBottoms
[row
] );
2561 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
2562 m_colRights
[col
], m_rowBottoms
[row
] );
2566 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
2568 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2569 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2571 int row
= coords
.GetRow();
2572 int col
= coords
.GetCol();
2574 dc
.SetBackgroundMode( wxSOLID
);
2576 if ( IsInSelection( coords
) )
2578 // TODO: improve this
2580 dc
.SetBrush( *wxBLACK_BRUSH
);
2584 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
2587 dc
.SetPen( *wxTRANSPARENT_PEN
);
2589 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
2590 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
2592 m_rowHeights
[row
]-1 );
2596 void wxGrid::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
2598 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2599 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2601 int row
= coords
.GetRow();
2602 int col
= coords
.GetCol();
2604 dc
.SetBackgroundMode( wxTRANSPARENT
);
2606 if ( IsInSelection( row
, col
) )
2608 // TODO: improve this
2610 dc
.SetTextBackground( wxColour(0, 0, 0) );
2611 dc
.SetTextForeground( wxColour(255, 255, 255) );
2615 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
2616 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
2618 dc
.SetFont( GetCellFont(row
, col
) );
2621 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
2624 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2625 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2626 rect
.SetWidth( m_colWidths
[col
] - 4 );
2627 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2629 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
2634 // TODO: remove this ???
2635 // This is used to redraw all grid lines e.g. when the grid line colour
2638 void wxGrid::DrawAllGridLines( wxDC
& dc
)
2640 if ( !m_gridLinesEnabled
||
2642 !m_numCols
) return;
2645 m_gridWin
->GetClientSize(&cw
, &ch
);
2647 // virtual coords of visible area
2649 int top
, bottom
, left
, right
;
2650 CalcUnscrolledPosition( 0, 0, &left
, &top
);
2651 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
2653 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2655 // horizontal grid lines
2658 for ( i
= 0; i
<= m_numRows
; i
++ )
2660 if ( m_rowBottoms
[i
] > bottom
)
2664 else if ( m_rowBottoms
[i
] >= top
)
2666 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
2671 // vertical grid lines
2673 for ( i
= 0; i
<= m_numCols
; i
++ )
2675 if ( m_colRights
[i
] > right
)
2679 else if ( m_colRights
[i
] >= left
)
2681 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
2687 void wxGrid::DrawRowLabels( wxDC
& dc
)
2689 if ( !m_numRows
|| !m_numCols
) return;
2692 size_t numLabels
= m_rowLabelsExposed
.GetCount();
2694 for ( i
= 0; i
< numLabels
; i
++ )
2696 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
2701 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
2703 if ( m_rowHeights
[row
] <= 0 ) return;
2705 // draw the label's horizontal border (the vertical border is
2706 // provided by the cell area window margin)
2708 dc
.SetPen( *wxBLACK_PEN
);
2710 dc
.DrawLine( 0, m_rowBottoms
[row
]+1,
2711 m_rowLabelWidth
, m_rowBottoms
[row
]+1 );
2713 dc
.SetPen( *wxWHITE_PEN
);
2715 dc
.DrawLine( 0, m_rowBottoms
[row
]+2,
2716 m_rowLabelWidth
, m_rowBottoms
[row
]+2 );
2718 dc
.SetBackgroundMode( wxTRANSPARENT
);
2719 dc
.SetTextForeground( GetLabelTextColour() );
2720 dc
.SetFont( GetLabelFont() );
2723 GetRowLabelAlignment( &hAlign
, &vAlign
);
2727 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2728 rect
.SetWidth( m_rowLabelWidth
- 4 );
2729 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2730 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
2734 void wxGrid::DrawColLabels( wxDC
& dc
)
2736 if ( !m_numRows
|| !m_numCols
) return;
2739 size_t numLabels
= m_colLabelsExposed
.GetCount();
2741 for ( i
= 0; i
< numLabels
; i
++ )
2743 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
2748 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
2750 if ( m_colWidths
[col
] <= 0 ) return;
2752 // draw the label's vertical border (the horizontal border is
2753 // provided by the cell area window margin)
2755 dc
.SetPen( *wxBLACK_PEN
);
2757 dc
.DrawLine( m_colRights
[col
]+1, 0,
2758 m_colRights
[col
]+1, m_colLabelHeight
);
2760 dc
.SetPen( *wxWHITE_PEN
);
2762 dc
.DrawLine( m_colRights
[col
]+2, 0,
2763 m_colRights
[col
]+2, m_colLabelHeight
);
2765 dc
.SetBackgroundMode( wxTRANSPARENT
);
2766 dc
.SetTextForeground( GetLabelTextColour() );
2767 dc
.SetFont( GetLabelFont() );
2770 GetColLabelAlignment( &hAlign
, &vAlign
);
2773 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2775 rect
.SetWidth( m_colWidths
[col
] - 4 );
2776 rect
.SetHeight( m_colLabelHeight
- 4 );
2777 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
2781 void wxGrid::DrawTextRectangle( wxDC
& dc
,
2782 const wxString
& value
,
2787 long textWidth
, textHeight
;
2788 long lineWidth
, lineHeight
;
2789 wxArrayString lines
;
2791 dc
.SetClippingRegion( rect
);
2792 StringToLines( value
, lines
);
2793 if ( lines
.GetCount() )
2795 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
2796 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
2799 switch ( horizAlign
)
2802 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
2806 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
2815 switch ( vertAlign
)
2818 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
2822 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
2831 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
2833 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
2838 dc
.DestroyClippingRegion();
2842 // Split multi line text up into an array of strings. Any existing
2843 // contents of the string array are preserved.
2845 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
2847 // TODO: this won't work for WXMAC ? (lines end with '\r')
2848 // => use wxTextFile functions then (VZ)
2851 while ( startPos
< (int)value
.Length() )
2853 pos
= value
.Mid(startPos
).Find( '\n' );
2858 else if ( pos
== 0 )
2860 lines
.Add( wxEmptyString
);
2864 if ( value
[startPos
+pos
-1] == '\r' )
2866 lines
.Add( value
.Mid(startPos
, pos
-1) );
2870 lines
.Add( value
.Mid(startPos
, pos
) );
2875 if ( startPos
< (int)value
.Length() )
2877 lines
.Add( value
.Mid( startPos
) );
2882 void wxGrid::GetTextBoxSize( wxDC
& dc
,
2883 wxArrayString
& lines
,
2884 long *width
, long *height
)
2891 for ( i
= 0; i
< lines
.GetCount(); i
++ )
2893 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
2894 w
= wxMax( w
, lineW
);
2904 // ------ Edit control functions
2908 void wxGrid::EnableEditing( bool edit
)
2910 // TODO: improve this ?
2912 if ( edit
!= m_editable
)
2916 // TODO: extend this for other edit control types
2918 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
2920 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
2926 #if 0 // disabled for the moment - the cell control is always active
2927 void wxGrid::EnableCellEditControl( bool enable
)
2929 if ( m_cellEditCtrl
&&
2930 enable
!= m_cellEditCtrlEnabled
)
2932 m_cellEditCtrlEnabled
= enable
;
2934 if ( m_cellEditCtrlEnabled
)
2936 SetEditControlValue();
2937 ShowCellEditControl();
2941 HideCellEditControl();
2942 SaveEditControlValue();
2949 void wxGrid::ShowCellEditControl()
2953 if ( IsCellEditControlEnabled() )
2955 if ( !IsVisible( m_currentCellCoords
) )
2961 rect
= CellToRect( m_currentCellCoords
);
2963 // convert to scrolled coords
2965 int left
, top
, right
, bottom
;
2966 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
2967 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
2970 m_gridWin
->GetClientSize( &cw
, &ch
);
2972 // Make the edit control large enough to allow for internal margins
2973 // TODO: remove this if the text ctrl sizing is improved esp. for unix
2975 #if defined (__WXMOTIF__)
2976 rect
.SetLeft( wxMax(0, left
-4) );
2977 rect
.SetTop( wxMax(0, top
-4) );
2978 rect
.SetRight( rect
.GetRight() + 8 );
2979 rect
.SetBottom( rect
.GetBottom() + 8 );
2981 rect
.SetLeft( wxMax(0, left
-2) );
2982 rect
.SetTop( wxMax(0, top
-2) );
2983 rect
.SetRight( rect
.GetRight() + 4 );
2984 rect
.SetBottom( rect
.GetBottom() + 4 );
2987 m_cellEditCtrl
->SetSize( rect
);
2988 m_cellEditCtrl
->Show( TRUE
);
2990 switch ( m_editCtrlType
)
2992 case wxGRID_TEXTCTRL
:
2993 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
2996 case wxGRID_CHECKBOX
:
2997 // TODO: anything ???
3002 // TODO: anything ???
3006 case wxGRID_COMBOBOX
:
3007 // TODO: anything ???
3012 m_cellEditCtrl
->SetFocus();
3018 void wxGrid::HideCellEditControl()
3020 if ( IsCellEditControlEnabled() )
3022 m_cellEditCtrl
->Show( FALSE
);
3027 void wxGrid::SetEditControlValue( const wxString
& value
)
3033 s
= GetCellValue(m_currentCellCoords
);
3037 if ( IsCellEditControlEnabled() )
3039 switch ( m_editCtrlType
)
3041 case wxGRID_TEXTCTRL
:
3042 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3045 case wxGRID_CHECKBOX
:
3046 // TODO: implement this
3051 // TODO: implement this
3055 case wxGRID_COMBOBOX
:
3056 // TODO: implement this
3065 void wxGrid::SaveEditControlValue()
3069 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3071 if ( IsCellEditControlEnabled() )
3073 ctrl
= m_cellEditCtrl
;
3080 bool valueChanged
= FALSE
;
3082 switch ( m_editCtrlType
)
3084 case wxGRID_TEXTCTRL
:
3085 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3086 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3087 SetCellValue( m_currentCellCoords
,
3088 ((wxTextCtrl
*) ctrl
)->GetValue() );
3091 case wxGRID_CHECKBOX
:
3092 // TODO: implement this
3097 // TODO: implement this
3101 case wxGRID_COMBOBOX
:
3102 // TODO: implement this
3109 SendEvent( EVT_GRID_CELL_CHANGE
,
3110 m_currentCellCoords
.GetRow(),
3111 m_currentCellCoords
.GetCol() );
3118 // ------ Grid location functions
3119 // Note that all of these functions work with the logical coordinates of
3120 // grid cells and labels so you will need to convert from device
3121 // coordinates for mouse events etc.
3124 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3126 coords
.SetRow( YToRow(y
) );
3127 coords
.SetCol( XToCol(x
) );
3131 int wxGrid::YToRow( int y
)
3135 for ( i
= 0; i
< m_numRows
; i
++ )
3137 if ( y
< m_rowBottoms
[i
] ) return i
;
3144 int wxGrid::XToCol( int x
)
3148 for ( i
= 0; i
< m_numCols
; i
++ )
3150 if ( x
< m_colRights
[i
] ) return i
;
3157 // return the row number that that the y coord is near the edge of, or
3158 // -1 if not near an edge
3160 int wxGrid::YToEdgeOfRow( int y
)
3164 for ( i
= 0; i
< m_numRows
; i
++ )
3166 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3168 d
= abs( y
- m_rowBottoms
[i
] );
3170 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3179 // return the col number that that the x coord is near the edge of, or
3180 // -1 if not near an edge
3182 int wxGrid::XToEdgeOfCol( int x
)
3186 for ( i
= 0; i
< m_numCols
; i
++ )
3188 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3190 d
= abs( x
- m_colRights
[i
] );
3192 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3201 wxRect
wxGrid::CellToRect( int row
, int col
)
3203 wxRect
rect( -1, -1, -1, -1 );
3205 if ( row
>= 0 && row
< m_numRows
&&
3206 col
>= 0 && col
< m_numCols
)
3208 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3209 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3210 rect
.width
= m_colWidths
[col
];
3211 rect
.height
= m_rowHeights
[ row
];
3218 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3220 // get the cell rectangle in logical coords
3222 wxRect
r( CellToRect( row
, col
) );
3224 // convert to device coords
3226 int left
, top
, right
, bottom
;
3227 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3228 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3230 // check against the client area of the grid window
3233 m_gridWin
->GetClientSize( &cw
, &ch
);
3235 if ( wholeCellVisible
)
3237 // is the cell wholly visible ?
3239 return ( left
>= 0 && right
<= cw
&&
3240 top
>= 0 && bottom
<= ch
);
3244 // is the cell partly visible ?
3246 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3247 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3252 // make the specified cell location visible by doing a minimal amount
3255 void wxGrid::MakeCellVisible( int row
, int col
)
3258 int xpos
= -1, ypos
= -1;
3260 if ( row
>= 0 && row
< m_numRows
&&
3261 col
>= 0 && col
< m_numCols
)
3263 // get the cell rectangle in logical coords
3265 wxRect
r( CellToRect( row
, col
) );
3267 // convert to device coords
3269 int left
, top
, right
, bottom
;
3270 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3271 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3274 m_gridWin
->GetClientSize( &cw
, &ch
);
3280 else if ( bottom
> ch
)
3282 int h
= r
.GetHeight();
3284 for ( i
= row
-1; i
>= 0; i
-- )
3286 if ( h
+ m_rowHeights
[i
] > ch
) break;
3288 h
+= m_rowHeights
[i
];
3289 ypos
-= m_rowHeights
[i
];
3297 else if ( right
> cw
)
3299 int w
= r
.GetWidth();
3301 for ( i
= col
-1; i
>= 0; i
-- )
3303 if ( w
+ m_colWidths
[i
] > cw
) break;
3305 w
+= m_colWidths
[i
];
3306 xpos
-= m_colWidths
[i
];
3310 if ( xpos
!= -1 || ypos
!= -1 )
3312 if ( xpos
!= -1 ) xpos
= xpos
/10;
3313 if ( ypos
!= -1 ) ypos
= ypos
/10;
3314 Scroll( xpos
, ypos
);
3322 // ------ Grid cursor movement functions
3325 bool wxGrid::MoveCursorUp()
3327 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3328 m_currentCellCoords
.GetRow() > 0 )
3330 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3331 m_currentCellCoords
.GetCol() );
3333 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3334 m_currentCellCoords
.GetCol() );
3343 bool wxGrid::MoveCursorDown()
3345 // TODO: allow for scrolling
3347 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3348 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3350 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3351 m_currentCellCoords
.GetCol() );
3353 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3354 m_currentCellCoords
.GetCol() );
3363 bool wxGrid::MoveCursorLeft()
3365 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3366 m_currentCellCoords
.GetCol() > 0 )
3368 MakeCellVisible( m_currentCellCoords
.GetRow(),
3369 m_currentCellCoords
.GetCol() - 1 );
3371 SetCurrentCell( m_currentCellCoords
.GetRow(),
3372 m_currentCellCoords
.GetCol() - 1 );
3381 bool wxGrid::MoveCursorRight()
3383 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3384 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
3386 MakeCellVisible( m_currentCellCoords
.GetRow(),
3387 m_currentCellCoords
.GetCol() + 1 );
3389 SetCurrentCell( m_currentCellCoords
.GetRow(),
3390 m_currentCellCoords
.GetCol() + 1 );
3399 bool wxGrid::MovePageUp()
3401 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3403 int row
= m_currentCellCoords
.GetRow();
3407 m_gridWin
->GetClientSize( &cw
, &ch
);
3409 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3410 int newRow
= YToRow( y
- ch
+ 1 );
3415 else if ( newRow
== row
)
3420 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3421 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3429 bool wxGrid::MovePageDown()
3431 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3433 int row
= m_currentCellCoords
.GetRow();
3434 if ( row
< m_numRows
)
3437 m_gridWin
->GetClientSize( &cw
, &ch
);
3439 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3440 int newRow
= YToRow( y
+ ch
);
3443 newRow
= m_numRows
- 1;
3445 else if ( newRow
== row
)
3450 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3451 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3459 bool wxGrid::MoveCursorUpBlock()
3462 m_currentCellCoords
!= wxGridNoCellCoords
&&
3463 m_currentCellCoords
.GetRow() > 0 )
3465 int row
= m_currentCellCoords
.GetRow();
3466 int col
= m_currentCellCoords
.GetCol();
3468 if ( m_table
->IsEmptyCell(row
, col
) )
3470 // starting in an empty cell: find the next block of
3476 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3479 else if ( m_table
->IsEmptyCell(row
-1, col
) )
3481 // starting at the top of a block: find the next block
3487 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3492 // starting within a block: find the top of the block
3497 if ( m_table
->IsEmptyCell(row
, col
) )
3505 MakeCellVisible( row
, col
);
3506 SetCurrentCell( row
, col
);
3514 bool wxGrid::MoveCursorDownBlock()
3517 m_currentCellCoords
!= wxGridNoCellCoords
&&
3518 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3520 int row
= m_currentCellCoords
.GetRow();
3521 int col
= m_currentCellCoords
.GetCol();
3523 if ( m_table
->IsEmptyCell(row
, col
) )
3525 // starting in an empty cell: find the next block of
3528 while ( row
< m_numRows
-1 )
3531 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3534 else if ( m_table
->IsEmptyCell(row
+1, col
) )
3536 // starting at the bottom of a block: find the next block
3539 while ( row
< m_numRows
-1 )
3542 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3547 // starting within a block: find the bottom of the block
3549 while ( row
< m_numRows
-1 )
3552 if ( m_table
->IsEmptyCell(row
, col
) )
3560 MakeCellVisible( row
, col
);
3561 SetCurrentCell( row
, col
);
3569 bool wxGrid::MoveCursorLeftBlock()
3572 m_currentCellCoords
!= wxGridNoCellCoords
&&
3573 m_currentCellCoords
.GetCol() > 0 )
3575 int row
= m_currentCellCoords
.GetRow();
3576 int col
= m_currentCellCoords
.GetCol();
3578 if ( m_table
->IsEmptyCell(row
, col
) )
3580 // starting in an empty cell: find the next block of
3586 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3589 else if ( m_table
->IsEmptyCell(row
, col
-1) )
3591 // starting at the left of a block: find the next block
3597 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3602 // starting within a block: find the left of the block
3607 if ( m_table
->IsEmptyCell(row
, col
) )
3615 MakeCellVisible( row
, col
);
3616 SetCurrentCell( row
, col
);
3624 bool wxGrid::MoveCursorRightBlock()
3627 m_currentCellCoords
!= wxGridNoCellCoords
&&
3628 m_currentCellCoords
.GetCol() < m_numCols
-1 )
3630 int row
= m_currentCellCoords
.GetRow();
3631 int col
= m_currentCellCoords
.GetCol();
3633 if ( m_table
->IsEmptyCell(row
, col
) )
3635 // starting in an empty cell: find the next block of
3638 while ( col
< m_numCols
-1 )
3641 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3644 else if ( m_table
->IsEmptyCell(row
, col
+1) )
3646 // starting at the right of a block: find the next block
3649 while ( col
< m_numCols
-1 )
3652 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3657 // starting within a block: find the right of the block
3659 while ( col
< m_numCols
-1 )
3662 if ( m_table
->IsEmptyCell(row
, col
) )
3670 MakeCellVisible( row
, col
);
3671 SetCurrentCell( row
, col
);
3682 // ------ Label values and formatting
3685 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
3687 *horiz
= m_rowLabelHorizAlign
;
3688 *vert
= m_rowLabelVertAlign
;
3691 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
3693 *horiz
= m_colLabelHorizAlign
;
3694 *vert
= m_colLabelVertAlign
;
3697 wxString
wxGrid::GetRowLabelValue( int row
)
3701 return m_table
->GetRowLabelValue( row
);
3711 wxString
wxGrid::GetColLabelValue( int col
)
3715 return m_table
->GetColLabelValue( col
);
3725 void wxGrid::SetRowLabelSize( int width
)
3727 // TODO: how to do this with the box sizers ?
3730 void wxGrid::SetColLabelSize( int height
)
3732 // TODO: how to do this with the box sizers ?
3735 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
3737 if ( m_labelBackgroundColour
!= colour
)
3739 m_labelBackgroundColour
= colour
;
3740 m_rowLabelWin
->SetBackgroundColour( colour
);
3741 m_colLabelWin
->SetBackgroundColour( colour
);
3742 m_cornerLabelWin
->SetBackgroundColour( colour
);
3744 if ( !GetBatchCount() )
3746 m_rowLabelWin
->Refresh();
3747 m_colLabelWin
->Refresh();
3748 m_cornerLabelWin
->Refresh();
3753 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
3755 if ( m_labelTextColour
!= colour
)
3757 m_labelTextColour
= colour
;
3758 if ( !GetBatchCount() )
3760 m_rowLabelWin
->Refresh();
3761 m_colLabelWin
->Refresh();
3766 void wxGrid::SetLabelFont( const wxFont
& font
)
3769 if ( !GetBatchCount() )
3771 m_rowLabelWin
->Refresh();
3772 m_colLabelWin
->Refresh();
3776 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
3778 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3780 m_rowLabelHorizAlign
= horiz
;
3783 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3785 m_rowLabelVertAlign
= vert
;
3788 if ( !GetBatchCount() )
3790 m_rowLabelWin
->Refresh();
3791 m_colLabelWin
->Refresh();
3795 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
3797 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3799 m_colLabelHorizAlign
= horiz
;
3802 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3804 m_colLabelVertAlign
= vert
;
3807 if ( !GetBatchCount() )
3809 m_rowLabelWin
->Refresh();
3810 m_colLabelWin
->Refresh();
3814 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
3818 m_table
->SetRowLabelValue( row
, s
);
3819 if ( !GetBatchCount() )
3821 // TODO: Optimize this
3823 m_rowLabelWin
->Refresh();
3828 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
3832 m_table
->SetColLabelValue( col
, s
);
3833 if ( !GetBatchCount() )
3835 // TODO: optimize this
3837 m_colLabelWin
->Refresh();
3842 void wxGrid::SetGridLineColour( const wxColour
& colour
)
3844 if ( m_gridLineColour
!= colour
)
3846 m_gridLineColour
= colour
;
3848 wxClientDC
dc( m_gridWin
);
3850 DrawAllGridLines( dc
);
3854 void wxGrid::EnableGridLines( bool enable
)
3856 if ( enable
!= m_gridLinesEnabled
)
3858 m_gridLinesEnabled
= enable
;
3860 if ( !GetBatchCount() )
3864 wxClientDC
dc( m_gridWin
);
3866 DrawAllGridLines( dc
);
3870 m_gridWin
->Refresh();
3877 int wxGrid::GetDefaultRowSize()
3879 return m_defaultRowHeight
;
3882 int wxGrid::GetRowSize( int row
)
3884 if ( row
>= 0 && row
< m_numRows
)
3885 return m_rowHeights
[row
];
3887 return 0; // TODO: log an error here
3890 int wxGrid::GetDefaultColSize()
3892 return m_defaultColWidth
;
3895 int wxGrid::GetColSize( int col
)
3897 if ( col
>= 0 && col
< m_numCols
)
3898 return m_colWidths
[col
];
3900 return 0; // TODO: log an error here
3903 wxColour
wxGrid::GetDefaultCellBackgroundColour()
3905 // TODO: replace this temp test code
3907 return wxColour( 255, 255, 255 );
3910 wxColour
wxGrid::GetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
) )
3912 // TODO: replace this temp test code
3914 return wxColour( 255, 255, 255 );
3917 wxColour
wxGrid::GetDefaultCellTextColour()
3919 // TODO: replace this temp test code
3921 return wxColour( 0, 0, 0 );
3924 wxColour
wxGrid::GetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
) )
3926 // TODO: replace this temp test code
3928 return wxColour( 0, 0, 0 );
3932 wxFont
wxGrid::GetDefaultCellFont()
3934 return m_defaultCellFont
;
3937 wxFont
wxGrid::GetCellFont( int WXUNUSED(row
), int WXUNUSED(col
) )
3939 // TODO: replace this temp test code
3941 return m_defaultCellFont
;
3944 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
3946 // TODO: replace this temp test code
3952 void wxGrid::GetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int *horiz
, int *vert
)
3954 // TODO: replace this temp test code
3960 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
3962 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
3964 if ( resizeExistingRows
)
3968 for ( row
= 0; row
< m_numRows
; row
++ )
3970 m_rowHeights
[row
] = m_defaultRowHeight
;
3971 bottom
+= m_defaultRowHeight
;
3972 m_rowBottoms
[row
] = bottom
;
3978 void wxGrid::SetRowSize( int row
, int height
)
3982 if ( row
>= 0 && row
< m_numRows
)
3984 int h
= wxMax( 0, height
);
3985 int diff
= h
- m_rowHeights
[row
];
3987 m_rowHeights
[row
] = h
;
3988 for ( i
= row
; i
< m_numRows
; i
++ )
3990 m_rowBottoms
[i
] += diff
;
3994 // Note: we are ending the event *after* doing
3995 // default processing in this case
3997 SendEvent( EVT_GRID_ROW_SIZE
,
4002 // TODO: log an error here
4006 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4008 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4010 if ( resizeExistingCols
)
4014 for ( col
= 0; col
< m_numCols
; col
++ )
4016 m_colWidths
[col
] = m_defaultColWidth
;
4017 right
+= m_defaultColWidth
;
4018 m_colRights
[col
] = right
;
4024 void wxGrid::SetColSize( int col
, int width
)
4028 if ( col
>= 0 && col
< m_numCols
)
4030 int w
= wxMax( 0, width
);
4031 int diff
= w
- m_colWidths
[col
];
4032 m_colWidths
[col
] = w
;
4034 for ( i
= col
; i
< m_numCols
; i
++ )
4036 m_colRights
[i
] += diff
;
4040 // Note: we are ending the event *after* doing
4041 // default processing in this case
4043 SendEvent( EVT_GRID_COL_SIZE
,
4048 // TODO: log an error here
4052 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& )
4054 // TODO: everything !!!
4058 void wxGrid::SetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4060 // TODO: everything !!!
4064 void wxGrid::SetDefaultCellTextColour( const wxColour
& )
4066 // TODO: everything !!!
4070 void wxGrid::SetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4072 // TODO: everything !!!
4076 void wxGrid::SetDefaultCellFont( const wxFont
& )
4078 // TODO: everything !!!
4082 void wxGrid::SetCellFont( int WXUNUSED(row
), int WXUNUSED(col
), const wxFont
& )
4084 // TODO: everything !!!
4088 void wxGrid::SetDefaultCellAlignment( int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4090 // TODO: everything !!!
4094 void wxGrid::SetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4096 // TODO: everything !!!
4103 // ------ cell value accessor functions
4106 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4110 m_table
->SetValue( row
, col
, s
.c_str() );
4111 if ( !GetBatchCount() )
4113 wxClientDC
dc( m_gridWin
);
4115 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4118 #if 0 // TODO: edit in place
4120 if ( m_currentCellCoords
.GetRow() == row
&&
4121 m_currentCellCoords
.GetCol() == col
)
4123 SetEditControlValue( s
);
4132 // ------ Block, row and col selection
4135 void wxGrid::SelectRow( int row
, bool addToSelected
)
4139 if ( IsSelection() && addToSelected
)
4141 if ( m_selectedTopLeft
.GetRow() > row
)
4142 m_selectedTopLeft
.SetRow( row
);
4144 m_selectedTopLeft
.SetCol( 0 );
4146 if ( m_selectedBottomRight
.GetRow() < row
)
4147 m_selectedBottomRight
.SetRow( row
);
4149 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4151 // TODO: optimize this so that we only refresh the newly
4154 r
= SelectionToDeviceRect();
4155 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4159 r
= SelectionToDeviceRect();
4161 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4163 m_selectedTopLeft
.Set( row
, 0 );
4164 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4165 r
= SelectionToDeviceRect();
4166 m_gridWin
->Refresh( TRUE
, &r
);
4169 wxGridRangeSelectEvent
gridEvt( GetId(),
4170 EVT_GRID_RANGE_SELECT
,
4173 m_selectedBottomRight
);
4175 GetEventHandler()->ProcessEvent(gridEvt
);
4179 void wxGrid::SelectCol( int col
, bool addToSelected
)
4183 if ( IsSelection() && addToSelected
)
4185 if ( m_selectedTopLeft
.GetCol() > col
)
4186 m_selectedTopLeft
.SetCol( col
);
4188 m_selectedTopLeft
.SetRow( 0 );
4190 if ( m_selectedBottomRight
.GetCol() < col
)
4191 m_selectedBottomRight
.SetCol( col
);
4193 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4195 // TODO: optimize this so that we only refresh the newly
4198 r
= SelectionToDeviceRect();
4199 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4203 r
= SelectionToDeviceRect();
4205 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4207 m_selectedTopLeft
.Set( 0, col
);
4208 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4209 r
= SelectionToDeviceRect();
4210 m_gridWin
->Refresh( TRUE
, &r
);
4213 wxGridRangeSelectEvent
gridEvt( GetId(),
4214 EVT_GRID_RANGE_SELECT
,
4217 m_selectedBottomRight
);
4219 GetEventHandler()->ProcessEvent(gridEvt
);
4223 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4227 if ( topRow
> bottomRow
)
4234 if ( leftCol
> rightCol
)
4241 m_selectedTopLeft
.Set( topRow
, leftCol
);
4242 m_selectedBottomRight
.Set( bottomRow
, rightCol
);
4245 r
= SelectionToDeviceRect();
4246 m_gridWin
->Refresh( TRUE
, &r
);
4248 // only generate an event if the block is not being selected by
4249 // dragging the mouse (in which case the event will be generated in
4250 // the mouse event handler)
4251 if ( !m_isDragging
)
4253 wxGridRangeSelectEvent
gridEvt( GetId(),
4254 EVT_GRID_RANGE_SELECT
,
4257 m_selectedBottomRight
);
4259 GetEventHandler()->ProcessEvent(gridEvt
);
4263 void wxGrid::SelectAll()
4265 m_selectedTopLeft
.Set( 0, 0 );
4266 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
4268 m_gridWin
->Refresh();
4272 void wxGrid::ClearSelection()
4274 m_selectedTopLeft
= wxGridNoCellCoords
;
4275 m_selectedBottomRight
= wxGridNoCellCoords
;
4279 // This function returns the rectangle that encloses the selected cells
4280 // in device coords clipped to the client size of the grid window.
4282 wxRect
wxGrid::SelectionToDeviceRect()
4287 if ( IsSelection() )
4289 cellRect
= CellToRect( m_selectedTopLeft
);
4290 if ( cellRect
!= wxGridNoCellRect
)
4296 rect
= wxRect( 0, 0, 0, 0 );
4299 cellRect
= CellToRect( m_selectedBottomRight
);
4300 if ( cellRect
!= wxGridNoCellRect
)
4306 return wxGridNoCellRect
;
4309 // convert to scrolled coords
4311 int left
, top
, right
, bottom
;
4312 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4313 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4316 m_gridWin
->GetClientSize( &cw
, &ch
);
4318 rect
.SetLeft( wxMax(0, left
) );
4319 rect
.SetTop( wxMax(0, top
) );
4320 rect
.SetRight( wxMin(cw
, right
) );
4321 rect
.SetBottom( wxMin(ch
, bottom
) );
4325 return wxGridNoCellRect
;
4333 // ------ Grid event classes
4336 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
4338 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
4339 int row
, int col
, int x
, int y
,
4340 bool control
, bool shift
, bool alt
, bool meta
)
4341 : wxNotifyEvent( type
, id
)
4347 m_control
= control
;
4352 SetEventObject(obj
);
4356 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
4358 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
4359 int rowOrCol
, int x
, int y
,
4360 bool control
, bool shift
, bool alt
, bool meta
)
4361 : wxNotifyEvent( type
, id
)
4363 m_rowOrCol
= rowOrCol
;
4366 m_control
= control
;
4371 SetEventObject(obj
);
4375 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
4377 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
4378 const wxGridCellCoords
& topLeft
,
4379 const wxGridCellCoords
& bottomRight
,
4380 bool control
, bool shift
, bool alt
, bool meta
)
4381 : wxNotifyEvent( type
, id
)
4383 m_topLeft
= topLeft
;
4384 m_bottomRight
= bottomRight
;
4385 m_control
= control
;
4390 SetEventObject(obj
);
4394 #endif // ifndef wxUSE_NEW_GRID