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
;
982 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
983 m_labelTextColour
= wxColour( "BLACK" );
985 // TODO: something better than this ?
987 m_labelFont
= this->GetFont();
988 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
990 m_rowLabelHorizAlign
= wxLEFT
;
991 m_rowLabelVertAlign
= wxCENTRE
;
993 m_colLabelHorizAlign
= wxCENTRE
;
994 m_colLabelVertAlign
= wxTOP
;
996 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
997 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
999 #if defined (__WXMOTIF__) // see also text ctrl sizing in ShowCellEditControl()
1000 m_defaultRowHeight
+= 8;
1002 m_defaultRowHeight
+= 4;
1005 m_rowHeights
.Alloc( m_numRows
);
1006 m_rowBottoms
.Alloc( m_numRows
);
1008 for ( i
= 0; i
< m_numRows
; i
++ )
1010 m_rowHeights
.Add( m_defaultRowHeight
);
1011 rowBottom
+= m_defaultRowHeight
;
1012 m_rowBottoms
.Add( rowBottom
);
1015 m_colWidths
.Alloc( m_numCols
);
1016 m_colRights
.Alloc( m_numCols
);
1018 for ( i
= 0; i
< m_numCols
; i
++ )
1020 m_colWidths
.Add( m_defaultColWidth
);
1021 colRight
+= m_defaultColWidth
;
1022 m_colRights
.Add( colRight
);
1025 // TODO: improve this ?
1027 m_defaultCellFont
= this->GetFont();
1029 m_gridLineColour
= wxColour( 128, 128, 255 );
1030 m_gridLinesEnabled
= TRUE
;
1032 m_cursorMode
= WXGRID_CURSOR_DEFAULT
;
1034 m_dragRowOrCol
= -1;
1035 m_isDragging
= FALSE
;
1037 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1038 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1040 m_currentCellCoords
= wxGridNoCellCoords
;
1042 m_selectedTopLeft
= wxGridNoCellCoords
;
1043 m_selectedBottomRight
= wxGridNoCellCoords
;
1045 m_editable
= TRUE
; // default for whole grid
1047 m_inOnKeyDown
= FALSE
;
1050 // TODO: extend this to other types of controls
1052 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1060 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1064 m_cellEditCtrl
->Show( FALSE
);
1065 m_cellEditCtrlEnabled
= TRUE
;
1066 m_editCtrlType
= wxGRID_TEXTCTRL
;
1070 void wxGrid::CalcDimensions()
1073 GetClientSize( &cw
, &ch
);
1075 if ( m_numRows
> 0 && m_numCols
> 0 )
1077 int right
= m_colRights
[ m_numCols
-1 ] + 20;
1078 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 20;
1080 // TODO: restore the scroll position that we had before sizing
1083 GetViewStart( &x
, &y
);
1084 SetScrollbars( 10, 10,
1085 right
/10, bottom
/10,
1091 // this is called when the grid table sends a message to say that it
1092 // has been redimensioned
1094 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1098 switch ( msg
.GetId() )
1100 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1102 size_t pos
= msg
.GetCommandInt();
1103 int numRows
= msg
.GetCommandInt2();
1104 for ( i
= 0; i
< numRows
; i
++ )
1106 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1107 m_rowBottoms
.Insert( 0, pos
);
1109 m_numRows
+= numRows
;
1112 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1114 for ( i
= pos
; i
< m_numRows
; i
++ )
1116 bottom
+= m_rowHeights
[i
];
1117 m_rowBottoms
[i
] = bottom
;
1123 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1125 int numRows
= msg
.GetCommandInt();
1126 for ( i
= 0; i
< numRows
; i
++ )
1128 m_rowHeights
.Add( m_defaultRowHeight
);
1129 m_rowBottoms
.Add( 0 );
1132 int oldNumRows
= m_numRows
;
1133 m_numRows
+= numRows
;
1136 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1138 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1140 bottom
+= m_rowHeights
[i
];
1141 m_rowBottoms
[i
] = bottom
;
1147 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1149 size_t pos
= msg
.GetCommandInt();
1150 int numRows
= msg
.GetCommandInt2();
1151 for ( i
= 0; i
< numRows
; i
++ )
1153 m_rowHeights
.Remove( pos
);
1154 m_rowBottoms
.Remove( pos
);
1156 m_numRows
-= numRows
;
1161 m_colWidths
.Clear();
1162 m_colRights
.Clear();
1163 m_currentCellCoords
= wxGridNoCellCoords
;
1167 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1168 m_currentCellCoords
.Set( 0, 0 );
1171 for ( i
= 0; i
< m_numRows
; i
++ )
1173 h
+= m_rowHeights
[i
];
1174 m_rowBottoms
[i
] = h
;
1182 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1184 size_t pos
= msg
.GetCommandInt();
1185 int numCols
= msg
.GetCommandInt2();
1186 for ( i
= 0; i
< numCols
; i
++ )
1188 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1189 m_colRights
.Insert( 0, pos
);
1191 m_numCols
+= numCols
;
1194 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1196 for ( i
= pos
; i
< m_numCols
; i
++ )
1198 right
+= m_colWidths
[i
];
1199 m_colRights
[i
] = right
;
1205 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1207 int numCols
= msg
.GetCommandInt();
1208 for ( i
= 0; i
< numCols
; i
++ )
1210 m_colWidths
.Add( m_defaultColWidth
);
1211 m_colRights
.Add( 0 );
1214 int oldNumCols
= m_numCols
;
1215 m_numCols
+= numCols
;
1218 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1220 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1222 right
+= m_colWidths
[i
];
1223 m_colRights
[i
] = right
;
1229 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1231 size_t pos
= msg
.GetCommandInt();
1232 int numCols
= msg
.GetCommandInt2();
1233 for ( i
= 0; i
< numCols
; i
++ )
1235 m_colWidths
.Remove( pos
);
1236 m_colRights
.Remove( pos
);
1238 m_numCols
-= numCols
;
1242 #if 0 // leave the row alone here so that AppendCols will work subsequently
1244 m_rowHeights
.Clear();
1245 m_rowBottoms
.Clear();
1247 m_currentCellCoords
= wxGridNoCellCoords
;
1251 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1252 m_currentCellCoords
.Set( 0, 0 );
1255 for ( i
= 0; i
< m_numCols
; i
++ )
1257 w
+= m_colWidths
[i
];
1270 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
1272 wxRegionIterator
iter( reg
);
1275 m_rowLabelsExposed
.Empty();
1282 // TODO: remove this when we can...
1283 // There is a bug in wxMotif that gives garbage update
1284 // rectangles if you jump-scroll a long way by clicking the
1285 // scrollbar with middle button. This is a work-around
1287 #if defined(__WXMOTIF__)
1289 m_gridWin
->GetClientSize( &cw
, &ch
);
1290 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1291 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1294 // logical bounds of update region
1297 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1298 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1300 // find the row labels within these bounds
1304 for ( row
= 0; row
< m_numRows
; row
++ )
1306 if ( m_rowBottoms
[row
] < top
) continue;
1308 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1309 if ( rowTop
> bottom
) break;
1311 m_rowLabelsExposed
.Add( row
);
1319 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
1321 wxRegionIterator
iter( reg
);
1324 m_colLabelsExposed
.Empty();
1331 // TODO: remove this when we can...
1332 // There is a bug in wxMotif that gives garbage update
1333 // rectangles if you jump-scroll a long way by clicking the
1334 // scrollbar with middle button. This is a work-around
1336 #if defined(__WXMOTIF__)
1338 m_gridWin
->GetClientSize( &cw
, &ch
);
1339 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1340 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1343 // logical bounds of update region
1346 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1347 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1349 // find the cells within these bounds
1353 for ( col
= 0; col
< m_numCols
; col
++ )
1355 if ( m_colRights
[col
] < left
) continue;
1357 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1358 if ( colLeft
> right
) break;
1360 m_colLabelsExposed
.Add( col
);
1368 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
1370 wxRegionIterator
iter( reg
);
1373 m_cellsExposed
.Empty();
1374 m_rowsExposed
.Empty();
1375 m_colsExposed
.Empty();
1377 int left
, top
, right
, bottom
;
1382 // TODO: remove this when we can...
1383 // There is a bug in wxMotif that gives garbage update
1384 // rectangles if you jump-scroll a long way by clicking the
1385 // scrollbar with middle button. This is a work-around
1387 #if defined(__WXMOTIF__)
1389 m_gridWin
->GetClientSize( &cw
, &ch
);
1390 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1391 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1392 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1393 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1396 // logical bounds of update region
1398 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1399 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1401 // find the cells within these bounds
1404 int colLeft
, rowTop
;
1405 for ( row
= 0; row
< m_numRows
; row
++ )
1407 if ( m_rowBottoms
[row
] < top
) continue;
1409 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1410 if ( rowTop
> bottom
) break;
1412 m_rowsExposed
.Add( row
);
1414 for ( col
= 0; col
< m_numCols
; col
++ )
1416 if ( m_colRights
[col
] < left
) continue;
1418 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1419 if ( colLeft
> right
) break;
1421 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1422 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1431 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1434 wxPoint
pos( event
.GetPosition() );
1435 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1437 if ( event
.Dragging() )
1439 m_isDragging
= TRUE
;
1441 if ( event
.LeftIsDown() )
1443 switch( m_cursorMode
)
1445 case WXGRID_CURSOR_RESIZE_ROW
:
1447 int cw
, ch
, left
, dummy
;
1448 m_gridWin
->GetClientSize( &cw
, &ch
);
1449 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1451 wxClientDC
dc( m_gridWin
);
1453 dc
.SetLogicalFunction(wxXOR
);
1454 if ( m_dragLastPos
>= 0 )
1456 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1458 dc
.DrawLine( left
, y
, left
+cw
, y
);
1463 case WXGRID_CURSOR_SELECT_ROW
:
1465 if ( (row
= YToRow( y
)) >= 0 &&
1466 !IsInSelection( row
, 0 ) )
1468 SelectRow( row
, TRUE
);
1477 m_isDragging
= FALSE
;
1480 // ------------ Left button pressed
1482 if ( event
.LeftDown() )
1484 // don't send a label click event for a hit on the
1485 // edge of the row label - this is probably the user
1486 // wanting to resize the row
1488 if ( YToEdgeOfRow(y
) < 0 )
1491 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
1493 SelectRow( row
, event
.ShiftDown() );
1494 m_cursorMode
= WXGRID_CURSOR_SELECT_ROW
;
1499 // starting to drag-resize a row
1501 m_rowLabelWin
->CaptureMouse();
1506 // ------------ Left double click
1508 else if (event
.LeftDClick() )
1510 if ( YToEdgeOfRow(y
) < 0 )
1513 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
1518 // ------------ Left button released
1520 else if ( event
.LeftUp() )
1522 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
1524 m_rowLabelWin
->ReleaseMouse();
1526 if ( m_dragLastPos
>= 0 )
1528 // erase the last line and resize the row
1530 int cw
, ch
, left
, dummy
;
1531 m_gridWin
->GetClientSize( &cw
, &ch
);
1532 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1534 wxClientDC
dc( m_gridWin
);
1536 dc
.SetLogicalFunction( wxINVERT
);
1537 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1538 HideCellEditControl();
1540 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
1541 SetRowSize( m_dragRowOrCol
, wxMax( y
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
1542 if ( !GetBatchCount() )
1544 // TODO: optimize this
1545 m_rowLabelWin
->Refresh();
1546 m_gridWin
->Refresh();
1549 ShowCellEditControl();
1551 // Note: we are ending the event *after* doing
1552 // default processing in this case
1554 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1562 // ------------ Right button down
1564 else if ( event
.RightDown() )
1567 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1569 // no default action at the moment
1574 // ------------ Right double click
1576 else if ( event
.RightDClick() )
1579 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1581 // no default action at the moment
1586 // ------------ No buttons down and mouse moving
1588 else if ( event
.Moving() )
1590 m_dragRowOrCol
= YToEdgeOfRow( y
);
1591 if ( m_dragRowOrCol
>= 0 )
1593 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1595 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1596 m_rowLabelWin
->SetCursor( m_rowResizeCursor
);
1601 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1603 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1604 m_rowLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1611 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
1614 wxPoint
pos( event
.GetPosition() );
1615 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1617 if ( event
.Dragging() )
1619 m_isDragging
= TRUE
;
1621 if ( event
.LeftIsDown() )
1623 switch( m_cursorMode
)
1625 case WXGRID_CURSOR_RESIZE_COL
:
1627 int cw
, ch
, dummy
, top
;
1628 m_gridWin
->GetClientSize( &cw
, &ch
);
1629 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1631 wxClientDC
dc( m_gridWin
);
1633 dc
.SetLogicalFunction(wxXOR
);
1634 if ( m_dragLastPos
>= 0 )
1636 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1638 dc
.DrawLine( x
, top
, x
, top
+ch
);
1643 case WXGRID_CURSOR_SELECT_COL
:
1645 if ( (col
= XToCol( x
)) >= 0 &&
1646 !IsInSelection( 0, col
) )
1648 SelectCol( col
, TRUE
);
1657 m_isDragging
= FALSE
;
1660 // ------------ Left button pressed
1662 if ( event
.LeftDown() )
1664 // don't send a label click event for a hit on the
1665 // edge of the col label - this is probably the user
1666 // wanting to resize the col
1668 if ( XToEdgeOfCol(x
) < 0 )
1671 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
1673 SelectCol( col
, event
.ShiftDown() );
1674 m_cursorMode
= WXGRID_CURSOR_SELECT_COL
;
1679 // starting to drag-resize a col
1681 m_colLabelWin
->CaptureMouse();
1686 // ------------ Left double click
1688 if ( event
.LeftDClick() )
1690 if ( XToEdgeOfCol(x
) < 0 )
1693 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
1698 // ------------ Left button released
1700 else if ( event
.LeftUp() )
1702 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
1704 m_colLabelWin
->ReleaseMouse();
1706 if ( m_dragLastPos
>= 0 )
1708 // erase the last line and resize the col
1710 int cw
, ch
, dummy
, top
;
1711 m_gridWin
->GetClientSize( &cw
, &ch
);
1712 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1714 wxClientDC
dc( m_gridWin
);
1716 dc
.SetLogicalFunction( wxINVERT
);
1717 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1718 HideCellEditControl();
1720 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
1721 SetColSize( m_dragRowOrCol
, wxMax( x
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
1723 if ( !GetBatchCount() )
1725 // TODO: optimize this
1726 m_colLabelWin
->Refresh();
1727 m_gridWin
->Refresh();
1730 ShowCellEditControl();
1732 // Note: we are ending the event *after* doing
1733 // default processing in this case
1735 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
1743 // ------------ Right button down
1745 else if ( event
.RightDown() )
1748 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
1750 // no default action at the moment
1755 // ------------ Right double click
1757 else if ( event
.RightDClick() )
1760 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
1762 // no default action at the moment
1767 // ------------ No buttons down and mouse moving
1769 else if ( event
.Moving() )
1771 m_dragRowOrCol
= XToEdgeOfCol( x
);
1772 if ( m_dragRowOrCol
>= 0 )
1774 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1776 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
1777 m_colLabelWin
->SetCursor( m_colResizeCursor
);
1782 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1784 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1785 m_colLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1792 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
1794 if ( event
.LeftDown() )
1796 // indicate corner label by having both row and
1799 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
1805 else if ( event
.LeftDClick() )
1807 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
1810 else if ( event
.RightDown() )
1812 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
1814 // no default action at the moment
1818 else if ( event
.RightDClick() )
1820 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
1822 // no default action at the moment
1828 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
1831 wxPoint
pos( event
.GetPosition() );
1832 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1834 wxGridCellCoords coords
;
1835 XYToCell( x
, y
, coords
);
1837 if ( event
.Dragging() )
1839 m_isDragging
= TRUE
;
1841 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1843 if ( coords
!= wxGridNoCellCoords
)
1845 if ( !IsSelection() )
1847 SelectBlock( coords
, coords
);
1851 if ( !IsInSelection( coords
) )
1852 SelectBlock( m_currentCellCoords
, coords
);
1860 m_isDragging
= FALSE
;
1862 if ( event
.LeftDown() )
1864 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
1869 MakeCellVisible( coords
);
1870 SetCurrentCell( coords
);
1875 // ------------ Left double click
1877 else if ( event
.LeftDClick() )
1879 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
1886 // ------------ Left button released
1888 else if ( event
.LeftUp() )
1890 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1892 if ( IsSelection() )
1894 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
1902 // ------------ Right button down
1904 else if ( event
.RightDown() )
1906 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
1911 // no default action at the moment
1916 // ------------ Right double click
1918 else if ( event
.RightDClick() )
1920 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
1925 // no default action at the moment
1932 // ------ interaction with data model
1934 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
1936 switch ( msg
.GetId() )
1938 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
1939 return GetModelValues();
1941 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
1942 return SetModelValues();
1944 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1945 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1946 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1947 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1948 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1949 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1950 return Redimension( msg
);
1959 // The behaviour of this function depends on the grid table class
1960 // Clear() function. For the default wxGridStringTable class the
1961 // behavious is to replace all cell contents with wxEmptyString but
1962 // not to change the number of rows or cols.
1964 void wxGrid::ClearGrid()
1969 SetEditControlValue();
1970 if ( !GetBatchCount() ) m_gridWin
->Refresh();
1975 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
1977 // TODO: something with updateLabels flag
1981 wxLogError( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
1987 bool ok
= m_table
->InsertRows( pos
, numRows
);
1989 // the table will have sent the results of the insert row
1990 // operation to this view object as a grid table message
1994 if ( m_numCols
== 0 )
1996 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
1998 // TODO: perhaps instead of appending the default number of cols
1999 // we should remember what the last non-zero number of cols was ?
2003 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2005 // if we have just inserted cols into an empty grid the current
2006 // cell will be undefined...
2008 SetCurrentCell( 0, 0 );
2012 if ( !GetBatchCount() ) Refresh();
2015 SetEditControlValue();
2025 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2027 // TODO: something with updateLabels flag
2031 wxLogError( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2035 if ( m_table
&& m_table
->AppendRows( numRows
) )
2037 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2039 // if we have just inserted cols into an empty grid the current
2040 // cell will be undefined...
2042 SetCurrentCell( 0, 0 );
2045 // the table will have sent the results of the append row
2046 // operation to this view object as a grid table message
2049 if ( !GetBatchCount() ) Refresh();
2059 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2061 // TODO: something with updateLabels flag
2065 wxLogError( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2069 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2071 // the table will have sent the results of the delete row
2072 // operation to this view object as a grid table message
2074 if ( m_numRows
> 0 )
2075 SetEditControlValue();
2077 HideCellEditControl();
2080 if ( !GetBatchCount() ) Refresh();
2090 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2092 // TODO: something with updateLabels flag
2096 wxLogError( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2102 HideCellEditControl();
2103 bool ok
= m_table
->InsertCols( pos
, numCols
);
2105 // the table will have sent the results of the insert col
2106 // operation to this view object as a grid table message
2110 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2112 // if we have just inserted cols into an empty grid the current
2113 // cell will be undefined...
2115 SetCurrentCell( 0, 0 );
2119 if ( !GetBatchCount() ) Refresh();
2122 SetEditControlValue();
2132 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2134 // TODO: something with updateLabels flag
2138 wxLogError( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2142 if ( m_table
&& m_table
->AppendCols( numCols
) )
2144 // the table will have sent the results of the append col
2145 // operation to this view object as a grid table message
2147 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2149 // if we have just inserted cols into an empty grid the current
2150 // cell will be undefined...
2152 SetCurrentCell( 0, 0 );
2156 if ( !GetBatchCount() ) Refresh();
2166 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2168 // TODO: something with updateLabels flag
2172 wxLogError( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2176 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2178 // the table will have sent the results of the delete col
2179 // operation to this view object as a grid table message
2181 if ( m_numCols
> 0 )
2182 SetEditControlValue();
2184 HideCellEditControl();
2187 if ( !GetBatchCount() ) Refresh();
2199 // ----- event handlers
2202 // Generate a grid event based on a mouse event and
2203 // return the result of ProcessEvent()
2205 bool wxGrid::SendEvent( const wxEventType type
,
2207 wxMouseEvent
& mouseEv
)
2209 if ( type
== EVT_GRID_ROW_SIZE
||
2210 type
== EVT_GRID_COL_SIZE
)
2212 int rowOrCol
= (row
== -1 ? col
: row
);
2214 wxGridSizeEvent
gridEvt( GetId(),
2218 mouseEv
.GetX(), mouseEv
.GetY(),
2219 mouseEv
.ControlDown(),
2220 mouseEv
.ShiftDown(),
2222 mouseEv
.MetaDown() );
2224 return GetEventHandler()->ProcessEvent(gridEvt
);
2226 else if ( type
== EVT_GRID_RANGE_SELECT
)
2228 wxGridRangeSelectEvent
gridEvt( GetId(),
2232 m_selectedBottomRight
,
2233 mouseEv
.ControlDown(),
2234 mouseEv
.ShiftDown(),
2236 mouseEv
.MetaDown() );
2238 return GetEventHandler()->ProcessEvent(gridEvt
);
2242 wxGridEvent
gridEvt( GetId(),
2246 mouseEv
.GetX(), mouseEv
.GetY(),
2247 mouseEv
.ControlDown(),
2248 mouseEv
.ShiftDown(),
2250 mouseEv
.MetaDown() );
2252 return GetEventHandler()->ProcessEvent(gridEvt
);
2257 // Generate a grid event of specified type and return the result
2258 // of ProcessEvent().
2260 bool wxGrid::SendEvent( const wxEventType type
,
2263 if ( type
== EVT_GRID_ROW_SIZE
||
2264 type
== EVT_GRID_COL_SIZE
)
2266 int rowOrCol
= (row
== -1 ? col
: row
);
2268 wxGridSizeEvent
gridEvt( GetId(),
2273 return GetEventHandler()->ProcessEvent(gridEvt
);
2277 wxGridEvent
gridEvt( GetId(),
2282 return GetEventHandler()->ProcessEvent(gridEvt
);
2287 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2289 wxPaintDC
dc( this );
2291 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2292 m_numRows
&& m_numCols
)
2294 m_currentCellCoords
.Set(0, 0);
2295 SetEditControlValue();
2296 ShowCellEditControl();
2301 // This is just here to make sure that CalcDimensions gets called when
2302 // the grid view is resized... then the size event is skipped to allow
2303 // the box sizers to handle everything
2305 void wxGrid::OnSize( wxSizeEvent
& event
)
2312 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2314 if ( m_inOnKeyDown
)
2316 // shouldn't be here - we are going round in circles...
2318 wxLogFatalError( wxT("wxGrid::OnKeyDown called while alread active") );
2321 m_inOnKeyDown
= TRUE
;
2323 // propagate the event up and see if it gets processed
2325 wxWindow
*parent
= GetParent();
2326 wxKeyEvent
keyEvt( event
);
2327 keyEvt
.SetEventObject( parent
);
2329 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2331 // try local handlers
2333 switch ( event
.KeyCode() )
2336 if ( event
.ControlDown() )
2338 MoveCursorUpBlock();
2347 if ( event
.ControlDown() )
2349 MoveCursorDownBlock();
2358 if ( event
.ControlDown() )
2360 MoveCursorLeftBlock();
2369 if ( event
.ControlDown() )
2371 MoveCursorRightBlock();
2384 if ( event
.ControlDown() )
2386 MakeCellVisible( 0, 0 );
2387 SetCurrentCell( 0, 0 );
2396 if ( event
.ControlDown() )
2398 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2399 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2416 // now try the cell edit control
2418 if ( IsCellEditControlEnabled() )
2420 event
.SetEventObject( m_cellEditCtrl
);
2421 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2427 m_inOnKeyDown
= FALSE
;
2431 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
2433 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
2435 // the event has been intercepted - do nothing
2439 wxClientDC
dc( m_gridWin
);
2442 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
2444 HideCellEditControl();
2445 SaveEditControlValue();
2448 m_currentCellCoords
= coords
;
2450 SetEditControlValue();
2451 ShowCellEditControl();
2453 if ( IsSelection() )
2455 wxRect
r( SelectionToDeviceRect() );
2457 if ( !GetBatchCount() ) m_gridWin
->Refresh( TRUE
, &r
);
2463 // ------ functions to get/send data (see also public functions)
2466 bool wxGrid::GetModelValues()
2470 // all we need to do is repaint the grid
2472 m_gridWin
->Refresh();
2480 bool wxGrid::SetModelValues()
2486 for ( row
= 0; row
< m_numRows
; row
++ )
2488 for ( col
= 0; col
< m_numCols
; col
++ )
2490 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
2502 // Note - this function only draws cells that are in the list of
2503 // exposed cells (usually set from the update region by
2504 // CalcExposedCells)
2506 void wxGrid::DrawGridCellArea( wxDC
& dc
)
2508 if ( !m_numRows
|| !m_numCols
) return;
2511 size_t numCells
= m_cellsExposed
.GetCount();
2513 for ( i
= 0; i
< numCells
; i
++ )
2515 DrawCell( dc
, m_cellsExposed
[i
] );
2520 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
2522 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2523 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2525 if ( m_gridLinesEnabled
)
2526 DrawCellBorder( dc
, coords
);
2528 DrawCellBackground( dc
, coords
);
2530 // TODO: separate functions here for different kinds of cells ?
2533 DrawCellValue( dc
, coords
);
2537 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
2539 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2540 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2542 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2543 int row
= coords
.GetRow();
2544 int col
= coords
.GetCol();
2546 // right hand border
2548 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
2549 m_colRights
[col
], m_rowBottoms
[row
] );
2553 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
2554 m_colRights
[col
], m_rowBottoms
[row
] );
2558 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
2560 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2561 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2563 int row
= coords
.GetRow();
2564 int col
= coords
.GetCol();
2566 dc
.SetBackgroundMode( wxSOLID
);
2568 if ( IsInSelection( coords
) )
2570 // TODO: improve this
2572 dc
.SetBrush( *wxBLACK_BRUSH
);
2576 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
2579 dc
.SetPen( *wxTRANSPARENT_PEN
);
2581 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
2582 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
2584 m_rowHeights
[row
]-1 );
2588 void wxGrid::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
2590 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2591 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2593 int row
= coords
.GetRow();
2594 int col
= coords
.GetCol();
2596 dc
.SetBackgroundMode( wxTRANSPARENT
);
2598 if ( IsInSelection( row
, col
) )
2600 // TODO: improve this
2602 dc
.SetTextBackground( wxColour(0, 0, 0) );
2603 dc
.SetTextForeground( wxColour(255, 255, 255) );
2607 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
2608 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
2610 dc
.SetFont( GetCellFont(row
, col
) );
2613 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
2616 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2617 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2618 rect
.SetWidth( m_colWidths
[col
] - 4 );
2619 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2621 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
2626 // TODO: remove this ???
2627 // This is used to redraw all grid lines e.g. when the grid line colour
2630 void wxGrid::DrawAllGridLines( wxDC
& dc
)
2632 if ( !m_gridLinesEnabled
||
2634 !m_numCols
) return;
2637 m_gridWin
->GetClientSize(&cw
, &ch
);
2639 // virtual coords of visible area
2641 int top
, bottom
, left
, right
;
2642 CalcUnscrolledPosition( 0, 0, &left
, &top
);
2643 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
2645 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2647 // horizontal grid lines
2650 for ( i
= 0; i
<= m_numRows
; i
++ )
2652 if ( m_rowBottoms
[i
] > bottom
)
2656 else if ( m_rowBottoms
[i
] >= top
)
2658 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
2663 // vertical grid lines
2665 for ( i
= 0; i
<= m_numCols
; i
++ )
2667 if ( m_colRights
[i
] > right
)
2671 else if ( m_colRights
[i
] >= left
)
2673 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
2679 void wxGrid::DrawRowLabels( wxDC
& dc
)
2681 if ( !m_numRows
|| !m_numCols
) return;
2684 size_t numLabels
= m_rowLabelsExposed
.GetCount();
2686 for ( i
= 0; i
< numLabels
; i
++ )
2688 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
2693 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
2695 if ( m_rowHeights
[row
] <= 0 ) return;
2697 // draw the label's horizontal border (the vertical border is
2698 // provided by the cell area window margin)
2700 dc
.SetPen( *wxBLACK_PEN
);
2702 dc
.DrawLine( 0, m_rowBottoms
[row
]+1,
2703 m_rowLabelWidth
, m_rowBottoms
[row
]+1 );
2705 dc
.SetPen( *wxWHITE_PEN
);
2707 dc
.DrawLine( 0, m_rowBottoms
[row
]+2,
2708 m_rowLabelWidth
, m_rowBottoms
[row
]+2 );
2710 dc
.SetBackgroundMode( wxTRANSPARENT
);
2711 dc
.SetTextForeground( GetLabelTextColour() );
2712 dc
.SetFont( GetLabelFont() );
2715 GetRowLabelAlignment( &hAlign
, &vAlign
);
2719 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2720 rect
.SetWidth( m_rowLabelWidth
- 4 );
2721 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2722 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
2726 void wxGrid::DrawColLabels( wxDC
& dc
)
2728 if ( !m_numRows
|| !m_numCols
) return;
2731 size_t numLabels
= m_colLabelsExposed
.GetCount();
2733 for ( i
= 0; i
< numLabels
; i
++ )
2735 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
2740 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
2742 if ( m_colWidths
[col
] <= 0 ) return;
2744 // draw the label's vertical border (the horizontal border is
2745 // provided by the cell area window margin)
2747 dc
.SetPen( *wxBLACK_PEN
);
2749 dc
.DrawLine( m_colRights
[col
]+1, 0,
2750 m_colRights
[col
]+1, m_colLabelHeight
);
2752 dc
.SetPen( *wxWHITE_PEN
);
2754 dc
.DrawLine( m_colRights
[col
]+2, 0,
2755 m_colRights
[col
]+2, m_colLabelHeight
);
2757 dc
.SetBackgroundMode( wxTRANSPARENT
);
2758 dc
.SetTextForeground( GetLabelTextColour() );
2759 dc
.SetFont( GetLabelFont() );
2762 GetColLabelAlignment( &hAlign
, &vAlign
);
2765 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2767 rect
.SetWidth( m_colWidths
[col
] - 4 );
2768 rect
.SetHeight( m_colLabelHeight
- 4 );
2769 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
2773 void wxGrid::DrawTextRectangle( wxDC
& dc
,
2774 const wxString
& value
,
2779 long textWidth
, textHeight
;
2780 long lineWidth
, lineHeight
;
2781 wxArrayString lines
;
2783 dc
.SetClippingRegion( rect
);
2784 StringToLines( value
, lines
);
2785 if ( lines
.GetCount() )
2787 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
2788 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
2791 switch ( horizAlign
)
2794 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
2798 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
2807 switch ( vertAlign
)
2810 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
2814 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
2823 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
2825 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
2830 dc
.DestroyClippingRegion();
2834 // Split multi line text up into an array of strings. Any existing
2835 // contents of the string array are preserved.
2837 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
2839 // TODO: this won't work for WXMAC ? (lines end with '\r')
2840 // => use wxTextFile functions then (VZ)
2843 while ( startPos
< (int)value
.Length() )
2845 pos
= value
.Mid(startPos
).Find( '\n' );
2850 else if ( pos
== 0 )
2852 lines
.Add( wxEmptyString
);
2856 if ( value
[startPos
+pos
-1] == '\r' )
2858 lines
.Add( value
.Mid(startPos
, pos
-1) );
2862 lines
.Add( value
.Mid(startPos
, pos
) );
2867 if ( startPos
< (int)value
.Length() )
2869 lines
.Add( value
.Mid( startPos
) );
2874 void wxGrid::GetTextBoxSize( wxDC
& dc
,
2875 wxArrayString
& lines
,
2876 long *width
, long *height
)
2883 for ( i
= 0; i
< lines
.GetCount(); i
++ )
2885 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
2886 w
= wxMax( w
, lineW
);
2896 // ------ Edit control functions
2900 void wxGrid::EnableEditing( bool edit
)
2902 // TODO: improve this ?
2904 if ( edit
!= m_editable
)
2908 // TODO: extend this for other edit control types
2910 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
2912 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
2918 #if 0 // disabled for the moment - the cell control is always active
2919 void wxGrid::EnableCellEditControl( bool enable
)
2921 if ( m_cellEditCtrl
&&
2922 enable
!= m_cellEditCtrlEnabled
)
2924 m_cellEditCtrlEnabled
= enable
;
2926 if ( m_cellEditCtrlEnabled
)
2928 SetEditControlValue();
2929 ShowCellEditControl();
2933 HideCellEditControl();
2934 SaveEditControlValue();
2941 void wxGrid::ShowCellEditControl()
2945 if ( IsCellEditControlEnabled() )
2947 if ( !IsVisible( m_currentCellCoords
) )
2953 rect
= CellToRect( m_currentCellCoords
);
2955 // convert to scrolled coords
2957 int left
, top
, right
, bottom
;
2958 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
2959 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
2962 m_gridWin
->GetClientSize( &cw
, &ch
);
2964 // Make the edit control large enough to allow for internal margins
2965 // TODO: remove this if the text ctrl sizing is improved esp. for unix
2967 #if defined (__WXMOTIF__)
2968 rect
.SetLeft( wxMax(0, left
-4) );
2969 rect
.SetTop( wxMax(0, top
-4) );
2970 rect
.SetRight( rect
.GetRight() + 8 );
2971 rect
.SetBottom( rect
.GetBottom() + 8 );
2973 rect
.SetLeft( wxMax(0, left
-2) );
2974 rect
.SetTop( wxMax(0, top
-2) );
2975 rect
.SetRight( rect
.GetRight() + 4 );
2976 rect
.SetBottom( rect
.GetBottom() + 4 );
2979 m_cellEditCtrl
->SetSize( rect
);
2980 m_cellEditCtrl
->Show( TRUE
);
2982 switch ( m_editCtrlType
)
2984 case wxGRID_TEXTCTRL
:
2985 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
2988 case wxGRID_CHECKBOX
:
2989 // TODO: anything ???
2994 // TODO: anything ???
2998 case wxGRID_COMBOBOX
:
2999 // TODO: anything ???
3004 m_cellEditCtrl
->SetFocus();
3010 void wxGrid::HideCellEditControl()
3012 if ( IsCellEditControlEnabled() )
3014 m_cellEditCtrl
->Show( FALSE
);
3019 void wxGrid::SetEditControlValue( const wxString
& value
)
3025 s
= GetCellValue(m_currentCellCoords
);
3029 if ( IsCellEditControlEnabled() )
3031 switch ( m_editCtrlType
)
3033 case wxGRID_TEXTCTRL
:
3034 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3037 case wxGRID_CHECKBOX
:
3038 // TODO: implement this
3043 // TODO: implement this
3047 case wxGRID_COMBOBOX
:
3048 // TODO: implement this
3057 void wxGrid::SaveEditControlValue()
3061 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3063 if ( IsCellEditControlEnabled() )
3065 ctrl
= m_cellEditCtrl
;
3072 bool valueChanged
= FALSE
;
3074 switch ( m_editCtrlType
)
3076 case wxGRID_TEXTCTRL
:
3077 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3078 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3079 SetCellValue( m_currentCellCoords
,
3080 ((wxTextCtrl
*) ctrl
)->GetValue() );
3083 case wxGRID_CHECKBOX
:
3084 // TODO: implement this
3089 // TODO: implement this
3093 case wxGRID_COMBOBOX
:
3094 // TODO: implement this
3101 SendEvent( EVT_GRID_CELL_CHANGE
,
3102 m_currentCellCoords
.GetRow(),
3103 m_currentCellCoords
.GetCol() );
3110 // ------ Grid location functions
3111 // Note that all of these functions work with the logical coordinates of
3112 // grid cells and labels so you will need to convert from device
3113 // coordinates for mouse events etc.
3116 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3118 coords
.SetRow( YToRow(y
) );
3119 coords
.SetCol( XToCol(x
) );
3123 int wxGrid::YToRow( int y
)
3127 for ( i
= 0; i
< m_numRows
; i
++ )
3129 if ( y
< m_rowBottoms
[i
] ) return i
;
3136 int wxGrid::XToCol( int x
)
3140 for ( i
= 0; i
< m_numCols
; i
++ )
3142 if ( x
< m_colRights
[i
] ) return i
;
3149 // return the row number that that the y coord is near the edge of, or
3150 // -1 if not near an edge
3152 int wxGrid::YToEdgeOfRow( int y
)
3156 for ( i
= 0; i
< m_numRows
; i
++ )
3158 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3160 d
= abs( y
- m_rowBottoms
[i
] );
3162 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3171 // return the col number that that the x coord is near the edge of, or
3172 // -1 if not near an edge
3174 int wxGrid::XToEdgeOfCol( int x
)
3178 for ( i
= 0; i
< m_numCols
; i
++ )
3180 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3182 d
= abs( x
- m_colRights
[i
] );
3184 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3193 wxRect
wxGrid::CellToRect( int row
, int col
)
3195 wxRect
rect( -1, -1, -1, -1 );
3197 if ( row
>= 0 && row
< m_numRows
&&
3198 col
>= 0 && col
< m_numCols
)
3200 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3201 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3202 rect
.width
= m_colWidths
[col
];
3203 rect
.height
= m_rowHeights
[ row
];
3210 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3212 // get the cell rectangle in logical coords
3214 wxRect
r( CellToRect( row
, col
) );
3216 // convert to device coords
3218 int left
, top
, right
, bottom
;
3219 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3220 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3222 // check against the client area of the grid window
3225 m_gridWin
->GetClientSize( &cw
, &ch
);
3227 if ( wholeCellVisible
)
3229 // is the cell wholly visible ?
3231 return ( left
>= 0 && right
<= cw
&&
3232 top
>= 0 && bottom
<= ch
);
3236 // is the cell partly visible ?
3238 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3239 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3244 // make the specified cell location visible by doing a minimal amount
3247 void wxGrid::MakeCellVisible( int row
, int col
)
3250 int xpos
= -1, ypos
= -1;
3252 if ( row
>= 0 && row
< m_numRows
&&
3253 col
>= 0 && col
< m_numCols
)
3255 // get the cell rectangle in logical coords
3257 wxRect
r( CellToRect( row
, col
) );
3259 // convert to device coords
3261 int left
, top
, right
, bottom
;
3262 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3263 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3266 m_gridWin
->GetClientSize( &cw
, &ch
);
3272 else if ( bottom
> ch
)
3274 int h
= r
.GetHeight();
3276 for ( i
= row
-1; i
>= 0; i
-- )
3278 if ( h
+ m_rowHeights
[i
] > ch
) break;
3280 h
+= m_rowHeights
[i
];
3281 ypos
-= m_rowHeights
[i
];
3289 else if ( right
> cw
)
3291 int w
= r
.GetWidth();
3293 for ( i
= col
-1; i
>= 0; i
-- )
3295 if ( w
+ m_colWidths
[i
] > cw
) break;
3297 w
+= m_colWidths
[i
];
3298 xpos
-= m_colWidths
[i
];
3302 if ( xpos
!= -1 || ypos
!= -1 )
3304 if ( xpos
!= -1 ) xpos
= xpos
/10;
3305 if ( ypos
!= -1 ) ypos
= ypos
/10;
3306 Scroll( xpos
, ypos
);
3314 // ------ Grid cursor movement functions
3317 bool wxGrid::MoveCursorUp()
3319 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3320 m_currentCellCoords
.GetRow() > 0 )
3322 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3323 m_currentCellCoords
.GetCol() );
3325 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3326 m_currentCellCoords
.GetCol() );
3335 bool wxGrid::MoveCursorDown()
3337 // TODO: allow for scrolling
3339 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3340 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3342 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3343 m_currentCellCoords
.GetCol() );
3345 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3346 m_currentCellCoords
.GetCol() );
3355 bool wxGrid::MoveCursorLeft()
3357 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3358 m_currentCellCoords
.GetCol() > 0 )
3360 MakeCellVisible( m_currentCellCoords
.GetRow(),
3361 m_currentCellCoords
.GetCol() - 1 );
3363 SetCurrentCell( m_currentCellCoords
.GetRow(),
3364 m_currentCellCoords
.GetCol() - 1 );
3373 bool wxGrid::MoveCursorRight()
3375 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3376 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
3378 MakeCellVisible( m_currentCellCoords
.GetRow(),
3379 m_currentCellCoords
.GetCol() + 1 );
3381 SetCurrentCell( m_currentCellCoords
.GetRow(),
3382 m_currentCellCoords
.GetCol() + 1 );
3391 bool wxGrid::MovePageUp()
3393 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3395 int row
= m_currentCellCoords
.GetRow();
3399 m_gridWin
->GetClientSize( &cw
, &ch
);
3401 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3402 int newRow
= YToRow( y
- ch
+ 1 );
3407 else if ( newRow
== row
)
3412 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3413 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3421 bool wxGrid::MovePageDown()
3423 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3425 int row
= m_currentCellCoords
.GetRow();
3426 if ( row
< m_numRows
)
3429 m_gridWin
->GetClientSize( &cw
, &ch
);
3431 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3432 int newRow
= YToRow( y
+ ch
);
3435 newRow
= m_numRows
- 1;
3437 else if ( newRow
== row
)
3442 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3443 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3451 bool wxGrid::MoveCursorUpBlock()
3454 m_currentCellCoords
!= wxGridNoCellCoords
&&
3455 m_currentCellCoords
.GetRow() > 0 )
3457 int row
= m_currentCellCoords
.GetRow();
3458 int col
= m_currentCellCoords
.GetCol();
3460 if ( m_table
->IsEmptyCell(row
, col
) )
3462 // starting in an empty cell: find the next block of
3468 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3471 else if ( m_table
->IsEmptyCell(row
-1, col
) )
3473 // starting at the top of a block: find the next block
3479 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3484 // starting within a block: find the top of the block
3489 if ( m_table
->IsEmptyCell(row
, col
) )
3497 MakeCellVisible( row
, col
);
3498 SetCurrentCell( row
, col
);
3506 bool wxGrid::MoveCursorDownBlock()
3509 m_currentCellCoords
!= wxGridNoCellCoords
&&
3510 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3512 int row
= m_currentCellCoords
.GetRow();
3513 int col
= m_currentCellCoords
.GetCol();
3515 if ( m_table
->IsEmptyCell(row
, col
) )
3517 // starting in an empty cell: find the next block of
3520 while ( row
< m_numRows
-1 )
3523 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3526 else if ( m_table
->IsEmptyCell(row
+1, col
) )
3528 // starting at the bottom of a block: find the next block
3531 while ( row
< m_numRows
-1 )
3534 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3539 // starting within a block: find the bottom of the block
3541 while ( row
< m_numRows
-1 )
3544 if ( m_table
->IsEmptyCell(row
, col
) )
3552 MakeCellVisible( row
, col
);
3553 SetCurrentCell( row
, col
);
3561 bool wxGrid::MoveCursorLeftBlock()
3564 m_currentCellCoords
!= wxGridNoCellCoords
&&
3565 m_currentCellCoords
.GetCol() > 0 )
3567 int row
= m_currentCellCoords
.GetRow();
3568 int col
= m_currentCellCoords
.GetCol();
3570 if ( m_table
->IsEmptyCell(row
, col
) )
3572 // starting in an empty cell: find the next block of
3578 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3581 else if ( m_table
->IsEmptyCell(row
, col
-1) )
3583 // starting at the left of a block: find the next block
3589 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3594 // starting within a block: find the left of the block
3599 if ( m_table
->IsEmptyCell(row
, col
) )
3607 MakeCellVisible( row
, col
);
3608 SetCurrentCell( row
, col
);
3616 bool wxGrid::MoveCursorRightBlock()
3619 m_currentCellCoords
!= wxGridNoCellCoords
&&
3620 m_currentCellCoords
.GetCol() < m_numCols
-1 )
3622 int row
= m_currentCellCoords
.GetRow();
3623 int col
= m_currentCellCoords
.GetCol();
3625 if ( m_table
->IsEmptyCell(row
, col
) )
3627 // starting in an empty cell: find the next block of
3630 while ( col
< m_numCols
-1 )
3633 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3636 else if ( m_table
->IsEmptyCell(row
, col
+1) )
3638 // starting at the right of a block: find the next block
3641 while ( col
< m_numCols
-1 )
3644 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3649 // starting within a block: find the right of the block
3651 while ( col
< m_numCols
-1 )
3654 if ( m_table
->IsEmptyCell(row
, col
) )
3662 MakeCellVisible( row
, col
);
3663 SetCurrentCell( row
, col
);
3674 // ------ Label values and formatting
3677 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
3679 *horiz
= m_rowLabelHorizAlign
;
3680 *vert
= m_rowLabelVertAlign
;
3683 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
3685 *horiz
= m_colLabelHorizAlign
;
3686 *vert
= m_colLabelVertAlign
;
3689 wxString
wxGrid::GetRowLabelValue( int row
)
3693 return m_table
->GetRowLabelValue( row
);
3703 wxString
wxGrid::GetColLabelValue( int col
)
3707 return m_table
->GetColLabelValue( col
);
3717 void wxGrid::SetRowLabelSize( int width
)
3719 // TODO: how to do this with the box sizers ?
3722 void wxGrid::SetColLabelSize( int height
)
3724 // TODO: how to do this with the box sizers ?
3727 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
3729 if ( m_labelBackgroundColour
!= colour
)
3731 m_labelBackgroundColour
= colour
;
3732 m_rowLabelWin
->SetBackgroundColour( colour
);
3733 m_colLabelWin
->SetBackgroundColour( colour
);
3734 m_cornerLabelWin
->SetBackgroundColour( colour
);
3736 if ( !GetBatchCount() )
3738 m_rowLabelWin
->Refresh();
3739 m_colLabelWin
->Refresh();
3740 m_cornerLabelWin
->Refresh();
3745 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
3747 if ( m_labelTextColour
!= colour
)
3749 m_labelTextColour
= colour
;
3750 if ( !GetBatchCount() )
3752 m_rowLabelWin
->Refresh();
3753 m_colLabelWin
->Refresh();
3758 void wxGrid::SetLabelFont( const wxFont
& font
)
3761 if ( !GetBatchCount() )
3763 m_rowLabelWin
->Refresh();
3764 m_colLabelWin
->Refresh();
3768 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
3770 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3772 m_rowLabelHorizAlign
= horiz
;
3775 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3777 m_rowLabelVertAlign
= vert
;
3780 if ( !GetBatchCount() )
3782 m_rowLabelWin
->Refresh();
3783 m_colLabelWin
->Refresh();
3787 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
3789 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3791 m_colLabelHorizAlign
= horiz
;
3794 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3796 m_colLabelVertAlign
= vert
;
3799 if ( !GetBatchCount() )
3801 m_rowLabelWin
->Refresh();
3802 m_colLabelWin
->Refresh();
3806 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
3810 m_table
->SetRowLabelValue( row
, s
);
3811 if ( !GetBatchCount() )
3813 // TODO: Optimize this
3815 m_rowLabelWin
->Refresh();
3820 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
3824 m_table
->SetColLabelValue( col
, s
);
3825 if ( !GetBatchCount() )
3827 // TODO: optimize this
3829 m_colLabelWin
->Refresh();
3834 void wxGrid::SetGridLineColour( const wxColour
& colour
)
3836 if ( m_gridLineColour
!= colour
)
3838 m_gridLineColour
= colour
;
3840 wxClientDC
dc( m_gridWin
);
3842 DrawAllGridLines( dc
);
3846 void wxGrid::EnableGridLines( bool enable
)
3848 if ( enable
!= m_gridLinesEnabled
)
3850 m_gridLinesEnabled
= enable
;
3852 if ( !GetBatchCount() )
3856 wxClientDC
dc( m_gridWin
);
3858 DrawAllGridLines( dc
);
3862 m_gridWin
->Refresh();
3869 int wxGrid::GetDefaultRowSize()
3871 return m_defaultRowHeight
;
3874 int wxGrid::GetRowSize( int row
)
3876 if ( row
>= 0 && row
< m_numRows
)
3877 return m_rowHeights
[row
];
3879 return 0; // TODO: log an error here
3882 int wxGrid::GetDefaultColSize()
3884 return m_defaultColWidth
;
3887 int wxGrid::GetColSize( int col
)
3889 if ( col
>= 0 && col
< m_numCols
)
3890 return m_colWidths
[col
];
3892 return 0; // TODO: log an error here
3895 wxColour
wxGrid::GetDefaultCellBackgroundColour()
3897 // TODO: replace this temp test code
3899 return wxColour( 255, 255, 255 );
3902 wxColour
wxGrid::GetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
) )
3904 // TODO: replace this temp test code
3906 return wxColour( 255, 255, 255 );
3909 wxColour
wxGrid::GetDefaultCellTextColour()
3911 // TODO: replace this temp test code
3913 return wxColour( 0, 0, 0 );
3916 wxColour
wxGrid::GetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
) )
3918 // TODO: replace this temp test code
3920 return wxColour( 0, 0, 0 );
3924 wxFont
wxGrid::GetDefaultCellFont()
3926 return m_defaultCellFont
;
3929 wxFont
wxGrid::GetCellFont( int WXUNUSED(row
), int WXUNUSED(col
) )
3931 // TODO: replace this temp test code
3933 return m_defaultCellFont
;
3936 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
3938 // TODO: replace this temp test code
3944 void wxGrid::GetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int *horiz
, int *vert
)
3946 // TODO: replace this temp test code
3952 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
3954 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
3956 if ( resizeExistingRows
)
3960 for ( row
= 0; row
< m_numRows
; row
++ )
3962 m_rowHeights
[row
] = m_defaultRowHeight
;
3963 bottom
+= m_defaultRowHeight
;
3964 m_rowBottoms
[row
] = bottom
;
3970 void wxGrid::SetRowSize( int row
, int height
)
3974 if ( row
>= 0 && row
< m_numRows
)
3976 int h
= wxMax( 0, height
);
3977 int diff
= h
- m_rowHeights
[row
];
3979 m_rowHeights
[row
] = h
;
3980 for ( i
= row
; i
< m_numRows
; i
++ )
3982 m_rowBottoms
[i
] += diff
;
3986 // Note: we are ending the event *after* doing
3987 // default processing in this case
3989 SendEvent( EVT_GRID_ROW_SIZE
,
3994 // TODO: log an error here
3998 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4000 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4002 if ( resizeExistingCols
)
4006 for ( col
= 0; col
< m_numCols
; col
++ )
4008 m_colWidths
[col
] = m_defaultColWidth
;
4009 right
+= m_defaultColWidth
;
4010 m_colRights
[col
] = right
;
4016 void wxGrid::SetColSize( int col
, int width
)
4020 if ( col
>= 0 && col
< m_numCols
)
4022 int w
= wxMax( 0, width
);
4023 int diff
= w
- m_colWidths
[col
];
4024 m_colWidths
[col
] = w
;
4026 for ( i
= col
; i
< m_numCols
; i
++ )
4028 m_colRights
[i
] += diff
;
4032 // Note: we are ending the event *after* doing
4033 // default processing in this case
4035 SendEvent( EVT_GRID_COL_SIZE
,
4040 // TODO: log an error here
4044 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& )
4046 // TODO: everything !!!
4050 void wxGrid::SetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4052 // TODO: everything !!!
4056 void wxGrid::SetDefaultCellTextColour( const wxColour
& )
4058 // TODO: everything !!!
4062 void wxGrid::SetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4064 // TODO: everything !!!
4068 void wxGrid::SetDefaultCellFont( const wxFont
& )
4070 // TODO: everything !!!
4074 void wxGrid::SetCellFont( int WXUNUSED(row
), int WXUNUSED(col
), const wxFont
& )
4076 // TODO: everything !!!
4080 void wxGrid::SetDefaultCellAlignment( int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4082 // TODO: everything !!!
4086 void wxGrid::SetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4088 // TODO: everything !!!
4095 // ------ cell value accessor functions
4098 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4102 m_table
->SetValue( row
, col
, s
.c_str() );
4103 if ( !GetBatchCount() )
4105 wxClientDC
dc( m_gridWin
);
4107 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4110 #if 0 // TODO: edit in place
4112 if ( m_currentCellCoords
.GetRow() == row
&&
4113 m_currentCellCoords
.GetCol() == col
)
4115 SetEditControlValue( s
);
4124 // ------ Block, row and col selection
4127 void wxGrid::SelectRow( int row
, bool addToSelected
)
4131 if ( IsSelection() && addToSelected
)
4133 if ( m_selectedTopLeft
.GetRow() > row
)
4134 m_selectedTopLeft
.SetRow( row
);
4136 m_selectedTopLeft
.SetCol( 0 );
4138 if ( m_selectedBottomRight
.GetRow() < row
)
4139 m_selectedBottomRight
.SetRow( row
);
4141 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4143 // TODO: optimize this so that we only refresh the newly
4146 r
= SelectionToDeviceRect();
4147 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4151 r
= SelectionToDeviceRect();
4153 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4155 m_selectedTopLeft
.Set( row
, 0 );
4156 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4157 r
= SelectionToDeviceRect();
4158 m_gridWin
->Refresh( TRUE
, &r
);
4161 wxGridRangeSelectEvent
gridEvt( GetId(),
4162 EVT_GRID_RANGE_SELECT
,
4165 m_selectedBottomRight
);
4167 GetEventHandler()->ProcessEvent(gridEvt
);
4171 void wxGrid::SelectCol( int col
, bool addToSelected
)
4175 if ( IsSelection() && addToSelected
)
4177 if ( m_selectedTopLeft
.GetCol() > col
)
4178 m_selectedTopLeft
.SetCol( col
);
4180 m_selectedTopLeft
.SetRow( 0 );
4182 if ( m_selectedBottomRight
.GetCol() < col
)
4183 m_selectedBottomRight
.SetCol( col
);
4185 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4187 // TODO: optimize this so that we only refresh the newly
4190 r
= SelectionToDeviceRect();
4191 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4195 r
= SelectionToDeviceRect();
4197 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4199 m_selectedTopLeft
.Set( 0, col
);
4200 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4201 r
= SelectionToDeviceRect();
4202 m_gridWin
->Refresh( TRUE
, &r
);
4205 wxGridRangeSelectEvent
gridEvt( GetId(),
4206 EVT_GRID_RANGE_SELECT
,
4209 m_selectedBottomRight
);
4211 GetEventHandler()->ProcessEvent(gridEvt
);
4215 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4219 if ( topRow
> bottomRow
)
4226 if ( leftCol
> rightCol
)
4233 m_selectedTopLeft
.Set( topRow
, leftCol
);
4234 m_selectedBottomRight
.Set( bottomRow
, rightCol
);
4237 r
= SelectionToDeviceRect();
4238 m_gridWin
->Refresh( TRUE
, &r
);
4240 // only generate an event if the block is not being selected by
4241 // dragging the mouse (in which case the event will be generated in
4242 // the mouse event handler)
4243 if ( !m_isDragging
)
4245 wxGridRangeSelectEvent
gridEvt( GetId(),
4246 EVT_GRID_RANGE_SELECT
,
4249 m_selectedBottomRight
);
4251 GetEventHandler()->ProcessEvent(gridEvt
);
4255 void wxGrid::SelectAll()
4257 m_selectedTopLeft
.Set( 0, 0 );
4258 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
4260 m_gridWin
->Refresh();
4264 void wxGrid::ClearSelection()
4266 m_selectedTopLeft
= wxGridNoCellCoords
;
4267 m_selectedBottomRight
= wxGridNoCellCoords
;
4271 // This function returns the rectangle that encloses the selected cells
4272 // in device coords clipped to the client size of the grid window.
4274 wxRect
wxGrid::SelectionToDeviceRect()
4279 if ( IsSelection() )
4281 cellRect
= CellToRect( m_selectedTopLeft
);
4282 if ( cellRect
!= wxGridNoCellRect
)
4288 rect
= wxRect( 0, 0, 0, 0 );
4291 cellRect
= CellToRect( m_selectedBottomRight
);
4292 if ( cellRect
!= wxGridNoCellRect
)
4298 return wxGridNoCellRect
;
4301 // convert to scrolled coords
4303 int left
, top
, right
, bottom
;
4304 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4305 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4308 m_gridWin
->GetClientSize( &cw
, &ch
);
4310 rect
.SetLeft( wxMax(0, left
) );
4311 rect
.SetTop( wxMax(0, top
) );
4312 rect
.SetRight( wxMin(cw
, right
) );
4313 rect
.SetBottom( wxMin(ch
, bottom
) );
4317 return wxGridNoCellRect
;
4325 // ------ Grid event classes
4328 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
4330 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
4331 int row
, int col
, int x
, int y
,
4332 bool control
, bool shift
, bool alt
, bool meta
)
4333 : wxNotifyEvent( type
, id
)
4339 m_control
= control
;
4344 SetEventObject(obj
);
4348 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
4350 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
4351 int rowOrCol
, int x
, int y
,
4352 bool control
, bool shift
, bool alt
, bool meta
)
4353 : wxNotifyEvent( type
, id
)
4355 m_rowOrCol
= rowOrCol
;
4358 m_control
= control
;
4363 SetEventObject(obj
);
4367 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
4369 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
4370 const wxGridCellCoords
& topLeft
,
4371 const wxGridCellCoords
& bottomRight
,
4372 bool control
, bool shift
, bool alt
, bool meta
)
4373 : wxNotifyEvent( type
, id
)
4375 m_topLeft
= topLeft
;
4376 m_bottomRight
= bottomRight
;
4377 m_control
= control
;
4382 SetEventObject(obj
);
4386 #endif // ifndef wxUSE_NEW_GRID