1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "grid.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
25 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
37 #include "wx/generic/grid.h"
39 #ifndef WXGRID_DRAW_LINES
40 #define WXGRID_DRAW_LINES 1
43 //////////////////////////////////////////////////////////////////////
45 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
46 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
48 // this is a magic incantation which must be done!
49 #include "wx/arrimpl.cpp"
51 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
54 // TODO: fixed so far - make configurable later (and also different for x/y)
55 static const size_t GRID_SCROLL_LINE
= 10;
57 //////////////////////////////////////////////////////////////////////
59 // Abstract base class for grid data (the model)
61 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
64 wxGridTableBase::wxGridTableBase()
67 m_view
= (wxGrid
*) NULL
;
70 wxGridTableBase::~wxGridTableBase()
75 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
77 wxLogWarning( wxT("Called grid table class function InsertRows(pos=%d, N=%d)\n"
78 "but your derived table class does not override this function"),
84 bool wxGridTableBase::AppendRows( size_t numRows
)
86 wxLogWarning( wxT("Called grid table class function AppendRows(N=%d)\n"
87 "but your derived table class does not override this function"),
93 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
95 wxLogWarning( wxT("Called grid table class function DeleteRows(pos=%d, N=%d)\n"
96 "but your derived table class does not override this function"),
102 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
104 wxLogWarning( wxT("Called grid table class function InsertCols(pos=%d, N=%d)\n"
105 "but your derived table class does not override this function"),
111 bool wxGridTableBase::AppendCols( size_t numCols
)
113 wxLogWarning( wxT("Called grid table class function AppendCols(N=%d)\n"
114 "but your derived table class does not override this function"),
120 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
122 wxLogWarning( wxT("Called grid table class function DeleteCols(pos=%d, N=%d)\n"
123 "but your derived table class does not override this function"),
130 wxString
wxGridTableBase::GetRowLabelValue( int row
)
137 wxString
wxGridTableBase::GetColLabelValue( int col
)
139 // default col labels are:
140 // cols 0 to 25 : A-Z
141 // cols 26 to 675 : AA-ZZ
148 s
+= (_T('A') + (wxChar
)( col%26
));
150 if ( col
< 0 ) break;
153 // reverse the string...
155 for ( i
= 0; i
< n
; i
++ )
165 //////////////////////////////////////////////////////////////////////
167 // Message class for the grid table to send requests and notifications
171 wxGridTableMessage::wxGridTableMessage()
173 m_table
= (wxGridTableBase
*) NULL
;
179 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
180 int commandInt1
, int commandInt2
)
184 m_comInt1
= commandInt1
;
185 m_comInt2
= commandInt2
;
190 //////////////////////////////////////////////////////////////////////
192 // A basic grid table for string data. An object of this class will
193 // created by wxGrid if you don't specify an alternative table class.
196 WX_DEFINE_OBJARRAY(wxGridStringArray
)
198 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
200 wxGridStringTable::wxGridStringTable()
205 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
210 m_data
.Alloc( numRows
);
214 for ( col
= 0; col
< numCols
; col
++ )
216 sa
.Add( wxEmptyString
);
219 for ( row
= 0; row
< numRows
; row
++ )
225 wxGridStringTable::~wxGridStringTable()
229 long wxGridStringTable::GetNumberRows()
231 return m_data
.GetCount();
234 long wxGridStringTable::GetNumberCols()
236 if ( m_data
.GetCount() > 0 )
237 return m_data
[0].GetCount();
242 wxString
wxGridStringTable::GetValue( int row
, int col
)
244 // TODO: bounds checking
246 return m_data
[row
][col
];
249 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& s
)
251 // TODO: bounds checking
253 m_data
[row
][col
] = s
;
256 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
258 // TODO: bounds checking
260 return (m_data
[row
][col
] == wxEmptyString
);
264 void wxGridStringTable::Clear()
267 int numRows
, numCols
;
269 numRows
= m_data
.GetCount();
272 numCols
= m_data
[0].GetCount();
274 for ( row
= 0; row
< numRows
; row
++ )
276 for ( col
= 0; col
< numCols
; col
++ )
278 m_data
[row
][col
] = wxEmptyString
;
285 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
289 size_t curNumRows
= m_data
.GetCount();
290 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
292 if ( pos
>= curNumRows
)
294 return AppendRows( numRows
);
298 sa
.Alloc( curNumCols
);
299 for ( col
= 0; col
< curNumCols
; col
++ )
301 sa
.Add( wxEmptyString
);
304 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
306 m_data
.Insert( sa
, row
);
311 wxGridTableMessage
msg( this,
312 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
316 GetView()->ProcessTableMessage( msg
);
322 bool wxGridStringTable::AppendRows( size_t numRows
)
326 size_t curNumRows
= m_data
.GetCount();
327 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
330 if ( curNumCols
> 0 )
332 sa
.Alloc( curNumCols
);
333 for ( col
= 0; col
< curNumCols
; col
++ )
335 sa
.Add( wxEmptyString
);
339 for ( row
= 0; row
< numRows
; row
++ )
346 wxGridTableMessage
msg( this,
347 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
350 GetView()->ProcessTableMessage( msg
);
356 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
360 size_t curNumRows
= m_data
.GetCount();
362 if ( pos
>= curNumRows
)
364 wxLogError( wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)...\n"
365 "Pos value is invalid for present table with %d rows"),
366 pos
, numRows
, curNumRows
);
370 if ( numRows
> curNumRows
- pos
)
372 numRows
= curNumRows
- pos
;
375 if ( numRows
>= curNumRows
)
377 m_data
.Empty(); // don't release memory just yet
381 for ( n
= 0; n
< numRows
; n
++ )
383 m_data
.Remove( pos
);
389 wxGridTableMessage
msg( this,
390 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
394 GetView()->ProcessTableMessage( msg
);
400 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
404 size_t curNumRows
= m_data
.GetCount();
405 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
407 if ( pos
>= curNumCols
)
409 return AppendCols( numCols
);
412 for ( row
= 0; row
< curNumRows
; row
++ )
414 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
416 m_data
[row
].Insert( wxEmptyString
, col
);
422 wxGridTableMessage
msg( this,
423 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
427 GetView()->ProcessTableMessage( msg
);
433 bool wxGridStringTable::AppendCols( size_t numCols
)
437 size_t curNumRows
= m_data
.GetCount();
440 // TODO: something better than this ?
442 wxLogError( wxT("Unable to append cols to a grid table with no rows.\n"
443 "Call AppendRows() first") );
447 for ( row
= 0; row
< curNumRows
; row
++ )
449 for ( n
= 0; n
< numCols
; n
++ )
451 m_data
[row
].Add( wxEmptyString
);
457 wxGridTableMessage
msg( this,
458 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
461 GetView()->ProcessTableMessage( msg
);
467 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
471 size_t curNumRows
= m_data
.GetCount();
472 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
474 if ( pos
>= curNumCols
)
476 wxLogError( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
477 "Pos value is invalid for present table with %d cols"),
478 pos
, numCols
, curNumCols
);
482 if ( numCols
> curNumCols
- pos
)
484 numCols
= curNumCols
- pos
;
487 for ( row
= 0; row
< curNumRows
; row
++ )
489 if ( numCols
>= curNumCols
)
495 for ( n
= 0; n
< numCols
; n
++ )
497 m_data
[row
].Remove( pos
);
504 wxGridTableMessage
msg( this,
505 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
509 GetView()->ProcessTableMessage( msg
);
515 wxString
wxGridStringTable::GetRowLabelValue( int row
)
517 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
519 // using default label
521 return wxGridTableBase::GetRowLabelValue( row
);
525 return m_rowLabels
[ row
];
529 wxString
wxGridStringTable::GetColLabelValue( int col
)
531 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
533 // using default label
535 return wxGridTableBase::GetColLabelValue( col
);
539 return m_colLabels
[ col
];
543 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
545 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
547 int n
= m_rowLabels
.GetCount();
549 for ( i
= n
; i
<= row
; i
++ )
551 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
555 m_rowLabels
[row
] = value
;
558 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
560 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
562 int n
= m_colLabels
.GetCount();
564 for ( i
= n
; i
<= col
; i
++ )
566 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
570 m_colLabels
[col
] = value
;
576 //////////////////////////////////////////////////////////////////////
578 IMPLEMENT_DYNAMIC_CLASS( wxGridTextCtrl
, wxTextCtrl
)
580 BEGIN_EVENT_TABLE( wxGridTextCtrl
, wxTextCtrl
)
581 EVT_KEY_DOWN( wxGridTextCtrl::OnKeyDown
)
585 wxGridTextCtrl::wxGridTextCtrl( wxWindow
*par
,
589 const wxString
& value
,
593 : wxTextCtrl( par
, id
, value
, pos
, size
, style
)
596 m_isCellControl
= isCellControl
;
600 void wxGridTextCtrl::OnKeyDown( wxKeyEvent
& event
)
602 switch ( event
.KeyCode() )
605 m_grid
->SetEditControlValue( startValue
);
606 SetInsertionPointEnd();
616 if ( m_isCellControl
)
618 // send the event to the parent grid, skipping the
619 // event if nothing happens
621 event
.Skip( m_grid
->ProcessEvent( event
) );
625 // default text control response within the top edit
633 if ( m_isCellControl
)
635 if ( !m_grid
->ProcessEvent( event
) )
637 #if defined(__WXMOTIF__) || defined(__WXGTK__)
638 // wxMotif needs a little extra help...
640 int pos
= GetInsertionPoint();
641 wxString
s( GetValue() );
642 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
644 SetInsertionPoint( pos
);
646 // the other ports can handle a Return key press
656 if ( m_isCellControl
)
658 // send the event to the parent grid, skipping the
659 // event if nothing happens
661 event
.Skip( m_grid
->ProcessEvent( event
) );
665 // default text control response within the top edit
677 void wxGridTextCtrl::SetStartValue( const wxString
& s
)
680 wxTextCtrl::SetValue(s
);
685 //////////////////////////////////////////////////////////////////////
687 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
689 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
690 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
691 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
692 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
695 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
697 const wxPoint
&pos
, const wxSize
&size
)
698 : wxWindow( parent
, id
, pos
, size
)
703 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
707 // NO - don't do this because it will set both the x and y origin
708 // coords to match the parent scrolled window and we just want to
709 // set the y coord - MB
711 // m_owner->PrepareDC( dc );
714 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
715 dc
.SetDeviceOrigin( 0, -y
);
717 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
718 m_owner
->DrawRowLabels( dc
);
722 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
724 m_owner
->ProcessRowLabelMouseEvent( event
);
728 // This seems to be required for wxMotif otherwise the mouse
729 // cursor must be in the cell edit control to get key events
731 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
733 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
738 //////////////////////////////////////////////////////////////////////
740 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
742 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
743 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
744 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
745 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
748 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
750 const wxPoint
&pos
, const wxSize
&size
)
751 : wxWindow( parent
, id
, pos
, size
)
756 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
760 // NO - don't do this because it will set both the x and y origin
761 // coords to match the parent scrolled window and we just want to
762 // set the x coord - MB
764 // m_owner->PrepareDC( dc );
767 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
768 dc
.SetDeviceOrigin( -x
, 0 );
770 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
771 m_owner
->DrawColLabels( dc
);
775 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
777 m_owner
->ProcessColLabelMouseEvent( event
);
781 // This seems to be required for wxMotif otherwise the mouse
782 // cursor must be in the cell edit control to get key events
784 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
786 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
791 //////////////////////////////////////////////////////////////////////
793 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
795 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
796 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
797 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
798 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
801 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
803 const wxPoint
&pos
, const wxSize
&size
)
804 : wxWindow( parent
, id
, pos
, size
)
809 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
813 int client_height
= 0;
814 int client_width
= 0;
815 GetClientSize( &client_width
, &client_height
);
817 dc
.SetPen( *wxBLACK_PEN
);
818 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
819 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
821 dc
.SetPen( *wxWHITE_PEN
);
822 dc
.DrawLine( 0, 0, client_width
, 0 );
823 dc
.DrawLine( 0, 0, 0, client_height
);
827 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
829 m_owner
->ProcessCornerLabelMouseEvent( event
);
833 // This seems to be required for wxMotif otherwise the mouse
834 // cursor must be in the cell edit control to get key events
836 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
838 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
843 //////////////////////////////////////////////////////////////////////
845 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
847 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
848 EVT_PAINT( wxGridWindow::OnPaint
)
849 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
850 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
853 wxGridWindow::wxGridWindow( wxGrid
*parent
,
854 wxGridRowLabelWindow
*rowLblWin
,
855 wxGridColLabelWindow
*colLblWin
,
856 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
857 : wxPanel( parent
, id
, pos
, size
, wxSUNKEN_BORDER
, "grid window" )
860 m_rowLabelWin
= rowLblWin
;
861 m_colLabelWin
= colLblWin
;
863 SetBackgroundColour( "WHITE" );
867 wxGridWindow::~wxGridWindow()
872 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
874 wxPaintDC
dc( this );
875 m_owner
->PrepareDC( dc
);
876 wxRegion reg
= GetUpdateRegion();
877 m_owner
->CalcCellsExposed( reg
);
878 m_owner
->DrawGridCellArea( dc
);
879 #if WXGRID_DRAW_LINES
880 m_owner
->DrawAllGridLines( dc
, reg
);
885 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
887 wxPanel::ScrollWindow( dx
, dy
, rect
);
888 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
889 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
893 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
895 m_owner
->ProcessGridCellMouseEvent( event
);
899 // This seems to be required for wxMotif otherwise the mouse
900 // cursor must be in the cell edit control to get key events
902 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
904 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
909 //////////////////////////////////////////////////////////////////////
911 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
913 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
914 EVT_PAINT( wxGrid::OnPaint
)
915 EVT_SIZE( wxGrid::OnSize
)
916 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
919 wxGrid::wxGrid( wxWindow
*parent
,
924 const wxString
& name
)
925 : wxScrolledWindow( parent
, id
, pos
, size
, style
, name
)
938 // ----- internal init and update functions
941 void wxGrid::Create()
943 int colLblH
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
944 int rowLblW
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
946 m_rowLabelWin
= new wxGridRowLabelWindow( this,
949 wxSize(rowLblW
,-1) );
951 m_colLabelWin
= new wxGridColLabelWindow( this,
954 wxSize(-1, colLblH
) );
956 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
959 wxSize(rowLblW
, colLblH
) );
961 m_gridWin
= new wxGridWindow( this,
968 SetTargetWindow( m_gridWin
);
970 m_mainSizer
= new wxBoxSizer( wxVERTICAL
);
972 m_topSizer
= new wxBoxSizer( wxHORIZONTAL
);
973 m_topSizer
->Add( m_cornerLabelWin
, 0 );
974 m_topSizer
->Add( m_colLabelWin
, 1 );
976 m_mainSizer
->Add( m_topSizer
, 0, wxEXPAND
);
978 m_middleSizer
= new wxBoxSizer( wxHORIZONTAL
);
979 m_middleSizer
->Add( m_rowLabelWin
, 0, wxEXPAND
);
980 m_middleSizer
->Add( m_gridWin
, 1, wxEXPAND
);
982 m_mainSizer
->Add( m_middleSizer
, 1, wxEXPAND
);
984 SetAutoLayout( TRUE
);
985 SetSizer( m_mainSizer
);
989 bool wxGrid::CreateGrid( int numRows
, int numCols
)
993 wxLogError( wxT("wxGrid::CreateGrid(numRows, numCols) called more than once") );
1001 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
1002 m_table
->SetView( this );
1015 if ( m_numRows
<= 0 )
1016 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
1018 if ( m_numCols
<= 0 )
1019 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
1021 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
1022 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1024 if ( m_rowLabelWin
)
1026 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1030 m_labelBackgroundColour
= wxColour( _T("WHITE") );
1033 m_labelTextColour
= wxColour( _T("BLACK") );
1035 // TODO: something better than this ?
1037 m_labelFont
= this->GetFont();
1038 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
1040 m_rowLabelHorizAlign
= wxLEFT
;
1041 m_rowLabelVertAlign
= wxCENTRE
;
1043 m_colLabelHorizAlign
= wxCENTRE
;
1044 m_colLabelVertAlign
= wxTOP
;
1046 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
1047 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1049 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1050 m_defaultRowHeight
+= 8;
1052 m_defaultRowHeight
+= 4;
1055 m_rowHeights
.Alloc( m_numRows
);
1056 m_rowBottoms
.Alloc( m_numRows
);
1058 for ( i
= 0; i
< m_numRows
; i
++ )
1060 m_rowHeights
.Add( m_defaultRowHeight
);
1061 rowBottom
+= m_defaultRowHeight
;
1062 m_rowBottoms
.Add( rowBottom
);
1065 m_colWidths
.Alloc( m_numCols
);
1066 m_colRights
.Alloc( m_numCols
);
1068 for ( i
= 0; i
< m_numCols
; i
++ )
1070 m_colWidths
.Add( m_defaultColWidth
);
1071 colRight
+= m_defaultColWidth
;
1072 m_colRights
.Add( colRight
);
1075 // TODO: improve this ?
1077 m_defaultCellFont
= this->GetFont();
1079 m_gridLineColour
= wxColour( 128, 128, 255 );
1080 m_gridLinesEnabled
= TRUE
;
1082 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1084 m_dragRowOrCol
= -1;
1085 m_isDragging
= FALSE
;
1087 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
1088 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
1090 m_currentCellCoords
= wxGridNoCellCoords
;
1092 m_selectedTopLeft
= wxGridNoCellCoords
;
1093 m_selectedBottomRight
= wxGridNoCellCoords
;
1095 m_editable
= TRUE
; // default for whole grid
1097 m_inOnKeyDown
= FALSE
;
1100 // TODO: extend this to other types of controls
1102 m_cellEditCtrl
= new wxGridTextCtrl( m_gridWin
,
1109 #if defined(__WXMSW__)
1110 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
1114 m_cellEditCtrl
->Show( FALSE
);
1115 m_cellEditCtrlEnabled
= TRUE
;
1116 m_editCtrlType
= wxGRID_TEXTCTRL
;
1120 void wxGrid::CalcDimensions()
1123 GetClientSize( &cw
, &ch
);
1125 if ( m_numRows
> 0 && m_numCols
> 0 )
1127 int right
= m_colRights
[ m_numCols
-1 ] + 20;
1128 int bottom
= m_rowBottoms
[ m_numRows
-1 ] + 20;
1130 // TODO: restore the scroll position that we had before sizing
1133 GetViewStart( &x
, &y
);
1134 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
1135 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
1141 // this is called when the grid table sends a message to say that it
1142 // has been redimensioned
1144 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
1148 switch ( msg
.GetId() )
1150 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
1152 size_t pos
= msg
.GetCommandInt();
1153 int numRows
= msg
.GetCommandInt2();
1154 for ( i
= 0; i
< numRows
; i
++ )
1156 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
1157 m_rowBottoms
.Insert( 0, pos
);
1159 m_numRows
+= numRows
;
1162 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
1164 for ( i
= pos
; i
< m_numRows
; i
++ )
1166 bottom
+= m_rowHeights
[i
];
1167 m_rowBottoms
[i
] = bottom
;
1173 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
1175 int numRows
= msg
.GetCommandInt();
1176 for ( i
= 0; i
< numRows
; i
++ )
1178 m_rowHeights
.Add( m_defaultRowHeight
);
1179 m_rowBottoms
.Add( 0 );
1182 int oldNumRows
= m_numRows
;
1183 m_numRows
+= numRows
;
1186 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
1188 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
1190 bottom
+= m_rowHeights
[i
];
1191 m_rowBottoms
[i
] = bottom
;
1197 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
1199 size_t pos
= msg
.GetCommandInt();
1200 int numRows
= msg
.GetCommandInt2();
1201 for ( i
= 0; i
< numRows
; i
++ )
1203 m_rowHeights
.Remove( pos
);
1204 m_rowBottoms
.Remove( pos
);
1206 m_numRows
-= numRows
;
1211 m_colWidths
.Clear();
1212 m_colRights
.Clear();
1213 m_currentCellCoords
= wxGridNoCellCoords
;
1217 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
1218 m_currentCellCoords
.Set( 0, 0 );
1221 for ( i
= 0; i
< m_numRows
; i
++ )
1223 h
+= m_rowHeights
[i
];
1224 m_rowBottoms
[i
] = h
;
1232 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
1234 size_t pos
= msg
.GetCommandInt();
1235 int numCols
= msg
.GetCommandInt2();
1236 for ( i
= 0; i
< numCols
; i
++ )
1238 m_colWidths
.Insert( m_defaultColWidth
, pos
);
1239 m_colRights
.Insert( 0, pos
);
1241 m_numCols
+= numCols
;
1244 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
1246 for ( i
= pos
; i
< m_numCols
; i
++ )
1248 right
+= m_colWidths
[i
];
1249 m_colRights
[i
] = right
;
1255 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
1257 int numCols
= msg
.GetCommandInt();
1258 for ( i
= 0; i
< numCols
; i
++ )
1260 m_colWidths
.Add( m_defaultColWidth
);
1261 m_colRights
.Add( 0 );
1264 int oldNumCols
= m_numCols
;
1265 m_numCols
+= numCols
;
1268 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
1270 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
1272 right
+= m_colWidths
[i
];
1273 m_colRights
[i
] = right
;
1279 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
1281 size_t pos
= msg
.GetCommandInt();
1282 int numCols
= msg
.GetCommandInt2();
1283 for ( i
= 0; i
< numCols
; i
++ )
1285 m_colWidths
.Remove( pos
);
1286 m_colRights
.Remove( pos
);
1288 m_numCols
-= numCols
;
1292 #if 0 // leave the row alone here so that AppendCols will work subsequently
1294 m_rowHeights
.Clear();
1295 m_rowBottoms
.Clear();
1297 m_currentCellCoords
= wxGridNoCellCoords
;
1301 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
1302 m_currentCellCoords
.Set( 0, 0 );
1305 for ( i
= 0; i
< m_numCols
; i
++ )
1307 w
+= m_colWidths
[i
];
1320 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
1322 wxRegionIterator
iter( reg
);
1325 m_rowLabelsExposed
.Empty();
1332 // TODO: remove this when we can...
1333 // There is a bug in wxMotif that gives garbage update
1334 // rectangles if you jump-scroll a long way by clicking the
1335 // scrollbar with middle button. This is a work-around
1337 #if defined(__WXMOTIF__)
1339 m_gridWin
->GetClientSize( &cw
, &ch
);
1340 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1341 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1344 // logical bounds of update region
1347 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
1348 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
1350 // find the row labels within these bounds
1354 for ( row
= 0; row
< m_numRows
; row
++ )
1356 if ( m_rowBottoms
[row
] < top
) continue;
1358 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1359 if ( rowTop
> bottom
) break;
1361 m_rowLabelsExposed
.Add( row
);
1369 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
1371 wxRegionIterator
iter( reg
);
1374 m_colLabelsExposed
.Empty();
1381 // TODO: remove this when we can...
1382 // There is a bug in wxMotif that gives garbage update
1383 // rectangles if you jump-scroll a long way by clicking the
1384 // scrollbar with middle button. This is a work-around
1386 #if defined(__WXMOTIF__)
1388 m_gridWin
->GetClientSize( &cw
, &ch
);
1389 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1390 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1393 // logical bounds of update region
1396 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
1397 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
1399 // find the cells within these bounds
1403 for ( col
= 0; col
< m_numCols
; col
++ )
1405 if ( m_colRights
[col
] < left
) continue;
1407 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1408 if ( colLeft
> right
) break;
1410 m_colLabelsExposed
.Add( col
);
1418 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
1420 wxRegionIterator
iter( reg
);
1423 m_cellsExposed
.Empty();
1424 m_rowsExposed
.Empty();
1425 m_colsExposed
.Empty();
1427 int left
, top
, right
, bottom
;
1432 // TODO: remove this when we can...
1433 // There is a bug in wxMotif that gives garbage update
1434 // rectangles if you jump-scroll a long way by clicking the
1435 // scrollbar with middle button. This is a work-around
1437 #if defined(__WXMOTIF__)
1439 m_gridWin
->GetClientSize( &cw
, &ch
);
1440 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
1441 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
1442 r
.SetRight( wxMin( r
.GetRight(), cw
) );
1443 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
1446 // logical bounds of update region
1448 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
1449 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
1451 // find the cells within these bounds
1454 int colLeft
, rowTop
;
1455 for ( row
= 0; row
< m_numRows
; row
++ )
1457 if ( m_rowBottoms
[row
] < top
) continue;
1459 rowTop
= m_rowBottoms
[row
] - m_rowHeights
[row
];
1460 if ( rowTop
> bottom
) break;
1462 m_rowsExposed
.Add( row
);
1464 for ( col
= 0; col
< m_numCols
; col
++ )
1466 if ( m_colRights
[col
] < left
) continue;
1468 colLeft
= m_colRights
[col
] - m_colWidths
[col
];
1469 if ( colLeft
> right
) break;
1471 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
) m_colsExposed
.Add( col
);
1472 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
1481 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
1484 wxPoint
pos( event
.GetPosition() );
1485 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1487 if ( event
.Dragging() )
1489 m_isDragging
= TRUE
;
1491 if ( event
.LeftIsDown() )
1493 switch( m_cursorMode
)
1495 case WXGRID_CURSOR_RESIZE_ROW
:
1497 int cw
, ch
, left
, dummy
;
1498 m_gridWin
->GetClientSize( &cw
, &ch
);
1499 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1501 wxClientDC
dc( m_gridWin
);
1503 dc
.SetLogicalFunction(wxINVERT
);
1504 if ( m_dragLastPos
>= 0 )
1506 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1508 dc
.DrawLine( left
, y
, left
+cw
, y
);
1513 case WXGRID_CURSOR_SELECT_ROW
:
1515 if ( (row
= YToRow( y
)) >= 0 &&
1516 !IsInSelection( row
, 0 ) )
1518 SelectRow( row
, TRUE
);
1527 m_isDragging
= FALSE
;
1530 // ------------ Left button pressed
1532 if ( event
.LeftDown() )
1534 // don't send a label click event for a hit on the
1535 // edge of the row label - this is probably the user
1536 // wanting to resize the row
1538 if ( YToEdgeOfRow(y
) < 0 )
1542 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
1544 SelectRow( row
, event
.ShiftDown() );
1545 m_cursorMode
= WXGRID_CURSOR_SELECT_ROW
;
1550 // starting to drag-resize a row
1552 m_rowLabelWin
->CaptureMouse();
1557 // ------------ Left double click
1559 else if (event
.LeftDClick() )
1561 if ( YToEdgeOfRow(y
) < 0 )
1564 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
1569 // ------------ Left button released
1571 else if ( event
.LeftUp() )
1573 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
1575 m_rowLabelWin
->ReleaseMouse();
1577 if ( m_dragLastPos
>= 0 )
1579 // erase the last line and resize the row
1581 int cw
, ch
, left
, dummy
;
1582 m_gridWin
->GetClientSize( &cw
, &ch
);
1583 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
1585 wxClientDC
dc( m_gridWin
);
1587 dc
.SetLogicalFunction( wxINVERT
);
1588 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
1589 HideCellEditControl();
1591 int rowTop
= m_rowBottoms
[m_dragRowOrCol
] - m_rowHeights
[m_dragRowOrCol
];
1592 SetRowSize( m_dragRowOrCol
, wxMax( y
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
1593 if ( !GetBatchCount() )
1595 // Only needed to get the correct rect.y:
1596 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
1598 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
1599 rect
.width
= m_rowLabelWidth
;
1600 rect
.height
= ch
- rect
.y
;
1601 m_rowLabelWin
->Refresh( TRUE
, &rect
);
1603 m_gridWin
->Refresh( TRUE
, &rect
);
1606 ShowCellEditControl();
1608 // Note: we are ending the event *after* doing
1609 // default processing in this case
1611 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1615 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1620 // ------------ Right button down
1622 else if ( event
.RightDown() )
1625 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1627 // no default action at the moment
1632 // ------------ Right double click
1634 else if ( event
.RightDClick() )
1637 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1639 // no default action at the moment
1644 // ------------ No buttons down and mouse moving
1646 else if ( event
.Moving() )
1648 m_dragRowOrCol
= YToEdgeOfRow( y
);
1649 if ( m_dragRowOrCol
>= 0 )
1651 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1653 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1654 m_rowLabelWin
->SetCursor( m_rowResizeCursor
);
1659 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1660 if ( m_rowLabelWin
->GetCursor() == m_rowResizeCursor
)
1661 m_rowLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1667 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
1670 wxPoint
pos( event
.GetPosition() );
1671 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1673 if ( event
.Dragging() )
1675 m_isDragging
= TRUE
;
1677 if ( event
.LeftIsDown() )
1679 switch( m_cursorMode
)
1681 case WXGRID_CURSOR_RESIZE_COL
:
1683 int cw
, ch
, dummy
, top
;
1684 m_gridWin
->GetClientSize( &cw
, &ch
);
1685 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1687 wxClientDC
dc( m_gridWin
);
1689 dc
.SetLogicalFunction(wxINVERT
);
1690 if ( m_dragLastPos
>= 0 )
1692 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1694 dc
.DrawLine( x
, top
, x
, top
+ch
);
1699 case WXGRID_CURSOR_SELECT_COL
:
1701 if ( (col
= XToCol( x
)) >= 0 &&
1702 !IsInSelection( 0, col
) )
1704 SelectCol( col
, TRUE
);
1713 m_isDragging
= FALSE
;
1716 // ------------ Left button pressed
1718 if ( event
.LeftDown() )
1720 // don't send a label click event for a hit on the
1721 // edge of the col label - this is probably the user
1722 // wanting to resize the col
1724 if ( XToEdgeOfCol(x
) < 0 )
1728 !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
1730 SelectCol( col
, event
.ShiftDown() );
1731 m_cursorMode
= WXGRID_CURSOR_SELECT_COL
;
1736 // starting to drag-resize a col
1738 m_colLabelWin
->CaptureMouse();
1743 // ------------ Left double click
1745 if ( event
.LeftDClick() )
1747 if ( XToEdgeOfCol(x
) < 0 )
1750 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
1755 // ------------ Left button released
1757 else if ( event
.LeftUp() )
1759 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
1761 m_colLabelWin
->ReleaseMouse();
1763 if ( m_dragLastPos
>= 0 )
1765 // erase the last line and resize the col
1767 int cw
, ch
, dummy
, top
;
1768 m_gridWin
->GetClientSize( &cw
, &ch
);
1769 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
1771 wxClientDC
dc( m_gridWin
);
1773 dc
.SetLogicalFunction( wxINVERT
);
1774 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
1775 HideCellEditControl();
1777 int colLeft
= m_colRights
[m_dragRowOrCol
] - m_colWidths
[m_dragRowOrCol
];
1778 SetColSize( m_dragRowOrCol
, wxMax( x
- colLeft
, WXGRID_MIN_COL_WIDTH
) );
1780 if ( !GetBatchCount() )
1782 // Only needed to get the correct rect.x:
1783 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
1785 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
1786 rect
.width
= cw
- rect
.x
;
1787 rect
.height
= m_colLabelHeight
;
1788 m_colLabelWin
->Refresh( TRUE
, &rect
);
1790 m_gridWin
->Refresh( TRUE
, &rect
);
1793 ShowCellEditControl();
1795 // Note: we are ending the event *after* doing
1796 // default processing in this case
1798 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
1802 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1807 // ------------ Right button down
1809 else if ( event
.RightDown() )
1812 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
1814 // no default action at the moment
1819 // ------------ Right double click
1821 else if ( event
.RightDClick() )
1824 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
1826 // no default action at the moment
1831 // ------------ No buttons down and mouse moving
1833 else if ( event
.Moving() )
1835 m_dragRowOrCol
= XToEdgeOfCol( x
);
1836 if ( m_dragRowOrCol
>= 0 )
1838 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1840 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
1841 m_colLabelWin
->SetCursor( m_colResizeCursor
);
1846 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1847 if ( m_colLabelWin
->GetCursor() == m_colResizeCursor
)
1848 m_colLabelWin
->SetCursor( *wxSTANDARD_CURSOR
);
1854 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
1856 if ( event
.LeftDown() )
1858 // indicate corner label by having both row and
1861 if ( !SendEvent( EVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
1867 else if ( event
.LeftDClick() )
1869 SendEvent( EVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
1872 else if ( event
.RightDown() )
1874 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
1876 // no default action at the moment
1880 else if ( event
.RightDClick() )
1882 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
1884 // no default action at the moment
1890 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
1893 wxPoint
pos( event
.GetPosition() );
1894 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
1896 wxGridCellCoords coords
;
1897 XYToCell( x
, y
, coords
);
1899 if ( event
.Dragging() )
1901 m_isDragging
= TRUE
;
1902 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1904 // Hide the edit control, so it
1905 // won't interfer with drag-shrinking.
1906 if ( IsCellEditControlEnabled() )
1907 HideCellEditControl();
1908 if ( coords
!= wxGridNoCellCoords
)
1910 if ( !IsSelection() )
1912 SelectBlock( coords
, coords
);
1916 SelectBlock( m_currentCellCoords
, coords
);
1924 m_isDragging
= FALSE
;
1926 if ( coords
!= wxGridNoCellCoords
)
1928 if ( event
.LeftDown() )
1930 if ( event
.ShiftDown() )
1932 SelectBlock( m_currentCellCoords
, coords
);
1936 if ( !SendEvent( EVT_GRID_CELL_LEFT_CLICK
,
1941 MakeCellVisible( coords
);
1942 SetCurrentCell( coords
);
1948 // ------------ Left double click
1950 else if ( event
.LeftDClick() )
1952 SendEvent( EVT_GRID_CELL_LEFT_DCLICK
,
1959 // ------------ Left button released
1961 else if ( event
.LeftUp() )
1963 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1965 if ( IsSelection() )
1967 SendEvent( EVT_GRID_RANGE_SELECT
, -1, -1, event
);
1971 // Show the edit control, if it has
1972 // been hidden for drag-shrinking.
1973 if ( IsCellEditControlEnabled() )
1974 ShowCellEditControl();
1980 // ------------ Right button down
1982 else if ( event
.RightDown() )
1984 if ( !SendEvent( EVT_GRID_CELL_RIGHT_CLICK
,
1989 // no default action at the moment
1994 // ------------ Right double click
1996 else if ( event
.RightDClick() )
1998 if ( !SendEvent( EVT_GRID_CELL_RIGHT_DCLICK
,
2003 // no default action at the moment
2007 // ------------ Moving and no button action
2009 else if ( event
.Moving() && !event
.IsButton() )
2011 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2018 // ------ interaction with data model
2020 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
2022 switch ( msg
.GetId() )
2024 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
2025 return GetModelValues();
2027 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
2028 return SetModelValues();
2030 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2031 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2032 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2033 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2034 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2035 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2036 return Redimension( msg
);
2045 // The behaviour of this function depends on the grid table class
2046 // Clear() function. For the default wxGridStringTable class the
2047 // behavious is to replace all cell contents with wxEmptyString but
2048 // not to change the number of rows or cols.
2050 void wxGrid::ClearGrid()
2055 SetEditControlValue();
2056 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2061 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2063 // TODO: something with updateLabels flag
2067 wxLogError( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2073 bool ok
= m_table
->InsertRows( pos
, numRows
);
2075 // the table will have sent the results of the insert row
2076 // operation to this view object as a grid table message
2080 if ( m_numCols
== 0 )
2082 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2084 // TODO: perhaps instead of appending the default number of cols
2085 // we should remember what the last non-zero number of cols was ?
2089 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2091 // if we have just inserted cols into an empty grid the current
2092 // cell will be undefined...
2094 SetCurrentCell( 0, 0 );
2098 if ( !GetBatchCount() ) Refresh();
2101 SetEditControlValue();
2111 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2113 // TODO: something with updateLabels flag
2117 wxLogError( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2121 if ( m_table
&& m_table
->AppendRows( numRows
) )
2123 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2125 // if we have just inserted cols into an empty grid the current
2126 // cell will be undefined...
2128 SetCurrentCell( 0, 0 );
2131 // the table will have sent the results of the append row
2132 // operation to this view object as a grid table message
2135 if ( !GetBatchCount() ) Refresh();
2145 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2147 // TODO: something with updateLabels flag
2151 wxLogError( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2155 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2157 // the table will have sent the results of the delete row
2158 // operation to this view object as a grid table message
2160 if ( m_numRows
> 0 )
2161 SetEditControlValue();
2163 HideCellEditControl();
2166 if ( !GetBatchCount() ) Refresh();
2176 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2178 // TODO: something with updateLabels flag
2182 wxLogError( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2188 HideCellEditControl();
2189 bool ok
= m_table
->InsertCols( pos
, numCols
);
2191 // the table will have sent the results of the insert col
2192 // operation to this view object as a grid table message
2196 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2198 // if we have just inserted cols into an empty grid the current
2199 // cell will be undefined...
2201 SetCurrentCell( 0, 0 );
2205 if ( !GetBatchCount() ) Refresh();
2208 SetEditControlValue();
2218 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2220 // TODO: something with updateLabels flag
2224 wxLogError( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2228 if ( m_table
&& m_table
->AppendCols( numCols
) )
2230 // the table will have sent the results of the append col
2231 // operation to this view object as a grid table message
2233 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2235 // if we have just inserted cols into an empty grid the current
2236 // cell will be undefined...
2238 SetCurrentCell( 0, 0 );
2242 if ( !GetBatchCount() ) Refresh();
2252 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2254 // TODO: something with updateLabels flag
2258 wxLogError( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2262 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2264 // the table will have sent the results of the delete col
2265 // operation to this view object as a grid table message
2267 if ( m_numCols
> 0 )
2268 SetEditControlValue();
2270 HideCellEditControl();
2273 if ( !GetBatchCount() ) Refresh();
2285 // ----- event handlers
2288 // Generate a grid event based on a mouse event and
2289 // return the result of ProcessEvent()
2291 bool wxGrid::SendEvent( const wxEventType type
,
2293 wxMouseEvent
& mouseEv
)
2295 if ( type
== EVT_GRID_ROW_SIZE
||
2296 type
== EVT_GRID_COL_SIZE
)
2298 int rowOrCol
= (row
== -1 ? col
: row
);
2300 wxGridSizeEvent
gridEvt( GetId(),
2304 mouseEv
.GetX(), mouseEv
.GetY(),
2305 mouseEv
.ControlDown(),
2306 mouseEv
.ShiftDown(),
2308 mouseEv
.MetaDown() );
2310 return GetEventHandler()->ProcessEvent(gridEvt
);
2312 else if ( type
== EVT_GRID_RANGE_SELECT
)
2314 wxGridRangeSelectEvent
gridEvt( GetId(),
2318 m_selectedBottomRight
,
2319 mouseEv
.ControlDown(),
2320 mouseEv
.ShiftDown(),
2322 mouseEv
.MetaDown() );
2324 return GetEventHandler()->ProcessEvent(gridEvt
);
2328 wxGridEvent
gridEvt( GetId(),
2332 mouseEv
.GetX(), mouseEv
.GetY(),
2333 mouseEv
.ControlDown(),
2334 mouseEv
.ShiftDown(),
2336 mouseEv
.MetaDown() );
2338 return GetEventHandler()->ProcessEvent(gridEvt
);
2343 // Generate a grid event of specified type and return the result
2344 // of ProcessEvent().
2346 bool wxGrid::SendEvent( const wxEventType type
,
2349 if ( type
== EVT_GRID_ROW_SIZE
||
2350 type
== EVT_GRID_COL_SIZE
)
2352 int rowOrCol
= (row
== -1 ? col
: row
);
2354 wxGridSizeEvent
gridEvt( GetId(),
2359 return GetEventHandler()->ProcessEvent(gridEvt
);
2363 wxGridEvent
gridEvt( GetId(),
2368 return GetEventHandler()->ProcessEvent(gridEvt
);
2373 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2375 wxPaintDC
dc( this );
2377 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2378 m_numRows
&& m_numCols
)
2380 m_currentCellCoords
.Set(0, 0);
2381 SetEditControlValue();
2382 ShowCellEditControl();
2387 // This is just here to make sure that CalcDimensions gets called when
2388 // the grid view is resized... then the size event is skipped to allow
2389 // the box sizers to handle everything
2391 void wxGrid::OnSize( wxSizeEvent
& event
)
2398 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2400 if ( m_inOnKeyDown
)
2402 // shouldn't be here - we are going round in circles...
2404 wxLogFatalError( wxT("wxGrid::OnKeyDown called while alread active") );
2407 m_inOnKeyDown
= TRUE
;
2409 // propagate the event up and see if it gets processed
2411 wxWindow
*parent
= GetParent();
2412 wxKeyEvent
keyEvt( event
);
2413 keyEvt
.SetEventObject( parent
);
2415 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2417 // try local handlers
2419 switch ( event
.KeyCode() )
2422 if ( event
.ControlDown() )
2424 MoveCursorUpBlock();
2433 if ( event
.ControlDown() )
2435 MoveCursorDownBlock();
2444 if ( event
.ControlDown() )
2446 MoveCursorLeftBlock();
2455 if ( event
.ControlDown() )
2457 MoveCursorRightBlock();
2466 if ( !IsEditable() )
2477 if ( event
.ControlDown() )
2479 event
.Skip(); // to let the edit control have the return
2488 if ( event
.ControlDown() )
2490 MakeCellVisible( 0, 0 );
2491 SetCurrentCell( 0, 0 );
2500 if ( event
.ControlDown() )
2502 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2503 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2520 // now try the cell edit control
2522 if ( IsCellEditControlEnabled() )
2524 event
.SetEventObject( m_cellEditCtrl
);
2525 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2531 m_inOnKeyDown
= FALSE
;
2535 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
2537 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
2539 // the event has been intercepted - do nothing
2543 wxClientDC
dc( m_gridWin
);
2546 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
2548 HideCellEditControl();
2549 SaveEditControlValue();
2552 m_currentCellCoords
= coords
;
2554 SetEditControlValue();
2555 ShowCellEditControl();
2557 if ( IsSelection() )
2559 wxRect
r( SelectionToDeviceRect() );
2561 if ( !GetBatchCount() ) m_gridWin
->Refresh( TRUE
, &r
);
2567 // ------ functions to get/send data (see also public functions)
2570 bool wxGrid::GetModelValues()
2574 // all we need to do is repaint the grid
2576 m_gridWin
->Refresh();
2584 bool wxGrid::SetModelValues()
2590 for ( row
= 0; row
< m_numRows
; row
++ )
2592 for ( col
= 0; col
< m_numCols
; col
++ )
2594 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
2606 // Note - this function only draws cells that are in the list of
2607 // exposed cells (usually set from the update region by
2608 // CalcExposedCells)
2610 void wxGrid::DrawGridCellArea( wxDC
& dc
)
2612 if ( !m_numRows
|| !m_numCols
) return;
2615 size_t numCells
= m_cellsExposed
.GetCount();
2617 for ( i
= 0; i
< numCells
; i
++ )
2619 DrawCell( dc
, m_cellsExposed
[i
] );
2624 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
2626 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2627 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2629 #if !WXGRID_DRAW_LINES
2630 if ( m_gridLinesEnabled
)
2631 DrawCellBorder( dc
, coords
);
2634 DrawCellBackground( dc
, coords
);
2636 // TODO: separate functions here for different kinds of cells ?
2639 DrawCellValue( dc
, coords
);
2643 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
2645 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2646 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2648 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2649 int row
= coords
.GetRow();
2650 int col
= coords
.GetCol();
2652 // right hand border
2654 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
2655 m_colRights
[col
], m_rowBottoms
[row
] );
2659 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
2660 m_colRights
[col
], m_rowBottoms
[row
] );
2664 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
2666 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2667 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2669 int row
= coords
.GetRow();
2670 int col
= coords
.GetCol();
2672 dc
.SetBackgroundMode( wxSOLID
);
2674 if ( IsInSelection( coords
) )
2676 // TODO: improve this
2678 dc
.SetBrush( *wxBLACK_BRUSH
);
2682 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
2685 dc
.SetPen( *wxTRANSPARENT_PEN
);
2687 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
2688 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
2690 m_rowHeights
[row
]-1 );
2694 void wxGrid::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
2696 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2697 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2699 int row
= coords
.GetRow();
2700 int col
= coords
.GetCol();
2702 dc
.SetBackgroundMode( wxTRANSPARENT
);
2704 if ( IsInSelection( row
, col
) )
2706 // TODO: improve this
2708 dc
.SetTextBackground( wxColour(0, 0, 0) );
2709 dc
.SetTextForeground( wxColour(255, 255, 255) );
2713 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
2714 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
2716 dc
.SetFont( GetCellFont(row
, col
) );
2719 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
2722 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2723 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2724 rect
.SetWidth( m_colWidths
[col
] - 4 );
2725 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2727 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
2732 // TODO: remove this ???
2733 // This is used to redraw all grid lines e.g. when the grid line colour
2736 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
2738 if ( !m_gridLinesEnabled
||
2740 !m_numCols
) return;
2742 int top
, bottom
, left
, right
;
2746 m_gridWin
->GetClientSize(&cw
, &ch
);
2748 // virtual coords of visible area
2750 CalcUnscrolledPosition( 0, 0, &left
, &top
);
2751 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
2755 reg
.GetBox(x
, y
, w
, h
);
2756 CalcUnscrolledPosition( x
, y
, &left
, &top
);
2757 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
2760 // avoid drawing grid lines past the last row and col
2762 right
= wxMin( right
, m_colRights
[m_numCols
-1] );
2763 bottom
= wxMin( bottom
, m_rowBottoms
[m_numRows
-1] );
2765 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2767 // horizontal grid lines
2770 for ( i
= 0; i
< m_numRows
; i
++ )
2772 if ( m_rowBottoms
[i
] > bottom
)
2776 else if ( m_rowBottoms
[i
] >= top
)
2778 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
2783 // vertical grid lines
2785 for ( i
= 0; i
< m_numCols
; i
++ )
2787 if ( m_colRights
[i
] > right
)
2791 else if ( m_colRights
[i
] >= left
)
2793 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
2799 void wxGrid::DrawRowLabels( wxDC
& dc
)
2801 if ( !m_numRows
|| !m_numCols
) return;
2804 size_t numLabels
= m_rowLabelsExposed
.GetCount();
2806 for ( i
= 0; i
< numLabels
; i
++ )
2808 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
2813 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
2815 if ( m_rowHeights
[row
] <= 0 ) return;
2817 // draw the label's horizontal border (the vertical border is
2818 // provided by the cell area window margin)
2820 dc
.SetPen( *wxBLACK_PEN
);
2822 dc
.DrawLine( 0, m_rowBottoms
[row
]+1,
2823 m_rowLabelWidth
, m_rowBottoms
[row
]+1 );
2825 dc
.SetPen( *wxWHITE_PEN
);
2827 dc
.DrawLine( 0, m_rowBottoms
[row
]+2,
2828 m_rowLabelWidth
, m_rowBottoms
[row
]+2 );
2830 dc
.SetBackgroundMode( wxTRANSPARENT
);
2831 dc
.SetTextForeground( GetLabelTextColour() );
2832 dc
.SetFont( GetLabelFont() );
2835 GetRowLabelAlignment( &hAlign
, &vAlign
);
2839 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2840 rect
.SetWidth( m_rowLabelWidth
- 4 );
2841 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2842 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
2846 void wxGrid::DrawColLabels( wxDC
& dc
)
2848 if ( !m_numRows
|| !m_numCols
) return;
2851 size_t numLabels
= m_colLabelsExposed
.GetCount();
2853 for ( i
= 0; i
< numLabels
; i
++ )
2855 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
2860 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
2862 if ( m_colWidths
[col
] <= 0 ) return;
2864 // draw the label's vertical border (the horizontal border is
2865 // provided by the cell area window margin)
2867 dc
.SetPen( *wxBLACK_PEN
);
2869 dc
.DrawLine( m_colRights
[col
]+1, 0,
2870 m_colRights
[col
]+1, m_colLabelHeight
);
2872 dc
.SetPen( *wxWHITE_PEN
);
2874 dc
.DrawLine( m_colRights
[col
]+2, 0,
2875 m_colRights
[col
]+2, m_colLabelHeight
);
2877 dc
.SetBackgroundMode( wxTRANSPARENT
);
2878 dc
.SetTextForeground( GetLabelTextColour() );
2879 dc
.SetFont( GetLabelFont() );
2882 GetColLabelAlignment( &hAlign
, &vAlign
);
2885 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2887 rect
.SetWidth( m_colWidths
[col
] - 4 );
2888 rect
.SetHeight( m_colLabelHeight
- 4 );
2889 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
2893 void wxGrid::DrawTextRectangle( wxDC
& dc
,
2894 const wxString
& value
,
2899 long textWidth
, textHeight
;
2900 long lineWidth
, lineHeight
;
2901 wxArrayString lines
;
2903 dc
.SetClippingRegion( rect
);
2904 StringToLines( value
, lines
);
2905 if ( lines
.GetCount() )
2907 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
2908 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
2911 switch ( horizAlign
)
2914 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
2918 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
2927 switch ( vertAlign
)
2930 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
2934 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
2943 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
2945 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
2950 dc
.DestroyClippingRegion();
2954 // Split multi line text up into an array of strings. Any existing
2955 // contents of the string array are preserved.
2957 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
2959 // TODO: this won't work for WXMAC ? (lines end with '\r')
2960 // => use wxTextFile functions then (VZ)
2963 while ( startPos
< (int)value
.Length() )
2965 pos
= value
.Mid(startPos
).Find( '\n' );
2970 else if ( pos
== 0 )
2972 lines
.Add( wxEmptyString
);
2976 if ( value
[startPos
+pos
-1] == '\r' )
2978 lines
.Add( value
.Mid(startPos
, pos
-1) );
2982 lines
.Add( value
.Mid(startPos
, pos
) );
2987 if ( startPos
< (int)value
.Length() )
2989 lines
.Add( value
.Mid( startPos
) );
2994 void wxGrid::GetTextBoxSize( wxDC
& dc
,
2995 wxArrayString
& lines
,
2996 long *width
, long *height
)
3003 for ( i
= 0; i
< lines
.GetCount(); i
++ )
3005 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
3006 w
= wxMax( w
, lineW
);
3016 // ------ Edit control functions
3020 void wxGrid::EnableEditing( bool edit
)
3022 // TODO: improve this ?
3024 if ( edit
!= m_editable
)
3028 // TODO: extend this for other edit control types
3030 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3032 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
3038 #if 0 // disabled for the moment - the cell control is always active
3039 void wxGrid::EnableCellEditControl( bool enable
)
3041 if ( m_cellEditCtrl
&&
3042 enable
!= m_cellEditCtrlEnabled
)
3044 m_cellEditCtrlEnabled
= enable
;
3046 if ( m_cellEditCtrlEnabled
)
3048 SetEditControlValue();
3049 ShowCellEditControl();
3053 HideCellEditControl();
3054 SaveEditControlValue();
3061 void wxGrid::ShowCellEditControl()
3065 if ( IsCellEditControlEnabled() )
3067 if ( !IsVisible( m_currentCellCoords
) )
3073 rect
= CellToRect( m_currentCellCoords
);
3075 // convert to scrolled coords
3077 int left
, top
, right
, bottom
;
3078 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3079 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3082 m_gridWin
->GetClientSize( &cw
, &ch
);
3084 // Make the edit control large enough to allow for internal margins
3085 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3088 #if defined(__WXMOTIF__)
3089 if ( m_currentCellCoords
.GetRow() == 0 ||
3090 m_currentCellCoords
.GetCol() == 0 )
3099 if ( m_currentCellCoords
.GetRow() == 0 ||
3100 m_currentCellCoords
.GetCol() == 0 )
3110 #if defined(__WXGTK__)
3113 if (left
!= 0) left_diff
++;
3114 if (top
!= 0) top_diff
++;
3115 rect
.SetLeft( left
+ left_diff
);
3116 rect
.SetTop( top
+ top_diff
);
3117 rect
.SetRight( rect
.GetRight() - left_diff
);
3118 rect
.SetBottom( rect
.GetBottom() - top_diff
);
3120 rect
.SetLeft( wxMax(0, left
- extra
) );
3121 rect
.SetTop( wxMax(0, top
- extra
) );
3122 rect
.SetRight( rect
.GetRight() + 2*extra
);
3123 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3126 m_cellEditCtrl
->SetSize( rect
);
3127 m_cellEditCtrl
->Show( TRUE
);
3129 switch ( m_editCtrlType
)
3131 case wxGRID_TEXTCTRL
:
3132 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3135 case wxGRID_CHECKBOX
:
3136 // TODO: anything ???
3141 // TODO: anything ???
3145 case wxGRID_COMBOBOX
:
3146 // TODO: anything ???
3151 m_cellEditCtrl
->SetFocus();
3157 void wxGrid::HideCellEditControl()
3159 if ( IsCellEditControlEnabled() )
3161 m_cellEditCtrl
->Show( FALSE
);
3166 void wxGrid::SetEditControlValue( const wxString
& value
)
3172 s
= GetCellValue(m_currentCellCoords
);
3176 if ( IsCellEditControlEnabled() )
3178 switch ( m_editCtrlType
)
3180 case wxGRID_TEXTCTRL
:
3181 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3184 case wxGRID_CHECKBOX
:
3185 // TODO: implement this
3190 // TODO: implement this
3194 case wxGRID_COMBOBOX
:
3195 // TODO: implement this
3204 void wxGrid::SaveEditControlValue()
3208 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3210 if ( IsCellEditControlEnabled() )
3212 ctrl
= m_cellEditCtrl
;
3219 bool valueChanged
= FALSE
;
3221 switch ( m_editCtrlType
)
3223 case wxGRID_TEXTCTRL
:
3224 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3225 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3226 SetCellValue( m_currentCellCoords
,
3227 ((wxTextCtrl
*) ctrl
)->GetValue() );
3230 case wxGRID_CHECKBOX
:
3231 // TODO: implement this
3236 // TODO: implement this
3240 case wxGRID_COMBOBOX
:
3241 // TODO: implement this
3248 SendEvent( EVT_GRID_CELL_CHANGE
,
3249 m_currentCellCoords
.GetRow(),
3250 m_currentCellCoords
.GetCol() );
3257 // ------ Grid location functions
3258 // Note that all of these functions work with the logical coordinates of
3259 // grid cells and labels so you will need to convert from device
3260 // coordinates for mouse events etc.
3263 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3265 int row
= YToRow(y
);
3266 int col
= XToCol(x
);
3268 if ( row
== -1 || col
== -1 )
3270 coords
= wxGridNoCellCoords
;
3274 coords
.Set( row
, col
);
3279 int wxGrid::YToRow( int y
)
3283 for ( i
= 0; i
< m_numRows
; i
++ )
3285 if ( y
< m_rowBottoms
[i
] ) return i
;
3292 int wxGrid::XToCol( int x
)
3296 for ( i
= 0; i
< m_numCols
; i
++ )
3298 if ( x
< m_colRights
[i
] ) return i
;
3305 // return the row number that that the y coord is near the edge of, or
3306 // -1 if not near an edge
3308 int wxGrid::YToEdgeOfRow( int y
)
3312 for ( i
= 0; i
< m_numRows
; i
++ )
3314 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3316 d
= abs( y
- m_rowBottoms
[i
] );
3318 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3327 // return the col number that that the x coord is near the edge of, or
3328 // -1 if not near an edge
3330 int wxGrid::XToEdgeOfCol( int x
)
3334 for ( i
= 0; i
< m_numCols
; i
++ )
3336 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3338 d
= abs( x
- m_colRights
[i
] );
3340 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3349 wxRect
wxGrid::CellToRect( int row
, int col
)
3351 wxRect
rect( -1, -1, -1, -1 );
3353 if ( row
>= 0 && row
< m_numRows
&&
3354 col
>= 0 && col
< m_numCols
)
3356 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3357 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3358 rect
.width
= m_colWidths
[col
];
3359 rect
.height
= m_rowHeights
[ row
];
3366 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3368 // get the cell rectangle in logical coords
3370 wxRect
r( CellToRect( row
, col
) );
3372 // convert to device coords
3374 int left
, top
, right
, bottom
;
3375 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3376 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3378 // check against the client area of the grid window
3381 m_gridWin
->GetClientSize( &cw
, &ch
);
3383 if ( wholeCellVisible
)
3385 // is the cell wholly visible ?
3387 return ( left
>= 0 && right
<= cw
&&
3388 top
>= 0 && bottom
<= ch
);
3392 // is the cell partly visible ?
3394 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3395 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3400 // make the specified cell location visible by doing a minimal amount
3403 void wxGrid::MakeCellVisible( int row
, int col
)
3406 int xpos
= -1, ypos
= -1;
3408 if ( row
>= 0 && row
< m_numRows
&&
3409 col
>= 0 && col
< m_numCols
)
3411 // get the cell rectangle in logical coords
3413 wxRect
r( CellToRect( row
, col
) );
3415 // convert to device coords
3417 int left
, top
, right
, bottom
;
3418 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3419 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3422 m_gridWin
->GetClientSize( &cw
, &ch
);
3428 else if ( bottom
> ch
)
3430 int h
= r
.GetHeight();
3432 for ( i
= row
-1; i
>= 0; i
-- )
3434 if ( h
+ m_rowHeights
[i
] > ch
) break;
3436 h
+= m_rowHeights
[i
];
3437 ypos
-= m_rowHeights
[i
];
3440 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
3441 // have rounding errors (this is important, because if we do, we
3442 // might not scroll at all and some cells won't be redrawn)
3443 ypos
+= GRID_SCROLL_LINE
/ 2;
3450 else if ( right
> cw
)
3452 int w
= r
.GetWidth();
3454 for ( i
= col
-1; i
>= 0; i
-- )
3456 if ( w
+ m_colWidths
[i
] > cw
) break;
3458 w
+= m_colWidths
[i
];
3459 xpos
-= m_colWidths
[i
];
3462 // see comment for ypos above
3463 xpos
+= GRID_SCROLL_LINE
/ 2;
3466 if ( xpos
!= -1 || ypos
!= -1 )
3468 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
3469 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
3470 Scroll( xpos
, ypos
);
3478 // ------ Grid cursor movement functions
3481 bool wxGrid::MoveCursorUp()
3483 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3484 m_currentCellCoords
.GetRow() > 0 )
3486 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3487 m_currentCellCoords
.GetCol() );
3489 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3490 m_currentCellCoords
.GetCol() );
3499 bool wxGrid::MoveCursorDown()
3501 // TODO: allow for scrolling
3503 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3504 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3506 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3507 m_currentCellCoords
.GetCol() );
3509 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3510 m_currentCellCoords
.GetCol() );
3519 bool wxGrid::MoveCursorLeft()
3521 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3522 m_currentCellCoords
.GetCol() > 0 )
3524 MakeCellVisible( m_currentCellCoords
.GetRow(),
3525 m_currentCellCoords
.GetCol() - 1 );
3527 SetCurrentCell( m_currentCellCoords
.GetRow(),
3528 m_currentCellCoords
.GetCol() - 1 );
3537 bool wxGrid::MoveCursorRight()
3539 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3540 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
3542 MakeCellVisible( m_currentCellCoords
.GetRow(),
3543 m_currentCellCoords
.GetCol() + 1 );
3545 SetCurrentCell( m_currentCellCoords
.GetRow(),
3546 m_currentCellCoords
.GetCol() + 1 );
3555 bool wxGrid::MovePageUp()
3557 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3559 int row
= m_currentCellCoords
.GetRow();
3563 m_gridWin
->GetClientSize( &cw
, &ch
);
3565 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3566 int newRow
= YToRow( y
- ch
+ 1 );
3571 else if ( newRow
== row
)
3576 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3577 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3585 bool wxGrid::MovePageDown()
3587 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3589 int row
= m_currentCellCoords
.GetRow();
3590 if ( row
< m_numRows
)
3593 m_gridWin
->GetClientSize( &cw
, &ch
);
3595 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3596 int newRow
= YToRow( y
+ ch
);
3599 newRow
= m_numRows
- 1;
3601 else if ( newRow
== row
)
3606 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3607 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3615 bool wxGrid::MoveCursorUpBlock()
3618 m_currentCellCoords
!= wxGridNoCellCoords
&&
3619 m_currentCellCoords
.GetRow() > 0 )
3621 int row
= m_currentCellCoords
.GetRow();
3622 int col
= m_currentCellCoords
.GetCol();
3624 if ( m_table
->IsEmptyCell(row
, col
) )
3626 // starting in an empty cell: find the next block of
3632 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3635 else if ( m_table
->IsEmptyCell(row
-1, col
) )
3637 // starting at the top of a block: find the next block
3643 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3648 // starting within a block: find the top of the block
3653 if ( m_table
->IsEmptyCell(row
, col
) )
3661 MakeCellVisible( row
, col
);
3662 SetCurrentCell( row
, col
);
3670 bool wxGrid::MoveCursorDownBlock()
3673 m_currentCellCoords
!= wxGridNoCellCoords
&&
3674 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3676 int row
= m_currentCellCoords
.GetRow();
3677 int col
= m_currentCellCoords
.GetCol();
3679 if ( m_table
->IsEmptyCell(row
, col
) )
3681 // starting in an empty cell: find the next block of
3684 while ( row
< m_numRows
-1 )
3687 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3690 else if ( m_table
->IsEmptyCell(row
+1, col
) )
3692 // starting at the bottom of a block: find the next block
3695 while ( row
< m_numRows
-1 )
3698 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3703 // starting within a block: find the bottom of the block
3705 while ( row
< m_numRows
-1 )
3708 if ( m_table
->IsEmptyCell(row
, col
) )
3716 MakeCellVisible( row
, col
);
3717 SetCurrentCell( row
, col
);
3725 bool wxGrid::MoveCursorLeftBlock()
3728 m_currentCellCoords
!= wxGridNoCellCoords
&&
3729 m_currentCellCoords
.GetCol() > 0 )
3731 int row
= m_currentCellCoords
.GetRow();
3732 int col
= m_currentCellCoords
.GetCol();
3734 if ( m_table
->IsEmptyCell(row
, col
) )
3736 // starting in an empty cell: find the next block of
3742 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3745 else if ( m_table
->IsEmptyCell(row
, col
-1) )
3747 // starting at the left of a block: find the next block
3753 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3758 // starting within a block: find the left of the block
3763 if ( m_table
->IsEmptyCell(row
, col
) )
3771 MakeCellVisible( row
, col
);
3772 SetCurrentCell( row
, col
);
3780 bool wxGrid::MoveCursorRightBlock()
3783 m_currentCellCoords
!= wxGridNoCellCoords
&&
3784 m_currentCellCoords
.GetCol() < m_numCols
-1 )
3786 int row
= m_currentCellCoords
.GetRow();
3787 int col
= m_currentCellCoords
.GetCol();
3789 if ( m_table
->IsEmptyCell(row
, col
) )
3791 // starting in an empty cell: find the next block of
3794 while ( col
< m_numCols
-1 )
3797 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3800 else if ( m_table
->IsEmptyCell(row
, col
+1) )
3802 // starting at the right of a block: find the next block
3805 while ( col
< m_numCols
-1 )
3808 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3813 // starting within a block: find the right of the block
3815 while ( col
< m_numCols
-1 )
3818 if ( m_table
->IsEmptyCell(row
, col
) )
3826 MakeCellVisible( row
, col
);
3827 SetCurrentCell( row
, col
);
3838 // ------ Label values and formatting
3841 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
3843 *horiz
= m_rowLabelHorizAlign
;
3844 *vert
= m_rowLabelVertAlign
;
3847 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
3849 *horiz
= m_colLabelHorizAlign
;
3850 *vert
= m_colLabelVertAlign
;
3853 wxString
wxGrid::GetRowLabelValue( int row
)
3857 return m_table
->GetRowLabelValue( row
);
3867 wxString
wxGrid::GetColLabelValue( int col
)
3871 return m_table
->GetColLabelValue( col
);
3881 void wxGrid::SetRowLabelSize( int width
)
3883 // TODO: how to do this with the box sizers ?
3886 void wxGrid::SetColLabelSize( int height
)
3888 // TODO: how to do this with the box sizers ?
3891 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
3893 if ( m_labelBackgroundColour
!= colour
)
3895 m_labelBackgroundColour
= colour
;
3896 m_rowLabelWin
->SetBackgroundColour( colour
);
3897 m_colLabelWin
->SetBackgroundColour( colour
);
3898 m_cornerLabelWin
->SetBackgroundColour( colour
);
3900 if ( !GetBatchCount() )
3902 m_rowLabelWin
->Refresh();
3903 m_colLabelWin
->Refresh();
3904 m_cornerLabelWin
->Refresh();
3909 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
3911 if ( m_labelTextColour
!= colour
)
3913 m_labelTextColour
= colour
;
3914 if ( !GetBatchCount() )
3916 m_rowLabelWin
->Refresh();
3917 m_colLabelWin
->Refresh();
3922 void wxGrid::SetLabelFont( const wxFont
& font
)
3925 if ( !GetBatchCount() )
3927 m_rowLabelWin
->Refresh();
3928 m_colLabelWin
->Refresh();
3932 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
3934 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3936 m_rowLabelHorizAlign
= horiz
;
3939 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3941 m_rowLabelVertAlign
= vert
;
3944 if ( !GetBatchCount() )
3946 m_rowLabelWin
->Refresh();
3950 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
3952 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3954 m_colLabelHorizAlign
= horiz
;
3957 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3959 m_colLabelVertAlign
= vert
;
3962 if ( !GetBatchCount() )
3964 m_colLabelWin
->Refresh();
3968 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
3972 m_table
->SetRowLabelValue( row
, s
);
3973 if ( !GetBatchCount() )
3975 wxRect rect
= CellToRect( row
, 0);
3976 if ( rect
.height
> 0 )
3978 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
3980 rect
.width
= m_rowLabelWidth
;
3981 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3987 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
3991 m_table
->SetColLabelValue( col
, s
);
3992 if ( !GetBatchCount() )
3994 wxRect rect
= CellToRect( 0, col
);
3995 if ( rect
.width
> 0 )
3997 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
3999 rect
.height
= m_colLabelHeight
;
4000 m_colLabelWin
->Refresh( TRUE
, &rect
);
4006 void wxGrid::SetGridLineColour( const wxColour
& colour
)
4008 if ( m_gridLineColour
!= colour
)
4010 m_gridLineColour
= colour
;
4012 wxClientDC
dc( m_gridWin
);
4014 DrawAllGridLines( dc
);
4018 void wxGrid::EnableGridLines( bool enable
)
4020 if ( enable
!= m_gridLinesEnabled
)
4022 m_gridLinesEnabled
= enable
;
4024 if ( !GetBatchCount() )
4028 wxClientDC
dc( m_gridWin
);
4030 DrawAllGridLines( dc
);
4034 m_gridWin
->Refresh();
4041 int wxGrid::GetDefaultRowSize()
4043 return m_defaultRowHeight
;
4046 int wxGrid::GetRowSize( int row
)
4048 if ( row
>= 0 && row
< m_numRows
)
4049 return m_rowHeights
[row
];
4051 return 0; // TODO: log an error here
4054 int wxGrid::GetDefaultColSize()
4056 return m_defaultColWidth
;
4059 int wxGrid::GetColSize( int col
)
4061 if ( col
>= 0 && col
< m_numCols
)
4062 return m_colWidths
[col
];
4064 return 0; // TODO: log an error here
4067 wxColour
wxGrid::GetDefaultCellBackgroundColour()
4069 // TODO: replace this temp test code
4071 return wxColour( 255, 255, 255 );
4074 wxColour
wxGrid::GetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4076 // TODO: replace this temp test code
4078 return wxColour( 255, 255, 255 );
4081 wxColour
wxGrid::GetDefaultCellTextColour()
4083 // TODO: replace this temp test code
4085 return wxColour( 0, 0, 0 );
4088 wxColour
wxGrid::GetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4090 // TODO: replace this temp test code
4092 return wxColour( 0, 0, 0 );
4096 wxFont
wxGrid::GetDefaultCellFont()
4098 return m_defaultCellFont
;
4101 wxFont
wxGrid::GetCellFont( int WXUNUSED(row
), int WXUNUSED(col
) )
4103 // TODO: replace this temp test code
4105 return m_defaultCellFont
;
4108 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
4110 // TODO: replace this temp test code
4116 void wxGrid::GetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int *horiz
, int *vert
)
4118 // TODO: replace this temp test code
4124 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
4126 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
4128 if ( resizeExistingRows
)
4132 for ( row
= 0; row
< m_numRows
; row
++ )
4134 m_rowHeights
[row
] = m_defaultRowHeight
;
4135 bottom
+= m_defaultRowHeight
;
4136 m_rowBottoms
[row
] = bottom
;
4142 void wxGrid::SetRowSize( int row
, int height
)
4146 if ( row
>= 0 && row
< m_numRows
)
4148 int h
= wxMax( 0, height
);
4149 int diff
= h
- m_rowHeights
[row
];
4151 m_rowHeights
[row
] = h
;
4152 for ( i
= row
; i
< m_numRows
; i
++ )
4154 m_rowBottoms
[i
] += diff
;
4158 // Note: we are ending the event *after* doing
4159 // default processing in this case
4161 SendEvent( EVT_GRID_ROW_SIZE
,
4166 // TODO: log an error here
4170 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4172 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4174 if ( resizeExistingCols
)
4178 for ( col
= 0; col
< m_numCols
; col
++ )
4180 m_colWidths
[col
] = m_defaultColWidth
;
4181 right
+= m_defaultColWidth
;
4182 m_colRights
[col
] = right
;
4188 void wxGrid::SetColSize( int col
, int width
)
4192 if ( col
>= 0 && col
< m_numCols
)
4194 int w
= wxMax( 0, width
);
4195 int diff
= w
- m_colWidths
[col
];
4196 m_colWidths
[col
] = w
;
4198 for ( i
= col
; i
< m_numCols
; i
++ )
4200 m_colRights
[i
] += diff
;
4204 // Note: we are ending the event *after* doing
4205 // default processing in this case
4207 SendEvent( EVT_GRID_COL_SIZE
,
4212 // TODO: log an error here
4216 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& )
4218 // TODO: everything !!!
4222 void wxGrid::SetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4224 // TODO: everything !!!
4228 void wxGrid::SetDefaultCellTextColour( const wxColour
& )
4230 // TODO: everything !!!
4234 void wxGrid::SetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4236 // TODO: everything !!!
4240 void wxGrid::SetDefaultCellFont( const wxFont
& )
4242 // TODO: everything !!!
4246 void wxGrid::SetCellFont( int WXUNUSED(row
), int WXUNUSED(col
), const wxFont
& )
4248 // TODO: everything !!!
4252 void wxGrid::SetDefaultCellAlignment( int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4254 // TODO: everything !!!
4258 void wxGrid::SetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4260 // TODO: everything !!!
4267 // ------ cell value accessor functions
4270 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4274 m_table
->SetValue( row
, col
, s
.c_str() );
4275 if ( !GetBatchCount() )
4277 wxClientDC
dc( m_gridWin
);
4279 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4282 #if 0 // TODO: edit in place
4284 if ( m_currentCellCoords
.GetRow() == row
&&
4285 m_currentCellCoords
.GetCol() == col
)
4287 SetEditControlValue( s
);
4296 // ------ Block, row and col selection
4299 void wxGrid::SelectRow( int row
, bool addToSelected
)
4303 if ( IsSelection() && addToSelected
)
4306 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4309 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4310 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4311 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4312 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4316 need_refresh
[0] = TRUE
;
4317 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
4318 wxGridCellCoords ( oldTop
- 1,
4320 m_selectedTopLeft
.SetRow( row
);
4325 need_refresh
[1] = TRUE
;
4326 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
4327 wxGridCellCoords ( oldBottom
,
4330 m_selectedTopLeft
.SetCol( 0 );
4333 if ( oldBottom
< row
)
4335 need_refresh
[2] = TRUE
;
4336 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
4337 wxGridCellCoords ( row
,
4339 m_selectedBottomRight
.SetRow( row
);
4342 if ( oldRight
< m_numCols
- 1 )
4344 need_refresh
[3] = TRUE
;
4345 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4347 wxGridCellCoords ( oldBottom
,
4349 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4352 for (i
= 0; i
< 4; i
++ )
4353 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4354 m_gridWin
->Refresh( TRUE
, &(rect
[i
]) );
4358 r
= SelectionToDeviceRect();
4360 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4362 m_selectedTopLeft
.Set( row
, 0 );
4363 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4364 r
= SelectionToDeviceRect();
4365 m_gridWin
->Refresh( TRUE
, &r
);
4368 wxGridRangeSelectEvent
gridEvt( GetId(),
4369 EVT_GRID_RANGE_SELECT
,
4372 m_selectedBottomRight
);
4374 GetEventHandler()->ProcessEvent(gridEvt
);
4378 void wxGrid::SelectCol( int col
, bool addToSelected
)
4380 if ( IsSelection() && addToSelected
)
4383 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4386 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4387 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4388 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4389 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4391 if ( oldLeft
> col
)
4393 need_refresh
[0] = TRUE
;
4394 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
4395 wxGridCellCoords ( m_numRows
- 1,
4397 m_selectedTopLeft
.SetCol( col
);
4402 need_refresh
[1] = TRUE
;
4403 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
4404 wxGridCellCoords ( oldTop
- 1,
4406 m_selectedTopLeft
.SetRow( 0 );
4409 if ( oldRight
< col
)
4411 need_refresh
[2] = TRUE
;
4412 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
4413 wxGridCellCoords ( m_numRows
- 1,
4415 m_selectedBottomRight
.SetCol( col
);
4418 if ( oldBottom
< m_numRows
- 1 )
4420 need_refresh
[3] = TRUE
;
4421 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
4423 wxGridCellCoords ( m_numRows
- 1,
4425 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4428 for (i
= 0; i
< 4; i
++ )
4429 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4430 m_gridWin
->Refresh( TRUE
, &(rect
[i
]) );
4436 r
= SelectionToDeviceRect();
4438 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4440 m_selectedTopLeft
.Set( 0, col
);
4441 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4442 r
= SelectionToDeviceRect();
4443 m_gridWin
->Refresh( TRUE
, &r
);
4446 wxGridRangeSelectEvent
gridEvt( GetId(),
4447 EVT_GRID_RANGE_SELECT
,
4450 m_selectedBottomRight
);
4452 GetEventHandler()->ProcessEvent(gridEvt
);
4456 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4459 wxGridCellCoords updateTopLeft
, updateBottomRight
;
4461 if ( topRow
> bottomRow
)
4468 if ( leftCol
> rightCol
)
4475 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
4476 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
4478 if ( m_selectedTopLeft
!= updateTopLeft
||
4479 m_selectedBottomRight
!= updateBottomRight
)
4481 // Compute two optimal update rectangles:
4482 // Either one rectangle is a real subset of the
4483 // other, or they are (almost) disjoint!
4485 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4488 // Store intermediate values
4489 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4490 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4491 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4492 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4494 // Determine the outer/inner coordinates.
4495 if (oldLeft
> leftCol
)
4501 if (oldTop
> topRow
)
4507 if (oldRight
< rightCol
)
4510 oldRight
= rightCol
;
4513 if (oldBottom
< bottomRow
)
4516 oldBottom
= bottomRow
;
4520 // Now, either the stuff marked old is the outer
4521 // rectangle or we don't have a situation where one
4522 // is contained in the other.
4524 if ( oldLeft
< leftCol
)
4526 need_refresh
[0] = TRUE
;
4527 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4529 wxGridCellCoords ( oldBottom
,
4533 if ( oldTop
< topRow
)
4535 need_refresh
[1] = TRUE
;
4536 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4538 wxGridCellCoords ( topRow
- 1,
4542 if ( oldRight
> rightCol
)
4544 need_refresh
[2] = TRUE
;
4545 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4547 wxGridCellCoords ( oldBottom
,
4551 if ( oldBottom
> bottomRow
)
4553 need_refresh
[3] = TRUE
;
4554 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
4556 wxGridCellCoords ( oldBottom
,
4562 m_selectedTopLeft
= updateTopLeft
;
4563 m_selectedBottomRight
= updateBottomRight
;
4565 // various Refresh() calls
4566 for (i
= 0; i
< 4; i
++ )
4567 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4568 m_gridWin
->Refresh( TRUE
, &(rect
[i
]) );
4571 // only generate an event if the block is not being selected by
4572 // dragging the mouse (in which case the event will be generated in
4573 // the mouse event handler)
4574 if ( !m_isDragging
)
4576 wxGridRangeSelectEvent
gridEvt( GetId(),
4577 EVT_GRID_RANGE_SELECT
,
4580 m_selectedBottomRight
);
4582 GetEventHandler()->ProcessEvent(gridEvt
);
4586 void wxGrid::SelectAll()
4588 m_selectedTopLeft
.Set( 0, 0 );
4589 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
4591 m_gridWin
->Refresh();
4595 void wxGrid::ClearSelection()
4597 m_selectedTopLeft
= wxGridNoCellCoords
;
4598 m_selectedBottomRight
= wxGridNoCellCoords
;
4602 // This function returns the rectangle that encloses the given block
4603 // in device coords clipped to the client size of the grid window.
4605 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
4606 const wxGridCellCoords
&bottomRight
)
4608 wxRect
rect( wxGridNoCellRect
);
4611 cellRect
= CellToRect( topLeft
);
4612 if ( cellRect
!= wxGridNoCellRect
)
4618 rect
= wxRect( 0, 0, 0, 0 );
4621 cellRect
= CellToRect( bottomRight
);
4622 if ( cellRect
!= wxGridNoCellRect
)
4628 return wxGridNoCellRect
;
4631 // convert to scrolled coords
4633 int left
, top
, right
, bottom
;
4634 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4635 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4638 m_gridWin
->GetClientSize( &cw
, &ch
);
4640 rect
.SetLeft( wxMax(0, left
) );
4641 rect
.SetTop( wxMax(0, top
) );
4642 rect
.SetRight( wxMin(cw
, right
) );
4643 rect
.SetBottom( wxMin(ch
, bottom
) );
4651 // ------ Grid event classes
4654 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
4656 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
4657 int row
, int col
, int x
, int y
,
4658 bool control
, bool shift
, bool alt
, bool meta
)
4659 : wxNotifyEvent( type
, id
)
4665 m_control
= control
;
4670 SetEventObject(obj
);
4674 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
4676 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
4677 int rowOrCol
, int x
, int y
,
4678 bool control
, bool shift
, bool alt
, bool meta
)
4679 : wxNotifyEvent( type
, id
)
4681 m_rowOrCol
= rowOrCol
;
4684 m_control
= control
;
4689 SetEventObject(obj
);
4693 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
4695 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
4696 const wxGridCellCoords
& topLeft
,
4697 const wxGridCellCoords
& bottomRight
,
4698 bool control
, bool shift
, bool alt
, bool meta
)
4699 : wxNotifyEvent( type
, id
)
4701 m_topLeft
= topLeft
;
4702 m_bottomRight
= bottomRight
;
4703 m_control
= control
;
4708 SetEventObject(obj
);
4712 #endif // ifndef wxUSE_NEW_GRID