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"
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
);
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 rect
.width
= m_rowLabelWidth
;
1599 rect
.height
= ch
- rect
.y
;
1600 m_rowLabelWin
->Refresh( TRUE
, &rect
);
1602 m_gridWin
->Refresh( TRUE
, &rect
);
1605 ShowCellEditControl();
1607 // Note: we are ending the event *after* doing
1608 // default processing in this case
1610 SendEvent( EVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
1618 // ------------ Right button down
1620 else if ( event
.RightDown() )
1623 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
1625 // no default action at the moment
1630 // ------------ Right double click
1632 else if ( event
.RightDClick() )
1635 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
1637 // no default action at the moment
1642 // ------------ No buttons down and mouse moving
1644 else if ( event
.Moving() )
1646 m_dragRowOrCol
= YToEdgeOfRow( y
);
1647 if ( m_dragRowOrCol
>= 0 )
1649 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1651 m_cursorMode
= WXGRID_CURSOR_RESIZE_ROW
;
1652 m_rowLabelWin
->SetCursor( m_rowResizeCursor
);
1657 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1659 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1660 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 rect
.width
= cw
- rect
.x
;
1786 rect
.height
= m_colLabelHeight
;
1787 m_colLabelWin
->Refresh( TRUE
, &rect
);
1789 m_gridWin
->Refresh( TRUE
, &rect
);
1792 ShowCellEditControl();
1794 // Note: we are ending the event *after* doing
1795 // default processing in this case
1797 SendEvent( EVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
1805 // ------------ Right button down
1807 else if ( event
.RightDown() )
1810 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
1812 // no default action at the moment
1817 // ------------ Right double click
1819 else if ( event
.RightDClick() )
1822 if ( !SendEvent( EVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
1824 // no default action at the moment
1829 // ------------ No buttons down and mouse moving
1831 else if ( event
.Moving() )
1833 m_dragRowOrCol
= XToEdgeOfCol( x
);
1834 if ( m_dragRowOrCol
>= 0 )
1836 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
1838 m_cursorMode
= WXGRID_CURSOR_RESIZE_COL
;
1839 m_colLabelWin
->SetCursor( m_colResizeCursor
);
1844 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
1846 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
1847 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
2011 // ------ interaction with data model
2013 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
2015 switch ( msg
.GetId() )
2017 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
2018 return GetModelValues();
2020 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
2021 return SetModelValues();
2023 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2024 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2025 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2026 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2027 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2028 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2029 return Redimension( msg
);
2038 // The behaviour of this function depends on the grid table class
2039 // Clear() function. For the default wxGridStringTable class the
2040 // behavious is to replace all cell contents with wxEmptyString but
2041 // not to change the number of rows or cols.
2043 void wxGrid::ClearGrid()
2048 SetEditControlValue();
2049 if ( !GetBatchCount() ) m_gridWin
->Refresh();
2054 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2056 // TODO: something with updateLabels flag
2060 wxLogError( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
2066 bool ok
= m_table
->InsertRows( pos
, numRows
);
2068 // the table will have sent the results of the insert row
2069 // operation to this view object as a grid table message
2073 if ( m_numCols
== 0 )
2075 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
2077 // TODO: perhaps instead of appending the default number of cols
2078 // we should remember what the last non-zero number of cols was ?
2082 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2084 // if we have just inserted cols into an empty grid the current
2085 // cell will be undefined...
2087 SetCurrentCell( 0, 0 );
2091 if ( !GetBatchCount() ) Refresh();
2094 SetEditControlValue();
2104 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
2106 // TODO: something with updateLabels flag
2110 wxLogError( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
2114 if ( m_table
&& m_table
->AppendRows( numRows
) )
2116 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2118 // if we have just inserted cols into an empty grid the current
2119 // cell will be undefined...
2121 SetCurrentCell( 0, 0 );
2124 // the table will have sent the results of the append row
2125 // operation to this view object as a grid table message
2128 if ( !GetBatchCount() ) Refresh();
2138 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
2140 // TODO: something with updateLabels flag
2144 wxLogError( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
2148 if ( m_table
&& m_table
->DeleteRows( pos
, numRows
) )
2150 // the table will have sent the results of the delete row
2151 // operation to this view object as a grid table message
2153 if ( m_numRows
> 0 )
2154 SetEditControlValue();
2156 HideCellEditControl();
2159 if ( !GetBatchCount() ) Refresh();
2169 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2171 // TODO: something with updateLabels flag
2175 wxLogError( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
2181 HideCellEditControl();
2182 bool ok
= m_table
->InsertCols( pos
, numCols
);
2184 // the table will have sent the results of the insert col
2185 // operation to this view object as a grid table message
2189 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2191 // if we have just inserted cols into an empty grid the current
2192 // cell will be undefined...
2194 SetCurrentCell( 0, 0 );
2198 if ( !GetBatchCount() ) Refresh();
2201 SetEditControlValue();
2211 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
2213 // TODO: something with updateLabels flag
2217 wxLogError( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
2221 if ( m_table
&& m_table
->AppendCols( numCols
) )
2223 // the table will have sent the results of the append col
2224 // operation to this view object as a grid table message
2226 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2228 // if we have just inserted cols into an empty grid the current
2229 // cell will be undefined...
2231 SetCurrentCell( 0, 0 );
2235 if ( !GetBatchCount() ) Refresh();
2245 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
2247 // TODO: something with updateLabels flag
2251 wxLogError( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
2255 if ( m_table
&& m_table
->DeleteCols( pos
, numCols
) )
2257 // the table will have sent the results of the delete col
2258 // operation to this view object as a grid table message
2260 if ( m_numCols
> 0 )
2261 SetEditControlValue();
2263 HideCellEditControl();
2266 if ( !GetBatchCount() ) Refresh();
2278 // ----- event handlers
2281 // Generate a grid event based on a mouse event and
2282 // return the result of ProcessEvent()
2284 bool wxGrid::SendEvent( const wxEventType type
,
2286 wxMouseEvent
& mouseEv
)
2288 if ( type
== EVT_GRID_ROW_SIZE
||
2289 type
== EVT_GRID_COL_SIZE
)
2291 int rowOrCol
= (row
== -1 ? col
: row
);
2293 wxGridSizeEvent
gridEvt( GetId(),
2297 mouseEv
.GetX(), mouseEv
.GetY(),
2298 mouseEv
.ControlDown(),
2299 mouseEv
.ShiftDown(),
2301 mouseEv
.MetaDown() );
2303 return GetEventHandler()->ProcessEvent(gridEvt
);
2305 else if ( type
== EVT_GRID_RANGE_SELECT
)
2307 wxGridRangeSelectEvent
gridEvt( GetId(),
2311 m_selectedBottomRight
,
2312 mouseEv
.ControlDown(),
2313 mouseEv
.ShiftDown(),
2315 mouseEv
.MetaDown() );
2317 return GetEventHandler()->ProcessEvent(gridEvt
);
2321 wxGridEvent
gridEvt( GetId(),
2325 mouseEv
.GetX(), mouseEv
.GetY(),
2326 mouseEv
.ControlDown(),
2327 mouseEv
.ShiftDown(),
2329 mouseEv
.MetaDown() );
2331 return GetEventHandler()->ProcessEvent(gridEvt
);
2336 // Generate a grid event of specified type and return the result
2337 // of ProcessEvent().
2339 bool wxGrid::SendEvent( const wxEventType type
,
2342 if ( type
== EVT_GRID_ROW_SIZE
||
2343 type
== EVT_GRID_COL_SIZE
)
2345 int rowOrCol
= (row
== -1 ? col
: row
);
2347 wxGridSizeEvent
gridEvt( GetId(),
2352 return GetEventHandler()->ProcessEvent(gridEvt
);
2356 wxGridEvent
gridEvt( GetId(),
2361 return GetEventHandler()->ProcessEvent(gridEvt
);
2366 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2368 wxPaintDC
dc( this );
2370 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
2371 m_numRows
&& m_numCols
)
2373 m_currentCellCoords
.Set(0, 0);
2374 SetEditControlValue();
2375 ShowCellEditControl();
2380 // This is just here to make sure that CalcDimensions gets called when
2381 // the grid view is resized... then the size event is skipped to allow
2382 // the box sizers to handle everything
2384 void wxGrid::OnSize( wxSizeEvent
& event
)
2391 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
2393 if ( m_inOnKeyDown
)
2395 // shouldn't be here - we are going round in circles...
2397 wxLogFatalError( wxT("wxGrid::OnKeyDown called while alread active") );
2400 m_inOnKeyDown
= TRUE
;
2402 // propagate the event up and see if it gets processed
2404 wxWindow
*parent
= GetParent();
2405 wxKeyEvent
keyEvt( event
);
2406 keyEvt
.SetEventObject( parent
);
2408 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
2410 // try local handlers
2412 switch ( event
.KeyCode() )
2415 if ( event
.ControlDown() )
2417 MoveCursorUpBlock();
2426 if ( event
.ControlDown() )
2428 MoveCursorDownBlock();
2437 if ( event
.ControlDown() )
2439 MoveCursorLeftBlock();
2448 if ( event
.ControlDown() )
2450 MoveCursorRightBlock();
2459 if ( !IsEditable() )
2470 if ( event
.ControlDown() )
2472 event
.Skip(); // to let the edit control have the return
2481 if ( event
.ControlDown() )
2483 MakeCellVisible( 0, 0 );
2484 SetCurrentCell( 0, 0 );
2493 if ( event
.ControlDown() )
2495 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
2496 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
2513 // now try the cell edit control
2515 if ( IsCellEditControlEnabled() )
2517 event
.SetEventObject( m_cellEditCtrl
);
2518 m_cellEditCtrl
->GetEventHandler()->ProcessEvent( event
);
2524 m_inOnKeyDown
= FALSE
;
2528 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
2530 if ( SendEvent( EVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
2532 // the event has been intercepted - do nothing
2536 wxClientDC
dc( m_gridWin
);
2539 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
2541 HideCellEditControl();
2542 SaveEditControlValue();
2545 m_currentCellCoords
= coords
;
2547 SetEditControlValue();
2548 ShowCellEditControl();
2550 if ( IsSelection() )
2552 wxRect
r( SelectionToDeviceRect() );
2554 if ( !GetBatchCount() ) m_gridWin
->Refresh( TRUE
, &r
);
2560 // ------ functions to get/send data (see also public functions)
2563 bool wxGrid::GetModelValues()
2567 // all we need to do is repaint the grid
2569 m_gridWin
->Refresh();
2577 bool wxGrid::SetModelValues()
2583 for ( row
= 0; row
< m_numRows
; row
++ )
2585 for ( col
= 0; col
< m_numCols
; col
++ )
2587 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
2599 // Note - this function only draws cells that are in the list of
2600 // exposed cells (usually set from the update region by
2601 // CalcExposedCells)
2603 void wxGrid::DrawGridCellArea( wxDC
& dc
)
2605 if ( !m_numRows
|| !m_numCols
) return;
2608 size_t numCells
= m_cellsExposed
.GetCount();
2610 for ( i
= 0; i
< numCells
; i
++ )
2612 DrawCell( dc
, m_cellsExposed
[i
] );
2617 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
2619 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2620 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2623 if ( m_gridLinesEnabled
)
2624 DrawCellBorder( dc
, coords
);
2627 DrawCellBackground( dc
, coords
);
2629 // TODO: separate functions here for different kinds of cells ?
2632 DrawCellValue( dc
, coords
);
2636 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
2638 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2639 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2641 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2642 int row
= coords
.GetRow();
2643 int col
= coords
.GetCol();
2645 // right hand border
2647 dc
.DrawLine( m_colRights
[col
], m_rowBottoms
[row
] - m_rowHeights
[row
],
2648 m_colRights
[col
], m_rowBottoms
[row
] );
2652 dc
.DrawLine( m_colRights
[col
] - m_colWidths
[col
], m_rowBottoms
[row
],
2653 m_colRights
[col
], m_rowBottoms
[row
] );
2657 void wxGrid::DrawCellBackground( wxDC
& dc
, const wxGridCellCoords
& coords
)
2659 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2660 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2662 int row
= coords
.GetRow();
2663 int col
= coords
.GetCol();
2665 dc
.SetBackgroundMode( wxSOLID
);
2667 if ( IsInSelection( coords
) )
2669 // TODO: improve this
2671 dc
.SetBrush( *wxBLACK_BRUSH
);
2675 dc
.SetBrush( wxBrush(GetCellBackgroundColour(row
, col
), wxSOLID
) );
2678 dc
.SetPen( *wxTRANSPARENT_PEN
);
2680 dc
.DrawRectangle( m_colRights
[col
] - m_colWidths
[col
] + 1,
2681 m_rowBottoms
[row
] - m_rowHeights
[row
] + 1,
2683 m_rowHeights
[row
]-1 );
2687 void wxGrid::DrawCellValue( wxDC
& dc
, const wxGridCellCoords
& coords
)
2689 if ( m_colWidths
[coords
.GetCol()] <=0 ||
2690 m_rowHeights
[coords
.GetRow()] <= 0 ) return;
2692 int row
= coords
.GetRow();
2693 int col
= coords
.GetCol();
2695 dc
.SetBackgroundMode( wxTRANSPARENT
);
2697 if ( IsInSelection( row
, col
) )
2699 // TODO: improve this
2701 dc
.SetTextBackground( wxColour(0, 0, 0) );
2702 dc
.SetTextForeground( wxColour(255, 255, 255) );
2706 dc
.SetTextBackground( GetCellBackgroundColour(row
, col
) );
2707 dc
.SetTextForeground( GetCellTextColour(row
, col
) );
2709 dc
.SetFont( GetCellFont(row
, col
) );
2712 GetCellAlignment( row
, col
, &hAlign
, &vAlign
);
2715 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2716 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2717 rect
.SetWidth( m_colWidths
[col
] - 4 );
2718 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2720 DrawTextRectangle( dc
, GetCellValue( row
, col
), rect
, hAlign
, vAlign
);
2725 // TODO: remove this ???
2726 // This is used to redraw all grid lines e.g. when the grid line colour
2729 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
2731 if ( !m_gridLinesEnabled
||
2733 !m_numCols
) return;
2735 int top
, bottom
, left
, right
;
2739 m_gridWin
->GetClientSize(&cw
, &ch
);
2741 // virtual coords of visible area
2743 CalcUnscrolledPosition( 0, 0, &left
, &top
);
2744 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
2748 reg
.GetBox(x
, y
, w
, h
);
2749 CalcUnscrolledPosition( x
, y
, &left
, &top
);
2750 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
2753 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
2755 // horizontal grid lines
2758 for ( i
= 0; i
<= m_numRows
; i
++ )
2760 if ( m_rowBottoms
[i
] > bottom
)
2764 else if ( m_rowBottoms
[i
] >= top
)
2766 dc
.DrawLine( left
, m_rowBottoms
[i
], right
, m_rowBottoms
[i
] );
2771 // vertical grid lines
2773 for ( i
= 0; i
<= m_numCols
; i
++ )
2775 if ( m_colRights
[i
] > right
)
2779 else if ( m_colRights
[i
] >= left
)
2781 dc
.DrawLine( m_colRights
[i
], top
, m_colRights
[i
], bottom
);
2787 void wxGrid::DrawRowLabels( wxDC
& dc
)
2789 if ( !m_numRows
|| !m_numCols
) return;
2792 size_t numLabels
= m_rowLabelsExposed
.GetCount();
2794 for ( i
= 0; i
< numLabels
; i
++ )
2796 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
2801 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
2803 if ( m_rowHeights
[row
] <= 0 ) return;
2805 // draw the label's horizontal border (the vertical border is
2806 // provided by the cell area window margin)
2808 dc
.SetPen( *wxBLACK_PEN
);
2810 dc
.DrawLine( 0, m_rowBottoms
[row
]+1,
2811 m_rowLabelWidth
, m_rowBottoms
[row
]+1 );
2813 dc
.SetPen( *wxWHITE_PEN
);
2815 dc
.DrawLine( 0, m_rowBottoms
[row
]+2,
2816 m_rowLabelWidth
, m_rowBottoms
[row
]+2 );
2818 dc
.SetBackgroundMode( wxTRANSPARENT
);
2819 dc
.SetTextForeground( GetLabelTextColour() );
2820 dc
.SetFont( GetLabelFont() );
2823 GetRowLabelAlignment( &hAlign
, &vAlign
);
2827 rect
.SetY( m_rowBottoms
[row
] - m_rowHeights
[row
] + 2 );
2828 rect
.SetWidth( m_rowLabelWidth
- 4 );
2829 rect
.SetHeight( m_rowHeights
[row
] - 4 );
2830 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
2834 void wxGrid::DrawColLabels( wxDC
& dc
)
2836 if ( !m_numRows
|| !m_numCols
) return;
2839 size_t numLabels
= m_colLabelsExposed
.GetCount();
2841 for ( i
= 0; i
< numLabels
; i
++ )
2843 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
2848 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
2850 if ( m_colWidths
[col
] <= 0 ) return;
2852 // draw the label's vertical border (the horizontal border is
2853 // provided by the cell area window margin)
2855 dc
.SetPen( *wxBLACK_PEN
);
2857 dc
.DrawLine( m_colRights
[col
]+1, 0,
2858 m_colRights
[col
]+1, m_colLabelHeight
);
2860 dc
.SetPen( *wxWHITE_PEN
);
2862 dc
.DrawLine( m_colRights
[col
]+2, 0,
2863 m_colRights
[col
]+2, m_colLabelHeight
);
2865 dc
.SetBackgroundMode( wxTRANSPARENT
);
2866 dc
.SetTextForeground( GetLabelTextColour() );
2867 dc
.SetFont( GetLabelFont() );
2870 GetColLabelAlignment( &hAlign
, &vAlign
);
2873 rect
.SetX( m_colRights
[col
] - m_colWidths
[col
] + 2 );
2875 rect
.SetWidth( m_colWidths
[col
] - 4 );
2876 rect
.SetHeight( m_colLabelHeight
- 4 );
2877 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
2881 void wxGrid::DrawTextRectangle( wxDC
& dc
,
2882 const wxString
& value
,
2887 long textWidth
, textHeight
;
2888 long lineWidth
, lineHeight
;
2889 wxArrayString lines
;
2891 dc
.SetClippingRegion( rect
);
2892 StringToLines( value
, lines
);
2893 if ( lines
.GetCount() )
2895 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
2896 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
2899 switch ( horizAlign
)
2902 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
2906 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
2915 switch ( vertAlign
)
2918 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
2922 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
2931 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
2933 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
2938 dc
.DestroyClippingRegion();
2942 // Split multi line text up into an array of strings. Any existing
2943 // contents of the string array are preserved.
2945 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
2947 // TODO: this won't work for WXMAC ? (lines end with '\r')
2948 // => use wxTextFile functions then (VZ)
2951 while ( startPos
< (int)value
.Length() )
2953 pos
= value
.Mid(startPos
).Find( '\n' );
2958 else if ( pos
== 0 )
2960 lines
.Add( wxEmptyString
);
2964 if ( value
[startPos
+pos
-1] == '\r' )
2966 lines
.Add( value
.Mid(startPos
, pos
-1) );
2970 lines
.Add( value
.Mid(startPos
, pos
) );
2975 if ( startPos
< (int)value
.Length() )
2977 lines
.Add( value
.Mid( startPos
) );
2982 void wxGrid::GetTextBoxSize( wxDC
& dc
,
2983 wxArrayString
& lines
,
2984 long *width
, long *height
)
2991 for ( i
= 0; i
< lines
.GetCount(); i
++ )
2993 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
2994 w
= wxMax( w
, lineW
);
3004 // ------ Edit control functions
3008 void wxGrid::EnableEditing( bool edit
)
3010 // TODO: improve this ?
3012 if ( edit
!= m_editable
)
3016 // TODO: extend this for other edit control types
3018 if ( m_editCtrlType
== wxGRID_TEXTCTRL
)
3020 ((wxTextCtrl
*)m_cellEditCtrl
)->SetEditable( m_editable
);
3026 #if 0 // disabled for the moment - the cell control is always active
3027 void wxGrid::EnableCellEditControl( bool enable
)
3029 if ( m_cellEditCtrl
&&
3030 enable
!= m_cellEditCtrlEnabled
)
3032 m_cellEditCtrlEnabled
= enable
;
3034 if ( m_cellEditCtrlEnabled
)
3036 SetEditControlValue();
3037 ShowCellEditControl();
3041 HideCellEditControl();
3042 SaveEditControlValue();
3049 void wxGrid::ShowCellEditControl()
3053 if ( IsCellEditControlEnabled() )
3055 if ( !IsVisible( m_currentCellCoords
) )
3061 rect
= CellToRect( m_currentCellCoords
);
3063 // convert to scrolled coords
3065 int left
, top
, right
, bottom
;
3066 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
3067 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
3070 m_gridWin
->GetClientSize( &cw
, &ch
);
3072 // Make the edit control large enough to allow for internal margins
3073 // TODO: remove this if the text ctrl sizing is improved esp. for unix
3076 #if defined(__WXMOTIF__)
3077 if ( m_currentCellCoords
.GetRow() == 0 ||
3078 m_currentCellCoords
.GetCol() == 0 )
3087 if ( m_currentCellCoords
.GetRow() == 0 ||
3088 m_currentCellCoords
.GetCol() == 0 )
3098 #if defined(__WXGTK__)
3101 if (left
!= 0) left_diff
++;
3102 if (top
!= 0) top_diff
++;
3103 rect
.SetLeft( left
+ left_diff
);
3104 rect
.SetTop( top
+ top_diff
);
3105 rect
.SetRight( rect
.GetRight() - left_diff
);
3106 rect
.SetBottom( rect
.GetBottom() - top_diff
);
3108 rect
.SetLeft( wxMax(0, left
- extra
) );
3109 rect
.SetTop( wxMax(0, top
- extra
) );
3110 rect
.SetRight( rect
.GetRight() + 2*extra
);
3111 rect
.SetBottom( rect
.GetBottom() + 2*extra
);
3114 m_cellEditCtrl
->SetSize( rect
);
3115 m_cellEditCtrl
->Show( TRUE
);
3117 switch ( m_editCtrlType
)
3119 case wxGRID_TEXTCTRL
:
3120 ((wxTextCtrl
*) m_cellEditCtrl
)->SetInsertionPointEnd();
3123 case wxGRID_CHECKBOX
:
3124 // TODO: anything ???
3129 // TODO: anything ???
3133 case wxGRID_COMBOBOX
:
3134 // TODO: anything ???
3139 m_cellEditCtrl
->SetFocus();
3145 void wxGrid::HideCellEditControl()
3147 if ( IsCellEditControlEnabled() )
3149 m_cellEditCtrl
->Show( FALSE
);
3154 void wxGrid::SetEditControlValue( const wxString
& value
)
3160 s
= GetCellValue(m_currentCellCoords
);
3164 if ( IsCellEditControlEnabled() )
3166 switch ( m_editCtrlType
)
3168 case wxGRID_TEXTCTRL
:
3169 ((wxGridTextCtrl
*)m_cellEditCtrl
)->SetStartValue(s
);
3172 case wxGRID_CHECKBOX
:
3173 // TODO: implement this
3178 // TODO: implement this
3182 case wxGRID_COMBOBOX
:
3183 // TODO: implement this
3192 void wxGrid::SaveEditControlValue()
3196 wxWindow
*ctrl
= (wxWindow
*)NULL
;
3198 if ( IsCellEditControlEnabled() )
3200 ctrl
= m_cellEditCtrl
;
3207 bool valueChanged
= FALSE
;
3209 switch ( m_editCtrlType
)
3211 case wxGRID_TEXTCTRL
:
3212 valueChanged
= (((wxGridTextCtrl
*)ctrl
)->GetValue() !=
3213 ((wxGridTextCtrl
*)ctrl
)->GetStartValue());
3214 SetCellValue( m_currentCellCoords
,
3215 ((wxTextCtrl
*) ctrl
)->GetValue() );
3218 case wxGRID_CHECKBOX
:
3219 // TODO: implement this
3224 // TODO: implement this
3228 case wxGRID_COMBOBOX
:
3229 // TODO: implement this
3236 SendEvent( EVT_GRID_CELL_CHANGE
,
3237 m_currentCellCoords
.GetRow(),
3238 m_currentCellCoords
.GetCol() );
3245 // ------ Grid location functions
3246 // Note that all of these functions work with the logical coordinates of
3247 // grid cells and labels so you will need to convert from device
3248 // coordinates for mouse events etc.
3251 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
3253 int row
= YToRow(y
);
3254 int col
= XToCol(x
);
3256 if ( row
== -1 || col
== -1 )
3258 coords
= wxGridNoCellCoords
;
3262 coords
.Set( row
, col
);
3267 int wxGrid::YToRow( int y
)
3271 for ( i
= 0; i
< m_numRows
; i
++ )
3273 if ( y
< m_rowBottoms
[i
] ) return i
;
3280 int wxGrid::XToCol( int x
)
3284 for ( i
= 0; i
< m_numCols
; i
++ )
3286 if ( x
< m_colRights
[i
] ) return i
;
3293 // return the row number that that the y coord is near the edge of, or
3294 // -1 if not near an edge
3296 int wxGrid::YToEdgeOfRow( int y
)
3300 for ( i
= 0; i
< m_numRows
; i
++ )
3302 if ( m_rowHeights
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3304 d
= abs( y
- m_rowBottoms
[i
] );
3306 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3315 // return the col number that that the x coord is near the edge of, or
3316 // -1 if not near an edge
3318 int wxGrid::XToEdgeOfCol( int x
)
3322 for ( i
= 0; i
< m_numCols
; i
++ )
3324 if ( m_colWidths
[i
] > WXGRID_LABEL_EDGE_ZONE
)
3326 d
= abs( x
- m_colRights
[i
] );
3328 if ( d
< WXGRID_LABEL_EDGE_ZONE
) return i
;
3337 wxRect
wxGrid::CellToRect( int row
, int col
)
3339 wxRect
rect( -1, -1, -1, -1 );
3341 if ( row
>= 0 && row
< m_numRows
&&
3342 col
>= 0 && col
< m_numCols
)
3344 rect
.x
= m_colRights
[col
] - m_colWidths
[col
];
3345 rect
.y
= m_rowBottoms
[row
] - m_rowHeights
[row
];
3346 rect
.width
= m_colWidths
[col
];
3347 rect
.height
= m_rowHeights
[ row
];
3354 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
3356 // get the cell rectangle in logical coords
3358 wxRect
r( CellToRect( row
, col
) );
3360 // convert to device coords
3362 int left
, top
, right
, bottom
;
3363 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3364 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3366 // check against the client area of the grid window
3369 m_gridWin
->GetClientSize( &cw
, &ch
);
3371 if ( wholeCellVisible
)
3373 // is the cell wholly visible ?
3375 return ( left
>= 0 && right
<= cw
&&
3376 top
>= 0 && bottom
<= ch
);
3380 // is the cell partly visible ?
3382 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
3383 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
3388 // make the specified cell location visible by doing a minimal amount
3391 void wxGrid::MakeCellVisible( int row
, int col
)
3394 int xpos
= -1, ypos
= -1;
3396 if ( row
>= 0 && row
< m_numRows
&&
3397 col
>= 0 && col
< m_numCols
)
3399 // get the cell rectangle in logical coords
3401 wxRect
r( CellToRect( row
, col
) );
3403 // convert to device coords
3405 int left
, top
, right
, bottom
;
3406 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3407 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3410 m_gridWin
->GetClientSize( &cw
, &ch
);
3416 else if ( bottom
> ch
)
3418 int h
= r
.GetHeight();
3420 for ( i
= row
-1; i
>= 0; i
-- )
3422 if ( h
+ m_rowHeights
[i
] > ch
) break;
3424 h
+= m_rowHeights
[i
];
3425 ypos
-= m_rowHeights
[i
];
3428 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
3429 // have rounding errors (this is important, because if we do, we
3430 // might not scroll at all and some cells won't be redrawn)
3431 ypos
+= GRID_SCROLL_LINE
/ 2;
3438 else if ( right
> cw
)
3440 int w
= r
.GetWidth();
3442 for ( i
= col
-1; i
>= 0; i
-- )
3444 if ( w
+ m_colWidths
[i
] > cw
) break;
3446 w
+= m_colWidths
[i
];
3447 xpos
-= m_colWidths
[i
];
3450 // see comment for ypos above
3451 xpos
+= GRID_SCROLL_LINE
/ 2;
3454 if ( xpos
!= -1 || ypos
!= -1 )
3456 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
3457 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
3458 Scroll( xpos
, ypos
);
3466 // ------ Grid cursor movement functions
3469 bool wxGrid::MoveCursorUp()
3471 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3472 m_currentCellCoords
.GetRow() > 0 )
3474 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
3475 m_currentCellCoords
.GetCol() );
3477 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
3478 m_currentCellCoords
.GetCol() );
3487 bool wxGrid::MoveCursorDown()
3489 // TODO: allow for scrolling
3491 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3492 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3494 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
3495 m_currentCellCoords
.GetCol() );
3497 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
3498 m_currentCellCoords
.GetCol() );
3507 bool wxGrid::MoveCursorLeft()
3509 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3510 m_currentCellCoords
.GetCol() > 0 )
3512 MakeCellVisible( m_currentCellCoords
.GetRow(),
3513 m_currentCellCoords
.GetCol() - 1 );
3515 SetCurrentCell( m_currentCellCoords
.GetRow(),
3516 m_currentCellCoords
.GetCol() - 1 );
3525 bool wxGrid::MoveCursorRight()
3527 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
3528 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
3530 MakeCellVisible( m_currentCellCoords
.GetRow(),
3531 m_currentCellCoords
.GetCol() + 1 );
3533 SetCurrentCell( m_currentCellCoords
.GetRow(),
3534 m_currentCellCoords
.GetCol() + 1 );
3543 bool wxGrid::MovePageUp()
3545 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3547 int row
= m_currentCellCoords
.GetRow();
3551 m_gridWin
->GetClientSize( &cw
, &ch
);
3553 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3554 int newRow
= YToRow( y
- ch
+ 1 );
3559 else if ( newRow
== row
)
3564 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3565 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3573 bool wxGrid::MovePageDown()
3575 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
3577 int row
= m_currentCellCoords
.GetRow();
3578 if ( row
< m_numRows
)
3581 m_gridWin
->GetClientSize( &cw
, &ch
);
3583 int y
= m_rowBottoms
[ row
] - m_rowHeights
[ row
];
3584 int newRow
= YToRow( y
+ ch
);
3587 newRow
= m_numRows
- 1;
3589 else if ( newRow
== row
)
3594 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
3595 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
3603 bool wxGrid::MoveCursorUpBlock()
3606 m_currentCellCoords
!= wxGridNoCellCoords
&&
3607 m_currentCellCoords
.GetRow() > 0 )
3609 int row
= m_currentCellCoords
.GetRow();
3610 int col
= m_currentCellCoords
.GetCol();
3612 if ( m_table
->IsEmptyCell(row
, col
) )
3614 // starting in an empty cell: find the next block of
3620 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3623 else if ( m_table
->IsEmptyCell(row
-1, col
) )
3625 // starting at the top of a block: find the next block
3631 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3636 // starting within a block: find the top of the block
3641 if ( m_table
->IsEmptyCell(row
, col
) )
3649 MakeCellVisible( row
, col
);
3650 SetCurrentCell( row
, col
);
3658 bool wxGrid::MoveCursorDownBlock()
3661 m_currentCellCoords
!= wxGridNoCellCoords
&&
3662 m_currentCellCoords
.GetRow() < m_numRows
-1 )
3664 int row
= m_currentCellCoords
.GetRow();
3665 int col
= m_currentCellCoords
.GetCol();
3667 if ( m_table
->IsEmptyCell(row
, col
) )
3669 // starting in an empty cell: find the next block of
3672 while ( row
< m_numRows
-1 )
3675 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3678 else if ( m_table
->IsEmptyCell(row
+1, col
) )
3680 // starting at the bottom of a block: find the next block
3683 while ( row
< m_numRows
-1 )
3686 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3691 // starting within a block: find the bottom of the block
3693 while ( row
< m_numRows
-1 )
3696 if ( m_table
->IsEmptyCell(row
, col
) )
3704 MakeCellVisible( row
, col
);
3705 SetCurrentCell( row
, col
);
3713 bool wxGrid::MoveCursorLeftBlock()
3716 m_currentCellCoords
!= wxGridNoCellCoords
&&
3717 m_currentCellCoords
.GetCol() > 0 )
3719 int row
= m_currentCellCoords
.GetRow();
3720 int col
= m_currentCellCoords
.GetCol();
3722 if ( m_table
->IsEmptyCell(row
, col
) )
3724 // starting in an empty cell: find the next block of
3730 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3733 else if ( m_table
->IsEmptyCell(row
, col
-1) )
3735 // starting at the left of a block: find the next block
3741 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3746 // starting within a block: find the left of the block
3751 if ( m_table
->IsEmptyCell(row
, col
) )
3759 MakeCellVisible( row
, col
);
3760 SetCurrentCell( row
, col
);
3768 bool wxGrid::MoveCursorRightBlock()
3771 m_currentCellCoords
!= wxGridNoCellCoords
&&
3772 m_currentCellCoords
.GetCol() < m_numCols
-1 )
3774 int row
= m_currentCellCoords
.GetRow();
3775 int col
= m_currentCellCoords
.GetCol();
3777 if ( m_table
->IsEmptyCell(row
, col
) )
3779 // starting in an empty cell: find the next block of
3782 while ( col
< m_numCols
-1 )
3785 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3788 else if ( m_table
->IsEmptyCell(row
, col
+1) )
3790 // starting at the right of a block: find the next block
3793 while ( col
< m_numCols
-1 )
3796 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
3801 // starting within a block: find the right of the block
3803 while ( col
< m_numCols
-1 )
3806 if ( m_table
->IsEmptyCell(row
, col
) )
3814 MakeCellVisible( row
, col
);
3815 SetCurrentCell( row
, col
);
3826 // ------ Label values and formatting
3829 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
3831 *horiz
= m_rowLabelHorizAlign
;
3832 *vert
= m_rowLabelVertAlign
;
3835 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
3837 *horiz
= m_colLabelHorizAlign
;
3838 *vert
= m_colLabelVertAlign
;
3841 wxString
wxGrid::GetRowLabelValue( int row
)
3845 return m_table
->GetRowLabelValue( row
);
3855 wxString
wxGrid::GetColLabelValue( int col
)
3859 return m_table
->GetColLabelValue( col
);
3869 void wxGrid::SetRowLabelSize( int width
)
3871 // TODO: how to do this with the box sizers ?
3874 void wxGrid::SetColLabelSize( int height
)
3876 // TODO: how to do this with the box sizers ?
3879 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
3881 if ( m_labelBackgroundColour
!= colour
)
3883 m_labelBackgroundColour
= colour
;
3884 m_rowLabelWin
->SetBackgroundColour( colour
);
3885 m_colLabelWin
->SetBackgroundColour( colour
);
3886 m_cornerLabelWin
->SetBackgroundColour( colour
);
3888 if ( !GetBatchCount() )
3890 m_rowLabelWin
->Refresh();
3891 m_colLabelWin
->Refresh();
3892 m_cornerLabelWin
->Refresh();
3897 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
3899 if ( m_labelTextColour
!= colour
)
3901 m_labelTextColour
= colour
;
3902 if ( !GetBatchCount() )
3904 m_rowLabelWin
->Refresh();
3905 m_colLabelWin
->Refresh();
3910 void wxGrid::SetLabelFont( const wxFont
& font
)
3913 if ( !GetBatchCount() )
3915 m_rowLabelWin
->Refresh();
3916 m_colLabelWin
->Refresh();
3920 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
3922 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3924 m_rowLabelHorizAlign
= horiz
;
3927 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3929 m_rowLabelVertAlign
= vert
;
3932 if ( !GetBatchCount() )
3934 m_rowLabelWin
->Refresh();
3938 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
3940 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
3942 m_colLabelHorizAlign
= horiz
;
3945 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
3947 m_colLabelVertAlign
= vert
;
3950 if ( !GetBatchCount() )
3952 m_colLabelWin
->Refresh();
3956 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
3960 m_table
->SetRowLabelValue( row
, s
);
3961 if ( !GetBatchCount() )
3963 wxRect rect
= CellToRect( row
, 0);
3964 if ( rect
.height
> 0 )
3967 rect
.width
= m_rowLabelWidth
;
3968 m_rowLabelWin
->Refresh( TRUE
, &rect
);
3974 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
3978 m_table
->SetColLabelValue( col
, s
);
3979 if ( !GetBatchCount() )
3981 wxRect rect
= CellToRect( 0, col
);
3982 if ( rect
.width
> 0 )
3985 rect
.height
= m_colLabelHeight
;
3986 m_colLabelWin
->Refresh( TRUE
, &rect
);
3992 void wxGrid::SetGridLineColour( const wxColour
& colour
)
3994 if ( m_gridLineColour
!= colour
)
3996 m_gridLineColour
= colour
;
3998 wxClientDC
dc( m_gridWin
);
4000 DrawAllGridLines( dc
);
4004 void wxGrid::EnableGridLines( bool enable
)
4006 if ( enable
!= m_gridLinesEnabled
)
4008 m_gridLinesEnabled
= enable
;
4010 if ( !GetBatchCount() )
4014 wxClientDC
dc( m_gridWin
);
4016 DrawAllGridLines( dc
);
4020 m_gridWin
->Refresh();
4027 int wxGrid::GetDefaultRowSize()
4029 return m_defaultRowHeight
;
4032 int wxGrid::GetRowSize( int row
)
4034 if ( row
>= 0 && row
< m_numRows
)
4035 return m_rowHeights
[row
];
4037 return 0; // TODO: log an error here
4040 int wxGrid::GetDefaultColSize()
4042 return m_defaultColWidth
;
4045 int wxGrid::GetColSize( int col
)
4047 if ( col
>= 0 && col
< m_numCols
)
4048 return m_colWidths
[col
];
4050 return 0; // TODO: log an error here
4053 wxColour
wxGrid::GetDefaultCellBackgroundColour()
4055 // TODO: replace this temp test code
4057 return wxColour( 255, 255, 255 );
4060 wxColour
wxGrid::GetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4062 // TODO: replace this temp test code
4064 return wxColour( 255, 255, 255 );
4067 wxColour
wxGrid::GetDefaultCellTextColour()
4069 // TODO: replace this temp test code
4071 return wxColour( 0, 0, 0 );
4074 wxColour
wxGrid::GetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
) )
4076 // TODO: replace this temp test code
4078 return wxColour( 0, 0, 0 );
4082 wxFont
wxGrid::GetDefaultCellFont()
4084 return m_defaultCellFont
;
4087 wxFont
wxGrid::GetCellFont( int WXUNUSED(row
), int WXUNUSED(col
) )
4089 // TODO: replace this temp test code
4091 return m_defaultCellFont
;
4094 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
4096 // TODO: replace this temp test code
4102 void wxGrid::GetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int *horiz
, int *vert
)
4104 // TODO: replace this temp test code
4110 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
4112 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
4114 if ( resizeExistingRows
)
4118 for ( row
= 0; row
< m_numRows
; row
++ )
4120 m_rowHeights
[row
] = m_defaultRowHeight
;
4121 bottom
+= m_defaultRowHeight
;
4122 m_rowBottoms
[row
] = bottom
;
4128 void wxGrid::SetRowSize( int row
, int height
)
4132 if ( row
>= 0 && row
< m_numRows
)
4134 int h
= wxMax( 0, height
);
4135 int diff
= h
- m_rowHeights
[row
];
4137 m_rowHeights
[row
] = h
;
4138 for ( i
= row
; i
< m_numRows
; i
++ )
4140 m_rowBottoms
[i
] += diff
;
4144 // Note: we are ending the event *after* doing
4145 // default processing in this case
4147 SendEvent( EVT_GRID_ROW_SIZE
,
4152 // TODO: log an error here
4156 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
4158 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
4160 if ( resizeExistingCols
)
4164 for ( col
= 0; col
< m_numCols
; col
++ )
4166 m_colWidths
[col
] = m_defaultColWidth
;
4167 right
+= m_defaultColWidth
;
4168 m_colRights
[col
] = right
;
4174 void wxGrid::SetColSize( int col
, int width
)
4178 if ( col
>= 0 && col
< m_numCols
)
4180 int w
= wxMax( 0, width
);
4181 int diff
= w
- m_colWidths
[col
];
4182 m_colWidths
[col
] = w
;
4184 for ( i
= col
; i
< m_numCols
; i
++ )
4186 m_colRights
[i
] += diff
;
4190 // Note: we are ending the event *after* doing
4191 // default processing in this case
4193 SendEvent( EVT_GRID_COL_SIZE
,
4198 // TODO: log an error here
4202 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& )
4204 // TODO: everything !!!
4208 void wxGrid::SetCellBackgroundColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4210 // TODO: everything !!!
4214 void wxGrid::SetDefaultCellTextColour( const wxColour
& )
4216 // TODO: everything !!!
4220 void wxGrid::SetCellTextColour( int WXUNUSED(row
), int WXUNUSED(col
), const wxColour
& )
4222 // TODO: everything !!!
4226 void wxGrid::SetDefaultCellFont( const wxFont
& )
4228 // TODO: everything !!!
4232 void wxGrid::SetCellFont( int WXUNUSED(row
), int WXUNUSED(col
), const wxFont
& )
4234 // TODO: everything !!!
4238 void wxGrid::SetDefaultCellAlignment( int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4240 // TODO: everything !!!
4244 void wxGrid::SetCellAlignment( int WXUNUSED(row
), int WXUNUSED(col
), int WXUNUSED(horiz
), int WXUNUSED(vert
) )
4246 // TODO: everything !!!
4253 // ------ cell value accessor functions
4256 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
4260 m_table
->SetValue( row
, col
, s
.c_str() );
4261 if ( !GetBatchCount() )
4263 wxClientDC
dc( m_gridWin
);
4265 DrawCell( dc
, wxGridCellCoords(row
, col
) );
4268 #if 0 // TODO: edit in place
4270 if ( m_currentCellCoords
.GetRow() == row
&&
4271 m_currentCellCoords
.GetCol() == col
)
4273 SetEditControlValue( s
);
4282 // ------ Block, row and col selection
4285 void wxGrid::SelectRow( int row
, bool addToSelected
)
4289 if ( IsSelection() && addToSelected
)
4292 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4295 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4296 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4297 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4298 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4302 need_refresh
[0] = TRUE
;
4303 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
4304 wxGridCellCoords ( oldTop
- 1,
4306 m_selectedTopLeft
.SetRow( row
);
4311 need_refresh
[1] = TRUE
;
4312 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
4313 wxGridCellCoords ( oldBottom
,
4316 m_selectedTopLeft
.SetCol( 0 );
4319 if ( oldBottom
< row
)
4321 need_refresh
[2] = TRUE
;
4322 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
4323 wxGridCellCoords ( row
,
4325 m_selectedBottomRight
.SetRow( row
);
4328 if ( oldRight
< m_numCols
- 1 )
4330 need_refresh
[3] = TRUE
;
4331 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4333 wxGridCellCoords ( oldBottom
,
4335 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
4338 for (i
= 0; i
< 4; i
++ )
4339 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4340 m_gridWin
->Refresh( TRUE
, &(rect
[i
]) );
4344 r
= SelectionToDeviceRect();
4346 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4348 m_selectedTopLeft
.Set( row
, 0 );
4349 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
4350 r
= SelectionToDeviceRect();
4351 m_gridWin
->Refresh( TRUE
, &r
);
4354 wxGridRangeSelectEvent
gridEvt( GetId(),
4355 EVT_GRID_RANGE_SELECT
,
4358 m_selectedBottomRight
);
4360 GetEventHandler()->ProcessEvent(gridEvt
);
4364 void wxGrid::SelectCol( int col
, bool addToSelected
)
4366 if ( IsSelection() && addToSelected
)
4369 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4372 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4373 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4374 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4375 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4377 if ( oldLeft
> col
)
4379 need_refresh
[0] = TRUE
;
4380 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
4381 wxGridCellCoords ( m_numRows
- 1,
4383 m_selectedTopLeft
.SetCol( col
);
4388 need_refresh
[1] = TRUE
;
4389 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
4390 wxGridCellCoords ( oldTop
- 1,
4392 m_selectedTopLeft
.SetRow( 0 );
4395 if ( oldRight
< col
)
4397 need_refresh
[2] = TRUE
;
4398 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
4399 wxGridCellCoords ( m_numRows
- 1,
4401 m_selectedBottomRight
.SetCol( col
);
4404 if ( oldBottom
< m_numRows
- 1 )
4406 need_refresh
[3] = TRUE
;
4407 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
4409 wxGridCellCoords ( m_numRows
- 1,
4411 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
4414 for (i
= 0; i
< 4; i
++ )
4415 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4416 m_gridWin
->Refresh( TRUE
, &(rect
[i
]) );
4422 r
= SelectionToDeviceRect();
4424 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( TRUE
, &r
);
4426 m_selectedTopLeft
.Set( 0, col
);
4427 m_selectedBottomRight
.Set( m_numRows
-1, col
);
4428 r
= SelectionToDeviceRect();
4429 m_gridWin
->Refresh( TRUE
, &r
);
4432 wxGridRangeSelectEvent
gridEvt( GetId(),
4433 EVT_GRID_RANGE_SELECT
,
4436 m_selectedBottomRight
);
4438 GetEventHandler()->ProcessEvent(gridEvt
);
4442 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
4445 wxGridCellCoords updateTopLeft
, updateBottomRight
;
4447 if ( topRow
> bottomRow
)
4454 if ( leftCol
> rightCol
)
4461 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
4462 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
4464 if ( m_selectedTopLeft
!= updateTopLeft
||
4465 m_selectedBottomRight
!= updateBottomRight
)
4467 // Compute two optimal update rectangles:
4468 // Either one rectangle is a real subset of the
4469 // other, or they are (almost) disjoint!
4471 bool need_refresh
[4] = { FALSE
, FALSE
, FALSE
, FALSE
};
4474 // Store intermediate values
4475 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
4476 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
4477 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
4478 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
4480 // Determine the outer/inner coordinates.
4481 if (oldLeft
> leftCol
)
4487 if (oldTop
> topRow
)
4493 if (oldRight
< rightCol
)
4496 oldRight
= rightCol
;
4499 if (oldBottom
< bottomRow
)
4502 oldBottom
= bottomRow
;
4506 // Now, either the stuff marked old is the outer
4507 // rectangle or we don't have a situation where one
4508 // is contained in the other.
4510 if ( oldLeft
< leftCol
)
4512 need_refresh
[0] = TRUE
;
4513 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4515 wxGridCellCoords ( oldBottom
,
4519 if ( oldTop
< topRow
)
4521 need_refresh
[1] = TRUE
;
4522 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4524 wxGridCellCoords ( topRow
- 1,
4528 if ( oldRight
> rightCol
)
4530 need_refresh
[2] = TRUE
;
4531 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
4533 wxGridCellCoords ( oldBottom
,
4537 if ( oldBottom
> bottomRow
)
4539 need_refresh
[3] = TRUE
;
4540 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
4542 wxGridCellCoords ( oldBottom
,
4548 m_selectedTopLeft
= updateTopLeft
;
4549 m_selectedBottomRight
= updateBottomRight
;
4551 // various Refresh() calls
4552 for (i
= 0; i
< 4; i
++ )
4553 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4554 m_gridWin
->Refresh( TRUE
, &(rect
[i
]) );
4557 // only generate an event if the block is not being selected by
4558 // dragging the mouse (in which case the event will be generated in
4559 // the mouse event handler)
4560 if ( !m_isDragging
)
4562 wxGridRangeSelectEvent
gridEvt( GetId(),
4563 EVT_GRID_RANGE_SELECT
,
4566 m_selectedBottomRight
);
4568 GetEventHandler()->ProcessEvent(gridEvt
);
4572 void wxGrid::SelectAll()
4574 m_selectedTopLeft
.Set( 0, 0 );
4575 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
4577 m_gridWin
->Refresh();
4581 void wxGrid::ClearSelection()
4583 m_selectedTopLeft
= wxGridNoCellCoords
;
4584 m_selectedBottomRight
= wxGridNoCellCoords
;
4588 // This function returns the rectangle that encloses the given block
4589 // in device coords clipped to the client size of the grid window.
4591 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
4592 const wxGridCellCoords
&bottomRight
)
4594 wxRect
rect( wxGridNoCellRect
);
4597 cellRect
= CellToRect( topLeft
);
4598 if ( cellRect
!= wxGridNoCellRect
)
4604 rect
= wxRect( 0, 0, 0, 0 );
4607 cellRect
= CellToRect( bottomRight
);
4608 if ( cellRect
!= wxGridNoCellRect
)
4614 return wxGridNoCellRect
;
4617 // convert to scrolled coords
4619 int left
, top
, right
, bottom
;
4620 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
4621 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
4624 m_gridWin
->GetClientSize( &cw
, &ch
);
4626 rect
.SetLeft( wxMax(0, left
) );
4627 rect
.SetTop( wxMax(0, top
) );
4628 rect
.SetRight( wxMin(cw
, right
) );
4629 rect
.SetBottom( wxMin(ch
, bottom
) );
4637 // ------ Grid event classes
4640 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
4642 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
4643 int row
, int col
, int x
, int y
,
4644 bool control
, bool shift
, bool alt
, bool meta
)
4645 : wxNotifyEvent( type
, id
)
4651 m_control
= control
;
4656 SetEventObject(obj
);
4660 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
4662 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
4663 int rowOrCol
, int x
, int y
,
4664 bool control
, bool shift
, bool alt
, bool meta
)
4665 : wxNotifyEvent( type
, id
)
4667 m_rowOrCol
= rowOrCol
;
4670 m_control
= control
;
4675 SetEventObject(obj
);
4679 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
4681 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
4682 const wxGridCellCoords
& topLeft
,
4683 const wxGridCellCoords
& bottomRight
,
4684 bool control
, bool shift
, bool alt
, bool meta
)
4685 : wxNotifyEvent( type
, id
)
4687 m_topLeft
= topLeft
;
4688 m_bottomRight
= bottomRight
;
4689 m_control
= control
;
4694 SetEventObject(obj
);
4698 #endif // ifndef wxUSE_NEW_GRID